def test_add16(): assert run(Add16, a=0, b=0).out == 0 assert run(Add16, a=0, b=-1).out == -1 assert run(Add16, a=-1, b=-1).out == -2 # Note: values get sign extended for convenience, but here we strip # that off for easy hex comparison in these odd cases assert unsigned(run(Add16, a=0xAAAA, b=0x5555).out) == 0xFFFF assert unsigned(run(Add16, a=0x3CC3, b=0x0FF0).out) == 0x4CB3 assert unsigned(run(Add16, a=0x1234, b=0x9876).out) == 0xAAAA
def test_assemble_sp_ops(): assert unsigned(parse_op("D=--SP")) == 0b100_1_110000_010_000 assert unsigned(parse_op("A=--SP")) == 0b100_1_110000_100_000 assert unsigned(parse_op("AD=--SP")) == 0b100_1_110000_110_000 with pytest.raises(SyntaxError) as exc_info: parse_op("M=--SP") assert str(exc_info.value).startswith("M not allowed as a destination for pop") assert unsigned(parse_op("SP++=0")) == 0b100_0_101010_000_000 assert unsigned(parse_op("SP++=D+1")) == 0b100_0_011111_000_000 with pytest.raises(Exception) as exc_info: parse_op("SP++=M") assert str(exc_info.value).startswith("unrecognized alu op")
def test_mux4way16(): for i in range(4): assert run(Mux4Way16, a=0, b=0, c=0, d=0, sel=i).out == 0 assert unsigned( run(Mux4Way16, a=11111, b=22222, c=33333, d=44444, sel=0b00).out) == 11111 assert unsigned( run(Mux4Way16, a=11111, b=22222, c=33333, d=44444, sel=0b01).out) == 22222 assert unsigned( run(Mux4Way16, a=11111, b=22222, c=33333, d=44444, sel=0b10).out) == 33333 assert unsigned( run(Mux4Way16, a=11111, b=22222, c=33333, d=44444, sel=0b11).out) == 44444
def test_assemble_shift_ops(): assert unsigned(parse_op("D=D>>1")) == solved_06.parse_op("D=D") & ~0b001_0_000000_000_000 assert unsigned(parse_op("A=D+M>>1")) == solved_06.parse_op("A=D+M") & ~0b001_0_000000_000_000
def test_alu_nostat(): alu = run(ALU) alu.x = 0 alu.y = -1 alu.zx = 1 alu.nx = 0 alu.zy = 1 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == 0 # 0 alu.zx = 1 alu.nx = 1 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 1 # 1 alu.zx = 1 alu.nx = 1 alu.zy = 1 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == -1 # -1 alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 0 alu.no = 0 assert alu.out == 0 # X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 0 assert alu.out == -1 # Y alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 0 alu.no = 1 assert alu.out == -1 # !X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 1 assert alu.out == 0 # !Y alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 0 # -X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 1 assert alu.out == 1 # -Y alu.zx = 0 alu.nx = 1 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 1 # X + 1 alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 0 # Y + 1 alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 0 assert alu.out == -1 # X - 1 alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == -2 # Y - 1 alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == -1 # X + Y alu.zx = 0 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 1 assert alu.out == 1 # X - Y alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == -1 # Y - X alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 0 assert alu.out == 0 # X & Y alu.zx = 0 alu.nx = 1 alu.zy = 0 alu.ny = 1 alu.f = 0 alu.no = 1 assert alu.out == -1 # X | Y alu.x = 23456 alu.y = 7890 alu.zx = 1 alu.nx = 0 alu.zy = 1 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == 0 # 0 alu.zx = 1 alu.nx = 1 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 1 # 1 alu.zx = 1 alu.nx = 1 alu.zy = 1 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == -1 # -1 alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 0 alu.no = 0 assert alu.out == 23456 # X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 0 assert alu.out == 7890 # Y alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 0 alu.no = 1 assert unsigned(alu.out) == 0xA45F # !X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 1 assert unsigned(alu.out) == 0xE12D # !Y alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == -23456 # -X alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 1 assert alu.out == -7890 # -Y alu.zx = 0 alu.nx = 1 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 23457 # X + 1 alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == 7891 # Y + 1 alu.zx = 0 alu.nx = 0 alu.zy = 1 alu.ny = 1 alu.f = 1 alu.no = 0 assert alu.out == 23455 # X - 1 alu.zx = 1 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == 7889 # Y - 1 alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 0 assert alu.out == 31346 # X + Y alu.zx = 0 alu.nx = 1 alu.zy = 0 alu.ny = 0 alu.f = 1 alu.no = 1 assert alu.out == 15566 # X - Y alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 1 alu.f = 1 alu.no = 1 assert alu.out == -15566 # Y - X alu.zx = 0 alu.nx = 0 alu.zy = 0 alu.ny = 0 alu.f = 0 alu.no = 0 assert unsigned(alu.out) == 0x1A80 # X & Y alu.zx = 0 alu.nx = 1 alu.zy = 0 alu.ny = 1 alu.f = 0 alu.no = 1 assert unsigned(alu.out) == 0x5FF2 # X | Y
computer.reset_program() for _ in range(210): computer.ticktock() assert computer.peek(2) == 42 def test_fill(Computer=solved_05.Computer, assemble=solved_06.assemble): # We're going to run a few million cycles, so the faster simulator is a better option: computer = run(Computer, simulator='codegen') pgm, _, _ = assemble(FILL_ASM) computer.init_rom(pgm) computer.set_keydown(0) # the keyboard is untouched for _ in range(1_000_000): computer.ticktock() for i in range(256 * 512 // 16): assert unsigned(computer.peek_screen(i)) == 0x0000 # white computer.set_keydown(1) # a keyboard key is pressed for _ in range(1_000_000): computer.ticktock() for i in range(256 * 512 // 16): assert unsigned(computer.peek_screen(i)) == 0xffff # black computer.set_keydown(0) # the keyboard is untouched for _ in range(1_000_000): computer.ticktock() for i in range(256 * 512 // 16): assert unsigned(computer.peek_screen(i)) == 0x0000 # white
def test_assemble_return(): assert unsigned(parse_op("RTN")) == 0b1000_0000_0000_0000
def test_assemble_call(): symbols = {"target": 12345} assert unsigned(parse_op("CALL target", symbols)) == 0b1000_0000_0000_0000 | 12345
def test_mux8way16(): for i in range(4): assert run(Mux8Way16, a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, sel=i).out == 0 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b000).out) == 11111 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b001).out) == 22222 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b010).out) == 33333 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b011).out) == 44444 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b100).out) == 55555 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b101).out) == 12345 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b110).out) == 23456 assert unsigned( run(Mux8Way16, a=11111, b=22222, c=33333, d=44444, e=55555, f=12345, g=23456, h=34567, sel=0b111).out) == 34567