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)
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)
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)
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)
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)
Example #6
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)
Example #7
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)])
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
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)
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)
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)])
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)])
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)
Example #14
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)
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)
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)
Example #17
0
def test_sim_bkpt16():
    instructions = [(opcode_factory.bkpt16(), 16),  # BKPT16
                   ]
    epiphany = MockEpiphany()
    epiphany.init_state(instructions)
    assert epiphany.state.running
    epiphany.run()
    expected_state = StateChecker(pc=(2 + RESET_ADDR))
    expected_state.check(epiphany.state)
    assert not epiphany.state.running
Example #18
0
def test_single_inst_add32():
    instructions = [(opcode_factory.add32_immediate(rd=1, rn=0, imm=0b01010101010), 32),
                    (opcode_factory.bkpt16(), 16)]
    epiphany = MockEpiphany()
    epiphany.init_state(instructions, rf0=0b01010101010)
    assert epiphany.state.running
    epiphany.run()
    expected_state = StateChecker(AZ=0, pc=(6 + RESET_ADDR), rf1=(0b01010101010 * 2))
    expected_state.check(epiphany.state)
    assert not epiphany.state.running
Example #19
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)
Example #20
0
def test_testset32():
    elf_filename = os.path.join(elf_dir, 'testset.elf')
    epiphany = Epiphany()
    with open(elf_filename, 'rb') as elf:
        if elf == 'movts.elf': print 'MOVTS'
        epiphany.init_state(elf, elf_filename, '', [], False, is_test=True)
        epiphany.state.mem.write(0x100004, 4, 0x0)
        epiphany.max_insts = 100000
        epiphany.run()
        expected = StateChecker(AZ=1, rf0=0, rf1=0x100000, rf2=0x4)
        expected.check(epiphany.state, memory=[(0x100004, 4, 0xFFFF)])
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)])
Example #22
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)
Example #23
0
def test_single_inst_sub32():
    from pydgin.utils import trim_32
    instructions = [(opcode_factory.sub32_immediate(rd=1, rn=0, imm=0b01010101010), 32),
                    (opcode_factory.bkpt16(), 16)]
    epiphany = MockEpiphany()
    epiphany.init_state(instructions, rf0=5)
    assert epiphany.state.running
    epiphany.run()
    expected_state = StateChecker(AZ=0, AN=1, AC=0,
                                  pc=(6 + RESET_ADDR), rf1=trim_32(5 - 0b01010101010))
    expected_state.check(epiphany.state)
    assert not epiphany.state.running
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)
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
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)
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)
Example #28
0
def test_sim_all_16bit():
    instructions = [ (opcode_factory.gie16(), 16),
                     (opcode_factory.gid16(), 16),
                     (opcode_factory.idle16(), 16),
                     (opcode_factory.bkpt16(), 16),
                   ]
    epiphany = MockEpiphany()
    epiphany.init_state(instructions)
    assert epiphany.state.running
    epiphany.run()
    expected_state = StateChecker(pc=(8 + RESET_ADDR))
    expected_state.check(epiphany.state)
    assert not epiphany.state.running
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
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)])