-1

我正在尝试为verilog写下ALU。我遇到了几个错误。

首先,这是我的代码:

module yAlu(z, ex, a, b, op);

input [31:0] a, b;
input [2:0] op;
output [31:0] z, ztemp;
output ex; 
wire[31:0]a0,a1,a2,a3,a4, atemp;

assign slt = 0; 
assign ex = 0; 

assign a0 = a & b;
assign a1 = a | b;
assign a2 = a + b;
assign a3 = a - b;
assign a4 = a[31] ^ b[31];

yMux #(32) lo(zLo, a0, a1, op[0]);  
yMux #(32) hi(zHi, a2, a3, op[0]);
yMux #(32) temp(atemp, zLo, zHi, op[1]);
assign z = (op[2] == 1) ? a4 : atemp; 
assign slt = z;

endmodule

yAlu.v 使用以下内容:

module yMux(z, a, b, c);
parameter SIZE = 2;
output [SIZE-1:0] z;
input [SIZE-1:0] a, b;
input c;
yMux1 mine[SIZE-1:0](z, a, b, c); // 2-­bit  2 -­to-­1  mux  and  it  would  be  cumbersome  to  write  32  mux  instantiation  lines.
endmodule

最后在 yMux 上面使用以下内容:

module yMux1(z, a, b, c);
output z;
input a, b, c;
wire notC, upper, lower;
// Gates and interconnections for MUX

// if c is 0, z=a.
// if c is 1, z=b
not my_not(notC, c);
and upperAnd(upper, a, notC);
and lowerAnd(lower, c, b);
or my_or(z, upper, lower);
endmodule 

这是它在 yAlu 上测试的内容:

module lab8;

reg [31:0] a, b;
reg [31:0] expect;
reg [2:0] op;
wire ex;
wire [31:0] z;
reg ok, flag;
yAlu mine(z, ex, a, b, op);

initial begin

repeat (10) begin

a = $random; b = $random;

if(op==0)
expect = a & b;
else if (op==1)
expect = a | b;
else if (op==2)
expect = a + b;
else if (op==3)
expect = a - b;
else if (op==4)
expect = (a < b) ? 1 : 0;

#1;

if(expect == z)     
$display("PASS : expected=%d, a=%d, b=%d, z=%d, op=%d", expect,a,b,z,op);

#1;

$finish;  
end 
end
endmodule

我的问题顺序如下:

Question 1. 

上面的代码只适用于 0 和 1。它的作用不止于此。例如,在第二个源代码中,有

a = $随机; b = $随机;

它不适合这个。它仅在 a=1 或 0 和 b=1 或 0 时有效。

Question 2. 

我不确定“slt”功能是否正常工作。教这个的老师从来没有告诉我 slt 在讲座中做了什么,而是让我们通过谷歌搜索或其他方式设计 slt。

Question 3. 

每当我编译时,我都会收到以下错误。为什么是这样?

    yAlu.v:38: warning: Port 1 (z) of yMux expects 32 bits, got 1.
    yAlu.v:38:        : Padding 31 high bits of the port.
    yAlu.v:39: warning: Port 1 (z) of yMux expects 32 bits, got 1.
    yAlu.v:39:        : Padding 31 high bits of the port.
    yAlu.v:40: warning: Port 2 (a) of yMux expects 32 bits, got 1.
    yAlu.v:40:        : Padding 31 high bits of the port.
    yAlu.v:40: warning: Port 3 (b) of yMux expects 32 bits, got 1.
    yAlu.v:40:        : Padding 31 high bits of the port.

I can't fix this at all.

我什至不知道我做得对。指导我做它所说的手册没有足够的解释,也没有样本输出。

反正我没能及时完成,所以我想这没关系,但我想我必须知道我的问题的解决方案。

如果你能帮助我,非常感谢。

4

1 回答 1

3

如警告所示,您的端口连接宽度不匹配。参考单一警告,其他同理。

  yAlu.v:38: warning: Port 1 (z) of yMux expects 32 bits, got 1.
    yAlu.v:38:        : Padding 31 high bits of the port.

该模块声明了由参数定义的端口、a宽度b和宽度。czSIZE

module yMux(z, a, b, c);
parameter SIZE = 2;
output [SIZE-1:0] z;
input [SIZE-1:0] a, b;

此外,在实例化时SIZE会被覆盖。现在, 的值为SIZE32。因此,、和中的每一个的宽度都是32abcz

yMux #(32) lo(zLo, a0, a1, op[0]);  
yMux #(32) hi(zHi, a2, a3, op[0]);
yMux #(32) temp(atemp, zLo, zHi, op[1]);

此处,zLozHi声明直接端口连接中使用 ports 。

参考IEEE 1800-2012,第 6.10 节-隐式声明:

如果在端口表达式声明中使用标识符,则应假定默认网络类型的隐式网络,以及端口表达式声明的向量宽度。

如果将未声明的标识符用作与实例的连接,则会推断出隐式网络。

因此,zLo式声明zHi为单比特网络其余 32 位零填充。只需将它们声明如下,所有警告都将被删除

wire [31:0] zLo,zHi;

要在这种情况下出错default_nettype none,请使用编译器指令。

有关更多信息,请参阅Sutherland SV Gotchas论文第 2.2 节和SystemVerilog IEEE 1800-2012以获取隐式声明。

于 2016-03-21T05:29:31.257 回答