1 # This code should be functional. Doesn't have to be optimal.
2 # I'm writing it to prove that it can be done.
4 #include "riscv/encoding.h"
6 # TODO: Update these constants once they're finalized in the doc.
8 #define DEBUG_RAM 0x400
9 #define DEBUG_RAM_SIZE 64
11 #define CLEARDEBINT 0x100
12 #define SETHALTNOT 0x10c
18 # Automatically called when Debug Mode is first entered.
20 # Should be called by Debug RAM code that has finished execution and
21 # wants to return to Debug Mode.
25 # Set the last word of Debug RAM to all ones, to indicate that we hit
37 bltz s1, restore_not_32
39 lw s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 4)(zero)
45 ld s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 8)(zero)
48 nop #lq s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 16)(zero)
51 # s0 contains ~0 if we got here through an exception, and 0 otherwise.
52 # Store this to the last word in Debug RAM so the debugger can tell if
53 # an exception occurred.
54 sw s0, (DEBUG_RAM + DEBUG_RAM_SIZE - 4)(zero)
56 # Clear debug interrupt.
58 sw s0, CLEARDEBINT(zero)
62 andi s0, s0, DCSR_HALT
75 # Check why we're here
77 # cause is in bits 8:6 of dcsr
78 andi s0, s0, DCSR_CAUSE
79 addi s0, s0, -(DCSR_CAUSE_DEBUGINT<<6)
80 bnez s0, spontaneous_halt
83 # Save s1 so that the debug program can use two registers.
88 sw s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 4)(zero)
94 sd s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 8)(zero)
97 nop #sq s1, (DEBUG_RAM + DEBUG_RAM_SIZE - 16)(zero)
102 sw s0, SETHALTNOT(zero)
103 csrsi CSR_DCSR, DCSR_HALT
107 andi s0, s0, DCSR_DEBUGINT
108 beqz s0, wait_for_interrupt