OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber25/] [a25_decode.v] - Blame information for rev 60

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
//  Decode stage of Amber 25 Core                               //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  This module is the most complex part of the Amber core      //
10
//  It decodes and sequences all instructions and handles all   //
11
//  interrupts                                                  //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Conor Santifort, csantifort.amber@gmail.com           //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2011 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
//////////////////////////////////////////////////////////////////
42
 
43
 
44
module a25_decode
45
(
46
input                       i_clk,
47
input       [31:0]          i_fetch_instruction,
48 35 csantifort
input                       i_core_stall,                   // stall all stages of the Amber core at the same time
49 16 csantifort
input                       i_irq,                          // interrupt request
50
input                       i_firq,                         // Fast interrupt request
51
input                       i_dabt,                         // data abort interrupt request
52
input                       i_iabt,                         // instruction pre-fetch abort flag
53
input                       i_adex,                         // Address Exception
54
input       [31:0]          i_execute_iaddress,             // Registered instruction address output by execute stage
55
input       [31:0]          i_execute_daddress,             // Registered instruction address output by execute stage
56
input       [7:0]           i_abt_status,                   // Abort status
57
input       [31:0]          i_execute_status_bits,          // current status bits values in execute stage
58
input                       i_multiply_done,                // multiply unit is nearly done
59
 
60
 
61
// --------------------------------------------------
62
// Control signals to execute stage
63
// --------------------------------------------------
64
output reg  [31:0]          o_imm32 = 'd0,
65
output reg  [4:0]           o_imm_shift_amount = 'd0,
66
output reg                  o_shift_imm_zero = 'd0,
67
output reg  [3:0]           o_condition = 4'he,             // 4'he = al
68
output reg                  o_decode_exclusive = 'd0,       // exclusive access request ( swap instruction )
69
output reg                  o_decode_iaccess = 1'd1,        // Indicates an instruction access
70
output reg                  o_decode_daccess = 'd0,         // Indicates a data access
71
output reg  [1:0]           o_status_bits_mode = 2'b11,     // SVC
72
output reg                  o_status_bits_irq_mask = 1'd1,
73
output reg                  o_status_bits_firq_mask = 1'd1,
74
 
75
output reg  [3:0]           o_rm_sel  = 'd0,
76
output reg  [3:0]           o_rs_sel  = 'd0,
77
output reg  [7:0]           o_load_rd = 'd0,                // [7] load flags with PC
78
                                                            // [6] load status bits with PC
79
                                                            // [5] Write into User Mode register
80
                                                            // [4] zero-extend load
81
                                                            // [3:0] destination register, Rd
82
output reg  [3:0]           o_rn_sel  = 'd0,
83
output reg  [1:0]           o_barrel_shift_amount_sel = 'd0,
84
output reg  [1:0]           o_barrel_shift_data_sel = 'd0,
85
output reg  [1:0]           o_barrel_shift_function = 'd0,
86
output reg  [8:0]           o_alu_function = 'd0,
87
output reg  [1:0]           o_multiply_function = 'd0,
88
output reg  [2:0]           o_interrupt_vector_sel = 'd0,
89
output reg  [3:0]           o_iaddress_sel = 4'd2,
90
output reg  [3:0]           o_daddress_sel = 4'd2,
91
output reg  [2:0]           o_pc_sel = 3'd2,
92
output reg  [1:0]           o_byte_enable_sel = 'd0,        // byte, halfword or word write
93
output reg  [2:0]           o_status_bits_sel = 'd0,
94
output reg  [2:0]           o_reg_write_sel,
95
output reg                  o_user_mode_regs_store_nxt,
96
output reg                  o_firq_not_user_mode,
97
 
98
output reg                  o_write_data_wen = 'd0,
99
output reg                  o_base_address_wen = 'd0,       // save ldm base address register
100
                                                            // in case of data abort
101
output reg                  o_pc_wen = 1'd1,
102
output reg  [14:0]          o_reg_bank_wen = 'd0,
103
output reg                  o_status_bits_flags_wen = 'd0,
104
output reg                  o_status_bits_mode_wen = 'd0,
105
output reg                  o_status_bits_irq_mask_wen = 'd0,
106
output reg                  o_status_bits_firq_mask_wen = 'd0,
107
 
108
// --------------------------------------------------
109
// Co-Processor interface
110
// --------------------------------------------------
111
output reg  [2:0]           o_copro_opcode1 = 'd0,
112
output reg  [2:0]           o_copro_opcode2 = 'd0,
113
output reg  [3:0]           o_copro_crn = 'd0,
114
output reg  [3:0]           o_copro_crm = 'd0,
115
output reg  [3:0]           o_copro_num = 'd0,
116
output reg  [1:0]           o_copro_operation = 'd0, // 0 = no operation, 
117
                                                     // 1 = Move to Amber Core Register from Coprocessor
118
                                                     // 2 = Move to Coprocessor from Amber Core Register
119
output reg                  o_copro_write_data_wen = 'd0,
120
output                      o_iabt_trigger,
121
output      [31:0]          o_iabt_address,
122
output      [7:0]           o_iabt_status,
123
output                      o_dabt_trigger,
124
output      [31:0]          o_dabt_address,
125
output      [7:0]           o_dabt_status,
126 20 csantifort
output                      o_conflict,
127
output reg                  o_rn_use_read,
128
output reg                  o_rm_use_read,
129
output reg                  o_rs_use_read,
130
output reg                  o_rd_use_read
131 16 csantifort
 
132
);
133
 
134
`include "a25_localparams.v"
135
`include "a25_functions.v"
136
 
137
localparam [4:0] RST_WAIT1      = 5'd0,
138
                 RST_WAIT2      = 5'd1,
139
                 INT_WAIT1      = 5'd2,
140
                 INT_WAIT2      = 5'd3,
141
                 EXECUTE        = 5'd4,
142
                 PRE_FETCH_EXEC = 5'd5,  // Execute the Pre-Fetched Instruction
143
                 MEM_WAIT1      = 5'd6,  // conditionally decode current instruction, in case
144
                                         // previous instruction does not execute in S2
145
                 MEM_WAIT2      = 5'd7,
146
                 PC_STALL1      = 5'd8,  // Program Counter altered
147
                                         // conditionally decude current instruction, in case
148
                                         // previous instruction does not execute in S2
149
                 PC_STALL2      = 5'd9,
150
                 MTRANS_EXEC1   = 5'd10,
151
                 MTRANS_EXEC2   = 5'd11,
152
                 MTRANS_ABORT   = 5'd12,
153
                 MULT_PROC1     = 5'd13,  // first cycle, save pre fetch instruction
154
                 MULT_PROC2     = 5'd14,  // do multiplication
155
                 MULT_STORE     = 5'd15,  // save RdLo
156
                 MULT_ACCUMU    = 5'd16,  // Accumulate add lower 32 bits
157
                 SWAP_WRITE     = 5'd17,
158
                 SWAP_WAIT1     = 5'd18,
159
                 SWAP_WAIT2     = 5'd19,
160
                 COPRO_WAIT     = 5'd20;
161
 
162
 
163
// ========================================================
164
// Internal signals
165
// ========================================================
166
wire    [31:0]         instruction;
167 35 csantifort
wire    [3:0]          type;                    // regop, mem access etc.
168 16 csantifort
wire                   instruction_iabt;        // abort flag, follows the instruction
169
wire                   instruction_adex;        // address exception flag, follows the instruction
170
wire    [31:0]         instruction_address;     // instruction virtual address, follows 
171
                                                // the instruction
172
wire    [7:0]          instruction_iabt_status; // abort status, follows the instruction
173
wire    [1:0]          instruction_sel;
174
wire    [3:0]          opcode;
175
wire    [7:0]          imm8;
176
wire    [31:0]         offset12;
177
wire    [31:0]         offset24;
178
wire    [4:0]          shift_imm;
179
 
180
wire                   opcode_compare;
181
wire                   mem_op;
182
wire                   load_op;
183
wire                   store_op;
184
wire                   write_pc;
185
wire                   current_write_pc;
186
reg                    load_pc_nxt;
187
reg                    load_pc_r = 'd0;
188
wire                   immediate_shift_op;
189
wire                   rds_use_rs;
190
wire                   branch;
191
wire                   mem_op_pre_indexed;
192
wire                   mem_op_post_indexed;
193
 
194
// Flop inputs
195
wire    [31:0]         imm32_nxt;
196
wire    [4:0]          imm_shift_amount_nxt;
197
wire                   shift_imm_zero_nxt;
198
wire    [3:0]          condition_nxt;
199
reg                    decode_exclusive_nxt;
200
reg                    decode_iaccess_nxt;
201
reg                    decode_daccess_nxt;
202
 
203
reg     [1:0]          barrel_shift_function_nxt;
204
wire    [8:0]          alu_function_nxt;
205
reg     [1:0]          multiply_function_nxt;
206
reg     [1:0]          status_bits_mode_nxt;
207
reg                    status_bits_irq_mask_nxt;
208
reg                    status_bits_firq_mask_nxt;
209
 
210
wire    [3:0]          rm_sel_nxt;
211
wire    [3:0]          rs_sel_nxt;
212
 
213
wire    [3:0]          rn_sel_nxt;
214
reg     [1:0]          barrel_shift_amount_sel_nxt;
215
reg     [1:0]          barrel_shift_data_sel_nxt;
216
reg     [3:0]          iaddress_sel_nxt;
217
reg     [3:0]          daddress_sel_nxt;
218
reg     [2:0]          pc_sel_nxt;
219
reg     [1:0]          byte_enable_sel_nxt;
220
reg     [2:0]          status_bits_sel_nxt;
221
reg     [2:0]          reg_write_sel_nxt;
222
wire                   firq_not_user_mode_nxt;
223
 
224
// ALU Function signals
225
reg                    alu_swap_sel_nxt;
226
reg                    alu_not_sel_nxt;
227
reg     [1:0]          alu_cin_sel_nxt;
228
reg                    alu_cout_sel_nxt;
229
reg     [3:0]          alu_out_sel_nxt;
230
 
231
reg                    write_data_wen_nxt;
232
reg                    copro_write_data_wen_nxt;
233
reg                    base_address_wen_nxt;
234
reg                    pc_wen_nxt;
235
reg     [14:0]         reg_bank_wen_nxt;
236
reg                    status_bits_flags_wen_nxt;
237
reg                    status_bits_mode_wen_nxt;
238
reg                    status_bits_irq_mask_wen_nxt;
239
reg                    status_bits_firq_mask_wen_nxt;
240
 
241
reg                    saved_current_instruction_wen;   // saved load instruction
242
reg                    pre_fetch_instruction_wen;       // pre-fetch instruction
243
 
244
reg     [4:0]          control_state = RST_WAIT1;
245
reg     [4:0]          control_state_nxt;
246
 
247
 
248
wire                   dabt;
249
reg                    dabt_reg = 'd0;
250
reg                    dabt_reg_d1;
251
reg                    iabt_reg = 'd0;
252
reg                    adex_reg = 'd0;
253
reg     [31:0]         fetch_address_r = 'd0;
254
reg     [7:0]          abt_status_reg = 'd0;
255
reg     [31:0]         fetch_instruction_r = 'd0;
256 35 csantifort
reg     [3:0]          fetch_instruction_type_r = 'd0;
257 16 csantifort
reg     [31:0]         saved_current_instruction = 'd0;
258 35 csantifort
reg     [3:0]          saved_current_instruction_type = 'd0;
259 16 csantifort
reg                    saved_current_instruction_iabt = 'd0;          // access abort flag
260
reg                    saved_current_instruction_adex = 'd0;          // address exception
261
reg     [31:0]         saved_current_instruction_address = 'd0;       // virtual address of abort instruction
262
reg     [7:0]          saved_current_instruction_iabt_status = 'd0;   // status of abort instruction
263
reg     [31:0]         pre_fetch_instruction = 'd0;
264 35 csantifort
reg     [3:0]          pre_fetch_instruction_type = 'd0;
265 16 csantifort
reg                    pre_fetch_instruction_iabt = 'd0;              // access abort flag
266
reg                    pre_fetch_instruction_adex = 'd0;              // address exception
267
reg     [31:0]         pre_fetch_instruction_address = 'd0;           // virtual address of abort instruction
268
reg     [7:0]          pre_fetch_instruction_iabt_status = 'd0;       // status of abort instruction
269
reg     [31:0]         hold_instruction = 'd0;
270 35 csantifort
reg     [3:0]          hold_instruction_type = 'd0;
271 16 csantifort
reg                    hold_instruction_iabt = 'd0;                   // access abort flag
272
reg                    hold_instruction_adex = 'd0;                   // address exception
273
reg     [31:0]         hold_instruction_address = 'd0;                // virtual address of abort instruction
274
reg     [7:0]          hold_instruction_iabt_status = 'd0;            // status of abort instruction
275
 
276
wire                   instruction_valid;
277
wire                   instruction_execute;
278 20 csantifort
reg                    instruction_execute_r = 'd0;
279 16 csantifort
 
280
reg     [3:0]          mtrans_reg1;             // the current register being accessed as part of stm/ldm
281
reg     [3:0]          mtrans_reg2;             // the next register being accessed as part of stm/ldm
282
reg     [31:0]         mtrans_instruction_nxt;
283
wire    [15:0]         mtrans_reg2_mask;
284
 
285
wire   [31:0]          mtrans_base_reg_change;
286
wire   [4:0]           mtrans_num_registers;
287
wire                   use_saved_current_instruction;
288
wire                   use_hold_instruction;
289
wire                   use_pre_fetch_instruction;
290
wire                   interrupt;
291 60 csantifort
wire                   interrupt_or_conflict;
292 16 csantifort
wire   [1:0]           interrupt_mode;
293
wire   [2:0]           next_interrupt;
294
reg                    irq = 'd0;
295
reg                    firq = 'd0;
296
wire                   firq_request;
297
wire                   irq_request;
298
wire                   swi_request;
299
wire                   und_request;
300
wire                   dabt_request;
301
reg    [1:0]           copro_operation_nxt;
302
reg                    restore_base_address = 'd0;
303
reg                    restore_base_address_nxt;
304
 
305
wire                   regop_set_flags;
306
 
307
wire    [7:0]          load_rd_nxt;
308
wire                   load_rd_byte;
309
wire                   ldm_user_mode;
310
wire                   ldm_status_bits;
311
wire                   ldm_flags;
312
wire    [6:0]          load_rd_d1_nxt;
313
reg     [6:0]          load_rd_d1 = 'd0;  // MSB is the valid bit
314 35 csantifort
 
315 16 csantifort
wire                   rn_valid;
316
wire                   rm_valid;
317
wire                   rs_valid;
318
wire                   rd_valid;
319
wire                   stm_valid;
320
wire                   rn_conflict1;
321
wire                   rn_conflict2;
322
wire                   rm_conflict1;
323
wire                   rm_conflict2;
324
wire                   rs_conflict1;
325
wire                   rs_conflict2;
326
wire                   rd_conflict1;
327
wire                   rd_conflict2;
328
wire                   stm_conflict1a;
329
wire                   stm_conflict1b;
330
wire                   stm_conflict2a;
331
wire                   stm_conflict2b;
332
wire                   conflict1;          // Register conflict1 with ldr operation
333
wire                   conflict2;          // Register conflict1 with ldr operation
334 35 csantifort
wire                   conflict;           // Register conflict1 with ldr operation
335 16 csantifort
reg                    conflict_r = 'd0;
336 20 csantifort
reg                    rn_conflict1_r = 'd0;
337
reg                    rm_conflict1_r = 'd0;
338
reg                    rs_conflict1_r = 'd0;
339
reg                    rd_conflict1_r = 'd0;
340 16 csantifort
 
341
 
342
// ========================================================
343
// Instruction Abort and Data Abort outputs
344
// ========================================================
345
 
346
assign o_iabt_trigger     = instruction_iabt && o_status_bits_mode == SVC && control_state == INT_WAIT1;
347
assign o_iabt_address     = instruction_address;
348
assign o_iabt_status      = instruction_iabt_status;
349
 
350
assign o_dabt_trigger     = dabt_reg && !dabt_reg_d1;
351
assign o_dabt_address     = fetch_address_r;
352
assign o_dabt_status      = abt_status_reg;
353
 
354
 
355
// ========================================================
356
// Instruction Decode
357
// ========================================================
358
 
359
// for instructions that take more than one cycle
360
// the instruction is saved in the 'saved_mem_instruction'
361
// register and then that register is used for the rest of
362
// the execution of the instruction.
363
// But if the instruction does not execute because of the
364
// condition, then need to select the next instruction to
365
// decode
366
assign use_saved_current_instruction = instruction_execute &&
367
                          ( control_state == MEM_WAIT1     ||
368
                            control_state == MEM_WAIT2     ||
369
                            control_state == MTRANS_EXEC1  ||
370
                            control_state == MTRANS_EXEC2  ||
371
                            control_state == MTRANS_ABORT  ||
372
                            control_state == MULT_PROC1    ||
373
                            control_state == MULT_PROC2    ||
374
                            control_state == MULT_ACCUMU   ||
375
                            control_state == MULT_STORE    ||
376
                            control_state == INT_WAIT1     ||
377
                            control_state == INT_WAIT2     ||
378
                            control_state == SWAP_WRITE    ||
379
                            control_state == SWAP_WAIT1    ||
380
                            control_state == SWAP_WAIT2    ||
381
                            control_state == COPRO_WAIT     );
382
 
383
assign use_hold_instruction = conflict_r;
384
 
385
assign use_pre_fetch_instruction = control_state == PRE_FETCH_EXEC;
386
 
387
 
388
assign instruction_sel  =         use_hold_instruction           ? 2'd3 :  // hold_instruction
389
                                  use_saved_current_instruction  ? 2'd1 :  // saved_current_instruction 
390
                                  use_pre_fetch_instruction      ? 2'd2 :  // pre_fetch_instruction     
391
                                                                   2'd0 ;  // fetch_instruction_r               
392
 
393
assign instruction      =         instruction_sel == 2'd0 ? fetch_instruction_r       :
394
                                  instruction_sel == 2'd1 ? saved_current_instruction :
395
                                  instruction_sel == 2'd3 ? hold_instruction          :
396
                                                            pre_fetch_instruction     ;
397 35 csantifort
 
398
assign type             =         instruction_sel == 2'd0 ? fetch_instruction_type_r       :
399
                                  instruction_sel == 2'd1 ? saved_current_instruction_type :
400
                                  instruction_sel == 2'd3 ? hold_instruction_type          :
401
                                                            pre_fetch_instruction_type     ;
402 16 csantifort
 
403
// abort flag
404
assign instruction_iabt =         instruction_sel == 2'd0 ? iabt_reg                       :
405
                                  instruction_sel == 2'd1 ? saved_current_instruction_iabt :
406
                                  instruction_sel == 2'd3 ? hold_instruction_iabt          :
407
                                                            pre_fetch_instruction_iabt     ;
408
 
409
assign instruction_address =      instruction_sel == 2'd0 ? fetch_address_r                   :
410
                                  instruction_sel == 2'd1 ? saved_current_instruction_address :
411
                                  instruction_sel == 2'd3 ? hold_instruction_address          :
412
                                                            pre_fetch_instruction_address     ;
413
 
414
assign instruction_iabt_status =  instruction_sel == 2'd0 ? abt_status_reg                        :
415
                                  instruction_sel == 2'd1 ? saved_current_instruction_iabt_status :
416
                                  instruction_sel == 2'd3 ? hold_instruction_iabt_status          :
417
                                                            pre_fetch_instruction_iabt_status     ;
418
 
419
// instruction address exception
420
assign instruction_adex =         instruction_sel == 2'd0 ? adex_reg                       :
421
                                  instruction_sel == 2'd1 ? saved_current_instruction_adex :
422
                                  instruction_sel == 2'd3 ? hold_instruction_adex          :
423
                                                            pre_fetch_instruction_adex     ;
424
 
425
 
426
// ========================================================
427
// Fixed fields within the instruction
428
// ========================================================
429
 
430
assign opcode               = instruction[24:21];
431
assign condition_nxt        = instruction[31:28];
432
 
433
assign rm_sel_nxt           = instruction[3:0];
434
assign rn_sel_nxt           = branch ? 4'd15 : instruction[19:16]; // Use PC to calculate branch destination
435
assign rs_sel_nxt           = control_state == SWAP_WRITE  ? instruction[3:0]   : // Rm gets written out to memory
436
                              type == MTRANS               ? mtrans_reg1         :
437
                              branch                       ? 4'd15              : // Update the PC
438
                              rds_use_rs                   ? instruction[11:8]  :
439
                                                             instruction[15:12] ;
440
 
441
// Load from memory into registers
442
assign ldm_user_mode        = type == MTRANS && {instruction[22:20],instruction[15]} == 4'b1010;
443
assign ldm_flags            = type == MTRANS && rs_sel_nxt == 4'd15 && instruction[20] && instruction[22];
444
assign ldm_status_bits      = type == MTRANS && rs_sel_nxt == 4'd15 && instruction[20] && instruction[22] && i_execute_status_bits[1:0] != USR;
445
assign load_rd_byte         = (type == TRANS || type == SWAP) && instruction[22];
446
assign load_rd_nxt          = {ldm_flags, ldm_status_bits, ldm_user_mode, load_rd_byte, rs_sel_nxt};
447
 
448
 
449
                            // MSB indicates valid dirty target register
450
assign load_rd_d1_nxt       = {o_decode_daccess && !o_write_data_wen, o_load_rd[3:0]};
451
assign shift_imm            = instruction[11:7];
452
assign offset12             = { 20'h0, instruction[11:0]};
453
assign offset24             = {{6{instruction[23]}}, instruction[23:0], 2'd0 }; // sign extend
454
assign imm8                 = instruction[7:0];
455
 
456
assign immediate_shift_op   = instruction[25];
457
assign rds_use_rs           = (type == REGOP && !instruction[25] && instruction[4]) ||
458
                              (type == MULT &&
459
                               (control_state == MULT_PROC1  ||
460
                                control_state == MULT_PROC2  ||
461 60 csantifort
//                                instruction_valid && !interrupt )) ;
462
// remove the '!conflict' term from the interrupt logic used here
463
// to break a combinational loop
464
                                (instruction_valid && !interrupt_or_conflict))) ;
465
 
466
 
467 16 csantifort
assign branch               = type == BRANCH;
468
assign opcode_compare       = opcode == CMP || opcode == CMN || opcode == TEQ || opcode == TST ;
469
assign mem_op               = type == TRANS;
470
assign load_op              = mem_op && instruction[20];
471
assign store_op             = mem_op && !instruction[20];
472
assign write_pc             = (pc_wen_nxt && pc_sel_nxt != 3'd0) || load_pc_r || load_pc_nxt;
473
assign current_write_pc     = (pc_wen_nxt && pc_sel_nxt != 3'd0) || load_pc_nxt;
474
assign regop_set_flags      = type == REGOP && instruction[20];
475
 
476
assign mem_op_pre_indexed   =  instruction[24] && instruction[21];
477
assign mem_op_post_indexed  = !instruction[24];
478
 
479
assign imm32_nxt            =  // add 0 to Rm
480
                               type == MULT               ? {  32'd0                      } :
481
 
482
                               // 4 x number of registers
483
                               type == MTRANS             ? {  mtrans_base_reg_change     } :
484
                               type == BRANCH             ? {  offset24                   } :
485
                               type == TRANS              ? {  offset12                   } :
486
                               instruction[11:8] == 4'h0  ? {            24'h0, imm8[7:0] } :
487
                               instruction[11:8] == 4'h1  ? { imm8[1:0], 24'h0, imm8[7:2] } :
488
                               instruction[11:8] == 4'h2  ? { imm8[3:0], 24'h0, imm8[7:4] } :
489
                               instruction[11:8] == 4'h3  ? { imm8[5:0], 24'h0, imm8[7:6] } :
490
                               instruction[11:8] == 4'h4  ? { imm8[7:0], 24'h0            } :
491
                               instruction[11:8] == 4'h5  ? { 2'h0,  imm8[7:0], 22'h0     } :
492
                               instruction[11:8] == 4'h6  ? { 4'h0,  imm8[7:0], 20'h0     } :
493
                               instruction[11:8] == 4'h7  ? { 6'h0,  imm8[7:0], 18'h0     } :
494
                               instruction[11:8] == 4'h8  ? { 8'h0,  imm8[7:0], 16'h0     } :
495
                               instruction[11:8] == 4'h9  ? { 10'h0, imm8[7:0], 14'h0     } :
496
                               instruction[11:8] == 4'ha  ? { 12'h0, imm8[7:0], 12'h0     } :
497
                               instruction[11:8] == 4'hb  ? { 14'h0, imm8[7:0], 10'h0     } :
498
                               instruction[11:8] == 4'hc  ? { 16'h0, imm8[7:0], 8'h0      } :
499
                               instruction[11:8] == 4'hd  ? { 18'h0, imm8[7:0], 6'h0      } :
500
                               instruction[11:8] == 4'he  ? { 20'h0, imm8[7:0], 4'h0      } :
501
                                                            { 22'h0, imm8[7:0], 2'h0      } ;
502
 
503
 
504
assign imm_shift_amount_nxt = shift_imm ;
505
 
506
       // This signal is encoded in the decode stage because 
507
       // it is on the critical path in the execute stage
508
assign shift_imm_zero_nxt   = imm_shift_amount_nxt == 5'd0 &&       // immediate amount = 0
509
                              barrel_shift_amount_sel_nxt == 2'd2;  // shift immediate amount
510
 
511
assign alu_function_nxt     = { alu_swap_sel_nxt,
512
                                alu_not_sel_nxt,
513
                                alu_cin_sel_nxt,
514
                                alu_cout_sel_nxt,
515
                                alu_out_sel_nxt  };
516
 
517
// ========================================================
518
// Register Conflict Detection
519
// ========================================================
520 20 csantifort
assign rn_valid       = type == REGOP || type == MULT || type == SWAP || type == TRANS || type == MTRANS || type == CODTRANS;
521
assign rm_valid       = type == REGOP || type == MULT || type == SWAP || (type == TRANS && immediate_shift_op);
522
assign rs_valid       = rds_use_rs;
523
assign rd_valid       = (type == TRANS  && store_op) || (type == REGOP || type == SWAP);
524
assign stm_valid      = type == MTRANS && !instruction[20];   // stm instruction
525
 
526
 
527
assign rn_conflict1   = instruction_execute   && rn_valid  && ( load_rd_d1_nxt[4] && rn_sel_nxt         == load_rd_d1_nxt[3:0] );
528
assign rn_conflict2   = instruction_execute_r && rn_valid  && ( load_rd_d1    [4] && rn_sel_nxt         == load_rd_d1    [3:0] );
529
assign rm_conflict1   = instruction_execute   && rm_valid  && ( load_rd_d1_nxt[4] && rm_sel_nxt         == load_rd_d1_nxt[3:0] );
530
assign rm_conflict2   = instruction_execute_r && rm_valid  && ( load_rd_d1    [4] && rm_sel_nxt         == load_rd_d1    [3:0] );
531
assign rs_conflict1   = instruction_execute   && rs_valid  && ( load_rd_d1_nxt[4] && rs_sel_nxt         == load_rd_d1_nxt[3:0] );
532
assign rs_conflict2   = instruction_execute_r && rs_valid  && ( load_rd_d1    [4] && rs_sel_nxt         == load_rd_d1    [3:0] );
533
assign rd_conflict1   = instruction_execute   && rd_valid  && ( load_rd_d1_nxt[4] && instruction[15:12] == load_rd_d1_nxt[3:0] );
534
assign rd_conflict2   = instruction_execute_r && rd_valid  && ( load_rd_d1    [4] && instruction[15:12] == load_rd_d1    [3:0] );
535
 
536
assign stm_conflict1a = instruction_execute   && stm_valid && ( load_rd_d1_nxt[4] && mtrans_reg1        == load_rd_d1_nxt[3:0] );
537
assign stm_conflict1b = instruction_execute   && stm_valid && ( load_rd_d1_nxt[4] && mtrans_reg2        == load_rd_d1_nxt[3:0] );
538
assign stm_conflict2a = instruction_execute_r && stm_valid && ( load_rd_d1    [4] && mtrans_reg1        == load_rd_d1    [3:0] );
539
assign stm_conflict2b = instruction_execute_r && stm_valid && ( load_rd_d1    [4] && mtrans_reg2        == load_rd_d1    [3:0] );
540 16 csantifort
 
541
assign conflict1      = instruction_valid &&
542
                        (rn_conflict1 || rm_conflict1 || rs_conflict1 || rd_conflict1 ||
543
                         stm_conflict1a || stm_conflict1b);
544
 
545 20 csantifort
assign conflict2      = instruction_valid && (stm_conflict2a || stm_conflict2b);
546 16 csantifort
 
547
assign conflict       = conflict1 || conflict2;
548
 
549
 
550
always @( posedge i_clk )
551 35 csantifort
    if ( !i_core_stall )
552 16 csantifort
        begin
553 20 csantifort
        conflict_r              <= conflict;
554
        instruction_execute_r   <= instruction_execute;
555
        rn_conflict1_r          <= rn_conflict1 && instruction_execute;
556
        rm_conflict1_r          <= rm_conflict1 && instruction_execute;
557
        rs_conflict1_r          <= rs_conflict1 && instruction_execute;
558
        rd_conflict1_r          <= rd_conflict1 && instruction_execute;
559
        o_rn_use_read           <= instruction_valid && ( rn_conflict1_r || rn_conflict2 );
560
        o_rm_use_read           <= instruction_valid && ( rm_conflict1_r || rm_conflict2 );
561
        o_rs_use_read           <= instruction_valid && ( rs_conflict1_r || rs_conflict2 );
562
        o_rd_use_read           <= instruction_valid && ( rd_conflict1_r || rd_conflict2 );
563 16 csantifort
        end
564
 
565
assign o_conflict = conflict;
566
 
567
 
568
// ========================================================
569
// MTRANS Operations
570
// ========================================================
571
 
572
   // Bit 15 = r15
573
   // Bit 0  = r0
574
   // In ldm and stm instructions r0 is loaded or stored first 
575
always @*
576
    casez ( instruction[15:0] )
577
    16'b???????????????1 : mtrans_reg1 = 4'h0 ;
578
    16'b??????????????10 : mtrans_reg1 = 4'h1 ;
579
    16'b?????????????100 : mtrans_reg1 = 4'h2 ;
580
    16'b????????????1000 : mtrans_reg1 = 4'h3 ;
581
    16'b???????????10000 : mtrans_reg1 = 4'h4 ;
582
    16'b??????????100000 : mtrans_reg1 = 4'h5 ;
583
    16'b?????????1000000 : mtrans_reg1 = 4'h6 ;
584
    16'b????????10000000 : mtrans_reg1 = 4'h7 ;
585
    16'b???????100000000 : mtrans_reg1 = 4'h8 ;
586
    16'b??????1000000000 : mtrans_reg1 = 4'h9 ;
587
    16'b?????10000000000 : mtrans_reg1 = 4'ha ;
588
    16'b????100000000000 : mtrans_reg1 = 4'hb ;
589
    16'b???1000000000000 : mtrans_reg1 = 4'hc ;
590
    16'b??10000000000000 : mtrans_reg1 = 4'hd ;
591
    16'b?100000000000000 : mtrans_reg1 = 4'he ;
592
    default              : mtrans_reg1 = 4'hf ;
593
    endcase
594
 
595
 
596
assign mtrans_reg2_mask = 1'd1<<mtrans_reg1;
597
 
598
always @*
599
    casez ( instruction[15:0] & ~mtrans_reg2_mask )
600
    16'b???????????????1 : mtrans_reg2 = 4'h0 ;
601
    16'b??????????????10 : mtrans_reg2 = 4'h1 ;
602
    16'b?????????????100 : mtrans_reg2 = 4'h2 ;
603
    16'b????????????1000 : mtrans_reg2 = 4'h3 ;
604
    16'b???????????10000 : mtrans_reg2 = 4'h4 ;
605
    16'b??????????100000 : mtrans_reg2 = 4'h5 ;
606
    16'b?????????1000000 : mtrans_reg2 = 4'h6 ;
607
    16'b????????10000000 : mtrans_reg2 = 4'h7 ;
608
    16'b???????100000000 : mtrans_reg2 = 4'h8 ;
609
    16'b??????1000000000 : mtrans_reg2 = 4'h9 ;
610
    16'b?????10000000000 : mtrans_reg2 = 4'ha ;
611
    16'b????100000000000 : mtrans_reg2 = 4'hb ;
612
    16'b???1000000000000 : mtrans_reg2 = 4'hc ;
613
    16'b??10000000000000 : mtrans_reg2 = 4'hd ;
614
    16'b?100000000000000 : mtrans_reg2 = 4'he ;
615
    default              : mtrans_reg2 = 4'hf ;
616
    endcase
617
 
618
always @*
619
    casez (instruction[15:0])
620
    16'b???????????????1 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 1],  1'd0};
621
    16'b??????????????10 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 2],  2'd0};
622
    16'b?????????????100 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 3],  3'd0};
623
    16'b????????????1000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 4],  4'd0};
624
    16'b???????????10000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 5],  5'd0};
625
    16'b??????????100000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 6],  6'd0};
626
    16'b?????????1000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 7],  7'd0};
627
    16'b????????10000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 8],  8'd0};
628
    16'b???????100000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15: 9],  9'd0};
629
    16'b??????1000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:10], 10'd0};
630
    16'b?????10000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:11], 11'd0};
631
    16'b????100000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:12], 12'd0};
632
    16'b???1000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:13], 13'd0};
633
    16'b??10000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15:14], 14'd0};
634
    16'b?100000000000000 : mtrans_instruction_nxt = {instruction[31:16], instruction[15   ], 15'd0};
635
    default              : mtrans_instruction_nxt = {instruction[31:16],                     16'd0};
636
    endcase
637
 
638
 
639
// number of registers to be stored
640
assign mtrans_num_registers =   {4'd0, instruction[15]} +
641
                                {4'd0, instruction[14]} +
642
                                {4'd0, instruction[13]} +
643
                                {4'd0, instruction[12]} +
644
                                {4'd0, instruction[11]} +
645
                                {4'd0, instruction[10]} +
646
                                {4'd0, instruction[ 9]} +
647
                                {4'd0, instruction[ 8]} +
648
                                {4'd0, instruction[ 7]} +
649
                                {4'd0, instruction[ 6]} +
650
                                {4'd0, instruction[ 5]} +
651
                                {4'd0, instruction[ 4]} +
652
                                {4'd0, instruction[ 3]} +
653
                                {4'd0, instruction[ 2]} +
654
                                {4'd0, instruction[ 1]} +
655
                                {4'd0, instruction[ 0]} ;
656
 
657
// 4 x number of registers to be stored
658
assign mtrans_base_reg_change = {25'd0, mtrans_num_registers, 2'd0};
659
 
660
// ========================================================
661
// Interrupts
662
// ========================================================
663
 
664
assign firq_request = firq && !i_execute_status_bits[26];
665
assign irq_request  = irq  && !i_execute_status_bits[27];
666
assign swi_request  = type == SWI;
667
assign dabt_request = dabt_reg;
668
 
669
// copro15 and copro13 only supports reg trans opcodes
670
// all other opcodes involving co-processors cause an 
671
// undefined instrution interrupt
672
assign und_request  =   type == CODTRANS ||
673
                        type == COREGOP  ||
674
                      ( type == CORTRANS && instruction[11:8] != 4'd15 );
675
 
676
 
677
  // in order of priority !!                 
678
  // Highest 
679
  // 1 Reset
680
  // 2 Data Abort (including data TLB miss)
681
  // 3 FIRQ
682
  // 4 IRQ
683
  // 5 Prefetch Abort (including prefetch TLB miss)
684
  // 6 Undefined instruction, SWI
685
  // Lowest                        
686
assign next_interrupt = dabt_request     ? 3'd1 :  // Data Abort
687
                        firq_request     ? 3'd2 :  // FIRQ
688
                        irq_request      ? 3'd3 :  // IRQ
689
                        instruction_adex ? 3'd4 :  // Address Exception 
690
                        instruction_iabt ? 3'd5 :  // PreFetch Abort, only triggered 
691
                                                   // if the instruction is used
692
                        und_request      ? 3'd6 :  // Undefined Instruction
693
                        swi_request      ? 3'd7 :  // SWI
694
                                           3'd0 ;  // none             
695
 
696 60 csantifort
 
697
// SWI and undefined instructions do not cause an interrupt in the decode
698
// stage. They only trigger interrupts if they arfe executed, so the
699
// interrupt is triggered if the execute condition is met in the execute stage
700 16 csantifort
assign interrupt      = next_interrupt != 3'd0 &&
701
                        next_interrupt != 3'd7 &&  // SWI
702 17 csantifort
                        next_interrupt != 3'd6 &&  // undefined interrupt
703
                        !conflict               ;  // Wait for conflicts to resolve before
704
                                                   // triggering int
705 16 csantifort
 
706 60 csantifort
 
707
// Added to use in rds_use_rs logic to break a combinational loop invloving
708
// the conflict signal
709
assign interrupt_or_conflict
710
                     =  next_interrupt != 3'd0 &&
711
                        next_interrupt != 3'd7 &&  // SWI
712
                        next_interrupt != 3'd6  ;  // undefined interrupt
713
 
714 16 csantifort
assign interrupt_mode = next_interrupt == 3'd2 ? FIRQ :
715
                        next_interrupt == 3'd3 ? IRQ  :
716
                        next_interrupt == 3'd4 ? SVC  :
717
                        next_interrupt == 3'd5 ? SVC  :
718
                        next_interrupt == 3'd6 ? SVC  :
719
                        next_interrupt == 3'd7 ? SVC  :
720
                        next_interrupt == 3'd1 ? SVC  :
721
                                                 USR  ;
722
 
723
 
724
// ========================================================
725
// Generate control signals
726
// ========================================================
727
always @*
728
    begin
729
    // default mode
730
    status_bits_mode_nxt            = i_execute_status_bits[1:0];   // change to mode in execute stage get reflected
731
                                                                    // back to this stage automatically
732
    status_bits_irq_mask_nxt        = o_status_bits_irq_mask;
733
    status_bits_firq_mask_nxt       = o_status_bits_firq_mask;
734
    decode_exclusive_nxt            = 1'd0;
735
    decode_daccess_nxt              = 1'd0;
736
    decode_iaccess_nxt              = 1'd1;
737
    copro_operation_nxt             = 'd0;
738
 
739
    // Save an instruction to use later
740
    saved_current_instruction_wen   = 1'd0;
741
    pre_fetch_instruction_wen       = 1'd0;
742
    restore_base_address_nxt        = restore_base_address;
743
 
744
    // default Mux Select values
745
    barrel_shift_amount_sel_nxt     = 'd0;  // don't shift the input
746
    barrel_shift_data_sel_nxt       = 'd0;  // immediate value
747
    barrel_shift_function_nxt       = 'd0;
748
    multiply_function_nxt           = 'd0;
749
    iaddress_sel_nxt                = 'd0;
750
    daddress_sel_nxt                = 'd0;
751
    pc_sel_nxt                      = 'd0;
752
    load_pc_nxt                     = 'd0;
753
    byte_enable_sel_nxt             = 'd0;
754
    status_bits_sel_nxt             = 'd0;
755
    reg_write_sel_nxt               = 'd0;
756
    o_user_mode_regs_store_nxt      = 'd0;
757
 
758
    // ALU Muxes
759
    alu_swap_sel_nxt                = 'd0;
760
    alu_not_sel_nxt                 = 'd0;
761
    alu_cin_sel_nxt                 = 'd0;
762
    alu_cout_sel_nxt                = 'd0;
763
    alu_out_sel_nxt                 = 'd0;
764
 
765
    // default Flop Write Enable values
766
    write_data_wen_nxt              = 'd0;
767
    copro_write_data_wen_nxt        = 'd0;
768
    base_address_wen_nxt            = 'd0;
769
    pc_wen_nxt                      = 'd1;
770
    reg_bank_wen_nxt                = 'd0;  // Don't select any
771
 
772
    status_bits_flags_wen_nxt       = 'd0;
773
    status_bits_mode_wen_nxt        = 'd0;
774
    status_bits_irq_mask_wen_nxt    = 'd0;
775
    status_bits_firq_mask_wen_nxt   = 'd0;
776
 
777
    if ( instruction_valid && !interrupt && !conflict )
778
        begin
779
        if ( type == REGOP )
780
            begin
781
            if ( !opcode_compare )
782
                begin
783
                // Check is the load destination is the PC
784
                if (instruction[15:12]  == 4'd15)
785
                    begin
786
                    pc_sel_nxt       = 3'd1; // alu_out
787
                    iaddress_sel_nxt = 4'd1; // alu_out
788
                    end
789
                else
790
                    reg_bank_wen_nxt = decode (instruction[15:12]);
791
                end
792 35 csantifort
 
793 16 csantifort
            if ( !immediate_shift_op )
794 35 csantifort
                begin
795 16 csantifort
                barrel_shift_function_nxt  = instruction[6:5];
796 35 csantifort
                end
797 16 csantifort
 
798
            if ( !immediate_shift_op )
799
                barrel_shift_data_sel_nxt = 2'd2; // Shift value from Rm register
800
 
801
            if ( !immediate_shift_op && instruction[4] )
802
                barrel_shift_amount_sel_nxt = 2'd1; // Shift amount from Rs registter
803
 
804
            if ( !immediate_shift_op && !instruction[4] )
805
                barrel_shift_amount_sel_nxt = 2'd2; // Shift immediate amount 
806
 
807
            if ( opcode == ADD || opcode == CMN )   // CMN is just like an ADD
808
                begin
809
                alu_out_sel_nxt  = 4'd1; // Add
810
                end
811
 
812
            if ( opcode == ADC ) // Add with Carry
813
                begin
814
                alu_out_sel_nxt  = 4'd1; // Add
815
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
816
                end
817
 
818
            if ( opcode == SUB || opcode == CMP ) // Subtract
819
                begin
820
                alu_out_sel_nxt  = 4'd1; // Add
821
                alu_cin_sel_nxt  = 2'd1; // cin = 1
822
                alu_not_sel_nxt  = 1'd1; // invert B
823
                end
824
 
825
            // SBC (Subtract with Carry) subtracts the value of its 
826
            // second operand and the value of NOT(Carry flag) from
827
            // the value of its first operand.
828
            //  Rd = Rn - shifter_operand - NOT(C Flag)
829
            if ( opcode == SBC ) // Subtract with Carry
830
                begin
831
                alu_out_sel_nxt  = 4'd1; // Add
832
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
833
                alu_not_sel_nxt  = 1'd1; // invert B
834
                end
835
 
836
            if ( opcode == RSB ) // Reverse Subtract
837
                begin
838
                alu_out_sel_nxt  = 4'd1; // Add
839
                alu_cin_sel_nxt  = 2'd1; // cin = 1
840
                alu_not_sel_nxt  = 1'd1; // invert B
841
                alu_swap_sel_nxt = 1'd1; // swap A and B
842
                end
843
 
844
            if ( opcode == RSC ) // Reverse Subtract with carry
845
                begin
846
                alu_out_sel_nxt  = 4'd1; // Add
847
                alu_cin_sel_nxt  = 2'd2; // carry in from status_bits
848
                alu_not_sel_nxt  = 1'd1; // invert B
849
                alu_swap_sel_nxt = 1'd1; // swap A and B
850
                end
851
 
852
            if ( opcode == AND || opcode == TST ) // Logical AND, Test  (using AND operator)
853
                begin
854
                alu_out_sel_nxt  = 4'd8;  // AND
855
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
856
                end
857
 
858
            if ( opcode == EOR || opcode == TEQ ) // Logical Exclusive OR, Test Equivalence (using EOR operator)
859
                begin
860
                alu_out_sel_nxt = 4'd6;  // XOR
861
                alu_cout_sel_nxt = 1'd1; // i_barrel_shift_carry
862
                end
863
 
864
            if ( opcode == ORR )
865
                begin
866
                alu_out_sel_nxt  = 4'd7; // OR
867
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
868
                end
869
 
870
            if ( opcode == BIC ) // Bit Clear (using AND & NOT operators)
871
                begin
872
                alu_out_sel_nxt  = 4'd8;  // AND
873
                alu_not_sel_nxt  = 1'd1;  // invert B
874
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
875
                end
876
 
877
            if ( opcode == MOV ) // Move
878
                begin
879
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
880
                end
881
 
882
            if ( opcode == MVN ) // Move NOT
883
                begin
884
                alu_not_sel_nxt  = 1'd1; // invert B
885
                alu_cout_sel_nxt = 1'd1;  // i_barrel_shift_carry
886
                end
887
            end
888
 
889
        // Load & Store instructions
890
        if ( mem_op )
891
            begin
892
            if ( load_op && instruction[15:12]  == 4'd15 ) // Write to PC
893
                begin
894
                saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
895
                pc_wen_nxt                      = 1'd0; // hold current PC value rather than an instruction fetch
896
                load_pc_nxt                     = 1'd1;
897
                end
898
 
899
            decode_daccess_nxt              = 1'd1; // indicate a valid data access
900
            alu_out_sel_nxt                 = 4'd1; // Add
901
 
902
            if ( !instruction[23] )  // U: Subtract offset
903
                begin
904
                alu_cin_sel_nxt  = 2'd1; // cin = 1
905
                alu_not_sel_nxt  = 1'd1; // invert B
906
                end
907
 
908
            if ( store_op )
909
                begin
910
                write_data_wen_nxt = 1'd1;
911
                if ( type == TRANS && instruction[22] )
912
                    byte_enable_sel_nxt = 2'd1;         // Save byte
913
                end
914
 
915
                // need to update the register holding the address ?
916
                // This is Rn bits [19:16]
917
            if ( mem_op_pre_indexed || mem_op_post_indexed )
918
                begin
919
                // Check is the load destination is the PC
920
                if ( rn_sel_nxt  == 4'd15 )
921
                    pc_sel_nxt = 3'd1;
922
                else
923
                    reg_bank_wen_nxt = decode ( rn_sel_nxt );
924
                end
925
 
926
                // if post-indexed, then use Rn rather than ALU output, as address
927
            if ( mem_op_post_indexed )
928
               daddress_sel_nxt = 4'd4; // Rn
929
            else
930
               daddress_sel_nxt = 4'd1; // alu out
931
 
932
            if ( instruction[25] && type ==  TRANS )
933
                barrel_shift_data_sel_nxt = 2'd2; // Shift value from Rm register
934
 
935
            if ( type == TRANS && instruction[25] && shift_imm != 5'd0 )
936
                begin
937
                barrel_shift_function_nxt   = instruction[6:5];
938
                barrel_shift_amount_sel_nxt = 2'd2; // imm_shift_amount
939
                end
940
            end
941
 
942
 
943
        if ( type == BRANCH )
944
            begin
945 35 csantifort
            pc_sel_nxt            = 3'd1; // alu_out
946
            iaddress_sel_nxt      = 4'd1; // alu_out
947
            alu_out_sel_nxt       = 4'd1; // Add
948 16 csantifort
 
949
            if ( instruction[24] ) // Link
950
                begin
951
                reg_bank_wen_nxt  = decode (4'd14);  // Save PC to LR
952
                reg_write_sel_nxt = 3'd1;            // pc - 32'd4
953
                end
954
            end
955
 
956
 
957
        if ( type == MTRANS )
958
            begin
959
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
960
            decode_daccess_nxt              = 1'd1; // valid data access
961
            alu_out_sel_nxt                 = 4'd1; // Add
962
            base_address_wen_nxt            = 1'd1; // Save the value of the register used for the base address,
963
                                                    // in case of a data abort, and need to restore the value                        
964
 
965
            if ( mtrans_num_registers > 4'd1 )
966
                begin
967
                iaddress_sel_nxt        = 4'd3; // pc  (not pc + 4)
968
                pc_wen_nxt              = 1'd0; // hold current PC value rather than an instruction fetch
969
                end
970
 
971
 
972
            // The spec says -
973
            // If the instruction would have overwritten the base with data 
974
            // (that is, it has the base in the transfer list), the overwriting is prevented.
975
            // This is true even when the abort occurs after the base word gets loaded
976
            restore_base_address_nxt        = instruction[20] &&
977
                                                (instruction[15:0] & (1'd1 << instruction[19:16]));
978
 
979
            // Increment
980
            if ( instruction[23] )
981
                begin
982
                if ( instruction[24] )    // increment before
983
                    daddress_sel_nxt = 4'd7; // Rn + 4
984
                else
985
                    daddress_sel_nxt = 4'd4; // Rn
986
                end
987
            else
988
            // Decrement
989
                begin
990
                alu_cin_sel_nxt  = 2'd1; // cin = 1
991
                alu_not_sel_nxt  = 1'd1; // invert B
992
                if ( !instruction[24] )    // decrement after
993
                    daddress_sel_nxt  = 4'd6; // alu out + 4
994
                else
995
                    daddress_sel_nxt  = 4'd1; // alu out
996
                end
997
 
998
            // Load or store ?
999
            if ( !instruction[20] )  // Store
1000
                write_data_wen_nxt = 1'd1;
1001
 
1002
            // stm: store the user mode registers, when in priviledged mode     
1003
            if ( {instruction[22:20]} == 3'b100 )
1004
                o_user_mode_regs_store_nxt = 1'd1;
1005
 
1006
            // update the base register ?
1007
            if ( instruction[21] )  // the W bit
1008
                reg_bank_wen_nxt  = decode (rn_sel_nxt);
1009
 
1010
            // write to the pc ?
1011
            if ( instruction[20] && mtrans_reg1 == 4'd15 ) // Write to PC
1012
                begin
1013
                saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
1014
                pc_wen_nxt                      = 1'd0; // hold current PC value rather than an instruction fetch
1015
                load_pc_nxt                     = 1'd1;
1016
                end
1017
            end
1018
 
1019
 
1020
        if ( type == MULT )
1021
            begin
1022
            multiply_function_nxt[0]        = 1'd1; // set enable
1023
                                                    // some bits can be changed just below
1024
            saved_current_instruction_wen   = 1'd1; // Save the Multiply instruction to 
1025
                                                    // refer back to later
1026
            pc_wen_nxt                      = 1'd0; // hold current PC value
1027
 
1028
            if ( instruction[21] )
1029
                multiply_function_nxt[1]    = 1'd1; // accumulate
1030
            end
1031
 
1032
 
1033
        // swp - do read part first
1034
        if ( type == SWAP )
1035
            begin
1036
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
1037
            pc_wen_nxt                      = 1'd0; // hold current PC value
1038
            decode_iaccess_nxt              = 1'd0; // skip the instruction fetch
1039
            decode_daccess_nxt              = 1'd1; // data access
1040
            barrel_shift_data_sel_nxt       = 2'd2; // Shift value from Rm register
1041
            daddress_sel_nxt                = 4'd4; // Rn
1042
            decode_exclusive_nxt            = 1'd1; // signal an exclusive access
1043
            end
1044
 
1045
 
1046
        // mcr & mrc - takes two cycles
1047
        if ( type == CORTRANS && !und_request )
1048
            begin
1049
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
1050
            pc_wen_nxt                      = 1'd0; // hold current PC value
1051
            iaddress_sel_nxt                = 4'd3; // pc  (not pc + 4)
1052
 
1053
            if ( instruction[20] ) // MRC
1054
                copro_operation_nxt         = 2'd1;  // Register transfer from Co-Processor
1055
            else // MCR
1056
                begin
1057
                 // Don't enable operation to Co-Processor until next period
1058
                 // So it gets the Rd value from the execution stage at the same time
1059
                copro_operation_nxt      = 2'd0;
1060
                copro_write_data_wen_nxt = 1'd1;  // Rd register value to co-processor
1061
                end
1062
            end
1063
 
1064
 
1065
        if ( type == SWI || und_request )
1066
            begin
1067
            // save address of next instruction to Supervisor Mode LR
1068
            reg_write_sel_nxt               = 3'd1;            // pc -4
1069
            reg_bank_wen_nxt                = decode (4'd14);  // LR
1070
 
1071
            iaddress_sel_nxt                = 4'd2;            // interrupt_vector
1072
            pc_sel_nxt                      = 3'd2;            // interrupt_vector
1073
 
1074
            status_bits_mode_nxt            = interrupt_mode;  // e.g. Supervisor mode
1075
            status_bits_mode_wen_nxt        = 1'd1;
1076
 
1077
            // disable normal interrupts
1078
            status_bits_irq_mask_nxt        = 1'd1;
1079
            status_bits_irq_mask_wen_nxt    = 1'd1;
1080
            end
1081
 
1082
 
1083
        if ( regop_set_flags )
1084
            begin
1085
            status_bits_flags_wen_nxt = 1'd1;
1086
 
1087
            // If <Rd> is r15, the ALU output is copied to the Status Bits. 
1088
            // Not allowed to use r15 for mul or lma instructions           
1089
            if ( instruction[15:12] == 4'd15 )
1090
                begin
1091
                status_bits_sel_nxt       = 3'd1; // alu out
1092
 
1093
                // Priviledged mode? Then also update the other status bits
1094
                if ( i_execute_status_bits[1:0] != USR )
1095
                    begin
1096
                    status_bits_mode_wen_nxt      = 1'd1;
1097
                    status_bits_irq_mask_wen_nxt  = 1'd1;
1098
                    status_bits_firq_mask_wen_nxt = 1'd1;
1099
                    end
1100
                end
1101
            end
1102
 
1103
        end
1104
 
1105
    // Handle asynchronous interrupts.
1106
    // interrupts are processed only during execution states
1107
    // multicycle instructions must complete before the interrupt starts
1108
    // SWI, Address Exception and Undefined Instruction interrupts are only executed if the
1109
    // instruction that causes the interrupt is conditionally executed so
1110
    // its not handled here
1111
    if ( instruction_valid && interrupt &&  next_interrupt != 3'd6 )
1112
        begin
1113
        // Save the interrupt causing instruction to refer back to later
1114
        // This also saves the instruction abort vma and status, in the case of an
1115
        // instruction abort interrupt
1116
        saved_current_instruction_wen   = 1'd1;
1117
 
1118
        // save address of next instruction to Supervisor Mode LR
1119
        // Address Exception ?
1120
        if ( next_interrupt == 3'd4 )
1121
            reg_write_sel_nxt               = 3'd7;            // pc
1122
        else
1123
            reg_write_sel_nxt               = 3'd1;            // pc -4
1124
 
1125
        reg_bank_wen_nxt                = decode (4'd14);  // LR
1126
 
1127
        iaddress_sel_nxt                = 4'd2;            // interrupt_vector
1128
        pc_sel_nxt                      = 3'd2;            // interrupt_vector
1129
 
1130
        status_bits_mode_nxt            = interrupt_mode;  // e.g. Supervisor mode
1131
        status_bits_mode_wen_nxt        = 1'd1;
1132
 
1133
        // disable normal interrupts
1134
        status_bits_irq_mask_nxt        = 1'd1;
1135
        status_bits_irq_mask_wen_nxt    = 1'd1;
1136
 
1137
        // disable fast interrupts
1138
        if ( next_interrupt == 3'd2 ) // FIRQ
1139
            begin
1140
            status_bits_firq_mask_nxt        = 1'd1;
1141
            status_bits_firq_mask_wen_nxt    = 1'd1;
1142
            end
1143
        end
1144
 
1145
 
1146
    // previous instruction was ldr
1147
    // if it is currently executing in the execute stage do the following    
1148
    if ( control_state == MEM_WAIT1 && !conflict )
1149
        begin
1150
        // Save the next instruction to execute later
1151
        // Do this even if the ldr instruction does not execute because of Condition
1152
        pre_fetch_instruction_wen   = 1'd1;
1153
 
1154
        if ( instruction_execute ) // conditional execution state
1155
            begin
1156
            iaddress_sel_nxt            = 4'd3; // pc  (not pc + 4)
1157
            pc_wen_nxt                  = 1'd0; // hold current PC value
1158
            load_pc_nxt                 = load_pc_r;
1159
            end
1160
        end
1161
 
1162
 
1163
    // completion of ldr instruction
1164
    if ( control_state == MEM_WAIT2 )
1165
        begin
1166
        if ( !dabt )  // dont load data there is an abort on the data read
1167
            begin
1168
            pc_wen_nxt                  = 1'd0; // hold current PC value
1169
 
1170
            // Check if the load destination is the PC
1171
            if (( type == TRANS && instruction[15:12]  == 4'd15 ) ||
1172
                ( type == MTRANS && instruction[20] && mtrans_reg1 == 4'd15 ))
1173
                begin
1174
                pc_sel_nxt       = 3'd3; // read_data_filtered
1175
                iaddress_sel_nxt = 4'd3; // hold value after reading in from mem
1176
                load_pc_nxt      = load_pc_r;
1177
                end
1178
            end
1179
        end
1180
 
1181
 
1182
    // second cycle of multiple load or store
1183
    if ( control_state == MTRANS_EXEC1 && !conflict )
1184
        begin
1185
        // Save the next instruction to execute later
1186
        pre_fetch_instruction_wen   = 1'd1;
1187
 
1188
        if ( instruction_execute ) // conditional execution state
1189
            begin
1190
            daddress_sel_nxt            = 4'd5;  // o_address
1191
            decode_daccess_nxt          = 1'd1;  // data access
1192
 
1193
            if ( mtrans_num_registers > 4'd2 )
1194
                decode_iaccess_nxt      = 1'd0;  // skip the instruction fetch
1195
 
1196
 
1197
            if ( mtrans_num_registers != 4'd1 )
1198
                begin
1199
                pc_wen_nxt              = 1'd0;  // hold current PC value
1200
                iaddress_sel_nxt        = 4'd3;  // pc  (not pc + 4)
1201
                end
1202
 
1203
 
1204
            if ( !instruction[20] ) // Store
1205
                write_data_wen_nxt = 1'd1;
1206
 
1207
            // stm: store the user mode registers, when in priviledged mode     
1208
            if ( {instruction[22:20]} == 3'b100 )
1209
                o_user_mode_regs_store_nxt = 1'd1;
1210
 
1211
            // write to the pc ?
1212
            if ( instruction[20] && mtrans_reg1 == 4'd15 ) // Write to PC
1213
                begin
1214
                saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
1215
                pc_wen_nxt                      = 1'd0; // hold current PC value rather than an instruction fetch
1216
                load_pc_nxt                     = 1'd1;
1217
                end
1218
            end
1219
        end
1220
 
1221
 
1222
    // third cycle of multiple load or store
1223
    if ( control_state == MTRANS_EXEC2 )
1224
        begin
1225
        daddress_sel_nxt            = 4'd5;  // o_address
1226
        decode_daccess_nxt          = 1'd1;  // data access
1227
 
1228
        if ( mtrans_num_registers > 4'd2 )
1229
            begin
1230
            decode_iaccess_nxt      = 1'd0;  // skip the instruction fetch
1231
            end
1232
 
1233
        if ( mtrans_num_registers > 4'd1 )
1234
            begin
1235
            pc_wen_nxt              = 1'd0; // hold current PC value
1236
            iaddress_sel_nxt        = 4'd3;  // pc  (not pc + 4)
1237
            end
1238
 
1239
        // Store
1240
        if ( !instruction[20] )
1241
            write_data_wen_nxt = 1'd1;
1242
 
1243
        // stm: store the user mode registers, when in priviledged mode     
1244
        if ( {instruction[22:20]} == 3'b100 )
1245
            o_user_mode_regs_store_nxt = 1'd1;
1246
 
1247
        // write to the pc ?
1248
        if ( instruction[20] && mtrans_reg1 == 4'd15 ) // Write to PC
1249
            begin
1250
            saved_current_instruction_wen   = 1'd1; // Save the memory access instruction to refer back to later
1251
            pc_wen_nxt                      = 1'd0; // hold current PC value rather than an instruction fetch
1252
            load_pc_nxt                     = 1'd1;
1253
            end
1254
        end
1255
 
1256
 
1257
    // state is for when a data abort interrupt is triggered during an ldm
1258
    if ( control_state == MTRANS_ABORT )
1259
        begin
1260
        // Restore the Base Address, if the base register is included in the
1261
        // list of registers being loaded
1262
        if (restore_base_address) // ldm with base address in register list
1263
            begin
1264
            reg_write_sel_nxt = 3'd6;                        // write base_register
1265
            reg_bank_wen_nxt  = decode ( instruction[19:16] ); // to Rn
1266
            end
1267
        end
1268
 
1269
 
1270
        // Multiply or Multiply-Accumulate
1271
    if ( control_state == MULT_PROC1 && instruction_execute && !conflict )
1272
        begin
1273
        // Save the next instruction to execute later
1274
        // Do this even if this instruction does not execute because of Condition
1275
        pre_fetch_instruction_wen   = 1'd1;
1276
        pc_wen_nxt                  = 1'd0;  // hold current PC value
1277
        multiply_function_nxt       = o_multiply_function;
1278
        end
1279
 
1280
 
1281
        // Multiply or Multiply-Accumulate
1282
        // Do multiplication
1283
        // Wait for done or accumulate signal
1284
    if ( control_state == MULT_PROC2 )
1285
        begin
1286
        // Save the next instruction to execute later
1287
        // Do this even if this instruction does not execute because of Condition
1288
        pc_wen_nxt              = 1'd0;  // hold current PC value
1289
        iaddress_sel_nxt        = 4'd3;  // pc  (not pc + 4)
1290
        multiply_function_nxt   = o_multiply_function;
1291
        end
1292
 
1293
 
1294
    // Save RdLo
1295
    // always last cycle of all multiply or multiply accumulate operations
1296
    if ( control_state == MULT_STORE )
1297
        begin
1298
        reg_write_sel_nxt     = 3'd2; // multiply_out
1299
        multiply_function_nxt = o_multiply_function;
1300
 
1301
        if ( type == MULT ) // 32-bit
1302
            reg_bank_wen_nxt      = decode (instruction[19:16]); // Rd
1303
        else  // 64-bit / Long
1304
            reg_bank_wen_nxt      = decode (instruction[15:12]); // RdLo
1305
 
1306
        if ( instruction[20] )  // the 'S' bit
1307
            begin
1308
            status_bits_sel_nxt       = 3'd4; // { multiply_flags, status_bits_flags[1:0] } 
1309
            status_bits_flags_wen_nxt = 1'd1;
1310
            end
1311
        end
1312
 
1313
 
1314
    // Add lower 32 bits to multiplication product
1315
    if ( control_state == MULT_ACCUMU )
1316
        begin
1317
        multiply_function_nxt = o_multiply_function;
1318
        pc_wen_nxt            = 1'd0;  // hold current PC value
1319
        iaddress_sel_nxt      = 4'd3;  // pc  (not pc + 4)
1320
        end
1321
 
1322
 
1323
    // swp - do write request in 2nd cycle
1324
    if ( control_state == SWAP_WRITE && instruction_execute && !conflict )
1325
        begin
1326
        barrel_shift_data_sel_nxt       = 2'd2; // Shift value from Rm register
1327
        daddress_sel_nxt                = 4'd4; // Rn
1328
        write_data_wen_nxt              = 1'd1;
1329
        decode_iaccess_nxt              = 1'd0; // skip the instruction fetch
1330
        decode_daccess_nxt              = 1'd1; // data access
1331
 
1332
        if ( instruction[22] )
1333
            byte_enable_sel_nxt = 2'd1;         // Save byte
1334
 
1335
        if ( instruction_execute )              // conditional execution state
1336
            pc_wen_nxt                  = 1'd0; // hold current PC value
1337
 
1338
        // Save the next instruction to execute later
1339
        // Do this even if this instruction does not execute because of Condition
1340
        pre_fetch_instruction_wen       = 1'd1;
1341
 
1342
        load_pc_nxt                     = load_pc_r;
1343
        end
1344
 
1345
 
1346
    // swp - receive read response in 3rd cycle
1347
    if ( control_state == SWAP_WAIT1 )
1348
        begin
1349
 
1350
        if ( instruction_execute ) // conditional execution state
1351
            begin
1352
            iaddress_sel_nxt            = 4'd3; // pc  (not pc + 4)
1353
            pc_wen_nxt                  = 1'd0; // hold current PC value
1354
            end
1355
 
1356
        if ( !dabt )
1357
            begin
1358
            // Check is the load destination is the PC
1359
            if ( instruction[15:12]  == 4'd15 )
1360
                begin
1361
                pc_sel_nxt       = 3'd3; // read_data_filtered
1362
                iaddress_sel_nxt = 4'd3; // hold value after reading in from mem
1363
                load_pc_nxt      = load_pc_r;
1364
                end
1365
            end
1366
        end
1367
 
1368
 
1369
    // 1 cycle delay for Co-Processor Register access
1370
    if ( control_state == COPRO_WAIT && instruction_execute && !conflict )
1371
        begin
1372
        pre_fetch_instruction_wen = 1'd1;
1373
 
1374
        if ( instruction[20] ) // mrc instruction
1375
            begin
1376
            // Check is the load destination is the PC
1377
            if ( instruction[15:12]  == 4'd15 )
1378
                begin
1379
                // If r15 is specified for <Rd>, the condition code flags are 
1380
                // updated instead of a general-purpose register.
1381
                status_bits_sel_nxt           = 3'd3;  // i_copro_data
1382
                status_bits_flags_wen_nxt     = 1'd1;
1383
 
1384
                // Can't change these in USR mode
1385
                if ( i_execute_status_bits[1:0] != USR )
1386
                   begin
1387
                   status_bits_mode_wen_nxt      = 1'd1;
1388
                   status_bits_irq_mask_wen_nxt  = 1'd1;
1389
                   status_bits_firq_mask_wen_nxt = 1'd1;
1390
                   end
1391
                end
1392
            else
1393
                reg_bank_wen_nxt = decode (instruction[15:12]);
1394
 
1395
            reg_write_sel_nxt = 3'd5;     // i_copro_data
1396
            end
1397
        else // mcr instruction
1398
            begin
1399
            copro_operation_nxt      = 2'd2;  // Register transfer to Co-Processor 
1400
            end
1401
        end
1402
 
1403
 
1404
    // Have just changed the status_bits mode but this
1405
    // creates a 1 cycle gap with the old mode
1406
    // coming back from execute into instruction_decode
1407
    // So squash that old mode value during this
1408
    // cycle of the interrupt transition    
1409
    if ( control_state == INT_WAIT1 )
1410
        status_bits_mode_nxt            = o_status_bits_mode;   // Supervisor mode
1411
 
1412
    end
1413
 
1414
 
1415
// Speed up the long path from u_decode/fetch_instruction_r to u_register_bank/r8_firq
1416
// This pre-encodes the firq_s3 signal thats used in u_register_bank
1417
// assign firq_not_user_mode_nxt = !user_mode_regs_load_nxt && status_bits_mode_nxt == FIRQ;
1418
assign firq_not_user_mode_nxt = status_bits_mode_nxt == FIRQ;
1419
 
1420
 
1421
// ========================================================
1422
// Next State Logic
1423
// ========================================================
1424
 
1425
// this replicates the current value of the execute signal in the execute stage
1426
assign instruction_execute = conditional_execute ( o_condition, i_execute_status_bits[31:28] );
1427
 
1428
 
1429
// First state of executing a new instruction
1430
// Its complex because of conditional execution of multi-cycle instructions
1431
assign instruction_valid = ((control_state == EXECUTE || control_state == PRE_FETCH_EXEC) ||
1432
                              // when last instruction was multi-cycle instruction but did not execute
1433
                              // because condition was false then act like you're in the execute state
1434
                             (!instruction_execute && (control_state == PC_STALL1    ||
1435
                                                       control_state == MEM_WAIT1    ||
1436
                                                       control_state == COPRO_WAIT   ||
1437
                                                       control_state == SWAP_WRITE   ||
1438
                                                       control_state == MULT_PROC1   ||
1439
                                                       control_state == MTRANS_EXEC1  ) ));
1440
 
1441
 
1442
 always @*
1443
    begin
1444
    // default is to hold the current state
1445
    control_state_nxt = control_state;
1446
 
1447
    // Note: The order is important here
1448
    if ( control_state == RST_WAIT1 )     control_state_nxt = RST_WAIT2;
1449
    if ( control_state == RST_WAIT2 )     control_state_nxt = EXECUTE;
1450
    if ( control_state == INT_WAIT1 )     control_state_nxt = INT_WAIT2;
1451
    if ( control_state == INT_WAIT2 )     control_state_nxt = EXECUTE;
1452
    if ( control_state == COPRO_WAIT )    control_state_nxt = PRE_FETCH_EXEC;
1453
    if ( control_state == PC_STALL1 )     control_state_nxt = PC_STALL2;
1454
    if ( control_state == PC_STALL2 )     control_state_nxt = EXECUTE;
1455
    if ( control_state == SWAP_WRITE )    control_state_nxt = SWAP_WAIT1;
1456
    if ( control_state == SWAP_WAIT1 )    control_state_nxt = SWAP_WAIT2;
1457
    if ( control_state == MULT_STORE )    control_state_nxt = PRE_FETCH_EXEC;
1458
    if ( control_state == MTRANS_ABORT )  control_state_nxt = PRE_FETCH_EXEC;
1459
 
1460
    if ( control_state == MEM_WAIT1 )
1461
        control_state_nxt = MEM_WAIT2;
1462
 
1463
    if ( control_state == MEM_WAIT2   ||
1464
        control_state == SWAP_WAIT2    )
1465
        begin
1466
        if ( write_pc ) // writing to the PC!! 
1467
            control_state_nxt = PC_STALL1;
1468
        else
1469
            control_state_nxt = PRE_FETCH_EXEC;
1470
        end
1471
 
1472
    if ( control_state == MTRANS_EXEC1 )
1473
        begin
1474
        if ( mtrans_instruction_nxt[15:0] != 16'd0 )
1475
            control_state_nxt = MTRANS_EXEC2;
1476
        else   // if the register list holds a single register 
1477
            begin
1478
            if ( dabt ) // data abort
1479
                control_state_nxt = MTRANS_ABORT;
1480
            else if ( write_pc ) // writing to the PC!! 
1481
                control_state_nxt = MEM_WAIT1;
1482
            else
1483
                control_state_nxt = PRE_FETCH_EXEC;
1484
            end
1485
        end
1486
 
1487
        // Stay in State MTRANS_EXEC2 until the full list of registers to
1488
        // load or store has been processed
1489
    if ( control_state == MTRANS_EXEC2 && mtrans_num_registers == 5'd1 )
1490
        begin
1491
        if ( dabt ) // data abort
1492
            control_state_nxt = MTRANS_ABORT;
1493
        else if ( write_pc ) // writing to the PC!! 
1494
            control_state_nxt = MEM_WAIT1;
1495
        else
1496
            control_state_nxt = PRE_FETCH_EXEC;
1497
        end
1498
 
1499
 
1500
    if ( control_state == MULT_PROC1 )
1501
        begin
1502
        if (!instruction_execute)
1503
            control_state_nxt = PRE_FETCH_EXEC;
1504
        else
1505
            control_state_nxt = MULT_PROC2;
1506
        end
1507
 
1508
    if ( control_state == MULT_PROC2 )
1509
        begin
1510
        if ( i_multiply_done )
1511
            if      ( o_multiply_function[1] )  // Accumulate ?
1512
                control_state_nxt = MULT_ACCUMU;
1513
            else
1514
                control_state_nxt = MULT_STORE;
1515
        end
1516
 
1517
 
1518
    if ( control_state == MULT_ACCUMU )
1519
        begin
1520
        control_state_nxt = MULT_STORE;
1521
        end
1522
 
1523
 
1524
    // This should come at the end, so that conditional execution works
1525
    // correctly
1526
    if ( instruction_valid )
1527
        begin
1528
        // default is to stay in execute state, or to move into this
1529
        // state from a conditional execute state
1530
        control_state_nxt = EXECUTE;
1531
 
1532
        if ( current_write_pc )
1533
             control_state_nxt = PC_STALL1;
1534
 
1535
        if ( load_op && instruction[15:12]  == 4'd15 )  // load new PC value
1536
             control_state_nxt = MEM_WAIT1;
1537
 
1538
        // ldm rx, {pc}
1539
        if ( type == MTRANS && instruction[20] && mtrans_reg1 == 4'd15 ) // Write to PC
1540
             control_state_nxt = MEM_WAIT1;
1541
 
1542
        if ( type == MTRANS && !conflict && mtrans_num_registers != 5'd0 && mtrans_num_registers != 5'd1 )
1543
            control_state_nxt = MTRANS_EXEC1;
1544
 
1545
        if ( type == MULT && !conflict )
1546
                control_state_nxt = MULT_PROC1;
1547
 
1548
        if ( type == SWAP && !conflict )
1549
                control_state_nxt = SWAP_WRITE;
1550
 
1551
        if ( type == CORTRANS && !und_request && !conflict )
1552
                control_state_nxt = COPRO_WAIT;
1553
 
1554
         // interrupt overrides everything else so its last       
1555
        if ( interrupt && !conflict )
1556
                control_state_nxt = INT_WAIT1;
1557
        end
1558
 
1559
    end
1560
 
1561
 
1562
// ========================================================
1563
// Register Update
1564
// ========================================================
1565
always @ ( posedge i_clk )
1566 35 csantifort
    if ( !i_core_stall )
1567 16 csantifort
        begin
1568
        if (!conflict)
1569
            begin
1570
            fetch_instruction_r         <= i_fetch_instruction;
1571 35 csantifort
            fetch_instruction_type_r    <= instruction_type(i_fetch_instruction);
1572 16 csantifort
            fetch_address_r             <= i_execute_iaddress;
1573
            iabt_reg                    <= i_iabt;
1574
            adex_reg                    <= i_adex;
1575
            abt_status_reg              <= i_abt_status;
1576
            end
1577
 
1578
        o_status_bits_mode          <= status_bits_mode_nxt;
1579
        o_status_bits_irq_mask      <= status_bits_irq_mask_nxt;
1580
        o_status_bits_firq_mask     <= status_bits_firq_mask_nxt;
1581
        o_imm32                     <= imm32_nxt;
1582
        o_imm_shift_amount          <= imm_shift_amount_nxt;
1583
        o_shift_imm_zero            <= shift_imm_zero_nxt;
1584
 
1585
                                        // when have an interrupt, execute the interrupt operation
1586
                                        // unconditionally in the execute stage
1587
                                        // ensures that status_bits register gets updated correctly
1588
                                        // Likewise when in middle of multi-cycle instructions
1589
                                        // execute them unconditionally
1590
        o_condition                 <= instruction_valid && !interrupt ? condition_nxt : AL;
1591
        o_decode_exclusive          <= decode_exclusive_nxt;
1592
        o_decode_iaccess            <= decode_iaccess_nxt;
1593
        o_decode_daccess            <= decode_daccess_nxt;
1594
 
1595
        o_rm_sel                    <= rm_sel_nxt;
1596
        o_rs_sel                    <= rs_sel_nxt;
1597
        o_load_rd                   <= load_rd_nxt;
1598
        load_rd_d1                  <= load_rd_d1_nxt;
1599
        load_pc_r                   <= load_pc_nxt;
1600
        o_rn_sel                    <= rn_sel_nxt;
1601
        o_barrel_shift_amount_sel   <= barrel_shift_amount_sel_nxt;
1602
        o_barrel_shift_data_sel     <= barrel_shift_data_sel_nxt;
1603
        o_barrel_shift_function     <= barrel_shift_function_nxt;
1604
        o_alu_function              <= alu_function_nxt;
1605
        o_multiply_function         <= multiply_function_nxt;
1606
        o_interrupt_vector_sel      <= next_interrupt;
1607
        o_iaddress_sel              <= iaddress_sel_nxt;
1608
        o_daddress_sel              <= daddress_sel_nxt;
1609
        o_pc_sel                    <= pc_sel_nxt;
1610
        o_byte_enable_sel           <= byte_enable_sel_nxt;
1611
        o_status_bits_sel           <= status_bits_sel_nxt;
1612
        o_reg_write_sel             <= reg_write_sel_nxt;
1613
        o_firq_not_user_mode        <= firq_not_user_mode_nxt;
1614
        o_write_data_wen            <= write_data_wen_nxt;
1615
        o_base_address_wen          <= base_address_wen_nxt;
1616
        o_pc_wen                    <= pc_wen_nxt;
1617
        o_reg_bank_wen              <= reg_bank_wen_nxt;
1618
        o_status_bits_flags_wen     <= status_bits_flags_wen_nxt;
1619
        o_status_bits_mode_wen      <= status_bits_mode_wen_nxt;
1620
        o_status_bits_irq_mask_wen  <= status_bits_irq_mask_wen_nxt;
1621
        o_status_bits_firq_mask_wen <= status_bits_firq_mask_wen_nxt;
1622
 
1623
        o_copro_opcode1             <= instruction[23:21];
1624
        o_copro_opcode2             <= instruction[7:5];
1625
        o_copro_crn                 <= instruction[19:16];
1626
        o_copro_crm                 <= instruction[3:0];
1627
        o_copro_num                 <= instruction[11:8];
1628
        o_copro_operation           <= copro_operation_nxt;
1629
        o_copro_write_data_wen      <= copro_write_data_wen_nxt;
1630
        restore_base_address        <= restore_base_address_nxt;
1631
        control_state               <= control_state_nxt;
1632
        end
1633
 
1634
 
1635
 
1636
always @ ( posedge i_clk )
1637 35 csantifort
    if ( !i_core_stall )
1638 16 csantifort
        begin
1639
        // sometimes this is a pre-fetch instruction
1640
        // e.g. two ldr instructions in a row. The second ldr will be saved
1641
        // to the pre-fetch instruction register
1642
        // then when its decoded, a copy is saved to the saved_current_instruction
1643
        // register
1644
        if      ( type == MTRANS )
1645
            begin
1646
            saved_current_instruction              <= mtrans_instruction_nxt;
1647 35 csantifort
            saved_current_instruction_type         <= type;
1648 16 csantifort
            saved_current_instruction_iabt         <= instruction_iabt;
1649
            saved_current_instruction_adex         <= instruction_adex;
1650
            saved_current_instruction_address      <= instruction_address;
1651
            saved_current_instruction_iabt_status  <= instruction_iabt_status;
1652
            end
1653
        else if ( saved_current_instruction_wen )
1654
            begin
1655
            saved_current_instruction              <= instruction;
1656 35 csantifort
            saved_current_instruction_type         <= type;
1657 16 csantifort
            saved_current_instruction_iabt         <= instruction_iabt;
1658
            saved_current_instruction_adex         <= instruction_adex;
1659
            saved_current_instruction_address      <= instruction_address;
1660
            saved_current_instruction_iabt_status  <= instruction_iabt_status;
1661
            end
1662
 
1663
        if      ( pre_fetch_instruction_wen )
1664
            begin
1665
            pre_fetch_instruction                  <= fetch_instruction_r;
1666 35 csantifort
            pre_fetch_instruction_type             <= fetch_instruction_type_r;
1667 16 csantifort
            pre_fetch_instruction_iabt             <= iabt_reg;
1668
            pre_fetch_instruction_adex             <= adex_reg;
1669
            pre_fetch_instruction_address          <= fetch_address_r;
1670
            pre_fetch_instruction_iabt_status      <= abt_status_reg;
1671
            end
1672 35 csantifort
 
1673
 
1674
        // TODO possible to use saved_current_instruction instead and save some regs?          
1675 16 csantifort
        hold_instruction              <= instruction;
1676 35 csantifort
        hold_instruction_type         <= type;
1677 16 csantifort
        hold_instruction_iabt         <= instruction_iabt;
1678
        hold_instruction_adex         <= instruction_adex;
1679
        hold_instruction_address      <= instruction_address;
1680
        hold_instruction_iabt_status  <= instruction_iabt_status;
1681
        end
1682
 
1683
 
1684
 
1685
always @ ( posedge i_clk )
1686 35 csantifort
    if ( !i_core_stall )
1687 16 csantifort
        begin
1688
        irq   <= i_irq;
1689
        firq  <= i_firq;
1690
 
1691
        if ( control_state == INT_WAIT1 && o_status_bits_mode == SVC )
1692
            begin
1693
            dabt_reg  <= 1'd0;
1694
            end
1695
        else
1696
            begin
1697
            dabt_reg  <= dabt_reg || i_dabt;
1698
            end
1699
 
1700
        dabt_reg_d1  <= dabt_reg;
1701
        end
1702
 
1703
assign dabt = dabt_reg || i_dabt;
1704
 
1705
 
1706
// ========================================================
1707
// Decompiler for debugging core - not synthesizable
1708
// ========================================================
1709
//synopsys translate_off
1710
 
1711
`include "debug_functions.v"
1712
 
1713
a25_decompile  u_decompile (
1714
    .i_clk                      ( i_clk                            ),
1715 35 csantifort
    .i_core_stall               ( i_core_stall                     ),
1716 16 csantifort
    .i_instruction              ( instruction                      ),
1717
    .i_instruction_valid        ( instruction_valid &&!conflict    ),
1718
    .i_instruction_execute      ( instruction_execute              ),
1719
    .i_instruction_address      ( instruction_address              ),
1720
    .i_interrupt                ( {3{interrupt}} & next_interrupt  ),
1721
    .i_interrupt_state          ( control_state == INT_WAIT2       ),
1722
    .i_instruction_undefined    ( und_request                      ),
1723
    .i_pc_sel                   ( o_pc_sel                         ),
1724
    .i_pc_wen                   ( o_pc_wen                         )
1725
);
1726
 
1727
 
1728
wire    [(15*8)-1:0]    xCONTROL_STATE;
1729
wire    [(15*8)-1:0]    xMODE;
1730
wire    [( 8*8)-1:0]    xTYPE;
1731
 
1732
assign xCONTROL_STATE        =
1733
                               control_state == RST_WAIT1      ? "RST_WAIT1"      :
1734
                               control_state == RST_WAIT2      ? "RST_WAIT2"      :
1735
 
1736
 
1737
                               control_state == INT_WAIT1      ? "INT_WAIT1"      :
1738
                               control_state == INT_WAIT2      ? "INT_WAIT2"      :
1739
                               control_state == EXECUTE        ? "EXECUTE"        :
1740
                               control_state == PRE_FETCH_EXEC ? "PRE_FETCH_EXEC" :
1741
                               control_state == MEM_WAIT1      ? "MEM_WAIT1"      :
1742
                               control_state == MEM_WAIT2      ? "MEM_WAIT2"      :
1743
                               control_state == PC_STALL1      ? "PC_STALL1"      :
1744
                               control_state == PC_STALL2      ? "PC_STALL2"      :
1745
                               control_state == MTRANS_EXEC1   ? "MTRANS_EXEC1"   :
1746
                               control_state == MTRANS_EXEC2   ? "MTRANS_EXEC2"   :
1747
                               control_state == MTRANS_ABORT   ? "MTRANS_ABORT"   :
1748
                               control_state == MULT_PROC1     ? "MULT_PROC1"     :
1749
                               control_state == MULT_PROC2     ? "MULT_PROC2"     :
1750
                               control_state == MULT_STORE     ? "MULT_STORE"     :
1751
                               control_state == MULT_ACCUMU    ? "MULT_ACCUMU"    :
1752
                               control_state == SWAP_WRITE     ? "SWAP_WRITE"     :
1753
                               control_state == SWAP_WAIT1     ? "SWAP_WAIT1"     :
1754
                               control_state == SWAP_WAIT2     ? "SWAP_WAIT2"     :
1755
                               control_state == COPRO_WAIT     ? "COPRO_WAIT"     :
1756
                                                                 "UNKNOWN "       ;
1757
 
1758
assign xMODE  = mode_name ( o_status_bits_mode );
1759
 
1760
assign xTYPE  =
1761
                               type == REGOP    ? "REGOP"    :
1762
                               type == MULT     ? "MULT"     :
1763
                               type == SWAP     ? "SWAP"     :
1764
                               type == TRANS    ? "TRANS"    :
1765
                               type == MTRANS   ? "MTRANS"   :
1766
                               type == BRANCH   ? "BRANCH"   :
1767
                               type == CODTRANS ? "CODTRANS" :
1768
                               type == COREGOP  ? "COREGOP"  :
1769
                               type == CORTRANS ? "CORTRANS" :
1770
                               type == SWI      ? "SWI"      :
1771
                                                  "UNKNOWN"  ;
1772
 
1773
 
1774
always @( posedge i_clk )
1775
    if (control_state == EXECUTE && ((instruction[0] === 1'bx) || (instruction[31] === 1'bx)))
1776
        begin
1777
        `TB_ERROR_MESSAGE
1778
        $display("Instruction with x's =%08h", instruction);
1779
        end
1780
//synopsys translate_on
1781
 
1782
endmodule
1783
 
1784
 

powered by: WebSVN 2.1.0

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