Skip to content

Commit 55c235d

Browse files
authored
Merge pull request #35 from jepler/wrap
Add support for .wrap / .wrap_target pseudo instructions
2 parents 5aa37e1 + 8bd335f commit 55c235d

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

LICENSES/BSD-3-Clause.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright (c) <year> <owner>.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
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.
8+
9+
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.
10+
11+
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.

adafruit_pioasm.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
5050
instructions = []
5151
sideset_count = 0
5252
sideset_enable = 0
53+
wrap = None
54+
wrap_target = None
5355
for i, line in enumerate(text_program.split("\n")):
5456
line = line.strip()
5557
if not line:
@@ -61,13 +63,14 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
6163
raise RuntimeError("Multiple programs not supported")
6264
program_name = line.split()[1]
6365
elif line.startswith(".wrap_target"):
64-
if len(instructions) > 0:
65-
raise RuntimeError("wrap_target not supported")
66+
wrap_target = len(instructions)
6667
elif line.startswith(".wrap"):
67-
pass
68+
if len(instructions) == 0:
69+
raise RuntimeError("Cannot have .wrap as first instruction")
70+
wrap = len(instructions) - 1
6871
elif line.startswith(".side_set"):
6972
sideset_count = int(line.split()[1])
70-
sideset_enable = 1 if "opt" in line else 0
73+
sideset_enable = "opt" in line
7174
elif line.endswith(":"):
7275
label = line[:-1]
7376
if label in labels:
@@ -221,10 +224,17 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
221224
# print(bin(assembled[-1]))
222225

223226
self.pio_kwargs = {
224-
"sideset_pin_count": sideset_count,
225227
"sideset_enable": sideset_enable,
226228
}
227229

230+
if sideset_count != 0:
231+
self.pio_kwargs["sideset_pin_count"] = sideset_count
232+
233+
if wrap is not None:
234+
self.pio_kwargs["wrap"] = wrap
235+
if wrap_target is not None:
236+
self.pio_kwargs["wrap_target"] = wrap_target
237+
228238
self.assembled = array.array("H", assembled)
229239

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

244254
print(
245-
f"{qualifier} int {name}_sideset_pin_count = {self.pio_kwargs['sideset_pin_count']};"
255+
f"{qualifier} int {name}_wrap = {self.pio_kwargs.get('wrap', len(self.assembled)-1)};"
256+
)
257+
print(
258+
f"{qualifier} int {name}_wrap_target = {self.pio_kwargs.get('wrap_target', 0)};"
246259
)
260+
sideset_pin_count = self.pio_kwargs.get("sideset_pin_count", 0)
261+
print(f"{qualifier} int {name}_sideset_pin_count = {sideset_pin_count};")
247262
print(
248263
f"{qualifier} bool {name}_sideset_enable = {self.pio_kwargs['sideset_enable']};"
249264
)

examples/pioasm_wrap.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2022 Jeff Epler, written for Adafruit Industries
2+
# SPDF-FileCopyrightText: 2020 Raspberry Pi (Trading) Ltd.
3+
#
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
import adafruit_pioasm
6+
7+
program = adafruit_pioasm.Program(
8+
"""
9+
set pindirs, 1
10+
.wrap_target
11+
set pins, 0
12+
set pins, 1
13+
.wrap""",
14+
build_debuginfo=True,
15+
)
16+
17+
program.print_c_program("test")

tests/__init__.py

Whitespace-only changes.

tests/testpioasm.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def testLimits(self):
111111
self.assertAssemblyFails(".side_set 1 opt\nnop side 0 [8]")
112112

113113
def testCls(self):
114-
self.assertPioKwargs("", sideset_pin_count=0, sideset_enable=False)
114+
self.assertPioKwargs("", sideset_enable=False)
115115
self.assertPioKwargs(".side_set 1", sideset_pin_count=1, sideset_enable=False)
116116
self.assertPioKwargs(
117117
".side_set 3 opt", sideset_pin_count=3, sideset_enable=True
@@ -136,3 +136,14 @@ def testMovReverse(self):
136136
# test moving and reversing bits
137137
self.assertAssemblesTo("mov x, :: x", [0b101_00000_001_10_001])
138138
self.assertAssemblesTo("mov x, ::x", [0b101_00000_001_10_001])
139+
140+
141+
class TestWrap(AssembleChecks):
142+
def testWrap(self):
143+
self.assertAssemblyFails(".wrap")
144+
self.assertPioKwargs(
145+
"nop\n.wrap_target\nnop\nnop\n.wrap",
146+
sideset_enable=False,
147+
wrap=2,
148+
wrap_target=1,
149+
)

0 commit comments

Comments
 (0)