实现中断与异常

This commit is contained in:
2025-04-18 19:18:49 +08:00
parent a096b91bc1
commit 38d433558d
11 changed files with 392 additions and 10 deletions

View File

@@ -197,6 +197,7 @@ typedef struct {
uint32_t rom_size;
uint32_t rom_addr_base;
uint32_t pc;
int32_t exc_code;
uint32_t reg[32];
uint32_t csrs[4096];
}riscv_t;
@@ -210,4 +211,37 @@ typedef struct {
#define rom_wrh(addr) ((uint16_t *)(riscv->rom))[(addr-riscv->rom_addr_base) >> 1]
// trap 相关定义
#define mstatus csrs[0x300] // 机器状态寄存器
#define misa csrs[0x301] // ISA扩展
#define medeleg csrs[0x302] // 异常委托寄存器
#define mideleg csrs[0x303] // 中断委托寄存器
#define mie csrs[0x304] // 中断使能寄存器
#define mtvec csrs[0x305] // 异常向量寄存器
#define mcounteren csrs[0x306] // 统计计数器使能寄存器
#define mstatush csrs[0x310] // 添加的状态寄存器高
#define mscratch csrs[0x340] // 存储器 软件中可用于暂时存放一个字大小的数据
#define mepc csrs[0x341] // 异常程序计数器 指向发生异常的指令
#define mcause csrs[0x342] // 异常原因寄存器
#define mtval csrs[0x343] // 异常值寄存器 地址例外出错的地址 非法指令本身
#define mip csrs[0x344] // 中断请求寄存器 它列出正在准备处理的中断
#define mtinst csrs[0x34a] // 存储器指令寄存器 当发生异常时,它包含异常指令
#define mtval2 csrs[0x34b] // 坏的物理地址
#define mcycle csrs[0xc00] // 循环计数器
#define minstret csrs[0xc02] // 执行指令计数器
// 只有机器模式的cpu只有这两个位起作用 清除这两个位表示禁用全局中断
#define MSTATUS_MIE 0x00000008 // 中断使能位
#define MSTATUS_MPIE 0x00000080 // 进异常时会自动清除MIE MPIE是清除之前的状态
#define MIP_MSIP (1 << IRQ_M_SOFT)
#define MIP_MTIP (1 << IRQ_M_TIMER)
#define MIP_MEIP (1 << IRQ_M_EXT)
#define IRQ_M_SOFT 3
#define IRQ_M_TIMER 7
#define IRQ_M_EXT 11
#endif