verilog中的4位双向计数器

时间:2015-01-02 12:57:21

标签: verilog

我的任务要求设计4位双向计数器,在达到12之后,从0到12逐渐增加,从12减少到0,每次增加3(0 2 4 6 8 10 12 9 6 3 0 2 4 ...)。在用户使用“stop”命令终止程序之前,该循环应该继续 这个项目将由人字拖设计,必须使用D触发器

这是我的verilog代码。当我创建textbench文件时,我给出了错误:

module CounterTopLevel(
input clk,
 input rst,
input stop,
output [3:0] counter_out
);

//-----------Input Ports---------------
//input clk, stop;

//-----------Output Ports---------------
//output reg [3:0] counter_out;
//initial counter_out = 4'b0000;

//------------Internal Variables--------

 wire rst;
 wire stop;
 wire clk;
 wire [3:0] counter_out;
 wire q3;
 wire q2;
 wire q1;
 wire q0;
 wire d3;
 wire d2;
 wire d1;
 wire d0;

//D3=Q3Q0' + Q2Q1Q0 
//D2= Q2Q1' + Q2Q0' + Q2'Q1Q0 
//D1= Q3'Q1'Q0 + Q1Q0'
//D0= Q0' 

//Z3=Q2
//Z2= Q3Q0' + Q2'Q1 + Q1Q0'
//Z1= Q3 + Q2'Q0
//Z0= Q3Q0 + Q2Q1Q0
assign d3 =  (q3 & ~q0) | (q2 & q1 & q0);
assign d2 =  (q2 & ~q1) | (q2 & ~q0) | (~q2 & q1 & q0);
assign d1 =  (~q3 & ~q1 & q0) | (q1 & ~q0);
assign d0 =  ~q0;
assign counter_out[3] =  q2;
assign counter_out[2] =  (q3 & ~q0) | (~q2 & q1) | (q1 & ~q0);
assign counter_out[1] =  q3 | (~q2 & q0) | (~q1 & q0);
assign counter_out[0] = (q3 & q0) | (q2 & q1 & q0);

DFF DFF0 (
    .clk(clk),
     .rst(rst),
    .d(d0),
     .stop(stop),
    .q(q0)
    );

DFF DFF1 (
    .clk(clk),
    .rst(rst),   
    .d(d1), 
     .stop(stop),
    .q(q1)
    );

DFF DFF2 (
    .clk(clk),
     .rst(rst),
    .d(d2),
     .stop(stop),    
    .q(q2)
    );

DFF DFF3 (
    .clk(clk),
    .rst(rst),   
    .d(d3),
    .stop(stop),     
    .q(q3)
    );
endmodule

module DFF(
input clk,
 input rst,
input d,
 input stop,
output reg q
);

   always @(posedge clk) begin
    if (rst == 1'b1) begin
        q <= 1'b0;
    end
    else begin
        if (stop == 1'b1) begin
            q <= q;
        end
        else begin
            q <= d;
        end
    end
   end
endmodule

这是textbench:

module test2;

// Inputs
reg clk;
reg rst;
reg stop;

// Outputs
wire [3:0] counter_out;

// Instantiate the Unit Under Test (UUT)
CounterTopLevel uut (
    .clk(clk), 
    .rst(rst), 
    .stop(stop), 
    .counter_out(counter_out)
);

initial begin
    // Initialize Inputs
    clk = 0;
    rst = 0;
    stop = 0;
    // Wait 100 ns for global reset to finish
    #100;
    rst=1;
    #100;
    clk=1; ===> line 28
    #20;
    rst=0;
    clk=0;
    #20; ===>line 32
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    stop=1;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    stop=0;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;
    clk=1;
    #20;
    clk=0;
    #20;

    // Add stimulus here

end

endmodule

它给出了这个错误:

ERROR:HDLCompiler:806 - "C:/Users/Ahmed Yasin/Desktop/exps/cift_yonlu_sayaci/test2.v" Line 28: Syntax error near ";".
ERROR:HDLCompiler:806 - "C:/Users/Ahmed Yasin/Desktop/exps/cift_yonlu_sayaci/test2.v" Line 32: Syntax error near "(".
ERROR:ProjectMgmt - 2 error(s) found while parsing design hierarchy.

我该如何解决?

1 个答案:

答案 0 :(得分:1)

我在EDA Playground上尝试过。它运行良好,有一些细微的变化。

CounterTopLevel我必须删除rststopclkcounter_out的重新声明。

对于您的测试平台时钟,您可能需要考虑以下几点:

initial begin
  clk =0;
  forever begin
    #20 clk = ~clk;
  end
end

对于测试程序,您可以这样做:

initial begin
  rst =0;
  stop=0;
  @(posedge clk);
  @(posedge clk);
  rst=1;
  repeat(10) 
    @(posedge clk);  //Wait ten clock edges until setting stop
  stop=1;
end