Skip to content

Commit 4d8f7c0

Browse files
committed
WIP. Python3 changes.
1 parent 8689758 commit 4d8f7c0

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

mem.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def cmd_rd(self, ui, args, n):
130130
return
131131
adr = util.align(adr, n)
132132
ui.put('[0x%08x] = ' % adr)
133-
ui.put('0x%%0%dx\n' % (n/4) % self.cpu.rd(adr, n))
133+
ui.put('0x%%0%dx\n' % (n >> 2) % self.cpu.rd(adr, n))
134134

135135
def cmd_rd8(self, ui, args):
136136
"""read 8 bits"""
@@ -160,7 +160,7 @@ def cmd_wr(self, ui, args, n):
160160
val = util.mask_val(val, n)
161161
self.cpu.wr(adr, val, n)
162162
ui.put('[0x%08x] = ' % adr)
163-
ui.put('0x%%0%dx\n' % (n/4) % val)
163+
ui.put('0x%%0%dx\n' % (n >> 2) % val)
164164

165165
def cmd_wr8(self, ui, args):
166166
"""write 8 bits"""
@@ -241,7 +241,7 @@ def __display(self, ui, args, width):
241241
else:
242242
assert False, 'bad width'
243243
# read and print the data
244-
for i in range(n/16):
244+
for i in range(n >> 4):
245245
# read 4, 32-bit words (16 bytes per line)
246246
io = iobuf.data_buffer(32)
247247
self.cpu.rdmem(adr, 4, io)
@@ -309,8 +309,8 @@ def cmd_pic(self, ui, args):
309309
# bytes per row
310310
bpr = cols * bps
311311
# work out the none padding
312-
nwords = n / 4
313-
nwords_displayed = (((cols * rows * bps) + 3) & ~3) / 4
312+
nwords = n >> 2
313+
nwords_displayed = (((cols * rows * bps) + 3) & ~3) >> 2
314314
none_pad = [None,] * ((nwords_displayed - nwords) * 4)
315315
# read the memory
316316
if n > (16 << 10):
@@ -354,7 +354,7 @@ def cmd_md5(self, ui, args):
354354
ui.put('reading memory ...\n')
355355
data = iobuf.data_buffer(32)
356356
t_start = time.time()
357-
self.cpu.rdmem(adr, n/4, data)
357+
self.cpu.rdmem(adr, n >> 2, data)
358358
t_end = time.time()
359359
ui.put('%s\n' % data.md5('le'))
360360
ui.put('%.2f KiB/sec\n' % (float(n)/((t_end - t_start) * 1024.0)))
@@ -374,7 +374,7 @@ def cmd_test(self, width, ui, args):
374374
# round up n to an integral multiple of 4 bytes
375375
n = (n + 3) & ~3
376376
# convert to n 32/16/8-bit units
377-
nx = n / (width / 8)
377+
nx = int(n / (width >> 3))
378378
maxval = (1 << width) - 1
379379
# we will typically be testing ram, so halt the cpu.
380380
self.cpu.halt()

soc.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def description_cleanup(s):
2929
"""cleanup a description string"""
3030
if s is None:
3131
return None
32-
s = util.rm_non_ascii(s)
3332
s = s.strip('."')
3433
# remove un-needed white space
3534
return ' '.join([x.strip() for x in s.split()])
@@ -299,7 +298,7 @@ def bind_cpu(self, cpu):
299298
self.cpu = cpu
300299

301300
def adr(self, idx, size):
302-
return self.parent.address + self.offset + (idx * (size / 8))
301+
return self.parent.address + self.offset + (idx * (size >> 3))
303302

304303
def rd(self, idx=0):
305304
return self.cpu.rd(self.adr(idx, self.size), self.size)
@@ -319,9 +318,7 @@ def clr_bit(self, val, idx=0):
319318
def field_list(self):
320319
"""return an ordered fields list"""
321320
# build a list of fields in most significant bit order
322-
f_list = self.fields.values()
323-
f_list.sort(key=lambda x: x.msb, reverse=True)
324-
return f_list
321+
return sorted(self.fields.values(), key = lambda x : x.msb, reverse=True)
325322

326323
def display(self, display_fields):
327324
"""return display columns (name, adr, val, descr) for this register"""
@@ -400,9 +397,7 @@ def register_list(self):
400397
"""return an ordered register list"""
401398
# build a list of registers in address offset order
402399
# tie break with the name to give a well-defined sort order
403-
r_list = self.registers.values()
404-
r_list.sort(key=lambda x: (x.offset << 16) + sum(bytearray(x.name)))
405-
return r_list
400+
return sorted(self.registers.values(), key = lambda x : (x.offset << 16) + sum(bytearray(x.name.encode('utf8'))))
406401

407402
def display(self, register_name=None, fields=False):
408403
"""return a display string for this peripheral"""
@@ -539,16 +534,12 @@ def peripheral_list(self):
539534
# build a list of peripherals in base address order
540535
# base addresses for peripherals are not always unique. e.g. nordic chips
541536
# so tie break with the name to give a well-defined sort order
542-
p_list = self.peripherals.values()
543-
p_list.sort(key=lambda x: (x.address << 16) + sum(bytearray(x.name)))
544-
return p_list
537+
return sorted(self.peripherals.values(), key = lambda x : (x.address << 16) + sum(bytearray(x.name.encode('utf8'))))
545538

546539
def interrupt_list(self):
547540
"""return an ordered interrupt list"""
548541
# sort by irq order
549-
i_list = self.interrupts.values()
550-
i_list.sort(key=lambda x: x.irq)
551-
return i_list
542+
return sorted(self.interrupts.values(), key = lambda x : x.irq)
552543

553544
def cmd_map(self, ui, args):
554545
"""display memory map"""

util.py

+9-23
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ def wrong_argc(ui, args, valid):
5252
argc = len(args)
5353
if argc in valid:
5454
return False
55-
else:
56-
ui.put(bad_argc)
57-
return True
55+
ui.put(bad_argc)
56+
return True
5857

5958
# -----------------------------------------------------------------------------
6059

@@ -85,9 +84,8 @@ def dict_arg(ui, arg, d):
8584
"""argument to value through a dictionary - or None"""
8685
if arg in d:
8786
return d[arg]
88-
else:
89-
ui.put(inv_arg)
90-
return None
87+
ui.put(inv_arg)
88+
return None
9189

9290
# ----------------------------------------------------------------------------
9391

@@ -195,8 +193,7 @@ def maskshift(field):
195193
"""return a mask and shift defined by field"""
196194
if len(field) == 1:
197195
return (1 << field[0], field[0])
198-
else:
199-
return (((1 << (field[0] - field[1] + 1)) - 1) << field[1], field[1])
196+
return (((1 << (field[0] - field[1] + 1)) - 1) << field[1], field[1])
200197

201198
def bits(val, field):
202199
"""return the bits (masked and shifted) from the value"""
@@ -246,7 +243,7 @@ def format_bit(x, c):
246243
"""return a character to represent a bit state"""
247244
if x == 0:
248245
return '.'
249-
elif x == 1:
246+
if x == 1:
250247
return c
251248
return ' '
252249

@@ -269,7 +266,7 @@ def rm_prefix(names, prefix_set):
269266
# remove the prefix
270267
if n:
271268
#print('removing prefix %s' % names[0][:n])
272-
for i in range(len(names)):
269+
for i, _ in enumerate(names):
273270
names[i] = names[i][n:]
274271

275272
def rm_suffix(names, suffix_set):
@@ -289,29 +286,18 @@ def rm_suffix(names, suffix_set):
289286
# remove the suffix
290287
if n:
291288
#print('removing suffix %s' % names[0][-n:])
292-
for i in range(len(names)):
289+
for i, _ in enumerate(names):
293290
names[i] = names[i][:-n]
294291

295292
# -----------------------------------------------------------------------------
296293

297-
def rm_non_ascii(s):
298-
"""remove non-ascii characters from a string"""
299-
for i in range(len(s)):
300-
try:
301-
s[i].encode('ascii')
302-
except:
303-
s = s[i].replace(' ')
304-
return s
305-
306-
# -----------------------------------------------------------------------------
307-
308294
def display_cols(clist, csize=None):
309295
"""
310296
return a string for a list of columns
311297
each element in clist is [col0_str, col1_str, col2_str, ...]
312298
csize is a list of column width minimums
313299
"""
314-
if len(clist) == 0:
300+
if not clist:
315301
return ''
316302
# how many columns?
317303
ncols = len(clist[0])

0 commit comments

Comments
 (0)