class SoundIOTestCase(unittest.TestCase): def setUp(self): self.sound = Sound() self.memory = DMGMemory(sound=self.sound) self.sound.memory = self.memory self.cpu = CPU(self.memory) def test_set_sound_length_and_duty(self): self.memory.write(0xFF11, 0b10101010) self.assertEqual(self.sound.voices[0].wave_duty, 0b10) self.assertEqual(self.sound.voices[0].sound_length, 0b101010) def test_read_sound_length_and_duty(self): self.sound.voices[0].sound_length = 0b101010 self.sound.voices[0].wave_duty = 0b10 self.assertEqual(self.memory.read(0xFF11), 0b10000000) def test_set_envelope_and_volume(self): self.memory.write(0xFF12, 0b01010001) self.assertEqual(self.sound.voices[0].initial_envelope_volume, 0b0101) self.assertEqual(self.sound.voices[0].envelope_up, False) self.assertEqual(self.sound.voices[0].number_envelope_sweep, 0b001) def test_read_envelope_and_volume(self): self.sound.voices[0].initial_envelope_volume = 0b1010 self.sound.voices[0].envelope_up = True self.sound.voices[0].number_envelope_sweep = 0b110 self.assertEqual(self.memory.read(0xFF12), 0b10101110) def test_set_frequency_low_byte(self): self.memory.write(0xFF13, 0b01010001) self.assertEqual(self.sound.voices[0].frequency, 0b01010001) def test_read_frequency_low_byte(self): self.assertRaises(NotImplementedError, self.memory.read, 0xFF13) def test_set_frequency_high_byte(self): self.memory.write(0xFF14, 0b11010001) self.assertTrue(self.sound.voices[0].initial_bit) self.assertTrue(self.sound.voices[0].counter_enabled) self.assertEqual(self.sound.voices[0].frequency, 1 << 8) def test_read_frequency_high_byte(self): self.assertFalse(self.memory.read(0xFF14))
class DefaultCPUStackTestCase(unittest.TestCase): def setUp(self): self.memory = DMGMemory() self.memory.boot_rom.data = [0] * 2**16 self.cpu = CPU(self.memory) self.cpu.register_stack_pointer.set(0xFFFE) def testPushPopStack(self): self.cpu.stack_push_byte(0x12) self.assertEqual(self.cpu.register_stack_pointer.get(), 0xFFFD) self.assertEqual(self.memory.read(0xFFFD), 0x12) self.cpu.stack_push_byte(0x24) self.assertEqual(self.cpu.register_stack_pointer.get(), 0xFFFC) self.assertEqual(self.memory.read(0xFFFC), 0x24) popped = self.cpu.stack_pop_byte() self.assertEqual(self.cpu.register_stack_pointer.get(), 0xFFFD) self.assertEqual(popped, 0x24) popped = self.cpu.stack_pop_byte() self.assertEqual(self.cpu.register_stack_pointer.get(), 0xFFFE) self.assertEqual(popped, 0x12)
class LoadInstructionTestCase(unittest.TestCase): def setUp(self): self.memory = DMGMemory() self.memory.boot_rom.is_rom = False self.cpu = CPU(self.memory) def test_LD_A_E(self): self.memory.boot_rom.data = [0x7B] self.cpu.register_e.set(0x12) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 4) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_a.get(), 0x12) def test_LD_C_A(self): self.memory.boot_rom.data = [0x4F] self.cpu.register_a.set(0x12) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 4) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_c.get(), 0x12) def test_LD_BC_ADDR_A(self): self.memory.boot_rom.data = [0x02] self.cpu.register_a.set(0x12) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.memory.read(0x0000), 0x12) def test_LD_A_ADD_BC(self): self.memory.boot_rom.data = [0x0A] self.cpu.register_de.set(0x00) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_a.get(), 0x0A) def test_LD_A_IMM_ADDR(self): self.memory.boot_rom.data = [0xFA, 0x01, 0x00] self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 16) self.assertEqual(self.cpu.register_program_counter.get(), 3) self.assertEqual(self.cpu.register_a.get(), 0x01) def test_LD_IMM_ADDR_A(self): self.memory.boot_rom.data = [0xEA, 0x02, 0x00] self.cpu.register_a.set(0x12) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 16) self.assertEqual(self.cpu.register_program_counter.get(), 3) self.assertEqual(self.memory.read(0x0002), 0x12) def test_LD__C__A(self): # We need big memory to make the write self.memory.boot_rom.data = [0xE2] self.cpu.register_a.set(0x12) self.cpu.register_c.set(0x84) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.memory.read(0xFF84), 0x12) def test_LD_A__C(self): # LD A,(C) # We need big memory to make the read self.memory.boot_rom.data = [0xF2] self.memory.write(0xFF84, 0x33) self.cpu.register_c.set(0x84) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_a.get(), 0x33) def test_LD_C_D8(self): self.memory.boot_rom.data = [0x0E, 0xFE] self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 2) self.assertEqual(self.cpu.register_c.get(), 0xFE) def test_LDH_N_A(self): self.memory.boot_rom.data[0x0000] = 0xE0 self.memory.boot_rom.data[0x0001] = 0x92 self.cpu.register_a.set(0x24) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 12) self.assertEqual(self.cpu.register_program_counter.get(), 2) self.assertEqual(self.memory.read(0xFF92), 0x24) def test_LD_HL_D16(self): self.memory.boot_rom.data = [0x21, 0x34, 0x12] self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 12) self.assertEqual(self.cpu.register_program_counter.get(), 3) self.assertEqual(self.cpu.register_hl.get(), 0x1234) def test_LD_SP_D16(self): self.memory.boot_rom.data = [0x31, 0x34, 0x12] self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 12) self.assertEqual(self.cpu.register_program_counter.get(), 3) self.assertEqual(self.cpu.register_stack_pointer.get(), 0x1234) def test_LDD_HL_A(self): self.memory.boot_rom.data = [0x32, 0x00] self.cpu.register_a.set(0x12) self.cpu.register_hl.set(0x1) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_hl.get(), 0) self.assertEqual(self.cpu.memory.read(0x1), 0x12) def test_LDD_A_HL(self): self.memory.boot_rom.data = [0x3A, 0x12] self.cpu.register_hl.set(0x1) self.cpu.step() self.assertEqual(self.cpu.total_clock_cycles_ran, 8) self.assertEqual(self.cpu.register_program_counter.get(), 1) self.assertEqual(self.cpu.register_hl.get(), 0) self.assertEqual(self.cpu.register_a.get(), 0x12)