Showing posts with label vhdl. Show all posts
Showing posts with label vhdl. Show all posts

VHDL Programming tutorial on Priority encoder

| 0 comments |
Priority Encoder is an important class of Encoder. It is different from a ordinary encoder in that it gives priority to the input signal. The signal corresponding to the higher priority is outputted by the Priority encoder.

Here the design of 8 to 3 priority encoder is taken as an example for VHDL programming tutorial.

The truth table of a 8 to 3priority encoder is shown below:

8 to 3 priority encoder

In the truth table, the input data is x7x6x5x4x3x2x1x0 which is 8 bit input. And the 3 bit output is y2y1y0. The z signal indicates a condition that all input bits are 0 which is also a possible combination.

The priority is the signal with input 1XXXXXXX where X indicates dont care which means it does not matter whether it is 0 or 1. The next priority goes to 01XXXXXX and so on up on the table.

Suppose if 10100110 is the input data then the output is 111.

Below is the VHDL code for this 8 to 3 priority encoder:

library ieee;
use ieee.std_logic_1164.all;

entity prio_encoder is
    port(
    x : in std_logic_vector(7 downto 0);
    y : out std_logic_vector(2 downto 0);
    z : out std_logic
    );
end prio_encoder;

architecture model of prio_encoder is
begin
   
    process(x)
    begin
        if (x(7) = 1) then y <= "111";
        elsif (x(6) = 1) then y <= "110";
        elsif (x(5) = 1) then y <= "101";
        elsif (x(4) = 1) then y <= "100";
        elsif (x(3) = 1) then y <= "011";
        elsif (x(2) = 1) then y <= "010";
        elsif (x(1) = 1) then y <= "001";
        else y <= "000";
        end if;
       
        end process;
       
        z <= 0 when x = "00000000" else 1;           

end model;

In the above code, we used if elsif then statement of VHDL to implement the priority encoder. Since the if statement is a sequential state it is written inside the Process statement with sensitivity signal as x. The first statement is checked first and then next statement inside a process since the statements are sequential. If the first statement is true then the output is send to the output and the process exits. If not then the second statement is checked and if it evaluates true then it corresponding output is outputted and so on. So this code implements priority.The last else statement is needed so that the synthesizer does not create a latch. That is all combination of the input must be included. Finally the z output when all inputs are 0s is implemented outside the process statement.

The simulated waveform is shown below:

8 to 3 priority encoder simulated waveform

Encoders are important part of a digital system. There are other application of encoders such as code conversion encoders and includes designs that needs to be encoded to reduce output signals.
Read More..

VHDL tutorial on Shift Register Design using D Flip Flop as component

| 0 comments |

This VHDL tutorial shows how D flip flop symbols can be used to implement a block diagram of a shift register and stimulate the design. The shift register has 4 flip flops. So first we need a D flip flop vhdl design then we can use it as component for the top level block diagram.

The VHDL code for the D flip flop is as follows,

library ieee;
use ieee.std_logic_1164.all;

entity DFF is
    port(
    clk: in STD_LOGIC;
    rst: in STD_LOGIC;
    D: in STD_LOGIC;
    Q: out STD_LOGIC
    );
    end DFF;

    architecture DFF_arch of DFF is
    begin
process (CLK)
begin
    if CLKevent and CLK=1 then 
        if rst=1 then   
            Q <= 0;
        else
            Q <= D;
        end if;
    end if;
end process;
    end DFF_arch;

Next we use create the shift register as a block diagram and place the D flip flop symbol that was created earlier into the block diagram. First we need create a new block diagram in the VHDL Software then we need to add its port which are Din, clk and rst as std_logic inputs and Dout which is the std_logic output.

After defining the block diagram we insert four D flip flip into the block diagram and connect them as shown in the following figure.

Shift register with D flip flop


The VHDL code for the same block diagram is as follows,

library IEEE;
use IEEE.std_logic_1164.all;

entity shift_reg_block is
  port(
       Din : in STD_LOGIC;
       clk : in STD_LOGIC;
       rst : in STD_LOGIC;
       Dout : out STD_LOGIC
  );
end shift_reg_block;

architecture shift_reg_block of shift_reg_block is

component DFF
  port (
       D : in STD_LOGIC;
       clk : in STD_LOGIC;
       rst : in STD_LOGIC;
       Q : out STD_LOGIC
  );
end component;


signal Q1, Q2, Q3 : STD_LOGIC;

begin

DFF1 : DFF
  port map(
       D => Din,
       Q => Q1,
       clk => clk,
       rst => rst
  );

DFF2 : DFF
  port map(
       D => Q1,
       Q => Q2,
       clk => clk,
       rst => rst
  );

DFF3 : DFF
  port map(
       D => Q2,
       Q => Q3,
       clk => clk,
       rst => rst
  );

DFF4 : DFF
  port map(
       D => Q3,
       Q => Dout,
       clk => clk,
       rst => rst
  );

end shift_reg_block;

Now in order to stimulate this design we need to create a testbench. Following is the testbench for this shift register.

library ieee;
use ieee.std_logic_1164.all;

entity shift_reg_block_tb is
end shift_reg_block_tb;

architecture TB_ARCHITECTURE of shift_reg_block_tb is

    component shift_reg_block
    port(
        Din : in STD_LOGIC;
        clk : in STD_LOGIC;
        rst : in STD_LOGIC;
        Dout : out STD_LOGIC );
    end component;

    signal Din : STD_LOGIC;
    signal clk : STD_LOGIC;
    signal rst : STD_LOGIC;

    signal Dout : STD_LOGIC;

begin

    UUT : shift_reg_block
        port map (
            Din => Din,
            clk => clk,
            rst => rst,
            Dout => Dout
        );

    clk_pro : process
    begin
        clk <= 0;
        wait for 5 ns;
       
        clk <= 1;
        wait for 5 ns;
       
    end process;
  
    sti_pro : process
    begin
        Din <= 0;
        wait for 10 ns;
       
        Din <= 1;
        wait for 10 ns;
       
        Din <= 1;
        wait for 10 ns;
       
        Din <= 0;
        wait for 10 ns;
       
        Din <= 0;
        wait for 10 ns;
       
        Din <= 1;
        wait for 10 ns;
       
        Din <= 1;  
        wait for 10 ns;
       
        Din <= 0;
        wait for 10 ns;

       
    end process;

end TB_ARCHITECTURE;

configuration TESTBENCH_FOR_shift_reg_block of shift_reg_block_tb is
    for TB_ARCHITECTURE
        for UUT : shift_reg_block
            use entity work.shift_reg_block(shift_reg_block);
        end for;
    end for;
end TESTBENCH_FOR_shift_reg_block;

The simulation waveform is shown below,

simulation of shift register



Read More..

Decoder implementation using the Shift operators in VHDL

| 0 comments |
One of the different method of implementing a decoder in VHDL is to use the shift operators of vhdl language. Using these operator can shorten the code and can be extended easily to higher number of input/output bits. Here it is shown how a 3x8 decoder can be implemented using vhdl shift operator and verified and simulated using the active-hdl VHDL software.

Before going to the implementation readers may want to read other ways of implementing decoder. For this follow the following links-
  • 74LS138 Decoder design using logical gates
  • 74LS138 Decoder design using With Select statement 
  • 74LS138 3x8 decoder design with case construct in vhdl
  • 74LS138 3x8 decoder design using if else construct
In this vhdl tutorial we gonna see how the shift operator in VHDL can be used to implement a decoder. The idea of using shift operator comes from the fact that there is bit shift in the output of the decoder according the input. If we let x be 4 bit input and y be the 8 bit output then one can observe the output y being in the form- 10000000, 01000000, 00100000, 00010000 ...00000001, which shows that 1 is shifted to the right one bit according to the input x. Now x is 4 bit binary but when it is converted to integer then x as integer indicates the number of shift required.

The following VHDL code illustrates how a decoder could be realized using shift operator in vhdl:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity decoder_shift is
    generic (n : positive := 4);
    port(
    x : in std_logic_vector(n-1 downto 0);
    y : out std_logic_vector(2**n-1 downto 0)
    );
end decoder_shift;

architecture decoder_shift_arch of decoder_shift is
constant yout : bit_vector(2**n-1 downto 0) := (0=>1,others => 0);
begin
    y <= to_stdlogicvector(yout sll to_integer(unsigned(x)));
   
end decoder_shift_arch;


In the above as x input is a std_logic_vector it is first unsigned and then to integer. The shifting operater ssl is used to shift the intermediate signal yout. Since shifting operator ssl acts only on bit_vector, intermediate signal yout is defined as bit_vector. After the shifting the bit_vector is converted back to std_logic_vector using the function to_stdlogic_vector and assigned to the original output y.

The advantage of using shift operator for modelling the decoder is that it requires less code lines to implement the decoder. Another advantage is that it can be easily adopted to higher number of input/output bits by changing the generic bit number n declaration.

The code was simulated with active-hdl vhdl software and the waveform of the simulation is shown below.

decoder simulation in vhdl software

Read More..

Code Converter VHDL

| 0 comments |
In digital system, many types of code exist that are used for different application. Binary Codes, Excess-3 codes and Gray codes are 3 different codes that are used in digital system. Binary codes are also called BCD(Binary Coded Decimal) and are 4 bit codes that represents 16 different decimal values. Excess-3 codes are codes obtained by adding 3 equivalent binary codes to each of the BCD binary codes starting from 0(0000). Gray codes are another type of codes which have the property that there is only one bit change in going from one code to the next.

 The following table shows the BCD, Excess-3 and the Gray codes and their relation:



A code converter is often required to convert from one code to another.

In VHDL programming we can use the concurrent statement select to convert the binary to excess-3 as follows:

library ieee;
use ieee.std_logic_1164.all;

entity bi2ex3 is
    port(x : in std_logic_vector(3 downto 0);
    y : out std_logic_vector(3 downto 0)
    );
end bi2ex3;

architecture model of bi2ex3 is
begin
    with x select
    y <= "0011" when "0000",  
    "0100" when "0001", 
    "0101" when "0010",  
    "0110" when "0011",   
    "0111" when "0100",   
    "1000" when "0101",   
    "1001" when "0110",       
    "1010" when "0111",       
    "1011" when "1000",       
    "1100" when "1001",
    "1101" when "1010",
    "1110" when "1011",
    "1111" when "1100",
    "0000" when "1101",
    "0001" when "1110",
    "0010" when others;
   
end model;

The simulated waveform obtained from a VHDL software is as follows:


Similarly we can use case statement of VHDL to construct the above code conversion truth table.
Read More..

Different ways of implementing D Flip Flip with VHDL

| 0 comments |
Shown below is a table that shows how D flip flop can be implemented in different ways with VHDL. The first row shows the same D flip flop different inputs, that is, with asynchronous and synchronous reset. The second row shows different way of writing the behavior of the D flip flop.




DFF without asynchronous reset
DFF with asynchronous reset
DFF with synchronous enable
DFF with synchronous enable
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d : in std_logic;
q : out std_logic;
clk : in std_logic
);
end dff;
architecture dff_arch of dff is
begin
process(clk)
begin
if(clkevent and clk = 1)then
q <= d;
end if;
end process;
end dff_arch;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d : in std_logic;
q : out std_logic;
rst : in std_logic;
clk : in std_logic
);
end dff;
architecture dff_arch of dff is
begin
process(clk, rst)
begin
if(rst = 1)then
q <= 0;
elsif(clkevent and clk = 1)then
q <= d;
end if;
end process;
end dff_arch;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d : in std_logic;
en : in std_logic;
rst : in std_logic;
clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_arch of dff is
begin
process(clk, rst)
begin
if(rst = 1)then
q <= 0;
elsif(clkevent and clk = 1)then
if(en = 1) then
q <= d;
end if;
end if;
end process;
end dff_arch;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d : in std_logic;
en : in std_logic;
rst : in std_logic;
clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_arch of dff is
signal int_reg, int_next : std_logic;
begin
process(clk, rst)
begin
if(rst = 1)then
int_reg <= 0;
elsif(clkevent and clk = 1)then
int_reg <= int_next;
end if;
end process;
int_next <= d when en = 1 else int_reg;
q <= int_reg;
end dff_arch;
Implicit function call example
Explicit function call example
No function call, simple edge detect
Using if statement and function call
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d, clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_behave of dff is
begin
process
begin
wait until rising_edge(clk);
q <= d;
end process;
end dff_behave;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d, clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_behave of dff is
begin
process
begin
wait on clk until rising_edge(clk);
q <= d;
end process;
end dff_behave;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d, clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_behave of dff is
begin
process
begin
wait until clkevent and clk = 1;
q <= d;
end process;
end dff_behave;
library ieee;
use ieee.std_logic_1164.all;
entity dff is
port(
d, clk : in std_logic;
q : out std_logic
);
end dff;
architecture dff_behave of dff is
begin
process
begin
wait on clk;
if rising_edge(clk) then
q <= d;
end if;
end process;
end dff_behave;
Read More..

How to design an ALU in VHDL

| 0 comments |
Arithmetic Logic Unit is an important part of the CPU or processor. It performs logic operation such as or, and, nand etc and arithmetic functions such as addition, subtraction etc. These operations are performed depending upon the opcode code that is fed into it and produces output depending upon the opcode. Here we show how to design an ALU in VHDL and verify the output. The VHDL code is provided and the design can be simulated in any VHDL Software.

The design of ALU in VHDL is fairly simple. You have to decide what logic and arithmetic operation you wish to the ALU to perform. And you have to give each function an opcode which is some 4 bit code say.

Let x and y be 8 bit input, c be 1 bit carry in to the ALU and z be the output which is also 8 bits.

Then the VHDL code for the ALU is pretty straight forward. The code is below.

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity alu is
    port(x,y: in std_logic_vector(7 downto 0);
    c : in std_logic;
    opcode : in std_logic_vector(3 downto 0);
    z : out std_logic_vector(7 downto 0)   
    );
end alu;

architecture arch of alu is

begin

    with opcode select
   
    -- logic unit
    z <= x when "0000",
    not x when "0001",
    y when "0010",
    not y when "0011",
    x and y when "0100",
    x nand y when "0101",
    x or y when "0110",
    x nor y when "0111",
   
    -- arithmetic unit
   
    x+1 when "1000",
    y+1 when "1001",
    x+y when "1010",
    x-y when "1011",
    0-x+y when "1100",
    0-x-y when "1101",
    x+y-1 when "1110",
    x+y+c when others;
   
end arch;

In the above VHDL code, the when statements was used which is a concurrent statement. Notice that in the code, + and - operators were used in the arithmetic section. Because these operators were used the VHDL library std_logic_unsigned was used. If they were not used then the operator cannot act on the std_logic_vector inputs x and y.

The block diagram and the simulation results are now shown.

ALU VHDL block diagram

Now the simulation results are shown for each of the opcodes.

Logic operation:

logic operation

Arithmetic operation:

arithmetic operation

These simulator used was the active hdl vhdl software.
Read More..