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

How to assign a register to an output in 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,抱歉打错了

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

你可能打算写这样的东西:

reg[7:0] temp;

always @(posedge CLK)
begin
    if (En)
    begin
        if (CLR)
            temp <= 0;
        else
            temp <= in;
    end
end

assign out = temp;

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

你可能打算写

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

assign 语句的 LHS 应该始终是连线。您已将 out 声明为 reg,最好在 always 块内的 LHS 中使用 reg 数据类型。