Question

I am trying to design a simple 8-bit 2's complementor. Here is my code:

twos_complement_of_8bits.v

//`include "complementor.v"

module twos_complement_of_8bits(output [7:0] out, input [7:0] in);
integer i;
initial
  begin
    for(i = 0; i <= 7; i = i + 1)
        complementor C(out[i], in[i]);
   end
   assign out = out + 1;
endmodule

I got an error at this line:

complementor C(out[i], in[i]);
Syntax error near 'C' found.

How can I fix it?

Was it helpful?

Solution

I think you can eliminate your complementor module, then change your twos_complement_of_8bits as follows:

module twos_complement_of_8bits (output [7:0] out, input [7:0] in);
    assign out = ~in + 1;
endmodule

If that doesn't give you the output you want, please show some expected output values.

In more complicated situations, you can place arrays of instances of modules or use a generate block.

Here is an example of how to use a generate block:

module twos_complement_of_8bits (output [7:0] out, input [7:0] in);
    wire [7:0] out_ones;
    genvar i;
    generate
        for (i=0; i<=7; i=i+1) begin
            complementor C[i] (out_ones[i], in[i]);
        end
    endgenerate
    assign out = out_ones + 1;
endmodule
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top