def test_execute_iny_255_to_0(): opcode = OpCode() registers = Registers() registers.y_index = 0xff opcode.execute(0xC8, registers, None) assert registers.y_index == 0 assert registers.negative_flag == False assert registers.zero_flag
def test_execute_dey_128_to_127(): opcode = OpCode() registers = Registers() registers.y_index = 0x80 opcode.execute(0x88, registers, None) assert registers.y_index == 0x7f assert registers.negative_flag == False assert registers.zero_flag == False
def test_execute_dey_0_to_minus1(): opcode = OpCode() registers = Registers() registers.y_index = 0 opcode.execute(0x88, registers, None) assert registers.y_index == 255 assert registers.negative_flag assert registers.zero_flag == False
def test_execute_iny_127_to_128(): opcode = OpCode() registers = Registers() dummy_value = 0x7f registers.y_index = dummy_value opcode.execute(0xC8, registers, None) assert registers.y_index == dummy_value + 1 assert registers.negative_flag assert registers.zero_flag == False
def test_execute_nop(): opcode = OpCode() registers = Registers() with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0xEA, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers == Registers()
def test_execute_dey_1_to_0(): opcode = OpCode() registers = Registers() registers.y_index = 1 with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0x88, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers.y_index == 0 assert registers.negative_flag == False assert registers.zero_flag
def test_execute_jmp_absolute(): opcode = OpCode() registers = Registers() with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.side_effect = [0x00, 0xc0] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x4C, registers, mock_memory_controller) assert count == 3 # Tested more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 2 assert registers.pc == 0xc000
def test_execute_iny_0_to_1(): opcode = OpCode() registers = Registers() dummy_value = 0x0 registers.y_index = dummy_value with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0xC8, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers.y_index == dummy_value + 1 assert registers.negative_flag == False assert registers.zero_flag == False
def test_execute_tsx(): opcode = OpCode() registers = Registers() # leave sp with default old_sp = registers.sp with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0xBA, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers.x_index == old_sp assert registers.sp == old_sp assert registers.negative_flag # default sp should be $fd assert registers.zero_flag == False
def test_execute_tya(): opcode = OpCode() registers = Registers() dummy_value = 0x7c # (positive, not zero) registers.y_index = dummy_value with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0x98, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers.y_index == dummy_value assert registers.accumulator == registers.y_index assert registers.negative_flag == False assert registers.zero_flag == False
def execute_inc_positive(actual_opcode, expected_clocks, pc_value, mock_memory_controller, **kwargs): opcode = OpCode() registers = Registers() if kwargs: for arg in kwargs: setattr(registers, arg, kwargs[arg]) registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(actual_opcode, registers, mock_memory_controller) assert count == expected_clocks assert registers.pc == pc_value assert registers.zero_flag == False assert registers.negative_flag == False
def test_execute_txs(): opcode = OpCode() registers = Registers() dummy_value = 0x7c # (positive, not zero) registers.x_index = dummy_value with patch.object(MemoryController, 'read', return_value=None) as mock_memory_controller: count = opcode.execute(0x9A, registers, mock_memory_controller) assert count == 2 mock_memory_controller.assert_not_called() assert registers.x_index == dummy_value assert registers.sp == dummy_value assert registers.negative_flag == False # default sp should be $fd assert registers.zero_flag == False
def test_execute_bvs_branch_taken(): opcode = OpCode() registers = Registers() registers.overflow_flag = True registers.pc = 0xc000 with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.side_effect = [0xfe] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x70, registers, mock_memory_controller) assert count == 3 # Tested more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 1 assert registers.pc == 0xc000
def test_execute_bmi_branch_not_taken(): opcode = OpCode() registers = Registers() registers.negative_flag = False registers.pc = 0xc000 with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.side_effect = [0x02] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x30, registers, mock_memory_controller) assert count == 2 # Tested more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 1 assert registers.pc == 0xc002
def test_execute_sta_zeropage(): opcode = OpCode() registers = Registers() registers.accumulator = 3 mock_memory_controller = Mock() # we're mocking 0x85 0x21 mock_memory_controller.read.side_effect = [0x21] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x85, registers, mock_memory_controller) assert count == 3 assert mock_memory_controller.read.call_count == 1 assert mock_memory_controller.read.call_args_list[0] == unittest.mock.call( 1) mock_memory_controller.write.assert_called_with(0x21, 3) assert registers.pc == 2
def test_execute_ldx_immediate_zero(): opcode = OpCode() registers = Registers() registers.zero_flag = False registers.negative_flag = True with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.return_value = 0x0 registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xA2, registers, mock_memory_controller) assert count == 2 mock_memory_controller.read.assert_called_with(1) assert registers.x_index == 0x0 assert registers.pc == 2 assert registers.zero_flag assert registers.negative_flag == False
def test_execute_sty_zeropage(): opcode = OpCode() registers = Registers() registers.y_index = 5 mock_memory_controller = Mock() # we're mocking 0x84 0x30 mock_memory_controller.read.side_effect = [0x30] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x84, registers, mock_memory_controller) assert count == 3 assert mock_memory_controller.read.call_count == 1 assert mock_memory_controller.read.call_args_list[0] == unittest.mock.call( 1) mock_memory_controller.write.assert_called_with(0x30, 5) assert registers.pc == 2
def test_execute_lda_immediate_negative(): opcode = OpCode() registers = Registers() registers.zero_flag = True registers.negative_flag = False with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.return_value = -1 registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xA9, registers, mock_memory_controller) assert count == 2 mock_memory_controller.read.assert_called_with(1) assert registers.accumulator == -1 assert registers.pc == 2 assert registers.zero_flag == False assert registers.negative_flag
def execute_adc_carry_clear( actual_opcode, expected_clocks): opcode = OpCode() registers = Registers() registers.accumulator = 5 registers.zero_flag = True registers.negative_flag = True with patch.object(MemoryController, 'read') as mock_memory_controller: mock_memory_controller.read.return_value = 0x22 registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(actual_opcode, registers, mock_memory_controller) assert count == expected_clocks assert registers.accumulator == 0x27 assert registers.zero_flag == False assert registers.negative_flag == False assert registers.carry_flag == False
def test_execute_sbc_immediate_borrow_in_no_borrow_out_overflow_positive_result(): opcode = OpCode() registers = Registers() registers.accumulator = 0xd0 #start with negative acc this time with patch.object(MemoryController, 'read') as mock_memory_controller: # Mocking 0xE9 0x70 so subtracting 0xd0 - 0x70 (-48 - 112 = 96 (overflow)) mock_memory_controller.read.return_value = 0x70 registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xE9, registers, mock_memory_controller) assert count == 2 assert registers.accumulator == 0x5f assert registers.zero_flag == False assert registers.negative_flag == False assert registers.carry_flag assert registers.overflow_flag
def test_execute_sta_absolute_y(): opcode = OpCode() registers = Registers() registers.accumulator = 0x20 registers.y_index = 3 mock_memory_controller = Mock() # we're mocking 0x99 0x2100 so write is to [0x2103] mock_memory_controller.read.side_effect = [0, 0x21] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x99, registers, mock_memory_controller) assert count == 5 # these are checked more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 2 mock_memory_controller.write.assert_called_with(0x2103, 0x20) assert registers.pc == 3
def test_execute_sbc_immediate_no_borrow_in_borrow_out_overflow_negative_result(): opcode = OpCode() registers = Registers() registers.accumulator = 0x50 registers.carry_flag = True with patch.object(MemoryController, 'read') as mock_memory_controller: # Mocking 0xE9 0xb0 so subtracting 0x50 - 0xb0 (80 - -80) mock_memory_controller.read.return_value = 0xb0 registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xE9, registers, mock_memory_controller) assert count == 2 assert registers.accumulator == 0xa0 assert registers.zero_flag == False assert registers.negative_flag assert registers.carry_flag == False assert registers.overflow_flag
def execute_sbc_borrow_in_borrow_out_no_overflow_positive_result( actual_opcode, expected_clocks, mock_memory_controller, **kwargs): opcode = OpCode() registers = Registers() setattr(registers,'accumulator',0x50) if kwargs: for arg in kwargs: setattr(registers, arg, kwargs[arg]) # Mocking 0x150 - 0xf0 (borrow 'in') registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(actual_opcode, registers, mock_memory_controller) assert count == expected_clocks assert registers.accumulator == 0x5f assert registers.zero_flag == False assert registers.negative_flag == False assert registers.carry_flag == False assert registers.overflow_flag == False
def test_execute_sty_absolute(): opcode = OpCode() registers = Registers() registers.y_index = 0x20 mock_memory_controller = Mock() # we're mocking 0x8C 0x21 so store to [0x0024] mock_memory_controller.read.side_effect = [0, 0x20] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x8C, registers, mock_memory_controller) assert count == 4 # these are checked more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 2 assert mock_memory_controller.read.call_args_list[0] == unittest.mock.call( 1) mock_memory_controller.write.assert_called_with(0x2000, 0x20) assert registers.pc == 3
def test_execute_adc_absolute(): opcode = OpCode() registers = Registers() registers.accumulator = 5 registers.zero_flag = True registers.negative_flag = True with patch.object(MemoryController, 'read') as mock_memory_controller: # we're mocking 0xb5 0x21 0x22 and value at [0x2221] = 1 mock_memory_controller.read.side_effect = [0x21, 0x22, 1] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x6D, registers, mock_memory_controller) assert count == 4 assert registers.accumulator == 6 assert registers.zero_flag == False assert registers.negative_flag == False assert registers.carry_flag == False
def test_execute_inc_zeropage_zero(): opcode = OpCode() registers = Registers() mock_memory_controller = Mock() mock_memory_controller.read.side_effect = [0x21, 0xff] # we're mocking 0xE6 0x21 and [0x21] = 0xff registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xE6, registers, mock_memory_controller) assert count == 5 assert mock_memory_controller.read.call_count == 2 assert mock_memory_controller.read.call_args_list[0] == unittest.mock.call( 1) assert mock_memory_controller.read.call_args_list[1] == unittest.mock.call( 0x21) mock_memory_controller.write.assert_called_with(0x21, 0) assert registers.pc == 2 assert registers.zero_flag assert registers.negative_flag == False
def test_execute_brk(): opcode = OpCode() registers = Registers() registers.sp = 0x200 mock_memory_controller = Mock() mock_memory_controller.read.side_effect = [0x00, 0x21] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x0, registers, mock_memory_controller) assert count == 7 assert mock_memory_controller.read.call_count == 2 assert mock_memory_controller.write.call_count == 3 assert mock_memory_controller.write.call_args_list[0], unittest.mock.call( 0x200 == 1) assert mock_memory_controller.write.call_args_list[1], unittest.mock.call( 0x1ff == 0) assert mock_memory_controller.write.call_args_list[2], unittest.mock.call( 0x1fe == registers.status_register()) assert registers.pc == 0x2100
def test_execute_ldy_absolute(): opcode = OpCode() registers = Registers() registers.zero_flag = True registers.negative_flag = False with patch.object(MemoryController, 'read') as mock_memory_controller: # we're mocking 0xa5 0x21 and value at [0x0021] = 1 mock_memory_controller.read.side_effect = [0x21, 0x22, 0xf0] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0xAC, registers, mock_memory_controller) assert count == 4 # these are checked more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 3 assert registers.pc == 3 assert registers.x_index == 0 assert registers.zero_flag == False assert registers.negative_flag
def test_execute_adc_absolute_y_page_boundary(): opcode = OpCode() registers = Registers() registers.accumulator = 5 registers.y_index = 3 registers.zero_flag = True registers.negative_flag = True with patch.object(MemoryController, 'read') as mock_memory_controller: # we're mocking 0xBD 0x2100 and value at [0x2202] = 1 mock_memory_controller.read.side_effect = [0xff, 0x21, 1] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x79, registers, mock_memory_controller) assert count == 5 assert registers.accumulator == 6 assert registers.zero_flag == False assert registers.negative_flag == False assert registers.carry_flag == False
def test_execute_sta_zeropage_x_wrap(): opcode = OpCode() registers = Registers() registers.accumulator = 0x20 registers.x_index = 3 mock_memory_controller = Mock() # we're mocking 0x95 0x21 so store to [0x0024] mock_memory_controller.read.side_effect = [0xfe] registers.pc += 1 #need to fake the cpu reading the opcode count = opcode.execute(0x95, registers, mock_memory_controller) assert count == 4 # these are checked more thoroughly in addressing_modes_tests assert mock_memory_controller.read.call_count == 1 assert mock_memory_controller.read.call_args_list[0] == unittest.mock.call( 1) mock_memory_controller.write.assert_called_with(0x01, 0x20) assert registers.pc == 2