Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetatz committed Nov 11, 2022
1 parent b465003 commit 03b2027
Showing 1 changed file with 46 additions and 64 deletions.
110 changes: 46 additions & 64 deletions cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -1688,7 +1688,7 @@ func (cpu *CPU) getCause(trap int, intr bool) uint64 {
return intrbit + uint64(trap)
}

func (cpu *CPU) handleExcp(excp *exception, curPC uint64) {
func (cpu *CPU) handleTrap(excp *exception, curPC uint64, intr bool) bool {
curMode := cpu.mode
cause := cpu.getCause(excp.code, false)

Expand Down Expand Up @@ -1741,78 +1741,60 @@ func (cpu *CPU) handleExcp(excp *exception, curPC uint64) {
}
}

func (cpu *CPU) handleExcp(excp *exception, curPC uint64) {
cpu.handleTrap(excp, curPC, false)
}

func (cpu *CPU) handleIntr(pc uint64) {
// @TODO: Optimize
//let minterrupt = self.read_csr_raw(CSR_MIP_ADDRESS) & self.read_csr_raw(CSR_MIE_ADDRESS);
mint := cpu.rcsr(mip) & cpu.rcsr(mie)

if mint&0x800 != 0 { // meip
if cpu.handleTrap(&exception{code: machineExternalIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x7ff)
cpu.wfi = false
return
}
}

if mint &0x008 != 0 { // msip
if cpu.handleTrap(&exception{code: machineSoftwareIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x0111)
cpu.wfi = false
return
}
}

if mint &0x080 != 0 { // mtip
if cpu.handleTrap(&exception{code: machineTimerIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x7f)
cpu.wfi = false
return
}
}

if mint &0x200!= 0 { // seip
if cpu.handleTrap(&exception{code: supervisorExternalIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x1ff)
cpu.wfi = false
return
}
}

if mint &0x002!= 0 { // ssip
if cpu.handleTrap(&exception{code: supervisorSoftwareIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x1)
cpu.wfi = false
return
}
}

//if (minterrupt & MIP_MEIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::MachineExternalInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// // Who should clear mip bit?
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_MEIP);
// self.wfi = false;
// return;
// }
//}
//if (minterrupt & MIP_MSIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::MachineSoftwareInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_MSIP);
// self.wfi = false;
// return;
// }
//}
//if (minterrupt & MIP_MTIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::MachineTimerInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_MTIP);
// self.wfi = false;
// return;
// }
//}
//if (minterrupt & MIP_SEIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::SupervisorExternalInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_SEIP);
// self.wfi = false;
// return;
// }
//}
//if (minterrupt & MIP_SSIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::SupervisorSoftwareInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_SSIP);
// self.wfi = false;
// return;
// }
//}
//if (minterrupt & MIP_STIP) != 0 {
// if self.handle_trap(Trap {
// trap_type: TrapType::SupervisorTimerInterrupt,
// value: self.pc // dummy
// }, instruction_address, true) {
// self.write_csr_raw(CSR_MIP_ADDRESS, self.read_csr_raw(CSR_MIP_ADDRESS) & !MIP_STIP);
// self.wfi = false;
// return;
// }
//}
if mint &0x020!= 0 { // stip
if cpu.handleTrap(&exception{code: supervisorTimerIntr}, pc, true) {
cpu.wcsr(mip, cpu.rcsr(mip) & 0x1f)
cpu.wfi = false
return
}
}
}

func parseIImm(inst uint64) uint64 {
Expand Down

0 comments on commit 03b2027

Please sign in to comment.