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

Subversion Repositories mips_16

[/] [mips_16/] [trunk/] [rtl/] [ID_stage.v] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 Doyya
/***************************************************
2
 * Module: ID_stage
3
 * Project: mips_16
4
 * Author: fzy
5
 * Description:
6
 *     IR, and instruction decoding
7
 *
8
 * Revise history:
9
 *
10
 ***************************************************/
11
`timescale 1ns/1ps
12
`include "mips_16_defs.v"
13
 
14
module ID_stage
15
(
16
        input                                   clk,
17
        input                                   rst,
18
        input                                   instruction_decode_en,
19
        //input                                 insert_bubble,
20
 
21
 
22
        // to EX_stage
23
        output  reg     [56:0]           pipeline_reg_out,       //      [56:22],35bits: ex_alu_cmd[2:0], ex_alu_src1[15:0], ex_alu_src2[15:0]
24
                                                                                                //      [21:5],17bits:  mem_write_en, mem_write_data[15:0]
25
                                                                                                //      [4:0],5bits:    write_back_en, write_back_dest[2:0], write_back_result_mux, 
26
 
27
        // to IF_stage
28
        input           [15:0]           instruction,
29
        output          [5:0]            branch_offset_imm,
30
        output  reg                             branch_taken,
31
 
32
        // to register file
33
        output          [2:0]            reg_read_addr_1,        // register file read port 1 address
34
        output          [2:0]            reg_read_addr_2,        // register file read port 2 address
35
        input           [15:0]           reg_read_data_1,        // register file read port 1 data
36
        input           [15:0]           reg_read_data_2,        // register file read port 2 data
37
 
38
        // to hazard detection unit
39
        output          [2:0]            decoding_op_src1,               //source_1 register number
40
        output          [2:0]            decoding_op_src2                //source_2 register number
41
 
42
);
43
 
44
        /********************** internal wires ***********************************/
45
        //----------------- Instruction Register signals --------------------//
46
        reg             [15:0]           instruction_reg;
47
        wire    [3:0]            ir_op_code;             //operation code
48
        wire    [2:0]            ir_dest;                //destination register number
49
        wire    [2:0]            ir_src1;                //source_1 register number
50
        wire    [2:0]            ir_src2;                //source_2 register number
51
        wire    [5:0]            ir_imm;                 //immediate number carried by the instruction
52
 
53
        //---------------- data path control signals --------------------------//
54
        // write back stage signals
55
        reg                                     write_back_en;                  // S3
56
        wire    [2:0]            write_back_dest;                // dest
57
        reg                                     write_back_result_mux;  // S1
58
        // mem stage signals
59
        wire                            mem_write_en;
60
        wire    [15:0]           mem_write_data;
61
        // ex stage signals
62
        reg             [2:0]            ex_alu_cmd;                             //S2
63
        wire    [15:0]           ex_alu_src1;
64
        wire    [15:0]           ex_alu_src2;
65
        // instruction decode stage signals
66
        reg                                     alu_src2_mux;                   // S4
67
        wire                            decoding_op_is_branch;  //S5
68
        wire                            decoding_op_is_store;   //S6
69
        wire    [3:0]            ir_op_code_with_bubble;
70
        wire    [2:0]            ir_dest_with_bubble;
71
        //reg                                   branch_condition_satisfied;
72
 
73
 
74
        /********************** Instruction Register *********************/
75
        always @ (posedge clk or posedge rst) begin
76
                if(rst) begin
77
                        instruction_reg <= 0;
78
                end
79
                else begin
80
                        if(instruction_decode_en) begin
81
                                instruction_reg <= instruction;
82
                        end
83
                end
84
        end
85
        assign ir_op_code = instruction_reg[15:12];
86
        assign ir_dest = instruction_reg[11: 9];
87
        assign ir_src1 = instruction_reg[ 8: 6];
88
        assign ir_src2 = (decoding_op_is_store)? instruction_reg[11: 9] : instruction_reg[ 5: 3];
89
        assign ir_imm  = instruction_reg[ 5: 0];
90
 
91
        /********************** pipeline bubble insertion *********************/
92
        // if instrcution decode is frozen, insert bubble operations into the pipeline
93
        assign ir_op_code_with_bubble = ( instruction_decode_en )?  ir_op_code : 0;
94
        // if instrcution decode is frozen, force destination reg number to 0, 
95
        // this operation is to prevent pipeline stall.
96
        assign ir_dest_with_bubble = ( instruction_decode_en )?  ir_dest : 0;
97
 
98
        /********************** Data path control logic *********************/
99
        always @ (*) begin
100
                if(rst) begin
101
                        write_back_en                   = 0;     // S3
102
                        write_back_result_mux   = 0;     // S1
103
                        ex_alu_cmd                              = 0;     // S2
104
                        alu_src2_mux                    = 0;     // S4
105
                end
106
                else begin
107
                        case( ir_op_code_with_bubble )
108
                                `OP_NOP :
109
                                        begin
110
                                                write_back_en                   = 0;             // S3
111
                                                write_back_result_mux   = 1'bx;         // S1
112
                                                ex_alu_cmd                              = `ALU_NC;      // S2
113
                                                alu_src2_mux                    = 1'bx;         // S4
114
                                        end
115
                                `OP_ADD :
116
                                        begin
117
                                                write_back_en                   = 1;            // S3
118
                                                write_back_result_mux   = 0;             // S1
119
                                                ex_alu_cmd                              = `ALU_ADD;     // S2
120
                                                alu_src2_mux                    = 0;             // S4
121
                                        end
122
                                `OP_SUB :
123
                                        begin
124
                                                write_back_en                   = 1;            // S3
125
                                                write_back_result_mux   = 0;             // S1
126
                                                ex_alu_cmd                              = `ALU_SUB;     // S2
127
                                                alu_src2_mux                    = 0;             // S4
128
                                        end
129
                                `OP_AND :
130
                                        begin
131
                                                write_back_en                   = 1;            // S3
132
                                                write_back_result_mux   = 0;             // S1
133
                                                ex_alu_cmd                              = `ALU_AND;     // S2
134
                                                alu_src2_mux                    = 0;             // S4
135
                                        end
136
                                `OP_OR  :
137
                                        begin
138
                                                write_back_en                   = 1;            // S3
139
                                                write_back_result_mux   = 0;             // S1
140
                                                ex_alu_cmd                              = `ALU_OR;      // S2
141
                                                alu_src2_mux                    = 0;             // S4
142
                                        end
143
                                `OP_XOR :
144
                                        begin
145
                                                write_back_en                   = 1;            // S3
146
                                                write_back_result_mux   = 0;             // S1
147
                                                ex_alu_cmd                              = `ALU_XOR;     // S2
148
                                                alu_src2_mux                    = 1'bx;         // S4
149
                                        end
150
                                `OP_SL  :
151
                                        begin
152
                                                write_back_en                   = 1;            // S3
153
                                                write_back_result_mux   = 0;             // S1
154
                                                ex_alu_cmd                              = `ALU_SL;      // S2
155
                                                alu_src2_mux                    = 0;             // S4
156
                                        end
157
                                `OP_SR  :
158
                                        begin
159
                                                write_back_en                   = 1;            // S3
160
                                                write_back_result_mux   = 0;             // S1
161
                                                ex_alu_cmd                              = `ALU_SR;      // S2
162
                                                alu_src2_mux                    = 0;             // S4
163
                                        end
164
                                `OP_SRU :
165
                                        begin
166
                                                write_back_en                   = 1;            // S3
167
                                                write_back_result_mux   = 0;             // S1
168
                                                ex_alu_cmd                              = `ALU_SRU;     // S2
169
                                                alu_src2_mux                    = 0;             // S4
170
                                        end
171
                                `OP_ADDI:
172
                                        begin
173
                                                write_back_en                   = 1;            // S3
174
                                                write_back_result_mux   = 0;             // S1
175
                                                ex_alu_cmd                              = `ALU_ADD;     // S2
176
                                                alu_src2_mux                    = 1;            // S4
177
                                        end
178
                                `OP_LD  :
179
                                        begin
180
                                                write_back_en                   = 1;            // S3
181
                                                write_back_result_mux   = 1;            // S1
182
                                                ex_alu_cmd                              = `ALU_ADD;     // S2
183
                                                alu_src2_mux                    = 1;            // S4
184
                                        end
185
                                `OP_ST  :
186
                                        begin
187
                                                write_back_en                   = 0;             // S3
188
                                                write_back_result_mux   = 1'bx;         // S1
189
                                                ex_alu_cmd                              = `ALU_ADD;     // S2
190
                                                alu_src2_mux                    = 1;            // S4
191
                                        end
192
                                `OP_BZ  :
193
                                        begin
194
                                                write_back_en                   = 0;             // S3
195
                                                write_back_result_mux   = 1'bx;         // S1
196
                                                ex_alu_cmd                              = `ALU_NC;      // S2
197
                                                alu_src2_mux                    = 1;            // S4
198
                                        end
199
                                default :
200
                                        begin
201
                                                write_back_en                   = 0;             // S3
202
                                                write_back_result_mux   = 1'bx;         // S1
203
                                                ex_alu_cmd                              = `ALU_NC;      // S2
204
                                                alu_src2_mux                    = 1'bx;         // S4
205
`ifndef CODE_FOR_SYNTHESIS
206
                                                $display("ERROR: Unknown Instruction: %b", ir_op_code_with_bubble);
207
                                                //$stop;
208
`endif
209
                                        end
210
                        endcase
211
                end
212
        end
213
 
214
        assign decoding_op_is_branch = ( ir_op_code == `OP_BZ )? 1 : 0;  // S5
215
        assign decoding_op_is_store     = ( ir_op_code == `OP_ST )? 1 : 0;       // S6
216
 
217
        /********************** singals to EX_stage *********************/
218
        assign mem_write_data = reg_read_data_2;
219
        assign mem_write_en = decoding_op_is_store;
220
        assign write_back_dest = ir_dest_with_bubble;
221
        assign ex_alu_src1 = reg_read_data_1;
222
        assign ex_alu_src2 = (alu_src2_mux)? {{10{ir_imm[5]}},ir_imm} : reg_read_data_2;
223
 
224
        //      pipeline_reg_out:
225
        //      [56:22],35bits: ex_alu_cmd[2:0], ex_alu_src1[15:0], ex_alu_src2[15:0],
226
        //      [21:5],17bits:  mem_write_en, mem_write_data[15:0],
227
        //      [4:0],5bits:    write_back_en, write_back_dest[2:0], write_back_result_mux,
228
 
229
        always @ (posedge clk or posedge rst) begin
230
                if(rst) begin
231
                        pipeline_reg_out[56:0] <= 0;
232
                end
233
                else begin
234
                        pipeline_reg_out[56:0] <= {
235
                                ex_alu_cmd[2:0],         // pipeline_reg_out[56:54]      //S2
236
                                ex_alu_src1[15:0],               // pipeline_reg_out[53:38]
237
                                ex_alu_src2[15:0],               // pipeline_reg_out[37:22]      
238
                                mem_write_en,                   // pipeline_reg_out[21]         //
239
                                mem_write_data[15:0],    // pipeline_reg_out[20:5]       //
240
                                write_back_en,                  // pipeline_reg_out[4]          //S3
241
                                write_back_dest[2:0],    // pipeline_reg_out[3:1]        //dest
242
                                write_back_result_mux   // pipeline_reg_out[0]          //S1
243
                                };
244
                end
245
        end
246
 
247
 
248
        /********************** interface with register file *********************/
249
        assign reg_read_addr_1 = ir_src1;
250
        assign reg_read_addr_2 = ir_src2;
251
 
252
        /********************** branch signals generate *********************/
253
        always @ (*) begin
254
                if(decoding_op_is_branch) begin
255
                        case( ir_dest_with_bubble )
256
                                `BRANCH_Z       :
257
                                        begin
258
                                                if(reg_read_data_1 == 0)
259
                                                        branch_taken = 1;
260
                                                else
261
                                                        branch_taken = 0;
262
                                        end
263
 
264
                                default:
265
                                        begin
266
                                                branch_taken = 0;
267
`ifndef CODE_FOR_SYNTHESIS
268
                                                $display("ERROR: Unknown branch condition %b, in branch instruction %b \n", ir_dest_with_bubble, ir_op_code_with_bubble);
269
                                                //$stop;
270
`endif
271
                                        end
272
                        endcase
273
                end
274
                else begin
275
                        branch_taken = 0;
276
                end
277
        end
278
        assign branch_offset_imm = ir_imm;
279
        //assign branch_taken = decoding_op_is_branch & branch_condition_satisfied ;
280
 
281
        /********************** to hazard detection unit *********************/
282
        assign decoding_op_src1 = ir_src1;
283
        assign decoding_op_src2 = (
284
                                        ir_op_code == `OP_NOP   ||
285
                                        ir_op_code == `OP_ADDI  ||
286
                                        ir_op_code == `OP_LD    ||
287
                                        ir_op_code == `OP_BZ
288
                                        )?
289
                                        3'b000 : ir_src2;
290
 
291
endmodule

powered by: WebSVN 2.1.0

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