EXP 3.1 32-BIT ALU USING IF STATEMENT_BECL606 - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

208


39
 
1
// Testbench for 32-Bit ALU
2
module tb_alu_32bit;
3
    reg [31:0] a, b;
4
    reg [2:0] opcode;
5
    wire [31:0] result;
6
   
7
    // Instantiate the ALU
8
    alu_32bit uut (
9
        .a(a),
10
        .b(b),
11
        .opcode(opcode),
12
        .result(result)
13
       
14
    );
15
16
    // Dump file setup for waveform simulation
17
    initial begin
18
        $dumpfile("alu_waveform.vcd");
19
        $dumpvars(1, tb_alu_32bit);
20
    end
21
22
    // Test sequence
23
    initial begin
24
      $display("Time\ta\t\tb\t\topcode\tresult");
25
        $monitor("%0d\t%h\t%h\t%b\t%h", $time, a, b, opcode, result);
26
27
        // Test cases
28
        a = 32'h00000010; b = 32'h00000005; opcode = 3'b000; #10; // Addition
29
        a = 32'h00000020; b = 32'h00000010; opcode = 3'b001; #10; // Subtraction
30
        a = 32'h00000003; b = 32'h00000004; opcode = 3'b010; #10; // Multiplication
31
        a = 32'h00000020; b = 32'h00000004; opcode = 3'b011; #10; // Division
32
        a = 32'h000000FF; b = 32'h0000000F; opcode = 3'b100; #10; // AND
33
        a = 32'h000000F0; b = 32'h0000000F; opcode = 3'b101; #10; // OR
34
        a = 32'h000000F0; b = 32'h0000000F; opcode = 3'b110; #10; // XOR
35
        a = 32'h000000F0; b = 32'h0000000F; opcode = 3'b111; #10; // NOR
36
37
        $finish;
38
    end
39
endmodule
xxxxxxxxxx
1
21
 
1
// 32-Bit ALU Module Supporting 4 Logical and 4 Arithmetic Operations
2
module alu_32bit (
3
    input [31:0] a,          // 32-bit input A
4
    input [31:0] b,          // 32-bit input B
5
    input [2:0] opcode,      // 3-bit opcode to select operation
6
    output reg [31:0] result // 32-bit result
7
);
8
9
    always @(*) 
10
      begin
11
        if (opcode == 3'b000)      result = a + b;          // Addition
12
        else if (opcode == 3'b001) result = a - b;          // Subtraction
13
        else if (opcode == 3'b010) result = a * b;          // Multiplication
14
        else if (opcode == 3'b011) result = a / b;          // Division
15
        else if (opcode == 3'b100) result = a & b;          // AND
16
        else if (opcode == 3'b101) result = a | b;          // OR
17
        else if (opcode == 3'b110) result = a ^ b;          // XOR
18
        else if (opcode == 3'b111) result = ~(a | b);       // NOR
19
        else                      result = 32'b0;           // Default case
20
      end
21
endmodule
229 views and 0 likes     
 
ALU Module (alu_32bit):
The ALU (Arithmetic Logic Unit) performs basic arithmetic and logical operations. It takes two 32-bit inputs (a and b), a 3-bit opcode to select the operation, and produces a 32-bit result along with a zero flag indicating if the result is zero.
Inputs and Outputs:
Inputs:
a [31:0]: First 32-bit operand.
b [31:0]: Second 32-bit operand.
opcode [2:0]: Selects the operation (3 bits allow 8 possible operations).
Outputs:
result [31:0]: The output of the ALU after performing the selected operation.
Operations Based on opcode:
000 – Addition (a + b)
001 – Subtraction (a - b)
010 – Multiplication (a * b)
011 – Division (a / b)
100 – Bitwise AND (a & b)
101 – Bitwise OR (a | b)
110 – Bitwise XOR (a ^ b)
111 – Bitwise NOR (~(a | b))
Behavioral Logic:
always @(*) Block: Ensures the ALU reacts to any change in the inputs or opcode.
case Statement: Selects the operation based on the opcode.

ALU Module (alu_32bit):
The ALU (Arithmetic Logic Unit) performs basic arithmetic and logical operations. It takes two 32-bit inputs (a and b), a 3-bit opcode to select the operation, and produces a 32-bit result along with a zero flag indicating if the result is zero.

Inputs and Outputs:

Inputs:

a [31:0]: First 32-bit operand.
b [31:0]: Second 32-bit operand.
opcode [2:0]: Selects the operation (3 bits allow 8 possible operations).

Outputs:

result [31:0]: The output of the ALU after performing the selected operation.

Operations Based on opcode:
000 – Addition (a + b)
001 – Subtraction (a - b)
010 – Multiplication (a * b)
011 – Division (a / b)
100 – Bitwise AND (a & b)
101 – Bitwise OR (a | b)
110 – Bitwise XOR (a ^ b)
111 – Bitwise NOR (~(a | b))

Behavioral Logic:
always @(*) Block: Ensures the ALU reacts to any change in the inputs or opcode.
case Statement: Selects the operation based on the opcode.

281570:0