From fde6fccaa053676ce7e3d305a6e42e593f677eb4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Thu, 13 Jan 2022 09:17:51 -0600 Subject: [PATCH 1/4] adding this file lets 'python3 -munittest' in the top directory work --- tests/__init__.py | 0 tests/testpioasm.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 tests/__init__.py 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..64e9de5 100644 --- a/tests/testpioasm.py +++ b/tests/testpioasm.py @@ -136,3 +136,9 @@ 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_count=0, sideset_enable=False, wrap=2, wrap_target=1) From d16bd7e6a4adf7c4ce301b98c2e9805b1e772446 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 13 Feb 2022 13:20:28 -0600 Subject: [PATCH 2/4] Add support for wrap/wrap_target .. This also needs support in the core for specifying them in the StateMachine constructor. --- adafruit_pioasm.py | 22 ++++++++++++++++++---- tests/testpioasm.py | 10 ++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index d63f5c0..e616c79 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: @@ -225,6 +228,11 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None: "sideset_enable": sideset_enable, } + 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: @@ -241,6 +249,12 @@ def print_c_program(self, name, qualifier="const"): linemap = self.debuginfo[0][:] # Use a copy since we destroy it program_lines = self.debuginfo[1].split("\n") + print( + 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)};" + ) print( f"{qualifier} int {name}_sideset_pin_count = {self.pio_kwargs['sideset_pin_count']};" ) diff --git a/tests/testpioasm.py b/tests/testpioasm.py index 64e9de5..ceb7620 100644 --- a/tests/testpioasm.py +++ b/tests/testpioasm.py @@ -137,8 +137,14 @@ def testMovReverse(self): 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_count=0, sideset_enable=False, wrap=2, wrap_target=1) + self.assertPioKwargs( + "nop\n.wrap_target\nnop\nnop\n.wrap", + sideset_pin_count=0, + sideset_enable=False, + wrap=2, + wrap_target=1, + ) From 16adbe4464579c5260880b57938c7150d57bcb52 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 6 Apr 2022 08:41:58 -0500 Subject: [PATCH 3/4] The core doesn't like sideset_pin_count=0, so don't send it --- adafruit_pioasm.py | 9 +++++---- tests/testpioasm.py | 3 +-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_pioasm.py b/adafruit_pioasm.py index e616c79..a336c27 100644 --- a/adafruit_pioasm.py +++ b/adafruit_pioasm.py @@ -224,10 +224,12 @@ 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: @@ -255,9 +257,8 @@ def print_c_program(self, name, qualifier="const"): print( f"{qualifier} int {name}_wrap_target = {self.pio_kwargs.get('wrap_target', 0)};" ) - print( - f"{qualifier} int {name}_sideset_pin_count = {self.pio_kwargs['sideset_pin_count']};" - ) + 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/tests/testpioasm.py b/tests/testpioasm.py index ceb7620..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 @@ -143,7 +143,6 @@ def testWrap(self): self.assertAssemblyFails(".wrap") self.assertPioKwargs( "nop\n.wrap_target\nnop\nnop\n.wrap", - sideset_pin_count=0, sideset_enable=False, wrap=2, wrap_target=1, From 8bd335f4be19eea56e16482cda845f01e24fdc53 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 6 Apr 2022 08:47:49 -0500 Subject: [PATCH 4/4] add a wrap example --- LICENSES/BSD-3-Clause.txt | 11 +++++++++++ examples/pioasm_wrap.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 LICENSES/BSD-3-Clause.txt create mode 100644 examples/pioasm_wrap.py 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/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")