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

Subversion Repositories aor3000

[/] [aor3000/] [trunk/] [rtl/] [pipeline/] [pipeline_rf.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 alfik
/*
2
 * This file is subject to the terms and conditions of the BSD License. See
3
 * the file "LICENSE" in the main directory of this archive for more details.
4
 *
5
 * Copyright (C) 2014 Aleksander Osman
6
 */
7
 
8
`include "defines.v"
9
 
10
module pipeline_rf(
11
    input               clk,
12
    input               rst_n,
13
 
14
    //
15
    input               exception_start,
16
 
17
    //
18
    input               if_exc_address_error,
19
    input               if_exc_tlb_inv,
20
    input               if_exc_tlb_miss,
21
    input               if_ready,
22
    input       [31:0]  if_instr,
23
    input       [31:0]  if_pc,
24
 
25
    //
26
    output      [6:0]   rf_cmd,
27
    output reg  [31:0]  rf_instr,
28
    output reg  [31:0]  rf_pc_plus4,
29
    output reg  [31:0]  rf_badvpn,
30
    output      [31:0]  rf_a,
31
    output      [31:0]  rf_b,
32
 
33
    //
34
    input               mem_stall,
35
 
36
    //
37
    input       [4:0]   exe_result_index,
38
    input       [31:0]  exe_result,
39
 
40
    input       [4:0]   mem_result_index,
41
    input       [31:0]  mem_result,
42
 
43
    input       [4:0]   muldiv_result_index,
44
    input       [31:0]  muldiv_result
45
);
46
 
47
//------------------------------------------------------------------------------
48
 
49
wire rf_load = (if_ready || if_exc_address_error || if_exc_tlb_inv || if_exc_tlb_miss) && ~(mem_stall);
50
 
51
//------------------------------------------------------------------------------
52
 
53
//rd <- rs OP rt
54
wire cmd_3arg_add  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100000;
55
wire cmd_3arg_addu = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100001;
56
wire cmd_3arg_and  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100100;
57
wire cmd_3arg_nor  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100111;
58
wire cmd_3arg_or   = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100101;
59
wire cmd_3arg_slt  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b101010;
60
wire cmd_3arg_sltu = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b101011;
61
wire cmd_3arg_sub  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100010;
62
wire cmd_3arg_subu = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100011;
63
wire cmd_3arg_xor  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b100110;
64
wire cmd_3arg_sllv = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000100;
65
wire cmd_3arg_srav = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000111;
66
wire cmd_3arg_srlv = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000110;
67
 
68
//rd <- rt OP imm
69
wire cmd_sll = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000000;
70
wire cmd_sra = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000011;
71
wire cmd_srl = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b000010;
72
 
73
//rt <- rs OP imm
74
wire cmd_addi  = rf_instr[31:26] == 6'b001000;
75
wire cmd_addiu = rf_instr[31:26] == 6'b001001;
76
wire cmd_andi  = rf_instr[31:26] == 6'b001100;
77
wire cmd_ori   = rf_instr[31:26] == 6'b001101;
78
wire cmd_slti  = rf_instr[31:26] == 6'b001010;
79
wire cmd_sltiu = rf_instr[31:26] == 6'b001011;
80
wire cmd_xori  = rf_instr[31:26] == 6'b001110;
81
 
82
//rd <- hi,lo
83
wire cmd_muldiv_mfhi  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b010000;
84
wire cmd_muldiv_mflo  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b010010;
85
 
86
//hi,lo <- rs
87
wire cmd_muldiv_mthi  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b010001 && rf_instr[15:11] == 5'b00000;
88
wire cmd_muldiv_mtlo  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b010011 && rf_instr[15:11] == 5'b00000;
89
 
90
//hi,lo <- rs OP rt
91
wire cmd_muldiv_mult  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b011000  && rf_instr[15:11] == 5'b00000;
92
wire cmd_muldiv_multu = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b011001  && rf_instr[15:11] == 5'b00000;
93
wire cmd_muldiv_div   = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b011010;
94
wire cmd_muldiv_divu  = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b011011;
95
 
96
//rt <- imm
97
wire cmd_lui = rf_instr[31:26] == 6'b001111;
98
 
99
//exception
100
wire cmd_break   = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b001101;
101
wire cmd_syscall = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b001100;
102
 
103
wire cmd_unusable123 = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] != 2'b00 && ~(cmd_cfc1_detect);
104
wire cmd_lwc123      = rf_instr[31:28] == 4'b1100 && rf_instr[27:26] != 2'b00;
105
wire cmd_swc123      = rf_instr[31:28] == 4'b1110 && rf_instr[27:26] != 2'b00;
106
 
107
//cmd_swc0, cmd_lwc0, cmd_cop0_inv: `CMD_exc_reserved_instr
108
 
109
wire exc_coproc_unusable = cmd_unusable123 || cmd_lwc123 || cmd_swc123;
110
 
111
// rt <- 0
112
wire cmd_cfc1_detect = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b01 && rf_instr[25:21] == 5'b00010 && rf_instr[15:11] == 5'b00000;
113
 
114
//rd_cp0 <- rt
115
wire cmd_mtc0  = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25:21] == 5'b00100;
116
//rt <- rd_cp0 
117
wire cmd_mfc0  = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25:21] == 5'b00000;
118
 
119
wire cmd_bc0f    = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25:21] == 5'b01000 && rf_instr[20:16] == 5'd0;
120
wire cmd_bc0t    = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25:21] == 5'b01000 && rf_instr[20:16] == 5'd1;
121
wire cmd_bc0_ign = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25:21] == 5'b01000 && (rf_instr[20:16] == 5'd2 || rf_instr[20:16] == 5'd3);
122
 
123
wire cmd_rfe   = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25] == 1'b1 && rf_instr[5:0] == 6'b010000;
124
wire cmd_tlbp  = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25] == 1'b1 && rf_instr[5:0] == 6'b001000;
125
wire cmd_tlbr  = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25] == 1'b1 && rf_instr[5:0] == 6'b000001;
126
wire cmd_tlbwi = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25] == 1'b1 && rf_instr[5:0] == 6'b000010;
127
wire cmd_tlbwr = rf_instr[31:28] == 4'b0100 && rf_instr[27:26] == 2'b00 && rf_instr[25] == 1'b1 && rf_instr[5:0] == 6'b000110;
128
 
129
//rt <- mem
130
wire cmd_lb  = rf_instr[31:26] == 6'b100000;
131
wire cmd_lbu = rf_instr[31:26] == 6'b100100;
132
wire cmd_lh  = rf_instr[31:26] == 6'b100001;
133
wire cmd_lhu = rf_instr[31:26] == 6'b100101;
134
wire cmd_lw  = rf_instr[31:26] == 6'b100011;
135
wire cmd_lwl = rf_instr[31:26] == 6'b100010;
136
wire cmd_lwr = rf_instr[31:26] == 6'b100110;
137
 
138
//mem <- rt
139
wire cmd_sb  = rf_instr[31:26] == 6'b101000;
140
wire cmd_sh  = rf_instr[31:26] == 6'b101001;
141
wire cmd_sw  = rf_instr[31:26] == 6'b101011;
142
wire cmd_swl = rf_instr[31:26] == 6'b101010;
143
wire cmd_swr = rf_instr[31:26] == 6'b101110;
144
 
145
//<- rs, rt
146
wire cmd_beq    = rf_instr[31:26] == 6'b000100;
147
wire cmd_bne    = rf_instr[31:26] == 6'b000101;
148
//<- rs
149
wire cmd_bgez   = rf_instr[31:26] == 6'b000001 && rf_instr[20:16] == 5'b00001;
150
wire cmd_bgtz   = rf_instr[31:26] == 6'b000111 && rf_instr[20:16] == 5'b00000;
151
wire cmd_blez   = rf_instr[31:26] == 6'b000110 && rf_instr[20:16] == 5'b00000;
152
wire cmd_bltz   = rf_instr[31:26] == 6'b000001 && rf_instr[20:16] == 5'b00000;
153
wire pre_jr     = if_instr[31:26] == 6'b000000 && if_instr[5:0] == 6'b001000;
154
wire cmd_jr     = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b001000 && rf_jr_check;
155
//r31 <- rs
156
wire cmd_bgezal = rf_instr[31:26] == 6'b000001 && rf_instr[20:16] == 5'b10001;
157
wire cmd_bltzal = rf_instr[31:26] == 6'b000001 && rf_instr[20:16] == 5'b10000;
158
//rd <- rs
159
wire cmd_jalr   = rf_instr[31:26] == 6'b000000 && rf_instr[5:0] == 6'b001001;
160
//r31 <-
161
wire cmd_jal    = rf_instr[31:26] == 6'b000011;
162
//
163
wire cmd_j      = rf_instr[31:26] == 6'b000010;
164
 
165
//------------------------------------------------------------------------------
166
 
167
assign rf_cmd =
168
    (exception_start)?          `CMD_null :
169
    (rf_exc_address_error)?     `CMD_exc_load_addr_err :
170
    (rf_exc_tlb_inv)?           `CMD_exc_load_tlb :
171
    (rf_exc_tlb_miss)?          `CMD_exc_tlb_load_miss :
172
    (~(rf_ready))?              `CMD_null :
173
    (cmd_3arg_add)?             `CMD_3arg_add :
174
    (cmd_3arg_addu)?            `CMD_3arg_addu :
175
    (cmd_3arg_and)?             `CMD_3arg_and :
176
    (cmd_3arg_nor)?             `CMD_3arg_nor :
177
    (cmd_3arg_or)?              `CMD_3arg_or :
178
    (cmd_3arg_slt)?             `CMD_3arg_slt :
179
    (cmd_3arg_sltu)?            `CMD_3arg_sltu :
180
    (cmd_3arg_sub)?             `CMD_3arg_sub :
181
    (cmd_3arg_subu)?            `CMD_3arg_subu :
182
    (cmd_3arg_xor)?             `CMD_3arg_xor :
183
    (cmd_3arg_sllv)?            `CMD_3arg_sllv :
184
    (cmd_3arg_srav)?            `CMD_3arg_srav :
185
    (cmd_3arg_srlv)?            `CMD_3arg_srlv :
186
    (cmd_sll)?                  `CMD_sll :
187
    (cmd_sra)?                  `CMD_sra :
188
    (cmd_srl)?                  `CMD_srl :
189
    (cmd_addi)?                 `CMD_addi :
190
    (cmd_addiu)?                `CMD_addiu :
191
    (cmd_andi)?                 `CMD_andi :
192
    (cmd_ori)?                  `CMD_ori :
193
    (cmd_slti)?                 `CMD_slti :
194
    (cmd_sltiu)?                `CMD_sltiu :
195
    (cmd_xori)?                 `CMD_xori :
196
    (cmd_muldiv_mfhi)?          `CMD_muldiv_mfhi :
197
    (cmd_muldiv_mflo)?          `CMD_muldiv_mflo :
198
    (cmd_muldiv_mthi)?          `CMD_muldiv_mthi :
199
    (cmd_muldiv_mtlo)?          `CMD_muldiv_mtlo :
200
    (cmd_muldiv_mult)?          `CMD_muldiv_mult :
201
    (cmd_muldiv_multu)?         `CMD_muldiv_multu :
202
    (cmd_muldiv_div)?           `CMD_muldiv_div :
203
    (cmd_muldiv_divu)?          `CMD_muldiv_divu :
204
    (cmd_lui)?                  `CMD_lui :
205
    (cmd_break)?                `CMD_break :
206
    (cmd_syscall)?              `CMD_syscall :
207
    (exc_coproc_unusable)?      `CMD_exc_coproc_unusable :
208
    (cmd_mtc0)?                 `CMD_mtc0 :
209
    (cmd_mfc0)?                 `CMD_mfc0 :
210
    (cmd_cfc1_detect)?          `CMD_cfc1_detect :
211
    (cmd_rfe)?                  `CMD_cp0_rfe :
212
    (cmd_tlbp)?                 `CMD_cp0_tlbp :
213
    (cmd_tlbr)?                 `CMD_cp0_tlbr :
214
    (cmd_tlbwi)?                `CMD_cp0_tlbwi :
215
    (cmd_tlbwr)?                `CMD_cp0_tlbwr :
216
    (cmd_bc0f)?                 `CMD_cp0_bc0f :
217
    (cmd_bc0t)?                 `CMD_cp0_bc0t :
218
    (cmd_bc0_ign)?              `CMD_cp0_bc0_ign :
219
    (cmd_lb)?                   `CMD_lb :
220
    (cmd_lbu)?                  `CMD_lbu :
221
    (cmd_lh)?                   `CMD_lh :
222
    (cmd_lhu)?                  `CMD_lhu :
223
    (cmd_lw)?                   `CMD_lw :
224
    (cmd_lwl)?                  `CMD_lwl :
225
    (cmd_lwr)?                  `CMD_lwr :
226
    (cmd_sb)?                   `CMD_sb :
227
    (cmd_sh)?                   `CMD_sh :
228
    (cmd_sw)?                   `CMD_sw :
229
    (cmd_swl)?                  `CMD_swl :
230
    (cmd_swr)?                  `CMD_swr :
231
    (cmd_beq)?                  `CMD_beq :
232
    (cmd_bne)?                  `CMD_bne :
233
    (cmd_bgez)?                 `CMD_bgez :
234
    (cmd_bgtz)?                 `CMD_bgtz :
235
    (cmd_blez)?                 `CMD_blez :
236
    (cmd_bltz)?                 `CMD_bltz :
237
    (cmd_jr)?                   `CMD_jr :
238
    (cmd_bgezal)?               `CMD_bgezal :
239
    (cmd_bltzal)?               `CMD_bltzal :
240
    (cmd_jalr)?                 `CMD_jalr :
241
    (cmd_jal)?                  `CMD_jal :
242
    (cmd_j)?                    `CMD_j :
243
                                `CMD_exc_reserved_instr;
244
 
245
reg rf_exc_address_error;
246
always @(posedge clk or negedge rst_n) begin
247
    if(rst_n == 1'b0)           rf_exc_address_error <= `FALSE;
248
    else if(exception_start)    rf_exc_address_error <= `FALSE;
249
    else if(rf_load)            rf_exc_address_error <= if_exc_address_error;
250
end
251
 
252
reg rf_exc_tlb_inv;
253
always @(posedge clk or negedge rst_n) begin
254
    if(rst_n == 1'b0)           rf_exc_tlb_inv <= `FALSE;
255
    else if(exception_start)    rf_exc_tlb_inv <= `FALSE;
256
    else if(rf_load)            rf_exc_tlb_inv <= if_exc_tlb_inv;
257
end
258
 
259
reg rf_exc_tlb_miss;
260
always @(posedge clk or negedge rst_n) begin
261
    if(rst_n == 1'b0)           rf_exc_tlb_miss <= `FALSE;
262
    else if(exception_start)    rf_exc_tlb_miss <= `FALSE;
263
    else if(rf_load)            rf_exc_tlb_miss <= if_exc_tlb_miss;
264
end
265
 
266
reg rf_ready;
267
always @(posedge clk or negedge rst_n) begin
268
    if(rst_n == 1'b0)           rf_ready <= `FALSE;
269
    else if(exception_start)    rf_ready <= `FALSE;
270
    else if(rf_load && if_ready)rf_ready <= `TRUE;
271
    else if(~(mem_stall))       rf_ready <= `FALSE;
272
end
273
 
274
always @(posedge clk or negedge rst_n) begin
275
    if(rst_n == 1'b0)           rf_instr <= 32'd0;
276
    else if(rf_load)            rf_instr <= if_instr;
277
end
278
 
279
always @(posedge clk or negedge rst_n) begin
280
    if(rst_n == 1'b0)           rf_pc_plus4 <= 32'd0;
281
    else if(rf_load)            rf_pc_plus4 <= if_pc + 32'd4;
282
end
283
 
284
always @(posedge clk or negedge rst_n) begin
285
    if(rst_n == 1'b0)           rf_badvpn <= 32'd0;
286
    else if(rf_load)            rf_badvpn <= if_pc;
287
end
288
 
289
//------------------------------------------------------------------------------
290
 
291
wire [4:0] rf_instr_rs = rf_instr[25:21];
292
wire [4:0] rf_instr_rt = rf_instr[20:16];
293
wire [4:0] rf_instr_rd = rf_instr[15:11];
294
 
295
assign rf_a =
296
    (exe_result_index != 5'd0    && rf_instr_rs == exe_result_index)?       exe_result :
297
    (muldiv_result_index != 5'd0 && rf_instr_rs == muldiv_result_index)?    muldiv_result :
298
    (mem_result_index != 5'd0    && rf_instr_rs == mem_result_index)?       mem_result :
299
                                                                            q_a_final;
300
 
301
assign rf_b =
302
    (exe_result_index != 5'd0    && rf_instr_rt == exe_result_index)?       exe_result :
303
    (muldiv_result_index != 5'd0 && rf_instr_rt == muldiv_result_index)?    muldiv_result :
304
    (mem_result_index != 5'd0    && rf_instr_rt == mem_result_index)?       mem_result :
305
                                                                            q_b_final;
306
 
307
wire rf_jr_check =
308
    (exe_result_index != 5'd0    && rf_instr_rd == exe_result_index)?       exe_result == 32'd0 :
309
    (muldiv_result_index != 5'd0 && rf_instr_rd == muldiv_result_index)?    muldiv_result == 32'd0 :
310
    (mem_result_index != 5'd0    && rf_instr_rd == mem_result_index)?       mem_result == 32'd0 :
311
                                                                            q_b_final == 32'd0;
312
 
313
//------------------------------------------------------------------------------
314
 
315
reg [4:0]  address_a_reg;
316
reg [4:0]  address_b_reg;
317
reg [4:0]  written_index_reg;
318
reg [31:0] written_data_reg;
319
 
320
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) address_a_reg     <= 5'd0;  else if(~(mem_stall)) address_a_reg <= address_a; end
321
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) address_b_reg     <= 5'd0;  else if(~(mem_stall)) address_b_reg <= address_b; end
322
 
323
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) written_data_reg  <= 32'd0; else written_data_reg  <= mem_result;       end
324
always @(posedge clk or negedge rst_n) begin if(rst_n == 1'b0) written_index_reg <= 5'd0;  else written_index_reg <= mem_result_index; end
325
 
326
wire [31:0] q_a_final = (written_index_reg != 5'd0 && address_a_reg == written_index_reg)? written_data_reg : q_a;
327
wire [31:0] q_b_final = (written_index_reg != 5'd0 && address_b_reg == written_index_reg)? written_data_reg : q_b;
328
 
329
//------------------------------------------------------------------------------
330
wire [4:0] if_instr_rs = if_instr[25:21];
331
wire [4:0] if_instr_rt = if_instr[20:16];
332
wire [4:0] if_instr_rd = if_instr[15:11];
333
 
334
wire [4:0] address_a = if_instr_rs;
335
wire [4:0] address_b = (pre_jr)? if_instr_rd : if_instr_rt;
336
 
337
wire [31:0] q_a;
338
wire [31:0] q_b;
339
 
340
model_simple_dual_ram #(
341
    .width          (32),
342
    .widthad        (5)
343
)
344
regs_a_inst(
345
    .clk            (clk),
346
 
347
    .address_a      ((mem_stall)? address_a_reg : address_a),
348
    .q_a            (q_a),
349
 
350
    .address_b      (mem_result_index),
351
    .wren_b         (mem_result_index != 5'd0),
352
    .data_b         (mem_result)
353
);
354
 
355
model_simple_dual_ram #(
356
    .width          (32),
357
    .widthad        (5)
358
)
359
regs_b_inst(
360
    .clk            (clk),
361
 
362
    .address_a      ((mem_stall)? address_b_reg : address_b),
363
    .q_a            (q_b),
364
 
365
    .address_b      (mem_result_index),
366
    .wren_b         (mem_result_index != 5'd0),
367
    .data_b         (mem_result)
368
);
369
 
370
 
371
//------------------------------------------------------------------------------
372
 
373
endmodule

powered by: WebSVN 2.1.0

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