*EDITOR: level editor

Dunjunz, official flippy disc (Bug-Byte, 1987), Model B side

Updated 8 Nov 2026

← All Dunjunz versions

FileLoadExecLengthSHA-256
Editor&1910&239D&158E38a2e784e20c2872166311ff5a3d7d791683073e8c7b803233debffb21188020

Unpack the loaded level into the slots

Turn a loaded level file into the editor's item slots, links and maps, so that it can be drawn and edited. Called once by start_editing, after crypt_level has decrypted the file at &2700 and the Chalice has been taken off the map; it runs at &7B00, copied there by start_editing, in screen memory hidden below the 30 rows shown. Read the tables from the last entry back, putting each item in the next slot down from &4F (unpack_slot): objects 30 to 1 (the exit, 30, first, into slot &4F), skipping any whose x is &80 or more, each given its editor item by item_for_type; then doors 20 to 1, likewise skipping those off the map, each with its key in the slot just below and the pair recorded as a door-key link, from link 19 down (unpack_link); then all eight trapdoors, unchecked. At most 30 + 2 x 20 + 8 = 78 of the 80 slots are filled. Last, copy the file's wall map and object-free map into wall_map and object_free_map. Counts left for show_counts and the save checks: object_count leaves out the exit (it starts at -1), door_count counts the doors and trapdoor_count is set to 8 without looking. A key is taken from the file whether or not it is on the map. pack_level reverses the order on saving, so each load and save reverses the tables.

On ExitAcorrupted
Xcorrupted
Ycorrupted
LoadExec
1910 7B00 .unpack_level←2Referenced by← 1F38 JSRUsed as index base by← 1EC8 STA
LDA #0 ; door_count = 0...
1912 7B02 STA door_count ; ...for the count shown
1914 7B04 STA object_count ; ...and object_count = -1, as the exit is counted...
1916 7B06 DEC object_count ; ...too, so it ends as the objects besides the exit
1918 7B08 LDA #8 ; trapdoor_count = 8...
191A 7B0A STA trapdoor_count ; ...as the level file has them all
191C 7B0C LDA #max_slot ; Slots from &4F down...
191E 7B0E STA unpack_slot ; ...the next slot to fill
1921 7B11 LDA #max_link ; ...links from 19 down
1923 7B13 STA unpack_link ; ...the next link to fill
; The objects, the exit first.
1926 7B16 LDX #max_object ; For objects 30 to 1...
1928 7B18 .unpack_object←1← 7B3B BNE
LDA level_object_x,x ; ...its x: on the map?
192B 7B1B BMI next_object ; &80 or more (&FF): not in this level
192D 7B1D INC object_count ; Yes: count it...
192F 7B1F LDY unpack_slot ; ...and put it in the next slot...
1932 7B22 STA slot_x,y ; ...the object's x as slot_x...
1935 7B25 LDA level_object_y,x ; ...then its y...
1938 7B28 STA slot_y,y ; ...into the slot too
193B 7B2B LDA level_object_type,x ; ...and its type, turned into...
193E 7B2E JSR item_for_type ; ...the editor's item for it (item_types)...
1941 7B31 LDY unpack_slot ; ...back to the slot being filled...
1944 7B34 STA slot_item,y ; ...stored with it...
1947 7B37 DEC unpack_slot ; ...and move to the next slot down
194A 7B3A .next_object←1← 7B1B BMI
DEX ; Next object down...
194B 7B3B BNE unpack_object ; ...to object 1
; The doors, each with its key in the slot below and a ; door-key link.
194D 7B3D LDX #max_door ; For doors 20 to 1...
194F 7B3F .unpack_door←1← 7B8C BNE
LDA level_door_x,x ; ...its x: on the map?
1952 7B42 BMI next_door ; &80 or more (&FF): no door here
1954 7B44 INC door_count ; Yes: count it...
1956 7B46 LDA unpack_slot ; ...link it: the door's slot...
1959 7B49 LDY unpack_link ; ...in this link...
195C 7B4C STA link_door,y ; ...recorded as the link's door...
195F 7B4F SEC ; ...and the key's, the slot below...
1960 7B50 SBC #1 ; ...door slot - 1...
1962 7B52 STA link_key,y ; ...is the key's slot...
1965 7B55 DEC unpack_link ; ...and move to the next link down
1968 7B58 LDA level_door_x,x ; ...put the door in its slot...
196B 7B5B LDY unpack_slot ; ...in the slot being filled...
196E 7B5E STA slot_x,y ; ...the door's x as slot_x...
1971 7B61 LDA level_door_y,x ; ...then its y...
1974 7B64 STA slot_y,y ; ...into the slot too
1977 7B67 LDA level_door_sprite,x ; ...and its sprite...
197A 7B6A JSR item_for_type ; ...as the editor's door item...
197D 7B6D LDY unpack_slot ; ...back to the slot being filled...
1980 7B70 STA slot_item,y ; ...stored with it...
1983 7B73 DEC unpack_slot ; ...and move to the next slot down
1986 7B76 DEY ; Y = the door's slot - 1: the key's slot
1987 7B77 LDA level_key_x,x ; ...the key's x, on the map or not...
198A 7B7A STA slot_x,y ; ...into the slot below...
198D 7B7D LDA level_key_y,x ; ...then its y...
1990 7B80 STA slot_y,y ; ...stored with it, in the slot below...
1993 7B83 LDA #spr_key ; ...the key item...
1995 7B85 STA slot_item,y ; ...as the slot's item...
1998 7B88 DEC unpack_slot ; ...and move to the next slot down
199B 7B8B .next_door←1← 7B42 BMI
DEX ; Next door down...
199C 7B8C BNE unpack_door ; ...to door 1
; The trapdoors.
199E 7B8E LDX #7 ; For trapdoors 7 to 0, all of them present...
19A0 7B90 .unpack_trapdoor←1← 7BA8 BPL
LDA level_trapdoor_x,x ; ...put each in the next slot
19A3 7B93 LDY unpack_slot ; ...in the slot being filled...
19A6 7B96 STA slot_x,y ; ...the trapdoor's x as slot_x...
19A9 7B99 LDA level_trapdoor_y,x ; ...then its y...
19AC 7B9C STA slot_y,y ; ...into the slot too
19AF 7B9F LDA #3 ; ...as the trapdoor item...
19B1 7BA1 STA slot_item,y ; ...stored with it...
19B4 7BA4 DEC unpack_slot ; ...and move to the next slot down
19B7 7BA7 DEX ; ...the next trapdoor down...
19B8 7BA8 BPL unpack_trapdoor ; ...to trapdoor 0
; The wall and object-free maps.
19BA 7BAA LDX #0 ; Copy the &C9 bytes of each map
19BC 7BAC .unpack_maps←1← 7BBB BNE
LDA level_walls,x ; Wall bits from the level...
19BF 7BAF STA wall_map,x ; ...into the wall map being edited...
19C2 7BB2 LDA level_object_free,x ; ...object-free bits from the level...
19C5 7BB5 STA object_free_map,x ; ...into the map being edited...
19C8 7BB8 INX ; ...on to the next map byte...
19C9 7BB9 CPX #map_bytes ; ...for the maps' &C9 bytes...
19CB 7BBB BNE unpack_maps ; ...until both are copied
19CD 7BBD RTS ; Return: the level is in the slots, links and maps

Find the editor's item for a level type

unpack_level's lookup from the game's object type (or door sprite, &1C or &1D) to the editor's item, reading item_types the other way from pack_items. Search item_types from entry 1 for the type, keeping it in bit_mask. Nothing bounds the search: a type with no item would be matched against the bytes beyond item_types (the checksums and sprite tables), and with no match at all Y would wrap to 0 and the code would run on into &7BCE, past the copied routine, which nothing copies to after the MODE 2 clear: a zero, a BRK.

On EntryAlevel type
On ExitAitem
Yitem
BIT_MASKthe type
LoadExec
19CE 7BBE .item_for_type←2← 7B2E JSR← 7B6A JSR
STA bit_mask ; Keep the type to compare with
19D0 7BC0 LDY #1 ; From item 1 (item 0, the wall, has no type)...
19D2 7BC2 .search_types←1← 7BCC BNE
LDA item_types,y ; ...take item Y's level type...
19D5 7BC5 CMP bit_mask ; ...the type being looked up?
19D7 7BC7 BNE next_type ; No: next type
19D9 7BC9 TYA ; Yes: return the item
19DA 7BCA RTS ; ...the item number in A
19DB 7BCB .next_type←1← 7BC7 BNE
INY ; On to the next item...
19DC 7BCC BNE search_types ; ...trying every item until one matches

Unused

Leftover bytes after unpack_level's image.

19DE EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00 ; Not used

Mark the cursor's cell as holding an object

Clear its bit in the object-free map, so that the game searches its object list when a hero steps there. The byte and bit are found as wall_bit finds them, on object_free_map at &7E00 instead of wall_map. put_item calls it first for every item placed, walls, doors, keys and trapdoors included, and nothing in the editor sets a bit again, so a deleted item leaves its cell clear and the game merely searches in vain there. The map is set to all ones by start_editing and, for a loaded level, replaced by the level's own map. It runs at &7D80, copied there by start_editing, in screen memory below the 30 rows shown (&3000-&7AFF).

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAcorrupted
Xcorrupted
Y0
MAP_PTRthe cell's object-free byte
BIT_MASKevery bit but the cell's
LoadExec
1A00 7D80 .mark_object_cell←2Referenced by← 2BD7 JSRUsed as index base by← 1ED5 STA
LDA cursor_y ; Byte = (y AND &F8) x 4 + (y AND 7) + (x AND &F8)
1A02 7D82 AND #&f8 ; ...the room row, as a multiple of 8...
1A04 7D84 ASL ; ...x 2...
1A05 7D85 ASL ; ...x 4: 32 bytes per row of rooms
1A06 7D86 STA map_ptr ; ...the row of rooms' offset so far
1A08 7D88 LDA cursor_y ; ...+ (y AND 7)...
1A0A 7D8A AND #7 ; ...the cell's row within the room
1A0C 7D8C CLC ; ...C clear for the addition
1A0D 7D8D ADC map_ptr ; ...added to the offset
1A0F 7D8F STA map_ptr ; ...the offset so far
1A11 7D91 LDA cursor_x ; ...+ (x AND &F8)...
1A13 7D93 AND #&f8 ; ...the room's column x 8: its 8 bytes
1A15 7D95 CLC ; ...C clear for the addition
1A16 7D96 ADC map_ptr ; ...added to the offset
1A18 7D98 STA map_ptr ; ...map_ptr: the cell's byte
1A1A 7D9A LDA #>(object_free_map) ; ...in object_free_map, page-aligned...
1A1C 7D9C STA map_ptr_hi ; ...as map_ptr's high byte
1A1E 7D9E LDA cursor_x ; Bit 7 - (x AND 7)...
1A20 7DA0 AND #7 ; ...the cell's column within the room...
1A22 7DA2 STA bit_mask ; ...kept to subtract...
1A24 7DA4 LDA #7 ; ...from 7...
1A26 7DA6 SEC ; ...C set for the subtraction
1A27 7DA7 SBC bit_mask ; ...7 - it: the bit, leftmost cell in bit 7
1A29 7DA9 BEQ mark_bit_0 ; Bit 0: the mask is 1
1A2B 7DAB TAX ; ...the bit number as a count...
1A2C 7DAC LDA #1 ; ...from bit 0...
1A2E 7DAE .mark_shift_bit←1← 7DB0 BNE
ASL ; Shift the mask up a bit...
1A2F 7DAF DEX ; ...one shift fewer to go
1A30 7DB0 BNE mark_shift_bit ; ...until it is in place
1A32 7DB2 BEQ mark_bit_found ; ...(always branches: X reached 0)
1A34 7DB4 .mark_bit_0←1← 7DA9 BEQ
LDA #1 ; ...the bit 0 mask
1A36 7DB6 .mark_bit_found←1← 7DB2 BEQ
EOR #&ff ; Invert the mask, to clear the cell's bit...
1A38 7DB8 STA bit_mask ; ...keeping every other bit
1A3A 7DBA LDY #0 ; ...Y = 0 for (map_ptr),Y
1A3C 7DBC LDA (map_ptr),y ; ...the cell's map byte...
1A3E 7DBE AND bit_mask ; ...with the cell's bit cleared: objects may lie here
1A40 7DC0 STA (map_ptr),y ; ...back in the map
1A42 7DC2 RTS ; Return with the cell marked

Unused

Leftover bytes, among them the text PAGE.

1A43 EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, ; No &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, ; t &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, ; us &00, &00, &01, &00, &46, &42, &01, &01, &06, &06, ; ed &7C, &60, &02, &02, &00, &01, &00, &FF, &FF, &8A, &B6, &8E, &B6, &0D, &00, &00, &00, &00, &00, &00, &00, &20, &02, &43, &45, &07, &50, &41, &47, &45, &20, &02, &50, &50, &07, &0D, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &FF, &0D, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &8B, &8E, &91, &93, &95, &95, &95, &95, &95, &95, &95, &1B, &2D, &01, &1B, &2D, &00, &1B, &47, &1B, &48, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &FF, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00

BREAK intercept: start again

The MOS calls the BREAK intercept twice during a reset, first with C clear and then, once the reset is done, with C set. editor_init points the intercept here, so BREAK (or an error, since BRKV is the reset entry) while the questions are being asked returns, on the second call, to editor_init, with the editor still in memory. start_editing disarms the intercept, so BREAK while editing is an ordinary reset; save_level arms its own, break_to_save.

; BREAK intercept while choosing a level: on the second call (C ; set) start again.
1C10 .break_to_start
BCS editor_init ; C set: the reset is done, so ask again
1C12 RTS ; C clear: the first call, let the reset go on

Set up and choose a level

An editing session runs: editor_start copies the plotter page into place; editor_init (here) asks in MODE 7 whether to edit a level (E) or start a new one (N); for E, choose_filing_system selects disc (and a drive) or tape, ask_filename reads the name and the file is *LOADed at &2700; new_level, for N, prepares an empty level instead; start_editing draws the MODE 2 map, decrypting and unpacking a loaded level with crypt_level and unpack_level; edit_loop then runs until SHIFT+S, which walls the edges (wall_edges), packs the slots back into the level file (pack_level), chooses the wall block (choose_wall_block) and saves (save_level); after the save, A comes back here for another level and Q resets the machine. editor_init itself disables ESCAPE, arms the BREAK intercept with break_to_start and points BRKV at MOS 1.20's reset entry, so that BREAK or any error at the questions (a missing file, a filing system error) resets and comes back here; there is no error message. It clears level_encrypted, as no save has encrypted the level buffer yet. It sets the map cursor's keys to ',' '.' 'A' 'Z' and last_slot to &4F, sends *TV255 1 (applied by the MODE 7 that follows) and asks the first question, leaving the answer in new_level_flag (0 edit, 1 new) for start_editing. Reached from editor_start, from break_to_start and from save_level when another level is asked for.

1C13 .editor_init←3← 1C10 BCS← 2392 JMP← 23B1 JMP
LDA #osbyte_escape_break_effect ; OSBYTE &C8 with X=1, Y=0: disable ESCAPE
1C15 LDX #1 ; X = 1: ESCAPE does nothing while editing
1C17 LDY #0 ; Y = 0: replace the whole setting
1C19 JSR osbyte ; Set it, so ESCAPE cannot stop the editor osbyte: escape break effect
; Make BREAK, and any error, come back to editor_init through a ; reset.
1C1C LDA #opcode_jmp ; BREAK intercept = JMP break_to_start...
1C1E STA os_break_intercept ; Arm the intercept with a JMP...
1C21 LDA #<(break_to_start) ; ...whose target is break_to_start...
1C23 STA os_break_intercept_lo ; ...its target's low byte...
1C26 LDA #>(break_to_start) ; ...high byte of the target...
1C28 STA os_break_intercept_hi ; ...so BREAK at a prompt restarts the editor
1C2B LDA #<(mos120_reset_entry) ; BRKV = &D9CD, MOS 1.20's reset entry: an error resets
1C2D STA brkv ; ...low byte of the error handler...
1C30 LDA #>(mos120_reset_entry) ; ...high byte of the error handler...
1C32 STA brkv + 1 ; ...so any error (File not found...) resets
1C35 LDA #0 ; Not encrypted by a save: save_level need not decrypt
1C37 STA level_encrypted ; ...so the level at &2700 is plain until saved
; Settings for the editing to come: the cursor keys and the slot ; count.
1C3A LDA #inkey_key_comma ; Map cursor keys for move_cursor: ',' left...
1C3C STA key_left ; Comma moves the map cursor left
1C3E LDA #inkey_key_full_stop ; ...'.' right...
1C40 STA key_right ; Full stop moves it right
1C42 LDA #inkey_key_a ; ...'A' up...
1C44 STA key_up ; A moves it up
1C46 LDA #inkey_key_z ; ...'Z' down
1C48 STA key_down ; Z moves it down
1C4A LDA #max_slot ; 80 item slots, 0-&4F
1C4C STA last_slot ; Slots are searched from the last, &4F, down
; The MODE 7 question screen: edit or new?
1C4E LDX #<(tv_command) ; *TV255 1: shift the picture a line, interlace off
1C50 LDY #>(tv_command) ; ...high byte of the command
1C52 JSR oscli ; ...taking effect at the MODE 7 change below
1C55 LDA #3 ; VDU 3: printer off
1C57 JSR oswrch ; Stop any printer output
1C5A LDA #&16 ; VDU 22,7: MODE 7
1C5C JSR oswrch ; Send the mode change...
1C5F LDA #7 ; ...for MODE 7...
1C61 JSR oswrch ; ...where the questions are asked
1C64 LDX #<(edit_or_new_prompt) ; Ask: edit (E) or new level (N)
1C66 LDY #>(edit_or_new_prompt) ; ...high byte of the prompt
1C68 LDA #&22 ; Internal key &22, E: answer 0, edit...
1C6A STA scratch ; ...as ask_key's first key
1C6C LDA #&55 ; Internal key &55, N: answer 1, new level...
1C6E STA scratch_hi ; ...as ask_key's second key
1C70 LDA #0 ; A = 0: take one of the two keys
1C72 JSR ask_key ; Ask, and get 0 for edit or 1 for new
1C75 STA new_level_flag ; Remember the choice for start_editing: 1 for new
1C78 BEQ edit_level ; E: edit a level file
1C7A JMP new_level ; N: start with an empty level
; Edit: choose the filing system, read a filename into ; load_command and *LOAD the level at &2700.
1C7D .edit_level←1← 1C78 BEQ
JSR new_lines ; Leave a blank line under the question
1C80 JSR choose_filing_system ; Choose disc or tape (and the drive)
1C83 LDA #' ' ; Blank the filename in load_command...
1C85 LDX #5 ; From the name field at load_command+5...
1C87 .blank_load_name←1← 1C8D BNE
STA load_command,x ; ...put a space...
1C8A INX ; ...in each character...
1C8B CPX #&0c ; ...up to load_command+11...
1C8D BNE blank_load_name ; ...so no longer earlier name is left in it
1C8F JSR ask_filename ; Read the filename (L if none is typed)
1C92 LDA #&0a ; LF, below the line ask_filename ended
1C94 JSR oswrch ; Leave a blank line under the typed name
1C97 LDA #' ' ; A space at the name's last character...
1C99 STA load_command + 11 ; ...already blank: the space at +12 ends the name
1C9C LDX #0 ; Copy the filename typed into load_command...
1C9E .copy_load_name←1← 1CA7 BNE
LDA filename_buffer,x ; Take a character of the typed name...
1CA1 STA load_command + 5,x ; ...into the command's name field...
1CA4 INX ; ...the next character...
1CA5 CPX scratch ; ...for the name's length, without its CR...
1CA7 BNE copy_load_name ; ...until the name is copied
1CA9 LDX #<(load_command) ; *LOAD name 2700
1CAB LDY #>(load_command) ; ...high byte of the command
1CAD JSR oscli ; Load the level file at &2700, still encrypted
1CB0 JMP start_editing ; Edit it: start_editing decrypts and draws it

Print a prompt and wait for one of its keys

The editor's one-keypress questions: editor_init's edit or new, choose_filing_system's disc or tape and its drive, and save_level's another level or quit. Print the text at XY up to (not including) its '*', flush the input buffer and scan the keyboard with OSBYTE &7A until an answer is held, ignoring every other key. With A=0 the answers are the two keys whose internal numbers are in scratch and scratch_hi, and A returns 0 for the first or 1 for the second, nothing being echoed. Otherwise the answers are the digits 0-3 (their internal numbers in drive_keys), and the digit is echoed and returned as a character, ready for drive_command. The flush runs with X still holding the prompt's low byte, which is non-zero for every prompt, so only the input buffer is flushed. Because the keys are read by scanning, the key pressed is left in the input buffer; ask_filename flushes it before reading a name.

On EntryA0 for two keys, else a drive digit
Xprompt address, low byte
Yprompt address, high byte
SCRATCHfirst key's internal number (A=0)
SCRATCH_HIsecond key's internal number (A=0)
On ExitA0 or 1 for the key pressed (A=0 on entry); else the digit's character
Ythe digit, 0-3 (A non-zero on entry)
1CB3 .ask_key←4← 1C72 JSR← 2113 JSR← 2128 JSR← 238D JSR
PHA ; Keep the mode: 0 for two answer keys, else digits
1CB4 STX plot_x ; Point plot_x/plot_y at the prompt...
1CB6 STY plot_y ; ...high byte: the prompt's address
1CB8 LDY #0 ; Print it up to the '*'...
1CBA .print_prompt←1← 1CC4 BNE
LDA (plot_x),y ; Take a character of the prompt...
1CBC JSR oswrch ; ...print it on the MODE 7 screen...
1CBF INY ; ...move to the next...
1CC0 LDA (plot_x),y ; ...and look ahead at it:
1CC2 CMP #'*' ; The '*' ends the prompt...
1CC4 BNE print_prompt ; ...so print until it
1CC6 LDA #osbyte_flush_buffer_class ; OSBYTE &0F, X = prompt low byte (non-zero): input
1CC8 JSR osbyte ; Discard keys pressed before the prompt Flush selected class of buffer
1CCB .scan_keys←2← 1CDF BMI← 1CF1 BNE
LDA #osbyte_scan_keyboard_from_16 ; OSBYTE &7A: scan the keyboard from key 16...
1CCD JSR osbyte ; ...X = the internal number of a key held, or &FF Scan keyboard from key 16
1CD0 PLA ; Mode kept on the stack: two keys or digits?
1CD1 PHA ; ...keeping the mode for the next pass
1CD2 BEQ check_two_keys ; A = 0: compare with the two answer keys
1CD4 LDY #3 ; Digits: is it 0, 1, 2 or 3?
1CD6 .check_digit←1← 1CDD BPL
TXA ; The internal key number pressed...
1CD7 CMP drive_keys,y ; ...against digit Y's key...
1CDA BEQ digit_pressed ; Match: digit Y was pressed
1CDC DEY ; No: try the digit below...
1CDD BPL check_digit ; ...down to 0
1CDF BMI scan_keys ; Not a drive digit: scan again
1CE1 .digit_pressed←1← 1CDA BEQ
PLA ; Drop the kept mode
1CE2 TYA ; Digit character = '0' + Y, the digit's value...
1CE3 CLC ; ...C clear for the addition...
1CE4 ADC #'0' ; ...as its character...
1CE6 JSR oswrch ; ...echoed after the prompt
1CE9 RTS ; Return the digit's character, for *DRIVE
1CEA .check_two_keys←1← 1CD2 BEQ
TXA ; The first key?
1CEB CMP scratch ; Is it the first answer's key...
1CED BEQ first_key ; Yes: first key
1CEF CMP scratch_hi ; The second key?
1CF1 BNE scan_keys ; Neither answer: scan again
1CF3 PLA ; Second: drop the kept mode
1CF4 LDA #1 ; The second answer: 1
1CF6 RTS ; Return the answer in A
1CF7 .first_key←1← 1CED BEQ
PLA ; First: drop the kept mode
1CF8 LDA #0 ; The first answer: 0
1CFA RTS ; Return the answer in A

Prompts and commands

The MODE 7 questions, the OSWORD 0 buffer and block, and the * commands passed to OSCLI. A prompt for ask_key ends with '*', which is not printed; filename_prompt and level_number_prompt end with a zero, printed by their callers; commands end with CR. load_command and save_command hold a filename field at +5 that editor_init and save_level blank and fill from filename_buffer; drive_command's digit is filled in by choose_filing_system. The filename fields and buffers start out holding leftover text and asterisks.

1CFB .edit_or_new_prompt
EQUS "Edit (E) or start new level (N) *" ; editor_init's first question, for ask_key
1D1C .disc_or_tape_prompt
EQUS "Disc (D) or tape (T) *" ; choose_filing_system's question, for ask_key
1D32 .filename_prompt←1Used as index base by← 1E3D LDA
EQUS "Filename " ; Printed by ask_filename; the name is typed after it
1D3B EQUB &00 ; Zero: the end
1D3C .drive_prompt
EQUS "Which drive *" ; Asked after *DISC; answered with a digit 0-3
; *DRIVE n, with the digit at +5.
1D49 .drive_command
EQUS "DRIVE"
1D4E .drive_digit←1← 212B STA
EQUB &20 ; The drive digit, '0'-'3', from ask_key
1D4F EQUB &0D ; CR: the end of the command
; OSWORD 0's buffer: a filename (up to 7 characters) or a level ; number's digits.
1D50 .filename_buffer←6Referenced by← 1E84 STA← 2283 STA← 22B5 LDA← 22BD LDAUsed as index base by← 1C9E LDA← 2368 LDA
EQUB &2A, &2A, &2A, &2A, &2A, &2A, &2A, &2A
; OSWORD 0 block: buffer address, maximum length, lowest and ; highest character, filled in before each call.
1D58 .read_line_block←2← 1E4E STA← 228B STA
EQUB &2A, &2A, &2A, &2A, &2A
; *LOAD name 2700, the name at +5.
1D5D .load_command←1Used as index base by← 1C87 STA
EQUS "LOAD LEVEL11 2700", &0D
1D6F .drive_0_command
EQUS "DRIVE0", &0D ; *DRIVE 0: edit_loop's, so Stones loads from drive 0
1D76 .disc_command
EQUS "DISC", &0D ; *DISC: choose_filing_system's D, and for Stones
1D7B .tv_command
EQUS "TV255 1", &0D ; *TV255 1: editor_init's, before its MODE 7
1D83 .tape_command
EQUS "TAPE", &0D ; *TAPE: choose_filing_system's T answer
1D88 .cat_command
EQUS "CAT", &0D ; *CAT: save_level's, when no level is typed
; VDU 31 and the labels of the three counts: Objs, Mons (the ; trapdoors, where monsters appear) and Dors.
1D8C .count_labels←1Used as index base by← 219A LDA
EQUB &1F, &10, &00 ; VDU 31,16,0: TAB(16,0)
1D8F EQUS "Objs"
1D93 EQUB &1F, &10, &04 ; VDU 31,16,4: TAB(16,4)
1D96 EQUS "Mons"
1D9A EQUB &1F, &10, &08 ; VDU 31,16,8: TAB(16,8)
1D9D EQUS "Dors"
1DA1 EQUB &0D ; CR: the end
; VDU 23,0,10,32 (no cursor), VDU 23,0,6,30 (30 rows), VDU ; 19,10,1 and VDU 19,9,3.
1DA2 .edit_screen_vdu←1Used as index base by← 2140 LDA
EQUB &17, &00, &0A, &20, ; VDU 23,0,10,32: 6845 register 10 &00, &00, &00, &00, ; = &20, no cursor &00, &00
1DAC EQUB &00 ; VDU 0: does nothing
1DAD EQUB &17, &00, &06, &1E, ; VDU 23,0,6,30: 6845 register 6 = &00, &00, &00, &00, ; 30 character rows displayed &00, &00
1DB7 EQUB &13, &0A, &01, ; VDU 19,10,1: logical colour 10 shows &00, &00, &00 ; red
1DBD EQUB &13, &09, &03, ; VDU 19,9,3: logical colour 9 shows &00, &00, &00 ; yellow

Unused

Four asterisks, not sent.

1DC3 .edit_screen_vdu_end
EQUB &2A, &2A, &2A, &2A ; Not used
1DC7 .level_number_prompt←1Used as index base by← 2272 LDA
EQUS "Level in the Dunjunz complex ", &00 ; Asked when saving: the level number
; *SAVE name 2700+2B0, the name at +5.
1DE5 .save_command←1Used as index base by← 2356 STA
EQUS "SAVE LEVEL22 2700+2B0", &0D ; *SAVE the &2B0-byte level file from &2700
1DFB .another_prompt
EQUS "Another level (A) or quit (Q) *" ; Asked after a save, for ask_key

Print a new line and a blank line

CR, LF, LF: ends the current line and leaves a blank one, spacing out the MODE 7 questions. Called by editor_init, ask_filename, choose_filing_system and save_level.

On ExitAcorrupted
Xpreserved
Ypreserved
1E1A .new_lines←6← 1C7D JSR← 1E38 JSR← 211F JSR← 23D2 JSR← 23D8 JSR← 23E2 JSR
LDA #&0d ; CR: back to the start of the line...
1E1C JSR oswrch ; ...sent
1E1F LDA #&0a ; LF, sent twice...
1E21 JSR oswrch ; ...down one line...
1E24 JSR oswrch ; ...and another, leaving a blank line
1E27 RTS ; Return, the cursor two lines down at the left

Ask for a filename

Ask for the name of the level file, for editor_init (to load) and save_level (to save). Pause for 65536 passes of a delay loop, then flush every buffer, so the key that answered ask_key's last question (still in the input buffer) is not taken as the start of the name. Print 'Filename ' and read up to seven characters, space to '~', into filename_buffer with OSWORD 0. If only RETURN is typed, go back up onto the prompt line, print 'L' after the prompt as if it had been typed, and use the one-letter name L. ESCAPE is disabled, so OSWORD 0's ESCAPE exit is not checked.

On ExitSCRATCHthe name's length, without its CR (1-7)
FILENAME_BUFFERthe name
1E28 .ask_filename←2← 1C8F JSR← 235E JSR
LDX #0 ; Pause for 256 x 256 loops...
1E2A .pause_outer←1← 1E31 BNE
LDY #0 ; Each outer pass...
1E2C .pause_inner←1← 1E2E BNE
NOP ; ...a NOP to waste time...
1E2D DEY ; ...counted down from 256...
1E2E BNE pause_inner ; ...in the inner loop...
1E30 DEX ; ...for 256 outer passes...
1E31 BNE pause_outer ; ...a short wait after the last answer
1E33 LDA #osbyte_flush_buffer_class ; Flush buffers (X = 0: all)
1E35 JSR osbyte ; Drop the answer key, so it is not read as the name Flush selected class of buffer
1E38 JSR new_lines ; Leave a blank line before the prompt
1E3B LDX #0 ; Print the filename prompt, up to and including its zero
1E3D .print_filename_prompt←1← 1E46 BNE
LDA filename_prompt,x ; Take a character of 'Filename '...
1E40 JSR oswrch ; ...print it...
1E43 INX ; ...move to the next...
1E44 CMP #0 ; ...the zero ends it...
1E46 BNE print_filename_prompt ; ...and is printed too
1E48 LDX #<(read_line_block) ; Point XY at read_line_block...
1E4A LDY #>(read_line_block) ; ...high byte of the block
1E4C LDA #<(filename_buffer) ; ...and fill it in: the buffer is filename_buffer...
1E4E STA read_line_block ; ...low byte of the buffer...
1E51 LDA #>(filename_buffer) ; ...high byte of the buffer...
1E53 STA read_line_block + 1 ; ...stored at +1
1E56 LDA #7 ; ...at most 7 characters...
1E58 STA read_line_block + 2 ; ...a DFS filename's length
1E5B LDA #' ' ; ...characters from space (lowest accepted)...
1E5D STA read_line_block + 3 ; ...lowest character accepted...
1E60 LDA #'~' ; ...to '~' (highest accepted)...
1E62 STA read_line_block + 4 ; ...highest character accepted
1E65 LDA #osword_read_line ; OSWORD 0: read the name
1E67 JSR osword ; Read the filename the user types Read line from input stream (exits with C=1 if ESCAPE pressed)
1E6A STY scratch ; Keep its length, not counting the CR
1E6C CPY #0 ; Length 0, only RETURN typed?
1E6E BNE filename_read ; A name was typed: return
1E70 LDA #&0b ; Only RETURN: VDU 11, up a line...
1E72 JSR oswrch ; ...back onto the prompt line
1E75 LDA #9 ; ...and VDU 9, right...
1E77 LDY #9 ; ...nine times, the length of 'Filename '...
1E79 .move_right←1← 1E7D BNE
JSR oswrch ; ...one step right...
1E7C DEY ; ...counting the steps...
1E7D BNE move_right ; ...until the cursor is past 'Filename '
1E7F LDA #'L' ; ...then print 'L' there...
1E81 JSR oswrch ; ...as if typed
1E84 STA filename_buffer ; ...and use it as the name
1E87 INY ; Length 1 (Y is 0 after the loop)
1E88 STY scratch ; ...as the name's length
1E8A JSR osnewl ; New line, as RETURN gave before
1E8D .filename_read←1← 1E6E BNE
RTS ; Return the name in filename_buffer, length in scratch

Start a new, empty level

Reached from editor_init when N is chosen. Fill the level's wall block (&30 bytes at &2980) with &03, then fall into start_editing, which sees new_level_flag set and starts from an empty map. Nothing reads the filler: the editor draws walls with its own sprite, and choose_wall_block always replaces the block with one from Stones before the level is saved. Nothing else of the level file is prepared here; start_editing clears its object, door and key tables, and pack_level packs the slots and maps into it when saving.

On ExitAcorrupted
Xcorrupted
Ypreserved
1E8E .new_level←1← 1C7A JMP
LDA #3 ; Fill level_wall_block with &03...
1E90 LDX #wall_block_bytes - 1 ; ...for all &30 bytes...
1E92 .fill_wall_block←1← 1E96 BPL
STA level_wall_block,x ; ...a filler, replaced before saving...
1E95 DEX ; ...from the last byte...
1E96 BPL fill_wall_block ; ...to the first, then fall into start_editing
fall through ↓

Set up the editing screen and edit

Turn the level at &2700 (or nothing, for a new level) into the editor's own form and draw it, ready for edit_loop. Reached from editor_init once a level file is loaded, and from new_level; new_level_flag says which. The editor keeps a level as 80 item slots (slot_x, slot_y, slot_item at &0C00), its own wall_map (&2F00) and object_free_map (&7E00), and the door-key links; the level file is only read here and written by pack_level. First, an empty editor: zero the wall map and every slot (a free slot is item 0, a wall, at (0, 0)), zero cursor_flash and link_index, select MODE 2 and disarm the BREAK intercept. The MODE 2 change clears &3000-&7FFF, so only then are unpack_level and mark_object_cell copied to &7B00 and &7D80 and the object-free map at &7E00 set to &FF (no object anywhere): set_edit_screen shows just 30 character rows, &3000-&7AFF, leaving &7B00-&7FFF as hidden workspace. Free all 20 links, note no door waiting for its key, set GCOL 3,3 (exclusive-OR, so a link line drawn twice is erased), zero the four counts and turn cursor editing off. For a loaded level: decrypt it with crypt_level, take an object of type &73 (the Chalice, which only level 25 has and the editor has no item for) off the map, unpack the tables into the slots and maps with unpack_level, show the labels and counts and plot the item in every one of the 32 x 48 cells that has one. exit_placed is then set without checking that the file's exit was on the map (unpack_level has already set trapdoor_count to 8 the same way). For both: set the level file's objects 1-29, doors and keys to &FF, so that pack_level need write only the items placed; show the labels and counts (a loaded level's for the second time); put the cursor at (11, 11), the top-left of the heroes' four start cells, with the wall as the current item, the slow cursor on and colour_10 at 1 (red, as set_edit_screen set it), and fall into edit_loop.

1E98 .start_editing←1← 1CB0 JMP
LDA #0 ; Zero all of &2F00-&2FFF, wall_map and beyond...
1E9A TAX ; ...from offset 0...
1E9B .clear_walls←1← 1E9F BNE
STA wall_map,x ; ...no walls in this byte...
1E9E DEX ; ...the next byte down...
1E9F BNE clear_walls ; ...through all 256
1EA1 .clear_slots←1← 1EA5 BNE
STA slot_x,x ; ...and the item slots (&0C00-&0CFF)
1EA4 DEX ; ...one byte of the slot tables...
1EA5 BNE clear_slots ; ...through all 256: every slot free, item 0
1EA7 LDA #0 ; Cursor flash phase 0...
1EA9 STA cursor_flash ; ...so the first wall pass plots the cursor
1EAC LDA #0 ; Door-key link 0...
1EAE STA link_index ; ...is the first to be made
; The MODE 2 map screen, which also clears &3000-&7FFF. BREAK ; from here on is an ordinary reset.
1EB1 LDA #&16 ; VDU 22,2: MODE 2
1EB3 JSR oswrch ; Send the mode change...
1EB6 LDA #2 ; ...for MODE 2...
1EB8 JSR oswrch ; ...the 16-colour screen the map is drawn on
1EBB LDA #0 ; Not a JMP: disarm the BREAK intercept...
1EBD STA os_break_intercept ; ...so BREAK while editing is an ordinary reset
1EC0 JSR set_edit_screen ; No cursor, 30 rows, colours 10 and 9
; Into the hidden screen memory above the 30 rows shown: the two ; routines, then an empty object-free map and door-key links.
1EC3 LDX #0 ; Copy unpack_level to &7B00...
1EC5 .copy_unpack←1← 1ECE BNE
LDA unpack_level_image,x ; Take a byte of the stored routine...
1EC8 STA unpack_level,x ; ...to &7B00, where it runs...
1ECB INX ; ...on to the next byte...
1ECC CPX #&ce ; ...for unpack_level's &CE bytes...
1ECE BNE copy_unpack ; ...until it is all copied
1ED0 LDX #0 ; ...and mark_object_cell to &7D80
1ED2 .copy_mark_object_cell←1← 1EDB BNE
LDA mark_object_cell_image,x ; Take a byte of the stored routine...
1ED5 STA mark_object_cell,x ; ...to &7D80, where it runs...
1ED8 INX ; ...on to the next byte...
1ED9 CPX #&43 ; ...for mark_object_cell's &43 bytes...
1EDB BNE copy_mark_object_cell ; ...until it is all copied
1EDD LDA #&ff ; Fill &7E00-&7EFF, object_free_map, with &FF...
1EDF LDX #0 ; ...from offset 0...
1EE1 .clear_free_map←1← 1EE5 BNE
STA object_free_map,x ; ...set every bit: no object anywhere...
1EE4 DEX ; ...the next byte down...
1EE5 BNE clear_free_map ; ...through all 256
1EE7 .clear_links←1← 1EF0 BNE
STA link_door,x ; ...and &FF: all 20 door-key links unused
1EEA STA link_key,x ; ...no key for it...
1EED INX ; ...the next link...
1EEE CPX #max_link + 1 ; ...all 20 of them...
1EF0 BNE clear_links ; ...until every link is free
1EF2 STA pending_door_item ; &FF: no door waiting for its key
; Drawing mode, counts and keys for editing.
1EF5 LDA #&12 ; VDU 18,3,3: GCOL 3,3 (exclusive-OR, for the link lines)
1EF7 JSR oswrch ; Send VDU 18, GCOL...
1EFA LDA #3 ; ...mode 3: exclusive-OR, so a line...
1EFC JSR oswrch ; ...drawn twice is erased...
1EFF JSR oswrch ; ...in colour 3
1F02 LDA #0 ; No trapdoors, objects, exit or doors
1F04 STA trapdoor_count ; No trapdoors yet
1F06 STA door_count ; No doors yet
1F08 STA exit_placed ; No exit yet
1F0A STA object_count ; No objects yet
1F0C LDA #osbyte_set_cursor_editing ; OSBYTE 4,1: cursor editing off...
1F0E LDX #1 ; ...X = 1: the arrow keys give codes &88-&8B
1F10 JSR osbyte ; Set it; the editor reads the arrow keys by scanning Set cursor editing keys
1F13 LDA new_level_flag ; new_level_flag: editor_init's answer, 1 for N
1F16 BEQ unpack_loaded ; 0, a loaded level: decrypt, unpack and draw it
1F18 LDA #0 ; Yes: link_index = 0...
1F1A STA link_index ; ...again (it was set above)
1F1D BEQ clear_level_tables ; ...and clear the level tables (always branches)
; A loaded level: decrypt, drop the Chalice, unpack.
1F1F .unpack_loaded←1← 1F16 BEQ
JSR crypt_level ; Decrypt the loaded level in place at &2700
1F22 LDX #max_object - 1 ; Find an object of type &73 (the Chalice)...
1F24 .find_chalice←1← 1F2C BNE
LDA level_object_type,x ; This object's type...
1F27 CMP #game_chalice_type ; ...is it &73, the Chalice?
1F29 BEQ drop_chalice ; Yes: take it off the map
1F2B DEX ; No: the next object down...
1F2C BNE find_chalice ; ...to object 1 (not the exit, 30)
1F2E BEQ draw_loaded ; No Chalice: unpack the level as it is
1F30 .drop_chalice←1← 1F29 BEQ
LDA #off_map ; Drop it: x and y &FF
1F32 STA level_object_x,x ; ...so unpack_level skips it: the editor...
1F35 STA level_object_y,x ; ...has no item for the Chalice
1F38 .draw_loaded←1← 1F2E BEQ
JSR unpack_level ; Unpack the level into the slots and maps
1F3B JSR show_labels ; Show the labels...
1F3E JSR show_counts ; ...and the unpacked level's counts
; Plot every cell of the unpacked level.
1F41 LDA #0 ; Draw every cell: rows 0-47...
1F43 STA cursor_y ; ...from the top row
1F45 .draw_row←1← 1F61 BNE
LDA #0 ; ...columns 0-31...
1F47 STA cursor_x ; ...from the left column
1F49 .draw_cell←1← 1F59 BNE
JSR item_at_cursor ; ...the item in this cell, if any
1F4C CMP #&ff ; &FF: nothing here
1F4E BEQ next_cell ; Empty: nothing to plot
1F50 JSR plot_at_cursor ; Plot the item's sprite in the cell
1F53 .next_cell←1← 1F4E BEQ
INC cursor_x ; Step the cursor right a cell
1F55 LDA cursor_x ; Which column now?
1F57 CMP #map_width ; Past the last column?
1F59 BNE draw_cell ; No: draw the next cell
1F5B INC cursor_y ; Row done: step the cursor down a row
1F5D LDA cursor_y ; Which row now?
1F5F CMP #map_height ; Past the last row?
1F61 BNE draw_row ; No: draw the next row
1F63 LDA #1 ; Take a loaded level to have its exit, unchecked...
1F65 STA exit_placed ; ...which SHIFT+S needs before it saves
; Both: empty the level file's object, door and key tables, for ; pack_level to fill.
1F67 .clear_level_tables←1← 1F1D BEQ
LDA #off_map ; Clear the level file's object tables...
1F69 LDX #max_object - 1 ; ...objects 29 to 1 (the exit is 30)...
1F6B .clear_object_entries←1← 1F72 BNE
STA level_object_x,x ; ...off the map...
1F6E STA level_object_y,x ; ...both x and y...
1F71 DEX ; ...the next object down...
1F72 BNE clear_object_entries ; ...until all are cleared
1F74 LDX #max_door ; ...and the door and key tables, for pack_level
1F76 .clear_door_entries←1← 1F83 BNE
STA level_door_x,x ; ...door off the map...
1F79 STA level_door_y,x ; ...its y too...
1F7C STA level_key_x,x ; ...its key off the map...
1F7F STA level_key_y,x ; ...and its y
1F82 DEX ; ...the next door down...
1F83 BNE clear_door_entries ; ...until doors 20 to 1 are cleared
; The side panel, and the cursor's starting state.
1F85 JSR show_labels ; Show the labels (again, for a loaded level)...
1F88 JSR show_counts ; ...and the counts
1F8B LDA #&0b ; Cursor at (11, 11)...
1F8D STA cursor_x ; ...as cursor_x, column 11...
1F8F STA cursor_y ; ...row 11: top-left of the heroes' start
1F91 LDA #0 ; Current item 0, so SPACE places walls...
1F93 STA current_item ; ...item 0, the wall
1F95 LDA #1 ; Slow cursor on, colour 10 = 1
1F97 STA slow_cursor ; Move the cursor every third frame
1F9A STA colour_10 ; Logical colour 10 shows red, as set above
1F9D LDA #osbyte_set_cursor_editing ; OSBYTE 4,1 again...
1F9F LDX #1 ; ...X = 1: cursor editing off...
1FA1 JSR osbyte ; ...a repeat of the call above Set cursor editing keys
fall through ↓

The editing loop

The editor proper: start_editing falls into it with the map drawn, and it loops for good, until SHIFT+S saves the level. Each pass reads the keys in this order:

  • f0 toggles the slow cursor (inline here). With it on, each pass first waits three frames, so the cursor steps at most one cell every four frames (move_cursor waits for one more).
  • SHIFT+S saves, but only once the exit, all eight trapdoors and a key for every door (check_door_keys) are placed: *DISC and *DRIVE 0, wall_edges, pack_level, choose_wall_block (from Stones on drive 0), MODE 7, then save_level, which never returns.
  • ',' '.' 'A' 'Z' move the cursor (move_cursor). With walls chosen, the cursor sprite flashes over the wall sprite on alternate passes.
  • Cursor up and cursor down step logical colour 10, the walls' colour on screen, between 1 and 7 (set_colour_10), one step a press.
  • '1' and '2' step the current item down and up, 0 (wall) to &0F (key), once a pass while held; show_cursor_position prints the cursor's cell first.
  • SPACE places the current item (place_item). Walls go anywhere and repeat while SPACE is held; other items are refused on the map's edge, and one goes down per press, followed by show_counts.
  • TAB deletes what is under the cursor (delete_item), then show_counts. The wait that follows tests B (&9B), not TAB (&9F), so holding TAB deletes again on every pass, wherever the cursor goes, and holding B with an item chosen stalls the editor.
  • CTRL shows the door-key links (draw_links) while held, and drawing them again on release rubs them out.

SPACE with walls, and SPACE refused at the edge, start the next pass at once, skipping TAB and CTRL.

1FA4 .edit_loop←2← 208F JMP← 20DF JMP
LDA slow_cursor ; Slow cursor on?
1FA7 BEQ check_f0 ; No: run the pass at once
1FA9 LDX #3 ; Yes: wait for three frames first
1FAB .slow_wait←1← 1FB5 BNE
TXA ; Keep the frames left to wait...
1FAC PHA ; ...on the stack, as OSBYTE corrupts X
1FAD LDA #osbyte_vsync ; OSBYTE &13: wait for the next frame
1FAF JSR osbyte ; Slow cursor: a step every fourth frame Wait for vertical sync
1FB2 PLA ; Recover the frames left to wait...
1FB3 TAX ; ...into X, the countdown
1FB4 DEX ; One frame fewer to wait
1FB5 BNE slow_wait ; Wait again until three have passed
; f0: toggle the slow cursor, once per press.
1FB7 .check_f0←1← 1FA7 BEQ
LDX #inkey_key_f0 ; Test f0 (INKEY -33)...
1FB9 JSR key_pressed ; ...is it held?
1FBC BNE check_save ; No: on to SHIFT+S
1FBE LDA slow_cursor ; Yes: toggle the slow cursor...
1FC1 EOR #1 ; ...between on (1) and off (0)...
1FC3 STA slow_cursor ; ...and keep the new setting
1FC6 .wait_f0_released←1← 1FCB BEQ
LDX #inkey_key_f0 ; Wait for f0 to be released...
1FC8 JSR key_pressed ; ...so one press toggles once
1FCB BEQ wait_f0_released ; Still held: keep waiting
; SHIFT+S: save, if the level is complete.
1FCD .check_save←1← 1FBC BNE
LDX #inkey_key_s ; Test S, for SHIFT+S...
1FCF JSR key_pressed ; ...is it held?
1FD2 BNE edit_keys ; No: on to the editing keys
1FD4 LDX #inkey_key_shift ; ...then test SHIFT...
1FD6 JSR key_pressed ; ...is it held as well?
1FD9 BNE edit_keys ; No: on to the editing keys
1FDB LDA exit_placed ; Yes: is the exit placed?
1FDD BEQ edit_keys ; No: cannot save yet
1FDF JSR check_door_keys ; Does every door have its key?
1FE2 BNE edit_keys ; No: cannot save yet
1FE4 LDA trapdoor_count ; How many trapdoors are placed?
1FE6 CMP #8 ; Exactly 8, the game's number?
1FE8 BNE edit_keys ; No: cannot save yet
1FEA LDX #<(disc_command) ; Complete: *DISC, as Stones is read from disc...
1FEC LDY #>(disc_command) ; ...(command's high byte)
1FEE JSR oscli ; ...select the disc filing system
1FF1 LDX #<(drive_0_command) ; ...then *DRIVE 0...
1FF3 LDY #>(drive_0_command) ; ...(command's high byte)
1FF5 JSR oscli ; ...select drive 0, where Stones is
1FF8 JSR wall_edges ; Wall in the map's four edges
1FFB JSR pack_level ; Pack the slots and maps into the level file
1FFE JSR choose_wall_block ; Choose its wall block from Stones
2001 LDA #&16 ; VDU 22,7: MODE 7 for the save prompts...
2003 JSR oswrch ; ...send VDU 22, select a mode...
2006 LDA #7 ; ...mode 7, the text screen...
2008 JSR oswrch ; ...send the mode number
200B JSR save_level ; Save it: ends by restarting the editor or resetting
200E RTS ; Not reached: save_level never returns
; Move the cursor; with walls chosen, flash it.
200F .edit_keys←5← 1FD2 BNE← 1FD9 BNE← 1FDD BEQ← 1FE2 BNE← 1FE8 BNE
JSR move_cursor ; Move the cursor, redrawing the cell it leaves
2012 LDA current_item ; Placing walls (item 0)?
2014 BNE check_cursor_up ; No: the item itself shows as the cursor
2016 LDA cursor_flash ; Yes: flash the cursor sprite over the wall...
2019 EOR #1 ; ...toggling cursor_flash each pass...
201B STA cursor_flash ; ...and keeping the new phase
201E BEQ check_cursor_up ; Off this pass: move_cursor's wall sprite shows
2020 LDA #spr_cursor ; On this pass: the cursor sprite...
2022 JSR plot_at_cursor ; ...over the wall move_cursor drew
; Cursor up and down: step the walls' colour on screen.
2025 .check_cursor_up←2← 2014 BNE← 201E BEQ
LDX #inkey_key_up ; Cursor up (INKEY -58)?
2027 JSR key_pressed ; ...(Z set if pressed)
202A BNE check_cursor_down ; Not pressed: try cursor down
202C INC colour_10 ; Yes: walls' colour one up, to at most 7...
202F LDA colour_10 ; ...read the new colour...
2032 CMP #8 ; ...past 7 (white)?
2034 BNE set_colour_up ; No: show the new colour
2036 DEC colour_10 ; Yes: back to 7, white
2039 .set_colour_up←1← 2034 BNE
JSR set_colour_10 ; Show the walls in colour_10's colour
203C .wait_up_released←1← 2041 BEQ
LDX #inkey_key_up ; Wait for cursor up to be released...
203E JSR key_pressed ; ...(Z set while held)
2041 BEQ wait_up_released ; Still held: keep waiting, one step a press
2043 .check_cursor_down←1← 202A BNE
LDX #inkey_key_down ; Cursor down (INKEY -42)?
2045 JSR key_pressed ; ...(Z set if pressed)
2048 BNE check_item_keys ; Not pressed: on to the item keys
204A DEC colour_10 ; Yes: walls' colour one down, to at least 1...
204D BNE set_colour_down ; Still 1 or more: show the new colour
204F INC colour_10 ; Down to 0 (black): back to 1, red
2052 .set_colour_down←1← 204D BNE
JSR set_colour_10 ; Show the walls in colour_10's colour
2055 .wait_down_released←1← 205A BEQ
LDX #inkey_key_down ; Wait for cursor down to be released...
2057 JSR key_pressed ; ...(Z set while held)
205A BEQ wait_down_released ; Still held: keep waiting, one step a press
; '1' and '2': choose the item.
205C .check_item_keys←1← 2048 BNE
JSR show_cursor_position ; Print the cursor's x and y beside the map
205F LDX #inkey_key_1 ; '1': the previous item?
2061 JSR key_pressed ; ...(Z set if pressed)
2064 BNE check_next_item ; Not pressed: try '2'
2066 DEC current_item ; Yes: the previous item, to at least 0
2068 BPL check_space ; Still 0 or more: on to SPACE
206A LDA #0 ; Below item 0: stay at item 0...
206C STA current_item ; ...the wall, the first item
206E BEQ check_space ; On to SPACE (always branches)
2070 .check_next_item←1← 2064 BNE
LDX #inkey_key_2 ; '2': the next item?
2072 JSR key_pressed ; ...(Z set if pressed)
2075 BNE check_space ; Not pressed: on to SPACE
2077 INC current_item ; Yes: the next item, to at most &0F
2079 LDA current_item ; ...read the new item...
207B CMP #spr_blank ; ...past the last item, the key (&0F)?
207D BNE check_space ; No: keep the new item
207F DEC current_item ; Yes: back to the key
; SPACE: place the item.
2081 .check_space←4← 2068 BPL← 206E BEQ← 2075 BNE← 207D BNE
LDX #inkey_key_space ; SPACE: place the item?
2083 JSR key_pressed ; ...(Z set if pressed)
2086 BNE check_tab ; Not pressed: try TAB
2088 LDA current_item ; Placing walls (item 0)?
208A BNE place_not_edge ; No: an item, which may not go on the edge
208C JSR place_item ; Yes: place a wall, anywhere
208F JMP edit_loop ; Next pass: walls repeat while SPACE is held
2092 .place_not_edge←1← 208A BNE
LDA cursor_x ; Other items: not on the edges, which are walled...
2094 BEQ next_pass ; Column 0: refuse
2096 CMP #map_width - 1 ; ...the right-hand column?
2098 BEQ next_pass ; Yes: refuse
209A LDA cursor_y ; ...the top row...
209C BEQ next_pass ; Row 0: refuse
209E CMP #map_height - 1 ; ...or the bottom row?
20A0 BEQ next_pass ; Yes: refuse
20A2 JSR place_item ; Place the item, within its limits
20A5 JSR show_counts ; Update the counts beside the map
20A8 .wait_space_released←1← 20B1 BNE
LDX #inkey_key_space ; Wait for SPACE to be released...
20AA JSR key_pressed ; ...(Z set while held)
20AD BNE check_tab ; Released: on to TAB
20AF LDA current_item ; ...an item, never walls, is chosen here...
20B1 BNE wait_space_released ; ...so keep waiting: one item per press
; TAB: delete what is under the cursor.
20B3 .check_tab←2← 2086 BNE← 20AD BNE
LDX #inkey_key_tab ; TAB: delete the cell's contents?
20B5 JSR key_pressed ; ...(Z set if pressed)
20B8 BNE check_ctrl ; Not pressed: try CTRL
20BA JSR delete_item ; Yes: delete what is under the cursor...
20BD JSR show_counts ; ...and update the counts
20C0 .wait_b_released←1← 20C9 BNE
LDX #inkey_key_b ; Wait while B (not TAB) is held, with an item chosen...
20C2 JSR key_pressed ; ...(Z set while held)
20C5 BNE check_ctrl ; B not held: on to CTRL
20C7 LDA current_item ; ...is an item chosen?
20C9 BNE wait_b_released ; Yes: keep waiting while B is held
; CTRL: show the door-key links while held.
20CB .check_ctrl←2← 20B8 BNE← 20C5 BNE
LDX #inkey_key_ctrl ; CTRL: show the links?
20CD JSR key_pressed ; ...(Z set if pressed)
20D0 BNE next_pass ; Not pressed: next pass
20D2 JSR draw_links ; Yes: draw the door-key links...
20D5 .wait_ctrl_released←1← 20DA BEQ
LDX #inkey_key_ctrl ; ...and leave them while CTRL is held...
20D7 JSR key_pressed ; ...(Z set while held)
20DA BEQ wait_ctrl_released ; Still held: keep the lines showing
20DC JSR draw_links ; ...then draw them again, which erases them
20DF .next_pass←5← 2094 BEQ← 2098 BEQ← 209C BEQ← 20A0 BEQ← 20D0 BNE
JMP edit_loop ; Next pass of the editing loop

Check that every door has its key

One of the edit loop's three tests before SHIFT+S may save. Count the door-key links in use and compare with door_count. A link is made only when a key is placed for a waiting door, and a door can be linked only once, so equal counts mean that no door lacks its key. A door still waiting for its key is counted in door_count but has no link, so it blocks saving.

On ExitZset if every door has a key
Athe number of links in use
X&FF
SCRATCHthe number of links in use
20E2 .check_door_keys←1← 1FDF JSR
LDA #0 ; Count the links in use...
20E4 STA scratch ; ...in scratch, from 0...
20E6 LDX #max_link ; ...over links 19 to 0
20E8 .count_links←1← 20F0 BPL
LDA link_door,x ; This link's door slot...
20EB BMI next_link ; ...&FF (negative): the link is free
20ED INC scratch ; In use: one more door with its key
20EF .next_link←1← 20EB BMI
DEX ; Next link down
20F0 BPL count_links ; ...until all 20 are counted
20F2 LDA scratch ; Links in use, one per door with a key...
20F4 CMP door_count ; ...against the doors placed: Z set if equal
20F6 RTS ; Return with Z set if every door has its key

Test a key

The edit loop's key test, for its fixed keys (f0, SHIFT+S, cursor up and down, '1', '2', SPACE, TAB, B and CTRL): OSBYTE &81 with the negative INKEY number in X, which returns X = Y = &FF while the key is held. move_cursor uses the same test in cursor_key_pressed, and the wall-block chooser in chooser_key_pressed.

On EntryXINKEY number (negative, as a byte)
On ExitZset if the key is pressed
Acorrupted
Xcorrupted
Ycorrupted
20F7 .key_pressed←16← 1FB9 JSR← 1FC8 JSR← 1FCF JSR← 1FD6 JSR← 2027 JSR← 203E JSR← 2045 JSR← 2057 JSR← 2061 JSR← 2072 JSR← 2083 JSR← 20AA JSR← 20B5 JSR← 20C2 JSR← 20CD JSR← 20D7 JSR
LDA #osbyte_inkey ; OSBYTE &81: scan the key...
20F9 LDY #&ff ; ...Y = &FF: test the key whose INKEY number is X
20FB JSR osbyte ; Scan it: X = Y = &FF if the key is held Test whether the key with negative INKEY number X is pressed
20FE CPY #&ff ; Z set if X and Y are both &FF
2100 BNE key_pressed_done ; Y not &FF: not pressed (Z clear)
2102 CPX #&ff ; ...Z set if X is &FF too: pressed
2104 .key_pressed_done←1← 2100 BNE
RTS ; Return with Z set if the key is held

Ask for disc or tape

Ask 'Disc or tape' and select the filing system the level file is loaded from or saved to: for disc, *DISC and then *DRIVE with the digit (0-3) typed at the drive prompt; for tape, *TAPE. Called by edit_level before a level is loaded, by save_level before the filename, and by catalogue before *CAT. The edit loop selects *DISC and *DRIVE 0 itself before saving, for Stones, so the choice made here comes after it.

On ExitAcorrupted
Xcorrupted
Ycorrupted
2105 .choose_filing_system←3← 1C80 JSR← 234F JSR← 23D5 JSR
LDX #<(disc_or_tape_prompt) ; Ask: disc (D) or tape (T)
2107 LDY #>(disc_or_tape_prompt) ; ...(the prompt's high byte)
2109 LDA #&32 ; ...first answer: D (internal key &32)...
210B STA scratch ; ...ask_key's first key
210D LDA #&23 ; ...second answer: T (internal key &23)
210F STA scratch_hi ; ...ask_key's second key
2111 LDA #0 ; ...A = 0: one of two keys
2113 JSR ask_key ; ...ask, A = 0 for D or 1 for T
2116 BNE choose_tape ; T: tape
2118 LDX #<(disc_command) ; D: select disc with *DISC...
211A LDY #>(disc_command) ; ...(the command's high byte)
211C JSR oscli ; ...select the disc filing system
211F JSR new_lines ; New line under the answer
2122 LDX #<(drive_prompt) ; Ask which drive...
2124 LDY #>(drive_prompt) ; ...(the prompt's high byte)
2126 LDA #1 ; ...A = 1: answer with a drive digit
2128 JSR ask_key ; ...ask, A = the digit typed
212B STA drive_digit ; ...and put the digit in drive_command
212E LDX #<(drive_command) ; *DRIVE n
2130 LDY #>(drive_command) ; ...(the command's high byte)
2132 JSR oscli ; ...select that drive
2135 RTS ; Return with the disc and drive selected
2136 .choose_tape←1← 2116 BNE
LDX #<(tape_command) ; T: select tape with *TAPE...
2138 LDY #>(tape_command) ; ...(the command's high byte)
213A JSR oscli ; ...select the cassette filing system
213D RTS ; Return with the tape selected

Set up the editing screen

Send edit_screen_vdu's &21 bytes, once start_editing has selected MODE 2: turn the text cursor off (6845 register 10), display 30 character rows (register 6), the map's 48 rows of five pixel rows needing 240, and set logical colours 10 and 9 to red and yellow. Hiding the rows from &7B00 up leaves &7B00-&7FFF free for unpack_level, mark_object_cell and object_free_map.

On ExitAcorrupted
X&21
Ypreserved
213E .set_edit_screen←1← 1EC0 JSR
LDX #0 ; Send its &21 bytes
2140 .send_edit_screen←1← 2149 BNE
LDA edit_screen_vdu,x ; Next byte of the VDU commands...
2143 JSR oswrch ; ...sent to the VDU drivers
2146 INX ; ...on to the next byte
2147 CPX #edit_screen_vdu_end - edit_screen_vdu ; ...all &21 sent?
2149 BNE send_edit_screen ; No: send the next
214B RTS ; Return with the edit screen set up

Encrypt or decrypt the level file

Exclusive-OR each byte of the level file, &2700-&29AF, with the low byte of its offset, which is that of its address, as the game's decrypt_level does at &6B00. Doing it twice restores the bytes. start_editing calls it to decrypt a level once loaded; save_level calls it to encrypt the level after setting the checksum, first decrypting it when an earlier save in this session left it encrypted (level_encrypted).

On ExitAcorrupted
Xcorrupted
Y0
214C .crypt_level←3← 1F1F JSR← 22F7 JSR← 2347 JSR
LDA #0 ; Point at &2700, X counting the offset...
214E TAX ; ...X = 0: the offset within the level
214F TAY ; ...Y = 0: scratch itself steps
2150 STA scratch ; ...scratch = &2700: page &27's start...
2152 LDA #>(level_object_x) ; ...page &27, the level file
2154 STA scratch_hi ; ...as scratch's high byte
2156 .crypt_byte←2← 2169 BNE← 216F BNE
LDA (scratch),y ; Exclusive-OR the byte with the offset's low byte...
2158 STX screen_ptr ; ...the offset's low byte, as the key...
215A EOR screen_ptr ; ...applied: XOR again undoes it
215C INX ; ...next offset
215D STA (scratch),y ; ...put the byte back
215F INC scratch ; Step scratch to the next byte
2161 BNE crypt_check_end ; No page crossed
2163 INC scratch_hi ; ...into the next page of the level
2165 .crypt_check_end←1← 2161 BNE
LDA scratch ; Until &29B0, the level file's end
2167 CMP #<(level_file_end) ; ...the low byte of &29B0?
2169 BNE crypt_byte ; No: next byte
216B LDA scratch_hi ; ...and the high byte?
216D CMP #>(level_file_end) ; ...&29B0's high byte?
216F BNE crypt_byte ; No: next byte
2171 RTS ; Return with all &2B0 bytes flipped

Show the walls in the chosen colour

VDU 19,10,n,0,0,0 with n from colour_10 (1-7). Logical colour 10 is what the wall sprite (all &CC bytes) and the cursor sprite are drawn in, so this changes the walls' colour on the screen, not in the level. The edit loop calls it after cursor up or down changes colour_10.

On EntryCOLOUR_10the colour to show
On ExitAcorrupted
Xpreserved
Ypreserved
2172 .set_colour_10←2← 2039 JSR← 2052 JSR
LDA #&13 ; VDU 19,10,n,0,0,0...
2174 JSR oswrch ; ...send VDU 19, redefine a logical colour
2177 LDA #&0a ; ...logical colour 10, the walls'...
2179 JSR oswrch ; ...send the logical colour
217C LDA colour_10 ; ...physical colour n from colour_10...
217F JSR oswrch ; ...send the physical colour
2182 LDA #0 ; ...then three zeros to end VDU 19
2184 JSR oswrch ; ...send the first zero
2187 JSR oswrch ; ...send the second zero
218A JSR oswrch ; ...send the third zero
218D RTS ; Return with the walls' colour changed

Print the counts' labels

In colour 1, at column 16 beside the map, on rows 0, 4 and 8: Objs, Mons (the trapdoors, where monsters appear) and Dors, each above its count. count_labels holds the VDU 31 moves with the text. Leaves colour 7 set for show_counts, which start_editing calls next.

On ExitAcorrupted
Xcorrupted
Ypreserved
218E .show_labels←2← 1F3B JSR← 1F85 JSR
LDA #&11 ; COLOUR 1, red, for the labels...
2190 JSR oswrch ; ...send VDU 17, COLOUR
2193 LDA #1 ; ...colour 1, red
2195 JSR oswrch ; ...send the colour
2198 LDX #0 ; Print count_labels up to its CR
219A .print_labels←1← 21A3 BNE
LDA count_labels,x ; Next label character...
219D JSR oswrch ; ...or VDU 31 byte, printed
21A0 INX ; ...on to the next byte
21A1 CMP #&0d ; ...the CR at the end?
21A3 BNE print_labels ; No: print the next
21A5 LDA #&11 ; COLOUR 7, white, for the counts...
21A7 JSR oswrch ; ...send VDU 17, COLOUR
21AA LDA #7 ; ...colour 7, white
21AC JSR oswrch ; ...send the colour
21AF RTS ; Return with colour 7 set for the counts

Print the counts

Objects at (17, 2), trapdoors at (17, 6) and doors at (17, 10), each under its label, as two digits in the text colour show_labels left. Called by start_editing and after each SPACE (an item, not a wall) or TAB in the edit loop.

On ExitAcorrupted
Xcorrupted
Ycorrupted
21B0 .show_counts←4← 1F3E JSR← 1F88 JSR← 20A5 JSR← 20BD JSR
LDX #&11 ; Objects at text column 17...
21B2 LDY #2 ; ...row 2, under Objs
21B4 LDA object_count ; ...the number of objects placed
21B6 JSR print_number ; ...printed as two digits
21B9 LDX #&11 ; Trapdoors at text column 17...
21BB LDY #6 ; ...row 6, under Mons
21BD LDA trapdoor_count ; ...the number of trapdoors placed
21BF JSR print_number ; ...printed as two digits
21C2 LDX #&11 ; Doors at text column 17...
21C4 LDY #&0a ; ...row 10, under Dors
21C6 LDA door_count ; ...the number of doors placed
21C8 JSR print_number ; ...printed as two digits
21CB RTS ; Return with the three counts shown

Print the cursor's cell

x at (17, 20) and y at (17, 21), beside the map. The edit loop calls it every pass, after the cursor and colour keys.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAcorrupted
Xcorrupted
Ycorrupted
21CC .show_cursor_position←1← 205C JSR
LDX #&11 ; The cursor's x at text column 17...
21CE LDY #&14 ; ...row 20
21D0 LDA cursor_x ; ...the cursor's column, 0-31
21D2 JSR print_number ; ...printed as two digits
21D5 LDX #&11 ; The cursor's y at text column 17...
21D7 LDY #&15 ; ...row 21
21D9 LDA cursor_y ; ...the cursor's row, 0-47
21DB JSR print_number ; ...printed as two digits
21DE RTS ; Return with the cursor's cell shown

Print a two-digit number at a text position

Move the text cursor to (X, Y) with VDU 31 and print A (0-99) as two digits, a leading zero included. The tens are counted by repeated subtraction of 10. show_counts and show_cursor_position use it.

On EntryAthe number
Xcolumn
Yrow
On ExitAcorrupted
Xthe tens digit
Ythe units digit
SCRATCHthe number
21DF .print_number←5← 21B6 JSR← 21BF JSR← 21C8 JSR← 21D2 JSR← 21DB JSR
STA scratch ; Keep the number in scratch
21E1 TYA ; Keep the position for VDU 31...
21E2 PHA ; ...the row pushed first...
21E3 TXA ; ...then the column...
21E4 PHA ; ...on top, to be sent first
21E5 LDA scratch ; Tens: count the 10s...
21E7 SEC ; ...C set for the subtractions
21E8 LDX #0 ; ...X counts them
21EA .count_tens←1← 21EF BNE
SBC #&0a ; Take away 10...
21EC BCC tens_counted ; ...gone below 0: X is the tens digit
21EE INX ; ...one more ten
21EF BNE count_tens ; ...and again (always branches)
21F1 .tens_counted←1← 21EC BCC
SEC ; Units: add the last 10 back
21F2 SBC #&f6 ; ...(A - &F6 = A + 10)
21F4 TAY ; ...Y = the units
21F5 LDA #&1f ; VDU 31, column, row: move the text cursor
21F7 JSR oswrch ; ...send VDU 31
21FA PLA ; ...the column, from the stack
21FB JSR oswrch ; ...send the column
21FE PLA ; ...the row, from the stack
21FF JSR oswrch ; ...send the row
2202 TXA ; Tens digit from X...
2203 CLC ; ...C clear for the addition
2204 ADC #'0' ; ...as an ASCII digit
2206 JSR oswrch ; ...printed, the first digit
2209 TYA ; Units digit (C clear)
220A ADC #'0' ; ...as an ASCII digit
220C JSR oswrch ; ...printed after the tens
220F RTS ; Return with both digits printed

Put walls round the edge of the map

Place a wall in every cell of columns 0 and 31 and rows 0 and 47, closing the map on every side. The edit loop calls it first when SHIFT+S saves, before pack_level. Each goes through place_item, which leaves a cell that already holds a wall alone and otherwise clears its object-free bit too (put_item). Items cannot be placed on the edge, so every edge cell ends up a wall, except that place_item takes the top-left cell, (0, 0), for a wall already whenever a slot is free (see item_at_cursor), so that cell's wall bit is left to save_level. It leaves cursor_x at &FF, cursor_y at 47 and the current item the wall, which no later code relies on: save_level never returns to the edit loop.

On ExitAcorrupted
Xcorrupted
Ycorrupted
CURSOR_X&FF
CURSOR_Y47
CURRENT_ITEM0, the wall
2210 .wall_edges←1← 1FF8 JSR
LDA #0 ; The wall, item 0...
2212 STA current_item ; ...as the item place_item puts down
2214 LDA #map_height - 1 ; First the side columns, for rows 47 to 0...
2216 STA cursor_y ; ...moving the cursor to each in turn
2218 .wall_left_right←1← 2228 BPL
LDA #0 ; ...in column 0...
221A STA cursor_x ; ...the left edge
221C JSR place_item ; ...walled in
221F LDA #map_width - 1 ; ...and column 31...
2221 STA cursor_x ; ...the right edge
2223 JSR place_item ; ...walled in
2226 DEC cursor_y ; ...one row up
2228 BPL wall_left_right ; ...until row 0 is done
; Then the top and bottom rows.
222A LDA #map_width - 1 ; For columns 31 to 0...
222C STA cursor_x ; ...moving the cursor to each in turn
222E .wall_top_bottom←1← 223E BPL
LDA #0 ; ...in row 0...
2230 STA cursor_y ; ...the top edge
2232 JSR place_item ; ...walled in
2235 LDA #map_height - 1 ; ...and row 47...
2237 STA cursor_y ; ...the bottom edge
2239 JSR place_item ; ...walled in
223C DEC cursor_x ; ...one column left
223E BPL wall_top_bottom ; ...until column 0 is done
2240 RTS ; Return with the map walled in

BREAK intercept: back to saving

save_level points the MOS BREAK intercept here, so that BREAK while saving does not lose the level. The MOS calls the intercept twice during a reset; on the second call, with C set, go back to save_level's start, with the packed level still at &2700. level_encrypted says whether it was already encrypted, so save_level decrypts it before summing again.

; BREAK intercept while saving: on the second call (C set) go ; back to save_level's prompt.
2241 .break_to_save
BCS save_level ; Second call (after the reset)? Save again
2243 RTS ; First call: return to the MOS's reset

Save the level

The last step of SHIFT+S: the edit loop has walled the map in, packed the slots and maps into the level file at &2700 and copied in the chosen wall block, then selected MODE 7. save_level also restarts itself: break_to_save comes back here after BREAK, and a level number out of range asks again from the top.

Point the BREAK intercept at break_to_save and BRKV at the reset entry, disable ESCAPE, and ask 'Level in the Dunjunz complex' for a number of up to two digits (RETURN alone: catalogue). Only 1-24 are accepted; level 25 is in Stones. The number only chooses the checksum, from the game's level_checksums: level_number is not read again, and the filename is typed separately, so a level saved under another level's filename fails the game's checksum test when it is loaded.

Wall the top-left cell, then set the level's first byte (object entry 0's x, which the game never reads) so that the &2B0 bytes sum to the checksum modulo 256: the sum is taken with that byte 0, and the byte is checksum - sum. Both of the code's branches give that value. Encrypt the level with crypt_level, choose disc or tape, ask for the filename and *SAVE name 2700+2B0, then ask 'Another level (A) or quit (Q)'. It never returns: A restarts the editor at editor_init, and Q disarms the BREAK intercept and resets the machine.

2244 .save_level←5← 200B JSR← 2241 BCS← 22C2 BEQ← 22D9 JMP← 22E0 JMP
LDA #opcode_jmp ; BREAK intercept = JMP break_to_save...
2246 STA os_break_intercept ; ...so BREAK while saving comes back here
2249 LDA #<(break_to_save) ; ...break_to_save's low byte...
224B STA os_break_intercept_lo ; ...as the JMP's operand, low...
224E LDA #>(break_to_save) ; ...and its high byte...
2250 STA os_break_intercept_hi ; ...as the JMP's operand, high
2253 LDA #<(mos120_reset_entry) ; BRKV = MOS 1.20's reset entry
2255 STA brkv ; ...BRKV's low byte
2258 LDA #>(mos120_reset_entry) ; ...the reset entry's high byte...
225A STA brkv + 1 ; ...high byte: an error resets the machine
225D LDA #osbyte_escape_break_effect ; OSBYTE &C8 with X=1, Y=0: disable ESCAPE
225F LDX #1 ; ...X = 1: ESCAPE does nothing
2261 LDY #0 ; ...Y = 0: replace the setting
2263 JSR osbyte ; Set it, so ESCAPE cannot stop the save osbyte: escape break effect
2266 LDA #&0c ; VDU 12: clear the screen
2268 JSR oswrch ; ...for the save prompts
; Ask for the level number, up to two digits, '0'-'9' only.
226B .ask_level_number←1← 23E5 JMP
LDA #osbyte_flush_buffer_class ; OSBYTE &0F: flush the buffers...
226D JSR osbyte ; Discard keys pressed before the level prompt Flush selected class of buffer
2270 LDX #0 ; Print the prompt, level_number_prompt...
2272 .print_level_prompt←1← 227B BNE
LDA level_number_prompt,x ; Next prompt character...
2275 JSR oswrch ; ...printed on the screen
2278 INX ; ...on to the next character
2279 CMP #0 ; ...the zero at its end?
227B BNE print_level_prompt ; No: print the next
227D LDX #<(read_line_block) ; Point XY at read_line_block...
227F LDY #>(read_line_block) ; ...(the block's high byte)
2281 LDA #'0' ; ...digits '0' '0' to start with...
2283 STA filename_buffer ; ...as the first digit
2286 STA filename_buffer + 1 ; ...and the second
2289 LDA #<(filename_buffer) ; ...line buffer filename_buffer...
228B STA read_line_block ; ...the buffer's low byte in the block
228E LDA #>(filename_buffer) ; ...and its high byte...
2290 STA read_line_block + 1 ; ...in the block
2293 LDA #2 ; ...at most two characters...
2295 STA read_line_block + 2 ; ...as the maximum length
2298 LDA #'0' ; ...characters '0' to '9' only...
229A STA read_line_block + 3 ; ...lowest character accepted
229D LDA #'9' ; ...to '9'...
229F STA read_line_block + 4 ; ...highest character accepted
22A2 LDA #osword_read_line ; OSWORD 0: read a line
22A4 JSR osword ; Read the level number the user types Read line from input stream (exits with C=1 if ESCAPE pressed)
22A7 CPY #0 ; Anything typed (Y = length)?
22A9 BNE level_typed ; Yes: convert it
22AB JMP catalogue ; RETURN alone: catalogue, then ask again
; Convert the one or two digits typed to the level number.
22AE .level_typed←1← 22A9 BNE
LDA filename_buffer + 1 ; Only one digit typed?
22B1 CMP #&0d ; ...the second character a CR?
22B3 BNE two_digits ; No: two digits
22B5 LDA filename_buffer ; Yes: the level is the digit
22B8 SEC ; ...C set for the subtraction
22B9 SBC #'0' ; ...ASCII to its value
22BB BPL check_level_number ; ...(always branches: A is 0-9)
22BD .two_digits←1← 22B3 BNE
LDA filename_buffer ; Two digits: tens x 10 + units...
22C0 CMP #&0d ; ...a CR in the first place?
22C2 BEQ save_level ; Not taken (Y > 0); it would ask again
22C4 SEC ; ...C set for the subtraction
22C5 SBC #'0' ; ...the tens digit's value...
22C7 ASL ; ...x 2...
22C8 STA scratch ; ...tens x 2, kept to add...
22CA ASL ; ...x 4...
22CB ASL ; ...x 8...
22CC CLC ; ...C clear for the addition
22CD ADC scratch ; ...+ x 2: tens x 10
22CF ADC filename_buffer + 1 ; ...plus the units digit's ASCII...
22D2 SEC ; ...C set for the subtraction
22D3 SBC #'0' ; ...less the units digit's ASCII bias
; Accept only levels 1 to 24, and take the checksum the game ; holds for that level.
22D5 .check_level_number←1← 22BB BPL
CMP #0 ; Level 0, which does not exist?
22D7 BNE level_not_zero ; No: check the top
22D9 JMP save_level ; Yes: ask again
22DC .level_not_zero←1← 22D7 BNE
CMP #max_level + 1 ; Level 25 or more?
22DE BCC level_number_ok ; No: 1-24, a level the game loads from a file
22E0 JMP save_level ; Yes: ask again
22E3 .level_number_ok←1← 22DE BCC
STA level_number ; Keep the level number (never read again)
22E6 TAX ; ...X = level, to index the checksums
22E7 LDA level_checksums - 1,x ; ...the game's checksum for that level...
22EA STA level_checksum ; ...which the level's bytes must sum to
22ED LDA #&0a ; Line feed under the number
22EF JSR oswrch ; ...sent to the screen
22F2 LDA level_encrypted ; Saved already (so encrypted)?
22F5 BEQ set_checksum ; No: the level is still plain
22F7 JSR crypt_level ; Yes: decrypt it again
; Make the level's bytes sum to its checksum, through the first ; byte, which the game never reads.
22FA .set_checksum←1← 22F5 BEQ
LDA level_walls ; Wall cell (0, 0), which wall_edges may have missed
22FD ORA #&80 ; ...bit 7 of the wall map's first byte...
22FF STA level_walls ; ...set in the level file
2302 LDA #0 ; Sum the level's bytes, with the first byte 0...
2304 TAY ; ...Y = 0: scratch steps through the file
2305 STA level_object_x ; ...first byte 0, so it adds nothing
2308 STA screen_ptr ; ...the sum starts at 0
230A STA scratch ; ...scratch at &2700, page-aligned...
230C LDA #>(level_object_x) ; ...page &27, the level file
230E STA scratch_hi ; ...as scratch's high byte
2310 .sum_level←2← 2321 BNE← 2327 BNE
LDA (scratch),y ; Next byte of the level...
2312 CLC ; ...C clear for the addition
2313 ADC screen_ptr ; ...added to the sum...
2315 STA screen_ptr ; ...modulo 256
2317 INC scratch ; Step scratch to the next byte
2319 BNE sum_check_end ; No page crossed
231B INC scratch_hi ; ...into the next page
231D .sum_check_end←1← 2319 BNE
LDA scratch ; ...to &29AF, the file's last byte
231F CMP #<(level_file_end) ; ...the low byte of &29B0?
2321 BNE sum_level ; No: next byte
2323 LDA scratch_hi ; ...and the high byte?
2325 CMP #>(level_file_end) ; ...&29B0's high byte?
2327 BNE sum_level ; No: next byte
2329 LDA screen_ptr ; First byte = checksum - sum (modulo 256)...
232B CMP level_checksum ; ...compared with the checksum
232E BCC sum_below ; Sum below the checksum
2330 SEC ; ...(sum above the checksum: &FF - sum + checksum + 1)...
2331 LDA #&ff ; ...starting from &FF...
2333 SBC screen_ptr ; ...less the sum...
2335 CLC ; ...C clear for the addition
2336 ADC level_checksum ; ...plus the checksum...
2339 ADC #1 ; ...plus 1: checksum - sum + 256
233B JMP store_first_byte ; ...stored as the first byte
233E .sum_below←1← 232E BCC
SEC ; ...(otherwise checksum - sum)
233F LDA level_checksum ; ...the level's checksum...
2342 SBC screen_ptr ; ...less the sum
2344 .store_first_byte←1← 233B JMP
STA level_object_x ; ...so that the level's sum is its checksum
; Encrypt the level, then save it under the filename typed.
2347 JSR crypt_level ; Encrypt the level for the game
234A LDA #1 ; Mark it encrypted
234C STA level_encrypted ; ...so a second save decrypts it first
234F JSR choose_filing_system ; Choose disc or tape
2352 LDA #' ' ; Blank the filename in save_command...
2354 LDX #5 ; ...offset 5, where the name goes...
2356 .blank_save_name←1← 235C BNE
STA save_command,x ; ...a space in each place...
2359 INX ; ...on to the next character
235A CPX #&0c ; ...up to offset 11?
235C BNE blank_save_name ; No: blank the next
235E JSR ask_filename ; Ask for the filename
2361 LDA #&0a ; Line feed under the name
2363 JSR oswrch ; ...sent to the screen
2366 LDX #0 ; Copy it into save_command...
2368 .copy_save_name←1← 2371 BNE
LDA filename_buffer,x ; Next filename character...
236B STA save_command + 5,x ; ...into the command
236E INX ; ...on to the next character
236F CPX scratch ; ...all the characters typed?
2371 BNE copy_save_name ; No: copy the next
2373 LDX #<(save_command) ; *SAVE name 2700+2B0
2375 LDY #>(save_command) ; ...(the command's high byte)
2377 JSR oscli ; ...save the level file
237A LDA #&0a ; Line feed under *SAVE's output
237C JSR oswrch ; ...sent to the screen
; Another level, or quit.
237F LDX #<(another_prompt) ; Ask: another level (A) or quit (Q)
2381 LDY #>(another_prompt) ; ...(the prompt's high byte)
2383 LDA #&41 ; ...first answer: A (internal key &41)...
2385 STA scratch ; ...ask_key's first key
2387 LDA #&10 ; ...second answer: Q (internal key &10)
2389 STA scratch_hi ; ...ask_key's second key
238B LDA #0 ; ...A = 0: one of two keys
238D JSR ask_key ; ...ask, A = 0 for A or 1 for Q
2390 BNE quit ; Q: quit
2392 JMP editor_init ; A: restart the editor for another level
2395 .quit←1← 2390 BNE
LDA #0 ; Quit: disable the BREAK intercept...
2397 STA os_break_intercept ; ...so the reset starts the machine afresh
239A JMP mos120_reset_entry ; ...and reset the machine

*EDITOR: copy the plotter page and start

The program's entry point, run once by *EDITOR. Copy &2700-&28FF to &0900-&0AFF (the plotter, screen_address, the packed counts, the item types and checksums, and the sprite tables), freeing &2700-&29AF for the level file, then start at editor_init. A restart (BREAK, or another level after saving) goes to editor_init directly, as the copy has been overwritten by then.

239D .editor_start
LDX #0 ; Copy &2700-&27FF to &0900...
239F .copy_plotter_lo←1← 23A6 BNE
LDA plotter_image,x ; Next byte of the stored plotter page...
23A2 STA plot_sprite,x ; ...to its run address
23A5 DEX ; ...on to the next byte
23A6 BNE copy_plotter_lo ; ...until all 256 are copied
23A8 .copy_plotter_hi←1← 23AF BNE
LDA plotter_image_page_2,x ; ...and &2800-&28FF to &0A00
23AB STA sprite_addr_lo,x ; ...to its run address
23AE DEX ; ...on to the next byte
23AF BNE copy_plotter_hi ; ...until all 256 are copied
23B1 JMP editor_init ; Set up and ask: edit a level or a new one

Catalogue the disc or tape

Reached from save_level when RETURN is pressed with no level number, so that the user can see which level files exist before choosing one. Show CAT after the prompt, as if it had been typed as the answer, choose disc or tape, *CAT, then ask for the level number again at ask_level_number, below the catalogue, without clearing the screen.

23B4 .catalogue←1← 22AB JMP
LDA #&0b ; VDU 11: up a line...
23B6 JSR oswrch ; ...back onto the prompt's line
23B9 LDA #9 ; ...and VDU 9, right past its 29 characters...
23BB LDX #&1d ; ...29 times, counted in X...
23BD .move_right_29←1← 23C1 BNE
JSR oswrch ; ...one place right...
23C0 DEX ; ...one fewer place to move
23C1 BNE move_right_29 ; ...until all 29 are done
23C3 LDA #'C' ; ...and show CAT as the answer: 'C'...
23C5 JSR oswrch ; ...'C' printed...
23C8 LDA #'A' ; ...then the 'A'...
23CA JSR oswrch ; ...'A' printed...
23CD LDA #'T' ; ...then the 'T'...
23CF JSR oswrch ; ...'T' printed
23D2 JSR new_lines ; New line under CAT
23D5 JSR choose_filing_system ; Choose disc or tape
23D8 JSR new_lines ; New line under the answer
23DB LDX #<(cat_command) ; *CAT, the filing system's catalogue...
23DD LDY #>(cat_command) ; ...(the command's high byte)
23DF JSR oscli ; ...list the files
23E2 JSR new_lines ; New line under the catalogue
23E5 JMP ask_level_number ; Ask for the level number again

Unused

Leftover bytes: &FF, then a stale copy of the end of catalogue (new line, choose disc or tape, new line, *CAT, new line, ask for the level number again) assembled at other addresses.

23E8 EQUB &FF, &20, &17, &1E, &20, &04, &21, &20, &17, &1E, &A2, &85, &A0, &1D, &20, &F7, &FF, &20, &17, &1E, &4C, &82, &22, &20 ; Not used

Choose the level's wall block

Let the user pick the picture the game draws every wall of the level with. Called from the edit loop when SHIFT+S saves, after pack_level and with drive 0 selected; save_level follows. Clear the screen, show only 25 character rows, load Stones (the 24 wall blocks) at &6E80, just below them, draw the blocks in a grid four wide and six high and frame block 0. Then loop: pause for a delay loop of about 25,000 iterations, read the keys (move_chooser: ',' '.' one block, 'A' 'Z' a row) and, when the choice moved, wait for the frame and move the frame round. SPACE copies the chosen block's &30 bytes from Stones into the level file's wall block and returns. The editing screen is not restored, as save_level selects MODE 7 next.

On ExitAcorrupted
Xcorrupted
Y&FF
LEVEL_WALL_BLOCKthe chosen block
2400 .choose_wall_block←1← 1FFE JSR
LDA #&0c ; VDU 12: clear the screen
2402 JSR oswrch ; ...of the edited map
2405 JSR show_25_rows ; Show 25 rows, hiding &6E80 on, where Stones goes
2408 JSR load_stones ; Load Stones at &6E80
240B NOP ; NOP: no effect
240C NOP ; NOP: no effect
240D NOP ; NOP: no effect
240E NOP ; NOP: no effect
240F LDA #0 ; Block 0 chosen...
2411 STA chosen_block ; ...to start with
2414 JSR draw_wall_blocks ; Draw the 24 blocks
2417 LDA #spr_chooser_frame ; Frame block 0...
2419 JSR frame_block ; ...drawing the frame round it
; Pause, read the keys, and take the block when SPACE is ; pressed.
241C .chooser_loop←2← 2471 BEQ← 2490 JMP
LDX #&64 ; Pause for 100 x 255 loops...
241E .chooser_pause←1← 2424 BNE
LDY #&ff ; ...255 inner loops each...
2420 .chooser_pause_inner←1← 2421 BNE
DEY ; ...one inner loop fewer...
2421 BNE chooser_pause_inner ; ...until the inner count is 0
2423 DEX ; ...one outer loop fewer...
2424 BNE chooser_pause ; ...until 0: slow enough to steer
2426 JSR move_chooser ; Read the movement keys
2429 LDX #inkey_key_space ; SPACE: take this block?
242B JSR chooser_key_pressed ; ...(Z set if pressed)
242E BNE chooser_moved ; Not pressed: redraw if the choice moved
; SPACE: copy block chosen_block, at &6E80 + block x &30, into ; the level file.
2430 LDA #0 ; Yes: the block's address, &6E80 + block x &30...
2432 STA plot_y ; ...high byte, in plot_y, starts at 0
2434 LDA chosen_block ; ...the chosen block number...
2437 ASL ; ...x 2...
2438 ASL ; ...x 4...
2439 ASL ; ...x 8...
243A ASL ; ...x 16, the carry...
243B ROL plot_y ; ...into the high byte
243D STA scratch ; ...block x 16, low byte...
243F LDA plot_y ; ...and block x 16's high byte...
2441 STA scratch_hi ; ...kept in scratch_hi
2443 LDA scratch ; ...block x 16 again...
2445 ASL ; ...x 2: block x 32...
2446 ROL plot_y ; ...carry into the high byte
2448 CLC ; ...C clear for the addition
2449 ADC scratch ; ...+ x 16: block x 48 (&30), low byte...
244B STA plot_x ; ...kept in plot_x
244D LDA plot_y ; ...and its high byte...
244F ADC scratch_hi ; ...plus x 16's high byte, with the carry
2451 STA plot_y ; ...kept in plot_y
2453 CLC ; ...C clear for the addition
2454 LDA plot_x ; ...the offset's low byte...
2456 ADC #<(wall_blocks) ; ...plus where Stones' blocks start...
2458 STA plot_x ; ...the block pointer's low byte
245A LDA plot_y ; ...the offset's high byte...
245C ADC #>(wall_blocks) ; ...plus the start's high byte
245E STA plot_y ; ...the block pointer's high byte
2460 LDY #wall_block_bytes - 1 ; Copy its &30 bytes into level_wall_block
2462 .copy_wall_block←1← 2468 BPL
LDA (plot_x),y ; Next byte of the chosen block...
2464 STA level_wall_block,y ; ...into the level file
2467 DEY ; ...down to the first
2468 BPL copy_wall_block ; ...until all &30 are copied
246A RTS ; Return with the wall block in the level
; Not SPACE: if the choice moved, move the frame from the old ; block to the new.
246B .chooser_moved←1← 242E BNE
LDA previous_block ; Did the keys move the choice?
246E CMP chosen_block ; ...still the block chosen before?
2471 BEQ chooser_loop ; Not moved: pause and read the keys again
2473 LDA #osbyte_vsync ; Yes: wait for the frame...
2475 JSR osbyte ; Pace the chooser to the display Wait for vertical sync
2478 LDA chosen_block ; ...then erase the old frame...
247B PHA ; ...the new choice kept
247C LDA previous_block ; ...the old one...
247F STA chosen_block ; ...made current for the frame
2482 LDA #spr_chooser_blank ; ...the blank frame sprite...
2484 JSR frame_block ; ...rubbed out round the old block
2487 PLA ; ...the new choice back
2488 STA chosen_block ; ...as the chosen block
248B LDA #spr_chooser_frame ; ...and frame the new block
248D JSR frame_block ; ...drawn round it
2490 JMP chooser_loop ; Pause and read the keys again

Draw the 24 wall blocks

Plot sprites &12-&29, whose data the editor's sprite tables point at the blocks in Stones at &6E80, at their places in the grid (block_place). Called once by choose_wall_block, after load_stones.

On ExitAcorrupted
X&FF
Ycorrupted
2493 .draw_wall_blocks←1← 2414 JSR
LDX #wall_block_count - 1 ; For blocks 23 to 0...
2495 .draw_wall_block←1← 24A6 BPL
TXA ; Keep the block number...
2496 PHA ; ...on the stack, as plot_sprite uses X
2497 CLC ; ...sprite &12 + block...
2498 ADC #spr_wall_block_0 ; ...its sprite in the editor's tables
249A STA sprite_number ; ...for plot_sprite
249C TXA ; ...at the block's place in the grid
249D JSR block_place ; ...set plot_x, plot_y for it
24A0 JSR plot_sprite ; ...plot the block
24A3 PLA ; ...the block number back
24A4 TAX ; ...into X, the countdown
24A5 DEX ; ...the block before
24A6 BPL draw_wall_block ; ...until block 0 is drawn
24A8 RTS ; Return with all 24 blocks drawn

Move the chosen wall block

'.' and ',' move one block on or back, 'Z' and 'A' a row of four; a move off the grid is undone, so ',' and '.' wrap from one row's end to the next row but stop at blocks 0 and 23. Only one key is taken each call, '.' first. choose_wall_block calls it once a pause and compares the two blocks afterwards to see whether the frame must be moved.

On ExitCHOSEN_BLOCKthe block now chosen
PREVIOUS_BLOCKthe block chosen before
Acorrupted
Xcorrupted
Ycorrupted
24A9 .move_chooser←1← 2426 JSR
LDA chosen_block ; Remember the block chosen
24AC STA previous_block ; ...to compare after moving
24AF LDX #inkey_key_full_stop ; '.': the next block?
24B1 JSR chooser_key_pressed ; ...(Z set if pressed)
24B4 BNE check_back ; Not pressed: try ','
24B6 INC chosen_block ; Yes: the next block...
24B9 LDA chosen_block ; ...read the new block
24BC CMP #wall_block_count ; ...past the last block?
24BE BNE chooser_moved_ok ; No: the move stands
24C0 BEQ undo_move ; Past 23: undo it
24C2 .check_back←1← 24B4 BNE
LDX #inkey_key_comma ; ',': the previous block?
24C4 JSR chooser_key_pressed ; ...(Z set if pressed)
24C7 BNE check_row_on ; Not pressed: try Z
24C9 DEC chosen_block ; Yes: the previous block...
24CC BPL chooser_moved_ok ; Still 0 or more: the move stands
24CE BMI undo_move ; Below 0: undo it
24D0 .check_row_on←1← 24C7 BNE
LDX #inkey_key_z ; 'Z': a row down?
24D2 JSR chooser_key_pressed ; ...(Z set if pressed)
24D5 BNE check_row_back ; Not pressed: try A
24D7 CLC ; Yes: a row on...
24D8 LDA chosen_block ; ...the chosen block...
24DB ADC #4 ; ...4 on, the block below in the grid
24DD STA chosen_block ; ...as the chosen block
24E0 CMP #wall_block_count ; ...past the last block?
24E2 BCC chooser_moved_ok ; Within the 24: the move stands
24E4 BCS undo_move ; Past 23: undo it
24E6 .check_row_back←1← 24D5 BNE
LDX #inkey_key_a ; 'A': a row up?
24E8 JSR chooser_key_pressed ; ...(Z set if pressed)
24EB BNE chooser_moved_ok ; Not pressed: no move
24ED SEC ; Yes: a row back...
24EE LDA chosen_block ; ...the chosen block...
24F1 SBC #4 ; ...4 back, the block above in the grid
24F3 STA chosen_block ; ...as the chosen block
24F6 BPL chooser_moved_ok ; Still 0 or more: the move stands
24F8 BMI undo_move ; Below 0: undo it
24FA .chooser_moved_ok←5← 24BE BNE← 24CC BPL← 24E2 BCC← 24EB BNE← 24F6 BPL
RTS ; Return with the new choice in chosen_block
24FB .undo_move←4← 24C0 BEQ← 24CE BMI← 24E4 BCS← 24F8 BMI
LDA previous_block ; Off the grid: undo the move
24FE STA chosen_block ; ...restore the block chosen before
2501 RTS ; Return with the choice unchanged

Test a key

The wall-block chooser's copy of key_pressed: OSBYTE &81 with the INKEY number in X, testing X before Y. Used for ',' '.' 'A' 'Z' and SPACE.

On EntryXINKEY number
On ExitZset if pressed
Acorrupted
Xcorrupted
Ycorrupted
2502 .chooser_key_pressed←5← 242B JSR← 24B1 JSR← 24C4 JSR← 24D2 JSR← 24E8 JSR
LDA #osbyte_inkey ; OSBYTE &81: scan the key...
2504 LDY #&ff ; ...Y = &FF: test the key whose INKEY number is X
2506 JSR osbyte ; Scan it: X = Y = &FF if the key is held Test for a key pressed
2509 CPX #&ff ; Z set if X and Y are both &FF
250B BNE chooser_key_done ; X not &FF: not pressed (Z clear)
250D CPY #&ff ; ...Z set if Y is &FF too: pressed
250F .chooser_key_done←1← 250B BNE
RTS ; Return with Z set if the key is held

Find a wall block's place in the grid

Block n is at column n mod 4, row n div 4: plot_x = 8 x column + &1A, plot_y = 24 x row + &14. Each block is 4 bytes by 12 rows, so the grid leaves room for the frame between them.

On EntryAblock number
On ExitPLOT_Xthe block's screen byte column
PLOT_Yits pixel row
Xthe block number
Cclear
2510 .block_place←3← 249D JSR← 252F JSR← 2540 JSR
TAX ; Keep the block number in X
2511 AND #3 ; plot_x = (n mod 4) x 8 + &1A
2513 ASL ; ...x 2...
2514 ASL ; ...x 4...
2515 ASL ; ...x 8 bytes across...
2516 CLC ; ...C clear for the addition
2517 ADC #&1a ; ...from column &1A
2519 STA plot_x ; ...as plot_x
251B TXA ; plot_y = (n div 4) x 24 + &14
251C AND #&fc ; ...the block's grid row x 4...
251E ASL ; ...x 8...
251F STA scratch ; ...row x 8, kept to add...
2521 ASL ; ...x 16...
2522 CLC ; ...C clear for the addition
2523 ADC scratch ; ...+ x 8: the row x 24 pixel rows
2525 ADC #&14 ; ...from pixel row &14
2527 STA plot_y ; ...as plot_y
2529 RTS ; Return with the block's place, C clear

Draw or erase the frame round the chosen block

Plot sprite A (&2A the frame, &2B a blank), 6 bytes by 18 rows, one byte left of and three rows above the chosen block, so that it surrounds the 4-byte by 12-row block, then plot the block again inside it, as both sprites cover it. choose_wall_block frames block 0 at the start and, when the choice moves, erases the old frame and draws the new one.

On EntryA&2A to frame, &2B to erase
CHOSEN_BLOCKthe block to frame
On ExitAcorrupted
Xcorrupted
Ycorrupted
252A .frame_block←3← 2419 JSR← 2484 JSR← 248D JSR
STA sprite_number ; The frame or blank sprite...
252C LDA chosen_block ; ...at the chosen block's place...
252F JSR block_place ; ...set plot_x, plot_y
2532 DEC plot_x ; ...one byte left...
2534 LDA plot_y ; ...and up (C is clear from block_place)...
2536 SBC #2 ; ...SBC #2 with C clear: three rows up
2538 STA plot_y ; ...as plot_y
253A JSR plot_sprite ; Plot the frame, covering the block
253D LDA chosen_block ; Then the block itself
2540 JSR block_place ; ...set plot_x, plot_y
2543 LDA chosen_block ; ...the block number...
2546 CLC ; ...C clear for the addition
2547 ADC #spr_wall_block_0 ; ...as its sprite
2549 STA sprite_number ; ...for plot_sprite
254B JSR plot_sprite ; ...plot it inside the frame
254E RTS ; Return with the block framed or unframed
; *LOAD STONES, for load_stones: Stones loads at its own ; address, &6E80.
254F .load_stones_command
EQUS "LOAD STONES", &0D ; OSCLI command: *LOAD STONES, at its own address

Show 25 rows

Send VDU 23,0,6,25,0,0,0,0,0,0: 6845 register 6, the rows displayed, = 25. Row 25 starts at &6E80, where load_stones puts Stones, so the wall-block chooser does not show it. Called once by choose_wall_block, before load_stones.

On ExitAcorrupted
X10
Ypreserved
255B .show_25_rows←1← 2405 JSR
LDX #0 ; Send rows_25_vdu's ten bytes
255D .send_25_rows←1← 2566 BNE
LDA rows_25_vdu,x ; Next byte of the VDU command...
2560 JSR oswrch ; ...sent to the VDU drivers
2563 INX ; ...on to the next byte
2564 CPX #&0a ; ...all ten sent?
2566 BNE send_25_rows ; No: send the next
2568 RTS ; Return with 25 rows displayed
; VDU 23,0,6,25,0,0,0,0,0,0: 6845 register 6 = 25 rows ; displayed, sent by show_25_rows.
2569 .rows_25_vdu←1Used as index base by← 255D LDA
EQUB &17, &00, &06, &19, &00, ; VDU 23,0,6,25,0,0,0,0,0,0: &00, &00, &00, &00, &00 ; display 25 rows

Unused

Leftover bytes: a fragment of BBC BASIC assembler.

2573 EQUS "A#255:LDX#0:."

The chooser's frame

Sprite &2A: 6 bytes by 18 pixel rows.

; Sprite &2A: the frame round the chosen wall block, 6 bytes by ; 18 rows.
2580 .frame_sprite
EQUB &3F, &3F, &3F, &3F, &3F, &3F, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &2A, &00, &00, &00, &00, &15, &3F, &3F, &3F, &3F, &3F, &3F

Unused

Leftover bytes.

25EC EQUB &0D, &09, &D8, &2E ; Not used

Variables

The editor's settings and state, kept in the program itself: they survive a BREAK, which restarts the editor without reloading it.

; 1 if a new, empty level is being made; 0 to edit a loaded one. ; Written by editor_init from the E/N answer, read by ; start_editing.
25F0 .new_level_flag←2← 1C75 STA← 1F13 LDA
EQUB &00
; The level number (1-24) typed when saving. Written by ; save_level and not read: the number only chooses ; level_checksum.
25F1 .level_number←1← 22E3 STA
EQUB &01
; That level's entry in level_checksums, which save_level makes ; the level's bytes sum to (modulo 256).
25F2 .level_checksum←4← 22EA STA← 232B CMP← 2336 ADC← 233F LDA
EQUB &62
; 1 for the slow cursor (three extra frames a pass), 0 for the ; fast one. start_editing sets it; f0 in the edit loop toggles ; it.
25F3 .slow_cursor←4← 1F97 STA← 1FA4 LDA← 1FBE LDA← 1FC3 STA
EQUB &01
; The physical colour, 1-7, that logical colour 10 (the walls on ; screen) shows. start_editing sets 1, red; cursor up and down ; step it.
25F4 .colour_10←7← 1F9A STA← 202C INC← 202F LDA← 2036 DEC← 204A DEC← 204F INC← 217C LDA
EQUB &01
; Toggled each edit-loop pass when placing walls; the cursor ; sprite is plotted over the wall when it is 1.
25F5 .cursor_flash←3← 1EA9 STA← 2016 LDA← 201B STA
EQUB &01
25F6 EQUB &00, &00 ; Not referenced
; The wall block chosen, 0-23: row x 4 + column in the chooser's ; grid.
25F8 .chosen_block←18← 2411 STA← 2434 LDA← 246E CMP← 2478 LDA← 247F STA← 2488 STA← 24A9 LDA← 24B6 INC← 24B9 LDA← 24C9 DEC← 24D8 LDA← 24DD STA← 24EE LDA← 24F3 STA← 24FE STA← 252C LDA← 253D LDA← 2543 LDA
EQUB &00
; The wall block chosen before move_chooser's last call, to tell ; whether the frame must move.
25F9 .previous_block←4← 246B LDA← 247C LDA← 24AC STA← 24FB LDA
EQUB &00
; 1 once save_level has encrypted the level at &2700, so that a ; later save (after BREAK) decrypts it first; editor_init clears ; it.
25FA .level_encrypted←3← 1C37 STA← 22F2 LDA← 234C STA
EQUB &00
; Internal key numbers of 0, 1, 2 and 3, the drive digits, ; indexed by the digit: ask_key matches the key pressed against ; them when asked for a drive.
25FB .drive_keys←1Used as index base by← 1CD7 CMP
EQUB &27, &30, &31, ; Internal key numbers of '0' '1' '2' &11 ; '3'
25FF EQUB &00 ; Not referenced

Pack the item slots into the level file

Turn the editor's item slots back into the level file's tables, in the game's types. pack_level calls it when SHIFT+S saves, after wall_edges.

Go through the slots from the last (&4F) to the first, so that the tables come out in the reverse of the order unpack_level read them: objects into entries 1 upwards (the exit always into entry 30), trapdoors into entries 0-7, and each door with its linked key into the next door entry. Keys and free slots are skipped. Each item's game type or door sprite comes from item_types, indexed by the editor's item number. Saving needs exactly eight trapdoors and a key for every door, so every trapdoor entry and every placed door's key is written; object and door entries beyond those placed keep the &FF (off the map) that start_editing put there. The counts are left in packed_objects, packed_trapdoors and packed_doors.

On ExitAcorrupted
X&FF
Ycorrupted
2600 .pack_items←1← 26A9 JSR
LDA #0 ; No objects, trapdoors or doors packed yet
2602 STA packed_objects ; ...no object entries yet...
2605 STA packed_trapdoors ; ...no trapdoor entries yet...
2608 STA packed_doors ; ...and no door entries yet
260B LDX #max_slot ; For slots &4F to 0...
260D .pack_slot←1← 2638 BPL
LDA slot_item,x ; ...what is in it?
2610 BEQ pack_next ; Free slot: skip it
2612 CMP #1 ; Is it the exit?
2614 BEQ pack_exit ; Yes: always object entry 30
2616 CMP #3 ; Is it a trapdoor?
2618 BEQ pack_trapdoor ; Yes: the trapdoor table
261A CMP #spr_door_vertical ; A door or key?
261C BCS pack_door ; Yes: the door tables
; An object: the next object entry, with its game type.
261E INC packed_objects ; An object: the next entry...
2621 TAY ; ...the editor's item number...
2622 LDA item_types,y ; ...as the game's object type
2625 LDY packed_objects ; ...the entry number
2628 STA level_object_type,y ; ...into the level's object types
262B LDA slot_x,x ; ...and its x...
262E STA level_object_x,y ; ...into the level's object x
2631 LDA slot_y,x ; ...and its y...
2634 STA level_object_y,y ; ...into the level's object y
2637 .pack_next←5← 2610 BEQ← 264E BPL← 2662 BNE← 2666 BEQ← 26A6 JMP
DEX ; Next slot down
2638 BPL pack_slot ; ...until slot 0 is done
263A RTS ; Return with every slot packed
; The exit: always object entry 30.
263B .pack_exit←1← 2614 BEQ
LDA #game_exit_type ; The exit: entry 30, type &51
263D LDY #max_object ; ...the exit's entry in the object tables
263F STA level_object_type,y ; ...its game type, &51
2642 LDA slot_x,x ; ...the exit's x...
2645 STA level_object_x,y ; ...as object 30's x
2648 LDA slot_y,x ; ...the exit's y...
264B STA level_object_y,y ; ...as object 30's y
264E BPL pack_next ; Next slot (always branches)
; A trapdoor: the next trapdoor entry, from 0.
2650 .pack_trapdoor←1← 2618 BEQ
LDY packed_trapdoors ; A trapdoor: the next trapdoor entry
2653 LDA slot_x,x ; ...the trapdoor's x...
2656 STA level_trapdoor_x,y ; ...into the level's trapdoor table
2659 LDA slot_y,x ; ...the trapdoor's y...
265C STA level_trapdoor_y,y ; ...into the level's trapdoor table
265F INC packed_trapdoors ; ...one more trapdoor packed
2662 BNE pack_next ; Next slot (always branches)
; A door: the next door entry, with its game sprite, and its ; key's position from the door's link.
2664 .pack_door←1← 261C BCS
CMP #spr_key ; Is it a key?
2666 BEQ pack_next ; Yes: skip it, as it is packed with its door
2668 INC packed_doors ; A door: the next door entry...
266B TAY ; ...the editor's item number...
266C LDA item_types,y ; ...as the game's door sprite
266F LDY packed_doors ; ...the entry number
2672 STA level_door_sprite,y ; ...into the level's door sprites
2675 LDA slot_x,x ; ...and its x...
2678 STA level_door_x,y ; ...into the level's door x
267B LDA slot_y,x ; ...and its y...
267E STA level_door_y,y ; ...into the level's door y
2681 STX bit_mask ; Find the link whose door is this slot...
2683 LDY #max_link ; ...over links 19 to 0
2685 .find_door_link←1← 268D BPL
LDA link_door,y ; This link's door slot...
2688 CMP bit_mask ; ...the door's slot?
268A BEQ pack_key ; Yes: Y is its link
268C DEY ; ...next link down
268D BPL find_door_link ; ...until all 20 are tried
268F .pack_key←1← 268A BEQ
TXA ; ...and put its key's position in the same entry
2690 PHA ; ...keeping the door's slot
2691 LDA link_key,y ; ...the key's slot...
2694 TAX ; ...into X, to index the slots
2695 LDY packed_doors ; ...the door's entry number
2698 LDA slot_x,x ; ...the key's x...
269B STA level_key_x,y ; ...as the door's key x
269E LDA slot_y,x ; ...the key's y...
26A1 STA level_key_y,y ; ...as the door's key y
26A4 PLA ; ...the door's slot back
26A5 TAX ; ...into X, the slot counter
26A6 JMP pack_next ; Next slot down

Pack the level for saving

Called from the edit loop when SHIFT+S saves, after wall_edges and before choose_wall_block. Pack the item slots into the object, door and trapdoor tables (pack_items), then copy the wall map and the object-free map, &C9 bytes each, into the level file. save_level then sets the checksum and encrypts.

On ExitAcorrupted
X&C9
Ycorrupted
26A9 .pack_level←1← 1FFB JSR
JSR pack_items ; Pack the items into the level's tables
26AC LDX #0 ; Copy the &C9 bytes of each map
26AE .copy_maps←1← 26BD BNE
LDA wall_map,x ; Next wall-map byte...
26B1 STA level_walls,x ; ...into the level file
26B4 LDA object_free_map,x ; ...and the object-free map byte...
26B7 STA level_object_free,x ; ...into the level file
26BA INX ; ...on to the next byte
26BB CPX #map_bytes ; ...all &C9 copied?
26BD BNE copy_maps ; No: copy the next
26BF RTS ; Return with the level file complete

Unused

Zeros.

26C0 EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00 ; Not used

Load the wall blocks

*LOAD STONES (at its own address, &6E80) with VDU output off. Called by choose_wall_block, after show_25_rows has hidden the screen from &6E80 up, with disc drive 0 already selected by the edit loop. Stones holds the 24 wall blocks (&480 bytes) and then level 25, which the editor does not use. A missing Stones is an error, and BRKV resets the machine.

On ExitAcorrupted
Xcorrupted
Ycorrupted
26D0 .load_stones←1← 2408 JSR
LDA #&15 ; VDU 21: output off
26D2 JSR oswrch ; ...so the *LOAD message does not show
26D5 LDX #<(load_stones_command) ; *LOAD STONES, to &6E80...
26D7 LDY #>(load_stones_command) ; ...(the command's high byte)
26D9 JSR oscli ; ...load the 24 wall blocks
26DC LDA #6 ; VDU 6: output on
26DE JSR oswrch ; ...sent to the VDU drivers
26E1 RTS ; Return with Stones at &6E80

Unused

Zeros.

26E2 EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00 ; Not used

Plot a sprite

Copy sprite sprite_number to the screen with its top-left corner at byte column plot_x, pixel row plot_y.

The four sprite tables, indexed by sprite number, give the address of its data and its size: sprite_width bytes across (two MODE 2 pixels to a byte) by sprite_height pixel rows. The data is stored row by row and is written over the screen contents without masking, so a sprite's black pixels erase what was there, and there is no clipping. The screen is laid out like MODE 2 with 640-byte character rows from &3000, so successive bytes of a sprite row are 8 bytes apart, and each pixel row starts one byte below the last, or 640-7 bytes on when it crosses into the next character row. screen_ptr walks across each row, row_start_ptr remembers where the row began, and sprite_data_ptr walks through the data; Y stays 0. sprite_width and sprite_height are left holding the sprite's size.

On EntryPLOT_Xscreen byte column (0-79)
PLOT_Ypixel row
SPRITE_NUMBERsprite to plot
On ExitAcorrupted
X0
Y0
LoadExec
2700 0900 .plot_sprite←6Referenced by← 24A0 JSR← 253A JSR← 254B JSR← 2B72 JSR← 2CF3 JSRUsed as index base by← 23A2 STA
LDX sprite_number ; X = sprite number, indexing the four sprite tables
2702 0902 LDA sprite_addr_lo,x ; Point sprite_data_ptr at the editor sprite's data...
2705 0905 STA sprite_data_ptr ; ...its low byte from sprite_addr_lo
2707 0907 LDA sprite_addr_hi,x ; ...high byte, from the editor's sprite tables...
270A 090A STA sprite_data_ptr_hi ; ...so sprite_data_ptr walks through the data
270C 090C LDA sprite_widths,x ; Width in screen bytes, from sprite_widths...
270F 090F STA scratch ; ...counted across each row
2711 0911 LDA sprite_heights,x ; Height in pixel rows, from sprite_heights...
2714 0914 STA scratch_hi ; ...counted down the sprite
2716 0916 JSR screen_address ; screen_ptr = address of the top-left byte
2719 0919 LDY #0 ; Index stays 0: the pointers themselves advance
271B 091B LDX scratch_hi ; X counts the pixel rows still to plot
271D 091D .plot_sprite_row←1← 0964 BNE
.plot_row←1← 0964 BNE
LDA screen_ptr ; Remember where this pixel row starts...
271F 091F STA row_start_ptr ; ...its low byte...
2721 0921 LDA screen_ptr_hi ; ...and its high byte...
2723 0923 STA row_start_ptr_hi ; ...in row_start_ptr, to step down from later
2725 0925 TXA ; Save the row count
2726 0926 PHA ; ...on the stack: X now counts bytes
2727 0927 LDX scratch ; X counts the bytes across this row
2729 0929 .plot_sprite_byte←1← 093F BNE
.plot_byte←1← 093F BNE
LDA (sprite_data_ptr),y ; Copy one sprite byte...
272B 092B STA (screen_ptr),y ; ...to the screen, over whatever was there
272D 092D CLC ; Step screen_ptr right one byte column: 8 bytes on
272E 092E LDA screen_ptr ; ...the screen address's low byte...
2730 0930 ADC #8 ; ...+ 8, the next byte column...
2732 0932 STA screen_ptr ; ...as screen_ptr's new low byte
2734 0934 BCC plot_sprite_next_data ; No carry: the high byte stands
2736 0936 INC screen_ptr_hi ; ...with carry into the high byte
2738 0938 .plot_sprite_next_data←1← 0934 BCC
.plot_next_data←1← 0934 BCC
INC sprite_data_ptr ; Next sprite data byte
273A 093A BNE plot_sprite_count_byte ; No carry: the high byte stands
273C 093C INC sprite_data_ptr_hi ; ...with carry into the high byte
273E 093E .plot_sprite_count_byte←1← 093A BNE
.plot_count_byte←1← 093A BNE
DEX ; One byte fewer left in this row
273F 093F BNE plot_sprite_byte ; More of the row: plot the next byte
2741 0941 CLC ; screen_ptr = row start + 1: the next pixel row down
2742 0942 LDA row_start_ptr ; The row start's low byte...
2744 0944 ADC #1 ; ...+ 1, one pixel row lower...
2746 0946 STA screen_ptr ; ...is the new low byte
2748 0948 LDA row_start_ptr_hi ; The row start's high byte...
274A 094A ADC #0 ; ...plus the carry...
274C 094C STA screen_ptr_hi ; ...is the new high byte
274E 094E LDA screen_ptr ; Has it crossed into the next character row?
2750 0950 AND #7 ; ...(pixel row within the character now 0)
2752 0952 BNE plot_sprite_next_row ; No: still within the character row
2754 0954 CLC ; Yes: add &278 more, to the top of the character...
2755 0955 LDA screen_ptr ; ...row below (row start + 1 + &278 = + 640 - 7)
2757 0957 ADC #&78 ; ...low byte + &78 (640 - 8 = &278)...
2759 0959 STA screen_ptr ; ...as screen_ptr's low byte
275B 095B LDA screen_ptr_hi ; ...then the high byte...
275D 095D ADC #2 ; ...+ 2 and the carry...
275F 095F STA screen_ptr_hi ; ...as screen_ptr's high byte: the row below
2761 0961 .plot_sprite_next_row←1← 0952 BNE
.plot_next_row←1← 0952 BNE
PLA ; Recover the row count
2762 0962 TAX ; ...back into X
2763 0963 DEX ; One pixel row fewer to plot
2764 0964 BNE plot_sprite_row ; More rows: plot the next
2766 0966 RTS ; Return: the sprite is on the screen

Calculate a screen address

Set screen_ptr to the screen address of byte column plot_x on pixel row plot_y: &3000 + 640 x (plot_y DIV 8) + (plot_y MOD 8) + 8 x plot_x. plot_sprite calls it once per sprite, for the top-left byte. The multiplication by 640 is done with a table inside MOS 1.20 (mos_row_offsets, two bytes per character row, high byte first), so this only works with that MOS. The high byte of 8 x plot_x is plot_x DIV 32, taken from plot_x's top three bits with four ROLs.

On EntryPLOT_Xscreen byte column (0-79)
PLOT_Ypixel row
On ExitSCREEN_PTRscreen address
Acorrupted
Ycorrupted
Xpreserved
LoadExec
2767 0967 .screen_address←1← 0916 JSR
LDA plot_y ; Table index = 2 x (plot_y DIV 8), the character row...
2769 0969 LSR ; ...plot_y DIV 2...
276A 096A LSR ; ...plot_y DIV 4: 2 x the character row, + plot_y bit 2...
276B 096B AND #&fe ; ...bit 0 cleared: the row's pair of table bytes
276D 096D TAY ; ...as the index into the row table
276E 096E LDA mos_row_offsets,y ; High byte of row x 640, from the MOS table...
2771 0971 CLC ; ...(carry clear for the add)
2772 0972 ADC #>(screen_start) ; ...plus &30: the screen starts at &3000
2774 0974 STA screen_ptr_hi ; ...as the screen address high byte
2776 0976 LDA mos_row_offsets_lo,y ; Low byte of row x 640
2779 0979 STA screen_ptr ; ...as the low byte
277B 097B LDA plot_y ; Add the pixel row within the character...
277D 097D AND #7 ; ...plot_y MOD 8...
277F 097F ADC screen_ptr ; ...(carry clear: adding &30 to the high byte cannot carry)
2781 0981 STA screen_ptr ; ...stored in the low byte
2783 0983 CLC ; Add 8 x plot_x: its high byte is plot_x DIV 32...
2784 0984 LDA plot_x ; ...taking the byte column...
2786 0986 AND #&e0 ; ...bits 7-5 kept...
2788 0988 ROL ; ...four ROLs through the carry: bit 7 out...
2789 0989 ROL ; ...bit 7 in at bit 0, bit 6 out
278A 098A ROL ; ...bits 7-6 at bits 1-0, bit 5 out
278B 098B ROL ; ...bits 7-5 now in bits 2-0
278C 098C CLC ; ...(carry clear for the add)
278D 098D ADC screen_ptr_hi ; ...added to the high byte
278F 098F STA screen_ptr_hi ; ...as screen_ptr's high byte
2791 0991 LDA plot_x ; Low byte + (plot_x x 8) AND &FF: plot_x...
2793 0993 ASL ; ...x 2...
2794 0994 ASL ; ...x 4...
2795 0995 ASL ; ...x 8, its top bits dropping out
2796 0996 CLC ; ...(carry clear for the add)
2797 0997 ADC screen_ptr ; ...added to the low byte
2799 0999 STA screen_ptr ; ...as screen_ptr's low byte
279B 099B BCC screen_address_done ; No carry: done
279D 099D INC screen_ptr_hi ; ...with carry into the high byte
279F 099F .screen_address_done←1← 099B BCC
RTS ; Return with screen_ptr set

Unused

Leftover bytes.

LoadExec
27A0 09A0 EQUB &20, &20, &20, &20, &44, &20, &20, &20, &20, &20, &20, &20, &44, &20, &20, &20 ; Not used
; pack_items's objects packed so far, besides the exit: ; zeroed, then incremented before each is written, so it ; is the entry just filled (1-29).
27B0 09B0 .packed_objects←3← 2602 STA← 261E INC← 2625 LDY
EQUB &20
; pack_items's trapdoors packed so far: the next trapdoor ; entry to fill (0-7), incremented after each.
27B1 09B1 .packed_trapdoors←3← 2605 STA← 2650 LDY← 265F INC
EQUB &20
; pack_items's doors packed so far, each with its key: ; incremented before each is written, so it is the door ; entry just filled (1-20).
27B2 09B2 .packed_doors←4← 2608 STA← 2668 INC← 266F LDY← 2695 LDY
EQUB &20
27B3 09B3 EQUB &20, &44, &20, &20, &20, &20, &20, &20, &20, &44, &20, &20, &20 ; Not used: leftover spaces and Ds

Item types and checksums

item_types, indexed by editor item (0-&0F), translates between the editor's items and the game's object types and door sprites: unpack_level's item_for_type searches it for a type when loading, and pack_items indexes it by item when saving. level_checksums, indexed by level number from &09CF, is a copy of the game's checksum table, which save_level reads so that a saved level passes the game's check.

LoadExec
; The level type of each editor item (its sprite number, ; 0-&0F): 1 exit &51; 2 energy drainer &53; 4-&0C treasure ; &28, food &29, teleporter &2B, magic sword &63, magic ; helmet &60, potion &61, weapons &62, Cross &2A, boots ; &5F; &0D and &0E the door sprites &1C and &1D. Entries 0 ; (wall), 3 (trapdoor) and &0F (key) are not used as ; types.
27C0 09C0 .item_types←3Used as index base by← 2622 LDA← 266C LDA← 7BC2 LDA
EQUB &22, &51, &53, &00, &28, &29, &2B, &63, &60, &61, &62, &2A, &5F, &1C, &1D, &31
; The game's checksum table (level_checksums, &0491): the ; sum, modulo 256, of each level's decrypted bytes. ; Indexed from &09CF by level number.
27D0 09D0 .level_checksums
EQUB &62, &CD, &0C, &44, &4D, &22, &F6, &42, &21, &7D, &5D, &D8, &A0, &F1, &0F, &CC, &D3, &53, &3B, &83, &71, &B2, &6F, &F3, &F1

Unused

Leftover bytes.

LoadExec
27E9 09E9 EQUB &00, &3A, &20, &20, &20, &3A, &20, &20, &20, &3A, &20, &20, &20, &3A, &20, &20, &20, &3A, &20, &20, &20, &3A, &20 ; Not used

Sprite tables

Each of the editor's 51 sprites' data address (low and high bytes), width in bytes and height in pixel rows. 0-&0F are the items (two bytes by five rows, at &2A00), &10 is blank (zeros at &7FF0), &11 a door waiting for its key, &12-&29 the 24 wall blocks in Stones (&6E80), &2A the wall-block chooser's frame and &2B a blank part of the screen that erases it, and &2C the cursor. Entries &2D-&32 are not used.

LoadExec
; Each sprite's data address, low byte.
2800 0A00 .sprite_addr_lo←2Used as index base by← 0902 LDA← 23AB STA
EQUB <(item_sprites), <(item_sprites + 10), <(item_sprites + 20), <(item_sprites + 30), <(item_sprites + 40), <(item_sprites + 50), <(item_sprites + 60), <(item_sprites + 70), <(item_sprites + 80), <(item_sprites + 90), <(item_sprites + 100), <(item_sprites + 110), <(item_sprites + 120), <(item_sprites + 130), <(item_sprites + 140), <(item_sprites + 150), <(blank_sprite), <(waiting_door_sprite), <(wall_blocks), <(wall_blocks + &30), <(wall_blocks + &60), <(wall_blocks + &90), <(wall_blocks + &c0), <(wall_blocks + &f0), <(wall_blocks + &0120), <(wall_blocks + &0150), <(wall_blocks + &0180), <(wall_blocks + &01b0), <(wall_blocks + &01e0), <(wall_blocks + &0210), <(wall_blocks + &0240), <(wall_blocks + &0270), <(wall_blocks + &02a0), <(wall_blocks + &02d0), <(wall_blocks + &0300), <(wall_blocks + &0330), <(wall_blocks + &0360), <(wall_blocks + &0390), <(wall_blocks + &03c0), <(wall_blocks + &03f0), <(wall_blocks + &0420), <(wall_blocks + &0450), <(frame_sprite), <(screen_start + &0200), <(cursor_sprite), &0A, &25, &00, &05, &00, &00
; Each sprite's data address, high byte.
2833 0A33 .sprite_addr_hi←1Used as index base by← 0907 LDA
EQUB >(item_sprites), >(item_sprites + 10), >(item_sprites + 20), >(item_sprites + 30), >(item_sprites + 40), >(item_sprites + 50), >(item_sprites + 60), >(item_sprites + 70), >(item_sprites + 80), >(item_sprites + 90), >(item_sprites + 100), >(item_sprites + 110), >(item_sprites + 120), >(item_sprites + 130), >(item_sprites + 140), >(item_sprites + 150), >(blank_sprite), >(waiting_door_sprite), >(wall_blocks), >(wall_blocks + &30), >(wall_blocks + &60), >(wall_blocks + &90), >(wall_blocks + &c0), >(wall_blocks + &f0), >(wall_blocks + &0120), >(wall_blocks + &0150), >(wall_blocks + &0180), >(wall_blocks + &01b0), >(wall_blocks + &01e0), >(wall_blocks + &0210), >(wall_blocks + &0240), >(wall_blocks + &0270), >(wall_blocks + &02a0), >(wall_blocks + &02d0), >(wall_blocks + &0300), >(wall_blocks + &0330), >(wall_blocks + &0360), >(wall_blocks + &0390), >(wall_blocks + &03c0), >(wall_blocks + &03f0), >(wall_blocks + &0420), >(wall_blocks + &0450), >(frame_sprite), >(screen_start + &0200), >(cursor_sprite), &00, &00, &00, &00, &00, &00
; Each sprite's width in screen bytes.
2866 0A66 .sprite_widths←1Used as index base by← 090C LDA
EQUB &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &02, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &04, &06, &06, &02, &00, &9D, &0A, &79, &25, &00
; Each sprite's height in pixel rows.
2899 0A99 .sprite_heights←1Used as index base by← 0911 LDA
EQUB &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &05, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &0C, &12, &12, &05, &00, &BF, &21, &D1, &0A, &63

Unused

Leftover bytes.

LoadExec
28CC 0ACC EQUB &68, &61, &00, &5A, &22, &D9, &0A, &64, &69, &73, &00, &21, &1E, &00, &00, &61, &64, &64, &00, &90, &22, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &FF ; Not used

Unused

Leftover bytes, covered by the level file (&2700-&29AF) when one is loaded.

2900 EQUB &FF, &FF, &FF, &BF, &FF, &7F, &FF, &FF, &FE, &FE, ; No &FE, &FE, &FE, &FE, &FE, &F6, &7F, &7F, &3F, &7F, ; t &7F, &7F, &7F, &7F, &FF, &EF, &FF, &FF, &FF, &FF, ; us &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, ; ed &FE, &FE, &7E, &FE, &FE, &FE, &FE, &FE, &7F, &7F, ; : &7F, &7F, &7F, &7F, &7F, &7F, &FF, &FF, &FF, &FF, ; th &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, ; e &FF, &FF, &FE, &FE, &FE, &FE, &FE, &FE, &FE, &FE, ; le &7F, &7F, &7F, &7F, &7F, &7F, &7F, &00, &FF, &FF, ; ve &FF, &FF, &FF, &FF, &FF, &00, &FF, &FF, &FF, &FF, ; l &FF, &FF, &FF, &00, &FE, &FE, &FE, &FE, &FE, &FE, ; fi &FE, &00, &FF, &FF, &FF, &FF, &FF, &FF, &FF, &FF, ; le &FF, &00, &00, &00, &00, &00, &00, &00 ; 's ; & ; am ; p ; ; ; 29 ; 00 ; -& ; am ; p ; ; ; 29 ; AF ; wh ; en ; on ; e ; is ; lo ; ad ; ed
; The level file: the level's wall block (sprite 0 in the game, ; &30 bytes).
2980 .level_wall_block←2Used as index base by← 1E92 STA← 2464 STA
EQUB &14, &3C, &3C, &28, &29, &03, &03, &16, ; Not used: &29, &03, &03, &16, &29, &03, &03, &16, ; the level's &29, &03, &03, &16, &14, &3C, &3C, &28, ; wall block &3C, &28, &14, &3C, &03, &16, &29, &03, ; when one is &03, &16, &29, &03, &03, &16, &29, &03, ; loaded &03, &16, &29, &03, &3C, &28, &14, &3C
29B0 EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00 ; Not used

Sprites

The cursor (sprite &2C), then the 16 items (sprites 0-&0F) and the door waiting for its key (&11), each two bytes by five pixel rows.

; Sprite &2C: the cursor.
29F6 .cursor_sprite
EQUB &CC, &CC, &88, &44, &88, &44, &88, &44, &CC, &CC
; Sprites 0-&0F, the editor's items: wall, exit, energy drainer, ; trapdoor, treasure, food, teleporter, magic sword, magic ; helmet, potion, weapons, Cross, boots, the two doors and the ; key.
2A00 .item_sprites
EQUB &CC, &CC, &CC, &CC, &CC, &CC, &CC, &CC, ; Sprite &00: &CC, &CC ; wall
2A0A EQUB &03, &03, &02, &01, &02, &01, &02, &01, ; Sprite &01: &03, &03 ; exit
2A14 EQUB &33, &33, &62, &BB, &62, &BB, &62, ; Sprite &02: &BB, &33, &33 ; drainer
2A1E EQUB &3C, &3C, &29, &16, &29, &16, &29, ; Sprite &03: &16, &3C, &3C ; trapdoor
2A28 EQUB &03, &01, &43, &A9, &53, &A1, &47, ; Sprite &04: &83, &03, &01 ; treasure
2A32 EQUB &2A, &00, &3F, &0A, &01, &0B, &05, &0B, ; Sprite &05: &00, &02 ; food
2A3C EQUB &3F, &3F, &3E, &38, &3E, &38, &3E, ; Sprite &06: &38, &30, &30 ; teleporter
2A46 EQUB &A8, &41, &00, &02, &3F, &03, ; Sprite &07: magic &00, &02, &82, &54 ; sword
2A50 EQUB &00, &28, &14, &3C, &10, &30, &14, ; Sprite &08: &34, &14, &3C ; helmet
2A5A EQUB &00, &2A, &00, &0A, &05, &0B, &05, ; Sprite &09: &03, &00, &02 ; potion
2A64 EQUB &05, &01, &0A, &2B, &00, &2B, &18, ; Sprite &0A: &2B, &08, &2A ; weapons
2A6E EQUB &00, &0A, &05, &0F, &00, &0A, &00, &0A, ; Sprite &0B: &00, &00 ; cross
2A78 EQUB &00, &05, &00, &07, &01, &22, &13, &22, ; Sprite &0C: &00, &00 ; boots
2A82 EQUB &14, &2A, &14, &2A, &14, &2A, ; Sprite &0D: door &14, &2A, &14, &2A ; vertical
2A8C EQUB &00, &00, &3C, &3C, &3F, &3F, ; Sprite &0E: door &00, &00, &00, &00 ; horizontal
2A96 EQUB &00, &00, &00, &05, &0F, &0F, &0F, &05, ; Sprite &0F: &00, &00 ; key
; Sprite &11: a door waiting for its key.
2AA0 .waiting_door_sprite
EQUB &45, &8A, &CF, &CF, &CF, &CF, &CF, &CF, &45, &8A

Unused

Leftover bytes.

2AAA EQUB &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00, &00 ; Not used

Move the cursor

Called at the start of every edit-loop pass, after the slow cursor's wait. Wait for the frame, rub out the cursor and put back what is under it (item_at_cursor), move with the keys in key_left, key_right, key_up and key_down (',' '.' 'A' 'Z') within the 32 by 48 map, and plot the current item there as the cursor. So the cursor shows what SPACE would place. The keys are read without waiting for release, so a held key repeats every pass. A move right skips the test for left, and a move up the test for down, so with both keys of a pair held '.' and 'A' win; the cursor can still move diagonally.

On ExitAcorrupted
Xcorrupted
Ycorrupted
2B00 .move_cursor←1← 200F JSR
LDA #osbyte_vsync ; Wait for the frame
2B02 JSR osbyte ; Pace the cursor's movement to the display Wait for vertical sync
2B05 LDA #spr_blank ; Rub out the cursor (the blank sprite)
2B07 JSR plot_at_cursor ; ...at the cursor's cell
2B0A JSR item_at_cursor ; Anything in the cell under the cursor?
2B0D BMI check_right ; Empty: leave the cell blank
2B0F JSR plot_at_cursor ; Yes: redraw the wall or item the cursor covered
; Move with the cursor keys, x then y.
2B12 .check_right←1← 2B0D BMI
LDX key_right ; Right key, '.' (key_right), pressed?
2B14 JSR cursor_key_pressed ; ...(Z set if pressed)
2B17 BNE check_left ; Not pressed: try left
2B19 INC cursor_x ; Yes: one column right...
2B1B BPL check_up ; ...always, skipping left; clamp_cursor limits it
2B1D .check_left←1← 2B17 BNE
LDX key_left ; Left key, ',' (key_left), pressed?
2B1F JSR cursor_key_pressed ; ...(Z set if pressed)
2B22 BNE check_up ; Not pressed: try up
2B24 DEC cursor_x ; Yes: left, to at least 0
2B26 BPL check_up ; Still 0 or more: on to up and down
2B28 LDA #0 ; Off the left: back to column 0
2B2A STA cursor_x ; ...as the cursor's column
2B2C .check_up←3← 2B1B BPL← 2B22 BNE← 2B26 BPL
LDX key_up ; Up key, 'A' (key_up), pressed?
2B2E JSR cursor_key_pressed ; ...(Z set if pressed)
2B31 BNE check_down ; Not pressed: try down
2B33 DEC cursor_y ; Yes: up, to at least 0
2B35 BPL clamp_cursor ; Still 0 or more: down is skipped
2B37 LDA #0 ; Off the top: back to row 0
2B39 STA cursor_y ; ...as the cursor's row
2B3B BEQ clamp_cursor ; ...skipping down (always branches)
2B3D .check_down←1← 2B31 BNE
LDX key_down ; Down key, 'Z' (key_down), pressed?
2B3F JSR cursor_key_pressed ; ...(Z set if pressed)
2B42 BNE clamp_cursor ; Not pressed: no move in y
2B44 INC cursor_y ; Yes: one row down...
2B46 BPL clamp_cursor ; ...always branches; clamp_y limits it
; Keep the cursor on the map, and show the current item there.
2B48 .clamp_cursor←4← 2B35 BPL← 2B3B BEQ← 2B42 BNE← 2B46 BPL
LDA cursor_x ; At most column 31...
2B4A CMP #map_width ; ...one past the right-hand column?
2B4C BNE clamp_y ; No: the column stands
2B4E LDA #map_width - 1 ; Yes: back to the right-hand column
2B50 STA cursor_x ; ...as the cursor's column
2B52 .clamp_y←1← 2B4C BNE
LDA cursor_y ; ...and at most row 47
2B54 CMP #map_height ; ...one past the bottom row?
2B56 BNE plot_cursor ; No: the row stands
2B58 LDA #map_height - 1 ; Yes: back to the bottom row
2B5A STA cursor_y ; ...as the cursor's row
2B5C .plot_cursor←1← 2B56 BNE
LDA current_item ; Plot the current item at the cursor...
2B5E JSR plot_at_cursor ; ...as the cursor, over whatever is there
2B61 RTS ; Return with the cursor moved and drawn

Plot a sprite in the cursor's cell

At screen byte column 2x and pixel row 5y: each map cell is two MODE 2 bytes (four pixels) by five pixel rows, so the 32 by 48 map fills the left 64 bytes and 240 rows of the screen. Used to draw the cursor, the cell under it and, at start-up, every cell of a loaded level.

On EntryAsprite number
CURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAcorrupted
Xcorrupted
Ycorrupted
2B62 .plot_at_cursor←5← 1F50 JSR← 2022 JSR← 2B07 JSR← 2B0F JSR← 2B5E JSR
STA sprite_number ; The sprite for plot_sprite...
2B64 LDA cursor_x ; ...at column 2x...
2B66 ASL ; ...two screen bytes per cell...
2B67 STA plot_x ; ...as plot_x
2B69 LDA cursor_y ; ...and row 5y
2B6B ASL ; ...x 2...
2B6C ASL ; ...x 4...
2B6D CLC ; ...C clear for the addition
2B6E ADC cursor_y ; ...+ y: five pixel rows per cell
2B70 STA plot_y ; ...as plot_y
2B72 JSR plot_sprite ; Plot it in the cell
2B75 RTS ; Return with the sprite drawn

Find what is in the cursor's cell

Search the slots for an item at the cursor; failing that, look at the wall map. A free slot is item 0 at (0, 0) (see free_slot), so whenever a slot is free the top-left cell reports a wall, whatever the wall map holds there. A door is found in its slot before its wall bit is looked at. The editor's one test of a cell's contents: move_cursor, place_item, remove_at_cursor, delete_item and start_editing's drawing all use it.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAthe item's sprite and X its slot; 0 for a wall; &FF for nothing
Nset for nothing
Zset for a wall
MAP_PTRfor no item: the cell's wall-map byte
BIT_MASKfor no item: the cell's bit
2B76 .item_at_cursor←5← 1F49 JSR← 2B0A JSR← 2C0D JSR← 2C29 JSR← 2D11 JSR
LDX last_slot ; For slots &4F to 0, free ones included...
2B78 .search_slots←1← 2B8B BPL
LDA slot_x,x ; ...the slot's column...
2B7B CMP cursor_x ; ...the cursor's column?
2B7D BNE next_slot ; No: next slot
2B7F LDA slot_y,x ; ...and the slot's row...
2B82 CMP cursor_y ; ...the cursor's row?
2B84 BNE next_slot ; No: next slot
2B86 LDA slot_item,x ; Yes: return its item (0 for a free slot)...
2B89 RTS ; ...with X its slot
2B8A .next_slot←2← 2B7D BNE← 2B84 BNE
DEX ; Next slot down
2B8B BPL search_slots ; ...until slot 0 is tried
2B8D JSR wall_bit ; No item: is there a wall?
2B90 LDA (map_ptr),y ; ...the cell's wall-map byte...
2B92 AND bit_mask ; ...its bit for the cell
2B94 BEQ nothing_here ; Clear: nothing here
2B96 LDA #0 ; Set: item 0, the wall
2B98 RTS ; Return 0: a wall (Z set)
2B99 .nothing_here←1← 2B94 BEQ
ORA #&ff ; No wall: &FF
2B9B RTS ; Return &FF: nothing (N set)

Find the cursor's bit in the wall map

As the game lays out its maps: the byte at (y AND &F8) x 4 + (y AND 7) + (x AND &F8), bit 7 - (x AND 7). So each room of 8 by 8 cells is 8 consecutive bytes, one per cell row, with the leftmost cell in bit 7; four rooms make a 32-byte row of rooms, and six rows of rooms the map. Used to test, set and clear walls, a door's included. mark_object_cell repeats the calculation for the object-free map.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitMAP_PTRaddress of the byte
BIT_MASKthe bit
Athe bit
Xcorrupted
Y0
2B9C .wall_bit←4← 2B8D JSR← 2BDE JSR← 2C19 JSR← 2C70 JSR
LDA cursor_y ; Byte = (y AND &F8) x 4 + (y AND 7) + (x AND &F8)
2B9E AND #&f8 ; ...the room row, as a multiple of 8...
2BA0 ASL ; ...x 2...
2BA1 ASL ; ...x 4: 32 bytes per row of rooms
2BA2 STA map_ptr ; ...the row of rooms' offset so far
2BA4 LDA cursor_y ; ...+ (y AND 7)...
2BA6 AND #7 ; ...the cell's row within the room
2BA8 CLC ; ...C clear for the addition
2BA9 ADC map_ptr ; ...added to the offset
2BAB STA map_ptr ; ...the offset so far
2BAD LDA cursor_x ; ...+ (x AND &F8)...
2BAF AND #&f8 ; ...the room's column x 8: its 8 bytes
2BB1 CLC ; ...C clear for the addition
2BB2 ADC map_ptr ; ...added to the offset
2BB4 STA map_ptr ; ...map_ptr: the cell's byte
2BB6 LDA #>(wall_map) ; ...in wall_map, page-aligned...
2BB8 STA map_ptr_hi ; ...as map_ptr's high byte
2BBA LDA cursor_x ; Bit 7 - (x AND 7)...
2BBC AND #7 ; ...the cell's column within the room...
2BBE STA bit_mask ; ...kept to subtract...
2BC0 LDA #7 ; ...from 7...
2BC2 SEC ; ...C set for the subtraction
2BC3 SBC bit_mask ; ...7 - it: the bit, leftmost cell in bit 7
2BC5 BEQ bit_0 ; Bit 0: the mask is 1
2BC7 TAX ; ...the bit number as a count...
2BC8 LDA #1 ; ...from bit 0...
2BCA .shift_bit←1← 2BCC BNE
ASL ; Shift the mask up a bit...
2BCB DEX ; ...one shift fewer to go
2BCC BNE shift_bit ; ...until it is in place
2BCE BEQ bit_found ; ...(always branches: X reached 0)
2BD0 .bit_0←1← 2BC5 BEQ
LDA #1 ; ...the bit 0 mask
2BD2 .bit_found←1← 2BCE BEQ
STA bit_mask ; ...as bit_mask
2BD4 LDY #0 ; Y = 0 for (map_ptr),Y
2BD6 RTS ; Return with the cell's byte and bit

Put the current item in the cursor's cell

place_item's last step, once the item is allowed. Clear the cell's object-free bit, for every item, walls included; nothing in the editor sets it again, so deleted items leave clear bits. A wall sets the wall map's bit; anything else takes a free slot, and a key is recorded as the key of the link being made.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
CURRENT_ITEMthe item to put there
LINK_INDEXfor a key, its door's link
On ExitAcorrupted
Xcorrupted
Ycorrupted
2BD7 .put_item←1← 2CA0 JMP
JSR mark_object_cell ; Clear the cell's object-free bit, whatever the item
2BDA LDA current_item ; Putting down a wall?
2BDC BNE put_in_slot ; No: put it in a slot
2BDE JSR wall_bit ; Yes: set its bit in the wall map
2BE1 LDA (map_ptr),y ; ...the cell's map byte...
2BE3 ORA bit_mask ; ...with the cell's bit set...
2BE5 STA (map_ptr),y ; ...back in the map
2BE7 RTS ; Return with the wall in the map
2BE8 .put_in_slot←1← 2BDC BNE
LDX last_slot ; Find a free slot, from &4F down...
2BEA .find_free_slot←1← 2BF0 BPL
LDA slot_item,x ; ...is its item 0?
2BED BEQ free_slot_found ; Yes: use it
2BEF DEX ; ...next slot down
2BF0 BPL find_free_slot ; ...until slot 0 is tried
2BF2 .free_slot_found←1← 2BED BEQ
LDA cursor_x ; ...and put the item in it: the cursor's column...
2BF4 STA slot_x,x ; ...as its column
2BF7 LDA cursor_y ; ...the cursor's row...
2BF9 STA slot_y,x ; ...as its row
2BFC LDA current_item ; ...the current item...
2BFE STA slot_item,x ; ...as the slot's item
2C01 CMP #spr_key ; Was it a key?
2C03 BNE put_item_done ; No: the item is placed
2C05 TXA ; Yes: this slot is the link's key...
2C06 LDX link_index ; ...of the link taken for the waiting door
2C09 STA link_key,x ; ...the key's slot in the link
2C0C .put_item_done←1← 2C03 BNE
RTS ; Return with the item in its slot

Remove what is in the cursor's cell

delete_item's last step, once it has uncounted the item and dealt with any door-key link. Free an item's slot, or clear a wall's bit in the wall map. Freeing a door's slot leaves its wall bit set, so the cell then shows a wall. The cell's object-free bit is not set again. The cell is redrawn by move_cursor on the next pass.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAcorrupted
Xcorrupted
Ycorrupted
2C0D .remove_at_cursor←1← 2D65 JMP
JSR item_at_cursor ; What is in the cursor's cell?
2C10 BEQ clear_wall ; A wall: clear its bit
2C12 BMI remove_done ; Nothing here: nothing to remove
2C14 JSR free_slot ; An item: free its slot, X from item_at_cursor
2C17 BEQ remove_done ; Return (always branches)

Clear the cursor's wall

Clear its bit in the wall map. remove_at_cursor's path for a wall, and delete_item's for a door deleted through its key, with the cursor moved to the door's cell.

2C19 .clear_wall←2← 2C10 BEQ← 2D9F JSR
JSR wall_bit ; Clear the cell's bit in the wall map
2C1C LDA bit_mask ; ...the cell's bit...
2C1E EOR #&ff ; ...inverted: every bit but the cell's...
2C20 STA bit_mask ; ...as a mask keeping the others
2C22 LDA (map_ptr),y ; ...the cell's map byte...
2C24 AND bit_mask ; ...with the cell's bit cleared...
2C26 STA (map_ptr),y ; ...back in the map
2C28 .remove_done←2← 2C12 BMI← 2C17 BEQ
RTS ; Return with the cell cleared

Place the current item at the cursor

In an empty cell, place the current item within the limits: one exit, eight trapdoors, 20 doors (a door is also a wall), 29 objects, and a key only when a door is waiting for one, which links the two. With a key chosen, SPACE on an unlinked door makes it the door waiting for its key (sprite &11), in place of any door waiting before. A door sets its wall bit before its count is checked, so a 21st door is refused but leaves a wall. Called from the edit loop on SPACE and by wall_edges. Apart from a door given back its own sprite, nothing is drawn here: move_cursor redraws the cell on the next pass. Refusals are silent.

A door and its key are linked in two steps. With the key chosen, SPACE on the door makes it wait (choose_door), and the next key placed anywhere becomes its key (place_key), taking a free link. A key with no door waiting is refused.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
CURRENT_ITEMthe item to place
On ExitAcorrupted
Xcorrupted
Ycorrupted
2C29 .place_item←6← 208C JSR← 20A2 JSR← 221C JSR← 2223 JSR← 2232 JSR← 2239 JSR
JSR item_at_cursor ; What is in the cursor's cell?
2C2C BMI place_in_empty ; Nothing: place the item
2C2E BEQ place_done ; A wall: nothing more can go here
2C30 CMP #spr_door_vertical ; A vertical door?
2C32 BEQ choose_door ; Yes: perhaps make it wait for a key
2C34 CMP #spr_door_horizontal ; ...or a horizontal door?
2C36 BEQ choose_door ; Yes: perhaps make it wait for a key
2C38 .place_done←1← 2C2E BEQ
RTS ; Occupied: refuse
; An empty cell: place the item, if its limit allows.
2C39 .place_in_empty←1← 2C2C BMI
LDA current_item ; The current item: a wall?
2C3B BEQ place_put ; Yes: put it in the map
2C3D CMP #1 ; Placing the exit?
2C3F BEQ place_exit ; Yes: only one
2C41 CMP #3 ; Placing a trapdoor?
2C43 BEQ place_trapdoor ; Yes: at most 8
2C45 CMP #spr_door_vertical ; A vertical door?
2C47 BEQ place_door ; Yes: at most 20
2C49 CMP #spr_door_horizontal ; ...or a horizontal door?
2C4B BEQ place_door ; Yes: at most 20
2C4D CMP #spr_key ; Placing a key?
2C4F BEQ place_key ; Yes: only with a door waiting
2C51 INC object_count ; An object: count it...
2C53 LDA object_count ; ...how many now?
2C55 CMP #max_object ; ...30, one too many?
2C57 BNE place_put ; ...and place it if there are now at most 29
2C59 DEC object_count ; Too many: uncount it
2C5B RTS ; ...and refuse the 30th object
2C5C .place_exit←1← 2C3F BEQ
LDA exit_placed ; The exit: only one
2C5E BNE place_refused ; Placed already: refuse
2C60 INC exit_placed ; ...it is placed now...
2C62 BNE place_put ; ...put it in (always branches)
2C64 .place_refused←3← 2C5E BNE← 2C87 BMI← 2CA7 BNE
RTS ; Refuse: nothing placed
2C65 .place_trapdoor←1← 2C43 BEQ
INC trapdoor_count ; A trapdoor: at most 8
2C67 LDA trapdoor_count ; ...how many now?
2C69 CMP #9 ; ...9, one too many?
2C6B BNE place_put ; No: put it in
2C6D DEC trapdoor_count ; Yes: uncount it and refuse
2C6F RTS ; ...the ninth trapdoor refused
2C70 .place_door←2← 2C47 BEQ← 2C4B BEQ
JSR wall_bit ; A door: it is a wall too, set before the count...
2C73 LDA (map_ptr),y ; ...the cell's map byte...
2C75 ORA bit_mask ; ...with its bit set...
2C77 STA (map_ptr),y ; ...back in the map
2C79 INC door_count ; ...at most 20
2C7B LDA door_count ; ...how many now?
2C7D CMP #max_door + 1 ; ...21, one too many?
2C7F BNE place_put ; No: put it in
2C81 DEC door_count ; Yes: uncount it and refuse...
2C83 RTS ; ...leaving its wall bit set
; A key: only for the door waiting for one, which it is linked ; to.
2C84 .place_key←1← 2C4F BEQ
LDA pending_door_item ; A key: is a door waiting for one?
2C87 BMI place_refused ; No door waiting: refuse
2C89 JSR find_free_link ; Yes: take a free link...
2C8C LDX link_index ; ...for the waiting door...
2C8F LDA pending_door_slot ; ...the waiting door's slot...
2C92 STA link_door,x ; ...as the link's door
2C95 JSR restore_waiting_door ; ...give the door back its own sprite...
2C98 LDA #no_link ; ...and nothing waiting
2C9A STA pending_door_item ; ...no door is waiting now
2C9D JMP place_put ; Place the key, recorded in link_index's link
2CA0 .place_put←6← 2C3B BEQ← 2C57 BNE← 2C62 BNE← 2C6B BNE← 2C7F BNE← 2C9D JMP
JMP put_item ; Put the item in the wall map or a slot
; An occupied door with the key chosen: make it the door waiting ; for its key.
2CA3 .choose_door←2← 2C32 BEQ← 2C36 BEQ
LDA current_item ; A door with a key chosen?
2CA5 CMP #spr_key ; ...is the key the current item?
2CA7 BNE place_refused ; No: refuse
2CA9 STX bit_mask ; Yes: is the door linked already?
2CAB LDX #max_link ; ...over links 19 to 0
2CAD .find_linked_door←1← 2CB5 BPL
LDA link_door,x ; This link's door slot...
2CB0 CMP bit_mask ; ...this door's?
2CB2 BEQ door_linked ; Yes: it is linked
2CB4 DEX ; ...next link
2CB5 BPL find_linked_door ; ...until all 20 are tried
2CB7 BMI door_not_linked ; No: door not linked
2CB9 .door_linked←1← 2CB2 BEQ
RTS ; Linked already: nothing to do
2CBA .door_not_linked←1← 2CB7 BMI
LDX bit_mask ; Another door waiting?
2CBC LDA pending_door_item ; ...(the waiting door's sprite)
2CBF BMI door_waits ; None: make this door wait
2CC1 TXA ; Yes: show it as it was
2CC2 PHA ; ...keeping this door's slot
2CC3 JSR restore_waiting_door ; ...put its own sprite back
2CC6 PLA ; ...this door's slot back
2CC7 TAX ; ...into X, to make it wait
2CC8 .door_waits←1← 2CBF BMI
LDA slot_item,x ; This door is waiting: remember its sprite and slot...
2CCB STA pending_door_item ; ...the sprite kept in pending_door_item...
2CCE STX pending_door_slot ; ...and its slot
2CD1 LDA #spr_waiting_door ; ...and show it as waiting (sprite &11)
2CD3 STA slot_item,x ; ...in place of the door's own
2CD6 RTS ; Return with the door waiting for its key

Show the waiting door as it was

Put the waiting door's own sprite back in its slot and plot it, by falling into plot_slot. Used by place_key when its key is placed, and by choose_door when another door is chosen to wait instead. pending_door_item is left for the caller to clear or replace.

On EntryPENDING_DOOR_ITEMthe waiting door's own sprite
PENDING_DOOR_SLOTits slot
On ExitAcorrupted
Xcorrupted
Ycorrupted
2CD7 .restore_waiting_door←2← 2C95 JSR← 2CC3 JSR
LDX pending_door_slot ; The waiting door's slot...
2CDA LDA pending_door_item ; ...and its own door sprite...
2CDD STA slot_item,x ; ...back in the slot, then plotted
fall through ↓

Plot a sprite at a slot's cell

At screen byte column 2x and pixel row 5y of slot X's cell: plot_at_cursor for a cell away from the cursor: a waiting door given back its sprite, and the blank sprite over the key or door deleted with its partner.

On EntryAsprite number
Xslot
On ExitAcorrupted
Xcorrupted
Ycorrupted
2CE0 .plot_slot←2← 2D58 JSR← 2D8E JSR
STA sprite_number ; The sprite for plot_sprite...
2CE2 LDA slot_x,x ; ...at column 2x...
2CE5 ASL ; ...x 2: two screen bytes per cell
2CE6 STA plot_x ; ...as plot_x
2CE8 LDA slot_y,x ; ...and row 5y
2CEB ASL ; ...x 2...
2CEC ASL ; ...x 4...
2CED CLC ; ...C clear for the addition
2CEE ADC slot_y,x ; ...+ the row: five pixel rows per cell
2CF1 STA plot_y ; ...as plot_y
2CF3 JSR plot_sprite ; Plot it in the slot's cell
2CF6 RTS ; Return with the sprite drawn

Find a free door-key link

The highest link whose door is &FF, for the key being placed for the waiting door; place_key fills in its door and put_item its key. Were none free, link_index would be &FF, but a door can wait only while fewer than 20 doors are linked, so one should always be free.

On ExitLINK_INDEXthe link
Xthe link
Acorrupted
2CF7 .find_free_link←1← 2C89 JSR
LDX #max_link ; For links 19 down...
2CF9 .search_links←1← 2CFF BPL
LDA link_door,x ; ...is this link's door slot...
2CFC BMI free_link_found ; ...&FF (negative)? Free: take it
2CFE DEX ; ...next link down
2CFF BPL search_links ; ...until all 20 are tried
2D01 .free_link_found←1← 2CFC BMI
STX link_index ; The link the waiting door and key will use
2D04 RTS ; Return with the free link in link_index

Free a slot

Zero its item and position. A free slot is therefore item 0 at (0, 0), as start_editing leaves every slot; item_at_cursor reads that as a wall in the top-left cell. Used by remove_at_cursor and by delete_item for the partner of a deleted door or key; nothing is drawn.

On EntryXslot
On ExitA0
Zset
2D05 .free_slot←3← 2C14 JSR← 2D5D JSR← 2D93 JSR
LDA #0 ; Zero its item...
2D07 STA slot_item,x ; ...so the slot is free
2D0A STA slot_x,x ; ...and position: column 0...
2D0D STA slot_y,x ; ...and row 0, the top-left cell
2D10 RTS ; Return with A = 0 and Z set

Delete what is in the cursor's cell

TAB in the edit loop, which calls show_counts after it. Uncount the item and remove it (remove_at_cursor). Deleting a door frees its link and removes its key too, but leaves the door's wall bit, so a wall shows where the door was; deleting a key frees its link and removes its door and the door's wall, moving the cursor to the door and back to do so. A door waiting for its key cannot be deleted; choosing another door to wait releases it. A wall is cleared. The edit loop repeats the delete each pass while TAB is held (see edit_loop).

A key with no link, which the editor never makes, would fall out of the link search into line_x_to_graphics and return with the key still in place.

On EntryCURSOR_Xthe cell's column
CURSOR_Ythe cell's row
On ExitAcorrupted
Xcorrupted
Ycorrupted
2D11 .delete_item←1← 20BA JSR
JSR item_at_cursor ; What is in the cursor's cell?
2D14 BMI delete_remove ; Nothing: remove_at_cursor finds nothing either
2D16 BEQ delete_remove ; A wall: clear it
2D18 CMP #spr_waiting_door ; A waiting door?
2D1A BNE delete_item_slot ; No: delete the item
2D1C RTS ; Yes: it cannot be deleted
; Uncount the item, by its kind.
2D1D .delete_item_slot←1← 2D1A BNE
STX bit_mask ; Keep the item's slot, to match against links
2D1F CMP #1 ; Deleting the exit?
2D21 BEQ delete_exit ; Yes: the exit is no longer placed
2D23 CMP #3 ; Deleting a trapdoor?
2D25 BEQ delete_trapdoor ; Yes: one fewer trapdoor
2D27 CMP #spr_door_vertical ; Deleting a vertical door?
2D29 BEQ delete_door ; Yes: unlink it and delete its key
2D2B CMP #spr_door_horizontal ; ...or a horizontal door?
2D2D BEQ delete_door ; Yes: unlink it and delete its key
2D2F CMP #spr_key ; Deleting a key?
2D31 BEQ delete_key ; Yes: delete its door too
2D33 DEC object_count ; An object: one fewer
2D35 BPL delete_remove ; ...remove it (always branches)
2D37 .delete_exit←1← 2D21 BEQ
LDA #0 ; The exit: none placed
2D39 STA exit_placed ; ...exit_placed = 0, so saving waits
2D3B BEQ delete_remove ; ...remove it (always branches)
2D3D .delete_trapdoor←1← 2D25 BEQ
DEC trapdoor_count ; A trapdoor: one fewer
2D3F BPL delete_remove ; ...remove it (always branches)
; A door: free its link, and rub out and free its key.
2D41 .delete_door←2← 2D29 BEQ← 2D2D BEQ
DEC door_count ; A door: uncount it...
2D43 LDX #max_link ; ...find its link...
2D45 .find_door_link_to_delete←1← 2D63 BPL
LDA link_door,x ; This link's door slot...
2D48 CMP bit_mask ; ...this door's?
2D4A BNE next_door_link ; No: next link
2D4C LDA #no_link ; Yes: free the link...
2D4E STA link_door,x ; ...in the link table
2D51 LDA link_key,x ; ...rub out its key...
2D54 TAX ; ...the key's slot into X...
2D55 PHA ; ...and kept on the stack
2D56 LDA #spr_blank ; ...the blank sprite...
2D58 JSR plot_slot ; ...rub it out in its cell
2D5B PLA ; ...the key's slot back...
2D5C TAX ; ...into X for free_slot
2D5D JSR free_slot ; ...and free the key's slot
2D60 BEQ delete_remove ; ...then remove the door (always branches)
2D62 .next_door_link←1← 2D4A BNE
DEX ; Next link down
2D63 BPL find_door_link_to_delete ; ...until all 20 are tried
2D65 .delete_remove←7← 2D14 BMI← 2D16 BEQ← 2D35 BPL← 2D3B BEQ← 2D3F BPL← 2D60 BEQ← 2DAC JMP
JMP remove_at_cursor ; Remove the item itself, or clear the wall
; A key: free its link, and rub out, free and unwall its door, ; from the door's cell.
2D68 .delete_key←1← 2D31 BEQ
LDA cursor_x ; A key: keep its cell, the cursor's...
2D6A STA line_x ; ...column in line_x...
2D6D LDA cursor_y ; ...and its row...
2D6F STA line_y ; ...in line_y, to come back to
2D72 LDX #max_link ; Find the key's link...
2D74 .find_key_link←1← 2DB0 BPL
LDA link_key,x ; This link's key slot...
2D77 CMP bit_mask ; ...this key's?
2D79 BNE next_key_link ; No: next link
2D7B TXA ; Yes: keep the link number...
2D7C PHA ; ...on the stack
2D7D LDA link_door,x ; ...take its door's slot...
2D80 TAX ; ...into X, to index the slots
2D81 PHA ; ...and kept on the stack
2D82 LDA slot_x,x ; ...and move the cursor to the door's cell...
2D85 STA cursor_x ; ...as the cursor's column...
2D87 LDA slot_y,x ; ...the door's row...
2D8A STA cursor_y ; ...as the cursor's row
2D8C LDA #spr_blank ; ...rub out the door...
2D8E JSR plot_slot ; ...in its cell
2D91 PLA ; ...free its slot: the slot back...
2D92 TAX ; ...into X for free_slot
2D93 JSR free_slot ; ...the door's slot freed
2D96 PLA ; Pull the link number back...
2D97 TAX ; ...into X, to free the link
2D98 DEC door_count ; Uncount the door: one fewer
2D9A LDA #no_link ; Free the link...
2D9C STA link_door,x ; ...in the link table
2D9F JSR clear_wall ; Clear the door's wall, at the cursor...
2DA2 LDA line_x ; ...and move the cursor back to the key's cell
2DA5 STA cursor_x ; ...as the cursor's column
2DA7 LDA line_y ; ...the key's row...
2DAA STA cursor_y ; ...as the cursor's row
2DAC JMP delete_remove ; Remove the key
2DAF .next_key_link←1← 2D79 BNE
DEX ; Next link down
2DB0 BPL find_key_link ; ...until all 20 are tried; none: fall through
fall through ↓

Convert line_x to graphics x

line_gx = line_x x 32: a cell is two screen bytes, four MODE 2 pixels, 32 graphics units wide. (delete_item also falls into it, harmlessly, when a key has no link.)

On EntryLINE_Xa map column
On ExitLINE_GXits graphics x, column x 32
Acorrupted
Xpreserved
Ypreserved
2DB2 .line_x_to_graphics←1← 2E0B JSR
LDA #0 ; Clear line_gx, the result...
2DB4 STA line_gx ; ...its low byte...
2DB7 STA line_gx_hi ; ...and its high byte
2DBA LDA line_x ; line_gx = column x 32...
2DBD ASL ; ...x 2...
2DBE ASL ; ...x 4...
2DBF ASL ; ...x 8 (a column is below 32: no carry)
2DC0 CLC ; ...C clear (the ASL sets it anyway)
2DC1 ASL ; ...x 16...
2DC2 ROL line_gx_hi ; ...carry into the high byte
2DC5 ASL ; ...x 32, with the carries in the high byte
2DC6 ROL line_gx_hi ; ...carry into the high byte
2DC9 STA line_gx ; ...the low byte, stored last
2DCC RTS ; Return with the column's graphics x

Convert line_y to graphics y

line_gy = &3FF - line_y x 20: a cell is five pixel rows, 20 graphics units, counted down from the top of the screen.

On EntryLINE_Ya map row
On ExitLINE_GYits graphics y, &3FF - row x 20
Acorrupted
Xpreserved
Ypreserved
2DCD .line_y_to_graphics←1← 2E11 JSR
LDA #0 ; Clear line_gy, the result...
2DCF STA line_gy ; ...its low byte...
2DD2 STA line_gy_hi ; ...and its high byte
2DD5 LDA line_y ; line_gy = &3FF - row x 20: row x 4...
2DD8 ASL ; ...x 2...
2DD9 ASL ; ...x 4 (a row is below 48: no carry)
2DDA STA bit_mask ; ...row x 4, kept in bit_mask to add
2DDC CLC ; ...C clear (the ASL sets it anyway)
2DDD ASL ; ...x 8...
2DDE ROL line_gy_hi ; ...carry into the high byte
2DE1 ASL ; ...x 16...
2DE2 ROL line_gy_hi ; ...carry into the high byte
2DE5 STA line_gy ; ...row x 16's low byte kept
2DE8 CLC ; ...plus x 4: x 20 graphics units a row
2DE9 ADC bit_mask ; ...row x 4 added...
2DEB STA line_gy ; ...row x 20's low byte
2DEE LDA line_gy_hi ; ...and the high byte...
2DF1 ADC #0 ; ...with the carry
2DF3 STA line_gy_hi ; ...row x 20's high byte
2DF6 LDA #&ff ; &3FF - row x 20, counting down from the top
2DF8 SEC ; ...C set for the subtraction
2DF9 SBC line_gy ; ...less row x 20, low byte...
2DFC STA line_gy ; ...line_gy's low byte
2DFF LDA #3 ; ...&3FF's high byte...
2E01 SBC line_gy_hi ; ...less the high byte...
2E04 STA line_gy_hi ; ...line_gy's high byte: row 0 at the top
2E07 RTS ; Return with the row's graphics y

PLOT to a cell

VDU 25 with plot code X and the graphics position of (line_x, line_y), the top-left corner of that cell. draw_links calls it twice a link: PLOT 68 to move to the door, PLOT 5 to draw to the key. (The A loaded before each conversion is not used.)

On EntryXPLOT code
LINE_Xthe cell's column
LINE_Ythe cell's row
On ExitAcorrupted
Xpreserved
Ypreserved
BIT_MASKcorrupted
2E08 .plot_line_point←2← 2E4E JSR← 2E66 JSR
LDA line_x ; The cell's column (unused: read from line_x)...
2E0B JSR line_x_to_graphics ; ...converted to graphics x
2E0E LDA line_y ; ...the cell's row (likewise unused)...
2E11 JSR line_y_to_graphics ; ...converted to graphics y
2E14 LDA #&19 ; VDU 25, X, x, y: PLOT there...
2E16 JSR oswrch ; ...send VDU 25, PLOT
2E19 TXA ; ...the PLOT code, in X
2E1A JSR oswrch ; ...send the PLOT code
2E1D LDA line_gx ; ...graphics x, low byte...
2E20 JSR oswrch ; ...send the x low byte
2E23 LDA line_gx_hi ; ...graphics x, high byte...
2E26 JSR oswrch ; ...send the x high byte
2E29 LDA line_gy ; ...graphics y, low byte...
2E2C JSR oswrch ; ...send the y low byte
2E2F LDA line_gy_hi ; ...graphics y, high byte...
2E32 JSR oswrch ; ...send the y high byte
2E35 RTS ; Return with the point plotted

Draw each door-key link as a line

For each link in use, move to the door's cell (PLOT 68) and draw a line to its key's (PLOT 5). The lines are exclusive-OR (GCOL 3, set at start-up), so the edit loop draws them when CTRL is pressed and again, rubbing them out, when it is released. Each line runs between the cells' top-left corners.

On ExitAcorrupted
X&FF
Ypreserved
2E36 .draw_links←2← 20D2 JSR← 20DC JSR
LDX #max_link ; For links 19 to 0...
2E38 .draw_link←1← 2E6C BPL
TXA ; Keep the link number...
2E39 PHA ; ...on the stack, as X indexes the slots
2E3A LDA link_door,x ; ...is the link in use?
2E3D BMI next_drawn_link ; Free: next link
2E3F TAX ; Yes: from the door's cell...
2E40 LDA slot_x,x ; ...the door's column...
2E43 STA line_x ; ...as line_x
2E46 LDA slot_y,x ; ...the door's row...
2E49 STA line_y ; ...as line_y
2E4C LDX #&44 ; ...PLOT 68: move absolute...
2E4E JSR plot_line_point ; ...to the door's cell, drawing nothing
2E51 PLA ; ...to the key's cell: the link number...
2E52 PHA ; ...kept on the stack again
2E53 TAX ; ...link number into X
2E54 LDA link_key,x ; ...the key's slot...
2E57 TAX ; ...into X, to index the slots
2E58 LDA slot_x,x ; ...the key's column...
2E5B STA line_x ; ...as line_x
2E5E LDA slot_y,x ; ...the key's row...
2E61 STA line_y ; ...as line_y
2E64 LDX #5 ; ...PLOT 5: line to there...
2E66 JSR plot_line_point ; ...draw the link, exclusive-OR (GCOL 3)
2E69 .next_drawn_link←1← 2E3D BMI
PLA ; Next link: the link number back...
2E6A TAX ; ...into X, the countdown
2E6B DEX ; ...the link before
2E6C BPL draw_link ; ...until link 0 is done
2E6E RTS ; Return with every link drawn (or erased)

Unused

Leftover bytes.

2E6F EQUB &F8, &A5, &6A, &60, &20, &A6, &11, &A2, &0B, ; Not &A0, &18, &A9, &94, &85, &0A, &A9, &2E, &85, ; used &0B, &20, &A6, &11, &A2, &08, &A0, &1A, &A9, &A8, &85, &0A, &A9, &2E, &85

Test a key

move_cursor's copy of key_pressed: OSBYTE &81 with the INKEY number in X, from key_left, key_right, key_up or key_down.

On EntryXINKEY number
On ExitZset if pressed
Acorrupted
Xcorrupted
Ycorrupted
2E90 .cursor_key_pressed←4← 2B14 JSR← 2B1F JSR← 2B2E JSR← 2B3F JSR
LDY #&ff ; Y = &FF: test the key whose INKEY number is X...
2E92 LDA #osbyte_inkey ; ...with OSBYTE &81, scan the keyboard
2E94 JSR osbyte ; Scan it: X = Y = &FF if the key is held Test whether the key with negative INKEY number X is pressed
2E97 CPX #&ff ; Z set if X and Y are both &FF
2E99 BNE cursor_key_done ; X not &FF: not pressed (Z clear)
2E9B CPY #&ff ; ...Z set if Y is &FF too: pressed
2E9D .cursor_key_done←1← 2E99 BNE
RTS ; Return with Z set if the key is held