0

I'm trying to learn verilog, and I don't understand what is wrong when using the for loop.

In the example module I'm trying to implement, I'm going to set the first bit of the output based on the inputs, while the remaining bits of the output are going to be set based on both the input, and the previous bit of the output-

module example_module(A,B,C);
input [15:0] A;
input [15:0] B;
output [15:0] C;
reg[15:0] C;
integer i;
always@(A or B)begin
  C[0] = A[0]&B[0];
  for(i = 0;i<15;i=i+1)begin
    C[i+1] = A[i+1]|(B[i+1]&C[i]) ;
  end

end
endmodule

For the testbench-

`timescale 1ns/1ns
module example_module_tb;
reg[15:0] A;
reg[15:0] B;
wire [15:0] C;

example_module e(A,B,C);
initial begin
A = 16'd23;
B = 16'd32;
end

initial begin
 $dumpfile("example_module_tb.vcd");
 $dumpvars;
end

initial #400 $finish;

endmodule

When I run this on veriwell, and then use GTK wave, I get the output wire C to be 16'bxxxxxxxxxxxxxxx0, basically the first bit is set, but the others are not?

Could someone please explain how to fix this?

Thanks!

EDIT: Here are the actual 3 files I'm using, but I'm getting xxx for the CLA_logic_tb.v!

GP_generator.v

module GP_generator(A,B,G,P);
input [15:0] A;
input [15:0] B;

output [15:0] G;
output [15:0] P;

reg [15:0] G;
reg [15:0] P;

integer i;

always@(A or B)
begin 
for(i = 0; i<16; i= i +1)begin
  G[i] = A[i]&B[i];
  P[i] = A[i]^B[i];
 end
end 
endmodule

CLA_logic.v

module CLA_logic(A,B,C);
input [15:0] A;
input [15:0] B;
output [15:0] C;

reg[15:0] C;


wire [15:0] G;
wire[15:0] P;

GP_generator g1(A,B,G,P);

integer i;


always@(A or B)begin
 C[0] = A[0]&B[0];
for(i = 0;i<15;i=i+1)begin
 C[i+1] = G[i]|(P[i]&C[i]) ;
 end

 end
endmodule

CLA_logic_tb.v

`timescale 1ns/1ns
module CLA_logic_tb;
reg[15:0] A;
reg[15:0] B;
wire [15:0] C;

CLA_logic c(A,B,C);

initial begin
    A = 16'd23;
    B = 16'd32;
end

initial begin
     $dumpfile("CLA_logic_tb.vcd");
     $dumpvars;
end

initial #400 $finish;

endmodule

The first bit of C is set, but the rest give xxx

2
  • Ran fine on edaplayground.com with VeriWell 2.8.7 and all other simulators. Commented Jun 13, 2017 at 23:27
  • Hey! Yea you're right! But when I try to extend it (please refer to my edited question) I still get xxxx for the output. Would you know why? Commented Jun 14, 2017 at 8:04

1 Answer 1

0

Hello to anyone still looking,

The error is in the always block!

instead of

always @(A or B)

it should include all the variables used! So it should be

always @(A or B or G or P)
Sign up to request clarification or add additional context in comments.

4 Comments

If you were not using VeriWell, which only supports Verilog-1995 (last I checked), a better solution is to use always @* (or the synonymous always @(*)) from Verilog-2001 or higher for auto sensitivity. The text editor Emacs does have a work around with its autos generation, see /*AS*/ at veripool.org/wiki/verilog-mode ; it can be used in batch mode from command line or your editor of choice (assuming the editor can run scripts and terminal commands).
Did a little extra research. The original VeriWell is limited to Verilog-1995. But there is a forked version on github (github.com/balanx/veriwell) called VeriWell 3 that does support some Verilog-2001 features, according to the change log.
Hey Greg thanks for your answer! Probably a really stupid question, but how do I get the forked version into my computer?
I provided a link to the github page that has instruction in my previous comment. You may also want to look into Icarus Verilog (main site and github page) as an alternative free simulator. If you are in school, then I recommend finding out if there is a deal to get a free or discounted simulator from Cadence, Synopsys, Mentor, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.