What is the output of the following Verilog code when simulated?
module adder(input wire a, input wire b, output wire sum); assign sum = a ^ b; endmodule module top; reg x = 1'b0; reg y = 1'b1; wire s; adder my_adder(.a(x), .b(y), .sum(s)); initial begin $display("Sum = %b", s); end endmodule
Think about the XOR operation between inputs 0 and 1.
The module adder computes the XOR of inputs a and b. Since a=0 and b=1, the output sum is 1.
What will be printed when this Verilog code runs?
module inverter(input wire in, output wire out); assign out = ~in; endmodule module top; reg a = 1'b1; wire b; inverter inv1(a, b); initial begin $display("Output = %b", b); end endmodule
Remember that ~ is bitwise NOT.
The inverter module outputs the bitwise NOT of input a. Since a=1, output b is 0.
Which option contains a syntax error in the module instantiation?
module multiplier(input wire [1:0] a, input wire [1:0] b, output wire [3:0] product); assign product = a * b; endmodule module top; reg [1:0] x = 2'b10; reg [1:0] y = 2'b11; wire [3:0] p; // Instantiations below // Choose the one with syntax error endmodule
Check the correct syntax for named port connections.
In Verilog, named port connections use the syntax .port(signal) without equals sign. Option A uses = which is invalid.
What is the value of out after simulation?
module and_gate(input wire a, input wire b, output wire out); assign out = a & b; endmodule module top; reg p = 1'b1; reg q = 1'b0; wire r, s, out; and_gate g1(.a(p), .b(q), .out(r)); and_gate g2(.a(p), .b(p), .out(s)); and_gate g3(.a(r), .b(s), .out(out)); initial begin $display("Final output = %b", out); end endmodule
Trace the AND operations step by step.
First gate: r = p & q = 1 & 0 = 0
Second gate: s = p & p = 1 & 1 = 1
Third gate: out = r & s = 0 & 1 = 0
Consider a top-level module instantiating two submodules. Which statement is true about the connections?
Think about how modules communicate in Verilog.
In Verilog, submodules communicate with the top module through ports. Named port mapping is a clear way to connect signals declared in the top module to submodules. Submodules cannot access internal signals of the top module directly without ports. Signals do not need to be global. Both named and positional mappings are allowed.
