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_instruction_unit.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
//   ALL RIGHTS RESERVED
6
//   ------------------------------------------------------------------
7
//
8
//   IMPORTANT: THIS FILE IS AUTO-GENERATED BY THE LATTICEMICO SYSTEM.
9
//
10
//   Permission:
11
//
12
//      Lattice Semiconductor grants permission to use this code
13
//      pursuant to the terms of the Lattice Semiconductor Corporation
14
//      Open Source License Agreement.
15
//
16
//   Disclaimer:
17
//
18
//      Lattice Semiconductor provides no warranty regarding the use or
19
//      functionality of this code. It is the user's responsibility to
20
//      verify the user's design for consistency and functionality through
21
//      the use of formal verification methods.
22
//
23
//   --------------------------------------------------------------------
24
//
25
//                  Lattice Semiconductor Corporation
26
//                  5555 NE Moore Court
27
//                  Hillsboro, OR 97214
28
//                  U.S.A
29
//
30
//                  TEL: 1-800-Lattice (USA and Canada)
31
//                         503-286-8001 (other locations)
32
//
33
//                  web: http://www.latticesemi.com/
34
//                  email: techsupport@latticesemi.com
35
//
36
//   --------------------------------------------------------------------
37
//                         FILE DETAILS
38
// Project      : LatticeMico32
39
// File         : lm32_instruction_unit.v
40
// Title        : Instruction unit
41
// Dependencies : lm32_include.v
42
// Version      : 6.1.17
43
//              : Initial Release
44
// Version      : 7.0SP2, 3.0
45
//              : No Change
46
// Version      : 3.1
47
//              : Support for static branch prediction is added. Fetching of
48
//              : instructions can also be altered by branches predicted in D
49
//              : stage of pipeline, and mispredicted branches in the X and M
50
//              : stages of the pipeline.
51
// Version      : 3.2
52
//              : EBRs use SYNC resets instead of ASYNC resets.
53
// Version      : 3.3
54
//              : Support for a non-cacheable Instruction Memory that has a
55
//              : single-cycle access latency. This memory can be accessed by
56
//              : data port of LM32 (so that debugger has access to it).
57
// Version      : 3.4
58
//              : No change
59
// Version      : 3.5
60
//              : Bug fix: Inline memory is correctly generated if it is not a
61
//              : power-of-two.
62
//              : Bug fix: Fixed a bug that caused LM32 (configured without
63
//              : instruction cache) to lock up in to an infinite loop due to a
64
//              : instruction bus error when EBA was set to instruction inline
65
//              : memory.
66
// Version      : 3.8
67
//              : Feature: Support for dynamically switching EBA to DEBA via a
68
//              : GPIO.
69
// =============================================================================
70
 
71
`include "lm32_include.v"
72
 
73
/////////////////////////////////////////////////////
74
// Module interface
75
/////////////////////////////////////////////////////
76
 
77
module lm32_instruction_unit (
78
    // ----- Inputs -------
79
    clk_i,
80
    rst_i,
81
`ifdef CFG_DEBUG_ENABLED
82
`ifdef CFG_ALTERNATE_EBA
83
    at_debug,
84
`endif
85
`endif
86
    // From pipeline
87
    stall_a,
88
    stall_f,
89
    stall_d,
90
    stall_x,
91
    stall_m,
92
    valid_f,
93
    valid_d,
94
    kill_f,
95
    branch_predict_taken_d,
96
    branch_predict_address_d,
97
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
98
    branch_taken_x,
99
    branch_target_x,
100
`endif
101
    exception_m,
102
    branch_taken_m,
103
    branch_mispredict_taken_m,
104
    branch_target_m,
105
`ifdef CFG_ICACHE_ENABLED
106
    iflush,
107
`endif
108
`ifdef CFG_DCACHE_ENABLED
109
    dcache_restart_request,
110
    dcache_refill_request,
111
    dcache_refilling,
112
`endif
113
`ifdef CFG_IROM_ENABLED
114
    irom_store_data_m,
115
    irom_address_xm,
116
    irom_we_xm,
117
`endif
118
`ifdef CFG_MMU_ENABLED
119
    itlb_enable,
120
    tlbpaddr,
121
    tlbvaddr,
122
    itlb_update,
123
    itlb_flush,
124
    itlb_invalidate,
125
`endif
126
`ifdef CFG_IWB_ENABLED
127
    // From Wishbone
128
    i_dat_i,
129
    i_ack_i,
130
    i_err_i,
131
`endif
132
`ifdef CFG_HW_DEBUG_ENABLED
133
    jtag_read_enable,
134
    jtag_write_enable,
135
    jtag_write_data,
136
    jtag_address,
137
`endif
138
    // ----- Outputs -------
139
    // To pipeline
140
    pc_f,
141
    pc_d,
142
    pc_x,
143
    pc_m,
144
    pc_w,
145
`ifdef CFG_ICACHE_ENABLED
146
    icache_stall_request,
147
    icache_restart_request,
148
    icache_refill_request,
149
    icache_refilling,
150
`endif
151
`ifdef CFG_IROM_ENABLED
152
    irom_data_m,
153
`endif
154
`ifdef CFG_MMU_ENABLED
155
    itlb_stall_request,
156
    itlb_miss_vfn,
157
    itlb_miss_x,
158
`endif
159
`ifdef CFG_IWB_ENABLED
160
    // To Wishbone
161
    i_dat_o,
162
    i_adr_o,
163
    i_cyc_o,
164
    i_sel_o,
165
    i_stb_o,
166
    i_we_o,
167
    i_cti_o,
168
    i_lock_o,
169
    i_bte_o,
170
`endif
171
`ifdef CFG_HW_DEBUG_ENABLED
172
    jtag_read_data,
173
    jtag_access_complete,
174
`endif
175
`ifdef CFG_BUS_ERRORS_ENABLED
176
    bus_error_d,
177
`endif
178
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
179
    instruction_f,
180
`endif
181
    instruction_d
182
    );
183
 
184
/////////////////////////////////////////////////////
185
// Parameters
186
/////////////////////////////////////////////////////
187
 
188
parameter eba_reset = `CFG_EBA_RESET;
189
parameter associativity = 1;                            // Associativity of the cache (Number of ways)
190
parameter sets = 512;                                   // Number of sets
191
parameter bytes_per_line = 16;                          // Number of bytes per cache line
192
parameter base_address = 0;                             // Base address of cachable memory
193
parameter limit = 0;                                    // Limit (highest address) of cachable memory
194
 
195
// For bytes_per_line == 4, we set 1 so part-select range isn't reversed, even though not really used
196
localparam addr_offset_width = bytes_per_line == 4 ? 1 : `CLOG2(bytes_per_line)-2;
197
localparam addr_offset_lsb = 2;
198
localparam addr_offset_msb = (addr_offset_lsb+addr_offset_width-1);
199
 
200
/////////////////////////////////////////////////////
201
// Inputs
202
/////////////////////////////////////////////////////
203
 
204
input clk_i;                                            // Clock
205
input rst_i;                                            // Reset
206
 
207
`ifdef CFG_DEBUG_ENABLED
208
`ifdef CFG_ALTERNATE_EBA
209
input at_debug;                                         // GPIO input that maps EBA to DEBA
210
`endif
211
`endif
212
 
213
input stall_a;                                          // Stall A stage instruction
214
input stall_f;                                          // Stall F stage instruction
215
input stall_d;                                          // Stall D stage instruction
216
input stall_x;                                          // Stall X stage instruction
217
input stall_m;                                          // Stall M stage instruction
218
input valid_f;                                          // Instruction in F stage is valid
219
input valid_d;                                          // Instruction in D stage is valid
220
input kill_f;                                           // Kill instruction in F stage
221
 
222
input branch_predict_taken_d;                           // Branch is predicted taken in D stage
223
input [`LM32_PC_RNG] branch_predict_address_d;          // Branch target address
224
 
225
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
226
input branch_taken_x;                                   // Branch instruction in X stage is taken
227
input [`LM32_PC_RNG] branch_target_x;                   // Target PC of X stage branch instruction
228
`endif
229
input exception_m;
230
input branch_taken_m;                                   // Branch instruction in M stage is taken
231
input branch_mispredict_taken_m;                        // Branch instruction in M stage is mispredicted as taken
232
input [`LM32_PC_RNG] branch_target_m;                   // Target PC of M stage branch instruction
233
 
234
`ifdef CFG_ICACHE_ENABLED
235
input iflush;                                           // Flush instruction cache
236
`endif
237
`ifdef CFG_DCACHE_ENABLED
238
input dcache_restart_request;                           // Restart instruction that caused a data cache miss
239
input dcache_refill_request;                            // Request to refill data cache
240
input dcache_refilling;
241
`endif
242
 
243
`ifdef CFG_IROM_ENABLED
244
input [`LM32_WORD_RNG] irom_store_data_m;               // Data from load-store unit
245
input [`LM32_WORD_RNG] irom_address_xm;                 // Address from load-store unit
246
input irom_we_xm;                                       // Indicates if memory operation is load or store
247
`endif
248
 
249
`ifdef CFG_MMU_ENABLED
250
input itlb_enable;                                      // Instruction TLB enable
251
input [`LM32_WORD_RNG] tlbpaddr;                        // TLBPADDR CSR
252
input [`LM32_WORD_RNG] tlbvaddr;                        // TLBVADDR CSR
253
input itlb_update;                                      // Instruction TLB update request
254
input itlb_flush;                                       // Instruction TLB flush request
255
input itlb_invalidate;                                  // Instruction TLB invalidate request
256
`endif
257
 
258
`ifdef CFG_IWB_ENABLED
259
input [`LM32_WORD_RNG] i_dat_i;                         // Instruction Wishbone interface read data
260
input i_ack_i;                                          // Instruction Wishbone interface acknowledgement
261
input i_err_i;                                          // Instruction Wishbone interface error
262
`endif
263
 
264
`ifdef CFG_HW_DEBUG_ENABLED
265
input jtag_read_enable;                                 // JTAG read memory request
266
input jtag_write_enable;                                // JTAG write memory request
267
input [`LM32_BYTE_RNG] jtag_write_data;                 // JTAG wrirte data
268
input [`LM32_WORD_RNG] jtag_address;                    // JTAG read/write address
269
`endif
270
 
271
/////////////////////////////////////////////////////
272
// Outputs
273
/////////////////////////////////////////////////////
274
 
275
output [`LM32_PC_RNG] pc_f;                             // F stage PC
276
reg    [`LM32_PC_RNG] pc_f;
277
output [`LM32_PC_RNG] pc_d;                             // D stage PC
278
reg    [`LM32_PC_RNG] pc_d;
279
output [`LM32_PC_RNG] pc_x;                             // X stage PC
280
reg    [`LM32_PC_RNG] pc_x;
281
output [`LM32_PC_RNG] pc_m;                             // M stage PC
282
reg    [`LM32_PC_RNG] pc_m;
283
output [`LM32_PC_RNG] pc_w;                             // W stage PC
284
reg    [`LM32_PC_RNG] pc_w;
285
 
286
`ifdef CFG_ICACHE_ENABLED
287
output icache_stall_request;                            // Instruction cache stall request
288
wire   icache_stall_request;
289
output icache_restart_request;                          // Request to restart instruction that cached instruction cache miss
290
wire   icache_restart_request;
291
output icache_refill_request;                           // Instruction cache refill request
292
wire   icache_refill_request;
293
output icache_refilling;                                // Indicates the icache is refilling
294
wire   icache_refilling;
295
`endif
296
 
297
`ifdef CFG_IROM_ENABLED
298
output [`LM32_WORD_RNG] irom_data_m;                    // Data to load-store unit on load
299
wire   [`LM32_WORD_RNG] irom_data_m;
300
`endif
301
 
302
`ifdef CFG_MMU_ENABLED
303
output itlb_stall_request;                              // Instruction TLB stall request
304
wire   itlb_stall_request;
305
output [`LM32_WORD_RNG] itlb_miss_vfn;                  // Virtual frame number of missed instruction
306
wire   [`LM32_WORD_RNG] itlb_miss_vfn;
307
output itlb_miss_x;                                     // Indicates if an instruction TLB miss occured in X stage
308
wire   itlb_miss_x;
309
`endif
310
 
311
`ifdef CFG_IWB_ENABLED
312
output [`LM32_WORD_RNG] i_dat_o;                        // Instruction Wishbone interface write data
313
`ifdef CFG_HW_DEBUG_ENABLED
314
reg    [`LM32_WORD_RNG] i_dat_o;
315
`else
316
wire   [`LM32_WORD_RNG] i_dat_o;
317
`endif
318
output [`LM32_WORD_RNG] i_adr_o;                        // Instruction Wishbone interface address
319
reg    [`LM32_WORD_RNG] i_adr_o;
320
output i_cyc_o;                                         // Instruction Wishbone interface cycle
321
reg    i_cyc_o;
322
output [`LM32_BYTE_SELECT_RNG] i_sel_o;                 // Instruction Wishbone interface byte select
323
`ifdef CFG_HW_DEBUG_ENABLED
324
reg    [`LM32_BYTE_SELECT_RNG] i_sel_o;
325
`else
326
wire   [`LM32_BYTE_SELECT_RNG] i_sel_o;
327
`endif
328
output i_stb_o;                                         // Instruction Wishbone interface strobe
329
reg    i_stb_o;
330
output i_we_o;                                          // Instruction Wishbone interface write enable
331
`ifdef CFG_HW_DEBUG_ENABLED
332
reg    i_we_o;
333
`else
334
wire   i_we_o;
335
`endif
336
output [`LM32_CTYPE_RNG] i_cti_o;                       // Instruction Wishbone interface cycle type
337
reg    [`LM32_CTYPE_RNG] i_cti_o;
338
output i_lock_o;                                        // Instruction Wishbone interface lock bus
339
reg    i_lock_o;
340
output [`LM32_BTYPE_RNG] i_bte_o;                       // Instruction Wishbone interface burst type
341
wire   [`LM32_BTYPE_RNG] i_bte_o;
342
`endif
343
 
344
`ifdef CFG_HW_DEBUG_ENABLED
345
output [`LM32_BYTE_RNG] jtag_read_data;                 // Data read for JTAG interface
346
reg    [`LM32_BYTE_RNG] jtag_read_data;
347
output jtag_access_complete;                            // Requested memory access by JTAG interface is complete
348
wire   jtag_access_complete;
349
`endif
350
 
351
`ifdef CFG_BUS_ERRORS_ENABLED
352
output bus_error_d;                                     // Indicates a bus error occured while fetching the instruction
353
reg    bus_error_d;
354
`endif
355
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
356
output [`LM32_INSTRUCTION_RNG] instruction_f;           // F stage instruction (only to have register indices extracted from)
357
wire   [`LM32_INSTRUCTION_RNG] instruction_f;
358
`endif
359
output [`LM32_INSTRUCTION_RNG] instruction_d;           // D stage instruction to be decoded
360
reg    [`LM32_INSTRUCTION_RNG] instruction_d;
361
 
362
/////////////////////////////////////////////////////
363
// Internal nets and registers
364
/////////////////////////////////////////////////////
365
 
366
reg [`LM32_PC_RNG] pc_a;                                // A stage PC
367
 
368
`ifdef LM32_CACHE_ENABLED
369
reg [`LM32_PC_RNG] restart_address;                     // Address to restart from after a cache miss
370
`endif
371
 
372
`ifdef CFG_ICACHE_ENABLED
373
wire icache_read_enable_f;                              // Indicates if instruction cache miss is valid
374
wire [`LM32_PC_RNG] icache_refill_address;              // Address that caused cache miss
375
`ifdef CFG_MMU_ENABLED
376
wire [`LM32_PC_RNG] icache_physical_refill_address;     // Physical address that caused cache miss
377
`endif
378
reg icache_refill_ready;                                // Indicates when next word of refill data is ready to be written to cache
379
reg [`LM32_INSTRUCTION_RNG] icache_refill_data;         // Next word of refill data, fetched from Wishbone
380
wire [`LM32_INSTRUCTION_RNG] icache_data_f;             // Instruction fetched from instruction cache
381
wire [`LM32_CTYPE_RNG] first_cycle_type;                // First Wishbone cycle type
382
wire [`LM32_CTYPE_RNG] next_cycle_type;                 // Next Wishbone cycle type
383
wire last_word;                                         // Indicates if this is the last word in the cache line
384
wire [`LM32_PC_RNG] first_address;                      // First cache refill address
385
`else
386
`ifdef CFG_IWB_ENABLED
387
reg [`LM32_INSTRUCTION_RNG] wb_data_f;                  // Instruction fetched from Wishbone
388
`endif
389
`endif
390
`ifdef CFG_IROM_ENABLED
391
wire irom_select_a;                                     // Indicates if A stage PC maps to a ROM address
392
reg irom_select_f;                                      // Indicates if F stage PC maps to a ROM address
393
wire [`LM32_INSTRUCTION_RNG] irom_data_f;               // Instruction fetched from ROM
394
`endif
395
`ifdef CFG_EBR_POSEDGE_REGISTER_FILE
396
`else
397
wire [`LM32_INSTRUCTION_RNG] instruction_f;             // F stage instruction
398
`endif
399
`ifdef CFG_BUS_ERRORS_ENABLED
400
reg bus_error_f;                                        // Indicates if a bus error occured while fetching the instruction in the F stage
401
`endif
402
 
403
`ifdef CFG_HW_DEBUG_ENABLED
404
reg jtag_access;                                        // Indicates if a JTAG WB access is in progress
405
`endif
406
 
407
`ifdef CFG_ALTERNATE_EBA
408
reg alternate_eba_taken;
409
`endif
410
 
411
`ifdef CFG_MMU_ENABLED
412
wire [`LM32_PC_RNG] physical_pc_f;                      // F stage physical PC
413
wire itlb_miss_f;                                       // Indicates if an instruction TLB miss occured in F stage
414
`endif
415
 
416
/////////////////////////////////////////////////////
417
// Functions
418
/////////////////////////////////////////////////////
419
 
420
/////////////////////////////////////////////////////
421
// Instantiations
422
/////////////////////////////////////////////////////
423
 
424
// Instruction ROM
425
`ifdef CFG_IROM_ENABLED
426
`define LM32_IROM_WIDTH `CLOG2(`CFG_IROM_LIMIT/4-`CFG_IROM_BASE_ADDRESS/4+1)
427
`define LM32_IROM_RNG (`LM32_IROM_WIDTH-1+2):2
428
 
429
lm32_dp_ram #(
430
    .data_width    (`LM32_WORD_WIDTH),
431
    .address_width (`LM32_IROM_WIDTH),
432
    .init_file     (`CFG_IROM_INIT_FILE)
433
  ) ram (
434
    // ----- Inputs -------
435
    .clk_a         (clk_i),
436
    .clk_b         (clk_i),
437
    .ce_a          (!stall_a),
438
    .ce_b          (!stall_x || !stall_m),
439
    .addr_a        (pc_a[`LM32_IROM_RNG]),
440
    .addr_b        (irom_address_xm[`LM32_IROM_RNG]),
441
    .di_a          ({32{1'b0}}),
442
    .di_b          (irom_store_data_m),
443
    .we_a          (`FALSE),
444
    .we_b          (irom_we_xm),
445
    // ----- Outputs -------
446
    .do_a          (irom_data_f),
447
    .do_b          (irom_data_m)
448
    );
449
`endif
450
 
451
`ifdef CFG_ICACHE_ENABLED
452
// Instruction cache
453
lm32_icache #(
454
    .associativity          (associativity),
455
    .sets                   (sets),
456
    .bytes_per_line         (bytes_per_line),
457
    .base_address           (base_address),
458
    .limit                  (limit)
459
    ) icache (
460
    // ----- Inputs -----
461
    .clk_i                  (clk_i),
462
    .rst_i                  (rst_i),
463
    .stall_a                (stall_a),
464
    .stall_f                (stall_f),
465
    .branch_predict_taken_d (branch_predict_taken_d),
466
    .valid_d                (valid_d),
467
    .address_a              (pc_a),
468
    .address_f              (pc_f),
469
`ifdef CFG_MMU_ENABLED
470
    .physical_address_f     (physical_pc_f),
471
`endif
472
    .read_enable_f          (icache_read_enable_f),
473
    .refill_ready           (icache_refill_ready),
474
    .refill_data            (icache_refill_data),
475
    .iflush                 (iflush),
476
    // ----- Outputs -----
477
    .stall_request          (icache_stall_request),
478
    .restart_request        (icache_restart_request),
479
    .refill_request         (icache_refill_request),
480
    .refill_address         (icache_refill_address),
481
`ifdef CFG_MMU_ENABLED
482
    .physical_refill_address(icache_physical_refill_address),
483
`endif
484
    .refilling              (icache_refilling),
485
    .inst                   (icache_data_f)
486
    );
487
`endif
488
 
489
`ifdef CFG_MMU_ENABLED
490
// Instruction TLB
491
lm32_itlb itlb (
492
    // ----- Inputs -----
493
    .clk_i                  (clk_i),
494
    .rst_i                  (rst_i),
495
    .enable                 (itlb_enable),
496
    .stall_a                (stall_a),
497
    .stall_f                (stall_f),
498
    .stall_d                (stall_d),
499
    .stall_x                (stall_x),
500
    .pc_a                   (pc_a),
501
    .pc_f                   (pc_f),
502
    .pc_x                   (pc_x),
503
    .read_enable_f          (icache_read_enable_f),
504
    .tlbpaddr               (tlbpaddr),
505
    .tlbvaddr               (tlbvaddr),
506
    .update                 (itlb_update),
507
    .flush                  (itlb_flush),
508
    .invalidate             (itlb_invalidate),
509
    // ----- Outputs -----
510
    .physical_pc_f          (physical_pc_f),
511
    .stall_request          (itlb_stall_request),
512
    .miss_vfn               (itlb_miss_vfn),
513
    .miss_f                 (itlb_miss_f),
514
    .miss_x                 (itlb_miss_x)
515
    );
516
`endif
517
 
518
/////////////////////////////////////////////////////
519
// Combinational Logic
520
/////////////////////////////////////////////////////
521
 
522
`ifdef CFG_ICACHE_ENABLED
523
// Generate signal that indicates when instruction cache misses are valid
524
assign icache_read_enable_f =    (valid_f == `TRUE)
525
                              && (kill_f == `FALSE)
526
`ifdef CFG_DCACHE_ENABLED
527
                              && (dcache_restart_request == `FALSE)
528
`endif
529
`ifdef CFG_IROM_ENABLED
530
                              && (irom_select_f == `FALSE)
531
`endif
532
`ifdef CFG_MMU_ENABLED
533
                              && (itlb_miss_f == `FALSE)
534
`endif
535
                              ;
536
`endif
537
 
538
// Compute address of next instruction to fetch
539
always @(*)
540
begin
541
    // The request from the latest pipeline stage must take priority
542
`ifdef CFG_DCACHE_ENABLED
543
    if (dcache_restart_request == `TRUE)
544
        pc_a = restart_address;
545
    else
546
`endif
547
      if (branch_taken_m == `TRUE)
548
        if ((branch_mispredict_taken_m == `TRUE) && (exception_m == `FALSE))
549
          pc_a = pc_x;
550
        else
551
          pc_a = branch_target_m;
552
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
553
      else if (branch_taken_x == `TRUE)
554
        pc_a = branch_target_x;
555
`endif
556
      else
557
        if ( (valid_d == `TRUE) && (branch_predict_taken_d == `TRUE) )
558
          pc_a = branch_predict_address_d;
559
        else
560
`ifdef CFG_ICACHE_ENABLED
561
          if (icache_restart_request == `TRUE)
562
            pc_a = restart_address;
563
          else
564
`endif
565
            pc_a = pc_f + 1'b1;
566
end
567
 
568
// Select where instruction should be fetched from
569
`ifdef CFG_IROM_ENABLED
570
assign irom_select_a = ({pc_a, 2'b00} >= `CFG_IROM_BASE_ADDRESS) && ({pc_a, 2'b00} <= `CFG_IROM_LIMIT);
571
`endif
572
 
573
// Select instruction from selected source
574
`ifdef CFG_ICACHE_ENABLED
575
`ifdef CFG_IROM_ENABLED
576
assign instruction_f = irom_select_f == `TRUE ? irom_data_f : icache_data_f;
577
`else
578
assign instruction_f = icache_data_f;
579
`endif
580
`else
581
`ifdef CFG_IROM_ENABLED
582
`ifdef CFG_IWB_ENABLED
583
assign instruction_f = irom_select_f == `TRUE ? irom_data_f : wb_data_f;
584
`else
585
assign instruction_f = irom_data_f;
586
`endif
587
`else
588
assign instruction_f = wb_data_f;
589
`endif
590
`endif
591
 
592
// Unused/constant Wishbone signals
593
`ifdef CFG_IWB_ENABLED
594
`ifdef CFG_HW_DEBUG_ENABLED
595
`else
596
assign i_dat_o = 32'd0;
597
assign i_we_o = `FALSE;
598
assign i_sel_o = 4'b1111;
599
`endif
600
assign i_bte_o = `LM32_BTYPE_LINEAR;
601
`endif
602
 
603
`ifdef CFG_ICACHE_ENABLED
604
// Determine parameters for next cache refill Wishbone access
605
generate
606
    case (bytes_per_line)
607
    4:
608
    begin
609
assign first_cycle_type = `LM32_CTYPE_END;
610
assign next_cycle_type = `LM32_CTYPE_END;
611
assign last_word = `TRUE;
612
`ifdef CFG_MMU_ENABLED
613
assign first_address = icache_physical_refill_address;
614
`else
615
assign first_address = icache_refill_address;
616
`endif
617
    end
618
    8:
619
    begin
620
assign first_cycle_type = `LM32_CTYPE_INCREMENTING;
621
assign next_cycle_type = `LM32_CTYPE_END;
622
assign last_word = i_adr_o[addr_offset_msb:addr_offset_lsb] == 1'b1;
623
`ifdef CFG_MMU_ENABLED
624
assign first_address = {icache_physical_refill_address[`LM32_PC_WIDTH+2-1:addr_offset_msb+1], {addr_offset_width{1'b0}}};
625
`else
626
assign first_address = {icache_refill_address[`LM32_PC_WIDTH+2-1:addr_offset_msb+1], {addr_offset_width{1'b0}}};
627
`endif
628
    end
629
    default:
630
    begin
631
assign first_cycle_type = `LM32_CTYPE_INCREMENTING;
632
assign next_cycle_type = i_adr_o[addr_offset_msb:addr_offset_lsb+1] == {addr_offset_width-1{1'b1}} ? `LM32_CTYPE_END : `LM32_CTYPE_INCREMENTING;
633
assign last_word = (&i_adr_o[addr_offset_msb:addr_offset_lsb]) == 1'b1;
634
`ifdef CFG_MMU_ENABLED
635
assign first_address = {icache_physical_refill_address[`LM32_PC_WIDTH+2-1:addr_offset_msb+1], {addr_offset_width{1'b0}}};
636
`else
637
assign first_address = {icache_refill_address[`LM32_PC_WIDTH+2-1:addr_offset_msb+1], {addr_offset_width{1'b0}}};
638
`endif
639
    end
640
    endcase
641
endgenerate
642
`endif
643
 
644
/////////////////////////////////////////////////////
645
// Sequential Logic
646
/////////////////////////////////////////////////////
647
 
648
// PC
649
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
650
begin
651
    if (rst_i == `TRUE)
652
    begin
653
`ifdef CFG_DEBUG_ENABLED
654
`ifdef CFG_ALTERNATE_EBA
655
        if (at_debug == `TRUE)
656
            pc_f <= (`CFG_DEBA_RESET-4)/4;
657
        else
658
            pc_f <= (eba_reset-4)/4;
659
`else
660
        pc_f <= (eba_reset-4)/4;
661
`endif
662
`else
663
        pc_f <= (eba_reset-4)/4;
664
`endif
665
        pc_d <= {`LM32_PC_WIDTH{1'b0}};
666
        pc_x <= {`LM32_PC_WIDTH{1'b0}};
667
        pc_m <= {`LM32_PC_WIDTH{1'b0}};
668
        pc_w <= {`LM32_PC_WIDTH{1'b0}};
669
    end
670
    else
671
    begin
672
        if (stall_f == `FALSE)
673
            pc_f <= pc_a;
674
        if (stall_d == `FALSE)
675
            pc_d <= pc_f;
676
        if (stall_x == `FALSE)
677
            pc_x <= pc_d;
678
        if (stall_m == `FALSE)
679
            pc_m <= pc_x;
680
        pc_w <= pc_m;
681
    end
682
end
683
 
684
`ifdef LM32_CACHE_ENABLED
685
// Address to restart from after a cache miss has been handled
686
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
687
begin
688
    if (rst_i == `TRUE)
689
        restart_address <= {`LM32_PC_WIDTH{1'b0}};
690
    else
691
    begin
692
`ifdef CFG_DCACHE_ENABLED
693
`ifdef CFG_ICACHE_ENABLED
694
            // D-cache restart address must take priority, otherwise instructions will be lost
695
            if (dcache_refill_request == `TRUE)
696
                restart_address <= pc_w;
697
            else if ((icache_refill_request == `TRUE) && (!dcache_refilling) && (!dcache_restart_request))
698
                restart_address <= icache_refill_address;
699
`else
700
            if (dcache_refill_request == `TRUE)
701
                restart_address <= pc_w;
702
`endif
703
`else
704
`ifdef CFG_ICACHE_ENABLED
705
            if (icache_refill_request == `TRUE)
706
                restart_address <= icache_refill_address;
707
`endif
708
`endif
709
    end
710
end
711
`endif
712
 
713
// Record where instruction was fetched from
714
`ifdef CFG_IROM_ENABLED
715
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
716
begin
717
    if (rst_i == `TRUE)
718
        irom_select_f <= `FALSE;
719
    else
720
    begin
721
        if (stall_f == `FALSE)
722
            irom_select_f <= irom_select_a;
723
    end
724
end
725
`endif
726
 
727
`ifdef CFG_HW_DEBUG_ENABLED
728
assign jtag_access_complete = (i_cyc_o == `TRUE) && ((i_ack_i == `TRUE) || (i_err_i == `TRUE)) && (jtag_access == `TRUE);
729
always @(*)
730
begin
731
    case (jtag_address[1:0])
732
    2'b00: jtag_read_data = i_dat_i[`LM32_BYTE_3_RNG];
733
    2'b01: jtag_read_data = i_dat_i[`LM32_BYTE_2_RNG];
734
    2'b10: jtag_read_data = i_dat_i[`LM32_BYTE_1_RNG];
735
    2'b11: jtag_read_data = i_dat_i[`LM32_BYTE_0_RNG];
736
    endcase
737
end
738
`endif
739
 
740
`ifdef CFG_IWB_ENABLED
741
// Instruction Wishbone interface
742
`ifdef CFG_ICACHE_ENABLED
743
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
744
begin
745
    if (rst_i == `TRUE)
746
    begin
747
        i_cyc_o <= `FALSE;
748
        i_stb_o <= `FALSE;
749
        i_adr_o <= {`LM32_WORD_WIDTH{1'b0}};
750
        i_cti_o <= `LM32_CTYPE_END;
751
        i_lock_o <= `FALSE;
752
        icache_refill_data <= {`LM32_INSTRUCTION_WIDTH{1'b0}};
753
        icache_refill_ready <= `FALSE;
754
`ifdef CFG_BUS_ERRORS_ENABLED
755
        bus_error_f <= `FALSE;
756
`endif
757
`ifdef CFG_HW_DEBUG_ENABLED
758
        i_we_o <= `FALSE;
759
        i_sel_o <= 4'b1111;
760
        jtag_access <= `FALSE;
761
`endif
762
    end
763
    else
764
    begin
765
        icache_refill_ready <= `FALSE;
766
        // Is a cycle in progress?
767
        if (i_cyc_o == `TRUE)
768
        begin
769
            // Has cycle completed?
770
            if ((i_ack_i == `TRUE) || (i_err_i == `TRUE))
771
            begin
772
`ifdef CFG_HW_DEBUG_ENABLED
773
                if (jtag_access == `TRUE)
774
                begin
775
                    i_cyc_o <= `FALSE;
776
                    i_stb_o <= `FALSE;
777
                    i_we_o <= `FALSE;
778
                    jtag_access <= `FALSE;
779
                end
780
                else
781
`endif
782
                begin
783
                    if (last_word == `TRUE)
784
                    begin
785
                        // Cache line fill complete
786
                        i_cyc_o <= `FALSE;
787
                        i_stb_o <= `FALSE;
788
                        i_lock_o <= `FALSE;
789
                    end
790
                    // Fetch next word in cache line
791
                    i_adr_o[addr_offset_msb:addr_offset_lsb] <= i_adr_o[addr_offset_msb:addr_offset_lsb] + 1'b1;
792
                    i_cti_o <= next_cycle_type;
793
                    // Write fetched data into instruction cache
794
                    icache_refill_ready <= `TRUE;
795
                    icache_refill_data <= i_dat_i;
796
                end
797
            end
798
`ifdef CFG_BUS_ERRORS_ENABLED
799
            if (i_err_i == `TRUE)
800
            begin
801
                bus_error_f <= `TRUE;
802
                $display ("Instruction bus error. Address: %x", i_adr_o);
803
            end
804
`endif
805
        end
806
        else
807
        begin
808
            if ((icache_refill_request == `TRUE) && (icache_refill_ready == `FALSE))
809
            begin
810
                // Read first word of cache line
811
`ifdef CFG_HW_DEBUG_ENABLED
812
                i_sel_o <= 4'b1111;
813
`endif
814
                i_adr_o <= {first_address, 2'b00};
815
                i_cyc_o <= `TRUE;
816
                i_stb_o <= `TRUE;
817
                i_cti_o <= first_cycle_type;
818
                //i_lock_o <= `TRUE;
819
`ifdef CFG_BUS_ERRORS_ENABLED
820
                bus_error_f <= `FALSE;
821
`endif
822
            end
823
`ifdef CFG_HW_DEBUG_ENABLED
824
            else
825
            begin
826
                if ((jtag_read_enable == `TRUE) || (jtag_write_enable == `TRUE))
827
                begin
828
                    case (jtag_address[1:0])
829
                    2'b00: i_sel_o <= 4'b1000;
830
                    2'b01: i_sel_o <= 4'b0100;
831
                    2'b10: i_sel_o <= 4'b0010;
832
                    2'b11: i_sel_o <= 4'b0001;
833
                    endcase
834
                    i_adr_o <= jtag_address;
835
                    i_dat_o <= {4{jtag_write_data}};
836
                    i_cyc_o <= `TRUE;
837
                    i_stb_o <= `TRUE;
838
                    i_we_o <= jtag_write_enable;
839
                    i_cti_o <= `LM32_CTYPE_END;
840
                    jtag_access <= `TRUE;
841
                end
842
            end
843
`endif
844
`ifdef CFG_BUS_ERRORS_ENABLED
845
            // Clear bus error when exception taken, otherwise they would be
846
            // continually generated if exception handler is cached
847
`ifdef CFG_FAST_UNCONDITIONAL_BRANCH
848
            if (branch_taken_x == `TRUE)
849
                bus_error_f <= `FALSE;
850
`endif
851
            if (branch_taken_m == `TRUE)
852
                bus_error_f <= `FALSE;
853
`endif
854
        end
855
    end
856
end
857
`else
858
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
859
begin
860
    if (rst_i == `TRUE)
861
    begin
862
        i_cyc_o <= `FALSE;
863
        i_stb_o <= `FALSE;
864
        i_adr_o <= {`LM32_WORD_WIDTH{1'b0}};
865
        i_cti_o <= `LM32_CTYPE_END;
866
        i_lock_o <= `FALSE;
867
        wb_data_f <= {`LM32_INSTRUCTION_WIDTH{1'b0}};
868
`ifdef CFG_BUS_ERRORS_ENABLED
869
        bus_error_f <= `FALSE;
870
`endif
871
    end
872
    else
873
    begin
874
        // Is a cycle in progress?
875
        if (i_cyc_o == `TRUE)
876
        begin
877
            // Has cycle completed?
878
            if((i_ack_i == `TRUE) || (i_err_i == `TRUE))
879
            begin
880
                // Cycle complete
881
                i_cyc_o <= `FALSE;
882
                i_stb_o <= `FALSE;
883
                // Register fetched instruction
884
                wb_data_f <= i_dat_i;
885
            end
886
`ifdef CFG_BUS_ERRORS_ENABLED
887
            if (i_err_i == `TRUE)
888
            begin
889
                bus_error_f <= `TRUE;
890
                $display ("Instruction bus error. Address: %x", i_adr_o);
891
            end
892
`endif
893
        end
894
        else
895
        begin
896
            // Wait for an instruction fetch from an external address
897
            if (   (stall_a == `FALSE)
898
`ifdef CFG_IROM_ENABLED
899
                && (irom_select_a == `FALSE)
900
`endif
901
               )
902
            begin
903
                // Fetch instruction
904
`ifdef CFG_HW_DEBUG_ENABLED
905
                i_sel_o <= 4'b1111;
906
`endif
907
                i_adr_o <= {pc_a, 2'b00};
908
                i_cyc_o <= `TRUE;
909
                i_stb_o <= `TRUE;
910
`ifdef CFG_BUS_ERRORS_ENABLED
911
                bus_error_f <= `FALSE;
912
`endif
913
            end
914
            else
915
            begin
916
                if (   (stall_a == `FALSE)
917
`ifdef CFG_IROM_ENABLED
918
                    && (irom_select_a == `TRUE)
919
`endif
920
                   )
921
                begin
922
`ifdef CFG_BUS_ERRORS_ENABLED
923
                    bus_error_f <= `FALSE;
924
`endif
925
                end
926
            end
927
        end
928
    end
929
end
930
`endif
931
`endif
932
 
933
// Instruction register
934
always @(posedge clk_i `CFG_RESET_SENSITIVITY)
935
begin
936
    if (rst_i == `TRUE)
937
    begin
938
        instruction_d <= {`LM32_INSTRUCTION_WIDTH{1'b0}};
939
`ifdef CFG_BUS_ERRORS_ENABLED
940
        bus_error_d <= `FALSE;
941
`endif
942
    end
943
    else
944
    begin
945
        if (stall_d == `FALSE)
946
        begin
947
            instruction_d <= instruction_f;
948
`ifdef CFG_BUS_ERRORS_ENABLED
949
            bus_error_d <= bus_error_f;
950
`endif
951
        end
952
    end
953
end
954
 
955
endmodule

powered by: WebSVN 2.1.0

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