Tuesday, 29 July 2025

Testbench Code 1

 `timescale 1ns / 1ps


module vending_machine_tb;

    // Inputs

    reg clk, reset, cancel;

    reg [1:0] coin, sel;

    

    // Outputs

    wire PrA, PrB, PrC, change;


    // Instantiate the vending machine module

    vending_machine uut (

        .clk(clk),

        .reset(reset),

        .cancel(cancel),

        .coin(coin),

        .sel(sel),

        .PrA(PrA),

        .PrB(PrB),

        .PrC(PrC),

        .change(change)

    );


    // Clock Generation: 10ns period (50MHz clock)

    always #5 clk = ~clk; 


    // Test Procedure

    initial begin

        // Initialize Inputs

        clk = 0;

        reset = 1;

        cancel = 0;

        coin = 2'b00;

        sel = 2'b00;

        

        // Hold reset for 20 ns

        #20 reset = 0;


        // Test Case 1: Insert Rs. 5 and Buy Product A

        #10 coin = 2'b01;   // Insert Rs. 5

        #10 sel = 2'b00;    // Select Product A

        #10 coin = 2'b00;   // Stop inserting coins

        #20;


        // Test Case 2: Insert Rs. 10 and Buy Product B

        #10 coin = 2'b10;   // Insert Rs. 10

        #10 sel = 2'b01;    // Select Product B

        #20;


        // Test Case 3: Insert Rs. 5 twice (Total Rs. 10) and Buy Product A (Expect Rs. 5 change)

        #10 coin = 2'b01;   // Insert Rs. 5

        #10 coin = 2'b01;   // Insert Rs. 5 again (Total Rs. 10)

        #10 sel = 2'b00;    // Select Product A

        #20;


        // Test Case 4: Insert Rs. 20 and Buy Product C

        #10 coin = 2'b10;   // Insert Rs. 10

        #10 coin = 2'b10;   // Insert Rs. 10 again (Total Rs. 20)

        #10 sel = 2'b10;    // Select Product C

        #20;


        // Test Case 5: Insert Rs. 10 and Cancel Transaction

        #10 coin = 2'b10;   // Insert Rs. 10

        #10 cancel = 1;     // Press Cancel

        #10 cancel = 0;     // Release Cancel

        #20;


        // End Simulation

        #50 $finish;

    end


    // Monitor the signals

    initial begin

        $monitor("Time = %d, Coin = %b, Sel = %b, Cancel = %b, PrA = %b, PrB = %b, PrC = %b, Change = %b",

                  $time, coin, sel, cancel, PrA, PrB, PrC, change);

    end


endmodule


No comments:

Post a Comment