Skip to main content

The end of the game

Overlay 256 · image 0x4f0f0–0x5146f (9,088 bytes) · 10 stub entries

All overlays · ← 255 · The introduction

In brief​

Overlay 256 runs the end of the game. When the final opponent dies — on level 16, sublevel 1 — kill a monster calls entry 4, which writes FINAL.SAV and releases most of the game's memory, then entry 1, 0x50416, which plays the whole ending; the program then returns to DOS. The seventeen other routines form a small sequence player, built like the introduction's but with its own code and tables: it loads scenes, runs animation lists, prints and fades texts, sets and fades palettes, waits on the clock or on the music, and scrolls the credits. The ending writes its credits text to CREDITS.TXT, draws the portraits of the party on its last picture and waits there for the Escape key. No code sets its skip flag, so it cannot be cut short. The behavior is described in the ending and summarized in how the game ends.

Addresses

Every address on this page is a file offset in START.EXE; calls into resident code are given as analysis addresses. See how addresses are written.

How it fits together​

What kill a monster calls, in order, when the final opponent dies, and the main helpers of the ending. Dashed boxes are outside this overlay.

Tables​

The overlay reads these tables. The data-segment ones are given as DS: offsets; the others are small resident blocks reached through fixed-up segments, given as analysis addresses.

LocationContents
DS:0x1b1eSkip flag (word): cleared and tested, never set.
DS:0x1b20Word set to 0 at the start and to 1 before the credits; never read.
DS:0x1b2221 far pointers to animation lists (analysis 0x36800–0x3739a).
DS:0x1b7613 far pointers, one per scene, to shape definitions; scenes 0, 3, 7, 9 and 10 have some.
DS:0x1baa13 far pointers to the scene picture names, DRAGON1.CPS … THANKS.CPS.
DS:0x1bde, DS:0x1be2, DS:0x6d78Target palette, rate and time of the next step of the delayed palette fade.
DS:0x1be4Six (x, y) positions of the portraits on the last picture.
0x36430–0x3674fThe 20 texts of the ending (file 0x29630–0x2994f).
0x36750Eight far pointers to the palettes FINALE_0.PAL … FINALE_7.PAL.
0x36770Far pointer to a copy of the last palette sent.
0x36780Far pointers to the cut shapes.
0x37470The credits text, 2,315 bytes and a terminating zero (file 0x2a670).

Animation lists and shape definitions have the same record layouts as the introduction's. The 235 animation records use commands 1, 2, 5 and 6 only, and none gives a palette.

Entry points​

Stub entries whose target is resolved, from the stub bytes or another independent anchor, or through the portal reading.

Encoded entryTargetRole
0x2b10:0x34, 0x2b10:0x25 (entries 4 and 1)0x4f0f0, 0x50416Write FINAL.SAV; the ending
0x2b10:0x20, 0x2b10:0x2a, 0x2b10:0x2f, 0x2b10:0x39, 0x2b10:0x3e, 0x2b10:0x43, 0x2b10:0x48, 0x2b10:0x4d (entries 0, 2, 3, 5, 6, 7, 8 and 9)0x502c7, 0x503a4, 0x500e0, 0x4fafb, 0x4f988, 0x50355, 0x4f99c, 0x50346Read from the stub bytes; no call through the stub or the portal 0x800 is known, they are called from inside the overlay

The stub declares 0x17e bytes of fixups, which follow the image at file offset 0x51470.

Routines at a glance​

RoutineAddressStub entry
Write FINAL.SAV0x4f0f04
Play the ending0x504161
Load a scene0x4f855—
Cut a scene's shapes0x4f99c8
Show the work page0x4f8ce—
Dissolve the work page onto the screen0x4f913—
Run an animation list0x4fbdb—
Print a text0x4ff49—
Fade the text out0x500ac—
Fade one colour to black0x500e03
Scroll the credits0x4f345—
Set the palette except colour 2550x4fafb5
Fade the palette0x50183—
One palette fade step0x502c70
Start a delayed palette fade0x503557
Advance the delayed palette fade0x503a42
Wait for a tick0x4f955—
Poll during a wait0x4f9886
Play a sound0x503469

Routines​

Write FINAL.SAV​

AddressRange
0x4f0f00x4f0f0–0x4f344

Entry 4 of the overlay. Calls the save writer with slot −1 (0x4f0f8–0x4f0fc), which writes FINAL.SAV, then passes the far pointers of most game buffers to 0x17ff2 (from 0x4f103). It is the only literal call to the writer outside the save menu. It then releases the door shapes, the monster shapes 0 to 35 and the decoration shapes (0x4f29a–0x4f2ae) and installs the font held at DS:0xf098. Last, for the first character whose portrait pointer still holds the address recorded in DS:0xd1c4 at start-up, it copies the 1,400-byte portrait to a newly allocated block and points the character at it (0x4f2c3–0x4f33d). That address lies 62,788 bytes into the page 7 buffer, into which the ending loads the 64,000-byte DESTROY2 (page 6, read as page 7); the copy keeps the portrait for the last picture. Its only caller is kill a monster (0x1abde), on level 16, sublevel 1, followed by the ending and the exit. No code of the executable reads FINAL.SAV: the name has a single reference, in the writer (0x49955).

Used by: Saving and restoring › What a saved game holds, Loading, failures and the end of the game › The end of the game, The game loop › How the game ends, The ending, Saved games (SAV), Level 16: reference tables › The final fight and the end of the game

Play the ending​

AddressRange
0x504160x50416–0x5146f

Entry 1, called only by kill a monster at 0x1abe3. It first writes the credits text (0x37470) to CREDITS.TXT, deleting any older file (file helpers, mode 2, 0x5041b–0x5045a); no code reads that file. It allocates a new 2,400-byte block for the scratch pointer DS:0xcbc4 (0x5045f–0x50472), replacing a pointer into the sound buffer, which is not a block to release; it loads the eight palettes FINALE_0.PAL to FINALE_7.PAL in every display mode, clears the screen through driver slot 14 (DS:0x655a), loads the sound file FINALE1 through load the sound file and takes FINALE_0 (mode 3) or FINALE_6 (other modes) as its working palette DS:0xf168.

It then plays a fixed sequence: 13 scene loads (each picture once), 20 texts in colours 10 and 15, 118 calls to animation lists, 5 waits on the music markers (sound driver 1 only, through 0x10251), 4 delayed palette fades in mode 3 (FINALE_3, FINALE_1, FINALE_4 at rate 1, FINALE_5 at rate 2), two dissolves, then FINALE2 and the credits (0x51221). In mode 3 it fades to black (0x512c5), loads HEROES.CPS, draws on it the portrait of every present character (eligibility test with CHARACTER_ELIGIBLE_PRESENT) at the positions of DS:0x1be4, with a frame from CREDITS2.CPS behind the fifth and sixth characters, copies the whole page to the screen and fades it in from black (0x513af; at once in the other modes). It then empties the input queue and reads keys until one translates to 0x1b, which the keyboard handler's key number 110, Escape, gives (0x513b5–0x513c2). Last it frees the shapes and palettes, clears the screen and returns.

Other details: the branches taken when the skip flag is set (0x50974, 0x511df) are never reached, see poll during a wait; after the credits the first three bytes of the credits text are overwritten with carriage returns (0x51243–0x5125d), and nothing reads them again.

Comparative source: REF-SCUMM-012, DarkMoonEngine::seq_playFinale.

Used by: The ending › The sequence, The ending › The credits, The ending › The last picture, The ending › Music and sounds, The ending › Display modes, The ending, Level 16: reference tables › The final fight and the end of the game

Load a scene​

AddressRange
0x4f8550x4f855–0x4f8cd

0x4f855(scene, page) loads picture scene of DS:0x1baa through the CPS image loader into page page + 1 (page | 1), with the working palette DS:0xf168 as the palette buffer (only HEROES.CPS carries a palette), cuts the scene's shapes (0x4f99c), in the modes other than 3 sets the working palette (0x3111d), and copies page page + 1 to page page through driver slot 10. It does nothing once the skip flag is set. As in the introduction's scene loader, the shapes are cut from the new picture before the copy, because the drivers' shape encoders read page page + 1 (VGA, EGA).

Used by: Play the ending, The ending › The sequence, Compressed containers (CPS)

Cut a scene's shapes​

AddressRange
0x4f99c0x4f99c–0x4fafa

0x4f99c(page, scene): for each shape definition of the scene, frees the shape already stored under that number and encodes the new one through driver slot 28 (negative number) or the shape encoder, slot 27 (positive number), into the shape table at 0x36780. Scenes 0 (DRAGON1), 3 (HURRY2), 7 (MAGIC), 9 (CREDITS2) and 10 (CREDITS3) have 3, 2, 3, 8 and 3 definitions; the numbers used are 0 to 6, 10, 11, 15 and 16.

Used by: Load a scene

Show the work page​

AddressRange
0x4f8ce0x4f8ce–0x4f912

0x4f8ce(page): in mode 3, sets the working palette except colour 255 (0x4fafb); then copies the 304 × 128 area at (0, 0) of page to (8, 8) on the screen. Used for DRAGON1, HURRY1 and DESTROY0.

Used by: Play the ending

Dissolve the work page onto the screen​

AddressRange
0x4f9130x4f913–0x4f954

0x4f913(page): in mode 3, sets the working palette except colour 255; then, in every mode, reveals the 304 × 128 area of page at (8, 8) on the screen through the pixel dissolve. Used for THANKS and DESTROY1.

Used by: Play the ending

Run an animation list​

AddressRange
0x4fbdb0x4fbdb–0x4ff48

Runs list n of DS:0x1b22 record by record, through a jump table at 0x4ff39, until the 0xff end mark. Each command's wait goes through wait for a tick; where a palette is given, mode 3 sets palette palette of 0x36750 first (no record gives one):

CommandHandlerEffect
00x4fc52Palette, then wait.
10x4fc7fDraw the shape on the screen clipped to text window 18 (the picture area), palette, wait, then copy its rectangle back from page 2.
20x4fe0aDraw the shape at (x, y) on the current page, palette, wait; the shape stays.
3, 40x4ff23Nothing.
50x4fe90Palette; copy a block of page 2 (shape byte 0) or page 6 (otherwise) to (x, y) on the screen; wait.
60x4fc47Play the sound given in the shape byte (0x50346).
70x4fd8aWait, then copy the shape's rectangle from 8 pixels up and left on page 2.

The 21 lists hold 235 records: command 5 187 times, 6 23 times, 2 19 times and 1 6 times, all in list 9. It does nothing once the skip flag is set.

Comparative source: REF-SCUMM-012, DarkmoonSequenceHelper::animCommand.

Used by: Play the ending, The ending, The ending

AddressRange
0x4ff490x4ff49–0x500ab

0x4ff49(colour, text) selects and clears text window 17. In mode 3 it copies palette entry colour of the working palette into entry 255, sends that entry and draws in colour 255; in the other modes it draws in colour 15 (text colour). The text is split at carriage returns; each line is centred in the window, 9 rows below the previous one, and drawn through driver slot 52.

Used by: Play the ending, The ending › The sequence, The ending › Display modes

Fade the text out​

AddressRange
0x500ac0x500ac–0x500df

In mode 3, fades colour 255 to black (0x500e0, step 2); then, in every mode, selects and clears text window 17.

Used by: Play the ending

Fade one colour to black​

AddressRange
0x500e00x500e0–0x50182

0x500e0(colour, step, palette), mode 3 only: lowers the three components of one palette entry by step, stopping at 0, waits for the vertical retrace and sends that entry, until it is black.

Used by: Fade the text out

Scroll the credits​

AddressRange
0x4f3450x4f345–0x4f854

0x4f345(text, window, backup page, work page, ticks), called once with the credits text at 0x37470, window 18 (the 304 × 128 picture area), pages 2 and 6 and 2 ticks. It first saves what the screen shows in the window to the backup page. Each step waits ticks ticks, keeps up to 35 lines in a list, copies the backup to the work page, draws there the lines inside the window, moves every line up 2 pixels and copies the work page to the screen. A line ends at a carriage return or at the end of the text. A line that starts with byte 2 is a picture: the next byte n selects shape n − 1, centred. A line that starts with byte 1 is drawn in the 6-pixel font, the others in the 8-pixel font, each text line twice: colour 12 one pixel left and down, then colour 240. Lines are spaced by their height plus a quarter. A line that has left the top is removed (0x4f7bd). The scroll ends when the text is exhausted and the last line reaches the bottom edge (0x4f806–0x4f83d), or when no line is left.

Two details of this routine are recorded as anomalies. ANOMALY-94: for a picture line, the line's text pointer is replaced by the shape pointer (0x4f4be–0x4f4cb, 0x4f5d7–0x4f5e5), so removing that line writes the saved line end into the shape's data (0x4f7ca–0x4f7e6). ANOMALY-95: lines are drawn while their window-relative position is below the window's height (0x4f66b), but the end test compares that position with the window's bottom on the screen (0x4f80b–0x4f83d), which stops the scroll 8 pixels early.

Comparative source: REF-SCUMM-012, DarkMoonEngine::seq_playCredits.

Used by: Play the ending, The ending › The credits, The ending, The ending, Anomalies and bugs

Set the palette except colour 255​

AddressRange
0x4fafb0x4fafb–0x4fbda

Mode 3 only: sends to the palette, through driver slot 48, the colours 0–254 that differ from the last palette sent, in runs of at most 80 colours, and keeps a copy of the whole palette at 0x36770. Colour 255, the text colour, is never changed.

Used by: Show the work page, Run an animation list, Advance the delayed palette fade

Fade the palette​

AddressRange
0x501830x50183–0x502c6

0x50183(palette, target, speed): in the modes other than 3, or with speed 0, copies target into palette and sets it at once (0x501ae–0x501d4). In mode 3 it takes d, the largest absolute difference between the 768 bytes of the two palettes (0x501d7–0x50216), and returns if it is 0. It computes the step time s = ⌊256 × speed / d⌋ in 1/256 tick with the long division helper 0x18551 (0x50221–0x5023a) and the limit max (1, ⌊ d / speed⌋) (0x5023d–0x50256), and records the free-running counter (0x5025e). Then, while a fade step with that limit changes a byte, it sets the palette (0x4fafb), adds s to an 8.8 accumulator, moves its target time on by the whole ticks and busy-waits until the counter reaches it (0x5026d–0x502a4). The fade takes ⌈ d / limit⌉ steps and ends about ⌊steps × s / 256⌋ ticks after it began, roughly speed² / d. Used with speed 10 to fade to black after the credits (1 tick with d = 63) and 18 to fade in the last picture (5 ticks).

Used by: Play the ending, The ending › Display modes, The ending, Compressed containers (CPS)

One palette fade step​

AddressRange
0x502c70x502c7–0x50345

0x502c7(target, palette, limit) moves each of the 768 bytes of palette towards target by at most limit; returns 1 when a byte changed.

Used by: Fade the palette, Advance the delayed palette fade, The ending › Display modes

Start a delayed palette fade​

AddressRange
0x503550x50355–0x503a3

0x50355(target, rate) copies the last palette sent into the working palette, records target and rate (DS:0x1bde, DS:0x1be2) and schedules the first step 2 ticks later (DS:0x6d78). The fade then runs while the sequence goes on, one step at a time from 0x503a4. The copy (0x5035c–0x50373, from the block of segment operand 0x5a8) happens in every display mode, but only mode 3 ever fills that block (0x4fb0d), and the steps run in mode 3 only (0x503a7). In the other modes no palette is sent from the working palette before FINALE_7 replaces it, so nothing visible follows (inferred).

Used by: Play the ending

Advance the delayed palette fade​

AddressRange
0x503a40x503a4–0x50415

Mode 3 only, when a delayed fade is running and its time has passed: one fade step of the working palette towards the target, with the rate as limit; if a component changed, sets the palette except colour 255 and schedules the next step 3 ticks later, otherwise ends the fade. Called from every wait, from the music-marker waits and from the credits.

Used by: Poll during a wait, Scroll the credits

Wait for a tick​

AddressRange
0x4f9550x4f955–0x4f987

Waits until the free-running counter reaches the deadline it is given, calling 0x4f988 on every turn; returns the skip flag.

Used by: Run an animation list, Play the ending

Poll during a wait​

AddressRange
0x4f9880x4f988–0x4f99b

While the skip flag DS:0x1b1e is 0, advances the delayed palette fade (0x503a4); returns the skip flag. It reads no input. No instruction of the executable stores a nonzero value in DS:0x1b1e: the ending only clears it (0x50473, 0x50b2a, 0x510ca, 0x51183, 0x51189, 0x51227) and tests it. The word DS:0x1b20, set to 1 before the credits (0x511e1), is never read. This routine, with the absence of any writer of DS:0x1b1e, is the proof of ANOMALY-93; the branches for a set flag in play the ending (0x50974, 0x511df) are never reached.

Used by: Wait for a tick, Scroll the credits, The ending, Anomalies and bugs

Play a sound​

AddressRange
0x503460x50346–0x50354

Calls the sound routine with its argument. The animation lists play sounds 4, 5, 6, 8, 9, 10 and 15 through it; the ending's own sounds are played directly.

Used by: Run an animation list

Sources​

The executable is identified in the file inventory; the overlay layout is described on the overlays page. Comparative sources are listed in the technical references.