Introduction
An ALU (Arithmetic Logic Unit) does math and logic inside a computer. Designing it in parts helps keep things clear and easy to fix.
An ALU (Arithmetic Logic Unit) does math and logic inside a computer. Designing it in parts helps keep things clear and easy to fix.
module alu(input [3:0] a, input [3:0] b, input [2:0] op, output reg [3:0] result); // internal modules called here endmodule
Use module to create parts of the ALU.
Use inputs and outputs to connect parts together.
module adder(input [3:0] a, input [3:0] b, output [3:0] sum); assign sum = a + b; endmodule
module logic_unit(input [3:0] a, input [3:0] b, input [1:0] op, output reg [3:0] out); always @(*) begin case(op) 2'b00: out = a & b; 2'b01: out = a | b; 2'b10: out = a ^ b; default: out = 4'b0000; endcase end endmodule
op.module alu(input [3:0] a, input [3:0] b, input [2:0] op, output reg [3:0] result); wire [3:0] sum; wire [3:0] logic_out; adder add1(a, b, sum); logic_unit logic1(a, b, op[1:0], logic_out); always @(*) begin case(op[2]) 1'b0: result = sum; 1'b1: result = logic_out; endcase end endmodule
This testbench shows how the ALU adds and does logic operations by selecting the right module inside.
module adder(input [3:0] a, input [3:0] b, output [3:0] sum); assign sum = a + b; endmodule module logic_unit(input [3:0] a, input [3:0] b, input [1:0] op, output reg [3:0] out); always @(*) begin case(op) 2'b00: out = a & b; 2'b01: out = a | b; 2'b10: out = a ^ b; default: out = 4'b0000; endcase end endmodule module alu(input [3:0] a, input [3:0] b, input [2:0] op, output reg [3:0] result); wire [3:0] sum; wire [3:0] logic_out; adder add1(a, b, sum); logic_unit logic1(a, b, op[1:0], logic_out); always @(*) begin case(op[2]) 1'b0: result = sum; 1'b1: result = logic_out; default: result = 4'b0000; endcase end endmodule module testbench; reg [3:0] a, b; reg [2:0] op; wire [3:0] result; alu myALU(a, b, op, result); initial begin a = 4'b0011; b = 4'b0101; op = 3'b000; #10; // add $display("Add: %b + %b = %b", a, b, result); op = 3'b100; #10; // AND $display("AND: %b & %b = %b", a, b, result); op = 3'b101; #10; // OR $display("OR: %b | %b = %b", a, b, result); op = 3'b110; #10; // XOR $display("XOR: %b ^ %b = %b", a, b, result); $finish; end endmodule
Break big designs into smaller modules to keep code clean.
Use wires to connect outputs of one module to inputs of another.
Test each small module before combining them.
Design ALU as smaller modules like adder and logic unit.
Use a top module to connect and control these parts.
This makes your design easier to understand and fix.
adder4 inside a top ALU module in Verilog?result when op = 2'b01, a = 4'b0011, and b = 4'b0101?
module alu_top(input [3:0] a, b, input [1:0] op, output reg [3:0] result);
wire [3:0] sum, and_out;
adder4 add(.a(a), .b(b), .sum(sum));
logic_unit logicu(.a(a), .b(b), .and_out(and_out));
always @(*) begin
case(op)
2'b00: result = sum;
2'b01: result = and_out;
default: result = 4'b0000;
endcase
end
endmodulemodule alu_top(input [3:0] a, b, input [1:0] op, output reg [3:0] result);
wire [3:0] sum, and_out;
adder4 add(a, b, .sum(sum));
logic_unit logicu(.a(a), .b(b), .and_out(and_out));
always @(*) begin
case(op)
2'b00: result = sum;
2'b01: result = and_out;
default: result = 4'b0000;
endcase
end
endmodulesub4 alongside the existing adder4 and logic_unit. How should you modify the top ALU module to select between addition and subtraction based on op signal, assuming op=2'b10 means subtraction?sub4 module and add a case for 2'b10 to assign its output to result. -> Option BPass the test and this pattern moves unknown