OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_processor/] [new_lm32/] [rtl/] [lm32_cpu.v] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
//   ==================================================================
2
//   >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<
3
//   ------------------------------------------------------------------
4
//   Copyright (c) 2006-2011 by Lattice Semiconductor Corporation
5
//   Copyright (c) 2011-2012 Yann Sionneau <yann.sionneau@gmail.com>
6
//   Copyright (c) 2012 Michael Walle <michael@walle.cc>
7
//   ALL RIGHTS RESERVED
8
//   ------------------------------------------------------------------
9
//
10
//   IMPORTANT: THIS FILE IS AUTO-GENERATED BY THE LATTICEMICO SYSTEM.
11
//
12
//   Permission:
13
//
14
//      Lattice Semiconductor grants permission to use this code
15
//      pursuant to the terms of the Lattice Semiconductor Corporation
16
//      Open Source License Agreement.
17
//
18
//   Disclaimer:
19
//
20
//      Lattice Semiconductor provides no warranty regarding the use or
21
//      functionality of this code. It is the user's responsibility to
22
//      verify the user's design for consistency and functionality through
23
//      the use of formal verification methods.
24
//
25
//   --------------------------------------------------------------------
26
//
27
//                  Lattice Semiconductor Corporation
28
//                  5555 NE Moore Court
29
//                  Hillsboro, OR 97214
30
//                  U.S.A
31
//
32
//                  TEL: 1-800-Lattice (USA and Canada)
33
//                         503-286-8001 (other locations)
34
//
35
//                  web: http://www.latticesemi.com/
36
//                  email: techsupport@latticesemi.com
37
//
38
//   --------------------------------------------------------------------
39
//                         FILE DETAILS
40
// Project          : LatticeMico32
41
// File             : lm32_cpu.v
42
// Title            : Top-level of CPU.
43
// Dependencies     : lm32_include.v
44
//
45
// Version 3.8
46
// 1. Feature: Support for dynamically switching EBA to DEBA via a GPIO.
47
// 2. Bug: EA now reports instruction that caused the data abort, rather than
48
//    next instruction.
49
//
50
// Version 3.4
51
// 1. Bug Fix: In a tight infinite loop (add, sw, bi) incoming interrupts were
52
//    never serviced.
53
//
54
// Version 3.3
55
// 1. Feature: Support for memory that is tightly coupled to processor core, and
56
//    has a single-cycle access latency (same as caches). Instruction port has
57
//    access to a dedicated physically-mapped memory. Data port has access to
58
//    a dedicated physically-mapped memory. In order to be able to manipulate
59
//    values in both these memories via the debugger, these memories also
60
//    interface with the data port of LM32.
61
// 2. Feature: Extended Configuration Register
62
// 3. Bug Fix: Removed port names that conflict with keywords reserved in System-
63
//    Verilog.
64
//
65
// Version 3.2
66
// 1. Bug Fix: Single-stepping a load/store to invalid address causes debugger to
67
//    hang. At the same time CPU fails to register data bus error exception. Bug
68
//    is caused because (a) data bus error exception occurs after load/store has
69
//    passed X stage and next sequential instruction (e.g., brk) is already in X
70
//    stage, and (b) data bus error exception had lower priority than, say, brk
71
//    exception.
72
// 2. Bug Fix: If a brk (or scall/eret/bret) sequentially follows a load/store to
73
//    invalid location, CPU will fail to register data bus error exception. The
74
//    solution is to stall scall/eret/bret/brk instructions in D pipeline stage
75
//    until load/store has completed.
76
// 3. Feature: Enable precise identification of load/store that causes seg fault.
77
// 4. SYNC resets used for register file when implemented in EBRs.
78
//
79
// Version 3.1
80
// 1. Feature: LM32 Register File can now be mapped in to on-chip block RAM (EBR)
81
//    instead of distributed memory by enabling the option in LM32 GUI.
82
// 2. Feature: LM32 also adds a static branch predictor to improve branch
83
//    performance. All immediate-based forward-pointing branches are predicted
84
//    not-taken. All immediate-based backward-pointing branches are predicted taken.
85
//
86
// Version 7.0SP2, 3.0
87
// No Change
88
//
89
// Version 6.1.17
90
// Initial Release
91
// =============================================================================
92
 
93
`include "lm32_include.v"
94
 
95
/////////////////////////////////////////////////////
96
// Module interface
97
/////////////////////////////////////////////////////
98
 
99
module lm32_cpu (
100
    // ----- Inputs -------
101
    clk_i,
102
`ifdef CFG_EBR_NEGEDGE_REGISTER_FILE
103
    clk_n_i,
104
`endif
105
    rst_i,
106
`ifdef CFG_DEBUG_ENABLED
107
`ifdef CFG_ALTERNATE_EBA
108
    at_debug,
109
`endif
110
`endif
111
    // From external devices
112
`ifdef CFG_INTERRUPTS_ENABLED
113
    interrupt,
114
`endif
115
    // From user logic
116
`ifdef CFG_USER_ENABLED
117
    user_result,
118
    user_complete,
119
`endif
120
`ifdef CFG_JTAG_ENABLED
121
    // From JTAG
122
    jtag_clk,
123
    jtag_update,
124
    jtag_reg_q,
125
    jtag_reg_addr_q,
126
`endif
127
`ifdef CFG_EXTERNAL_BREAK_ENABLED
128
    ext_break,
129
`endif
130
`ifdef CFG_IWB_ENABLED
131
    // Instruction Wishbone master
132
    I_DAT_I,
133
    I_ACK_I,
134
    I_ERR_I,
135
    I_RTY_I,
136
`endif
137
    // Data Wishbone master
138
    D_DAT_I,
139
    D_ACK_I,
140
    D_ERR_I,
141
    D_RTY_I,
142
    // ----- Outputs -------
143
`ifdef CFG_TRACE_ENABLED
144
    trace_pc,
145
    trace_pc_valid,
146
    trace_exception,
147
    trace_eid,
148
    trace_eret,
149
`ifdef CFG_DEBUG_ENABLED
150
    trace_bret,
151
`endif
152
`endif
153
`ifdef CFG_JTAG_ENABLED
154
    jtag_reg_d,
155
    jtag_reg_addr_d,
156
`endif
157
`ifdef CFG_USER_ENABLED
158
    user_valid,
159
    user_opcode,
160
    user_operand_0,
161
    user_operand_1,
162
`endif
163
`ifdef CFG_IWB_ENABLED
164
    // Instruction Wishbone master
165
    I_DAT_O,
166
    I_ADR_O,
167
    I_CYC_O,
168
    I_SEL_O,
169
    I_STB_O,
170
    I_WE_O,
171
    I_CTI_O,
172
    I_LOCK_O,
173
    I_BTE_O,
174
`endif
175
    // Data Wishbone master
176
    D_DAT_O,
177
    D_ADR_O,
178
    D_CYC_O,
179
    D_SEL_O,
180
    D_STB_O,
181
    D_WE_O,
182
    D_CTI_O,
183
    D_LOCK_O,
184
    D_BTE_O
185
    );
186
 
187
/////////////////////////////////////////////////////
188
// Parameters
189
/////////////////////////////////////////////////////
190
 
191
parameter eba_reset = `CFG_EBA_RESET;                           // Reset value for EBA CSR
192
`ifdef CFG_DEBUG_ENABLED
193
parameter deba_reset = `CFG_DEBA_RESET;                         // Reset value for DEBA CSR
194
`endif
195
 
196
`ifdef CFG_ICACHE_ENABLED
197
parameter icache_associativity = `CFG_ICACHE_ASSOCIATIVITY;     // Associativity of the cache (Number of ways)
198
parameter icache_sets = `CFG_ICACHE_SETS;                       // Number of sets
199
parameter icache_bytes_per_line = `CFG_ICACHE_BYTES_PER_LINE;   // Number of bytes per cache line
200
parameter icache_base_address = `CFG_ICACHE_BASE_ADDRESS;       // Base address of cachable memory
201
parameter icache_limit = `CFG_ICACHE_LIMIT;                     // Limit (highest address) of cachable memory
202
`else
203
parameter icache_associativity = 1;
204
parameter icache_sets = 512;
205
parameter icache_bytes_per_line = 16;
206
parameter icache_base_address = 0;
207
parameter icache_limit = 0;
208
`endif
209
 
210
`ifdef CFG_DCACHE_ENABLED
211
parameter dcache_associativity = `CFG_DCACHE_ASSOCIATIVITY;     // Associativity of the cache (Number of ways)
212
parameter dcache_sets = `CFG_DCACHE_SETS;                       // Number of sets
213
parameter dcache_bytes_per_line = `CFG_DCACHE_BYTES_PER_LINE;   // Number of bytes per cache line
214
parameter dcache_base_address = `CFG_DCACHE_BASE_ADDRESS;       // Base address of cachable memory
215
parameter dcache_limit = `CFG_DCACHE_LIMIT;                     // Limit (highest address) of cachable memory
216
`else
217
parameter dcache_associativity = 1;
218
parameter dcache_sets = 512;
219
parameter dcache_bytes_per_line = 16;
220
parameter dcache_base_address = 0;
221
parameter dcache_limit = 0;
222
`endif
223
 
224
`ifdef CFG_DEBUG_ENABLED
225
parameter watchpoints = `CFG_WATCHPOINTS;                       // Number of h/w watchpoint CSRs
226
`else
227
parameter watchpoints = 0;
228
`endif
229
`ifdef CFG_ROM_DEBUG_ENABLED
230
parameter breakpoints = `CFG_BREAKPOINTS;                       // Number of h/w breakpoint CSRs
231
`else
232
parameter breakpoints = 0;
233
`endif
234
 
235
`ifdef CFG_INTERRUPTS_ENABLED
236
parameter interrupts = `CFG_INTERRUPTS;                         // Number of interrupts
237
`else
238
parameter interrupts = 0;
239
`endif
240
 
241
/////////////////////////////////////////////////////
242
// Inputs
243
/////////////////////////////////////////////////////
244
 
245
input clk_i;                                    // Clock
246
`ifdef CFG_EBR_NEGEDGE_REGISTER_FILE
247
input clk_n_i;                                  // Inverted clock
248
`endif
249
input rst_i;                                    // Reset
250
 
251
`ifdef CFG_DEBUG_ENABLED
252
`ifdef CFG_ALTERNATE_EBA
253
input at_debug;                                 // GPIO input that maps EBA to DEBA
254
`endif
255
`endif
256
 
257
`ifdef CFG_INTERRUPTS_ENABLED
258
input [`LM32_INTERRUPT_RNG] interrupt;          // Interrupt pins
259
`endif
260
 
261
`ifdef CFG_USER_ENABLED
262
input [`LM32_WORD_RNG] user_result;             // User-defined instruction result
263
input user_complete;                            // User-defined instruction execution is complete
264
`endif
265
 
266
`ifdef CFG_JTAG_ENABLED
267
input jtag_clk;                                 // JTAG clock
268
input jtag_update;                              // JTAG state machine is in data register update state
269
input [`LM32_BYTE_RNG] jtag_reg_q;
270
input [2:0] jtag_reg_addr_q;
271
`endif
272
 
273
`ifdef CFG_IWB_ENABLED
274
input [`LM32_WORD_RNG] I_DAT_I;                 // Instruction Wishbone interface read data
275
input I_ACK_I;                                  // Instruction Wishbone interface acknowledgement
276
input I_ERR_I;                                  // Instruction Wishbone interface error
277
input I_RTY_I;                                  // Instruction Wishbone interface retry
278
`endif
279
 
280
input [`LM32_WORD_RNG] D_DAT_I;                 // Data Wishbone interface read data
281
input D_ACK_I;                                  // Data Wishbone interface acknowledgement
282
input D_ERR_I;                                  // Data Wishbone interface error
283
input D_RTY_I;                                  // Data Wishbone interface retry
284
 
285
`ifdef CFG_EXTERNAL_BREAK_ENABLED
286
input ext_break;
287
`endif
288
 
289
/////////////////////////////////////////////////////
290
// Outputs
291
/////////////////////////////////////////////////////
292
 
293
`ifdef CFG_TRACE_ENABLED
294
output [`LM32_PC_RNG] trace_pc;                 // PC to trace
295
reg    [`LM32_PC_RNG] trace_pc;
296
output trace_pc_valid;                          // Indicates that a new trace PC is valid
297
reg    trace_pc_valid;
298
output trace_exception;                         // Indicates an exception has occured
299
reg    trace_exception;
300
output [`LM32_EID_RNG] trace_eid;               // Indicates what type of exception has occured
301
reg    [`LM32_EID_RNG] trace_eid;
302
output trace_eret;                              // Indicates an eret instruction has been executed
303
reg    trace_eret;
304
`ifdef CFG_DEBUG_ENABLED
305
output trace_bret;                              // Indicates a bret instruction has been executed
306
reg    trace_bret;
307
`endif
308
`endif
309
 
310
`ifdef CFG_JTAG_ENABLED
311
output [`LM32_BYTE_RNG] jtag_reg_d;
312
wire   [`LM32_BYTE_RNG] jtag_reg_d;
313
output [2:0] jtag_reg_addr_d;
314
wire   [2:0] jtag_reg_addr_d;
315
`endif
316
 
317
`ifdef CFG_USER_ENABLED
318
output user_valid;                              // Indicates if user_opcode is valid
319
wire   user_valid;
320
output [`LM32_USER_OPCODE_RNG] user_opcode;     // User-defined instruction opcode
321
reg    [`LM32_USER_OPCODE_RNG] user_opcode;
322
output [`LM32_WORD_RNG] user_operand_0;         // First operand for user-defined instruction
323
wire   [`LM32_WORD_RNG] user_operand_0;
324
output [`LM32_WORD_RNG] user_operand_1;         // Second operand for user-defined instruction
325
wire   [`LM32_WORD_RNG] user_operand_1;
326
`endif
327
 
328
`ifdef CFG_IWB_ENABLED
329
output [`LM32_WORD_RNG] I_DAT_O;                // Instruction Wishbone interface write data
330
wire   [`LM32_WORD_RNG] I_DAT_O;
331
output [`LM32_WORD_RNG] I_ADR_O;                // Instruction Wishbone interface address
332
wire   [`LM32_WORD_RNG] I_ADR_O;
333
output I_CYC_O;                                 // Instruction Wishbone interface cycle
334
wire   I_CYC_O;
335
output [`LM32_BYTE_SELECT_RNG] I_SEL_O;         // Instruction Wishbone interface byte select
336
wire   [`LM32_BYTE_SELECT_RNG] I_SEL_O;
337
output I_STB_O;                                 // Instruction Wishbone interface strobe
338
wire   I_STB_O;
339
output I_WE_O;                                  // Instruction Wishbone interface write enable
340
wire   I_WE_O;
341
output [`LM32_CTYPE_RNG] I_CTI_O;               // Instruction Wishbone interface cycle type
342
wire   [`LM32_CTYPE_RNG] I_CTI_O;
343
output I_LOCK_O;                                // Instruction Wishbone interface lock bus
344
wire   I_LOCK_O;
345
output [`LM32_BTYPE_RNG] I_BTE_O;               // Instruction Wishbone interface burst type
346
wire   [`LM32_BTYPE_RNG] I_BTE_O;
347
`endif
348
 
349
output [`LM32_WORD_RNG] D_DAT_O;                // Data Wishbone interface write data
350
wire   [`LM32_WORD_RNG] D_DAT_O;
351
output [`LM32_WORD_RNG] D_ADR_O;                // Data Wishbone interface address
352
wire   [`LM32_WORD_RNG] D_ADR_O;
353
output D_CYC_O;                                 // Data Wishbone interface cycle
354
wire   D_CYC_O;
355
output [`LM32_BYTE_SELECT_RNG] D_SEL_O;         // Data Wishbone interface byte select
356
wire   [`LM32_BYTE_SELECT_RNG] D_SEL_O;
357
output D_STB_O;                                 // Data Wishbone interface strobe
358
wire   D_STB_O;
359
output D_WE_O;                                  // Data Wishbone interface write enable
360
wire   D_WE_O;
361
output [`LM32_CTYPE_RNG] D_CTI_O;               // Data Wishbone interface cycle type
362
wire   [`LM32_CTYPE_RNG] D_CTI_O;
363
output D_LOCK_O;                                // Date Wishbone interface lock bus
364
wire   D_LOCK_O;
365
output [`LM32_BTYPE_RNG] D_BTE_O;               // Data Wishbone interface burst type
366
wire   [`LM32_BTYPE_RNG] D_BTE_O;
367
 
368
/////////////////////////////////////////////////////
369
// Internal nets and registers
370
/////////////////////////////////////////////////////
371
 
372
// Pipeline registers
373
 
374
`ifdef LM32_CACHE_ENABLED
375
reg valid_a;                                    // Instruction in A stage is valid
376
`endif
377
reg valid_f;                                    // Instruction in F stage is valid
378
reg valid_d;                                    // Instruction in D stage is valid
379
reg valid_x;                                    // Instruction in X stage is valid
380
reg valid_m;                                    // Instruction in M stage is valid
381
reg valid_w;                                    // Instruction in W stage is valid
382
 
383
wire q_x;
384
wire [`LM32_WORD_RNG] immediate_d;              // Immediate operand
385
wire load_d;                                    // Indicates a load instruction
386
reg load_x;
387
reg load_m;
388
wire load_q_x;
389
wire store_q_x;
390
wire store_d;                                   // Indicates a store instruction
391
reg store_x;
392
reg store_m;
393
wire [`LM32_SIZE_RNG] size_d;                   // Size of load/store (byte, hword, word)
394
reg [`LM32_SIZE_RNG] size_x;
395
wire branch_d;                                  // Indicates a branch instruction
396
wire branch_predict_d;                          // Indicates a branch is predicted
397
wire branch_predict_taken_d;                    // Indicates a branch is predicted taken
398
wire [`LM32_PC_RNG] branch_predict_address_d;   // Address to which predicted branch jumps
399
wire [`LM32_PC_RNG] branch_target_d;
400
wire bi_unconditional;
401
wire bi_conditional;
402
reg branch_x;
403
reg branch_predict_x;
404
reg branch_predict_taken_x;
405
reg branch_m;
406
reg branch_predict_m;
407
reg branch_predict_taken_m;
408
wire branch_mispredict_taken_m;                 // Indicates a branch was mispredicted as taken
409
wire branch_flushX_m;                           // Indicates that instruction in X stage must be squashed
410
wire branch_reg_d;                              // Branch to register or immediate
411
wire [`LM32_PC_RNG] branch_offset_d;            // Branch offset for immediate branches
412
reg [`LM32_PC_RNG] branch_target_x;             // Address to branch to
413
reg [`LM32_PC_RNG] branch_target_m;
414
wire [`LM32_D_RESULT_SEL_0_RNG] d_result_sel_0_d; // Which result should be selected in D stage for operand 0
415
wire [`LM32_D_RESULT_SEL_1_RNG] d_result_sel_1_d; // Which result should be selected in D stage for operand 1
416
 
417
wire x_result_sel_csr_d;                        // Select X stage result from CSRs
418
reg x_result_sel_csr_x;
419
`ifdef LM32_MC_ARITHMETIC_ENABLED
420
wire x_result_sel_mc_arith_d;                   // Select X stage result from multi-cycle arithmetic unit
421
reg x_result_sel_mc_arith_x;
422
`endif
423
`ifdef LM32_NO_BARREL_SHIFT
424
wire x_result_sel_shift_d;                      // Select X stage result from shifter
425
reg x_result_sel_shift_x;
426
`endif
427
`ifdef CFG_SIGN_EXTEND_ENABLED
428
wire x_result_sel_sext_d;                       // Select X stage result from sign-extend logic
429
reg x_result_sel_sext_x;
430
`endif
431
wire x_result_sel_logic_d;                      // Select X stage result from logic op unit
432
reg x_result_sel_logic_x;
433
`ifdef CFG_USER_ENABLED
434
wire x_result_sel_user_d;                       // Select X stage result from user-defined logic
435
reg x_result_sel_user_x;
436
`endif
437
wire x_result_sel_add_d;                        // Select X stage result from adder
438
reg x_result_sel_add_x;
439
wire m_result_sel_compare_d;                    // Select M stage result from comparison logic
440
reg m_result_sel_compare_x;
441
reg m_result_sel_compare_m;
442
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
443
wire m_result_sel_shift_d;                      // Select M stage result from shifter
444
reg m_result_sel_shift_x;
445
reg m_result_sel_shift_m;
446
`endif
447
wire w_result_sel_load_d;                       // Select W stage result from load/store unit
448
reg w_result_sel_load_x;
449
reg w_result_sel_load_m;
450
reg w_result_sel_load_w;
451
`ifdef CFG_PL_MULTIPLY_ENABLED
452
wire w_result_sel_mul_d;                        // Select W stage result from multiplier
453
reg w_result_sel_mul_x;
454
reg w_result_sel_mul_m;
455
reg w_result_sel_mul_w;
456
`endif
457
wire x_bypass_enable_d;                         // Whether result is bypassable in X stage
458
reg x_bypass_enable_x;
459
wire m_bypass_enable_d;                         // Whether result is bypassable in M stage
460
reg m_bypass_enable_x;
461
reg m_bypass_enable_m;
462
wire sign_extend_d;                             // Whether to sign-extend or zero-extend
463
reg sign_extend_x;
464
wire write_enable_d;                            // Register file write enable
465
reg write_enable_x;
466
wire write_enable_q_x;
467
reg write_enable_m;
468
wire write_enable_q_m;
469
reg write_enable_w;
470
wire write_enable_q_w;
471
wire read_enable_0_d;                           // Register file read enable 0
472
wire [`LM32_REG_IDX_RNG] read_idx_0_d;          // Register file read index 0
473
wire read_enable_1_d;                           // Register file read enable 1
474
wire [`LM32_REG_IDX_RNG] read_idx_1_d;          // Register file read index 1
475
wire [`LM32_REG_IDX_RNG] write_idx_d;           // Register file write index
476
reg [`LM32_REG_IDX_RNG] write_idx_x;
477
reg [`LM32_REG_IDX_RNG] write_idx_m;
478
reg [`LM32_REG_IDX_RNG] write_idx_w;
479
wire [`LM32_CSR_RNG] csr_d;                     // CSR read/write index
480
reg  [`LM32_CSR_RNG] csr_x;
481
wire [`LM32_CONDITION_RNG] condition_d;         // Branch condition
482
reg [`LM32_CONDITION_RNG] condition_x;
483
`ifdef CFG_DEBUG_ENABLED
484
wire break_d;                                   // Indicates a break instruction
485
reg break_x;
486
`endif
487
wire scall_d;                                   // Indicates a scall instruction
488
reg scall_x;
489
wire eret_d;                                    // Indicates an eret instruction
490
reg eret_x;
491
wire eret_q_x;
492
`ifdef CFG_MMU_ENABLED
493
wire eret_k_q_x;
494
`endif
495
reg eret_m;
496
`ifdef CFG_TRACE_ENABLED
497
reg eret_w;
498
`endif
499
`ifdef CFG_DEBUG_ENABLED
500
wire bret_d;                                    // Indicates a bret instruction
501
reg bret_x;
502
wire bret_q_x;
503
`ifdef CFG_MMU_ENABLED
504
wire bret_k_q_x;
505
`endif
506
reg bret_m;
507
`ifdef CFG_TRACE_ENABLED
508
reg bret_w;
509
`endif
510
`endif
511
wire csr_write_enable_d;                        // CSR write enable
512
reg csr_write_enable_x;
513
wire csr_write_enable_q_x;
514
`ifdef CFG_MMU_ENABLED
515
wire csr_write_enable_k_q_x;
516
`endif
517
`ifdef CFG_USER_ENABLED
518
wire [`LM32_USER_OPCODE_RNG] user_opcode_d;     // User-defined instruction opcode
519
`endif
520
 
521
`ifdef CFG_BUS_ERRORS_ENABLED
522
wire bus_error_d;                               // Indicates an bus error occured while fetching the instruction in this pipeline stage
523
reg bus_error_x;
524
reg data_bus_error_exception_m;
525
reg [`LM32_PC_RNG] memop_pc_w;
526
`endif
527
 
528
reg [`LM32_WORD_RNG] d_result_0;                // Result of instruction in D stage (operand 0)
529
reg [`LM32_WORD_RNG] d_result_1;                // Result of instruction in D stage (operand 1)
530
reg [`LM32_WORD_RNG] x_result;                  // Result of instruction in X stage
531
reg [`LM32_WORD_RNG] m_result;                  // Result of instruction in M stage
532
reg [`LM32_WORD_RNG] w_result;                  // Result of instruction in W stage
533
 
534
reg [`LM32_WORD_RNG] operand_0_x;               // Operand 0 for X stage instruction
535
reg [`LM32_WORD_RNG] operand_1_x;               // Operand 1 for X stage instruction
536
reg [`LM32_WORD_RNG] store_operand_x;           // Data read from register to store
537
reg [`LM32_WORD_RNG] operand_m;                 // Operand for M stage instruction
538
reg [`LM32_WORD_RNG] operand_w;                 // Operand for W stage instruction
539
 
540
// To/from register file
541
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
542
reg [`LM32_WORD_RNG] reg_data_live_0;
543
reg [`LM32_WORD_RNG] reg_data_live_1;
544
reg use_buf;                                    // Whether to use reg_data_live or reg_data_buf
545
reg [`LM32_WORD_RNG] reg_data_buf_0;
546
reg [`LM32_WORD_RNG] reg_data_buf_1;
547
`endif
548
`ifdef LM32_EBR_REGISTER_FILE
549
`else
550
reg [`LM32_WORD_RNG] registers[0:(1<<`LM32_REG_IDX_WIDTH)-1];   // Register file
551
`endif
552
wire [`LM32_WORD_RNG] reg_data_0;               // Register file read port 0 data
553
wire [`LM32_WORD_RNG] reg_data_1;               // Register file read port 1 data
554
reg [`LM32_WORD_RNG] bypass_data_0;             // Register value 0 after bypassing
555
reg [`LM32_WORD_RNG] bypass_data_1;             // Register value 1 after bypassing
556
wire reg_write_enable_q_w;
557
 
558
reg interlock;                                  // Indicates pipeline should be stalled because of a read-after-write hazzard
559
 
560
wire stall_a;                                   // Stall instruction in A pipeline stage
561
wire stall_f;                                   // Stall instruction in F pipeline stage
562
wire stall_d;                                   // Stall instruction in D pipeline stage
563
wire stall_x;                                   // Stall instruction in X pipeline stage
564
wire stall_m;                                   // Stall instruction in M pipeline stage
565
 
566
// To/from adder
567
wire adder_op_d;                                // Whether to add or subtract
568
reg adder_op_x;
569
reg adder_op_x_n;                               // Inverted version of adder_op_x
570
wire [`LM32_WORD_RNG] adder_result_x;           // Result from adder
571
wire adder_overflow_x;                          // Whether a signed overflow occured
572
wire adder_carry_n_x;                           // Whether a carry was generated
573
 
574
// To/from logical operations unit
575
wire [`LM32_LOGIC_OP_RNG] logic_op_d;           // Which operation to perform
576
reg [`LM32_LOGIC_OP_RNG] logic_op_x;
577
wire [`LM32_WORD_RNG] logic_result_x;           // Result of logical operation
578
 
579
`ifdef CFG_SIGN_EXTEND_ENABLED
580
// From sign-extension unit
581
wire [`LM32_WORD_RNG] sextb_result_x;           // Result of byte sign-extension
582
wire [`LM32_WORD_RNG] sexth_result_x;           // Result of half-word sign-extenstion
583
wire [`LM32_WORD_RNG] sext_result_x;            // Result of sign-extension specified by instruction
584
`endif
585
 
586
// To/from shifter
587
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
588
wire direction_d;                               // Which direction to shift in
589
reg direction_x;
590
wire [`LM32_WORD_RNG] shifter_result_m;         // Result of shifter
591
`endif
592
`ifdef CFG_MC_BARREL_SHIFT_ENABLED
593
wire shift_left_d;                              // Indicates whether to perform a left shift or not
594
wire shift_left_q_d;
595
wire shift_right_d;                             // Indicates whether to perform a right shift or not
596
wire shift_right_q_d;
597
`endif
598
`ifdef LM32_NO_BARREL_SHIFT
599
wire [`LM32_WORD_RNG] shifter_result_x;         // Result of single-bit right shifter
600
`endif
601
 
602
// To/from multiplier
603
`ifdef LM32_MULTIPLY_ENABLED
604
wire [`LM32_WORD_RNG] multiplier_result_w;      // Result from multiplier
605
`endif
606
`ifdef CFG_MC_MULTIPLY_ENABLED
607
wire multiply_d;                                // Indicates whether to perform a multiply or not
608
wire multiply_q_d;
609
`endif
610
 
611
// To/from divider
612
`ifdef CFG_MC_DIVIDE_ENABLED
613
wire divide_d;                                  // Indicates whether to perform a divider or not
614
wire divide_q_d;
615
wire modulus_d;
616
wire modulus_q_d;
617
wire divide_by_zero_x;                          // Indicates an attempt was made to divide by zero
618
`endif
619
 
620
// To from multi-cycle arithmetic unit
621
`ifdef LM32_MC_ARITHMETIC_ENABLED
622
wire mc_stall_request_x;                        // Multi-cycle arithmetic unit stall request
623
wire [`LM32_WORD_RNG] mc_result_x;
624
`endif
625
 
626
// From CSRs
627
`ifdef CFG_INTERRUPTS_ENABLED
628
wire [`LM32_WORD_RNG] interrupt_csr_read_data_x;// Data read from interrupt CSRs
629
`endif
630
wire [`LM32_WORD_RNG] cfg;                      // Configuration CSR
631
wire [`LM32_WORD_RNG] cfg2;                     // Extended Configuration CSR
632
`ifdef CFG_MMU_ENABLED
633
wire [`LM32_WORD_RNG] psw;                      // Processor Status Word CSR
634
`endif
635
`ifdef CFG_CYCLE_COUNTER_ENABLED
636
reg [`LM32_WORD_RNG] cc;                        // Cycle counter CSR
637
`endif
638
reg [`LM32_WORD_RNG] csr_read_data_x;           // Data read from CSRs
639
 
640
// To/from instruction unit
641
wire [`LM32_PC_RNG] pc_f;                       // PC of instruction in F stage
642
wire [`LM32_PC_RNG] pc_d;                       // PC of instruction in D stage
643
wire [`LM32_PC_RNG] pc_x;                       // PC of instruction in X stage
644
wire [`LM32_PC_RNG] pc_m;                       // PC of instruction in M stage
645
wire [`LM32_PC_RNG] pc_w;                       // PC of instruction in W stage
646
`ifdef CFG_TRACE_ENABLED
647
reg [`LM32_PC_RNG] pc_c;                        // PC of last commited instruction
648
`endif
649
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
650
wire [`LM32_INSTRUCTION_RNG] instruction_f;     // Instruction in F stage
651
`endif
652
//pragma attribute instruction_d preserve_signal true
653
//pragma attribute instruction_d preserve_driver true
654
wire [`LM32_INSTRUCTION_RNG] instruction_d;     // Instruction in D stage
655
`ifdef CFG_ICACHE_ENABLED
656
wire iflush;                                    // Flush instruction cache
657
wire icache_stall_request;                      // Stall pipeline because instruction cache is busy
658
wire icache_restart_request;                    // Restart instruction that caused an instruction cache miss
659
wire icache_refill_request;                     // Request to refill instruction cache
660
wire icache_refilling;                          // Indicates the instruction cache is being refilled
661
`endif
662
`ifdef CFG_IROM_ENABLED
663
wire [`LM32_WORD_RNG] irom_store_data_m;        // Store data to instruction ROM
664
wire [`LM32_WORD_RNG] irom_address_xm;          // Address to instruction ROM from load-store unit
665
wire [`LM32_WORD_RNG] irom_data_m;              // Load data from instruction ROM
666
wire irom_we_xm;                                // Indicates data needs to be written to instruction ROM
667
wire irom_stall_request_x;                      // Indicates D stage needs to be stalled on a store to instruction ROM
668
`endif
669
 
670
// To/from load/store unit
671
`ifdef CFG_DCACHE_ENABLED
672
wire dflush_x;                                  // Flush data cache
673
reg dflush_m;
674
wire dcache_stall_request;                      // Stall pipeline because data cache is busy
675
wire dcache_restart_request;                    // Restart instruction that caused a data cache miss
676
wire dcache_refill_request;                     // Request to refill data cache
677
wire dcache_refilling;                          // Indicates the data cache is being refilled
678
`endif
679
wire [`LM32_WORD_RNG] load_data_w;              // Result of a load instruction
680
wire stall_wb_load;                             // Stall pipeline because of a load via the data Wishbone interface
681
 
682
// To/from JTAG interface
683
`ifdef CFG_JTAG_ENABLED
684
`ifdef CFG_JTAG_UART_ENABLED
685
wire [`LM32_WORD_RNG] jtx_csr_read_data;        // Read data for JTX CSR
686
wire [`LM32_WORD_RNG] jrx_csr_read_data;        // Read data for JRX CSR
687
`endif
688
`ifdef CFG_HW_DEBUG_ENABLED
689
wire jtag_csr_write_enable;                     // Debugger CSR write enable
690
wire [`LM32_WORD_RNG] jtag_csr_write_data;      // Data to write to specified CSR
691
wire [`LM32_CSR_RNG] jtag_csr;                  // Which CSR to write
692
wire jtag_read_enable;
693
wire [`LM32_BYTE_RNG] jtag_read_data;
694
wire jtag_write_enable;
695
wire [`LM32_BYTE_RNG] jtag_write_data;
696
wire [`LM32_WORD_RNG] jtag_address;
697
wire jtag_access_complete;
698
`endif
699
`ifdef CFG_DEBUG_ENABLED
700
wire jtag_break;                                // Request from debugger to raise a breakpoint
701
`endif
702
`endif
703
 
704
// Hazzard detection
705
wire raw_x_0;                                   // RAW hazzard between instruction in X stage and read port 0
706
wire raw_x_1;                                   // RAW hazzard between instruction in X stage and read port 1
707
wire raw_m_0;                                   // RAW hazzard between instruction in M stage and read port 0
708
wire raw_m_1;                                   // RAW hazzard between instruction in M stage and read port 1
709
wire raw_w_0;                                   // RAW hazzard between instruction in W stage and read port 0
710
wire raw_w_1;                                   // RAW hazzard between instruction in W stage and read port 1
711
 
712
// Control flow
713
wire cmp_zero;                                  // Result of comparison is zero
714
wire cmp_negative;                              // Result of comparison is negative
715
wire cmp_overflow;                              // Comparison produced an overflow
716
wire cmp_carry_n;                               // Comparison produced a carry, inverted
717
reg condition_met_x;                            // Condition of branch instruction is met
718
reg condition_met_m;
719
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
720
wire branch_taken_x;                            // Branch is taken in X stage
721
`endif
722
wire branch_taken_m;                            // Branch is taken in M stage
723
 
724
wire kill_f;                                    // Kill instruction in F stage
725
wire kill_d;                                    // Kill instruction in D stage
726
wire kill_x;                                    // Kill instruction in X stage
727
wire kill_m;                                    // Kill instruction in M stage
728
wire kill_w;                                    // Kill instruction in W stage
729
 
730
reg [`LM32_EBA_RNG] eba;                        // Exception Base Address (EBA) CSR
731
`ifdef CFG_DEBUG_ENABLED
732
reg [`LM32_EBA_RNG] deba;                       // Debug Exception Base Address (DEBA) CSR
733
`endif
734
reg [`LM32_EID_RNG] eid_x;                      // Exception ID in X stage
735
`ifdef CFG_TRACE_ENABLED
736
reg [`LM32_EID_RNG] eid_m;                      // Exception ID in M stage
737
reg [`LM32_EID_RNG] eid_w;                      // Exception ID in W stage
738
`endif
739
 
740
`ifdef CFG_DEBUG_ENABLED
741
`ifdef LM32_SINGLE_STEP_ENABLED
742
wire dc_ss;                                     // Is single-step enabled
743
`endif
744
wire dc_re;                                     // Remap all exceptions
745
wire exception_x;                               // An exception occured in the X stage
746
reg exception_m;                                // An instruction that caused an exception is in the M stage
747
wire debug_exception_x;                         // Indicates if a debug exception has occured
748
reg debug_exception_m;
749
reg debug_exception_w;
750
wire debug_exception_q_w;
751
wire non_debug_exception_x;                     // Indicates if a non debug exception has occured
752
reg non_debug_exception_m;
753
reg non_debug_exception_w;
754
wire non_debug_exception_q_w;
755
`else
756
wire exception_x;                               // Indicates if a debug exception has occured
757
reg exception_m;
758
reg exception_w;
759
wire exception_q_w;
760
`endif
761
 
762
`ifdef CFG_DEBUG_ENABLED
763
`ifdef CFG_JTAG_ENABLED
764
wire reset_exception;                           // Indicates if a reset exception has occured
765
`endif
766
`endif
767
`ifdef CFG_INTERRUPTS_ENABLED
768
wire interrupt_exception;                       // Indicates if an interrupt exception has occured
769
`endif
770
`ifdef CFG_DEBUG_ENABLED
771
wire breakpoint_exception;                      // Indicates if a breakpoint exception has occured
772
wire watchpoint_exception;                      // Indicates if a watchpoint exception has occured
773
`endif
774
`ifdef CFG_BUS_ERRORS_ENABLED
775
wire instruction_bus_error_exception;           // Indicates if an instruction bus error exception has occured
776
wire data_bus_error_exception;                  // Indicates if a data bus error exception has occured
777
`endif
778
`ifdef CFG_MC_DIVIDE_ENABLED
779
wire divide_by_zero_exception;                  // Indicates if a divide by zero exception has occured
780
`endif
781
wire system_call_exception;                     // Indicates if a system call exception has occured
782
 
783
`ifdef CFG_BUS_ERRORS_ENABLED
784
reg data_bus_error_seen;                        // Indicates if a data bus error was seen
785
`endif
786
 
787
`ifdef CFG_EXTERNAL_BREAK_ENABLED
788
reg ext_break_r;
789
`endif
790
 
791
`ifdef CFG_MMU_ENABLED
792
reg itlbe;                                      // Instruction TLB enable
793
reg dtlbe;                                      // Data TLB enable
794
reg usr;                                        // User mode
795
reg eitlbe;                                     // Exception instruction enable
796
reg edtlbe;                                     // Exception data TLB enable
797
reg eusr;                                       // Exception user mode
798
`ifdef CFG_DEBUG_ENABLED
799
reg bitlbe;                                     // Breakpoint instruction TLB enable
800
reg bdtlbe;                                     // Breakpoint data TLB enable
801
reg busr;                                       // Breakpoint user mode
802
`endif
803
 
804
reg itlb_invalidate;                            // Invalidate an ITLB entry
805
reg itlb_flush;                                 // Flush all ITLB entries
806
reg itlb_update;                                // Update an ITLB entry
807
reg dtlb_invalidate;                            // Invalidate a DTLB entry
808
reg dtlb_flush;                                 // Flush all DTLB entries
809
reg dtlb_update;                                // Update an DTLB entry
810
reg [`LM32_WORD_RNG] tlbpaddr;                  // TLBPADDR CSR
811
reg [`LM32_WORD_RNG] tlbvaddr;                  // TLBVADDR CSR
812
reg [`LM32_WORD_RNG] tlbbadvaddr;               // TLBBADVADDR CSR
813
wire [`LM32_WORD_RNG] dtlb_miss_vfn;            // VFN of the missed address
814
wire [`LM32_WORD_RNG] itlb_miss_vfn;            // VFN of the missed instruction
815
wire itlb_miss_x;                               // Indicates if an ITLB miss has occured in the X stage
816
wire itlb_stall_request;                        // Stall pipeline because instruction TLB is busy
817
wire dtlb_miss_x;                               // Indicates if an DTLB miss has occured in the X stage
818
wire dtlb_fault_x;                              // Indicates if an DTLB fault has occured in the X stage
819
wire dtlb_stall_request;                        // Stall pipeline because data TLB is busy
820
 
821
wire itlb_miss_exception;                       // Indicates if an ITLB miss exception has occured
822
wire itlb_exception;                            // Indicates if an ITLB exception has occured
823
wire dtlb_miss_exception;                       // Indicates if a DTLB miss exception has occured
824
wire dtlb_fault_exception;                      // Indicates if a DTLB fault exception has occured
825
wire dtlb_exception;                            // Indicates if a DTLB exception has occured
826
wire privilege_exception;                       // Indicates if a privilege exception has occured
827
`endif
828
 
829
/////////////////////////////////////////////////////
830
// Functions
831
/////////////////////////////////////////////////////
832
 
833
/////////////////////////////////////////////////////
834
// Instantiations
835
/////////////////////////////////////////////////////
836
 
837
// Instruction unit
838
lm32_instruction_unit #(
839
    .eba_reset              (eba_reset),
840
    .associativity          (icache_associativity),
841
    .sets                   (icache_sets),
842
    .bytes_per_line         (icache_bytes_per_line),
843
    .base_address           (icache_base_address),
844
    .limit                  (icache_limit)
845
  ) instruction_unit (
846
    // ----- Inputs -------
847
    .clk_i                  (clk_i),
848
    .rst_i                  (rst_i),
849
`ifdef CFG_DEBUG_ENABLED
850
`ifdef CFG_ALTERNATE_EBA
851
    .at_debug               (at_debug),
852
`endif
853
`endif
854
    // From pipeline
855
    .stall_a                (stall_a),
856
    .stall_f                (stall_f),
857
    .stall_d                (stall_d),
858
    .stall_x                (stall_x),
859
    .stall_m                (stall_m),
860
    .valid_f                (valid_f),
861
    .valid_d                (valid_d),
862
    .kill_f                 (kill_f),
863
    .branch_predict_taken_d (branch_predict_taken_d),
864
    .branch_predict_address_d (branch_predict_address_d),
865
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
866
    .branch_taken_x         (branch_taken_x),
867
    .branch_target_x        (branch_target_x),
868
`endif
869
    .exception_m            (exception_m),
870
    .branch_taken_m         (branch_taken_m),
871
    .branch_mispredict_taken_m (branch_mispredict_taken_m),
872
    .branch_target_m        (branch_target_m),
873
`ifdef CFG_ICACHE_ENABLED
874
    .iflush                 (iflush),
875
`endif
876
`ifdef CFG_IROM_ENABLED
877
    .irom_store_data_m      (irom_store_data_m),
878
    .irom_address_xm        (irom_address_xm),
879
    .irom_we_xm             (irom_we_xm),
880
`endif
881
`ifdef CFG_DCACHE_ENABLED
882
    .dcache_restart_request (dcache_restart_request),
883
    .dcache_refill_request  (dcache_refill_request),
884
    .dcache_refilling       (dcache_refilling),
885
`endif
886
`ifdef CFG_MMU_ENABLED
887
    .itlb_enable            (itlbe),
888
    .tlbpaddr               (tlbpaddr),
889
    .tlbvaddr               (tlbvaddr),
890
    .itlb_update            (itlb_update),
891
    .itlb_flush             (itlb_flush),
892
    .itlb_invalidate        (itlb_invalidate),
893
`endif
894
`ifdef CFG_IWB_ENABLED
895
    // From Wishbone
896
    .i_dat_i                (I_DAT_I),
897
    .i_ack_i                (I_ACK_I),
898
    .i_err_i                (I_ERR_I),
899
`endif
900
`ifdef CFG_HW_DEBUG_ENABLED
901
    .jtag_read_enable       (jtag_read_enable),
902
    .jtag_write_enable      (jtag_write_enable),
903
    .jtag_write_data        (jtag_write_data),
904
    .jtag_address           (jtag_address),
905
`endif
906
    // ----- Outputs -------
907
    // To pipeline
908
    .pc_f                   (pc_f),
909
    .pc_d                   (pc_d),
910
    .pc_x                   (pc_x),
911
    .pc_m                   (pc_m),
912
    .pc_w                   (pc_w),
913
`ifdef CFG_ICACHE_ENABLED
914
    .icache_stall_request   (icache_stall_request),
915
    .icache_restart_request (icache_restart_request),
916
    .icache_refill_request  (icache_refill_request),
917
    .icache_refilling       (icache_refilling),
918
`endif
919
`ifdef CFG_IROM_ENABLED
920
    .irom_data_m            (irom_data_m),
921
`endif
922
`ifdef CFG_MMU_ENABLED
923
    .itlb_stall_request     (itlb_stall_request),
924
    .itlb_miss_vfn          (itlb_miss_vfn),
925
    .itlb_miss_x            (itlb_miss_x),
926
`endif
927
`ifdef CFG_IWB_ENABLED
928
    // To Wishbone
929
    .i_dat_o                (I_DAT_O),
930
    .i_adr_o                (I_ADR_O),
931
    .i_cyc_o                (I_CYC_O),
932
    .i_sel_o                (I_SEL_O),
933
    .i_stb_o                (I_STB_O),
934
    .i_we_o                 (I_WE_O),
935
    .i_cti_o                (I_CTI_O),
936
    .i_lock_o               (I_LOCK_O),
937
    .i_bte_o                (I_BTE_O),
938
`endif
939
`ifdef CFG_HW_DEBUG_ENABLED
940
    .jtag_read_data         (jtag_read_data),
941
    .jtag_access_complete   (jtag_access_complete),
942
`endif
943
`ifdef CFG_BUS_ERRORS_ENABLED
944
    .bus_error_d            (bus_error_d),
945
`endif
946
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
947
    .instruction_f          (instruction_f),
948
`endif
949
    .instruction_d          (instruction_d)
950
    );
951
 
952
// Instruction decoder
953
lm32_decoder decoder (
954
    // ----- Inputs -------
955
    .instruction            (instruction_d),
956
    // ----- Outputs -------
957
    .d_result_sel_0         (d_result_sel_0_d),
958
    .d_result_sel_1         (d_result_sel_1_d),
959
    .x_result_sel_csr       (x_result_sel_csr_d),
960
`ifdef LM32_MC_ARITHMETIC_ENABLED
961
    .x_result_sel_mc_arith  (x_result_sel_mc_arith_d),
962
`endif
963
`ifdef LM32_NO_BARREL_SHIFT
964
    .x_result_sel_shift     (x_result_sel_shift_d),
965
`endif
966
`ifdef CFG_SIGN_EXTEND_ENABLED
967
    .x_result_sel_sext      (x_result_sel_sext_d),
968
`endif
969
    .x_result_sel_logic     (x_result_sel_logic_d),
970
`ifdef CFG_USER_ENABLED
971
    .x_result_sel_user      (x_result_sel_user_d),
972
`endif
973
    .x_result_sel_add       (x_result_sel_add_d),
974
    .m_result_sel_compare   (m_result_sel_compare_d),
975
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
976
    .m_result_sel_shift     (m_result_sel_shift_d),
977
`endif
978
    .w_result_sel_load      (w_result_sel_load_d),
979
`ifdef CFG_PL_MULTIPLY_ENABLED
980
    .w_result_sel_mul       (w_result_sel_mul_d),
981
`endif
982
    .x_bypass_enable        (x_bypass_enable_d),
983
    .m_bypass_enable        (m_bypass_enable_d),
984
    .read_enable_0          (read_enable_0_d),
985
    .read_idx_0             (read_idx_0_d),
986
    .read_enable_1          (read_enable_1_d),
987
    .read_idx_1             (read_idx_1_d),
988
    .write_enable           (write_enable_d),
989
    .write_idx              (write_idx_d),
990
    .immediate              (immediate_d),
991
    .branch_offset          (branch_offset_d),
992
    .load                   (load_d),
993
    .store                  (store_d),
994
    .size                   (size_d),
995
    .sign_extend            (sign_extend_d),
996
    .adder_op               (adder_op_d),
997
    .logic_op               (logic_op_d),
998
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
999
    .direction              (direction_d),
1000
`endif
1001
`ifdef CFG_MC_BARREL_SHIFT_ENABLED
1002
    .shift_left             (shift_left_d),
1003
    .shift_right            (shift_right_d),
1004
`endif
1005
`ifdef CFG_MC_MULTIPLY_ENABLED
1006
    .multiply               (multiply_d),
1007
`endif
1008
`ifdef CFG_MC_DIVIDE_ENABLED
1009
    .divide                 (divide_d),
1010
    .modulus                (modulus_d),
1011
`endif
1012
    .branch                 (branch_d),
1013
    .bi_unconditional       (bi_unconditional),
1014
    .bi_conditional         (bi_conditional),
1015
    .branch_reg             (branch_reg_d),
1016
    .condition              (condition_d),
1017
`ifdef CFG_DEBUG_ENABLED
1018
    .break_opcode           (break_d),
1019
`endif
1020
    .scall                  (scall_d),
1021
    .eret                   (eret_d),
1022
`ifdef CFG_DEBUG_ENABLED
1023
    .bret                   (bret_d),
1024
`endif
1025
`ifdef CFG_USER_ENABLED
1026
    .user_opcode            (user_opcode_d),
1027
`endif
1028
    .csr_write_enable       (csr_write_enable_d)
1029
    );
1030
 
1031
// Load/store unit
1032
lm32_load_store_unit #(
1033
    .associativity          (dcache_associativity),
1034
    .sets                   (dcache_sets),
1035
    .bytes_per_line         (dcache_bytes_per_line),
1036
    .base_address           (dcache_base_address),
1037
    .limit                  (dcache_limit)
1038
  ) load_store_unit (
1039
    // ----- Inputs -------
1040
    .clk_i                  (clk_i),
1041
    .rst_i                  (rst_i),
1042
    // From pipeline
1043
    .stall_a                (stall_a),
1044
    .stall_x                (stall_x),
1045
    .stall_m                (stall_m),
1046
    .kill_m                 (kill_m),
1047
    .exception_m            (exception_m),
1048
    .store_operand_x        (store_operand_x),
1049
    .load_store_address_x   (adder_result_x),
1050
    .load_store_address_m   (operand_m),
1051
    .load_store_address_w   (operand_w[1:0]),
1052
`ifdef CFG_MMU_ENABLED
1053
    .load_d                 (load_d),
1054
    .store_d                (store_d),
1055
`endif
1056
    .load_x                 (load_x),
1057
    .store_x                (store_x),
1058
    .load_q_x               (load_q_x),
1059
    .store_q_x              (store_q_x),
1060
    .load_q_m               (load_q_m),
1061
    .store_q_m              (store_q_m),
1062
    .sign_extend_x          (sign_extend_x),
1063
    .size_x                 (size_x),
1064
`ifdef CFG_DCACHE_ENABLED
1065
    .dflush                 (dflush_m),
1066
`endif
1067
`ifdef CFG_IROM_ENABLED
1068
    .irom_data_m            (irom_data_m),
1069
`endif
1070
`ifdef CFG_MMU_ENABLED
1071
    .dtlb_enable            (dtlbe),
1072
    .tlbpaddr               (tlbpaddr),
1073
    .tlbvaddr               (tlbvaddr),
1074
    .dtlb_update            (dtlb_update),
1075
    .dtlb_flush             (dtlb_flush),
1076
    .dtlb_invalidate        (dtlb_invalidate),
1077
`endif
1078
    // From Wishbone
1079
    .d_dat_i                (D_DAT_I),
1080
    .d_ack_i                (D_ACK_I),
1081
    .d_err_i                (D_ERR_I),
1082
    .d_rty_i                (D_RTY_I),
1083
    // ----- Outputs -------
1084
    // To pipeline
1085
`ifdef CFG_DCACHE_ENABLED
1086
    .dcache_refill_request  (dcache_refill_request),
1087
    .dcache_restart_request (dcache_restart_request),
1088
    .dcache_stall_request   (dcache_stall_request),
1089
    .dcache_refilling       (dcache_refilling),
1090
`endif
1091
`ifdef CFG_IROM_ENABLED
1092
    .irom_store_data_m      (irom_store_data_m),
1093
    .irom_address_xm        (irom_address_xm),
1094
    .irom_we_xm             (irom_we_xm),
1095
    .irom_stall_request_x   (irom_stall_request_x),
1096
`endif
1097
    .load_data_w            (load_data_w),
1098
    .stall_wb_load          (stall_wb_load),
1099
`ifdef CFG_MMU_ENABLED
1100
    .dtlb_stall_request     (dtlb_stall_request),
1101
    .dtlb_miss_vfn          (dtlb_miss_vfn),
1102
    .dtlb_miss_x            (dtlb_miss_x),
1103
    .dtlb_fault_x           (dtlb_fault_x),
1104
`endif
1105
    // To Wishbone
1106
    .d_dat_o                (D_DAT_O),
1107
    .d_adr_o                (D_ADR_O),
1108
    .d_cyc_o                (D_CYC_O),
1109
    .d_sel_o                (D_SEL_O),
1110
    .d_stb_o                (D_STB_O),
1111
    .d_we_o                 (D_WE_O),
1112
    .d_cti_o                (D_CTI_O),
1113
    .d_lock_o               (D_LOCK_O),
1114
    .d_bte_o                (D_BTE_O)
1115
    );
1116
 
1117
// Adder
1118
lm32_adder adder (
1119
    // ----- Inputs -------
1120
    .adder_op_x             (adder_op_x),
1121
    .adder_op_x_n           (adder_op_x_n),
1122
    .operand_0_x            (operand_0_x),
1123
    .operand_1_x            (operand_1_x),
1124
    // ----- Outputs -------
1125
    .adder_result_x         (adder_result_x),
1126
    .adder_carry_n_x        (adder_carry_n_x),
1127
    .adder_overflow_x       (adder_overflow_x)
1128
    );
1129
 
1130
// Logic operations
1131
lm32_logic_op logic_op (
1132
    // ----- Inputs -------
1133
    .logic_op_x             (logic_op_x),
1134
    .operand_0_x            (operand_0_x),
1135
 
1136
    .operand_1_x            (operand_1_x),
1137
    // ----- Outputs -------
1138
    .logic_result_x         (logic_result_x)
1139
    );
1140
 
1141
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
1142
// Pipelined barrel-shifter
1143
lm32_shifter shifter (
1144
    // ----- Inputs -------
1145
    .clk_i                  (clk_i),
1146
    .rst_i                  (rst_i),
1147
    .stall_x                (stall_x),
1148
    .direction_x            (direction_x),
1149
    .sign_extend_x          (sign_extend_x),
1150
    .operand_0_x            (operand_0_x),
1151
    .operand_1_x            (operand_1_x),
1152
    // ----- Outputs -------
1153
    .shifter_result_m       (shifter_result_m)
1154
    );
1155
`endif
1156
 
1157
`ifdef CFG_PL_MULTIPLY_ENABLED
1158
// Pipeline fixed-point multiplier
1159
lm32_multiplier multiplier (
1160
    // ----- Inputs -------
1161
    .clk_i                  (clk_i),
1162
    .rst_i                  (rst_i),
1163
    .stall_x                (stall_x),
1164
    .stall_m                (stall_m),
1165
    .operand_0              (d_result_0),
1166
    .operand_1              (d_result_1),
1167
    // ----- Outputs -------
1168
    .result                 (multiplier_result_w)
1169
    );
1170
`endif
1171
 
1172
`ifdef LM32_MC_ARITHMETIC_ENABLED
1173
// Multi-cycle arithmetic
1174
lm32_mc_arithmetic mc_arithmetic (
1175
    // ----- Inputs -------
1176
    .clk_i                  (clk_i),
1177
    .rst_i                  (rst_i),
1178
    .stall_d                (stall_d),
1179
    .kill_x                 (kill_x),
1180
`ifdef CFG_MC_DIVIDE_ENABLED
1181
    .divide_d               (divide_q_d),
1182
    .modulus_d              (modulus_q_d),
1183
`endif
1184
`ifdef CFG_MC_MULTIPLY_ENABLED
1185
    .multiply_d             (multiply_q_d),
1186
`endif
1187
`ifdef CFG_MC_BARREL_SHIFT_ENABLED
1188
    .shift_left_d           (shift_left_q_d),
1189
    .shift_right_d          (shift_right_q_d),
1190
    .sign_extend_d          (sign_extend_d),
1191
`endif
1192
    .operand_0_d            (d_result_0),
1193
    .operand_1_d            (d_result_1),
1194
    // ----- Outputs -------
1195
    .result_x               (mc_result_x),
1196
`ifdef CFG_MC_DIVIDE_ENABLED
1197
    .divide_by_zero_x       (divide_by_zero_x),
1198
`endif
1199
    .stall_request_x        (mc_stall_request_x)
1200
    );
1201
`endif
1202
 
1203
`ifdef CFG_INTERRUPTS_ENABLED
1204
// Interrupt unit
1205
lm32_interrupt interrupt_unit (
1206
    // ----- Inputs -------
1207
    .clk_i                  (clk_i),
1208
    .rst_i                  (rst_i),
1209
    // From external devices
1210
    .interrupt              (interrupt),
1211
    // From pipeline
1212
    .stall_x                (stall_x),
1213
`ifdef CFG_DEBUG_ENABLED
1214
    .non_debug_exception    (non_debug_exception_q_w),
1215
    .debug_exception        (debug_exception_q_w),
1216
`else
1217
    .exception              (exception_q_w),
1218
`endif
1219
    .eret_q_x               (eret_k_q_x),
1220
`ifdef CFG_DEBUG_ENABLED
1221
    .bret_q_x               (bret_k_q_x),
1222
`endif
1223
    .csr                    (csr_x),
1224
    .csr_write_data         (operand_1_x),
1225
    .csr_write_enable       (csr_write_enable_k_q_x),
1226
    // ----- Outputs -------
1227
    .interrupt_exception    (interrupt_exception),
1228
    // To pipeline
1229
    .csr_read_data          (interrupt_csr_read_data_x)
1230
    );
1231
`endif
1232
 
1233
`ifdef CFG_JTAG_ENABLED
1234
// JTAG interface
1235
lm32_jtag jtag (
1236
    // ----- Inputs -------
1237
    .clk_i                  (clk_i),
1238
    .rst_i                  (rst_i),
1239
    // From JTAG
1240
    .jtag_clk               (jtag_clk),
1241
    .jtag_update            (jtag_update),
1242
    .jtag_reg_q             (jtag_reg_q),
1243
    .jtag_reg_addr_q        (jtag_reg_addr_q),
1244
    // From pipeline
1245
`ifdef CFG_JTAG_UART_ENABLED
1246
    .csr                    (csr_x),
1247
    .csr_write_data         (operand_1_x),
1248
    .csr_write_enable       (csr_write_enable_k_q_x),
1249
    .stall_x                (stall_x),
1250
`endif
1251
`ifdef CFG_HW_DEBUG_ENABLED
1252
    .jtag_read_data         (jtag_read_data),
1253
    .jtag_access_complete   (jtag_access_complete),
1254
`endif
1255
`ifdef CFG_DEBUG_ENABLED
1256
    .exception_q_w          (debug_exception_q_w || non_debug_exception_q_w),
1257
`endif
1258
    // ----- Outputs -------
1259
    // To pipeline
1260
`ifdef CFG_JTAG_UART_ENABLED
1261
    .jtx_csr_read_data      (jtx_csr_read_data),
1262
    .jrx_csr_read_data      (jrx_csr_read_data),
1263
`endif
1264
`ifdef CFG_HW_DEBUG_ENABLED
1265
    .jtag_csr_write_enable  (jtag_csr_write_enable),
1266
    .jtag_csr_write_data    (jtag_csr_write_data),
1267
    .jtag_csr               (jtag_csr),
1268
    .jtag_read_enable       (jtag_read_enable),
1269
    .jtag_write_enable      (jtag_write_enable),
1270
    .jtag_write_data        (jtag_write_data),
1271
    .jtag_address           (jtag_address),
1272
`endif
1273
`ifdef CFG_DEBUG_ENABLED
1274
    .jtag_break             (jtag_break),
1275
    .jtag_reset             (reset_exception),
1276
`endif
1277
    // To JTAG
1278
    .jtag_reg_d             (jtag_reg_d),
1279
    .jtag_reg_addr_d        (jtag_reg_addr_d)
1280
    );
1281
`endif
1282
 
1283
`ifdef CFG_DEBUG_ENABLED
1284
// Debug unit
1285
lm32_debug #(
1286
    .breakpoints            (breakpoints),
1287
    .watchpoints            (watchpoints)
1288
  ) hw_debug (
1289
    // ----- Inputs -------
1290
    .clk_i                  (clk_i),
1291
    .rst_i                  (rst_i),
1292
    .pc_x                   (pc_x),
1293
    .load_x                 (load_x),
1294
    .store_x                (store_x),
1295
    .load_store_address_x   (adder_result_x),
1296
    .csr_write_enable_x     (csr_write_enable_k_q_x),
1297
    .csr_write_data         (operand_1_x),
1298
    .csr_x                  (csr_x),
1299
`ifdef CFG_HW_DEBUG_ENABLED
1300
    .jtag_csr_write_enable  (jtag_csr_write_enable),
1301
    .jtag_csr_write_data    (jtag_csr_write_data),
1302
    .jtag_csr               (jtag_csr),
1303
`endif
1304
`ifdef LM32_SINGLE_STEP_ENABLED
1305
    .eret_q_x               (eret_k_q_x),
1306
    .bret_q_x               (bret_k_q_x),
1307
    .stall_x                (stall_x),
1308
    .exception_x            (exception_x),
1309
    .q_x                    (q_x),
1310
`ifdef CFG_DCACHE_ENABLED
1311
    .dcache_refill_request  (dcache_refill_request),
1312
`endif
1313
`endif
1314
    // ----- Outputs -------
1315
`ifdef LM32_SINGLE_STEP_ENABLED
1316
    .dc_ss                  (dc_ss),
1317
`endif
1318
    .dc_re                  (dc_re),
1319
    .bp_match               (bp_match),
1320
    .wp_match               (wp_match)
1321
    );
1322
`endif
1323
 
1324
// Register file
1325
 
1326
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
1327
   /*----------------------------------------------------------------------
1328
    Register File is implemented using EBRs. There can be three accesses to
1329
    the register file in each cycle: two reads and one write. On-chip block
1330
    RAM has two read/write ports. To accomodate three accesses, two on-chip
1331
    block RAMs are used (each register file "write" is made to both block
1332
    RAMs).
1333
 
1334
    One limitation of the on-chip block RAMs is that one cannot perform a
1335
    read and write to same location in a cycle (if this is done, then the
1336
    data read out is indeterminate).
1337
    ----------------------------------------------------------------------*/
1338
   wire [31:0] regfile_data_0, regfile_data_1;
1339
   reg [31:0]  w_result_d;
1340
   reg         regfile_raw_0, regfile_raw_0_nxt;
1341
   reg         regfile_raw_1, regfile_raw_1_nxt;
1342
 
1343
   /*----------------------------------------------------------------------
1344
    Check if read and write is being performed to same register in current
1345
    cycle? This is done by comparing the read and write IDXs.
1346
    ----------------------------------------------------------------------*/
1347
   always @(reg_write_enable_q_w or write_idx_w or instruction_f)
1348
     begin
1349
        if (reg_write_enable_q_w
1350
            && (write_idx_w == instruction_f[25:21]))
1351
          regfile_raw_0_nxt = 1'b1;
1352
        else
1353
          regfile_raw_0_nxt = 1'b0;
1354
 
1355
        if (reg_write_enable_q_w
1356
            && (write_idx_w == instruction_f[20:16]))
1357
          regfile_raw_1_nxt = 1'b1;
1358
        else
1359
          regfile_raw_1_nxt = 1'b0;
1360
     end
1361
 
1362
   /*----------------------------------------------------------------------
1363
    Select latched (delayed) write value or data from register file. If
1364
    read in previous cycle was performed to register written to in same
1365
    cycle, then latched (delayed) write value is selected.
1366
    ----------------------------------------------------------------------*/
1367
   always @(regfile_raw_0 or w_result_d or regfile_data_0)
1368
     if (regfile_raw_0)
1369
       reg_data_live_0 = w_result_d;
1370
     else
1371
       reg_data_live_0 = regfile_data_0;
1372
 
1373
   /*----------------------------------------------------------------------
1374
    Select latched (delayed) write value or data from register file. If
1375
    read in previous cycle was performed to register written to in same
1376
    cycle, then latched (delayed) write value is selected.
1377
    ----------------------------------------------------------------------*/
1378
   always @(regfile_raw_1 or w_result_d or regfile_data_1)
1379
     if (regfile_raw_1)
1380
       reg_data_live_1 = w_result_d;
1381
     else
1382
       reg_data_live_1 = regfile_data_1;
1383
 
1384
   /*----------------------------------------------------------------------
1385
    Latch value written to register file
1386
    ----------------------------------------------------------------------*/
1387
   always @(posedge clk_i `CFG_RESET_SENSITIVITY)
1388
     if (rst_i == `TRUE)
1389
       begin
1390
          regfile_raw_0 <= 1'b0;
1391
          regfile_raw_1 <= 1'b0;
1392
          w_result_d <= 32'b0;
1393
       end
1394
     else
1395
       begin
1396
          regfile_raw_0 <= regfile_raw_0_nxt;
1397
          regfile_raw_1 <= regfile_raw_1_nxt;
1398
          w_result_d <= w_result;
1399
       end
1400
 
1401
   /*----------------------------------------------------------------------
1402
    Register file instantiation as Pseudo-Dual Port EBRs.
1403
    ----------------------------------------------------------------------*/
1404
   // Modified by GSI: removed non-portable RAM instantiation
1405
   lm32_ram
1406
     #(
1407
       // ----- Parameters -----
1408
       .data_width(32),
1409
       .address_width(5)
1410
       )
1411
   reg_0
1412
     (
1413
      // ----- Inputs -----
1414
      .read_clk      (clk_i),
1415
      .write_clk     (clk_i),
1416
      .reset         (rst_i),
1417
      .enable_read   (`TRUE),
1418
      .read_address  (instruction_f[25:21]),
1419
      .enable_write  (`TRUE),
1420
      .write_address (write_idx_w),
1421
      .write_data    (w_result),
1422
      .write_enable  (reg_write_enable_q_w),
1423
      // ----- Outputs -----
1424
      .read_data     (regfile_data_0)
1425
      );
1426
 
1427
   lm32_ram
1428
     #(
1429
       .data_width(32),
1430
       .address_width(5)
1431
       )
1432
   reg_1
1433
     (
1434
      // ----- Inputs -----
1435
      .read_clk      (clk_i),
1436
      .write_clk     (clk_i),
1437
      .reset         (rst_i),
1438
      .enable_read   (`TRUE),
1439
      .read_address  (instruction_f[20:16]),
1440
      .enable_write  (`TRUE),
1441
      .write_address (write_idx_w),
1442
      .write_data    (w_result),
1443
      .write_enable  (reg_write_enable_q_w),
1444
      // ----- Outputs -----
1445
      .read_data     (regfile_data_1)
1446
      );
1447
`endif
1448
 
1449
`ifdef CFG_EBR_NEGEDGE_REGISTER_FILE
1450
   lm32_ram
1451
     #(
1452
       .data_width(32),
1453
       .address_width(5)
1454
       )
1455
   reg_0
1456
     (
1457
      // ----- Inputs -----
1458
      .read_clk      (clk_i),
1459
      .write_clk     (clk_n_i),
1460
      .reset         (rst_i),
1461
      .enable_read   (stall_f == `FALSE),
1462
      .read_address  (read_idx_0_d),
1463
      .enable_write  (`TRUE),
1464
      .write_address (write_idx_w),
1465
      .write_data    (w_result),
1466
      .write_enable  (reg_write_enable_q_w),
1467
      // ----- Outputs -----
1468
      .read_data     (reg_data_0)
1469
      );
1470
 
1471
   lm32_ram
1472
     #(
1473
       .data_width(32),
1474
       .address_width(5)
1475
       )
1476
   reg_1
1477
     (
1478
      // ----- Inputs -----
1479
      .read_clk      (clk_i),
1480
      .write_clk     (clk_n_i),
1481
      .reset         (rst_i),
1482
      .enable_read   (stall_f == `FALSE),
1483
      .read_address  (read_idx_1_d),
1484
      .enable_write  (`TRUE),
1485
      .write_address (write_idx_w),
1486
      .write_data    (w_result),
1487
      .write_enable  (reg_write_enable_q_w),
1488
      // ----- Outputs -----
1489
      .read_data     (reg_data_1)
1490
      );
1491
`endif
1492
 
1493
 
1494
/////////////////////////////////////////////////////
1495
// Combinational Logic
1496
/////////////////////////////////////////////////////
1497
 
1498
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
1499
// Select between buffered and live data from register file
1500
assign reg_data_0 = use_buf ? reg_data_buf_0 : reg_data_live_0;
1501
assign reg_data_1 = use_buf ? reg_data_buf_1 : reg_data_live_1;
1502
`endif
1503
`ifdef LM32_EBR_REGISTER_FILE
1504
`else
1505
// Register file read ports
1506
assign reg_data_0 = registers[read_idx_0_d];
1507
assign reg_data_1 = registers[read_idx_1_d];
1508
`endif
1509
 
1510
// Detect read-after-write hazzards
1511
assign raw_x_0 = (write_idx_x == read_idx_0_d) && (write_enable_q_x == `TRUE);
1512
assign raw_m_0 = (write_idx_m == read_idx_0_d) && (write_enable_q_m == `TRUE);
1513
assign raw_w_0 = (write_idx_w == read_idx_0_d) && (write_enable_q_w == `TRUE);
1514
assign raw_x_1 = (write_idx_x == read_idx_1_d) && (write_enable_q_x == `TRUE);
1515
assign raw_m_1 = (write_idx_m == read_idx_1_d) && (write_enable_q_m == `TRUE);
1516
assign raw_w_1 = (write_idx_w == read_idx_1_d) && (write_enable_q_w == `TRUE);
1517
 
1518
// Interlock detection - Raise an interlock for RAW hazzards
1519
always @(*)
1520
begin
1521
    if (   (   (x_bypass_enable_x == `FALSE)
1522
            && (   ((read_enable_0_d == `TRUE) && (raw_x_0 == `TRUE))
1523
                || ((read_enable_1_d == `TRUE) && (raw_x_1 == `TRUE))
1524
               )
1525
           )
1526
        || (   (m_bypass_enable_m == `FALSE)
1527
            && (   ((read_enable_0_d == `TRUE) && (raw_m_0 == `TRUE))
1528
                || ((read_enable_1_d == `TRUE) && (raw_m_1 == `TRUE))
1529
               )
1530
           )
1531
       )
1532
        interlock = `TRUE;
1533
    else
1534
        interlock = `FALSE;
1535
end
1536
 
1537
// Bypass for reg port 0
1538
always @(*)
1539
begin
1540
    if (raw_x_0 == `TRUE)
1541
        bypass_data_0 = x_result;
1542
    else if (raw_m_0 == `TRUE)
1543
        bypass_data_0 = m_result;
1544
    else if (raw_w_0 == `TRUE)
1545
        bypass_data_0 = w_result;
1546
    else
1547
        bypass_data_0 = reg_data_0;
1548
end
1549
 
1550
// Bypass for reg port 1
1551
always @(*)
1552
begin
1553
    if (raw_x_1 == `TRUE)
1554
        bypass_data_1 = x_result;
1555
    else if (raw_m_1 == `TRUE)
1556
        bypass_data_1 = m_result;
1557
    else if (raw_w_1 == `TRUE)
1558
        bypass_data_1 = w_result;
1559
    else
1560
        bypass_data_1 = reg_data_1;
1561
end
1562
 
1563
   /*----------------------------------------------------------------------
1564
    Branch prediction is performed in D stage of pipeline. Only PC-relative
1565
    branches are predicted: forward-pointing conditional branches are not-
1566
    taken, while backward-pointing conditional branches are taken.
1567
    Unconditional branches are always predicted taken!
1568
    ----------------------------------------------------------------------*/
1569
   assign branch_predict_d = bi_unconditional | bi_conditional;
1570
   assign branch_predict_taken_d = bi_unconditional ? 1'b1 : (bi_conditional ? instruction_d[15] : 1'b0);
1571
 
1572
   // Compute branch target address: Branch PC PLUS Offset
1573
   assign branch_target_d = pc_d + branch_offset_d;
1574
 
1575
   // Compute fetch address. Address of instruction sequentially after the
1576
   // branch if branch is not taken. Target address of branch is branch is
1577
   // taken
1578
   assign branch_predict_address_d = branch_predict_taken_d ? branch_target_d : pc_f;
1579
 
1580
// D stage result selection
1581
always @(*)
1582
begin
1583
    d_result_0 = d_result_sel_0_d[0] ? {pc_f, 2'b00} : bypass_data_0;
1584
    case (d_result_sel_1_d)
1585
    `LM32_D_RESULT_SEL_1_ZERO:      d_result_1 = {`LM32_WORD_WIDTH{1'b0}};
1586
    `LM32_D_RESULT_SEL_1_REG_1:     d_result_1 = bypass_data_1;
1587
    `LM32_D_RESULT_SEL_1_IMMEDIATE: d_result_1 = immediate_d;
1588
    default:                        d_result_1 = {`LM32_WORD_WIDTH{1'bx}};
1589
    endcase
1590
end
1591
 
1592
`ifdef CFG_USER_ENABLED
1593
// Operands for user-defined instructions
1594
assign user_operand_0 = operand_0_x;
1595
assign user_operand_1 = operand_1_x;
1596
`endif
1597
 
1598
`ifdef CFG_SIGN_EXTEND_ENABLED
1599
// Sign-extension
1600
assign sextb_result_x = {{24{operand_0_x[7]}}, operand_0_x[7:0]};
1601
assign sexth_result_x = {{16{operand_0_x[15]}}, operand_0_x[15:0]};
1602
assign sext_result_x = size_x == `LM32_SIZE_BYTE ? sextb_result_x : sexth_result_x;
1603
`endif
1604
 
1605
`ifdef LM32_NO_BARREL_SHIFT
1606
// Only single bit shift operations are supported when barrel-shifter isn't implemented
1607
assign shifter_result_x = {operand_0_x[`LM32_WORD_WIDTH-1] & sign_extend_x, operand_0_x[`LM32_WORD_WIDTH-1:1]};
1608
`endif
1609
 
1610
// Condition evaluation
1611
assign cmp_zero = operand_0_x == operand_1_x;
1612
assign cmp_negative = adder_result_x[`LM32_WORD_WIDTH-1];
1613
assign cmp_overflow = adder_overflow_x;
1614
assign cmp_carry_n = adder_carry_n_x;
1615
always @(*)
1616
begin
1617
    case (condition_x)
1618
    `LM32_CONDITION_U1:   condition_met_x = `TRUE;
1619
    `LM32_CONDITION_U2:   condition_met_x = `TRUE;
1620
    `LM32_CONDITION_E:    condition_met_x = cmp_zero;
1621
    `LM32_CONDITION_NE:   condition_met_x = !cmp_zero;
1622
    `LM32_CONDITION_G:    condition_met_x = !cmp_zero && (cmp_negative == cmp_overflow);
1623
    `LM32_CONDITION_GU:   condition_met_x = cmp_carry_n && !cmp_zero;
1624
    `LM32_CONDITION_GE:   condition_met_x = cmp_negative == cmp_overflow;
1625
    `LM32_CONDITION_GEU:  condition_met_x = cmp_carry_n;
1626
    default:              condition_met_x = 1'bx;
1627
    endcase
1628
end
1629
 
1630
// X stage result selection
1631
always @(*)
1632
begin
1633
    x_result =   x_result_sel_add_x ? adder_result_x
1634
               : x_result_sel_csr_x ? csr_read_data_x
1635
`ifdef CFG_SIGN_EXTEND_ENABLED
1636
               : x_result_sel_sext_x ? sext_result_x
1637
`endif
1638
`ifdef CFG_USER_ENABLED
1639
               : x_result_sel_user_x ? user_result
1640
`endif
1641
`ifdef LM32_NO_BARREL_SHIFT
1642
               : x_result_sel_shift_x ? shifter_result_x
1643
`endif
1644
`ifdef LM32_MC_ARITHMETIC_ENABLED
1645
               : x_result_sel_mc_arith_x ? mc_result_x
1646
`endif
1647
               : logic_result_x;
1648
end
1649
 
1650
// M stage result selection
1651
always @(*)
1652
begin
1653
    m_result =   m_result_sel_compare_m ? {{`LM32_WORD_WIDTH-1{1'b0}}, condition_met_m}
1654
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
1655
               : m_result_sel_shift_m ? shifter_result_m
1656
`endif
1657
               : operand_m;
1658
end
1659
 
1660
// W stage result selection
1661
always @(*)
1662
begin
1663
    w_result =    w_result_sel_load_w ? load_data_w
1664
`ifdef CFG_PL_MULTIPLY_ENABLED
1665
                : w_result_sel_mul_w ? multiplier_result_w
1666
`endif
1667
                : operand_w;
1668
end
1669
 
1670
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
1671
// Indicate when a branch should be taken in X stage
1672
assign branch_taken_x =      (stall_x == `FALSE)
1673
                          && (   (branch_x == `TRUE)
1674
                              && ((condition_x == `LM32_CONDITION_U1) || (condition_x == `LM32_CONDITION_U2))
1675
                              && (valid_x == `TRUE)
1676
                              && (branch_predict_x == `FALSE)
1677
                             );
1678
`endif
1679
 
1680
// Indicate when a branch should be taken in M stage (exceptions are a type of branch)
1681
assign branch_taken_m =      (stall_m == `FALSE)
1682
                          && (   (   (branch_m == `TRUE)
1683
                                  && (valid_m == `TRUE)
1684
                                  && (   (   (condition_met_m == `TRUE)
1685
                                          && (branch_predict_taken_m == `FALSE)
1686
                                         )
1687
                                      || (   (condition_met_m == `FALSE)
1688
                                          && (branch_predict_m == `TRUE)
1689
                                          && (branch_predict_taken_m == `TRUE)
1690
                                         )
1691
                                     )
1692
                                 )
1693
                              || (exception_m == `TRUE)
1694
                             );
1695
 
1696
// Indicate when a branch in M stage is mispredicted as being taken
1697
assign branch_mispredict_taken_m =    (condition_met_m == `FALSE)
1698
                                   && (branch_predict_m == `TRUE)
1699
                                   && (branch_predict_taken_m == `TRUE);
1700
 
1701
// Indicate when a branch in M stage will cause flush in X stage
1702
assign branch_flushX_m =    (stall_m == `FALSE)
1703
                         && (   (   (branch_m == `TRUE)
1704
                                 && (valid_m == `TRUE)
1705
                                 && (   (condition_met_m == `TRUE)
1706
                                     || (   (condition_met_m == `FALSE)
1707
                                         && (branch_predict_m == `TRUE)
1708
                                         && (branch_predict_taken_m == `TRUE)
1709
                                        )
1710
                                    )
1711
                                )
1712
                             || (exception_m == `TRUE)
1713
                            );
1714
 
1715
// Generate signal that will kill instructions in each pipeline stage when necessary
1716
assign kill_f =    (   (valid_d == `TRUE)
1717
                    && (branch_predict_taken_d == `TRUE)
1718
                   )
1719
                || (branch_taken_m == `TRUE)
1720
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
1721
                || (branch_taken_x == `TRUE)
1722
`endif
1723
`ifdef CFG_ICACHE_ENABLED
1724
                || (icache_refill_request == `TRUE)
1725
`endif
1726
`ifdef CFG_DCACHE_ENABLED
1727
                || (dcache_refill_request == `TRUE)
1728
`endif
1729
`ifdef CFG_MMU_ENABLED
1730
                || (itlb_miss_exception == `TRUE)
1731
`endif
1732
                ;
1733
assign kill_d =    (branch_taken_m == `TRUE)
1734
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
1735
                || (branch_taken_x == `TRUE)
1736
`endif
1737
`ifdef CFG_ICACHE_ENABLED
1738
                || (icache_refill_request == `TRUE)
1739
`endif
1740
`ifdef CFG_DCACHE_ENABLED
1741
                || (dcache_refill_request == `TRUE)
1742
`endif
1743
`ifdef CFG_MMU_ENABLED
1744
                || (itlb_miss_exception == `TRUE)
1745
`endif
1746
                ;
1747
assign kill_x =    (branch_flushX_m == `TRUE)
1748
`ifdef CFG_DCACHE_ENABLED
1749
                || (dcache_refill_request == `TRUE)
1750
`endif
1751
                ;
1752
assign kill_m =    `FALSE
1753
`ifdef CFG_DCACHE_ENABLED
1754
                || (dcache_refill_request == `TRUE)
1755
`endif
1756
                ;
1757
assign kill_w =    `FALSE
1758
`ifdef CFG_DCACHE_ENABLED
1759
                || (dcache_refill_request == `TRUE)
1760
`endif
1761
                ;
1762
 
1763
// Exceptions
1764
 
1765
`ifdef CFG_DEBUG_ENABLED
1766
assign breakpoint_exception =    (   (   (break_x == `TRUE)
1767
                                      || (bp_match == `TRUE)
1768
                                     )
1769
                                  && (valid_x == `TRUE)
1770
                                 )
1771
`ifdef CFG_JTAG_ENABLED
1772
                              || (jtag_break == `TRUE)
1773
`endif
1774
`ifdef CFG_EXTERNAL_BREAK_ENABLED
1775
                              || (ext_break_r == `TRUE)
1776
`endif
1777
                              ;
1778
`endif
1779
 
1780
`ifdef CFG_DEBUG_ENABLED
1781
assign watchpoint_exception = wp_match == `TRUE;
1782
`endif
1783
 
1784
`ifdef CFG_BUS_ERRORS_ENABLED
1785
assign instruction_bus_error_exception = (   (bus_error_x == `TRUE)
1786
                                          && (valid_x == `TRUE)
1787
                                         );
1788
assign data_bus_error_exception = data_bus_error_seen == `TRUE;
1789
`endif
1790
 
1791
`ifdef CFG_MC_DIVIDE_ENABLED
1792
assign divide_by_zero_exception = divide_by_zero_x == `TRUE;
1793
`endif
1794
 
1795
assign system_call_exception = (   (scall_x == `TRUE)
1796
`ifdef CFG_BUS_ERRORS_ENABLED
1797
                                && (valid_x == `TRUE)
1798
`endif
1799
                               );
1800
 
1801
`ifdef CFG_MMU_ENABLED
1802
assign itlb_miss_exception = (   (itlb_miss_x == `TRUE)
1803
                              && (itlbe == `TRUE)
1804
                              && (valid_x == `TRUE)
1805
                            );
1806
assign itlb_exception = (itlb_miss_exception == `TRUE);
1807
 
1808
assign dtlb_miss_exception = (   (dtlb_miss_x == `TRUE)
1809
                              && (dtlbe == `TRUE)
1810
                              && (valid_x == `TRUE)
1811
                            );
1812
assign dtlb_fault_exception = (   (dtlb_fault_x == `TRUE)
1813
                               && (dtlbe == `TRUE)
1814
                               && (valid_x == `TRUE)
1815
                            );
1816
assign dtlb_exception = (dtlb_miss_exception == `TRUE) || (dtlb_fault_exception == `TRUE);
1817
 
1818
assign privilege_exception = (   (usr == `TRUE)
1819
                              && (   (csr_write_enable_q_x == `TRUE)
1820
                                  || (eret_q_x == `TRUE)
1821
`ifdef CFG_DEBUG_ENABLED
1822
                                  || (bret_q_x == `TRUE)
1823
`endif
1824
                                 )
1825
                             );
1826
`endif
1827
 
1828
`ifdef CFG_DEBUG_ENABLED
1829
assign debug_exception_x =  (breakpoint_exception == `TRUE)
1830
                         || (watchpoint_exception == `TRUE)
1831
                         ;
1832
 
1833
assign non_debug_exception_x = (system_call_exception == `TRUE)
1834
`ifdef CFG_JTAG_ENABLED
1835
                            || (reset_exception == `TRUE)
1836
`endif
1837
`ifdef CFG_BUS_ERRORS_ENABLED
1838
                            || (instruction_bus_error_exception == `TRUE)
1839
                            || (data_bus_error_exception == `TRUE)
1840
`endif
1841
`ifdef CFG_MC_DIVIDE_ENABLED
1842
                            || (divide_by_zero_exception == `TRUE)
1843
`endif
1844
`ifdef CFG_INTERRUPTS_ENABLED
1845
                            || (   (interrupt_exception == `TRUE)
1846
`ifdef LM32_SINGLE_STEP_ENABLED
1847
                                && (dc_ss == `FALSE)
1848
`endif
1849
`ifdef CFG_BUS_ERRORS_ENABLED
1850
                                && (store_q_m == `FALSE)
1851
                                && (D_CYC_O == `FALSE)
1852
`endif
1853
                               )
1854
`endif
1855
`ifdef CFG_MMU_ENABLED
1856
                            || (itlb_exception == `TRUE)
1857
                            || (dtlb_exception == `TRUE)
1858
                            || (privilege_exception == `TRUE)
1859
`endif
1860
                            ;
1861
 
1862
assign exception_x = (debug_exception_x == `TRUE) || (non_debug_exception_x == `TRUE);
1863
`else
1864
assign exception_x =           (system_call_exception == `TRUE)
1865
`ifdef CFG_BUS_ERRORS_ENABLED
1866
                            || (instruction_bus_error_exception == `TRUE)
1867
                            || (data_bus_error_exception == `TRUE)
1868
`endif
1869
`ifdef CFG_MC_DIVIDE_ENABLED
1870
                            || (divide_by_zero_exception == `TRUE)
1871
`endif
1872
`ifdef CFG_INTERRUPTS_ENABLED
1873
                            || (   (interrupt_exception == `TRUE)
1874
`ifdef LM32_SINGLE_STEP_ENABLED
1875
                                && (dc_ss == `FALSE)
1876
`endif
1877
`ifdef CFG_BUS_ERRORS_ENABLED
1878
                                && (store_q_m == `FALSE)
1879
                                && (D_CYC_O == `FALSE)
1880
`endif
1881
                               )
1882
`endif
1883
`ifdef CFG_MMU_ENABLED
1884
                            || (itlb_exception == `TRUE)
1885
                            || (dtlb_exception == `TRUE)
1886
                            || (privilege_exception == `TRUE)
1887
`endif
1888
                            ;
1889
`endif
1890
 
1891
// Exception ID
1892
always @(*)
1893
begin
1894
`ifdef CFG_DEBUG_ENABLED
1895
`ifdef CFG_JTAG_ENABLED
1896
    if (reset_exception == `TRUE)
1897
        eid_x = `LM32_EID_RESET;
1898
    else
1899
`endif
1900
`ifdef CFG_BUS_ERRORS_ENABLED
1901
         if (data_bus_error_exception == `TRUE)
1902
        eid_x = `LM32_EID_DATA_BUS_ERROR;
1903
    else
1904
`endif
1905
         if (breakpoint_exception == `TRUE)
1906
        eid_x = `LM32_EID_BREAKPOINT;
1907
    else
1908
`endif
1909
`ifdef CFG_BUS_ERRORS_ENABLED
1910
         if (data_bus_error_exception == `TRUE)
1911
        eid_x = `LM32_EID_DATA_BUS_ERROR;
1912
    else
1913
         if (instruction_bus_error_exception == `TRUE)
1914
        eid_x = `LM32_EID_INST_BUS_ERROR;
1915
    else
1916
`endif
1917
`ifdef CFG_DEBUG_ENABLED
1918
         if (watchpoint_exception == `TRUE)
1919
        eid_x = `LM32_EID_WATCHPOINT;
1920
    else
1921
`endif
1922
`ifdef CFG_MC_DIVIDE_ENABLED
1923
         if (divide_by_zero_exception == `TRUE)
1924
        eid_x = `LM32_EID_DIVIDE_BY_ZERO;
1925
    else
1926
`endif
1927
`ifdef CFG_INTERRUPTS_ENABLED
1928
         if (   (interrupt_exception == `TRUE)
1929
`ifdef LM32_SINGLE_STEP_ENABLED
1930
             && (dc_ss == `FALSE)
1931
`endif
1932
            )
1933
        eid_x = `LM32_EID_INTERRUPT;
1934
    else
1935
`endif
1936
`ifdef CFG_MMU_ENABLED
1937
         if (itlb_miss_exception == `TRUE)
1938
        eid_x = `LM32_EID_ITLB_MISS;
1939
    else
1940
         if (dtlb_miss_exception == `TRUE)
1941
        eid_x = `LM32_EID_DTLB_MISS;
1942
    else
1943
         if (dtlb_fault_exception == `TRUE)
1944
        eid_x = `LM32_EID_DTLB_FAULT;
1945
    else
1946
         if (privilege_exception == `TRUE)
1947
        eid_x = `LM32_EID_PRIVILEGE;
1948
    else
1949
`endif
1950
        eid_x = `LM32_EID_SCALL;
1951
end
1952
 
1953
// Stall generation
1954
 
1955
assign stall_a = (stall_f == `TRUE);
1956
 
1957
assign stall_f =   (stall_d == `TRUE)
1958
`ifdef CFG_MMU_ENABLED
1959
                // We need to stall for one cycle. Otherwise the icache
1960
                // starts one cycle earlier and the restart address will be
1961
                // wrong in case of a miss, that is one instruction is
1962
                // skipped.
1963
                || (   (itlbe == `TRUE)
1964
`ifdef CFG_DEBUG_ENABLED
1965
                    && (   (debug_exception_q_w == `TRUE)
1966
                        || (non_debug_exception_q_w == `TRUE)
1967
                       )
1968
`else
1969
                    && (exception_q_w == `TRUE)
1970
`endif
1971
                   )
1972
`endif
1973
                ;
1974
 
1975
assign stall_d =   (stall_x == `TRUE)
1976
                || (   (interlock == `TRUE)
1977
                    && (kill_d == `FALSE)
1978
                   )
1979
                || (   (   (eret_d == `TRUE)
1980
                        || (scall_d == `TRUE)
1981
`ifdef CFG_BUS_ERRORS_ENABLED
1982
                        || (bus_error_d == `TRUE)
1983
`endif
1984
                       )
1985
                    && (   (load_q_x == `TRUE)
1986
                        || (load_q_m == `TRUE)
1987
                        || (store_q_x == `TRUE)
1988
                        || (store_q_m == `TRUE)
1989
                        || (D_CYC_O == `TRUE)
1990
                       )
1991
                    && (kill_d == `FALSE)
1992
                   )
1993
`ifdef CFG_DEBUG_ENABLED
1994
                || (   (   (break_d == `TRUE)
1995
                        || (bret_d == `TRUE)
1996
                       )
1997
                    && (   (load_q_x == `TRUE)
1998
                        || (store_q_x == `TRUE)
1999
                        || (load_q_m == `TRUE)
2000
                        || (store_q_m == `TRUE)
2001
                        || (D_CYC_O == `TRUE)
2002
                       )
2003
                    && (kill_d == `FALSE)
2004
                   )
2005
`endif
2006
                || (   (csr_write_enable_d == `TRUE)
2007
                    && (load_q_x == `TRUE)
2008
                   )
2009
                ;
2010
 
2011
assign stall_x =    (stall_m == `TRUE)
2012
`ifdef LM32_MC_ARITHMETIC_ENABLED
2013
                 || (   (mc_stall_request_x == `TRUE)
2014
                     && (kill_x == `FALSE)
2015
                    )
2016
`endif
2017
`ifdef CFG_IROM_ENABLED
2018
                 // Stall load/store instruction in D stage if there is an ongoing store
2019
                 // operation to instruction ROM in M stage
2020
                 || (   (irom_stall_request_x == `TRUE)
2021
                     && (   (load_d == `TRUE)
2022
                         || (store_d == `TRUE)
2023
                        )
2024
                    )
2025
`endif
2026
                 ;
2027
 
2028
assign stall_m =    (stall_wb_load == `TRUE)
2029
`ifdef CFG_SIZE_OVER_SPEED
2030
                 || (D_CYC_O == `TRUE)
2031
`else
2032
                 || (   (D_CYC_O == `TRUE)
2033
                     && (   (store_m == `TRUE)
2034
                         /*
2035
                          Bug: Following loop does not allow interrupts to be services since
2036
                          either D_CYC_O or store_m is always high during entire duration of
2037
                          loop.
2038
                          L1:   addi    r1, r1, 1
2039
                                sw      (r2,0), r1
2040
                                bi      L1
2041
 
2042
                          Introduce a single-cycle stall when a wishbone cycle is in progress
2043
                          and a new store instruction is in Execute stage and a interrupt
2044
                          exception has occured. This stall will ensure that D_CYC_O and
2045
                          store_m will both be low for one cycle.
2046
                          */
2047
`ifdef CFG_INTERRUPTS_ENABLED
2048
                         || ((store_x == `TRUE) && (interrupt_exception == `TRUE))
2049
`endif
2050
                         || (load_m == `TRUE)
2051
                         || (load_x == `TRUE)
2052
                        )
2053
                    )
2054
`endif
2055
`ifdef CFG_DCACHE_ENABLED
2056
                 || (dcache_stall_request == `TRUE)     // Need to stall in case a taken branch is in M stage and data cache is only being flush, so wont be restarted
2057
`endif
2058
`ifdef CFG_ICACHE_ENABLED
2059
                 || (icache_stall_request == `TRUE)     // Pipeline needs to be stalled otherwise branches may be lost
2060
                 || ((I_CYC_O == `TRUE) && ((branch_m == `TRUE) || (exception_m == `TRUE)))
2061
`else
2062
`ifdef CFG_IWB_ENABLED
2063
                 || (I_CYC_O == `TRUE)
2064
`endif
2065
`endif
2066
`ifdef CFG_USER_ENABLED
2067
                 || (   (user_valid == `TRUE)           // Stall whole pipeline, rather than just X stage, where the instruction is, so we don't have to worry about exceptions (maybe)
2068
                     && (user_complete == `FALSE)
2069
                    )
2070
`endif
2071
`ifdef CFG_MMU_ENABLED
2072
                 || (itlb_stall_request == `TRUE)       // ITLB is busy
2073
                 || (dtlb_stall_request == `TRUE)       // DTLB is busy or a lookup is in progress
2074
`endif
2075
                 ;
2076
 
2077
// Qualify state changing control signals
2078
`ifdef LM32_MC_ARITHMETIC_ENABLED
2079
assign q_d = (valid_d == `TRUE) && (kill_d == `FALSE);
2080
`endif
2081
`ifdef CFG_MC_BARREL_SHIFT_ENABLED
2082
assign shift_left_q_d = (shift_left_d == `TRUE) && (q_d == `TRUE);
2083
assign shift_right_q_d = (shift_right_d == `TRUE) && (q_d == `TRUE);
2084
`endif
2085
`ifdef CFG_MC_MULTIPLY_ENABLED
2086
assign multiply_q_d = (multiply_d == `TRUE) && (q_d == `TRUE);
2087
`endif
2088
`ifdef CFG_MC_DIVIDE_ENABLED
2089
assign divide_q_d = (divide_d == `TRUE) && (q_d == `TRUE);
2090
assign modulus_q_d = (modulus_d == `TRUE) && (q_d == `TRUE);
2091
`endif
2092
assign q_x = (valid_x == `TRUE) && (kill_x == `FALSE);
2093
assign csr_write_enable_q_x = (csr_write_enable_x == `TRUE) && (q_x == `TRUE);
2094
assign csr_write_enable_k_q_x = (csr_write_enable_q_x == `TRUE)
2095
`ifdef CFG_MMU_ENABLED
2096
               && (usr == `FALSE)
2097
`endif
2098
                  ;
2099
assign eret_q_x = (eret_x == `TRUE) && (q_x == `TRUE);
2100
assign eret_k_q_x = (eret_q_x == `TRUE)
2101
`ifdef CFG_MMU_ENABLED
2102
               && (usr == `FALSE)
2103
`endif
2104
                  ;
2105
`ifdef CFG_DEBUG_ENABLED
2106
assign bret_q_x = (bret_x == `TRUE) && (q_x == `TRUE);
2107
assign bret_k_q_x = (bret_q_x == `TRUE)
2108
`ifdef CFG_MMU_ENABLED
2109
               && (usr == `FALSE)
2110
`endif
2111
                  ;
2112
`endif
2113
assign load_q_x = (load_x == `TRUE)
2114
               && (q_x == `TRUE)
2115
`ifdef CFG_DEBUG_ENABLED
2116
               && (bp_match == `FALSE)
2117
`endif
2118
                  ;
2119
assign store_q_x = (store_x == `TRUE)
2120
               && (q_x == `TRUE)
2121
`ifdef CFG_DEBUG_ENABLED
2122
               && (bp_match == `FALSE)
2123
`endif
2124
                  ;
2125
`ifdef CFG_USER_ENABLED
2126
assign user_valid = (x_result_sel_user_x == `TRUE) && (q_x == `TRUE);
2127
`endif
2128
assign q_m = (valid_m == `TRUE) && (kill_m == `FALSE) && (exception_m == `FALSE);
2129
assign load_q_m = (load_m == `TRUE) && (q_m == `TRUE);
2130
assign store_q_m = (store_m == `TRUE) && (q_m == `TRUE);
2131
`ifdef CFG_DEBUG_ENABLED
2132
assign debug_exception_q_w = ((debug_exception_w == `TRUE) && (valid_w == `TRUE));
2133
assign non_debug_exception_q_w = ((non_debug_exception_w == `TRUE) && (valid_w == `TRUE));
2134
`else
2135
assign exception_q_w = ((exception_w == `TRUE) && (valid_w == `TRUE));
2136
`endif
2137
// Don't qualify register write enables with kill, as the signal is needed early, and it doesn't matter if the instruction is killed (except for the actual write - but that is handled separately)
2138
assign write_enable_q_x = (write_enable_x == `TRUE) && (valid_x == `TRUE) && (branch_flushX_m == `FALSE);
2139
assign write_enable_q_m = (write_enable_m == `TRUE) && (valid_m == `TRUE);
2140
assign write_enable_q_w = (write_enable_w == `TRUE) && (valid_w == `TRUE);
2141
// The enable that actually does write the registers needs to be qualified with kill
2142
assign reg_write_enable_q_w = (write_enable_w == `TRUE) && (kill_w == `FALSE) && (valid_w == `TRUE);
2143
 
2144
// Configuration (CFG) CSR
2145
assign cfg = {
2146
              `LM32_REVISION,
2147
              watchpoints[3:0],
2148
              breakpoints[3:0],
2149
              interrupts[5:0],
2150
`ifdef CFG_JTAG_UART_ENABLED
2151
              `TRUE,
2152
`else
2153
              `FALSE,
2154
`endif
2155
`ifdef CFG_ROM_DEBUG_ENABLED
2156
              `TRUE,
2157
`else
2158
              `FALSE,
2159
`endif
2160
`ifdef CFG_HW_DEBUG_ENABLED
2161
              `TRUE,
2162
`else
2163
              `FALSE,
2164
`endif
2165
`ifdef CFG_DEBUG_ENABLED
2166
              `TRUE,
2167
`else
2168
              `FALSE,
2169
`endif
2170
`ifdef CFG_ICACHE_ENABLED
2171
              `TRUE,
2172
`else
2173
              `FALSE,
2174
`endif
2175
`ifdef CFG_DCACHE_ENABLED
2176
              `TRUE,
2177
`else
2178
              `FALSE,
2179
`endif
2180
`ifdef CFG_CYCLE_COUNTER_ENABLED
2181
              `TRUE,
2182
`else
2183
              `FALSE,
2184
`endif
2185
`ifdef CFG_USER_ENABLED
2186
              `TRUE,
2187
`else
2188
              `FALSE,
2189
`endif
2190
`ifdef CFG_SIGN_EXTEND_ENABLED
2191
              `TRUE,
2192
`else
2193
              `FALSE,
2194
`endif
2195
`ifdef LM32_BARREL_SHIFT_ENABLED
2196
              `TRUE,
2197
`else
2198
              `FALSE,
2199
`endif
2200
`ifdef CFG_MC_DIVIDE_ENABLED
2201
              `TRUE,
2202
`else
2203
              `FALSE,
2204
`endif
2205
`ifdef LM32_MULTIPLY_ENABLED
2206
              `TRUE
2207
`else
2208
              `FALSE
2209
`endif
2210
              };
2211
 
2212
assign cfg2 = {
2213
`ifdef CFG_MMU_ENABLED
2214
               `TRUE,
2215
`else
2216
               `FALSE,
2217
`endif
2218
               29'b0,
2219
`ifdef CFG_IROM_ENABLED
2220
               `TRUE,
2221
`else
2222
               `FALSE,
2223
`endif
2224
`ifdef CFG_DRAM_ENABLED
2225
               `TRUE
2226
`else
2227
               `FALSE
2228
`endif
2229
               };
2230
 
2231
`ifdef CFG_MMU_ENABLED
2232
assign psw = {
2233
              {`LM32_WORD_WIDTH-12{1'b0}},
2234
`ifdef CFG_DEBUG_ENABLED
2235
              busr,
2236
`else
2237
              `FALSE,
2238
`endif
2239
              eusr,
2240
              usr,
2241
`ifdef CFG_DEBUG_ENABLED
2242
              bdtlbe,
2243
`else
2244
              `FALSE,
2245
`endif
2246
              edtlbe,
2247
              dtlbe,
2248
`ifdef CFG_DEBUG_ENABLED
2249
              bitlbe,
2250
`else
2251
              `FALSE,
2252
`endif
2253
              eitlbe,
2254
              itlbe,
2255
              interrupt_csr_read_data_x[2:0]
2256
              };
2257
`endif
2258
 
2259
// Cache flush
2260
`ifdef CFG_ICACHE_ENABLED
2261
assign iflush = (   (csr_write_enable_d == `TRUE)
2262
                 && (csr_d == `LM32_CSR_ICC)
2263
                 && (stall_d == `FALSE)
2264
                 && (kill_d == `FALSE)
2265
                 && (valid_d == `TRUE))
2266
// Added by GSI: needed to flush cache after loading firmware per JTAG
2267
`ifdef CFG_HW_DEBUG_ENABLED
2268
             ||
2269
                (   (jtag_csr_write_enable == `TRUE)
2270
                 && (jtag_csr == `LM32_CSR_ICC))
2271
`endif
2272
                 ;
2273
`endif
2274
`ifdef CFG_DCACHE_ENABLED
2275
assign dflush_x = (   (csr_write_enable_q_x == `TRUE)
2276
                   && (csr_x == `LM32_CSR_DCC))
2277
// Added by GSI: needed to flush cache after loading firmware per JTAG
2278
`ifdef CFG_HW_DEBUG_ENABLED
2279
               ||
2280
                  (   (jtag_csr_write_enable == `TRUE)
2281
                   && (jtag_csr == `LM32_CSR_DCC))
2282
`endif
2283
                   ;
2284
`endif
2285
 
2286
// Extract CSR index
2287
assign csr_d = read_idx_0_d[`LM32_CSR_RNG];
2288
 
2289
// CSR reads
2290
always @(*)
2291
begin
2292
    case (csr_x)
2293
`ifdef CFG_INTERRUPTS_ENABLED
2294
    `LM32_CSR_IE,
2295
    `LM32_CSR_IM,
2296
    `LM32_CSR_IP:   csr_read_data_x = interrupt_csr_read_data_x;
2297
`endif
2298
`ifdef CFG_CYCLE_COUNTER_ENABLED
2299
    `LM32_CSR_CC:   csr_read_data_x = cc;
2300
`endif
2301
    `LM32_CSR_CFG:  csr_read_data_x = cfg;
2302
    `LM32_CSR_EBA:  csr_read_data_x = {eba, {(`LM32_WORD_WIDTH-`LM32_EBA_WIDTH){1'b0}}};
2303
`ifdef CFG_DEBUG_ENABLED
2304
    `LM32_CSR_DEBA: csr_read_data_x = {deba, {(`LM32_WORD_WIDTH-`LM32_EBA_WIDTH){1'b0}}};
2305
`endif
2306
`ifdef CFG_JTAG_UART_ENABLED
2307
    `LM32_CSR_JTX:  csr_read_data_x = jtx_csr_read_data;
2308
    `LM32_CSR_JRX:  csr_read_data_x = jrx_csr_read_data;
2309
`endif
2310
    `LM32_CSR_CFG2: csr_read_data_x = cfg2;
2311
`ifdef CFG_MMU_ENABLED
2312
    `LM32_CSR_PSW:  csr_read_data_x = psw;
2313
    `LM32_CSR_TLBVADDR: csr_read_data_x = tlbvaddr;
2314
    `LM32_CSR_TLBBADVADDR: csr_read_data_x = tlbbadvaddr;
2315
`endif
2316
 
2317
    default:        csr_read_data_x = {`LM32_WORD_WIDTH{1'bx}};
2318
    endcase
2319
end
2320
 
2321
/////////////////////////////////////////////////////
2322
// Sequential Logic
2323
/////////////////////////////////////////////////////
2324
 
2325
`ifdef CFG_MMU_ENABLED
2326
// Processor status word (PSW) handling
2327
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2328
begin
2329
    if (rst_i)
2330
    begin
2331
        itlbe <= `FALSE;
2332
        eitlbe <= `FALSE;
2333
        dtlbe <= `FALSE;
2334
        edtlbe <= `FALSE;
2335
        usr <= `FALSE;
2336
        eusr <= `FALSE;
2337
`ifdef CFG_DEBUG_ENABLED
2338
        bitlbe <= `FALSE;
2339
        bdtlbe <= `FALSE;
2340
        busr <= `FALSE;
2341
`endif
2342
    end
2343
    else
2344
    begin
2345
`ifdef CFG_DEBUG_ENABLED
2346
        if (non_debug_exception_q_w == `TRUE)
2347
        begin
2348
            // Save and then clear ITLB and DTLB enable
2349
            eitlbe <= itlbe;
2350
            itlbe <= `FALSE;
2351
            edtlbe <= dtlbe;
2352
            dtlbe <= `FALSE;
2353
            eusr <= usr;
2354
            usr <= `FALSE;
2355
        end
2356
        else if (debug_exception_q_w == `TRUE)
2357
        begin
2358
            // Save and then clear TLB enable
2359
            bitlbe <= itlbe;
2360
            itlbe <= `FALSE;
2361
            bdtlbe <= dtlbe;
2362
            dtlbe <= `FALSE;
2363
            busr <= usr;
2364
            usr <= `FALSE;
2365
        end
2366
`else
2367
        if (exception_q_w == `TRUE)
2368
        begin
2369
            // Save and then clear ITLB and DTLB enable
2370
            eitlbe <= itlbe;
2371
            itlbe <= `FALSE;
2372
            edtlbe <= dtlbe;
2373
            dtlbe <= `FALSE;
2374
            eusr <= usr;
2375
            usr <= `FALSE;
2376
        end
2377
`endif
2378
 
2379
        else if (stall_x == `FALSE)
2380
        begin
2381
            if (eret_k_q_x == `TRUE)
2382
            begin
2383
                // Restore ITLB and DTLB enable
2384
                itlbe <= eitlbe;
2385
                dtlbe <= edtlbe;
2386
                usr <= eusr;
2387
            end
2388
`ifdef CFG_DEBUG_ENABLED
2389
            else if (bret_k_q_x == `TRUE)
2390
            begin
2391
                // Restore ITLB and DTLB enable
2392
                itlbe <= bitlbe;
2393
                dtlbe <= bdtlbe;
2394
                usr <= busr;
2395
            end
2396
`endif
2397
            else if (csr_write_enable_k_q_x == `TRUE)
2398
            begin
2399
                // Handle wcsr write
2400
                if (csr_x == `LM32_CSR_PSW)
2401
                begin
2402
                    // ie, eie and bie are shadowed from IE register
2403
                    itlbe  <= operand_1_x[3];
2404
                    eitlbe <= operand_1_x[4];
2405
                    dtlbe  <= operand_1_x[6];
2406
                    edtlbe <= operand_1_x[7];
2407
                    usr    <= operand_1_x[9];
2408
                    eusr   <= operand_1_x[10];
2409
`ifdef CFG_DEBUG_ENABLED
2410
                    bitlbe <= operand_1_x[5];
2411
                    bdtlbe <= operand_1_x[8];
2412
                    busr   <= operand_1_x[11];
2413
`endif
2414
                end
2415
            end
2416
        end
2417
    end
2418
end
2419
 
2420
// TLBVADDR CSR
2421
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2422
begin
2423
    if (rst_i == `TRUE)
2424
    begin
2425
        itlb_flush <= `FALSE;
2426
        itlb_invalidate <= `FALSE;
2427
        dtlb_flush <= `FALSE;
2428
        dtlb_invalidate <= `FALSE;
2429
        tlbvaddr <= {`LM32_WORD_WIDTH{1'b0}};
2430
    end
2431
    else
2432
    begin
2433
        itlb_flush <= `FALSE;
2434
        itlb_invalidate <= `FALSE;
2435
        dtlb_flush <= `FALSE;
2436
        dtlb_invalidate <= `FALSE;
2437
        if (stall_x == `FALSE)
2438
        begin
2439
            if (dtlb_exception == `TRUE)
2440
                tlbvaddr <= {dtlb_miss_vfn[`LM32_WORD_WIDTH-1:1], 1'b1};
2441
            else if (itlb_exception == `TRUE)
2442
                tlbvaddr <= {itlb_miss_vfn[`LM32_WORD_WIDTH-1:1], 1'b0};
2443
            else if ((csr_write_enable_k_q_x == `TRUE) && (csr_x == `LM32_CSR_TLBVADDR))
2444
            begin
2445
                tlbvaddr <= operand_1_x;
2446
                if (operand_1_x[0] == 1'b0)
2447
                begin
2448
                    case (operand_1_x[`LM32_TLB_OP_RNG])
2449
                    `LM32_TLB_OP_FLUSH:      itlb_flush <= `TRUE;
2450
                    `LM32_TLB_OP_INVALIDATE: itlb_invalidate <= `TRUE;
2451
                    endcase
2452
                end
2453
                if (operand_1_x[0] == 1'b1)
2454
                begin
2455
                    case (operand_1_x[`LM32_TLB_OP_RNG])
2456
                    `LM32_TLB_OP_FLUSH:      dtlb_flush <= `TRUE;
2457
                    `LM32_TLB_OP_INVALIDATE: dtlb_invalidate <= `TRUE;
2458
                    endcase
2459
                end
2460
            end
2461
        end
2462
    end
2463
end
2464
 
2465
// TLBPADDR CSR
2466
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2467
begin
2468
    if (rst_i == `TRUE)
2469
    begin
2470
        itlb_update <= `FALSE;
2471
        dtlb_update <= `FALSE;
2472
        tlbpaddr <= {`LM32_WORD_WIDTH{1'b0}};
2473
    end
2474
    else
2475
    begin
2476
        itlb_update <= `FALSE;
2477
        dtlb_update <= `FALSE;
2478
        if ((csr_write_enable_k_q_x == `TRUE) && (csr_x == `LM32_CSR_TLBPADDR) && (stall_x == `FALSE))
2479
        begin
2480
            /* updates take change in the M stage */
2481
            tlbpaddr <= operand_1_x;
2482
            if (operand_1_x[0] == 1'b0)
2483
                itlb_update <= `TRUE;
2484
            if (operand_1_x[0] == 1'b1)
2485
                dtlb_update <= `TRUE;
2486
        end
2487
    end
2488
end
2489
 
2490
// TLBBADVADDR CSR
2491
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2492
begin
2493
    if (rst_i == `TRUE)
2494
        tlbbadvaddr <= {`LM32_WORD_WIDTH{1'b0}};
2495
    else
2496
    begin
2497
        if (stall_x == `FALSE)
2498
        begin
2499
            if (dtlb_exception == `TRUE)
2500
                tlbbadvaddr <= adder_result_x;
2501
            else if (itlb_exception == `TRUE)
2502
                tlbbadvaddr <= {pc_x, {`LM32_WORD_WIDTH-`LM32_PC_WIDTH{1'b0}}};
2503
        end
2504
    end
2505
end
2506
`endif
2507
 
2508
// Exception Base Address (EBA) CSR
2509
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2510
begin
2511
    if (rst_i == `TRUE)
2512
        eba <= eba_reset[`LM32_EBA_RNG];
2513
    else
2514
    begin
2515
        if ((csr_write_enable_k_q_x == `TRUE) && (csr_x == `LM32_CSR_EBA) && (stall_x == `FALSE))
2516
            eba <= operand_1_x[`LM32_EBA_RNG];
2517
`ifdef CFG_HW_DEBUG_ENABLED
2518
        if ((jtag_csr_write_enable == `TRUE) && (jtag_csr == `LM32_CSR_EBA))
2519
            eba <= jtag_csr_write_data[`LM32_EBA_RNG];
2520
`endif
2521
    end
2522
end
2523
 
2524
`ifdef CFG_DEBUG_ENABLED
2525
// Debug Exception Base Address (DEBA) CSR
2526
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2527
begin
2528
    if (rst_i == `TRUE)
2529
        deba <= deba_reset[`LM32_EBA_RNG];
2530
    else
2531
    begin
2532
        if ((csr_write_enable_k_q_x == `TRUE) && (csr_x == `LM32_CSR_DEBA) && (stall_x == `FALSE))
2533
            deba <= operand_1_x[`LM32_EBA_RNG];
2534
`ifdef CFG_HW_DEBUG_ENABLED
2535
        if ((jtag_csr_write_enable == `TRUE) && (jtag_csr == `LM32_CSR_DEBA))
2536
            deba <= jtag_csr_write_data[`LM32_EBA_RNG];
2537
`endif
2538
    end
2539
end
2540
`endif
2541
 
2542
// Cycle Counter (CC) CSR
2543
`ifdef CFG_CYCLE_COUNTER_ENABLED
2544
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2545
begin
2546
    if (rst_i == `TRUE)
2547
        cc <= {`LM32_WORD_WIDTH{1'b0}};
2548
    else
2549
        cc <= cc + 1'b1;
2550
end
2551
`endif
2552
 
2553
`ifdef CFG_BUS_ERRORS_ENABLED
2554
// Watch for data bus errors
2555
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2556
begin
2557
    if (rst_i == `TRUE)
2558
        data_bus_error_seen <= `FALSE;
2559
    else
2560
    begin
2561
        // Set flag when bus error is detected
2562
        if ((D_ERR_I == `TRUE) && (D_CYC_O == `TRUE))
2563
            data_bus_error_seen <= `TRUE;
2564
        // Clear flag when exception is taken
2565
        if ((exception_m == `TRUE) && (kill_m == `FALSE))
2566
            data_bus_error_seen <= `FALSE;
2567
    end
2568
end
2569
`endif
2570
 
2571
`ifdef CFG_EXTERNAL_BREAK_ENABLED
2572
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2573
begin
2574
    if (rst_i == `TRUE)
2575
        ext_break_r <= `FALSE;
2576
    else
2577
    begin
2578
        if (ext_break == `TRUE)
2579
            ext_break_r <= `TRUE;
2580
        if (debug_exception_q_w == `TRUE)
2581
            ext_break_r <= `FALSE;
2582
    end
2583
end
2584
`endif
2585
 
2586
// Valid bits to indicate whether an instruction in a partcular pipeline stage is valid or not
2587
 
2588
`ifdef CFG_ICACHE_ENABLED
2589
`ifdef CFG_DCACHE_ENABLED
2590
always @(*)
2591
begin
2592
    if (   (icache_refill_request == `TRUE)
2593
        || (dcache_refill_request == `TRUE)
2594
       )
2595
        valid_a = `FALSE;
2596
    else if (   (icache_restart_request == `TRUE)
2597
             || (dcache_restart_request == `TRUE)
2598
            )
2599
        valid_a = `TRUE;
2600
    else
2601
        valid_a = !icache_refilling && !dcache_refilling;
2602
end
2603
`else
2604
always @(*)
2605
begin
2606
    if (icache_refill_request == `TRUE)
2607
        valid_a = `FALSE;
2608
    else if (icache_restart_request == `TRUE)
2609
        valid_a = `TRUE;
2610
    else
2611
        valid_a = !icache_refilling;
2612
end
2613
`endif
2614
`else
2615
`ifdef CFG_DCACHE_ENABLED
2616
always @(*)
2617
begin
2618
    if (dcache_refill_request == `TRUE)
2619
        valid_a = `FALSE;
2620
    else if (dcache_restart_request == `TRUE)
2621
        valid_a = `TRUE;
2622
    else
2623
        valid_a = !dcache_refilling;
2624
end
2625
`endif
2626
`endif
2627
 
2628
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2629
begin
2630
    if (rst_i == `TRUE)
2631
    begin
2632
        valid_f <= `FALSE;
2633
        valid_d <= `FALSE;
2634
        valid_x <= `FALSE;
2635
        valid_m <= `FALSE;
2636
        valid_w <= `FALSE;
2637
    end
2638
    else
2639
    begin
2640
        if ((kill_f == `TRUE) || (stall_a == `FALSE))
2641
`ifdef LM32_CACHE_ENABLED
2642
            valid_f <= valid_a;
2643
`else
2644
            valid_f <= `TRUE;
2645
`endif
2646
        else if (stall_f == `FALSE)
2647
            valid_f <= `FALSE;
2648
 
2649
        if (kill_d == `TRUE)
2650
            valid_d <= `FALSE;
2651
        else if (stall_f == `FALSE)
2652
            valid_d <= valid_f & !kill_f;
2653
        else if (stall_d == `FALSE)
2654
            valid_d <= `FALSE;
2655
 
2656
        if (stall_d == `FALSE)
2657
            valid_x <= valid_d & !kill_d;
2658
        else if (kill_x == `TRUE)
2659
            valid_x <= `FALSE;
2660
        else if (stall_x == `FALSE)
2661
            valid_x <= `FALSE;
2662
 
2663
        if (kill_m == `TRUE)
2664
            valid_m <= `FALSE;
2665
        else if (stall_x == `FALSE)
2666
            valid_m <= valid_x & !kill_x;
2667
        else if (stall_m == `FALSE)
2668
            valid_m <= `FALSE;
2669
 
2670
        if (stall_m == `FALSE)
2671
            valid_w <= valid_m & !kill_m;
2672
        else
2673
            valid_w <= `FALSE;
2674
    end
2675
end
2676
 
2677
// Microcode pipeline registers
2678
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
2679
begin
2680
    if (rst_i == `TRUE)
2681
    begin
2682
`ifdef CFG_USER_ENABLED
2683
        user_opcode <= {`LM32_USER_OPCODE_WIDTH{1'b0}};
2684
`endif
2685
        operand_0_x <= {`LM32_WORD_WIDTH{1'b0}};
2686
        operand_1_x <= {`LM32_WORD_WIDTH{1'b0}};
2687
        store_operand_x <= {`LM32_WORD_WIDTH{1'b0}};
2688
        branch_target_x <= {`LM32_PC_WIDTH{1'b0}};
2689
        x_result_sel_csr_x <= `FALSE;
2690
`ifdef LM32_MC_ARITHMETIC_ENABLED
2691
        x_result_sel_mc_arith_x <= `FALSE;
2692
`endif
2693
`ifdef LM32_NO_BARREL_SHIFT
2694
        x_result_sel_shift_x <= `FALSE;
2695
`endif
2696
`ifdef CFG_SIGN_EXTEND_ENABLED
2697
        x_result_sel_sext_x <= `FALSE;
2698
`endif
2699
        x_result_sel_logic_x <= `FALSE;
2700
`ifdef CFG_USER_ENABLED
2701
        x_result_sel_user_x <= `FALSE;
2702
`endif
2703
        x_result_sel_add_x <= `FALSE;
2704
        m_result_sel_compare_x <= `FALSE;
2705
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2706
        m_result_sel_shift_x <= `FALSE;
2707
`endif
2708
        w_result_sel_load_x <= `FALSE;
2709
`ifdef CFG_PL_MULTIPLY_ENABLED
2710
        w_result_sel_mul_x <= `FALSE;
2711
`endif
2712
        x_bypass_enable_x <= `FALSE;
2713
        m_bypass_enable_x <= `FALSE;
2714
        write_enable_x <= `FALSE;
2715
        write_idx_x <= {`LM32_REG_IDX_WIDTH{1'b0}};
2716
        csr_x <= {`LM32_CSR_WIDTH{1'b0}};
2717
        load_x <= `FALSE;
2718
        store_x <= `FALSE;
2719
        size_x <= {`LM32_SIZE_WIDTH{1'b0}};
2720
        sign_extend_x <= `FALSE;
2721
        adder_op_x <= `FALSE;
2722
        adder_op_x_n <= `FALSE;
2723
        logic_op_x <= 4'h0;
2724
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2725
        direction_x <= `FALSE;
2726
`endif
2727
        branch_x <= `FALSE;
2728
        branch_predict_x <= `FALSE;
2729
        branch_predict_taken_x <= `FALSE;
2730
        condition_x <= `LM32_CONDITION_U1;
2731
`ifdef CFG_DEBUG_ENABLED
2732
        break_x <= `FALSE;
2733
`endif
2734
        scall_x <= `FALSE;
2735
        eret_x <= `FALSE;
2736
`ifdef CFG_DEBUG_ENABLED
2737
        bret_x <= `FALSE;
2738
`endif
2739
`ifdef CFG_BUS_ERRORS_ENABLED
2740
        bus_error_x <= `FALSE;
2741
        data_bus_error_exception_m <= `FALSE;
2742
`endif
2743
        csr_write_enable_x <= `FALSE;
2744
        operand_m <= {`LM32_WORD_WIDTH{1'b0}};
2745
        branch_target_m <= {`LM32_PC_WIDTH{1'b0}};
2746
        m_result_sel_compare_m <= `FALSE;
2747
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2748
        m_result_sel_shift_m <= `FALSE;
2749
`endif
2750
        w_result_sel_load_m <= `FALSE;
2751
`ifdef CFG_PL_MULTIPLY_ENABLED
2752
        w_result_sel_mul_m <= `FALSE;
2753
`endif
2754
        m_bypass_enable_m <= `FALSE;
2755
        branch_m <= `FALSE;
2756
        branch_predict_m <= `FALSE;
2757
        branch_predict_taken_m <= `FALSE;
2758
        exception_m <= `FALSE;
2759
        load_m <= `FALSE;
2760
        store_m <= `FALSE;
2761
        write_enable_m <= `FALSE;
2762
        write_idx_m <= {`LM32_REG_IDX_WIDTH{1'b0}};
2763
        condition_met_m <= `FALSE;
2764
`ifdef CFG_DCACHE_ENABLED
2765
        dflush_m <= `FALSE;
2766
`endif
2767
`ifdef CFG_DEBUG_ENABLED
2768
        debug_exception_m <= `FALSE;
2769
        non_debug_exception_m <= `FALSE;
2770
`endif
2771
        operand_w <= {`LM32_WORD_WIDTH{1'b0}};
2772
        w_result_sel_load_w <= `FALSE;
2773
`ifdef CFG_PL_MULTIPLY_ENABLED
2774
        w_result_sel_mul_w <= `FALSE;
2775
`endif
2776
        write_idx_w <= {`LM32_REG_IDX_WIDTH{1'b0}};
2777
        write_enable_w <= `FALSE;
2778
`ifdef CFG_DEBUG_ENABLED
2779
        debug_exception_w <= `FALSE;
2780
        non_debug_exception_w <= `FALSE;
2781
`else
2782
        exception_w <= `FALSE;
2783
`endif
2784
`ifdef CFG_BUS_ERRORS_ENABLED
2785
        memop_pc_w <= {`LM32_PC_WIDTH{1'b0}};
2786
`endif
2787
    end
2788
    else
2789
    begin
2790
        // D/X stage registers
2791
 
2792
        if (stall_x == `FALSE)
2793
        begin
2794
`ifdef CFG_USER_ENABLED
2795
            user_opcode <= user_opcode_d;
2796
`endif
2797
            operand_0_x <= d_result_0;
2798
            operand_1_x <= d_result_1;
2799
            store_operand_x <= bypass_data_1;
2800
            branch_target_x <= branch_reg_d == `TRUE ? bypass_data_0[`LM32_PC_RNG] : branch_target_d;
2801
            x_result_sel_csr_x <= x_result_sel_csr_d;
2802
`ifdef LM32_MC_ARITHMETIC_ENABLED
2803
            x_result_sel_mc_arith_x <= x_result_sel_mc_arith_d;
2804
`endif
2805
`ifdef LM32_NO_BARREL_SHIFT
2806
            x_result_sel_shift_x <= x_result_sel_shift_d;
2807
`endif
2808
`ifdef CFG_SIGN_EXTEND_ENABLED
2809
            x_result_sel_sext_x <= x_result_sel_sext_d;
2810
`endif
2811
            x_result_sel_logic_x <= x_result_sel_logic_d;
2812
`ifdef CFG_USER_ENABLED
2813
            x_result_sel_user_x <= x_result_sel_user_d;
2814
`endif
2815
            x_result_sel_add_x <= x_result_sel_add_d;
2816
            m_result_sel_compare_x <= m_result_sel_compare_d;
2817
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2818
            m_result_sel_shift_x <= m_result_sel_shift_d;
2819
`endif
2820
            w_result_sel_load_x <= w_result_sel_load_d;
2821
`ifdef CFG_PL_MULTIPLY_ENABLED
2822
            w_result_sel_mul_x <= w_result_sel_mul_d;
2823
`endif
2824
            x_bypass_enable_x <= x_bypass_enable_d;
2825
            m_bypass_enable_x <= m_bypass_enable_d;
2826
            load_x <= load_d;
2827
            store_x <= store_d;
2828
            branch_x <= branch_d;
2829
            branch_predict_x <= branch_predict_d;
2830
            branch_predict_taken_x <= branch_predict_taken_d;
2831
            write_idx_x <= write_idx_d;
2832
            csr_x <= csr_d;
2833
            size_x <= size_d;
2834
            sign_extend_x <= sign_extend_d;
2835
            adder_op_x <= adder_op_d;
2836
            adder_op_x_n <= ~adder_op_d;
2837
            logic_op_x <= logic_op_d;
2838
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2839
            direction_x <= direction_d;
2840
`endif
2841
            condition_x <= condition_d;
2842
            csr_write_enable_x <= csr_write_enable_d;
2843
`ifdef CFG_DEBUG_ENABLED
2844
            break_x <= break_d;
2845
`endif
2846
            scall_x <= scall_d;
2847
`ifdef CFG_BUS_ERRORS_ENABLED
2848
            bus_error_x <= bus_error_d;
2849
`endif
2850
            eret_x <= eret_d;
2851
`ifdef CFG_DEBUG_ENABLED
2852
            bret_x <= bret_d;
2853
`endif
2854
            write_enable_x <= write_enable_d;
2855
        end
2856
 
2857
        // X/M stage registers
2858
 
2859
        if (stall_m == `FALSE)
2860
        begin
2861
            operand_m <= x_result;
2862
            m_result_sel_compare_m <= m_result_sel_compare_x;
2863
`ifdef CFG_PL_BARREL_SHIFT_ENABLED
2864
            m_result_sel_shift_m <= m_result_sel_shift_x;
2865
`endif
2866
            if (exception_x == `TRUE)
2867
            begin
2868
                w_result_sel_load_m <= `FALSE;
2869
`ifdef CFG_PL_MULTIPLY_ENABLED
2870
                w_result_sel_mul_m <= `FALSE;
2871
`endif
2872
            end
2873
            else
2874
            begin
2875
                w_result_sel_load_m <= w_result_sel_load_x;
2876
`ifdef CFG_PL_MULTIPLY_ENABLED
2877
                w_result_sel_mul_m <= w_result_sel_mul_x;
2878
`endif
2879
            end
2880
            m_bypass_enable_m <= m_bypass_enable_x;
2881
            load_m <= load_x;
2882
            store_m <= store_x;
2883
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
2884
            branch_m <= branch_x && !branch_taken_x;
2885
`else
2886
            branch_m <= branch_x;
2887
            branch_predict_m <= branch_predict_x;
2888
            branch_predict_taken_m <= branch_predict_taken_x;
2889
`endif
2890
`ifdef CFG_DEBUG_ENABLED
2891
            // Data bus errors are generated by the wishbone and are
2892
            // made known to the processor only in next cycle (as a
2893
            // non-debug exception). A break instruction can be seen
2894
            // in same cycle (causing a debug exception). Handle non
2895
            // -debug exception first!
2896
            if (non_debug_exception_x == `TRUE)
2897
                write_idx_m <= `LM32_EA_REG;
2898
            else if (debug_exception_x == `TRUE)
2899
                write_idx_m <= `LM32_BA_REG;
2900
            else
2901
                write_idx_m <= write_idx_x;
2902
`else
2903
            if (exception_x == `TRUE)
2904
                write_idx_m <= `LM32_EA_REG;
2905
            else
2906
                write_idx_m <= write_idx_x;
2907
`endif
2908
            condition_met_m <= condition_met_x;
2909
`ifdef CFG_DEBUG_ENABLED
2910
            if (exception_x == `TRUE)
2911
                if (   (dc_re == `TRUE)
2912
`ifdef CFG_ALTERNATE_EBA
2913
                    || (at_debug == `TRUE)
2914
`endif
2915
                    || (   (debug_exception_x == `TRUE)
2916
                        && (non_debug_exception_x == `FALSE))
2917
                   )
2918
                    branch_target_m <= {deba, eid_x, {3{1'b0}}};
2919
                else
2920
                    branch_target_m <= {eba, eid_x, {3{1'b0}}};
2921
            else
2922
                branch_target_m <= branch_target_x;
2923
`else
2924
            branch_target_m <= exception_x == `TRUE ? {eba, eid_x, {3{1'b0}}} : branch_target_x;
2925
`endif
2926
`ifdef CFG_TRACE_ENABLED
2927
            eid_m <= eid_x;
2928
`endif
2929
`ifdef CFG_DCACHE_ENABLED
2930
            dflush_m <= dflush_x;
2931
`endif
2932
            eret_m <= eret_k_q_x;
2933
`ifdef CFG_DEBUG_ENABLED
2934
            bret_m <= bret_k_q_x;
2935
`endif
2936
            write_enable_m <= exception_x == `TRUE ? `TRUE : write_enable_x;
2937
`ifdef CFG_DEBUG_ENABLED
2938
            debug_exception_m <= debug_exception_x;
2939
            non_debug_exception_m <= non_debug_exception_x;
2940
`endif
2941
        end
2942
 
2943
        // State changing regs
2944
        if (stall_m == `FALSE)
2945
        begin
2946
            if ((exception_x == `TRUE) && (q_x == `TRUE) && (stall_x == `FALSE))
2947
                exception_m <= `TRUE;
2948
            else
2949
                exception_m <= `FALSE;
2950
`ifdef CFG_BUS_ERRORS_ENABLED
2951
            data_bus_error_exception_m <=    (data_bus_error_exception == `TRUE)
2952
`ifdef CFG_DEBUG_ENABLED
2953
`ifdef CFG_JTAG_ENABLED
2954
                                          && (reset_exception == `FALSE)
2955
`endif
2956
`endif
2957
                                          ;
2958
`endif
2959
        end
2960
 
2961
        // M/W stage registers
2962
`ifdef CFG_BUS_ERRORS_ENABLED
2963
        operand_w <= exception_m == `TRUE ? (data_bus_error_exception_m ? {memop_pc_w, 2'b00} : {pc_m, 2'b00}) : m_result;
2964
`else
2965
        operand_w <= exception_m == `TRUE ? {pc_m, 2'b00} : m_result;
2966
`endif
2967
        w_result_sel_load_w <= w_result_sel_load_m;
2968
`ifdef CFG_PL_MULTIPLY_ENABLED
2969
        w_result_sel_mul_w <= w_result_sel_mul_m;
2970
`endif
2971
        write_idx_w <= write_idx_m;
2972
`ifdef CFG_TRACE_ENABLED
2973
        eid_w <= eid_m;
2974
        eret_w <= eret_m;
2975
`ifdef CFG_DEBUG_ENABLED
2976
        bret_w <= bret_m;
2977
`endif
2978
`endif
2979
        write_enable_w <= write_enable_m;
2980
`ifdef CFG_DEBUG_ENABLED
2981
        debug_exception_w <= debug_exception_m;
2982
        non_debug_exception_w <= non_debug_exception_m;
2983
`else
2984
        exception_w <= exception_m;
2985
`endif
2986
`ifdef CFG_BUS_ERRORS_ENABLED
2987
        if (   (stall_m == `FALSE)
2988
            && (data_bus_error_exception == `FALSE)
2989
            && (   (load_q_m == `TRUE)
2990
                || (store_q_m == `TRUE)
2991
               )
2992
           )
2993
          memop_pc_w <= pc_m;
2994
`endif
2995
    end
2996
end
2997
 
2998
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
2999
// Buffer data read from register file, in case a stall occurs, and watch for
3000
// any writes to the modified registers
3001
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
3002
begin
3003
    if (rst_i == `TRUE)
3004
    begin
3005
        use_buf <= `FALSE;
3006
        reg_data_buf_0 <= {`LM32_WORD_WIDTH{1'b0}};
3007
        reg_data_buf_1 <= {`LM32_WORD_WIDTH{1'b0}};
3008
    end
3009
    else
3010
    begin
3011
        if (stall_d == `FALSE)
3012
            use_buf <= `FALSE;
3013
        else if (use_buf == `FALSE)
3014
        begin
3015
            reg_data_buf_0 <= reg_data_live_0;
3016
            reg_data_buf_1 <= reg_data_live_1;
3017
            use_buf <= `TRUE;
3018
        end
3019
        if (reg_write_enable_q_w == `TRUE)
3020
        begin
3021
            if (write_idx_w == read_idx_0_d)
3022
                reg_data_buf_0 <= w_result;
3023
            if (write_idx_w == read_idx_1_d)
3024
                reg_data_buf_1 <= w_result;
3025
        end
3026
    end
3027
end
3028
`endif
3029
 
3030
`ifdef LM32_EBR_REGISTER_FILE
3031
`else
3032
// Register file write port
3033
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
3034
begin
3035
    begin
3036
        if (reg_write_enable_q_w == `TRUE)
3037
            registers[write_idx_w] <= w_result;
3038
    end
3039
end
3040
`endif
3041
 
3042
`ifdef CFG_TRACE_ENABLED
3043
// PC tracing logic
3044
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
3045
begin
3046
    if (rst_i == `TRUE)
3047
    begin
3048
        trace_pc_valid <= `FALSE;
3049
        trace_pc <= {`LM32_PC_WIDTH{1'b0}};
3050
        trace_exception <= `FALSE;
3051
        trace_eid <= `LM32_EID_RESET;
3052
        trace_eret <= `FALSE;
3053
`ifdef CFG_DEBUG_ENABLED
3054
        trace_bret <= `FALSE;
3055
`endif
3056
        pc_c <= eba_reset/4;
3057
    end
3058
    else
3059
    begin
3060
        trace_pc_valid <= `FALSE;
3061
        // Has an exception occured
3062
`ifdef CFG_DEBUG_ENABLED
3063
        if ((debug_exception_q_w == `TRUE) || (non_debug_exception_q_w == `TRUE))
3064
`else
3065
        if (exception_q_w == `TRUE)
3066
`endif
3067
        begin
3068
            trace_exception <= `TRUE;
3069
            trace_pc_valid <= `TRUE;
3070
            trace_pc <= pc_w;
3071
            trace_eid <= eid_w;
3072
        end
3073
        else
3074
            trace_exception <= `FALSE;
3075
 
3076
        if ((valid_w == `TRUE) && (!kill_w))
3077
        begin
3078
            // An instruction is commiting. Determine if it is non-sequential
3079
            if (pc_c + 1'b1 != pc_w)
3080
            begin
3081
                // Non-sequential instruction
3082
                trace_pc_valid <= `TRUE;
3083
                trace_pc <= pc_w;
3084
            end
3085
            // Record PC so we can determine if next instruction is sequential or not
3086
            pc_c <= pc_w;
3087
            // Indicate if it was an eret/bret instruction
3088
            trace_eret <= eret_w;
3089
`ifdef CFG_DEBUG_ENABLED
3090
            trace_bret <= bret_w;
3091
`endif
3092
        end
3093
        else
3094
        begin
3095
            trace_eret <= `FALSE;
3096
`ifdef CFG_DEBUG_ENABLED
3097
            trace_bret <= `FALSE;
3098
`endif
3099
        end
3100
    end
3101
end
3102
`endif
3103
 
3104
/////////////////////////////////////////////////////
3105
// Behavioural Logic
3106
/////////////////////////////////////////////////////
3107
 
3108
// synthesis translate_off
3109
 
3110
// Reset register 0. Only needed for simulation.
3111
initial
3112
begin
3113
`ifdef LM32_EBR_REGISTER_FILE
3114
    reg_0.mem[0] = {`LM32_WORD_WIDTH{1'b0}};
3115
    reg_1.mem[0] = {`LM32_WORD_WIDTH{1'b0}};
3116
`else
3117
    registers[0] = {`LM32_WORD_WIDTH{1'b0}};
3118
`endif
3119
end
3120
 
3121
// synthesis translate_on
3122
 
3123
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.