Verilog Notes
Verilog Notes
Verilog
•Verilog was developed by Gateway Design
Automation as a proprietary language for logic
simulation in 1984.
•Gateway was acquired by Cadence in 1989
•Verilog was made an open standard in 1990 under
the control of Open Verilog International.
•The language became an IEEE standard in 1995
(IEEE STD 1364) and was updated in 2001 and
2005.
Verilog Key Featurers:
Case sensitive - keywords are lower case
Semicolons(;) are line terminators
Concurrent execution
Comments:
–One line comments start with // ...
–Multi-line comments start with /*and end
with*/
System tasks and functions start with a dollar
sign, ex $display, $signed
Verilog Syntax:
module and2_1( X, A, B ) ;
output X ;
input A, B ;
assign X= A & B ;
endmodule
Logical values
•Logic with multilevel (0,1,X,Z) logic values
–Nand anything with 0 is 1
–Nand two get an X
•True tables define the how outputs are
compute
& 0 1 X Z
0 0 0 0 0
1 0 1 X X
X 0 X X X
Z 0 X X X
Data types
1) Register :
Example: reg y;
2) Nets:
Example : wire X;
3) Integer: (integer)
–Convenient to counting purposes
–At least 32-bit wide
–Useful for loop
Example : integer i;
4) Real :(real) simulation only
–Can be specified in decimal and scientific
notation
5) vectors :
Know as BUS in hardware
•Declare by a range following the type
<data type> [left range : right range] <Variable name>
7) Parameters:
- Subtract
/ Divide
* Multiply
% Modulus
2) Bitwise Operators
Characte Operation
r performed
~ Invert each bit
& And each bit
| Or each bit
^ Xor each bit
~^ Xnor each bit
3) Reduction Operator
Characte Operation
r performed
& And all bits
~& Nand all bits
| Or all bits
~| Nor all bits
^ Xor all bits
~^ Xnor all bits
X=1100
Y= &X= 1&(1&(0&0))= 0
Z=^X=1^(1^(0^0))= 0
M=|X= 1|(1|(0|0))= 1
4) Boolean Operator
Characte
Operation performed
r
! Not true
&& Both expressions true
One ore both expressions
||
true
let x = 4'b1010, y =
4'b0000
1) x | y //bitwise OR, result is 4'b1010
x || y //logical OR, result is 1
2) x&y=0000
x&&y // result 0
3) !x // result 1
4) !y // result 0
5) Relational Operator
Characte Operation
r performed
> Greater than
< Smaller than
Greater than or
>=
equal
Smaller than or
<=
equal
== Equality
!= Inequality
let a = 4, b = 3, and...
x = 4'b1010, y = 4'b1101, z = 4'b1xxx
a <= b //evaluates to logical zero
a > b //evaluates to logical one
y >= x //evaluates to logical 1
y < z //evaluates to x
6) Shift Operator
Characte Operation
r performed
>> Shift right
<< Shift left
let x = 4'b1100
y = x >> 1; // y is 4'b0110
y = x << 1; // y is 4'b1000
y = x << 2; // y is 4'b0000
7) Other Operators
Characte Operation
r performed
?: Conditions testing
{} Concatenate
x=2’b11 z=2’b01
p=2’b11 q=2’b00
1) assign Y = (x==z) p:q; // returned value is 0
2) assign big= (x>z) x:z; // result is x ie 11
3) Y={x,z} // result 1101
Assignment:
a = 4'b0011; b = 4'b0100; d = 6; e = 4; f = 2;
then,
a + b //add a and b;
b - a //subtract a from b;
a * b //multiply a and b;
d / e //divide d by e,
e ** f //raises e to the power f.
a = 4'b0011;
// b = 4'b0100;
// d = 6; e = 4; f = 2;
//then,
a + b //evaluates to 4'b0111
b - a // evaluates to 4'b0001
a * b // evaluates to 4'b1100
d / e // evaluates to 4'b0001. Truncates fractional part
e ** f // evaluates to 4'b1111
Replication Operator
Repetitive concatenation of the same number
Operands are number of repetitions, and the
bus or wire
let a = 1'b1, b = 2'b00, c = 2'b10, d = 3'b110
y = { 4{a} } // y = 4'b1111
y = { 4{a}, 2{b} } // y = 8'b11110000
y = { 4{a}, 2{b}, c } // y = 10'b1111000010
Description styles
module HA ( S, Co, A, B ) ;
output S, Co ;
input A, B ;
assign Co = A & B ;
assign S = A ^ B ;
endmodule
Behavioral Description:
TT or Functional relationship between input
and output
Output signal must be declared as reg
reg S, Co ;
always block
conditional statements can be included
sequential execution inside always block
syntax of an always@
module mux2_1 ( Y , S , I) ;
output Y ;
input S ;
input [1:0] I;
reg Y ;
always@( S , I )
begin
if (s==0)
begin
Y=I[0];
end
else
begin
Y= I[1];
end
end
endmodule
Mux4:1 Verilog program
module mux4_1( Y, S, I ) ;
output Y ;
input [1:0] S ;
input [3:0] I ;
reg Y ;
always@( S, I )
begin
case( S )
2'b00 : begin Y = I[0]; end
2'b01 : begin Y = I[1]; end
2'b10 : begin Y = I[2]; end
2'b11: begin Y = I[3]; end
endcase
end
endmodule
module HA ( S, Co, A, B ) ;
output S, Co ;
input A, B ;
reg S, Co;
always@ ( A, B )
begin
case ( { B, A } )
2'b00: begin {Co,S}=2'b00; end
2'b01: begin {Co,S}=2'b01; end
2'b10: begin {Co,S}=2'b01; end
2'b11: begin {Co,S}=2'b10; end
endcase
end
endmodule
module AOI(Y,A,B,C,D);
output y;
input A,B,C,D;
wire w1,w2,w3;
and x1(w1,A,B);
and x2(w2,C,D);
or x3(w3,w1,w2);
not x4(Y,w3);
endmodule
Switch level description
module inv ( in, out ) ;
output out ;
input in ;
supply0 a ;
supply1 b ;
nmos ( out, a, in );
pmos ( out, b, in );
endmodule
1) Data flow method
module mux4_1(Y,S1,S0,I0,I1,I2,I3);
output Y;
input S1,S0,I0,I1,I2,I3;
wire w1,w2,w3,w4;
assign w1=(~S1)&(~S0)&I0;
assign w2=(~S1)&S0&I1;
assign w3=S1&(~S0)&I2;
assign w4=S1&S0&I3;
assign Y=w1 | w2| w3|w4;
endmodule
2) BEHAVIOURAL DESCRIPTION
module mux4_1(Y,S1,S0,I0,I1,I2,I3);
output Y;
input S1,S0,I0,I1,I2,I3;
reg Y;
always@(S0,S1,I0,I1,I2,I3)
begin
case({S1,S0})
2’B00: begin Y=I0; end
2’B01: begin Y=I1; end
2’B10: begin Y=I2; end
2’B11: begin Y=I3; end
endcase
end
endmodule
Gate level description
module mux4_1(Y,S1,S0,I0,I1,I2,I3);
output Y;
input S1,S0,I0,I1,I2,I3;
wire w1,w2,w3,w4,w5,w6;
or x7(Y, w3,w4,w5,w6);
and x6 (w6,S0,S1,I3);
and x5 (w5,w1,S1,I2);
and x4 (w4,S0,w2,I1);
and x3 (w3,w1,w2,I0);
not x1 (w1,S0);
not x2 (w2,S1);
endmodule
module mux2_1(out,i0,i1,s0);
output out;
input i0,i1,s0;
not x4(w1,s0);
and x1(w2,i0,w1);
and x2(w3,i1,s0);
or x3(out,w2,w3);
endmodule
Logic synthesis
Verilog Signal-Assignment Statement Y = 2 * X + 3 input is of 2 bit
vector