Equivalent / alternative examples
1 - The one I already posted with a correction:
module mux (#parameter WIDTH = 8,
# parameter CHANNELS = 4) (
input in_bus,
input sel,
output out
);
genvar ig;
wire input_array ;
assign out = input_array;
generate
for(ig=0; ig<CHANNELS; ig=ig+1) begin: array_assignments
assign input_array = in_bus;
end
endgenerate
//define the clogb2 function
function integer clogb2;
input depth;
integer i,result;
begin
for (i = 0; 2 ** i < depth; i = i + 1)
result = i + 1;
clogb2 = result;
end
endfunction
endmodule
2 - Similar to# 1 but without using the "Variable Vector Part Select" operator:
module mux (#parameter WIDTH = 8,
# parameter CHANNELS = 4) (
input in_bus,
input sel,
output out
);
genvar ig;
wire input_array ;
assign out = input_array;
generate
for(ig=0; ig<CHANNELS; ig=ig+1) begin: array_assignments
assign input_array = in_bus;
end
endgenerate
//define the clogb2 function
function integer clogb2;
input depth;
integer i,result;
begin
for (i = 0; 2 ** i < depth; i = i + 1)
result = i + 1;
clogb2 = result;
end
endfunction
endmodule
3 - Equivalent logic without using a generate statement. I don't recommend this because some simulators don't work well with the "reg" type declaration for the array. Again, you could rewrite this to not use the "+:" operator if you wish.
module module mux (#parameter WIDTH = 8,
# parameter CHANNELS = 4) (
input in_bus,
input sel,
output out
);
integer i;
reg input_array ;
assign out = input_array;
always @*
for(i=0; i<CHANNELS; i=i+1)
input_array = in_bus;
//define the clogb2 function
function integer clogb2;
input depth;
integer i,result;
begin
for (i = 0; 2 ** i < depth; i = i + 1)
result = i + 1;
clogb2 = result;
end
endfunction
endmodule
Jake