Pipeline3 管線設計範例

Verilog

基本語法

型態

全域變數

基本元件

多樣的寫法

指定

assign

always

initial

運算式

分枝

迴圈

模組

函數

Task

陣列

輸出入

觀察

真值表

測試程式

訊息顯示

注意事項

模擬程序

硬體工程

程式範例

Xor

Xor3

全加器

加法器

加減器

快速加法器

乘法器

ALU

閂鎖器

脈衝偵測

計數器

多工器

暫存器群

記憶體

延遲問題

浮點數

狀態機

程式計數器

CPU0-Mini

CPU0

pipeline

工具

QuartusII

Icarus

Veritek

訊息

相關網站

參考文獻

最新修改

簡體版

English

Verilog 程式:pipeline3.v

`define IDLE 1'b0     // 閒置中
`define WAIT 2'b1     // 等待回應

// pipe : 單一根管子 (iReady, iMsg, iGet) 為輸入部分,(oReady, oMsg, oGet) 為輸出部分, id 為管線代號
module pipe(input clock, reset, iReady, input [7:0] iMsg, output iGet, 
            output oReady, output [7:0] oMsg, input oGet, input [3:0] id);
reg oReady, iGet, state;
reg [7:0] iMsgReg, oMsg;
    always @(posedge clock) begin
        if (reset) begin oReady=0; iGet=0; state=`IDLE; end 
        else 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, reset; // 時脈
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, reset, iReady,     iMsg, p1iGet,  p1oReady, p1Msg, p2iGet,1); // 第一根管子
pipe p2(clock, reset, p1oReady, p1Msg, p2iGet, p2oReady, p2Msg, p3iGet,2); // 第二根管子
pipe p3(clock, reset, p2oReady, p2Msg, p3iGet, p3oReady, p3Msg, oGet,     3); // 第三根管子

initial begin
  reset = 1;
  clock = 0;
  #100 reset = 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 pipeline3.v -o pipeline3

D:\oc\cpu0p>vvp pipeline3
112     :p1 iMsg=  0, iGet
118     :p1 oMsg=  1, oReady
132     :p2 iMsg=  1, iGet
138     :p2 oMsg=  2, oReady
152     :p3 iMsg=  2, iGet
152     :p1 oGet
158     :p3 oMsg=  3, oReady
172     :p3 oGet
172     :p1 iMsg=  0, iGet
172     :p2 oGet
178     :p1 oMsg=  1, oReady
192     :p2 iMsg=  1, iGet
198     :p2 oMsg=  2, oReady
212     :p3 iMsg=  2, iGet
212     :p1 oGet
218     :p3 oMsg=  3, oReady
232     :p3 oGet
232     :p1 iMsg= 10, iGet
232     :p2 oGet
238     :p1 oMsg= 11, oReady
252     :p2 iMsg= 11, iGet
258     :p2 oMsg= 12, oReady
272     :p3 iMsg= 12, iGet
272     :p1 oGet
278     :p3 oMsg= 13, oReady
292     :p3 oGet
292     :p1 iMsg= 10, iGet
292     :p2 oGet
298     :p1 oMsg= 11, oReady
312     :p2 iMsg= 11, iGet
318     :p2 oMsg= 12, oReady
332     :p3 iMsg= 12, iGet
332     :p1 oGet
338     :p3 oMsg= 13, oReady
352     :p3 oGet
352     :p1 iMsg= 20, iGet
352     :p2 oGet
358     :p1 oMsg= 21, oReady
372     :p2 iMsg= 21, iGet
378     :p2 oMsg= 22, oReady
392     :p3 iMsg= 22, iGet
392     :p1 oGet
398     :p3 oMsg= 23, oReady

歷史版本

  1. pipe — 不太像 pipeline 的 pipeline。
  2. pipeline1 — 展示如何設計 pipeline 的原理, Ready, Ack 版。
  3. pipeline2 — 展示如何設計 pipeline 的原理, 直接初始化版。

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License