Design Domain and Levels of Abstraction
Design Domain and Levels of Abstraction
• Revised in 2001
Basic Limitation of Verilog
Description of digital systems only
Module
module my_module(out1, .., inN);
f .. // declarations
inN outM .. // description of f (maybe
.. // sequential)
endmodule
Everything you write in Verilog must be inside a module
exception: compiler directives (can be inside or out side)
Ex: `include
`if def
Example: Half Adder
A S assign S = A ^ B;
Half assign C = A & B;
Half
B Adder
Adder C
endmodule
Main Language Concepts (i)
Verilog has its own language constructs in most cases they
resemble like c language
Verilog is case sensitive
• Case sensitivity
– myid Myid
Comments
• /* Multiple line
comment */
work */
Verilog Value Set
• 0 represents low logic level or false condition
No
Noof of Binary
Binary
bbororBB Consecutive chars
bits Octal o or O Consecutive chars
bits Octal o or O 0-f,
0-f,x,x,zz
Decimal
Decimal
ddororDD
Hexadecimal
Hexadecimal hhororHH
– 8’h ax = 1010xxxx
– 12’o 3zx7 = 011zzzxxx111
Numbers in Verilog (ii)
• You can insert “_” for readability
– 12’b 000_111_010_100
– 12’b 000111010100 Represent the same number
– 12’o 07_24
• Bit extension
– MS bit = 0, x or z extend this
• 4’b x1 = 4’b xx_x1
– MS bit = 1 zero extension
• 4’b 1x = 4’b 00_1x
Numbers in Verilog (iii)
• If size is ommitted it
– is inferred from the value or
– takes the simulation specific number of bits or
– takes the machine specific number of bits
assumed
– 15 = <size>’d 15
Nets (i)
• Can be thought as hardware wires driven by logic
• Equal z when unconnected
• Various types of nets
– wire
– wand (wired-AND)
– wor (wired-OR)
– tri (tri-state)
• In following examples: Y is evaluated,
automatically, every time A or B changes
Nets (ii)
A wire Y; // declaration
Y
B assign Y = A & B;
wand Y; // declaration
assign Y = A;
A assign Y = B;
Y
B
wor Y; // declaration
assign Y = A;
assign Y = B;
dr
tri Y; // declaration
A Y
assign Y = (dr) ? A : z;
Registers
• Variables that store values
• Do not represent real hardware but ..
• .. real hardware can be implemented with registers
• Only one type: reg
reg A, C; // declaration
// assignments are always done inside a procedure
A = 1;
C = A; // C gets the logical value 1
A = 0; // C is still 1
C = 0; // C is now 0
• Register values are updated explicitly!!
Vectors
• Represent buses
wire [3:0] busA;
reg [1:4] busB;
reg [1:0] busC;
• Left number is MS bit
• Slice management
busC[1] = busA[2];
busC = busA[2:1];
busC[0] = busA[1];
• No multi-dimentional arrays
reg var[1:10] [1:100]; // WRONG!!
• Escaped chars:
– \n newline
– \t tab
– %% %
– \\ \
– \“ “
Logical Operators
• && logical AND
• || logical OR
• ! logical NOT
• Operands evaluated to ONE bit value: 0, 1 or x
• Result is ONE bit value: 0, 1 or x
A = 6; A && B 1 && 0 0
B = 0; A || !B 1 || 1 1
C = x; C || B x || 0 x
but
butC&&B=0
C&&B=0
Bitwise Operators (i)
•& bitwise AND
•| bitwise OR
•~ bitwise NOT
•^ bitwise XOR
• ~^ or ^~ bitwise XNOR
• a = 4’b1010;
b = 4’b1100;
c = a ^ b;
• a = 4’b1010;
b = 2’b11;
Reduction Operators
• & AND
• | OR
• ^ XOR
• ~& NAND
• ~| NOR
• ~^ or ^~ XNOR
A
1
Y
B Y = (sel)? A : B;
0
sel
Arithmetic Operators (i)
• +, -, *, /, %
• Negative integers:
– can be assigned negative values
– different treatment depending on base specification or not
reg [15:0] regA;
integer intA;
..
intA = -12/3; // evaluates to -4 (no base spec)
intA = -’d12/3; // evaluates to 1431655761 (base spec)
Operator Precedence
Use parentheses to
enforce your
priority
Hierarchical Design
Top
TopLevel
Level E.g.
Module
Module
Full
FullAdder
Adder
Sub-Module
Sub-Module Sub-Module
Sub-Module
11 22
Half
HalfAdder
Adder Half
HalfAdder
Adder
Basic
BasicModule
Module Basic
BasicModule
Module Basic
BasicModule
Module
11 22 33
Module
module my_module(out1, .., inN);
f .. // declarations
inN outM .. // description of f (maybe
.. // sequential)
endmodule
A S assign S = A ^ B;
Half assign C = A & B;
Half
B Adder
Adder C
endmodule
Example: Full Adder
in1 A Half S I1 A Half S sum
Half Half
Adder
Adder11 I2 Adder
Adder
in2 B C B C I3
ha1
ha1 ha2
ha2 cout
cin
module full_adder(sum, cout, in1, in2, cin);
output sum, cout;
input in1, in2, cin;
endmodule
Hierarchical Names
ha2.A
cin
module
module
net net
• Inouts
Continuous Assignements
a closer look
• Syntax:
assign #del <id> = <expr>;
optional
optional net
nettype
type!!!!
• Where to write them:
– inside a module
– outside procedures
• Properties:
– they all execute in parallel
– are order independent
– are continuously active
Structural Model (Gate Level)
• Built-in gate primitives:
and, nand, nor, or, xor, xnor, buf, not, bufif0,
bufif1, notif0, notif1
• Usage:
nand (out, in1, in2); 2-input NAND without delay
and #2 (out, in1, in2, in3); 3-input AND with 2 t.u. delay
not #1 N1(out, in); NOT with 1 t.u. delay and instance name
xor X1(out, in1, in2); 2-input XOR with instance name
• Write them inside module, outside procedures
Example: Half Adder,
2nd Implementation
A module half_adder(S, C, A, B);
S
output S, C;
B input A, B;
C
wire S, C, A, B;
initial
$display(“I’m first”); Will
Willbebedisplayed
displayed
atatsim
simtime
time00
initial begin
#50; Will
Willbe bedisplayed
displayed
$display(“Really?”); atatsim
simtime
time50
50
end
endmodule
“Always” Blocks
• Start execution at sim time zero and
continue until sim finishes
Events (i)
• @
always @(signal1 or signal2 or ..) begin
..
end execution
executiontriggers
triggersevery
every
time
timeany
anysignal
signalchanges
changes
endmodule
Events (ii)
• wait (expr)
always begin
wait (ctrl)
execution
executionloops
loopsevery
every
#10 cnt = cnt + 1;
#10 cnt2 = cnt2 + 2;
time
timectrl
ctrl==11(level
(level
sensitive
sensitivetiming
timingcontrol)
control)
end
d
initial
initial begin
begin
#5
#5 cc == 1;
1; c
#5
#5 bb == 0;
0;
#5 b
#5 dd == c;
c;
end
end
0 5 10 15
Time
Each assignment is
blocked by its previous one
Timing (ii)
d
initial
initial begin
begin
fork
fork c
#5
#5 cc == 1;
1;
#5 b = 0; b
#5 b = 0;
#5
#5 dd == c;
c;
join 0 5 10 15
join
end
end Time
Assignments are
not blocked here
Procedural Statements: if
E.g. 4-to-1 mux:
module mux4_1(out, in, sel);
output out;
if (expr1) input [3:0] in;
true_stmt1; input [1:0] sel;
reg out;
else if (expr2) wire [3:0] in;
wire [1:0] sel;
true_stmt2;
.. always @(in or sel)
if (sel == 0)
else out = in[0];
def_stmt; else if (sel == 1)
out = in[1];
else if (sel == 2)
out = in[2];
else
out = in[3];
endmodule
Procedural Statements: case
E.g. 4-to-1 mux:
module mux4_1(out, in, sel);
case (expr) output out;
input [3:0] in;
input [1:0] sel;
item_1, .., item_n: stmt1;
reg out;
item_n+1, .., item_m: stmt2; wire [3:0] in;
.. wire [1:0] sel;
reg [3:0] Y;
wire start;
integer i;
initial
Y = 0;
reg [3:0] Y;
wire start;
integer i;
while (expr) stmt;
initial
Y = 0;
E.g.
module count(Y, start);
output [3:0] Y;
input start;
initial
Can
Canbebeeither
eitheran
an Y = 0;
integer
integeror
oraavariable
variable
always @(posedge start)
repeat (4) #10 Y = Y + 1;
endmodule
Procedural Statements:
forever
Typical example:
clock generation in test modules
module test;
endmodule
Mixed Model
Code that contains various both structure and behavioral styles
module simple(Y, c, clk, res);
output Y;
input c, clk, res;
reg Y;
wire c, clk, res;
res wire n;
c n Y not(n, c); // gate-level
clk
always @(res or posedge clk)
if (res)
Y = 0;
else
Y = n;
endmodule
System Tasks
Always
Alwayswritten
writteninside
insideprocedures
procedures
endmodule endmodule
Parameter
s (ii)
module top(out, in, clk);
output [1:0] out;
A. input [3:0] in;
A.Implelementation
Implelementation input clk;
without
withoutparameters
parameters(cont.)
(cont.)
wire [1:0] out;
wire [3:0] in;
wire clk;
endmodule
Parameters
(iii)
module top(out, in, clk);
B.
B.Implelementation
Implelementation output [1:0] out;
with input [3:0] in;
withparameters
parameters input clk;
wire [1:0] out;
module dff(Q, D, clk); wire [3:0] in;
wire clk;
parameter WIDTH = 4;
output [WIDTH-1:0] Q; wire [3:0] p_in;
input [WIDTH-1:0] D; wire wu, wd;
input clk;
assign wu = p_in[3] & p_in[2];
reg [WIDTH-1:0] Q; assign wd = p_in[1] & p_in[0];
wire [WIDTH-1:0] D;
wire clk; dff instA(p_in, in, clk);
// WIDTH = 4, from declaration
always @(posedge clk) dff instB(out, {wu, wd}, clk);
Q = D; defparam instB.WIDTH = 2;
// We changed WIDTH for instB only
endmodule
endmodule
Testing Your
Modules
module top_test;
wire [1:0] t_out; // Top’s signals
reg [3:0] t_in;
reg clk;
endmodule
The Veriwell Simulator
• Assuming that modules dff, top and top_test
reside in files dff.v, top.v and top_test.v
respectively, run:
~hy225/veriwell/sparc_bin/veriwell dff.v top.v
top_test.v
• result:
.. (initial messages)
0 xxxx -> xx
5 0101 -> xx
25 1110 -> xx
30 1110 -> 00
45 1111 -> 00
50 1111 -> 10
70 1111 -> 11
.. (final messages)