gpt4 book ai didi

verilog - LED 开关的简单 Verilog 示例?

转载 作者:行者123 更新时间:2023-12-02 08:40:43 24 4
gpt4 key购买 nike

我正在尝试为 1-hot 编码的简单 LED 开关按钮构建状态机。

特别是我试图通过我的例子来理解阻塞和非阻塞赋值。

您认为以下内容可以做得更好,还是在任何 block 中都是完全错误的?

module example (
input clk,
input rst,
input push,

output reg led_on
);


reg on;
reg off;

reg t_on_off;
reg t_off_on;


always @* begin
t_on_off = on & (push);
end

always @* begin
t_off_on = off & (push);
end


always @(posedge clk or posedge rst) begin
if (rst) on <= 1'b0;
else if (t_off_on) on <= 1'b1;
else if (t_on_off) on <= 1'b0;
end

always @(posedge clk or posedge rst) begin
if (rst) off <= 1'b1;
else if (t_off_on) off <= 1'b0;
else if (t_on_off) off <= 1'b1;
end


always @* begin
led_on = on;
end


endmodule

特别是我想知道:我可以将转换的分配合并到一个 block 中,例如:

always @* begin
t_on_off = on & (push);
t_off_on = off & (push);
end

?

最佳答案

如果不需要one-hot,那么简化为:

module example (
input clk,
input rst,
input push,

output reg led_on
);

always @(posedge clk or posedge rst) begin
if (rst) led_on <= 1'b0;
else if (push) led_on <= !led_on;
end

endmodule

它的功能等同于您拥有的并且更具可读性。

关于verilog - LED 开关的简单 Verilog 示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16840869/

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