Case Statement Implementation

The multiplexor is now implemented using a case statement. This is a lot easier to understand, there are four assignments, each is made explicitly.

        module multiplexor4_1 (out, in1, in2, in3, in4, cntrl1, cntrl2);
           output out;
           input  in1, in2, in3, in4, cntrl1, cntrl2;
           reg      out;

           always @(in1 or in2 or in3 or in4 or cntrl1 or cntrl2)
              case ({cntrl1, cntrl2})
             2'b00 : out = in1;
             2'b01 : out = in2;
             2'b10 : out = in3;
             2'b11 : out = in4;
             default : $display("Please check control bits");
              endcase
        endmodule

The first three lines are the same as in the previous section.
        module multiplexor4_1 (out, in1, in2, in3, in4, cntrl1, cntrl2);
          output out;
          input in1, in2, in3, in4, cntrl1, cntrl2;
          reg out;

But now we have out defined as a register, this is because we are going to assign values to it explicitly and not drive them, this is called procedural assignment. A wire data type cannot be assigned to explicitly, it must have its value driven into it by a device (eg. another module, a gate, etc), called continuous assignment
        always @(in1 or in2 or in3 or in4 or cntrl1 or cntrl2)

If this is read as it is written, " always at [a change in] (in1 or in2 or...." it is clear what is going on. This is a construct containing statements which are only executed when any of the variables in the list of variables change. The list of variables is called the sensitivity list, because this construct is sensitive to their change. The keyword are always @( expr or expr ... );
              case ({cntrl2, cntrl1})
             2'b00 : out = in1;
             2'b01 : out = in2;
             2'b10 : out = in3;
             2'b11 : out = in4;
             default : $display("Please check control bits");
              endcase
        endmodule

This is a case statement indicated by the keyword case, it is similar to the case statement in C. The conditional is ({cntrl2,cntrl1}), the concatenation of cntr2 and cntr1 into a 2-bit number. The test values are 2'b00 etc, and the actions are out= in1; etc. It has a default if none of the tests are met, and ends with a endcase.

Note the difference in procedural and continuous assignments, here the control signals are tested and out is assigned a value accordingly.


previous next contents