34 lines
1.3 KiB
Python
Executable File
34 lines
1.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
|
|
|
|
def u32(value: int) -> int: return value & 0xFFFFFFFF
|
|
def s32(value: int) -> int: value &= 0xFFFFFFFF; return value - 0x100000000 if value & 0x80000000 else value
|
|
|
|
|
|
def task01() -> None:
|
|
a, b = 0x80000000, 1
|
|
results = [u32(7 + 5), u32(7 - 5), 0xA5 & 0x3C, 0xA5 | 0x3C, 0xA5 ^ 0x3C,
|
|
u32(1 << 4), 0x80000000 >> 4, u32(s32(0x80000000) >> 4), int(s32(a) < s32(b)), int(a < b)]
|
|
assert results == [12, 2, 0x24, 0xBD, 0x99, 16, 0x08000000, 0xF8000000, 1, 0]
|
|
print("PASS task01 ops=10 signed_lt=1 unsigned_lt=0 sra=f8000000")
|
|
|
|
|
|
def task02() -> None:
|
|
add = (2 << 20) | (1 << 15) | (3 << 7) | 0x33
|
|
addi = (0xFFC << 20) | (6 << 15) | (5 << 7) | 0x13
|
|
assert (add, addi) == (0x002081B3, 0xFFC30293)
|
|
print("PASS task02 add=002081b3 addi_neg4=ffc30293")
|
|
|
|
|
|
def task03() -> None:
|
|
regs = [0] * 32
|
|
regs[1] = 7; regs[2] = 5; regs[3] = regs[1] + regs[2]; regs[4] = regs[3] - regs[2]
|
|
assert regs[1:5] == [7, 5, 12, 7]
|
|
print("PASS task03 instructions=4 x1=7 x2=5 x3=12 x4=7")
|
|
|
|
|
|
TASKS = {"task01_alu_core": task01, "task02_verilog_assembler": task02, "task03_execute_stage": 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]]()
|