Search code examples
verilogsystem-verilog

Verilog input and output array


This Verilog code doesn't compile.

module two_to_one_com(SW,LEDR,LEDG);

input [15:0]SW ;
input SW17;
output [7:0]LEDR;
output [7:0]LEDG;

assign X = SW[7:0];
assign Y = SW[15:8];
assign s = SW17;
assign LEDR[7:0] = SW;
assign M = LEDG[7:0];

integer index = 0;
initial
begin
for(index=0;index<=7;index = index+1)
    begin
    assign M[index] = (~s &X[index])|(s&Y[index]);
    end
end

endmodule

It just keeps telling me that arrays M, X and Y can't be indexed as they are undeclared. Does anyone know a solution?


Solution

  • You need to declare all signals with some type and a width (if greater than 1 bit); assign is not a declaration key word. For example, I use wire:

    module two_to_one_com(SW,LEDR,LEDG,SW17);
    
    input [15:0]SW ;
    input SW17;
    output [7:0]LEDR;
    output [7:0]LEDG;
    
    wire [7:0] X = SW[7:0];
    wire [7:0] Y = SW[15:8];
    wire s = SW17;
    

    Note that I also added SW17 to the port list.

    I am still getting complaints about M, but I need to know how wide it should be (# of bits).