Analysis model: gpt-5.5 xhigh

# Microprose's Pirates by The FiRM - Technical Dissection

`Microprose's Pirates` is a 18 August 1989 MS-DOS cracktro / game
conversion wrapper by The FiRM. Demozoo lists it as a The FiRM cracktro
released on 18 August 1989 for MS-DOS, and the Scene.org archive is
`demos/compilations/lost_found_and_more/cracktro/pirates_cracktro.zip`.

Release year: 1989.

Sources:

- Demozoo: <https://demozoo.org/productions/171320/>
- Scene.org file info:
  <https://files.scene.org/view/demos/compilations/lost_found_and_more/cracktro/pirates_cracktro.zip>
- Scene.org archive:
  <https://files.scene.org/get/demos/compilations/lost_found_and_more/cracktro/pirates_cracktro.zip>

The release note says the package converts the game from its floppy-oriented
form to a hard-disk/file-oriented one, removes the documentation check, adds a
save-file mechanism, and adds a Ctrl-Q drop-to-DOS escape. The analysis below
focuses on the loader/cracktro side that is present in the small public archive,
not on a full original-game comparison.

## Visual References

| Time | Image | Notes |
| --- | --- | --- |
| `00:00.000` | ![Microprose's Pirates public Demozoo FiRM screen](images/microprose-pirates-firm-demozoo.png) | Public Demozoo still of the FiRM startup screen. This is the visible cracktro/front-end frame driven by the loader and sidecar screen data. |

![Microprose's Pirates FiRMLOADER-2 and screen pipeline map](images/microprose-pirates-firm-loader-map.png)

The screenshot is public Demozoo material. I did not use a local DOSBox runtime
capture for this pass; the timing marker above is therefore a static reference
point, not a measured playback timestamp.

## Archive Shape

The Scene.org ZIP is 17,947 bytes:

```text
$README.DOC    2940 bytes  1989-08-18 02:26
FIRM.SCR      18289 bytes  1989-08-01 16:03
PIRATES.COM    3505 bytes  1989-08-18 02:53
PIRATES.FRM       0 bytes  2017-05-02 09:04
```

SHA-256:

```text
7f34bb33bb382b7636f66437044b19476dbad2584ce2464c1e50cf2a1ec5fc4a  pirates_cracktro.zip
fde6f3bc7f4cc8eb3c17d76894cea47557d0bbef5e3d4c153fa76e90eea7f21b  $README.DOC
948521433244d8a6700e52f86552bcd0749f7151aa2589e753ccb45a685589d8  FIRM.SCR
01e049c6b32ce4dbf8712be605690a3d20be853185940cb02d1bbbb3419ecaa2  PIRATES.COM
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  PIRATES.FRM
```

`PIRATES.FRM` is empty in this archive. The release note discusses a
`PIRATES.SAV` save mechanism, but that file is not part of this ZIP. The active
front-end files here are `PIRATES.COM` and `FIRM.SCR`.

## COM Name, MZ Body

`PIRATES.COM` is not a flat COM program. Its first bytes are an MZ header:

```text
4D 5A 91 01 07 00 00 00 02 00 65 00 FF FF 37 01
80 00 00 00 12 00 C4 00 1E 00 00 00 01 00 00 00
```

Important MZ fields:

```text
header paragraphs     0002h = 32 bytes
relocation records    0000h
minalloc              0065h
maxalloc              FFFFh
initial SS:SP         0137h:0080h
initial CS:IP         00C4h:0012h
relocation table      001Eh
overlay number        0000h
entry load offset     0C52h
```

The first loaded bytes are not active code. They are the visible packer banner:

```text
"  FiRMLOADER-2   was written by       The       Software Surgeons"
```

There is one size wrinkle. If the MZ page fields are interpreted as the total
MZ file size, they describe `0D91h` bytes, while the physical file is `0DB1h`
bytes. The difference is exactly 32 bytes. The loader's copied `0151h`-byte
stub reaches into that physical tail, and the relocation stream needs the final
tail word `0819h`. For this reconstruction I treated the bytes after the
32-byte MZ header as the physical copied image:

```text
actual file size       0DB1h
declared MZ size       0D91h if read as total file size
physical load bytes    0D91h after the 20h-byte header
copy-up source range   0C40h..0D90h
```

That is why this loader should not be reduced to "ordinary MZ EXE with no
overlay." The packer table and copied stub intentionally use the physical tail.

## Offset Convention

The outer unpacker is easiest to read in flat load-image offsets, where offset
zero is the first byte after the MZ header.

After unpacking, the restored runtime jumps to `load_base+0010h:06EDh`.
Therefore restored CS-relative offset `xxxxh` maps to flat load offset
`xxxxh + 0100h`. For example:

```text
CS:06EDh  -> flat 07EDh  restored entry
CS:01CBh  -> flat 02CBh  relocated runtime segment word
CS:0C3Dh  -> flat 0D3Dh  in-place decoder routine
```

Stack-relative `[BP]` references are different again. After the stack switch,
`SS = load_base`, so `SS:[0000..003F]` points at the original FiRMLOADER banner
area, not at restored `CS:0000..003F`.

That distinction matters for the second decoder.

## FiRMLOADER-2 Table

Immediately before the active entry, at load offset `0C40h`, is the generic
FiRMLOADER-2 table:

```text
0C40 / CS:0000  06ED   restored IP
0C42 / CS:0002  0010   restored CS delta, patched by adding load base
0C44 / CS:0004  0000   load base segment, filled at runtime
0C46 / CS:0006  0151   copied-stub byte count
0C48 / CS:0008  0100   restored SP
0C4A / CS:000A  0000   restored SS delta
0C4C / CS:000C  0121   copied-stub segment delta
0C4E / CS:000E  0001   source/destination paragraph delta
```

The restored far entry is therefore:

```text
CS = load_base + 0010h
IP = 06EDh
flat load offset = 0100h + 06EDh = 07EDh
```

## Copy-Up Stub

The MZ entry begins at load offset `0C52h`, reached as `CS:0012h` where the
entry segment begins at flat `0C40h`.

```asm
0C52  mov ax,es
0C54  add ax,0010h       ; AX = MZ load base, from PSP segment in ES
0C57  push cs
0C58  pop  ds            ; DS = packed entry segment
0C59  mov [0004h],ax     ; save load base in table
0C5C  add ax,[000Ch]     ; AX = load_base + 0121h
0C60  mov es,ax          ; copied-stub segment
0C62  mov cx,[0006h]     ; CX = 0151h
0C66  mov di,cx
0C68  dec di             ; DI = 0150h
0C69  mov si,di
0C6B  std
0C6C  rep movsb          ; copy CS:0150..0000 to high segment
0C6E  mov dx,[000Eh]     ; DX = 0001h
0C72  push ax
0C73  mov ax,0038h
0C76  push ax
0C77  retf               ; copied_stub_segment:0038h
```

The inner string loop is:

```text
while CX != 0:
    byte ES:DI = byte DS:SI
    SI -= 1
    DI -= 1
    CX -= 1
```

The backward direction is deliberate. The copied block includes the table,
decompressor code, corruption branch, relocation stream, and the physical tail
bytes after the declared MZ size.

## Backward Packed Stream

After the far return, the copied stub still has the direction flag set. It
derives a source segment one paragraph below the original stub and a destination
segment one paragraph below the copied stub:

```asm
0C78  mov bx,es
0C7A  mov ax,ds
0C7C  sub ax,dx
0C7E  mov ds,ax
0C80  mov es,ax
0C82  mov di,000Fh
0C85  mov cx,0010h
0C88  mov al,0FFh
0C8A  repe scasb         ; scan the FF pad downward
0C8C  inc di
0C8D  mov si,di          ; SI lands on the last packed command
0C8F  mov ax,bx
0C91  sub ax,dx
0C93  mov es,ax
0C95  mov di,000Fh       ; destination high edge
```

With correct `SCASB` semantics, the scan sees:

```text
DS:000F = FFh
DS:000E = B2h
```

`SCASB` decrements `DI` even on the mismatching `B2h`; the following `INC DI`
therefore leaves `SI = 000Eh`, exactly on the first command byte.

Each record iteration begins by normalizing downward 16-bit pointers:

```asm
normalize_source:
    ax = not si
    ax >>= 4
    if ax != 0:
        ds -= ax
        si |= FFF0h

normalize_destination:
    ax = not di
    ax >>= 4
    if ax != 0:
        es -= ax
        di |= FFF0h
```

That `(~offset) >> 4` expression is a paragraph-borrow calculation. When a
descending pointer would pass below zero, the segment is moved downward and the
offset is placed back near `FFF0h`.

The command fetch:

```asm
0CBC  lodsb              ; AL = command, SI--
0CBD  mov dl,al
0CBF  dec si
0CC0  lodsw              ; AX = count word, SI -= 2 under DF=1
0CC1  mov cx,ax
0CC3  inc si
0CC4  mov al,dl
0CC6  and al,0FEh
```

Because `DF=1`, the packed record is read high-to-low:

```text
command byte
count word immediately below it
optional fill byte or literal bytes immediately below that
```

There are only two operation families:

```asm
if (command & FEh) == B0h:
    value = lodsb()
    repeat CX times:
        byte ES:DI = value
        DI -= 1

if (command & FEh) == B2h:
    repeat CX times:
        byte ES:DI = byte DS:SI
        SI -= 1
        DI -= 1

if command & 1:
    finish stream
```

So `B0h` and `B1h` are fill records, while `B2h` and `B3h` are literal-copy
records. The low bit marks the final record.

The exact `PIRATES.COM` stream recovered by the emulator:

| Record | Command location | Command | Meaning | Count | Destination |
| --- | --- | --- | --- | ---: | --- |
| 1 | `0C3Eh` | `B2` | literal copy | `0A23h` / 2595 | `07EDh..120Fh` |
| 2 | `0218h` | `B0` | zero fill | `051Fh` / 1311 | `02CEh..07ECh` |
| 3 | `0214h` | `B2` | literal copy | `01CEh` / 462 | `0100h..02CDh` |
| 4 | `0043h` | `B1` | fill and terminate | `00C0h` / 192 | `0040h..00FFh`, byte `73h` |

The output range touched by the packed stream is `0040h..120Fh`. The lower
`0000h..003Fh` banner bytes remain as key/state material for the later decoder.

## Relocation Repair

When the stream terminates, the copied stub switches back to forward direction
and reads relocation records from copied-stub offset `012Dh`:

```asm
0CDE  mov si,012Dh
0CE1  push cs
0CE2  pop ds
0CE3  mov bx,[0004h]     ; BX = load base
0CE7  cld
0CE8  xor dx,dx
```

The relocation loop is page-oriented. `DX` steps through `0000h`, `1000h`,
`2000h`, and so on up to `F000h`. For each page:

```text
count = lodsw()
if count != 0:
    ES = load_base + DX
    repeat count times:
        DI = lodsw()
        if DI == FFFFh:
            patch the word that crosses the segment boundary
        else:
            word ES:DI += load_base
```

This file has two relocation words:

```text
page 0000h, offset 02CBh: 0010h -> load_base+0010h
page 0000h, offset 0819h: 0000h -> load_base
```

Those two fixups explain the restored code's segment assumptions:

- flat `02CBh` is the runtime segment word later loaded through `CS:[01CBh]`;
- flat `0819h` is the stack-switch immediate that becomes `SS = load_base`.

The final handoff:

```asm
0D1E  mov ax,bx
0D20  mov di,[0008h]     ; SP = 0100h
0D24  mov si,[000Ah]     ; SS delta = 0000h
0D28  add si,ax          ; SS = load_base
0D2A  add [0002h],ax     ; restored CS += load_base
0D2E  sub ax,0010h
0D31  mov ds,ax          ; PSP-ish segment
0D33  mov es,ax
0D38  cli
0D39  mov ss,si
0D3B  mov sp,di
0D3D  sti
0D3E  jmp far cs:[0000h] ; load_base+0010h:06EDh
```

## Restored Entry And Stack Threading

The restored entry at flat `07EDh`, logical `CS:06EDh`, begins:

```asm
06ED  pushf
06EE  pop  word cs:[01DDh]    ; save flags
06F3  mov  cs:[01DFh],ax      ; save AX
06F7  call 0716h              ; stack/segment setup
06FA  call 0B10h              ; state snapshot
06FD  call 0AE0h              ; interrupt vector install
0700  jb   0737h              ; error/protected branch
0702  push ax
0703  push bp
0704  mov  bp,sp
0706  mov  ax,0700h
0709  xchg ax,[bp+04h]
070C  xor  ax,ss:[0814h]
0711  xchg ax,[bp+02h]
0714  pop  bp
0715  ret
```

The helper at logical `0716h` switches to the restored stack:

```asm
0716  pop bx
0717  cli
0718  mov sp,load_base_word   ; relocated word at flat 0819h
071B  mov ss,sp
071D  mov sp,0100h
0720  sti
0721  mov ds,cs:[01CBh]       ; flat 02CBh -> load_base+0010h
0726  mov es,[01CBh]
072A  jmp bx
```

After that point:

```text
SS = load_base
SP = 0100h
DS = ES = load_base + 0010h
```

The code is stack-threaded. The sequence at `0702h..0715h` does not merely
return to its caller. It exchanges words with the stack and uses
`SS:[0814h]` as a key, causing the next `RET` to land in another protected
routine. In the reconstructed image, the first computed path reaches logical
`0B65h`, then returns back to logical `0700h`.

That is why straight-line disassembly of `0700h` looks strange. The code is
using the stack as a transfer table.

## State Snapshot

The helper at logical `0B10h` snapshots machine state before the cracktro
changes video, ports, or vectors:

```asm
0B10  mov ah,0Fh
0B12  int 10h
0B14  mov [01D6h],al      ; current video mode

0B17  mov ah,03h
0B19  int 10h
0B1B  mov [01D7h],cx      ; cursor shape

0B1F  mov ax,3300h
0B22  int3                ; later used as DOS proxy / protected boundary
0B23  mov [01D9h],dl      ; Ctrl-Break flag

0B27  in  al,61h
0B29  mov [01DAh],al
0B2C  in  al,21h
0B2E  mov [01DBh],al
0B31  in  al,0A1h
0B33  mov [01DCh],al
```

It then copies low-memory state into the program image:

```asm
0B36  xor si,si
0B38  mov ds,si
0B3A  mov di,02B9h
0B3D  mov cx,0200h
0B40  rep movsw           ; 0000:0000 IVT, 1024 bytes

0B42  mov si,0410h
0B45  mov cl,07h
0B47  rep movsb           ; BIOS equipment area

0B49  mov si,04F0h
0B4C  mov cl,08h
0B4E  rep movsw           ; more BIOS data

0B50  mov si,050Eh
0B53  mov cl,09h
0B55  rep movsw
```

Finally it restores `DS` to the runtime segment. This mirrors other
FiRMLOADER-2 productions: snapshot first, install vectors second, restore on
exit.

## Interrupt Vector Install

The logical `0AE0h` helper writes directly into the interrupt vector table:

```asm
0AE0  mov al,23h
0AE2  mov dx,cs
0AE4  mov cx,072Ch
0AE7  call 0AF4h          ; INT 23h = CS:072C

0AEA  mov al,03h
0AEC  mov dx,[033Fh]
0AF0  mov cx,[033Dh]

0AF4  push ds
0AF5  xor bx,bx
0AF7  mov ds,bx
0AF9  xor ah,ah
0AFB  mov bx,ax
0AFD  shl bx,1
0AFF  shl bx,1
0B01  cli
0B02  mov [bx],cx
0B04  mov [bx+2],dx
0B07  sti
0B08  pop ds
0B09  ret
```

The first vector catches Ctrl-Break. The second points `INT 03h` at the saved
old DOS vector from the IVT snapshot. Later code uses byte `CCh` as a compact
DOS-call proxy.

## Display And CPU Probes

The decoded helper at logical `0B79h` tests the display class:

```asm
0B79  mov es,[01CDh]      ; BIOS data segment
0B7D  mov ah,es:[0003h]
0B82  mov es,[01CBh]
0B86  mov al,00h
0B88  and ah,0F0h
0B8B  cmp ah,0B0h
0B8E  je  0BA0h
0B90  mov ax,1200h
0B93  mov bl,10h
0B95  int 10h             ; EGA/VGA config query
0B97  mov al,01h
0B99  and bl,0Fh
0B9C  je  0BA0h
0B9E  mov al,02h
0BA0  cbw
0BA1  ret
```

The result is saved at logical `01D5h`. The loader derives a video segment word
from it:

```asm
call 0B79h
mov [01D5h],al
shl al,1
shl al,1
shl al,1
xor al,0B0h
xchg ah,al
mov [01CFh],ax
```

For text/mono/CGA/EGA cases that turns into a segment such as `B000h`,
`B800h`, or an EGA/VGA graphics target.

The helper at logical `0BA2h` measures timer speed by watching the BIOS tick
byte at `0040:006Ch`:

```asm
0BA2  mov es,[01CDh]
0BA6  mov bl,04h
0BA8  xor dx,dx
0BAA  xor cx,cx
0BAC  mov ah,es:[000Ch]
wait_change:
0BB1  mov al,es:[000Ch]
0BB5  cmp al,ah
0BB7  je  wait_change
sample_loop:
0BB9  mov al,es:[000Ch]
0BBD  cmp al,es:[000Ch]
0BC2  jne changed
0BC4  loop sample_loop
0BC6  inc dx
changed:
0BC7  dec bl
0BC9  jne sample_loop
```

It folds the result down with shifts/rotates and stores a delay constant at
`01D1h`. The delay routines later spin on that value, but still poll the
keyboard so the loader can break out.

The CPU check at logical `0BE8h` is a flags-behavior probe. It pushes a crafted
flags word, pops it back, masks high flag bits, and then uses a large-shift test
to distinguish older x86 behavior from 286/386-style behavior. The result is a
small timing/capability code stored at `01D3h`.

## SS-Keyed Decoder At Logical 0C3D

The next protection layer is an in-place decoder. It is reached by calls with
inline arguments:

```asm
0C27  call 0C3Dh
      dw 0006h
      dw 01C5h

0C2E  call 0C3Dh
      dw 072Fh
      dw 03B1h

0C35  call 0C3Dh
      dw 0C5Bh
      dw 0383h
```

The decoder:

```asm
0C3D  pop si              ; SI points at inline parameters
0C3E  lodsw
0C3F  mov di,ax           ; destination/source offset
0C41  lodsw
0C42  push si             ; return after inline parameters
0C43  push bp
0C44  xor bp,bp
0C46  mov si,di
0C48  mov cx,ax
loop:
0C4A  lodsb               ; AL = encoded byte, AH remains live
0C4B  xor ax,[bp+00h]     ; default segment is SS
0C4E  xor ax,cx
0C50  xor al,ah
0C52  stosb
0C53  inc bp
0C54  and bp,003Fh        ; 64-byte rolling key window
0C57  loop loop
0C59  pop bp
0C5A  ret
```

The important detail is `[BP]`. On 8086, BP-based memory operands default to
`SS`, not `DS`. At this point:

```text
SS = load_base
DS = ES = load_base + 0010h
```

So the decoder reads its rolling 64-byte key from flat load offsets
`0000h..003Fh`, the original FiRMLOADER banner/key area, while it decodes
CS-relative blocks in the restored runtime.

A byte-level model:

```text
AX starts as the block length.
BP starts at 0.

for CX from length down to 1:
    AL = encoded_byte[SI]
    AX ^= word SS:[BP]
    AX ^= CX
    AL ^= AH
    decoded_byte[DI] = AL
    BP = (BP + 1) & 3Fh
```

Because `LODSB` does not clear `AH`, the high byte of `AX` carries state from
the previous iteration. This is not a simple stateless XOR.

After correcting the BP/SS segment rule, the decoder reveals the DOS error
strings, display code, `FIRM.SCR` stream code, and FiRM credit text.

## Error/Text Bytecode Printer

The decoded printer at logical `077Eh` renders bytecoded messages with BIOS
cursor and character services. It is used for error paths and for public FiRM
screen text.

Core loop:

```asm
077E  mov bx,0007h        ; current attribute in BL
0781  mov dx,0100h        ; initial cursor row/col
0784  mov ah,02h
0786  int 10h             ; set cursor

next:
0788  lodsb
0789  or al,al
078B  je next             ; skip zero bytes
078D  cmp al,0FFh
078F  je done
0791  cmp al,0FEh
0793  jne not_cursor
0795  lodsw
0796  mov dx,ax
0798  jmp set_cursor
```

Control bytes:

```text
00h       skip
FEh word  set cursor DX, then continue
FFh       return
A0h       BIOS teletype BEL, AX=0E07h
80h..8Fh  update low attribute nibble
90h..9Fh  update high attribute nibble
other     print one character
```

The character path:

```asm
07CB  mov cx,0001h
07CE  mov ah,09h
07D0  int 10h             ; write AL with BL attribute once
07D2  inc dl
07D4  cmp dl,50h
07D7  jb  set_cursor
07D9  inc dh
07DB  xor dl,dl
```

This is a compact screen-script interpreter. It writes through BIOS rather than
directly poking text memory, which lets the same message routine serve multiple
display modes.

Decoded public strings include:

```text
Memory allocation error
File ... not found
Insufficent memory
DOS error-code (...) detected
Thanks for supporting THE FiRM!
Another superior FiRM crack by The Software Surgeon
```

The misspelling `Insufficent` is present in the decoded bytes.

## Restore/Exit Path

The decoded restore helper at logical `07E7h` copies the saved IVT and BIOS
state back:

```asm
07E7  mov si,02B9h
07EA  xor di,di
07EC  mov es,di
07EE  mov cx,0200h
07F1  cli
07F2  rep movsw           ; restore IVT

07F4  mov di,0410h
07F7  mov cl,07h
07F9  rep movsb

07FB  mov di,04F0h
07FE  mov cl,08h
0800  rep movsw

0802  mov di,050Eh
0805  mov cl,09h
0807  rep movsw

0809  les di,[0331h]
080D  mov cl,0Bh
080F  rep movsb
0811  sti
```

Then it restores ports and timer/PIC state:

```asm
0816  mov al,[01DAh]
0819  out 61h,al
081B  mov al,[01DBh]
081E  out 21h,al
0820  mov al,[01DCh]
0823  out 0A1h,al
0825  mov al,80h
0827  out 0A0h,al
0829  mov al,7Fh
082B  out 70h,al
082D  in  al,71h
082F  mov al,36h
0831  out 43h,al
0833  mov al,00h
0835  out 40h,al
0839  out 40h,al
```

The design is symmetric: save machine state, install vectors and video mode,
show the loader screen or error, restore state, and then either exit or hand
off to the protected game path.

## FIRM.SCR Path

`FIRM.SCR` is 18,289 bytes:

```text
size       18289
unique     246 byte values
zeroes    4719
FF bytes   605
```

The first bytes are not a raw bitmap header:

```text
0000: 87 00 09 07 11 39 00 00 34 3D 00 3F FF 00 B3 00
0010: 03 02 00 30 86 00 08 01 C0 02 00 07 FF E0 60 86
0020: 00 08 01 40 06 00 1F F8 01 C0 86 00 08 01 40 06
```

A raw 640x350x16 EGA frame would be 112,000 bytes, so the 18 KB sidecar is a
compressed or scripted graphics stream.

The decoded screen path at logical `0C5Bh` has mode-specific output:

- text fallback: BIOS text mode 03h and bytecode printer calls;
- CGA-ish path: BIOS mode 06h and row/pattern decoding;
- EGA/VGA path: BIOS mode 10h, palette call `AX=1002h`, sequencer map-mask
  writes through ports `03C4h/03C5h`, and plane-by-plane output.

The file reader at logical `0D1Bh` prepares a filename/open buffer, opens a
file with `AX=3D00h`, reads up to `D000h` bytes into the screen buffer at
`1127h`, and closes it, using `INT 03h` as the compact DOS proxy:

```asm
0D20  mov dx,0158h
0D23  mov ax,3D00h
0D26  int3                ; open
0D29  mov bx,ax
0D2B  mov cx,D000h
0D2E  mov dx,[1127h]
0D32  mov ah,3Fh
0D34  int3                ; read
0D37  mov ah,3Eh
0D39  int3                ; close
```

The exact filename materialization is protected by another small inline
decoder. The shipped sidecar and the visible public screen identify the
practical target as `FIRM.SCR`.

## Screen Inflater Loop

The decoded graphics loop around logical `0D42h` is the bridge from the
sidecar stream to video memory. It builds a 40-word row buffer at `1130h`, then
copies it to the selected video segment:

```asm
0D42  push di
0D43  mov di,1130h
0D46  mov cx,0050h
0D49  call 0DB8h          ; fill/decode 80 bytes into row buffer
0D4C  mov dx,[1124h]      ; row countdown / mode-specific height
...
0D90  pop di
0D91  mov si,1130h
0D94  mov cx,0028h
0D97  mov es,[01CFh]      ; video segment derived from display probe
0D9B  rep movsw           ; 40 words to video
0D9D  mov es,[01CBh]      ; restore runtime segment
0DA1  dec dx
0DA2  jne next_row
```

The stream expander at logical `0DB8h` keeps a tiny state word at `1121h` and a
source pointer at `1127h`:

```asm
0DB8  mov si,[1127h]
0DBC  mov ax,[1121h]
0DBF  or  al,al
0DC1  jne have_run
0DC3  lodsb
0DC4  cbw
0DC5  and al,7Fh
0DC7  je  fail_empty

have_run:
0DC9  movsb
0DCA  dec al
0DCC  je  store_state
0DCE  ror ah,1
0DD0  jae store_state
0DD2  dec si              ; repeat previous source byte on carry

store_state:
0DD3  loop 0DBFh
0DD5  mov [1121h],ax
0DD8  mov [1127h],si
0DDC  ret
```

This is a compact run/repeat expander:

```text
if no active run:
    AL = next control byte & 7Fh
    if AL == 0: signal failure/end

for each output byte requested:
    copy one source byte
    AL -= 1
    if AL != 0:
        rotate AH right
        if carry set:
            SI -= 1       ; repeat the same source byte next time
```

The surrounding mode-specific code decides whether that row buffer is copied
linearly, interleaved for CGA, or plane-by-plane for EGA/VGA.

## Delay And Keyboard Loop

The delay helper at logical `0EA3h` is used by the screen animation and palette
steps:

```asm
0EA3  push cx
0EA4  mov cx,[01D1h]
0EA8  loop 0EA8h          ; calibrated spin
0EAA  pop cx
0EAB  mov ah,01h
0EAD  int 16h             ; key pending?
0EAF  jne key
0EB1  loop 0EA3h
0EB3  ret
```

On keypress:

```asm
0EB4  mov ah,0Bh
0EB6  int3                ; DOS keyboard status through proxy
0EB7  mov ah,00h
0EB9  int 16h             ; consume BIOS key
0EBB  sub al,[0036h]
0EBF  je abort
```

It can either return normally, unwind to the caller's frame, or set carry for
an abort path. This is how the front-end remains interruptible while using
busy-wait timing.

## What Each Part Does

High-level execution:

```text
1. DOS loads PIRATES.COM as an MZ executable despite the .COM extension.
2. MZ entry reaches FiRMLOADER-2 at flat load offset 0C52h.
3. The stub stores the actual load base from ES+10h.
4. It copies a 0151h-byte decompressor block upward with STD / REP MOVSB.
5. The copied stub scans the FF pad and walks the packed stream backward.
6. B2 records copy literal bytes; B0/B1 records fill repeated bytes.
7. Four records rebuild flat offsets 0040h..120Fh.
8. The relocation stream patches flat 02CBh and 0819h.
9. The final far jump enters load_base+0010h:06EDh.
10. The restored entry switches stack and segments.
11. It snapshots video, IVT, BIOS data, and ports.
12. It installs INT 23h and INT 03h proxy vectors.
13. It uses stack-threaded returns to reach more protected code.
14. The SS-keyed decoder reveals error strings, display code, and screen logic.
15. The text printer handles bytecoded status/error/credit strings.
16. The screen path opens and reads the sidecar stream, practically FIRM.SCR.
17. The stream expander fills 40-word row buffers and copies them to video.
18. The delay loop calibrates from timer observation and polls the keyboard.
19. The restore path puts IVT, BIOS data, ports, and video state back.
```

## Relationship To Nearby FiRM Loaders

Compared with the simpler August 1989 `The Firm Loader (1)`, this is clearly a
later generation:

- it is an MZ executable hidden behind a `.COM` name;
- it uses the reusable FiRMLOADER-2 `B0/B2` backward packed stream;
- it has a relocation stream independent of the MZ relocation table;
- it uses stack-threaded protected control flow;
- it has a second SS-keyed in-place decoder;
- it drives a separate encoded `FIRM.SCR` screen file.

Compared with later `Operation Wolf` and `Licence To Kill` FiRMLOADER-2
packages, the outer pattern is already recognizable, but the sidecar is
smaller: 18,289 bytes here instead of the 29,070-byte screen sidecar seen in
those later releases.

## Boundaries

Verified in this pass:

- archive identity and public metadata;
- MZ header and misleading `.COM` filename;
- FiRMLOADER-2 table and copy-up stub;
- four exact packed-stream records;
- two exact relocation words;
- restored entry and stack/segment setup;
- state snapshot and IVT writer;
- SS-keyed decoder semantics;
- decoded text/error printer;
- decoded restore path;
- decoded file/screen-stream path;
- row-buffer expander used by the screen renderer.

Not fully verified here:

- exact original-game patch locations after the front-end hands off;
- full local DOSBox timing of the startup animation;
- a complete standalone decoder for every protected filename/table helper.

The important correction versus a shallow read is that `PIRATES.COM` is not just
"a packed EXE." It is a renamed MZ carrier whose outer FiRMLOADER-2 pass rebuilds
a second stack-threaded runtime, and that runtime then uses SS-relative keying,
inline decoder parameters, BIOS/DOS proxy calls, and the `FIRM.SCR` sidecar to
produce the public FiRM screen before game handoff.
