This commit is contained in:
yunlongLi 2024-11-07 19:29:17 +08:00
parent 84871abdab
commit 47b2ee8d25
18 changed files with 626 additions and 0 deletions

75
course/hitArchi/cpu_top.v Normal file
View File

@ -0,0 +1,75 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : cpu_top
//--Function: cpu to contains the whole module
//=======================================
module cpu_top (
//==========================< 端口 >=========================
input clk,
input rst_n,
output [32 -1:0] debug_wb_pc,
output debug_wb_rf_wen,
output [4:0] debug_wb_rf_addr,
output [32 -1:0] debug_wb_rf_wdata
);
//==========================< start define the connection lines >=========================
//==========================< IF part >=========================
wire [32 -1:0] IF_NPC;
wire [32 -1:0] IF_PC;
wire [32 -1:0] IF_INS;
wire [32 -1:0] IF_ADDNPC;
// wire [32 -1:0] signal_1;
// wire [32 -1:0] signal_2;
// wire [4 -1:0] base;
// wire stall;
//==========================< ID part >=========================
wire [32 -1:0] ID_NPC;
wire [32 -1:0] ID_INS;
wire [32 -1:0] ID_IMM;
wire [32 -1:0] ID_RS;
wire [32 -1:0] ID_RT;
wire [32 -1:0] ID_RC;
//==========================< EX part >=========================
wire [32 -1:0] EX_NPC;
wire [32 -1:0] EX_INS;
wire [32 -1:0] EX_RS;
wire [32 -1:0] EX_RT;
wire [32 -1:0] EX_A;
wire [32 -1:0] EX_B;
wire [32 -1:0] EX_IMM;
wire EX_JUDG;
wire [32 -1:0] EX_ALUOUT;
wire [32 -1:0] EX_PC;
//==========================< MEM part >=========================
wire [32 -1:0] MEM_INS;
wire MEM_JUDG;
wire [32 -1:0] MEM_ALUOUT;
wire [32 -1:0] MEM_RT;
wire [32 -1:0] MEM_MEMOUT;
wire [32 -1:0] MME_PC;
//==========================< WB part >=========================
wire [32 -1:0] WB_INS;
wire [32 -1:0] WB_MEMOUT;
wire [32 -1:0] WB_ALUOUT;
wire [32 -1:0] WB_DATA;
wire [32 -1:0] WB_PC;
wire [5 -1:0] WB_ADDR;
wire WB_ABLE;
//==========================< start link module >=========================
assign debug_wb_pc = WB_PC;
assign debug_wb_rf_wen = WB_ABLE;
assign debug_wb_rf_addr = WB_ADDR;
assign debug_wb_rf_wdata= WB_DATA
//==========================< IF part >=========================
endmodule

87
course/hitArchi/ex/alu.v Normal file
View File

@ -0,0 +1,87 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : alu
//--Function: alu caculation
//=======================================
`define ADD 6'b100000
`define SUB 6'b100010
`define OR 6'b100101
`define AND 6'b100100
`define XOR 6'b100110
`define SLT 6'b101010 //比较A<B则返回1否则返回0
`define MOVZ 6'b001010 //MOVZ指令B为0则返回A
`define SLL 6'b000000
module alu (
//==========================< 端口 >=========================
input [32 -1:0] a,
input [32 -1:0] b,
input [5 -1:0] sll,
input [6 -1:0] card,
input [6 -1:0] opcode,
output [32 -1:0] f
);
//==========================< 端口 >=========================
reg [32 -1:0] add_result;
reg [32 -1:0] sub_result;
reg [32 -1:0] or_result;
reg [32 -1:0] and_result;
reg [32 -1:0] xor_result;
reg [32 -1:0] slt_result;
reg [32 -1:0] movz_result;
reg [32 -1:0] lw_result;
reg [32 -1:0] sw_result;
reg [32 -1:0] bne_result;
reg [32 -1:0] jmp_result;
reg [32 -1:0] sll_result;
//=========================================================
//-- result
//=========================================================
always @(*) begin
case (opcode)
6'b00_0000: begin
case (card)
`ADD: assign add_result = a + b;
`SUB: assign sub_result = a - b;
`OR : assign or_result = a | b;
`AND: assign and_result = a & b;
`XOR: assign xor_result = a ^ b;
`SLT: assign slt_result = (a<b)? 1:0;
`MOVZ: assign movz_result= (b==0) A:0;
`SLL: assign sll_result = b << sll;
default: ;
endcase
end
6'b10_1011: begin
assign sw_result = a + b;
end
6'b10_0011: begin
assign lw_result = a + b;
end
6'b00_0101: begin
assign bne_result= a + (b<<2);
end
6'b00_0010: begin
assign jmp_result={a[31:28],b[25:0]<<2}
end
default: ;
endcase
end
assign F = ({32{opcode==6'b000000 && Card == `ADD}} & add_result) |
({32{opcode==6'b000000 && Card == `SUB}} & sub_result) |
({32{opcode==6'b000000 && Card == `OR}} & or_result) |
({32{opcode==6'b000000 && Card == `AND}} & and_result) |
({32{opcode==6'b000000 && Card == `XOR}} & xor_result) |
({32{opcode==6'b000000 && Card == `SLT}} & slt_result) |
({32{opcode==6'b000000 && Card == `MOVZ}} & movz_result) |
({32{opcode==6'b000000 && Card == `SLL}} & sll_result) |
({32{opcode==6'b101011}} & sw_result) |
({32{opcode==6'b100011}} & lw_result) |
({32{opcode==6'b000101}} & bne_result) |
({32{opcode==6'b000010}} & jmp_result);
endmodule

View File

@ -0,0 +1,44 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : ex_mem
//--Function: register the result
//=======================================
module ex_mem (
//==========================< 端口 >=========================
input clk,
input rst_n,
input wdata_3_1,
input [32 -1:0] wdata_3_2,
input [32 -1:0] wdata_3_3,
input [32 -1:0] wdata_3_4,
input [32 -1:0] wdata_3_5,
output reg rdata_3_1,
output reg [32 -1:0] rdata_3_2,
output reg [32 -1:0] rdata_3_3,
output reg [32 -1:0] rdata_3_4,
output reg [32 -1:0] rdata_3_5
);
//=========================================================
//-- rdata_3_1/2/3/4/5
//=========================================================
always @(posedge clk ) begin
if (!rst_n) begin
rdata_3_1 <= 0;
rdata_3_2 <= 0;
rdata_3_3 <= 0;
rdata_3_4 <= 0;
rdata_3_5 <= 0;
end else begin
rdata_3_1 <= wdata_3_1;
rdata_3_2 <= wdata_3_2;
rdata_3_3 <= wdata_3_3;
rdata_3_4 <= wdata_3_4;
rdata_3_5 <= wdata_3_5;
end
end
endmodule

View File

@ -0,0 +1,18 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : mux_1
//--Function:
//=======================================
module mux_1 (
//==========================< 端口 >=========================
input [32 -1:0] d0,
input [32 -1:0] d1,
input [5:0] select,
output [32 -1:0] out
);
assign out = (select==6'b00_0000 || select==6'b10_1011|| select==6'b10_0011) ? d1 : d0;
endmodule

View File

@ -0,0 +1,20 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : mux_2
//--Function:
//=======================================
module mux_2 (
//==========================< 端口 >=========================
input [32 -1:0] d0,
input [32 -1:0] d1,
input [5:0] select,
output [32 -1:0] out
);
//=========================================================
//-- out:
//=========================================================
assign out = (select==6'b00_0000) ? d1 : d0;
endmodule

21
course/hitArchi/ex/zero.v Normal file
View File

@ -0,0 +1,21 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : zero
//--Function: judge whether it is zero
//=======================================
module zero (
//==========================< 端口 >=========================
input [32 -1:0] a,
input [32 -1:0] b,
input [5:0] opcode,
output judge
);
//=========================================================
//-- judge
//=========================================================
assign judge = (opcode==6'b00_0000 || opcode==6'b10_1011 ||opcode==6'b10_0011 ||a==b) ? 0 : 1;
endmodule

View File

@ -0,0 +1,17 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : externder
//--Function: extend the width
//=======================================
module externder (
//==========================< 端口 >=========================
input [25:0] imm,
input [5:0] opcode,
output [32 -1:0] extend_result
);
assign extend_result = (opcode==6'b00_0010)? {{6{imm[25]}},imm[25:0]} : {{16{imm[15]}},imm[15:0]};
endmodule

View File

@ -0,0 +1,47 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : id_ex
//--Function: decode the instruc and execute the instruc
//=======================================
module id_ex (
//==========================< 端口 >=========================
input clk,
input rst_n,
input [32 -1:0] wdata_2_1,
input [32 -1:0] wdata_2_2,
input [32 -1:0] wdata_2_3,
input [32 -1:0] wdata_2_4,
input [32 -1:0] wdata_2_5,
input [32 -1:0] wdata_2_6,
output reg [32 -1:0] rdata_2_1,
output reg [32 -1:0] rdata_2_2,
output reg [32 -1:0] rdata_2_3,
output reg [32 -1:0] rdata_2_4,
output reg [32 -1:0] rdata_2_5,
output reg [32 -1:0] rdata_2_6
);
//=========================================================
//-- rdata_2_1/2/3/4/5/6
//=========================================================
always @(posedge clk ) begin
if (!rst_n) begin
rdata_2_1 <= 0;
rdata_2_2 <= 0;
rdata_2_3 <= 0;
rdata_2_4 <= 0;
rdata_2_5 <= 0;
rdata_2_6 <= 0;
end else begin
rdata_2_1 <= wdata_2_1;
rdata_2_2 <= wdata_2_2;
rdata_2_3 <= wdata_2_3;
rdata_2_4 <= wdata_2_4;
rdata_2_5 <= wdata_2_5;
rdata_2_6 <= wdata_2_6;
end
end
endmodule

44
course/hitArchi/id/regs.v Normal file
View File

@ -0,0 +1,44 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : regs
//--Function: read the instruction
//=======================================
module regs (
//==========================< 端口 >=========================
input clk,
input [4:0] raddr1,
output[32-1:0] rdata1,
input [4:0] raddr2,
output[32-1:0] rdata2,
input we,
input [4:0] waddr,
output[32-1:0] wdata
);
//==========================< 端口 >=========================
reg [32 -1:0] reg_file[32 -1:0];
initial begin
$readmemh("xx/xx",reg_file);
end
//=========================================================
//-- rdata1/2
//=========================================================
assign rdata1 = reg_file[raddr1];
assign rdata2 = reg_file[raddr2];
//=========================================================
//-- wdata
//=========================================================
always @(posedge clk) begin
if (we && !waddr) begin
reg_file[waddr] <= wdata;
end else begin
reg_file[waddr] <= reg_file[waddr];
end
end
endmodule

18
course/hitArchi/if/add.v Normal file
View File

@ -0,0 +1,18 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : add
//--Function: the althomie add
//=======================================
module add (
//==========================< 端口 >=========================
input wire [32 -1:0] pc,
output wire[32 -1:0] add_result
);
//==========================< 参数 >=========================
localparam PC_SHIFT = 4;
assign add_result = pc + PC_SHIFT;
endmodule

View File

@ -0,0 +1,34 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : IF_ID
//--Function: meet the instru fetch and instruc decoder
//=======================================
module if_id (
//==========================< 端口 >=========================
input wire clk,
input wire rst_n,
input wire [32 -1:0] wdata_1_1,
input wire [32 -1:0] wdata_1_2,
input wire [32 -1:0] wdata_1_3,
output reg [32 -1:0] rdata_1_1,
output reg [32 -1:0] rdata_1_2,
output reg [32 -1:0] rdata_1_3
);
//=========================================================
//-- rdata_1_1/2/3
//=========================================================
always @(posedge clk) begin
if (!rst_n) begin
rdata_1_1 <= 0;
rdata_1_2 <= 0;
rdata_1_3 <= 0;
end else begin
rdata_1_1 <= wdata_1_1;
rdata_1_2 <= wdata_1_2;
rdata_1_3 <= wdata_1_3;
end
end
endmodule

View File

@ -0,0 +1,28 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : ins_reg
//--Function: instrction register
//=======================================
module ins_reg (
//==========================< 端口 >=========================
input wire [32 -1:0] ins_addr,
output wire[32 -1:0] ins_out
);
//==========================< 端口 >=========================
reg [32 -1:0] reg [256 -1:0];
//=========================================================
//-- read the instrc
//=========================================================
initial begin
//$readmemh("F:/OneDrive/Pang/vivado/computer_structure/lab1_env_new/lab_1/lab_1.data/base_inst_data",regs);
//$readmemh("xxx/xxx",regs);
end
//=========================================================
//-- ins_out:read the instruction
//=========================================================
assign ins_out = regs[ins_addr/4];
endmodule

View File

@ -0,0 +1,21 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : npc_mux
//--Function: next_pc mux between add_pc and jmp_pc;
//=======================================
module npc_mux (
//==========================< 端口 >=========================
input wire [32 -1:0] ori_npc,
input wire [32 -1:0] jmp_pc,
input wire select,
output wire [32 -1:0] npc;
);
//=========================================================
//-- npc: do the mux
//=========================================================
assign npc = (select?)
endmodule

31
course/hitArchi/if/pc.v Normal file
View File

@ -0,0 +1,31 @@
//=======================================
//--Author : colonel
//--Date 1107
//--Module : pc
//--Function: output the pc
//=======================================
module pc (
//==========================< 端口 >=========================
input clk,
input rst_n,
input [32 -1:0] next_pc,
output [32 -1:0] pc
);
//==========================< 端口 >=========================
reg [32 -1:0] pc_r;
//=========================================================
//-- pc:flop-flop the register
//=========================================================
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
pc_r <= 0;
end else begin
pc_r <= next_pc;
end
end
assign pc = pc_r;
endmodule

View File

@ -0,0 +1,34 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : data_regs
//--Function: store data
//=======================================
module data_regs (
//==========================< 端口 >=========================
input clk,
input [5:0] opcode,
input [32 -1:0] data_reg_wdata,
input [32 -1:0] data_reg_addr,
output [32 -1:0] data_reg_rdata
);
//==========================< 端口 >=========================
reg [32 -1:0] regs[256 -1:0];
initial begin
$readmemh("xxx/xxx",regs);
end
//=========================================================
//-- data_reg_rdata
//=========================================================
assign data_reg_rdata = regs[data_reg_addr/4];
always @(posedge clk ) begin
if (opcode==6'b10_1011) begin
regs[data_reg_addr/4] <= data_reg_wdata;
end
end
endmodule

View File

@ -0,0 +1,51 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : mem_wb
//--Function:
//=======================================
`define ALU = 6'b000000
`define LW = 6'b100011
module mem_wb (
//==========================< 端口 >=========================
input clk,
input rst_n,
input [32 -1:0] wdata_4_1,
input [32 -1:0] wdata_4_2,
input [32 -1:0] wdata_4_3,
input [32 -1:0] wdata_4_4,
output reg wb_enable,
output reg [32 -1:0] rdata_4_1,
output reg [32 -1:0] rdata_4_2,
output reg [32 -1:0] rdata_4_3,
output reg [32 -1:0] rdata_4_4
);
//==========================< 端口 >=========================
wire [5:0] opcode = wdata_4_3[31:26];
wire [5:0] aluFunc= wdata_4_3[5:0];
wire [4:0] rt = wdata_4_3[20:16];
//=========================================================
//-- rdata_4_1/2/3/4
//=========================================================
always @(posedge clk ) begin
if (!rst_n) begin
rdata_4_1 <= 0;
rdata_4_2 <= 0;
rdata_4_3 <= 0;
rdata_4_4 <= 0;
wb_enable <= 0;
end else begin
rdata_4_1 <= wdata_4_1;
rdata_4_2 <= wdata_4_2;
rdata_4_3 <= wdata_4_3;
rdata_4_4 <= wdata_4_4;
wb_enable <= (opcode== `LW || (opcode== `ALU && (aluFunc== `MOVZ && rt!=0))) || (opcode == `ALU && (ALUfunc == `MOVZ && wdata_4_2!=0)) && wdata_4_3!= 32'h00000000 ? 1 : 0;
end
end
endmodule

View File

@ -0,0 +1,18 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : wb_mux
//--Function:
//=======================================
module wb_mux(
//==========================< 端口 >=========================
input [32 -1:0] d0,
input [32 -1:0] d1,
input [5:0] select,
output[32 -1:0] out
);
assign out = (select==6'b100011) ? d0 : d1;
endmodule

View File

@ -0,0 +1,18 @@
//=======================================
//--Author : colonel
//--Date 11-07
//--Module : wb_mux_2
//--Function:
//=======================================
module wb_mux_2 (
//==========================< 端口 >=========================
input [4:0] a,
input [4:0] b,
input [5:0] opcode,
output[4:0] result
);
assign result = (opcode==6'b100011) ? a : b;
endmodule