Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for "out" and "in". #63

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions adafruit_pioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,25 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
elif instruction[0] == "in":
# instr delay src count
assembled.append(0b010_00000_000_00000)
assembled[-1] |= IN_SOURCES.index(instruction[1]) << 5
source = instruction[1]
try:
assembled[-1] |= IN_SOURCES.index(source) << 5
except ValueError as exc:
raise ValueError(f"Invalid in source '{source}'") from exc
count = int(instruction[-1], 0)
if not 1 <= count <= 32:
raise RuntimeError("Count out of range")
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
elif instruction[0] == "out":
# instr delay dst count
assembled.append(0b011_00000_000_00000)
assembled[-1] |= OUT_DESTINATIONS.index(instruction[1]) << 5
destination = instruction[1]
try:
assembled[-1] |= OUT_DESTINATIONS.index(destination) << 5
except ValueError as exc:
raise ValueError(
f"Invalid out destination '{destination}'"
) from exc
count = int(instruction[-1], 0)
if not 1 <= count <= 32:
raise RuntimeError("Count out of range")
Expand Down
69 changes: 69 additions & 0 deletions tests/test_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# SPDX-FileCopyrightText: KB Sriram
#
# SPDX-License-Identifier: MIT

"""
Tests in
"""

import pytest
from pytest_helpers import assert_assembles_to, assert_assembly_fails


@pytest.mark.parametrize(
"source,expected",
[
("pins", 0b000),
("x", 0b001),
("y", 0b010),
("null", 0b011),
("isr", 0b110),
("osr", 0b111),
],
)
def test_in_sources(source: str, expected: int) -> None:
# delay src bitcount
encoding = 0b010_00000_000_10001
# add in the expected source
encoding |= expected << 5
assert_assembles_to(f"in {source}, 17", [encoding])


@pytest.mark.parametrize("delay", [0, 1, 9, 17, 31])
def test_in_delay(delay: int) -> None:
# delay src bitcount
encoding = 0b010_00000_000_10001
# add in the expected delay
encoding |= delay << 8
assert_assembles_to(f"in pins, 17 [{delay}]", [encoding])


@pytest.mark.parametrize("bitcount", [1, 9, 17, 32])
def test_in_bitcount(bitcount: int) -> None:
# delay dst bitcount
encoding = 0b010_00000_000_00000
# add in the expected bitcount. Note that
# 32 should be encoded as 0, which we do by
# masking the bitcount with 0x1f
encoding |= bitcount & 0x1F
assert_assembles_to(f"in pins, {bitcount}", [encoding])


def test_in_delay_with_sideset() -> None:
source = [
".side_set 2",
"in pins 17 side 2 [5]",
]
assert_assembles_to("\n".join(source), [0b010_10_101_000_10001])


def test_in_bad_source():
assert_assembly_fails(
"in bad, 17", match="Invalid in source 'bad'", errtype=ValueError
)


def test_in_bad_bitcount():
assert_assembly_fails(
"in pins, 0", match="Count out of range", errtype=RuntimeError
)
71 changes: 71 additions & 0 deletions tests/test_out.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# SPDX-FileCopyrightText: KB Sriram
#
# SPDX-License-Identifier: MIT

"""
Tests out
"""

import pytest
from pytest_helpers import assert_assembles_to, assert_assembly_fails


@pytest.mark.parametrize(
"destination,expected",
[
("pins", 0b000),
("x", 0b001),
("y", 0b010),
("null", 0b011),
("pindirs", 0b100),
("pc", 0b101),
("isr", 0b110),
("exec", 0b111),
],
)
def test_out_destinations(destination: str, expected: int) -> None:
# delay dst bitcount
encoding = 0b011_00000_000_10001
# add in the expected destination
encoding |= expected << 5
assert_assembles_to(f"out {destination}, 17", [encoding])


@pytest.mark.parametrize("delay", [0, 1, 9, 17, 31])
def test_out_delay(delay: int) -> None:
# delay dst bitcount
encoding = 0b011_00000_000_10001
# add in the expected delay
encoding |= delay << 8
assert_assembles_to(f"out pins, 17 [{delay}]", [encoding])


@pytest.mark.parametrize("bitcount", [1, 9, 17, 32])
def test_out_bitcount(bitcount: int) -> None:
# delay dst bitcount
encoding = 0b011_00000_000_00000
# add in the expected bitcount. Note that
# 32 should be encoded as 0, which we do by
# masking the bitcount with 0x1f
encoding |= bitcount & 0x1F
assert_assembles_to(f"out pins, {bitcount}", [encoding])


def test_out_delay_with_sideset() -> None:
source = [
".side_set 2",
"out pins 17 side 2 [5]",
]
assert_assembles_to("\n".join(source), [0b011_10_101_000_10001])


def test_out_bad_destination():
assert_assembly_fails(
"out bad, 17", match="Invalid out destination 'bad'", errtype=ValueError
)


def test_out_bad_bitcount():
assert_assembly_fails(
"out pins, 0", match="Count out of range", errtype=RuntimeError
)
Loading