Skip to content

Commit 41a7218

Browse files
authored
Merge pull request #63 from kbsriram/add-out-tests
Add tests for "out" and "in".
2 parents fcc52f3 + d87b446 commit 41a7218

File tree

3 files changed

+152
-2
lines changed

3 files changed

+152
-2
lines changed

Diff for: adafruit_pioasm.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,25 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
147147
elif instruction[0] == "in":
148148
# instr delay src count
149149
assembled.append(0b010_00000_000_00000)
150-
assembled[-1] |= IN_SOURCES.index(instruction[1]) << 5
150+
source = instruction[1]
151+
try:
152+
assembled[-1] |= IN_SOURCES.index(source) << 5
153+
except ValueError as exc:
154+
raise ValueError(f"Invalid in source '{source}'") from exc
151155
count = int(instruction[-1], 0)
152156
if not 1 <= count <= 32:
153157
raise RuntimeError("Count out of range")
154158
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
155159
elif instruction[0] == "out":
156160
# instr delay dst count
157161
assembled.append(0b011_00000_000_00000)
158-
assembled[-1] |= OUT_DESTINATIONS.index(instruction[1]) << 5
162+
destination = instruction[1]
163+
try:
164+
assembled[-1] |= OUT_DESTINATIONS.index(destination) << 5
165+
except ValueError as exc:
166+
raise ValueError(
167+
f"Invalid out destination '{destination}'"
168+
) from exc
159169
count = int(instruction[-1], 0)
160170
if not 1 <= count <= 32:
161171
raise RuntimeError("Count out of range")

Diff for: tests/test_in.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: KB Sriram
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests in
7+
"""
8+
9+
import pytest
10+
from pytest_helpers import assert_assembles_to, assert_assembly_fails
11+
12+
13+
@pytest.mark.parametrize(
14+
"source,expected",
15+
[
16+
("pins", 0b000),
17+
("x", 0b001),
18+
("y", 0b010),
19+
("null", 0b011),
20+
("isr", 0b110),
21+
("osr", 0b111),
22+
],
23+
)
24+
def test_in_sources(source: str, expected: int) -> None:
25+
# delay src bitcount
26+
encoding = 0b010_00000_000_10001
27+
# add in the expected source
28+
encoding |= expected << 5
29+
assert_assembles_to(f"in {source}, 17", [encoding])
30+
31+
32+
@pytest.mark.parametrize("delay", [0, 1, 9, 17, 31])
33+
def test_in_delay(delay: int) -> None:
34+
# delay src bitcount
35+
encoding = 0b010_00000_000_10001
36+
# add in the expected delay
37+
encoding |= delay << 8
38+
assert_assembles_to(f"in pins, 17 [{delay}]", [encoding])
39+
40+
41+
@pytest.mark.parametrize("bitcount", [1, 9, 17, 32])
42+
def test_in_bitcount(bitcount: int) -> None:
43+
# delay dst bitcount
44+
encoding = 0b010_00000_000_00000
45+
# add in the expected bitcount. Note that
46+
# 32 should be encoded as 0, which we do by
47+
# masking the bitcount with 0x1f
48+
encoding |= bitcount & 0x1F
49+
assert_assembles_to(f"in pins, {bitcount}", [encoding])
50+
51+
52+
def test_in_delay_with_sideset() -> None:
53+
source = [
54+
".side_set 2",
55+
"in pins 17 side 2 [5]",
56+
]
57+
assert_assembles_to("\n".join(source), [0b010_10_101_000_10001])
58+
59+
60+
def test_in_bad_source():
61+
assert_assembly_fails(
62+
"in bad, 17", match="Invalid in source 'bad'", errtype=ValueError
63+
)
64+
65+
66+
def test_in_bad_bitcount():
67+
assert_assembly_fails(
68+
"in pins, 0", match="Count out of range", errtype=RuntimeError
69+
)

Diff for: tests/test_out.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# SPDX-FileCopyrightText: KB Sriram
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
Tests out
7+
"""
8+
9+
import pytest
10+
from pytest_helpers import assert_assembles_to, assert_assembly_fails
11+
12+
13+
@pytest.mark.parametrize(
14+
"destination,expected",
15+
[
16+
("pins", 0b000),
17+
("x", 0b001),
18+
("y", 0b010),
19+
("null", 0b011),
20+
("pindirs", 0b100),
21+
("pc", 0b101),
22+
("isr", 0b110),
23+
("exec", 0b111),
24+
],
25+
)
26+
def test_out_destinations(destination: str, expected: int) -> None:
27+
# delay dst bitcount
28+
encoding = 0b011_00000_000_10001
29+
# add in the expected destination
30+
encoding |= expected << 5
31+
assert_assembles_to(f"out {destination}, 17", [encoding])
32+
33+
34+
@pytest.mark.parametrize("delay", [0, 1, 9, 17, 31])
35+
def test_out_delay(delay: int) -> None:
36+
# delay dst bitcount
37+
encoding = 0b011_00000_000_10001
38+
# add in the expected delay
39+
encoding |= delay << 8
40+
assert_assembles_to(f"out pins, 17 [{delay}]", [encoding])
41+
42+
43+
@pytest.mark.parametrize("bitcount", [1, 9, 17, 32])
44+
def test_out_bitcount(bitcount: int) -> None:
45+
# delay dst bitcount
46+
encoding = 0b011_00000_000_00000
47+
# add in the expected bitcount. Note that
48+
# 32 should be encoded as 0, which we do by
49+
# masking the bitcount with 0x1f
50+
encoding |= bitcount & 0x1F
51+
assert_assembles_to(f"out pins, {bitcount}", [encoding])
52+
53+
54+
def test_out_delay_with_sideset() -> None:
55+
source = [
56+
".side_set 2",
57+
"out pins 17 side 2 [5]",
58+
]
59+
assert_assembles_to("\n".join(source), [0b011_10_101_000_10001])
60+
61+
62+
def test_out_bad_destination():
63+
assert_assembly_fails(
64+
"out bad, 17", match="Invalid out destination 'bad'", errtype=ValueError
65+
)
66+
67+
68+
def test_out_bad_bitcount():
69+
assert_assembly_fails(
70+
"out pins, 0", match="Count out of range", errtype=RuntimeError
71+
)

0 commit comments

Comments
 (0)