2016-09-23 5 views
1

이 카운터에 문제가 있습니다. 출력은 모두 xxxxxxxx입니다. 카운트의 초기 값을 설정하고 0으로 오버플로해야한다는 것을 알고 있지만 그런 식으로 오류가 발생합니다.카운터 시스템 Verilog 코드

// Code your testbench here 
// or browse Examples 
module tb(); 

    reg [3:0] in; 
    reg clk,start; 
    wire [7:0] count; 
    reg overflow = 1'b0; 

    initial begin 

    $display ("time\t clk start in count overflow"); 
    $monitor ("%g\t %b %b %b %b", $time, clk, start, in, count, overflow); 

    clk=0; 
    in=0; 
    start=0; 
    // overflow=0; 
    // count=0; 

    #5 in=4'd1; 
    #5 in=4'd5; 
    #5 in=4'd4; 
    #5 in=5'd5; 
    #5 in=4'd1; 
    #5 in=4'd5; 
    #5 in=4'd4; 
    #5 in=5'd5; 
    #5 in=4'd1; 
    #5 in=4'd5; 
    #5 in=4'd4; 
    #5 in=5'd5; 
    #5 in=4'd1; 
    #5 in=4'd5; 
    #5 in=4'd4; 
    #5 in=5'd5; 
    #50 $finish; 

    end 

    always #5 clk=~clk; 

    counter u0(.*); 

endmodule 

나는 그것이 간단한 질문입니다 알지만, 너희들이 도와 만약 내가 그것을 감사 :

// Code your design here 
module counter (in, start, count, clk, overflow); 

input [3:0] in; 
input clk; 
input start; 
output reg [7:0] count; 
output reg overflow; 
//reg count; 
//count =0; 
//overflow=0; 
always @ (posedge clk) 
    begin 
    if (start) begin 
     count <= 8'b0; 
     overflow <= 1'b0; 
    end 

    else if (in == 4'b0101) begin 
     count <= count+1; 
    end 
    if (count == 4'b1111) begin 
     overflow <=1'b1; 
    end 
    end 

endmodule 

이 테스트 벤치입니다 :이 코드입니다.

+0

카운트는 X에서 유지됩니다 (테스트 벤치의 데이터를 구동하기 위해 posedge 이벤트를 사용)? –

+0

예 시작 신호를 통해 카운터를 재설정하지 않았습니다. –

답변

1

해결해야 할 두 가지 문제가 있습니다.

1) 동시에 in = 5는 클럭의 네거티브 에지 동안에 만 설정됩니다. 이것은 clk주기가 # 10이고 tb 코드가 매 # 5마다 "in"값을 변경하기 때문입니다. 카운터가 posedge의 값을 확인하면 in = 5를 누락합니다.인 기간은 # 10 또는 TB가 필요합니다 신호를 "in"으로 설정하기 전에 clk의 위치를 ​​기다릴 수 있습니다.

2) 카운터가 재설정되도록 시작을 설정해야합니다. 그렇지 않으면 count = x (알 수 없음) 및 count + 1 => x + 1 인 값이 x와 같습니다. 따라서 카운터는 증가하지 않고 계속 x를 유지합니다.

업데이트 된 tb는 다음과 같습니다.

module tb(); 

    reg [3:0] in; 
    reg clk,start; 
    wire [7:0] count; 
    reg overflow = 1'b0; 

    initial begin 

    $display ("time\t clk start in count overflow"); 
    $monitor ("%g\t %b %b %b %b", $time, clk, start, in, count, overflow); 

    clk=0; 
    in=0; 
    start=0; 
    // overflow=0; 
    // count=0; 

    #10 start = 1'b1; // reset counter 
    #10 start = 1'b0; 
    #10 in=4'd1; 
    #10 in=4'd5; 
    #10 in=4'd4; 
    #10 in=5'd5; 
    #10 in=4'd1; 
    #10 in=4'd5; 
    #10 in=4'd4; 
    #10 in=5'd5; 
    #10 in=4'd1; 
    #10 in=4'd5; 
    #10 in=4'd4; 
    #10 in=5'd5; 
    #10 in=4'd1; 
    #10 in=4'd5; 
    #10 in=4'd4; 
    #10 in=5'd5; 
    #50 $finish; 

    end 

    always #5 clk=~clk; 

    counter u0(.*); 

initial // Dump waveform for debug 
$dumpvars; 

endmodule 

$ dumpvars 명령을 사용하여 파형을 덤프하여 디버깅 할 수 있습니다.

대체 코드`start` 당신이지고 어떤 오류 1로 될 때까지

// Code your testbench here 
// or browse Examples 
module tb(); 

    reg [3:0] in; 
    reg clk,start; 
    wire [7:0] count; 
    reg overflow = 1'b0; 

    initial begin 

    $display ("time\t clk start in count overflow"); 
    $monitor ("%g\t %b %b %b %b", $time, clk, start, in, count, overflow); 

    clk=0; 
    in=0; 
    start=0; 
    // overflow=0; 
    // count=0; 

    @(posedge clk) start = 1'b1;// reset counter 
    @(posedge clk) start = 1'b0; 
    @(posedge clk) in=4'd1; 
    @(posedge clk) in=4'd5; 
    @(posedge clk) in=4'd4; 
    @(posedge clk) in=5'd5; 
    @(posedge clk) in=4'd1; 
    @(posedge clk) in=4'd5; 
    @(posedge clk) in=4'd4; 
    @(posedge clk) in=5'd5; 
    @(posedge clk) in=4'd1; 
    @(posedge clk) in=4'd5; 
    @(posedge clk) in=4'd4; 
    @(posedge clk) in=5'd5; 
    @(posedge clk) in=4'd1; 
    @(posedge clk) in=4'd5; 
    @(posedge clk) in=4'd4; 
    @(posedge clk) in=5'd5; 
    #50 $finish; 

    end 

    always #5 clk=~clk; 

    counter u0(.*); 

initial 
$dumpvars; 

endmodule