Download a Python translation here.
DEFSECT ".ram", DATA AT 14E0H
SECT ".ram"
$14E0 RemainingPlays: DS 1
$14E1 KeyPad: DS 1
$14E2 NumGameStructs: DS 1
$14E3 LoopN: DS 1
$14E4 GameStructs: DS 64 ; array of 8-byte structures
$1524 NextGameStruct: DS 2
$1526 NextTilePos: DS 2
$1528 CurrentRowStart: DS 1
$1529 FinalNameRowStartMin48: DS 1
$152A FinalNameRowStart: DS 1
$152B MoveCooldown: DS 1
$152C KeyHistory: DS 1
$152D TotalAnimFrames: DS 2
$152F FramesRemaining: DS 1 ; "rendered frames" remaining for a frame
$1530 BlankTilemap: DS 96
$1590 GameSelectMap: DS 96
$15F0 GameNamesMap: DS 96
$1650 InsertCartMaps: DS 180
$1704 LowBatteryMaps: DS 300
$1830 TilesetBase: DS 328
VFlipStart EQU 1920h
$1978 VFlippedTiles: DS 88
$19D0 GameNameTiles: DS 576
DEFSECT ".bios", CODE AT 0000H
SECT ".bios"
; Vector table
$0000 DW _reset_vector
$0002 DW _reset2
$0004 DW _reset2
$0006 DW _prc_frame_copy_irq
$0008 DW _prc_render_irq
$000A DW _timer_2h_underflow_irq
$000C DW _timer_2l_underflow_irq
$000E DW _timer_1h_underflow_irq
$0010 DW _timer_1l_underflow_irq
$0012 DW _timer_3h_underflow_irq
$0014 DW _timer_3_cmp_irq
$0016 DW _timer_32hz_irq
$0018 DW _timer_8hz_irq
$001A DW _timer_2hz_irq
$001C DW _timer_1hz_irq
$001E DW _ir_rx_irq
$0020 DW _shake_irq
$0022 DW _reset2
$0024 DW _reset2
$0026 DW _cart_eject_irq
$0028 DW _cartridge_irq
$002A DW _key_power_irq
$002C DW _key_right_irq
$002E DW _key_left_irq
$0030 DW _key_down_irq
$0032 DW _key_up_irq
$0034 DW _key_c_irq
$0036 DW _key_b_irq
$0038 DW _key_a_irq
$003A DW _unknown_irq0
$003C DW _unknown_irq1
$003E DW _unknown_irq2
$0040 DW 0FFF1h
$0042 DW _suspend_system
$0044 DW _sleep
$0046 DW _sleep_with_display
$0048 DW _shutdown
$004A DW _sleep2
$004C DW _default_contrast
$004E DW _change_contrast
$0050 DW _apply_default_contrast
$0052 DW _get_default_contrast
$0054 DW _set_temp_contrast
$0056 DW _lcd_on
$0058 DW _reset_lcd
$005A DW _lcd_off
$005C DW _ena_ram_vec
$005E DW _dis_ram_vec
$0060 DW _disable_cart_eject_irq_if
$0062 DW _enable_cart_eject_irq_if
$0064 DW _unknown_eject1
$0066 DW _unknown_eject2
$0068 DW _dev_card0
$006A DW _dev_card1
$006C DW _enable_cart_eject_irq
$006E DW _disable_cart_eject_irq
$0070 DW _check_ejected
$0072 DW _enter_normal_operation
$0074 DW _enter_low_power_operation
$0076 DW _cart_slot_off
$0078 DW _cart_slot_on
$007A DW _cart_detect
$007C DW _read_structure
$007E DW _set_prc_rate
$0080 DW _get_prc_rate
$0082 DW _game_id_valid
$0084 DW _flash_read_ids
$0086 DW _flash_reset
$0088 DW _flash_program_byte
$008A DW _flash_erase_sector
$008C DW _flash_enter_bank_select_mode
$008E DW _flash_select_bank
$0090 DW _flash_allow_remapping_page_0
$0092 DW _flash_remap_ids
$0094 DW _flash_prepare_game
$0096 DW 0000h
$0098 DW _ir_pulse
hdr_nintendo EQU 21A4h
_reset_vector:
$009A LD EP, #00h ; address hardware registers
$009D LD BR, #20h ; ...
$009F LD [BR:08h], #02h ; reset seconds timer
$00A2 OR [BR:08h], #01h ; start seconds timer
$00A5 LD [BR:00h], #7Ch ; set default contrast to 31 (~50%)
$00A8 LD [BR:02h], #00h ; initialize to 0
_reset2:
$00AB LD SC, #0C0h ; interrupt flags to NMI-only
$00AD LD SP, #2000h ; initial stack location (end of RAM)
$00B1 CARL initregs ; initialize registers for BIOS's sake
$00B4 LD [BR:80h], A ; disable LCD controller (A = 0)
$00B6 CARL enter_normal_operation ; clock CPU with OSC3
$00B9 LD NB, #01h ; set default NB
$00BC JRS copy_nb_to_cb
copy_nb_to_cb:
$00BE LD A, [BR:52h] ; store initial keypad state
$00C0 LD [KeyPad], A ; ...to $14E1
$00C4 OR [BR:00h], #03h ; send Chip Enable (CE) signals
$00C7 LD [BR:01h], #30h ; set console is awake and ??
$00CA OR [BR:02h], #0C0h ; ??
$00CD CARL init_io
$00D0 LD [BR:80h], #02h ; enable tilemap (for BIOS screens)
$00D3 LD [BR:81h], #09h ; set frame divider to 2 and allow init_display to work
$00D6 LD IX, #TilesetBase ; tileset base for the screens
$00D9 LD [02082h], IX ; ...
$00DC LD [BR:84h], #00h ; ... (why not B? macro?)
$00DF LD [BR:85h], B ; init map scroll to 0
$00E1 LD [BR:86h], B ; ...
$00E3 LD [BR:87h], B ; init sprite set base to 0
$00E5 LD [BR:88h], B ; ...
$00E7 LD [BR:89h], B ; ...
$00E9 CARL power_cart ; power the cartridge
$00EC CARL wait_8000_cycles
$00EF LD [BR:28h], #02h ; enable cart eject IRQ
$00F2 LD SC, #80h ; interrupt flags to IRQ3+
$00F4 LD [BR:0FEh], #0E3h ; send NOP command to LCD driver
$00F7 LD H, #0B0h ; set page 0 command
loop_from_B0_to_B8: ; loop through all display pages
$00F9 LD B, #60h ; 96 columns (screen width)
$00FB LD [BR:0FEh], H ; send set page command
$00FD LD [BR:0FEh], #10h ; send set column 0 command
$0100 LD [BR:0FEh], #00h ; ...
clear_96_vertical_lines:
$0103 LD [BR:0FFh], #00h ; clear column (and increment cursor)
$0106 DJR NZ, clear_96_vertical_lines
$0108 INC H ; next page
$0109 CP H, #0B8h ; stop at last display page
$010C JRS NZ, loop_from_B0_to_B8
$010E CARL apply_default_contrast
$0111 XOR A, A ; wait between setup and display on
$0112 CARL init_display
$0115 CARL decode_picture ; load tiles/maps into RAM
$0118 BIT [BR:53h], #02h ; check if cart is inserted
$011B JRS Z, check_voltage ; 0 = cart is inserted
$011D JRL insert_cart_screen ; if no cart is inserted...
check_voltage:
$0120 OR [BR:10h], #10h ; write 1 to SVDON
$0123 LD B, #32h ; wait 202 cycles
cv__wait_200:
$0125 DJR NZ, cv__wait_200
$0127 AND [BR:10h], #~10h ; write 0 to SVDON
$012A BIT [BR:10h], #20h ; check battery level
$012D JRS Z, cv__sufficient ; 0 = sufficient voltage supply
$012F CARL low_battery_screen
cv__sufficient:
$0132 JRL check_cart_type
; ----------------------
nintendo_check:
$0135 LD SP, #2000h
$0139 LD B, #08h ; 8 letters in 'NINTENDO'
$013B LD IX, #hdr_nintendo ; offset: start of 'NINTENDO' signature in game header
$013E LD IY, #nintendo_string ; offset 0E1Ah: start of 'NINTENDO' field in BIOS, used for checking header
nc__loop:
$0141 LD A, [IX] ; compare byte at IX with byte at IY
$0142 CP A, [IY] ; ...
$0143 JRL NZ, insert_cart_screen ; if they're not the same, jump down to insert_cart_screen
$0146 INC IX ; else, increment IX and IY to point to the next part of the 'NINTENDO' string
$0147 INC IY ; ..
$0148 DJR NZ, nc__loop ; and JDBNZ back to the start of the loop
prepare_boot:
$014A XOR A, A ; set A to 0
$014B LD [BR:80h], A ; disable LCD controller
$014D LD [BR:81h], #07h ; set frame divider to 12
$0150 LD [BR:82h], A ; set tileset base to 0
$0152 LD [BR:83h], A ; ...
$0154 AND [BR:01h], #~10h ; ??
$0157 CARS initregs ; reset registers
$0159 LD H, [KeyPad] ; load initial keypad into H (loaded into 0x14E1 by _reset_vector)
$015D LD L, [RemainingPlays] ; < 0 = unlimited
$0161 LD IX, #1000h ; clear RAM
pb__loop: ; ...
$0164 LD [IX], A ; ...
$0165 INC IX ; ...
$0166 CP IX, #2000h ; ...
$0169 JRS NZ, pb__loop ; ...
$016B LD IX, BA ; clear registers
$016D LD BR, A ; ...
$016F LD BA, HL ; ...except forward info in BA
$0171 LD HL, IX ; ...
$0173 AND SC, #0C0h ; keep only interrupt flags
$0175 JRL 2102h ; jump to game entrypoint
; ----------------------
; Returns:
; BR: $20
; BA, EP, XP, YP, HL, IX, IY: 0
initregs:
$0178 LD BR, #20h ; set BR to 20 for hardware register area
$017A XOR A, A ; set all other general purpose registers to 0
$017B LD B, A ; ...
$017C LD EP, A ; ...
$017E LD XP, A ; ...
$0180 LD YP, A ; ...
$0182 LD HL, BA ; ...
$0184 LD IX, BA ; ...
$0186 LD IY, BA ; ...
$0188 RET
; ----------------------
; Returns:
; B: 0
; Clobbers: A
init_io:
$0189 LD B, #00h
$018B LD [BR:10h], #08h ; set SYS_BATT to 8
$018E AND [BR:19h], #~30h ; disable OSC1/OSC3 for timer use
$0191 LD [BR:20h], B ; set IRQ priorities to none (don't fire)
$0193 LD [BR:21h], #30h ; ...except set cartridge IRQs' to 3
$0196 LD [BR:22h], #02h ; ...except set IR/shock to 2
$0199 LD [BR:23h], B ; disable IRQs
$019B LD [BR:24h], #02h ; ...except cart eject IRQ
$019E LD [BR:25h], B ; ...
$01A0 LD [BR:26h], B ; ...
$01A2 LD [BR:40h], B ; disable clock timer
$01A4 LD [BR:44h], B ; ??
$01A6 LD [BR:50h], #0FFh ; set keypad IRQs to falling edge detection (on press)
$01A9 LD [BR:51h], B ; set cart eject and ? IRQs to rising edge (ejected)
$01AB LD A, [BR:54h] ; set input level check time for ?? to 4ms?
$01AD AND A, #~07h ; ...
$01AF OR A, #01h ; ...
$01B1 LD [BR:54h], A ; ...
$01B3 LD [BR:55h], #01h ; set input level check time for ?? to 4ms?
$01B6 OR [BR:60h], #0Ch ; set EEPROM data/clock directions to output
$01B9 AND [BR:61h], #~04h ; set EEPROM data bit low
$01BC OR [BR:61h], #08h ; set EEPROM clock bit high
$01BF OR [BR:61h], #04h ; set EEPROM data bit high
$01C2 LD [BR:61h], #20h ; set all ports low except IR disable(?)
$01C5 LD [BR:60h], #32h ; IR disable(?), rumble, IR TXD set to output
$01C8 LD [BR:62h], B ; ??
$01CA LD [BR:70h], B ; ??
$01CC LD [BR:71h], B ; ??
$01CE RET
; ----------------------
_prc_frame_copy_irq:
$01CF PUSH EP
$01D0 PUSH BR
$01D1 LD EP, #00h
$01D4 LD BR, #20h
$01D6 BIT [BR:01h], #20h ; if console is asleep,
$01D9 CARS Z, jrl1_wake_from_irq ; wake it up
$01DB POP BR
$01DC POP EP
$01DD JRL 2108h ; jump to software handler
; ----------------------
_prc_render_irq:
$01E0 PUSH EP
$01E1 PUSH BR
$01E2 LD EP, #00h
$01E5 LD BR, #20h
$01E7 BIT [BR:01h], #20h ; if console is asleep,
$01EA CARS Z, jrl1_wake_from_irq ; wake it up
$01EC POP BR
$01ED POP EP
$01EE JRL 210Eh ; jump to software handler
; ----------------------
_timer_2h_underflow_irq:
$01F1 PUSH EP
$01F2 PUSH BR
$01F3 LD EP, #00h
$01F6 LD BR, #20h
$01F8 BIT [BR:01h], #20h ; if console is asleep,
$01FB CARS Z, jrl1_wake_from_irq ; wake it up
$01FD POP BR
$01FE POP EP
$01FF JRL 2114h ; jump to software handler
; ----------------------
_timer_2l_underflow_irq:
$0202 PUSH EP
$0203 PUSH BR
$0204 LD EP, #00h
$0207 LD BR, #20h
$0209 BIT [BR:01h], #20h ; if console is asleep,
$020C CARS Z, jrl1_wake_from_irq ; wake it up
$020E POP BR
$020F POP EP
$0210 JRL 211Ah ; jump to software handler
; ----------------------
_timer_1h_underflow_irq:
$0213 PUSH EP
$0214 PUSH BR
$0215 LD EP, #00h
$0218 LD BR, #20h
$021A BIT [BR:01h], #20h ; if console is asleep,
$021D CARS Z, jrl1_wake_from_irq ; wake it up
$021F POP BR
$0220 POP EP
$0221 JRL 2120h ; jump to software handler
; ----------------------
_timer_1l_underflow_irq:
$0224 PUSH EP
$0225 PUSH BR
$0226 LD EP, #00h
$0229 LD BR, #20h
$022B BIT [BR:01h], #20h ; if console is asleep,
$022E CARS Z, jrl1_wake_from_irq ; wake it up
$0230 POP BR
$0231 POP EP
$0232 JRL 2126h ; jump to software handler
; ----------------------
_timer_3h_underflow_irq:
$0235 PUSH EP
$0236 PUSH BR
$0237 LD EP, #00h
$023A LD BR, #20h
$023C BIT [BR:01h], #20h ; if console is asleep,
$023F CARS Z, jrl1_wake_from_irq ; wake it up
$0241 POP BR
$0242 POP EP
$0243 JRL 212Ch ; jump to software handler
; ----------------------
_timer_3_cmp_irq:
$0246 PUSH EP
$0247 PUSH BR
$0248 LD EP, #00h
$024B LD BR, #20h
$024D BIT [BR:01h], #20h ; if console is asleep,
$0250 CARS Z, jrl1_wake_from_irq ; wake it up
$0252 POP BR
$0253 POP EP
$0254 JRL 2132h ; jump to software handler
; ----------------------
jrl1_wake_from_irq:
$0257 JRL wake_from_irq
; ----------------------
_timer_32hz_irq:
$025A PUSH EP
$025B PUSH BR
$025C LD EP, #00h
$025F LD BR, #20h
$0261 BIT [BR:01h], #20h ; if console is asleep,
$0264 CARS Z, jrl1_wake_from_irq ; wake it up
$0266 POP BR
$0267 POP EP
$0268 JRL 2138h ; jump to software handler
; ----------------------
_timer_8hz_irq:
$026B PUSH EP
$026C PUSH BR
$026D LD EP, #00h
$0270 LD BR, #20h
$0272 BIT [BR:01h], #20h ; if console is asleep,
$0275 CARS Z, jrl1_wake_from_irq ; wake it up
$0277 POP BR
$0278 POP EP
$0279 JRL 213Eh ; jump to software handler
; ----------------------
_timer_2hz_irq:
$027C PUSH EP
$027D PUSH BR
$027E LD EP, #00h
$0281 LD BR, #20h
$0283 BIT [BR:01h], #20h ; if console is asleep,
$0286 CARS Z, jrl1_wake_from_irq ; wake it up
$0288 POP BR
$0289 POP EP
$028A JRL 2144h ; jump to software handler
; ----------------------
_timer_1hz_irq:
$028D PUSH EP
$028E PUSH BR
$028F LD EP, #00h
$0292 LD BR, #20h
$0294 BIT [BR:01h], #20h ; if console is asleep,
$0297 CARS Z, jrl1_wake_from_irq ; wake it up
$0299 POP BR
$029A POP EP
$029B JRL 214Ah ; jump to software handler
; ----------------------
_ir_rx_irq:
$029E PUSH EP
$029F PUSH BR
$02A0 LD EP, #00h
$02A3 LD BR, #20h
$02A5 BIT [BR:01h], #20h ; if console is asleep,
$02A8 CARS Z, jrl1_wake_from_irq ; wake it up
$02AA POP BR
$02AB POP EP
$02AC JRL 2150h ; jump to software handler
; ----------------------
_shake_irq:
$02AF PUSH EP
$02B0 PUSH BR
$02B1 LD EP, #00h
$02B4 LD BR, #20h
$02B6 BIT [BR:01h], #20h ; if console is asleep,
$02B9 CARS Z, jrl1_wake_from_irq ; wake it up
$02BB POP BR
$02BC POP EP
$02BD JRL 2156h ; jump to software handler
; ----------------------
_cartridge_irq:
$02C0 PUSH EP
$02C1 PUSH BR
$02C2 LD EP, #00h
$02C5 LD BR, #20h
$02C7 BIT [BR:01h], #20h ; if console is asleep,
$02CA CARS Z, jrl1_wake_from_irq ; wake it up
$02CC POP BR
$02CD POP EP
$02CE JRL 219Eh ; jump to software handler
; ----------------------
_key_right_irq:
$02D1 PUSH EP
$02D2 PUSH BR
$02D3 LD EP, #00h
$02D6 LD BR, #20h
$02D8 BIT [BR:01h], #20h ; if console is asleep,
$02DB CARS Z, jrl2_wake_from_irq ; wake it up
$02DD POP BR
$02DE POP EP
$02DF JRL 2162h ; jump to software handler
; ----------------------
_key_left_irq:
$02E2 PUSH EP
$02E3 PUSH BR
$02E4 LD EP, #00h
$02E7 LD BR, #20h
$02E9 BIT [BR:01h], #20h ; if console is asleep,
$02EC CARS Z, jrl2_wake_from_irq ; wake it up
$02EE POP BR
$02EF POP EP
$02F0 JRL 2168h ; jump to software handler
; ----------------------
_key_down_irq:
$02F3 PUSH EP
$02F4 PUSH BR
$02F5 LD EP, #00h
$02F8 LD BR, #20h
$02FA BIT [BR:01h], #20h ; if console is asleep,
$02FD CARS Z, jrl2_wake_from_irq ; wake it up
$02FF POP BR
$0300 POP EP
$0301 JRL 216Eh ; jump to software handler
; ----------------------
_key_up_irq:
$0304 PUSH EP
$0305 PUSH BR
$0306 LD EP, #00h
$0309 LD BR, #20h
$030B BIT [BR:01h], #20h ; if console is asleep,
$030E CARS Z, jrl2_wake_from_irq ; wake it up
$0310 POP BR
$0311 POP EP
$0312 JRL 2174h ; jump to software handler
; ----------------------
_key_c_irq:
$0315 PUSH EP
$0316 PUSH BR
$0317 LD EP, #00h
$031A LD BR, #20h
$031C BIT [BR:01h], #20h ; if console is asleep,
$031F CARS Z, jrl2_wake_from_irq ; wake it up
$0321 POP BR
$0322 POP EP
$0323 JRL 217Ah ; jump to software handler
; ----------------------
_key_b_irq:
$0326 PUSH EP
$0327 PUSH BR
$0328 LD EP, #00h
$032B LD BR, #20h
$032D BIT [BR:01h], #20h ; if console is asleep,
$0330 CARS Z, jrl2_wake_from_irq ; wake it up
$0332 POP BR
$0333 POP EP
$0334 JRL 2180h ; jump to software handler
; ----------------------
_key_a_irq:
$0337 PUSH EP
$0338 PUSH BR
$0339 LD EP, #00h
$033C LD BR, #20h
$033E BIT [BR:01h], #20h ; if console is asleep,
$0341 CARS Z, jrl2_wake_from_irq ; wake it up
$0343 POP BR
$0344 POP EP
$0345 JRL 2186h ; jump to software handler
; ----------------------
_unknown_irq0:
$0348 PUSH EP
$0349 PUSH BR
$034A LD EP, #00h
$034D LD BR, #20h
$034F BIT [BR:01h], #20h ; if console is asleep,
$0352 CARS Z, jrl2_wake_from_irq ; wake it up
$0354 POP BR
$0355 POP EP
$0356 JRL 218Ch ; jump to software handler
; ----------------------
jrl2_wake_from_irq:
$0359 JRL wake_from_irq
; ----------------------
_unknown_irq1:
$035C PUSH EP
$035D PUSH BR
$035E LD EP, #00h
$0361 LD BR, #20h
$0363 BIT [BR:01h], #20h ; if console is asleep,
$0366 CARS Z, jrl2_wake_from_irq ; wake it up
$0368 POP BR
$0369 POP EP
$036A JRL 2192h ; jump to software handler
; ----------------------
_unknown_irq2:
$036D PUSH EP
$036E PUSH BR
$036F LD EP, #00h
$0372 LD BR, #20h
$0374 BIT [BR:01h], #20h ; if console is asleep,
$0377 CARS Z, jrl2_wake_from_irq ; wake it up
$0379 POP BR
$037A POP EP
$037B JRL 2198h ; jump to software handler
; ----------------------
; If you intend to wake the unit from something other than pressing the
; power key, write a JRL/RET instruction to $1FFD before sleeping!
; Or otherwise ensure $2001 is 0b0x0x0xx1 where x can be any.
wake_from_irq:
$037E BIT [BR:02h], #08h ; if clocking CPU with OSC3,
$0381 JRS NZ, wfi__already_fast ; no need to start it up again
$0383 PUSH SC
$0384 LD SC, #0C0h ; interrupt flags to NMI-only
$0386 CARL enter_normal_operation
$0389 POP SC
wfi__already_fast:
$038A BIT [BR:71h], #04h ; if cart is powered,
$038D JRS Z, wfi__check_01_bit7 ;
$038F BIT [BR:02h], #40h ; if ??,
$0392 JRS Z, wfi__check_01_bit7 ;
$0394 PUSH SC
$0395 LD SC, #0C0h ; interrupt flags to NMI-only
$0397 CARL power_cart
$039A BIT [BR:01h], #40h ; if ??,
$039D JRS Z, wfi__dont_prepare_yet ;
$039F CARS prepare_selected_game
wfi__dont_prepare_yet:
$03A1 POP SC
wfi__check_01_bit7:
$03A2 BIT [BR:01h], #80h ; if ??,
$03A5 JRS Z, wfi__prepare_and_return ;
$03A7 ADD SP, #0003h ; remove the call to this one
$03AB POP BR ; restore BR/EP from interrupt's pushes
$03AC POP EP ; ...
$03AD JRL 1FFDh ; jump to RAM, hope it's another JRL!
wfi__prepare_and_return:
$03B0 PUSH SC
$03B1 LD SC, #0C0h ; interrupt flags to NMI-only
$03B3 CARS prepare_selected_game
$03B5 OR [BR:01h], #20h ; set console as awake
$03B8 POP SC
$03B9 RET
; ----------------------
_key_power_irq:
$03BA PUSH EP
$03BB PUSH BR
$03BC LD EP, #00h
$03BF LD BR, #20h
$03C1 BIT [BR:01h], #20h ; if console is awake,
$03C4 JRS NZ, kpi__software ; jump straight to software handler
$03C6 BIT [BR:01h], #10h ; if ??,
$03C9 JRL NZ, _reset2 ; reset console
$03CC BIT [BR:02h], #08h ; if clocking CPU with OSC3,
$03CF JRS NZ, kpi__already_fast ; no need to start it up again
$03D1 PUSH SC
$03D2 LD SC, #0C0h ; interrupt flags to NMI-only
$03D4 CARL enter_normal_operation
$03D7 POP SC
kpi__already_fast:
$03D8 BIT [BR:71h], #04h ; if cartidge is powered,
$03DB JRS Z, loc_0x03F0 ; no need to power again
$03DD BIT [BR:02h], #40h ; if ??,
$03E0 JRS Z, loc_0x03F0 ;
$03E2 PUSH SC
$03E3 LD SC, #0C0h ; interrupt flags to NMI-only
$03E5 CARL power_cart
$03E8 BIT [BR:01h], #40h ; if ??,
$03EB JRS Z, kpi__dont_prepare_yet ; pop SC from above then loc_0x03F0
$03ED CARS prepare_selected_game
kpi__dont_prepare_yet:
$03EF POP SC
loc_0x03F0:
$03F0 BIT [BR:01h], #80h ; if ??,
$03F3 JRS Z, kpi__prepare_banks ;
$03F5 BIT [BR:02h], #20h ; if ??,
$03F8 JRS NZ, kpi__return ; return (don't go to software)
$03FA POP BR
$03FB POP EP
$03FC JRL 1FFDh ; jump to RAM, hope it's another JRL!
kpi__prepare_banks:
$03FF PUSH SC
$0400 LD SC, #0C0h ; interrupt flags to NMI-only
$0402 CARS prepare_selected_game
$0404 OR [BR:01h], #20h ; set console as awake
$0407 POP SC
$0408 BIT [BR:02h], #20h ; if ??,
$040B JRS Z, kpi__software ; jump to software handler
kpi__return:
$040D LD [BR:29h], #80h ; clear power key
$0410 POP BR
$0411 POP EP
$0412 RETE
kpi__software:
$0413 POP BR
$0414 POP EP
$0415 JRL 215Ch ; jump to software handler
; ----------------------
; Clobbers: XP
prepare_selected_game:
$0418 PUSH IP
$0419 PUSH BA
$041A PUSH HL
$041B BIT [BR:01h], #08h ; if game ID is valid,
$041E JRS NZ, psg__use_game_id ; use it
$0420 BIT [BR:01h], #01h ; otherwise, if used startup action 2,
$0423 JRS Z, psg__return
$0425 LD L, #00h ; select game 0
$0427 JRS psg__prepare_banks
psg__use_game_id:
$0429 LD L, [BR:01h] ; load game ID into L
$042B AND L, #07h ; ...
psg__prepare_banks:
$042E LD XP, #07h ; set external page 1 to load bank 0
$0431 LD A, #00h ; ...
$0433 CARL flash_select_bank ; ...
$0436 LD A, L ; prepare select game's bank selections
$0437 CARL flash_prepare_game ; ...
psg__return:
$043A POP HL
$043B POP BA
$043C POP IP
$043D RET
; ----------------------
; This can be called from cart ejection or insertion.
; It's dependent on the polarity, set in $2051.
; The BIOS only ever sets it to eject, but software can change it.
_cart_eject_irq:
$043E PUSH EP
$043F PUSH BR
$0440 LD EP, #00h
$0443 LD BR, #20h
$0445 BIT [BR:01h], #30h ; if console is awake or ??,
$0448 JRL NZ, _reset2 ; reset console
$044B BIT [BR:02h], #08h ; if clocking CPU with OSC3,
$044E JRS NZ, cei__already_fast ; no need to start it up again
$0450 PUSH SC
$0451 LD SC, #0C0h ; interrupt flags to NMI-only
$0453 CARL enter_normal_operation
$0456 POP SC
cei__already_fast:
$0457 BIT [BR:01h], #80h ; if ??,
$045A JRS Z, cei__shutdown ; shutdown console
$045C BIT [BR:01h], #40h ; if ??,
$045F JRS Z, cei__dont_shutdown ; don't shutdown console
cei__shutdown:
$0461 JRL _shutdown
cei__dont_shutdown:
$0464 BIT [BR:71h], #04h ; if cartidge is powered,
$0467 JRS Z, cei__jump_to_ram ; no need to power again
$0469 BIT [BR:02h], #40h ; if ??,
$046C JRS Z, cei__jump_to_ram ;
$046E PUSH SC
$046F LD SC, #0C0h ; interrupt flags to NMI-only
$0471 CARL power_cart
$0474 POP SC
cei__jump_to_ram:
$0475 POP BR
$0476 POP EP
$0477 JRL 1FFDh ; jump to RAM, hope it's another JRL!
; ----------------------
; Call _flash_remap_ids before this
; Reads 2100+ so that the reads make it to the cart
; making the temporary remapping necessary.
; Returns:
; B: manufacturer ID
; A: device ID
_flash_read_ids:
$047A POP SC
flash_read_ids:
$047B PUSH SC
$047C LD SC, #0C0h ; interrupt flags to NMI-only
$047E PUSH EP
$047F PUSH HL
$0480 CARL flash_begin_cmd
$0483 LD HL, #5555h
$0486 LD [HL], #90h
$0488 LD B, [2100h]
$048C LD A, [2101h]
$0490 POP HL
$0491 POP EP
$0492 RETE
; ----------------------
_flash_reset:
$0493 POP SC
flash_reset:
$0494 PUSH SC
$0495 LD SC, #0C0h ; interrupt flags to NMI-only
$0497 PUSH EP
$0498 PUSH HL
$0499 CARL flash_begin_cmd
$049C LD HL, #5555h
$049F LD [HL], #0F0h
$04A1 POP HL
$04A2 POP EP
$04A3 RETE
; ----------------------
; Arguments
; XP/IX: address to write to
; A: byte to write
; Returns
; A: -1 = write failed, 0 = success
; Clobbers: B
_flash_program_byte:
$04A4 POP SC
flash_program_byte:
$04A5 PUSH SC
$04A6 LD SC, #0C0h ; interrupt flags to NMI-only
$04A8 PUSH EP
$04A9 PUSH HL
$04AA CARL flash_begin_cmd
$04AD LD HL, #5555h ; final part of write unlock
$04B0 LD [HL], #0A0h ; ...
$04B2 LD [IX], A ; write byte
$04B3 LD H, #08h ; try 7x2 reads
fpb__wait_for_write:
$04B5 DEC H
$04B6 JRS NZ, fpb__wfw__unfinished
$04B8 LD A, #0FFh ; write failed
$04BA JRS fpb__return
fpb__wfw__unfinished:
$04BC LD B, [IX] ; check if byte was written correctly
$04BD CP A, B ; ...
$04BE JRS NZ, fpb__wait_for_write
$04C0 LD B, [IX] ; confirm it's not a data ghost ??
$04C1 CP A, B ; ...
$04C2 JRS NZ, fpb__wait_for_write
$04C4 XOR A, A ; write success
fpb__return:
$04C5 POP HL
$04C6 POP EP
$04C7 RETE
; ----------------------
; A sector is a 4 KiB section, uniformily placed.
; IX may contain any address in the section.
; The cleared sector is filled with 0xFF bytes.
; Arguments
; XP/IX: sector to erase
; Clobbers: BA
_flash_erase_sector:
$04C8 LD SC, #0C0h ; interrupt flags to NMI-only
$04CA PUSH EP
$04CB PUSH HL
$04CC CARL flash_begin_cmd
$04CF LD HL, #5555h ; erase sequence
$04D2 LD [HL], #80h ; ...
$04D4 LD [HL], #0AAh ; ...
$04D6 LD HL, #2AAAh ; ...
$04D9 LD [HL], #55h ; ...
$04DB LD [IX], #30h ; specify sector to erase
$04DD LD A, #0FFh ; check for 0xFF to be written
$04DF LD HL, #05001 ; loop 5000 times
fes__wait_for_erase:
$04E2 DEC HL
$04E3 JRS NZ, fes__wfe__top
$04E5 LD A, #0FFh ; erase failed
$04E7 JRS fes__return
fes__wfe__top:
; note that for any read while erase is still going
; bit 7 will be 0 no matter the data at the location
; you're reading from
$04E9 LD B, [IX] ; check if byte was erased correctly
$04EA CP A, B ; ...
$04EB JRS NZ, fes__wait_for_erase
$04ED LD B, [IX] ; copypasta
$04EE CP A, B ; ...
$04EF JRS NZ, fes__wait_for_erase
$04F1 XOR A, A ; erase success
fes__return:
$04F2 POP HL
$04F3 POP EP
$04F4 RETE
; ----------------------
; Makes writes to $xxFFFF be interpreted as
; bank selects at the mapper level.
_flash_enter_bank_select_mode:
$04F5 POP SC
flash_enter_bank_select_mode:
$04F6 PUSH SC
$04F7 LD SC, #0C0h ; interrupt flags to NMI-only
$04F9 PUSH EP
$04FA PUSH HL
$04FB CARL flash_begin_cmd
$04FE LD HL, #5555h ; send a command to the mapper
$0501 LD [HL], #0D0h ; ...to unlock bank select
$0503 POP HL
$0504 POP EP
$0505 RETE
; ----------------------
; Each bank is 256 KiB which is an addressing space of 0x040000.
; Thus the system can hold 8 banks. XP=7 with A=1 should be the
; default state while XP=7 with A=0 allows you to read from the
; first $2100 bytes of the cartridge by addressing data page 4,
; code page 8.
; Arguments
; XP: Bank register to write to; 3+4*x
; A: Value to write
_flash_select_bank:
$0506 POP SC
flash_select_bank:
$0507 PUSH SC
$0508 LD SC, #0C0h ; interrupt flags to NMI-only
$050A CARL flash_enter_bank_select_mode ; unlock bank registers
$050D PUSH IX
$050E LD IX, #0FFFFh ; write to bank select register
$0511 LD [IX], A ; ...
$0512 POP IX
$0513 CARL flash_reset ; lock bank registers
$0516 RETE
; ----------------------
; Without this, writing to 0x03FFFF bank select
; register is (probably) a NOP.
_flash_allow_remapping_page_0:
$0517 POP SC
flash_allow_remapping_page_0:
$0518 PUSH SC
$0519 LD SC, #0C0h ; interrupt flags to NMI-only
$051B PUSH EP
$051C PUSH HL
$051D CARL flash_begin_cmd
$0520 LD HL, #5555h
$0523 LD [HL], #0C9h
$0525 LD [HL], A
$0526 POP HL
$0527 POP EP
$0528 RETE
; ----------------------
; Instructs the mapper to subtract $2100 from the input address.
; Though it may not be generally usable in this state?
; This is used with ID mode to read info.
_flash_remap_ids:
$0529 POP SC
flash_remap_ids:
$052A PUSH SC
$052B LD SC, #0C0h ; interrupt flags to NMI-only
$052D PUSH EP
$052E PUSH HL
$052F CARL flash_begin_cmd
$0532 LD HL, #5555h
$0535 LD [HL], #0C0h
$0537 POP HL
$0538 POP EP
$0539 RETE
; ----------------------
; Prepares banks to run the game.
; Doesn't run the game itself.
; Arguments:
; A: game ID (0-based)
; Clobbers: B
_flash_prepare_game:
$053A POP SC
flash_prepare_game:
$053B PUSH SC
$053C LD SC, #0C0h ; interrupt flags to NMI-only
$053E PUSH IP
$053F PUSH IX
$0540 PUSH IY
$0541 PUSH HL
$0542 LD L, #60h ; size of game structure
$0544 MLT ; mlt to get addr in array
$0546 LD IX, #1048h ; base address of structures + bank select array offset
$0549 ADD IX, HL ; offset
$054B LD XP, #04h ; and read
$054E LD A, [IX] ; ...
$054F CP A, #08h ; if bank ID is >= 8,
$0551 JRS NC, fsg__skip_remap_0 ; don't disable region 0
$0553 CARL flash_allow_remapping_page_0
fsg__skip_remap_0:
$0556 SUB SP, #0008h ; copy banks to heap
$055A LD YP, #00h ; ...
$055D LD IY, SP ; ...
$055F LD L, #00h ; ...
fsg__select_banks:
$0561 LD A, [IX+L] ; copy bank ID from ROM
$0563 LD [IY+L], A ; ...to heap
$0565 INC L
$0566 CP L, #08h ; there's 8 of them
$0569 JRS NZ, fsg__select_banks
$056B CARL flash_enter_bank_select_mode
$056E LD A, #03h ; start at first bank select register
$0570 LD IX, #0FFFFh ; bank select register address
fsg__bank_select_loop:
$0573 LD XP, A
$0575 LD B, [IY]
$0576 CP B, #08h ; if value is >= 8,
$0579 JRS C, fsg__bsl__already_le8
$057B LD B, #08h ; cap to 8 (likely disables region)
fsg__bsl__already_le8:
$057D LD [IX], B
$057E INC IY
$057F ADD A, #04h ; increment to next bank select register
$0581 CP A, #23h ; unless we hit the end,
$0583 JRS NZ, fsg__bank_select_loop ; keep going
$0585 CARL flash_reset ; lock bank select registers
$0588 LD SP, IY
$058A POP HL
$058B POP IY
$058C POP IX
$058D POP IP
$058E RETE
; ----------------------
; Common portion of a JEDEC SDP command sequence.
; Returns:
; EP: $00
; Clobbers: HL
flash_begin_cmd:
$058F LD EP, #00h
$0592 LD HL, #5555h
$0595 LD [HL], #0AAh
$0597 LD HL, #2AAAh
$059A LD [HL], #55h
$059C RET
; ----------------------
; Read cart information presuming it's a SST39LF/VF016 chip
; If it's not, perform some recovery.
; Returns:
; Z: 0 = SST39LF/VF016, 1 = commercial game/other chip
; Clobbers: BA
flash_detect:
$059D LD B, [5555h] ; backup in case the chip is something else
$05A1 LD A, [2AAAh] ; ...
$05A5 PUSH BA
$05A6 LD A, #0FFh
$05A8 LD [2AAAh], A ; NOPs to clear state ??
$05AC LD [2AAAh], A ; ...
$05B0 CARL flash_reset ; make sure device is in normal state
$05B3 CARL flash_remap_ids ; have mapped read from $0000 when $2100 is requested
$05B6 CARL flash_read_ids ; read manufacturer/device ID into BA
$05B9 CARL flash_reset ; exit Software ID mode
$05BC CP BA, #0BFD9h ; check manufacturer/device ID (SST39?F016)
$05BF POP BA
$05C0 JRS Z, fd__return
$05C2 LD [5555h], B ; if it was an unexpected chip
$05C6 LD [2AAAh], A ; try to restore the data (NOP if ROM)
fd__return:
$05CA LD A, [2AAAh] ; dummy read?
$05CE RET
; ----------------------
check_cart_type:
$05CF CARL flash_detect ; Z=0 if using a dev cart
$05D2 JRS NZ, cct__not_flash
$05D4 PUSH IP
$05D5 LD XP, #07h ; page register to write to
$05D8 LD A, #00h ; bank to select
$05DA CARL flash_select_bank
$05DD LD XP, #04h ; read startup action from cartridge
$05E0 LD IX, #1300h ; ...
$05E3 LD A, [IX] ; ...
$05E4 POP IP
$05E5 BIT A, #02h
$05E7 JRS NZ, cct__startup_action_2
$05E9 LD NB, #08h ; startup action 0 and 1,
$05EC CARL 41400h ; call 0x001400 (physical) on cartridge
$05EF LD [RemainingPlays], A ; which returns the remaining plays in A
$05F3 JRL nintendo_check ; continue to boot
cct__startup_action_2:
$05F6 BIT A, #01h ; if startup action is 3,
$05F8 JRS NZ, game_select_menu ; goto selection menu
$05FA XOR A, A ; startup action 2 picks the 0th slot
$05FB CARL flash_prepare_game ; ...
$05FE OR [BR:01h], #01h ; remember that we used startup action 2
cct__not_flash:
$0601 LD A, #0FFh ; normal carts, unexpected chips, and startup action 2,
$0603 LD [RemainingPlays], A ; unlimited play
$0607 JRL nintendo_check ; continue to boot
$060A LD IX, #GameNameTiles ; address right after the v-flipped decoded tile data
$060D XOR A, A ; 0-fill $19D0~$1C10 (exclusive)
gsm__clear: ; ...
$060E LD [IX], A ; ...
$060F INC IX ; ...
$0610 CP IX, #1C10h ; ...
$0613 JRS NZ, gsm__clear ; ...
$0615 PUSH IP
$0616 LD A, #04h ; read from data page 4 (region 1, bank 0)
$0618 LD XP, A ; ...
$061A LD YP, A ; ...
$061C XOR A, A ; initialize memory
$061D LD [NumGameStructs], A ; ...
$0621 LD [LoopN], A ; ...
$0625 LD HL, #GameStructs ; ...
$0628 LD [NextGameStruct], HL ; ...
$062B LD HL, #GameNameTiles ; pointer to new tiles
$062E LD [NextTilePos], HL ; ...
$0631 LD IY, #1050h ; starting at type byte for entry 0
gsm__load_data_loop:
$0634 LD A, [IY] ; read from $041050 (first loop)
$0635 OR A, A ; if it's 0,
$0636 JRL Z, gsm__ldl__end ; skip entry
$0639 CP A, #0FFh ; if it's -1,
$063B JRL Z, gsm__ldl__end ; skip entry
$063E CP A, #0FEh ; if it's -2,
$0640 JRS Z, gsm__ldl__load_data ; load data/tiles
$0642 LD IX, IY ; otherwise,
$0644 INC IX ; read next byte
$0645 LD A, [IX] ; ...
$0646 BIT A, #80h ; if MSB is 1
$0648 JRS NZ, gsm__ldl__loadable ;
$064A XOR A, A ; otherwise write 0
$064B DEC IX ; to $041050 (first loop)
$064C CARL flash_program_byte
$064F OR A, A ; if byte was written successfully,
$0650 JRL Z, gsm__ldl__end ; skip entry
$0653 POP IP ; otherwise,
$0654 JRL insert_cart_screen
gsm__ldl__loadable:
$0657 LD L, #00h ; number of 1-bits
$0659 LD BA, IX ; ...from IX
$065B ADD BA, #15 ; ...for 15 bytes
$065E PUSH BA
gsm__ldl__count_1s_in_byte:
$065F LD A, [IX] ; fetch byte
$0660 LD B, #80h ; starting mask (MSB)
gsm__ldl__count_1_bits:
$0662 BIT A, B ; if bit is 0,
$0663 JRS Z, gsm__ldl__remove_bit ; exit loop
$0665 INC L ; number of 1-bits +1
$0666 SRL B ; move to next bit
$0668 JRS NZ, gsm__ldl__count_1_bits
$066A INC IX ; move to next byte
$066B POP BA ; restore end address for check
$066C PUSH BA ; ...
$066D CP BA, IX ; ...
$066F JRS NZ, gsm__ldl__continue_counting ; continue if we haven't hit it (exclusive)
$0671 LD B, #80h ; reset mask to MSB for check below
$0673 JRS gsm__ldl__remove_bit
gsm__ldl__continue_counting:
$0675 JRS gsm__ldl__count_1s_in_byte ; could've just jumped straight there...
gsm__ldl__remove_bit:
$0677 RLC B ; if we got here from any bit but the MSB being 0,
$0679 CP B, #01h ; these would not be equal,
$067C JRS NZ, gsm__ldl__no_dec ; so we don't dec IX
$067E DEC IX ; otherwise, dec IX back to the last 0xFF byte (IX+15)
gsm__ldl__no_dec:
$067F CPL B ; mask everything but bit before the "0"
$0681 LD A, [IX] ; reload in case we decremented
$0682 AND A, B ; ...
$0683 LD H, A ; cases:
; 01 80 -> H = 0x00
; 01 FF08 -> H = 0xFE
; 01 FFF8 -> H = 0xF0
; 01 FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF -> H = 0xFE
$0684 POP BA
$0685 LD B, H
$0686 DEC L ; next number for plays remaining
$0687 LD A, L ; ...
gsm__ldl__load_data:
$0688 PUSH BA
$0689 LD A, [NumGameStructs] ; loading 1 more struct
$068D INC A ; ...
$068E LD [NumGameStructs], A ; ...
$0692 LD HL, [NextGameStruct] ; where we're loading the data to
$0695 LD A, [LoopN] ; $00 - index on cart
$0699 LD [HL], A ; ...
$069A INC HL
$069B POP BA
$069C LD [HL], A ; 0xFE or remaining play count
$069D INC HL
$069E LD [HL], B ; new count byte (or garbage)
$069F INC HL
$06A0 LD [HL], IX ; location of last count byte (or garbage)
$06A2 ADD HL, #5 ; two bytes from the above addr + 3 slop
$06A5 LD [NextGameStruct], HL ; store addr for next entry
$06A8 LD HL, [NextTilePos] ; where we're loading tile gfx to
$06AB LD IX, IY ; $041050 (first loop)
$06AD LD B, #72 ; read 9 tiles (72 bytes)
$06AF SUB IX, #0050h ; $041000 (first loop)
gsm__ldl__load_name_tiles:
$06B2 LD [HL], [IX] ; load in tile
$06B3 INC HL ; next
$06B4 INC IX ; ...
$06B5 DJR NZ, gsm__ldl__load_name_tiles
$06B7 LD [NextTilePos], HL ; store addr for next entry
gsm__ldl__end:
$06BA ADD IY, #0060h ; $0410B0 (first loop)
$06BD LD A, [LoopN] ; increment loop count
$06C1 INC A ; ...
$06C2 LD [LoopN], A ; ...
$06C6 CP A, #08h ; if we haven't looped 8 times,
$06C8 JRL NZ, gsm__load_data_loop ; keep going
$06CB POP IP
$06CC LD A, [NumGameStructs] ; check if any data was loaded
$06D0 OR A, A ; and if so,
$06D1 JRS NZ, gsm__games_available ; show screen
$06D3 JRL insert_cart_screen ; otherwise
gsm__games_available:
$06D6 OR [BR:01h], #08h ; set game ID is valid
$06D9 JRL game_select_screen
; ----------------------
; This is jumped to not called
; Arguments:
; A: game index (in RAM table)
; Clobbers: any
run_selected_game:
$06DC SLL A ; multiply index by 8 (size of a game struct in memory)
$06DE SLL A ; ...
$06E0 SLL A ; ...
$06E2 LD B, #00h ; indexing into game structs array
$06E4 LD IY, #GameStructs ; ...
$06E7 ADD IY, BA ; ...
$06E9 LD H, [IY] ; ...read index in list on cart to H
$06EA INC IY
$06EB LD B, [IY] ; 0xFE or remaining plays
$06EC LD [RemainingPlays], B ; ...
$06F0 CP B, #0FEh ; if it's -2,
$06F3 JRS Z, rsg__skip_write ; skip writing
$06F5 INC IY
$06F6 LD A, [IY] ; new remaining count byte
$06F7 INC IY
$06F8 LD IX, [IY] ; original location of A
$06FA PUSH IP
$06FB LD XP, #04h ; write new byte
$06FE CARL flash_program_byte ; ...
$0701 POP IP
$0702 OR A, A ; if it wasn't written successfully,
$0703 JRL NZ, insert_cart_screen ; exit
rsg__skip_write:
$0706 LD B, [BR:01h]
$0708 LD A, H ; game index to load
$0709 OR A, B ; TODO: confirm if line $00C7 makes this safe every time
$070A LD [BR:01h], A ; ...
$070C LD A, H
$070D CARL flash_prepare_game
$0710 JRL nintendo_check
; ----------------------
; Clobbers: BA
_suspend_system:
$0713 PUSH EP
$0714 PUSH BR
$0715 LD SC, #0C0h ; interrupt flags to NMI-only
$0717 CARL set_br
$071A OR [BR:02h], #20h ; ??
; back up registers that will be changed
$071D LD A, [BR:80h] ; back up LCD controller config
$071F PUSH BA ; ...
$0720 LD B, [BR:50h] ; back up input polarities (except cart slot)
$0722 LD A, [BR:21h] ; back up keypad IRQ priorities
$0724 AND A, #0Ch ; ...
$0726 PUSH BA ; ...
$0727 LD B, [BR:23h] ; back up IRQ enable status
$0729 LD A, [BR:24h] ; ...
$072B PUSH BA ; ...
$072C LD B, [BR:25h] ; ...
$072E LD A, [BR:26h] ; ...
$0730 PUSH BA ; ...
; When called directly, this needs to be a shutdown sequence
; See _shutdown for requirements
do_shutdown:
$0731 LD [BR:50h], #80h ; leave power as falling edge, others to rising edge
$0734 LD [BR:54h], #01h ; set input level check time for ?? to 4ms?
$0737 LD [BR:23h], #00h ; disable IRQs
$073A LD [BR:24h], #00h ; ...
$073D LD [BR:25h], #80h ; ...except power key
$0740 LD [BR:26h], #00h ; ...
$0743 OR [BR:21h], #0Ch ; power key IRQ priority to 3
$0746 CARL wait_8000_cycles
$0749 LD [BR:29h], #80h ; clear power key IRQ
$074C LD A, #80h ; set interrupt flags to 2 before halt
$074E CARL halt_cpu_and_lcd
; After resuming from halt (if that's possible!)
; The IRQ that wakes the console will run before
; resuming from halt, which may result in a restart.
resume:
$0751 CARL lcd_on
$0754 AND [BR:02h], #~20h ; ??
; restore registers after awakening
$0757 POP BA ; restore IRQ enable status
$0758 LD [BR:26h], A ; ...
$075A LD [BR:25h], B ; ...
$075C POP BA ; ...
$075D LD [BR:23h], B ; ...
$075F BIT [BR:01h], #40h ; ??
$0762 JRS Z, r__trust_24
$0764 OR A, #02h ; ...also enable cart eject IRQ
r__trust_24:
$0766 LD [BR:24h], A ; ...
$0768 POP A
$076A LD B, [BR:21h] ; only add back keypad IRQ priorities
$076C AND B, #~0Ch ; ...
$076F OR A, B ; ...
$0770 LD [BR:21h], A ; ...
$0772 POP B ; restore input polarities (except cart slot)
$0774 LD [BR:50h], B ; ...
$0776 POP BA ; restore LCD controller config
$0777 LD [BR:80h], A ; ...
$0779 POP BR
$077A POP EP
$077B RETE
; ----------------------
_sleep:
$077C PUSH EP
$077D PUSH BR
$077E LD SC, #0C0h ; interrupt flags to NMI-only
$0780 CARL set_br
$0783 LD A, #40h ; set interrupt flags to 1 before halt
$0785 CARL halt_cpu_and_lcd
$0788 POP BR
$0789 POP EP
$078A RETE
; ----------------------
_sleep_with_display:
$078B PUSH EP
$078C PUSH BR
$078D LD SC, #0C0h ; interrupt flags to NMI-only
$078F CARL set_br
$0792 AND [BR:80h], #~08h ; disable LCD controller
$0795 LD A, #40h ; set interrupt flags to 1 before halt
$0797 CARL halt_cpu
$079A POP BR
$079B POP EP
$079C RETE
; ----------------------
; Power button resets system after this.
_shutdown:
$079D LD SC, #0C0h ; interrupt flags to NMI-only
$079F LD SP, #2000h
$07A3 CARL set_br
$07A6 CARL init_io
shutdown:
$07A9 LD SC, #0C0h ; interrupt flags to NMI-only
$07AB LD [BR:01h], #90h ; ??
$07AE JRL do_shutdown
; ----------------------
; Same as _sleep just lines 3 and 4 swapped...
; Official software uses _sleep rather than this.
_sleep2:
$07B1 PUSH EP
$07B2 PUSH BR
$07B3 CARL set_br
$07B6 LD SC, #0C0h ; interrupt flags to NMI-only
$07B8 LD A, #40h ; set interrupt flags to 1 before halt
$07BA CARL halt_cpu_and_lcd
$07BD POP BR
$07BE POP EP
$07BF RETE
; ----------------------
; Arguments:
; A copied to SC right before HALT
halt_cpu_and_lcd:
$07C0 CARL lcd_off
halt_cpu:
$07C3 BIT [BR:01h], #80h
$07C6 JRS Z, hc__wake_on_cart_eject
$07C8 BIT [BR:01h], #40h
$07CB JRS Z, hc__halt_sequence ; only 0b10xxxxxx skips wake_on_cart_eject
hc__wake_on_cart_eject:
$07CD OR [BR:24h], #02h ; enable cart eject IRQ
$07D0 OR [BR:21h], #30h ; set cartridge IRQs' priorities to 3
hc__halt_sequence:
$07D3 AND [BR:51h], #~02h ; set cart eject IRQ to rising edge (ejected)
$07D6 LD [BR:55h], #01h ; set input level check time for ?? to 4ms?
$07D9 AND [BR:01h], #~20h ; set console as asleep
$07DC CARL unpower_cart
$07DF CARL enter_low_power_operation
$07E2 LD SC, A
$07E4 HALT
$07E6 LD SC, #0C0h ; interrupt flags to NMI-only
$07E8 RET
; ----------------------
; Stores and applies a contrast value which can be
; restored with _apply_default_contrast (INT [50h]).
; Default set by the BIOS is 31.
; Arguments:
; A: new default contrast, 0~63
; Clobbers: B
_default_contrast:
$07E9 POP SC
default_contrast:
$07EA SLL A ; shift into position
$07EC SLL A ; ...
$07EE PUSH EP
$07EF PUSH HL
$07F0 LD EP, #00h
$07F3 LD HL, #2000h
$07F6 AND [HL], #03h ; clear default contrast
$07F9 OR [HL], A ; set it
$07FB POP HL
$07FC POP EP
$07FD CARL get_default_contrast
$0800 JRS set_temp_contrast
; ----------------------
; Step contrast up or down based on Z
; Arguments:
; Z: 0 = decrement, 1 = increment
; Returns:
; A: -1 = fail, 0 = success
; Clobbers: B
_change_contrast:
$0802 CARL get_default_contrast
$0805 POP SC
$0806 PUSH SC
$0807 JRS Z, cc__try_decrement ; decrement if Z=0
$0809 CP A, #03Fh ; if contrast is already max,
$080B JRS Z, cc__fail ; fail out
$080D INC A ; Z=1 so increment
cc__set_and_return:
$080E CARS default_contrast ; store and apply
$0810 XOR A, A ; return success
$0811 RETE
cc__try_decrement:
$0812 OR A, A ; if A > 0,
$0813 JRS NZ, cc__decrement ; decrement
cc__fail:
$0815 LD A, #0FFh ; return failure (already max or min)
$0817 RETE
cc__decrement:
$0818 DEC A
$0819 JRS cc__set_and_return
; ----------------------
; Returns:
; A: contrast
; Clobbers: B
_apply_default_contrast:
$081B POP SC
apply_default_contrast:
$081C CARL get_default_contrast
$081F JRS set_temp_contrast
; ----------------------
; Returns:
; A: contrast
_get_default_contrast:
$0821 POP SC
get_default_contrast:
$0822 PUSH EP
$0823 PUSH BR
$0824 CARL set_br
$0827 LD A, [BR:00h]
$0829 SRL A
$082B SRL A
$082D POP BR
$082E POP EP
$082F RET
; ----------------------
; Arguments:
; A: new contrast, 0~63
; Clobbers: B
_set_temp_contrast:
$0830 POP SC
set_temp_contrast:
$0831 PUSH EP
$0832 PUSH BR
$0833 CARL set_br
$0836 LD B, A
$0837 BIT [BR:80h], #08h ; store whether or not LCD controller was enabled
$083A LD A, SC ; ...
$083C AND [BR:80h], #~08h ; disable LCD controller
$083F LD [BR:0FEh], #81h ; send contrast command
$0842 LD [BR:0FEh], B ; ...
$0844 LD SC, A ; if LCD controller was enabled,
$0846 JRS Z, stc__return
$0848 OR [BR:80h], #08h ; re-enable it
stc__return:
$084B POP BR
$084C POP EP
$084D RET
; ----------------------
; Clobbers: BA
_lcd_on:
$084E POP SC
$084F PUSH EP
$0850 PUSH BR
$0851 CARL set_br
$0854 CARS lcd_on
$0856 POP BR
$0857 POP EP
$0858 RET
; Clobbers: BA
lcd_on:
$0859 PUSH SC
$085A LD SC, #0C0h ; interrupt flags to NMI-only
$085C BIT [BR:02h], #04h ; if OSC3 is on,
$085F JRS Z, lcdo__reset_02_bit4 ;
$0861 OR [BR:02h], #10h ; ??
$0864 JRS lcdo__return
lcdo__reset_02_bit4:
$0866 AND [BR:02h], #~10h ; ??
lcdo__return:
$0869 OR [BR:81h], #01h ; actually initialize display
$086C XOR A, A ; wait between setup and display on
$086D CARL init_display
$0870 RETE
; ----------------------
; Manually resets the LCD state.
; Can only be run while the LCD is on.
; Clobbers: BA
_reset_lcd:
$0871 POP SC
$0872 PUSH EP
$0873 PUSH BR
$0874 CARL set_br
$0877 CARS init_display_without_wait
$0879 POP BR
$087A POP EP
$087B RET
; ----------------------
; Clobbers: BA
init_display_without_wait:
$087C LD A, #0FFh
; Arguments
; A: 0 = wait, non-zero = don't wait
; Clobbers: B
init_display:
$087E PUSH SC ; back up SC
$087F LD SC, #0C0h ; interrupt flags to NMI-only
$0881 BIT [BR:81h], #01h ; if LCD was not shut off,
$0884 JRS NZ, id__init ; continue to init
$0886 RETE ; otherwise, restore SC and return
id__init:
$0887 PUSH HL
$0888 LD HL, #20FEh ; using [HL] is less bytes than [BR:ll] for all
$088B LD B, [BR:80h] ; back up LCD controller config
$088D AND [BR:80h], #~08h ; disable LCD controller
$0890 LD [HL], #0E3h ; send NOP
$0892 LD [HL], #0A4h ; send "set all on" off
$0894 LD [HL], #0ADh ; send enable static indicator
$0896 LD [HL], #02h ; blink static indicator at half second intervals
$0898 LD [HL], #0EEh ; send RMW end
$089A LD [HL], #40h ; send set display start line 0
$089C LD [HL], #0A2h ; send LCD bias 1/9
$089E LD [HL], #0A0h ; send ADC select normal
$08A0 LD [HL], #0C0h ; send row direction normal
$08A2 LD [HL], #0A6h ; send color invert off
$08A4 LD [HL], #02Fh ; send power supply circuits all on
$08A6 OR A, A ; A=0 do wait, A≠0 don't wait
$08A7 JRS NZ, id__on_and_return
$08A9 PUSH IX
$08AA BIT [BR:02h], #08h ; check what oscillator is clocking the CPU
$08AD JRS NZ, id__using_hso
$08AF LD A, #01h ; wait 5711 cycles (from start of loop)
$08B1 LD IX, #0592h ; ...
$08B4 JRS id__wait_for_warmup
id__using_hso:
$08B6 LD A, #02h ; wait 349978 cycles (from start of loop)
$08B8 LD IX, #55C4h ; ...
id__wait_for_warmup:
$08BB DEC IX
$08BC JRS NZ, id__wait_for_warmup
$08BE LD IX, #0FFFFh
$08C1 DEC A ; exit at 0, so A=1 doesn't do extra
$08C2 JRS NZ, id__wait_for_warmup
$08C4 POP IX
id__on_and_return:
$08C5 LD [HL], #0AFh ; send display on
$08C7 LD [BR:80h], B ; restore frame copy state
$08C9 POP HL
$08CA RETE
; ----------------------
_lcd_off:
$08CB POP SC
$08CC PUSH EP
$08CD PUSH BR
$08CE CARL set_br
$08D1 CARS lcd_off
$08D3 POP BR
$08D4 POP EP
$08D5 RET
lcd_off:
$08D6 PUSH SC ; back up flags
$08D7 LD SC, #0C0h ; interrupt flags to NMI-only
$08D9 AND [BR:80h], #~08h ; disable LCD controller
$08DC LD [BR:0FEh], #0AEh ; send display off
$08DF LD [BR:0FEh], #0ACh ; send static indicator off
$08E2 LD [BR:0FEh], #28h ; send power supply circuits all off
$08E5 LD [BR:0FEh], #0A5h ; send "set all on" on (enter sleep mode)
$08E8 AND [BR:81h], #~01h ; prevent init_display from working
$08EB RETE ; restore flags
; ----------------------
_ena_ram_vec:
$08EC PUSH EP
$08ED PUSH BR
$08EE CARL set_br
$08F1 LD SC, #0C0h ; interrupt flags to NMI-only
$08F3 BIT [BR:01h], #80h ; if bit 7 is already set,
$08F6 JRS NZ, loc_0x0901 ; skip
$08F8 OR [BR:01h], #80h ; otherwise set it
$08FB OR [BR:01h], #40h ; and bit 6
$08FE AND [BR:01h], #~20h ; set console as asleep
loc_0x0901:
$0901 POP BR
$0902 POP EP
$0903 RETE
; ----------------------
_dis_ram_vec:
$0904 PUSH EP
$0905 PUSH BR
$0906 CARL skip_if_01_not_bit6
$0909 LD [BR:55h], #01h ; set input level check time for ?? to 4ms?
$090C AND [BR:51h], #~02h ; set cart eject IRQ to rising edge (ejected)
$090F LD SC, #0C0h ; interrupt flags to NMI-only
$0911 OR [BR:21h], #30h ; set cartridge IRQs' priority to IRQ3
$0914 OR [BR:24h], #02h ; enable cart eject IRQ
$0917 OR [BR:01h], #20h ; set console as awake
$091A AND [BR:01h], #~40h ; clear bit 6
$091D AND [BR:01h], #~80h ; and bit 7
$0920 POP BR
$0921 POP EP
$0922 RETE
; ----------------------
_disable_cart_eject_irq_if:
$0923 PUSH EP
$0924 PUSH BR
$0925 CARL skip_if_01_not_bit6
$0928 AND [BR:24h], #~02h ; disable cart eject IRQ
$092B POP BR
$092C POP EP
$092D RETE
; ----------------------
_enable_cart_eject_irq_if:
$092E PUSH EP
$092F PUSH BR
$0930 CARL skip_if_01_not_bit6
$0933 OR [BR:24h], #02h ; enable cart eject IRQ
$0936 POP BR
$0937 POP EP
$0938 RETE
; ----------------------
; MUST push EP, BR, SC (maybe from INT) before calling
skip_if_01_not_bit6:
$0939 CARL set_br
$093C BIT [BR:01h], #40h ; if bit 6 is set,
$093F JRS NZ, si__return ; just return
$0941 ADD SP, #0003h ; otherwise return back to caller's caller
$0945 POP BR ; and give BR/EP/SC back
$0946 POP EP ; ...
$0947 RETE ; ...
si__return:
$0948 RET
; ----------------------
_unknown_eject1:
$0949 PUSH EP
$094A PUSH BR
$094B CARL set_br
$094E LD SC, #0C0h ; interrupt flags to NMI-only
$0950 BIT [BR:01h], #80h ; if bit 7 is set,
$0953 JRS NZ, loc_0x095E ; just return
$0955 AND [BR:24h], #~02h ; otherwise, disable cart eject IRQ
$0958 OR [BR:01h], #80h ; set bit 7
$095B AND [BR:01h], #~20h ; set console as asleep
loc_0x095E:
$095E POP BR
$095F POP EP
$0960 RETE
; ----------------------
_unknown_eject2:
$0961 PUSH EP
$0962 PUSH BR
$0963 CARL skip_if_01_bit6_or_not_bit7
$0966 LD [BR:55h], #01h ; set input level check time for ?? to 4ms?
$0969 AND [BR:51h], #~02h ; set cart eject IRQ to rising edge (ejected)
$096C LD SC, #0C0h ; interrupt flags to NMI-only
$096E OR [BR:21h], #30h ; set cartridge IRQs' priorities to 3
$0971 OR [BR:24h], #02h ; enable cart eject IRQ
$0974 OR [BR:01h], #20h ; set console as awake
$0977 AND [BR:01h], #~80h ; reset bit 7
$097A POP BR
$097B POP EP
$097C RETE
; ----------------------
; Similar to prepare_selected_game but:
; Conditional execution.
; Does not assume cart is inserted or powered.
; Check startup action instead of $2001.3~0 being 0b0xx1.
; Ensures the game struct in ROM is valid.
; When this returns 0, it leaves the cart powered.
; Returns:
; A:
; 0 = banks perpared
; 1 = no cart inserted
; 2 = error:
; unexpected game ID in $2001
; expected game ID in $2001
; selected game ID has no valid entry in ROM
; unchanged if skipped
; Clobbers: B, XP
_dev_card0:
$097D PUSH EP
$097E PUSH BR
$097F LD SC, #0C0h ; interrupt flags to NMI-only
$0981 CARL skip_if_01_bit6_or_not_bit7
$0984 PUSH IX
$0985 PUSH HL
$0986 BIT [BR:53h], #02h ; if cart is inserted,
$0989 JRS Z, dc0__cart_inserted ; continue
$098B LD A, #01h ; otherwise, return 1
$098D JRS dc0__return1
dc0__cart_inserted:
$098F LD B, [BR:80h] ; back up LCD controller settings
$0991 LD [BR:80h], #00h ; disable LCD controller
$0994 LD A, [BR:71h] ; back up cart power/volume
$0996 PUSH BA ; ...
$0997 CARL power_cart
$099A CARL wait_8000_cycles
$099D LD [BR:28h], #02h ; clear cart eject IRQ
$09A0 CARL select_boot
$09A3 JRS NZ, dc0__verify_selected_game ; 0 = banks were selected (or didn't need to be)
$09A5 BIT [BR:01h], #08h ; we don't expect a valid game index in this case so,
$09A8 JRS NZ, dc0__return3 ; return 2
$09AA JRS dc0__leave_powered
dc0__verify_selected_game:
$09AC BIT [BR:01h], #08h ; if bits 2~0 don't contain a game index,
$09AF JRS Z, dc0__return3 ; return 2
$09B1 LD A, [BR:01h] ; otherwise, load the game index
$09B3 AND A, #07h ; ...
$09B5 LD L, #60h ; size of a game struct
$09B7 MLT ; ...
$09B9 LD IX, #1050h ; read from $041050 + 96 * game id
$09BC ADD IX, HL ; ...
$09BE PUSH IP ; ...
$09BF LD XP, #04h ; ...
$09C2 LD B, [IX] ; ...
$09C3 POP IP ; ...
$09C4 CP B, #00h ; if it's 0 (empty),
$09C7 JRS Z, dc0__return3 ; return 2
$09C9 CP B, #0FFh ; if it's 0xff (erased),
$09CC JRS Z, dc0__return3 ; return 2
$09CE CARL flash_prepare_game ; otherwise select game in A
dc0__leave_powered:
$09D1 OR [BR:02h], #40h ; ??
$09D4 POP BA
$09D5 XOR A, A ; return 0
$09D6 JRS dc0__return2 ; leave cart powered
dc0__return3:
$09D8 POP BA
$09D9 LD [BR:71h], A ; restore cart power/volume
$09DB LD A, #02h ; return 2
dc0__return2:
$09DD LD [BR:80h], B ; restore LCD controller settings
dc0__return1:
$09DF POP HL
$09E0 POP IX
$09E1 POP BR
$09E2 POP EP
$09E3 RETE
; ----------------------
; Prepares bank selections only if cart is inserted
; and startup action is 0/2 (or not a dev cart).
; Powers on cart if one's inserted.
; Returns:
; A:
; -1 = select_boot didn't prepare anything
; 0 = banks perpared
; 1 = no cart inserted
; unchanged if skipped
; Clobbers: B, XP
_dev_card1:
$09E4 PUSH EP
$09E5 PUSH BR
$09E6 LD SC, #0C0h ; interrupt flags to NMI-only
$09E8 CARL skip_if_01_bit6_or_not_bit7
$09EB LD B, [BR:80h] ; back up LCD controller config
$09ED LD [BR:80h], #00h ; disable LCD controller
$09F0 PUSH B
$09F2 PUSH IX
$09F3 PUSH HL
$09F4 BIT [BR:53h], #02h ; if cart is inserted,
$09F7 JRS Z, dc1__prepare_autoboot_only ; ...
$09F9 LD A, #01h ; otherwise return 1
$09FB JRS dc1__return
dc1__prepare_autoboot_only:
$09FD CARL cart_slot_on
$0A00 CARL select_boot
$0A03 JRS Z, dc1__return ; done selecting banks, so return
$0A05 PUSH IP
$0A06 LD XP, #07h ; set external page 1 to load bank 1
$0A09 LD A, #01h ; ...
$0A0B CARL flash_select_bank ; ...
$0A0E POP IP
$0A0F LD A, #0FFh ; return -1
dc1__return:
$0A11 POP HL
$0A12 POP IX
$0A13 POP B
$0A15 LD [BR:80h], B
$0A17 POP BR
$0A18 POP EP
$0A19 RETE
; ----------------------
; MUST push EP, BR, SC (maybe from INT) before calling
skip_if_01_bit6_or_not_bit7:
$0A1A CARL set_br
$0A1D BIT [BR:01h], #80h ; if bit 7 is 0,
$0A20 JRS Z, si2__do_skip ; perform skip
$0A22 BIT [BR:01h], #40h ; if bit 6 is 0,
$0A25 JRS Z, si2__return ; don't skip
si2__do_skip:
$0A27 ADD SP, #0003h
$0A2B POP BR
$0A2C POP EP
$0A2D RETE
si2__return:
$0A2E RET ; only 0b10xxxxxx returns
; ----------------------
; If the startup action bit 0 is 0, prepare game 0.
; Otherwise, do further checks.
; Returns:
; Z: 0 = ready to boot, 1 = do more checks
; Clobbers: A, XP, IX
select_boot:
$0A2F CARL flash_detect
$0A32 JRS NZ, sb__return_fail ; fail if it's not a flash cart
$0A34 PUSH IP
$0A35 LD XP, #07h ; bank register
$0A38 LD A, #00h ; value to write
$0A3A CARL flash_select_bank
$0A3D LD XP, #04h ; read startup action
$0A40 LD IX, #1300h ; ...
$0A43 LD A, [IX] ; ...
$0A44 POP IP
$0A45 BIT A, #01h ; if it's 0,
$0A47 JRS NZ, sb__return
$0A49 XOR A, A ; start the first game automatically
$0A4A CARL flash_prepare_game ; ...
sb__return_fail:
$0A4D XOR A, A ; fail out
sb__return:
$0A4E RET
; ----------------------
; Arguments:
; A: cart eject IRQ priority
; Clobbers: B
_enable_cart_eject_irq:
$0A4F POP SC
$0A50 PUSH EP
$0A51 PUSH BR
$0A52 CARL set_br
$0A55 SWAP A ; same as A <<= 4
$0A56 AND A, #30h ; limit to 0~3
$0A58 PUSH SC
$0A59 LD SC, #0C0h ; interrupt flags to NMI-only
$0A5B LD B, [BR:21h] ; get IRQ priorities
$0A5D AND B, #0CFh ; clear cart eject's
$0A60 OR A, B ; set it to value in A
$0A61 LD [BR:21h], A ; ...
$0A63 POP SC
$0A64 LD [BR:55h], #01h ; set input level check time for ?? to 4ms?
$0A67 AND [BR:51h], #~02h ; set cart eject IRQ to rising edge (ejected)
$0A6A CARL wait_8000_cycles
$0A6D LD [BR:28h], #02h ; clear cart eject
$0A70 OR [BR:24h], #02h ; enable cart eject IRQ
$0A73 POP BR
$0A74 POP EP
$0A75 RET
; ----------------------
_disable_cart_eject_irq:
$0A76 PUSH EP
$0A77 PUSH BR
$0A78 CARL set_br
$0A7B AND [BR:24h], #~02h ; disable cart eject IRQ
$0A7E POP BR
$0A7F POP EP
$0A80 RETE
; ----------------------
; Arguments:
; I1~I0: compared with cart IRQs' priority
; Returns:
; Z: 1=priority equal (not >) and IRQ fired
_check_ejected:
$0A81 POP SC
$0A82 PUSH EP
$0A83 PUSH BR
$0A84 PUSH BA
$0A85 CARL set_br
$0A88 LD A, SC ; get interrupt flags
$0A8A SRL A ; ...shift into place
$0A8C SRL A ; ...
$0A8E AND A, #30h ; ...
$0A90 LD B, [BR:21h] ; compare with cart IRQs' priority
$0A92 AND B, #30h ; ...
$0A95 CP A, B ; ...
$0A96 XOR SC, #01h ; invert Z, so 0 = not equal
$0A98 JRS Z, ce__return ; return if they're not equal
$0A9A BIT [BR:28h], #02h ; check if cart eject fired
$0A9D JRS Z, ce__return ; if not, return
$0A9F LD [BR:28h], #02h ; if so, clear
ce__return:
$0AA2 POP BA
$0AA3 POP BR
$0AA4 POP EP
$0AA5 RET
; ----------------------
_enter_normal_operation:
$0AA6 PUSH EP
$0AA7 PUSH BR
$0AA8 CARL set_br
$0AAB LD SC, #0C0h ; interrupt flags to NMI-only
$0AAD CARS enter_normal_operation
$0AAF POP BR
$0AB0 POP EP
$0AB1 RETE
enter_normal_operation:
$0AB2 PUSH IX
$0AB3 AND [BR:02h], #~01h ; write 00 (intended) to VD1C (mid operating voltage)
$0AB6 LD IX, #20 ; wait 83 cycles
eno__wait_80:
$0AB9 DEC IX
$0ABA JRS NZ, eno__wait_80
$0ABC OR [BR:02h], #04h ; write 1 to OSCC
$0ABF LD IX, #409 ; wait 1639 cycles
eno__wait_1636:
$0AC2 DEC IX
$0AC3 JRS NZ, eno__wait_1636
$0AC5 OR [BR:02h], #08h ; clock CPU with OSC3
$0AC8 OR [BR:02h], #10h ; ??
$0ACB POP IX
$0ACC RET
; ----------------------
_enter_low_power_operation:
$0ACD PUSH EP
$0ACE PUSH BR
$0ACF CARL set_br
$0AD2 LD SC, #0C0h ; interrupt flags to NMI-only
$0AD4 CARS enter_low_power_operation
$0AD6 POP BR
$0AD7 POP EP
$0AD8 RETE
enter_low_power_operation:
$0AD9 AND [BR:02h], #~10h ; ??
$0ADC AND [BR:02h], #~08h ; clock CPU with OSC1
$0ADF AND [BR:02h], #~04h ; turn off OSC3
$0AE2 OR [BR:02h], #01h ; write 01 (intended) to VD1C (lowest operating voltage)
$0AE5 RET
; ----------------------
_cart_slot_off:
$0AE6 PUSH EP
$0AE7 PUSH BR
$0AE8 CARL set_br
$0AEB LD SC, #0C0h ; interrupt flags to NMI-only
$0AED CARS cart_slot_off
$0AEF POP BR
$0AF0 POP EP
$0AF1 RETE
cart_slot_off:
$0AF2 AND [BR:02h], #~40h ; ??
unpower_cart:
$0AF5 OR [BR:71h], #04h ; turn off cart power
$0AF8 RET
; ----------------------
_cart_slot_on:
$0AF9 PUSH EP
$0AFA PUSH BR
$0AFB CARL set_br
$0AFE LD SC, #0C0h ; interrupt flags to NMI-only
$0B00 CARS cart_slot_on
$0B02 POP BR
$0B03 POP EP
$0B04 RETE
cart_slot_on:
$0B05 OR [BR:02h], #40h ; ??
power_cart:
$0B08 PUSH IX
$0B09 PUSH BA
$0B0A AND [BR:71h], #~04h ; power the cart
$0B0D OR [BR:02h], #80h ; ??
$0B10 LD IX, #0032h ; wait for 203 cycles
pc__wait_200:
$0B13 DEC IX
$0B14 JRS NZ, pc__wait_200
$0B16 LD A, [2AAAh] ; dummy read to cancel any half-input flash commands
$0B1A OR [BR:02h], #80h ; ??
$0B1D POP BA
$0B1E POP IX
$0B1F RET
; ----------------------
; Returns
; Z: 0=cart is inserted
_cart_detect:
$0B20 POP SC
$0B21 PUSH EP
$0B22 PUSH BR
$0B23 CARL set_br
$0B26 BIT [BR:53h], #02h
$0B29 XOR SC, #01h
$0B2B POP BR
$0B2C POP EP
$0B2D RET
; ----------------------
; Arguments:
; XP:IX: start address of structure
_read_structure:
$0B2E PUSH EP ; omg just use PUSH ALE
$0B2F PUSH BR
$0B30 CARL set_br
$0B33 PUSH IP
$0B34 PUSH IX
$0B35 PUSH IY
$0B36 PUSH HL
$0B37 PUSH B
$0B39 LD SC, #0C0h ; interrupt flags to NMI-only
$0B3B LD A, [IX] ; read byte $00
$0B3C BIT A, #01h ; if bit 0 is 0,
$0B3E LD A, SC ; (save result for later)
$0B40 JRS Z, loc_0x0B4E ;
$0B42 ADD IX, #000Ah ; read byte $0A
$0B45 LD H, [IX] ; ...
$0B46 DEC IX ; read byte $09
$0B47 LD L, [IX] ; ...
$0B48 DEC IX ; read byte $08
$0B49 PUSH HL
$0B4A LD B, [IX] ; ...
$0B4B DEC IX ; addr $07
$0B4C JRS loc_0x0B51
loc_0x0B4E:
$0B4E ADD IX, #0007h ; addr $07
loc_0x0B51:
$0B51 PUSH BA
$0B52 LD B, [IX] ; read byte $07
$0B53 DEC IX ; read byte $06
$0B54 LD A, [IX] ; ...
$0B55 DEC IX ; read byte $05
$0B56 LD H, [IX] ; ...
$0B57 DEC IX ; read byte $04
$0B58 LD L, [IX] ; ...
$0B59 DEC IX ; addr $03
$0B5A LD YP, A ; byte $06 to YP
$0B5C LD IY, HL ; bytes $05~04 to IY
$0B5E LD A, [IX] ; read byte $03
$0B5F DEC IX ; read byte $02
$0B60 LD H, [IX] ; ...
$0B61 DEC IX ; read byte $01
$0B62 LD L, [IX] ; ...
$0B63 LD XP, A ; byte $03 to XP, why is this necessary?
$0B65 LD IX, HL ; bytes $02~$01 to IX
$0B67 OR [BR:02h], #80h ; ??
$0B6A LD [IX], #0FFh ; write 0xFF to IX
$0B6C OR [BR:02h], #80h ; ???
$0B6F LD A, [IY] ; read byte into A
$0B70 OR [BR:02h], #80h ; ????
$0B73 CP A, B ; compare with byte $07
$0B74 POP BA ; restore: B = byte $08, A = state of byte $00 bit 0
$0B75 PUSH SC ; store result of CP
$0B76 LD SC, A ; re-check byte $00 bit 0
$0B78 JRS Z, loc_0x0B82 ; which allows B to definitely be byte $08
$0B7A POP SC ; just popped to allow POP BA
$0B7B LD L, B ; L = byte $08
$0B7C POP BA ; (from $0B49) B = byte $0A, A = byte $09
$0B7D LD H, A ; H = byte $09
$0B7E LD A, B ; A = byte $0A
$0B7F PUSH SC ; pushed back to stack unchanged
; subroutine should fetch [SP+3] for the result of
; the compare then put its result in its place
$0B80 CARS call_a_hl
loc_0x0B82:
$0B82 POP BA ; B = B before call, A = SC from $0B75 or $0B7F
$0B83 AND A, #01h ; A = Z, result of compare with byte $07
$0B85 POP HL
$0B86 POP IY
$0B87 POP IX
$0B88 POP IP
$0B89 POP BR
$0B8A POP EP
$0B8B RETE
; ----------------------
; Used by _read_structure
; Arguments
; A: code page
; HL: subroutine address
; [SP+3]: compare byte result (from SC)
; Clobbers: any/all
call_a_hl:
$0B8C LD NB, A
$0B8E JP HL
; ----------------------
; Arguments
; A: rate divider
_set_prc_rate:
$0B8F PUSH EP
$0B90 PUSH HL
$0B91 AND A, #07h ; limit to 3 bits
$0B93 SLL A ; position
$0B95 LD EP, #00h
$0B98 LD HL, #2081h
$0B9B AND [HL], #0F1h ; reset those bits
$0B9E OR [HL], A ; insert new value
$0BA0 POP HL
$0BA1 POP EP
$0BA2 RETE
; ----------------------
; Returns
; A: rate divider value
_get_prc_rate:
$0BA3 PUSH EP
$0BA4 PUSH BR
$0BA5 CARL set_br
$0BA8 LD A, [BR:81h]
$0BAA AND A, #0Eh
$0BAC SRL A
$0BAE POP BR
$0BAF POP EP
$0BB0 RETE
; ----------------------
; This has to do with the Game Select screen
; Returns
; Z: 1=$2001 bits 2~0 are a valid game index
_game_id_valid:
$0BB1 POP SC
$0BB2 PUSH EP
$0BB3 PUSH BR
$0BB4 CARL set_br
$0BB7 BIT [BR:01h], #08h
$0BBA POP BR
$0BBB POP EP
$0BBC RET
; ----------------------
; Write to IR bit, wait, then reset IR bit
; Arguments:
; IY = $2061
; B = cycles to wait / 4
_ir_pulse:
$0BBD LD [IY], #02h
irp__wait:
$0BBF DJR NZ, irp__wait
$0BC1 LD [IY], #00h
$0BC3 RETE
; ----------------------
; Set [BR:ll] indexing for hardware registers.
; Returns:
; EP: page for [BR:ll] (set to 0)
; BR: BR set to $20 for hardware register space
set_br:
$0BC4 LD EP, #00h
$0BC7 LD BR, #20h
$0BC9 RET
; ----------------------
wait_8000_cycles: ; including the CARL to get here
$0BCA PUSH IX
$0BCB LD IX, #1995
w8kc__loop:
$0BCE DEC IX
$0BCF JRS NZ, w8kc__loop
$0BD1 POP IX
$0BD2 RET
; ----------------------
; Arguments:
; H: previous inverted keypad state
; Returns:
; A: newly pressed keys
; H: keys being pressed, new or old
; Clobbers: B
read_keys_buffer:
$0BD3 LD B, [BR:52h] ; read current keymap state
$0BD5 CPL B ; invert so 1 is pressed
$0BD7 LD A, H
$0BD8 LD H, B
$0BD9 XOR A, B ; 0 out any keys that were pressed last time
$0BDA AND A, B ; ...
$0BDB RET
; ----------------------
; Arguments:
; IY: tilemap source
; Returns:
; IX: $1360 (start of tilemap memory)
; Clobbers: L
load_tilemap:
$0BDC LD IX, #1360h ; tilemap base
$0BDF LD L, #00h ; loop 0 to 96 (exclusive), a full 12x8 screen
ltm__loop:
$0BE1 LD B, [IY+L] ; read from map starting at IY
$0BE3 LD [IX+L], B ; copy into IX
$0BE5 INC L
$0BE6 CP L, #60h
$0BE9 JRS NZ, ltm__loop
$0BEB RET
; ----------------------
; Arguments:
; IY: tilemap source
; Returns:
; IX: $1378 (top of names list)
; Clobbers: L
load_middle_5_tilemap: ; middle 5 rows
$0BEC LD IX, #1378h ; start of row 2 (0-based)
$0BEF LD L, #00h ; loop from 0 to 60 (exclusive), a 12x5 subsection
$0BF1 AND [BR:80h], #~08h ; disable LCD controller
lm5tm__loop:
$0BF4 LD B, [IY+L] ; read from map starting at IY
$0BF6 LD [IX+L], B ; copy into IX
$0BF8 INC L
$0BF9 CP L, #3Ch
$0BFC JRS NZ, lm5tm__loop
$0BFE RET
; ----------------------
; Copy one frame while the LCD controller is disabled
; Will disable it again by the end of the function
copy_one_frame:
$0BFF LD [BR:27h], #80h ; clear Frame Copy Complete
$0C02 OR [BR:80h], #08h ; enable LCD controller
cof__wait_for_vsync:
$0C05 BIT [BR:27h], #80h ; wait for Frame Copy Complete to trigger
$0C08 JRS Z, cof__wait_for_vsync
$0C0A AND [BR:80h], #~08h ; disable LCD controller
$0C0D RET
; ----------------------
; Arguments:
; A: number of game structs loaded
; Clobbers: B, HL, IX, IY
game_select_screen:
$0C0E DEC A ; get starting index of final row
$0C0F LD L, #12 ; ...a row is 12 tiles wide (screen width)
$0C11 MLT ; ...
$0C13 LD [FinalNameRowStart], L ; ...
$0C17 CP L, #48 ; if L < 48,
$0C1A JRS C, gss__min_final
$0C1C LD L, #48 ; 48 is the start of the 5th entry, basing 0 at the first entry
gss__min_final:
$0C1E LD [FinalNameRowStartMin48], L
$0C22 LD IY, #GameSelectMap ; load the base tilemap (not the five name lines)
$0C25 CARL load_tilemap
$0C28 LD IX, #CurrentRowStart
$0C2B LD IY, #MoveCooldown
$0C2E XOR A, A
$0C2F LD [KeyHistory], A ; no keys pressed
$0C33 LD A, [BR:52h] ; take cpl of keymap (so 1=pressed)
$0C35 CPL A ; ...
$0C37 LD H, A ; ...into H
$0C38 XOR A, A ; draw names from first name
$0C39 LD L, A ; with the first name selected
$0C3A LD [IX], A ; CurrentRowStart = 0
$0C3B CARL draw_game_names
gss__main_loop:
$0C3E LD [BR:27h], #40h ; clear frame divided overflow
gss__ml__wait_for_framediv_overflow:
$0C41 BIT [BR:27h], #40h
$0C44 JRS Z, gss__ml__wait_for_framediv_overflow
$0C46 CARL init_display_without_wait
$0C49 CARL apply_default_contrast
$0C4C CARL read_keys_buffer
$0C4F LD B, A
$0C50 LD A, [KeyHistory] ; update key history with newly pressed keys
$0C54 OR A, B ; ...
$0C55 LD [KeyHistory], A ; ...
$0C59 EX A, B ; (after EX) A = new keys, B = updated key history
$0C5A OR A, A ; if any new keys were pressed
$0C5B JRS Z, gss__ml__no_new_keys
$0C5D LD [IY], #13 ; start cooldown at 13 frames
$0C5F JRS gss__ml__continue
gss__ml__no_new_keys:
$0C61 LD A, H ; if we're still pressing a key we've (ever) pressed before
$0C62 AND A, B ; (including any pressed this frame),
$0C63 JRS Z, gss__ml__wait_for_keyup
$0C65 LD B, [IY] ; decrement cooldown
$0C66 DEC B ; ...
$0C67 LD [IY], B ; ...
$0C68 JRS NZ, gss__ml__wait_for_keyup ; and wait until we're not
$0C6A LD [IY], #5 ; reset cooldown to 5 frames
gss__ml__continue:
$0C6C CARL gss__check_keys
gss__ml__wait_for_keyup:
$0C6F JRS gss__main_loop
; ----------------------
gss__check_keys:
$0C71 BIT A, #05h ; check if A or C was pressed
$0C73 JRS Z, gss__ck__back
$0C75 LD H, #0 ; divide selected row tile index
$0C77 LD A, #12 ; ...by the screen width
$0C79 DIV ; ...to get the game index
$0C7B LD A, L ; ...and store it in A
$0C7C AND [BR:80h], #~08h ; disable LCD controller
$0C7F LD IY, #BlankTilemap ; load the blank tilemap
$0C82 CARL load_tilemap ; ...
$0C85 CARL copy_one_frame ; draw it to screen
$0C88 JRL run_selected_game ; boot the game
gss__ck__back:
$0C8B BIT A, #82h ; check if power or B button was pressed
$0C8D JRS Z, gss__ck__up
$0C8F JRL shutdown
gss__ck__up:
$0C92 BIT A, #08h ; check if up button was pressed
$0C94 JRS Z, gss__ck__down
$0C96 LD A, L ; if selected row tile index
$0C97 OR A, A ; ...is not the first row,
$0C98 JRS Z, gss__ck__return
$0C9A SUB A, #12 ; then move the cursor up 1 row (12 tiles)
$0C9C LD L, A ; ...
$0C9D OR A, A ; if it's the first now,
$0C9E LD A, [IX] ; (A = CurrentRowStart)
$0C9F JRS Z, gss__ck__redraw2 ; then redraw
$0CA1 LD B, L ; if the top visible row changed,
$0CA2 CP A, B ; ...
$0CA3 JRS NZ, gss__ck__redraw2 ; then redraw
$0CA5 SUB A, #12 ; otherwise, move the top up one
$0CA7 LD [IX], A ; ...
gss__ck__redraw2:
$0CA8 JRS gss__ck__redraw
gss__ck__down:
$0CAA BIT A, #10h ; check if down button was pressed
$0CAC JRS Z, gss__ck__return
$0CAE LD A, L ; if the selected row
$0CAF LD B, [FinalNameRowStart] ; ...is not the final row,
$0CB3 CP A, B ; ...
$0CB4 JRS Z, gss__ck__return
$0CB6 ADD A, #12 ; then move the cursor down 1 row (12 tiles)
$0CB8 LD L, A ; ...
$0CB9 CP A, B ; if it's now the final row,
$0CBA LD A, [IX] ; (A = CurrentRowStart)
$0CBB JRS Z, gss__ck__redraw ; then redraw
$0CBD ADD A, [FinalNameRowStartMin48] ; otherwise if it's at the bottom of the list,
$0CC0 LD B, L ; ...
$0CC1 CP A, B ; ...
$0CC2 LD A, [IX] ; (A = CurrentRowStart)
$0CC3 JRS NZ, gss__ck__redraw
$0CC5 ADD A, #12 ; then move the top down 1 row
$0CC7 LD [IX], A
gss__ck__redraw:
$0CC8 CARL draw_game_names
gss__ck__return:
$0CCB RET
; ----------------------
; Arguments
; L: selected row in tiles from absolute top of names
; A: top of visible names as tile index
draw_game_names:
$0CCC PUSH HL
$0CCD PUSH IX
$0CCE PUSH IY
$0CCF LD H, L
$0CD0 LD IY, #GameNamesMap ; game select tilemap, names section
$0CD3 LD B, #00h
$0CD5 ADD IY, BA ; scroll to top of screen
$0CD7 CARL load_middle_5_tilemap ; draw names
$0CDA LD IY, #1372h ; tile 18 (6,1)
$0CDD OR A, A ; if a is at the top,
$0CDE JRS Z, dgn__list_is_at_top ; don't show an up arrow
$0CE0 LD [IY], #21h ; draw "up arrow with line above" tile
$0CE2 JRS dgn__check_list_bottom
dgn__list_is_at_top:
$0CE4 LD [IY], #1Fh ; draw "line above without arrow" tile
dgn__check_list_bottom:
$0CE6 LD B, A
$0CE7 ADD A, [FinalNameRowStartMin48] ; add visible rows (up to 12*4=48)
$0CEA LD IY, #13BAh ; tile 90 (6,7)
$0CED CP A, [FinalNameRowStart] ; if that is the last row,
$0CF0 JRS Z, dgn__list_is_at_bottom ; don't show a down arrow
$0CF2 LD [IY], #2Ch ; draw "down arrow with line below" tile
$0CF4 JRS dgn__draw_cursor
dgn__list_is_at_bottom:
$0CF6 LD [IY], #2Ah ; draw "line below without arrow" tile
dgn__draw_cursor:
$0CF8 LD A, H ; calculate position of selection arrow
$0CF9 SUB A, B ; ...
$0CFA ADD A, #01h ; ...
$0CFC LD B, #00h ; ...
$0CFE ADD IX, BA ; ...
$0D00 LD [IX], #0Bh ; draw selection arrow tile
$0D02 OR [BR:80h], #08h ; re-enable frame copy
$0D05 POP IY
$0D06 POP IX
$0D07 POP HL
$0D08 RET
; ----------------------
; Arguments:
; IX: start of animation footer
; IY: start of animation data
; Returns:
; Z: 0=animation ended, 1=key pressed
; A: 0=animation ended, otherwise keys that were pressed
; Clobbers: B, HL
play_animation:
$0D09 PUSH IX
$0D0A PUSH IY
$0D0B LD IY, #BlankTilemap
$0D0E CARL load_tilemap
$0D11 POP IY
$0D12 POP IX ; read in the footer
$0D13 LD A, [IX] ; length in rendered frames (lo byte)
$0D14 INC IX ; size in bytes of animation info
$0D15 LD B, [IX] ; and 1 hi bit for length
$0D16 SRL B ; put length's hi bit in C
$0D18 LD L, B ; store the size in L
$0D19 LD B, #00h ; put hi bit of total frame count back into b
$0D1B RL B ; ...
$0D1D LD IX, #TotalAnimFrames ; store total frame count
$0D20 LD [IX], BA ; ...as remaining frames to render
$0D22 LD [BR:27h], #40h ; clear frame divider overflow
pa__wait_for_framediv_overflow:
$0D25 BIT [BR:27h], #40h
$0D28 JRS Z, pa__wait_for_framediv_overflow
$0D2A LD [BR:27h], #40h ; clear frame divider overflow
$0D2D LD A, [BR:52h] ; take cpl of keymap (so 1=pressed)
$0D2F CPL A ; ...
$0D31 LD H, A ; ...into H
$0D32 PUSH L ; backup size of animation data
pa__read_loop:
$0D34 DEC L ; reading bytes right-to-left
$0D35 LD B, [IY+L] ; frame count + hi bit of addr
$0D37 DEC L
$0D38 LD A, [IY+L] ; map address offset (lo)
$0D3A JRS NZ, pa__not_done_reading
$0D3C POP L ; return to first frame if we finished reading
$0D3E PUSH L ; ...
pa__not_done_reading:
$0D40 SRL B
$0D42 LD [FramesRemaining], B ; store frame count here
$0D46 LD B, #00h ; map address offset hi bit back into b
$0D48 RL B ; ...
$0D4A PUSH HL
$0D4B PUSH IX
$0D4C PUSH IY
$0D4D LD IY, #InsertCartMaps ; load selected frame
$0D50 ADD IY, BA ; offset from the base address
$0D52 CARL load_middle_5_tilemap
$0D55 OR [BR:80h], #08h ; enable LCD controller
$0D58 POP IY
$0D59 POP IX
$0D5A POP HL
pa__draw_again:
pa__wait_for_framediv_overflow2:
$0D5B BIT [BR:27h], #40h
$0D5E JRS Z, pa__wait_for_framediv_overflow2
$0D60 LD [BR:27h], #40h ; clear frame divider overflow
$0D63 CARL init_display_without_wait
$0D66 CARL apply_default_contrast
$0D69 CARL read_keys_buffer ; read keys excluding anything pressed down since before animation started
$0D6C BIT A, #87h ; if power, A, B, or C is being pressed,
$0D6E JRS NZ, pa__return ; break
pa__continue_playing:
$0D70 LD BA, [IX] ; decrement total rendered frames remaining
$0D72 DEC BA ; ...
$0D73 LD [IX], BA ; ...
$0D75 JRS Z, pa__return ; break if none left
$0D77 LD A, [FramesRemaining] ; decrement rendered frames remaining for this image
$0D7B DEC A ; ...
$0D7C LD [FramesRemaining], A ; ...
$0D80 JRS NZ, pa__draw_again ; if there's more to render, keep drawing
$0D82 JRS pa__read_loop
pa__return:
$0D84 POP B ; first frame's source addr offset
$0D86 RET
; ----------------------
; Play low battery animation and check key presses.
; A/C -> skip shutdown, play software anyway.
; B/Power -> shutdown early.
; Assumes XP/YP = $00
; Clobbers: BA, HL, IX, IY
low_battery_screen:
$0D87 LD IX, #low_battery_animation_footer
$0D8A LD IY, #low_battery_animation
$0D8D CARL play_animation
$0D90 JRS Z, lbs__skip_shutdown ; if animation finished, don't shutdown
$0D92 BIT A, #82h ; if A or C was pressed,
$0D94 JRS Z, lbs__skip_shutdown ; don't shutdown
$0D96 JRL shutdown
lbs__skip_shutdown:
$0D99 AND [BR:80h], #~08h ; disable LCD controller
$0D9C LD IY, #BlankTilemap ; load blank screen for one frame
$0D9F CARL load_tilemap ; ...
$0DA2 CARL copy_one_frame ; ...force draws it despite the shutoff
$0DA5 RET
; ----------------------
; Play insert cartridge animation and check key presses.
; A/C -> check for cartridge again.
; B/Power -> shutdown early.
; Assumes XP/YP = $00
; Clobbers: any
insert_cart_screen:
$0DA6 LD SC, #0C0h ; interrupt flags to NMI-only
$0DA8 OR [BR:71h], #04h ; cart power off
$0DAB LD IX, #insert_cart_animation_footer
$0DAE LD IY, #insert_cart_animation
$0DB1 CARL play_animation
ics__check_loop:
$0DB4 JRS Z, ics__shutdown ; if animation finished, shutdown
$0DB6 BIT A, #82h ; if power or B key was pressed,
$0DB8 JRS NZ, ics__shutdown ; shutdown
$0DBA BIT [BR:53h], #02h ; if card was inserted,
$0DBD JRL Z, _reset2 ; reset to boot it
$0DC0 CARS ics__continue_playing
$0DC2 JRS ics__check_loop
ics__shutdown:
$0DC4 JRL shutdown
ics__continue_playing:
$0DC7 PUSH B
$0DC9 JRL pa__continue_playing
; ----------------------
; Decode encoded maps and graphics from imgdata to RAM
; Clobbers: BA, L, IX, IY
decode_picture:
$0DCC LD IY, #imgdata ; source
$0DCF LD IX, #BlankTilemap ; destination
dp__loop:
$0DD2 LD A, [IY] ; read encoded byte
$0DD3 INC IY ; increment offset
$0DD4 CP A, #0F9h ; compare value to 0xF9
$0DD6 JRS NZ, dp__checkFB ; if it isn't, jump
$0DD8 LD B, [IY] ; value after F9 is number of 0s to fill - 1
$0DD9 INC IY ; inc offset
$0DDA INC B ; add 1 to find actual number of 0s
$0DDB XOR A, A ; set A to 0
dp__zero_fill_loop:
$0DDC LD [IX], A ; store 0 in destination offset
$0DDD INC IX ; inc dest offset
$0DDE DJR NZ, dp__zero_fill_loop ; dec B and loop if not 0
$0DE0 JRS dp__continue
dp__checkFB:
$0DE2 CP A, #0FBh ; compare value to 0xFB
$0DE4 JRS NZ, dp__raw_byte ; if it's not, continue as normal byte to write
$0DE6 LD A, [IY] ; otherwise, store next source byte in A
$0DE7 INC IY ; inc offset
$0DE8 LD B, [IY] ; next source byte into B
$0DE9 INC IY ; increment source offset
$0DEA INC B ; and B
dp__fill_loop:
$0DEB LD [IX], A ; store A in destination offset
$0DEC INC IX ; increment destination
$0DED DJR NZ, dp__fill_loop ; dec B and loop if not 0
$0DEF JRS dp__continue ; end of fill section
dp__raw_byte:
$0DF1 LD [IX], A ; store A in destination offset
$0DF2 INC IX ; increment destination offset
dp__continue:
$0DF3 CP IY, #1000h ; if not all 0x1C0 bytes have been decoded yet,
$0DF6 JRS NZ, dp__loop ; go back to the decode loop
$0DF8 LD IX, #VFlipStart ; tile gfx for top left corner of game select border
$0DFB LD IY, #VFlippedTiles ; the first byte after the decoded data
; v-flip and append last 11 tiles
dp__vflip_loop:
$0DFE LD A, [IX] ; load one byte of tile data
$0DFF LD L, #8 ; 8 bits
dp__vflip_byte:
$0E01 RL A ; pop top bit of tile data
$0E03 RR B ; pushed onto top bit of B
$0E05 DEC L
$0E06 JRS NZ, dp__vflip_byte
$0E08 LD [IY], B ; store flipped data
$0E09 INC IX ; next source addr
$0E0A INC IY ; next destination addr
$0E0B CP IX, #VFlippedTiles ; since they're sequential, if IX makes it here we're done flipping
$0E0E JRS NZ, dp__vflip_loop
$0E10 RET
; ----------------------
$0E11 DB 0FFh, 0FFh, 0FFh, 0FFh, 0FFh, 0FFh, 0FFh, 0FFh, 0FFh
; ----------------------
nintendo_string:
$0E1A ASCII "NINTENDO"
; ----------------------
; Format is backwards, starting at the right and going left.
; Each image of the animation is two bytes of the form:
; map address (lo), 0bLLLLLLLH
; L = length in rendered frames
; H = map address (hi)
; The map address is an offset from $1650
; The footer is two bytes of the form:
; length (lo), 0bNNNNNNNH
; N = number of bytes in the animation info
; H = length (hi)
low_battery_animation:
$0E22 DB 68h, 6Fh, 0A4h, 13h, 68h, 13h, 0A4h, 13h, 68h, 13h, 0A4h, 13h, 68h, 13h, 2Ch, 13h
$0E32 DB 0F0h, 12h, 0B4h, 12h
$0E36 DB 98h, 29h
insert_cart_animation:
$0E38 DB 78h, 0Ch, 3Ch, 18h, 00h, 3Ch
$0E3E DB 20h, 0Dh
; Simple RLE compression algorithm:
; 0xF9LL inserts LL+1 zeroes
; 0xFBXXLL inserts the byte 0xXX LL+1 times
; otherwise it copies the raw byte
imgdata:
; Blank tilemap starting at $1530
$0E40 DB 0F9h, 60h
; Including one zero byte from above, the GAME SELECT screen's rows 0,1,7
$0E42 DB 01h, 02h, 03h, 04h, 05h, 06h, 07h, 08h, 09h, 0Ah, 00h
$0E4D DB 1Eh, 0FBh, 1Fh, 09h, 20h
$0E52 DB 0F9h, 3Bh
$0E54 DB 29h, 0FBh, 2Ah, 09h, 2Bh
; The GAME SELECT screen's middle 5 rows (two pages?)
$0E59 DB 0Ch, 00h, 34h, 35h, 36h, 37h, 38h, 39h, 3Ah, 3Bh, 3Ch, 0Dh
$0E65 DB 0Ch, 00h, 3Dh, 3Eh, 3Fh, 40h, 41h, 42h, 43h, 44h, 45h, 0Dh
$0E71 DB 0Ch, 00h, 46h, 47h, 48h, 49h, 4Ah, 4Bh, 4Ch, 4Dh, 4Eh, 0Dh
$0E7D DB 0Ch, 00h, 4Fh, 50h, 51h, 52h, 53h, 54h, 55h, 56h, 57h, 0Dh
$0E89 DB 0Ch, 00h, 58h, 59h, 5Ah, 5Bh, 5Ch, 5Dh, 5Eh, 5Fh, 60h, 0Dh
$0E95 DB 0Ch, 00h, 61h, 62h, 63h, 64h, 65h, 66h, 67h, 68h, 69h, 0Dh
$0EA1 DB 0Ch, 00h, 6Ah, 6Bh, 6Ch, 6Dh, 6Eh, 6Fh, 70h, 71h, 72h, 0Dh
$0EAD DB 0Ch, 00h, 73h, 74h, 75h, 76h, 77h, 78h, 79h, 7Ah, 7Bh, 0Dh
; Insert cart animation maps
$0EB9 DB 0F9h, 0Fh, 12h, 13h, 14h, 15h ; Frame 1
$0EBF DB 0F9h, 07h, 16h, 17h, 18h, 19h
$0EC5 DB 0F9h, 07h, 1Ah, 1Bh, 1Ch, 1Dh
$0ECB DB 0F9h, 14h, 0Eh, 0Fh ; Frame 2
$0ECF DB 0F9h, 08h, 12h, 13h, 14h, 15h
$0ED5 DB 0F9h, 07h, 16h, 17h, 18h, 19h
$0EDB DB 0F9h, 07h, 1Ah, 1Bh, 1Ch, 1Dh
$0EE1 DB 0F9h, 14h, 10h, 11h ; Frame 3
$0EE5 DB 0F9h, 08h, 12h, 13h, 14h, 15h
$0EEB DB 0F9h, 07h, 16h, 17h, 18h, 19h
$0EF1 DB 0F9h, 07h, 1Ah, 1Bh, 1Ch, 1Dh
; Low battery animation maps
$0EF7 DB 0F9h, 1Fh, 22h, 23h, 23h, 24h ; Frame 1
$0EFD DB 0F9h, 07h, 2Dh, 2Eh, 2Eh, 2Fh
$0F03 DB 0F9h, 2Bh, 25h, 26h, 23h, 24h ; Frame 2
$0F09 DB 0F9h, 07h, 30h, 31h, 2Eh, 2Fh
$0F0F DB 0F9h, 2Bh, 25h, 27h, 26h, 24h ; Frame 3
$0F15 DB 0F9h, 07h, 30h, 32h, 31h, 2Fh
$0F1B DB 0F9h, 2Bh, 25h, 27h, 27h, 28h ; Frame 4, 6, 8, 10
$0F21 DB 0F9h, 07h, 30h, 32h, 32h, 33h
$0F27 DB 0F9h, 65h ; Frame 5, 7, 9 (+ some 0s from below)
; Tile graphics
;DB ; tile 0: blank
$0F29 DB 07Ch, 0FEh ; tile 1: start of G
$0F2B DB 082h, 092h, 0F6h, 074h, 000h, 0F0h, 0FCh, 026h ; tile 2: GA
$0F33 DB 026h, 0FCh, 0F0h, 000h, 0FEh, 0FEh, 008h, 010h ; tile 3: AM
$0F3B DB 008h, 0FEh, 0FEh, 000h, 0FEh, 0FEh, 092h, 092h ; tile 4: ME
$0F43 DB 092h, 0F9h, 003h, 0CCh, 0DEh, 092h ; tile 5: ES
$0F49 DB 0F6h, 066h, 000h, 0FEh, 0FEh, 092h, 092h, 092h ; tile 6: SE
$0F51 DB 000h, 0FEh, 0FEh, 080h, 080h, 080h, 000h, 0FEh ; tile 7: LE
$0F59 DB 0FEh, 092h, 092h, 092h, 000h, 07Ch, 0FEh, 082h ; tile 8: EC
$0F61 DB 082h, 0C6h, 044h, 000h, 002h, 002h, 0FEh, 0FEh ; tile 9: CT
$0F69 DB 002h, 002h, 0F9h, 007h ; tile 10: T
$0F6D DB 0FEh, 07Ch, 038h, 010h, 0F9h, 005h ; tile 11: select arrow
$0F73 DB 0FFh, 0F9h, 005h ; tile 12: left side of border
$0F76 DB 0FFh, 0F9h, 004h ; tile 13: right side of border
$0F79 DB 0F0h, 0FBh, 008h, 00Bh ; tile 14: extended cartridge left
$0F7D DB 0F0h, 000h ; tile 15: extended cartridge right
$0F7F DB 000h, 080h, 0FBh, 040h, 00Bh ; tile 16: inserted cartridge left
$0F84 DB 080h, 0F9h ; tile 17: inserted cartridge right
$0F86 DB 006h, 0F8h, 004h ; tile 18: PM (0,0)
$0F89 DB 082h, 0E1h, 0E1h, 031h, 031h, 0FBh, 039h, 005h ; tile 19: PM (1,0)
$0F91 DB 031h, 031h, 0E1h, 0E1h, 082h ; tile 20: PM (2,0)
$0F96 DB 006h, 0FAh, 004h, 0F8h, 0F9h, 008h ; tile 21: PM (3,0)
$0F9C DB 0FCh, 003h, 000h ; tile 22: PM (0,1)
$0F9F DB 007h, 0DFh, 0DFh, 030h, 030h, 0FBh, 070h, 005h ; tile 23: PM (1,1)
$0FA7 DB 030h, 030h, 01Fh, 09Fh, 087h ; tile 24: PM (2,1)
$0FAC DB 000h, 003h, 0FDh, 0F9h, 009h ; tile 25: PM (3,1)
$0FB1 DB 007h, 008h, 013h ; tile 26: PM (0,2)
$0FB4 DB 013h, 02Fh, 02Fh, 023h, 023h, 0FBh, 020h, 005h ; tile 27: PM (1,2)
$0FBC DB 046h, 046h, 040h, 081h, 081h ; tile 28: PM (2,2)
$0FC1 DB 080h, 040h, 03Fh, 0F9h, 008h ; tile 29: PM (3,2)
$0FC6 DB 0FEh, 0FBh, 002h, 00Dh ; tile 30: top left corner of border
;DB ; tile 31: top side of border
$0FCA DB 0FEh, 0F9h, 003h ; tile 32: top right corner of border
$0FCD DB 002h, 022h, 032h, 03Ah, 03Ah, 032h, 022h, 002h ; tile 33: more games upward arrow
$0FD5 DB 0E0h, 020h, 0BCh, 084h, 0F4h, 0F4h, 004h, 0F4h ; tile 34: full battery top left
$0FDD DB 0F4h, 0F4h, 004h, 0F4h, 0F4h, 0F4h, 004h, 0F4h ; tile 35: full battery top middle
$0FE5 DB 0F4h, 0F4h, 004h, 0F4h, 0F4h, 0F4h, 004h, 0FCh ; tile 36: full battery top right
$0FED DB 0E0h, 020h, 03Ch, 0FBh, 004h, 007h ; tile 37: empty battery top left
$0FF3 DB 0F4h, 0F4h, 0F4h, 004h, 0F4h ; tile 38: drained battery top middle
$0FF8 DB 0FBh, 004h, 00Ah ; tile 39: empty battery top middle
$0FFB DB 0F4h, 0F4h, 0F4h, 004h, 0FCh ; tile 40: drained battery top right
; tiles 41-51 (inclusive) are v-flipped versions
; of 30-40 which are generated at runtime