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>
This commit is contained in:
1 parent
19278d1d74
commit
71fd403294
31 files changed
+9143
-7638
No files matched your search
+293
@@ -0,0 +1,293 @@
|
||||
"""Bit-exact IEEE 754 single precision, the way RISC-V's F extension does it.
|
||||
|
||||
The reference for rtl/rv32_fpu.v and the arithmetic of tools/rv32emu.py.
|
||||
Values are 32-bit patterns (ints); every operation returns (bits, flags),
|
||||
flags being fflags: NV 16, DZ 8, OF 4, UF 2, NX 1. Rounding modes are
|
||||
RISC-V's: 0 RNE, 1 RTZ, 2 RDN, 3 RUP, 4 RMM. Tininess is detected after
|
||||
rounding; NaN results are the canonical 7FC00000.
|
||||
|
||||
Arithmetic is exact (Fraction), then rounded once.
|
||||
"""
|
||||
from fractions import Fraction
|
||||
|
||||
NV, DZ, OF, UF, NX = 16, 8, 4, 2, 1
|
||||
RNE, RTZ, RDN, RUP, RMM = 0, 1, 2, 3, 4
|
||||
QNAN = 0x7FC00000
|
||||
M32 = 0xFFFFFFFF
|
||||
|
||||
|
||||
def sign(x): return x >> 31
|
||||
def expo(x): return (x >> 23) & 0xFF
|
||||
def frac(x): return x & 0x7FFFFF
|
||||
def is_nan(x): return expo(x) == 255 and frac(x) != 0
|
||||
def is_snan(x): return is_nan(x) and not (x >> 22) & 1
|
||||
def is_inf(x): return (x & 0x7FFFFFFF) == 0x7F800000
|
||||
def is_zero(x): return (x & 0x7FFFFFFF) == 0
|
||||
|
||||
|
||||
def value(x):
|
||||
"""The exact value of a finite pattern, without its sign."""
|
||||
e, f = expo(x), frac(x)
|
||||
if e == 0:
|
||||
return Fraction(f, 1 << 149)
|
||||
m = f | 0x800000
|
||||
k = e - 150
|
||||
return Fraction(m << k) if k >= 0 else Fraction(m, 1 << -k)
|
||||
|
||||
|
||||
def _round_int(q, s, rm, neg):
|
||||
"""Round q (a non-negative Fraction) to an integer; s marks a value a hair
|
||||
above q (an inexact square root). Returns (n, inexact)."""
|
||||
n = q.numerator // q.denominator
|
||||
rem = q - n
|
||||
if rem == 0 and not s:
|
||||
return n, False
|
||||
half = Fraction(1, 2)
|
||||
if rm == RNE:
|
||||
up = rem > half or (rem == half and (s or n & 1))
|
||||
elif rm == RTZ:
|
||||
up = False
|
||||
elif rm == RDN:
|
||||
up = neg
|
||||
elif rm == RUP:
|
||||
up = not neg
|
||||
else: # RMM
|
||||
up = rem >= half
|
||||
return n + up, True
|
||||
|
||||
|
||||
def round_f32(neg, x, rm, s=False):
|
||||
"""Round the exact value (-1)^neg * x (x a non-negative Fraction; s: the
|
||||
true value is a hair above x) to single precision. Returns (bits, flags)."""
|
||||
sb = int(neg) << 31
|
||||
if x == 0 and not s:
|
||||
return sb, 0
|
||||
# exponent e with 2^e <= x < 2^(e+1)
|
||||
e = x.numerator.bit_length() - x.denominator.bit_length()
|
||||
if Fraction(2) ** e > x:
|
||||
e -= 1
|
||||
# rounding with an unbounded exponent (for tininess after rounding)
|
||||
n_u, _ = _round_int(x * Fraction(2) ** (23 - e), s, rm, neg)
|
||||
tiny = n_u * Fraction(2) ** (e - 23) < Fraction(1, 1 << 126)
|
||||
if e < -126:
|
||||
n, inex = _round_int(x * Fraction(1 << 149), s, rm, neg)
|
||||
flags = (NX | (UF if tiny else 0)) if inex else 0
|
||||
if n >= 1 << 23: # rounded up to the smallest normal
|
||||
return sb | (1 << 23) | (n - (1 << 23)), flags
|
||||
return sb | n, flags
|
||||
n, inex = _round_int(x * Fraction(2) ** (23 - e), s, rm, neg)
|
||||
if n == 1 << 24:
|
||||
n >>= 1
|
||||
e += 1
|
||||
if e > 127:
|
||||
big = rm in (RNE, RMM) or (rm == RDN and neg) or (rm == RUP and not neg)
|
||||
return sb | (0x7F800000 if big else 0x7F7FFFFF), OF | NX
|
||||
return sb | ((e + 127) << 23) | (n - (1 << 23)), NX if inex else 0
|
||||
|
||||
|
||||
def fma(a, b, c, rm, negp=False, negc=False, use_c=True):
|
||||
"""(-1)^negp * a*b + (-1)^negc * c; use_c False is a plain multiply."""
|
||||
sp = sign(a) ^ sign(b) ^ negp
|
||||
sc = sign(c) ^ negc
|
||||
nans = is_nan(a) or is_nan(b) or (use_c and is_nan(c))
|
||||
inv = is_snan(a) or is_snan(b) or (use_c and is_snan(c))
|
||||
inv |= (is_inf(a) and is_zero(b)) or (is_zero(a) and is_inf(b))
|
||||
pinf = (is_inf(a) or is_inf(b)) and not nans
|
||||
if pinf and use_c and is_inf(c) and sp != sc:
|
||||
inv = True
|
||||
if inv or nans:
|
||||
return QNAN, NV if inv else 0
|
||||
if pinf:
|
||||
return (sp << 31) | 0x7F800000, 0
|
||||
if use_c and is_inf(c):
|
||||
return (sc << 31) | 0x7F800000, 0
|
||||
p = value(a) * value(b) * (-1 if sp else 1)
|
||||
if not use_c:
|
||||
if p == 0:
|
||||
return sp << 31, 0
|
||||
return round_f32(p < 0, abs(p), rm)
|
||||
q = value(c) * (-1 if sc else 1)
|
||||
r = p + q
|
||||
if r == 0:
|
||||
if p == 0 and q == 0 and sp == sc:
|
||||
return sp << 31, 0
|
||||
return (1 << 31 if rm == RDN else 0), 0
|
||||
return round_f32(r < 0, abs(r), rm)
|
||||
|
||||
|
||||
ONE = 0x3F800000
|
||||
|
||||
|
||||
def add(a, b, rm): return fma(a, ONE, b, rm)
|
||||
def sub(a, b, rm): return fma(a, ONE, b, rm, negc=True)
|
||||
def mul(a, b, rm): return fma(a, b, 0, rm, use_c=False)
|
||||
|
||||
|
||||
def div(a, b, rm):
|
||||
s = sign(a) ^ sign(b)
|
||||
if is_nan(a) or is_nan(b):
|
||||
return QNAN, NV if is_snan(a) or is_snan(b) else 0
|
||||
if (is_inf(a) and is_inf(b)) or (is_zero(a) and is_zero(b)):
|
||||
return QNAN, NV
|
||||
if is_inf(a):
|
||||
return (s << 31) | 0x7F800000, 0
|
||||
if is_inf(b):
|
||||
return s << 31, 0
|
||||
if is_zero(b):
|
||||
return (s << 31) | 0x7F800000, DZ
|
||||
if is_zero(a):
|
||||
return s << 31, 0
|
||||
return round_f32(s, value(a) / value(b), rm)
|
||||
|
||||
|
||||
def isqrt(n):
|
||||
if n == 0:
|
||||
return 0
|
||||
x = 1 << ((n.bit_length() + 1) // 2)
|
||||
while True:
|
||||
y = (x + n // x) // 2
|
||||
if y >= x:
|
||||
return x
|
||||
x = y
|
||||
|
||||
|
||||
def sqrt(a, rm):
|
||||
if is_nan(a):
|
||||
return QNAN, NV if is_snan(a) else 0
|
||||
if is_zero(a):
|
||||
return a, 0
|
||||
if sign(a):
|
||||
return QNAN, NV
|
||||
if is_inf(a):
|
||||
return a, 0
|
||||
n = value(a) * Fraction(4) ** 150 # an integer
|
||||
n = n.numerator
|
||||
r = isqrt(n)
|
||||
return round_f32(0, Fraction(r, 1 << 150), rm, s=r * r != n)
|
||||
|
||||
|
||||
def sgnj(a, b, f3):
|
||||
s = [sign(b), sign(b) ^ 1, sign(a) ^ sign(b)][f3]
|
||||
return (s << 31) | (a & 0x7FFFFFFF), 0
|
||||
|
||||
|
||||
def _lt(a, b):
|
||||
if is_zero(a) and is_zero(b):
|
||||
return False
|
||||
if sign(a) != sign(b):
|
||||
return sign(a) == 1
|
||||
if sign(a):
|
||||
return (a & 0x7FFFFFFF) > (b & 0x7FFFFFFF)
|
||||
return (a & 0x7FFFFFFF) < (b & 0x7FFFFFFF)
|
||||
|
||||
|
||||
def _eq(a, b):
|
||||
return a == b or (is_zero(a) and is_zero(b))
|
||||
|
||||
|
||||
def minmax(a, b, is_max):
|
||||
fl = NV if is_snan(a) or is_snan(b) else 0
|
||||
if is_nan(a) and is_nan(b):
|
||||
return QNAN, fl
|
||||
if is_nan(a):
|
||||
return b, fl
|
||||
if is_nan(b):
|
||||
return a, fl
|
||||
if is_zero(a) and is_zero(b):
|
||||
if is_max:
|
||||
return (a if not sign(a) else b), fl
|
||||
return (a if sign(a) else b), fl
|
||||
if is_max:
|
||||
return (b if _lt(a, b) else a), fl
|
||||
return (a if _lt(a, b) else b), fl
|
||||
|
||||
|
||||
def cmp(a, b, f3):
|
||||
"""f3: 0 FLE, 1 FLT, 2 FEQ. Returns (0 or 1, flags)."""
|
||||
if is_nan(a) or is_nan(b):
|
||||
if f3 == 2:
|
||||
return 0, NV if is_snan(a) or is_snan(b) else 0
|
||||
return 0, NV
|
||||
if f3 == 2:
|
||||
return int(_eq(a, b)), 0
|
||||
if f3 == 1:
|
||||
return int(_lt(a, b)), 0
|
||||
return int(_lt(a, b) or _eq(a, b)), 0
|
||||
|
||||
|
||||
def fclass(a):
|
||||
s, e, f = sign(a), expo(a), frac(a)
|
||||
if e == 255:
|
||||
if f == 0:
|
||||
return 1 << 0 if s else 1 << 7
|
||||
return 1 << 9 if f >> 22 else 1 << 8
|
||||
if e == 0:
|
||||
if f == 0:
|
||||
return 1 << 3 if s else 1 << 4
|
||||
return 1 << 2 if s else 1 << 5
|
||||
return 1 << 1 if s else 1 << 6
|
||||
|
||||
|
||||
def cvt_w(a, rm, unsigned):
|
||||
"""float -> int32 / uint32: (bits, flags)."""
|
||||
if is_nan(a):
|
||||
return (M32 if unsigned else 0x7FFFFFFF), NV
|
||||
if is_inf(a):
|
||||
if unsigned:
|
||||
return (0 if sign(a) else M32), NV
|
||||
return (0x80000000 if sign(a) else 0x7FFFFFFF), NV
|
||||
neg = sign(a)
|
||||
n, inex = _round_int(value(a), False, rm, neg)
|
||||
v = -n if neg else n
|
||||
lo, hi = (0, M32) if unsigned else (-(1 << 31), (1 << 31) - 1)
|
||||
if v < lo or v > hi:
|
||||
if unsigned:
|
||||
return (0 if neg else M32), NV
|
||||
return (0x80000000 if neg else 0x7FFFFFFF), NV
|
||||
return v & M32, NX if inex else 0
|
||||
|
||||
|
||||
def cvt_s(x, rm, unsigned):
|
||||
"""int32 / uint32 (as a 32-bit pattern) -> float."""
|
||||
v = x if unsigned or not x >> 31 else x - (1 << 32)
|
||||
if v == 0:
|
||||
return 0, 0
|
||||
return round_f32(v < 0, Fraction(abs(v)), rm)
|
||||
|
||||
|
||||
def execute(ins, rm, a, b, c):
|
||||
"""Run one F instruction (the 32-bit encoding) on operand patterns a, b, c
|
||||
(a is the integer register for FMV.W.X and FCVT.S.W[U]); rm is the
|
||||
resolved rounding mode. Returns (result, flags, to_int)."""
|
||||
opc = ins & 0x7F
|
||||
f3 = (ins >> 12) & 7
|
||||
f5 = ins >> 27
|
||||
r2 = (ins >> 20) & 31
|
||||
if opc in (0x43, 0x47, 0x4B, 0x4F):
|
||||
r, fl = fma(a, b, c, rm, negp=bool(opc & 8), negc=bool(opc & 4))
|
||||
return r, fl, False
|
||||
if f5 == 0:
|
||||
return (*add(a, b, rm), False)
|
||||
if f5 == 1:
|
||||
return (*sub(a, b, rm), False)
|
||||
if f5 == 2:
|
||||
return (*mul(a, b, rm), False)
|
||||
if f5 == 3:
|
||||
return (*div(a, b, rm), False)
|
||||
if f5 == 0x0B:
|
||||
return (*sqrt(a, rm), False)
|
||||
if f5 == 0x04:
|
||||
return (*sgnj(a, b, f3), False)
|
||||
if f5 == 0x05:
|
||||
return (*minmax(a, b, f3 == 1), False)
|
||||
if f5 == 0x18:
|
||||
return (*cvt_w(a, rm, r2 & 1), True)
|
||||
if f5 == 0x1C:
|
||||
return (fclass(a) if f3 == 1 else a, 0, True)
|
||||
if f5 == 0x14:
|
||||
return (*cmp(a, b, f3), True)
|
||||
if f5 == 0x1A:
|
||||
return (*cvt_s(a, rm, r2 & 1), False)
|
||||
if f5 == 0x1E:
|
||||
return a, 0, False
|
||||
raise ValueError("not an F instruction: %08x" % ins)
|
||||
Executable
+92
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test vectors for rtl/rv32_fpu.v from tools/f32.py: every F instruction,
|
||||
every rounding mode, special values, subnormals, boundaries and near-
|
||||
cancellations. One line per case, hex: ins rm a b c result flags.
|
||||
|
||||
tools/fpu-vectors.py COUNT [SEED] > vectors.txt
|
||||
"""
|
||||
import os
|
||||
import random
|
||||
import sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import f32
|
||||
|
||||
SPECIAL = [0, 0x80000000, 0x7F800000, 0xFF800000, 0x7FC00000, 0xFFC00000, 0x7F800001,
|
||||
0x7FBFFFFF, 0x00000001, 0x80000001, 0x007FFFFF, 0x807FFFFF, 0x00800000,
|
||||
0x80800000, 0x7F7FFFFF, 0xFF7FFFFF, 0x3F800000, 0xBF800000, 0x3F000000,
|
||||
0x4B000000, 0x4F000000, 0xCF000000, 0x4F800000, 0x3EFFFFFF, 0x3FC00000]
|
||||
|
||||
|
||||
def rnd():
|
||||
k = random.random()
|
||||
if k < 0.12:
|
||||
return random.choice(SPECIAL)
|
||||
if k < 0.32:
|
||||
e = random.choice([0, 1, 2, 23, 24, 25, 100, 125, 126, 127, 128, 150, 157, 158, 159, 253, 254])
|
||||
return (random.getrandbits(1) << 31) | (e << 23) | random.choice(
|
||||
[random.getrandbits(23), 0, 0x7FFFFF, 1, 0x400000, random.getrandbits(4)])
|
||||
if k < 0.55:
|
||||
e = random.randint(110, 145)
|
||||
return (random.getrandbits(1) << 31) | (e << 23) | random.getrandbits(23)
|
||||
return random.getrandbits(32)
|
||||
|
||||
|
||||
def enc_op(f5, f3, rs2=0):
|
||||
return (f5 << 27) | (rs2 << 20) | (f3 << 12) | 0x53
|
||||
|
||||
|
||||
def main():
|
||||
n = int(sys.argv[1])
|
||||
random.seed(int(sys.argv[2]) if len(sys.argv) > 2 else 1)
|
||||
kinds = (['fma'] * 6 + ['add'] * 4 + ['sub'] * 3 + ['mul'] * 3 + ['div'] * 3 + ['sqrt'] * 2 +
|
||||
['sgnj', 'minmax', 'cmp', 'class', 'mvxw', 'mvwx', 'cvtw', 'cvtw', 'cvts', 'cvts'])
|
||||
for _ in range(n):
|
||||
k = random.choice(kinds)
|
||||
rm = random.randint(0, 4)
|
||||
f3 = rm
|
||||
a, b, c = rnd(), rnd(), rnd()
|
||||
if k == 'fma':
|
||||
opc = random.choice([0x43, 0x47, 0x4B, 0x4F])
|
||||
ins = (opc | (f3 << 12))
|
||||
if random.random() < 0.4: # near cancellation
|
||||
p, _ = f32.mul(a, b, rm)
|
||||
if not f32.is_nan(p):
|
||||
want_neg = bool(opc & 8) == bool(opc & 4) # makes c cancel the product
|
||||
c = (p ^ (0x80000000 if want_neg else 0)) + random.choice([0, 0, 1, -1, 2, 0x80, -0x1000])
|
||||
c &= 0xFFFFFFFF
|
||||
elif k in ('add', 'sub', 'mul', 'div', 'sqrt'):
|
||||
f5 = {'add': 0, 'sub': 1, 'mul': 2, 'div': 3, 'sqrt': 0x0B}[k]
|
||||
ins = enc_op(f5, f3)
|
||||
if k in ('add', 'sub') and random.random() < 0.35:
|
||||
b = (a ^ (0x80000000 if k == 'add' else 0)) + random.choice([0, 1, -1, 3, 0x40, 0x800000, -0x800000])
|
||||
b &= 0xFFFFFFFF
|
||||
elif k == 'sgnj':
|
||||
f3 = random.randint(0, 2); ins = enc_op(0x04, f3)
|
||||
elif k == 'minmax':
|
||||
f3 = random.randint(0, 1); ins = enc_op(0x05, f3)
|
||||
if random.random() < 0.3: b = a ^ random.choice([0, 0x80000000, 1])
|
||||
elif k == 'cmp':
|
||||
f3 = random.randint(0, 2); ins = enc_op(0x14, f3)
|
||||
if random.random() < 0.3: b = a ^ random.choice([0, 0x80000000, 1])
|
||||
elif k == 'class':
|
||||
f3 = 1; ins = enc_op(0x1C, 1)
|
||||
elif k == 'mvxw':
|
||||
f3 = 0; ins = enc_op(0x1C, 0)
|
||||
elif k == 'mvwx':
|
||||
f3 = 0; ins = enc_op(0x1E, 0)
|
||||
elif k == 'cvtw':
|
||||
u = random.randint(0, 1); ins = enc_op(0x18, f3, u)
|
||||
if random.random() < 0.5:
|
||||
e = random.randint(120, 160)
|
||||
a = (random.getrandbits(1) << 31) | (e << 23) | random.getrandbits(23)
|
||||
else:
|
||||
u = random.randint(0, 1); ins = enc_op(0x1A, f3, u)
|
||||
if random.random() < 0.5:
|
||||
a = random.getrandbits(random.randint(1, 32))
|
||||
if random.random() < 0.3: a = (-a) & 0xFFFFFFFF
|
||||
r, fl, _ = f32.execute(ins, rm, a, b, c)
|
||||
print('%08x %x %08x %08x %08x %08x %02x' % (ins, rm, a, b, c, r, fl))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
+106
-5
@@ -1,8 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
"""RV32IM emulator of the Basys 3 RISC-V computer, for testing programs on the
|
||||
"""RV32IMF emulator of the Basys 3 RISC-V computer, for testing programs on the
|
||||
PC and as a reference for rtl/rv32_cpu.v.
|
||||
|
||||
The same machine as rtl/rv32_cpu.v and rtl/rv32_system.v: RV32IM, machine
|
||||
The same machine as rtl/rv32_cpu.v and rtl/rv32_system.v: RV32IMF (floating
|
||||
point from tools/f32.py, bit for bit as rtl/rv32_fpu.v), machine
|
||||
mode, the CSRs the core has (no triggers), the resumable NMI, 80 KB of RAM
|
||||
(writes below rom_top ignored), the monitor's 32 KB ROM at 20000h and 8 KB of
|
||||
RAM at 28000h, the I/O ports at FFFFFF00h + N (io_in / io_out, to be
|
||||
@@ -15,7 +16,10 @@ runs a raw image from address 0 with the console on ports 0/1: text from
|
||||
each instruction run. The emulator also counts the clocks rv32_cpu would take
|
||||
(cycles; see cost()).
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import f32 # noqa: E402
|
||||
|
||||
M32 = 0xFFFFFFFF
|
||||
RAM_END = 0x14000
|
||||
@@ -43,6 +47,10 @@ class RV32:
|
||||
self.m[:len(mem)] = mem
|
||||
self.rom_top = rom_top # writes below are ignored
|
||||
self.x = [0] * 32
|
||||
self.f = [0] * 32 # float registers (bit patterns)
|
||||
self.fs = 1 # mstatus.FS: Initial
|
||||
self.fflags = 0
|
||||
self.frm = 0
|
||||
self.pc = 0
|
||||
self.cache = {}
|
||||
self.csr = {0x300: 0, 0x304: 0, 0x305: 0, 0x340: 0, 0x341: 0, 0x342: 0, 0x343: 0,
|
||||
@@ -172,9 +180,13 @@ class RV32:
|
||||
# ------------------------------------------------------------------ CSRs
|
||||
def csr_read(self, n):
|
||||
if n == 0x300:
|
||||
return self.csr[0x300] | 0x1800
|
||||
return self.csr[0x300] | 0x1800 | self.fs << 13 | (0x80000000 if self.fs == 3 else 0)
|
||||
if n == 0x301:
|
||||
return 0x40001100
|
||||
return 0x40001120
|
||||
if n in (1, 2, 3):
|
||||
if not self.fs:
|
||||
raise Trap(2)
|
||||
return [0, self.fflags, self.frm, self.frm << 5 | self.fflags][n]
|
||||
if n == 0x344:
|
||||
return 0
|
||||
if n == 0x744:
|
||||
@@ -194,6 +206,17 @@ class RV32:
|
||||
raise Trap(2)
|
||||
if n == 0x300:
|
||||
self.csr[0x300] = v & 0x88
|
||||
self.fs = v >> 13 & 3
|
||||
elif n in (1, 2, 3):
|
||||
if not self.fs:
|
||||
raise Trap(2)
|
||||
if n == 1:
|
||||
self.fflags = v & 31
|
||||
elif n == 2:
|
||||
self.frm = v & 7
|
||||
else:
|
||||
self.fflags, self.frm = v & 31, v >> 5 & 7
|
||||
self.fs = 3
|
||||
elif n == 0x304:
|
||||
self.csr[0x304] = v & 0xFFFF0888
|
||||
elif n in (0x305, 0x341, 0x741):
|
||||
@@ -367,6 +390,8 @@ class RV32:
|
||||
x[rd] = op(x[rs1], x[rs2])
|
||||
return nxt
|
||||
return f
|
||||
if opc in (0x07, 0x27, 0x43, 0x47, 0x4B, 0x4F, 0x53):
|
||||
return self.decode_f(ins, pc)
|
||||
if opc == 0x0F and f3 in (0, 1): # fence, fence.i
|
||||
return lambda: nxt
|
||||
if opc == 0x73:
|
||||
@@ -408,6 +433,74 @@ class RV32:
|
||||
return f
|
||||
return illegal
|
||||
|
||||
def decode_f(self, ins, pc):
|
||||
"""The F extension: as rv32_cpu decodes it (illegal while mstatus.FS
|
||||
is Off, or with a reserved rounding mode)."""
|
||||
opc, rd, f3 = ins & 0x7F, ins >> 7 & 31, ins >> 12 & 7
|
||||
rs1, rs2, rs3 = ins >> 15 & 31, ins >> 20 & 31, ins >> 27
|
||||
x, fr = self.x, self.f
|
||||
nxt = (pc + 4) & M32
|
||||
ill = Trap(2, ins)
|
||||
|
||||
def illegal():
|
||||
raise ill
|
||||
if opc == 0x07 and f3 == 2: # flw
|
||||
immi = sx(ins >> 20, 12)
|
||||
|
||||
def f():
|
||||
if not self.fs:
|
||||
raise ill
|
||||
a = (x[rs1] + immi) & M32
|
||||
if a & 3:
|
||||
raise Trap(4, a)
|
||||
fr[rd] = self.rd32(a)
|
||||
self.fs = 3
|
||||
return nxt
|
||||
return f
|
||||
if opc == 0x27 and f3 == 2: # fsw
|
||||
imms = sx((ins >> 25) << 5 | (ins >> 7 & 31), 12)
|
||||
|
||||
def f():
|
||||
if not self.fs:
|
||||
raise ill
|
||||
a = (x[rs1] + imms) & M32
|
||||
if a & 3:
|
||||
raise Trap(6, a)
|
||||
self.wr32(a, fr[rs2])
|
||||
return nxt
|
||||
return f
|
||||
if opc in (0x07, 0x27) or (ins >> 25) & 3:
|
||||
return illegal
|
||||
f5 = ins >> 27
|
||||
uses_rm = opc != 0x53 or f5 in (0, 1, 2, 3, 0x0B, 0x18, 0x1A)
|
||||
if opc == 0x53:
|
||||
ok = (f5 <= 3 or (f5 == 0x0B and rs2 == 0) or (f5 == 4 and f3 <= 2) or
|
||||
(f5 == 5 and f3 <= 1) or (f5 == 0x14 and f3 <= 2) or
|
||||
(f5 in (0x18, 0x1A) and rs2 <= 1) or (f5 == 0x1C and rs2 == 0 and f3 <= 1) or
|
||||
(f5 == 0x1E and rs2 == 0 and f3 == 0))
|
||||
if not ok:
|
||||
return illegal
|
||||
to_int = opc == 0x53 and f5 in (0x18, 0x1C, 0x14)
|
||||
from_int = opc == 0x53 and f5 in (0x1A, 0x1E)
|
||||
|
||||
def f():
|
||||
if not self.fs:
|
||||
raise ill
|
||||
rm = self.frm if f3 == 7 else f3
|
||||
if uses_rm and rm > 4:
|
||||
raise ill
|
||||
a = x[rs1] if from_int else fr[rs1]
|
||||
r, fl, _ = f32.execute(ins, rm, a, fr[rs2], fr[rs3])
|
||||
self.fflags |= fl
|
||||
self.fs = 3
|
||||
if to_int:
|
||||
if rd:
|
||||
x[rd] = r
|
||||
else:
|
||||
fr[rd] = r
|
||||
return nxt
|
||||
return f
|
||||
|
||||
# ------------------------------------------------------------------ run
|
||||
def trap(self, cause, tval):
|
||||
self.csr[0x341] = self.pc
|
||||
@@ -452,9 +545,17 @@ class RV32:
|
||||
def cost(self, pc):
|
||||
"""Clocks rv32_cpu takes for the instruction at pc, just run: 2, or 3
|
||||
for a load, store, jump or taken branch (4 for I/O), 4 for a multiply,
|
||||
35 for a divide."""
|
||||
35 for a divide; floating point as listed in rv32_cpu.v (the shortcut
|
||||
for infinite, NaN and zero operands isn't counted)."""
|
||||
ins = self.rd32(pc)
|
||||
op = ins & 0x7F
|
||||
if op in (0x07, 0x27):
|
||||
return 3
|
||||
if op in (0x43, 0x47, 0x4B, 0x4F):
|
||||
return 12
|
||||
if op == 0x53:
|
||||
f5 = ins >> 27
|
||||
return {0: 12, 1: 12, 2: 12, 3: 36, 0x0B: 36, 0x18: 5, 0x1A: 8}.get(f5, 4)
|
||||
if op in (0x03, 0x23):
|
||||
a = (self.x[ins >> 15 & 31] + sx(ins >> 20 if op == 3 else (ins >> 25) << 5 | (ins >> 7 & 31), 12)) & M32
|
||||
return 4 if a >= 0xFFFFFE00 or MON_RAM <= a < MON_END else 3
|
||||
|
||||
Reference in new issue
Block a user