我们的目标是使用一个ALU和一个带有使能和复位功能的寄存器将一个12位的数字递增2。
主要的问题是,在我的 Q
应该是增量器的输出,我得到了这个错误。
incrementor_tb.v:14: warning: incrementor_tb.v:14: 警告:增量器的端口7(Q)期望12位,得到1位。填充了端口的11个高位。
除此之外,它还不能工作;当我尝试显示我的 Q
.
请记住,我不知道我在做什么。所以可能有很多错误。最主要的是我想不通的是,为什么当我挂上了 Q
到输出 f
的 alu
它应该是投入的总和 a
和 b
都是12位),我得到的答案是1位。Alu我已经测试过了,可以用。寄存器应该也能用。
我有理由相信,我设置选区的方式和? b
在我 alu
可能是错误的。我试着把比特直接传给 s
在 alu
在增量器中的实例化中,如 .s(3'b000)
但它没有工作。 我不知道这是否被允许,所以我把它改成了现在的样子,但还是得到了错误。
这里是文件。
//12 bit alu
module alu(
input [11:0] a,
input [11:0] b,
input [2:0] s,
output [11:0] f,
output take_branch,
output ovf
);
reg [11:0] f;
reg take_branch;
reg ovf;
always @(s, a, b)
case(s)
3'b000: begin // was add a and b
take_branch = 0;
f = a + b;
ovf = (a[7] & b[7] & ~f[7]) | (~a[7] & ~b[7] & f[7]);
end
3'b001: begin //was bitwise inverse
take_branch = 0;
f = ~b;
ovf = 0;
end
3'b010: begin //bitwise and of a and b
take_branch = 0;
f = a & b;
ovf = 0;
end
3'b011: begin //bitwise or of a and b
take_branch = 0;
f = a | b;
ovf = 0;
end
3'b100: begin //arithmetic shift right
take_branch = 0;
f = a >>> 1;
ovf = 0;
end
3'b101: begin //logical shift left
take_branch = 0;
f = a << 1;
ovf = 0;
end
3'b110: begin //branch if equal
take_branch = (a == b);
f = 0;
ovf = 0;
end
3'b111: begin //branch not equal
take_branch = (a != b);
f = 0;
ovf = 0;
end
default: begin
take_branch = 0;
f = 0;
ovf = 0;
end
endcase
endmodule
注册
// 12 bit registyer with enable
module register (
input clk,
input reset,
input en,
input [11:0] d,
output [11:0] out
);
reg out;
//async reset, synchronoyus enable
always @ (negedge clk, negedge reset)
if (reset) out <= 12'b000000000000;
else if (en) out <= d;
endmodule
增量器
//increment by 2
module incrementor(baseNumber, clk, clear, inc, sel, incBy, Q);
input [11:0] baseNumber;
input clk;
input clear;
input inc;
input [2:0] sel;
input [11:0] incBy;
output [11:0] Q;
wire [11:0] reg_out;
//wire [11:0] Q;
//reg ovf;
//instantiate alu
alu myalu (
.a(reg_out), //in to out
.b(incBy),
.s(sel),
.f(Q),
.take_branch(),
.ovf()
);
//instantiate register
register myregister (
.clk(clk),
.reset(clear),
.en(inc),
.d(baseNumber),
.out(reg_out)
);
//assign q behavior
//assign q = q;
endmodule
最后是testbench文件
module incrementor_tb();
reg [11:0] baseNumber;
reg clear;
reg clk;
reg inc;
parameter [2:0] sel = 000;
parameter [11:0] incBy = 12'b000000000010;
wire Q;
incrementor uut (
.baseNumber(baseNumber),
.clk(clk),
.clear(clear),
.inc(inc),
.sel(sel),
.incBy(incBy),
.Q(Q)
);
initial
begin
clk=1'b0;
repeat (20)
#5 clk = ~clk;
end
initial
begin
#0 clear = 1'b1;
baseNumber = 12'b000000000001;
inc = 1'b1;
clear = 0;
$display(Q);
#100 clear = 1'b0;
end
initial
$monitor("time = %0d: Q = %1h", $time, Q);
endmodule
在我的Q上,应该是增量器的输出,我得到的错误是:... incrementor_tb
,改。
wire Q;
改为:
wire [11:0] Q;
范围说明器([11:0]
)就是宣称 Q
作为一个12位信号。 当你没有明确使用指定器时。Q
是一个1位信号。
正如你所猜测的那样,你的代码还有其他几个基本的Verilog问题,但完整的代码评论是Stack Overflow的题外话。