def test_MUL_resulting_in_signed_negative_overflow(vm): vm.registers[2] = to_u16(-400) vm.registers[3] = to_u16(-200) helper(vm, "MUL(R1, R2, R3)") assert vm.registers[1] == 14464 assert vm.flag_carry assert vm.flag_overflow
def test_ADD_resulting_in_negative(vm): vm.registers[2] = to_u16(-14) vm.registers[3] = 8 helper(vm, "ADD(R1, R2, R3)") assert vm.registers[1] == to_u16(-6) assert vm.flag_sign assert not vm.flag_zero
def test_AND_does_not_set_other_flags(vm): vm.registers[2] = to_u16(-1) vm.registers[3] = to_u16(-1) helper(vm, "AND(R1, R2, R3)") assert vm.registers[1] == to_u16(-1) assert not vm.flag_carry assert not vm.flag_overflow
def test_XOR_does_not_set_other_flags(vm): vm.registers[2] = 0 vm.registers[3] = to_u16(-37) helper(vm, "XOR(R1, R2, R3)") assert vm.registers[1] == to_u16(-37) assert not vm.flag_carry assert not vm.flag_overflow
def test_ADD_resulting_in_negative_overflow(vm): vm.registers[2] = to_u16(-32768) vm.registers[3] = to_u16(-32768) helper(vm, "ADD(R1, R2, R3)") assert vm.registers[1] == 0 assert vm.flag_overflow assert vm.flag_carry
def test_SUB_with_no_carry_block_and_no_borrow(vm): vm.flag_carry = True vm.registers[2] = to_u16(-64) vm.registers[3] = 22 helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == to_u16(-86) assert vm.flag_carry
def test_LOAD_sets_sign_flag(vm): vm.memory[4] = to_u16(-2) vm.registers[2] = 4 helper(vm, "LOAD(R1, 0, R2)") assert vm.registers[1] == to_u16(-2) assert not vm.flag_zero assert vm.flag_sign
def test_SUB_resulting_in_maximum_positive_overflow(vm): vm.flag_carry_block = True vm.registers[2] = 32767 vm.registers[3] = to_u16(-32768) helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == to_u16(-1) assert not vm.flag_carry assert vm.flag_overflow
def test_SUB_with_two_negatives(vm): vm.flag_carry_block = True vm.registers[2] = to_u16(-20) vm.registers[3] = to_u16(-40) helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == 20 assert vm.flag_carry assert not vm.flag_overflow
def test_SUB_resulting_in_big_positive_overflow(vm): vm.flag_carry_block = True vm.registers[2] = 27500 vm.registers[3] = to_u16(-7040) helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == to_u16(-30996) assert not vm.flag_carry assert vm.flag_overflow
def test_SUB_resulting_in_zero(vm): vm.flag_carry_block = True vm.registers[2] = to_u16(-37) vm.registers[3] = to_u16(-37) helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == 0 assert not vm.flag_sign assert vm.flag_zero
def test_SUB_resulting_in_negative(vm): vm.flag_carry_block = True vm.registers[2] = to_u16(-64) vm.registers[3] = 22 helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == to_u16(-86) assert vm.flag_sign assert not vm.flag_zero
def test_OR_does_not_clear_other_flags(vm): vm.flag_carry = True vm.flag_overflow = True vm.registers[2] = to_u16(-1) vm.registers[3] = to_u16(-1) helper(vm, "OR(R1, R2, R3)") assert vm.flag_carry assert vm.flag_overflow
def test_LSL_shifts_out_carry_when_blocked(vm): vm.flag_carry_block = True vm.registers[2] = to_u16(-20000) helper(vm, "LSL(R1, R2)") assert vm.flag_carry
def test_DEC_ignores_incoming_carry(vm): vm.flag_carry = True helper(vm, "DEC(R8, 5)") assert vm.registers[8] == to_u16(-5) assert not vm.flag_carry
def test_ASL_shifts_out_carry_when_blocked(vm): vm.registers[2] = to_u16(-20000) helper(vm, "ASL(R1, R2)") assert vm.flag_carry assert vm.flag_overflow
def test_INC_sets_carry_flag(vm): vm.registers[8] = to_u16(-1) helper(vm, "INC(R8, 1)") assert vm.flag_carry assert not vm.flag_overflow
def test_arrays(): # Figure 6.2, p. 34 program = """\ DLABEL(ArrayOfSevenPrimes) INTEGER(7) INTEGER(2) INTEGER(3) INTEGER(5) INTEGER(7) DSKIP(1) INTEGER(13) INTEGER(17) DLABEL(ArrayOfSevenSquaredPrimes) DSKIP(8) CBON() SET(Rt, ArrayOfSevenPrimes) SETLO(R1, 5) ADD(Rt, Rt, R1) SETLO(R1, 11) STORE(R1, 0, Rt) SET(R1, ArrayOfSevenPrimes) SET(R2, ArrayOfSevenSquaredPrimes) LOAD(R3, 0, R1) STORE(R3, 0, R2) LABEL(SquareNextOne) DEC(R3, 1) BL(NoMoreSquares) INC(R1, 1) INC(R2, 1) LOAD(R4, 0, R1) MUL(R4, R4, R4) STORE(R4, 0, R2) BR(SquareNextOne) LABEL(NoMoreSquares) HALT() """ vm = execute_program_helper(program) assert vm.registers[1] == DEFAULT_DATA_START + 7 assert vm.registers[2] == DEFAULT_DATA_START + 15 assert vm.registers[3] == to_u16(-1) assert vm.registers[4] == 289 assert vm.memory[DEFAULT_DATA_START] == 7 assert vm.memory[DEFAULT_DATA_START + 1] == 2 assert vm.memory[DEFAULT_DATA_START + 2] == 3 assert vm.memory[DEFAULT_DATA_START + 3] == 5 assert vm.memory[DEFAULT_DATA_START + 4] == 7 assert vm.memory[DEFAULT_DATA_START + 5] == 11 assert vm.memory[DEFAULT_DATA_START + 6] == 13 assert vm.memory[DEFAULT_DATA_START + 7] == 17 assert vm.memory[DEFAULT_DATA_START + 8] == 7 assert vm.memory[DEFAULT_DATA_START + 9] == 4 assert vm.memory[DEFAULT_DATA_START + 10] == 9 assert vm.memory[DEFAULT_DATA_START + 11] == 25 assert vm.memory[DEFAULT_DATA_START + 12] == 49 assert vm.memory[DEFAULT_DATA_START + 13] == 121 assert vm.memory[DEFAULT_DATA_START + 14] == 169 assert vm.memory[DEFAULT_DATA_START + 15] == 289
def test_INC_sets_sign_flag(vm): vm.registers[1] = 32765 helper(vm, "INC(R1, 5)") assert vm.registers[1] == to_u16(-32766) assert not vm.flag_zero assert vm.flag_sign
def test_INC_sets_zero_flag(vm): vm.registers[7] = to_u16(-1) helper(vm, "INC(R7, 1)") assert vm.registers[7] == 0 assert vm.flag_zero assert not vm.flag_sign
def test_DEC_sets_overflow_flag(vm): vm.registers[8] = to_u16(-32768) helper(vm, "DEC(R8, 5)") assert vm.registers[8] == 32763 assert vm.flag_carry assert vm.flag_overflow
def test_DEC_sets_sign_flag(vm): vm.registers[1] = 1 helper(vm, "DEC(R1, 5)") assert vm.registers[1] == to_u16(-4) assert not vm.flag_zero assert vm.flag_sign
def test_MUL_with_sign_flag_and_negative_result(vm): vm.flag_sign = True vm.registers[2] = 20000 vm.registers[3] = to_u16(-200) helper(vm, "MUL(R1, R2, R3)") assert vm.registers[1] == 0b1111111111000010
def test_LSL_with_negative_overflow(vm): vm.registers[2] = to_u16(-20000) helper(vm, "LSL(R1, R2)") assert vm.registers[1] == 25536 assert vm.flag_carry assert not vm.flag_overflow
def test_SUB_does_not_set_carry_for_equal_operands_with_no_incoming_carry(vm): vm.registers[2] = 12 vm.registers[3] = 12 helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == to_u16(-1) assert not vm.flag_carry
def test_LSR_shifts_in_carry(vm): vm.flag_carry = True vm.registers[2] = 6 helper(vm, "LSR(R1, R2)") assert vm.registers[1] == to_u16(-32765) assert not vm.flag_carry
def test_ADD_resulting_in_big_overflow(vm): vm.registers[2] = 32767 vm.registers[3] = 32767 helper(vm, "ADD(R1, R2, R3)") assert vm.registers[1] == to_u16(-2) assert vm.flag_overflow assert not vm.flag_carry
def test_ADD_resulting_in_zero(vm): vm.registers[2] = to_u16(-4) vm.registers[3] = 4 helper(vm, "ADD(R1, R2, R3)") assert vm.registers[1] == 0 assert not vm.flag_sign assert vm.flag_zero
def test_MUL_resulting_in_signed_positive_overflow(vm): vm.registers[2] = 17000 vm.registers[3] = 3 helper(vm, "MUL(R1, R2, R3)") assert vm.registers[1] == to_u16(-14536) assert not vm.flag_carry assert vm.flag_overflow
def test_SUB_overflow_from_borrow(vm): vm.registers[2] = to_u16(-32767) vm.registers[3] = 1 helper(vm, "SUB(R1, R2, R3)") assert vm.registers[1] == 32767 assert vm.flag_carry assert vm.flag_overflow