Assignment 5 CG
Assignment 5 CG
UG-IV Semester-VII
Submitted By - Adway Paul
Roll - 002110701100
Group - G1
Assignment - 3
Question: Design a 4-bit Arithmetic Logic Unit (ALU) capable of
performing arithmetic and logic operations such
as addition, subtraction, AND, OR, and NOT. Implement the ALU using
suitable logic gates and
components. Verify its functionality by testing it with a range of test inputs and
operations.
Operations:
Implementation:
Program Code:
Here we used verilog to design our4bit ALU using above specifications .
module ALU_4bit (
input [3:0] A, // 4-bit input A
input [3:0] B, // 4-bit input B
input [2:0] opcode, // 3-bit opcode for operation selection
output reg [3:0] result, // 4-bit output result
output reg carry_out, // Carry out for addition/subtraction
output zero // Zero flag (1 if result is zero)
);
wire [4:0] sum, diff; // Wires for storing sum and difference
wire [3:0] and_res, or_res, not_res; // Wires for logical operations
// Arithmetic operations
assign sum = A + B; // Addition
assign diff = A - B; // Subtraction
// Logical operations
assign and_res = A & B; // AND
assign or_res = A | B; // OR
assign not_res = ~A; // NOT (only applied to A)
Testbench Code:
module test_ALU_4bit_random;
reg [3:0] A, B; // 4-bit inputs
reg [2:0] opcode; // 3-bit opcode
wire [3:0] result; // 4-bit result
wire carry_out, zero; // carry-out and zero flags
initial begin
// Randomly generate test cases for a certain number of cycles
for (i = 0; i < 10; i = i + 1) begin
A = $random % 16; // Random 4-bit value for A (0 to 15)
B = $random % 16; // Random 4-bit value for B (0 to 15)
opcode = $random % 5; // Random 3-bit opcode (0 to 4, as we have 5
operations)
Simulation Output:
Output Waveform:
Conclusion: This 4-bit ALU supports basic arithmetic and logic operations,
and its functionality can be verified using the provided testbench. The testbench
will generate 20 random tests, displaying the input values and the ALU outputs
after each cycle.