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

Subversion Repositories amber

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

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

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

powered by: WebSVN 2.1.0

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