PipelineRegisters.v 6.3 KB
module PCregister(clk, stall, flush, in_pc, out_nextpc);
input clk, stall, flush;

input[31:0] in_pc;
output reg[31:0] out_nextpc;

reg[31:0] PC;
reg tempstall;

initial begin
	PC = 32'h00000000;
	tempstall = 1'b1;
end

always @(posedge clk) begin
	if(tempstall == 1'b1) tempstall = 1'b0;
	else if(stall == 1'b0 && flush == 1'b0) PC = PC+4;
	out_nextpc = PC;
end
always @(negedge flush) begin
	if(tempstall == 1'b0) PC = in_pc;
	tempstall = 1'b1;
end
endmodule


module IF_ID(clk, stall, flush, in_instruction, in_PC_4, 
	out_instruction, out_PC_4);
input clk, stall, flush;

input[31:0] in_instruction, in_PC_4;

output reg[31:0] out_instruction, out_PC_4;

reg[31:0] temp_instruction, temp_PC_4;
	
always @(posedge clk) begin
	if(stall == 1'b1) begin
		out_instruction <= temp_instruction;
		out_PC_4 <= temp_PC_4;
	end else if(flush == 1'b1) begin
		out_instruction <= 32'h00000000;
		out_PC_4 <= 32'h00000000;
	end else begin
		out_instruction <= in_instruction;
		out_PC_4 <= in_PC_4;
	end
end
always @(posedge stall) begin
	temp_instruction <= out_instruction;
	temp_PC_4 <= out_PC_4;
end
endmodule


module ID_EX(clk, flush, 
	in_writereg_num, in_readreg_num1, in_readreg_num2, in_regwrite, in_alusrc, in_aluctrl, in_memread, in_memwrite, in_memtoreg, in_branch, in_jump, in_jumpreg, in_link, 
	in_readdata1, in_readdata2, in_extenddata, in_PC_4, in_tempPCjump, 
	out_writereg_num, out_readreg_num1, out_readreg_num2, out_regwrite, out_alusrc, out_aluctrl, out_memread, out_memwrite, out_memtoreg, out_branch, out_jump, out_jumpreg, out_link, 
	out_readdata1, out_readdata2, out_extenddata, out_PC_4, out_tempPCjump);
input clk, flush;

input[4:0] in_writereg_num, in_readreg_num1, in_readreg_num2;
input in_regwrite, in_alusrc, in_memread, in_memwrite, in_memtoreg, in_jump, in_jumpreg, in_link;
input[3:0] in_aluctrl;
input[2:0] in_branch;
input[31:0] in_readdata1, in_readdata2, in_extenddata, in_PC_4, in_tempPCjump;

output reg[4:0] out_writereg_num, out_readreg_num1, out_readreg_num2;
output reg out_regwrite, out_alusrc, out_memread, out_memwrite, out_memtoreg, out_jump, out_jumpreg, out_link;
output reg[3:0] out_aluctrl;
output reg[2:0] out_branch;
output reg[31:0] out_readdata1, out_readdata2, out_extenddata, out_PC_4, out_tempPCjump;

always @(posedge clk) begin
	if(flush == 1'b1) begin
		out_writereg_num <= 5'b00000;
		out_readreg_num1 <= 5'b00000;
		out_readreg_num2 <= 5'b00000;

		out_regwrite <= 1'b0;
		out_alusrc <= 1'b0;
		out_aluctrl <= 4'b0000;
		out_memread <= 1'b0;
		out_memwrite <= 1'b0;
		out_memtoreg <= 1'b0;
		out_branch <= 3'b000;
		out_jump <= 1'b0;
		out_jumpreg <= 1'b0;
		out_link <= 1'b0;

		out_readdata1 <= 32'h00000000;
		out_readdata2 <= 32'h00000000;
		out_extenddata <= 32'h00000000;
		out_PC_4 <= 32'h00000000;
		out_tempPCjump <= 32'h00000000;
	end else begin
		out_writereg_num <= in_writereg_num;
		out_readreg_num1 <= in_readreg_num1;
		out_readreg_num2 <= in_readreg_num2;

		out_regwrite <= in_regwrite;
		out_alusrc <= in_alusrc;
		out_aluctrl <= in_aluctrl;
		out_memread <= in_memread;
		out_memwrite <= in_memwrite;
		out_memtoreg <= in_memtoreg;
		out_branch <= in_branch;
		out_jump <= in_jump;
		out_jumpreg <= in_jumpreg;
		out_link <= in_link;

		out_readdata1 <= in_readdata1;
		out_readdata2 <= in_readdata2;
		out_extenddata <= in_extenddata;
		out_PC_4 <= in_PC_4;
		out_tempPCjump <= in_tempPCjump;
	end
end
endmodule


module EX_MEM(clk, flush, in_writereg_num, in_regwrite, in_memread, in_memwrite, in_memtoreg, in_branch, in_jump, in_link, 
	in_aluresult, in_mem_writedata, in_PC_4, in_PCjump, in_tempPCbranch, 
	out_writereg_num, out_regwrite, out_memread, out_memwrite, out_memtoreg, out_branch, out_jump, out_link, 
	out_aluresult, out_mem_writedata, out_PC_4, out_PCjump, out_tempPCbranch);
input clk, flush;

input[4:0] in_writereg_num;
input in_regwrite, in_memread, in_memwrite, in_memtoreg, in_branch, in_jump, in_link;
input[31:0] in_aluresult, in_mem_writedata, in_PC_4, in_PCjump, in_tempPCbranch;
output reg[4:0] out_writereg_num;
output reg out_regwrite, out_memread, out_memwrite, out_memtoreg, out_branch, out_jump, out_link;
output reg[31:0] out_aluresult, out_mem_writedata, out_PC_4, out_PCjump, out_tempPCbranch;

always @(posedge clk) begin
	if(flush == 1'b1) begin
		out_writereg_num <= 5'b00000;
		out_regwrite <= 1'b0;
		out_memread <= 1'b0;
		out_memwrite <= 1'b0;
		out_memtoreg <= 1'b0;
		out_branch <= 1'b0;
		out_jump <= 1'b0;
		out_link <= 1'b0;

		out_aluresult <= 32'h00000000;
		out_mem_writedata <= 32'h00000000;
		out_PC_4 <= 32'h00000000;
		out_PCjump <= 32'h00000000;
		out_tempPCbranch <= 32'h00000000;
	end else begin
		out_writereg_num <= in_writereg_num;
		out_regwrite <= in_regwrite;
		out_memread <= in_memread;
		out_memwrite <= in_memwrite;
		out_memtoreg <= in_memtoreg;
		out_branch <= in_branch;
		out_jump <= in_jump;
		out_link <= in_link;

		out_aluresult <= in_aluresult;
		out_mem_writedata <= in_mem_writedata;
		out_PC_4 <= in_PC_4;
		out_PCjump <= in_PCjump;
		out_tempPCbranch <= in_tempPCbranch;
	end
end
endmodule


module MEM_WB(clk, flush, in_writereg_num, in_regwrite, in_memtoreg, in_branch, in_jump, in_link, in_aluresult, in_memreaddata, in_PC_4, in_PCjump, in_PCbranch, 
	out_writereg_num, out_regwrite, out_memtoreg, out_branch, out_jump, out_link, out_aluresult, out_memreaddata, out_PC_4, out_PCjump, out_PCbranch);
input clk, flush;

input[4:0] in_writereg_num;
input in_regwrite, in_memtoreg, in_branch, in_jump, in_link;
input[31:0] in_aluresult, in_memreaddata, in_PC_4, in_PCjump, in_PCbranch;
output reg[4:0] out_writereg_num;
output reg out_regwrite, out_memtoreg, out_branch, out_jump, out_link;
output reg[31:0] out_aluresult, out_memreaddata, out_PC_4, out_PCjump, out_PCbranch;

always @(posedge clk) begin
	if(flush == 1'b1) begin
		out_writereg_num <= 5'b00000;
		out_regwrite <= 1'b0;
		out_memtoreg <= 1'b0;
		out_branch <= 1'b0;
		out_jump <= 1'b0;
		out_link <= 1'b0;

		out_aluresult <= 32'h00000000;
		out_memreaddata <= 32'h00000000;
		out_PC_4 <= 32'h00000000;
		out_PCjump <= 32'h00000000;
		out_PCbranch <= 32'h00000000;
	end else begin
		out_writereg_num <= in_writereg_num;
		out_regwrite <= in_regwrite;
		out_memtoreg <= in_memtoreg;
		out_branch <= in_branch;
		out_jump <= in_jump;
		out_link <= in_link;

		out_aluresult <= in_aluresult;
		out_memreaddata <= in_memreaddata;
		out_PC_4 <= in_PC_4;
		out_PCjump <= in_PCjump;
		out_PCbranch <= in_PCbranch;
	end
end
endmodule