Lecture 4

ECE 526
Compiler Directives
`timescale Directive
• Timescales use two numbers: the reference
time unit and the precision.
p
• The first number sets the unit of
measurements for times and delays.
delays
• The second sets the precision, that is, the
smallest unit of time that the simulator will
track without rounding.
2
`timescale Rules
• `timescale uses back tick (`), not an
apostrophe (‘).
• The units of measurement can never be less
than the precision.
– i.e. `timescale 1 ns / 10 ns is ILLEGAL
• Only 1, 10 and 100 are valid numbers for
specifying either time units or precision.
– i.e. `timescale 1 ns / 500 ps is ILLEGAL
3
`timescale is a Directive
• `timescale is a Compiler Directive.
• This means that it does NOT follow certain
Verilog HDL conventions.
– 11. The directive does NOT end in a semicolon
semicolon.
– 2. It is set BEFORE the module/endmodule
structure.
structure
4
Meaning of Timescale Numbers
• Each Timescale directive has two numbers:
– `timescale 100 ps / 10 ps
• The first number says what the units of time
in the file are going to mean.
– #10 CLK <= 1’b0;
– Wait for 10 time units, then assign a value of
zero to signal CLK.
– 10 time units means 1 ns here.
5
Examples of Good `timescale
timescale
Directives
• `timescale 1 ns / 1 ns
• `timescale
timescale 1 ns / 100 ps
• `timescale 100 ps / 10 ps
6
`timescale
timescale Compiler Directive
• `timescale compiler directive specifies the time unit and its
precision, which is used in measuring times and delays. It has
the following format:
``timescale
i
l <time_unit>/
i
i / <time_precision>
i
ii
Where:
<time_unit>
specifies the time unit.
<time_precision> specifies the precision used in
rounding
di delay
d l values
l
before
b f
using
i them
h in
i
simulation.
7
`timescale
timescale Compiler Directive
• Each of <time_unit> or <time_precision> consists of an integer,
to represent
p
the magnitude,
g
, followed by
y a time unit specifier.
p
• The valid values of the magnitude are 1, 10, 100.
• The valid time unit specifiers are: s(second),
and
ms(millisecond) = 10E-3 second,
us(microsecond) = 10E-6 second,
ns(nanosecond) = 10E-9
10E 9 second
second,
ps(picosecond) = 10E-12 second,
fs(femtosecond) = 10E-15 second.
• The `timescale compiler directive must be written outside the
boundary of the modules.
• <time_precision>
<time precision> must be at least as small as the <time_unit>
<time unit> .
8
Timescale Resolution
• The second timescale number specifies the
granularity in which the simulation will be
run.
– `timescale 100 ps / 10 ps
• This will cause rounding for any unit of
time less than 10 ps.
– and #1.26 ANDGATE1(OUT, IN1, IN2);
• OUT will change 130 ps after IN1 or IN2
9
`timescale
timescale Compiler Directive
Directi e
The time unit of the simulation is determined by the smallest
value of all the timescale directives.
directives
`timescale 10 ms/ 1 ms
// all time units are in tens of milliseconds
module \2*4_bin_dec (f0,f1,f2,f3,I1,I2);
input I1, I2;
output f0, f1, f2, f3;
The values in the modules are multiples
of 10 ms
ms, rounded to the nearest ms.
ms
Therefore, the not time delay will be rounded to
17 ms and the and time delay will be rounded to
26 ms.
not #1.66 not1 (\~I1 , I1);
not #1.66
#1 66 not2 (\~I2 , I2);
and #2.578 and1 (f0 , \~I1 , \~I2 );
If the time precision was 100 us instead,
and #2.578 and2 (f1 , I1 , \~I2 );
the not time delay would have been set to 16.6 ms
and #2.578 and3 (f2 , \\~I1
I1 , I2 );
and the and time delay would have been rounded to
and #2.578 and4 (f3 , I1 , I2 );
25.8 ms.
10
end module
Timescales and Time Delays
Outside of the module there should be a timescale
directive:
`timescale 1 ns / 1 ns
In the absence of such a directive, most simulators will
default to 1 ns / 1 ns. This is not guaranteed.
Even
E
en if 1 ns / 1 ns is what
hat you
o want,
ant it’s good polic
policy to
explicitly put it in each file. Failure to do so can have
surprising results.
11
Timescales and Hierarchy
• Even simple
p Verilog
g designs
g have hierarchyy
– Example: test bench instantiates DUT
• Verilog
g compiler
p
over-writes timescale
every time a new one is read.
• Thus a module containing
g time delays
y but
no timescale directive will use the last
timescale directive read, which may not be
what is wanted.
• Moral: if a module contains time delays, it
must also contain a timescale directive.
12
`define Compiler Directive
• The `define
define directive is used for text substitution.
substitution It has the
format:
`define
define <macro
<macro_name>
name> <macro
<macro_text>
text>
• When used in a Verilog code, the compiler will replace
`macro
macro_name
name with macro_text.
macro text
• The `define directive does not get affected by `resetall
compiler directive.
13
`define Compiler Directive
`define gate
and
`d fi gate_delay
`define
d l
#2
`define not_delay #1
module \2*4_bin_dec (f0,f1,f2,f3,I1,I2); module \2*4_bin_dec (f0,f1,f2,f3,I1,I2);
input I1, I2;
output f0, f1, f2, f3;
input I1, I2;
output f0, f1, f2, f3;
nott #1 not1
t1 (\~I1
(\ I1 , I1);
I1)
not `not_delay not1 (\~I1 , I1);
not #1 not2 (\~I2 , I2);
not `not_delay not2 (\~I2 , I2);
`gate `gate_delay and1 (f0 , \~I1 , \~I2 ); and #2 and1 (f0 , \~I1 , \~I2 );
and #2 and2 (f1 , I1 , \~I2 );
` t `gate_delay
`gate
` t d l and2
d2 (f1 , I1 , \~I2
\ I2 );
)
and #2 and3 (f2 , \~I1 , I2 );
`gate `gate_delay and3 (f2 , \~I1 , I2 );
and #2 and4 (f3 , I1 , I2 );
`gate `gate_delay and4 (f3 , I1 , I2 );
end module
end module
14
`define is dumb macro substitution
• It will just substitute whatever follows the
macro name.
–
–
–
–
–
–
`define whatever 1234;
`whatever
whatever will be 1234;
Semicolon will be included.
Usually that’s
that s not what is wanted
wanted.
`define whatever 1234
Now `whatever
whatever will be replaced with 1234.
1234
15
`define Macros and Parenthesis
• Define is for macro substitution.
– Example: `define CUCU a+4
• Then you want to multiply CUCU by 12
– `CUCU * 12
• What you get is
– a + 48, because multiplication has higher precedence
than
h addition.
ddi i
• To get 12a +48, use parenthesis
– `define
`d fi CUCU ((a+4)
+4)
16
https://www.synopsys.com/news/pubs/snug/boston2008/bresticker_paper.pdf
Trying Too Hard
• Multiple things to define: can they be
combined?
– Try `define one 1, two 2
• No go.
go
• Must use one `define per macro
17
Homework 1
• Homework 1 is now posted on the course
web site.
• It will be due in one week.
18
Lab Reports
The most important elements of lab reports
are:
– Statement of what you are accomplishing,
demonstrating, determining, etc.
– Methodology: statement of how you did it.
Specifically includes test plan.
– Analysis of results: what did you
demonstrate/prove.
– Conclusions.
C l i
19
Lab Reports
• All Verilog files need header and in-line
comments.
• Computer printouts (source code, test bench
code wave pictures
code,
pictures, log files) are
supporting documentation, NOT an lab
report by themselves.
themselves
• Your lab report should reference this
supporting documentation.
documentation
20
Behavioral Modeling
Modeling In Verilog
• Structural Modeling
– The entire hierarchy is built from primitives.
• Behavioral Modeling
– Algorithmic-Based Modeling
• In this modeling, a procedural code is used to describe the
input/output behavior without any details about actual hardware
implementation.
– Data Flow/RTL Modeling
• In Data Flow/RTL Modeling,
g, Verilogg HDL operators
p
are used to
describe the model behavior.
22
Four-bit
Four
bit Counter
23
Image © Texas Instruments
HDL Counter
reg [3:0] Count;
always @(posedge CLOCK) Count <= Count + 1;
/*Verilog oddity: “Count++” is NOT supported syntax*/
24
Customization is Easy
always @(posedge CLOCK) begin
if (COUNT == 27) COUNT <= 0;
else if ((COUNT == 13)) COUNT <= 15;;
else COUNT <= COUNT + 1;
end
T that
Try
h with
i h K-maps!
K
!
25
Modeling In Verilog
Full Adder Boolean Equations:
S=A^B^C
Co = A•B + A•C + B•C
A B
A
B
C
Co
Co
Full Adder
S
Ci
Ci
S
26
Structural ModelingA
module full_adder (Co, S, A, B, Ci);
input A, B, Ci;
output Co
Co, S;
xor (t1, A, B);
xor (S,
(S t1,
1 Ci)
Ci);
and (t2, A, B);
and (t3, A, Ci);
and (t4, Ci, B);
or (Co, t2, t3, t4);
endmodule
Co
B
Full Adder
Ci
S
A B
Co
Ci
S
27
Behavioral Modeling
Data Flow/RTL Modeling
module full_adder (Co, S, A, B, Ci);
input A, B, Ci;
output Co, S;
assign S = A^ B ^ Ci ;
assign Co = A&B | A&Ci | B&Ci ;
A
Co
B
Ci
Full Adder
S
endmodule
28
Behavioral Modeling
D t Fl
Data
Flow/RTL
/RTL Modeling
M d li
module
d l ffull_adder
ll dd (Co,
(C S,
S A,
A B,
B Ci)
Ci);
A
B
input A, B, Ci;
output Co, S;
Co
assign {Co, S} = A+ B+ Ci;
endmodule
Ci
Full Adder
S
Note use of addition operator. “+”
+ is NOT the same as logical and.
29
Behavioral Modeling
• If you were actually going to model something so trivial as
a single-bit adder, behavioral would be the way to go.
Structural HDL using primitives is useful for library
development, not for digital design.
{Co S} = A+ B+ Ci;
{Co,
• Modeling individual components at such a detailed level
d f t the
defeats
th purpose off using
i HDL andd synthesis.
th i
30
Basic Rules
• All the action takes place within “Modules.”
• One module per file is good practice.
practice
• Give modules and files meaningful names,
preferably the same names.
names
• Use synchronous design techniques
whenever
h
possible.
ibl
31
More Basic Rules
• Every file requires documentation,
especially in the header.
• Commenting code is not something you do
after the project is done, maybe, if you have
time. It is an integral part of the design.
• Unlike assembly language programming,
there is no premium on writing tight, cryptic
Verilog.
32
P
Procedural
d l Blocks
Bl k
•Procedural Blocks are used in test fixtures to apply stimulus and display
results .
• They are of two types:
* initial procedural blocks, which executes only once.
* always procedural blocks, which execute continuously in a loop.
initial Condition
begin
------------end
always Condition
begin
------------end
Initial procedural blocks are used not only for initialization but also for
all operations that need to be executed once.
33
Procedural Blocks
* Procedural blocks are activated at time 0 and executed
when the associated condition becomes TRUE.
* If there is no condition, procedural block is executed
immediatelyy followingg its activation.
* All procedural blocks are executed concurrently.
34
Procedural Blocks and Synthesis
• always blocks are synthesizable.
• initial blocks are not.
not
• What kind of sense does this make?
35
Initial and Always Blocks
• Both initial and always blocks are useful in
test fixtures.
• Only always blocks may be used in
synthesizable code,
code ii.e.
e circuit descriptions.
descriptions
36