Median Filter FSM Modelling
This looks like a mistake:

    else if(state == S0)
        BYP = 1;
        if(counter == 8)
            BYP = 0;


Note that the else that follows this gets matched up with the second if, not the first one!

A begin/end pair would fix this, but based on the indentation and the general structure of that block, you probably wanted something more like:

    else if (state == S0)
        BYP = (counter == 4'd8) ? 0 : 1;