訊息
|
Verilog 程式:pipeline.v
`define IDLE 1'b0 // 閒置中
`define WAIT 2'b1 // 等待回應
// pipe : 單一根管子 (iReady, iMsg, iGet) 為輸入部分,(oReady, oMsg, oGet) 為輸出部分, id 為管線代號
module pipe(input clock, iReady, input [7:0] iMsg, output iGet,
output oReady, output [7:0] oMsg, input oGet, input [3:0] id);
reg oReady=0, iGet=0, state=`IDLE;
reg [7:0] iMsgReg, oMsg;
always @(posedge clock) begin
case (state)
`IDLE: begin // 閒置中
if (iReady) begin // 輸入資料已準備好
iMsgReg <= iMsg; // 儲存輸入資料
#2;
$display("%-8d:p%x iMsg=%d, iGet", $stime, id, iMsg);
iGet <= 1;
#3;
oMsg = iMsg + 1; // 設定輸出資料
#3;
oReady <= 1; // 輸出資料已準備好
$display("%-8d:p%x oMsg=%d, oReady", $stime, id, oMsg);
#3;
state <= `WAIT; // 進入等帶狀態
#2;
end
end
`WAIT:begin // 等待回應 (資料被取走)
if (oGet) begin // 資料被取走了
#2;
$display("%-8d:p%x oGet", $stime, id); // 顯示資料已經取走
oReady <= 0; // 下一筆輸出資料尚未準備好。
#3;
state <= `IDLE; // 回到閒置狀態,準備取得下一筆輸入資料
#2;
end
iGet <= 0; // 下一筆輸入資料尚未準備好。
end
endcase
end
endmodule
module pipeline; // pipeline : 多根管子連接後形成的管線
reg clock; // 時脈
wire [7:0] p1Msg, p2Msg, p3Msg; // pipe 傳遞的訊息
wire p1iGet, p2iGet, p3iGet; // pipe 輸入是否準備好了
wire p1oReady, p2oReady, p3oReady; // pipe 輸出是否準備好了
reg [7:0] iMsg=0; // pipeline 的整體輸入訊息
parameter iReady = 1'b1, oGet=1'b1; // pipeline 的整體輸入輸出是否準備好了 (隨時都準備好,這樣才會不斷驅動)。
pipe p1(clock, iReady, iMsg, p1iGet, p1oReady, p1Msg, p2iGet,1); // 第一根管子
pipe p2(clock, p1oReady, p1Msg, p2iGet, p2oReady, p2Msg, p3iGet,2); // 第二根管子
pipe p3(clock, p2oReady, p2Msg, p3iGet, p3oReady, p3Msg, oGet, 3); // 第三根管子
initial begin
clock = 0;
#100 iMsg = 10; // pipelie 的輸入資料改為 10
#100 iMsg = 20; // pipelie 的輸入資料改為 20
#100 $finish;
end
always #10 begin
clock=clock+1;
end
endmodule
Icarus 執行結果
D:\oc\cpu0p>iverilog pipeline.v -o pipeline
D:\oc\cpu0p>vvp pipeline
12 :p1 iMsg= 0, iGet
18 :p1 oMsg= 1, oReady
32 :p2 iMsg= 1, iGet
38 :p2 oMsg= 2, oReady
52 :p3 iMsg= 2, iGet
52 :p1 oGet
58 :p3 oMsg= 3, oReady
72 :p3 oGet
72 :p1 iMsg= 0, iGet
72 :p2 oGet
78 :p1 oMsg= 1, oReady
92 :p2 iMsg= 1, iGet
98 :p2 oMsg= 2, oReady
112 :p3 iMsg= 2, iGet
112 :p1 oGet
118 :p3 oMsg= 3, oReady
132 :p3 oGet
132 :p1 iMsg= 10, iGet
132 :p2 oGet
138 :p1 oMsg= 11, oReady
152 :p2 iMsg= 11, iGet
158 :p2 oMsg= 12, oReady
172 :p3 iMsg= 12, iGet
172 :p1 oGet
178 :p3 oMsg= 13, oReady
192 :p3 oGet
192 :p1 iMsg= 10, iGet
192 :p2 oGet
198 :p1 oMsg= 11, oReady
212 :p2 iMsg= 11, iGet
218 :p2 oMsg= 12, oReady
232 :p3 iMsg= 12, iGet
232 :p1 oGet
238 :p3 oMsg= 13, oReady
252 :p3 oGet
252 :p1 iMsg= 20, iGet
252 :p2 oGet
258 :p1 oMsg= 21, oReady
272 :p2 iMsg= 21, iGet
278 :p2 oMsg= 22, oReady
292 :p3 iMsg= 22, iGet
292 :p1 oGet
298 :p3 oMsg= 23, oReady
歷史版本
- pipe — 不太像 pipeline 的 pipeline。
- pipeline1 — 展示如何設計 pipeline 的原理, Ready, Ack 版。
Facebook
|
Post preview:
Close preview