mem.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. class mem:
  2. class memregion:
  3. def __init__(self):
  4. self.start = 0
  5. self.end = 0
  6. self.size = 0
  7. self.mem = []
  8. def __str__(self):
  9. return 'Region {}-{}: {}'.format(hex(self.start),hex(self.end),self.mem)
  10. def __repr__(self):
  11. return str(self)
  12. def __init__(self):
  13. self.regions = []
  14. def create_region(self,start,size):
  15. region = mem.memregion()
  16. region.start = start
  17. region.size = size
  18. region.end = start+size-1
  19. region.mem = [0] * size
  20. self.regions.append(region)
  21. def write(self,address,value):
  22. value &= 0xFF
  23. for region in self.regions:
  24. if address >= region.start:
  25. if address <= region.end:
  26. region.mem[address-region.start] = value
  27. return
  28. raise Exception('not a valid address {}'.format(hex(address)))
  29. def read(self,address,mode='r'):
  30. for region in self.regions:
  31. if address >= region.start:
  32. if address <= region.end:
  33. return region.mem[address-region.start]
  34. raise Exception('not a valid address {}'.format(hex(address)))
  35. def dump(self):
  36. dumpfile = open('memdump','w')
  37. for region in self.regions:
  38. addr = region.start
  39. while addr < region.end:
  40. dumpfile.write('{}: '.format(hex(addr)))
  41. for i in range(16):
  42. if addr > region.end:
  43. break
  44. dumpfile.write('{} '.format(hex(self.read(addr)+256)[3:]))
  45. addr += 1
  46. dumpfile.write('\n')