sim.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import elf
  2. import mem
  3. import cpu
  4. from rvtypes import uint32
  5. import sys
  6. bp = []
  7. STACK_TOP=0x8000000
  8. STACK_SIZE=0x1000
  9. if __name__ == '__main__':
  10. code = elf.elf_file(sys.argv[1])
  11. code.load()
  12. m = mem.mem()
  13. for i in range(len(code.sheaders)):
  14. section = code.sheaders[i]
  15. if section.sh_type == 1:
  16. if (section.sh_flags & 6)==6 or (section.sh_flags & 6)==2:
  17. m.create_region(section.sh_addr,section.sh_size)
  18. buff = code.read_segment(i)
  19. for j in range(len(buff)):
  20. m.write(section.sh_addr+j,buff[j])
  21. m.create_region(STACK_TOP,STACK_SIZE)
  22. c = cpu.cpu(m)
  23. c.registers[2] = uint32(STACK_TOP+STACK_SIZE)
  24. c.pc = code.header.e_entry
  25. try:
  26. while True:
  27. res = c.step()
  28. if res > 0:
  29. break
  30. if c.pc in bp:
  31. break
  32. except Exception as e:
  33. import traceback
  34. traceback.print_exception(e)
  35. print(e)
  36. #m.dump()