Professional Documents
Culture Documents
VISHVAKIRAN R C
2 0 1 9
ALU should use combinational logic to calculate an output based on the four bit op-
code input.
ALU should pass the result to the out bus when enable line in high, and tri-state the out
bus when the enable line is low.
ALU should decode the 4 bit op-code according to the example given below.
OPCODE ALU Operation
1. A+B
2. A-B
3. A Complement
4. A*B
5. A AND B
6. A OR B
7. A NAND B
8. A XOR B
5. Develop the Verilog code for the following flip-flops, SR, D, JK and T.
6. Design a 4 bit binary, BCD counters (Synchronous reset and Asynchronous
reset) and ―any sequence‖ counters, using Verilog code.
Part–B: INTERFACING (at least four of the following must be covered using
VHDL/Verilog)
PART A:
PROGRAMMING (Verilog Using Xilinx Tool)
1. Write Verilog code to realize all the logic gates ............................................................................ 1
Write a Verilog program for the following combinational designs............................................ 3-16
2. 2 to 4 decoder .................................................................................................................................. 3
3. 8 to 3 encoder without priority ........................................................................................................ 5
4. 8 to 3 encoder with priority ............................................................................................................. 7
5. 8 to 1 multiplexer ............................................................................................................................ 9
6. 4 bit binary to gray converter ....................................................................................................... 11
7. De-multiplexer............................................................................................................................... 14
8. Comparator ................................................................................................................................... 16
9. a. Write a Verilog code to describe the functions of a Full Adder Using 3 modeling styles . 18-20
i. Full Adder Data Flow Description ......................................................................................... 18
ii. Full Adder Behavioral Description ........................................................................................ 19
iii. Full Adder Structural Description .......................................................................................... 20
b. Write a VHDL code to describe the functions of a Full Adder Using 3 modeling styles .. 22-27
i. Full Adder Data Flow Description ......................................................................................... 22
ii. Full Adder Behavioral Description ........................................................................................ 23
iii. Full Adder Structural Description .......................................................................................... 24
10. Write a model for 32 bit ALU using the schematic diagram
shown below A (31:0) B (31:0) ................................................................................................... 28
ALU should use combinational logic to calculate an output based on the four-bit
op-code input.
ALU should pass the result to the out bus when enable line in high, and tri-state the out bus
when the enable line is low.
ALU should decode the 4 bit op-code according to the given in example below.
OPCODE ALU OPERATION
0. A+B
1. A–B OPCODE
ALU
2. A OR B
OPERATION
3. A AND B ENABLE
4. A Complement
5. A×B
6. A XOR B
7. A NAND B
Develop the Verilog code for the following flip-flops, SR, D, JK and T......................................... 31-38
11. SR Flip Flop ................................................................................................................................. 31
12. JK Flip Flop .................................................................................................................................. 33
13. D Flip Flop ................................................................................................................................... 35
14. T Flip Flop .................................................................................................................................... 37
i
Design a 4 bit binary, BCD counters (Synchronous reset and Asynchronous reset) and
“any sequence” counters, using Verilog code................................................................................ 39-47
15. Binary Synchronous Reset 4bit Counter ...................................................................................... 39
16. Binary Asynchronous Reset 4bit Counter .................................................................................... 41
17. BCD Synchronous Reset 4bit Counter ......................................................................................... 43
18. BCD Asynchronous Reset 4bit Counter ....................................................................................... 45
19. Binary Any Sequence up down 4bit Counter ............................................................................... 47
PART B:
INTERFACING (at least four of the following must be covered using VHDL/Verilog)
Write HDL code to display messages on the given seven segment display and LCD and accepting Hex
keypad input data ............................................................................................................................ 49-56
1. 7 Segment Display ........................................................................................................................ 49
2. LCD Display ................................................................................................................................. 53
Write HDL code to control speed, direction of DC and Stepper motor ........................................ 57-62
3. Stepper Motor ............................................................................................................................... 57
4. DC Motor ..................................................................................................................................... 60
5. Write HDL code to generate different waveforms (Sine, Square, Triangle,
Ramp etc.,) using DAC change the frequency and amplitude ............................................... 63-72
i. Sine Wave .............................................................................................................................. 63
ii. Square Wave .......................................................................................................................... 67
iii. Triangle Wave ........................................................................................................................ 69
iv. Positive Ramp ........................................................................................................................ 71
v. Negative Ramp ....................................................................................................................... 72
Weblink:
https://drive.google.com/file/d/0B1DE8wa5nPe0Um4zZlhwbnVqNjg/view?usp=sharing
https://www.scribd.com/document/393126020/HDL-Manual-2019-5th-Sem-E-CE-17ECL58
ii
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
1. ALL LOGIC GATES
not_op
a_in and_op
ALL nand_op
inputs LOGIC outputs
or_op
GATES nor_op
b_in xor_op
xnor_op
Figure 1: Block Diagram of All Logic Gates
not_op
a_in
and_op
b_in
nand_op
or_op
nor_op
xor_op
xnor_op
Inputs Outputs
not_op
a_in b_in and_op nand_op or_op nor_op xor_op xnor_op
(a_in)
0 0 1 0 1 0 1 0 1
0 1 1 0 1 1 0 1 0
1 0 0 0 1 1 0 1 0
1 1 0 1 0 1 0 0 1
Truth Table 1: All Logic Gates
Verilog File Name: alg.v
// All Logic Gates
module allgates( a_in, b_in, not_op, and_op, nand_op, or_op,
nor_op, xor_op, xnor_op );
input a_in, b_in;
output not_op, and_op, nand_op, or_op, nor_op, xor_op, xnor_op;
endmodule
initial
begin
ain = 0; bin = 0;
#10 bin = 1;
#10 ain = 1; bin = 0;
#10 bin = 1;
end
endmodule
0 0 0 1 0 1 0 1 0 1
10 0 1 1 0 1 1 0 1 0
20 1 0 0 0 1 1 0 1 0
30 1 1 0 1 0 1 0 0 1
Stopped at time : 40 ns
inputs 4
Decoder 2 to 4 d_op
outputs
en
Inputs Outputs
en d_in(1) d_in(0) d_op(3) d_op(2) d_op(1) d_op(0)
1 X X Z Z Z Z
0 0 0 0 0 0 1
0 0 1 0 0 1 0
0 1 0 0 1 0 0
0 1 1 1 0 0 0
Truth Table 2: Decoder 2 to 4
endmodule
Waveform 2: Decoder 2 to 4
0 1 xx zzzz
10 0 00 0001
20 0 01 0010
30 0 10 0100
40 0 11 1000
50 1 10 zzzz
Stopped at time : 60 ns
Transcript 2: Decoder 2 to 4
inputs Encoder 3
y_op
Without Priority
outputs
en
Inputs Outputs
y_op y_op y_op
en a_in(7) a_in(6) a_in(5) a_in(4) a_in(3) a_in(2) a_in(1) a_in(0)
(2) (1) (0)
1 X X X X X X X X Z Z Z
0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0 0 1
0 0 0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 1 1
0 0 0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1 0 1
0 0 1 0 0 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 0 1 1 1
Truth Table 3: Encoder Without Priority
Verilog File Name: encd_wo_prior.v
// encoder without priority
module encd_wo_prior( en, a_in, y_op );
input en;
input [7:0] a_in;
output [2:0] y_op;
wire en;
wire [7:0] a_in;
reg [2:0] y_op;
inputs Encoder 3
y_op
With Priority
outputs
en
Inputs Outputs
y_op y_op y_op
en a_in(7) a_in(6) a_in(5) a_in(4) a_in(3) a_in(2) a_in(1) a_in(0)
(2) (1) (0)
1 X X X X X X X X Z Z Z
0 1 X X X X X X X 1 1 1
0 0 1 X X X X X X 1 1 0
0 0 0 1 X X X X X 1 0 1
0 0 0 0 1 X X X X 1 0 0
0 0 0 0 0 1 X X X 0 1 1
0 0 0 0 0 0 1 X X 0 1 0
0 0 0 0 0 0 0 1 X 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0
Truth Table 4: Encoder With Priority
Verilog File Name: encd_w_prior.v
// encoder with priority
module encd_w_prior( en, a_in, y_op );
input en;
input [7:0] a_in;
output [2:0] y_op;
wire en;
wire [7:0] a_in;
reg [2:0] y_op;
inputs y_out
Multiplexer 8 to 1
output
en
3
sel
Figure 5: Block Diagram of Multiplexer 8 to 1
Inputs Output
sel sel sel i_in i_in i_in i_in i_in i_in i_in i_in
en y_out
(2) (1) (0) (7) (6) (5) (4) (3) (2) (1) (0)
1 X X X X X X X X X X X Z
0 0 0 0 0 0 0 0 0 0 0 1 1
0 0 0 1 0 0 0 0 0 0 1 0 1
0 0 1 0 0 0 0 0 0 1 0 0 1
0 0 1 1 0 0 0 0 1 0 0 0 1
0 1 0 0 0 0 0 1 0 0 0 0 1
0 1 0 1 0 0 1 0 0 0 0 0 1
0 1 1 0 0 1 0 0 0 0 0 0 1
0 1 1 1 1 0 0 0 0 0 0 0 1
Truth Table 5: Mux 8 to 1
Verilog File Name: mux8to1.v
// Multiplexer 8 to 1
module mux8to1(en,i_in,sel,y_out);
input en;
input [2:0] sel;
input [7:0] i_in;
output y_out;
wire en;
wire [7:0] i_in;
wire [2:0] sel;
reg y_out;
always@(en,sel,i_in)
begin
if(en != 0) // Active Low Enabled
y_out = 1'bZ;
else
begin
case(sel)
3'b000: y_out = i_in[0];
3'b001: y_out = i_in[1];
3'b010: y_out = i_in[2];
3'b011: y_out = i_in[3];
3'b100: y_out = i_in[4];
PART-A: NON-INTERFACING 9 RCVK
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
3'b101: y_out = i_in[5];
3'b110: y_out = i_in[6];
3'b111: y_out = i_in[7];
default: y_out = 1'bZ;
endcase
end
end
endmodule
end
initial #80 $finish;
initial $monitor ($time," %b %b %b %b ",en, i_in, sel, yout);
endmodule
Waveform 5: Mux 8 to 1
0 1 xxxxxxxx xxx z
10 0 xxxxxxxx xxx z
20 0 01000010 001 1
30 0 00000100 010 1
40 0 00001000 011 1
50 0 00010000 100 1
60 0 00100000 101 1
70 0 01000000 001 0
Stopped at time : 80 ns
Transcript 5: Multiplexer 8 to 1
Binary
inputs 4
to g_op
4
b_in Gray
outputs
Converter
Inputs Outputs
Binary Gray
Decimal
b_in(3) b_in(2) b_in(1) b_in(0) g_op(3) g_op(2) g_op(1) g_op(0)
0 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 1
2 0 0 1 0 0 0 1 1
3 0 0 1 1 0 0 1 0
4 0 1 0 0 0 1 1 0
5 0 1 0 1 0 1 1 1
6 0 1 1 0 0 1 0 1
7 0 1 1 1 0 1 0 0
8 1 0 0 0 1 1 0 0
9 1 0 0 1 1 1 0 1
10 1 0 1 0 1 1 1 1
11 1 0 1 1 1 1 1 0
12 1 1 0 0 1 0 1 0
13 1 1 0 1 1 0 1 1
14 1 1 1 0 1 0 0 1
15 1 1 1 1 1 0 0 0
Truth Table 6: Binary to Gray
Verilog File Name: bin_to_gray_4bit.v
// Binary to Gray 4bit Converter
module bin_to_gray_4bit( b_in, g_op );
input [3:0] b_in;
output [3:0] g_op;
wire [3:0] b_in;
reg [3:0] g_op;
always @ (b_in)
begin
0 0000 0000
10 0001 0001
20 0010 0011
30 0011 0010
40 0100 0110
50 0101 0111
60 0110 0101
70 0111 0100
80 1000 1100
90 1001 1101
100 1010 1111
110 1011 1110
120 1100 1010
130 1101 1011
140 1110 1001
150 1111 1000
Stopped at time : 160 ns
Transcript 6: 4Bit Binary to Gray Converter
en
inputs
4 y_out
2 Demultiplexer 1 to 4
sel outputs
a_in
Inputs Outputs
sel sel y_out y_out y_out y_out
en a_in
(1) (0) (3) (2) (1) (0)
1 X X X Z Z Z Z
0 0 0 1 0 0 0 1
0 0 1 1 0 0 1 0
0 1 0 1 0 1 0 0
0 1 1 1 1 0 0 0
Truth Table 7: Demux 1 to 4
Verilog File Name: demux1to4.v
// Demultiplexer 1 to 4
module demux1to4(en,sel,a_in,y_out);
input en;
input [1:0] sel;
input a_in;
output [3:0] y_out;
wire en;
wire [1:0] sel;
wire a_in;
reg [3:0] y_out;
always@(en,sel,a_in)
begin
if(en != 0) // Active Low Enabled
y_out = 4'bZZZZ;
else
begin
y_out = 4'b0000;
case(sel)
2'b00 : y_out[0] = a_in;
2'b01 : y_out[1] = a_in;
2'b10 : y_out[2] = a_in;
2'b11 : y_out[3] = a_in;
default : y_out = 4'bZZZZ;
endcase
end
end
endmodule
Waveform 7: Demultiplexer 1 to 4
0 1 x xx zzzz
10 0 1 00 0001
20 0 1 10 0100
30 0 1 01 0010
40 0 x 01 00x0
50 0 1 11 1000
Stopped at time : 60 ns
Transcript 7: Demultiplexer 1 to 4
4 g_op
a_in
outputs
Comparator 4bit L_op
inputs
4 e_op
b_in
Outputs
Inputs
a_in > b_in a_in = b_in a_in < b_in
a_in b_in g_op e_op L_op
---- ---- Z Z Z
1100 0011 1 0 0
0110 0110 0 1 0
1000 1110 0 0 1
Truth Table 8: Comparator 4Bits
Verilog File Name: comparator4bit.v
// Comparator 4bit
module comparator4bit( a_in,b_in,g_op,L_op,e_op);
input [3:0] a_in,b_in;
output g_op,L_op,e_op;
wire [3:0] a_in,b_in;
reg g_op,L_op,e_op;
always@(a_in,b_in)
if( a_in > b_in)
begin
g_op = 1; L_op = 0; e_op = 0;
end
else if( a_in < b_in)
begin
g_op = 0; L_op = 1; e_op = 0;
end
else if( a_in == b_in)
begin
g_op = 0; L_op = 0; e_op = 1;
end
else
begin
g_op = 1'bZ; L_op = 1'bZ; e_op = 1'bZ;
end
endmodule
module comp4bit_tb;
reg [3:0] ain,bin;
wire grt,Less,equ;
initial
begin
ain = 4'b1100; bin = 4'b0011;
#10 ain = 4'b0110; bin = 4'b0110;
#10 ain = 4'b1000; bin = 4'b1110;
#10 ain = 4'b0000; bin = 4'b0001;
end
initial #40 $finish;
initial $monitor ($time," %b %b %b %b %b",ain,
bin,grt,Less,equ);
endmodule
a_in sum
b_in Full Adder outputs
inputs
c_in carry
a_in S1
b_in x1 sum
x2
a2
S3
carry
o1
a1
S2
c_in
Inputs Outputs
a_in b_in c_in sum carry
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
Truth Table 9a: Full Adder
Full Adder Data Flow Description
end
initial #9 $finish;
initial $monitor ($time," %b%b%b --> %b,%b", ain, bin, cin, sum,
carry);
endmodule
end
initial #9 $finish;
initial $monitor ($time," %b%b%b --> %b,%b", ain, bin, cin, sum,
carry);
endmodule
endmodule
end
initial #9 $finish;
initial $monitor ($time," %b%b%b --> %b,%b", ain, bin, cin, sum,
carry);
endmodule
ain sum
bin Full Adder outputs
inputs
cin carry
ain S1
bin x1 sum
x2
a2
S3
carry
o1
a1
S2
cin
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_test is
end full_adder_test;
component full_adder
port(
ain, bin, cin : in std_logic;
sum, carry : out std_logic);
end component;
--inputs
signal ain, bin, cin : std_logic := '0';
--outputs
signal sum : std_logic;
signal carry : std_logic;
begin
-- stimulus process
stim_proc: process
begin
-- hold reset state for 100 ns.
wait for 10 ns;
ain <= '0'; bin <= '0'; cin <= '1'; wait for 10ns;
ain <= '0'; bin <= '1'; cin <= '0'; wait for 10ns;
ain <= '0'; bin <= '1'; cin <= '1'; wait for 10ns;
ain <= '1'; bin <= '0'; cin <= '0'; wait for 10ns;
ain <= '1'; bin <= '0'; cin <= '1'; wait for 10ns;
ain <= '1'; bin <= '1'; cin <= '0'; wait for 10ns;
ain <= '1'; bin <= '1'; cin <= '1'; wait for 10ns;
end process;
end;
entity full_adder is
port ( ain, bin, cin : in std_logic;
sum, carry : out std_logic);
end full_adder;
begin
process( ain, bin, cin)
begin
if (ain = '0' and bin = '0' and cin = '0') then sum<='0'; carry <='0';
elsif (ain = '0' and bin = '0' and cin = '1') then sum<='1'; carry <='0';
elsif (ain = '0' and bin = '1' and cin = '0') then sum<='1'; carry <='0';
elsif (ain = '0' and bin = '1' and cin = '1') then sum<='0'; carry <='1';
elsif (ain = '1' and bin = '0' and cin = '0') then sum<='1'; carry <='0';
elsif (ain = '1' and bin = '0' and cin = '1') then sum<='0'; carry <='1';
elsif (ain = '1' and bin = '1' and cin = '0') then sum<='0'; carry <='1';
else sum<='1'; carry <='1';
end if;
end process;
end behavioral;
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_test is
end full_adder_test;
entity full_adder is
port ( ain, bin, cin : in std_logic;
sum, carry : out std_logic);
end full_adder;
signal c1,c2,c3:std_logic;
component xor_3
port(x,y,z : in std_logic;
u : out std_logic);
end component;
component and_2
port(l,m : in std_logic;
n : out std_logic);
end component;
component or_3
port(p,q,r : in std_logic;
s : out std_logic);
end component;
begin
x1: xor_3 port map ( ain, bin, cin, sum);
a1: and_2 port map ( ain, bin, c1);
a2: and_2 port map ( bin, cin, c2);
a3: and_2 port map ( ain, cin, c3);
o1: or_3 port map (c1, c2, c3, carry);
end structural;
entity xor_3 is
port ( x,y,z : in std_logic;
u : out std_logic);
end xor_3;
entity or_3 is
port ( p,q,r : in std_logic;
s : out std_logic);
end or_3;
entity and_2 is
port ( l,m : in std_logic;
n : out std_logic);
end and_2;
library ieee;
use ieee.std_logic_1164.all;
entity full_adder_test is
end full_adder_test;
32
a_in
ALU 32bits 32
inputs zout
outputs
32
b_in
3
opc
Figure 10: Block Diagram of ALU 32bits
Inputs Outputs Actio
opc a_in b_in zout ns
No
XX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
Chan
X XXXXXXXXXXXX XXXXXXXXXXX ZZ
ge
a_in +
000 000000000000000000010111 000000000000000000101101 00000000000000000000000001000100
b_in
a_in -
001 000000000000000000010111 000000000000000000101101 11111111111111111111111111101010
b_in
a_in
010 000000000000000000010111 000000000000000000101101 00000000000000000000000000111111 or
b_in
a_in
011 000000000000000000010111 000000000000000000101101 00000000000000000000000000000101 and
b_in
not
100 000000000000000000010111 000000000000000000101101 11111111111111111111111111101000
a_in
a_in *
101 000000000000000000010111 000000000000000000101101 00000000000000000000010000001011
b_in
a_in
110 000000000000000000010111 000000000000000000101101 00000000000000000000000000111010 xor
b_in
a_in
111 000000000000000000010111 000000000000000000101101 11111111111111111111111111111010 nand
b_in
Truth Table 10: ALU 32bits
Verilog File Name: alu32bit.v
// ALU 32bit
`timescale 1ns / 1ps
module alu32bit(a_in,b_in,opcode,zout);
input [23:0] a_in,b_in;
input [2:0] opcode;
output [31:0] zout;
reg [31:0] zout;
wire [31:0] a, b;
assign a = {8'b0,a_in};
assign b = {8'b0,b_in};
always@ *
begin
case (opcode)
3'b000 : zout = a+b;
3'b001 : zout = a-b;
PART-A: NON-INTERFACING 28 RCVK
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
3'b010 : zout = a | b;
3'b011 : zout = a & b;
3'b100 : zout = ~ a;
3'b101 : zout = a*b;
3'b110 : zout = a ^ b;
3'b111 : zout = ~( a & b );
default : zout = 32'd0;
endcase
end
endmodule
Verilog Testbench File Name: alu32bit_test.v
module alu32bit_test;
// Inputs
reg [23:0] a_in;
reg [23:0] b_in;
reg [2:0] opcode;
// Outputs
wire [31:0] zout;
initial begin
// Initialize Inputs
a_in = 23; b_in = 45;
opcode = 0;
#1 opcode = 1;
#1 opcode = 2;
#1 opcode = 3;
#1 opcode = 4;
#1 opcode = 5;
#1 opcode = 6;
#1 opcode = 7;
#1 a_in = 53; b_in = 56;
#1 opcode = 0;
#1 opcode = 1;
#1 opcode = 2;
#1 opcode = 3;
#1 opcode = 4;
#1 opcode = 5;
#1 opcode = 6;
#1 opcode = 7;
end
endmodule
clk
Figure 11: Block Diagram of SR Flip Flop
Inputs Outputs
rst clk s r q qb Action
1 ↑ X X q qb No Change
0 ↑ 0 0 q qb No Change
0 ↑ 0 1 0 1 Reset
0 ↑ 1 0 1 0 Set
0 ↑ 1 1 - - Illegal
Truth Table 11: S R Flip Flop
Verilog File Name: sr_ff.v
//Async SR Flip Flop
module sr_ff( sr , clk , reset , q ,qb );
input [1:0] sr;
input clk, reset ;
output q,qb;
reg q,qb;
initial
begin
clk = 0; rst=1; sr=2'b00;
end
always
#5 clk=~clk;
initial
begin
#10; rst=0;
#10 sr = 2'b10;
#10 sr = 2'b00;
#10 sr = 2'b01;
#10 sr = 2'b11;
#20 sr = 2'b10;
#10 sr = 2'b00;
end
initial #90 $finish;
initial $monitor ($time,” %b %b %b %b%b ”,clk,rst,sr,q,qb);
endmodule
0 0 1 00 01
5 1 1 00 01
10 0 0 00 01
15 1 0 00 01
20 0 0 10 01
25 1 0 10 10
30 0 0 00 10
35 1 0 00 10
40 0 0 01 10
45 1 0 01 01
50 0 0 11 01
55 1 0 11 xx
60 0 0 11 xx
65 1 0 11 xx
70 0 0 10 xx
75 1 0 10 10
80 0 0 00 10
85 1 0 00 10
Stopped at time : 90 ns
Transcript 11: S R Flip Flop
clk
Figure 12: Block Diagram of JK Flip Flop
Inputs Outputs
rst clk j k q qb Action
1 ↑ X X q qb No Change
0 ↑ 0 0 q qb No Change
0 ↑ 0 1 0 1 Reset
0 ↑ 1 0 1 0 Set
0 ↑ 1 1 q' q' Toggle
Truth Table 12: J K Flip Flop
Verilog File Name: jk_ff.v
//Async JK Flip Flop
module jk_ff( jk , clk , reset , q ,qb );
input [1:0] jk;
input clk, reset ;
output q,qb;
reg q,qb;
end
initial #110 $finish;
initial $monitor ($time,” %b %b %b %b%b ”,clk,rst,jk,q,qb);
endmodule
d q
clk
Figure 13: Block Diagram of D Flip Flop
Inputs Outputs
rst clk d q qb Action
1 ↑ X q qb No Change
0 ↑ 0 0 1 Reset
0 ↑ 1 1 0 Set
Truth Table 13: D Flip Flop
end
initial #80 $finish;
initial $monitor ($time,” %b %b %b %b%b ”,clk,rst,d,q,qb);
endmodule
0 0 1 x 01
5 1 1 x 01
10 0 0 x 01
15 1 0 x xx
20 0 0 0 xx
25 1 0 0 01
30 0 0 1 01
35 1 0 1 10
40 0 0 1 10
45 1 0 1 10
50 0 0 0 10
55 1 0 0 01
60 0 0 1 01
65 1 0 1 10
70 0 0 1 10
75 1 0 1 10
Stopped at time : 80 ns
Transcript 13: D Flip Flop
t q
clk
Figure 14: Block Diagram of T Flip Flop
Inputs Outputs
rst clk t q qb Action
1 ↑ X q qb No Change
0 ↑ 0 q qb No Change
0 ↑ 1 q' q' Toggle
Truth Table 14: T Flip Flop
Verilog File Name: t_ff.v
//Async T Flip Flop
module t_ff( t, clk, reset, q, qb );
input t, clk, reset ;
output q,qb;
reg q,qb;
initial
begin
#10; rst=0;
#10 t = 0;
#10 t = 1;
#20
#10 t = 0;
#10 t = 1;
end
initial #90 $finish;
initial $monitor ($time,” %b %b %b %b%b ”,clk,rst,t,q,qb);
endmodule
Binary Synchronous 4
rst bin_out
Reset 4bit Counter outputs
inputs
clk
Figure 15: Block Diagram of Binary Synchronous Reset 4bit
Counter
initial
begin
clk = 0; rst=1;
end
always
#5 clk=~clk;
initial
begin
#5; rst=0;
#55;
#10; rst=1;
#5; rst=0;
#30;
#2; rst=1;
#10;
end
initial #120 $finish;
initial $monitor ($time,” %b %b %b ”,clk,rst,count);
endmodule
0 0 1 0000
5 1 0 0001
10 0 0 0001
15 1 0 0010
20 0 0 0010
25 1 0 0011
30 0 0 0011
35 1 0 0100
40 0 0 0100
45 1 0 0101
50 0 0 0101
55 1 0 0110
60 0 0 0110
65 1 0 0111
70 0 1 0111
75 1 0 1000
80 0 0 1000
85 1 0 1001
90 0 0 1001
95 1 0 1010
100 0 0 1010
105 1 0 1011
107 1 1 1011
110 0 1 1011
115 1 1 0000
Stopped at time : 120 ns
clk
Figure 16: Block Diagram of Binary Asynchronous Reset 4bit Counter
initial
begin
clk = 0; rst=1;
end
always
#5 clk=~clk;
initial
begin
#5; rst=0;
#55;
#10; rst=1;
#5; rst=0;
#30;
#2; rst=1;
#10;
end
initial #120 $finish;
initial $monitor ($time,” %b %b %b ”,clk,rst,count);
endmodule
0 0 1 0000
5 1 0 0001
10 0 0 0001
15 1 0 0010
20 0 0 0010
25 1 0 0011
30 0 0 0011
35 1 0 0100
40 0 0 0100
45 1 0 0101
50 0 0 0101
55 1 0 0110
60 0 0 0110
65 1 0 0111
70 0 1 0000
75 1 0 0001
80 0 0 0001
85 1 0 0010
90 0 0 0010
95 1 0 0011
100 0 0 0011
105 1 0 0100
107 1 1 0000
110 0 1 0000
115 1 1 0000
Stopped at time : 120 ns
Transcript 16: Binary Asynchronous Reset 4bit Counter
clk
Figure 17: Block Diagram of BCD Synchronous Reset 4bit Counter
initial
begin
clk = 0; rst=1;
end
always
#5 clk=~clk;
initial
begin
#5; rst=0;
#105;
#10; rst=1;
#15; rst=0;
#30;
endmodule
0 0 1 0000
5 1 0 0001
10 0 0 0001
15 1 0 0010
20 0 0 0010
25 1 0 0011
30 0 0 0011
35 1 0 0100
40 0 0 0100
45 1 0 0101
50 0 0 0101
55 1 0 0110
60 0 0 0110
65 1 0 0111
70 0 0 0111
75 1 0 1000
80 0 0 1000
85 1 0 1001
90 0 0 1001
95 1 0 0000
100 0 0 0000
105 1 0 0001
110 0 0 0001
115 1 0 0010
120 0 1 0010
125 1 1 0000
130 0 1 0000
135 1 0 0001
140 0 0 0001
145 1 0 0010
150 0 0 0010
155 1 0 0011
160 0 0 0011
165 1 0 0100
167 1 1 0100
170 0 1 0100
175 1 1 0000
Stopped at time : 180 ns
Transcript 17: BCD Synchronous Reset 4bit Counter
clk
Figure 18: Block Diagram of BCD Asynchronous Reset 4bit Counter
initial
begin
clk = 0; rst=1;
end
always
#5 clk=~clk;
initial
begin
#5; rst=0;
#105;
#10; rst=1;
#15; rst=0;
#30;
#2; rst=1;
#10;
end
initial #180 $finish;
initial $monitor ($time,” %b %b %b ”,clk,rst,count);
endmodule
0 0 1 0000
5 1 0 0001
10 0 0 0001
15 1 0 0010
20 0 0 0010
25 1 0 0011
30 0 0 0011
35 1 0 0100
40 0 0 0100
45 1 0 0101
50 0 0 0101
55 1 0 0110
60 0 0 0110
65 1 0 0111
70 0 0 0111
75 1 0 1000
80 0 0 1000
85 1 0 1001
90 0 0 1001
95 1 0 0000
100 0 0 0000
105 1 0 0001
110 0 0 0001
115 1 0 0010
120 0 1 0000
125 1 1 0000
130 0 1 0000
135 1 0 0001
140 0 0 0001
145 1 0 0010
150 0 0 0010
155 1 0 0011
160 0 0 0011
165 1 0 0100
167 1 1 0000
170 0 1 0000
175 1 1 0000
Stopped at time : 180 ns
Transcript 18: BCD Asynchronous Reset 4bit Counter
clk
Figure 19: Block Diagram of Binary Any Sequence 4bit Counter
initial
begin
clk = 0; rst=1; Load =0; updn =0;
end
always
#5 clk=~clk;
initial
begin
#5; rst=0; Load =1; din = 5;
#5; Load =0;
#55;
#10; updn =1;
#15; rst=1; din= 8;
#30;
PART-A: NON-INTERFACING 47 RCVK
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
#2; rst=0;
#55;
#10; updn =0;
#10;
end
initial #220 $finish;
initial $display(“\t\tclk\trst\t|Load\tupdown\t|din - - > count|”);
initial $monitor ($time,” %b %b |%b %b|%b - - > %b| ”,clk, rst,
Load, updn, din, count);
endmodule
PC JT AG (Comman Cathode)
Power CPLD 0 1 2 3
J7 BUS/STRIP CABLE
J1
Supply XC9572
4 5 6 7
J6 BUS/STRIP CABLE
J2 8 9 A B
C D E F
Keypad
CPLD Board Mega KBDSP1
Figure 1: Interface Diagram of CPLD Board and MEGA KBDSP1
4
4 key_sc
key_rl outputs
7 Segment Display
inputs to accept Hex 6
digit
clk Keypad Input Data 7
seg_out
Figure 2: Block diagram of 7Segment Display to accept Hex Keypad Input Data
always@(posedge clk_div[12])
begin
key_sc_temp = {key_sc_temp[2:0],key_sc_temp[3]};
key_sc = key_sc_temp;
end
#keypad_led_display.ucf
NET "clk" LOC = "p9" ;
NET "digit<0>" LOC = "p68" ;
NET "digit<1>" LOC = "p67" ;
NET "digit<2>" LOC = "p66" ;
NET "digit<3>" LOC = "p65" ;
NET "digit<4>" LOC = "p63" ;
NET "digit<5>" LOC = "p62" ;
NET "key_rl<0>" LOC = "p1" ;
NET "key_rl<1>" LOC = "p84" ;
NET "key_rl<2>" LOC = "p83" ;
NET "key_rl<3>" LOC = "p82" ;
NET "key_sc<0>" LOC = "p5" ;
NET "key_sc<1>" LOC = "p4" ;
NET "key_sc<2>" LOC = "p3" ;
NET "key_sc<3>" LOC = "p2" ;
NET "seg_out<0>" LOC = "p81" ;
NET "seg_out<1>" LOC = "p80" ;
NET "seg_out<2>" LOC = "p79" ;
NET "seg_out<3>" LOC = "p75" ;
NET "seg_out<4>" LOC = "p72" ;
NET "seg_out<5>" LOC = "p71" ;
NET "seg_out<6>" LOC = "p70" ;
ELECTRONICS!
PC JTAG
Power CPLD 0 1 2 3
J6 BUS/STRIP CABLE
J2
Supply XC9572
4 5 6 7
8 9 A B
C D E F
Keypad
CPLD Board Mega KBDSP1
Figure 3: Interface Diagram of CPLD Board and MEGA KBDSP1
8
data
input outputs
reg_sel
clk LCD
rd-wr
en
Direction
Control
To
J4 Stepper
Motor
PC JTAG
Power CPLD
J6 BUS/STRIP CABLE
J3 KEY1 KEY2 KEY3 KEY4
Supply XC9572
RELAY1 RELAY2
KEY
1
C_A
Direction 4
Control inputs STEPPER MOTOR d_out
clk_4M outputs
initial
begin
a = 0;
step_d [0]=4'b0110;
step_d [1]=4'b1010;
step_d [2]=4'b1001;
step_d [3]=4'b0101;
end
always@(posedge clk_div[16])
begin
if (C_A)
begin
a=(a==4)?0:(a+1);
end
else
begin
a=(a==0)?3:(a-1);
end
d_out = step_d[a];
PART-B: INTERFACING 58 RCVK
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
end
endmodule
PC JTAG
Power CPLD
J6 BUS/STRIP CABLE
J3 KEY1 KEY2 KEY3 KEY4
Supply XC9572
RELAY1 RELAY2
To
J5 DC
Motor
4
spd_cnt cl
inputs DC MOTOR outputs
clk_4M
acl
always@(posedge clk_div[19])
begin
case(spd_cnt)
4'b1000 : begin cl = (clk_div[18] && clk_div[15]); acl = 0; end
4'b0100 : begin cl = (clk_div[18] && clk_div[16]); acl = 0; end
4'b0010 : begin cl = (clk_div[19] && clk_div[18]); acl = 0; end
4'b0001 : begin cl = 1; acl = 0; end
4'b0000 : begin cl = 0; acl = 1; end
default : ;
endcase
end
endmodule
PC JTAG
Power CPLD
J6 BUS/STRIP CABLE
J2 RELAY3 RELAY1
Supply XC9572
RELAY4 RELAY2
rst
inputs
Signal Generator 8
dac_out
using DAC
clk outputs
always@(posedge (clk))
begin
if(rst)
dac_out = 8'b0;
else
begin
dac_out = sine[i];
i = i + 1;
if (i == 31)
i = 4;
end
end
endmodule
always@(posedge clk)
begin
if(rst) 0
count <= 8'b0; t
else
count <= count +1'b1;
if (count[7]== 1'b1)
dac_out <= 8'b00000000;
else
dac_out <= 8'b11111111;
end
endmodule
reg downup;
always@(posedge clk) 0 t
begin
if(rst)
begin
downup <= 1'b0;
dac_out <= 8'b0;
end
else
begin
if (dac_out == 8'b0)
begin
downup <= 1'b0;
dac_out <= 8'b00000001;
end
else if (dac_out == 8'b11111111)
begin
downup <= 1'b1;
dac_out <= 8'b11111110;
end
else if (downup)
dac_out <= dac_out - 1;
else
dac_out <= dac_out +1;
PART-B: INTERFACING 69 RCVK
5TH SEM, HDL Lab, 17ECL58 2019-20 DEPT. E&C, CEC, Bengaluru
end
end
endmodule
always@(posedge clk)
begin
if(rst) 0 t
dac_out <= 8'b0;
else
dac_out <= dac_out - 1;
end
endmodule