Пример #1
0
def test_registers_differ():
    expected = StateChecker(rf0=0, rf1=float2bits(5.2), rfLR=0, rfDEBUGSTATUS=1)
    got = new_state(rf0=1)
    with pytest.raises(ValueError):
        expected.check(got)
    got = new_state(rf1=float2bits(5.200000001), rfDEBUGSTATUS=1)
    expected.fp_check(got)
    got = new_state(rf1=float2bits(5.3), rfLR=0, rfDEBUGSTATUS=1)
    with pytest.raises(ValueError):
        expected.fp_check(got)
Пример #2
0
def test_flags_differ():
    expected = StateChecker(AZ=0, rfLR=0)
    got = new_state(AZ=1, rfLR=0)
    with pytest.raises(ValueError):
        expected.check(got)
    with pytest.raises(ValueError):
        expected.fp_check(got)
Пример #3
0
def test_execute_arith32_immediate(opcode, imm, expected):
    state = new_state(rf0=5)
    instr = opcode(rd=1, rn=0, imm=imm)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(pc=(4 + RESET_ADDR), **expected)
    expected_state.check(state)
Пример #4
0
def test_execute_add16sub16(factory, expected):
    state = new_state(rf0=2, rf1=5)
    instr = factory(rd=2, rn=1, rm=0)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(pc=(2 + RESET_ADDR), **expected)
    expected_state.check(state)
Пример #5
0
def test_execute_gid16():
    state = new_state(rfSTATUS=0)
    instr = opcode_factory.gid16()
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rfSTATUS=0b10)
    expected_state.check(state)
Пример #6
0
def test_execute_nop16():
    state = new_state()
    instr = opcode_factory.nop16()
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(pc=(2 + RESET_ADDR))
    expected_state.check(state)
Пример #7
0
def test_execute_rti16():
    state = new_state(rfIRET=224, rfSTATUS=0b10, pc=0)
    instr = opcode_factory.rti16()
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(pc=224, rfIRET=224, rfSTATUS=0b00)
    expected_state.check(state)
Пример #8
0
def test_check_memory():
    expected = StateChecker()
    got = new_state()
    got.mem.write(4, 4, 0xFFFF)
    got.mem.write(0, 4, 0x0)
    expected.check(got, memory=[(0x4, 4, 0xFFFF)])
    with pytest.raises(ValueError):
        expected.check(got, memory=[(0x0, 4, 0xFFFF)])
Пример #9
0
def test_execute_farith_unary32(factory, rn, ex):
    state = new_state(rf1=float2bits(7.2), rf2=float2bits(0.0),
                      rf3=float2bits(-1.5), rf4=float2bits(float('nan')),
                      rf5=0, rf6=-1, rf7=1)
    instr = factory(rd=0, rn=rn)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    ex.fp_check(state)
Пример #10
0
def test_execute_farith_fix(is16bit, rn, ex):
    state = new_state(rf1=float2bits(7.2), rf2=float2bits(0.0),
                      rf3=float2bits(-1.5), rf4=float2bits(float('nan')),
                      rf5=0, rf6=-1, rf7=1)
    instr = opcode_factory.fix16(rd=0, rn=rn) if is16bit else opcode_factory.fix32(rd=0, rn=rn)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    ex.check(state)
Пример #11
0
def test_execute_bkpt16():
    state = new_state(rfDEBUGSTATUS=0)
    instr = opcode_factory.bkpt16()
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rfDEBUGSTATUS=1)
    expected_state.check(state)
    assert not state.running
Пример #12
0
def test_execute_bitwise16(factory, expected):
    state = new_state(rf0=5, rf1=7)
    instr = factory(rd=2, rn=1, rm=0)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(AZ=0, AV=0, AC=0, pc=(2 + RESET_ADDR),
                                  rf2=expected)
    expected_state.check(state)
Пример #13
0
def test_execute_bcond(is16bit, cond, imm, offset):
    state = new_state(AZ=1, pc=90)
    factory = opcode_factory.bcond16 if is16bit else opcode_factory.bcond32
    instr = factory(condition=cond, imm=imm)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(pc=(90 + offset), AZ=1)
    expected_state.check(state)
Пример #14
0
def test_infinite_loop(is16bit):
    state = new_state(AZ=1, pc=0)
    factory = opcode_factory.bcond16 if is16bit else opcode_factory.bcond32
    # Branch unconditionally to instruction at pc=0.
    instr = factory(condition=0, imm=0)
    name, executefn = decode(instr)
    with pytest.raises(RuntimeError):
        executefn(state, Instruction(instr, None))
Пример #15
0
def test_execute_str_disp_pm(sub, new_rn):
    # Store.
    state = new_state(rf0=0xFFFFFFFF, rf5=8)
    # bb: 00=byte, 01=half-word, 10=word, 11=double-word
    instr = opcode_factory.ldstrpmd32(rd=0, rn=5, sub=sub, imm=1, bb=0b10, s=1)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rf0=0xFFFFFFFF, rf5=new_rn)
    expected_state.check(state, memory=[(8, 4, 0xFFFFFFFF)])
Пример #16
0
def test_testset32_nonzero():
    state = new_state(rf0=0, rf1=0x80002, rf2=0x80002)
    size = 0b10  # Word
    state.mem.write(0x00100004, 4, 0xFFFF)
    instr = opcode_factory.testset32(rd=0, rn=1, rm=2, sub=0, bb=size)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rf0=0xFFFF, rf1=0x80002, rf2=0x80002,)
    expected_state.check(state, memory=[(0x00100004, 4, 0xFFFF)])
Пример #17
0
def test_execute_shift_left(factory, is16bit):
    state = new_state(rf0=5, rf1=7)
    instr = factory(rd=2, rn=1, rm=0)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(AZ=0, AN=0, AC=0, AV=0,
                                  pc=((2 if is16bit else 4) + RESET_ADDR),
                                  rf2=7 << 5)
    expected_state.check(state)
Пример #18
0
def test_execute_movcond(is16bit, val):
    state = new_state(AZ=1, rf1=val)
    instr = (opcode_factory.movcond16(condition=0b0000, rd=0, rn=1) if is16bit
             else opcode_factory.movcond32(condition=0b0000, rd=0, rn=1))
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    pc_expected = (2 if is16bit else 4) + RESET_ADDR
    expected_state = StateChecker(pc=pc_expected, rf0=val)
    expected_state.check(state)
Пример #19
0
def test_execute_bitr(bits, expected, is16bit):
    state = new_state(rf0=0, rf1=bits)
    instr = (opcode_factory.bitr16_immediate(rd=2, rn=1, imm=0) if is16bit
             else opcode_factory.bitr32_immediate(rd=2, rn=1, imm=0))
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(AZ=0, AC=0, AV=0,
                                  pc=((2 if is16bit else 4) + RESET_ADDR),
                                  rf2=expected)
    expected_state.check(state)
Пример #20
0
def test_branch_link(is16bit):
    state = new_state()
    cond = 0b1111  # Condition code for branch-and-link
    factory = opcode_factory.bcond16 if is16bit else opcode_factory.bcond32
    instr = factory(condition=cond, imm=0b00011000)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_LR = (2 if is16bit else 4) + RESET_ADDR
    expected = StateChecker(rfLR=expected_LR, pc=(RESET_ADDR + (0b00011000 << 1)))
    expected.check(state)
Пример #21
0
def test_execute_ldr_disp_pm(sub, new_rn):
    # Load.
    state = new_state(rf5=8)
    state.mem.write(8, 4, 42) # Start address, number of bytes, value
    # bb: 00=byte, 01=half-word, 10=word, 11=double-word
    instr = opcode_factory.ldstrpmd32(rd=0, rn=5, sub=sub, imm=1, bb=0b10, s=0)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rf0=42, rf5=new_rn)
    expected_state.check(state)
Пример #22
0
def test_str_index(is16bit):
    # Store.
    state = new_state(rf0=0xFFFFFFFF, rf5=8, rf6=8)
    if is16bit:
        instr = opcode_factory.ldstrind16(rd=0, rn=5, rm=6, bb=0b10, s=1)
    else:
        instr = opcode_factory.ldstrind32(rd=0, rn=5, rm=6, sub=0, bb=0b10, s=1)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rf0=0xFFFFFFFF, rf5=8, rf6=8)
    expected_state.check(state, memory=[(16, 4, 0xFFFFFFFF)])
Пример #23
0
def test_sub16_neg_imm():
    state = new_state(rf2=0, rf1=0)
    bits = opcode_factory.sub16_immediate(rd=2, rn=1, imm=0b111)
    name, executefn = decode(bits)
    instr = Instruction(bits, '')
    assert instr.rd == 2
    assert instr.rn == 1
    assert instr.imm3 == 0b111
    executefn(state, instr)
    expected_state = StateChecker(rf2=1, rf1=0)
    expected_state.check(state)
Пример #24
0
def test_execute_arith_shift_right_imm(rn, imm, is16bit):
    rd = 2
    state = new_state(rf0=trim_32(rn))
    instr = (opcode_factory.asr16_immediate(rd=rd, rn=0, imm=imm) if is16bit
             else opcode_factory.asr32_immediate(rd=rd, rn=0, imm=imm))
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(AZ=(False if rn < 0 else True), # 1 >> 5 == 0
                                  AV=0, AC=0,
                                  pc=((2 if is16bit else 4) + RESET_ADDR),
                                  rf2=(trim_32(-1) if rn < 0 else 0))
    expected_state.check(state)
Пример #25
0
def test_ldr_pm(is16bit):
    # Load.
    state = new_state(rf0=0, rf5=8, rf6=8)
    state.mem.write(16, 4, 0xFFFFFFFF)
    if is16bit:
        instr = opcode_factory.ldstrpm16(rd=0, rn=5, rm=6, bb=0b10, s=0)
    else:
        instr = opcode_factory.ldstrpm32(rd=0, rn=5, rm=6, sub=0, bb=0b10, s=0)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(rf0=0xFFFFFFFF, rf5=16, rf6=8)
    expected_state.check(state)
Пример #26
0
def test_execute_trap_warning(capsys):
    state = new_state()
    instr = opcode_factory.trap16(trap=0b11111)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    out, err = capsys.readouterr()
    expected_state = StateChecker(pc=(2 + RESET_ADDR))
    expected_text = ('WARNING: syscall not implemented: 31')
    expected_state.check(state)
    assert expected_text in out
    assert err == ''
    assert state.running
Пример #27
0
 def init_state(self, instructions, **args):
     """Load the program into a memory object.
     This function should ONLY be called by unit tests.
     """
     mem = new_memory()
     written_so_far = 0
     for i, (data, width) in enumerate(instructions):
         num_bytes = width / 8
         mem.write(RESET_ADDR + written_so_far, num_bytes, data)
         written_so_far += num_bytes
     self.state = new_state(mem=mem, debug=self.debug, **args)
     self.max_insts = len(instructions)
Пример #28
0
def test_execute_logical_shift_right(rn, rm, is16bit):
    rd = 2
    state = new_state(rf0=trim_32(rn), rf1=trim_32(rm))
    instr = (opcode_factory.lsr16(rd=rd, rn=0, rm=1) if is16bit
             else opcode_factory.lsr32(rd=rd, rn=0, rm=1))
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    expected_state = StateChecker(AZ=(False if rn < 0 else True), # 1 >> 5 == 0
                                  AV=0, AC=0,
                                  pc=((2 if is16bit else 4) + RESET_ADDR),
                                  rf2=(0b1111 if rn < 0 else 0))
    expected_state.check(state)
Пример #29
0
def test_execute_str_disp(is16bit, sub, expected):
    # Store.
    state = new_state(rf0=0xFFFFFFFF, rf5=8)
    # bb: 00=byte, 01=half-word, 10=word, 11=double-word
    if is16bit:
        instr = opcode_factory.ldstrdisp16(rd=0, rn=5, imm=1, bb=0b10, s=1)
    else:
        instr = opcode_factory.ldstrdisp32(rd=0, rn=5, sub=sub, imm=1, bb=0b10, s=1)
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    address = 8 - (1 << 2) if sub else 8 + (1 << 2)
    expected_state = StateChecker(rf0=0xFFFFFFFF, rf5=8)
    expected_state.check(state, memory=[(address, 4, 0xFFFFFFFF)])
Пример #30
0
def test_execute_idle16(capsys):
    state = new_state(rfSTATUS=1)
    instr = opcode_factory.idle16()
    name, executefn = decode(instr)
    executefn(state, Instruction(instr, None))
    out, err = capsys.readouterr()
    expected_state = StateChecker(pc=(2 + RESET_ADDR), rfSTATUS=0)
    expected_text = ('IDLE16 does not wait in this simulator. ' +
                     'Moving to next instruction.')
    expected_state.check(state)
    assert expected_text in out
    assert err == ''
    assert state.running