More topics in VLSI and VHDL course

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

Next up in this VHDL course, we will be 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.

Contents

## 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.

### Truth table of the half subtractor

 A B DIFFERENCE BORROW 0 0 0 0 0 1 1 1 1 0 1 0 1 1 0 0

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

X More topics in VLSI and VHDL course
 Data Types in VHDL What is VLSI? And what are the job opportunities for a VLSI student? Operators in VHDL – Easy explanation VHDL code for all logic gates using dataflow method – full code and explanation VHDL code for half adder & full adder using dataflow method – full code & explanation VHDL code for full subtractor & half subtractor using dataflow method – full code & explanation VHDL code for multiplexer using dataflow method – full code and explanation VHDL code for demultiplexer using dataflow method – full code & explanation VHDL code for an encoder using dataflow method – full code and explanation VHDL code for decoder using dataflow method – full code and explanation VHDL code for full adder using behavioral method – full code & explanation VHDL code for half subtractor using behavioral method – full code & explanation VHDL code for full subtractor using behavioral method – full code & explanation VHDL code for a 2-bit multiplier – All modeling styles VHDL code for comparator using behavioral method – full code and explanation VHDL code for multiplexer using behavioral method – full code and explanation VHDL code for demultiplexer using behavioral method – full code & explanation VHDL code for an encoder using behavioral method – full code and explanation VHDL code for decoder using behavioral method – full code and explanation VHDL code for flip-flops using behavioral method – full code VHDL code for synchronous counters using behavioral method 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 a priority encoder – All modeling styles VHDL code for ALU (1-bit) using structural method – full code and explanation VHDL Quiz | MCQs | Interview Questions
CLOSE