Verilog HDL: ECE 248 Introduction To Digital Systems Design
Verilog HDL: ECE 248 Introduction To Digital Systems Design
Verilog HDL: ECE 248 Introduction To Digital Systems Design
Verilog HDL
Courtesy of Dr. Peng Li
ECEN 248
3.1
HDLs
Hardware Description Languages Widely used in logic design Verilog and VHDL Describe hardware using code Document logic functions Simulate logic before building Synthesize code into gates and layout
- Requires a library of standard cells
ECEN 248
2.2
Taste of Verilog
Module name
module Add_half ( sum, c_out, a, b ); input a, b; Declaration of port modes output sum, c_out; wire c_out_bar; Declaration of internal
signal
Module ports
xor (sum, a, b); nand (c_out_bar, a, b); not (c_out, c_out_bar); endmodule
Verilog keywords
ECEN 248
sum
Behavioral Description
module Add_half ( sum, c_out, a, b ); input a, b; output sum, c_out; a Add_half reg sum, c_out; b always @ ( a or b ) begin sum = a ^ b; // Exclusive or c_out = a & b; // And end endmodule
sum c_out
ECEN 248
2.4
Example of Flip-flop
module Flip_flop ( q, data_in, clk, rst ); input data_in, clk, rst; output q; reg q; always @ ( posedge clk ) begin if ( rst == 1) q = 0; else q = data_in; end endmodule
rst
data_in
clk
Procedural statement
ECEN 248
2.5
Gate Delay
and (yout, x1, x2);
// default, zero gate delay and #3 (yout, x1, x2); // 3 units delay for all transitions and #(2,3) G1(yout, x1, x2); // rising, falling delay and #(2,3) G1(yout, x1, x2), G2(yout2, x3, x4); // Multiple instances a_buffer #(3,5,2) (yout, x); // UDP, rise, fall, turnoff bufif1 #(3:4:5, 6:7:9, 5:7:8) (yout, xin, enable); // min:typ:max / rise, fall, turnoff
Simulators simulate with only one of min, typ and max delay values Selection is made through compiler directives or user interfaces Default delay is typ delay
ECEN 248 2.6
Time Scales
Time scale directive: timescale <time_unit>/<time_precision> time_unit -> physical unit of measure, time scale of delay time_precision -> time resolution/minimum step size during
simulation time_unit time_precision Unit/precision Delay specification 1ns / 100ps 100ns / ns 10ns / 100ps #4 #4 #4.629 Simulator time Delay value in step simulation 0.1ns 1ns 0.1ns 4.0ns 400ns 46.3ns
ECEN 248
2.7
Net Delay
wire #2 y_tran; and #3 (y_tran, x1, x2); buf #1 (buf_out, y_tran); and #3 (y_inertial, x1, x2);
x1 2 x2 2 y_inertial 2 4 6 8 10 4 6 8 10 4 6 8 10
x1 x2
y_tran
buf_out
y_tran 2 4 6 8 10
y_inertial buf_out 2
ECEN 248
10
2.8
ECEN 248
2.9
Behavioral Statements
initial | always single_statement; | begin block_of_statements; end
initial
Activated from tsim = 0 Executed once Initialize a simulation
always
Activated from tsim = 0 Executed cyclically Continue till simulation
terminates
ECEN 248
2.10
clk
50
tsim
ECEN 248
2.11
Assignment
Continuous assignment Values are assigned to net variables due to some input variable changes assign = Procedural assignment Values are assigned to register variables when certain statement is executed in a behavioral description Procedural assignment, = Procedural continuous assignment, assign = [deassign] Non-blocking assignment, <=
ECEN 248
2.12
net variables
dynamic binding for variables assign deassign for register variables only
ECEN 248
2.13
Can be overridden by
module flop ( q, qbar, preset, clear, clock, data ); assign qbar = ~q; initial q = 0; always @ ( negedge clk ) q = data; always @ ( clear or preset ) begin if ( !preset ) assign q = 1; else if ( !clear ) assign q = 0; else deassign q; end endmodule
ECEN 248
2.14
Example of assign
module mux4_PCA(a, b, c, d, select, y_out); input a, b, c, d; input [1:0] select; output y_out; reg y_out; always @(select) y_out changes with a; begin if (select == 0) assign y_out=a; else if (select == 1) assign y_out=b; else if (select == 2) assign y_out=c; else if (select == 3) assign y_out=d; else assign y_out=1bx; end endmodule
ECEN 248
2.15
Alternative
module mux4_PCA(a, b, c, d, select, y_out); input a, b, c, d; input [1:0] select; output y_out; reg y_out; always @(select or a or b or c or d) begin Value of a is assigned to if (select == 0) y_out=a; y_out at this time else if (select == 1) y_out=b; else if (select == 2) y_out=c; else if (select == 3) y_out=d; else y_out=1bx; end endmodule
ECEN 248
2.16
Blocking assignment =
Statement order matters A statement has to be executed
ECEN 248
2.17
ECEN 248
2.18
ECEN 248
2.19
Activity flow is
expression is true
Other processes keep
going
ECEN 248
2.20
2.21
Indeterminate Assignment
module multi_assign(); reg a, b, c, d; initial begin #5 a = 1; b = 0; end always @ ( posedge a ) begin c = a; end always @ ( posedge a ) begin c = b; end always @ ( posedge a ) begin d = b; end always @ ( posedge a ) begin d = a; end
endmodule
ECEN 248 2.22
Multiple assignments
Value depends on
Similar to race-
conditions in hardware
Syntax: if ( expression )
ECEN 248
2.23
Conditional Operator ( ? : )
always @ ( posedge clock ) yout = ( sel ) ? a + b : a b;
ECEN 248
2.24
order
Exact match between case
cares
casex treats both x and
z as dont cares
2.25
ECEN 248
2.26
ECEN 248
2.27
module sth ( externalSig ); input externalSig; always begin while ( externalSig ); end endmodule
ECEN 248
2.28
ECEN 248
2.29
ECEN 248
2.30
Task
module bit_counter (data, count); input [7:0] data; output [3:0] count; reg [3:0] count; always @(data) t(data, count); 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
ECEN 248 2.31
Function
module word_aligner (w_in, w_out); input [7:0] w_in; output [7:0] w_out; assign w_out = align (w_in); function [7:0] align; input [7:0] word; begin align = word; if (align != 0) while (align[7] == 0) align = align << 1; end endfunction endmodule
ECEN 248
2.32
ECEN 248
2.33
ECEN 248
2.34
ECEN 248
2.35
Synthesis of Loops
repeat, for, while, forever Depends on Venders Timing control Data dependencies A loop is static or data-independent if the number of
ECEN 248
2.36
ECEN 248
2.37
2.39