def test_asl(self, accumulator_state, expected): test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state asl(test_cpu, cpu.AddressingMode.accumulator) assert test_cpu.accumulator == expected
def test_carry(accumulator_state, immediate, expected): """Test that carry bit is set when add operation overflows.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.status.carry == expected
def test_clear(self, status_flag, init_state): """Test that status flags are always set to false when clear instruction is called.""" test_cpu = cpu.Cpu() setattr(test_cpu.status, status_flag.name, init_state) clear._clear_flag(test_cpu, status_flag) assert not getattr(test_cpu.status, status_flag.name)
def test_negative_flag(self, accumulator_state, expected): """Test that negative bit is set if the result is negative.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state asl(test_cpu, cpu.AddressingMode.accumulator) assert test_cpu.status.negative == expected
def test_adding(accumulator_state, immediate, expected): """Test basic adding functionality between accumulator and immediate value.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.accumulator == expected assert not test_cpu.status.carry
def test_zero_flag(self, accumulator_state, expected): """Test that result is zero.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state asl(test_cpu, cpu.AddressingMode.accumulator) assert test_cpu.status.zero == expected
def test_negative_flag(accumulator_state, immediate, expected): """Test that negative bit is set if the result is negative.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.status.negative == expected
def test_absolute(): """Test that absolute addressing gets the value from memory, then performs same addition as immediate.""" test_cpu = cpu.Cpu() test_cpu.memory = bytearray(b'\x00\x00\x05\x00') with mock.patch.object(add, '_add_with_carry_immediate', wraps=add._add_with_carry_immediate) as mock_add: add.add_with_carry(test_cpu, cpu.AddressingMode.absolute, 2) mock_add.assert_called_with(test_cpu, 5), 'Memory is not accessed in the right place.'
def test_overflow(accumulator_state, immediate, expected): """Test that overflow bit is set when add operation overflows. This is only meaningful if the inputs were intended to be signed values.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.status.overflow == expected
def test_negative(self, accumulator_state, memory_value, expected): """Test that negative is set to 7th bit of memory value.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.memory = bytearray(10) test_cpu.memory[2] = memory_value bit.bit(test_cpu, 2) assert test_cpu.status.negative == expected
def test_overflow(self, accumulator_state, memory_value, expected): """Test that overflow is set to 6th bit of memory value.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.memory = bytearray(10) test_cpu.memory[2] = memory_value bit.bit(test_cpu, 2) assert test_cpu.status.overflow == expected
def test_zero_page(): """Test that zero page addressing is alias for absolute addressing.""" test_cpu = cpu.Cpu() test_cpu.memory = bytearray(b'\x00\x00\x05\x00') function = add._add_with_carry_absolute with mock.patch.object(add, function.__name__, wraps=function) as mock_add: add.add_with_carry(test_cpu, cpu.AddressingMode.zero_page, 2) assert mock_add.called, 'zero_page addressing mode should be identical to absolute mode'
def test_zero(self, accumulator_state, memory_value, expected): """Test that zero flag is only set if bitmasking is zero.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.memory = bytearray(10) test_cpu.memory[2] = memory_value bit.bit(test_cpu, 2) assert test_cpu.status.zero == expected
def test_zero_flag(accumulator_state, immediate): """Test that result is zero.""" assert ( accumulator_state + immediate ) % cpu.MAX_UNSIGNED_VALUE == 0, 'Test code assertion, test inputs must only for result == 0' test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.status.zero
def test_unaffected_flag(accumulator_state, immediate, flag_state): """Test that other flags are unchanged.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.status.interrupt_disable = flag_state test_cpu.status.decimal = flag_state test_cpu.status.break_ = flag_state add.add_with_carry(test_cpu, cpu.AddressingMode.immediate, immediate) assert test_cpu.status.interrupt_disable == flag_state assert test_cpu.status.decimal == flag_state assert test_cpu.status.break_ == flag_state
def test_unaffected_flag(self, accumulator_state, flag_state): """Test that other flags are unchanged.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.status.interrupt_disable = flag_state test_cpu.status.decimal = flag_state test_cpu.status.break_ = flag_state test_cpu.status.overflow = flag_state asl(test_cpu, cpu.AddressingMode.accumulator) assert test_cpu.status.interrupt_disable == flag_state assert test_cpu.status.decimal == flag_state assert test_cpu.status.break_ == flag_state assert test_cpu.status.overflow == flag_state
def test_unaffected_flag(self, status_flag, init_state, flag_state): """Test that other flags are unchanged.""" test_cpu = cpu.Cpu() setattr(test_cpu.status, status_flag.name, init_state) other_status = set(cpu.StatusFlag) - {status_flag} # Set state of target status flags for i in other_status: setattr(test_cpu.status, i.name, flag_state) clear._clear_flag(test_cpu, status_flag) # Assert state of target status flags unchanged for i in other_status: assert getattr(test_cpu.status, i.name) == flag_state
def test_unaffected_flag(self, accumulator_state, memory_value, flag_state): """Test that other flags are unchanged.""" test_cpu = cpu.Cpu() test_cpu.accumulator = accumulator_state test_cpu.memory = bytearray(10) test_cpu.memory[2] = memory_value test_cpu.status.carry = flag_state test_cpu.status.interrupt_disable = flag_state test_cpu.status.decimal = flag_state test_cpu.status.break_ = flag_state bit.bit(test_cpu, 2) assert test_cpu.status.carry == flag_state assert test_cpu.status.interrupt_disable == flag_state assert test_cpu.status.decimal == flag_state assert test_cpu.status.break_ == flag_state
def test_unaffected_flag(self, test_cpu, flag_state): """Test that other flags are unchanged.""" test_cpu = cpu.Cpu() test_cpu.program_counter = 100 test_cpu.status.carry = flag_state test_cpu.status.zero = flag_state test_cpu.status.interrupt_disable = flag_state test_cpu.status.decimal = flag_state test_cpu.status.break_ = flag_state test_cpu.status.overflow = flag_state test_cpu.status.negative = flag_state branch._branch(test_cpu, flag_state, 10) assert test_cpu.status.carry == flag_state assert test_cpu.status.zero == flag_state assert test_cpu.status.interrupt_disable == flag_state assert test_cpu.status.decimal == flag_state assert test_cpu.status.break_ == flag_state assert test_cpu.status.overflow == flag_state assert test_cpu.status.negative == flag_state
def test_cpu(): test_cpu = cpu.Cpu() test_cpu.program_counter = 100 test_cpu.status.carry = False yield test_cpu
def test_cpu(): test_cpu = cpu.Cpu() test_cpu.memory = bytearray(b'\x00\x00\x05\x00') yield test_cpu
def test_unsupported_flags(self, status_flag): """Test that these statuses do not have clear instruction support.""" test_cpu = cpu.Cpu() with pytest.raises(NotImplementedError): clear._clear_flag(test_cpu, status_flag)
def test_clear_carry(self, mock_clear): test_cpu = cpu.Cpu() clear.clear_carry(test_cpu) mock_clear.assert_called_with(test_cpu, cpu.StatusFlag.carry)
def test_clear_decimal(self, mock_clear): test_cpu = cpu.Cpu() clear.clear_decimal(test_cpu) mock_clear.assert_called_with(test_cpu, cpu.StatusFlag.decimal)
def test_clear_interrupt(self, mock_clear): test_cpu = cpu.Cpu() clear.clear_interrupt(test_cpu) mock_clear.assert_called_with(test_cpu, cpu.StatusFlag.interrupt_disable)
def test_clear_overflow(self, mock_clear): test_cpu = cpu.Cpu() clear.clear_overflow(test_cpu) mock_clear.assert_called_with(test_cpu, cpu.StatusFlag.overflow)
def test_not_placeholder(self): cpu_instance = cpu.Cpu() cpu_instance.decode_instruction('NOT_PLACEHOLDER') # type: ignore