VHDL code for half subtractor using behavioral method – full code & explanation

Let’s start with writing the VHDL code for half subtractor using the behavioral architecture. First, we will understand the working of a half subtractor and then take a look at its truth table. We will then take a look at the syntax for the half subtractor’s VHDL programming. For the full code, scroll down.

Explanation of the VHDL code for half subtractor using behavioral method. How does the code work?

A half subtractor is an arithmetic combinational logic circuit that subtracts two bits and gives two outputs, the Difference, and the Borrow output. The half subtractor does not account the borrow’s value in the subtraction process, so it doesn’t exactly perform the entire subtraction. Hence it is known as the half-subtractor.

Logic circuit of the half subtractor

A half subtractor has two inputs and two outputs. As we know, the entity part of a VHDL program deals with declaring only the I/O ports of the logic circuit. Hence, that’s all the information we needed from this diagram of the half subtractor. The behavior of the half subtractor for writing its VHDL program is extracted from its truth table.

Half Subtractor

Truth table of the half subtractor

ABDIFFERENCEBORROW
0000
0111
1010
1100

We are working with the truth table in the behavioral architecture of the half subtractors code. Hence, we will declare the I/O ports as vector quantities in the entity-architecture declaration. Let’s name the entity as HALFSUBTRACTOR_BEHAVIORAL_SOURCE. The half-subtractor has two inputs and two outputs. Thus the vectors will have a size of two (1 downto 0). Moreover, since we are using behavioral architecture, keep in mind that we will be using two begin statements and a process statement between them.

entity HALFSUBTRACTOR_BEHAVIORAL_SOURCE is

    Port ( A : in  STD_LOGIC_VECTOR (1 downto 0);

           Y : out  STD_LOGIC_VECTOR (1 downto 0));

end HALFSUBTRACTOR_BEHAVIORAL_SOURCE;




architecture Behavioral of HALFSUBTRACTOR_BEHAVIORAL_SOURCE is


begin


process(A)


begin

One of the statement types offered to us by the behavioral architecture are the if-else-if statements. We saw syntax for these in our post on behavioral architecture. In this particular scenario, we will use our understanding of the behavior of the half-subtractor from its truth table and code it in using the if-else-if statements.

if (A = "00" or A = "11") then

Y<="00";

else if (A = "01") then

Y<="11";

else

Y<="10";

Don’t forget to close off the if statements and the process statement with their respective commands.

Full VHDL code for half subtractor using behavioral method

library IEEE;

use IEEE.STD_LOGIC_1164.ALL;

use IEEE.STD_LOGIC_ARITH.ALL;

use IEEE.STD_LOGIC_UNSIGNED.ALL;




entity HALFSUBTRACTOR_BEHAVIORAL_SOURCE is

    Port ( A : in  STD_LOGIC_VECTOR (1 downto 0);

           Y : out  STD_LOGIC_VECTOR (1 downto 0));

end HALFSUBTRACTOR_BEHAVIORAL_SOURCE;




architecture Behavioral of HALFSUBTRACTOR_BEHAVIORAL_SOURCE is


begin


process(A)


begin


if (A = "00" or A = "11") then

Y<="00";

else if (A = "01") then

Y<="11";

else

Y<="10";


end if;

end if;

end process;

end Behavioral;

About The Writer

More topics in VLSI and VHDL course
What is VLSI? And what are the job opportunities for a VLSI student?
VHDL code for full subtractor & half subtractor using dataflow method – full code & explanation
VHDL code for full adder using behavioral method – full code & explanation
VHDL code for all logic gates using dataflow method – full code and explanation
VHDL code for full subtractor using behavioral method – full code & explanation
VHDL code for half adder & full adder using dataflow method – full code & explanation
VHDL code for demultiplexer using dataflow method – full code & explanation
VHDL code for multiplexer using dataflow method – full code and explanation
VHDL code for an encoder using dataflow method – full code and explanation
VHDL code for half subtractor using behavioral method – full code & explanation
VHDL code for multiplexer using behavioral method – full code and explanation
VHDL code for comparator using behavioral method – full code and explanation
VHDL code for decoder using dataflow method – full code and explanation
VHDL code for full adder using structural method – full code and explanation
VHDL code for EXOR using NAND & structural method – full code & explanation
VHDL code for ALU (1-bit) using structural method – full code and explanation
VHDL code for demultiplexer using behavioral method – full code & explanation
VHDL code for decoder using behavioral method – full code and explanation
VHDL code for an encoder using behavioral method – full code and explanation
VHDL code for flip-flops using behavioral method – full code of all flip-flops
VHDL code for synchronous counters using behavioral method

Leave a Reply

Your email address will not be published. Required fields are marked *