The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and traps, interrupts, two hardware triggers and a resumable NMI for the monitor. Passes riscv-tests rv32ui/um/mi. rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor ROM. BASIC, the five games and the monitor (with an RV32 disassembler, assembler, breakpoints and single step) are ported to RISC-V GCC. Python emulator and tests in tools/; full-system simulation in sim/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
179 lines
4.1 KiB
ArmAsm
179 lines
4.1 KiB
ArmAsm
# Interrupts: each source, masking by mie and mstatus.MIE, priority, and code
|
|
# that is interrupted again and again giving the same result as without.
|
|
# Uses the test device in sim/tb_cpu.v at FFFFFF00.
|
|
|
|
#include "riscv_test.h"
|
|
#include "test_macros.h"
|
|
|
|
#define DEV -256 /* 0xFFFFFF00 */
|
|
#define SOFT 1
|
|
#define TIMER 2
|
|
#define EXT 4
|
|
#define LOCAL 8
|
|
|
|
# raise lines after delay cycles
|
|
#define RAISE(lines, delay) li t0, ((delay) << 8) | (lines); sw t0, DEV(zero)
|
|
|
|
# spin until the handler has run (s2 != count), or fail after a while
|
|
#define WAIT_IRQ(count) \
|
|
li t1, 1000; \
|
|
1: li t0, count; bne s2, t0, 2f; addi t1, t1, -1; bnez t1, 1b; j fail; 2:
|
|
|
|
RVTEST_RV32M
|
|
RVTEST_CODE_BEGIN
|
|
|
|
li s2, 0
|
|
csrsi mstatus, MSTATUS_MIE
|
|
|
|
# each source, with its own enable bit
|
|
li TESTNUM, 2
|
|
li t0, MIP_MTIP; csrw mie, t0
|
|
RAISE(TIMER, 20)
|
|
WAIT_IRQ(0)
|
|
li t0, 0x80000007; bne s1, t0, fail
|
|
|
|
li TESTNUM, 3
|
|
li t0, MIP_MEIP; csrw mie, t0
|
|
RAISE(EXT, 5)
|
|
WAIT_IRQ(1)
|
|
li t0, 0x8000000b; bne s1, t0, fail
|
|
|
|
li TESTNUM, 4
|
|
li t0, MIP_MSIP; csrw mie, t0
|
|
RAISE(SOFT, 0)
|
|
WAIT_IRQ(2)
|
|
li t0, 0x80000003; bne s1, t0, fail
|
|
|
|
li TESTNUM, 5
|
|
li t0, 1 << 16; csrw mie, t0
|
|
RAISE(LOCAL, 3)
|
|
WAIT_IRQ(3)
|
|
li t0, 0x80000010; bne s1, t0, fail
|
|
|
|
# masked by mie: pending shows in mip but nothing is taken
|
|
li TESTNUM, 6
|
|
csrw mie, zero
|
|
RAISE(TIMER, 0)
|
|
li t1, 100
|
|
1: addi t1, t1, -1; bnez t1, 1b
|
|
li t0, 4; bne s2, t0, fail
|
|
csrr t0, mip; andi t0, t0, MIP_MTIP; beqz t0, fail
|
|
li t0, MIP_MTIP; csrs mie, t0 # now it is taken
|
|
WAIT_IRQ(4)
|
|
|
|
# masked by mstatus.MIE
|
|
li TESTNUM, 7
|
|
csrci mstatus, MSTATUS_MIE
|
|
RAISE(EXT, 0)
|
|
li t0, MIP_MEIP; csrw mie, t0
|
|
li t1, 100
|
|
1: addi t1, t1, -1; bnez t1, 1b
|
|
li t0, 5; bne s2, t0, fail
|
|
csrsi mstatus, MSTATUS_MIE
|
|
WAIT_IRQ(5)
|
|
li t0, 0x8000000b; bne s1, t0, fail
|
|
# the handler saw MPIE set and MIE clear
|
|
li t0, MSTATUS_MPIE; and t1, s3, t0; beqz t1, fail
|
|
andi t1, s3, MSTATUS_MIE; bnez t1, fail
|
|
# and mret turned MIE back on
|
|
csrr t0, mstatus; andi t0, t0, MSTATUS_MIE; beqz t0, fail
|
|
|
|
# priority: external, then software, then timer, then local
|
|
li TESTNUM, 8
|
|
li t0, MIP_MEIP | MIP_MSIP | MIP_MTIP | (1 << 16); csrw mie, t0
|
|
csrci mstatus, MSTATUS_MIE
|
|
RAISE(EXT | SOFT | TIMER | LOCAL, 0)
|
|
li t1, 10
|
|
1: addi t1, t1, -1; bnez t1, 1b
|
|
csrsi mstatus, MSTATUS_MIE
|
|
WAIT_IRQ(6)
|
|
li t0, 0x8000000b; bne s1, t0, fail
|
|
li TESTNUM, 9
|
|
csrci mstatus, MSTATUS_MIE
|
|
RAISE(SOFT | TIMER | LOCAL, 0)
|
|
li t1, 10
|
|
1: addi t1, t1, -1; bnez t1, 1b
|
|
csrsi mstatus, MSTATUS_MIE
|
|
WAIT_IRQ(7)
|
|
li t0, 0x80000003; bne s1, t0, fail
|
|
li TESTNUM, 10
|
|
csrci mstatus, MSTATUS_MIE
|
|
RAISE(TIMER | LOCAL, 0)
|
|
li t1, 10
|
|
1: addi t1, t1, -1; bnez t1, 1b
|
|
csrsi mstatus, MSTATUS_MIE
|
|
WAIT_IRQ(8)
|
|
li t0, 0x80000007; bne s1, t0, fail
|
|
|
|
# a workout: once quietly, once with the timer firing every few cycles
|
|
li TESTNUM, 11
|
|
csrw mie, zero
|
|
jal workout
|
|
mv s5, a0
|
|
li s4, 1 # handler re-arms the timer
|
|
li t0, MIP_MTIP; csrw mie, t0
|
|
RAISE(TIMER, 3)
|
|
jal workout
|
|
csrw mie, zero
|
|
li s4, 0
|
|
bne a0, s5, fail
|
|
li t0, 100; blt s2, t0, fail # it really was interrupted a lot
|
|
|
|
TEST_PASSFAIL
|
|
|
|
# mixes ALU, mul, div, loads, stores and branches; result in a0
|
|
workout:
|
|
la a1, buf
|
|
li a0, 12345
|
|
li a2, 150
|
|
1:
|
|
mul a3, a0, a2
|
|
addi a3, a3, 77
|
|
divu a4, a3, a2
|
|
rem a5, a3, a2
|
|
xor a0, a0, a4
|
|
add a0, a0, a5
|
|
slli a6, a2, 2
|
|
andi a6, a6, 60
|
|
add a6, a6, a1
|
|
sw a0, 0(a6)
|
|
lh a7, 2(a6)
|
|
lbu a3, 1(a6)
|
|
add a0, a0, a7
|
|
sub a0, a0, a3
|
|
mulh a4, a0, a0
|
|
add a0, a0, a4
|
|
sb a0, 5(a6)
|
|
lw a4, 4(a6)
|
|
add a0, a0, a4
|
|
addi a2, a2, -1
|
|
bnez a2, 1b
|
|
ret
|
|
|
|
.align 2
|
|
.global mtvec_handler
|
|
mtvec_handler:
|
|
csrr s1, mcause
|
|
csrr s3, mstatus
|
|
bgez s1, fail # not an interrupt
|
|
addi s2, s2, 1
|
|
li t5, DEV
|
|
beqz s4, 1f
|
|
andi t6, s2, 31 # re-arm after 20..51 cycles
|
|
addi t6, t6, 20
|
|
slli t6, t6, 8
|
|
ori t6, t6, TIMER
|
|
sw t6, 0(t5)
|
|
mret
|
|
1:
|
|
sw zero, 0(t5)
|
|
mret
|
|
|
|
RVTEST_CODE_END
|
|
|
|
.data
|
|
RVTEST_DATA_BEGIN
|
|
TEST_DATA
|
|
buf: .fill 32, 4, 0
|
|
RVTEST_DATA_END
|