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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber23/] [a23_decode.v] - Blame information for rev 87

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

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

powered by: WebSVN 2.1.0

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