Pipeline5

Verilog

基本語法

型態

全域變數

基本元件

多樣的寫法

指定

assign

always

initial

運算式

分枝

迴圈

模組

函數

Task

陣列

輸出入

觀察

真值表

測試程式

訊息顯示

注意事項

模擬程序

硬體工程

程式範例

Xor

Xor3

全加器

加法器

加減器

快速加法器

乘法器

ALU

閂鎖器

脈衝偵測

計數器

多工器

暫存器群

記憶體

延遲問題

浮點數

狀態機

程式計數器

CPU0-Mini

CPU0

pipeline

工具

QuartusII

Icarus

Veritek

訊息

相關網站

參考文獻

最新修改

簡體版

English

Verilog 程式:pipeline.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;

    task taskPipe(input reset, inout state, input iReady, input [7:0] iMsg, inout [7:0] iMsgReg, output iGet, oReady, output [7:0] oMsg, input oGet);
        if (reset) begin oReady=0; iGet=0; state=`IDLE; end 
        else case (state) 
            `IDLE: begin // 閒置中
                if (iReady) begin // 輸入資料已準備好
                    iMsgReg <= iMsg; // 儲存輸入資料
                    iGet <= 1;
                    state <= `WAIT; // 進入等帶狀態
                    $display("%-8d:p%x iMsg=%d, iGet", $stime, id, iMsg);
                    #1 oMsg <= iMsg + 1; // 設定輸出資料
                    #2;
                    $display("%-8d:p%x oMsg=%d, oReady", $stime, id, oMsg);
                    oReady <= 1; // 這裏有修改 *****
                end else 
                    $display("%-8d:p%x IDLE, not iReady", $stime, id);
            end
            `WAIT:begin // 等待回應 (資料被取走)
                if (oGet) begin // 資料被取走了
                    oReady <= 0; // 下一筆輸出資料尚未準備好。
                    state <= `IDLE; // 回到閒置狀態,準備取得下一筆輸入資料
                    #2;
                    $display("%-8d:p%x oGet", $stime, id); // 顯示資料已經取走
                end else begin
                    oReady <= 1; // 這裏有修改 *****
                     $display("%-8d:p%x WAIT, oReady, not oGet", $stime, id);
                end
                iGet <= 0;  // 下一筆輸入資料尚未準備好。
            end
        endcase
    endtask

    always @(posedge clock) begin
        taskPipe(reset, state, iReady, iMsg, iMsgReg, iGet, oReady, oMsg, oGet);
    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
  #200 $finish;
end

always #10 begin
  clock=clock+1;
end

endmodule

Icarus 模擬結果

D:\oc\pipe>iverilog pipeline.v -o pipeline

D:\oc\pipe>vvp pipeline
110     :p1 iMsg=  0, iGet
110     :p2 IDLE, not iReady
110     :p3 IDLE, not iReady
113     :p1 oMsg=  1, oReady
130     :p2 IDLE, not iReady
130     :p3 IDLE, not iReady
130     :p1 WAIT, oReady, not oGet
150     :p2 iMsg=  1, iGet
150     :p3 IDLE, not iReady
150     :p1 WAIT, oReady, not oGet
153     :p2 oMsg=  2, oReady
170     :p3 IDLE, not iReady
170     :p2 WAIT, oReady, not oGet
172     :p1 oGet
190     :p3 iMsg=  2, iGet
190     :p2 WAIT, oReady, not oGet
190     :p1 iMsg=  0, iGet
193     :p3 oMsg=  3, oReady
193     :p1 oMsg=  1, oReady
210     :p1 WAIT, oReady, not oGet
212     :p2 oGet
212     :p3 oGet
230     :p1 WAIT, oReady, not oGet
230     :p2 iMsg=  1, iGet
230     :p3 IDLE, not iReady
233     :p2 oMsg=  2, oReady
250     :p3 IDLE, not iReady
250     :p2 WAIT, oReady, not oGet
252     :p1 oGet
270     :p3 iMsg=  2, iGet
270     :p2 WAIT, oReady, not oGet
270     :p1 iMsg= 10, iGet
273     :p3 oMsg=  3, oReady
273     :p1 oMsg= 11, oReady
290     :p1 WAIT, oReady, not oGet
292     :p2 oGet
292     :p3 oGet
310     :p1 WAIT, oReady, not oGet
310     :p2 iMsg= 11, iGet
310     :p3 IDLE, not iReady
313     :p2 oMsg= 12, oReady
330     :p3 IDLE, not iReady
330     :p2 WAIT, oReady, not oGet
332     :p1 oGet
350     :p3 iMsg= 12, iGet
350     :p2 WAIT, oReady, not oGet
350     :p1 iMsg= 20, iGet
353     :p3 oMsg= 13, oReady
353     :p1 oMsg= 21, oReady
370     :p1 WAIT, oReady, not oGet
372     :p2 oGet
372     :p3 oGet
390     :p1 WAIT, oReady, not oGet
390     :p2 iMsg= 21, iGet
390     :p3 IDLE, not iReady
393     :p2 oMsg= 22, oReady
410     :p3 IDLE, not iReady
410     :p2 WAIT, oReady, not oGet
412     :p1 oGet
430     :p3 iMsg= 22, iGet
430     :p2 WAIT, oReady, not oGet
430     :p1 iMsg= 20, iGet
433     :p3 oMsg= 23, oReady
433     :p1 oMsg= 21, oReady
450     :p1 WAIT, oReady, not oGet
452     :p2 oGet
452     :p3 oGet
470     :p1 WAIT, oReady, not oGet
470     :p2 iMsg= 21, iGet
470     :p3 IDLE, not iReady
473     :p2 oMsg= 22, oReady
490     :p3 IDLE, not iReady
490     :p2 WAIT, oReady, not oGet
492     :p1 oGet

Facebook

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