0

对于数字设计课程的实验室,我正在设计部分 ALU。我已经定义了它的操作,并且需要使用 casez 语句来控制由四个 2- sel的位组合:

module eightbit_palu(input  wire[7:0] a, b,
                     input  wire[1:0] sel,
                     output wire[7:0] f,
                     output wire ovf);
                     
    wire[7:0] sum, bitInv, bitAnd, bitOr;
    wire sumOvf;
    
    assign sum = a + b;
    assign sumOvf = (a+b>127)||(a+b<-128) ? 1 : 0;
    assign bitInv = !b;
    assign bitAnd = a & b;
    assign bitOr = a | b;
    
    always @(a or b or sel) begin
        casez(sel)
            2'b00: f = sum; ovf = sumOvf;
            2'b01: f = bitInv; ovf = 0;
            2'b10: f = bitAnd; ovf = 0;
            2'b11: f = bitOr; ovf = 0;
        endcase
    end
    
endmodule

我已经在 always @ 中嵌入了 casez 语句,但我得到一个语法错误,我认为这可能是由于输出是连线;但我不确定如何解决这个看似简单的问题。我尝试将在分配语句之前声明的变量转换为 reg,但是将它们转换为连线时出现问题。真的很想了解 Verilog 的工作原理——不幸的是,我的教授正在使用 SystemVerilog 教学,但我们的实验室必须使用 Verilog!

谢谢。

4

1 回答 1

1

是的,您应该将输出线更改为,reg因为您正在对它们进行程序分配(在always块内)。

另一个错误是您需要在每个项目begin/end中的多个语句周围添加关键字。case这是为我编译干净的代码:

module eightbit_palu(input  wire[7:0] a, b,
                     input  wire[1:0] sel,
                     output reg [7:0] f,
                     output reg ovf);
                     
    wire[7:0] sum, bitInv, bitAnd, bitOr;
    wire sumOvf;
    
    assign sum = a + b;
    assign sumOvf = (a+b>127)||(a+b<-128) ? 1 : 0;
    assign bitInv = !b;
    assign bitAnd = a & b;
    assign bitOr = a | b;
    
    always @(a or b or sel) begin
        casez(sel)
            2'b00: begin f = sum;    ovf = sumOvf; end
            2'b01: begin f = bitInv; ovf = 0;      end
            2'b10: begin f = bitAnd; ovf = 0;      end
            2'b11: begin f = bitOr;  ovf = 0;      end
        endcase
    end
    
endmodule

此行也有问题:

always @(a or b or sel) begin

敏感度列表应仅包括块内分配的 RHS 上的信号。您的列表错误地包含aand b,并且它错过了其他类似sum. 您应该使用自动包含适当信号的隐式敏感度列表。

always @*
于 2020-10-05T15:41:50.187 回答