我正在 HDLBits 上尝试关于边缘检测的 Verilog 练习。
但是,我很困惑,无法理解使用
if ({d_last, in} == 2'b01)
和 pedge <= in & ~d_last
之间的不同行为?我的回答:
module top_module (
input clk,
input [7:0] in,
output [7:0] pedge
);
reg [7:0] d_last;
always @(posedge clk) begin
d_last <= in;
if ({d_last, in} == 2'b01)
pedge <= 1;
else
pedge <= 0;
end
endmodule
警告信息:
警告 (13024):输出引脚卡在 VCC 或 GND
此警告表示输出引脚永远不会改变(“卡住”)。如果输出引脚不应该是常量,这有时可能表明存在错误。如果该引脚不应该保持不变,请检查是否有导致分配的值永远不会改变的错误(例如,
assign a = x & ~x;
)。
HDLBits 解决方案:
module top_module(
input clk,
input [7:0] in,
output reg [7:0] pedge);
reg [7:0] d_last;
always @(posedge clk) begin
d_last <= in; // Remember the state of the previous cycle
pedge <= in & ~d_last; // A positive edge occurred if input was 0 and is now 1.
end
endmodule
您的答案与 HDLBits 解决方案非常不同。更详细的
if/else
写法是:
if ({d_last, in} == 2'b01)
pedge[7:0] <= 8'b0000_0001;
else
pedge[7:0] <= 8'b0000_0000;
如您所见,7 位向量
pedge[7:1]
始终为 7'b0000_000
。 pedge
的 7 位始终为 0。这就是警告消息的原因。
此外,您的
if
状况可能并不像您想象的那样。另一种写法是:
if ({d_last[7:0], in[7:0]} == 2'b01)
位宽不匹配:LHS 是 16 位,但 RHS 只有 2 位。