Skip to content

Commit 679feb8

Browse files
committed
Fix assembly of irq & mov rxfifo[] instructions to match sdk pioasm
1 parent 89fc1a1 commit 679feb8

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

adafruit_pioasm.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def parse_rxfifo_brackets(arg, fifo_dir):
234234

235235
max_delay = 2 ** (5 - sideset_count - sideset_enable) - 1
236236
assembled = []
237-
for line in instructions:
237+
for line in instructions: # pylint: disable=too-many-nested-blocks
238238
instruction = splitter(line.strip())
239239
delay = 0
240240
if (
@@ -299,21 +299,32 @@ def parse_rxfifo_brackets(arg, fifo_dir):
299299
assembled[-1] |= num
300300
assembled[-1] |= 0b11 << 5 # JMPPIN wait source
301301
else:
302+
idx = 3
302303
assembled[-1] |= WAIT_SOURCES.index(instruction[2]) << 5
303-
num = int(instruction[3], 0)
304-
if not 0 <= num <= 31:
305-
raise RuntimeError("Wait num out of range")
304+
if source == "irq":
305+
if instruction[idx] == "next":
306+
require_version(1, "wait irq next")
307+
assembled[-1] |= 0b11000
308+
idx += 1
309+
elif instruction[idx] == "prev":
310+
require_version(1, "wait irq prev")
311+
assembled[-1] |= 0b01000
312+
idx += 1
313+
314+
limit = 8
315+
# The flag index is decoded in the same way as the IRQ
316+
# index field, decoding down from the two MSBs
317+
if instruction[-1] == "rel":
318+
if assembled[-1] & 0b11000:
319+
raise RuntimeError("cannot use next/prev with rel")
320+
assembled[-1] |= 0b10000
321+
else:
322+
limit = 32
323+
num = int_in_range(
324+
instruction[idx], 0, limit, "wait {instruction[2]}"
325+
)
306326
assembled[-1] |= num
307-
# The flag index is decoded in the same way as the IRQ
308-
# index field, decoding down from the two MSBs
309-
if instruction[-1] == "next":
310-
require_version(1, "wait irq next")
311-
assembled[-1] |= 0b11000
312-
elif instruction[-1] == "prev":
313-
require_version(1, "wait irq prev")
314-
assembled[-1] |= 0b01000
315-
elif instruction[-1] == "rel":
316-
assembled[-1] |= 0b10000
327+
317328
elif instruction[0] == "in":
318329
# instr delay src count
319330
assembled.append(0b010_00000_000_00000)
@@ -352,15 +363,15 @@ def parse_rxfifo_brackets(arg, fifo_dir):
352363
elif instruction[0] == "mov":
353364
# instr delay dst op src
354365
if instruction[1].startswith("rxfifo["): # mov rxfifo[], isr
355-
assembled.append(0b100_00000_0001_0_000)
366+
assembled.append(0b100_00000_0001_1_000)
356367
if instruction[2] != "isr":
357368
raise ValueError("mov rxfifo[] source must be isr")
358-
assembled[-1] |= parse_rxfifo_brackets(instruction[1], "txput")
369+
assembled[-1] ^= parse_rxfifo_brackets(instruction[1], "txput")
359370
elif instruction[2].startswith("rxfifo["): # mov osr, rxfifo[]
360-
assembled.append(0b100_00000_1001_0_000)
371+
assembled.append(0b100_00000_1001_1_000)
361372
if instruction[1] != "osr":
362373
raise ValueError("mov ,rxfifo[] target must be osr")
363-
assembled[-1] |= parse_rxfifo_brackets(instruction[2], "txget")
374+
assembled[-1] ^= parse_rxfifo_brackets(instruction[2], "txget")
364375
else:
365376
assembled.append(0b101_00000_000_00_000)
366377
assembled[-1] |= mov_destinations.index(instruction[1]) << 5
@@ -388,30 +399,35 @@ def parse_rxfifo_brackets(arg, fifo_dir):
388399
assembled.append(0b110_00000_0_0_0_00000)
389400

390401
irq_type = 0
391-
if instruction[-1] == "prev":
402+
idx = 1
403+
if instruction[idx] == "wait":
404+
assembled[-1] |= 0x20
405+
idx += 1
406+
elif instruction[idx] == "clear":
407+
assembled[-1] |= 0x40
408+
idx += 1
409+
410+
if instruction[idx] == "prev":
392411
irq_type = 1
393412
require_version(1, "irq prev")
394-
instruction.pop()
395-
elif instruction[-1] == "next":
413+
idx += 1
414+
elif instruction[idx] == "next":
396415
irq_type = 3
397416
require_version(1, "irq next")
398-
instruction.pop()
399-
elif instruction[-1] == "rel":
417+
idx += 1
418+
419+
if instruction[-1] == "rel":
420+
if irq_type != 0:
421+
raise RuntimeError("cannot use next/prev with rel")
400422
irq_type = 2
401423
instruction.pop()
402424

403425
assembled[-1] |= irq_type << 3
404426

405-
num = int_in_range(instruction[-1], 0, 8, "irq index")
427+
num = int_in_range(instruction[idx], 0, 8, "irq index")
406428
assembled[-1] |= num
407429
instruction.pop()
408430

409-
if len(instruction) > 1: # after rel has been removed
410-
if instruction[-1] == "wait":
411-
assembled[-1] |= 0x20
412-
elif instruction[-1] == "clear":
413-
assembled[-1] |= 0x40
414-
# All other values are the default of set without waiting
415431
elif instruction[0] == "set":
416432
# instr delay dst data
417433
assembled.append(0b111_00000_000_00000)

tests/test_version.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,32 @@ def test_dot_set() -> None:
115115

116116

117117
def test_irq_v1() -> None:
118-
assert_assembly_fails("irq 7 next")
119-
assert_assembles_to(".pio_version 1\nirq 5 next", [0b110_00000_0_0_0_11_101])
120-
assert_assembles_to(".pio_version 1\nirq wait 1 prev", [0b110_00000_0_0_1_01_001])
118+
assert_assembly_fails("irq next 7")
119+
assert_assembly_fails(".pio_version 1\nirq next 7 rel")
120+
assert_assembles_to(".pio_version 1\nirq next 5", [0b110_00000_0_0_0_11_101])
121+
assert_assembles_to(".pio_version 1\nirq wait prev 1", [0b110_00000_0_0_1_01_001])
121122

122123

123124
def test_mov_v1() -> None:
124125
assert_assembly_fails("mov osr, rxfifo[y]")
125126
assert_assembly_fails(".pio_version 1\nmov osr, rxfifo[y]")
126127
prefix = ".pio_version 1\n.fifo putget\n"
127128
assert_assembly_fails(prefix + "mov osr, rxfifo[8]")
128-
assert_assembles_to(prefix + "mov rxfifo[y], isr", [0b100_00000_0001_1_000])
129-
assert_assembles_to(prefix + "mov osr, rxfifo[1]", [0b100_00000_1001_0_001])
129+
assert_assembles_to(prefix + "mov rxfifo[y], isr", [0b100_00000_0001_0_000])
130+
assert_assembles_to(prefix + "mov osr, rxfifo[1]", [0b100_00000_1001_1_001])
130131

131132
assert_assembly_fails("mov pindirs, null", errtype=ValueError)
132133
assert_assembles_to(prefix + "mov pindirs, null", [0b101_00000_01100011])
133134

134135

135136
def test_wait_v1() -> None:
136137
assert_assembly_fails("wait 0 jmppin")
137-
assert_assembly_fails("wait 0 irq 5 next")
138+
assert_assembly_fails("wait 0 irq next 5")
138139
prefix = ".pio_version 1\n"
139140
assert_assembly_fails(prefix + "wait 0 jmppin +")
140141
assert_assembly_fails(prefix + "wait 0 jmppin + 7")
141142
assert_assembles_to(prefix + "wait 0 jmppin + 3", [0b001_00000_0_11_00011])
142143
assert_assembles_to(prefix + "wait 1 jmppin", [0b001_00000_1_11_00000])
143144

144-
assert_assembles_to(prefix + "wait 0 irq 5 next", [0b001_00000_0_10_11_101])
145-
assert_assembles_to(prefix + "wait 1 irq 4 prev", [0b001_00000_1_10_01_100])
145+
assert_assembles_to(prefix + "wait 0 irq next 5", [0b001_00000_0_10_11_101])
146+
assert_assembles_to(prefix + "wait 1 irq prev 4", [0b001_00000_1_10_01_100])

0 commit comments

Comments
 (0)