我知道VHDL,现在我尝试做一些verilog。我有两个文件,一个包含一个计数器,另一个包含一个32位全加器。
Kounter.v:
module counter (
input clk,
input enable,
input reset,
output reg [3:0] count
);
wire [31:0] temp2 = 0;
reg [31:0] clk_count = 0;
wire [31:0] test = 32'b1;
parameter integer number_of_clk_cycles = 15;
adder adder1(clk_count, test, temp2);
always @(posedge clk) begin
if (reset) begin
count = 0;
end else if (enable) begin
clk_count <= temp2;
if(clk_count == number_of_clk_cycles) begin
count <= count + 1;
clk_count <= 0;
end
end
end
endmodule
Adder.v:
module adder(
input [31:0] a,
input [31:0] b,
output [31:0] c
);
wire [32:0] cin; //The internal Carry signal
assign cin[0] = 0; //Force the carry line to 0, since the first adder has no carry
genvar i;
for(i=0; i<32;i = i + 1) begin
fa fa1( a[i], b[i], cin[i], c[i], cin[i+1]);
end
endmodule
module ha( a, b, s, c);
input a, b;
output s, c;
xor xor1(s ,a, b); //Output first, then inputs
and and1(c, a ,b); //Output first, then inputs
endmodule
module fa (a , b, cin, s, cout);
input a, b, cin;
output s, cout;
ha ha1(a, b, ha1_sum, ha1_cout); //Half adder 1
ha ha2(ha1_sum, cin, s, ha2_cout); //Half adder 2
or or1(cout, ha1_cout, ha2_cout); //Carry out
endmodule
我在ModelSIM中验证了我的完整加法器代码,它始终有效。但是当我尝试运行counter.v代码时,输出adder1总是'X'(不关心)。如果启用设置为“1”,则不关心将通过加法器(clk_count <= temp1;
)波动。我错过了什么?
在verilog中,如果您使用不同的非z值驱动相同的wire
,则结果值将为x
。
在你的情况下,你驱动temp2两次。第一次来这里:
wire [31:0] temp2 = 0;
这相当于
wire [31:0] temp2;
assign temp2 = 0;
第二次,加法器的输出。
因此,如果加法器的输出产生的值不为零,则temp2
将变为x
,否则将为0
。
所以,不要将0
分配给它。