def test_program_counter_wraps(): cpu = Computer() program = [0xFE] * 256 cpu.switches.load_program(program) for _ in range(957): cpu.step(debug=False)
def test_opcode_ota(): pc = Computer() program = [ 0xF6, # 0x00 OTA ] pc.switches.load_program(program) pc.reg_a.value = 0xF8 pc.step(instructionwise=True) assert pc.reg_o.value == 0xF8
def test_opcode_lda_immediate(): pc = Computer() program = [ 0x20, 0x02, # 0x00 LDA 02H ] pc.switches.load_program(program) pc.step(instructionwise=True, debug=True) assert pc.reg_a.value == 0x02
def test_opcode_add_immediate(): pc = Computer() program = [ 0x21, 0x3, # 0x00 ADD 02H ] pc.switches.load_program(program) pc.reg_a.value = 0xCC pc.step(instructionwise=True) assert pc.reg_a.value == 0xCF
def test_opcode_sub(): pc = Computer() program = [ 0x02, 0x02, # 0x00 SUB 02H 0x22, # 0x02 22H ] pc.switches.load_program(program) pc.reg_a.value = 0xCC pc.step(instructionwise=True) assert pc.reg_a.value == 0xAA
def test_opcode_add_absolute(): pc = Computer() program = [ 0x01, 0x02, # 0x00 ADD 02H 0x22, # 0x02 22H ] pc.switches.load_program(program) pc.reg_a.value = 0xCC pc.step(instructionwise=True) assert pc.reg_a.value == 0xEE
def test_computer_halts(): pc = Computer() program = [ 0xFE, # 0x00 NOP 0xFE, # 0x01 NOP 0xFE, # 0x02 NOP 0xFF, # 0x03 HLT 0xFE, # 0x04 NOP 0xFE, # 0x05 NOP 0xFE, # 0x06 NOP ] pc.switches.load_program(program) counterprogress = [0, 1, 2, 3, 3, 3, 3] for counter_value in counterprogress: assert pc.pc.value == counter_value pc.step(instructionwise=True)
def test_opcode_lda_indirect(): pc = Computer() program = [ 0x10, 0x02, # 0x00 LDA ($02) 0x07, # 0x02 $07 0x22, # 0x03 $22 0x23, # 0x04 $23 0x24, # 0x05 $24 0x25, # 0x06 $25 0x26, # 0x07 $26 0x27, # 0x08 $27 ] pc.switches.load_program(program) program_counter = pc.pc.value pc.step(instructionwise=True, debug=True) assert pc.reg_a.value == 0x26 assert program_counter + 2 == pc.pc.value # two byte instruction
def test_opcode_sta_indirect(): pc = Computer() program = [ 0x20, 0x09, # 0x00 LDA $09 0x45, 0x04, # 0x02 STA ($04) 0x07, # 0x04 $22 0x23, # 0x05 $23 0x24, # 0x06 $24 0x25, # 0x07 $25 0x26, # 0x08 $26 0x27, # 0x09 $27 0x28, # 0x0A $28 0x29, # 0x0B $29 ] pc.switches.load_program(program) pc.step(instructionwise=True) assert pc.reg_a.value == 0x09 program_counter = pc.pc.value pc.step(instructionwise=True) assert program_counter + 2 == pc.pc.value # two byte instruction assert pc.mar.value == 0x07 assert pc.ram.data(con=['er']) == 0x09 # don't corrupt other memory pc.mar.clock(data=0x04, con=['lm']) assert pc.ram.data(con=['er']) == 0x07
def test_opcode_sta_absolute(): pc = Computer() program = [ 0x20, 0x09, # 0x00 LDA $09 0x35, 0x04, # 0x02 STA $04 0x07, # 0x04 $22 0x23, # 0x05 $23 0x24, # 0x06 $24 0x25, # 0x07 $25 0x26, # 0x08 $26 0x27, # 0x09 $27 0x28, # 0x0A $28 0x29, # 0x0B $29 ] pc.switches.load_program(program) pc.step(instructionwise=True) assert pc.reg_a.value == 0x09 program_counter = pc.pc.value pc.step(instructionwise=True) assert program_counter + 2 == pc.pc.value # two byte instruction assert pc.mar.value == 0x04 assert pc.ram.data(con=['er']) == 0x09
def test_opcode_program_sequence(): pc = Computer() program = [ 0x00, 0x06, # 0x00 LDA 06H 0x01, 0x07, # 0x02 ADD 07H 0xF6, # 0x04 OTA 0xFF, # 0x05 HLT 0xA1, # 0x06 A1H 0x22, # 0x07 22H ] pc.switches.load_program(program) pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) assert pc.reg_o.value == 0xC3
def test_opcode_jmp_immediate(): pc = Computer() program = [ 0x00, 0x07, # 0x00 LDA 07H 0x01, 0x08, # 0x20 ADD 08H 0xF6, # 0x04 OTA 0x34, 0x02, # 0x05 JMP $02H 0x00, # 0x07 A1H 0x03, # 0x08 22H ] pc.switches.load_program(program) pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) assert pc.reg_o.value == 0x03 pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) assert pc.reg_o.value == 0x06 pc.step(instructionwise=True) pc.step(instructionwise=True) pc.step(instructionwise=True) assert pc.reg_o.value == 0x09 pc.step(instructionwise=True)