*EXPAND: picture unpacker

Dunjunz, Ole's crack of the cassette (1986)

Updated 8 Nov 2026

← All Dunjunz versions

FileLoadExecLengthSHA-256
EXPAND&2E00&2E00&01A76a18d63bf6abef3b2a5e8c65d9c886719c6eb203dfa24635d8f0271cd028e8d1

EXPAND: unpack a compressed MODE 2 picture

EXPAND unpacks a run-length-compressed MODE 2 picture onto the screen. It is run as *EXPAND <filename>: the crack's BASIC PLOAD runs *EXPAND GIRL1 for the intro picture, in MODE 2 with the palette set up for it. It is entered here, at the load address, which jumps over the copyright notice to expand. The run, in expand: refuse anything but MODE 2; find the filename on the command line; open the file only to read its length; black out all sixteen colours; load the file so that it ends at &8000; unpack it run by run from &3000 until the screen is full; put back the zero page (&80-&87) and page &0C bytes it borrowed; and VDU 20, which restores the default palette and shows the picture. It returns to its caller with the picture on the screen, the default palette in place of the caller's, the file closed, and the zero page and &0C00 bytes as it found them. Each packed byte holds a screen byte in bits 5-0 and a run code in bits 7-6: codes 0-2 give a run of 1-3, and code 3 takes the run, n+1, from the next byte. There is no end marker.

On EntryCOMMAND LINEthe filename, after *EXPAND
On ExitAcorrupted
Xcorrupted
Ycorrupted
2E00 .expand_start
JMP expand ; Skip the copyright notice

Copyright notice

2E03 .copyright_notice
EQUS "(C) 1985 Rhys E. Davies." ; The author's copyright

Find the next argument on the command line

Step command_ptr past spaces and commas. A carriage return (the end of the line) is left for the next call. Called once, by expand, to find the filename after *EXPAND; on success command_ptr is one past the name's first character, so expand steps it back by one.

On ExitCset if the line has no more arguments; clear if command_ptr is one past the argument's first character
2E1B .skip_separators←1← 2E68 JSR
LDY #0 ; Y = 0 throughout: command_ptr itself advances
2E1D .skip_separators_loop←2← 2E27 BEQ← 2E2B BEQ
LDA (command_ptr),y ; Read the next character of the command line
2E1F INC command_ptr ; Advance command_ptr past it...
2E21 BNE check_separator ; No carry: pointer done
2E23 INC command_ptr + 1 ; Carry into the high byte
2E25 .check_separator←1← 2E21 BNE
CMP #' ' ; A space, which separates arguments?
2E27 BEQ skip_separators_loop ; A space: read on
2E29 CMP #',' ; A comma, which separates them too?
2E2B BEQ skip_separators_loop ; A comma: read on
2E2D CMP #&0d ; The end of the line?
2E2F BNE argument_found ; No: an argument starts here
2E31 DEC command_ptr ; Step command_ptr back onto the carriage return...
2E33 LDA command_ptr ; ...did the low byte wrap...
2E35 CMP #&ff ; ...from &00 to &FF?
2E37 BNE no_argument ; No: pointer done
2E39 DEC command_ptr + 1 ; Yes: borrow from the high byte
2E3B .no_argument←1← 2E37 BNE
SEC ; Carry set: no argument
2E3C RTS ; Return with C set: the line has no filename
2E3D .argument_found←1← 2E2F BNE
CLC ; Carry clear: an argument found
2E3E RTS ; Return with C clear: a filename starts one byte back

Load the named picture and unpack it onto the screen

The whole of *EXPAND, reached by expand_start's JMP. Refuses to run outside MODE 2. Finds the filename on the command line, opens the file to learn its length, and loads it so that it ends at &8000, with all sixteen logical colours black so that neither the packed bytes nor the unpacking show. Unpacks it from &3000 until the screen is full, restores the zero page and page &0C bytes it used, and resets the palette (VDU 20) to reveal the picture. The packed bytes lie in the same screen memory that the picture is unpacked into, ending together at &8000, so the writing must never overtake the reading. Every packed byte gives at least one screen byte, except a code-3 pair for a run of one, so the writing stays behind unless the file holds such a pair; nothing checks this. All the state EXPAND saves is kept on the stack: 26 bytes during the unpacking.

On EntryCOMMAND LINEthe filename, after *EXPAND
On ExitAcorrupted
Xcorrupted
Ycorrupted
2E3F .expand←1← 2E00 JMP
LDA #osbyte_read_char_at_cursor ; Read the screen mode into Y
2E41 JSR osbyte ; ...Y = the current screen mode Read character at text cursor position and current screen MODE
2E44 CPY #2 ; MODE 2, the only mode EXPAND unpacks into?
2E46 BEQ mode_ok ; Yes: carry on
2E48 BRK ; No: error 25, "Bad Mode"
2E49 EQUB &19 ; Error number 25 (BASIC's own "Bad MODE" number)
2E4A .bad_mode_message
EQUS "Bad Mode" ; Error message
2E52 EQUB &00 ; End of the message
; Find the filename on the command line and open the file.
2E53 .mode_ok←1← 2E46 BEQ
LDA command_ptr ; Save the four zero-page bytes OSARGS will overwrite...
2E55 PHA ; ...low byte of command_ptr...
2E56 LDA command_ptr + 1 ; ...then its second byte (&85)...
2E58 PHA ; ...the caller's, kept...
2E59 LDA command_ptr + 2 ; ...then its third byte (&86)...
2E5B PHA ; ...pushed after the second...
2E5C LDA command_ptr + 3 ; ...then its fourth byte (&87)...
2E5E PHA ; ...kept, to restore before returning
2E5F LDA #1 ; OSARGS 1: command_ptr = the rest of the command line...
2E61 LDX #command_ptr ; ...X = command_ptr, to receive the address...
2E63 LDY #0 ; ...Y = 0 for this call...
2E65 JSR osargs ; ...so command_ptr points past "EXPAND" Get address of remaining command line into zero page address X (A=1, Y=0)
2E68 JSR skip_separators ; Find the filename
2E6B BCC open_picture ; Found: open it
2E6D PLA ; None: restore command_ptr...
2E6E STA command_ptr + 3 ; ...its fourth byte...
2E70 PLA ; ...then the third, pulled in reverse...
2E71 STA command_ptr + 2 ; ...back in place...
2E73 PLA ; ...then the second byte...
2E74 STA command_ptr + 1 ; ...back in place...
2E76 PLA ; ...then the first byte...
2E77 STA command_ptr ; ...restored, so the caller's zero page is intact
2E79 BRK ; ...and give error 0, "Filename expected"
2E7A EQUB &00 ; Error number 0
2E7B .filename_expected_message
EQUS "Filename expected" ; Error message
2E8C EQUB &00 ; End of the message
2E8D .open_picture←1← 2E6B BCC
LDX command_ptr ; Step command_ptr back one, onto the filename's first character...
2E8F LDY command_ptr + 1 ; ...high byte into Y...
2E91 DEX ; ...low byte minus one...
2E92 CPX #&ff ; ...wrapped to &FF?
2E94 BNE filename_found ; No: no borrow
2E96 DEY ; Yes: borrow from the high byte
2E97 .filename_found←1← 2E94 BNE
STX command_ptr ; ...stored back, low byte...
2E99 STY command_ptr + 1 ; ...and high byte; XY = the filename too
2E9B LDA #osfind_open_input ; Open the file for input (X, Y = the filename)
2E9D JSR osfind ; ...A = the handle, or 0 if not found osfind: open file for input
2EA0 TAY ; Y = the file handle
2EA1 BNE load_picture ; Opened: go on
2EA3 PLA ; Not found: restore command_ptr...
2EA4 STA command_ptr + 3 ; ...its fourth byte...
2EA6 PLA ; ...then the third, pulled in reverse...
2EA7 STA command_ptr + 2 ; ...back in place...
2EA9 PLA ; ...then the second byte...
2EAA STA command_ptr + 1 ; ...back in place...
2EAC PLA ; ...then the first byte...
2EAD STA command_ptr ; ...restored, so the caller's zero page is intact
2EAF BRK ; ...and give error 0, "No such compressed picture"
2EB0 EQUB &00 ; Error number 0
2EB1 .no_such_picture_message
EQUS "No such compressed picture" ; Error message
2ECB EQUB &00 ; End of the message
; Set up the OSFILE block at &0C00 and work out the load address ; from the file's length.
2ECC .load_picture←1← 2EA1 BNE
LDX #osfile_block_length - 1 ; For each of the 18 bytes of the OSFILE block at &0C00, last first:
2ECE .set_up_osfile_block←1← 2ED9 BPL
LDA osfile_block,x ; Save the byte already there
2ED1 PHA ; ...on the stack, for restoring afterwards
2ED2 LDA osfile_block_template,x ; Replace it with the template's
2ED5 STA osfile_block,x ; ...in the block at &0C00
2ED8 DEX ; Next byte down, 17 to 0
2ED9 BPL set_up_osfile_block ; Until all 18 bytes are set
2EDB LDA command_ptr ; Point the block at the filename...
2EDD STA osfile_block ; ...its low byte at +0...
2EE0 LDA command_ptr + 1 ; ...then the filename's page...
2EE2 STA osfile_block + 1 ; ...into the block's filename address
2EE5 LDA packed_ptr ; Save the four zero-page bytes OSARGS will overwrite...
2EE7 PHA ; ...packed_ptr low byte kept...
2EE8 LDA packed_ptr + 1 ; ...then packed_ptr's high byte...
2EEA PHA ; ...pushed after the low byte...
2EEB LDA screen_ptr ; ...screen_ptr low byte...
2EED PHA ; ...pushed in its turn...
2EEE LDA screen_ptr + 1 ; ...then screen_ptr's high byte...
2EF0 PHA ; ...kept, to restore before returning
2EF1 LDX #packed_ptr ; OSARGS 2 (Y = the handle): packed_ptr = the file's length (EXT#)...
2EF3 LDA #2 ; ...reason 2: the file's length
2EF5 JSR osargs ; ...four bytes into packed_ptr onwards Get length of file into zero page address X (A=2)
2EF8 LDA #0 ; Load address = &8000 - length: the file ends at the top...
2EFA SEC ; ...ready to subtract...
2EFB SBC packed_ptr ; ...the length's low byte...
2EFD STA osfile_block + 2 ; ...into the block's load address, low byte
2F00 PHA ; ...and keep it on the stack, to start unpacking from
2F01 LDA #>(screen_end) ; ...&80, the top of the screen's page...
2F03 SBC packed_ptr + 1 ; ...minus the length's high byte...
2F05 STA osfile_block + 3 ; ...into the block's load address, high byte
2F08 PHA ; ...and keep it too
2F09 LDA #osfind_close ; Close the file (Y = its handle)
2F0B JSR osfind ; ...the length is known, so it is not needed open osfind: close one or all files
; Black out the palette, then load the packed picture.
2F0E LDX #&0f ; For logical colours 15 down to 0:
2F10 .blacken_colours←1← 2F24 BPL
LDA #&13 ; VDU 19,colour,0,0,0,0: make it black...
2F12 JSR oswrch ; ...VDU 19: redefine a logical colour...
2F15 TXA ; ...logical colour X...
2F16 JSR oswrch ; ...the colour being redefined
2F19 LDA #0 ; ...physical colour 0, black, and three zeros...
2F1B LDY #4 ; ...four bytes to send...
2F1D .send_zeros←1← 2F21 BNE
JSR oswrch ; ...each a zero...
2F20 DEY ; ...one fewer to go...
2F21 BNE send_zeros ; Not all four: send another
2F23 DEX ; Next colour down
2F24 BPL blacken_colours ; Until colours 15 to 0 are black
2F26 LDA #osfile_load ; Load the file at the block's address (exec byte 0)...
2F28 LDX #<(osfile_block) ; ...XY = the block at &0C00...
2F2A LDY #>(osfile_block) ; ...and its page, &0C...
2F2C JSR osfile ; ...so the file lands ending at &8000 osfile: load file
2F2F PLA ; packed_ptr = the load address, the first packed byte...
2F30 STA packed_ptr + 1 ; ...high byte first, as pushed last...
2F32 PLA ; ...then the low byte...
2F33 STA packed_ptr ; ...completing packed_ptr: the file's first byte
; Unpack the runs from &3000 up to &8000.
2F35 LDA #0 ; screen_ptr = &3000, the MODE 2 screen's start...
2F37 TAY ; ...Y = 0 for the (pointer),Y accesses...
2F38 STA screen_ptr ; ...screen_ptr low byte...
2F3A LDA #>(screen_start) ; ...the screen's page...
2F3C STA screen_ptr + 1 ; ...as screen_ptr's high byte
2F3E .unpack_run←1← 2F61 JMP
JSR read_packed_byte ; Read a packed byte
2F41 PHA ; Keep it for its screen byte, used after the run
2F42 AND #&c0 ; Top two bits: the run length code...
2F44 CLC ; ...moved down to bits 1-0 (through the carry)
2F45 ROL ; ...bit 7 into the carry...
2F46 ROL ; ...bit 6 into the carry, bit 7 into bit 0...
2F47 ROL ; ...and bit 6 into bit 0: the code, 0-3
2F48 CMP #3 ; Code 3: the run length is in the next byte?
2F4A BNE run_length_known ; No: the run is the code + 1 bytes
2F4C JSR read_packed_byte ; Yes: the run is the next byte + 1 (0 = 256)
2F4F .run_length_known←1← 2F4A BNE
TAX ; X = the run length...
2F50 INX ; ...plus one: 1-256 (0 wraps to 256)
2F51 PLA ; A = the screen byte: six pixel bits, colours 0-7
2F52 AND #&3f ; ...pixel bits only, dropping the run code
2F54 .write_run←1← 2F5F BNE
STA (screen_ptr),y ; Write it to the screen
2F56 INC screen_ptr ; Advance screen_ptr to the next screen byte...
2F58 BNE next_in_run ; No carry: same page
2F5A INC screen_ptr + 1 ; Carry: the next page
2F5C BMI unpacked ; Reached &8000: the screen is full
2F5E .next_in_run←1← 2F58 BNE
DEX ; Count down the run
2F5F BNE write_run ; More of the run: write it again
2F61 JMP unpack_run ; Next packed byte
; Put back everything EXPAND used, and show the picture.
2F64 .unpacked←1← 2F5C BMI
PLA ; Restore the zero-page bytes...
2F65 STA screen_ptr + 1 ; ...screen_ptr high byte...
2F67 PLA ; ...then screen_ptr's low byte...
2F68 STA screen_ptr ; ...back in place...
2F6A PLA ; ...packed_ptr high byte...
2F6B STA packed_ptr + 1 ; ...back in place...
2F6D PLA ; ...then packed_ptr's low byte...
2F6E STA packed_ptr ; ...the caller's values again
2F70 LDX #0 ; Restore the 18 bytes at &0C00...
2F72 .restore_osfile_block←1← 2F79 BNE
PLA ; ...each from the stack, byte 0 first...
2F73 STA osfile_block,x ; ...back into the block's place
2F76 INX ; ...on to the next of the 18...
2F77 CPX #osfile_block_length ; ...all 18 bytes put back?
2F79 BNE restore_osfile_block ; No: restore the next
2F7B PLA ; Restore command_ptr...
2F7C STA command_ptr + 3 ; ...its fourth byte...
2F7E PLA ; ...then the third, pulled in reverse...
2F7F STA command_ptr + 2 ; ...back in place...
2F81 PLA ; ...then the second byte...
2F82 STA command_ptr + 1 ; ...back in place...
2F84 PLA ; ...then the first byte...
2F85 STA command_ptr ; ...the caller's zero page is intact
2F87 LDA #&14 ; VDU 20: restore the default colours, revealing the picture, and return
2F89 JMP oswrch ; ...OSWRCH returns to EXPAND's caller

Read the next packed byte

Read the byte at packed_ptr and step packed_ptr past it. Called by expand for each packed byte, and again for the length byte that follows a code-3 byte.

On EntryY0
On ExitAthe byte at packed_ptr, which advances past it
2F8C .read_packed_byte←2← 2F3E JSR← 2F4C JSR
LDA (packed_ptr),y ; Read the byte
2F8E INC packed_ptr ; Advance packed_ptr past it...
2F90 BNE read_packed_byte_done ; No carry: pointer done
2F92 INC packed_ptr + 1 ; Carry into the high byte
2F94 .read_packed_byte_done←1← 2F90 BNE
RTS ; Return with the packed byte in A

OSFILE block template

Copied to &0C00. The filename address (+0) and the low half of the load address (+2) are filled in; the high half, &FFFF, loads into the I/O processor, and exec address byte 0 (+6) tells OSFILE to use this load address. The start and end addresses (+10, +14) are unused by a load.

2F95 .osfile_block_template←1Used as index base by← 2ED2 LDA
EQUB &00, &00 ; +0 filename address (filled in)
2F97 EQUB &00, &2C, ; +2 load address (low half filled in) &FF, &FF
2F9B EQUB &00, &00, ; +6 exec address: low byte 0 means load at &00, &00 ; +2
2F9F EQUB &00, &2C, &FF, &FF ; +10 start address (unused)
2FA3 EQUB &00, &7C, &FF, &FF ; +14 end address (unused)