Logic Design Lab Verilog 101
Logic Design Lab Verilog 101
Instructor: Kuan Jen Lin () E-Mail: kjlin@mails.fju.edu.tw Web: http://vlsi.ee.fju.edu.tw/teacher/kjlin/kjlin.htm Room: SF 727B
Verify design: give test patterns, and check if outputs meet specification.
Simulation
Graphical input/ouput Embedded test_modules in HDL programs
Emulation (Prototyping):
FPGA/CPLD Discrete components
3
Schematic Capture
Simulation
What is an HDL?
A Hardware Description Language (HDL) is a high level programming language with special language constructs used to model the function of hardware logic circuits. The special language constructs provide you the ability to: Describe the connectivity (structure) of a circuit Describe the functionality (bhavior) of a circuit Describe a circuit at various levels of abstraction Describe the timing information and timing constraints of a circuit Express concurrency
6
First Example
set
g1 g2
q qBar
module nandLatch Reset (q, qBar,set, reset); ouput q, qbar; input set, reset; nand g1 (q, qBar, set); nand g2 (qBar, q, reset); endmodule
Primitives
10
endmodule
high-active or low-active
Source: Mano, Digital Design, 3th edition.
12
13
Operators (1/2)
Logic operators : return a value. &&, ||, ! assign a = b && c; Bitwise logic operators : return result in bus form. &, |, ~, assign a[2:0] = b[2:0] & c[2:0]; Equality operator: ==, != Reduction operator: a = &b; Relational operator: >=, > , <=, <
15
Operators (2/2)
Are all Verilog operators synthesizable? Conditional operators Shift operators e.g. assign a = b << 2; Arithmetic operators
e.g. assign a = b+c; assign a = b-c; assign a = b*c; assign a = b/c; assign a = b%c; e.g. assign a = c ? x : y ;
Module Hierarchy
module fulladder(S, Co, A, B, Ci);
input A, B, Ci; output S, Co; assign Co = { (A ^ B) & Ci } | (A & B); assign S = A ^ B ^ Ci;
A Co S B Ci
endmodule
A B
Co Ci
A[3] B[3] C4
A B
A[2] B[2] C3
A B
A[1] B[1] C2
A B
A[0] B[0] C1
A B
Co FA3 Ci S
Co FA2 Ci S
Co FA1 Ci S
Co FA0 Ci S
C0
S[3] input [3:0] A, B; input C0; output [3:0] S; output C4; wire C1, C2, C3;
S[2]
S[1]
S[0]
endmodule
Behavioral Description
module decoder_2x4(D, A, B, enable); output [3:0] D; input A, B, enable; Sensitive list always @(A, B, enable) begin D=4b1111; // Dont miss this line if (~A & ~B & ~enable) D[0] = 0; if (~A & B & ~enable) D[1] = 0; if ( A & ~B & ~enable) D[2] = 0; if (A & B & ~enable) D[3] = 0; C-like end Procedural endmodule statements
19
Behavioral Description-2
module decoder_2x4(D, A, B, enable); output [3:0] D; input A, B, enable; always @(A, B, enable) begin D=4b1111; // Dont miss this line case ( {A, B, enable} ) 3b000: D[0] = 0; 3b010: D[1] = 0; 3b100: D[2] = 0; 3b110: D[3] = 0; // Other cases??? endcase end endmodule
21
Behavioral modelling
A behavioral model of a module is an abstraction of how the module works. always defines a process Procedural statement
Suspend execution of this always process until a change occurs one of variable in the sensitive list. C programming-like Conversely, structural descriptions are concurrent statements.
Verilog
module name( port list) Declarartion of signals Interconnections of low-level modules or primitives assign p= a&b assign Concurrent always@(...) begin running . end always@(...) begin .. end
endmodule
23
24
25
module_testBench
module testBench; wire w1, w2, w3, w4, w5; binaryToESeg d (w1, w2, w3, w4, w5); test_bToESeg t (w1, w2, w3, w4, w5); endmodule
26
Test module
module test_bToESeg (output reg A, B, C, D, input eSeg); initial // two slashes introduce a single line comment begin $monitor ( $time,, "A = %b B = %b C = %b D = %b, eSeg = %b", A, B, C, D, eSeg); //waveform for simulating the nand lip lop #10 A = 0; B = 0; C = 0; D = 0; #10 D = 1; 0 A = x B = x C = x D = x, eSeg = x 10 A = 0 B = 0 C = 0 D = 0, eSeg = x #10 C = 1; D = 0; 12 A = 0 B = 0 C = 0 D = 0, eSeg = 1 #10 $finish; 20 A = 0 B = 0 C = 0 D = 1, eSeg = 1 end 22 A = 0 B = 0 C = 0 D = 1, eSeg = 0 endmodule 30 A = 0 B = 0 C = 1 D = 0, eSeg = 0 27
32 A = 0 B = 0 C = 1 D = 0, eSeg = 1
eSeg A B C D
Test (DUT)
28
Q0 x clk
Q0
29
//D flip-flop module D_FF(D, Q, CLK, RST); input D, CLK, RST; output Q; reg Q; always @(posedge CLK or negedge RST) if(~RST) Q = 1'b0; else Q = D; endmodule
reset Q0 x clk x Q1 D Q clk Q0 D Q clk ~Q1
module state_machine(x, reset, clk, z); input x, Reset, clk; output z; wire D1, D0; assign z =~Q1 & Q0; assign D1 =Q0 & x; assign D0= x | Q1; D_FF A1(D1, Q1, clk, reset); D_FF A0(D0, Q0, clk, reset); endmodule
Z
31
D_FF
always @(posedge clk, negedge reset) begin if (~reset) curState <=0; else curState <= nextState; end endmodule
32
Module Hierarchy
board
Display driver
count
m16
clk
m555
A counter example
33
Top module
Bus concatenation module boardWithConcatenation; wire clock, eSeg, w3, w2, w1, w0; m16 counter ({w3, w2, w1, w0}, clock); m555 clockGen (clock); binaryToESeg disp (eSeg, w3, w2, w1, w0); instantiate
initial $monitor ($time,,,"count=%d, eSeg=%d", {w3, w2, w1, w0}, eSeg); endmodule
34
m16 (Counter)
module m16 (output reg [3:0] ctr = 1, input clock); always @(posedge clock) ctr <= ctr + 1; endmodule
35
Rules for Synthesizable Combinational Circuits All inputs to your combinational function must be listed in the sensitive list. Combinational output(s) must be assigned to every control path.
37
module synAutoSensitivity ( input a, b, c, output reg f); always @( a, b ,c) always @( *) if (a == 1) f = b; else f = c; endmodule
38
module synAutoSensitivity ( input a, b, c, output reg f); always @(*) begin f = c; if (a == 1) f = b; end endmodule
39
Inferred Latches
amodule synAutoSensitivity ( input a, b, c, output reg f); always @(*) if (a == 1) f=b&c; // else f = ? endmodule
40
41
3b000: f = 1b0; 3b001: f = 1b1; 3b010: f = 1b1; 3b011: f = 1b1; 3b100: f = 1b1; 3b101: f = 1b0; 3b110: f = 1b0; 3b111: f = 1b1; endcase endmodule
42
You cant say if (a == 1bx) this has meaning in ab simulation, but not in 00 01 11 10 c synthesis. 0 x 1 0 1 However, an unknown x 1 1 x 1 1 on the right-hand side will be interpreted as a The inverse function was implemented; dont care.
xs taken as ones.
44
45
if (~reset),,,,
Latch inferences
module synLatchReset( Q, g, d, reset); input g, d, reset; output Q; reg Q; always @(*) if (~reset) Q = 0; else if (g) Q = d; // else Q = ? endmodule To infer a latch, two situations must exist in the always statement: At least one control path must exist that does not assign to an output. The sensitivity list must not contain any edge-sensitive specifications. (levelsensitive )
47
48
Conclusion
Type of Logic Combinational Output Assign To An output must be assigned to in all control path Edge Specifiers in Sensitivity List Not allowed. The whole input set must be in the sensitivity list.The construct @(*) assure this.
Interred latch
There must exist Not allowed. at least one control path where an output is not assign to. From this omission,the tool infers a latch. No affect. Required from the presence of an edge specifier, the tool infers a flip flop. All registers in the always block are clocked by the specified edge.
49