Forum Discussion
Altera_Forum
Honored Contributor
16 years agoFirst could you clarify? Are you trying to get:
a) (2^n)-1 or b) 2^(n-1) I suspect it's a). If not let me know.
module pow_2toN(
input n,
output result
);
wire shift_reg;
assign shift_reg = {16'd0,16'hffff} << n;
assign result = shift_reg;
endmodule or... module pow_2toN(
input n,
output result
);
genvar i;
generate
for(i=0;i<16;i=i+1) begin : result_assigns
assign result = (i < n) : 1'b1 : 1'b0;
end
endgenerate
endmodule
or... module pow_2toN(
input n,
output reg result
);
integer i;
always @* begin
result = 16'd0;
for(i=0;i<16;i=i+1)
if(i<n) result = 1'b1;
end
endmodule
or... module pow_2toN(
input n,
output reg result
);
integer i;
always @* begin
result = 16'd1;
for(i=1;i<16;i=i+1)
if(i<=n) result = {result,1'b0};
result = result + 16'hffff;
end
endmodule
We could keep going but I digress. I may have some typos in there. What might be entertaining for you is to try all these variants and see what wildly different compilation results they produce. Jake