Consider this Verilog ALU module snippet. The ALUop controls the operation: 0=add, 1=subtract, 2=AND, 3=OR.
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result);
always @(*) begin
case (ALUop)
2'b00: Result = A + B;
2'b01: Result = A - B;
2'b10: Result = A & B;
2'b11: Result = A | B;
default: Result = 4'b0000;
endcase
end
endmoduleWhat is the value of Result when A=4, B=3, ALUop=2?
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result); always @(*) begin case (ALUop) 2'b00: Result = A + B; 2'b01: Result = A - B; 2'b10: Result = A & B; 2'b11: Result = A | B; default: Result = 4'b0000; endcase end endmodule
Remember that ALUop=2 means AND operation between A and B.
ALUop=2 corresponds to bitwise AND. 4 (0100) & 3 (0011) = 0000 (decimal 0).
Given the following testbench for a hierarchical ALU design, what is the printed output?
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result);
always @(*) begin
case (ALUop)
2'b00: Result = A + B;
2'b01: Result = A - B;
2'b10: Result = A & B;
2'b11: Result = A | B;
default: Result = 4'b0000;
endcase
end
endmodule
module ALU_top();
reg [3:0] A, B;
reg [1:0] ALUop;
wire [3:0] Result;
ALU alu_inst(A, B, ALUop, Result);
initial begin
A = 4; B = 1; ALUop = 2'b01;
#10;
$display("Result = %d", Result);
end
endmodulemodule ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result); always @(*) begin case (ALUop) 2'b00: Result = A + B; 2'b01: Result = A - B; 2'b10: Result = A & B; 2'b11: Result = A | B; default: Result = 4'b0000; endcase end endmodule module ALU_top(); reg [3:0] A, B; reg [1:0] ALUop; wire [3:0] Result; ALU alu_inst(A, B, ALUop, Result); initial begin A = 4; B = 1; ALUop = 2'b01; #10; $display("Result = %d", Result); end endmodule
ALUop=01 means subtraction: A - B.
With A=4 and B=1, subtraction gives 3.
Examine this Verilog code snippet for an ALU hierarchy. What error will the compiler report?
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result);
always @(*) begin
case ALUop
2'b00: Result = A + B;
2'b01: Result = A - B;
2'b10: Result = A & B;
2'b11: Result = A | B;
default: Result = 4'b0000;
endcase
end
endmodulemodule ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result); always @(*) begin case ALUop 2'b00: Result = A + B; 2'b01: Result = A - B; 2'b10: Result = A & B; 2'b11: Result = A | B; default: Result = 4'b0000; endcase end endmodule
Check the syntax of the case statement in Verilog.
In Verilog, the case statement requires parentheses around the expression: case(ALUop).
Given this Verilog hierarchy:
module Adder(input [3:0] A, input [3:0] B, output [3:0] Sum);
assign Sum = A + B;
endmodule
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result);
wire [3:0] sum_out;
Adder add1(A, B, sum_out);
always @(*) begin
case(ALUop)
2'b00: Result = sum_out;
2'b01: Result = A - B;
2'b10: Result = A & B;
2'b11: Result = A | B;
default: Result = 4'b0000;
endcase
end
endmoduleHow many modules are instantiated inside the ALU module?
Look for module instantiations inside ALU.
Only one module, Adder, is instantiated inside ALU.
Consider this Verilog code with nested modules:
module AndGate(input [3:0] A, input [3:0] B, output [3:0] Out);
assign Out = A & B;
endmodule
module OrGate(input [3:0] A, input [3:0] B, output [3:0] Out);
assign Out = A | B;
endmodule
module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result);
wire [3:0] and_out, or_out;
AndGate and1(A, B, and_out);
OrGate or1(A, B, or_out);
always @(*) begin
case(ALUop)
2'b10: Result = and_out;
2'b11: Result = or_out;
default: Result = 4'b1111;
endcase
end
endmodule
module ALU_top();
reg [3:0] A, B;
reg [1:0] ALUop;
wire [3:0] Result;
ALU alu_inst(A, B, ALUop, Result);
initial begin
A = 4'b1100; B = 4'b1010; ALUop = 2'b11;
#10;
$display("Result = %b", Result);
end
endmoduleWhat is the printed Result?
module AndGate(input [3:0] A, input [3:0] B, output [3:0] Out); assign Out = A & B; endmodule module OrGate(input [3:0] A, input [3:0] B, output [3:0] Out); assign Out = A | B; endmodule module ALU(input [3:0] A, input [3:0] B, input [1:0] ALUop, output reg [3:0] Result); wire [3:0] and_out, or_out; AndGate and1(A, B, and_out); OrGate or1(A, B, or_out); always @(*) begin case(ALUop) 2'b10: Result = and_out; 2'b11: Result = or_out; default: Result = 4'b1111; endcase end endmodule module ALU_top(); reg [3:0] A, B; reg [1:0] ALUop; wire [3:0] Result; ALU alu_inst(A, B, ALUop, Result); initial begin A = 4'b1100; B = 4'b1010; ALUop = 2'b11; #10; $display("Result = %b", Result); end endmodule
ALUop=11 means OR operation between A and B.
Bitwise OR of 1100 and 1010 is 1110.
