@@ -37,16 +37,16 @@ module cpu_pipelined(
3737 wire mem_wb_reg_write;
3838 wire mem_wb_nop_instruction;
3939
40- // Branch prediction and handling signals
40+ // Branch prediction ke signals
4141 wire branch_predicted;
4242 wire [63 :0 ] predicted_pc;
4343 wire branch_mispredicted;
4444 wire flush;
4545
46- // Add a one -cycle stall flag for load hazards
46+ // Ye ek -cycle stall ka flag hai - load hazard ke liye important hai
4747 reg stalled_last_cycle;
4848
49- // Program Counter
49+ // Program Counter - instruction address track karta hai
5050 wire [63 :0 ] pc_next;
5151 wire [63 :0 ] pc_current;
5252 program_counter pc (
@@ -56,14 +56,14 @@ module cpu_pipelined(
5656 .pc(pc_current)
5757 );
5858
59- // Instruction Fetch
59+ // Instruction Fetch - memory se instruction lata hai
6060 wire [31 :0 ] instruction;
6161 instruction_memory imem (
6262 .pc(pc_current),
6363 .instruction(instruction)
6464 );
6565
66- // Modified branch prediction and handling logic
66+ // Branch prediction - hum assume karte hain ki branch hamesha liya jayega
6767 wire is_branch = (instruction[6 :0 ] == 7'b1100011 );
6868 wire [63 :0 ] if_branch_offset = {{51 {instruction[31 ]}},
6969 instruction[31 ],
@@ -73,39 +73,36 @@ module cpu_pipelined(
7373 1'b0 };
7474 wire [63 :0 ] if_branch_target = pc_current + if_branch_offset;
7575
76- // Always predict taken for branches
7776 assign branch_predicted = is_branch;
78- // Key change: Use branch prediction only for branch instructions
7977 assign predicted_pc = branch_predicted ? if_branch_target : (pc_current + 4 );
8078
8179 wire nop_instruction;
8280 assign nop_instruction = (instruction == 32'b0 );
8381
84- // IF/ID Pipeline Register
82+ // IF/ID Pipeline Register - instruction ko agle stage tak pahunchata hai
8583 wire [63 :0 ] if_id_pc;
8684 wire [31 :0 ] if_id_instruction;
8785 wire if_id_nop_instruction;
8886 wire if_id_branch_predicted;
8987 wire [63 :0 ] if_id_predicted_pc;
9088
91- wire [31 :0 ] instr_to_use = flush ? 32'h00000013 : instruction; // NOP when flush
89+ wire [31 :0 ] instr_to_use = flush ? 32'h00000013 : instruction; // Agar flush hai to NOP bhejo
9290
93- // Updated to handle one-cycle stall
9491 if_id_register if_id (
9592 .clk(clk),
9693 .reset(reset | flush),
97- .en(~stall | stalled_last_cycle | branch_predicted), // Enable if we stalled last cycle
94+ .en(~stall | stalled_last_cycle | branch_predicted),
9895 .d({
9996 pc_current,
100- stall & ~stalled_last_cycle ? 32'h00000013 : instr_to_use, // Insert NOP during initial stall
97+ stall & ~stalled_last_cycle ? 32'h00000013 : instr_to_use, // Load hazard ke time NOP dalne ke liye
10198 stall & ~stalled_last_cycle ? 1'b1 : nop_instruction,
10299 branch_predicted,
103100 predicted_pc
104101 }),
105102 .q({if_id_pc, if_id_instruction, if_id_nop_instruction, if_id_branch_predicted, if_id_predicted_pc})
106103 );
107104
108- // Decode (Control Signals)
105+ // Decode stage - instruction ko samajhne ke liye
109106 wire branch;
110107 wire mem_read;
111108 wire mem_to_reg;
@@ -123,7 +120,7 @@ module cpu_pipelined(
123120 .reg_write(reg_write)
124121 );
125122
126- // Register File
123+ // Register File - CPU ke registers
127124 wire [4 :0 ] rs1 = if_id_instruction[19 :15 ];
128125 wire [4 :0 ] rs2 = if_id_instruction[24 :20 ];
129126 wire [4 :0 ] reg_rd;
@@ -151,27 +148,27 @@ module cpu_pipelined(
151148 wire id_ex_branch_predicted;
152149 wire [63 :0 ] id_ex_predicted_pc;
153150
154- // Load hazard detection
151+ // Load hazard ka pata lagane ke liye - jab load ke turant baad uska data use ho
155152 wire [4 :0 ] id_ex_rd = id_ex_instruction[11 :7 ];
156- // Only detect hazard if we haven't stalled for it already
153+ // Sirf tab detect karo jab pehle se stall na kiya ho
157154 wire load_hazard = id_ex_mem_read &
158155 ((id_ex_rd == rs1) | (id_ex_rd == rs2)) &
159156 (id_ex_rd != 0 ) &
160157 ~ stalled_last_cycle;
161158 wire stall = load_hazard;
162159
163- // Stalled cycle tracking
160+ // Stall tracking - yaad rakhne ke liye ki pichle cycle mein stall kiya tha
164161 always @(posedge clk or posedge reset) begin
165162 if (reset)
166163 stalled_last_cycle <= 1'b0 ;
167164 else
168- stalled_last_cycle <= stall; // Track if we stalled in the previous cycle
165+ stalled_last_cycle <= stall; // Pichle cycle mein stall kiya tha ki nahi
169166 end
170167
171168 id_ex_register id_ex (
172169 .clk(clk),
173170 .reset(reset | flush),
174- .en(~stall | stalled_last_cycle), // Always enable after one stall cycle
171+ .en(~stall | stalled_last_cycle), // Ek stall ke baad hamesha enable karo
175172 .d({
176173 if_id_pc,
177174 reg_read_data1,
@@ -204,52 +201,46 @@ module cpu_pipelined(
204201 })
205202 );
206203
207- // Forwarding Logic (EX stage)
204+ // Forwarding Logic - data dependency handle karne ke liye
208205 wire [4 :0 ] ex_mem_rd = ex_mem_instruction[11 :7 ];
209206 wire [4 :0 ] mem_wb_rd = mem_wb_instruction[11 :7 ];
210207 wire [4 :0 ] id_ex_rs1 = id_ex_instruction[19 :15 ];
211208 wire [4 :0 ] id_ex_rs2 = id_ex_instruction[24 :20 ];
212209
213- // Improved forwarding logic
210+ // Super-smart forwarding - load instructions ke liye special handling
214211 reg [1 :0 ] forwardA, forwardB;
215212
216213 always @(* ) begin
217214 forwardA = 2'b00 ; // Default: no forwarding
218215
219- // Most recent data ( EX/MEM) except in case of loads
216+ // EX/MEM stage se forward karo, lekin load ke case mein nahi
220217 if (ex_mem_reg_write && ex_mem_rd != 0 && ex_mem_rd == id_ex_rs1) begin
221- // Special handling: don't forward from EX/MEM if it's a load ( data not ready yet)
218+ // Agar load hai to forward mat karo - data abhi ready nahi hai
222219 if (! ex_mem_mem_to_reg)
223- forwardA = 2'b10 ; // Forward from EX/MEM
220+ forwardA = 2'b10 ;
224221 end
225222
226- // Data from MEM/WB (includes load results)
223+ // MEM/WB se forward karo ( load results bhi isme shamil hain )
227224 if (mem_wb_reg_write && mem_wb_rd != 0 && mem_wb_rd == id_ex_rs1) begin
228- // Always forward from MEM/WB if matching
229- // This overrides EX/MEM forwarding for loads (after stall)
230- forwardA = 2'b01 ; // Forward from MEM/WB
225+ // Load ke baad EX/MEM forwarding ko override karo
226+ forwardA = 2'b01 ;
231227 end
232228 end
233229
234230 always @(* ) begin
235- forwardB = 2'b00 ; // Default: no forwarding
231+ forwardB = 2'b00 ;
236232
237- // Most recent data (EX/MEM) except in case of loads
238233 if (ex_mem_reg_write && ex_mem_rd != 0 && ex_mem_rd == id_ex_rs2) begin
239- // Special handling: don't forward from EX/MEM if it's a load (data not ready yet)
240234 if (! ex_mem_mem_to_reg)
241- forwardB = 2'b10 ; // Forward from EX/MEM
235+ forwardB = 2'b10 ;
242236 end
243237
244- // Data from MEM/WB (includes load results)
245238 if (mem_wb_reg_write && mem_wb_rd != 0 && mem_wb_rd == id_ex_rs2) begin
246- // Always forward from MEM/WB if matching
247- // This overrides EX/MEM forwarding for loads (after stall)
248- forwardB = 2'b01 ; // Forward from MEM/WB
239+ forwardB = 2'b01 ;
249240 end
250241 end
251242
252- // ALU Operand Forwarding
243+ // ALU ke inputs ready karo with forwarding
253244 wire signed [63 :0 ] ex_forward_value = ex_mem_alu_result;
254245 wire signed [63 :0 ] mem_forward_value = mem_wb_mem_to_reg ? mem_wb_mem_read_data : mem_wb_alu_result;
255246
@@ -271,14 +262,11 @@ module cpu_pipelined(
271262 endcase
272263 end
273264
274- // Immediate Generation
265+ // Immediate Generation - constants jo instruction mein encode hote hain
275266 wire [63 :0 ] imm_val;
276267
277- // I-type immediate
278268 wire [63 :0 ] i_imm = {{53 {id_ex_instruction[31 ]}}, id_ex_instruction[30 :20 ]};
279- // S-type immediate
280269 wire [63 :0 ] s_imm = {{53 {id_ex_instruction[31 ]}}, id_ex_instruction[30 :25 ], id_ex_instruction[11 :7 ]};
281- // B-type immediate
282270 wire [63 :0 ] b_imm = {{51 {id_ex_instruction[31 ]}}, id_ex_instruction[31 ], id_ex_instruction[7 ],
283271 id_ex_instruction[30 :25 ], id_ex_instruction[11 :8 ], 1'b0 };
284272
@@ -288,7 +276,7 @@ module cpu_pipelined(
288276
289277 wire signed [63 :0 ] alu_in2 = id_ex_alu_src ? imm_val : operandB;
290278
291- // ALU
279+ // ALU - calculations karne ke liye
292280 wire signed [63 :0 ] alu_result;
293281 wire zero;
294282 alu main_alu (
@@ -299,31 +287,31 @@ module cpu_pipelined(
299287 .zero(zero)
300288 );
301289
302- // Branch Target Calculation (EX stage)
290+ // Branch ka target calculate karo
303291 wire [63 :0 ] branch_target = id_ex_pc + b_imm;
304292
305- // Branch condition checking
306- wire branch_eq = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b000 ); // BEQ
307- wire branch_ne = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b001 ); // BNE
308- wire branch_lt = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b100 ); // BLT
309- wire branch_ge = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b101 ); // BGE
293+ // Branch condition checking - konsa branch lena hai
294+ wire branch_eq = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b000 );
295+ wire branch_ne = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b001 );
296+ wire branch_lt = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b100 );
297+ wire branch_ge = id_ex_branch & (id_ex_instruction[14 :12 ] == 3'b101 );
310298
311- // branch logic
299+ // Branch lena hai ya nahi decide karo
312300 wire branch_taken = (branch_eq & zero) |
313301 (branch_ne & ~ zero) |
314302 (branch_lt & (operandA < operandB)) |
315303 (branch_ge & (operandA >= operandB));
316304
317- // Branch misprediction detection
305+ // Agar prediction galat thi to misprediction signal do
318306 assign branch_mispredicted = id_ex_branch && (branch_taken != id_ex_branch_predicted);
319307
320- // Correct address when mispredicted
308+ // Prediction galat thi to correct PC ka calculation
321309 wire [63 :0 ] correct_pc = branch_taken ? branch_target : (id_ex_pc + 4 );
322310
323- // Flush pipeline on misprediction
311+ // Pipeline flush karo jab prediction galat ho
324312 assign flush = branch_mispredicted & ~ id_ex_branch_predicted;
325313
326- // PC update with stall for exactly one cycle then force progress
314+ // PC ka update: stall, branch misprediction, ya normal increment
327315 wire [63 :0 ] pc_to_use = stall & ~ stalled_last_cycle ? pc_current :
328316 (branch_mispredicted ? correct_pc : predicted_pc);
329317 assign pc_next = pc_to_use;
@@ -363,7 +351,7 @@ module cpu_pipelined(
363351 })
364352 );
365353
366- // Data Memory
354+ // Data Memory - RAM jisme data store hota hai
367355 wire signed [63 :0 ] mem_read_data;
368356 data_memory dmem (
369357 .clk(clk),
@@ -397,12 +385,12 @@ module cpu_pipelined(
397385 })
398386 );
399387
400- // Write-Back Stage
388+ // Write-Back Stage - register mein value wapas likhne ke liye
401389 assign reg_write_data = mem_wb_mem_to_reg ? mem_wb_mem_read_data : mem_wb_alu_result;
402390 wire [4 :0 ] mem_wb_rd_fixed = mem_wb_instruction[11 :7 ];
403391 assign reg_rd = mem_wb_rd_fixed;
404392
405- // End of Program
393+ // Program khatam hone ka signal - jab NOP execute ho aur koi branch na ho
406394 assign end_program = mem_wb_nop_instruction & ~ branch_mispredicted & ~ branch_predicted;
407395
408396endmodule
0 commit comments