Documente Academic
Documente Profesional
Documente Cultură
Basic idea is a programming language to describe hardware Initial purpose was to allow abstract design and simulation
Purpose of HDL:
1.
2. 3. 4.
Describe the circuit in algorithmic level (like c) and in gate-level (e.g. And gate) Simulation Synthesis Words are better than pictures
HDL is not a programming language (HDL is a description language) HDL is not highly abstract, e.g., implement the DSP algorithm y(n) = 0.75y(n-1) + 0.3x(n) (HDL is at the RTL level (register transfer))
Aug 9, 2001
Synthesizable Subset
Verilog (and VHDL) began life as simulation and modeling tools Hardware synthesis developed during the 1990s Need to use a subset of Verilog and specific coding styles to allow synthesis tool to infer correct (and realizable) hardware
Aug 9, 2001
Synthesizable Subset
Use this to write testbenches for behavioral simulation Verilog
May try to write HDL code as if it will eventually be executed by some mysterious processor device in the FPGA Code is written sequentially (like a program), but you are simply writing descriptions of the various hardware entities in your system
Aug 9, 2001
Design Methodologies:
1. Top Down Approach 2. Bottom Up Approach
clb 1
clb 2
Verilog Basics
helloWorld.v
module helloWorld ; initial begin $display ("Hello World!!!"); $finish; end endmodule System calls.
Modules are the unit building-blocks (components) Verilog uses to describe an entire hardware system. Modules are (for us) of three types: behavioral, dataflow, gate-level. We ignore the switch-level in this course. This module is behavioral. Behavioral modules contain code in procedural blocks.
This is a procedural block. There are two types of procedural blocks: initial and always. More than one statement must be put in a begin-end group.
Y
Z
Output O
All code is contained in modules A module can invoke other modules Modules cannot be contained in another module
Module declaration
Input X Y Z
Module
Circuit Wire Output O
Module name module sample (X,Y,Z,O); input X,Y,Z; output O; // Describe the circuit using logic symbols assign O = (X^Y)&Z; endmodule
Module Ports
Similar to pins on a chip Provide a way to communicate with outside world Ports can be input, output or inout
Module AND (i0, i1, o); input i0, i1; output o; i0 o i1
endmodule
Module instances
Verilog models consist of a hierarchy of module instances In C++ speak: modules are classes and instances are objects
AND3
i0 i1 i2 o
Module AND3 (i0, i1, i2, o); input i0, i1, i2; output 0; wire temp; AND a0 (temp,i0,i1)); AND a1 (o,i2,temp); endmodule
Port Connection
System Tasks
$monitor
$monitor ($time,"%d %d %d",address,sinout,cosout); Displays the values of the argument list whenever any of the arguments change except $time. $display ("%d %d %d",address,sinout,cosout); Prints out the current values of the signals in the argument list $finish Terminate the simulation
$display
$finish
System Tasks
$display examples:
$display($time);
Output: 230
reg [0:40] virtual_addr; $display(At time %d virtual address is %h, $time, virtual_addr);
Output: At time 200 virtual address is 1fe000001c
System Tasks
$monitor Examples:
initial begin $monitor($time, Value of signals clock=%b, reset=%b, clock, reset); end
Output: 0 value of signals clock=0, reset=1 5 value of signals clock=1, reset=1 10 value of signals clock=0, reset=0
Lexicography
Comments:
// Comment
Two Types:
Character Set:
Number Representation
Number Representation
Number Representation
Examples:
gives 010111 gives 00000110 gives xx01 gives 0000001110101011 gives 00011000 gives 11110 gives xxxxxxxxxxxxxxxx gives zzzzzzzz
Data Types
Nets and Registers Vectors Integer, Real, and Time Register Data Types Arrays Memories Parameters Strings
Nets
Keyword: wire
unless declared as vectors For trireg, default is x wire a; wire b, c; wire d=1b0;
Default value: z
Examples
Registers
Retain value until next assignment NOTE: this is not a hardware register or flipflop Keyword: reg Default value: x Example:
reg reset; initial begin reset = 1b1; #100 reset=1b0; end
Vectors
Net and register data types can be declared as vectors (multiple bit widths) Syntax:
Example
wire a; wire [7:0] bus; wire [31:0] busA, busB, busC; reg clock; reg [0:40] virtual_addr;
Vectors (contd)
Consider
wire [7:0] bus; wire [31:0] busA, busB, busC; reg [0:40] virtual_addr;
Integer
integer variables are signed numbers reg vectors are unsigned numbers Designer can also specify a width:
integer [7:0] tmp;
Examples:
integer counter; initial counter = -1;
Real
Time
Used to store values of simulation time Keyword: time Bit width: implementation-dependent (at least 64) $time system function gives current simulation time Example:
time save_sim_time; initial save_sim_time = $time;
Arrays
Syntax:
<data_type> <var_name>[start_idx : end_idx];
Allowed in reg, integer, time, real and vector register data types.
Examples:
integer count[0:7]; reg bool[31:0]; time chk_point[1:100]; reg [4:0] port_id[0:7]; integer matrix[4:0][4:0]; // two dimensional array count[5] chk_point[100] port_id[3]
module sample (a,b,c,d); input a,b; output c,d; wire [7:0] b; reg c,d; integer k;
0,1,x,z
Reg
May synthesize into latches, flip-flops or wires Used in procedural code
Variable Declaration
Declaring a net
wire [<range>] <net_name> [<net_name>*]; Range is specified as [MSb:LSb]. Default is one bit wide
Declaring a register
reg [<range>] <reg_name> [<reg_name>*];
Declaring memory
reg [<range>] <memory_name> [<start_addr> : <end_addr>];
Examples
reg r; // 1-bit reg variable wire w1, w2; // 2 1-bit wire variable reg [7:0] vreg; // 8-bit register reg [7:0] memory [0:1023]; a 1 KB memory
Register/net
net input
register/net
output
net
net
inout
net
Operators
Arithmetic:
*,+,-, /,%
Relational
Bit-wise Operators
<,<=,>,>=,==, !=
Not: ~ XOR: ^ And : & 5b11001 & 5b01101 ==> 5b01001 OR: | XNOR: ~^ or ^~
Logical Operators
Operators
Reduction Operators:
Unary operations returns single-bit values & : and | :or ~& : nand ~| : nor ^ : xor ~^ :xnor
module sample (a, b, c, d); input [2:0] a, b; output [2;0] c, d; wire z,y; assign z = ~| a; c = a * b; If(a==b) d = 1; else d =0; d = a ~^ b; if ((a>=b) && (z)) y=1; else y = !x;
assign d << 2; //shift left twice assign {carry, d} = a + b; assign c = {2{carry},2{1b0}}; // c = {carry,carry,0,0}
assign c= (inc==2)? a+1:a-1;
Net Concatenation
Module B
Module A
3o7 Representations {b[3:0],c[2:0]} {a,b[3:0],w,3b101} {4{w}} {b,{3{a,b}}} Meanings
{b[3] ,b[2] ,b[1] ,b[0], c[2] ,c[1] ,c[0]} {a,b[3] ,b[2] ,b[1] ,b[0],w,1b1,1b0,1b1} {w,w,w,w} {b,a,b,a,b,a,b}
Module C
Logic circuit can be described using basic logic gates. The module is a text description of the circuit layout. Verilog has all the standard gates
Primatives
The standard logic gates are Verilog system primatives. It is possible to specify new user-defined primatives (UDPs). UDPs are specified by there truth-table. UDPs may only have one output.
Using a Primative
A Primative by itself is not a module. To use it (e.g. for testing), it needs to be instantiated in a module. Its not necessary to explicitly give a name while instantiating a primitive. e.g. and (op,in1,in2); not (op,in);
Circuit to code
module smpl_circuit(A,B,C,x,y); input A,B,C; output x,y; wire e; and g1(e,A,B); not g2(y, C); or g3(x,e,y); endmodule
Input signals
In order to simulate a circuit the input signals need to be known so as to generate an output signal. The input signals are often called the circuit stimulus. An HDL module is written to provide the circuit stimulus. This is known as a testbench.
Testbench
The testbench module includes the module to be tested. There are no input or output ports for the testbench. The inputs to the test circuit are defined with reg and the outputs with wire. The input values are specified with the keyword initial A sequence of values can be specified between begin and end.
Ci 0 0
A 0 0 1 1 0 0 1 1
B 0 1 0 1 0 1 0 1
Co 0 0 0 1 0 1 1 1
S 0 1 1 0 1 0 0 1
Co
Full Adder S
Ci
0 0 1 1 1 1
51
Co = AB + BCi + CiA
A B B Ci Ci A
Co
52
sum = a b ci
a b c sum
a b c
sum
53
a b b c c a
a b c
54
2 to 1 multiplexer
55