Skip to main content

Part-1 Verilog Examples for Sequential circuits


Find MATlAB Programs for signals and systems with output here↓ 
https://meenakshiagarwalvlsi.blogspot.com/p/graphical-representation-of-time.html


Counters with synchronous and asynchronous reset

Example-1 Verilog code for an n-bit Up-counter with enable (en), load (l) and asynchronous reset with test bench.

module upcount(r,rst,clk,en,l,Q);
parameter n=16;
input [n-1:0]r;
input rst,clk,en,l;
output reg [n-1:0]Q;
always@(negedge rst, posedge clk)  // asynchronous clear, posedge triggered clock a
begin
if(!rst)
Q<=0;
else if (l)  // synchronous load from R
Q<=r;
else if(en)      //counts up if en=1
Q<=Q+1;
end
endmodule

Test Bench with a clock of time period 10 time units

module test_upcount;
parameter n=16;
reg [n-1:0]r;
reg rst,clk,en,l;
wire [n-1:0]Q;

upcount t0(r,rst,clk,en,l,Q);

initial
begin
clk=0; l=0;en=0;rst=0;r=0;
$monitor("r=%b,en=%b,l=%b,Q=%d \n",r,en,l,Q);
end

always
#5 clk=~clk;

initial
begin
#1 rst=1;
#7 l=1;en=0;r=1;
#5en=1;l=0;
#6 r=0; l=1;en=0;
#5en=1;l=0;
#5en=0;l=0;
#5en=1;l=0;
end
endmodule


Simulation Results


Synthesis Schematic








































Example-2 Verilog code for an n-bit Up-counter with enable (en), load (l) and synchronous reset with test bench.


module upcount(r,rst,clk,en,l,Q);
parameter n=16;
input [n-1:0]r;
input rst,clk,en,l;
output reg [n-1:0]Q;
always@(posedge clk)  // synchronous reset, posedge triggered clock a
begin
if(!rst)
Q<=0;
else if (l)  // synchronous load from R
Q<=r;
else if(en)      //counts up if en=1
Q<=Q+1;
end
endmodule

Test Bench with a clock of time period 10 time units

module test_syn_rstUPcount;

parameter n=16;

reg [n-1:0]r;

reg rst,clk,en,l;

wire [n-1:0]Q;


upcount_syn_reset r1(r,rst,clk,en,l,Q);


initial

begin

clk=0; l=0;en=0;rst=0;r=0;

$monitor("r=%b,en=%b,l=%b,Q=%d \n",r,en,l,Q);

end


always

#5 clk=~clk;

initial

begin

#1 rst=1;

#3 l=1;en=0;r=16;

#9 en=1;l=0;
#10 en=1;
#30 rst=0;
#10 rst=1; r=18; l=1;en=0;
#10 r=20;
#10 r=25;
#10 en=1;
#40 en=0;l=0; rst=0;

end

endmodule



Simulation Results










Comments

Popular posts from this blog

Verilog HDL: Structured Procedures: Initial Statement

Structured Procedures: used in behavioral modelling There are two structured procedure statements in Verilog: always and initial. These statements are the two most basic statements in behavioral modeling. All other behavioral statements can appear only inside these structured procedure statements. Verilog is a concurrent programming language unlike the C programming language, which is sequential in nature. Activity flows in Verilog run in parallel rather than in sequence. Each always and initial statement represents a separate activity flow in Verilog. Each activity flow starts at simulation time 0. The statements always and initial cannot be nested. 1. Initial Statement All statements inside an initial statement constitute an initial block. An initial block starts at time 0, executes exactly once during a simulation, and then does not execute again. If there are multiple initial blocks, each block starts to execute concurrently at time 0. Each block finishes execution in

VERILOG HDL: Vectors and arrays

Vectors:  Nets or reg data types in verilog HDL can be declared as vectors (a word of multiple bits). wire a;      // single bit variable. wire [4:0]b;  //5-bit vector wire reg clock;     // scalar register reg [0:31] bus;  // 32-bits bus register reg [63:0]count1;  // 64-bit register count1 reg [0:31]count2;   // 32-bit register count2 Part select of a variable vector bus[31]=1'b0;   // set 31th bit of bus count1[0]; // 0th bit of count1 register count1[31-:8] ;  //// start bit=31, width=8=> data[31:24] count1[24+:8] ;  //// start bit=24, width=8=> data[31:24] count2[31-:8] ;  //// start bit=31, width=8=> data[24:31] count2[24+:8] ;  //// start bit=24, width=8=> data[24:31] //in a loop to select all bytes of the vector. input a,b; reg j; reg [255:0]data1; reg [0:255]data2; alsways@ (a,b) begin for (j=0; j<=31; j=j+1) byte = data1[(j*8)+:8]; //Sequence is [7:0], [15:8]... [255:248] //Can initialize a part of the vector data1[(byteN

Vrilog HDL: Nonblocking Procedural Assignments

Nonblocking Procedural Assignment Nonblocking assignments allow scheduling of assignments without blocking execution of the statements that follow in a sequential block. A <= operator is used to specify nonblocking assignments. Note that this operator has the same symbol as a relational operator, less_than_equal_to. The operator <= is interpreted as a relational operator in an expression and as an assignment operator in the context of a nonblocking assignment. To illustrate the behavior of nonblocking statements and its difference from blockinglets take an example. Example: reg x, y, z; reg [15:0] reg_a, reg_b; integer count; //All behavioral statements must be inside an initial or always block initial begin x = 0; y = 1; z = 1; //Scalar assignments count = 0; //Assignment to integer variables reg_a = 16'b0; reg_b = reg_a; //Initialize vectors reg_a[2] <= #15 1'b1; //Bit select assignment with delay reg_b[15:13] <= #10 {x, y, z}; //Assign result