Verilog
Verilog
Verilog
(Lect 9)
Dr. RAJIB MALL
Professor Department Of Computer Science & Engineering IIT Kharagpur.
03/08/10 1
Softwre Spec
Our focus
Structure
Time
7
Netlist:
List of gates and how theyre connected A natural representation of a digital logic circuit
11
VHDL
V is short for Very High Speed Integrated Circuits. Designed for and sponsored by US Department of Defense.
Designed by a committee (1981-1985).
Syntax based on Ada programming language. Was made an IEEE Standard in 1987.
13
Verilog HDL
Introduced in 1985 by Gateway Design System Corporation:
Now a part of Cadence Design Systems, Inc.
Verilog
Provides comprehensive support for low-level digital design. Not available in native VHDL
E.g. Range of type definitions and supporting functions (called packages).
15
VHDL is harder to simulate quickly VHDL has fewer built-in facilities for hardware modelling VHDL is a much more verbose language
Most examples dont fit on slides
16
Design Methodology
17
Structure:
Wires, interconnection, etc.
18
Behavioral verilog:
The use of regs, explicit time
delays, arithmetic expressions, procedural assignments, and other verilog control flow structures.
21
22
Four-Bit Adder
module adder_4_v(B, A, C0, S, C4); input[3:0] B, A; input C0; output[3:0] S; output C4; wire[3:1] C;
Look at connections between adders
full_adder_v Bit0(B[0], A[0], C0, S[0], C[1]), Bit1(B[1], A[1], C[1], S[1], C[2]), Bit2(B[2], A[2], C[2], S[2], C[3]), Bit3(B[3], A[3], C[3], S[3], C4); endmodule
25
Behavioral Verilog
// 4-bit Adder: Behavioral Verilog module adder_4_b_v(A, B, C0, S, C4); input[3:0] A, B; Mixing structural and input C0; behaviorial code. output[3:0] S; output C4; Addition (unsigned) assign {C4, S} = A + B + C0; endmodule Concatenation operation
Each module takes parameters and returns values Let us write a Verilog program for
(A NAND B) NAND (C NAND D)
27
p q o o o
endmodule
28
Discrete-event Simulation
Basic idea: only do work when something changes Centered around an event queue
Contains events labeled with the simulated time at which they are to be executed
Verilog keywords
xor (sum, a, b); nand (c_out_bar, a, b); not (c_out, c_out_bar); endmodule
sum
Module Structure
Verilog program built from modules with I/O interfaces
A module may contain instances of other modules A module can contain local signals, etc. Module configurations are static and all run concurrently
32
Nets
Can be thought as hardware wires driven by logic Equals z when unconnected Various types of nets wire wand (wired-AND) wor (wired-OR) tri (tri-state)
35
wor / wand
inserts an OR / AND gate at the connection.
36
Nets
A Y B
wire Y; // declaration assign Y = A & B; wand Y; // declaration assign Y = A; assign Y = B; wor Y; // declaration assign Y = A; assign Y = B;
A Y B
dr A Y
The reg declaration requires explicit specification of the size. reg x, y; // single-bit register variables reg [15:0] bus; // 16-bit bus, bus[15] MSB For integer, it takes the default size, usually 32-bits.
Synthesizer tries to determine the size.
38
Registers
Variables that store values Do not represent real hardware but ..
.. real hardware can be implemented with registers
integer imem[0:1023]; // Array of 1024 integers reg [31:0] dcache[0:63];// A 32-bit memory
40
Vectors
Represent buses
wire [3:0] busA; reg [1:4] busB; reg [1:0] busC;
busB = busA;
41
Arrays (i)
Syntax
integer count[1:5]; // 5 integers reg var[-15:16]; // 32 1-bit regs reg [7:0] mem[0:1023]; // 1024 8-bit regs
44
Arrays (ii)
Limitation: Cannot access array subfield or entire array at once
var[2:9] = ???; // WRONG!! var = ???; // WRONG!!
No multi-dimentional arrays
reg var[1:10] [1:100]; // WRONG!!
Verilog Operators
Arithmetic operators
*, /, +, -, %
Logical operators
! && ||
Relational operators
&, ~&, |, ~|, ^, ~^ accepts a single word operand and produces a single bit as output
Shift operators
>>, <<
Concatenation Replication Conditional {} {{}}
Bitwise Operators
~ & | ^ ~| ~& ^~ or ~^ NOT AND OR XOR NOR NAND XNOR
48
Note
reg are used with always and initial blocks. reg variables:
Store the last value that was procedurally assigned to them
wire variables:
Represent physical connections between structural entities such as gates.
50
Continuous Assignment
A continuous assignment statement is declared with the keyword assign, followed by a net variable, an assignment operator(=), and an expression. assign corresponds to a connection. Target should not be a reg variable.
assign A = B | (C & ~D); assign B[3:0] = 4'b01XX; assign C[15:0] = 16'h00ff; //(MSB:LSB) assign {Cout, S[3:0]} = A[3:0] + B[3:0] + Cin;
51
Other differences:
In arithmetic expressions,
An integer is treated as a 2s complement signed integer. A reg is treated as an unsigned quantity.
Instantiating a Module
module mymod(y, a, b);
mymod mm1(y1, a1, b1); // Connect-by-position mymod (y2, a1, b1), (y3, a2, b2); // Instance names omitted mymod mm2(.a(a2), .b(b2), .y(c2)); // Connect-by-name
54
Gate-level Primitives
Verilog provides the following: and or xor buf bufif0 bifif1 nand nor xnor not notif0 notif1 logical AND/NAND logical OR/NOR logical XOR/XNOR buffer/inverter Tristate with low enable Tristate with high enable
55
User-Defined Primitives
Way to define gates and sequential elements using a truth table
Often simulate faster than using expressions, collections of primitive gates, etc. Gives more control over behavior with X inputs Most often used for specifying custom gate libraries
57
Example: wire [1:10] A, B; integer C; C = A + B; The size of C can be determined to be equal to 11 (10 bits plus a carry).
58
A Carry Primitive
primitive carry(out, a, b, c); output out; Always have exactly input a, b, c; one output table Truth table may 00? : 0; include dont-care (?) 0?0 : 0; entries ?00 : 0; 11? : 1; 1?1 : 1; ?11 : 1; endtable endprimitive
59
A Sequential Circuit
Primitive dff( q, clk, data); output q; reg q; input clk, data; table // clk data q new-q // Latch a 0 (01) 0 : ? : 0; (01) 1 : ? : 1; // Latch a 1 (0x) 1 : 1 : 1; // Hold when d and q both 1 (0x) 0 : 0 : 0; // Hold when d and q both 0 (?0) ? : ? : -; // Hold when clk falls ? (??) : ? : -; // Hold when clk stable endtable endprimitive
60
Continuous Assignment
Another way to describe combinational function Convenient for logical or datapath specifications
Define bus widths
Continuous assignment: permanently sets the value of sum to be a+b+carryin Recomputed when a, b, or carryin changes
61
Identifiers
An identifier is composed of a space-free sequence of uppercase and lowercase letters: alphabet, digits (0,1,.9), underscore (_), and the $ symbol. Verilog is a case sensitive language.
c_out_bar and C_OUT_BAR are two different identifiers
The name of a variable may not begin with a digit or $, and may be up to 1,024 characters long.
e.g. clock_, state_3
62
Comments
There are two kinds of comments: single line and multiline. A single-line comment begins // A multiline comment: /* comment */ Example:
// This is a single-line comments /* This is a multiline comments more comments here . */
63
Numbers in Verilog
<size><radix> <value>
No of bits Binary Octal Decimal Hex b or B o or O d or D h or H Consecutive chars 0-f, x, z
Numbers
Numbers are specified using the following form
<size><base format><number>
Size: a decimal number specifies the size of the number in bits. Base format: is the character followed by one of the following characters
b for binary,d for decimal,o(octal),h(hex).
65
Numbers
x = 4b101 // 4- bit binary number 0101 x = 6o12 // 6-bit octal number x = 16h87f7 // 16-bit hex number h87f7 x = 6b101010
Numbers in Verilog
You can insert _ for readability
12b 000_111_010_100 12b 000111010100 Represent the same number 12o 07_24
Bit extension
MS bit = 0, x or z extend this
4b x1 = 4b xx_x1
Parameters
A parameter is a constant with a name.
No size is allowed to be specified for a parameter.
The size gets decided from the constant itself (32-bits if nothing is specified).
Examples:
Four-valued Data
Verilogs nets and registers hold four-valued data 0, 1
Obvious
Z
Output of an undriven tri-state driver Models case where nothing is setting a wires value
X
Models when the simulator cant decide the value Initial state of registers When a wire is being driven to 0 and 1 simultaneously Output of a gate with Z inputs
70
Logic Values
The common values used in modeling hardware are:
0 1 x z :: :: :: :: Logic-0 or FALSE Logic-1 or TRUE Unknown (or dont care) High impedance
Initialization:
All unconnected nets set to z All register variables set to x
71
Four-valued Logic
Logical operators work on threevalued logic
0
0 1 X Z 0 0 0 0
1
0 1 X X
X
0 X X X
Z
0 X X X Output X if both inputs are gibberish Output 0 if one input is 0
72
0 x
Primitive Gates
Primitive logic gates (instantiations):
and nand or nor xor xnor not buf G (out, in1, in2); G (out, in1, in2); G (out, in1, in2); G (out, in1, in2); G (out, in1, in2); G (out, in1, in2); G (out1, in); G (out1, in);
74
`timescale 1 ns / 1ns module exclusive_or (f, a, b); input a, b; output f; wire t1, t2, t3; nand #5 m1 (t1, a, b); and #5 m2 (t2, a, t1); and #5 m3 (t3, t1, b); or #5 m4 (f, t2, t3); endmodule
76
module reg_maps_to_wire (A, B, C, f1, f2); input A, B, C; output f1, f2; wire A, B, C; reg f1, f2; always @(A or B or C) begin The synthesis system f1 = ~(A & B); will generate a wire for f1 f2 = f1 ^ C; end endmodule
78
module operator_example (x, y, f1, f2); input x, y; output f1, f2; wire [9:0] x, y; wire [4:0] f1; wire f2; assign f1 assign f2 assign f2 assign f1 endmodule = = = = x[4:0] & y[4:0]; x[2] | ~f1[3]; ~& x; f2 ? x[9:5] : x[4:0];
79
// An 8-bit adder description module parallel_adder (sum,cout, in1,in2,cin); input [7:0] in1, in2; input cin; output [7:0] sum; output cout; assign #20 {cout, sum} = in1 + in2 + cin; endmodule
80
Some Points
The logical operators (!, &&, | |) all evaluate to a 1-bit result (0, 1 or x). The relational operators (>, <, <=, >=, ~=, ==) also evaluate to a 1-bit result (0 or 1). Boolean false is equivalent to 1b0 Boolean true is equivalent to 1b1.
81
2. Behavioral
Procedural assignment Blocking Nonblocking
83
The net being assigned on the LHS, The expression on the RHS.
The assignment is continuously active. Almost exclusively used to model combinational logic.
84
A Verilog module can contain any number of continuous assignment statements. For an assign statement,
The expression on RHS may contain both register or net type variables. The LHS must be of net type, typically a wire.
85
module generate_mux (data, select, out); input [0:7] data; input [0:2] select; output out; assign out = data [ select]; endmodule Non-constant index in
expression on RHS generates a MUX
86
module generate_decoder (out, in, select); input in; input [0:1] select; output [0:3] out; assign out [ select] = in; endmodule Non-constant index in
expression on LHS generates a decoder
87
module generate_set_of_MUX (a, b, f, sel); input [0:3] a, b, sel; output [0:3] f; assign f = sel ? a : b; endmodule Conditional operator
generates a MUX
88
Procedures run:
until they delay for a period of time or wait for a triggering event
91
Procedural Blocks
There are two types of procedural blocks in Verilog.
initial for single-pass behavior : initial blocks execute only once at time zero (start execution at time zero). always for cyclic behavior: always blocks loop to execute over and over again, in other words as name means, it executes always.
93
Procedural Blocks
Procedural assignment may only appear in initial and always constructs. The initial and always constructs are used to model sequential logic. Continuous statement is used to model combinational logic.
94
Behavioral Model - Procedures (i) Procedures = sections of code that we know execute sequentially e.g. 2-to-1 mux implem:
begin if (sel == 0) Y = B; else Y = A; end
Execution Flow
95
Events (i)
always @(signal1 or signal2 or ..) begin .. execution triggers every end time any signal changes always @(posedge clk) begin .. end
97
Procedural Assignment
Inside an initial or always block: sum = a + b + cin; Just like in C:
RHS evaluated and assigned to LHS before next statement executes
Imperative Statements
if (select == 1) y = a; else y = b; case (op) 2b00: y = a + b; 2b01: y = a b; 2b10: y = a ^ b; default: y = hxxxx; endcase
99
For Loops
A increasing sequence of values on an output reg [3:0] i, output; for ( i = 0 ; i <= 15 ; i = i + 1 ) begin output = i; #10; end
100
While Loops
A increasing sequence of values on an output
reg [3:0] i, output; i = 0; while (I <= 15) begin output = i; #10 i = i + 1; end
101
103
simulation finishes)
Initial Blocks
Start execution at sim time zero and finish when their last statement executes
module nothing; initial $display(Im first); initial begin #50; $display(Really?); end endmodule
Will be displayed at sim time 0
106
Always Blocks
Start execution at sim time zero and continue until sim finishes
107
A module can contain any number of always blocks, all of which execute concurrently. Basic syntax of always block: always @ (event_expression) begin statement; statement; end
Sequential statements
The @(event_expression) is required for both combinational and sequential logic descriptions.
108
Only reg type variables can be assigned within an always block. Why??
The sequential always block executes only when the event expression triggers. At other times the block is doing nothing. An object being assigned to must therefore remember the last value assigned (not continuously driven). So, only reg type variables can be assigned within the always block. Of course, any kind of variable may appear in the event expression (reg, wire, etc.). 109
4. forever sequential_statement 5. repeat (expression) sequential_statement 6. while (expression) sequential_statement 7. for (expr1; expr2; expr3) sequential_statement
111
8. # (time_value) Makes a block suspend for time_value time units. 9. @ (event_expression) Makes a block suspend until event_expression triggers.
112
Selection
if statement:
if (A == 4) begin B = 2; end else begin B = 4; end
case statements: case (<expression>) <value1>: <statement> <value2>: <statement> default: <statement> endcase
113
// A combinational logic example module mux21 (in1, in0, s, f); input in1, in0, s; output f; reg f; always @ (in1 or in0 or s) if (s) f = in1; else f = in0; endmodule
114
// A sequential logic example module dff_negedge (D, clock, Q, Qbar); input D, clock; output Q, Qbar; reg Q, Qbar; always @ (negedge clock) begin Q = D; Qbar = ~D; end endmodule
115
module ALU_4bit (f, a, b, op); input [1:0] op; output [3:0] f; input [3:0] a, b; reg [3:0] f;
parameter ADD=2b00, SUB=2b01, MUL=2b10, DIV=2b11; always @ (a or b or op) case (op) ADD : f = a + b; SUB : f = a b; MUL : f = a * b; DIV : f = a / b; endcase endmodule
116
module priority_encoder (in, code); input [0:3] in; output [0:1] code; reg [0:1] code; always @ (in) case (1b1) input[0] : code = 2b00; input[1] : code = 2b01; input[2] : code = 2d10; input[3] : code = 2b11; endcase endmodule
117
module simple_counter (clk, rst, count); input clk, rst; output count; reg [31:0] count; always @(posedge clk) begin if (rst) count = 32b0; else count = count + 1; end endmodule
118
119
A B C D Y
Sel[1:0]
122
123
Examples
module half_adder(S, Behavioral edgeC, A, B); triggered DFF output S, C; module dff(Q, D, Clk);
input A, B; reg S,C; wire A, B; always @(A or B) begin S = A ^ B; C = A & B; end endmodule
output Q; input D, Clk; reg Q; wire D, Clk; always @(posedge Clk) Q = D; endmodule
124
Timing (i)
d initial begin #5 c = 1; #5 b = 0; #5 d = c; end c b 0 5 10 15
125
Timing (ii)
d initial begin fork #5 c = 1; #5 b = 0; #5 d = c; join end c b 0 5 10 15
Time
126
Non-blocking assignment
Uses the => operator
127
130
Non-blocking Assignments
This version does work: reg d1, d2, d3, d4; always @(posedge clk) d2 <= d1; always @(posedge clk) d3 <= d2; always @(posedge clk) d4 <= d3;
LHS updated only after all events for the current 131 instant have run Nonblocking rule: RHS evaluated when assignment runs
RHS of nonblocking taken from latches RHS of blocking taken from wires
a = 1; b = a; c = b; 1 a <= 1; b <= a; c <= b; c
133
a b
134
The assignment to the target gets scheduled for the end of the simulation cycle.
Statements subsequent to the instruction under consideration are not blocked by the assignment.
A variable cannot appear as the target of both a blocking and a non-blocking assignment.
// Up-down counter (synchronous clear) module counter (mode, clr, ld, d_in, clk, count); input mode, clr, ld, clk; input [0:7] d_in; output [0:7] count; reg [0:7] count; always @ (posedge clk) if (ld) count <= d_in; else if (clr) count <= 0; else if (mode) count <= count + 1; else count <= count + 1; endmodule
137
// Parameterized design:: an N-bit counter module counter (clear, clock, count); parameter N = 7; input clear, clock; output [0:N] count; reg [0:N] count; always @ (negedge clock) if (clear) count <= 0; else count <= count + 1; endmodule
138
// Using more than one clocks in a module module multiple_clk (clk1, clk2, a, b, c, f1, f2); input clk1, clk2, a, b, c; output f1, f2; reg f1, f2; always @ (posedge clk1) f1 <= a & b; always @ (negedge clk2) f2 <= b ^ c; endmodule
139
// Using multiple edges of the same clock module multi_phase_clk (a, b, f, clk); input a, b, clk; output f; reg f, t; always @ (posedge clk) f <= t & b; always @ (negedge clk) t <= a | b; endmodule
140
141
module ring_counter_modi1 (clk, init, count); input clk, init; output [7:0] count; reg [7:0] count; always @ (posedge clk) begin if (init) count = 8b10000000; else begin count <= count << 1; count[0] <= count[7]; end end endmodule
142
Modeling Memory
Synthesis tools are usually not very efficient in synthesizing memory.
module memory_example (en, clk, adbus, dbus, rw); parameter N = 16; input en, rw, clk; input [N-1:0] adbus; output [N-1:0] dbus; ROM Mem1 (clk, en, rw, adbus, dbus); endmodule
145
146
Mealy Machine
NS NS F/F Logic PS O/p Logic
147
module traffic_light (clk, light); input clk; output [0:2] light; reg [0:2] light; parameter S0=0, S1=1, S2=2; parameter RED=3b100, GREEN=3b010, YELLOW=3b001; reg [0:1] state; always @ (posedge clk) case (state) S0: begin // S0 means RED light <= YELLOW; state <= S1; 149 end
S1: begin // S1 means YELLOW light <= GREEN; state <= S2; end S2: begin // S2 means GREEN light <= RED; state <= S0; end default: begin light <= RED; state <= S0; end endcase 150 endmodule
module traffic_light_nonlatched_op (clk, light); input clk; output [0:2] light; reg [0:2] light; parameter S0=0, S1=1, S2=2; parameter RED=3b100, GREEN=3b010, YELLOW=3b001; reg [0:1] state; always @ (posedge clk) case (state) S0: state <= S1; S1: state <= S2; S2: state <= S0; default: state <= S0; 152 endcase
always @ (state) case (state) S0: light S1: light S2: light default: light endcase endmodule
= = = =
153
x=0
x=1
154
module parity_gen (x, clk, z); input x, clk; output z; reg z; reg even_odd; // The machine state parameter EVEN=0, ODD=1; always @ (posedge clk) case (even_odd) EVEN: begin z <= x ? 1 : 0; even_odd <= x ? ODD : EVEN; 155 end
ODD: begin z <= x ? 0 : 1; even_odd <= x ? EVEN : ODD; end endcase endmodule If no output latches need to be synthesized, we
can follow the principle shown in the last example.
156
S3
157
// Sequence detector for the pattern 0110 module seq_detector (x, clk, z) input x, clk; output z; reg z; parameter S0=0, S1=1, S2=2, S3=3; reg [0:1] PS, NS; always @ (posedge clk) PS <= NS;
158
always @ (PS or x) case (PS) S0: begin z = x ? 0 : 0; NS = x ? S0 : S1; end; S1: begin z = x ? 0 : 0; NS = x ? S2 : S1; end; S2: begin z = x ? 0 : 0; NS = x ? S3 : S1; end;
159
160
Bout
c_in Adder
add_sub P
161
carry
sum
Parity Checker
module complementor (Y, X, comp); input [7:0] X; input comp; output [7:0] Y; reg [7:0] Y; always @ (X or comp) if (comp) Y = ~X; else Y = X; endmodule
162
module adder (sum, cy_out, in1, in2, cy_in); input [7:0] in1, in2; input cy_in; output [7:0] sum; reg [7:0] sum; output cy_out; reg cy_out; always @ (in1 or in2 or cy_in) {cy_out, sum} = in1 + in2 + cy_in; endmodule
163
module parity_checker (out_par, in_word); input [8:0] in_word; output out_par; always @ (in_word) out_par = ^ (in_word); endmodule
164
// Top level module module add_sub_parity (p, a, b, add_sub); input [7:0] a, b; input add_sub; // 0 for add, 1 for subtract output p; // parity of the result wire [7:0] Bout, sum; wire carry; complementor M1 (Bout, B, add_sub); adder M2 (sum, carry, A, Bout, add_sub); parity_checker M3 (p, {carry, sum}); endmodule
165
Mostly used for simulation purposes. For small memories, even for synthesis.
166
Typical Example
module memory_model ( .. )
endmodule
167
An Example
module memory_model ( .. ) reg [7:0] mem [0:1023]; initial begin $readmemh (mem.dat, mem); end endmodule
169
A Specific Example ::
module ram_1 (addr, data, clk, rd, wr, cs) input [9:0] addr; input clk, rd, wr, cs; inout [7:0] data; reg [7:0] mem [1023:0]; reg [7:0] d_out;
assign data = (cs && rd) ? d_out ; 8bz; always @ (posedge clk) if (cs && wr && !rd) mem [addr] = data; always @ (posedge clk) if (cs && rd && !wr) d_out = mem [addr]; endmodule
170
A Specific Example ::
module ram_2 (addr, data, rd, wr, cs) input [9:0] addr; input rd, wr, cs; inout [7:0] data; reg [7:0] mem [1023..0]; reg [7:0] d_out;
assign data = (cs && rd) ? d_out ; 8bz; always @ (addr or data or rd or wr or cs) if (cs && wr && !rd) mem [addr] = data; always @ (addr or rd or wr or cs) if (cs && rd && !wr) d_out = mem [addr]; endmodule
171
A Specific Example ::
module rom (addr, data, rd_en, cs) input [2:0] addr; input rd_en, cs; output [7:0] data; reg [7:0] data; always @ (addr or rd_en or cs) case (addr) 0: 22; 1: 45; 7: 12; endcase endmodule
ROM/EPROM
172
Stimulus
Compare logic
Test Bench
174
Initialization
Assign some known values to the MUT inputs.
$display
Prints text or variables to stdout. Syntax same as printf.
$monitor
Similar to $display, but prints the value whenever the value of some variable in the given list changes.
$finish
Terminates the simulation process.
$dumpfile
Specify the file that will be used for storing the waveform.
$dumpvars
Starts dumping all the signals to the specified file.
176
Repetition
//while loop i = 0; while(i < 10) begin $display(i = %d", i); i = i + 1; end // repeat loop repeat (5) //repeats the block 5 times, begin $display(i = %d", i); i = i + 1; end
177
Example Testbench
module shifter_toplevel; reg clk, clear, shift; wire [7:0] data; shift_register S1 (clk, clear, shift, data); initial begin clk = 0; clear = 0; shift = 0; end always #10 clk = !clk; endmodule
178
Testbench:
module shifter_toplevel; reg clk, clear, shift; wire [7:0] data;
shift_register S1 (clk, clear, shift, data); initial begin clk = 0; clear = 0; shift = 0; end always #10 clk = !clk; contd..
179
initial begin $dumpfile (shifter.vcd); $dumpvars; end initial begin $display (\ttime, \tclk, \tclr, \tsft, \tdata); $monitor (%d, %d, %d, %d, %d, $time, clk, reset, clear, shift, data); end initial #400 $finish; ***** REMAINING CODE HERE ****** endmodule
180
A Complete Example
module testbench; wire w1, w2, w3; xyz m1 (w1, w2, w3); test_xyz m2 (w1, w2, w3); endmodule module xyz (f, A, B); input A, B; output f; nor #1 (f, A, B); ndmodule contd..
181
module test_xyz (f, A, B); input f; output A, B; reg A, B; initial begin $monitor ($time, A=%b, B=%b, f=%b, A, B, f); #10 A = 0; B = 0; #10 A = 1; B = 0; #10 A = 1; B = 1; #10 $finish; end endmodule
182
Test Bench
module <test module name> ; // Data type declaration // Instantiate module ( call the module that is going to be tested) // Apply the stimulus // Display results endmodule
183
module test_my_nand; // Test bench to test half adder reg A, B; wire s, cOut; Add_half initial begin
end initial #500 $finish; initial begin // setup monitoring //$monitor("Time=%0d a=%b b=%b out1=%b", $time, A, B, F); end endmodule
184
Logic Synthesis
Verilog is used in two ways
Model for discrete-event simulation Specification for a logic synthesis system
Logic synthesis :
Converts a subset of the Verilog language into an efficient netlist One of the major breakthroughs in designing logic chips in the last 20 years
185
Logic Synthesis
Takes place in two stages:
Translation of Verilog source to a netlist Optimization of the resulting netlist to improve speed and area
Most critical part of the process Algorithms very complicated
186
Simulation-synthesis Mismatches
Many possible sources of conflict Synthesis ignores delays (e.g., #10), but simulation behavior can be affected by them Simulator models X explicitly, synthesis doesnt Behaviors resulting from shared-variable-like behavior of regs is not synthesized
always @(posedge clk) a = 1; New value of a may be seen by other @(posedge clk) statements in simulation, never in synthesis
187
Practice Problem 1
Design an encoder that encodes 16 input lines into a 4bit binary output.
Encoder in 0000 0000 0000 0010 1 0000 0000 0000 0100 2 0000 0000 0000 1000 3 0000 0000 0001 0000 4 0000 0000 0010 0000 5 0000 0000 0100 0000 6 0000 0000 1000 0000 7 0000 0001 0000 0000 8 Binary 0000 0010 0000 0000 9 Out 0000 0100 0000 0000 10 0000 1000 0000 0000 11 0001 0000 0000 0000 12 0010 0000 0000 0000 13 0100 0000 0000 0000 14 1000 0000 0000 0000 15
Encoder
Enable
Solution
module encoder( binary_out , encoder_in , enable) //-----------Output Ports------------ output [3:0] binary_out ; //-----------Input Ports-------------- input enable ; input [15:0] encoder_in ; //------------Internal Variables------- reg [3:0] binary_out ;
end end
endmodule
Practice Problem 2
Design an decoder that encodes 16 input lines into a 4-bit binary output using case stmt.
Binary in 1 0000 0000 0000 0010 2 0000 0000 0000 0100 3 0000 0000 0000 1000 4 0000 0000 0001 0000 5 0000 0000 0010 0000 6 0000 0000 0100 0000 7 0000 0000 1000 0000 8 0000 0001 0000 0000 Encoder 9 0000 0010 0000 0000 Out 10 0000 0100 0000 0000 11 0000 1000 0000 0000 12 0001 0000 0000 0000 13 0010 0000 0000 0000 14 0100 0000 0000 0000 15 1000 0000 0000 0000
Encoder
Enable
Solution
module encoder(encoder_out , binary_in , enable) //-----------Output Ports------------ output [15:0] encoder_out; //-----------Input Ports-------------- input enable ; input [3:0] binary_in; //------------Internal Variables------- reg [3:0] binary_in ;
end end
endmodule
References
Cadence Design Systems, Inc., Verilog-XL Reference Manual. Ciletti, Michael D., Starting Guides to Verilog 2001, Prentice Hall 2004 http://www.eg.bucknell.edu/~cs320/Fall2003/ verilog.html http://www.verilog.net/index.html http://www.eecs.berkeley.edu/~culler http://www-inst.eecs.berkeley.edu/~cs150
194