Outline: - Introduction To Hardware Description Language (HDL)
Outline: - Introduction To Hardware Description Language (HDL)
Outline
• Introduction to hardware description language (HDL)
– Why it is better than schematic entry
• Verilog 語法
– Lexical convention of Verilog
– Data types
– Structural Verilog
– Functional Verilog
• One language, many coding styles
• Continuous v.s. procedural assignments
• Blocking vs NonBlocking
VLSI Signal Processing Lab.
• Control statements
• For loop and parameterized design
• SystemVerilog
• Gotchas 正確的使用方法 <= ( 會踩到的陷阱 )
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
VERILOG 語法
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
和 C 一樣,大小寫有差
只能字母 _ 開頭
後接字母數字 $_ ,
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Reserved keywords
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
9
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
命名建議規則
• Lowercase letters for signal names: valid 讓三個月後的自己也可看的懂
• Uppercase letters for constants: MAX
• clk sub-string for clocks: in_clk
• rst sub-string for resets: g_rst
• Suffix
– _n for active-low, _z for tri-state, _a for async , …: rst_n, out_z
VLSI Signal Processing Lab.
copyright © 2004 10
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
$<identifier>
• The ‘$’ sign denotes Verilog system tasks and functions.
• A number of system tasks and functions are available to perform different operations, such
as:
– Finding the current simulation time ($time))
– Displaying/monitoring the values of the signal ($display, $monitor)
VLSI Signal Processing Lab.
11
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
The pound sign (#) character denotes the delay specification for
procedural statements, and for gate instances but not module
instances.
• The # delay for gate instances is referred to by many names, including gate delay, propagation delay, intrinsic
delay, and intra-object delay.
module MUX2_1 (out, a, b, sel) ;
output out ;
input a, b, sel;
VLSI Signal Processing Lab.
not #1 not1(sel_,sel);
and #2 and1(a1,a,sel_);
and #2 and2(b1,b,sel);
or #1 or1(out,a1,b1);
endmodule
12
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Compiler directives
• The directives start with a grave
accent ( ` ) followed by some `include “file1.v”
keyword // Used as `WORD_SIZE in code
`define WORD_SIZE 32
`define
Text-macro substitution module test ();
`ifdef TEST
`ifdef, `ifndef, `else, // A implementation
`else
`endif
// B implementation
Conditional compilation
VLSI Signal Processing Lab.
`endif
assign out = `WORD_SIZE{1’b1};
`include endmodule
File inclusion
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
– 變數都用小寫
– 常數都用大寫
– clk sub-string for clocks
– rst sub-string for resets
14
– _n for active-low N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Outline
• Introduction to hardware description language (HDL)
– Why it is better than schematic entry
• Quick overview of Verilog
• Verilog 語法
– Lexical convention of Verilog
– Data types
– Structural Verilog
– Functional Verilog
• One language, many coding styles
• Continuous v.s. procedural assignments
VLSI Signal Processing Lab.
• Blocking vs NonBlocking
• Control statements
• For loop and parameterized design
• SystemVerilog
• Gotchas 正確的使用方法 <= ( 會踩到的陷阱 )
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
了解 VERILOG 資料型態
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Value Meaning
0 Logic zero (false)
1 Logic one (True)
X Unknown logic value or conflict
Z High impedance, floating
(unconnected input are set to z)
VLSI Signal Processing Lab.
unconnected
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Logic Values
• Logic with multilevel (0,1,X,Z) logic values
– NAND anything with 0 is 1
– NAND with X/Z get an X
• True tables define the how outputs are compute
• Z is treated as X
& 0 1 X Z | 0 1 X Z
VLSI Signal Processing Lab.
0 0 0 0 0 0 0 1 X X
1 0 1 X X 1 1 1 1 1
X 0 X X X X X 1 X X
Z 0 X X X Z X 1 X X
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Decimal number
representing size in bits
We’ll learn how to
actually assign literals to
nets a little later
Number Representation
Size: number of bits (optional)
• Integer
– Syntax: <size>’<base format><value>
• Decimal: 8’d79
• Binary: 8’b0100_1111 Underscore _ is ignored, for easy to read
• Octal: 8’o117
• Hexadecimal: 8’h4f = 8’h4F
– Negative value stored by 2’d complement
– Bit value
• 1, 0: logic high and low
VLSI Signal Processing Lab.
• Z: high impedance
• X: unknown
• ?: don’t care
– Default size is 32-bits decimal number
• When no base is specified 20
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
More examples
• Example
4’b0 // 4-bit 0000
4’b1001 // 4-bit 1001
8’b1111_1111// 8-bit 1111 1111, ‘_’ underscore can be use for readability
16’hfe12 // 16-bit 1111 1110 0001 0010
9’o457 // 9-bit 100 101 111
8’d220 // 8-bit decimal 220
-8’d12 // 8-bit decimal -12
8’d-12 // illegal
VLSI Signal Processing Lab.
21
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
– If MSB is 1 number is extend to fill MSBs with 0/1, depending on the sign
6’ha: 6-bit, store as 6’b00_1010 (a => 1010, filled with 2-bit ‘0’ on left)
-3’b1=-3’b01=3’b111, (negative number, extended with 1)
3’b1 = 3’b001, (positive number, extended with 0)
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
More examples
• Example
//Unsized numbers (at least 32 bit)
123 // default: decimal radix, 32-bit width
‘d123 // decimal 123
‘h7B // hexdecimal 7B
`hxx // xxxx xxxx
//Sized numbers
16’d5 // 16-bit constant ‘b0000_0000_0000_0101
11’h1X? // 11-bit constant ‘b001_XXXX_ZZZZ
VLSI Signal Processing Lab.
//signed numbers
8’shFF // 8-bit signed constant,2’s complement -1
-8’d12 // -12
To be absolutely clear in your intent it’s usually best to explicitly specify the width and base
23
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Data type
• Net
– Structural connection between components
wire, tri Interconnecting wire - no special resolution function
wor, trior Wired outputs OR together (models ECL)
wand, triand Wired outputs AND together (models open-collector)
tri0, tri1 Net pulls-down or pulls-up when not driven
supply0, supply1 Net has a constant logic 0 or logic 1 (supply strength)
trireg Retains last value, when driven by z (tristate).
• Register
– Variable to store data
VLSI Signal Processing Lab.
24
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
signed
– Used for RHS variables in always/initial procedural statements
reg c, d; // 1-bit c and 1-bit d
reg [7:0] a; // 8-bit a with msb=a[7] and lsb=a[0]
reg [0:7] a; // 8-bit a with msb=a[0] and lsb=a[7], avoid this
N C T U . E E , Hsinchu, Taiwan
Note. reg wire 傻傻分不清
• wire: Verilog 的預設資料型態 ( 用在 structural Verilog ouput)
– assign 的輸出,用 wire
– structure 連接,也用 wire
• Reg ( 用在 behavioral Verilog output)
– 和 register (DFF) 沒半點關係,純粹是歷史原因
– Procedural block 的 輸出,都要用 reg
• always, initial, fork join
• 好麻煩,有簡單一點的嗎 ?
• logic
VLSI Signal Processing Lab.
Verilog vectors
Known as BUS in hardware
• Declare by a range following the type
<data type> [left range : right range] <Variable name>
• Single element that is n-bits wide
reg [0:7] a, b; //Two 8-bit reg with MSB as the 0th bit
wire [3:0] data; //4-bit wide wire MSB as the 4th bit
• Vector part select (access)
a[5] // bit # 5 of vector a
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
d = { a[7:2], b[1:0] };
7 6 5 4 1 0 0 1 e
e = { a[7:4], 4’b1001 };
f = { 2{a[7:4] }}; 7 6 5 4 7 6 5 4 f
29
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
?
memory_req
instruction
instruction
small_net
VLSI Signal Processing Lab.
Verilog arrays
• Array: range follows the name
<datatype> <array name> [<array indices>]
reg B [15:0]; // array of 16 reg elements
• Array of vectors
<data type> [<vector indices>]<array name>[<array indices>]
reg [15:0] C [1023:0]; // vector with 1024 elements, each 16b
• Memory access
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
assign Obus =
ObusReg;
VLSI Signal Processing Lab.
always @(posedge
Clk)
if (Read==1’b0)
mem[Adr]
= Ibus;
else
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
...
Vector
VLSI Signal Processing Lab.
Memory
(Array)
Memory bank
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Exercise
https://hdlbits.01xz.net/wiki/Vector2 https://hdlbits.01xz.net/wiki/Vector3
• reverse the byte ordering of the 4-byte
word.
• AaaaaaaaBbbbbbbbCcccccccDdddddd
d =>
DdddddddCcccccccBbbbbbbbAaaaaaa
a
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Boolean operators
• Bitwise operators perform bit-oriented operations on vectors
~(4’b0101) = {~0,~1,~0,~1} = 4’b1010
4’b0101 & 4’b0011 = {0&0, 1&0, 0&1, 1&1} = 4’b0001
• Reduction operators act on each bit of a single input vector
&(4’b0101) = 0 & 1 & 0 & 1 = 1’b0
• Logical operators return one-bit (true/false) results
!(4’b0101) = 1’b0
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Operators Precedence
Operator Property
Arithmetic +, -, *
/, % Be careful to use these
Relational <, >, <=, >=
Equality ===, !== Including x, z comparison, for test
bench only
==, !=
Logic !, &&, ||
Bitwise ~, &, |, ^, ~^, ^~
Reduction &, ~&, |, ~|, ^, ~^, ^~ Multiple bit to 1 bit
Shift <<, >> Can be replaced by connection
<<<, >>> With signed extension, not used
Replication {n{m}} Duplicate m by n times
VLSI Signal Processing Lab.
Conditional A ? B : C;
41
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Operator Examples
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Exercise
• https://hdlbits.01xz.net/wiki/Vectorgates
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Outline
• Introduction to hardware description language (HDL)
– Why it is better than schematic entry
• Lexical convention of Verilog
• Data types
• Structural Verilog
• Functional Verilog
– One language, many coding styles
– Continuous v.s. procedural assignments
– Blocking vs NonBlocking
VLSI Signal Processing Lab.
– Control statements
– For loop and parameterized design
• SystemVerilog
• Gotchas
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
STRUCTURAL VERILOG
VLSI Signal Processing Lab.
VERILOG MODULES
STRUCTURES AND HIERARCHY
了解 VERILOG 模組與階層的寫法
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Parameters
• Parameters are means of giving names to constant values
• The values can be overridden when the design is compiled
• Parameters cannot be used as variables
• Syntax:
parameter <name> = <constant expression>;
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Parameter declaration
• Default value need to be set at declaration time
• 32 bit wide by default, but may be declared of any width
• 2 declaration flavors:
Inside a module In module header
module test (... I/O’s ...) module test
VLSI Signal Processing Lab.
parameter ASIZE = 32, BSIZE =16; #(parameter ASIZE = 32, BSIZE =16)
//… (... I/O’s ...);
reg [ASIZE -1:0] Abus, //…
Zbus; wire [BSIZE-1:0] reg [ASIZE -1:0] Abus,
Bwire; Zbus; wire [BSIZE-1:0]
//… Bwire;
endmodule //…
endmodule
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Example
module Adder (A, B, Cin, S,
A[N-1:0]
Cout, Clk); Adder S[N-1:0]
parameter N=8; B[N-1:0]
N-
input [N-1:0]A, B;
input Cin; Cin bits Cout
input Clk; (8 by default)
output [N-1:0] S; reg-ouputs
output Cout;
reg [N-1:0] S; Clk
reg Cout;
//module module Adder #(parameter N=8)
internals (input [N-1:0]A, B,
endmodule input Cin,
VLSI Signal Processing Lab.
input
Clk,
output reg
ANSI C style [N-1:0] S,
output reg
Cout Combine port and variable
); declaration
//module internals N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Outline
• Introduction to hardware description language (HDL)
– Why it is better than schematic entry
• Quick overview of Verilog
• Verilog 語法
– Lexical convention of Verilog
– Data types
– Structural Verilog
– Functional Verilog
• One language, many coding styles
• Continuous v.s. procedural assignments
VLSI Signal Processing Lab.
• Blocking vs NonBlocking
• Control statements
• For loop and parameterized design
• SystemVerilog
• Gotchas 正確的使用方法 <= ( 會踩到的陷阱 )
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
FUNCTIONAL VERILOG
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Levels of Abstraction
• One design, many coding styles to describe hardware functions
Slower simulation
(More detailed)
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
NOT
C Y
2 g2
– Procedural assignment
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
• Executed in parallel
• Order does not matter
• Different procedural executed in
parallel
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Concurrent blocks
• Blocks of code with no well-defined order relative to one
another
– Module instance is the most important concurrent block
– Continuous assignments, and procedural blocks are concurrent
within a module
– Note. Hardware is concurrently executed
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
wire [15:0] netA = 16’h3333; assignment, a implicit net declaration will be inferred
wire [15:0] netB = netA;
endmodule 先後順序不重要
Same result if you write like this
VLSI Signal Processing Lab.
The order of these continuous assign out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
assign t1 = ~( (sel[1] & d) | (~sel[1] & b) );
assignment statements does not matter.
assign t0 = ~( (sel[1] & c) | (~sel[1] & a) );
They essentially happen in parallel!
But better to follow the circuit topological order
Exercise
• https://hdlbits.01xz.net/wiki/Conditional
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
PROCEDURAL BLOCKS
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Procedural blocks
• Each procedural block represent a separate activity flow in
Verilog
• Procedural blocks
– always blocks
• To model a block of activity that is repeated continuously
– initial blocks simulation only
• To model a block of activity that is executed at the beginning
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Procedural assignments
• Procedural assignment changes the state of a reg
• Used for both combinational and sequential logic inference
• All procedural statements must be within always (or initial) block
Feel confusing?
reg A; Use logic
always @ (B or C)
Sensitivity list
begin Signal changes triggers
logic A;
A = ~(B & C); actions in the body always @ (B or C)
VLSI Signal Processing Lab.
end begin
A = ~(B & C);
end
Output should use reg declaration for variable
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) ); ORDER MATTERS in procedural blocks
t1 = ~( (sel[1] & d) | (~sel[1] & b) ); Executed sequentially from top to bottom
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) ); 對 blocking assignment 順序很重要
VLSI Signal Processing Lab.
end
The always block run once
endmodule
whenever a signal in its
sensitivity list changes
always @( a or b or c or d or sel )
begin
t0 = ~( (sel[1] & c) | (~sel[1] & a) );
t1 = ~( (sel[1] & d) | (~sel[1] & b) );
out = ~( (t0 | sel[0]) & (t1 | ~sel[0]) );
VLSI Signal Processing Lab.
end
What happens if we accidentally
endmodule
forget a signal on the sensitivity list?
Simulation will miss, but synthesis will ignore the sensitivity list
=> Simulation synthesis mismatch
6.375 Complex Digital Systems N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
end
Verilog-2001 provides special syntax to
endmodule
automatically create a sensitivity list for
all signals read in the always block
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
// Controlled by
negative edge of clk or
rst NonBlocking assignment
always @(negedge clk,
negedge rst)
if (!rst) Z <= B & C;
else Z <= B & N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
CONTROL STATEMENTS
VLSI Signal Processing Lab.
Why bother?
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
out = d end
else endmodule sel[0] sel[1]
out = 1’bx;
end
endmodule
STATUS)
else if (<expression>)
begin
// statement2
q = newstatus;
else
// statement3 end Something wrong here
end
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
case (<expression>)
<alternative 1> : <statement 1>;
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Incomplete Specification:
Added unwanted latch
Unintentional latch
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
TIPS: If you don’t care about the assignment in a case (for instance you know that it will never come up) then assign
the value “x” to the variable; E.g.: default: out = 1‘bx; The x is treated as a “don’t care” for synthesis and will simplify
the logic
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
out = d; end
//else endmodule sel[0] sel[1]
// out = 1’bx;
end
endmodule
A COMPLETED EXAMPLE
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
module design(
// input
input [7:0] data_in, // combinational circuit
// system always_comb
input clk_p, beign
input rst_n, value_w = data_in + 7’d1;
// output end
output logic [7:0] data_out // sequential circuit
); always_ff@(posedge clk_p, negedge rst_n)
logic [7:0] value_w; begin
if(~rst_n)begin
data_out <= 7’d0;
VLSI Signal Processing Lab.
Module
Combinational Sequential end else begin
IN logic logic data_out <= value_w;
Reg.
Reg. end
OUT
Reg.
end
endmodule
86
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Simple testbench
`timescale 1ns/1ps //time resolution
// dumpping waveform
`include “design.v” initial begin
`include “pattern.v” $fsdbDumpfile(“waveform.fsdb”);
$fsdbDumpvars; Save waveforms of all variables
module testbench; end
// inter connection wire endmodule
wire clk_p;
wire rst_n;
wire [7:0] data_in;
wire [7:0] data_out;
// connection, test pattern
testbench.v
pattern U_pattern(
.clk_p(clk_p),
.rst_n(rst_n), Test pattern generation
.data_in(data_in),
and response check pattern.v design.v
VLSI Signal Processing Lab.
.data_out(data_out));
//my design
design U_design(
.clk_p(clk_p),
.rst_n(rst_n),
.data_in(data_in), DUT
.data_out(data_out));
87
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
.*));
// dumpping waveform
`ifdef GATE
initial begin `timescale 1ns/10ps
$fsdbDumpfile(“waveform.fsdb”); `endif
$fsdbDumpvars;
end
endmodule //global parameters for all modules
`define CLK_PERIOD 30.0
88
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
89
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
loop
• The for loops are supported, with two constraints:
– The loop index range must be globally static, and the loop body must not contain
a wait statement.
– Latches are also synthesized whenever a for loop statement does not assign a
variable for all possible executions of the for loop and when a variable assigned
inside the for loop is not assigned a value before entering the enclosing for loop.
• The while loops are supported, but the loop body must contain at least
one wait statement.
VLSI Signal Processing Lab.
• Combinational while loops are supported if the iterative bound is statically determinable. The
loop statements with no iteration scheme (infinite loops) are supported, but the loop body
must contain at least one wait statement.
• Repeat is not supported in Synopsys Design Compiler
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always@( a or b ) begin
c[0] = a[0] & b[0];
c[1] = a[1] & b[1];
c[2] = a[2] & b[2];
c[3] = a[3] & b[3];
end
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always@(posedge clk)begin
reg [7:0]temp;
temp <= temp+0;
always@(posedge clk)begin
temp <= temp+1;
for(idx=0;idx<10;idx=idx+1)begin
temp <= temp+2;
temp <= temp+idx;
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always@(posedge clk)begin
if(reset)
temp <= 0;
要用 for loop 請先展開看看合不合 else if(counter<10)
temp <= temp_nxt;
end
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
begin
data[index] <= data[index] + 1;
index <= index + 1;
end
end
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
module for_loop_synthesis_tb (); //
module for_loop_synthesis (i_Clock); Testbench
input i_Clock; reg r_Clock = 1'b0;
integer ii=0; // Instantiate the Unit Under Test (UUT)
reg [3:0] r_Shift_With_For = 4'h1; for_loop_synthesis UUT
reg [3:0] r_Shift_Regular = 4'h1; (.i_Clock(r_Clock));
always
// Performs a shift left using a for loop #10 r_Clock = !r_Clock;
always @(posedge i_Clock) endmodule
begin
for(ii=0; ii<3; ii=ii+1)
r_Shift_With_For[ii+1] <= r_Shift_With_For[ii];
end
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Bit Reversal
• Given a 100-bit input vector [99:0], reverse its bit ordering
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
population count
• A "population count" circuit counts the number of '1's in an input vector.
Build a population count circuit for a 255-bit input vector
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
BAD Good
bit visit bus visit
loop overhead parallel evaluation
BAD Good
loop overhead concatenation
For Loop
• It simulates slow
– from 10X to > 1000X slower than non-for loop versions
• It synthesizes slow
• Memory clear
– legitimate for loop use in chip design
PARAMETERIZED DESIGN
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
for(i = 0; i <= 7; i = i + 1)
begin: u
adder8 add(sum[(i*8)+:8], co[i+1],
a[(i*8)+:8], b[(i*8)+:8], ci[i]);
end
endgenerate
Verilog-2001
generate
if ((a_width < 8) || (b_width < 8))
CLA_multiplier #(a_width, b_width) u1 (a, b, product);
else
WALLACE_multiplier #(a_width, b_width) u1 (a, b, product);
endgenerate
VLSI Signal Processing Lab.
endmodule
Verilog-2001
copyright © 2004 106
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Generate Loop
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Generate
• Use for loops to generate any number of instances of:
– modules, primitives, procedures, continuous assignments, tasks, functions,
variables, nets
• Use if–else and case decisions to control what instances are
generated
– provides greater control than the VHDL generate
• New reserved words added:
– generate, endgenerate, genvar
VLSI Signal Processing Lab.
Exercise
• https://hdlbits.01xz.net/wiki/Vector100r
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan