Skip to content

Add support for .wrap / .wrap_target pseudo instructions #35

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

Merged
merged 5 commits into from
Apr 6, 2022
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
11 changes: 11 additions & 0 deletions LICENSES/BSD-3-Clause.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (c) <year> <owner>.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 21 additions & 6 deletions adafruit_pioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
instructions = []
sideset_count = 0
sideset_enable = 0
wrap = None
wrap_target = None
for i, line in enumerate(text_program.split("\n")):
line = line.strip()
if not line:
Expand All @@ -61,13 +63,14 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
raise RuntimeError("Multiple programs not supported")
program_name = line.split()[1]
elif line.startswith(".wrap_target"):
if len(instructions) > 0:
raise RuntimeError("wrap_target not supported")
wrap_target = len(instructions)
elif line.startswith(".wrap"):
pass
if len(instructions) == 0:
raise RuntimeError("Cannot have .wrap as first instruction")
wrap = len(instructions) - 1
elif line.startswith(".side_set"):
sideset_count = int(line.split()[1])
sideset_enable = 1 if "opt" in line else 0
sideset_enable = "opt" in line
elif line.endswith(":"):
label = line[:-1]
if label in labels:
Expand Down Expand Up @@ -221,10 +224,17 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
# print(bin(assembled[-1]))

self.pio_kwargs = {
"sideset_pin_count": sideset_count,
"sideset_enable": sideset_enable,
}

if sideset_count != 0:
self.pio_kwargs["sideset_pin_count"] = sideset_count

if wrap is not None:
self.pio_kwargs["wrap"] = wrap
if wrap_target is not None:
self.pio_kwargs["wrap_target"] = wrap_target

self.assembled = array.array("H", assembled)

if build_debuginfo:
Expand All @@ -242,8 +252,13 @@ def print_c_program(self, name, qualifier="const"):
program_lines = self.debuginfo[1].split("\n")

print(
f"{qualifier} int {name}_sideset_pin_count = {self.pio_kwargs['sideset_pin_count']};"
f"{qualifier} int {name}_wrap = {self.pio_kwargs.get('wrap', len(self.assembled)-1)};"
)
print(
f"{qualifier} int {name}_wrap_target = {self.pio_kwargs.get('wrap_target', 0)};"
)
sideset_pin_count = self.pio_kwargs.get("sideset_pin_count", 0)
print(f"{qualifier} int {name}_sideset_pin_count = {sideset_pin_count};")
print(
f"{qualifier} bool {name}_sideset_enable = {self.pio_kwargs['sideset_enable']};"
)
Expand Down
17 changes: 17 additions & 0 deletions examples/pioasm_wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: 2022 Jeff Epler, written for Adafruit Industries
# SPDF-FileCopyrightText: 2020 Raspberry Pi (Trading) Ltd.
#
# SPDX-License-Identifier: BSD-3-Clause
import adafruit_pioasm

program = adafruit_pioasm.Program(
"""
set pindirs, 1
.wrap_target
set pins, 0
set pins, 1
.wrap""",
build_debuginfo=True,
)

program.print_c_program("test")
Empty file added tests/__init__.py
Empty file.
13 changes: 12 additions & 1 deletion tests/testpioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def testLimits(self):
self.assertAssemblyFails(".side_set 1 opt\nnop side 0 [8]")

def testCls(self):
self.assertPioKwargs("", sideset_pin_count=0, sideset_enable=False)
self.assertPioKwargs("", sideset_enable=False)
self.assertPioKwargs(".side_set 1", sideset_pin_count=1, sideset_enable=False)
self.assertPioKwargs(
".side_set 3 opt", sideset_pin_count=3, sideset_enable=True
Expand All @@ -136,3 +136,14 @@ def testMovReverse(self):
# test moving and reversing bits
self.assertAssemblesTo("mov x, :: x", [0b101_00000_001_10_001])
self.assertAssemblesTo("mov x, ::x", [0b101_00000_001_10_001])


class TestWrap(AssembleChecks):
def testWrap(self):
self.assertAssemblyFails(".wrap")
self.assertPioKwargs(
"nop\n.wrap_target\nnop\nnop\n.wrap",
sideset_enable=False,
wrap=2,
wrap_target=1,
)