Files
lab-rv32i-asm-addresses-memory/tests/reference_model.py
T
2026-07-21 17:14:02 +02:00

34 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
def task01():
pc, imm20 = 0x100, 0x12345
upper = imm20 << 12
assert upper == 0x12345000 and (pc + upper) & 0xFFFFFFFF == 0x12345100
print("PASS task01 lui=12345000 auipc=12345100")
def task02():
ram = [0] * 16
ram[0], ram[1] = 0x11223344, 0xAABBCCDD
before = list(ram)
misaligned = (2 & 3) != 0
assert ram[0] == 0x11223344 and ram[1] == 0xAABBCCDD and misaligned and ram == before
print("PASS task02 word0=11223344 word1=aabbccdd misaligned=1")
def task03():
base, immediate = 0x20, 12
address = (base + immediate) & 0xFFFFFFFF
ram = {address: 0xDEADBEEF}
fault = ((base + 2) & 3) != 0
assert address == 0x2C and ram[address] == 0xDEADBEEF and fault
print("PASS task03 address=0000002c load=deadbeef fault=1")
TASKS = {"task01_upper_immediate": task01, "task02_word_ram": task02, "task03_address_memory_path": task03}
if len(sys.argv) != 2 or sys.argv[1] not in TASKS:
raise SystemExit(f"usage: {sys.argv[0]} {'|'.join(TASKS)}")
TASKS[sys.argv[1]]()