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

Subversion Repositories amber

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

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

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

powered by: WebSVN 2.1.0

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