add BSD license
[riscv-isa-sim.git] / riscv / processor.h
1 // See LICENSE for license details.
2
3 #ifndef _RISCV_PROCESSOR_H
4 #define _RISCV_PROCESSOR_H
5
6 #include "decode.h"
7 #include <cstring>
8 #include "trap.h"
9 #include "config.h"
10
11 #define MAX_UTS 2048
12
13 class processor_t;
14 class mmu_t;
15 typedef reg_t (*insn_func_t)(processor_t*, insn_t, reg_t);
16 class sim_t;
17
18 // this class represents one processor in a RISC-V machine.
19 class processor_t
20 {
21 public:
22 processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id);
23 ~processor_t();
24
25 void reset(bool value);
26 void step(size_t n, bool noisy); // run for n cycles
27 void deliver_ipi(); // register an interprocessor interrupt
28 bool running() { return run; }
29 void set_pcr(int which, reg_t val);
30 void set_interrupt(int which, bool on);
31 reg_t get_pcr(int which);
32 mmu_t* get_mmu() { return &mmu; }
33
34 private:
35 sim_t& sim;
36 mmu_t& mmu; // main memory is always accessed via the mmu
37
38 // user-visible architected state
39 regfile_t<reg_t, NXPR, true> XPR;
40 regfile_t<freg_t, NFPR, false> FPR;
41 reg_t pc;
42
43 // counters
44 reg_t cycle;
45
46 // privileged control registers
47 reg_t epc;
48 reg_t badvaddr;
49 reg_t evec;
50 reg_t pcr_k0;
51 reg_t pcr_k1;
52 reg_t cause;
53 reg_t tohost;
54 reg_t fromhost;
55 uint32_t id;
56 uint32_t sr; // only modify the status register using set_pcr()
57 uint32_t fsr;
58 uint32_t count;
59 uint32_t compare;
60
61 // # of bits in an XPR (32 or 64). (redundant with sr)
62 int xprlen;
63
64 bool run; // !reset
65
66 // functions
67 void take_interrupt(); // take a trap if any interrupts are pending
68 void set_fsr(uint32_t val); // set the floating-point status register
69 void take_trap(reg_t t, bool noisy); // take an exception
70 void disasm(insn_t insn, reg_t pc); // disassemble and print an instruction
71
72 // vector stuff
73 void vcfg();
74 void setvl(int vlapp);
75
76 reg_t vecbanks;
77 uint32_t vecbanks_count;
78
79 bool utmode;
80 uint32_t utidx;
81 int vlmax;
82 int vl;
83 int nxfpr_bank;
84 int nxpr_use;
85 int nfpr_use;
86 processor_t* uts[MAX_UTS];
87
88 // this constructor is used for each of the uts
89 processor_t(sim_t* _sim, mmu_t* _mmu, uint32_t _id, uint32_t _utidx);
90
91 friend class sim_t;
92 friend class mmu_t;
93 friend class htif_isasim_t;
94
95 #include "dispatch.h"
96 };
97
98 #ifndef RISCV_ENABLE_RVC
99 # define set_pc(x) \
100 do { if((x) & (sizeof(insn_t)-1)) \
101 { badvaddr = (x); throw trap_instruction_address_misaligned; } \
102 npc = (x); \
103 } while(0)
104 #else
105 # define set_pc(x) \
106 do { if((x) & ((sr & SR_EC) ? 1 : 3)) \
107 { badvaddr = (x); throw trap_instruction_address_misaligned; } \
108 npc = (x); \
109 } while(0)
110 #endif
111
112 #endif