HDLbits 刷题 -- Always if2

A common source of errors: How to avoid making latches

When designing circuits, you must think first in terms of circuits:

  • I want this logic gate
  • I want a combinational blob of logic that has these inputs and produces these outputs
  • I want a combinational blob of logic followed by a set of flip-flops

What you must not do is write the code first, then hope it generates a proper circuit.

  • If (cpu_overheated) then shut_off_computer = 1;
  • If (~arrived) then keep_driving = ~gas_tank_empty;

Syntactically-correct code does not necessarily result in a reasonable circuit (combinational logic + flip-flops). The usual reason is: "What happens in the cases other than those you specified?". Verilog's answer is: Keep the outputs unchanged.

This behaviour of "keep outputs unchanged" means the current state needs to be remembered, and thus produces a latch. Combinational logic (e.g., logic gates) cannot remember any state. Watch out for Warning (10240): ... inferring latch(es)" messages. Unless the latch was intentional, it almost always indicates a bug. Combinational circuits must have a value assigned to all outputs under all conditions. This usually means you always need else clauses or a default value assigned to the outputs.

译:

一个常见的错误来源:如何避免制造锁存器
在设计电路时,你必须首先从电路的角度来思考:

我想要这个逻辑门
我想要一个具有这些输入并产生这些输出的组合逻辑块
我想要一个组合逻辑块后面跟着一组触发器
你不应该做的是先写代码,然后希望它能生成一个合适的电路。

如果 (cpu_overheated) 那么 shut_off_computer = 1;
如果 (~arrived) 那么 keep_driving = ~gas_tank_empty;
语法正确的代码并不一定会产生一个合理的电路(组合逻辑 + 触发器)。通常的原因是:“在你指定的情况之外的案例中会发生什么?”。Verilog的答案是:保持输出不变。

“保持输出不变”的行为意味着需要记住当前状态,从而产生一个锁存器。组合逻辑(例如,逻辑门)无法记住任何状态。要注意警告(10240):...推断出锁存器的消息。除非锁存器是有意为之,否则它几乎总是表示一个错误。组合电路在所有条件下都必须为所有输出分配一个值。这通常意味着你总是需要 `else` 子句或者为输出分配一个默认值。

Demonstration

The following code contains incorrect behaviour that creates a latch. Fix the bugs so that you will shut off the computer only if it's really overheated, and stop driving if you've arrived at your destination or you need to refuel.

以下代码包含错误的行为,这会导致产生一个锁存器。修复这些错误,以便只有在计算机真正过热时才关闭计算机,并且在到达目的地或需要加油时停止驱动。

// synthesis verilog_input_version verilog_2001
module top_module (
    input      cpu_overheated,
    output reg shut_off_computer,
    input      arrived,
    input      gas_tank_empty,
    output reg keep_driving  ); //

    always @(*) begin
        if (cpu_overheated)
           shut_off_computer = 1;
       	else
           shut_off_computer = 0;
    end
	
    always @(*) begin
        if (~arrived)
           keep_driving = ~gas_tank_empty;
        else
           keep_driving = 0;
    end

endmodule

运行结果:

分析:

        本题意为让人养成Verilog思维,Verilog本质上是电路,要先考虑电路的合理性,然后再写代码,比如 if 后面,一定要有else的调用,给电路一个备选项,以免造成死机

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: docker run --restart=always 的意思是在容器退出时自动重启容器,并且在Docker守护进程启动时启动容器。这个命令可以保证容器的持续运行,即使容器出现故障或崩溃也会自动重启。 ### 回答2: docker run --restart=always 是docker命令中的一个参数,作用是设置在docker服务异常退出时自动重启容器。 在容器运行过程中,可能会出现一些问导致容器异常退出,如应用程序或服务崩溃、内存溢出、网络故障等。如果不进行设置自动重启,这就需要手动对容器进行重启,否则服务将会中断,造成不必要的麻烦和时间浪费。 使用 --restart=always 参数可以在容器运行过程中自动重启,保证服务的不间断提供,提高了容器的可靠性和稳定性。无论是容器异常退出还是物理机或虚拟机故障,都能自动重启,保证应用可以及时恢复。同时,该参数还能让开发者更加便捷地进行容器的管理,提高了开发效率。 例如,使用命令 docker run --restart=always -d nginx ,启动nginx容器,如果在运行过程中nginx服务异常退出,docker服务会自动重启该容器,保证服务可以及时恢复,提高了应用程序的可靠性和稳定性。 总之,使用 docker run --restart=always 参数可以保证服务的可靠性,提高容器的稳定性和可靠性,为应用程序的开发和部署提供了保障。 ### 回答3: docker run --restart=always是一条Docker命令,用于在容器启动时自动重启容器。当容器运行出现故障或其他问导致容器停止,自动重启容器可以确保应用程序始终处于运行状态,保持高可用性。 该命令中的--restart参数有多个选项,其中最常见的是always。使用该选项时,Docker会在容器异常退出时自动重启容器,直到手动停止容器。 为了实现容器的自动重启,Docker会在后台运行一个重启策略。当Docker检测到容器退出时,该策略会尝试根据预定义的规则自动重启容器。 在使用Docker部署应用程序时,特别是在生产环境中,使用--restart=always是一个很好的实践,可以确保应用程序在任何情况下都可以正常运行。 总之,Docker的--restart选项是一个很实用的功能,能够确保应用程序的高可用性和稳定性。在使用该选项时,需要根据实际情况选择适当的重启策略,以确保应用程序的恢复能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刚及格的陆拾伍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值