ELEC2141 Week 7 After Lecture
ELEC2141 Week 7 After Lecture
Verilog HDL
1
Summary
Moore and Mealy sequential circuits
State reduction
State assignment
Sequential circuit design
Circuit synthesis
Moore to Mealy/Mealy to Moore conversion
2
Overview
Hardware Description Languages
Verilog
Module declaration
Test bench
Dataflow modelling
Behavioural modelling
Sequential circuit implementation
3
Hardware Description Language (HDL)
A hardware description language (HDL) is a computer
based language that describes the hardware of digital
systems in textual form
It is similar to the ordinary programming language such
as C, but specifically oriented for describing the
structure and behavior of logic circuits
-
-
Why HDL?
To facilitate the design and testing of digital circuits
Designing and testing of practical digital circuits involves
large number of circuit components, and computer
aided tools need to be used
Computer based design tools are required to leverage
the creativity and effort of a designer and reduce the
risk of producing a flawed design
CAD design tools rely on HDL
-
---
A module is fundamental descriptive unit (or component)
in the Verilog language
It is used to define a circuit or a sub circuit
a-
It is declared by the keyword module and must always be
a
Module Declaration Is
⇐
OK AND outputs
input AND
- - ⇒
⇒ -
intercom - wires
Module Declaration
The gates that form the circuit are specified from 12
predefined primitive gates
=
=
e.g.
A
and G1(C,A,B); G1 C
B
-
Module Declaration
Verilog HDL for the following combinational logic
A W1
B G1 D
G3
C G2 E
A W1
B G1 D
G3
C G2 E
module Simple_Circuit_Prop_delay(A,B,C,D,E);
output D,E;
input A,B,C;
wire w1;
←
→ 0123
-
I
-
⇐
E
input A,B,E;
wire A_b, B_b, E_b;
G6(D[2],B_b,A,E_b), G7(D[3],B,A,E_b);
endmodule
Test bench
To simulate a circuit with an HDL, inputs are applied to
the circuit so the simulator can generate an output
response
A HDL description that provides the stimulus to a design
is a test bench and consists of a signal generator and
=
instantiation of the module to be verified
A test bench has no input or output ports as it does not
interact with its environment
Initial statements are used to provides initial values and
-
'
2 boo
initial ←
begin
A=1’b0; B=1’b0;C=1’b0; ←
Ill
module test_module_name;
// declare local reg and wire identifiers
// instantiate the design module under test
// specify a stopwatch using $finish to terminate the simulation
// generate stimulus using initial and always statements
// display the output response (text or graphics)
endmodule
Test bench
The response to the stimulus will appear in text format as standard output
and as waveforms
Numerical outputs are displayed by using Verilog system tasks which are
built-in system functions recognized by keywords that begin with a symbol
The syntax for $display, $write and $monitor is of the form
Taskname (format specification, argument list);
$display (“%d %b %b”, C, A,B); specifies the display of C in decimal and A and B in binary
module Circuit_below(A,B,C,F1,F2); A , ,
D
output F1,F2; *TT
plz ] DID ,
input A,B,C;
,
£
and g2(T2,A,B,C);
and g3(E1,A,B);
and g4(E2,A,C); A T2
and g5(E3,B,C); B g2
C g9 F1
or g6(F2,E1,E2,E3);
not g7(F2_b,F2); A T1
and g8(T3,T1,F2_b); B g
'
or g9(F1,T2,T3); C g8 T3
endmodule A El g1
g3 F2_b
B
A EV
gu g6 F2
C # z
B E3
C gas
Test bench
7-
DE 2
] -
A
Dfl ] -
B
// Gate-level description of circuit D Co ] -
C
module test_circuit;
reg[0:2] D;
¥X⇒
wire F1,F2; - 5¥
Circuit_below M2(D[2],D[1],D[0],F1,F2);
initial
- - -
at
begin
(
D=3’b000;
repeat(7) #10 D= D+ 3’b001;
end
initial
$monitor (“ABC=%b F1= %b F2 = %b”, D, F1, F2);
-
endmodule
Circuit modeling
Circuits can be modelled using
Structural modeling:– here a set of interconnected
components representing the functionality of the
circuit is used
Dataflow modeling: here a set of concurrent
assignment statements are used to assign values to
signals and model the flow of data
Behavioural modeling: here procedural blocks or
processes are defined to represent the behaviour of
circuits
27
Dataflow modeling
Combinational logic can be expressed in Verilog with
Boolean equations
Done with a continuous assignment statement consisting
of the keyword assign followed by a Boolean expression
The symbols for logical operators in Verilog are AND(&),
OR(|), and NOT(~)
e.g. E= A + BC +B’D and F = B’C + BC’D’
// Verilog model: Circuit with Boolean expressions
module Circuit_Boolean_CA(E,F,A,B,C,D);
output E,F;
input A,B,C,D;
assign E = A|(B&C)|(~B&D);
assign F = (~B&C)|(B&~C&~D);
endmodule
Dataflow modeling
Verilog HDL provides 30 // module decoder_2x4_df(
different operators output [0:3] D,
input A,B,enable);
Symbol Operation
+ binary addition assign D[0] = ~A&~B&enable,
D[1] = ~A&B&enable,
- Binary subtraction
D[2] = A&~B&enable,
& Bitwise AND D[3] = A&B&enable;
| Bitwise OR endmodule
^ Bitwise XOR // module binary_adder(
~ Bitwise NOT output [3:0] sum,
== Equality output C_out,
> Greater than input [3:0] A,B
input C_in);
< Less than
{ } Concatenate assign {C_out,Sum} = A+B+ C_in;
?: Conditional endmodule
Dataflow modeling
HDL for module with two four bit inputs A and B and
three outputs
One output (A_lt_B) is logic 1 if A is less than B, a second
output (A_eq_B) is logic 1 if A is equal to B. A third
output (A_gt_B) is logic 1 if A is greater than B
module mag_compare(
output A_lt_B, A_eq_B, A_gt_B,
input [3:0] A,B);
endmodule
Dataflow modeling
Conditional operator (? :) takes three operands:
condition? true-expression: false-expression;
When the condition is evaluated, if the result is logic
1, the true expression is evaluated but if the result
is logic 0, the false expression is evaluated
assign OUT = select ? A:B;
Specifies that OUT = A if select is 1 else OUT= B
module mux_2X1_df(m_out, A, B,select)
output m_out;
input A,B, select;
endmodule
Behavioural modeling
Behavioural modeling performed in HDL for digital circuits at a
functional and algorithmic level.
Mostly used for sequential circuits, but can be also used for
combinational circuits
It uses the keyword always; always blocks loop to execute over and
over again
Syntax for always
always @(the event control expression)
list of statements (procedural assignment statements)
The target output of procedural statements must be of reg data type
because a reg data type retains its value until a new value assigned
This is contrary to the wire data type ( output data type), where the
target is continuously update
Behavioural modeling
// Behavioral description of two-to-one line multiplexer
if (select == 1) m_out = A;
else m_out = B;
endmodule
34
Behavioural modeling
// Behavioural description of four-to-one line multiplexer
module mux_2X1_beh
(output reg m_out,
input in_0,in_1,in_2,in_3,
input [1:0] select);
endmodule
Flip-flops and latches
// Description of D flip-flop // Description of D flip-flop with
asynchronous rest
module D_FF (Q,D,clk);
output Q; module DFF
input D, clk; (output reg Q,
reg Q; input clk,D, rst);
endmodule endmodule
Flip-flops and latches
HDL model for JK flip-flop from characteristic
// Description of JK FF from characteristic table
2’b00: Q<=Q;
2’b01: Q<=1’b0;
2’b10: Q<=1’b1;
2’b11: Q<= ~Q;
endcase
endmodule
HDL models for state diagram
HDL models for the operation of sequential circuit can be
based on the state diagram
0/0 1/0
0/1
00 10
0/1
1/0 0/1 1/0
01 11
1/0
HDL model for state diagram
HDL model for the operation of sequential circuit can be
based on the state diagram
// Mealy FSM zero detector
module Mealy_Zero_Detector(
output reg y_out,
input X_in, clock, reset
);
reg[1:0] state,next_state;
parameter S0=2’b00, S1=2’b01,S2=2’b10,S3=2’b11;
endmodule
1 1
0
00 11
0
0 0
1
01 10
1
HDL model for state diagram
// Moore model FSM
module Moore_Model(
output [1:0] y_out,
input X_in, clock, reset
);
endmodule
Structural description:
clocked sequential circuits
Sequential circuits are composed of combinational logic
and flip-flops.
Combinational part can be described with assign
statements and Boolean equations.
The flip-flops are described with an always statement.
The separate modules can be combined to form a
structural model by instantiation within a module.
Structural description:
clocked sequential circuits
0 0
00/ 1 01/
0 0
1 1
0 0
11/ 1 10/
1 0
Structural description:
clocked sequential circuits
// structural model // structural model
module Moore_Model_One( module TFF(Q,T,CLK,RST_b);
output y_out,A,B, output Q;
input X_in, clock, reset input T, CLK,RST_b;
); reg Q;
TFF MA(A,TA,clock,reset);
TFF MB(B,TB,clock,reset);
end module