-
Notifications
You must be signed in to change notification settings - Fork 8
/
mem.py
541 lines (474 loc) · 12.2 KB
/
mem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#!/usr/local/bin/python
#
# This is the class that simulates memory.
#
# In addition to a word-size, it is possible to store "qualifier" bits
# with each word. This can be used to mark memory words with bits of
# various observations, one example being "relocated" which usually
# is a strong indication of non-instruction-ness
#
from __future__ import print_function
import struct
import array
def ascii(x):
x &= 0xff
if x < 32 or x > 126:
return " "
return "%c" % x
#######################################################################
#
# We report trouble as these exceptions
#
class MemError(Exception):
def __init__(self, adr, reason):
self.adr = adr
self.reason = reason
self.value = ("0x%x:" % adr + str(self.reason),)
def __str__(self):
return repr(self.value)
#######################################################################
#
# A very abstract memory class, can handle up to 32 bit wide locations,
# including (optional) flags and qualifiers
#
class base_mem(object):
def __init__(self, start=0, end=0, bits=8, qualifiers=0, flags=False):
if end < start:
raise MemError(start,
"Negative Length (0x%x-0x%x" % (start, end))
self.start = start
self.end = end
self.flags = flags
self.qualifiers = qualifiers
self.bits = bits
self.qmask = ((1<<qualifiers) - 1) << bits
self.bmask = (1<<bits) - 1
self.apct = "%%0%dx" % len("%x" % (self.end - 1))
self.dpct = "%%0%dx" % (bits / 4)
self.qpct = "[%%0%dx]" % (qualifiers / 4)
w = bits + qualifiers
if flags:
self.fmask = ((1<<6) - 1) << w
# Flag bits
self.can_read = 1 << (w + 0)
self.can_write = 1 << (w + 1)
self.invalid = 1 << (w + 2)
self.undef = 1 << (w + 3)
self.acc_read = 1 << (w + 4)
self.acc_write = 1 << (w + 5)
w += 6
else:
self.can_read = 0
self.can_write = 0
self.invalid = 0
self.undef = 0
self.acc_read = 0
self.acc_write = 0
if w <= 8:
self.mem = array.array('B')
elif w <= 16:
self.mem = array.array('H')
elif w <= 32:
self.mem = array.array('L')
else:
raise MemError(w, "Too many bits (%d) wide" % w)
for a in range(self.start, self.end):
self.mem.append(self.invalid | self.undef)
# Format a memory address as a hex string
# If you don't like hex, you can override .[adq]pct or subclass
# and overload .[adq]fmt()
def afmt(self, a):
return self.apct % a
def dfmt(self, d):
return self.dpct % d
def qfmt(self, q):
return self.qpct % q
# Default adr/data/ascii column formatter
# returns a list of lines, all the same width
def col1(self, p, start, end, lvl):
l = list()
while start < end:
try:
x = self.rd(start)
except:
l.append(self.afmt(start) + " --")
start += 1
continue
s = self.afmt(start) + " " + self.dfmt(x)
if self.qualifiers > 0:
s += self.qfmt(self.rdqual(start))
s += " |"
for b in range(24,-1,-8):
if self.bits > b:
s += ascii(x >> b)
s += "|\t"
l.append(s)
start += 1
return l
# Default content formatter
def col2(self, p, start, end, lvl):
l = list()
while start < end:
l.append(".XXX")
start += 1
return l
# Check if an address is inside this piece of memory
def chkadr(self, start, end=None):
if start < self.start:
raise MemError(start, "Invalid location")
if start >= self.end:
raise MemError(start, "Invalid location")
if end == None:
return
if end < self.start:
raise MemError(end, "Invalid location")
if end > self.end:
raise MemError(end, "Invalid location")
# Set some flags on a range of memory
def setflags(self, start, end = None, set=0, reset=0):
if not self.flags:
return
if set & ~self.fmask:
raise MemError(start,
"Invalid set flag (0x%x)" % set)
if reset & ~self.fmask:
raise MemError(start,
"Invalid reset flag (0x%x)" % reset)
self.chkadr(start, end)
if end == None:
end = start + 1
for i in range(start, end):
self.mem[i - self.start] |= set
self.mem[i - self.start] &= ~reset
# Test if flags are set on all locations in range
def tstflags(self, start, end = None, flags = 0, fallback=None):
if not self.flags:
return fallback
if flags & ~self.fmask:
raise MemError(start,
"Invalid set flag (0x%x)" % flags)
self.chkadr(start, end)
if end == None:
end = start + 1
for i in range(start, end):
if not self.mem[i - self.start] & flags:
return False
return True
# Read a single location
def rd(self, adr):
assert type(adr) == int
self.chkadr(adr)
x = self.mem[adr - self.start]
if self.flags:
if x & self.invalid:
raise MemError(adr, "Invalid location")
if not x & self.can_read:
raise MemError(adr, "Read forbidden")
self.mem[adr - self.start] |= self.acc_read
if x & self.undef:
return None
return x & self.bmask
# Write a single location
def wr(self, adr, data):
self.chkadr(adr)
i = adr - self.start
if data & ~self.bmask:
raise MemError(adr,
"Write illegal bits (0x%x)" % data)
x = self.mem[i]
if self.flags:
if x & self.invalid:
raise MemError(adr, "Invalid location")
if not x & self.can_write:
raise MemError(adr, "Write forbidden")
x |= self.acc_write
x &= ~self.undef
self.mem[i] = (x & ~self.bmask) | data
# Read a single location
def rdqual(self, adr):
assert type(adr) == int
self.chkadr(adr)
x = self.mem[adr - self.start]
if self.flags:
if x & self.invalid:
raise MemError(adr, "Invalid location")
if not x & self.can_read:
raise MemError(adr, "Read forbidden")
if x & self.undef:
return None
return (x & self.qmask) >> self.bits
# Write a single location
def wrqual(self, adr, qual):
self.chkadr(adr)
i = adr - self.start
if (qual << self.bits) & ~self.qmask:
raise MemError(adr,
"Write illegal qualifier (0x%x)" % qual)
x = self.mem[i]
if self.flags:
if x & self.invalid:
raise MemError(adr, "Invalid location")
if not x & self.can_write:
raise MemError(adr, "Write forbidden")
self.mem[i] = (x & ~self.qmask) | (qual << self.bits)
# Find a particular pattern
# 'None' is a wildcard
def find(self, start, end, pattern):
l = list()
lx = len(pattern)
i = 0
for ax in range(start, end-lx):
if pattern[i] != None:
try:
x = self.rd(ax)
except:
i = 0
continue
if pattern[i] != x:
i = 0
continue
i += 1
if i < lx:
continue
l.append(ax - (lx-1))
i = 0
return l
#######################################################################
#
# The normal microprocessor byte addressable model
#
class byte_mem(base_mem):
def __init__(self, start=0, end=0, qualifiers=0, flags=False, endian=None):
base_mem.__init__(self,
start = start,
end = end,
bits = 8,
qualifiers = qualifiers,
flags = flags)
# Number of bytes per line
self.bcols = 8
if endian == "big-endian" or endian == ">":
self.w16 = self.b16
self.s16 = self.sb16
self.w32 = self.b32
self.s32 = self.sb32
elif endian == "little-endian" or endian == "<":
self.w16 = self.l16
self.s16 = self.sl16
self.w32 = self.l32
self.s32 = self.sl32
elif endian != None:
raise MemError(0, "Unknown endianess (%s)" % endian)
# All these functions come in big and little endian
# signed versions have 's' prefix
def s8(self, adr):
a = self.rd(adr)
if a & 0x80:
a -= 256
return a
def l16(self, adr):
return self.rd(adr + 1) << 8 | self.rd(adr)
def b16(self, adr):
return self.rd(adr) << 8 | self.rd(adr + 1)
def sl16(self, adr):
a = self.l16(adr)
if a & 0x8000:
a -= 65536
return a
def sb16(self, adr):
a = self.b16(adr)
if a & 0x8000:
a -= 65536
return a
def l32(self, adr):
return self.rd(adr + 3) << 24 | \
self.rd(adr + 2) << 16 | \
self.rd(adr + 1) << 8 | self.rd(adr)
def sl32(self, adr):
v = self.l32(adr)
if v & (1 << 31):
return v - (1<<32)
return v
def b32(self, adr):
return self.rd(adr) << 24 | \
self.rd(adr + 1) << 16 | \
self.rd(adr + 2) << 8 | self.rd(adr + 3)
def sb32(self, adr):
v = self.b32(adr)
if v & (1 << 31):
return v - (1<<32)
return v
def ascii(self, adr, len=-1):
s = ""
while True:
x = self.rd(adr)
if len == -1 and x == 0:
break
if x >= 32 and x <= 126:
s += "%c" % x
elif x == 10:
s += "\\n"
elif x == 13:
s += "\\r"
else:
s += "\\x%02x" % x
if len > 0:
len -= 1
if len == 0:
break
adr += 1
return s
def fromhexfile(self, fn, offset = 0, step = 1):
f = open(fn, "r")
na = 0
for i in f.readlines():
if i[0] == "#":
continue
j = i.split()
a = int(j[0], 16)
if a != na:
assert "address wrong" == "in hexfile"
na = a + 1
d = int(j[1], 16)
self.setflags(offset, None,
self.can_read|self.can_write,
self.invalid|self.undef)
self.wr(offset, d)
offset += step
def fromfile(self, fn, offset = 0, step = 1):
f = open(fn, "rb")
d = f.read()
d = bytearray(d)
f.close()
for i in d:
self.setflags(offset, None,
self.can_read|self.can_write,
self.invalid|self.undef)
self.wr(offset, i)
offset += step
def col1(self, p, start, end, lvl):
l = list()
while start < end:
s = self.afmt(start)
s += " "
t = "|"
for i in range(0,self.bcols):
if start + i >= end:
s += " "
t += " "
else:
try:
x = self.rd(start + i)
except:
s += " --"
t += " "
continue
s += " %02x" % x
t += ascii(x)
s += " " + t + "| "
s += p.indent
l.append(s)
start += self.bcols
return l
# Default content formatter
def col2(self, p, start, end, lvl):
l = list()
while start < end:
l.append(".XXX")
start += self.bcols
return l
class seg_mem(object):
def __init__(self, segmask, offmask):
assert (segmask & offmask) == 0
self.segmask = segmask
self.segshift = 0
while not (segmask & (1 << self.segshift)):
self.segshift += 1
self.offmask = offmask
self.offshift = 0
while not (offmask & (1 << self.offshift)):
self.offshift += 1
self.nonmask = ~(segmask | offmask)
print("SEGmem(%08x, %d, %08x, %d, %08x)" % (
self.segmask, self.segshift,
self.offmask, self.offshift,
self.nonmask
))
self.segs = dict()
self.start= None
self.end= None
self.segfmt = "%02x:"
def seg_off(self, adr):
if adr & self.nonmask:
raise MemError(start,
"Invalid segmented address 0x%x" % adr)
s = (adr & self.segmask) >> self.segshift
o = (adr & self.offmask) >> self.offshift
return (s,o)
def linadr(self, seg, off):
return (seg << self.segshift) | (off << self.offshift)
def next_adr(self, adr):
s = (adr & self.segmask) >> self.segshift
o = (adr & self.offmask) >> self.offshift
if s in self.segs:
try:
r = self.segs[s].next_adr(o)
return r
except:
pass
s += 1
return (s << self.segshift)
def afmt(self, adr):
s,o = self.seg_off(adr)
return self.segfmt % s + self.segs[s].afmt(o)
def add_seg(self, seg, m):
assert seg not in self.segs
assert m.end - 1 <= self.offmask
b = seg << self.segshift
if self.start == None or b < self.start:
self.start = b
if self.end == None or b > self.end:
self.end = b + m.end
self.segs[seg] = m
def rd(self, adr):
s,o = self.seg_off(adr)
return self.segs[s].rd(o)
def w16(self, adr):
s,o = self.seg_off(adr)
return self.segs[s].w16(o)
def b16(self, adr):
s,o = self.seg_off(adr)
return self.segs[s].b16(o)
def b32(self, adr):
s,o = self.seg_off(adr)
return self.segs[s].b32(o)
def w32(self, adr):
s,o = self.seg_off(adr)
return self.segs[s].w32(o)
def chkadr(self, adr):
try:
s,o = self.seg_off(adr)
except:
raise MemError(start, "Invalid location")
if not s in self.segs:
raise MemError(start, "Invalid location")
return self.segs[s].chkadr(o)
def ascii(self, adr, len):
s,o = self.seg_off(adr)
return self.segs[s].ascii(o, len)
def col1(self, p, start, end, lvl):
ss,so = self.seg_off(start)
es,eo = self.seg_off(end)
assert ss == es
c1 = self.segs[ss].col1(p, so, eo, lvl)
c11 = list()
for i in c1:
c11.append(self.segfmt % ss + i)
return c11
def col2(self, p, start, end, lvl):
ss,so = self.seg_off(start)
es,eo = self.seg_off(end)
assert ss == es
return self.segs[ss].col2(p, so, eo, lvl)
if __name__ == "__main__":
m = byte_mem(0,0x500)