Verilog HDL Basics
Verilog HDL Basics
Verilog HDL Basics
By Name
− counter dut (.count(count), .clk(clk), .reset(reset));
Comment and White Space
module MUX2_1 (out, sel, inb, ina);
// Port declarations, single line comments space, tab,
output out; and newline
Identifier:
Names of instances, modules, nets, ports, and variables
Keyword:
Language construct, lower case
Devices/Blocks
Storage
Examples:
reg a; // single bit
reg [3:0] b; // 4 bits
reg [7:0] c, d; // two 8bits regs
Parameters
Use module parameters to configure module
instances: parameter list_of_assignments
− A parameter is an un-typed constant
− You can use a parameter anywhere that you can use a
constant or liter
Correct Data Type
Number
[[<size>]’<radix>]<value>
− size is the size in bits
− B or B (binary), o or O (octal), d or D (decimal), h or H (hexadecimal)
Examples
12 unsized decimal (extended to 32 bits with "0")
'H83a unsized hexadecimal (extended to 32 bits with "0")
8'b1100_0001 8-bit binary, _ for readability
8'h61 8-bit hex
64'hff01 64-bit hexadecimal (extended to 64 bits with "0")
16'h456a 16-bit hex
9'O17 9-bit octal
32'bz01x extended to 32 bits with "z"
3’b1010_1101 3-bit number, truncated to 3’b101
6.3 decimal notation
32e-4 scientific notation for 0.0032
4.1E3 scientific notation for 4100
String
string constants, but has no explicit string data
type.
Store ASCII character strings in a reg array
Example:
− $monitor("This string formats a value: %b", monitored_object);
integer kkk;
kkk = $fopen(file);
Compiler Directives
`define Define a text replacement macro
`ifdef Conditionally compile source code,
`else dependent upon which text macros
`endif are defined
`include Include a file of source code
`resetall Reset all compiler directives
`timescale Establish a simulation timescale
`undef Undefine a text replacement macro
timescale
`timescale 1ns/100ps //time_unit, time_precision
Place the directive outside of a module definition
seconds (s), milliseconds (ms), microseconds (us), nanoseconds (ns),
picoseconds (ps), or femtoseconds (fs)
to advance 1 ns simulation time
`timescale 1 ns / 100 ps
10 steps
`timescale 1 ns / 1 ps
1000 steps
aa
$monitor($time,"wire in A =%b",
dut.aa.a_in);
Module A
wire a_in
bb
Module B
LAB
Verilog HDL Basics
Simulator, IO
Event-Driven Simulation
The simulation scheduler
− keeps track of when events occur, communicates events to
appropriate parts of the model,
− executes the model of those parts, and
− as a result, possibly schedules more events for a future time.
− Event-based simulation algorithms process only the changes in
circuit state.
− it maintains “simulated time” (sometimes “virtual time”) and the
event list.
− The simulation propagates values forward, through the circuit, in
response to input pin events or autonomous event generators
(such as clocks).
Event-Driven Simulation
The simulator creates the initial queues upon
compiling the data structures
The simulator processes all events on the current
queue, then advances
The simulator moves forward through time, never
backward
A simulation time queue represents concurrent
hardware events
System Tasks and Functions
escape
Operator
Operator: Arithmetic
Operator: Arithmetic Example
Integer division discards any remainder
A modulus operation retains the sign of the first operand
The integer register data type is signed.
The reg and time register data types are unsigned
reg [3:0] A, B
1. if (!A) // false if all of its bits are 0
if (A) // true if any of its bits are 1
Operator: Equality
Operator: Relational
Operators: Shift
if (conditional_expression)
LHS = true_expression;
else
LHS = false_expression;"
Lowest
Verilog HDL Basics
Delay
wire #5 eq = (a == b);
Conditional
forece..release
Apply to net as well as reg
Primarily used in testbench
force sig_a = 1;
release sig_a = 0;
Procedure Assignment
Initial Bloclk
Always Block
Example#1
To reg
Example #2
When to start
this block
Cout = ( a & b) |
( a & !b &cin) |
( );
Sequential
Procedural Timing Control: #
#0 IN1=0;IN2=1; // t=0
#100 IN3=1; // t=100
#100 IN4=1 // t=200
Procedural Timing Control: @
Sensitivity List
happened when signal transition
Clock qualifier
posedge, negedge
// suspend here
// until (ack || rst) true
Missing Timing Control
Priority
Conditional Statement: case