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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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