HDL Shubham
HDL Shubham
For
Bachelor of Technology
In
Electronics and Communication Engineering Department
Submitted by
Reg. No. UI21EC63
Name: Shubham yadav
Course Faculty
Ms. Sejal Rathod
Certificate
Task 1: Write HDL description using Verilog for the given circuit
using (a) Boolean-Equations With and Without gate delays, (b)
Structural-Objects With and Without gate delays, (c) Truth-Table.
Write testbench and verify their functionality using simulation.
Task 2: Write HDL description using VHDL for the given circuit.
output graph:-
B) Verilog code for basic logic gates using Structural
Object
Code -
testbench Design code
// Code your testbench here // Code your design here
// or browse Examples `timescale 1ns/1ps
`timescale 1ns/1ps module ec_63_db(output
module ec_63(); AND63,OR63,NAND63,NOR63,XOR63,XNOR63,NOT63, input
wire A63,B63);
AND_63,OR_63,NOT_63,NAND_63,NOR_63,XOR_63,XNOR_63; and(AND63,A63,B63);
reg P_63,Q_63; or(OR63,A63,B63);
ec_63_db I1(AND_63,OR_63,NOT_63,NAND_63,NOR_63, nand(NAND63, A63, B63);
XOR_63,XNOR_63,P_63,Q_63); nor (NOR63, A63, B63);
initial begin xor (XOR63, A63, B63);
$dumpfile("dump.vcd"); xnor (XNOR63, A63, B63);
$dumpvars(1); not (NOT63, A63);
P_63 = 1'b0; Q_63 = 1'b0; endmodule
#10 P_63 = 1'b0; Q_63 = 1'b1;
#10 P_63 = 1'b1; Q_63 = 1'b0;
#10 P_63 = 1'b1; Q_63 = 1'b1;
#10 P_63 = 1'b0; Q_63 = 1'b0;
end
initial #50 $finish;
endmodule
LAB – 2
Aim – Write HDL description in VHDL for basic logic gates
using Boolean-Equations. Write testbench and verify their
functionality using simulation.
Code -
testbench code
B) Write HDL description using VHDL for the circuit given above
using
Boolean-Equations: With and without gate delays
Code –
testbench code
// Code your testbench here // Code your design here
// or browse Examples `timescale 1ns/1ps
`timescale 1ns/1ps module ec_63(output E63,input A63,B63,C63);
module tb_ec_63(); wire D63;
wire E_63; or #5 (D63,A63,B63);
reg A_63,B_63,C_63; and #10 (E63,C63,D63);
ec_63 I1 (E_63,A_63,B_63,C_63); endmodule
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
A_63 = 1'b0; B_63 = 1'b0; C_63 = 1'b0;
#20 A_63 =1'b0; B_63 = 1'b0; C_63 =1'b1;
#20 A_63 =1'b0; B_63 = 1'b1; C_63 =1'b0;
#20 A_63 =1'b0; B_63 = 1'b1; C_63 =1'b1;
#20 A_63 =1'b1; B_63 = 1'b0; C_63 =1'b0;
#20 A_63 =1'b1; B_63 = 1'b0; C_63 =1'b1;
#20 A_63 =1'b1; B_63 = 1'b1; C_63 =1'b0;
#20 A_63 =1'b1; B_63 = 1'b1; C_63 =1'b1;
#20 A_63 =1'b0; B_63 = 1'b0; C_63 =1'b0;
end
initial #170 $finish;
endmodule
Witout delay
// Code your design here
`timescale 1ns/1ps
module ec_63(output E63,input A63,B63,C63);
wire D63;
or (D63,A63,B63);
and (E63,C63,D63);
endmodule
reg reg
A_63,B_63,SUM_63,CARRY_63; A_63,B_63,DIFF_63,BORROW_63;
half_sub_63
half_adder_63
I1(SUM_63,CARRY_63,A_63,B_63); I1(DIFF_63,BORROW_63,A_63,B_63);
$dumpfile("dump.vcd"); $dumpfile("dump.vcd");
$dumpvars(1); $dumpvars(1);
A_63 = 1'b0; B_63 = 1'b0; A_63 = 1'b0; B_63 = 1'b0;
endmodule
halfadd halfsub
module module
half_adder_63(output half_sub_63(output
SUM63,CARRY63,input DIFF63,BORROW63,input
A63,B63); A63,B63);
half_add_63 half_sub_63
I1(SUM_63,CARRY_63,A_63,B_63); I1(DIFF_63,BORROW_63,A_63,B_63);
$dumpfile("dump.vcd"); $dumpfile("dump.vcd");
$dumpvars(1); $dumpvars(1);
#20 A_63 = 1'b1; B_63 = 1'b0; #20 A_63 = 1'b1; B_63 = 1'b0;
#20 A_63 = 1'b1; B_63 = 1'b1; #20 A_63 = 1'b1; B_63 = 1'b1;
#20 A_63 = 1'b0; B_63 = 1'b0; #20 A_63 = 1'b0; B_63 = 1'b0;
end end
endmodule endmodule
halffadd halffsub
xor(SUM63,A63,B63); xor(DIFF63,A63,B63);
endmodule endmodule
3) Verilog code for the Half Adder and Half Subtractor
using UDP (User-Defined-Primitives).
primitive primitive
HALFADDER_CARRY_63 HALFSUB_BORROW_63
(CARRY_63,A_63,B_63); (BORROW_63,A_63,B_63);
output CARRY_63; output BORROW_63;
input A_63,B_63; input A_63,B_63;
table table
// a63 b63 : // a63 b63 :
carry63 carry63
0 0 : 0; 0 0 : 0;
0 1 : 0; 0 1 : 1;
1 1 : 1; 1 1 : 0;
1 0 : 0; 1 0 : 0;
endtable endtable
endprimitive endprimitive
`timescale 1ns/1ps `timescale 1ns/1ps
module module
half_adder_63(output half_sub_63(output
SUM_63,CARRY_63,input SUM_63,BORROW_63,input
A_63,B_63); A_63,B_63);
HALFADDER_SUM_63 HALFSUB_DIFF_63
U1(SUM_63,A_63,B_63); U1(SUM_63,A_63,B_63);
HALFADDER_CARRY_63 HALFSUB_BORROW_63
U2(CARRY_63,A_63,B_63); U2(CARRY_63,A_63,B_63);
endmodule endmodule
B) 1) VHDL code for the Half Adder and Half Subtractor using
Dataflow modeling.
DUT: cb_63 port A63 <= '0', '0' after 20 ns , '1' after 40 ns,
map(A63,B63,DIFF63,BORROW63); '1' after 60 ns, '0' after 80 ns ;
A63 <= '0', '0' after 20 ns , '1' after 40 ns, B63 <= '0', '1' after 20 ns , '0' after 40 ns,
'1' after 60 ns, '0' after 80 ns ; '0' after 60 ns, '1' after 80 ns ;
end ec_63;
Code halfsub halfadd
begin begin
DIFF63 <= A63 XOR B63; SUM63 <= A63 XOR B63;
BORROW63 <= NOT(A63) AND B63; CARRY63 <= A63 AND B63;
half_adder_63 half_sub_63
I1(SUM_63,CARRY_63,A_63,B_63); I1(DIFF_63,BORROW_63,A_63,B_63);
$dumpfile("dump.vcd"); $dumpfile("dump.vcd");
$dumpvars(1); $dumpvars(1);
#20 A_63 = 1'b0; B_63 =1'b1; #20 A_63 = 1'b0; B_63 =1'b1;
#20 A_63 = 1'b1; B_63 = 1'b0; #20 A_63 = 1'b1; B_63 = 1'b0;
#20 A_63 = 1'b1; B_63 = 1'b1; #20 A_63 = 1'b1; B_63 = 1'b1;
#20 A_63 = 1'b0; B_63 = 1'b0; #20 A_63 = 1'b0; B_63 = 1'b0;
end end
endmodule endmodule
Half Adder Half Subtractor
// Code your design here // Code your design here
(SUM_63,A_63,B_63); (DIFF_63,A_63,B_63);
table table
0 0 : 0; 0 0 : 0;
0 1 : 1; 0 1 : 1;
1 0 : 1; 1 0 : 1;
1 1 : 0; 1 1 : 0;
endtable endtable
endprimitive endprimitive
(CARRY_63,A_63,B_63); (BORROW_63,A_63,B_63);
table table
0 0 : 0; 0 0 : 0;
0 1 : 0; 0 1 : 1;
1 1 : 1; 1 1 : 0;
1 0 : 0; 1 0 : 0;
endtable endtable
endprimitive endprimitive
HALFADDER_SUM_63 HALFSUB_DIFF_63
U1(SUM_63,A_63,B_63); U1(SUM_63,A_63,B_63);
HALFADDER_CARRY_63 HALFSUB_BORROW_63
U2(CARRY_63,A_63,B_63); U2(CARRY_63,A_63,B_63);
endmodule endmodule
LAB – 5
Aim – A) Write HDL description in Verilog using dataflow modeling
for a) 1-bit magnitude comparator circuit {w/o predefined
operators}. b) 4-bit magnitude comparator circuit {with
predefined operators}. c) a circuit which receives 3-bit inputs
A and B and produces results for A * B where * is various bit-
wise operators and logical operators. Write testbench and verify
their functionality using simulation.
Testbench code
// Code your testbench here // Code your design here
// or browse Examples `timescale 1ns/1ps
`timescale 1ns/1ps module bit_comp_63(input
module mc_ec_63(); A63,B63, output
LESS63,EQUAL63,GREATER63);
reg
A63,B63,LESS63,EQUAL63,GREATER63; assign LESS63= ~A63 & B63;
bit_comp_63 I1(A63,B63,LESS63, assign EQUAL63 = A63 &
EQUAL63, GREATER63); ~B63;
initial begin
assign GREATER63 = ~((~A63 &
$dumpfile("dump.vcd"); B63) | A63 & ~B63);
$dumpvars(1); endmodule
A63 = 1'b0 ; B63= 1'b0;
testbench code
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
3) Write HDL description in Verilog for a circuit which
receives 3-bit inputs A and B and produces results for
A * B where * is various bit-wise operators and logical
operators.
testbench code
// Code your testbench here // Code your design here
// or browse Examples `timescale 1ns/1ps
`timescale 1ns/1ps module bit_comp_63 (input [2:0] A63,B63, output [2:0]
module bit_comps_63; AND63,OR63,XOR63,NOT63,AND_L63,OR_L63,NOT_L63);
reg [2:0] assign AND63 = A63 & B63;
A63,B63,AND63,OR63,XOR63,NOT63,AND_L63,OR_L63,NOT_L63; assign OR63 = A63 | B63;
bit_comp_63 assign XOR63 = A63 ^ B63;
I1(A63,B63,AND63,OR63,XOR63,NOT63,AND_L63,OR_L63,NOT_L63); assign NOT63 = ~(A63);
initial begin
$dumpfile("dump.vcd"); assign AND_L63 = A63 && B63;
$dumpvars(1); assign OR_L63 = A63 || B63;
A63 = 3'b000 ; B63= 3'b000; assign NOT_L63 = !(A63);
endmodule
#20 A63 = 3'b110; B63 = 3'b010;
#20 A63 = 3'b011; B63 = 3'b100;
#20 A63 = 3'b111; B63 = 3'b010;
#20 A63 = 3'b000; B63 = 3'b101;
end
initial #80 $finish;
endmodule
A_63 <= '0', '0' after 20 ns , '1' after 40 ns , '0' after 60 ns , '1'
after 80 ns ;
B_63 <= '0', '1' after 20 ns , '0' after 40 ns , '1' after 60 ns , '0'
after 80 ns ;
end bit1_63;
testbench code
-- Code your testbench here -- Code your design here
library IEEE; library IEEE;
use IEEE.std_logic_1164.all; use IEEE.std_logic_1164.all;
entity bit3_ec_63 is entity bit3_comp_63 is
end bit3_ec_63 ; port (A63,B63 : in std_logic_vector(2 downto 0);
architecture ec_arch_63 of bit3_ec_63 is AND63,OR63,NAND63,NOR63,XOR63,XNOR63,NOT63 : out
component bit3_comp_63 is std_logic_vector(2 downto 0));
port (A63,B63 : in std_logic_vector(2 downto 0); end bit3_comp_63 ;
AND63,OR63,NAND63,NOR63,XOR63,XNOR63,NOT63 : out std_logic_vector(2 architecture bit3_design_63 of bit3_comp_63 is
downto 0)); begin
end component; AND63 <= A63 AND B63; -- logical operator act as bitwise
signal A_63,B_63 : std_logic_vector(2 downto 0); OR63 <= A63 OR B63;
signal AND_63,OR_63,NAND_63,NOR_63,XOR_63,XNOR_63,NOT_63 : NAND63 <= A63 NAND B63;
std_logic_vector(2 downto 0); NOR63 <= A63 NOR B63;
begin XOR63 <= A63 XOR B63;
DUT:bit3_comp_63 XNOR63 <= A63 XNOR B63;
port NOT63 <= NOT(A63);
map(A_63,B_63,AND_63,OR_63,NAND_63,NOR_63,XOR_63,XNOR_63,NOT_63 end bit3_design_63;
);
A_63 <= "000" after 0 ns, "100" after 20 ns, "011" after 40 ns, "111" after 60 ns,
"000" after 80 ns ;
B_63 <= "000" after 0 ns, "010" after 20 ns, "100" after 40 ns, "010" after 60 ns,
"101" after 80 ns ;
end ec_arch_63;
LAB – 6
Aim – Write HDL description in VHDL using structural modeling
for A) Full-Adder circuit B) Full-Subtractor circuit C) Full-
Adder / FullSubtractor circuit using mode control input. Write
testbench and verify their functionality using simulation.
Circuit –
Code –
testbench code
-- Code your testbench here -- Code your design here
library IEEE;
use IEEE.std_logic_1164.all; ---XOR GATE
entity ec_full_adder_ui_63 is library IEEE;
end ec_full_adder_ui_63 ; use IEEE.std_logic_1164.all;
architecture ec_ui_63 of ec_full_adder_ui_63
is entity xor_gate_63 is
component full_add_63 is port (A63,B63 : in std_logic ; D63 : out
port(A63,B63,Cin63 : in std_logic; std_logic);
SUM63,Cout63 : out std_logic); end xor_gate_63 ;
end component; architecture xor_63 of xor_gate_63 is
signal A63,B63,Cin63,SUM63,Cout63 : begin
std_logic; D63 <= A63 xor B63;
begin end xor_63 ;
DUT : full_add_63 port
map (A63,B63,Cin63,SUM63,Cout63);
A63 <= '0', '1' after 80 ns , '0' after 160 ns ; --AND GATE
B63 <= '0', '1' after 40 ns , '0' after 80 ns, '1' library IEEE;
after 120 ns , '0' after 160 ns ; use IEEE.std_logic_1164.all;
Cin63 <= '0', '1' after 20 ns , '0' after 40 ns , '1' entity and_gate_63 is
after 60 ns , '0' after 80 ns , '1' after 100 ns , '0' port(A63,B63 : in std_logic; D63 : out std_logic);
after 120 ns , '1' after 140 ns ,'0' after 160 ns ; end and_gate_63;
end ec_ui_63; architecture and_63 of and_gate_63 is
begin
D63 <= A63 and B63;
end and_63;
--OR GATE
library IEEE;
use IEEE.std_logic_1164.all;
entity or_gate_63 is
port(A63,B63 : in std_logic ; D63 : out
std_logic);
end or_gate_63;
architecture or_63 of or_gate_63 is
begin
D63 <= A63 or B63 ;
end or_63;
--FULL ADDER
library IEEE;
use IEEE.std_logic_1164.all;
entity full_add_63 is
port(A63, B63, Cin63 : in std_logic; Sum63,
Cout63 : out std_logic);
end full_add_63;
architecture full_adderr_63 of full_add_63 is
signal XOR1, XOR2,AND1,AND2,OR1 : std_logic;
component xor_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component ;
component and_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component;
component or_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component;
begin
XOR1_gate : xor_gate_63 port map
(A63,B63,XOR1);
XOR2_gate : xor_gate_63 port map
(XOR1,Cin63,XOR2);
AND1_gate : and_gate_63 port map
(XOR1,Cin63,AND1);
AND2_gate : and_gate_63 port map (A63, B63,
AND2);
OR1_gate : or_gate_63 port map
(AND1,AND2,OR1);
Sum63 <= XOR2;
Cout63 <= OR1;
end full_adderr_63;
B) VHDL code for Full Subtractor circuit using
structural modeling.
Circuit –
Code –
testbench code
-- Code your testbench here -- Code your design here
library IEEE;
use IEEE.std_logic_1164.all; ---XOR GATE
library IEEE;
use IEEE.std_logic_1164.all;
entity ec_full_sub_ui_63 is library IEEE;
end ec_full_sub_ui_63 ; use IEEE.std_logic_1164.all;entity xor_gate_63
architecture ec_ui_63 of ec_full_sub_ui_63 is is
component full_sub_63 is port (A63,B63 : in std_logic ; D63 : out
port(A63,B63,Cin63 : in std_logic; std_logic);
DIFF63,BORROW63 : out std_logic); end xor_gate_63 ;
end component; architecture xor_63 of xor_gate_63 is
signal A63,B63,Cin63,DIFF63,BORROW63 : begin
std_logic ; D63 <= A63 xor B63;
begin end xor_63 ;
DUT : full_sub_63 port map
(A63,B63,Cin63,DIFF63,BORROW63); --AND GATE
A63 <= '0', '1' after 80 ns , '0' after 160 ns ; library IEEE;
B63 <= '0', '1' after 40 ns , '0' after 80 ns, '1' use IEEE.std_logic_1164.all;
after 120 ns , '0' after 160 ns ;
entity and_gate_63 is
Cin63 <= '0', '1' after 20 ns , '0' after 40 ns , '1' port(A63,B63 : in std_logic; D63 : out std_logic);
after 60 ns , '0' after 80 ns , '1' after 100 ns , '0' end and_gate_63;
after 120 ns , '1' after 140 ns ,'0' after 160 ns ; architecture and_63 of and_gate_63 is
end ec_ui_63; begin
D63 <= A63 and B63;
end and_63;
--OR GATE
library IEEE;
use IEEE.std_logic_1164.all;
entity or_gate_63 is
port(A63,B63 : in std_logic ; D63 : out
std_logic);
end or_gate_63;
architecture or_63 of or_gate_63 is
begin
D63 <= A63 or B63 ;
end or_63;
--NOT GATE
library IEEE;
use IEEE.std_logic_1164.all;
entity not_gate_63 is
port ( A63 : in std_logic; D63 : out std_logic );
end not_gate_63;
architecture not_63 of
not_gate_63 is
begin
D63 <= not(A63);
end not_63;
--FULL SUB
library IEEE;
use IEEE.std_logic_1164.all;
entity full_sub_63 is
port(A63, B63, Cin63 : in std_logic; DIFF63,
BORROW63 : out std_logic);
end full_sub_63;
architecture full_subb_63 of full_sub_63 is
signal
XOR1,XOR2,XOR3,A_NOT,AND1,AND2,OR1 :
std_logic;
component xor_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component ;
component and_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component;
component or_gate_63
port(A63,B63 : in std_logic; D63 : out std_logic);
end component;
component not_gate_63
port (A63 : in std_logic; D63 : out std_logic);
end component;
begin
XOR1_gate : xor_gate_63 port map
(A63,B63,XOR1);
XOR2_gate : xor_gate_63 port map
(XOR1,Cin63,XOR2);
NOT1_gate: not_gate_63 port map (A63 ,
A_NOT);
XOR3_gate : xor_gate_63 port map
(B63,Cin63,XOR3);
AND1_gate : and_gate_63 port map (A_NOT,
XOR3, AND1);
AND2_gate : and_gate_63 port map (B63,
Cin63, AND2);
OR1_gate : or_gate_63 port map
(AND1,AND2,OR1);
DIFF63 <= XOR2;
BORROW63 <= OR1;
end full_subb_63;
LAB 7
Aim : A. Write HDL description using Verilog in (1) Boolean-Equations, (2) Structural Method
B. Write HDL description using VHDL in (1) Boolean-Equations, (2) Structural Method (3) with-select-
when Data flow, (4) w
TESTBENCH CODE
// Test Bench
`timescale 1ns/1ps // Verilog Decoder, boolean equation
module TestBench(); `timescale 1ns/1ps
reg A, B; module Decoder(output [3:0] d, input A, B);
wire [3:0] d; assign d[0] = ~A && ~B;
Decoder I1 (d, A, B); assign d[1] = A && ~B;
initial begin assign d[2] = ~A && B;
$dumpfile("dump.vcd"); assign d[3] = A && B;
$dumpvars(1); endmodule
A = 1'b0; B = 1'b0;
#10 A = 1'b0; B = 1'b1;
#10 A = 1'b1; B = 1'b0;
#10 A = 1'b1; B = 1'b1;
#10 A = 1'b0; B = 1'b0;
end
initial #50 $finish;
endmodule
testbench code
// Test Bench `timescale 1ns/1ps
`timescale 1ns/1ps module Decoder(output [3:0] d, input a, b);
module TestBench(); wire not_a, not_b;
reg a, b; not(not_a, a);
wire [3:0] d, not_a, not_b; not(not_b, b);
Decoder I1 (d, a, b); and(d[0], not_a, not_b);
initial begin and(d[2], not_a, b);
$dumpfile("dump.vcd"); and(d[1], a, not_b);
$dumpvars(1); and(d[3], a, b);
a = 1'b0; b = 1'b0; endmodule
#10 a = 1'b0; b = 1'b1;
#10 a = 1'b1; b = 1'b0;
#10 a = 1'b1; b = 1'b1;
#10 a = 1'b0; b = 1'b0;
end
initial #50 $finish;
endmodule
Code: B. VHDL
testbench code
-- Code your testbench here -- VHDL Decoder, Boolean Equation
library IEEE; library IEEE;
use IEEE.std_logic_1164.all; use IEEE.std_logic_1164.all;
library IEEE;
use IEEE.std_logic_1164.all;
entity decoder is
port (
d: out std_logic_vector(3 downto 0);
a, b: in std_logic
);
end decoder;
Test-bench 4. when-else
component decoder );
); begin
a <= "00", "01" after 10 ns, "10" after 20 ns, "11" end arc_decoder;
after 30 ns, "00" after 40 ns;
end arc_test;
(4) when-else
(4) when-else
Code Part 1:
A. In Verilog
Test-bench 1. Boolean-Equations
$dumpfile("dump.vcd"); endmodule
$dumpvars(1);
b = 4'b0000;
#10 b = 4'b0011;
#10 b = 4'b0110;
#10 b = 4'b1011;
#10 b = 4'b1100;
#10 b = 4'b1111;
end
endmodule
Output:
$dumpfile("dump.vcd");
$dumpvars(1);
b = 4'b0000;
#10 b = 4'b0011;
#10 b = 4'b0110;
#10 b = 4'b1011;
#10 b = 4'b1100;
#10 b = 4'b1111;
end
Output:
B. In VHDL
Test-bench 1. Boolean-Equations
component btog );
); begin
end TEST;
Output:
y: out std_logic;
component btog );
); begin
end BUF34;
x, y: in std_logic
);
end xor34;
begin
z <= x xor y;
end XOR34;
library IEEE;
use IEEE.std_logic_1164.all;
entity btog is
port(
b: in std_logic_vector(3 downto 0)
);
end btog;
component buf34
port(
y: out std_logic;
x: in std_logic);
end component;
component xor34
port(
z: out std_logic;
x, y: in std_logic
);
end component;
begin
end BTOG;
Output:
Test-bench 3. with-select-when
component btog );
); begin
b <= "0000", "0011" after 10 ns, "0110" after 20 ns, "0110" when "0100",
"1011" after 30 ns, "1100" after 40 ns, "1111" after
"0111" when "0101",
50 ns;
"0101" when "0110",
end TEST;
"0100" when "0111",
end BTOG;
Output:
Test-bench 4. when-else
component btog );
); begin
DUT: btog port map(g, b); "0110" when (b <= "0100") else
b <= "0000", "0011" after 10 ns, "0110" after 20 ns, "0111" when (b <= "0101") else
"1011" after 30 ns, "1100" after 40 ns, "1111" after
"0101" when (b <= "0110") else
50 ns;
"0100" when (b <= "0111") else
end TEST;
"1100" when (b <= "1000") else
"1000";
end BTOG;
Output:
Code Part 2:
A. In Verilog
Test-bench 1. Boolean-Equations
$dumpfile("dump.vcd"); endmodule
$dumpvars(1);
g = 4'b0000;
#10 g = 4'b0010;
#10 g = 4'b0101;
#10 g = 4'b1110;
#10 g = 4'b1010;
#10 g = 4'b1000;
end
endmodule
Output:
$dumpfile("dump.vcd");
$dumpvars(1);
g = 4'b0000;
#10 g = 4'b0010;
#10 g = 4'b0101;
#10 g = 4'b1110;
#10 g = 4'b1010;
#10 g = 4'b1000;
end
initial #60 $finish;
endmodule
Output:
B. In VHDL
Test-bench 1. Boolean-Equations
component gtob );
); begin
end TEST;
Output:
Lab 9
Aim : Task 1:Write HDL Description for 4x1 Multiplexer in Verilog
Behavior Method (a) If-else conditional, (b) Case conditional.
Task 2: Write HDL Description for 4x1 Multiplexer in VHDL using (a)
withselect-when, (b) when-else, (c) Behavior Method, (d) If-else
conditional, (e) Case conditional.
Code:
Test-bench for Verilog
// Test Bench `timescale 1ns/1ps module test; wire Mux_Out; reg
[3:0] d; reg [1:0] s;
mux I1 (Mux_out, d, s); initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
d = 4'b0000; s = 2'b00; #10 d = 4'b1000; s = 2'b00;
#10 d = 4'b1100; s = 2'b01;
#10 d = 4'b1110; s = 2'b10; #10 d = 4'b1111; s = 2'b11; end
initial #50 $finish;
endmodule
Output:
Test-bench VHDL
-- Code your testbench here
library IEEE; use
IEEE.std_logic_1164.all;
entity test is
end test;
Output:
Output:
Code:
Test-bench
// Test Bench `timescale 1ns/1ps module test(); wire q, q_not;
reg s, r;
sr_latch I1 (q, q_not, s, r); initial begin
$dumpfile("dump.vcd");
$dumpvars(1); s = 1'b0; r = 1'b0; #10 s = 1'b0; r = 1'b1;
#10 s = 1'b1; r = 1'b0; #10 s = 1'b1; r = 1'b1;
end
initial #40 $finish;
endmodule
Output:
Test-bench T flip flop in Verilog
// Test Bench for T Flip Flop in Verilog // T flip flop in Verilog Behavior method
`timescale 1ns/1ps `timescale 1ns/1ps
module test(); module T_flip_flop(output reg q, q_not,
wire q, q_not; reg input clk, rstn, t);
clk, rstn, t; always@(posedge clk)
T_flip_flop I1 (q, q_not, clk, rstn, t); begin if (!rstn) q
initial begin <= 0;
$dumpfile("dump.vcd");
$dumpvars(1); else
clk = 0; q <= (t ? ~q:q);
forever #5 clk = ~clk; end
end initial begin rstn assign q_not = ~q;
= 1; t = 0; #10 t = 1; endmodule
#5 rstn = 0; #5 rstn = 1;
#5 t = 1; #5 t = 0;
#5 rstn = 0; #5 rstn = 1;
#5 t = 1; #5 t = 0; #5 t = 1; #5 t = 0;
end
initial #50 $finish;
endmodule
Output:
Test-bench D Flip Flop in VHDL
-- Test Bench library IEEE; -- VHDL, D FF library IEEE;
use IEEE.std_logic_1164.all; use IEEE.std_logic_1164.all;
Output:
Test-bench JK Flip Flop in VHDL
-- Test Bench library IEEE; -- VHDL, JK FF library IEEE;
use IEEE.std_logic_1164.all; use IEEE.std_logic_1164.all;
); begin
end component; process (j, k, clk)
signal q, q_not, j, k, clk: std_logic; begin
if rising_edge (clk) then
begin if j = '1' and k = '1' then
DUT: jk_flip_flop port map (q, q_not, j, q <= not(q); q_not <=
k, clk); clk <= '0', '1' after 5 ns, '0' after not (q_not); elsif j = '1'
10 ns, '1' after 15 ns, '0' after 20 ns, '1' then q <= '1'; q_not <=
after 25 ns, '0' after 30 ns, '1' after 35
'0'; elsif k = '0' then q
ns, '0' after 40 ns;
<= '0'; q_not <= '1'; end
j <= '0', '1' after 20 ns, '0' after 40 ns; k
if; end if; end process;
<= '0', '1' after 10 ns, '0' after 20 ns, '1'
end JK_FF;
after 30 ns, '0' after 40 ns; end TEST;
Output:
Test-bench T Flip Flop in VHDL
-- Test Bench library IEEE; use -- VHDL, T FF library IEEE; use
IEEE.std_logic_1164.all; IEEE.std_logic_1164.all;
end TEST;
Output:
Conclusion: From the experiment we learn about Latch and Flip Flop. Also
Implement them in Verilog/VHDL.
Lab 11
Aim : Task 1: Write HDL description for 4 bit binary up counter using if
statement in Verilog Behavior Method
Task 2: Write HDL description for 3 bit binary up counter using case
statement in VHDL Behavior Method Code:
Test-bench 4 bit binary up counter
// Test Bench // Verilog 4bit up counter
`timescale `timescale 1ns/1ps
1ns/1ps module module counter (output reg [3:0]
test; wire [3:0] count, input clk, rstn);
count; reg clk,
rstn; always @(posedge clk) begin
counter I1 (count, clk, rstn); if (!rstn) begin count <=
4'h0; end else begin
initial begin clk count <= count + 1'b1;
= 0; rstn = 0; end
#5 rstn = 1; end
#70 $finish; endmodule
end
always #2 clk = ~clk;
initial begin
$dumpfile("dump.vcd");
$dumpvars(1);
end
endmodule
Output:
Test-bench 3-bit up counter in VHDL
-- Test Bench library IEEE; -- VHDL, 3 bit up counter
use IEEE.std_logic_1164.all; library IEEE; use
IEEE.std_logic_1164.all;
entity test is
end test; entity counter is
port(
architecture TEST of test is count: out std_logic_vector(2 downto 0);
component counter clk, rstn: in std_logic
port( );
count: out std_logic_vector(2 downto 0); end counter;
clk, rstn: in std_logic
); architecture COUNTER of counter is
end component; signal cnt: std_logic_vector(2 downto
signal count: std_logic_vector(2 downto 0) := "000";
0); signal clk, rstn: std_logic := '0'; begin
constant clk_period : time := 10 process(clk, rstn)
ns; begin