1(import riscv test)2(import (chicken condition))34(define exception-raised #t)5(define-syntax test-exception6 (syntax-rules ()7 ((test-exception body ...)8 (call-with-current-continuation9 (lambda (con)10 (with-exception-handler11 (lambda (e) (con exception-raised))12 (lambda ()13 (begin14 (begin body ...)15 (con (not exception-raised))))))))))1617(test-group "encode R-type"18 (test "encode add instruction"19 add-instr20 (r-type (get-opcode 'ADD) (get-funct3 'ADD) (get-funct7 'ADD)21 12 10 11))2223 (test "encode add instruction with invalid RD"24 exception-raised25 (test-exception26 (r-type (get-opcode 'ADD) (get-funct3 'ADD) (get-funct7 'ADD) 12 10 42))))2728(test-group "encode I-type"29 (test "encode addi instruction"30 addi-instr31 (i-type (get-opcode 'ADDI) (get-funct3 'ADDI) 6 5 42))3233 (test "encode addi with negative immediate"34 -2335 (instr-i-imm (i-type (get-opcode 'ADDI) (get-funct3 'ADDI) 6 5 -23)))3637 (test "encode addi with invalide immediate"38 exception-raised39 (test-exception40 (i-type (get-opcode 'ADDI) (get-funct3 'ADDI) 6 5 2048))))4142(test-group "encode S-type"43 (test "encode sw instruction"44 sw-instr45 (s-type (get-opcode 'SW) (get-funct3 'SW) 9 1 23))4647 (test "encode sw instruction with invalid immediate"48 exception-raised49 (test-exception50 (s-type (get-opcode 'SW) (get-funct3 'SW) 9 1 (expt 2 12)))))5152(test-group "encode B-type"53 (test "encode beq instruction"54 beq-instr55 (b-type (get-opcode 'BEQ) (get-funct3 'BEQ) 10 11 32))5657 (test "encode beq instruction with negative immediate"58 -204859 (instr-b-imm (b-type (get-opcode 'BEQ) (get-funct3 'BEQ) 10 11 -2048))))6061(test-group "encode U-type"62 (test "encode lui instruction"63 lui-instr64 (u-type (get-opcode 'LUI) 28 #xfffff))6566 (test "encode lui instruction with invalid immediate"67 exception-raised68 (test-exception69 (u-type (get-opcode 'LUI) 28 (expt 2 20)))))7071(test-group "encode J-type"72 (test "encode jal instruction"73 jal-instr74 (j-type (get-opcode 'JAL) 1 32)))