#!/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 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=rv32im_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()