13 Verilog
13 Verilog
Hierarchical Design
4-bit adder in terms of full-adders
Full-adders in terms of half-adders
Half-adders in terms of gates.
2
Hierarchical Design
//Description of full adder (see Fig 4-8)
module fulladder (S,C,x,y,z);
input x,y,z;
output S,C;
wire S1,D1,D2; //Outputs of first XOR and two AND gates
//Instantiate the halfadder
halfadder HA1 (S1,D1,x,y),
HA2 (S,D2,S1,z);
or g1(C,D2,D1);
endmodule
S1
D1 D2
3
Hierarchical Design
4
Dataflow Description
• Using some
operators and operator Operation
+ Binary addition
signal assignments - Binary
concat appends two subtraction
& Bitwise and
operands and makes a
| Bitwise or
larger one.
^ Bitwise xor
~ Bitwise not
== Equality
> Greater
< Less
{ } Concatenation
?: Conditional
5
Decoder
continuous assignment:
− assigns a value to a net (wire, output).
6
MUX
7
4-Bit Adder
12
Testbench
• Testbench:
Applies inputs and is used to see the outputs.
Uses initial to drive the values.
− runs once.
always runs many times
initial
begin
A = 1; B = 0;
#10 A = 0;
#20 A = 1; B = 1;
end
13
Testbench
001 is added to D seven times with 10 time
units in between.
initial
begin
D = 3’b000;
repeat (7)
#10 D = D + 3’b001;
end
14
Testbench Structure
Testbench items:
1. test module name
2. reg and wire declarations
3. instantiation of circuit under test
4. initial/always statement
5. outputs display
No input/output ports:
− Signals are applied through local regs.
− Outputs to be displayed are declared as
wires.
15
Testbench
You can see waveforms
Values can be displayed by Verilog system
tasks:
− $display: displays variable value once (with
newline)
− $display (format, argument list);
− $write: as $display without newline,
− $monitor: displays any variable that is changed
during simulation.
− $time: shows simulation time.
− $finish: finishes simulation.
$display (“%d %b %b”, C, A, B);
$display (“time = %0d A = %b B = %b”, $time, A, B)
16
Testbench: Example
//HDL Example 4-9 Testbench items:
//---------------------------- 1.test module name,
//Stimulus for mux2x1_df.
module testmux; 2.reg and wire declarations,
reg TA,TB,TS; //inputs for mux 3.instantiation of circuit under
wire Y; //output from mux
mux2x1_df mx (TA,TB,TS,Y);//instantiate muxtest,
initial 4.initial/always statement,
begin
TS = 1; TA = 0; TB = 1; 5.outputs display,
#10 TA = 1; TB = 0;
#10 TS = 0;
#10 TA = 0; TB = 1;
end
initial
$monitor("select = %b A = %b B = %b OUT = %b time = %0d",
TS, TA, TB, Y, $time);
endmodule
18
Testbench: Example 2
19
Testbench: Example2
//HDL Example 4-10
//------------------------------------------
//Gate-level description of circuit of Fig. 4-2
module analysis (A,B,C,F1,F2);
input A,B,C;
output F1,F2;
wire T1,T2,T3,F2not,E1,E2,E3;
or g1 (T1,A,B,C);
and g2 (T2,A,B,C);
and g3 (E1,A,B); //Stimulus to analyze the circuit
and g4 (E2,A,C); module test_circuit;
and g5 (E3,B,C); reg [2:0]D;
or g6 (F2,E1,E2,E3); wire F1,F2;
not g7 (F2not,F2); analysis fig42(D[2],D[1],D[0],F1,F2);
and g8 (T3,T1,F2not); initial
Simulation Log:
or g9 (F1,T2,T3); begin
endmodule D = 3'b000;
ABC = 000 F1 = 0 F2 =0 repeat(7)
ABC = 001 F1 = 1 F2 =0 #10 D = D + 1'b1;
ABC = 010 F1 = 1 F2 =0 end
ABC = 011 F1 = 0 F2 =1 initial
ABC = 100 F1 = 1 F2 =0 $monitor ("ABC = %b F1 = %b F2 =%b ",D,
ABC = 101 F1 = 0 F2 =1 F1, F2);
ABC = 110 F1 = 0 F2 =1 endmodule
ABC = 111 F1 = 1 F2 =1
20