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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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