Kcs Verilog 22-23 P21ec404 Unit-1
Kcs Verilog 22-23 P21ec404 Unit-1
(P18EC43)
Lexical
convention
Whitespace Comments
Basic concepts
`define `include
Value set strength level
System tasks and
Complier Data Types
directives
Nets Registers Integer Real
$display $monitor $stop $finish
Lexical Conventions
Whitespace
Blank spaces (\b)
Tabs (\t)
Newlines (\n) comprise the whitespace.
Comments
a = b && c; // This is a one-line comment
/ * This is a multiple line
comment * /
/ * This is / * an illegal * / comment * /
Operators
Operators are of three types
Unary
a = ~ b; // ~ is a unary operator. b is the operand
Binary
a = b && c; // && is a binary operator. b and c are operands
Ternary
a = b ? c : d; // ?: is a ternary operator. b, c and d are
operands
Number Specification
Sized numbers
Sized numbers are represented as
<size> ‘<base format> <number>.
4'b1111 // This is a 4-bit binary number
12'habc // This is a 12-bit hexadecimal number
16'd255 // This is a 16-bit decimal number.
Unsized numbers
Numbers that are specified without a <base format>
specification are decimal numbers by default.
Numbers that are written without a <size> specification
have a default number of bits that is simulator- and
machine-specific (must be at least 32).
Negative numbers
-8'd3 // 8-bit negative number stored as 2's
complement of 3
4'd-2 // Illegal specification
Underscore characters and question
marks
An underscore character “_" is allowed anywhere in a
number except the first character.
Underscore characters are allowed only to improve
readability of numbers and are ignored by Verilog.
A question mark "?" is the Verilog HDL alternative for z in
the context of numbers.
\wire*
\busa+index
\-clock
\***error-condition***
\net1/\net2
Data Types
Value Set
Value Level Condition in Hardware Circuits
0 Logic zero, false condition
1 Logic one, true condition
X Unknown value
Z High impedance, floating state
Strength Levels
Strength Level TYPE Degree
matrix[1] [0] = 33559; //Set value of element indexed by [1] [0] to 33559
array_4d [0] [0] [0] [0] [15:0] =0; //Clear bits 15:0 of the
register
//accessed by indices [0] [0] [0] [0]
port_id =0; //Illegal syntax - Attempt to write the entire array
matrix [1] = 0; //Illegal syntax - Attempt to write [1] [0] .. [1] [255]
Memories
In digital simulation modeling of register files, RAMs, and
ROMs is done.
Memories are modeled in Verilog simply as a one-
dimensional array of registers.
Each element of the array is known as an element or word
and is addressed by a single array index.
It is important to differentiate between n 1-bit registers
and one n-bit register.
A particular word in memory is obtained by using the
address as a memory array subscript.
reg mem1bit[0:1023]; //memory mem1bit with 1K 1-bit words
reg [7 : 0] membyte [0 : 1023]; // memory membyte with 1K
// 8-bit words (bytes)
membyte[511] // fetches 1 byte word whose address is 511.
Parameters
Verilog allows constants to be defined in a module by
the keyword parameter.
Parameters cannot be used as variables.
Parameter values for each module instance can be
overridden individually at compile time.
This allows the module instances to be customized.
Parameter types and sizes can also be defined.
parameter port_id = 5; //Defines a constant port_id
parameter cache_line_width=256;//constant defines
//width of cache line
parameter signed [15:0] WIDTH; // Fixed sign and range for
// parameter WIDTH
Strings
Strings can be stored in reg.
The width of the register variables must be large enough to
hold the string.
Each character in the string takes up 8 bits (1 byte).
If the width of the register is greater than the size of the
string, Verilog fills bits to the left of the string with
zeros.
If the register width is smaller than the string width, Verilog
truncates the leftmost bits of the string.
It is always safe to declare a string that is slightly wider than
necessary.
reg [8*19:1] string_value; //Declare a variable that is 19 bytes wide
initial
String_value = "Hello Verilog World"; // String can be stored in variable
Special characters serve a special purpose in displaying
strings such as newline, tabs and
displaying argument values.
Special characters can be displayed in strings only
when they are preceded by escape characters
System Tasks
Verilog provides standard system tasks to do certain
routine operations.
All system tasks appear in the form $<keyword>.
Displaying information
$display is the main system task(most useful) for
displaying values of variables or strings or expressions.
$display(pl, p2, p3 ,....., pn);
pl, p2, p3, ..., pn can be quoted strings or variables or
expressions.
A $display inserts a newline at the end of the string by
default.
Format Display
%d or %D Display variable in decimal
%b or %B Display variable in binary
%S or %S Display string
%h or %H Display variable in hex
%c or %C Display ASCII character
%m or %M Display hierarchical name (no argument required)
%v or %V Display strength
%o or %O Display variable in octal
%t or %T Display in current time format
%e or %E Display real number in scientific format (e.g., 3e10)
%f or %F Display real number in decimal format (e.g., 2.13)
%g or %G Display real number in scientific or decimaI,
//Display the string in quotes
$display("Hello Verilog World");
-- Hello Verilog World
//Display value of current simulation time 230
$display ($time) ;
-- 230
//Display value of 41-bit virtual address 1fe0000001c and
time 200
reg [0:40] virtual_addr;
$display("At time %d virtual address is %h”, $time,
virtual_addr);
-- At time 200 virtual address is 1fe0000001c
//Display value of port-id 5 in binary
reg [4:0] port-id;
$display("ID of the port is %b", port-id);
-- ID of the port is 00101
//Display X characters
//Display value of 4-bit bus 10xx (signal contention) in binary
reg [3:0] bus;
$display("Bus value is %b”, bus);
-- Bus value is 10xx
//Display the hierarchical name of instance p1 instantiated under
//the highest-level module called top. No argument is required.
// This is a useful feature)
$display(“This string is displayed from %m level of hierarchy");
-- This string is displayed from top.pl level of hierarchy
Monitoring information
Verilog provides a mechanism to monitor a signal when its
value changes. This facility is provided by the $monitor task.
$monitor(p1 ,p2,p3 ,...., pn);
The parameters pl, p2, ... , pn can be variables, signal
names, or quoted strings.
Unlike $display, $monitor needs to be invoked only once.
Only one monitoring list can be active at a time. If there is
more than one $monitor statement in your simulation,
the last $monitor statement will be the active statement.
The earlier $monitor statements will be overridden.
Two tasks are used to switch monitoring on and off.
//Monitor time and value of the signals clock and reset
//Clock toggles every 5 time units and reset goes down at 10 time
unit:
initial
begin
$monitor($time, “Value of signals clock = %b reset = %b”, clock,
reset);
end
Partial output of the monitor statement:
-- 0 Value of signals clock = 0 reset = 1
-- 5 Value of signals clock = 1 reset = 1
-- 10 Value of signals clock = 0 reset = 0
Stopping and finishing in a
simulation
The task $stop is provided to stop during a simulation.
Usage: $stop;
The $stop task puts the simulation in an interactive
mode. The designer can then debug the design from
the interactive mode. The $stop task is used whenever
the designer wants to suspend the simulation and
examine the values of signals in the design.
The $finish task terminates the simulation.
Usage: $finish;
// Stop at time 100 in the simulation and examine the results
// Finish the simulation at time.
initial // to be explained later. time = 0
begin
clock = 0;
reset = 1;
#100 $stop; // This will suspend the simulation at time =100
#900 $finish; // This will terminate the simulation at time =900
end
Compiler Directives
'define
The 'define directive is used to define text macros in Verilog
//define a text macro that defines default word size
//Used as 'WORD_SIZE in the code
'define WORD_SIZE 32
//define an alias. A $stop will be substituted wherever 'S appears
'define S $stop.
//define a frequently used text string
'define WORD_REG reg [31:0]
// you can then define a 32_bit register as 'WORD-REG reg32;
'include
The ' include directive allows you to include entire
contents of a Verilog source file in another Verilog file
during compilation.
This directive is typically used to include header files,
which typically contain global or commonly used
definitions
// Include the file header.v, which contains declarations in //the
main verilog file design.v
'include header.v
...
...
<Verilog code in file design.vz>
Modules and Ports
Modules
• Width matching
• Unconnected ports
fulladd4 faO(SUM, , A, B, C_IN); // Output port c-out is
unconnected
Illegal Port Connection
module Top;
//Declare connection variables
reg [3:0]A,B;
reg C_IN;
reg [3:0] SUM;
wire C_OUT;
//Instantiate fulladd4, call it fa0
fulladd4 fa0(SUM, C_OUT, A, B, C_IN);
//Illegal connection because output port sum in module fulladd4
//is connected to a register variable SUM in module Top.
………
………
<Stimulus>
..
endmodule
Connecting Ports to External Signals
Connecting by ordered list
module Top;
//Declare connection variables
reg [3:O]A,B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
//Instantiate fulladd4, call it fa-ordered. Signals are connected to ports
in //order (by position)
fulladd4 fa_ordered (SUM, C_OUT, A, B, C_IN);
...
<stimulus>
...
endmodule
Connecting ports by name
// Instantiate module fa-byname and connect
signals // to ports by name
fulladd4 fa_byname (.c_out
(C_OUT), .sum(SUM), .b(B), .c_in
(C_IN), .a(A));
OUT+ + s1s03
module mux4_to_1 (out, i0, il, i2, i3, sl, S0);
output out;
input i0, i1, i2, i3 ,s1, s0;
wire s1n, s0n, y0, yl, y2, y3;
not (s1n, s1) ;
not (s0n, s0);
// 3-input and gates instantiated
and (y0, i0, s1n, s0n);
and (y1, i1, s1n, s0);
and (y2, i2, s1, s0n);
and (y3, i3, s1, s0);
endmodule
module fulladd4 (sum, c_out, a, b, c_in);
output [3:0] sum;
output c_out;
input [3 : 0] a, b;
input c_in;
wire c1, c2, c3;
endmodule
module stimulus;
reg [3:0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
fulladd4 FA1_4(SUM, C_OUT, A, B, C_IN) ;
initial
begin
$monitor($time," A= %b, B=%b, C_IN= %b,--- C_OUT= %b, SUM= %b\n”,
A, B, C_IN, C_OUT, SUM);
end
initial
begin
A = 4'd0; B = 4'd0; C_IN = 1'b0;
#5 A = 4'd10; B = 4'd5; C_IN = 1'b1;
#5 A = 4'd12; B = 4'd8; C_IN = 1'b1;
end
endmodule
Gate Delays
• Rise delay
• Fall delay
Turn-off delay
The turn-off delay is associated with a gate output
transition to the high impedance value (Z) from
another value.
If the value changes to X, the minimum of the
three delays is considered.
// Delay of delay-time for all transitions
and #(delay_2time) a1(out, i1, i2);
• Operator Types
arithmetic, logical, relational, equality, bitwise,
reduction, shift, concatenation, or conditional
Arithmetic Operators
Binary Operators:
A = 4'b0011; B = 4'b0100; // A and B are register
vectors
D = 6; E = 4; // D and E are integers
A * B // Multiply A and B. Evaluates to 4'b1100
D / E // Divide D by E. Evaluates to 1. Truncates any
//fractional part
A + B // Add A and B. Evaluates to 4'b0111
B - A // Subtract A from B. Evaluates to 4'b0001
13 % 3 // evaluate to 1
in1 = 4'b101x;
in2 = 4'b1010;
sum = in1 + in2; // sum will be evaluated to the value 4'bx
Unary operators
The operators + and - can also work as unary operators.
-4 // Negative 4
+5 // Positive 5
-10 / 5 // Evaluates to -2
-’d10 / 5 // is equivalent (2's complement of 10) /5 = (2^32 -10) /5
Logical Operators
// Logical operations
A = 3; B = 0;
A && B // Evaluates to 0. Equivalent to (logical-1 && logical-0)
A II B // Evaluates to 1. Equivalent to (logical-1 || logical-0)
! A / / Evaluates to 0. Equivalent to not(logical-1)
!B // Evaluates to 1. Equivalent to not(logica1-0)
// Unknowns
A = 2'b0x; B = 2'b10;
A && B // Evaluates to X. Equivalent to (X && logical 1)
// Expressions
(a == 2) && (b == 3) // Evaluates to 1 if both a == 2 and
// b== 3 are true. Evaluates to 0 if either is false.
Logical operators
follow these conditions:
Logical operators always evaluate to a l-bit value, o
(false), 1 (true), or X (ambiguous).
If an operand is not equal to zero, it is equivalent to a
logical 1 (true condition). If it is equal to zero, it is
equivalent to a logical o (false condition). If any
operand bit is X or z, it is equivalent to X (ambiguous
condition) and is normally treated by simulators as a
false condition.
Logical operators take variables or expressions as
operands.
Relational Operators
//A=4,B=3
// X = 4'b1010, Y = 4'b1101, Z = 4'b1xxx
A <= B // Evaluates to a logical 0
A > B // Evaluates to a logical 1
Y >= X // Evaluates to a logical 1
Y < Z // Evaluates to an X
Equality Operators
Expression Description Value
a == b a equal to b, result unknown if X or 0, 1, X
Z in a or b
A == B // Results in logical 0
X != Y // Results in logical 1
X == Z // Results in X
Z === M //results in logical 1 (all bits match, including
X and Z)
Z === N //results in logical 0 (least significant bit does
not match)
M !== N // Results in logical 1
Bitwise Operators
and 0 1 X Or 0 1 X xor 0 1 X xnor 0 1 X
0 0 0 0 0 0 1 X 0 0 1 X 0 1 0 X
1 0 1 X 1 1 1 1 1 1 0 X 1 0 1 X
X 0 X X X X 1 X X X X X X X X X
//x=4’b1010,y=4’b1101
//z=4’b10x1 Not Result
0 1
~X // Negation. Result is 4'b0101 1 0
X & Y // Bitwise and. Result is 4'b1000 X X
X | Y // Bitwise or. Result is 4'b1111
X ^ Y // Bitwise xor. Result is 4'b0111
X ^~ Y // Bitwise xnor. Result is 4’b1000
X&Z // Result is 4’b10x0
Y = {B , C} // Result Y is 4'b0010
Y = {A , B , C , D , 3'b001) // Result Y is 11'b10010110001
Y = {A , B[0], C[1] } // Result Y is 3'b101
Replication Operator
reg A;
reg [1:0] B, C;
reg [2:0] D;
A = 1'b1; B = 2'b00; C = 2'b10; D = 3'b110;
Y = { 4{A} } // Result Y is 4'b1111
Y = { 4{A} , 2{B} } // Result Y is 8'b11110000
Y = { 4{A} , 2{B} , C } // Result Y is 10'b1111000010
Conditional Operator
Usage: condition_expr ? True_expr : false_expr ;