gpt4 book ai didi

verilog 生成 if/else

转载 作者:行者123 更新时间:2023-12-02 21:37:41 35 4
gpt4 key购买 nike

我尝试编写一些代码,例如(在verilog中):

parameter N = 128;

if (encoder_in[0] == 1) begin
23 binary_out = 1;
24 end else if (encoder_in[1] == 1) begin
25 binary_out = 2;
26 end else if (encoder_in[2] == 1) begin
27 binary_out = 3;
28 end else if (encoder_in[3] == 1) begin
29 binary_out = 4;
30 end else if (encoder_in[4] == 1) begin
31 binary_out = 5;
32 end else if (encoder_in[5] == 1) begin
33 binary_out = 6;
34 end else if (encoder_in[6] == 1) begin
35 binary_out = 7;
36 end else if (encoder_in[7] == 1) begin
37 binary_out = 8;
......
......
36 end else if (encoder_in[127] == 1) begin
37 binary_out = 8;
end

我希望我可以将 N 更改为我想要的任何值并且它仍然有效。“生成”会在这里起作用吗?像这样:

parameter N = 128;

if (encoder_in[0] == 1) begin
binary_out = 1;
generate for (i=1; i<N; i=i+1) begin
end else if (encoder_in[i] == 1) begin
binary_out = i+1;
end endgenarate

end

如果没有,我该怎么办?非常感谢!

最佳答案

生成 block 不能在另一个语句中使用。我仍在寻找 IEEE std 1800-2012 中的确切引用.

如果您想要一个具有更高 LSB 优先级的解码器,那么以下内容将在不使用生成 block 的情况下工作:

parameter N = 128;
integer i;
...
always @* begin
binary_out = 0; // default value
...
for(i=N-1; i>=0; i=i-1) begin
if (encoder_in[i]==1'b1) begin
binary_out = i+1;
end
end
end

请注意,这是使用倒计时 for 循环。向上计数将优先考虑 MSB。

关于verilog 生成 if/else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21000911/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com