Verilog Basic
Verilog Basic
See timescale.pdf
Verilog – Module (4)
//Description of circuit with delay
module circuit_with_delay (A,B,C,x,y);
input A,B,C;
output x,y;
wire e;
and #(30) g1(e,A,B);
or #(20) g3(x,e,y);
not #(10) g2(y,C);
endmodule
Operators
• There are three types of operators: unary,
binary, and ternary or conditional.
– Unary operators shall appear to the left of their
operand
– Binary operators shall appear between their
operands
– Conditional operators have two separate
operators that separate three operands
Arithmetic Operators
• These perform arithmetic operations. The + and -
can be used as either unary (-z) or binary (x-y)
operators.
• Operators Example
parameter n = 4;
+ (addition) reg[3:0] a, c, f, g, count;
f = a + c;
- (subtraction) g = c - n;
count = (count +1)%16; //Can count 0 thru 15.
* (multiplication)
/ (division)
% (modulus)
Relational Operators
• Relational operators compare two operands and return a
single bit 1or 0. These operators synthesize into comparators.
• Wire and reg variables are positive Thus (-3’b001) = = 3’b111
and (-3d001)>3d110. However for integers -1< 6.
Operators
Example
< (lessthan)
if (x = = y) e = 1;
<= (less than or equal to)
else e = 0;
> (greaterthan)
// Compare in 2’s compliment; a>b
>= (greater than or equal to)
reg [3:0] a,b;
== (equal to)
if (a[3]= = b[3]) a[2:0] > b[2:0];
!= (not equal to)
else b[3];
Bit-wise Operators
• Bit-wise operators do a bit-by-bit comparison
between two operands
Operators Example
~ (bitwiseNOT) module and2 (a, b, c);
& (bitwiseAND) input [1:0] a, b;
| (bitwiseOR) output [1:0] c;
^ (bitwiseXOR) assign c = a & b;
~^ or ^~(bitwise XNOR) endmodule
Logical Operators
• Logical operators return a single bit 1 or 0. They are the same
as bit-wise operators only for single bit operands.
• They can work on expressions, integers or groups of bits, and
treat all values that are nonzero as “1”.
• Logical operators are typically used in conditional (if ... else)
statements since they work with expressions.
Example
Operators
wire[7:0] x, y, z; // x, y and z are multibit variables.
! (logicalNOT)
reg a;
&& (logical AND)
...
|| (logical OR)
if ((x == y) && (z)) a = 1; // a = 1 if x equals y, and z is nonzero.
else a = !x; // a =0 if x is anything but zero.
Reduction Operators
• Reduction operators operate on all the bits of an operand
vector and return a single-bit value. These are the unary (one
argument) form of the bit-wise operators above.
Operators Example
& (reductionAND) module chk_zero (a, z);
| (reductionOR) input [2:0] a;
~& (reduction NAND) output z;
~| (reduction NOR) assign z = ~| a; // Reduction NOR
^ (reductionXOR) endmodule
~^ or ^~(reduction XNOR)
Number Format
• We are most familiar with numbers being
represented as decimals.
• However, numbers can also be represented in binary,
octal and hexadecimal.
• By default, Verilog simulators treat numbers as
decimals. In order to represent them in a different
radix, certain rules have to be followed.
16 // Number 16 in decimal
0x10 // Number 16 in hexadecimal
10000 // Number 16 in binary
20 // Number 16 in octal
Sized
• Sized numbers are represented as shown below, where size is
written only in decimal to specify the number of bits in the
number.
[size]'[base_format][number]
-6'd3; // legal
8'd-4; // Illegal
Identifiers
• Identifiers are names of variables so that they can be
referenced later on.
• They are made up of alphanumeric characters [a-z][A-Z][0-9] ,
underscores _ or dollar sign $ and are case sensitive.
• They cannot start with a digit or a dollar sign.
Keywords
• Keywords are special identifiers reserved to
define the language constructs and are in
lower case. A list of important keywords is
given below.
Data Types
• Value Set: Verilog consists of only four basic
values. Almost all Verilog data types store all
these values:
– 0 (logic zero, or false condition)
– 1 (logic one, or true condition)
– x (unknown logic value) x and z have limited use
for synthesis.
– z (high impedance state)
integer
• Integers are general-purpose variables.
• they are used mainly loops-indicies, parameters, and
constants.
• They are of implicitly of type reg. However they store data as
signed numbers
• whereas explicitly declared reg types store them as unsigned.
Example
integer a; // single 32-bit integer
assign b=63; // 63 defaults to a 7-bit variable.
Reg
• Declare type reg for all data objects on the left hand side of
expressions in inital and always procedures, or functions.
• A reg is the data type that must be used for latches, flip-flops
and memory.
• However it often synthesizes into leads rather than storage.
• In multi-bit registers, data is stored as unsigned numbers and
no sign extension is done for what the user might have
thought were two’s complement numbers.
Example
reg a; // single 1-bit register variable
reg [7:0] tom; // an 8-bit vector; a bank of 8 registers.
reg [5:0] b, c; // two 6-bit variables
Wire
• A wire represents a physical wire in a circuit and
is used to connect gates or modules.
• The value of a wire can be read, but not assigned
to, in a function or block.
• A wire does not store its value but must be
driven by a continuous assignment statement or
by connecting it to the output of a gate or
module
• Ex wire out;
Time
• Time is a 64-bit quantity that can be used in
conjunction with the $time system task to
hold simulation time.
• Time is not supported for synthesis and hence
is used only for simulation purposes.
Example
time c;
c = $time; // c = current simulation time
Scalar and Vector
• A reg declaration without a range specification
is considered 1-bit wide and is a scalar.
• If a range is specified, then the reg becomes a
multibit entity known as a vector.
• The range gives the ability to address
individual bits in a vector.
• The most significant bit of the vector should
be specified as the left hand value in the range
while the least significant bit of the vector
should be specified on the right.
wire o_nor; // single bit scalar net
wire [7:0] o_flop; // 8-bit vector net
reg parity; // single bit scalar variable
reg [31:0] addr; // 32 bit vector variable
Bit-selects
• Any bit in a vectored variable can be individually
selected and assigned a new value as shown below.
This is called as a bit select.
• If the bit-select is out of bounds or the bit-select is x
or z, then the value returned will be x.
Blocking
blocking
Non blocking
Non blocking
Verilog – Module (14)