-
Notifications
You must be signed in to change notification settings - Fork 2
/
sys_defs.svh
355 lines (303 loc) · 9.05 KB
/
sys_defs.svh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/////////////////////////////////////////////////////////////////////////
// //
// Modulename : sys_defs.svh //
// //
// Description : This file has the macro-defines for macros used in //
// the pipeline design. //
// //
/////////////////////////////////////////////////////////////////////////
`ifndef __SYS_DEFS_VH__
`define __SYS_DEFS_VH__
/* Synthesis testing definition, used in DUT module instantiation */
`ifdef SYNTH_TEST
`define DUT(mod) mod``_svsim
`else
`define DUT(mod) mod
`endif
`define REGS 32
`define REG_LEN 64
`define PRF 64
`define ROB 32
`define RS 16
`define OLEN 16
`define PCLEN 32
`define WAYS 3
//////////////////////////////////////////////
//
// R10K algorithm
//
//////////////////////////////////////////////
`define LOGPRF 6 //$clog2(`PRF)
/*
`define BYTE 2'b0
`define HALF 2'h1
`define WORD 2'h2
`define DOUBLE 2'h3
`define MEM_SIZE [1:0]
*/
typedef enum logic [1:0] {
BUS_NONE = 2'h0,
BUS_LOAD = 2'h1,
BUS_STORE = 2'h2
} BUS_COMMAND;
typedef enum logic [1:0] {
BYTE = 2'h0,
HALF = 2'h1,
WORD = 2'h2,
DOUBLE = 2'h3
} MEM_SIZE;
`define LSQSZ 8
//////////////////////////////////////////////
//
// Memory/testbench attribute definitions
//
//////////////////////////////////////////////
`define NUM_MEM_TAGS 15
`define MEM_SIZE_IN_BYTES (64*1024)
`define MEM_64BIT_LINES (`MEM_SIZE_IN_BYTES/8)
//you can change the clock period to whatever, 10 is just fine
`define VERILOG_CLOCK_PERIOD 10.0
`define SYNTH_CLOCK_PERIOD 10.0 // Clock period for synth and memory latency
`define MEM_LATENCY_IN_CYCLES (100.0/`SYNTH_CLOCK_PERIOD+0.49999)
// the 0.49999 is to force ceiling(100/period). The default behavior for
// float to integer conversion is rounding to nearest
typedef union packed {
logic [7:0][7:0] byte_level;
logic [3:0][15:0] half_level;
logic [1:0][31:0] word_level;
} EXAMPLE_CACHE_BLOCK;
//////////////////////////////////////////////
// Exception codes
// This mostly follows the RISC-V Privileged spec
// except a few add-ons for our infrastructure
// The majority of them won't be used, but it's
// good to know what they are
//////////////////////////////////////////////
typedef enum logic [3:0] {
INST_ADDR_MISALIGN = 4'h0,
INST_ACCESS_FAULT = 4'h1,
ILLEGAL_INST = 4'h2,
BREAKPOINT = 4'h3,
LOAD_ADDR_MISALIGN = 4'h4,
LOAD_ACCESS_FAULT = 4'h5,
STORE_ADDR_MISALIGN = 4'h6,
STORE_ACCESS_FAULT = 4'h7,
ECALL_U_MODE = 4'h8,
ECALL_S_MODE = 4'h9,
NO_ERROR = 4'ha, //a reserved code that we modified for our purpose
ECALL_M_MODE = 4'hb,
INST_PAGE_FAULT = 4'hc,
LOAD_PAGE_FAULT = 4'hd,
HALTED_ON_WFI = 4'he, //another reserved code that we used
STORE_PAGE_FAULT = 4'hf
} EXCEPTION_CODE;
//////////////////////////////////////////////
//
// Datapath control signals
//
//////////////////////////////////////////////
//
// ALU opA input mux selects
//
typedef enum logic [1:0] {
OPA_IS_RS1 = 2'h0,
OPA_IS_NPC = 2'h1,
OPA_IS_PC = 2'h2,
OPA_IS_ZERO = 2'h3
} ALU_OPA_SELECT;
//
// ALU opB input mux selects
//
typedef enum logic [3:0] {
OPB_IS_RS2 = 4'h0,
OPB_IS_I_IMM = 4'h1,
OPB_IS_S_IMM = 4'h2,
OPB_IS_B_IMM = 4'h3,
OPB_IS_U_IMM = 4'h4,
OPB_IS_J_IMM = 4'h5
} ALU_OPB_SELECT;
//
// Destination register select
//
typedef enum logic [1:0] {
DEST_RD = 2'h0,
DEST_NONE = 2'h1
} DEST_REG_SEL;
//
// ALU function code input
// probably want to leave these alone
//
typedef enum logic [4:0] {
ALU_ADD = 5'h00,
ALU_SUB = 5'h01,
ALU_SLT = 5'h02,
ALU_SLTU = 5'h03,
ALU_AND = 5'h04,
ALU_OR = 5'h05,
ALU_XOR = 5'h06,
ALU_SLL = 5'h07,
ALU_SRL = 5'h08,
ALU_SRA = 5'h09,
ALU_MUL = 5'h0a,
ALU_MULH = 5'h0b,
ALU_MULHSU = 5'h0c,
ALU_MULHU = 5'h0d,
ALU_DIV = 5'h0e,
ALU_DIVU = 5'h0f,
ALU_REM = 5'h10,
ALU_REMU = 5'h11
} ALU_FUNC;
//////////////////////////////////////////////
//
// Assorted things it is not wise to change
//
//////////////////////////////////////////////
//
// actually, you might have to change this if you change VERILOG_CLOCK_PERIOD
// JK you don't ^^^
//
`define SD #1
// the RISCV register file zero register, any read of this register always
// returns a zero value, and any write to this register is thrown away
//
`define ZERO_REG 5'd0
//
// Memory bus commands control signals
//
//
// useful boolean single-bit definitions
//
`define FALSE 1'h0
`define TRUE 1'h1
// RISCV ISA SPEC
`define XLEN 32
typedef union packed {
logic [31:0] inst;
struct packed {
logic [6:0] funct7;
logic [4:0] rs2;
logic [4:0] rs1;
logic [2:0] funct3;
logic [4:0] rd;
logic [6:0] opcode;
} r; //register to register instructions
struct packed {
logic [11:0] imm;
logic [4:0] rs1; //base
logic [2:0] funct3;
logic [4:0] rd; //dest
logic [6:0] opcode;
} i; //immediate or load instructions
struct packed {
logic [6:0] off; //offset[11:5] for calculating address
logic [4:0] rs2; //source
logic [4:0] rs1; //base
logic [2:0] funct3;
logic [4:0] set; //offset[4:0] for calculating address
logic [6:0] opcode;
} s; //store instructions
struct packed {
logic of; //offset[12]
logic [5:0] s; //offset[10:5]
logic [4:0] rs2;//source 2
logic [4:0] rs1;//source 1
logic [2:0] funct3;
logic [3:0] et; //offset[4:1]
logic f; //offset[11]
logic [6:0] opcode;
} b; //branch instructions
struct packed {
logic [19:0] imm;
logic [4:0] rd;
logic [6:0] opcode;
} u; //upper immediate instructions
struct packed {
logic of; //offset[20]
logic [9:0] et; //offset[10:1]
logic s; //offset[11]
logic [7:0] f; //offset[19:12]
logic [4:0] rd; //dest
logic [6:0] opcode;
} j; //jump instructions
`ifdef ATOMIC_EXT
struct packed {
logic [4:0] funct5;
logic aq;
logic rl;
logic [4:0] rs2;
logic [4:0] rs1;
logic [2:0] funct3;
logic [4:0] rd;
logic [6:0] opcode;
} a; //atomic instructions
`endif
`ifdef SYSTEM_EXT
struct packed {
logic [11:0] csr;
logic [4:0] rs1;
logic [2:0] funct3;
logic [4:0] rd;
logic [6:0] opcode;
} sys; //system call instructions
`endif
} INST; //instruction typedef, this should cover all types of instructions
//
// Basic NOP instruction. Allows pipline registers to clearly be reset with
// an instruction that does nothing instead of Zero which is really an ADDI x0, x0, 0
//
`define NOP 32'h00000013
//////////////////////////////////////////////
//
// IF Packets:
// Data that is exchanged between the IF and the ID stages
//
//////////////////////////////////////////////
typedef struct packed {
logic valid; // If low, the data in this struct is garbage
INST inst; // fetched instruction out
logic [`XLEN-1:0] NPC; // PC + 4
logic [`XLEN-1:0] PC; // PC
} IF_ID_PACKET;
//////////////////////////////////////////////
//
// ID Packets:
// Data that is exchanged from ID to EX stage
//
//////////////////////////////////////////////
typedef struct packed {
logic [`XLEN-1:0] NPC; // PC + 4
logic [`XLEN-1:0] PC; // PC
logic [`XLEN-1:0] rs1_value; // reg A value
logic [`XLEN-1:0] rs2_value; // reg B value
ALU_OPA_SELECT opa_select; // ALU opa mux select (ALU_OPA_xxx *)
ALU_OPB_SELECT opb_select; // ALU opb mux select (ALU_OPB_xxx *)
INST inst; // instruction
logic [$clog2(`PRF)-1:0] dest_PRF_idx; // destination (writeback) register index
logic [$clog2(`ROB)-1:0] rob_idx;
logic [1:0] mem_size; // byte, half-word or word
logic reg_write;
ALU_FUNC alu_func; // ALU function select (ALU_xxx *)
logic rd_mem; // does inst read memory?
logic wr_mem; // does inst write memory?
logic cond_branch; // is inst a conditional branch?
logic uncond_branch; // is inst an unconditional branch?
logic halt; // is this a halt?
logic illegal; // is this instruction illegal? (unknown instruction)
logic csr_op; // is this a CSR operation? (we only used this as a cheap way to get return code)
logic valid; // is inst a valid instruction to be counted for CPI calculations?
} ID_EX_PACKET;
typedef struct packed {
logic [`XLEN-1:0] alu_result; // alu_result
logic [`XLEN-1:0] NPC; //pc + 4
logic take_branch; // is this a taken branch?
//pass throughs from decode stage
logic [`XLEN-1:0] rs2_value;
logic rd_mem;
logic wr_mem;
logic reg_write;
logic [1:0] mem_size; // byte, half-word or word
logic [$clog2(`PRF)-1:0] dest_PRF_idx;
logic [$clog2(`ROB)-1:0] rob_idx;
logic halt, illegal, csr_op, valid;
} EX_MEM_PACKET;
`endif // __SYS_DEFS_VH__