1 // See LICENSE for license details.
2 #ifndef _RISCV_PROCESSOR_H
3 #define _RISCV_PROCESSOR_H
14 typedef reg_t (*insn_func_t
)(processor_t
*, insn_t
, reg_t
);
28 struct commit_log_reg_t
46 // architectural state of a RISC-V hart
52 regfile_t
<reg_t
, NXPR
, true> XPR
;
53 regfile_t
<freg_t
, NFPR
, false> FPR
;
55 // control and status registers
82 bool serialized
; // whether timer CSRs are in a well-defined state
84 // When true, execute a single instruction and then enter debug mode. This
85 // can only be set by executing dret.
92 reg_t load_reservation
;
94 #ifdef RISCV_ENABLE_COMMITLOG
95 commit_log_reg_t log_reg_write
;
100 // this class represents one processor in a RISC-V machine.
101 class processor_t
: public abstract_device_t
104 processor_t(const char* isa
, sim_t
* sim
, uint32_t id
, bool halt_on_reset
=false);
107 void set_debug(bool value
);
108 void set_histogram(bool value
);
110 void step(size_t n
); // run for n cycles
111 void set_csr(int which
, reg_t val
);
112 void raise_interrupt(reg_t which
);
113 reg_t
get_csr(int which
);
114 mmu_t
* get_mmu() { return mmu
; }
115 state_t
* get_state() { return &state
; }
116 extension_t
* get_extension() { return ext
; }
117 bool supports_extension(unsigned char ext
) {
118 if (ext
>= 'a' && ext
<= 'z') ext
+= 'A' - 'a';
119 return ext
>= 'A' && ext
<= 'Z' && ((isa
>> (ext
- 'A')) & 1);
121 void set_privilege(reg_t
);
122 void yield_load_reservation() { state
.load_reservation
= (reg_t
)-1; }
123 void update_histogram(reg_t pc
);
124 const disassembler_t
* get_disassembler() { return disassembler
; }
126 void register_insn(insn_desc_t
);
127 void register_extension(extension_t
*);
129 // MMIO slave interface
130 bool load(reg_t addr
, size_t len
, uint8_t* bytes
);
131 bool store(reg_t addr
, size_t len
, const uint8_t* bytes
);
133 // When true, display disassembly of each instruction that's executed.
138 mmu_t
* mmu
; // main memory is always accessed via the mmu
140 disassembler_t
* disassembler
;
146 std::string isa_string
;
147 bool histogram_enabled
;
150 std::vector
<insn_desc_t
> instructions
;
151 std::map
<reg_t
,uint64_t> pc_histogram
;
153 static const size_t OPCODE_CACHE_SIZE
= 8191;
154 insn_desc_t opcode_cache
[OPCODE_CACHE_SIZE
];
157 void take_interrupt(); // take a trap if any interrupts are pending
158 void take_trap(trap_t
& t
, reg_t epc
); // take an exception
159 void disasm(insn_t insn
); // disassemble and print an instruction
162 void enter_debug_mode(uint8_t cause
);
167 friend class extension_t
;
169 void parse_isa_string(const char* isa
);
170 void build_opcode_map();
171 void register_base_instructions();
172 insn_func_t
decode_insn(insn_t insn
);
175 reg_t
illegal_instruction(processor_t
* p
, insn_t insn
, reg_t pc
);
177 #define REGISTER_INSN(proc, name, match, mask) \
178 extern reg_t rv32_##name(processor_t*, insn_t, reg_t); \
179 extern reg_t rv64_##name(processor_t*, insn_t, reg_t); \
180 proc->register_insn((insn_desc_t){match, mask, rv32_##name, rv64_##name});