Branch Statements

The case statement.

The above method for implementing the multiplexor already looks hard to follow, if there are more than 4 input lines it would become almost impossible to write out the correct if statement. A clearer implementation uses a case statement (this is very similar in syntax to the case statement in C).

Syntax:
conditional::== case (condition) case_item+ endcase
case_item::==
expression+ (seperated by commas) : statement* |
default : statement*

An implementation of the 4 to 1 multiplexor using the case statement.


        module multiplexor4_1 (out, in1, in2, in3, in4, cntrl1, cntrl2);
          output out;
          input in1, in2, in3, in4, cntrl1, cntrl2;
          reg out; // note must be a register

          always @(in1 | in2 | in3 | in4 | cntrl1 | cntrl2)
             case ({cntrl2, cntrl1}) // concatenation
                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 stimulus block is the same as the one for the above multiplexor descriptions.

Variants of the case statement are casez and casex. Whereas the case statement compares the expression to the condition bit by bit, insuring the 0, 1, x, and zs match, the casez treats all the zs in the condition and expression as ?s, ie irrelevants. The casex similarly treats all the xs and zs as ?s. These alteratives, if not used carefuly and sparingly can easily lead to bugs.

EXERCISE

What is the output from the following:

        module testCase(value);
           input [4:0] value;

           always @(value)
              casez (value)
                5'b010zx : $display("%b: Matched OK", value);
                default : $display("%b: Did not match", value);
              endcase // casez (value)

        endmodule // testCase

        module runTest;
           reg [4:0] value;

           testCase mod1 (value);

           initial begin
              #10 value = 5'bzz01x;
              #10 value = 5'bxxxzx;
              #10 value = 5'b010xz;
              #10 value = 5'bzzz0z;
              #10 $finish;
           end // initial begin
        endmodule // runTest

Answers


previous next contents