Lecture 3: Logic Systems, Data Types,
and Operators for Modeling in Verilog
HDL
1
Variables and Logic Value Set
Variables: represent the values of signals in a circuit
Two kinds of variables: Nets and Registers
Nets: represent the structural connectivity in a circuit
Registers: represent storage elements
Logic Value Set
Logic Value Interpretation
0 Logic 0, or false condition
1 Logic 1, or true condition
x represent an unknown logic value
z represent a high impedance condition
2
Data Types: Nets
Nets for connectivity:
wire establishes connectivity
tri same as wire and it will be tri-stated in hardware
wand a net has multiple drivers, wires and, i.e., open
collector circuit
wor a net has multiple drivers, wired or, i.e., emitter
coupled circuit
triand a net that has multiple drivers. It models wired-and. It is
tri-stated.
trior a net that has multiple drivers. It models wired-or. It
is tri- stated.
supply0 a global net connected to the circuit ground
supply1 a global net connected to the power supply
tri0 a net connected to the ground by a resistive
pulldown connection.
tri1 a net connected to the power supply by a resistive
pullup connection.
trireg a net that models the charge stored on a physical
net.
3
Data Types: Registers
Registers for storage
A register variable is an abstraction of a hardware storage
element, but it needs not correspond to a physical storage
element in a circuit.
An example
module synDFF (q, d, clock);
output q;
input clock, d;
reg q;
always @(negedge clock)
q <= d;
endmodule
4
Net Declaration
wire [7:0] data_bus; // 8 bit bus, data_bus[7] is MSB
wire [0:3] control_bus; // control_bus[0] is MSB
data_bus[3] // access data_bus bit 3
data_bus[3:0] // access bit 3 to bit 0 of data_bus
data_bus[k+2] // access a bit of the data_bus,
// depending on k+2
wire scalared [31:0] bus_a // bits can be accessed individually
or
// part-select, by default a net is scalar.
wire vectored [31:0] bus_a // access bus_a as a vector,
individual // bits may not be
referenced.
wire y, x, z; // y, x, z are three wires
wand A, B,C; // A, B, C wired and nets
trireg [7:0] A; // declare A as charge storage net
wire A = B + C,
D = E + F; // declarations and assignments
5
What if a wire or tri type net is driven
by multiple driver?
Verilog issues a warning and determines the value by pairwise
application of the following table
wire/tri 0 1 x z
0 0 x x 0
1 x 1 x 1
x x x x x
z 0 1 x z
Design engineers don't want to drive a wire with more than
one signals.
Undeclared nets will default implicitly to type wire.
6
BUF and NOT gates
BUF input output
0 0
1 1
x x
z x
NOT input output
0 1
1 0
x x
z x
7
BUFIF and NOTIF gates
Bufif0 control input
0 1 x z
data 0 0 z L L
input 1 1 z H H
x x z x x
z x z x x
Bufif1 control input
0 1 x z
data 0 z 0 L L
input 1 z 1 H H
x z x x x
z z x x x
8
BUFIF and NOTIF gates
Notif0 control input
0 1 x z
data 0 1 z H H
input 1 0 z L L
x x z x x
z x z x x
Notif1 control input
0 1 x z
data 0 z 1 H H
input 1 z 0 L L
x z x x x
z z x x x
9
What is the initial value of a net?
A net driven by a primitive , module, or continuous
assignment has a value "x" at the start of simulation.
A net without any drivers is default to "z".
wire a, b, c;
assign a = b+ c; // initial value by default b = z, c = z, a = x
The initial value for a register variable is by default also "x".
10
Wired logic
triand
/wand 0 1 x z
0 0 0 0 0
1 0 1 x 1
x 0 x x x
z 0 1 x z
trior
/wor 0 1 x z
0 0 1 x 0
1 0 1 1 1
x x 1 x x
z 0 1 x z
11
Verilog examples
module good_wand;
reg a, b, c;
wire w_nor, w_buf. w_mult;
wand w_wand;
nor (w_nor, a, b);
buf (w_buf, c);
nor (w_mult, a, b); // incorrect connecting
buf (w_mult, c); // two outputs w_mult
nor (w_wand, a, b);
buf (w_wand, c);
endmodule
12
Register Data Types
Rgister Data Types: reg, integer, time, realtime
Register type Usage
reg Stores a logic value
integer Supports computation
time Supports time as a 64-bit unsigned number
real Stores values (e.g., delay) as real numbers
realtime Stores time values as real numbers
A register may never be the output of a primitive gate, or the
target of a continuous assignment
13
Verilog Example: using register
module synTriState (bus, in, driveEnable);
input in, driveEnable;
output bus;
reg bus;
always @(in or driveEnable)
if (driveEnable)
bus = in;
else bus = 1`bz;
endmodule
14
Initial Value of a Register Variable
reg A, B;
initial
begin
A = 0; // assign an initial value to A
B = 1; // assign an initial value to B
end
// All registers have an initial value of "x" by default.
15
Passing Variables Through Ports
Port Mode
Variable Type Input Output InOut
net variable yes yes yes
register variable no yes no
input port of a module is of type net.
output port of a module is of type net, or reg.
inout port of a module is of type net.
16
Memory Declaration
Memory Declaration
reg [31:0] m [0:8191]; // 8192 x 32 bit memory
reg [12:0] pc; // 13 bit program counter
reg [31:0] acc; // 32 bit accumulator
reg [15:0] ir; // 16 bit instruction register
reg ck; // a clock signal
17
Hierarchical De-referencing
module test_Add_rca_4();
reg [3:0] a,b;
reg c_in;
wire [3:0] sum;
wire c_out;
initial
begin
$monitor ($time,, "c_out= %b c_in4=%b c_in3=%b c_in2=%b
c_in=%b ",
c_out, M1.c_in4, M1.c_in3, M1.c_in2, c_in);
end
initial
begin
// stimus patterns generated here
end
Add_rca_4 M1 (sum, c_out, a, b, c_in);
endmodule
18
Verilog model: 4 bit RCA
module Add_rca_4 (sum, c_out, a, b, c_in);
output [3:0] sum;
output c_out;
input [3:0] a, b;
input c_in;
wire c_out, c_in4, c_in3, c_in2;
Add_full G1 (sum[0], c_in2, a[0], b[0], c_in);
Add_full G2 (sum[1], c_in3, a[1], b[1], c_in2);
Add_full G3 (sum[2], c_in4, a[2], b[2], c_in3);
Add_full G2 (sum[3], c_out, a[3], b[3], c_in4);
endmodule
19
Parameters Substitution
module modXnor (y_out, a, b);
parameter size=8, delay=15;
output [size-1:0] y_out;
input [size-1:0] a, b;
wire [size-1:0] #delay y_out=a~^b;
endmodule
module Param;
wire [7:0] y1_out;
wire [3:0] y2_out;
reg [7:0] b1, c1;
reg [3:0] b2, c2;
modXnor G1 (y1_out, b1, c1);
modXnor #(4, 5) G2 (y2_out, b2, c2);
endmodule
20
Indirect Parameters Substitution
module modXnor (y_out, a, b);
parameter size=8, delay=15;
output [size-1:0] y_out;
input [size-1:0] a, b;
wire [size-1:0] #delay y_out=a~^b;
endmodule
module hdref_Param;
wire [7:0] y1_out;
wire [3:0] y2_out;
reg [7:0] b1, c1;
reg [3:0] b2, c2;
modXnor G1 (y1_out, b1, c1);
modXnor G2 (y2_out, b2, c2);
endmodule
module annotate;
defparam
hdref_Param.G2.size = 4,
hdref_Param.G2.delay = 5;
endmodule
21
Verilog Model Example
Design a 4-to-1 mux by cascading 2-to-1 muxes.
module mux2to1 (f, a, b, sel);
output f;
inputa, b, sel;
and g1 (f1, a , nsel),
g2 (f2, b, sel);
or g3 (f, f1, f2);
not g4 (nsel, sel);
endmodule
module mux4to1 (f, a, b, c, d, sel0, sel1);
output f;
inputa, b, c, d, sel0, sel1;
wire w1, w2;
mux2to1 m1 (w1, a, b, sel0),
m2 (w2, c, d, sel0),
m3 (f, w1, w2, sel1);
endmodule
22
module test_mux4to1 (a, b, c, d, sel0, sel1, f);
// generating all inputs to the mux4to1,
// receiving f from the mux4to1 output
inputf;
output a, b, c, d, sel0, sel1;
reg a, b, c, d, sel0. sel1;
initial begin
$monitor ($time,, "a = %b, b = %b, c = %b, d = %b,
sel1 = %b, sel0 = %b, f = %b", a, b, c, d, sel1, sel0, f);
a = 1; b =0; c =1; d =0;
sel = 0; sel0 = 0;
#10 sel1= 0; sel0 = 1;
#10 sel1 = 1; sel0 = 0;
#10 sel 1 = 1; sel0 = 1;
#10 a = 0; b =0; c= 1; d = 1;
sel = 0; sel0 = 0;
#10 sel1= 0; sel0 = 1;
#10 sel1 = 1; sel0 = 0;
#10 sel 1 = 1; sel0 = 1;
#10 $finish;
end
endmodule 23
module testbench;
wire a, b, c, d, sel0, sel1, f;
test_mux4to1my_tester (a, b, c, d, sel0, sel1, f);
mux4to1 my_design (f, a, b, c, d, sel0, sel1);
endmodule
the simulation output should look similar to the following
0 a =1, b = 0, c= 1, d= 0, sel1= 0, sel0=0. f = 1
10 a =1, b = 0, c= 1, d=0, sel1=0, sel0=1, f = 0
20
30
40
50
60
70
24