2020年2月28日 星期五

Verilog 4bit 上數計數器( Counter )


Verilog 4bit 上數計數器( Counter )

源自於 https://hom-wang.gitbooks.io/verilog-hdl/content/Chapter_07.html
程式( 計數器 ):
module Counter( CLK, RST, Cnt_Num, Cnt_Data );

    parameter Cnt_Num_Size = 2;
    parameter Cnt_Data_Size = 16;

    input   CLK, RST;
    input   [Cnt_Num_Size-1:0] Cnt_Num;
    output  [Cnt_Data_Size-1:0] Cnt_Data;

    wire    CLK, RST;
    wire    [Cnt_Num_Size-1:0] Cnt_Num;
    reg     [Cnt_Data_Size-1:0] Cnt_Data;

    always @( posedge CLK, negedge RST ) begin
        if( !RST )
            Cnt_Data <= 0;
        else
            Cnt_Data <= Cnt_Data + Cnt_Num;
    end

endmodule

修改成4bit 計數器
//上數計數器( Counter )
//程式( 計數器 ):
// Cnt_Num =02 每次+2     Cnt_Num =01 每次+1     
//===========================================
module Counter_4bit( CLK, RST, Cnt_Num, Cnt_Data );

    parameter Cnt_Num_Size = 2;
    //parameter Cnt_Data_Size = 16;
    parameter Cnt_Data_Size = 4;
    
    input   CLK, RST;
    input   [Cnt_Num_Size-1:0] Cnt_Num;
    output  [Cnt_Data_Size-1:0] Cnt_Data;

    wire    CLK, RST;
    wire    [Cnt_Num_Size-1:0] Cnt_Num;
    reg     [Cnt_Data_Size-1:0] Cnt_Data;

    always @( posedge CLK, negedge RST ) begin
        if( !RST )
            Cnt_Data <= 0;
        else
            Cnt_Data <= Cnt_Data + Cnt_Num;
    end

endmodule


//===========================================
// 時間單位 100ns, 時間精確度100 ps
`timescale 100ns/100ps
module Test_bench;
    parameter Cnt_Num_Size = 2;
    parameter Cnt_Data_Size = 4;
    
//    input   CLK, RST;
//    input   [Cnt_Num_Size-1:0] Cnt_Num;
//    output  [Cnt_Data_Size-1:0] Cnt_Data;

 // Inputs
 reg CLK, RST;
 reg [Cnt_Num_Size-1:0] Cnt_Num;

 // Outputs
 wire [Cnt_Data_Size-1:0] Cnt_Data;


//Counter_4bit( CLK, RST, Cnt_Num, Cnt_Data );
// Instantiate the Unit Under Test (UUT)

Counter_4bit DUT( 
.CLK(CLK), 
.RST(RST),
.Cnt_Num(Cnt_Num), 
.Cnt_Data(Cnt_Data)
);


 initial begin
  $monitor(CLK, RST, Cnt_Num, Cnt_Data );
 // Initialize Inputs
 CLK = 0;
 RST = 1;
 Cnt_Num=2;
 fork
#250 RST = 0;
#270 RST = 1;
#300 Cnt_Num=1;
#600  $stop;
 join
end
   always #20  CLK = !CLK;
   
endmodule

//===========================================






2 則留言:

  1. 你好,我想請問一下,在我複製您的程式碼去modelsim執行之後,我的波型圖最後一欄會有部分呈現紅色,這會是什麼原因造成的呢?

    回覆刪除
  2. 大概是25000000ps錢都是一條紅色線

    回覆刪除

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...