HDL Lab Manual
HDL Lab Manual
Block diagram:
enable
Example of decoder:
Truth Table:
EN A B D3 D2 D1 D0
0 x x 1 1 1 1
1 0 0 1 1 1 0
1 0 1 1 1 0 1
1 1 0 1 0 1 1
1 1 1 0 1 1 1
Testbench:
module test_dec24str;
reg test_A,test_B,test_EN;
wire test_D3,test_D2,test_D1,test_D0;
dec24str DUT (test_A,test_B,test_EN,test_D3,test_D2,test_D1,test_D0);
initial
begin
test_A =1'b0; test_B=1'b0;test_EN=1'b0;
#20 test_A =1'b0; test_B=1'b0;test_EN=1'b1;
#20 test_A = 0; test_B=1;
#20 test_A = 1; test_B=0;
#20 test_A = 1; test_B=1;
#20;
end
initial
begin
$monitor($time,"test_A:%b, test_B:%b,test_EN:%b, test_D3:%b,
test_D2:%b,test_D1:%b,test_D0:%b ",test_A,test_B,test_EN, test_D3,
test_D2,test_D1,test_D0);
$dumpfile("dec.vcd");
$dumpvars;
end
endmodule
WAVEFORM OF 2 TO 4 DECODER:
UCF:
d(7:0)
Truth Table:
enable d(7) d(6) d(5) d(4) d(3) d(2) d(1) d(0) y(2) y(1) y(0)
0 X X X X X X X X 0 0 0
1 0 0 0 0 0 0 0 1 0 0 0
1 0 0 0 0 0 0 1 0 0 0 1
1 0 0 0 0 0 1 0 0 0 1 0
1 0 0 0 0 1 0 0 0 0 1 1
1 0 0 0 1 0 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1 1 0
1 1 0 0 0 0 0 0 0 1 1 1
Testbench Code:
module TB_encoder83();
regtest_enable;
reg [7:0] test_d;
wire [2:0] test_y;
encoder83 dut (.enable(test_enable), .d(test_d), .y(test_y));
initial
begin
test_d = 8'b00000001;
#20 test_enable = 1'b0;
#20 test_enable = 1'b1;
#20 test_d = 8'b00000010;
#20 test_d = 8'b00000100;
#20 test_d = 8'b00001000;
#20 test_d = 8'b00010000;
#20 test_d = 8'b00100000;
#20 test_d = 8'b01000000;
#20 test_d = 8'b10000000;
#20;
end
initial
begin
$monitor($time, " test_enable:%b, test_d:%b, test_y:%b ", test_enable, test_d, test_y);
$dumpfile("dec1.vcd");
$dumpvars;
$dumpvars(1);
end
endmodule
WAVEFORM OF 8 TO 3 ENCODER :
UCF:
Block diagram:
8:3
d(7:0) y(2:0)
Priority Encoder
VALID
Truth Table:
d(7) d(6) d(5) d(4) d(3) d(2) d(1) d(0) y(2) y(1) y(0)
VALID
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 X 0 0 1 1
0 0 0 0 0 1 X X 0 1 0 1
0 0 0 0 1 X X X 0 1 1 1
0 0 0 1 X X X X 1 0 0 1
0 0 1 X X X X X 1 0 1 1
0 1 X X X X X X 1 1 0 1
1 X X X X X X X 1 1 1 1
Testbench Code:
module TB_encoder83p();
reg [7:0] test_d;
wire [2:0] test_y;
encoder83p dut (.d(test_d), .y(test_y));
initial
begin
#20 test_d = 8'b11111111;
#20 test_d = 8'b01111111;
#20 test_d = 8'b00111111;
#20 test_d = 8'b00011111;
#20 test_d = 8'b00001111;
#20 test_d = 8'b00000111;
#20 test_d = 8'b00000011;
#20 test_d = 8'b00000001;
#20 test_d = 8'b00000000;
#20;
end
initial
begin
$monitor($time, " test_d:%b, test_y:%b ", test_d, test_y);
$dumpfile("dec1.vcd");
$dumpvars;
//$dumpvars(1);
end
endmodule
UCF:
Block diagram:
din(7:0)
8:1 MULTIPLEXER
dout
sel(2:0)
Truth Table:
Selection Lines Input data Lines o/p
i/ps
sel(2) sel(1) sel(0) din(7) din(6) din(5) din(4) din(3) din(2) din(1) din(0) dout
0 0 0 X X X X X X X 0/1 din(0)
0 0 1 X X X X X X 0/1 X din(1)
0 1 0 X X X X X 0/1 X X din(2)
0 1 1 X X X X 0/1 X X X din(3)
1 0 0 X X X 0/1 X X X X din(4)
1 0 1 X X 0/1 X X X X X din(5)
1 1 0 X 0/1 X X X X X X din(6)
1 1 1 0/1 X X X X X X X din(7)
Testbench Code:
module TB_mx81();
reg [7:0] test_din;
reg [2:0] test_sel;
wire test_dout;
Testbench Code:
module TB_mx81();
reg [7:0] test_din;
reg [2:0] test_sel;
wire test_dout;
mux81 DUT (test_din,test_sel,test_dout);
initial
begin
test_din =8'b11001100;
test_sel= 3'b000;
#10 test_sel= 3'b001;
#10 test_sel= 3'b010;
#10 test_sel= 3'b011;
#10 test_sel= 3'b100;
#10 test_sel= 3'b101;
UCF:
NET "din<7>" LOC = "p80";
NET "din<6>" LOC = "p81";
NET "din<5>" LOC = "p82";
NET "din<4>" LOC = "p84";
NET "din<3>" LOC = "p83";
NET "din<2>" LOC = "p88";
NET "din<1>" LOC = "p85";
NET "din<0>" LOC = "p92";
NET "SEL<2>" LOC = "p114”;
NET "SEL<1>" LOC = "p115";
NET "SEL<0>" LOC = "p134";
NET "dout" LOC = "p93" ;
Block diagram:
Truth Table:
(a)Logic Diagram of 4-bit Binary-to-Gray Code converter using 1-bit Adder and Subtractor
circuit:
(b)4-bit Binary- to- Gray Code Converter using 1-bit Gray-to-Binary Code Converter
Top-level Module:
module bin_gray_1 (B,G);
input [3:0] B ;
output [3:0] G ;
assign G[3] = B[3];
one_bit_G_to_B GB1 (B[3], B[2], G[2]);
one_bit_G_to_B GB2 (B[2], B[1], G[1]);
one_bit_G_to_B GB3 (B[1], B[0], G[0]);
endmodule
UCF:
PROGRAM 2A: Model in Verilog for a full adder and add functionality to perform logical
operations of XOR, XNOR, AND and OR gates. Write test bench with appropriate input
patterns to verify the modelled behaviour.
Block Diagram:
A Sum
B Full Adder
Cout
Cin
Truth Table:
Inputs Outputs
A B Cin SUM Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Source Code:
module fa_dataflow (A, B, Cin, Sum, Cout);
input A;
input B;
input Cin ;
output Sum ;
output Cout ;
wire s1,s2 ;
assign s1 = Cin& (~(x ^ y));
assign s2 = ~Cin& (x ^ y);
assign Sum = s1 | s2;
assign Sum = Cin& (~(A ^ B)) | ~Cin& (A ^ B);
assign Cout = (A & B) | (B &Cin) | (A&Cin) ;
endmodule
Testbench Code:
moduleTB_fa_dataflow();
reg test_A;
reg test_B;
reg test_Cin;
wire test_Sum;
wire test_Cout;
fa_dataflow dut (.A(test_A), .B(test_B), .Cin(test_Cin), .Sum(test_Sum), .Cout(test_Cout));
initial
begin
test_A = 1'b0; test_B = 1'b0; test_Cin = 1'b0;
#20 test_A= 1'b0; test_B= 1'b0; test_Cin = 1'b1;
#20 test_A= 1'b0; test_B = 1'b1; test_Cin = 1'b0;
#20 test_A = 1'b0; test_B = 1'b1; test_Cin = 1'b1;
#20 test_A= 1'b1; test_B= 1'b0; test_Cin = 1'b0;
#20 test_A = 1'b1; test_B = 1'b0; test_Cin = 1'b1;
#20 test_A = 1'b1; test_B = 1'b1; test_Cin = 1'b0;
#20 test_A = 1'b1; test_B = 1'b1; test_Cin = 1'b1;
#20 ;
end
initial
begin
$monitor($time, " A:%b, B:%b, Cin:%b, Sum:%b, Cout:%b ", test_A, test_B, test_Cin,
test_Sum, test_Cout);
$dumpfile("fa.vcd");
$dumpvars;
end
endmodule
Waveforms:
opcode(2:0) 1. A+B
2. A-B
3. A Complement
32- BIT 4. A*B
enable 5. A AND B
ALU 6. A OR B
7. A NAND B
8. A XOR B
Results(32:0)
end
endcase
end
endmodule
TEST BENCH
module alu_TB;
reg enable;
reg [2:0] opcode;
reg [31:0] A,B;
wire[32:0] Results;
wire ack;
alu_32bit A1( .enable(enable),.opcode(opcode),.A(A),.B(B),.Results(Results),.ack(ack));
initial
begin
enable = 1'b0;
A=32'd3; B = 32'd2;
#10 enable = 1'b1;
#10 opcode= 3'b000;
#10 opcode = 3'b001;
#10 opcode = 3'b010;
#10 opcode = 3'b011;
#10 opcode = 3'b100;
#50 enable = 1'b0;
#10 enable = 1'b1;
#10 opcode = 3'b101;
#10 opcode = 3'b110;
#10 opcode = 3'b111;
#10;
end
initial
begin
$monitor($time, " enable:%b, opcode :%b, A:%b, B:%b, Results:%b, ack
:%b",enable,opcode,A,B,Results,ack);
$dumpfile("ALU.vcd");
$dumpvars;
end
endmodule
UCF :
Generally Flip Flop and Counter outputs are seen using LED. But whenever a LED becomes
ON or OFF with a high frequency clock, human being can not visualize the change (ON and
OFF) by looking into it. Thus we need to reduce the frequency of clock. Use the following
clock division program for all the FLIP FLOPS and COUNTERS for implementing the
designs and download into FPGA kit.
Note : For simulation don’t include the clock division program in the Verilog source code
(Use on board 4 MHz clock only)
• Use the low frequency SIGNAL nclk in place of clk in the Verilog Source code
Block diagram:
s q
r qb
o
reset
Truth Table:
UCF:
d q q
D Flip Flop
clk clk qb qb
reset
Truth Table:
UCF:
j q
o
reset
Truth Table:
module jkff(
input reset, clk, j, k, output q, qb);
reg q;
always @ (negedge reset, posedge clk) begin
if (!reset)
q <= 1'b0;
else
begin case ({j,k})
2'b00: q <= q;
2'b01: q <= 1'b0;
2'b10: q <= 1'b1;
2'b11: q <= ~ q; default: q <= 1'bz; endcase
end end
assign qb = ~ q;
endmodule
JK FF Test Bench
module jkff_test;
reg reset,clk,j,k;
wire q,qb;
jkff UUT (.reset(reset),.clk(clk),.j(j),.k(k),.q(q),.qb(qb));
initial
begin
clk = 1'b0;
end
always # 5 clk = ~clk;
initial
begin
reset = 1'b0;
#15 reset = 1'b1;j = 1'b0; k = 1'b0;
#10 j = 1'b0; k = 1'b1;
#10 j = 1'b1; k = 1'b0;
#10 j = 1'b1; k = 1'b1;
#10 j = 1'b1; k = 1'b1;
#10 j = 1'b1; k = 1'b1;
#10 j = 1'b1; k = 1'b1;
#10;
end
initial
begin
$monitor($time, "reset = %b, clk = %b, j=%b, k=%b,q = %b, qb = %b", reset, clk, j, k,q, qb);
$dumpfile("jk.vcd");
$dumpvars();
#100 $finish ;
end
endmodule
UCF:
module bcd_syn_counter(
input reset, clk ,
output [3:0] q );
reg [3:0] q ;
always @ (negedge reset or posedgeclk)
begin
if(!reset)
q<=4'b0000 ;
else
begin
q<=q+1 ;
if (q == 4'b1001) //checking the condition
q<=4'b0000 ;
end
end
endmodule
moduletb_bcd;
reg reset;
regclk =1'b0;
wire [3:0] q;
bcd_syn_counter DUT (reset,clk,q);
always #5 clk = ~ clk ;
initial
begin
reset = 1'b0;
#15 reset =1'b1;
#200 $finish;
end
initial
begin
$monitor($time,"reset=%b,clk=%b,q = %b",reset,clk,q);
$dumpfile("bcd.vcd");
$dumpvars();
end
endmodule
UCF:
NET "reset" LOC = "p80";
NET "clk" LOC = "p55";
NET "q<3>" LOC = "p93";
NET "q<2>" LOC = "p94";
NET "q<1>" LOC = "p95";
NET "q<0>" LOC = "p97";
Note : Use same UCF for all counters.
Program 8: Write Verilog code for counter with given input clock and check whether it works
as clock divider performing division of clock by 2, 4, 8 and 16. Verify the functionality of the
code.
module binary_counter( input reset, clk, output div_2, div_4, div_8, div_16);
reg [3:0] q;
always @ (posedgeclk) begin
if(!reset) q<=4'b0000;
else q<=q+1;
end
assign div_2 = q[0];
assign div_4 = q[1];
assign div_8 = q[2];
assign div_16 = q[3];
endmodule
Test_Bench
module tb_bcd;
reg reset;
reg clk =1'b0;
wire div_2, div_4, div_8, div_16;
binary_counter DUT (reset,clk,div_2, div_4, div_8, div_16);
always #5 clk = ~ clk ;
initial
begin
reset = 1'b0;
#15 reset =1'b1;
#200 $finish;
end
initial
begin
$monitor($time,"reset=%b,clk=%b,div_2 = %b, div_4 = %b, div_8 = %b, div_16 =
%b",reset,clk,div_2, div_4, div_8,div_16);
$dumpfile("cdiv.vcd");
$dumpvars();
end
endmodule
WAVEFORMS