1(import srfi-151)23;;;;4;; Utility procedures5;;;;67;; The bit-field procedure from SRFI-151 returns a bit field in the8;; interval [start, end - 1]. Contrary to bit-field, instr-field returns9;; a field in the interval [start, end] to allow using the same notation10;; as used by the RISC-V specification.11(define (instr-field instr start end)12 (bit-field instr start (+ end 1)))1314;; https://en.wikipedia.org/wiki/Two%27s_complement#Converting_from_two's_complement_representation15(define (from-twocomp numbits input)16 (let ((mask (expt 2 (- numbits 1))))17 (+ (* -1 (bitwise-and input mask))18 (bitwise-and input (bitwise-not mask)))))1920;;;;21;; Instruction fields22;;;;2324(define (instr-opcode instr)25 (instr-field instr 0 6))2627(define (instr-funct3 instr)28 (instr-field instr 12 14))2930(define (instr-funct7 instr)31 (instr-field instr 25 31))3233(define (instr-rs1 instr)34 (instr-field instr 15 19))3536(define (instr-rs2 instr)37 (instr-field instr 20 24))3839(define (instr-rd instr)40 (instr-field instr 7 11))4142;;;;43;; Instruction immediates44;;;4546(define (instr-i-imm instr)47 (from-twocomp 1248 (instr-field instr 20 31)))4950(define (instr-s-imm instr)51 (from-twocomp 1252 (bitwise-ior53 (arithmetic-shift (instr-field instr 25 31) 5)54 (instr-field instr 7 11))))5556(define (instr-b-imm instr)57 (from-twocomp 1258 (bitwise-ior59 (arithmetic-shift (instr-field instr 31 31) 12)60 (arithmetic-shift (instr-field instr 7 7) 11)61 (arithmetic-shift (instr-field instr 25 30) 5)62 (arithmetic-shift (instr-field instr 8 11) 1))))6364(define (instr-u-imm instr)65 (instr-field instr 12 31))6667(define (instr-j-imm instr)68 (from-twocomp 2069 (bitwise-ior70 (arithmetic-shift (instr-field instr 31 31) 20)71 (arithmetic-shift (instr-field instr 19 12) 12)72 (arithmetic-shift (instr-field instr 20 20) 11)73 (arithmetic-shift (instr-field instr 21 30) 1))))