Skip to content

accept 0x, 0b, 0o literals in most places numbers can be used #42

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 1 commit into from
Apr 26, 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
20 changes: 10 additions & 10 deletions adafruit_pioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
raise RuntimeError("Cannot have .wrap as first instruction")
wrap = len(instructions) - 1
elif line.startswith(".side_set"):
sideset_count = int(line.split()[1])
sideset_count = int(line.split()[1], 0)
sideset_enable = "opt" in line
elif line.endswith(":"):
label = line[:-1]
Expand All @@ -88,7 +88,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
instruction = splitter(instruction.strip())
delay = 0
if instruction[-1].endswith("]"): # Delay
delay = int(instruction[-1].strip("[]"))
delay = int(instruction[-1].strip("[]"), 0)
if delay < 0:
raise RuntimeError("Delay negative:", delay)
if delay > max_delay:
Expand All @@ -97,7 +97,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
if len(instruction) > 1 and instruction[-2] == "side":
if sideset_count == 0:
raise RuntimeError("No side_set count set")
sideset_value = int(instruction[-1])
sideset_value = int(instruction[-1], 0)
if sideset_value >= 2**sideset_count:
raise RuntimeError("Sideset value too large")
delay |= sideset_value << (5 - sideset_count - sideset_enable)
Expand All @@ -113,7 +113,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
assembled.append(0b000_00000_000_00000)
target = instruction[-1]
if target[:1] in "0123456789":
assembled[-1] |= int(target)
assembled[-1] |= int(target, 0)
elif instruction[-1] in labels:
assembled[-1] |= labels[target]
else:
Expand All @@ -130,12 +130,12 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
elif instruction[0] == "wait":
# instr delay p sr index
assembled.append(0b001_00000_0_00_00000)
polarity = int(instruction[1])
polarity = int(instruction[1], 0)
if not 0 <= polarity <= 1:
raise RuntimeError("Invalid polarity")
assembled[-1] |= polarity << 7
assembled[-1] |= WAIT_SOURCES.index(instruction[2]) << 5
num = int(instruction[3])
num = int(instruction[3], 0)
if not 0 <= num <= 31:
raise RuntimeError("Wait num out of range")
assembled[-1] |= num
Expand All @@ -145,15 +145,15 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
# instr delay src count
assembled.append(0b010_00000_000_00000)
assembled[-1] |= IN_SOURCES.index(instruction[1]) << 5
count = int(instruction[-1])
count = int(instruction[-1], 0)
if not 1 <= count <= 32:
raise RuntimeError("Count out of range")
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
elif instruction[0] == "out":
# instr delay dst count
assembled.append(0b011_00000_000_00000)
assembled[-1] |= OUT_DESTINATIONS.index(instruction[1]) << 5
count = int(instruction[-1])
count = int(instruction[-1], 0)
if not 1 <= count <= 32:
raise RuntimeError("Count out of range")
assembled[-1] |= count & 0x1F # 32 is 00000 so we mask the top
Expand Down Expand Up @@ -195,7 +195,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
if instruction[-1] == "rel":
assembled[-1] |= 0x10 # Set the high bit of the irq value
instruction.pop()
num = int(instruction[-1])
num = int(instruction[-1], 0)
if not 0 <= num <= 7:
raise RuntimeError("Interrupt index out of range")
assembled[-1] |= num
Expand All @@ -214,7 +214,7 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
raise ValueError(
f"Invalid set destination '{instruction[1]}'"
) from exc
value = int(instruction[-1])
value = int(instruction[-1], 0)
if not 0 <= value <= 31:
raise RuntimeError("Set value out of range")
assembled[-1] |= value
Expand Down
14 changes: 13 additions & 1 deletion tests/testpioasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def testMovNonHappy(self):
def testMovInvert(self):
# test moving and inverting
self.assertAssemblesTo("mov x, ~ x", [0b101_00000_001_01_001])
self.assertAssemblesTo("mov x, ~ x", [0b101_00000_001_01_001])
self.assertAssemblesTo("mov x, ~x", [0b101_00000_001_01_001])
self.assertAssemblesTo("mov x, !x", [0b101_00000_001_01_001])

Expand All @@ -147,3 +146,16 @@ def testWrap(self):
wrap=2,
wrap_target=1,
)


class TestRadix(AssembleChecks):
def testOctal(self):
self.assertAssemblesTo(".side_set 0o1\nset x, 0o11", [0b111_00000_001_01001])

def testBinary(self):
self.assertAssemblesTo(
".side_set 0b101\nnop side 0b10001", [0b101_10001_010_00_010]
)

def testHex(self):
self.assertAssemblesTo(".side_set 0x0\nnop [0x10]", [0b101_10000_010_00_010])