Search code examples
verilogsystem-verilogfpga

Binary to Grey Code and Grey to Binary using mode switch


I am implementing Code converter with a mode switch such that mode 0 implies binary to grey code and mode 1 implies grey to binary conversion. My design and testbench is as shown below.

Design


    module bin2gray(input [3:0] bin, output [3:0] G );
    
    assign G[3] = bin[3];
    assign G[2] = bin[3] ^ bin[2];
    assign G[1] = bin[2] ^ bin[1];
    assign G[0] = bin[1] ^ bin[0];
    
    endmodule
    
    module gray2bin (input [3:0] G, output [3:0] bin );
    
    assign bin[3] = G[3];
    assign bin[2] = G[3] ^ G[2];
    assign bin[1] = G[3] ^ G[2] ^ G[1];
    assign bin[0] = G[3] ^ G[2] ^ G[1] ^ G[0];
    
    endmodule
    
    module code_converter(in,mode_switch,out);
    input [3:0]in;
    input mode_switch;
    output [3:0] out;
    always @(in or mode_switch);
    if(mode_switch == 1'b1)
        gray2bin m2(in,out);
    else
        bin2gray m1(in,out);
    endmodule

TestBench


    module tb();
    
        reg [3:0] in;
        reg mode_switch;
        wire [3:0] out;
     
    code_converter uut(.in(in),.mode_switch(mode_switch),.out(out));
        
       // stimulus
       initial  begin
    
        mode_switch <=0;       
            in <= 0; #10;
            in <= 1;   #10;
            in <= 2;   #10;
            in <= 3;   #10;
            in <= 4;   #10;
            in <= 5;   #10;
            in <= 6;   #10;
            in <= 7;   #10;
            in <= 8;   #10;
            in <= 9;   #10;
            in <= 10;  #10;
            in <= 11;  #10;
            in <= 12;  #10;
            in <= 13;  #10;
            in <= 14;  #10;
            in <= 15;  #10;
            #100;   
    
        mode_switch <=1;
            in <= 0; #10;
            in <= 1;   #10;
            in <= 2;   #10;
            in <= 3;   #10;
            in <= 4;   #10;
            in <= 5;   #10;
            in <= 6;   #10;
            in <= 7;   #10;
            in <= 8;   #10;
            in <= 9;   #10;
            in <= 10;  #10;
            in <= 11;  #10;
            in <= 12;  #10;
            in <= 13;  #10;
            in <= 14;  #10;
            in <= 15;  #10;
            #100;
          $stop;
       end
    initial begin
    $dumpvars;
    $dumpfile("sth.vcd");
    end
    endmodule

I am getting the following syntax error while compiling the code.

Logs


    vu2swz@PPDP01:~$ iverilog codeconverter.v 
    codeconverter.v:24: error: Unable to bind parameter `mode_switch' in `tb.uut'
    codeconverter.v:24: error: Cannot evaluate genvar conditional expression: (mode_switch)==(1'd1)
    codeconverter.v:24: error: Unable to bind parameter `mode_switch' in `tb.uut'
    codeconverter.v:24: error: Cannot evaluate genvar conditional expression: (mode_switch)==(1'd1)

4 error(s) during elaboration.

Is there any problem with the design and syntax?


Solution

  • The problem is that you can not instantiate modules conditionally based on a signal value at runtime.

    You can add both instances, then choose the desired instance output based on the switch signal.

    module code_converter(in,mode_switch,out);
        input [3:0]in;
        input mode_switch;
        output [3:0] out;
        wire [3:0] out_b2g;
        wire [3:0] out_g2b;
    
        assign out = (mode_switch) ? out_g2b : out_b2g;
        gray2bin m2 (in, out_g2b);
        bin2gray m1 (in, out_b2g);
    endmodule