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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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