# Monitor start-up. The monitor has ROM at 20000-27FFF and RAM (data only) at # 28000-29FFF. It is entered by the NMI (20000h: the Pause key, MONITOR, a # breakpoint or a single step) or by a trap (20004h, BASIC's mtvec: an illegal # instruction, a misaligned access, ebreak, ecall, an unhandled interrupt). # The program's registers go to u (struct regs in monitor.c), the monitor runs, # and mnret takes the program back to u.pc with its registers as u has them. .equ U, 0x29F00 # struct regs, below RAM's last 256 bytes .equ INITED, 0x29FFC # C data is set up once, then kept .equ U_PC, 128 .equ U_HOW, 132 # 0 NMI, 1 trap .equ U_CAUSE, 136 .equ U_TVAL, 140 .equ U_STATUS, 144 .section .text.start, "ax" .globl _start, u .set u, U _start: j nmi_entry # 20000h j trap_entry # 20004h .macro SAVE_REGS sw x1, 4(sp) .irp n, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 sw x\n, 4*\n(sp) .endr csrr t0, 0x740 # mnscratch: the program's sp sw t0, 8(sp) sw zero, 0(sp) .endm nmi_entry: csrw 0x740, sp # mnscratch li sp, U SAVE_REGS csrr t0, 0x741 # mnepc sw t0, U_PC(sp) sw zero, U_HOW(sp) j common trap_entry: csrci 0x744, 8 # NMIE off first: no NMI may reuse mnscratch csrw 0x740, sp li sp, U SAVE_REGS csrr t0, mepc sw t0, U_PC(sp) li t0, 1 sw t0, U_HOW(sp) csrr t0, mcause sw t0, U_CAUSE(sp) csrr t0, mtval sw t0, U_TVAL(sp) csrr t0, mstatus sw t0, U_STATUS(sp) common: li sp, U # the monitor's stack, below u li t0, INITED lw t1, 0(t0) li t2, 0xA5A5 beq t1, t2, 3f la a0, __data_start # copy initialised data la a1, __data_load la a2, __data_end 1: bgeu a0, a2, 2f lw t1, 0(a1) sw t1, 0(a0) addi a0, a0, 4 addi a1, a1, 4 j 1b 2: la a0, __bss_start # clear the rest la a2, __bss_end 21: bgeu a0, a2, 22f sw zero, 0(a0) addi a0, a0, 4 j 21b 22: sw t2, 0(t0) 3: call mon_main li sp, U lw t0, U_PC(sp) csrw 0x741, t0 # mnepc lw t0, U_HOW(sp) beqz t0, 4f csrr t0, mstatus # from a trap: interrupts back as they were andi t0, t0, 0x80 beqz t0, 4f csrsi mstatus, 8 4: lw x1, 4(sp) .irp n, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 lw x\n, 4*\n(sp) .endr lw sp, 8(sp) .word 0x70200073 # mnret