Verilog HDL Lab Quiz
Verilog HDL Lab Quiz
5. Multiplexers, Demultiplexers.
9. Waveform generators
12. Sequence detector using Mealy and Moore type state machines.
Mini Project:
i) 8 bit CPU
iii) RTL code for Booths algorithm for signed binary number multiplication.
v) Development of HDL code for MAC unit and realization of FIR filter.
3
7 4-Bit Comparators 39
8 ALU Modeling 43
9 Waveform Generators 48
*10 Latches 51
11 Flip-Flops 53
Verilog Theory:
Design Styles:
Bottom-Up Design:
The desired design-style of all designers is the top-down one. A real top-down
design allows early testing, easy change of different technologies, a structured system
design and offers many other advantages. But it is very difficult to follow a pure top-
down design. Due to this fact most designs are a mix of both methods, implementing
some key elements of both design styles.
5
Specification
High Level Design
Micro Design/Low level design
RTL Coding
Simulation
Synthesis
Place & Route
Post Silicon Validation
Simulation:
Synthesis:
It is the process in which synthesis tools like design compiler take RTL in Verilog or
VHDL, target technology, and constrains as input and maps the RTL to target
technology primitives. Synthesis tool, after mapping the RTL to gates, also do the
minimal amount of timing analysis to see if the mapped design is meeting the timing
requirements. (Important thing to note is, synthesis tools are not aware of wire delays,
they only know of gate delays). After the synthesis there are a couple of things that
are normally done before passing the netlist to backend (Place and Route).
The gate level netlist from the synthesis tool is taken and imported into place
and route tool in Verilog netlist format. All the gates and flip-flops are placed; clock
tree synthesis and reset is routed. After this each block is routed. The PAR tool output
is a GDS file, used by foundry for fabricating the ASIC. Backend team normally
6
dumps out SPEF (standard parasitic exchange format) /RSPF (reduced parasitic
exchange format)/DSPF (detailed parasitic exchange format) from layout tools like
ASTRO to the frontend team, who then use the read_parasitic command in tools like
Prime Time to write out SDF (standard delay format) for gate level simulation
purposes.
Module:
In Verilog, we call our "black boxes" module. This is a reserved word within the
program used to refer to things with inputs, outputs, and internal logic workings.
Here we have only two types of ports, input and output. In real life, we can have
bi-directional ports as well. Verilog allows us to define bi-directional ports as "inout."
Operators:
Nearly all operators are exactly the same as their counterparts in the C
programming language.
Control Statements:
One could use any operator in the condition checking, as in the case of C
language. If needed we can have nested if else statements; statements without else are
also ok, but they have their own problem, when modeling combinational logic, in case
they result in a Latch (this is not always true).
Case statements are used where we have one variable which needs to be
checked for multiple values. like an address decoder, where the input is an address
and it needs to be checked for all the values that it can take. Instead of using multiple
7
nested if-else statements, one for each value we're looking for, we use a single case
statement: this is similar to switch statements in languages like C++.
For loops in Verilog are almost exactly like for loops in C or C++. The only
difference is that the ++ and -- operators are not supported in Verilog.
Repeat is similar to the for loop. Instead of explicitly specifying a variable and
incrementing it when we declare the for loop, we tell the program how many times to
run through the code, and no variables are incremented.
• The sensitive list is the one which tells the always block when to execute the
block of code. The @ symbol after reserved word ' always', indicates that the
block will be triggered "at" the condition in parenthesis after symbol @.
• One important note about always block: it cannot drive wire data type, but can
drive reg and integer data types.
• An assign statement is used for modeling only combinational logic and it is
executed continuously. So the assign statement is called 'continuous
assignment statement' as there is no sensitive list.
When repeating the same old things again and again, Verilog, like any other
programming language, provides means to address repeated used code, these are
called Tasks and Functions.
Functions and tasks have the same syntax; one difference is that tasks can
have delays, whereas functions can not have any delay. This means that function can
be used for modeling combinational logic.
A second difference is that functions can return a value, whereas tasks cannot.
Module Instantiation
• Verilog has built in primitives like gates, transmission gates, and switches.
These are rarely used in design (RTL Coding), but are used in post synthesis
step for modeling the ASIC/FPGA cells; these cells are then used for gate
level simulation, or what is called as SDF simulation. Also the output netlist
format from the synthesis tool, which is imported into the place and route tool,
is also in Verilog gate level primitives. Ex: and , or etc:
• There are six different switch primitives (transistor models) used in Verilog,
nmos, pmos and cmos and the corresponding three resistive versions rnmos,
rpmos and rcmos. The cmos types of switches have two gates and so have two
control signals.
• Transmission gates tran and rtran are permanently on and do not have a
control line. Tran can be used to interface two wires with separate drives, and
rtran can be used to weaken signals. Resistive devices reduce the signal
strength which appears on the output by one level. All the switches only pass
signals from source to drain; incorrect wiring of the devices will result in high
impedance outputs.
Delays:
In real circuits, logic gates have delays associated with them. Verilog provides the
mechanism to associate delays with gates
• The rise delay is associated with a gate output transition to 1 from another
value (0, x, z).
• The fall delay is associated with a gate output transition to 0 from another
value (1, x, z).
• The Turn-off delay is associated with a gate output transition to z from another
value (0, 1, x).
• The min value is the minimum delay value that the gate is expected to have.
• The typ value is the typical delay value that the gate is expected to have.
• The max value is the maximum delay value that the gate is expected to have.
Verilog has built-in primitives like gates, transmission gates, and switches. This is
a rather small number of primitives; if we need more complex primitives, then
Verilog provides UDP, or simply User Defined Primitives. Using UDP we can model.
• Combinational Logic
• Sequential Logic
UDP begins with reserve word primitive and ends with endprimitive.
Ports/terminals of primitive should follow. This is similar to what we do for module
definition. UDPs should be defined outside module and endmodule.
table: It is used for describing the function of UDP. Verilog reserved word
table marks the start of table and reserved word endtable marks the end of table.
Each line inside a table is one condition; when an input changes, the input condition is
matched and the output is evaluated to reflect the new change in input.
• Concatenations: are expressed using the brace characters { & }, with commas
separating the expressions within.
o Example: + {a, b[3:0], c, 4'b1001} // if a and c are 8-bit numbers, the
results has 24 bits
• Unsized constant numbers are not allowed in concatenations.
Abstraction Levels:
Procedural Blocks:
• initial: initial blocks execute only once at time zero (start execution at time
zero).
• always: always blocks loop to execute over and over again; in other words, as
the name suggests, it executes always.
If a procedure block contains more than one statement, those statements must be
enclosed within
When using begin-end, we can give name to that group. This is called named
blocks
begin - end
fork - join
• Blocking assignments are executed in the order they are coded, hence they are
sequential. Since they block the execution of next statement, till the current
statement is executed, they are called blocking assignments. Assignment are
made with "=" symbol.
Example a = b;
• Special versions of the case statement allow the x ad z logic values to be used
as "don't care":
Looping statements:
• Appear inside procedural blocks only; Verilog has four looping statements
like any other programming language.
• The forever loop executes continually, the loop never ends. Normally we use
forever statements in initial blocks.
• The repeat loop executes < statement > a fixed < number > of times
• The while loop executes as long as an < expression > evaluates as true. This is
the same as in any other programming language.
• The for loop is the same as the for loop used in any other programming
language.
• Continuous assignment statements drive nets (wire data type). They represent
structural connections.
14
AIM:
a) Write verilog code for two input logic gates
b) Verify the code using test bench
PROGRAMME:
module gate2(a,b,x);
input a,b;
output x;
// 2- input Logic gates
1. and(x,a,b);
2. or(x,a,b);
4. nand(x,a,b);
5. nor(x,a,b);
6. xor(x,a,b);
endmodule
15
OUTPUT WAVEFORM:
1. and gate
Fig. 1
2. or gate
Fig. 2
3. not gate
Fig. 3
4. nand gate
Fig. 4
16
5. nor gate
Fig. 5
6. xor gate
Fig. 6
Fig.7
17
Fig. 8
Fig. 9
1. What is a Simulator?
2. What are the different levels of abstractions in the Verilog?
3. What are the differences between ‘C’ language and ‘Verilog’?
4. What is the basic component in Verilog programme?
5. What is the difference between wire and reg data types?
6. What is the difference between wire and reg?
7. What is the difference between blocking and non-blocking
assignments?
8. What is the difference between bit wise, unary and logical operators?
18
AIM:
a) Realize Four Variable functions
b) Verify the code using test bench
i) f(a,b,c,d) = Σ 0, 1, 3, 5, 7, 11, 15
Minimize the above expressions using Boolean algebra and write the data flow model.
19
1. What are the different logic minimization techniques and name them?
2. What is the difference between Minterm and Maxterm?
3. What is the difference between POS and SOP?
4. In logic minimization, which method is preferred if the number of
variables are more than 5?
5. How many Boolean equations are possible with ‘n’ variables?
6. Which one preferred-casex or casez?
7. What is the difference between task and function?
8. What is the difference between casex, casez and case statements?
20
AIM:
Write verilog code for halfadder, fulladder, 4-bit parallel adder and full subtractor.
PROGRAMME:
Half Adder:
module halfadder(sum,carryout,in0,in1);
input ino,in1;
output sum,carryout;
1. xor x1(s,a,b);
and a1(c,a,b);
2. assign sum = a ^ b;
assign carryout = (a & b) | (b & c) | (c & a);
endmodule
OUTPUT WAVEFORM:
Fig. 10
Full – Adder:
21
Fig. 11
Fig. 12
N-Bit Adder:
module addern (carryin, X, Y, S, carryout);
parameter n=32;
input carryin;
input [n-1:0] X, Y;
output [n-1:0] S;
output carryout;
reg [n-1:0] S;
reg carryout;
reg [n:0] C;
integer k;
begin
C[0] = carryin;
for (k = 0; k <= n-1; k = k+1)
begin
S[k] = X[k] ^ Y[k] ^ C[k];
C[k+1] = (X[k] & Y[k]) | (X[k] & C[k]) | (Y[k] &
C[k]);
end
carryout = C[n];
end
endmodule
Carry Look Ahead adder:
module CLA_4b(sum,c_4,a,b,c_0);
input [3:0]a,b;
input c_0;
output [3:0]sum;
output c_4;
wire p0,p1,p2,p3,g0,g1,g2,g3;
wire c1,c2,c3,c4;
assign
24
p0=a[0]^b[0],
p1=a[1]^b[1],
p2=a[2]^b[2],
p3=a[3]^b[3],
g0=a[0]&b[0],
g1=a[1]&b[1],
g2=a[2]&b[2],
g3=a[3]&b[3];
assign
c1=g0|(p0&c_0),
c2=g1|(p1&g0)|(p1&p0&c_0),
c3=g2|(p2&g1)|(p2&p1&g0)|(p2&p1&p0&c_0),
c4=g3|(p3&g2)|(p3&p2&p1&g1)|(p3&p2&p1&g0)|(p3&p2&p1&p0&c_0);
assign
sum[0]=p0^c_0,
sum[1]=p1^c1,
sum[2]=p2^c2,
sum[3]=p3^c3,
c_4=c4;
endmodule
OUTPUT WAVEFORM:
Fig. 13
Full Subtractor:
25
module fullsub(x1,x2,x3,d,b);
input x1,x2,x3;
output d,b;
assign d= x1^x2^x3;
assign b=(~x1)&((x1^x3)|(x2&x3));
endmodule
OUTPUT WAVEFORM:
Fig. 14
AIM:
a) Write the verilog code for N – Bit Parallel Adder
b) Verify the code using test bench
PROGRAMME:
N-Bit Adder:
module addern (carryin, X, Y, S, carryout);
parameter n=32;
input carryin;
input [n-1:0] X, Y;
output [n-1:0] S;
output carryout;
reg [n-1:0] S;
reg carryout;
reg [n:0] C;
integer k;
endmodule
OUTPUT WAVEFORM:
Fig. 15
AIM:
a) Write the verilog code for Multiplexers & Demultiplexers
b) Verify the code using test bench
PROGRAMME:
Mux 2x1:
module mux2to1 (w0, w1, s, f);
input w0, w1, s;
output f;
reg f;
1. assign f = s ? w1 : w0;
2. always @(w0 or w1 or s)
f = s ? w1 : w0;
3. always @(w0 or w1 or s)
if (s==0)
f = w0;
else
f = w1;
endmodule
OUTPUT WAVEFORM:
Fig. 16
29
Mux 4x1:
module mux4to1 (w0, w1, w2, w3, S, f);
input w0, w1, w2, w3;
input [1:0] S;
output f;
1. assign f = S[1] ? (S[0] ? w3 : w2) : (S[0] ? w1 : w0);
2. always @(w0 or w1 or w2 or w3 or S)
if (S == 2'b00) // (S == 1)
f = w0;
else if (S == 2'b01) // (S == 2)
f = w1;
else if (S == 2'b10) // (S == 3)
f = w2;
else if (S == 2'b11) // (S == 4)
f = w3;
3. always @(W or S)
case (S)
0: f = W[0];
1: f = W[1];
2: f = W[2];
3: f = W[3];
endcase
endmodule
OUTPUT WAVEFORM:
Fig. 17
30
Mux 16x1:
module mux16to1 (W, S16, f); // Structural Modeling
input [0:15] W;
input [3:0] S16;
output f;
wire [0:3] M;
mux4to1 Mux1 (W[0:3], S16[1:0], M[0]);
mux4to1 Mux2 (W[4:7], S16[1:0], M[1]);
mux4to1 Mux3 (W[8:11], S16[1:0], M[2]);
mux4to1 Mux4 (W[12:15], S16[1:0], M[3]);
mux4to1 Mux5 (M[0:3], S16[3:2], f);
endmodule
OUTPUT WAVEFORM:
Fig. 18
DEMULTIPLEXERS
1x4 Demux:
module demux1x4(y,s,i);
input i;
input [1:0]s;
output [3:0]y;
wire w0,w1;
not(w0,s0);
not(w1,s0);
and(y[0],i,w0,w1);
and(y[1],i,w1,s0);
and(y[2],i,s1,w0);
and(y[3],i,s1,s0);
endmodule
31
1x8 Demux:
module demux18(i,s,f);
input i;
input[2:0] s;
output[7:0] f;
1. always @(s)
begin
case (s)
3'b000: f[0]=i;
3'b001: f[1]=i;
3'b010: f[2]=i;
3'b011: f[3]=i;
3'b100: f[4]=i;
3'b101: f[5]=i;
3'b110: f[6]=i;
3'b111: f[7]=i;
endcase
end
2. wire[2:0] sb;
assign sb[0]=~s[0];
assign sb[1]=~s[1];
assign sb[2]=~s[2];
assign f[0]=i&sb[2]&sb[1]&sb[0];
assign f[1]=i&sb[2]&sb[1]&s[0];
assign f[2]=i&sb[2]&s[1]&sb[0];
assign f[3]=i&sb[2]&s[1]&s[0];
assign f[4]=i&s[2]&sb[1]&sb[0];
assign f[5]=i&s[2]&sb[1]&s[0];
assign f[6]=i&s[2]&s[1]&sb[0];
assign f[7]=i&s[2]&s[1]&s[0];
endmodule
32
OUTPUT WAVEFORM:
Fig. 19
AIM:
a) Write the verilog code for encoders, priority encoders & decoders
b) Verify the code using test bench
PROGRAMME:
4 to2 Encoder:
Module encoder4to2(W,Y,En);
input En;
input [3:0]W;
output [1:0]Y;
if (En == 0)
Y = 2'b00;
else
case (W)
0: Y = 2'b00;
1: Y = 2'b01;
2: Y = 2'b10;
3: Y = 2'b11;
endcase
endmodule
OUTPUT WAVEFORM:
Fig. 20
34
DECODERS
2 to 4 Decoder:
module dec2to4 (W, Y, En);
input [1:0]W; // Address lines
input En; // Enable
output [0:3]Y;
reg [0:3]Y;
1. always @(W or En)
case ({En, W})
3'b100: Y = 4'b1000;
3'b101: Y = 4'b0100;
3'b110: Y = 4'b0010;
3'b111: Y = 4'b0001;
default: Y = 4'b0000;
endcase
2. if (En == 0)
Y = 4'b0000;
else
case (W)
0: Y = 4'b1000;
1: Y = 4'b0100;
2: Y = 4'b0010;
3: Y = 4'b0001;
endcase
and a1(Y[1],r1,r2);
and a2(Y[2],r1,W[0]);
and a3(Y[3],r2,W[1]);
and a4(Y[4],W[1],W[0]);
endmodule
OUTPUT WAVEFORM:
Fig. 21
4 to 16 Decoder:
module dec4to16(W, Y, En);
input [3:0] W;
input En;
output [0:15] Y;
wire [0:3] M;
1. integer k;
always @(W or En)
for (k = 0; k <= 3; k = k+1)
if (W == k)
Y[k] = En;
2. dec2to4 Dec1 (W[3:2], M[0:3], En);
dec2to4 Dec2 (W[1:0], Y[0:3], M[0]);
dec2to4 Dec3 (W[1:0], Y[4:7], M[1]);
dec2to4 Dec4 (W[1:0], Y[8:11], M[2]);
dec2to4 Dec5 (W[1:0], Y[12:15], M[3]);
endmodule
36
OUTPUT WAVEFORM:
Fig. 22
PRIORITY ENCODER
1. always @(W)
begin
z = 1;
casex(W)
4'b1xxx: Y = 3;
4'b01xx: Y = 2;
4'b001x: Y = 1;
4'b0001: Y = 0;
default: begin
z = 0;
Y = 2'bx;
end
endcase
end
37
2. Y = 2'bx;
z = 0;
for (k = 0; k < 4; k = k+1)
if(W[k])
begin
Y = k;
z = 1;
end
end
endmodule
OUTPUT WAVEFORM:
Fig. 23
if(d[2]) q=3'b010;
if(d[1]) q=3'b001;
if(d[0]) q=3'b000;
else
begin
q3=0;
q=3'b000;
end
end
endmodule
OUTPUT WAVEFORM:
Fig. 24
7. 4-Bit Comparator
AIM:
a) Write the verilog code for 4-bit comparator
b) Verify the code using test bench
PROGRAMME:
always @(A or B)
begin
AeqB = 0;
AgtB = 0;
AltB = 0;
if(A == B)
AeqB = 1;
else if (A > B)
AgtB = 1;
else
AltB = 1;
end
endmodule
40
OUTPUT WAVEFORM:
Fig. 25
endmodule
41
module mc(a,b,l,g,e);
input [3:0]a,b;
output [3:0]l,g,e;
reg [3:0]l,g,e;
always @(a or b)
begin
l=less(a,b);
g=great(a,b);
e=equal(a,b);
end
function [3:0]less;
input [3:0]a,b;
if(a<b)
begin
less=a;
$display(“a<b”);
end
else
less=0;
endfunction
function [3:0]great;
input [3:0]a,b;
if(a>b)
begin
great=a;
$display(“a>b”);
end
else
great=0;
endfunction
42
function [3:0]equal;
input [3:0]a,b;
if(a==b)
begin
equal=a;
$display(“a==b”);
end
else
equal=0;
endfunction
endmodule
8. ALU Modeling
AIM:
a) Write the verilog code for ALU with 16 Operations
b) Verify the code using test bench
PROGRAMME:
module alu_8(out,in1,in2,s);
input [8:0]in1,in2;
input [3:0]s;
output [8:0]out;
reg [8:0]out;
//,flag;
always@(s)
begin
case(s)
4'b0000: out=in1+in2; //8-bit addition
endcase
end
endmodule
OUTPUT WAVEFORM:
Fig. 26
always @(s or A or B)
case (s)
0: F = 4'b0000;
1: F = B - A;
2: F = A - B;
3: F = A + B;
4: F = A ^ B;
5: F = A | B;
6: F = A & B;
7: F = 4'b1111;
endcase
endmodule
3’b110:out=rshift(a,b);
3’b111:out=lshift(a,b);
endcase
end
function [15:0]add;
input [7:0]a,b;
add=a+b;
endfunction
function [15:0]sub;
input [7:0]a,b;
sub=a-b;
endfunction
function [15:0]mul;
input [7:0]a,b;
mul=a*b;
endfunction
function [15:0]and1;
input [7:0]a,b;
and1=a&b;
endfunction
function [15:0]or1;
input [7:0]a,b;
or1=a|b;
endfunction
function [15:0]xor1;
input [7:0]a,b;
xor1=a^b;
endfunction
47
function [15:0]lshift;
input [7:0]a,b;
lshift=a<<b;
endfunction
function [15:0]rshift;
input [7:0]a,b;
rshift=a>>b;
endfunction
endmodule
9. Waveform Generators
48
AIM:
a) Write the verilog code for Waveform Generators
b) Verify the code using test bench
PROGRAMME:
module square(clk);
inout clk;
reg clk;
begin
repeat(10)
#10 clk=~clk;
end
endmodule
module square;
parameter TON=5,TOFF=5;
reg clk;
initial
clk=1'b0;
always
begin
#TOFF clk=1'b0;
#TON clk=1'b1;
end
endmodule
49
OUTPUT WAVEFORM:
Fig. 27
module trianglewave(out);
output out;
voltage out;
integer slope;
real offset;
analog begin
@(timer(0,period))
begin
slope=1;
offset=$realtime;
discontuinity(1);
end
@(time(period/2,period))
begin
slope=-1;
offset=$realtime;
50
discontuinity(1);
end
v(out)<amp1*slope*(4*$realtime-offset),period-1);
end
endmodule
module rectangle;
parameter TON=10,TOFF=5;
reg clk;
initial
clk=1'b0;
always
begin
#TOFF clk=1'b0;
#TON clk=1'b1;
end
endmodule
OUTPUT WAVEFORM:
Fig. 28
1. Write verilog program to generate square wave of duty cycle with 66.6 %?
2. Write a program to explain pin – pin delay?
3. Write a verilog program to generate rectangular wave?
4. What are timing constraints?
5. What are the differences between ‘always’ and ‘initial’ statements?
6. How to generate sine wave using verilog coding style?
51
10. Latches
AIM:
a) Write the verilog code for D Latch, SR Latch
b) Verify the code using test bench
PROGRAMME:
D Latch:
module D_latch(D, Clk, Q);
input D, Clk;
output Q;
reg Q;
Fig. 29
SR latch:
module SRlatch(q,q_bar,s,r);
input s,r;
output q,q_bar;
nor(q_bar,r,q);
nor(q,s,q_bar);
endmodule
52
OUTPUT WAVEFORM:
Fig. 30
11. Flip-Flops
AIM:
a) Write the verilog code for SR Flip-Flop, D Flip-Flop, JK Flip-Flop
b) Verify the code using test bench
PROGRAMME:
SR Flip Flop:
module srff(q,s,r,clr,clk);
input s,r,clk,clr;
output q;
reg q;
initial q=1'b 0;
always@(r or s or clk or clr)
begin
if(clk==1 && clr==0)
begin
if(s==0 && r==0)
q=q;
else
if((s==0 && r==1) || (s==1 && r==0))
q=s;
else
if(s==1 && r==1)
$display("Undefined operation
performed");
else
q=q;
end
end
endmodule
54
OUTPUT WAVEFORM:
Fig. 31
D Flip Flop:
module flipflop(D, Clock, Resetn, Q);
input D, Clock, Resetn;
output Q;
reg Q;
1. always @(posedge Clock)
Q = D;
Fig. 32
JK Flip Flop:
module jkff(q,qb,j,k,clr,clk);
input j,k,clk,clr;
output q,qb;
reg q,qb;
55
initial
q=1'b 0;
always @(j or j or posedge clk or posedge clr)
begin
if(clr==0)
q=1’b0;
else
begin
if(j==0 && k==0)
q=q;
else
if((j==0 && k==1)
q=1’b0;
else
if (j==1 && k==0))
q=1’b1;
else
if(j==1 && k==1)
q=~q;
qb=~qb;
end
end
endmodule
OUTPUT WAVEFORM:
Fig. 33
56
OUTPUT WAVEFORM:
Fig. 34
AIM:
a) Write the verilog code for 4-bit shift register & counters
b) Verify the code using test bench
PROGRAMME:
SHIFT REGISTER
4-bit Shift Register:
module shift4(R, L, w, Clock, Q);
input [3:0] R;
input L, w, Clock;
output [3:0] Q;
reg [3:0] Q;
always @(posedge Clock)
if (L)
Q <= R;
else
begin
Q[0] <= Q[1];
Q[1] <= Q[2];
Q[2] <= Q[3];
Q[3] <= w;
end
endmodule
OUTPUT WAVEFORM:
Fig. 35
58
OUTPUT WAVEFORM:
Fig. 36
if (L)
Q <= R;
else
begin
for (k=0; k<n-1; k=k+1)
Q[k] <= Q[k+1];
Q[n-1] <= w;
end
endmodule
60
COUNTER
Up Counter:
module upcount(R, Resetn, Clock, E, L, Q);
input [3:0] R;
input Resetn, Clock, E, L;
output [3:0] Q;
reg [3:0] Q;
Fig.37
Up-Down Counter:
module updowncount(R, Clock, L, E, up_down, Q);
parameter n=8;
input [n-1:0] R;
input Clock, L, E, up_down;
61
output [n-1:0] Q;
reg [n-1:0] Q;
integer direction;
always @(posedge Clock)
begin
if (up_down)
direction = 1;
else
direction = -1;
if (L)
Q <= R;
else if (E)
Q <= Q + direction;
end
endmodule
OUTPUT WAVEFORM:
Fig.38
BCD Counter:
module BCDcount(Clock, Clear, E, BCD1, BCD0);
input Clock, Clear, E;
output [3:0] BCD1, BCD0;
reg [3:0] BCD1, BCD0;
always @(posedge Clock)
62
begin
if (Clear)
begin
BCD1 <= 0;
BCD0 <= 0;
end
else if (E)
if (BCD0 == 4'b1001)
begin
BCD0 <= 0;
if (BCD1 == 4'b1001)
BCD1 <= 0;
else
BCD1 <= BCD1 + 1;
end
else
BCD0 <= BCD0 + 1;
end
endmodule
AIM:
a) Write the verilog code for Parity Generator
b) Verify the code using test bench
PROGRAMME:
Parity Generator:
module parity_9bit(d,even,odd);
input[0:8]d;
output even,odd;
xor
xe0(e0,d[0],d[1]),
xe1(e1,d[2],d[3]),
xe2(e2,d[4],d[5]),
xe3(e3,d[6],d[7]),
xf0(f0,e0,e1),
xf1(f1,e2,e3),
xh0(h0,f0,f1),
xeven(even,d[8],h0);
not
xodd(odd,even);
endmodule
OUTPUT WAVEFORM:
Fig.39
64
AIM:
a) Write the verilog code for FSM using Moore & Mealy models
b) Verify the code using test bench
PROGRAMME:
Moore Machine:
module moore (Clock, w, Resetn, z);
input Clock, w, Resetn;
output z;
reg [1:0] y, Y;
parameter A = 2'b00, B = 2'b01, C = 2'b10;
always @(w or y)
begin
case (y)
A: if (w == 0) Y = A;
else Y = B;
B: if (w == 0) Y = A;
else Y = C;
C: if (w == 0) Y = A;
else Y = C;
default: Y = 2'bxx;
endcase
end
always @(posedge Clock or negedge Resetn)
begin
if (Resetn == 0)
y <= A;
else
y <= Y;
end
assign z = (y == C);
endmodule
66
OUTPUT WAVEFORM:
Fig.40
else
moore_state=st2;
end
st2 :
begin
z=0;
if (a)
moore_state =st3;
else
moore_state =st0;
end
st3:
begin
if (a)
begin
moore_state =st0;
z=1;
end
else
begin
moore_state =st2;
z=0;
end
end
endcase
end
endmodule
OUTPUT WAVEFORM:
68
Fig.41
Mealy Machine:
module mealy (Clock, w, Resetn, z);
input Clock, w, Resetn ;
output z ;
reg z;
reg y, Y;
parameter A = 0, B = 1;
always @(w or y)
case (y)
A: if (w == 0)
begin
Y = A;
z = 0;
end
else
begin
Y = B;
z = 0;
end
B: if (w == 0)
begin
Y = A;
z = 0;
end
else
begin
Y = B;
z = 1;
end
endcase
69
Fig.42