verilog之四位計數器(編譯仿真查看波形)


先上一段計數器的verilog代碼:

/*4位計數器
這例子非常好的表達了一個概念就是同步復位的概念。
這個概念非常重要,在XILINX的器件所有硬核都使用同步復位。
如果使用異步復位需要多耗費資源。

接着說計數器,計數器必須有時鍾,如果要能進入到初始值,必須有復位輸入。
和一個計數器的輸出。該4位計數器,三個選項全部具備。
在時鍾上升沿,如果復位信號有效,則復位為0,如果復位信號無效,則計數器需要加一。
另外讓大家思考下,如果是計數器的最大值是 13怎么辦?

低電平復位
時鍾上升沿計數
*/
module count4(out,reset,clk);
    output[3:0] out;
    input reset,clk;
    reg[3:0] out;

    always @(posedge clk)
        begin
            if (reset) 
                out<=0; //同步復位
            else 
                out<=out+1'b1; //計數
        end
endmodule

再附一首testbeach:

/*
File Name    :    ctr_tb.v
Description    :    The testbench of the ctr_4.v
Written    By    :    LiMing
Data        :    2011/04/19 16:13


modefied    :    Period = 4ns
*/

`timescale 1ns/1ns
module test;

    /*Make a reset that pulses once.*/
    reg reset = 0;
    
    initial
        begin
            #2  reset = 1;        //reset
            #3  reset = 0;        //start count
            #24 reset = 1;        //reset
            #2  reset = 0;        //start count
            #48 reset = 1;        //reset
            #1  reset = 0;        //start count
            #60 reset = 1;        //reset
            #3  reset = 0;        //start count
            #100 $stop;
        end

    
    /*Make a regular pulsing closk*/
    parameter clk_period = 4;
    reg clk;
    initial
        clk = 0;
        always #(clk_period/2) 
            clk = ~clk;
    
    wire[3:0] out;
    count4 ctr(out,reset,clk);
    
    initial
        $monitor("At time %t, value = %h (%0d)",$time, out, out);
        
    initial
        begin            
            $dumpfile("test.lxt");
            $dumpvars(0,test);
        end
endmodule

再再附批處理文件:

ECHO OFF
ECHO *********************************
ECHO *        Batch file
ECHO *********************************
ECHO *
ECHO ON
iverilog -o test ctr_4.v ctr_tb.v
vvp -n test -lxt2
gtkwave test.lxt

運行結果:

G:\Verilog HDL\examples\Verilog135\02_4bitctr>go.bat

G:\Verilog HDL\examples\Verilog135\02_4bitctr>ECHO OFF
*********************************
*               Batch file
*********************************
*

G:\Verilog HDL\examples\Verilog135\02_4bitctr>iverilog -o test ctr_4.v ctr_tb.v

G:\Verilog HDL\examples\Verilog135\02_4bitctr>vvp -n test -lxt2
LXT2 info: dumpfile test.lxt opened for output.
At time                    0, value = x (x)
At time                    2, value = 0 (0)
At time                    6, value = 1 (1)
At time                   10, value = 2 (2)
At time                   14, value = 3 (3)
At time                   18, value = 4 (4)
At time                   22, value = 5 (5)
At time                   26, value = 6 (6)
At time                   30, value = 0 (0)
At time                   34, value = 1 (1)
At time                   38, value = 2 (2)
At time                   42, value = 3 (3)
At time                   46, value = 4 (4)
At time                   50, value = 5 (5)
At time                   54, value = 6 (6)
At time                   58, value = 7 (7)
At time                   62, value = 8 (8)
At time                   66, value = 9 (9)
At time                   70, value = a (10)
At time                   74, value = b (11)
At time                   78, value = c (12)
At time                   82, value = d (13)
At time                   86, value = e (14)
At time                   90, value = f (15)
At time                   94, value = 0 (0)
At time                   98, value = 1 (1)
At time                  102, value = 2 (2)
At time                  106, value = 3 (3)
At time                  110, value = 4 (4)
At time                  114, value = 5 (5)
At time                  118, value = 6 (6)
At time                  122, value = 7 (7)
At time                  126, value = 8 (8)
At time                  130, value = 9 (9)
At time                  134, value = a (10)
At time                  138, value = b (11)
At time                  142, value = 0 (0)
At time                  146, value = 1 (1)
At time                  150, value = 2 (2)
At time                  154, value = 3 (3)
At time                  158, value = 4 (4)
At time                  162, value = 5 (5)
At time                  166, value = 6 (6)
At time                  170, value = 7 (7)
At time                  174, value = 8 (8)
At time                  178, value = 9 (9)
At time                  182, value = a (10)
At time                  186, value = b (11)
At time                  190, value = c (12)
At time                  194, value = d (13)
At time                  198, value = e (14)
At time                  202, value = f (15)
At time                  206, value = 0 (0)
At time                  210, value = 1 (1)
At time                  214, value = 2 (2)
At time                  218, value = 3 (3)
At time                  222, value = 4 (4)
At time                  226, value = 5 (5)
At time                  230, value = 6 (6)
At time                  234, value = 7 (7)
At time                  238, value = 8 (8)
At time                  242, value = 9 (9)

G:\Verilog HDL\examples\Verilog135\02_4bitctr>gtkwave test.lxt

GTKWave的波形圖:

全局

復位0處的波形:

復位1處的波形:

復位2處的波形:

復位3處的波形:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM