Chapter 6 code

This commit is contained in:
Danny Staple
2022-01-28 21:10:14 +00:00
parent fd4b0cb544
commit 881355daaa
23 changed files with 723 additions and 33 deletions
+44
View File
@@ -0,0 +1,44 @@
"""Sometimes, you need to debug a register.
ISR is a bit special - when you push, you destroy it's content -
you'll need to restore this. If you remove the mov from x,
it is set to 0s.
It'll cost a few instructions to do."""
import rp2pio
import adafruit_pioasm
import array
program = """
.program debug_register
set y, 21 ; use y to set ISR
mov isr, y
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("first push: {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("second: {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
+43
View File
@@ -0,0 +1,43 @@
"""Sometimes we want to extract a specific bit for
comparison or testing"""
"""Shifting a register."""
import rp2pio
import adafruit_pioasm
import array
# extracting bit 2
bit_to_extract = 30
program = f"""
.program extract_bit
pull block
; print initial state
mov isr, osr
push noblock ; and use PUSH to put it on the receive FIFO
; extract - by shifting
in osr, {bit_to_extract} ; get n bits
in null, 31 ; keep only 1 bit
push noblock
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
sm.write(array.array('I', [0b01101000_00000000_00000000_00000000]))
# read the data
sm.readinto(buffer)
print("Initial Y: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("Y bit extracted: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
+129
View File
@@ -0,0 +1,129 @@
"""Sometimes we want to extract a specific bit for
comparison or testing"""
"""Shifting a register."""
import rp2pio
import adafruit_pioasm
import array
def scenario(test_data, bit_to_extract, expected):
program = f"""
.program extract_bit
set y, {test_data} ; set a value in y
; extract - by shifting
in null, 32 ; clear the isr
in y, {bit_to_extract + 1} ; get n bits
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
in null, 31 ; Shift off all but last bit (leaving it)
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
mov y, isr ; reverse the isr back into y
; send y to fifo
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
with rp2pio.StateMachine(
assembled,
frequency=2000,
) as sm:
buffer = array.array('I', [0])
print("source bits: {0} 0b{0:032b} 0x{0:x}".format(test_data))
sm.readinto(buffer)
print("isr mid: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("isr after shift off: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("Y bit extracted: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
if buffer[0] != expected:
print("Didn't get what I expected")
print("Y bit expected: {0} 0b{0:032b} 0x{0:x}".format(expected))
print("")
def pull_scenario(test_data, bit_to_extract, expected):
program = f"""
.program extract_bit_pull
pull block ; pull data
mov y, osr
; extract - by shifting
in null, 32 ; clear the isr
in y, {bit_to_extract + 1} ; get n bits
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
in null, 31 ; Shift off all but last bit (leaving it)
; debug isr
mov x, isr ; preserve isr - pushing destroys it
push noblock
mov isr, x ; restore isr
; done debug
mov y, isr ; reverse the isr back into y
; send y to fifo
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
with rp2pio.StateMachine(
assembled,
frequency=2000,
) as sm:
sm.write(array.array('I', [test_data]))
buffer = array.array('I', [0])
print("source bits: {0} 0b{0:032b} 0x{0:x}".format(test_data))
sm.readinto(buffer)
print("isr mid: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("isr after shift off: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("Y bit extracted: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
if buffer[0] != expected:
print("Didn't get what I expected")
print("Y bit expected: {0} 0b{0:032b} 0x{0:x}".format(expected))
print("")
scenario(0b101, 1, 0)
scenario(0b110, 1, 1)
scenario(0b010, 1, 1)
scenario(0b100, 2, 1)
scenario(0b011, 2, 0)
scenario(0b1011, 3, 1)
scenario(0b1010, 3, 1)
scenario(0b1000, 3, 1)
scenario(0b0111, 3, 0)
# bit I want
# v
pull_scenario(0b10101000_00000000_00000000_00000000, 29, 1)
pull_scenario(0b11011000_00000000_00000000_00000000, 29, 0)
# v
pull_scenario(0b01011000_00000000_00000000_00000000, 30, 1)
pull_scenario(0b10111000_00000000_00000000_00000000, 30, 0)
# v
pull_scenario(0b10111000_00000000_00000000_00000000, 31, 1)
pull_scenario(0b01111000_00000000_00000000_00000000, 31, 0)
+37
View File
@@ -0,0 +1,37 @@
"""We can invert the bits of a register."""
import rp2pio
import adafruit_pioasm
import array
program = """
.program invert_register
set y, 21 ; set a value in y
; send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
; invert
mov isr, ~ y ; copy and reversed the registe into the ISR
; send it to be printed
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('i', [0])
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
+37
View File
@@ -0,0 +1,37 @@
"""Count down"""
import rp2pio
import adafruit_pioasm
import array
program = """
.program counting_down
set y, 21 ; set a value in y
; now send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
jmp y--, fake ; subtract from it
fake:
; now send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("Before {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
# read the data
sm.readinto(buffer)
print("After {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
+39
View File
@@ -0,0 +1,39 @@
"""Count down"""
import rp2pio
import adafruit_pioasm
import array
program = """
.program counting_up
set y, 21 ; set a value in y
; now send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
; Add 1
mov y, ~ y ; invert it
jmp y--, fake ; subtract from it
fake:
mov isr, ~ y
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("Before {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
# read the data
sm.readinto(buffer)
print("After {0} 0b{0:08b} 0x{0:x}".format(buffer[0]))
@@ -0,0 +1,26 @@
"""Sometimes, you need to debug a register.
Note - ISR content is destroyed.
It'll cost a few instructions to do."""
import rp2pio
import adafruit_pioasm
import array
program = """
.program debug_register
set y, 21 ; set a value in y
; now send it to be printed
mov isr, y ; copy the register you need to dump into the ISR
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(assembled, frequency=2000)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("{0} 0b{0:08b}".format(buffer[0]))
+37
View File
@@ -0,0 +1,37 @@
"""We can reverse the content of a register."""
import rp2pio
import adafruit_pioasm
import array
program = """
.program reverse_register
set y, 21 ; set a value in y
; send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
; reverse it
mov isr, :: y ; copy and reversed the register into the ISR
; print it
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
+40
View File
@@ -0,0 +1,40 @@
"""Shift and reverse"""
import rp2pio
import adafruit_pioasm
import array
program = """
.program debug_register
set y, 2 ; set a value in y
; send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
; shift and reverse
in y, 1 ; get bits
mov isr, :: isr ; reverse it
; print again
push noblock ; and use PUSH to put it on the receive FIFO
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
# read the data
sm.readinto(buffer)
print("{0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
+49
View File
@@ -0,0 +1,49 @@
"""Shifting a register.
Default shift direction is in from the left.
Space is made for the new value, and n bits from the source are
shifted in.
"""
import rp2pio
import adafruit_pioasm
import array
program = f"""
.program debug_register
set y, {0b10011} ; set a value in y
; send it to be printed
mov isr, y
push noblock ; and use PUSH to put it on the receive FIFO
; shift pts
in null, 32 ; clear the isr
in y,3 ; get n bits from y
; now send it to be printed
push noblock ; and use PUSH to put it on the receive FIFO
; show state of y
mov isr, y
push noblock
"""
assembled = adafruit_pioasm.assemble(program)
## set up a statemachine
sm = rp2pio.StateMachine(
assembled,
frequency=2000,
)
buffer = array.array('I', [0])
# read the data
sm.readinto(buffer)
print("Before shift: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("ISR shifted: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))
sm.readinto(buffer)
print("Y state: {0} 0b{0:032b} 0x{0:x}".format(buffer[0]))