Skip to content

Commit 6f27e01

Browse files
committed
Adding some missing components and elaborating the decoding scheme
1 parent a129cf8 commit 6f27e01

File tree

4 files changed

+224
-142
lines changed

4 files changed

+224
-142
lines changed

peripheral_drivers/i2cbridge/assem.py

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ def write(self, dadr, madr, data, addr_bytes=1):
336336
int madr : Memory address within device ('addr_bytes' long)
337337
[int] data : Data to write (List of byte-sized ints)
338338
int addr_bytes : Size in bytes of memory address (1 or 2)
339-
Returns: None
339+
Returns: program instructions
340340
"""
341341
if madr is None:
342342
addr_bytes = 0
343-
self._program += super().write(dadr, madr, data, addr_bytes=addr_bytes)
343+
pval = super().write(dadr, madr, data, addr_bytes=addr_bytes)
344+
self._program += pval
344345
self._check_pc()
345-
return None
346+
return pval
346347

347348
def read(self, dadr, madr, dlen, addr_bytes=1, reg_name=None):
348349
"""Add an I2C read transaction to the program.
@@ -353,24 +354,27 @@ def read(self, dadr, madr, dlen, addr_bytes=1, reg_name=None):
353354
int addr_bytes : Size in bytes of memory address (1 or 2)
354355
str reg_name : A name to associate with result memory address
355356
Returns: Starting memory offset of result
357+
NOPE! Returns: program instructions
356358
"""
357359
if madr is None:
358360
addr_bytes = 0
359-
self._program += super().read(dadr, madr, dlen, addr_bytes=addr_bytes)
361+
pval = super().read(dadr, madr, dlen, addr_bytes=addr_bytes)
362+
self._program += pval
360363
self._check_pc()
361364
if reg_name is None:
362365
reg_name = self._mkRegName(dadr, madr, self._rc)
363366
self._memdict[reg_name] = (self._rc, dlen)
364367
self._rc += dlen
365368
self._check_rc()
366-
return self._rc
369+
return pval
367370

368371
def pause(self, n):
369372
"""Add a pause of 'n' ticks to the program
370373
See README.md for discussion of tick length."""
371-
self._program += super().pause(n)
374+
pval = super().pause(n)
375+
self._program += pval
372376
self._check_pc()
373-
return
377+
return pval
374378

375379
def jump(self, n):
376380
"""Add a jump instruction to program counter n*32
@@ -387,9 +391,10 @@ def jump(self, n):
387391
"Jump would result in 'jump here' instruction"
388392
+ "(a jump to the program counter value of the jump instruction)."
389393
)
390-
self._program += super().jump(n)
394+
pval = super().jump(n)
395+
self._program += pval
391396
self._check_pc()
392-
return
397+
return pval
393398

394399
def jump_address(self, address):
395400
"""Add a jump instruction to program counter 'address'
@@ -475,11 +480,12 @@ def set_resx(self, n=None):
475480
if n is None:
476481
n = (self._rc // 32) + 1 # ceil(rc/32)
477482
n = int(n)
478-
self._program += super().set_resx(n)
483+
pval = super().set_resx(n)
484+
self._program += pval
479485
self._check_pc()
480486
self._rc = 32 * n
481487
self._check_rc() # This should be redundant but certainly can't hurt
482-
return
488+
return pval
483489

484490
def set_resx_address(self, address=None):
485491
"""Add a set result address pointer instruction to program.
@@ -505,33 +511,48 @@ def set_resx_address(self, address=None):
505511

506512
def buffer_flip(self):
507513
"""Add a buffer flip instruction to the program."""
508-
self._program += super().buffer_flip()
514+
pval = super().buffer_flip()
515+
self._program += pval
509516
self._check_pc()
510-
return
517+
return pval
511518

512519
def trig_analyz(self):
513520
"""Add an analyzer trigger instruction to the program."""
514-
self._program += super().trig_analyz()
521+
pval = super().trig_analyz()
522+
self._program += pval
515523
self._check_pc()
516-
return
524+
return pval
517525

518526
def hw_config(self, n):
519527
"""Add a hw_config set instruction to the program.
520528
Params:
521529
int n : 4-bit mask of hw_config outputs of module i2c_chunk"""
522-
self._program += super().hw_config(n)
530+
pval = super().hw_config(n)
531+
self._program += pval
523532
self._check_pc()
524-
return
533+
return pval
525534

526-
def pad(self, n=None):
535+
def pad(self, *args, **kwargs):
527536
"""Pad program memory up to location 32*n. Typically used to pad program
528537
to a location you can jump to. Consider using jump_pad() for this purpose.
529538
Params:
530539
int n : Integer multiple of 32 to pad to.
531540
Gotchas:
532541
32*n must be > current program counter. Use with no arg (n=None) to avoid
533542
this pitfall (pads up to next nearest multiple of 32).
534-
Returns: program counter index (pc/32) after pad"""
543+
Returns: program counter index (pc/32) after pad
544+
NOTE! To allow drop-in compatibility, this function also operates using the
545+
same interface as i2c_assem.pad(n, length). When used in this way, it returns
546+
a list of program instructions - the same as the old-style usage."""
547+
if len(args) == 2:
548+
# Hack to preserve API of parent class
549+
n = args[0]
550+
current_pc = args[1]
551+
return super().pad(n, current_pc)
552+
elif len(args) == 1:
553+
n = args[0]
554+
if 'n' in kwargs.keys():
555+
n = kwargs.get('n')
535556
if n is None:
536557
n = (self._pc() // 32) + 1 # ceil(pc/32)
537558
n = int(n)
@@ -544,7 +565,8 @@ def pad(self, n=None):
544565
raise I2C_Assembler_Exception(
545566
f"Program counter index {n} exceeds maximum {self._INDEX_MAX}"
546567
)
547-
self._program += super().pad(n, self._pc())
568+
pval = super().pad(n, self._pc())
569+
self._program += pval
548570
self._check_pc()
549571
return self._pc() // 32
550572

0 commit comments

Comments
 (0)