Posts

1-to-4 Demultiplexer (Using with-select-when Statement)

Image
The demultiplexer is a combinational logic circuit designed to switch one input line to one of several separate output lines. It is exactly opposite of Multiplexer. In 1-to-4 Demux the input line is connected to one of the 4 output lines depending on the select line input. The below VHDL code is written using with-select-when  statement. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity demux is     Port ( Din : in  STD_LOGIC;            Dout : out  STD_LOGIC_VECTOR (3 downto 0);            Sel : in  STD_LOGIC_VECTOR (1 downto 0)); end demux; architecture Behavioral of demux is begin with sel select Dout <= "000"&Din when "00", "00"&Din&'0' when "01", '0'&Din&"00" when "10", Din&"000" when "11", "XXXX" when others; end Behavioral; Testbench: LIBRARY ieee; USE ieee.std_logic_1164.ALL;   ENTITY Demux_tb IS END Demux_tb; ...

1-to-4 Demultiplexer (if-else statement)

Image
  The demultiplexer is a combinational logic circuit designed to switch one input line to one of several separate output lines. It is exactly opposite of Multiplexer. In 1-to-4 Demux the input line is connected to one of the 4 output lines depending on the select line input. The below VHDL code is written using  if-else  statement. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity demux14 is     Port ( Din : in  STD_LOGIC;            Sel : in  STD_LOGIC_VECTOR (1 downto 0);            dout : out  STD_LOGIC_VECTOR (3 downto 0)); end demux14; architecture Behavioral of demux14 is begin process(din,sel) begin if(sel="00") then dout <= "000"&din; elsif(sel="01") then dout <= "00"&din&'0'; elsif(sel="10") then dout <= '0'&din&"00"; else dout <= din&"000"; end if; end process; end Behavioral; Testbenc...

4-to-1 Multiplexer (if-else Statement)

Image
  The  4-to-1 Multiplexer is used to select between multiple input lines based on select line. The below code is written using  if-else  statement (sequential statement). VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Mux41 is     Port ( i0,i1,i2,i3,s0,s1 : in  STD_LOGIC;            Y : out  STD_LOGIC); end Mux41; architecture Mux41 of Mux41 is begin process(i0,i1,i2,i3,s0,s1) begin if(s1='0' and s0='0') then Y <= i0; elsif(s1='0' and s0='1') then Y <= i1; elsif(s1='1' and s0='0') then Y <= i2; elsif(s1='1' and s0='1') then Y <= i3; else  Y <= 'Z'; end if; end process; end Mux41; Testbench: LIBRARY ieee; USE ieee.std_logic_1164.ALL;   ENTITY mux41_tb IS END mux41_tb;   ARCHITECTURE behavior OF mux41_tb IS      COMPONENT Mux41     PORT(          i0 : IN  std_logic; ...

1-bit Full Adder (Dataflow & Behavioral Style)

Image
The 1-bit Full Adder circuit is used for adding two 1-bit numbers along with carry, if any, and generates two outputs viz. Sum and Carry. VHDL Code: (Dataflow) library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity FA_DF is     Port ( A,B,Cin : in  STD_LOGIC;            Sum,Carry : out  STD_LOGIC); end FA_DF; architecture Behavioral of FA_DF is begin Sum <= A xor B xor Cin; Carry <= (A and B) or (B and Cin) or (A and Cin); end Behavioral; VHDL Code: (Behavioral) library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity FA_DF is     Port ( A,B,Cin : in  STD_LOGIC;            Sum,Carry : out  STD_LOGIC); end FA_DF; architecture Behavioral of FA_DF is begin process(A,B,Cin) begin if(A='0' and B='0' and Cin='0') then Sum <= '0'; Carry <='0'; elsif(A='0' and B='0' and Cin='1') then Sum <= '1'; Carry <='0'; elsif(A='0' and B='1' and Cin='0') then Su...

Sequence Detector (Mealy Machine-1011) VHDL Code

Image
A sequence detector is a sequential circuit that outputs '1' when a particular pattern of bits sequentially arrives at its data input. A sequence detector typically has 1-bit Data Input, CLOCK Input and RESET Input. It generates 1-Bit Output. In this design, circuit receives input at W, generates output on Z. Every change occurs on Positive clock. The design detects occurrence of '1011' (non-overlapping) in the input sequence and produces output '1' after successfully detecting the sequence. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Mealy_1011 is     Port ( W, clk, reset : in  STD_LOGIC;            z : out  STD_LOGIC); end Mealy_1011; architecture Behavioral of Mealy_1011 is type state_type is (a,b,c,d); signal Y : state_type; begin process(clk,reset) begin if(reset ='1') then Y <= A; ELSIF(RISING_EDGE(CLK)) THEN CASE Y IS WHEN A => IF(W='0') THEN y <= A; z ...

Shift Register (Parallel-In-Serial-Out) VHDL Code

Image
In PISO type shift register, the data is given as input to all the flip-flops at the same time (simultaneously), and the circuit will produce the serial output. In the VHDL Code Pin: Parallel Input CLR: clear input (or RESET input) CLK: clock input L_S: Load/Shift signal, used to control whether to load the Shift register with new data on Pin (i.e. L_S='1')  or to shift the existing data in the Shift register (i.e. L_S='0'). Sout: Output Signal VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Left_SR is     Port ( Pin : in  STD_LOGIC_VECTOR (7 downto 0);            clr, clk, L_S : in  STD_LOGIC;            sout : out  STD_LOGIC); end Left_SR; architecture Behavioral of Left_SR is signal temp: std_logic_vector(7 downto 0); begin process(clk,clr) begin if(clr='1') then temp <= "00000000"; elsif (clk'event and clk='1') then if(L_S='1') then temp <=...

Up-Down Counter VHDL Code

Image
In digital design, a counter is a device which stores the number of times a particular event has occurred, often with respect to a clock signal. The most common type is a sequential digital logic circuit with an input line called the clock and multiple output lines representing the count. The values on the output lines represent a number in the binary or BCD number system. Each pulse applied to the clock input increments or decrements the number in the counter. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_unsigned.ALL; entity U_DCOUNTER is     Port ( RESET : in  STD_LOGIC;            U_D : in  STD_LOGIC;            CLK : in  STD_LOGIC;            Cout : out  STD_LOGIC_VECTOR (3 downto 0)); end U_DCOUNTER; architecture U_DCOUNTER of U_DCOUNTER is begin process(CLK,RESET) variable temp:STD_LOGIC_VECTOR (3 downto 0):="0000"; begin...

Getting Started with Arduino IDE

Image
Arduino IDE basics and how to install new boards and libraries in Arduino IDE. The channel is created for getting started with free software available for use. Main focus is on engineering tools for simulation, synthesis and modelling of electronic circuits, the channel will also cover programming languages in the coming days. SUBSCRIBE the channel to stay updated with new videos. Thank You.

T Flip-flop VHDL Code

Image
If the T input is high, the T flip-flop changes state (i.e. toggles) whenever the clock input is applied. If the T input is low, the flip-flop holds the previous value. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity T_FF is     Port ( T : in  STD_LOGIC;            clk : in  STD_LOGIC;            Q : out  STD_LOGIC;            rst : in  STD_LOGIC); end T_FF; architecture Behavioral of T_FF is signal temp : std_logic; begin process(clk,rst,t) begin if (rst='1') then temp <= '0'; elsif(clk'event and clk ='1') then if(T='1') then temp <= not temp; end if; end if; end process; Q <= temp; end Behavioral; Testbench: LIBRARY ieee; USE ieee.std_logic_1164.ALL;   ENTITY T_FF_tb IS END T_FF_tb;   ARCHITECTURE behavior OF T_FF_tb IS        -- Component Declaration for the Unit Under Te...

D Flip-flop VHDL Code

Image
In electronics, a flip-flop or latch is a circuit that has two stable states and can be used to store 1-bit of information. The circuit can be made to change state by signals applied to one or more control inputs and will have one or two outputs. It is the basic storage element in sequential logic. The D flip-flop, which is widely used, also known as a "data" or "delay" flip-flop. The D flip-flop captures the value of the D-input at a definite portion of the clock cycle (such as the rising edge or falling edge of the clock). That captured value becomes the Q output. At other times, the output Q does not change. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity D_FF is     Port ( D : in  STD_LOGIC;            clk : in  STD_LOGIC;            Q : out  STD_LOGIC); end D_FF; architecture Behavioral of D_FF is begin process begin --if (clk'event and clk='1') then wait until cl...

Priority Encoder VDHL Code

Image
The output of a priority encoder is the binary representation of the index of the most significant activated line, starting from zero. They are often used to control interrupt requests by acting on the highest priority interrupt input. If two or more inputs are given at the same time, the input having the highest priority will take precedence. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Priority_encode is port(I : in std_logic_vector(3 downto 0); Y : out std_logic_vector(1 downto 0); P : out std_logic); end Priority_encode; architecture Behavioral of Priority_encode is begin P <= I(0) or I(1) or I(2) or I(3); Y <= "11" when I(3) = '1' else "10" when I(2) = '1' else "01" when I(1) = '1' else "00" when I(0) = '1' else "ZZ"; end Behavioral; Testbench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY PE_tb IS END PE_tb;   ARCHITECTURE behavior OF PE_tb I...

Comparator VHDL Code (if-else statement)

Image
VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity Compare_4 is     Port ( A : in  STD_LOGIC_VECTOR (3 downto 0);            B : in  STD_LOGIC_VECTOR (3 downto 0);            Eq : out  STD_LOGIC;            Gt : out  STD_LOGIC;            Lt : out  STD_LOGIC); end Compare_4; architecture Behavioral of Compare_4 is begin process (a,b) is     begin         if (a=b) then             eq <= '1';             gt <= '0';             lt <= '0';         elsif (a<b) then             eq <= '0';             gt <= '0';             lt <= '1';         else     ...

4-bit Comparator VHDL Code (when-else statement)

Image
Comparator circuit is used to compare two input signals and generates signals accordingly. The 4-bit comparator circuit compares two 4-bit numbers and generates 1-bit signals less than(Lt), greater than(Gt) or equal to(Eq). Irrespective of size of input numbers being compared the output signals remains the same. VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity compare_4 is     Port ( A : in  STD_LOGIC_VECTOR (3 downto 0);            B : in  STD_LOGIC_VECTOR (3 downto 0);            Eq : out  STD_LOGIC;            Gt : out  STD_LOGIC;            Lt : out  STD_LOGIC); end compare_4; architecture Behavioral of compare_4 is begin Eq <= '1' WHEN A = B ELSE '0' ; Gt <= '1' WHEN A > B ELSE '0' ; Lt <= '1' WHEN A < B ELSE '0' ; end Behavioral; Testbench: LIBRARY ieee; USE ieee.std_logic_1164.ALL; ENTITY Compar...

3-to-8 line Decoder VHDL Code (with-select-when)

Image
A binary decoder is a combinational logic circuit that converts binary information from the n coded inputs to a maximum of 2^n unique outputs.  VHDL Code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity decoder38 is     Port ( A : in  STD_LOGIC_VECTOR(2 downto 0);            G1,G2b,G3b : in  STD_LOGIC;            Y : out  STD_LOGIC_VECTOR (7 downto 0)); end decoder38; architecture Behavioral of decoder38 is signal en : std_logic_vector (5 downto 0) ; begin en <= (G1 & G2b & G3b) & A ; with en select  y <= "00000001" when "100000", "00000010" when "100001", "00000100" when "100010", "00001000" when "100011", "00010000" when "100100", "00100000" when "100101", "01000000" when "100110", "10000000" when "100111", "00000000" when others; en...