1 // See LICENSE for license details.
15 processor_t::processor_t(sim_t
* _sim
, mmu_t
* _mmu
, uint32_t _id
)
16 : sim(*_sim
), mmu(*_mmu
), id(_id
), opcode_bits(0)
19 mmu
.set_processor(this);
21 #define DECLARE_INSN(name, match, mask) \
22 register_insn(match, mask, (insn_func_t)&processor_t::rv32_##name, (insn_func_t)&processor_t::rv64_##name);
27 processor_t::~processor_t()
31 void processor_t::reset(bool value
)
37 // the ISA guarantees on boot that the PC is 0x2000 and the the processor
38 // is in supervisor mode, and in 64-bit mode, if supported, with traps
39 // and virtual memory disabled.
41 set_pcr(PCR_SR
, SR_S
| SR_S64
| SR_IM
);
44 // the following state is undefined upon boot-up,
45 // but we zero it for determinism
61 void processor_t::set_fsr(uint32_t val
)
63 fsr
= val
& ~FSR_ZERO
; // clear FSR bits that read as zero
66 void processor_t::take_interrupt()
68 uint32_t interrupts
= (sr
& SR_IP
) >> SR_IP_SHIFT
;
69 interrupts
&= (sr
& SR_IM
) >> SR_IM_SHIFT
;
71 if(interrupts
&& (sr
& SR_ET
))
72 for(int i
= 0; ; i
++, interrupts
>>= 1)
77 void processor_t::step(size_t n
, bool noisy
)
90 // execute_insn fetches and executes one instruction
91 #define execute_insn(noisy) \
93 mmu_t::insn_fetch_t fetch = _mmu.load_insn(npc); \
94 if(noisy) disasm(fetch.insn, npc); \
95 npc = fetch.func(this, fetch.insn, npc); \
99 if(noisy
) for( ; i
< n
; i
++) // print out instructions as we go
103 // unrolled for speed
104 for( ; n
> 3 && i
< n
-3; i
+=4)
117 // an exception occurred in the target processor
122 take_trap((1ULL << ((sr
& SR_S64
) ? 63 : 31)) + t
.i
, noisy
);
127 // update timer and possibly register a timer interrupt
128 uint32_t old_count
= count
;
130 if(old_count
< compare
&& uint64_t(old_count
) + i
>= compare
)
131 set_interrupt(IRQ_TIMER
, true);
134 void processor_t::take_trap(reg_t t
, bool noisy
)
139 fprintf(stderr
, "core %3d: interrupt %d, epc 0x%016" PRIx64
"\n",
142 fprintf(stderr
, "core %3d: trap %s, epc 0x%016" PRIx64
"\n",
143 id
, trap_name(trap_t(t
)), pc
);
146 // switch to supervisor, set previous supervisor bit, disable traps
147 set_pcr(PCR_SR
, (((sr
& ~SR_ET
) | SR_S
) & ~SR_PS
) | ((sr
& SR_S
) ? SR_PS
: 0));
151 badvaddr
= mmu
.get_badvaddr();
154 void processor_t::deliver_ipi()
157 set_pcr(PCR_CLR_IPI
, 1);
160 void processor_t::disasm(insn_t insn
, reg_t pc
)
162 // the disassembler is stateless, so we share it
163 static disassembler disasm
;
164 fprintf(stderr
, "core %3d: 0x%016" PRIx64
" (0x%08" PRIxFAST32
") %s\n",
165 id
, pc
, insn
.bits
, disasm
.disassemble(insn
).c_str());
168 void processor_t::set_pcr(int which
, reg_t val
)
173 sr
= (val
& ~SR_IP
) | (sr
& SR_IP
);
174 #ifndef RISCV_ENABLE_64BIT
175 sr
&= ~(SR_S64
| SR_U64
);
177 #ifndef RISCV_ENABLE_FPU
180 #ifndef RISCV_ENABLE_RVC
183 #ifndef RISCV_ENABLE_VEC
199 set_interrupt(IRQ_TIMER
, false);
209 set_interrupt(IRQ_IPI
, val
& 1);
222 set_interrupt(IRQ_HOST
, val
!= 0);
228 reg_t
processor_t::get_pcr(int which
)
247 return mmu
.get_ptbr();
264 void processor_t::set_interrupt(int which
, bool on
)
266 uint32_t mask
= (1 << (which
+ SR_IP_SHIFT
)) & SR_IP
;
273 insn_func_t
processor_t::decode_insn(insn_t insn
)
275 bool rv64
= (sr
& SR_S
) ? (sr
& SR_S64
) : (sr
& SR_U64
);
277 auto key
= insn
.bits
& ((1L << opcode_bits
)-1);
278 auto it
= opcode_map
.find(key
);
279 for (auto it
= opcode_map
.find(key
); it
!= opcode_map
.end() && it
->first
== key
; ++it
)
280 if ((insn
.bits
& it
->second
.mask
) == it
->second
.match
)
281 return rv64
? it
->second
.rv64
: it
->second
.rv32
;
283 return &processor_t::illegal_instruction
;
286 reg_t
processor_t::illegal_instruction(insn_t insn
, reg_t pc
)
288 throw trap_illegal_instruction
;
291 void processor_t::register_insn(uint32_t match
, uint32_t mask
, insn_func_t rv32
, insn_func_t rv64
)
294 if (opcode_bits
== 0 || (mask
& ((1L << opcode_bits
)-1)) != ((1L << opcode_bits
)-1))
297 while ((mask
& ((1L << (x
+1))-1)) == ((1L << (x
+1))-1) &&
298 (opcode_bits
== 0 || x
<= opcode_bits
))
302 decltype(opcode_map
) new_map
;
303 for (auto it
= opcode_map
.begin(); it
!= opcode_map
.end(); ++it
)
304 new_map
.insert(std::make_pair(it
->second
.match
& ((1L<<x
)-1), it
->second
));
305 opcode_map
= new_map
;
308 opcode_map
.insert(std::make_pair(match
& ((1L<<opcode_bits
)-1),
309 (opcode_map_entry_t
){match
, mask
, rv32
, rv64
}));