Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Whitney Knitter
Published © GPL3+

How to Test Your Design with Vivado's Behavioral Simulation

This tutorial walks through a simple demonstration of how to deploy your testbench using Vivado's behavioral simulation.

BeginnerFull instructions provided1 hour11,915
How to Test Your Design with Vivado's Behavioral Simulation

Things used in this project

Software apps and online services

Vivado Design Suite
AMD Vivado Design Suite

Story

Read more

Code

sr_flipflop.v

Verilog
module sr_flipflop(
    input clk,
    input set,
    input reset,
    input ena,
    output reg q,
    output reg q_n
    );
    
    always @ (posedge clk)
        begin
            if (ena == 1'b1)
                begin
                    if (set == 1'b1 && reset == 1'b1)
                        begin
                            q <= q;
                            q_n <= ~q;
                        end
                    else if (set == 1'b1 && reset == 1'b0)
                        begin
                            q <= 1'b0;
                            q_n <= 1'b1;
                        end
                    else if (set == 1'b0 && reset == 1'b1)
                        begin
                            q <= 1'b1;
                            q_n <= 1'b0;
                        end
                    else
                        begin
                            q <= 1'bz;
                            q_n <= 1'bz;
                        end
                end
            else
                begin
                    q <= 1'b0;
                    q_n <= 1'b0;
                end
        end
    
endmodule

tb_sr_flipflop.v

Verilog
module tb_sr_flipflop;

    reg clk, set, reset, ena;
    wire q, q_n;
    
    /*
     * 100Mhz (10ns) clock 
     */
    always begin
        clk = 1; #5;
        clk = 0; #5;
    end
    
    always begin 
        ena = 1; #100;
        ena = 0; #100;
    end
    
    always begin
        set = 1; #25; // set = 1 reset = 1 ena = 1 so q = q q_n = ~q
        set = 1; #25; // set = 1 reset = 0 ena = 1 so q = 0 q_n = 1
        set = 0; #25; // set = 0 reset = 1 ena = 1 so q = 1 q_n = 0
        set = 0; #25; // set = 0 reset = 0 ena = 1 so q = z q_n = z
        
        set = 1; #25; // set = 1 reset = 1 ena = 0 so q = 0 q_n = 0
        set = 1; #25; // set = 1 reset = 0 ena = 0 so q = 0 q_n = 0
        set = 0; #25; // set = 0 reset = 1 ena = 0 so q = 0 q_n = 0
        set = 0; #25; // set = 0 reset = 0 ena = 0 so q = 0 q_n = 0
    end
    
    always begin
        reset = 1; #25; // set = 1 reset = 1 ena = 1 so q = q q_n = ~q
        reset = 0; #25; // set = 1 reset = 0 ena = 1 so q = 0 q_n = 1
        reset = 1; #25; // set = 0 reset = 1 ena = 1 so q = 1 q_n = 0
        reset = 0; #25; // set = 0 reset = 0 ena = 1 so q = z q_n = z
        
        reset = 1; #25; // set = 1 reset = 1 ena = 0 so q = 0 q_ = 0
        reset = 0; #25; // set = 1 reset = 0 ena = 0 so q = 0 q_ = 0
        reset = 1; #25; // set = 0 reset = 1 ena = 0 so q = 0 q_ = 0
        reset = 0; #25; // set = 0 reset = 0 ena = 0 so q = 0 q_ = 0
    end
    
    sr_flipflop sr_flipflop_i(
        .clk(clk),
        .set(set),
        .reset(reset),
        .ena(ena),
        .q(q),
        .q_n(q_n)
    );
    
endmodule

Credits

Whitney Knitter
169 projects • 1727 followers
All thoughts/opinions are my own and do not reflect those of any company/entity I currently/previously associate with.

Comments