-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatapath.v
166 lines (155 loc) · 4.47 KB
/
datapath.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 2017/11/02 15:12:22
// Design Name:
// Module Name: datapath
// Project Name:
// Target Devices:
// Tool Versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module datapath(
input wire clk,rst,
//fetch stage
output wire[31:0] pcF,
input wire[31:0] instrF,
//decode stage
input wire pcsrcD,branchD,
input wire jumpD,
output wire equalD,
output wire [31:0] instrD,
input wire is_IMM, //判断是否是立即数
//execute stage
input wire memtoregE,
input wire alusrcE,regdstE,
input wire regwriteE,
input wire[4:0] alucontrolE,
output wire flushE,
//mem stage
input wire memtoregM,
input wire regwriteM,
output wire[31:0] aluoutM,writedataM,
input wire[31:0] readdataM,
input wire hilo_writeM,
input wire hi_to_regM,
input wire lo_to_regM,
//writeback stage
input wire memtoregW,
input wire regwriteW,
input wire hilo_writeW
);
//fetch stage
wire stallF;
//FD
wire [31:0] pcnextFD,pcnextbrFD,pcplus4F,pcbranchD;
//decode stage
wire [31:0] pcplus4D;
wire forwardaD,forwardbD;
wire [4:0] rsD,rtD,rdD;
wire flushD,stallD;
wire [31:0] signimmD,signimmshD;
wire [31:0] srcaD,srca2D,srcbD,srcb2D;
wire [4:0] saD; //因SLL,SRL,SRA指令加入
//execute stage
wire [1:0] forwardaE,forwardbE;
wire [4:0] rsE,rtE,rdE;
wire [4:0] writeregE;
wire [31:0] signimmE;
wire [31:0] srcaE,srca2E,srcbE,srcb2E,srcb3E;
wire [31:0] aluoutE;
wire [4:0] saE;
//mem stage
wire [4:0] writeregM;
wire [31:0] hi_outM;
wire [31:0] lo_outM;
wire [31:0] alu_hilo_dataM;
//writeback stage
wire [4:0] writeregW;
wire [31:0] aluoutW,readdataW,resultW;
wire [31:0] alu_hilo_dataW;
//hazard detection
hazard h(
//fetch stage
stallF,
//decode stage
rsD,rtD,
branchD,
forwardaD,forwardbD,
stallD,
//execute stage
rsE,rtE,
writeregE,
regwriteE,
memtoregE,
forwardaE,forwardbE,
flushE,
//mem stage
writeregM,
regwriteM,
memtoregM,
//write back stage
writeregW,
regwriteW
);
//next PC logic (operates in fetch an decode)
mux2 #(32) pcbrmux(pcplus4F,pcbranchD,pcsrcD,pcnextbrFD);
mux2 #(32) pcmux(pcnextbrFD,
{pcplus4D[31:28],instrD[25:0],2'b00},
jumpD,pcnextFD);
//regfile (operates in decode and writeback)
regfile rf(clk,regwriteW,rsD,rtD,writeregW,resultW,srcaD,srcbD);
//fetch stage logic
pc #(32) pcreg(clk,rst,~stallF,pcnextFD,pcF);
adder pcadd1(pcF,32'b100,pcplus4F);
//decode stage
flopenr #(32) r1D(clk,rst,~stallD,pcplus4F,pcplus4D);
flopenrc #(32) r2D(clk,rst,~stallD,flushD,instrF,instrD);
signext se(instrD[15:0],is_IMM,signimmD);
sl2 immsh(signimmD,signimmshD);
adder pcadd2(pcplus4D,signimmshD,pcbranchD);
mux2 #(32) forwardamux(srcaD,aluoutM,forwardaD,srca2D);
mux2 #(32) forwardbmux(srcbD,aluoutM,forwardbD,srcb2D);
eqcmp comp(srca2D,srcb2D,equalD);
assign rsD = instrD[25:21];
assign rtD = instrD[20:16];
assign rdD = instrD[15:11];
assign saD = instrD[10:6];
//execute stage
floprc #(32) r1E(clk,rst,flushE,srcaD,srcaE);
floprc #(32) r2E(clk,rst,flushE,srcbD,srcbE);
floprc #(32) r3E(clk,rst,flushE,signimmD,signimmE);
floprc #(5) r4E(clk,rst,flushE,rsD,rsE);
floprc #(5) r5E(clk,rst,flushE,rtD,rtE);
floprc #(5) r6E(clk,rst,flushE,rdD,rdE);
floprc #(5) r7E(clk,rst,flushE,saD,saE);
mux3 #(32) forwardaemux(srcaE,resultW,aluoutM,forwardaE,srca2E);
mux3 #(32) forwardbemux(srcbE,resultW,aluoutM,forwardbE,srcb2E);
mux2 #(32) srcbmux(srcb2E,signimmE,alusrcE,srcb3E);
alu alu(srca2E,srcb3E,alucontrolE,aluoutE,saE);
mux2 #(5) wrmux(rtE,rdE,regdstE,writeregE);
//mem stage
flopr #(32) r1M(clk,rst,srcb2E,writedataM);
flopr #(32) r2M(clk,rst,aluoutE,aluoutM);
flopr #(5) r3M(clk,rst,writeregE,writeregM);
hilo_reg hilo(clk,rst,hilo_writeM,aluoutM,aluoutM,hi_outM,lo_outM);
// assign aluoutM = ({hi_to_regM,lo_to_regM}==2'b00) ? aluoutM :
// ({hi_to_regM,lo_to_regM}==2'b10) ? hi_outM :
// ({hi_to_regM,lo_to_regM}==2'b01) ? lo_outM : aluoutM;
mux3 #(32) alu_hilomuxM(aluoutM,hi_outM,lo_outM,{lo_to_regM,hi_to_regM},alu_hilo_dataM);
//writeback stage
flopr #(32) r1W(clk,rst,aluoutM,aluoutW);
flopr #(32) r2W(clk,rst,readdataM,readdataW);
flopr #(5) r3W(clk,rst,writeregM,writeregW);
flopr #(32) r4W(clk,rst,alu_hilo_dataM,alu_hilo_dataW);
mux2 #(32) resmux(alu_hilo_dataW,readdataW,memtoregW,resultW);
endmodule