You don't need any bidirectional signals here, you just need to redefine your ports a bit. With system verilog you could pass multidimensional arrays as ports which would make this code a bit more compact. You could also create structs to contain the 3 inputs and 1 output in one object. However, this is a verilog question so we will do it that way:
module coder(
selCh0, selCh1, selCh2, selCh3,
outCh0, outCh1, outCh2, outCh3,
inCh0, inCh1, inCh2, inCh3,
inEnc0, inEnc1, inEnc2, inEnc3,
outEnc0, outEnc1, outEnc2, outEnc3
);
input [1:0] selCh0, selCh1, selCh2, selCh3;
input [2:0] inEnc0, inEnc1, inEnc2, inEnc3;
output outEnc0, outEnc1, outEnc2, outEnc3;
output [2:0] outCh0, outCh1, outCh2, outCh3;
input inCh0, inCh1, inCh2, inCh3;
wire [2:0] inEnc [0:3];
wire inCh [0:3];
assign inEnc[0] = inEnc0;
assign inEnc[1] = inEnc1;
assign inEnc[2] = inEnc2;
assign inEnc[3] = inEnc3;
assign inCh[0] = inCh0;
assign inCh[1] = inCh1;
assign inCh[2] = inCh2;
assign inCh[3] = inCh3;
assign outCh0 = inEnc[selCh0];
assign outCh1 = inEnc[selCh1];
assign outCh2 = inEnc[selCh2];
assign outCh3 = inEnc[selCh3];
assign outEnc0 = inCh[selCh0];
assign outEnc1 = inCh[selCh1];
assign outEnc2 = inCh[selCh2];
assign outEnc3 = inCh[selCh3];
endmodule
Here is it repeated with arrays as ports in system verilog. This is also parameterized for flexibility in # of channels. Since the number of channels might not be a power of 2, we have to check that selCh is a legal value. If not, assign a default value to the outputs. The $clog2
function is used to calculate the minimum number of bits needed to select from NUMCH
inputs:
module coder #(parameter NUMCH=4) (
input [$clog2(NUMCH)-1:0] selCh[NUMCH],
input [2:0] inEnc[NUMCH],
output reg outEnc[NUMCH],
output reg [2:0] outCh[NUMCH],
input inCh[NUMCH]
);
always_comb begin
for (int i=0; i<NUMCH; i++) begin
if(selCh[i]<NUMCH) begin
outCh[i] = inEnc[selCh[i]];
outEnc[i] = inCh[selCh[i]];
end
else begin
outCh[i] = '0;
outEnc[i] = '0;
end
end
end
endmodule