"""The board's memory map, port decode and I/O chip, against the wiki and MAME's maps.""" from __future__ import annotations from sms_python.board import ( GAME_GEAR, MEGA_DRIVE_PBC, SC3000, SC3000_PAL, SG1000, SMS1_PAL, SMS2, SMS_JAPAN, Board, ) from sms_python.cartridge import BANK, Cartridge from sms_python.io import BUTTON_1, UP, IOChip, Joypad, keys_for from sms_python.vdp import MCLOCKS_PER_LINE def rom(banks: int) -> Cartridge: return Cartridge(b"".join(bytes([bank]) * BANK for bank in range(banks))) def test_sms_ram_is_8k_mirrored(): board = Board(SMS2, rom(4)) board.write_byte(0xC123, 0x42) assert board.read_byte(0xE123) == 0x42 def test_sg1000_ram_is_1k_mirrored(): board = Board(SG1000, rom(2)) board.write_byte(0xC005, 0x77) assert board.read_byte(0xC405) == 0x77 assert board.read_byte(0xFC05) == 0x77 def test_mapper_writes_reach_ram_and_the_cartridge(): board = Board(SMS2, rom(16)) board.write_byte(0xFFFF, 9) assert board.read_byte(0x8000) == 9 assert board.read_byte(0xFFFF) == 9 # "the last value written ... read back" assert board.read_byte(0xDFFF) == 9 # the same RAM byte def test_sg1000_has_no_mapper_registers(): board = Board(SG1000, rom(4)) board.write_byte(0xFFFF, 3) assert board.read_byte(0x8000) == 2 def test_bios_then_cartridge_through_port_3e(): bios = Cartridge(b"\xb1" * BANK) board = Board(SMS2, rom(4), bios=bios) assert board.read_byte(0x0000) == 0xB1 # MAME power-on $3E = $E0 with a BIOS board.write_port(0x3E, 0xA8) # BIOS off, cartridge on assert board.read_byte(0x0000) == 0x00 def test_mapper_writes_reach_only_the_enabled_slot(): # The export BIOS banks its own ROM through $FFFD-$FFFF; the cartridge, disabled, # must boot with banks 0, 1, 2 (Versus, from the compatibility sweep). bios = Cartridge(b"".join(bytes([0xB0 + bank]) * BANK for bank in range(4))) board = Board(SMS2, rom(16), bios=bios) board.write_byte(0xFFFF, 3) assert board.read_byte(0x8000) == 0xB3 board.write_port(0x3E, 0xA8) # BIOS off, cartridge on assert board.read_byte(0x8000) == 2 board.write_byte(0xFFFF, 5) assert board.read_byte(0x8000) == 5 assert bios.banks[2] == 3 def test_port_decode_uses_a7_a6_a0(): board = Board(SMS2, rom(1)) board.write_port(0xBF, 0x34) # control, via its canonical port board.write_port(0x81, 0x40) # ... and a mirror: VRAM write at $0034 board.write_port(0x80, 0x99) assert board.vdp.vram[0x34] == 0x99 board.write_port(0x40, 0x92) # a PSG mirror: channel 0 volume 2 board.advance(2) # the PSG takes a write in on its next clock assert board.psg.volume[0] == 2 def test_v_counter_reads_through_any_even_port_in_40_7f(): board = Board(SMS2, rom(1)) board.advance(MCLOCKS_PER_LINE // 3 * 10) assert board.read_port(0x7E) == 10 assert board.read_port(0x42) == 10 def test_th_rising_edge_latches_the_h_counter(): board = Board(SMS2, rom(1)) board.advance(1000) board.write_port(0x3F, 0x55) # TH A and B outputs, low board.write_port(0x3F, 0xF5) # ... high: a rising edge latched = board.read_port(0x7F) board.advance(50) assert board.read_port(0x7F) == latched # "HCounter keeps value" def test_export_console_reads_back_th_outputs(): chip = IOChip() chip.write_control(0xF5) # TH outputs high assert chip.read_dd() & 0xC0 == 0xC0 chip.write_control(0x55) # TH outputs low assert chip.read_dd() & 0xC0 == 0x00 def test_japanese_console_reads_zero_from_th_outputs(): board = Board(SMS_JAPAN, rom(1)) board.write_port(0x3F, 0xF5) assert board.read_port(0xDD) & 0xC0 == 0x00 # the region-detection difference def test_joypads_are_active_low(): chip = IOChip(pad1=Joypad({UP, BUTTON_1}), pad2=Joypad({UP})) assert chip.read_dc() == 0xFF & ~0x01 & ~0x10 & ~0x40 def test_game_gear_port_0_start_and_region(): board = Board(GAME_GEAR, rom(1)) assert board.read_port(0x00) == 0xC0 # start released, overseas board.gg_ports.start_pressed = True assert board.read_port(0x00) == 0x40 board.write_port(0x06, 0x0F) assert board.psg.stereo == 0x0F def test_game_gear_ext_port_with_nothing_plugged_in(): # MAME 0.285's model (GameGearPorts): inputs read 1, outputs their data bit, and # PC0/PC1 appear as port $DC bits 6-7 (Streets of Rage reads $7F there). board = Board(GAME_GEAR, rom(1)) assert (board.read_port(0x01), board.read_port(0x02)) == (0xFF, 0xFF) assert (board.read_port(0xDC), board.read_port(0xDD)) == (0xFF, 0xFF) board.write_port(0x01, 0x00) board.write_port(0x02, 0x7E) # PC0 an output driving 0 assert board.read_port(0x01) == 0x7E assert board.read_port(0xDC) == 0xBF board.write_port(0x02, 0x3D) # PC1 and PC6 outputs, driving 0 assert board.read_port(0xDC) == 0x7F assert board.read_port(0xDD) == 0x7F assert board.read_port(0xC5) == 0xFF # only $C0/$C1/$DC/$DD decode def test_pause_is_nmi_except_on_the_game_gear(): sms = Board(SMS2, rom(1)) sms.press_pause() assert sms.nmi gg = Board(GAME_GEAR, rom(1)) gg.press_pause() assert not gg.nmi def test_game_gear_send_buffer_is_occupied_while_a_byte_goes_out(): # Port $05 bit 0 (GearToGearCable.pmwiki), with no cable: the first byte goes # straight to the shift register, the second waits 10 bit times for it. board = Board(GAME_GEAR, rom(1)) board.write_port(0x05, 0x38) # 4800 bits per second byte = 10 * 3_579_545 // 4800 board.write_port(0x03, 0xFB) assert board.read_port(0x05) == 0x38 board.write_port(0x03, 0x18) assert board.read_port(0x05) == 0x39 board.cycles += byte - 1 assert board.read_port(0x05) == 0x39 board.cycles += 1 assert board.read_port(0x05) == 0x38 # the second byte is going out now board.write_port(0x03, 0x25) assert board.read_port(0x05) == 0x39 board.cycles += byte assert board.read_port(0x05) == 0x38 assert board.read_port(0x03) == 0x25 def test_mega_drive_with_power_base_converter_ports(): # smstech sections 3 and 4: $DD bit 5 reads 0 on a Genesis, $3E does nothing # there, and with the converter the rest of $C0-$FF reads $FF. board = Board(MEGA_DRIVE_PBC, rom(1)) assert board.read_port(0xDD) == 0xDF assert board.read_port(0xDC) == 0xFF assert board.read_port(0xDE) == 0xFF board.write_port(0x3E, 0xFF) # every slot "disabled" assert board.read_byte(0x0000) == rom(1).read(0x0000) board.write_byte(0xC000, 0x5A) assert board.read_byte(0xC000) == 0x5A board.press_pause() # the converter's pause button assert board.nmi def test_sc3000_keyboard_rows_and_joysticks(): # sc3000h section 5: port C selects a row; A and B read it, active low. board = Board(SC3000, rom(1)) board.io.pad1.pressed.add(BUTTON_1) assert board.read_port(0xDC) == 0xEF # untouched 8255: row 7, the joysticks board.write_port(0xDF, 0x92) # A and B inputs, C output board.write_port(0xDE, 0x07) assert board.read_port(0xDC) == 0xEF board.keyboard.keys.update(keys_for("a")) # SHIFT and A board.write_port(0xDE, 0x00) assert board.read_port(0xDC) == 0xFB # row 0: A on PA2 assert board.read_port(0xDD) == 0x7F # nothing on PB0-PB3; bits 7-5 read 0,1,1 board.write_port(0xDE, 0x06) assert board.read_port(0xDD) == 0x77 # row 6: SHIFT on PB3 assert board.read_port(0xDE) == 0x06 # an output port reads back board.write_port(0xDF, 0x01) # bit set: PC0 assert board.keyboard.row == 7 def test_pal_sc3000_keeps_the_ntsc_crystal(): # sc3000h section 2: the TMS9929A takes 10.738635 MHz directly, the Z80 a third. assert SC3000_PAL.clock == SC3000.clock == 3_579_545 assert Board(SC3000_PAL, rom(1)).vdp.lines_per_frame == 313 assert round(SC3000_PAL.frame_rate, 2) == 50.16 # MAME 0.285 sc3000pal: 50.158969 assert round(SMS1_PAL.frame_rate, 2) == 49.70 assert round(SC3000.frame_rate, 2) == 59.92 def test_sc3000_port_decode_and_ram(): # sc3000h section 4: A7 low the PSG, A6 low the VDP, A5 low the 8255. board = Board(SC3000, rom(1)) board.write_byte(0xC000, 0x12) assert board.read_byte(0xC800) == 0x12 # 2 KB, mirrored board.write_port(0xBF, 0x34) board.write_port(0xBF, 0x87) # VDP register 7 through $A0-$BF assert board.vdp.registers[7] == 0x34 assert board.read_port(0xFF) == 0xFF # nothing answers $E0-$FF