Lab Sheet 3 Final
Lab Sheet 3 Final
Learning Objectives:
i) Introduction to Sequential circuits
ii) Blocking and Non-Blocking Assignments
iii) Sequential and Parallel blocks
iv) Finite State Machine Implementation (Mealy Machine)
v) 4-bit Shift Register Implementation
Introduction:
In the first two labs, we have learned how to simulate digital circuits using different types
of modeling (i.e. Gate level, Data flow, Behavioral) on combinational circuit simulation
in VeriLog. In this lab, we will learn how to simulate digital circuits using different types
of modeling on sequential circuit simulation in VeriLog.
A Sequential circuit is a circuit made up by combining logic gates such that the required
logic at the output(s) depends not only on the current input logic conditions but also on
the past inputs, outputs and sequences.
Sequential Circuits has a feedback of the output(s) from a stage to the input of either that
stage or any previous stage.
Problem 1:
// Test Bench
module Testing;
dff_sync_clear dff (d, clk, rst, q); // Or dff_async_clear dff (d, clk,
rst, q);
always @ (a or b or c)
begin
x = a | b; 1.Evaluate a|b, assign result to x.
y = a ^ b ^ c; 2.Evaluate a^b^c, assign result to y.
z = b & ~c; 3.Evaluate b& ~c, assign result to z.
end
In Nonblocking assignment, all assignments deferred until all right-hand sides have been
evaluated (end of simulation timestep).
always @ (a or b or c)
begin
x <= a | b; 1.Evaluate a|b, but differ assignment of x.
y <= a ^ b ^ c; 2.Evaluate a^b^c, but differ assignment of y.
z <= b & ~c; 3.Evaluate b& ~c, but differ assignment of z.
end 4.Assign x, y, and z with their new values
q1 q2
in D Q D Q D Q out
Example - 1: Example - 2:
The Example - 1 is showing the sequential block without delays, All the statements
written inside the begin-end will execute sequentially and after the execution of initial
block, final values are a=1, b=0 and c=1
The Example - 2 is showing the sequential block with delays, In this case, the same
statements are given with some delays, Since All the statements execute sequentially, the
a will get value 1 after 5 time unit, b gets value after 15 time unit and c will take value 1
after 30 time unit
Parallel Block:
The statements are written inside the parallel block, execute parallel, If the sequencing is
required then it can be given by providing some delays before the statements. In parallel
blocks, all the statements occur within fork and join
Example - 1: Example - 2:
In Example-1, all the statements written inside the fork and join, executes parallel, it
means the c with have value ‘1′ after 15 time unit.
In Example-2, the initial block contains begin-end and fork-join both. In this case c takes
value after 15 time unit, and d takes the value after 30 time unit.
Mealy Machine:
A Mealy machine is a finite state transducer that generates an output based on its current
state and input. This means that the state diagram will include both an input and output
signal for each transition edge.
Solution:
module mealy( clk, rst, inp, outp);
else begin
case( state )
2'b00: begin
if( inp ) begin
state <= 2'b01;
outp <= 0;
end
else begin
state <= 2'b10;
outp <= 0;
end
end
2'b01: begin
if( inp ) begin
state <= 2'b00;
outp <= 1;
end
else begin
state <= 2'b10;
outp <= 0;
end
end
2'b10: begin
if( inp ) begin
state <= 2'b01;
outp <= 0;
end
else begin
state <= 2'b00;
outp <= 1;
end
end
default: begin
state <= 2'b00;
outp <= 0;
end
endcase
end
end
endmodule
// Test Bench
module mealy_test;
initial
begin
clk = 0;
rst = 1;
sequence = 16'b0101_0111_0111_0010;
#5 rst = 0;
task testing;
for( i = 0; i <= 15; i = i + 1)
begin
inp = $random % 2;
#2 clk = 1;
#2 clk = 0;
$display("State = ", duty.state, " Input = ", inp, ", Output =
", outp);
end
endtask
endmodule
Following figure shows a 4-bit shift register that uses D flip-flops for the individual
storage elements.
initial
Q=4'd10;
always @(posedge CLK)
begin
if (EN)
Q={in,Q[n-1:1]};
end
endmodule
module shiftregtest;
parameter n= 4;
reg EN,in , CLK;
wire [n-1:0] Q;
//reg [n-1:0] Q;
shiftreg shreg(EN,in,CLK,Q);
initial
begin
CLK=0;
end
always
#2 CLK=~CLK;
initial
$monitor($time,"EN=%b in= %b Q=%b\n",EN,in,Q);
initial
begin
in=0;EN=0;
#4 in=1;EN=1;
#4 in=1;EN=0;
#4 in=0;EN=1;
#5 $finish;
end
endmodule
Exercises:
(Q.1) Implement Binary 4-bit Synchronus counter using J-K Flip Flops. Verify the
design by writing a testbench module.
(Q.2.) Design a FSM (Finite State Machine) to detect a sequence 10110.
(Q.3.) Design and implement a four-bit serial adder. Make use of four-bit shift register
constructed in Problem 2. Verify the design by writing a testbench module.