Files
Basys3RiscV/monitor/test-asm.py
T
brad ArantandClaude Opus 5.5 71fd403294 Add a single-precision FPU (the F extension)
rtl/rv32_fpu.v: IEEE 754 single precision with all five rounding modes,
subnormals, the exception flags and the canonical NaN. Multiply-add
(DSPs, exact product, 77-bit sum) with add, subtract and multiply as its
special cases; divide and square root a bit a clock; one normalise-and-
round back end. 12 clocks for add/mul/fma, 36 for div/sqrt. About 3000
LUTs and 2 DSPs; the system still meets 90 MHz.

rv32_cpu.v: float registers, FLW/FSW, OP-FP and the R4 instructions,
fflags/frm/fcsr, mstatus.FS (Initial at reset), misa F.

tools/f32.py is an exact model (checked against x86 hardware) used by
scripts/fputest.sh (random vectors, every instruction and rounding mode)
and by tools/rv32emu.py, which now runs RV32IMF. cputest.sh adds rv32uf.

BASIC and the games are built for rv32imf/ilp32f (BASIC 36.4 -> 28.7 KB,
SIN about twice as fast); setjmp saves fs0-fs11; sqrtf is FSQRT.S. The
monitor saves and restores f0-f31 and fcsr, shows them (F), sets them
(R F1=...), and its disassembler and assembler know every F instruction
(checked against objdump and GNU as). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 20:29:08 -08:00

167 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""Assemble a test program with the monitor's assembler (out/monitor-host)
and with GNU as, and check the bytes are the same.
python3 test-asm.py
"""
import os
import re
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
# the monitor's syntax; to_gnu() below turns it into GNU as's
SRC = r"""
ORG 12000H
COUNT EQU 10
START: LA A1,MSG ; the message
LI A2,COUNT
LI A3,BIG ; defined further on: LUI and ADDI
LI T0,-2048
LI T1,2047
LOOP: LBU A0,0(A1)
BEQZ A0,DONE
WAIT: LBU T0,-256(ZERO)
ANDI T0,T0,2
BEQZ T0,WAIT
SB A0,-255(ZERO)
ADDI A1,A1,1
J LOOP
DONE: ADDI A2,A2,-1
BNEZ A2,START
MUL A4,A2,A3
DIVU A5,A4,A3
REMU A6,A4,A3
SLLI A7,A6,3
SRAI S2,A7,1
SUB S3,S2,A7
NEG S4,S3
NOT S5,S4
SEQZ S6,S5
SNEZ S7,S5
BGT S6,S7,DONE
BLEU S6,S7,LOOP
BGTZ S6,LOOP
BLEZ S6,LOOP
JAL RA,DONE
JAL DONE
JALR T0
JALR RA,4(T0)
JR T0
CSRR A0,MSTATUS
CSRW MSCRATCH,A0
CSRSI MSTATUS,8
CSRRW A1,0x305,A2
LUI S8,0x12345
AUIPC S9,1
SW S9,-4(SP)
SH S9,2(SP)
LH S10,2(SP)
LHU S11,2(SP)
LW S11,0(SP)
ECALL
EBREAK
MRET
WFI
FENCE
FENCE.I
NOP
FLW FA0,8(SP)
FSW F31,-12(A0)
FADD.S FA0,FA1,FA2
FSUB.S F1,F2,F3,RTZ
FMUL.S FT0,FT11,FS0
FDIV.S FS11,FS1,FS2,RMM
FSQRT.S F4,F5,DYN
FMADD.S F1,F2,F3,F4
FNMSUB.S F1,F2,F3,F4,RUP
FSGNJX.S F6,F7,F8
FMV.S F9,F10
FNEG.S F9,F10
FABS.S F9,F10
FMIN.S F11,F12,F13
FMAX.S F11,F12,F13
FEQ.S A0,F1,F2
FLT.S A1,F1,F2
FLE.S A2,F1,F2
FCVT.W.S A3,F4,RTZ
FCVT.WU.S A4,F4
FCVT.S.W F5,A5
FCVT.S.WU F6,A6,RDN
FMV.X.W T0,F7
FMV.W.X F8,T1
FCLASS.S T2,F9
CSRR A0,FCSR
CSRW FRM,A1
RET
MSG: DB "HI",13,10,0
ALIGN 4
TAB: DW START,DONE,0x12345678
DH 1234,0xFFFF
DS 6
DB 'A'
BIG EQU 0x12345
"""
def to_gnu(src):
out = [".text", ".option norelax", ".option norvc", "_start:"]
for line in src.strip().splitlines():
line = line.split(";")[0]
line = line.replace("$", ".")
m = re.match(r"(\w+)\s+EQU\s+(.*)", line.strip())
if m:
out.append(".equ %s, %s" % (m.group(1).lower(), m.group(2).lower()))
continue
if line.strip().startswith("ORG"):
continue
line = re.sub(r'\bDB "(\w+)",', r'.ascii "\1"\n .byte ', line)
line = re.sub(r"\bDB\b", ".byte", line)
line = re.sub(r"\bDH\b", ".half", line)
line = re.sub(r"\bDW\b", ".word", line)
line = re.sub(r"\bDS\s+(\d+)", r".skip \1", line)
line = re.sub(r"\bALIGN (\d+)", r".balign \1, 0", line)
line = re.sub(r"\bJAL DONE\b", "jal ra,DONE", line)
line = re.sub(r"\bJALR T0\b", "jalr ra,0(t0)", line)
line = re.sub(r"\bLI A3,BIG\b", "lui a3,%hi(BIG)\n addi a3,a3,%lo(BIG)", line)
out.append(line.lower().replace("'a'", "'A'").replace('"hi"', '"HI"'))
return "\n".join(out) + "\n"
def main():
pre = subprocess.run(["bash", "-c", "source %s/../scripts/rv_env.sh; echo $RV_PREFIX" % HERE],
capture_output=True, text=True).stdout.strip()
ours = subprocess.run([os.path.join(HERE, "out", "monitor-host"), "asmbin"], input=SRC,
capture_output=True, text=True).stdout.strip()
if not re.fullmatch(r"[0-9A-F]{5} [0-9A-F]+", ours):
print("FAIL the monitor's assembler: " + ours)
sys.exit(1)
start, ours = ours.split()
tmp = tempfile.mkdtemp()
open(os.path.join(tmp, "t.s"), "w").write(to_gnu(SRC))
subprocess.run([pre + "as", "-march=rv32imf_zicsr_zifencei", "-mno-relax", "-o", os.path.join(tmp, "t.o"),
os.path.join(tmp, "t.s")], check=True)
subprocess.run([pre + "ld", "-m", "elf32lriscv", "--no-relax", "-e", "0x12000", "-Ttext=0x12000", "-o", os.path.join(tmp, "t.elf"),
os.path.join(tmp, "t.o")], check=True)
subprocess.run([pre + "objcopy", "-O", "binary", os.path.join(tmp, "t.elf"), os.path.join(tmp, "t.bin")],
check=True)
gnu = open(os.path.join(tmp, "t.bin"), "rb").read().hex().upper()
# GNU pads the section's end to a word
if len(gnu) > len(ours) and set(gnu[len(ours):]) <= {"0"}:
gnu = gnu[:len(ours)]
ok = start == "12000" and ours == gnu
if not ok:
for i in range(0, max(len(ours), len(gnu)), 8):
if ours[i:i + 8] != gnu[i:i + 8]:
print("first difference at %05X: monitor %s, GNU %s" % (0x12000 + i // 2, ours[i:i + 8], gnu[i:i + 8]))
break
print("lengths: monitor %d bytes, GNU %d" % (len(ours) // 2, len(gnu) // 2))
print(("PASS" if ok else "FAIL") + " %d bytes from 12000h the same as GNU as" % (len(ours) // 2))
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()