You are on page 1of 4

VHDL code explanation for xor xnor

Inputs:

a and b: 4-bit input vectors

ctrl2: A signal that decides which operation to perform

Output:

y: A 5-bit output vector (always with a 0 in the last bit)

What It Does:

Chooses Operation Based on Control Signal:

If ctrl2 is 0, it performs XOR on corresponding bits of a and b.

If ctrl2 is 1, it performs XNOR on corresponding bits of a and b.

The Loop:

The loop goes through each of the 4 bits of a and b individually.

For each corresponding pair of bits, it either:

XORs them (producing 1 if they're different, 0 if they're the same)

XNORs them (producing 1 if they're the same, 0 if they're different)

This creates the first 4 bits of the output y.

Vhdl code for OR and NOR

Function:

It selectively performs bitwise OR or NOR operations on two 4-bit input vectors (a and b), controlled by
the ctrl1 signal.

It outputs the result as a 5-bit vector (y), with the first 4 bits holding the operation's results and the last
bit fixed at 0 as place holder
Logic:

Process Sensitivity: The code activates whenever ctrl1, a, or b change.

Control Check:

If ctrl1 is 0, it performs bitwise OR.

If ctrl1 is 1, it performs bitwise NOR.

Loop for the Operations:

Iterates through each corresponding bit of a and b (bits 0 to 3).

Applies the chosen operation (OR or NOR) to each bit pair.

Stores the result in the corresponding bit of the output vector y.

VHDL code For AND & NAND

Function:

It selectively performs bitwise AND or NAND operations on 4-bit input vectors (a and b), based on the
control signal (ctrl).

It outputs the result as a 5-bit vector (y), with the first 4 bits holding the operation's results and the last
bit

Logic:

Process Activation: The code runs when ctrl, a, or b change.

Control-Based Operation:

If ctrl is 0, it performs bitwise AND.

If ctrl is 1, it performs bitwise NAND.

Loop for Bitwise Operations:


Iterates through corresponding bits of a and b (bits 0 to 3).

Applies the chosen operation (AND or NAND) to each bit pair.

Stores the result in the corresponding bit of the output vector y.

VHDL code for adder

It adds two 4-bit binary numbers, producing a 5-bit output (to accommodate a potential carry).

Logic:

Process Activation: The code runs when either input (a or b) changes.

Carry Initialization: Starts with carry = 0 to handle the first bit addition.

Loop for Bitwise Addition:

Iterates through corresponding bits of a and b (bits 0 to 3).

Calculates each output bit (sum(i)) using XOR operations on a(i), b(i), and carry(i).

Calculates potential carry for the next bit (carry(i+1)) using logical AND/OR operations.

Final Carry Output: Assigns the last carry (carry(4)) to the highest output bit (sum(4)) to complete the 5-
bit sum

VHDL code for substractor


Function:
It subtracts two 4-bit binary numbers, producing a 5-bit output (to accommodate a potential borrow).
Logic:

Process Activation: The code runs when either input (a or b) changes.


Borrow Initialization: Starts with bor = 0 to handle the first bit subtraction.
Loop for Bitwise Subtraction:
Iterates through corresponding bits of a and b (bits 0 to 3).
Calculates each output bit (diff(i)) using XOR operations on a(i), b(i), and bor(i).
Calculates potential borrow for the next bit (bor(i+1)) using logical AND/OR operations, incorporating
the negation of a(i) for subtraction.
Final Borrow Output: Assigns the last borrow (bor(4)) to the highest output bit (diff(4)) to complete the 5-
bit difference.

Function:

It performs either addition or subtraction on two 4-bit binary numbers (a and b), based on a control
signal (ctrl).

It outputs the 5-bit result (cb), accommodating potential carry/borrow.

Logic:

Process Activation: Runs when ctrl or inputs (a or b) change.

Controlled Operation:

If ctrl = 0, it performs addition.

If ctrl = 1, it performs subtraction.

Carry/Borrow Initialization: Starts with cabo = 0 for the first bit calculation.

Loop for Bitwise Calculation:

Iterates through corresponding bits (0 to 3).

Calculates each output bit (cb(i)) using XOR operations on a(i), b(i), and cabo(i).

Calculates potential carry/borrow (cabo(i+1)) using logical AND/OR operations, adjusted for subtraction
when ctrl = 1.

Final Carry/Borrow Output: Assigns cabo(4) to cb(4) for the 5-bit result (

You might also like