Verilog Code
Verilog Code
1 D/F
Name:
USN:
Batch:                                     Section:
    .                                                                              QMP 7.1 D/F
                                         Approved by:
                                          Dr. Sekar R
                                        HOD, Dept. of ECE
.
                Channabasaveshwara Institute of Technology
                    (Affiliated to VTU, Belagavi & Approved by AICTE, New Delhi)
                                 ( ISO 9001:2015 Certified Institution)
                     NH 206 (B.H. Road), Gubbi, Tumkur – 572 216. Karnataka.
Department Mission
   To nurture the technical/professional/engineering and entrepreneurial skills for overall self and
    societal upliftment through co-curricular and extra-curricular events.
   To orient the Faculty/Student community towards the higher education, research and
    development activities.
• Provide technical solutions to real world problems in the areas of electronics and
 communication by developing suitable systems.
• Pursue engineering career in Industry and/or pursue higher education and research.
• Acquire and follow best professional and ethical practices in Industry and Society.
• Communicate effectively and have the ability to work in team and to lead the team.
At the end of the B.E Electronics & Communication Engineering program, students are
expected to have developed the following program specific outcomes.
PSO1: Build Analog and Digital Electronic systems for Multimedia Applications, VLSI and
       Embedded Systems in Interdisciplinary Research / Development.
PSO2: Design and Develop Communication Systems as per Real Time Applications and Current
       Trends.
              Department of Electronics & Communication Engineering
SYLLABUS
Using suitable simulation software, demonstrate the operation of the following circuits:
Using Xilinx Tool)
   Sl.No                                        Experiments
      1.    To simplify the given Boolean expressions and realize using Verilog program
      2.    To realize Adder/Subtractor (Full/half) circuits using Verilog data flow description.
      3.    To realize 4-bit ALU using Verilog program.
      4.    To realize the following Code converters using Verilog Behavioral description
             a) Gray to binary and vice versa b) Binary to excess3 and vice versa
      5.    To realize using Verilog Behavioral description: 8:1 mux, 8:3 encoder, Priority
            encoder
      6.    To realize using Verilog Behavioral description: 1:8 Demux, 3:8 decoder, 2-bit
            Comparator
      7.    To realize using Verilog Behavioral description:
            Flip-flops: a) JK type b) SR type c) T type and d) D type
      8.    To realize Counters - up/down (BCD and binary) using Verilog Behavioral
            description.
           Demonstration Experiments (For CIE only – not to be included for SEE)
      Use FPGA/CPLD kits for downloading Verilog codes and check the output for interfacing
                                           experiments.
  9.        Verilog Program to interface a Stepper motor to the FPGA/CPLD and rotate the
            motor in the specified direction (by N steps).
  10.       Verilog programs to interface a Relay or ADC to the FPGA/CPLD and demonstrate
            its working.
  11.       Verilog programs to interface DAC to the FPGA/CPLD for Waveform generation.
  12.       Verilog programs to interface Switches and LEDs to the FPGA/CPLD and
            demonstrate its working.
                                                                                    QMP 7.1 D/F
                Channabasaveshwara Institute of Technology
                     (Affiliated to VTU, Belagavi & Approved by AICTE, New Delhi)
                                 (ISO 9001:2015 Certified Institution)
                  NH 206 (B.H. Road), Gubbi, Tumkur – 572 216. Karnataka.
            DEPARTMENT OF ELECTRONICS AND COMMUNICATION
                              TABLE OF CONTENTS
Sl.No.                             Experiment Names                                 Page No
   1   To simplify the given Boolean expressions and realize using Verilog program.  1-2
  2     To realize Adder/Subtractor (Full/half) circuits using Verilog data flow
                                                                                             3-6
        description.
  3     To realize 4-bit ALU using Verilog program.                                          7-8
  4    To realize the following Code converters using Verilog Behavioral description
                                                                                             9 - 14
       a) Gray to binary and vice versa b) Binary to excess3 and vice versa
  5    To realize using Verilog Behavioral description: 8:1 mux, 8:3 encoder, Priority
                                                                                            15 - 20
       encoder.
  6    To realize using Verilog Behavioral description: 1:8 Demux, 3:8 decoder, 2-bit
                                                                                            21 - 26
       Comparator
  7    To realize using Verilog Behavioral description:
                                                                                            27 - 34
       Flip-flops: a) JK type b) SR type c) T type and d) D type
  8    To realize Counters - up/down (BCD and binary) using Verilog Behavioral
       description.                                                                         35 - 38
        Question Bank                                                                         51
        Sample Viva Questions                                                                 52
                                   INDEX PAGE
                                                                            Record Marks
                                                                            Manual Marks
                                                    Date
(Max . 15)
Signature
                                                                                                       Signature
                                                                              (Max. 10)
(Student)
                                                                                                       (Faculty)
      Name of the Experiment
Sl.                                                            Submission
                                     Conduction   Repetition
No                                                              of Record
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
                               Average
                        General Instructions to Students
a.
F = ~ (AB + CD)
b.
F = ~ (AB + CD)
Result:
Experiment No 1: Date: / /
To simplify the given Boolean expressions and realize using Verilog program.
OR
                              SUM = A ^ B ^ Cin
                           Carry_OUT = AB + C(A^B)
Result:
   Experiment No 2:                                  Date:    //
To realize Adder/Subtractor (Full/half) circuits using Verilog data flow
description.
a. Half adder
  module halfadder(
    input A,
    input B,
    output S,
    output C );
  assign S = A ^ B ;
  assign C = A & B;
  endmodule
b. Full adder
Half Subtractor:
Difference = A ^ B
Borrow = ~A & B
Full Subtractor:
                             D = A ^ B ^ Cin
                           Bout = A′B + C(A^B)′
c. Half Subtractor
  module halfsubtractor(
    input a,
    input b,
    output difference,
    output borrow );
  assign difference = a ^ b ;
  assign borrow = ~a & b;
  endmodule
d. Full Subtractor
                                   ALU
               Opcode
                                 Operation
                                             Addition of two
                   000              a+b
                                               numbers
                                             Subtraction of
                   001              a-b
                                              two numbers
                                             Left shift a by
                   010             a << 1
                                               1position
                                              Multiplication
                   011              a*b
                                             of two numbers
                   100            a && b      Logical AND
                   101              a || b    Logical OR
                   110               !a        Negation a
                   111               ~a      Complment a
Result:
Experiment No 3: Date: / /
Binary to Gray:
G3 = B3
G2 = B3 ^ B2
G1 = B2 ^ B1
G0 = B1 ^ Bo
     Experiment No 4:                                  Date:    //
 To realize the following Code converters using Verilog Behavioral
 description.
a) Gray to binary and vice versa      b) Binary to excess3 and vice versa
a. Binary to Gray
 //Dataflow model
 module binary_to_gray(b_in, g_op);
 input [3:0] B;
 output [3:0] G;
 assign G[3] = B[3];
 assign G[2] = B[3] ^ B[2];
 assign G[1] = B[2] ^ B[1];
 assign G[0] = B[1] ^ B[0];
 endmodule
OR
 // Behavioral model
 module bin_gray(b,g);
 input [3:0] b;
 output [3:0] g;
 reg [3:0] g;
 always@(b)
  begin
         g[3]=b[3];
         g[2]=b[3]^b[2];
         g[1]=b[2]^b[1];
         g[0]=b[1]^b[0];
  end
  endmodule
Gray to Binary:
Truth Table:
   b[3]=g[3];
   b[2]=b[3]^g[2];
   b[1]=b[2]^g[1];
   b[0]=b[1]^g[0];
Result:
b. Gray to Binary
   OR
   module gray_bin(g,b); //behavioral model
    input [3:0] g;
    output [3:0] b;
    reg [3:0] b;
   always@(g)
   begin
   b[3]=g[3];
   b[2]=b[3]^g[2];
   b[1]=b[2]^g[1];
   b[0]=b[1]^g[0];
   end
    endmodule
Binary to Excess-3:
  B3   B2      B1   B0   E3   E2   E1   E0
   0   0       0    0    0    0    1    1
   0   0       0    1    0    1    0    0
   0   0       1    0    0    1    0    1
   0   0       1    1    0    1    1    0      E3 = B2(B1 + B0) + B3
   0   1       0    0    0    1    1    1      E2 = B2′(B1 + B0) + B2B1′B0′
   0   1       0    1    1    0    0    0      E1 = B1′B0′+B1B0
   0   1       1    0    1    0    0    1
                                               E0 = B0′
   0   1       1    1    1    0    1    0
   1   0       0    0    1    0    1    1
   1   0       0    1    1    1    0    0
   1   0       1    0    1    1    0    1
   1   0       1    1    1    1    1    0
   1   1       0    0    1    1    1    1
   1   1       0    1    x    x    x    x
   1   1       1    0    x    x    x    x
   1   1       1    1    x    x    x    x
Excess-3 to Binary:
  E3      E2   E1   E0   B3   B2   B1   B0
   0       0    0    0    x    x    x    x   B3 = E3E2 + E3E1E0
   0       0    0    1    x    x    x    x
                                             B2 = E2E1E0 + E2′(E1′+E0′)
   0       0    1    0    x    x    x    x
   0       0    1    1    0    0    0    0   B1 = E1′E0 + E1E0′
   0       1    0    0    0    0    0    1   B0 = E0′
   0       1    0    1    0    0    1    0
   0       1    1    0    0    0    1    1
   0       1    1    1    0    1    0    0
   1       0    0    0    0    1    0    1
   1       0    0    1    0    1    1    0
   1       0    1    0    0    1    1    1
   1       0    1    1    1    0    0    0
   1       1    0    0    1    0    0    1
   1       1    0    1    1    0    1    0
   1       1    1    0    1    0    1    1
   1       1    1    1    1    1    0    0
Result:
e. Binary to Excess-3
   module binary2ex3(b,e);
   input [3:0]b;
   output [3:0]e;
   reg [3:0]e;
   always@(b)
   begin
      e[3] = (b[2]&(b[0] | b[1])) | b[3];
      e2 = (~b[2])&(b[1] | b[0]) | (b2)&(~b1)&(~b0);
      e1 = (~b1)&(~b0) | b1&b0;
      e0 = ~b[0]
    end
   endmodule
f. Excess-3 to Binary
   module binary2ex3(b,e);
   input [3:0]e;
   output [3:0]b;
   reg [3:0]b;
   always@(e)
   begin
      b[3] = (e[3] & e[2]) | (e[3] & e[1] & e[0])
      b2 = (e[2] & e[1] & e[0]) | (~e[2]&(~e[1] | ~e[0])
      b1 = (~e[1] & e[0]) | (e[1] & ~e[0])
      b0 = ~e[0]
    end
  endmodule
a. 8:1 Mux
Truth Table:
S1 S2 S3 Y
0 0 0 I0
0 0 1 I1
0 1 0 I2
0 1 1 I3
1 0 0 I4
1 0 1 I5
1 1 0 I6
1 1 1 I7
Experiment No 5: Date: / /
a. 8:1 mux
module mux8_to_1(i,sel,y);
input [7:0] i;
input [2:0] sel;
output y;
reg y;
         always@(i,sel)
         begin
                 case(sel)
                 3'b000: y=i[0];
                 3'b001: y=i[1];
                 3'b010: y=i[2];
                 3'b011: y=i[3];
                 3'b100: y=i[4];
                 3'b101: y=i[5];
                 3'b110: y=i[6];
                 3'b111: y=i[7];
                 default:y=3'b000;
                 endcase
                 end
                 endmodule
8:3 encoder
Truth Table:
Result:
c. Priority encoder
Truth Table:
Result:
1:8 Demux:
Result:
      reg [7:0]y;
      always @ (i or s0 or s1 or s2)
      case ({s2,s1,s0})
      0 : y0 = D;
      1 : y1 = D;
      2 : y2 = D;
      3 : y3 = D;
      4 : y4 = D;
      5 : y5 = D;
      6 : y6 = D;
      7 : y7 = D;
      default : y = 8’bxxxxxxx;
      endcase
      endmodule
3:8 Decoder:
b. 3:8 Decoder:
   module 3_8_DEC(
     input [3:0]A,
     output [7:0]Y
     );
   reg [7:0]Y;
   always @ (A)
   case (A)
     0 : Y[0] = 1;
     1 : Y[1] = 1;
     2 : Y[2] = 1;
     3 : Y[3] = 1;
     4 : Y[4] = 1;
     5 : Y[5] = 1;
     6 : Y[6] = 1;
     7 : Y[7] = 1;
     default : Y = 8’bxxxxxxxx;
   endcase
   endmodule
    2-bit Comparator:
 Input: 2-bit A and B for comparison
 Output:
     A_greater_B: high if A > B else low
     A_equal_B: high if A = B else low
     A_less_B: high if A<B else low
c. 2-bit Comparator:
      module compr_2(a,b,altb,aeqb,agtb);
      input [1:0] a,b;
      output reg altb,aeqb,agtb;
      always @(a or b)
      begin
       agtb = (A[1] & ~B[1]) | (A[0] & ~B[1] & ~B[0] ) | (A[0] & A[1] & ~B[0])
       altb = (~A[1] & B[1]) | (~A[0] & B[1] & B[0]) + (~A[0] & B[1] & B[0])
       aeqb = ~ (A[ 1] ^ B[1]) & ~(A[0] ^ B[0])
      end
      endmodule
RTL Schematic:
Truth Table:
Result:
   Experiment No 7:                                  Date:    / /
      To realize using Verilog Behavioral description:
      Flip-flops: a) JK type b) SR type c) T type and d) D type
Simulation Result:
RTL Schematic
Truth Table:
               qb = 1;
       end
       else
       begin
               q = d;
               qb = ~d;
       end
end
endmodule
Simulation Result:
RTL Schematic:
Truth Table:
Simulation Result:
RTL Schematic:
Truth Table:
Simulation Result:
Experiment No 8: Date: / /
module upordown_counter(
 Clk,
 reset,
 UpOrDown, //high for UP counter and low for Down counter
 Count
 );
endmodule
RTL Schematic:
Truth table:
        PART B
      Demonstration
       Experiments
      (For CIE only – not to be included for SEE)
Hardware Details:
Result:
      Experiment No 9:                                       Date:   //
          Verilog Code to control direction of Stepper Motor
if (dir==1'b0)
endmodule
Procedure:
1. Make the connection between FRC9 of the FPGA board and stepper motor
   connector of interfacing card
2. Make the connection between FRC1 of the FPGA board and DIP
   switch connector of the GPIOcard-2.
3. Assign appropriate pins to input and output.
4. Connect USB cable and power supply to the FPGA board.
Experiment No 10 :                                           Date:     / /
      Verilog programs to interface a Relay or ADC to the FPGA/CPLD
                                 and demonstrate its working.
Procedure:
      1. Make the connection between FRC9 of the FPGA board to the External light connector
             of the VTU card2.
      2. Make the connection between FRC1 of the FPGA board to the Dip switch connector of the
             VTU card2.
      3. Connect the downloading cable and power supply to the FPGA board.
      4. Then open the Xilinx iMPACT software (refer ISE flow) select the slave serial mode and
         select the respective BIT file and click program.
      5. Make the reset switch on (active low) and analyze the data.
Design:
  4MHz                                       fc                   0 to 255
            Divide by N counter                   8-bit Counter
 Count*1/fc= T
256*1/fc=0.5ms
    fc=512 KHz
    2N =fs/fc
=4 MHz/512 KHz =8
    N=3
    Counter value =DC*Count
0.50*256
128
0 255
fc
               f
                       0         127
always @(counter)
begin
        if(counter<=127)
               dac_out=8'd1;
        else
               dac_out=8'd0;
end
endmodule
Procedure:
1. Make the connection between FRC5 of the FPGA board and DAC
connector of GPIOcard-2
2. Make the connection between FRC1 of the FPGA board and DIP switch
connector of the GPIOcard-2.
Result:
       end
end
endmodule
Result:
end
assign led=led_out;
endmodule
UCF:
NET “led” LOC=P119;
NET “switch1” LOC=P124;
                                       Question Bank
1.    Verilog program to simplify the given Boolean expressions.
2.    Verilog program for the following combinational design to verify the design of 3 to 8
      decoder realization (behavioral model).
3.    Verilog program for the following combinational design to verify the design of 8 to 3
      encoder with priority and without priority (behavioral model).
4.    Verilog program for the following combinational design to verify the design of 8 to 1
      multiplexer using behavioral model.
5.    Verilog program for the following combinational design to verify the design of 4-bit
      binary to gray converter using behavioral model.
6.    Verilog program for the following combinational design to verify the design of 4-bit
      gray to binary converter using behavioral model.
7.    Verilog program for the following combinational design to verify the design of 4-bit
      binary to excess3 converter using behavioral model.
8.    Verilog program for the following combinational design to verify the design of 4-bit
      excess3 to binary converter using behavioral model.
9.    Verilog program for the following combinational design to verify the design of 1 to 8
      demultiplexer using behavioral model.
10.   Verilog program for the following combinational design to verify the design of 3 to 8
      decoder.
11. Verilog program for the following combinational design      to verify the design of 2-bit
      comparator.
12. Verilog   code for a half adder/full adder using data flow description.
13. Verilog   code for a half subtractor/full subtractor using data flow description.
14. Verilog   code for 4-bit ALU which performs different functionalities.
15. Verilog   code for SR/D flip flop using behavioral model.
16. Verilog   code for JK/T flip flop using behavioral model.
17. Verilog   code for up/down BCD and binary counter using behavioral model.
18. Verilog   code to interface a Stepper motor to FPGA and to control the Stepper motor
      rotation in specified direction by N steps.
19. Verilog   programs to interface a Relay or ADC to the FPGA/CPLD,demonstrate its working.
20. Verilog   programs to interface DAC to the FPGA/CPLD for Waveform generation.
21. Verilog   programs to interface Switches and LEDs to the FPGA/CPLD and demonstrate
      its working.
Dept. of ECE, CIT, Gubbi                                                                Page 51
HDL Laboratory (21EC32)                                                           2022-23
VIVA QUESTIONS