如何在 verilog 中将寄存器分配给输出



我很难弄清楚如何将 temp 的值分配给 out。 我在网上搜索了答案并尝试了各种方法,但仍然无法分配输出。 代码如下:

module Reg8bit(
    input CLK,
    input En,
    input CLR,
    input [7:0] in,
    output [7:0] out
    );
    reg[7:0] temp;
    always @(posedge CLK)
    begin
        if (En)
        begin
            if (CLR)
                out = 8'b0000_0000;
            else
                out = in;
        end
    end
    assign out = tempQ;
endmodule

编辑:temp应该是tempQ,对不起错别字

你可能想写

module Reg8bit(
    input CLK,
    input En,
    input CLR,
    input [7:0] in,
    output reg [7:0] out // out is a variable, not a wire
    );
    always @(posedge CLK)
    begin
        if (En)
        begin
            if (CLR)
                out <= 8'b0000_0000; // use Non-blocking assignments
            else
                out <= in;
        end
    end
endmodule

你的代码没有多大意义。您正在分配两次,并且不使用临时寄存器。

你可能想写这样的东西:

reg[7:0] temp;
always @(posedge CLK)
begin
    if (En)
    begin
        if (CLR)
            temp <= 0;
        else
            temp <= in;
    end
end
assign out = temp;

通常(并非总是)被认为是在始终块中使用非阻塞分配的良好做法。我认为在这种情况下,您甚至可以在没有临时寄存器的情况下执行此操作。

assign 语句的 LHS 应始终是一根导线。您已经声明为 reg,最好在 always 块内的 LHS 中使用 reg 数据类型。

最新更新