circuitpython/tests/micropython/viper_ptr8_store_boundary_intbig.py
Chris Webb 953da2080e tests/micropython: Test that viper offset stores don't clobber base reg.
When running the viper boundary tests, assert that the offset stores don't
clobber the base register, which is saved and temporarily modified on some
architectures.

Signed-off-by: Chris Webb <chris@arachsys.com>
2025-08-01 14:17:49 +10:00

53 lines
1.3 KiB
Python

# Test boundary conditions for various architectures
SET_TEMPLATE = """
@micropython.viper
def set{off}(dest: ptr8):
saved = dest
dest[{off}] = {val}
assert int(saved) == int(dest)
set{off}(buffer)
print(hex(get_index(buffer, {off})))
"""
BIT_THRESHOLDS = (5, 8, 11, 12)
SIZE = 1
MASK = (1 << (8 * SIZE)) - 1
@micropython.viper
def set_index(dest: ptr8, i: int, val: uint):
saved = dest
dest[i] = val
assert int(dest) == int(saved)
def get_index(src: ptr8, i: int):
return src[i]
buffer = bytearray(((1 << max(BIT_THRESHOLDS) + 1) // 1024) * 1024)
next = 1
val = 0
for bit in BIT_THRESHOLDS:
print("---", bit)
pre, idx, post = (((1 << bit) - (2 * SIZE)), ((1 << bit) - (1 * SIZE)), (1 << bit))
val = (val << 8) + next
next += 1
set_index(buffer, pre, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, idx, val & MASK)
val = (val << 8) + next
next += 1
set_index(buffer, post, val & MASK)
val = (val << 8) + next
next += 1
print(hex(get_index(buffer, pre)), hex(get_index(buffer, idx)), hex(get_index(buffer, post)))
exec(SET_TEMPLATE.format(off=pre, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=idx, val=val & MASK))
val = (val << 8) + next
next += 1
exec(SET_TEMPLATE.format(off=post, val=val & MASK))