def _cp_value(processor, value, carry): signed_a = to_signed(processor.main_registers['a']) if carry: value = (value + 1) & 0xff result, half_carry, full_carry = bitwise_sub(processor.main_registers['a'], value) signed_result = to_signed(result) set_condition = processor.set_condition set_condition('s', signed_result < 0) set_condition('z', result == 0) set_condition('h', half_carry) set_condition('p', (signed_a < 0) != (signed_result < 0)) set_condition('n', True) set_condition('c', full_carry)
def check_sbc_indexed_indirect(self, register, op_codes): # given self.given_register_contains_value('a', 0b00001000) self.processor.set_condition('c', True) self.processor.index_registers[register] = 0xbeef offset = random.randint(0, 255) signed_offset = to_signed(offset) self.memory[0xbeef + signed_offset] = 0b00000001 self.given_next_instruction_is(op_codes, offset) # when self.processor.execute() # then self.assert_register('a').equals(0b00000110) self.assert_flag('s').is_reset() self.assert_flag('z').is_reset() self.assert_flag('h').is_reset() self.assert_flag('p').is_reset() self.assert_flag('n').is_set() self.assert_flag('c').is_reset()
def execute(self, processor, memory, pc): operand, pc = fetch_byte(memory, pc) immediate_value, pc = fetch_byte(memory, pc) offset = to_signed(operand) self.memory[0xffff & (self.processor.index_registers[self.indexed_reg] + offset)] = immediate_value return 19, False, pc
def execute(self, processor, memory, pc): result, half_carry, _ = bitwise_sub(0, processor.main_registers['a']) set_condition = processor.set_condition set_condition('s', to_signed(result) < 0) set_condition('z', result == 0) set_condition('h', half_carry) set_condition('p', processor.main_registers['a'] == 0x80) set_condition('n', True) set_condition('c', processor.main_registers['a'] != 0x00) processor.main_registers['a'] = result return 8, False, pc
def check_ld_indexed_addr_immediate(self, op_codes, index_register): # given operand = random_byte() immediate_value = random_byte() self.processor.index_registers[index_register] = 0x1000 self.given_next_instruction_is(op_codes[0], op_codes[1], operand, immediate_value) # when self.processor.execute() # then assert_equals(self.memory[0xffff & (0x1000 + to_signed(operand))], immediate_value)
def check_set_indexed_indirect(self, reg, reg_op_code, op_code, bit_pos): # given offset = randint(0, 255) address = 0x1234 + to_signed(offset) self.processor.index_registers[reg] = 0x1234 self.memory[address] = 0x00 self.given_next_instruction_is(reg_op_code, 0xcb, offset, op_code) # when self.processor.execute() # then assert_true(self.memory[address] & pow(2, bit_pos) > 0)
def check_bit_indexed_indirect(self, reg, reg_op_code, op_code, bit_pos, bit_value): # given offset = randint(0, 255) self.processor.index_registers[reg] = 0x1234 self.memory[0x1234 + to_signed(offset)] = pow(2, bit_pos) if bit_value else 0 self.given_next_instruction_is(reg_op_code, 0xcb, offset, op_code) # when self.processor.execute() # then self.assert_flag('z').equals(not bit_value) self.assert_flag('h').is_set() self.assert_flag('n').is_reset()
def check_ld_reg_indexed_addr(self, op_codes, destination_register, index_register, operand): # given self.given_next_instruction_is(op_codes[0], op_codes[1], operand) self.given_register_contains_value(index_register, 0x1000) referenced_address = 0x1000 + to_signed(operand) address_value = random_byte() self.memory[referenced_address] = address_value # when self.processor.execute() # then self.assert_pc_address().equals(0x0003) assert_equals(self.processor.main_registers[destination_register], address_value)
def check_ld_indexed_reg_from_reg(self, op_codes, destination_index_register, source_register, operand): # given self.given_next_instruction_is(op_codes[0], op_codes[1], operand) register_value = random_byte() self.given_register_contains_value(source_register, register_value) self.given_register_contains_value(destination_index_register, 0x1000) # when self.processor.execute() # then self.assert_pc_address().equals(0x0003) referenced_address = 0x1000 + to_signed(operand) assert_equals(self.memory[referenced_address], register_value)
def check_dec_reg_with_negative_result(self, reg, op_code): # given self.given_register_contains_value(reg, 0b10000001) self.given_next_instruction_is(op_code) # when self.processor.execute() # then assert_equals(to_signed(self.processor.main_registers[reg]), -128) self.assert_flag('s').is_set() self.assert_flag('z').is_reset() self.assert_flag('h').is_reset() self.assert_flag('p').is_reset() self.assert_flag('n').is_set()
def check_dec_indexed_indirect(self, register, op_codes): # given self.processor.index_registers[register] = 0xbeef offset = random.randint(0, 255) signed_offset = to_signed(offset) self.memory[0xbeef + signed_offset] = 0b00111000 self.given_next_instruction_is(op_codes, offset) # when self.processor.execute() # then self.assert_memory(0xbeef + signed_offset).contains(0b00110111) self.assert_flag('s').is_reset() self.assert_flag('z').is_reset() self.assert_flag('h').is_reset() self.assert_flag('p').is_reset() self.assert_flag('n').is_set()
def check_sla_indexed_indirect(self, op_code, reg): # given offset = random.randint(0, 255) address = 0x4000 + to_signed(offset) self.processor.index_registers[reg] = 0x4000 self.processor.set_condition('c', False) self.memory[address] = 0b10101010 self.given_next_instruction_is(op_code, 0xcb, offset, 0x26) # when self.processor.execute() # then self.assert_memory(address).contains(0b01010100) self.assert_flag('s').is_reset() self.assert_flag('z').is_reset() self.assert_flag('h').is_reset() self.assert_flag('p').is_reset() self.assert_flag('n').is_reset() self.assert_flag('c').is_set()
def check_cp_indexed_indirect(self, register, op_codes): # given self.given_register_contains_value("a", 0b00001000) self.processor.index_registers[register] = 0xBEEF offset = random.randint(0, 255) signed_offset = to_signed(offset) self.memory[0xBEEF + signed_offset] = 0b00000001 self.given_next_instruction_is(op_codes, offset) # when self.processor.execute() # then self.assert_register("a").equals(0b00001000) self.assert_flag("s").is_reset() self.assert_flag("z").is_reset() self.assert_flag("h").is_reset() self.assert_flag("p").is_reset() self.assert_flag("n").is_set() self.assert_flag("c").is_reset()
def execute(self, processor, memory, pc): operand, pc = fetch_byte(memory, pc) offset = to_signed(operand) self.memory[0xffff & (self.processor.index_registers[self.indexed_reg] + offset)] = \ self.processor.main_registers[self.source_reg] return 19, False, pc
def fetch_signed_byte(memory, pc): return to_signed(memory[pc]), (pc + 1) & 0xffff