Skip to content

Commit 5429dc1

Browse files
committed
accept 0x, 0b, 0o literals in most places numbers can be used
Closes: #30 (though I don't know what octal syntax is accepted by official pioasm, we will accept python-style 0o567 not C-style 0567)
1 parent 871640f commit 5429dc1

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

adafruit_pioasm.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
6969
raise RuntimeError("Cannot have .wrap as first instruction")
7070
wrap = len(instructions) - 1
7171
elif line.startswith(".side_set"):
72-
sideset_count = int(line.split()[1])
72+
sideset_count = int(line.split()[1], 0)
7373
sideset_enable = "opt" in line
7474
elif line.endswith(":"):
7575
label = line[:-1]
@@ -88,7 +88,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
8888
instruction = splitter(instruction.strip())
8989
delay = 0
9090
if instruction[-1].endswith("]"): # Delay
91-
delay = int(instruction[-1].strip("[]"))
91+
delay = int(instruction[-1].strip("[]"), 0)
9292
if delay < 0:
9393
raise RuntimeError("Delay negative:", delay)
9494
if delay > max_delay:
@@ -97,7 +97,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
9797
if len(instruction) > 1 and instruction[-2] == "side":
9898
if sideset_count == 0:
9999
raise RuntimeError("No side_set count set")
100-
sideset_value = int(instruction[-1])
100+
sideset_value = int(instruction[-1], 0)
101101
if sideset_value >= 2**sideset_count:
102102
raise RuntimeError("Sideset value too large")
103103
delay |= sideset_value << (5 - sideset_count - sideset_enable)
@@ -113,7 +113,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
113113
assembled.append(0b000_00000_000_00000)
114114
target = instruction[-1]
115115
if target[:1] in "0123456789":
116-
assembled[-1] |= int(target)
116+
assembled[-1] |= int(target, 0)
117117
elif instruction[-1] in labels:
118118
assembled[-1] |= labels[target]
119119
else:
@@ -130,12 +130,12 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
130130
elif instruction[0] == "wait":
131131
# instr delay p sr index
132132
assembled.append(0b001_00000_0_00_00000)
133-
polarity = int(instruction[1])
133+
polarity = int(instruction[1], 0)
134134
if not 0 <= polarity <= 1:
135135
raise RuntimeError("Invalid polarity")
136136
assembled[-1] |= polarity << 7
137137
assembled[-1] |= WAIT_SOURCES.index(instruction[2]) << 5
138-
num = int(instruction[3])
138+
num = int(instruction[3], 0)
139139
if not 0 <= num <= 31:
140140
raise RuntimeError("Wait num out of range")
141141
assembled[-1] |= num
@@ -145,15 +145,15 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
145145
# instr delay src count
146146
assembled.append(0b010_00000_000_00000)
147147
assembled[-1] |= IN_SOURCES.index(instruction[1]) << 5
148-
count = int(instruction[-1])
148+
count = int(instruction[-1], 0)
149149
if not 1 <= count <= 32:
150150
raise RuntimeError("Count out of range")
151151
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
152152
elif instruction[0] == "out":
153153
# instr delay dst count
154154
assembled.append(0b011_00000_000_00000)
155155
assembled[-1] |= OUT_DESTINATIONS.index(instruction[1]) << 5
156-
count = int(instruction[-1])
156+
count = int(instruction[-1], 0)
157157
if not 1 <= count <= 32:
158158
raise RuntimeError("Count out of range")
159159
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
@@ -195,7 +195,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
195195
if instruction[-1] == "rel":
196196
assembled[-1] |= 0x10 # Set the high bit of the irq value
197197
instruction.pop()
198-
num = int(instruction[-1])
198+
num = int(instruction[-1], 0)
199199
if not 0 <= num <= 7:
200200
raise RuntimeError("Interrupt index out of range")
201201
assembled[-1] |= num
@@ -214,7 +214,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
214214
raise ValueError(
215215
f"Invalid set destination '{instruction[1]}'"
216216
) from exc
217-
value = int(instruction[-1])
217+
value = int(instruction[-1], 0)
218218
if not 0 <= value <= 31:
219219
raise RuntimeError("Set value out of range")
220220
assembled[-1] |= value

tests/testpioasm.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def testMovNonHappy(self):
128128
def testMovInvert(self):
129129
# test moving and inverting
130130
self.assertAssemblesTo("mov x, ~ x", [0b101_00000_001_01_001])
131-
self.assertAssemblesTo("mov x, ~ x", [0b101_00000_001_01_001])
132131
self.assertAssemblesTo("mov x, ~x", [0b101_00000_001_01_001])
133132
self.assertAssemblesTo("mov x, !x", [0b101_00000_001_01_001])
134133

@@ -147,3 +146,16 @@ def testWrap(self):
147146
wrap=2,
148147
wrap_target=1,
149148
)
149+
150+
151+
class TestRadix(AssembleChecks):
152+
def testOctal(self):
153+
self.assertAssemblesTo(".side_set 0o1\nset x, 0o11", [0b111_00000_001_01001])
154+
155+
def testBinary(self):
156+
self.assertAssemblesTo(
157+
".side_set 0b101\nnop side 0b10001", [0b101_10001_010_00_010]
158+
)
159+
160+
def testHex(self):
161+
self.assertAssemblesTo(".side_set 0x0\nnop [0x10]", [0b101_10000_010_00_010])

0 commit comments

Comments
 (0)