Challenge - 5 Problems
Generate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Verilog generate block?
Consider the following Verilog code snippet using a generate statement. What will be the value of the wire array
out after simulation?module gen_example(output wire [3:0] out);
genvar i;
generate
for (i = 0; i < 4; i = i + 1) begin : gen_loop
assign out[i] = (i % 2 == 0) ? 1'b1 : 1'b0;
end
endgenerate
endmoduleVerilog
module gen_example(output wire [3:0] out); genvar i; generate for (i = 0; i < 4; i = i + 1) begin : gen_loop assign out[i] = (i % 2 == 0) ? 1'b1 : 1'b0; end endgenerate endmodule
Attempts:
2 left
💡 Hint
Remember that the index i starts at 0 and increments by 1. The ternary operator assigns 1 when i is even.
✗ Incorrect
The generate loop assigns 1 to out[i] when i is even (0 and 2), and 0 when i is odd (1 and 3). So out[0] = 1, out[1] = 0, out[2] = 1, out[3] = 0, which is 4'b1010 in binary. Verilog bit ordering means out[3] is the most significant bit, so the vector is 4'b1010.
❓ Predict Output
intermediateHow many instances are created by this generate block?
Given the following Verilog code, how many instances of the module
bit_cell are created?module top_module();
genvar idx;
generate
for (idx = 0; idx < 8; idx = idx + 2) begin : gen_cells
bit_cell cell_inst();
end
endgenerate
endmodule
module bit_cell();
// Some internal logic
endmoduleVerilog
module top_module();
genvar idx;
generate
for (idx = 0; idx < 8; idx = idx + 2) begin : gen_cells
bit_cell cell_inst();
end
endgenerate
endmodule
module bit_cell();
// Some internal logic
endmoduleAttempts:
2 left
💡 Hint
Count how many times the loop runs with the given step size.
✗ Incorrect
The loop variable idx starts at 0 and increments by 2 until it is less than 8. The values are 0, 2, 4, 6, so 4 iterations and thus 4 instances of bit_cell are created.
🔧 Debug
advancedIdentify the error in this generate block
Examine the following Verilog code. What error will occur during compilation?
module error_gen();
genvar i;
generate
for (i = 0; i < 3; i = i + 1) begin
wire [3:0] temp;
assign temp = i + 1;
end
endgenerate
endmoduleVerilog
module error_gen();
genvar i;
generate
for (i = 0; i < 3; i = i + 1) begin
wire [3:0] temp;
assign temp = i + 1;
end
endgenerate
endmoduleAttempts:
2 left
💡 Hint
Consider how generate blocks create multiple instances and how signals must be uniquely named.
✗ Incorrect
The wire 'temp' is declared inside the generate loop without unique names for each instance. This causes multiple declarations of 'temp' in the same scope, leading to multiple drivers error.
📝 Syntax
advancedWhich option shows the correct syntax for a generate-if statement?
Select the option that correctly uses a generate-if statement in Verilog to conditionally instantiate a module.
Attempts:
2 left
💡 Hint
Remember Verilog uses parentheses for conditions and 'begin-end' blocks.
✗ Incorrect
Option D uses correct Verilog syntax: 'if (condition) begin ... end'. Options B, C, and D use incorrect syntax such as missing parentheses, 'then' keyword, or braces which are not valid in Verilog.
🚀 Application
expertHow to generate a parameterized bus with alternating bits using generate?
You want to create a parameterized Verilog module that outputs a bus of width
Assume
WIDTH where bits alternate between 1 and 0 starting with 1 at bit 0. Which generate block correctly implements this?Assume
parameter WIDTH = 8;Verilog
module alt_bus #(parameter WIDTH = 8) (output wire [WIDTH-1:0] bus_out); genvar i; generate for (i = 0; i < WIDTH; i = i + 1) begin : gen_alt // Fill in the assignment here end endgenerate endmodule
Attempts:
2 left
💡 Hint
Bit 0 should be 1, so check which condition makes bit 0 equal to 1.
✗ Incorrect
Option C assigns 1 to even bits (i=0,2,4...) and 0 to odd bits, matching the requirement that bit 0 is 1 and bits alternate. Option C assigns 1 to odd bits, so bit 0 would be 0. Option C and D invert the pattern incorrectly.
