|
| 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