3ST Verilog STWT Slides
3ST Verilog STWT Slides
Verilog
HDL
~~~~~~~ ~~~~~~~
~~~~~~~ ~~~~~~~
~~~~~~~ ~~~~~~~
~~~~~~~ ~~~~~~~
~~~~~~~
~~~~~~~
~~~~~~~
~~~~~~~
Programming HDL
Language C
parser
parser
model generator
Code generator
elaborator
linker
Application Program
Executable Operating System Simulator
Hardware
Start Simulation
End Simulation
• First, write HDL code in any HDL tool and save it
• Compile the saved code and rectify the reported error if any
• Run the compiled code and check if the compiled code results are
meeting your design requirement, if not, try to find what could be the
possible error and rectify it ,save it, compile it and simulate it
• Repeat the above steps until you get the simulation result which exactly
matches your requirement
3ST Technologies Verilog HDL STWT 16
Introduction
• Software tools available in the market
• Logic Simulation
• Mentor Graphics – ModelSim (free) or QuestaSim
• Cadence – ncvlog, ncelab, ncsim or nclaunch
• Synopsys – vcs
• Logic Synthesis
• Mentor Graphics – leonardo or precision
• Cadence – rc-complier
• Synopsys – design compiler (dc)
• References
• Verilog HDL : A guide to Digital Design & Synthesis by Samir
Palnitkar
• Verilog Primer by J. Bhasker
• Web-Site : www.asic-world.com
3ST Technologies Verilog HDL STWT 17
Session II
Modeling Styles
Gate Level Modeling
Conventions
3ST Technologies Verilog HDL STWT 18
Modeling Styles
• Understanding level of abstraction
• Switch Level
• Gate Level
• Data flow
• Behavioral
module gatelevel ( o , a , b );
input a , b ;
output o ;
and a1( o , a , b );
endmodule
assign o = a & b ;
endmodule
3ST Technologies Verilog HDL STWT 21
Modeling Styles
• Behavioral Model Writing – curtain raiser
• Hardware design can be implemented in terms of the desired design without
concern for the hardware implementation details
• Designing at this level is very similar to C programming but with hardware in mind
• Verilog procedural statements are used to model design at behavioural level, it is a
higher Level of Abstraction than the other levels, also a Black box modelling style
• always keyword is used for behavioural modelling, initial for initializations
• Each always block represent a separate activity flow
• Different always blocks represent the concurrent activity flows which is an inherent
concurrent representation of hardware model
• The example below shows a behavioural model for which all the activity flows are
defined by the initial and always constructs which start together at simulation time
zero, the initial constructs execute once, and the always constructs execute
repetitively module behave;
reg a;
initial
a = 0;
always
#50 a = ~a;
endmodule
3ST Technologies Verilog HDL STWT 22
Modeling Styles
• Design Methodologies : Structural
• Top – Down
• Define top level block and identify sub-block necessary to build top-
level block
input a , b ;
output y ;
Declarative Part
endmodule
top(root)
or1 nt2
qbar, q, reset
(or) (not)
n1 n2 x1
(and) (nand) (xor)
3ST Technologies Verilog HDL STWT 30
Gate Level Modeling
• Module Instantiation
• Hierarchical Naming
• Hierarchical names referencing allows us to denote every identifier in the
design hierarchy
• A hierarchical name is a list of identifier separated by dots ( “ . ” ) for each
level of hierarchy
• The top module is called the root module because it is not instantiated
anywhere in the hierarchy, basically it is the starting point
• To assign a unique name to an identifier , start from top level module and
trace the path along the design hierarchy to the desired identifier
• As an illustration here are some examples
a w1 sum
h1 h2 w3
b
cin cout
w2
endmodule
endmodule statement
Amem
// A memory of 8 one-bit elements
reg Amem [7:0];
0
3ST Technologies Verilog HDL STWT 67
Data Types
• Reg Types
• Arrays 7
7
0
// A memory of four 8-bit words
reg [7:0] Cmem [0:3]; Cmem
3
3ST Technologies Verilog HDL STWT 68
Data Types
• Reg Types
• Arrays
localparam count = 0;
$display("Displaying hello vlsi id number = %d", count);
endmodule
module top;
hello_vlsi w1();
hello_vlsi w2();
defparam w1.count = 1, w2.count = 2;
endmodule
3ST Technologies Verilog HDL STWT 74
Exercises
• Data Types
1. Write a declaration for a wire and a bus of 16 bits
2. Write memory declarations for a 4K x 16-bit memory
3. Check the correctness of the following declarations
net n1, n2;
reg [63:0] r1, r2, r3;
reg [0:-5] r;
events e1, e2;
4. Write a declaration of a tri-stated wire with charge storage of medium capacitance
1 0 1 x z 1 0 1 x z 1 0 1 x z
0 0 1 x x 0 0 0 0 0 0 0 1 x x
1 1 1 1 1 1 0 1 x x 1 1 0 x x
x x 1 x x x 0 x x x x x x x x
z 0 x x x z x x x x
z x 1 x x
?: 0 1 x z
expr2
0 0 x x x
1 x 1 x x expr1
x x x x x
z x x x x
input X, Y; 0 0 1 0 0 0
+ & +
& &&
3ST Technologies Verilog HDL STWT 93
Exercises
• Data Flow Modeling
1. Given that a, b, c and d are declared as below
[7:0] a, b; wire [8:0]c1,c2;wire c ; wire [15:0] d;
Evaluate the following expressions
i) a = 255; b = 155; c1= a + b; /* Evaluate c */
ii) c2= 9'b000000001 + a + b; /* Evaluate c */
iii) d= { a, b };/* Evaluate d*/
iv) c3 = &b;
2. Design 4 bit shift (left shift , right shift, arithmetic left shift , arithmetic right shift,
circulatory left shift, circulatory right shift) registers using data flow
3. Design a 8X3 encoder using data flow
4. Design a 4 bit carry look ahead adder using data flow modelling
5. Design a 3X8 decoder using data flow
6. Design a 16x1 mux using 4x1 mux , 2x1 mux , 8x1 mux using data flow
7. Design a 4 bit full subtractor using data flow
8. Design a parity generator and checker using data flow modelling
9. Design a binary to grey and grey to binary using data flow
Procedural Behavioural
assignment
Processes Blocks Timing control
statement
Delay based
Blocking (=) Combinatorial If -else Sequential
Non-blocking
Clocked Case Concurrent Event based
(<=)
Loops
initial
#60 $finish;
@(posedge clk)
@ (clock)
rega <= regb;
rega <= regb ;
@(negedge clk)
rega<=regb;
always @ (in)
o2 <= in;
always @ (in) 0 5 10 20 30 32 35 40 45 50
#5 o3 = in; in
clk
always @ (in)
#5 o4 <= in;
always @ (in)
o5 = #5 in;
always @ (in)
o6 <= #5 in; Verilog HDL STWT
3ST Technologies 118
Behavioral Modeling
• Behavioral Modeling Examples
• Half Subtractor – combinational logic
module half_sub(diff, borrow, x, y);
output reg diff, borrow;
input x, y ; X diff
always @ (x, y) Y half subtractor borrow
begin
diff=x^y;
borrow=(~x)&y;
end
endmodule
reg clk;
initial begin
clk=0;
forever #5 clk=~clk;
end
Defining a
Function
function [7:0] getbytexor;
input [15:0] data;
begin
getbytexor = ^data;
end function [7:0] getbyteor (input [15:0] data);
endfunction begin
getbyteor = |data;
end
endfunction
3ST Technologies Verilog HDL STWT 146
Functions and Tasks
• Functions – procedural blocks - examples
module arithmetic_unit (res_1, res_2,
operand_1, operand_2);
output [4:0] res_1, [3:0] res_2;
input [3:0] operand_1, operand_2;
reg[7:0] CheckSum; assign res_1 = sum_operands (operand_1, operand_2);
function[7:0] doCheckSum; assign res_2 = largest_of_two (operand_1, operand_2);
input[63:0] DataArray; function [4:0] sum_operands ;
reg[15:0] temp1, temp2; // local declarations input [3:0] operand_1, operand_2;
begin begin
temp1 = DataArray[15:0] ^ DataArray[31:16]; sum_operands= operand_1 + operand_2;
temp2 = DataArray[63:48] ^ DataArray[47:32]; end
doCheckSum = temp1[7:0] + endfunction
temp2[7:0] ^ temp1[15:8] + function [3:0] largest_of_two;
temp2[15:8]; input operand_1, operand_2;
end begin
endfunction largest_of_two= (operand_1 > operand_2) ?
operand_1 : operand_2;
end
endfunction
endmodule
task t;
input [7:0] a; output [3:0] c; reg [3:0] c; reg [7:0] tmp;
begin c = 0; tmp = a;
while (tmp)
begin
c = c + tmp[0];
tmp = tmp >> 1;
end
end
endtask
endmodule
STATE MEMORY
SET
D Q
OUTPUTS
NEXT CLR Q
NEXT STATE STATE OUTPUT LOGIC
INPUTS LOGIC ...
SET
D Q
CLR Q
CLK
3ST Technologies Verilog HDL STWT 159
Finite State Machines
• FSM – Mealy and Moore machines
• Mealy machine : The output depends on the current state and inputs
PREVIOUS STATE
STATE MEMORY
SET
D Q
OUTPUTS
NEXT CLR Q
COMBINATIONAL STATE OUTPUT LOGIC
...
INPUTS LOGIC
SET
D Q
CLR Q
CLK
STATE MEMORY
SET
D Q
INPUTS LOGIC
D
SET
Q PREVIOUS STATE
CLR Q
STATE MEMORY
SET
D Q
OUTPUTS
NEXT CLR Q
CLK COMBINATIONAL OUTPUT LOGIC
STATE
...
INPUTS LOGIC
SET
D Q
CLR Q
CLK
3ST Technologies Verilog HDL STWT 161
Finite State Machines
• FSM – Mealy machines – State Table
A B x A+ B+ y
0 0 0 0 0 0
0 0 1 0 1 0
0 1 0 0 0 1
0 1 1 1 1 0
1 0 0 0 0 1
1 0 1 1 0 0
1 1 0 0 0 1
1 1 1 1 0 0
0/1
1/0 0/1 1/0
1/0
01 11
1 1
1 1 0 0 0 0 0 0
B+ = A’x y=(A+B)x΄
3ST Technologies Verilog HDL STWT 164
Finite State Machines
• Mealy machine – Gate Level Implementation using DFF