Skip to content

Commit 895f8eb

Browse files
committed
add mov rxfifo
1 parent 4f98480 commit 895f8eb

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

adafruit_pioasm.py

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ def int_in_range(arg, low, high, what, radix=0):
9292
f"{what} must be at least {low} and no greater than {high}, got {result}"
9393
)
9494

95+
def parse_rxfifo_brackets(arg, fifo_dir):
96+
require_version(1, line)
97+
if ( # pylint: disable=consider-using-in
98+
fifo_type != "putget" and fifo_type != fifo_dir
99+
):
100+
raise RuntimeError(
101+
f"FIFO must be configured for '{fifo_dir}' or 'putget' for {line}"
102+
)
103+
if arg.endswith("[y]"):
104+
return 0b1000
105+
return int_in_range(arg[7:-1], 0, 8, "rxfifo index")
106+
95107
for i, line in enumerate(text_program.split("\n")):
96108
line = line.split(";")[0].strip()
97109
if not line:
@@ -212,7 +224,11 @@ def int_in_range(arg, low, high, what, radix=0):
212224
for line in instructions:
213225
instruction = splitter(line.strip())
214226
delay = 0
215-
if len(instruction) > 1 and instruction[-1].endswith("]"): # Delay
227+
if (
228+
len(instruction) > 1
229+
and instruction[-1].startswith("[")
230+
and instruction[-1].endswith("]")
231+
): # Delay
216232
delay = int(instruction[-1].strip("[]"), 0)
217233
if delay < 0:
218234
raise RuntimeError("Delay negative:", delay)
@@ -303,33 +319,43 @@ def int_in_range(arg, low, high, what, radix=0):
303319
assembled[-1] |= 0x40
304320
elif instruction[0] == "mov":
305321
# instr delay dst op src
306-
assembled.append(0b101_00000_000_00_000)
307-
assembled[-1] |= MOV_DESTINATIONS.index(instruction[1]) << 5
308-
source = instruction[-1]
309-
source_split = mov_splitter(source)
310-
if len(source_split) == 1:
311-
try:
312-
assembled[-1] |= MOV_SOURCES.index(source)
313-
except ValueError as exc:
314-
raise ValueError(f"Invalid mov source '{source}'") from exc
322+
if instruction[1].startswith("rxfifo["): # mov rxfifo[], isr
323+
assembled.append(0b100_00000_0001_0_000)
324+
if instruction[2] != "isr":
325+
raise ValueError("mov rxfifo[] source must be isr")
326+
assembled[-1] |= parse_rxfifo_brackets(instruction[1], "put")
327+
elif instruction[2].startswith("rxfifo["): # mov osr, rxfifo[]
328+
assembled.append(0b100_00000_1001_0_000)
329+
if instruction[1] != "osr":
330+
raise ValueError("mov ,rxfifo[] target must be osr")
331+
assembled[-1] |= parse_rxfifo_brackets(instruction[2], "get")
315332
else:
316-
assembled[-1] |= MOV_SOURCES.index(source_split[1])
317-
if source[:1] == "!":
318-
assembled[-1] |= 0x08
319-
elif source[:1] == "~":
320-
assembled[-1] |= 0x08
321-
elif source[:2] == "::":
322-
assembled[-1] |= 0x10
333+
assembled.append(0b101_00000_000_00_000)
334+
assembled[-1] |= MOV_DESTINATIONS.index(instruction[1]) << 5
335+
source = instruction[-1]
336+
source_split = mov_splitter(source)
337+
if len(source_split) == 1:
338+
try:
339+
assembled[-1] |= MOV_SOURCES.index(source)
340+
except ValueError as exc:
341+
raise ValueError(f"Invalid mov source '{source}'") from exc
323342
else:
324-
raise RuntimeError("Invalid mov operator:", source[:1])
325-
if len(instruction) > 3:
326-
assembled[-1] |= MOV_OPS.index(instruction[-2]) << 3
343+
assembled[-1] |= MOV_SOURCES.index(source_split[1])
344+
if source[:1] == "!":
345+
assembled[-1] |= 0x08
346+
elif source[:1] == "~":
347+
assembled[-1] |= 0x08
348+
elif source[:2] == "::":
349+
assembled[-1] |= 0x10
350+
else:
351+
raise RuntimeError("Invalid mov operator:", source[:1])
352+
if len(instruction) > 3:
353+
assembled[-1] |= MOV_OPS.index(instruction[-2]) << 3
327354
elif instruction[0] == "irq":
328355
# instr delay z c w tp/idx
329356
assembled.append(0b110_00000_0_0_0_00000)
330357

331358
irq_type = 0
332-
print(f"check prev/next/rel {instruction=}")
333359
if instruction[-1] == "prev":
334360
irq_type = 1
335361
require_version(1, "irq prev")

tests/test_version.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,12 @@ def test_irq_v1() -> None:
113113
assert_assembly_fails("irq 7 next")
114114
assert_assembles_to(".pio_version 1\nirq 5 next", [0b110_00000_0_0_0_11_101])
115115
assert_assembles_to(".pio_version 1\nirq wait 1 prev", [0b110_00000_0_0_1_01_001])
116+
117+
118+
def test_mov_v1() -> None:
119+
assert_assembly_fails("mov osr, rxfifo[y]")
120+
assert_assembly_fails(".pio_version 1\nmov osr, rxfifo[y]")
121+
prefix = ".pio_version 1\n.fifo putget\n"
122+
assert_assembly_fails(prefix + "mov osr, rxfifo[8]")
123+
assert_assembles_to(prefix + "mov rxfifo[y], isr", [0b100_00000_0001_1_000])
124+
assert_assembles_to(prefix + "mov osr, rxfifo[1]", [0b100_00000_1001_0_001])

0 commit comments

Comments
 (0)