自動售貨機(2)

Testbeach代碼

代碼只是寫了兩種情況,一個好的驗證testbeach代碼必須包含所有情況,可以重複但一定不能漏掉情況。

`timescale 1ns/1ps;
module DRINK_STATUS_MOORE_TB();
    localparam  CHARGE_WIDTH = 6;
    localparam  RESET_INIT_H =10;
    localparam  RESET_INIT_L =20;
    localparam  FREQ         =50; //單位MHz
    localparam  ZHOUQI       =1000/50;
    
    wire                      oney;
    wire                      twoy;
    wire                      fivey;
    reg                       clk;
    reg                       rst_n;
    wire                      out;
    wire   [CHARGE_WIDTH-1:0] charge;

    reg    [15:0]             charge_out_array[1023:0];
    reg    [15:0]             charge_gld_array[1023:0];

    reg    [15:0]             output_cnt;
    reg    [15:0]             input_cnt;
    
    reg    [2:0]              money_int;
    reg    [2:0]              send_finish;
    reg    [15:0]             error_num;
    integer                   i;
    

    assign oney  = money[0];
    assign twoy  = money[1];
    assign fivey = money[2];
    
    //
    initial begin : rst_n_QQ
        rst_n = 1'b1;
    #RESET_INIT_H;
        rst_n = 1'b0;
    #RESET_INIT_L;
        rst_n = 1'b1;
    end
//
    initial begin : clk_qq    
        clk = 1'b0;
        forever #(ZHOUQI/2) clk = ~clk;
    end    
//task模塊 :相當於函數的作用  與function意思大致
    task money_in(
                    input [2:0] oney_time,
                    input [2:0] twoy_time,
                    input [2:0] fivey_time
                  );
            repeat(oney_time) begin
                @(posedge clk);
                money_int = 3'b001;
            end
            repeat(twoy_time) begin
                @(posedge clk);
                money_int = 3'b010;
            end
            repeat(fivey_time) begin
                @(posedge clk);
                money_int = 3'b100;
            end
                @(posedge clk);
                money_int = 3'b000;
            input_cnt = input_cnt + 1'b1;   //給變量賦值,不在此處,但一定要賦值
        endtask

//創建激勵信號

        initial begin 
            send_finish = 1'b0;
            money_int = 3'b000;
            input_cnt = 16'h0000;
            #RESET_INIT_H+RESET_INIT_L;
            @(posedge clk);
            money_in(0,0,2);
            charge_out_array[input_cnt-1] = 6'd20;
            money_in(1,1,1);
            charge_out_array[input_cnt-1] = 6'd00;
            send_finish = 1'b1;
            #1000;
            $finish();
         end

        initial begin : OUTPUT_REAL_CLK
         money_int = 3'b000;
         #RESET_INIT_H+RESET_INIT_L;
         @(posedge clk);
         repeat(3) begin
             @(posedge clk);
         end
         #1;
            if((out == 1'b0) &&(charge == 5'd20)) begin
                $display("successful");
            end
            else begin
                $display("ERROR,out=%d,charge=%d",out,charge);
            end
          @(posedge clk);
         repeat(5) begin
             @(posedge clk);
         end
         #1;
            if((out == 1'b0) &&(charge == 5'd0)) begin
                $display("successful");
            end
            else begin
                $display("ERROR,out=%d,charge=%d",out,charge);
            end
        
        always@(posedge out,negedge rst_n) begin
            if(rst_n == 1'b0) begin
                output_cnt <= 1'b0;
            end
            else begin
                output_cnt <= output_cnt + 1'b1;
                charge_gld_array[output_cnt-1] <= charge;
            end
        end
        initial begin
            error_num =16'h0000;
            @(posedge send_finish);
            @(posedge clk);
            @(posedge clk);
            @(posedge clk);
            if(output_cnt == input_cnt) begin
                $display("successful");
            end
            else begin 
                error_num = error_num + 1'b1;
                $display("ERROR,output_cnt=%d,input_cnt=%d",output_cnt,input_cnt);
            end
            
            for(i=0; i<input_cnt;i=i+1)
                if(charge_gld_array[i]==charge_out_array[i]) begin
                     $display("successful");
                end
                else begin               $display("ERROR,times=%d,charge_gld_array=%d,charge_out_array=%d",i+1,charge_gld_array,charge_out_array);
                error_num = error_num + 1'b1;
                end
            initial begin 
                if(error_num == 16'h0000)
                    $display("PASS");
                end
                else begin
                    $display("ERROR");
                end
            end
//端口名與tb上層名沒有例化               
endmodule
        

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章