diff --git a/LICENSES/BSD-3-Clause.txt b/LICENSES/BSD-3-Clause.txt new file mode 100644 index 0000000..086d399 --- /dev/null +++ b/LICENSES/BSD-3-Clause.txt @@ -0,0 +1,11 @@ +Copyright (c) . + +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. diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index 1237f13..fe7a036 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -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: @@ -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: @@ -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: @@ -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']};" ) diff --git a/examples/pioasm_wrap.py b/examples/pioasm_wrap.py new file mode 100644 index 0000000..3796a18 --- /dev/null +++ b/examples/pioasm_wrap.py @@ -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") diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/testpioasm.py b/tests/testpioasm.py index 629a780..3667e51 100644 --- a/tests/testpioasm.py +++ b/tests/testpioasm.py @@ -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 @@ -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, + )