Week8 Module8
Week8 Module8
COURSE INTENDED On the completion of the course, student is expected to be able to do the
LEARNING OUTCOMES: following:
II. OBJECTIVES: By the end of this module you should be able to:
III. INTRODUCTION:
The purpose of this module is to introduce a section of Verilog similar to
the “Switch” statement in programming. This would help the students in
manipulation of conditions using selection process.
IV. CONTENTS:
Lesson Coverage:
- Selection Statements
INTRODUCTION
CASE STATEMENTS
The Verilog Case Statement works exactly the way that a switch statement in C works. Given an input,
the statement looks at each possible condition to find one that the input signal satisfies. They are useful
to check one input signal against many combinations.
Just like in C, the VHDL designer should always specify a default condition provided that none of the
case statements are chosen. This is done via the "default: " statement.
One thing to note with case statements is that Verilog does not allow the use of less than or greater
than relational operators in the test condition. Only values that are equal to the signal in the case test
can be used.
Note that the example below uses the brackets for concatenation.
endmodule // case_statement
Syntax
The case statement compares an expression to a series of cases and executes the statement or
statement group associated with the first matching case:
case ()
< case1 > : < statement >
< case2 > : < statement >
.....
default : < statement >
endcase
14 2'bxx,2'bx0,2'bx1,2'b0x,2'b1x,
15 2'bzz,2'bz0,2'bz1,2'b0z,2'b1z : $display("Error in SEL");
16 endcase
17
18 endmodule
The example above shows how to specify multiple case items as a single case item.
The Verilog case statement does an identity comparison (like the === operator); one can use the case
statement to check for logic x and z values as shown in the example below.
1 module case_xz(enable);
2 input enable;
3
4 always @ (enable)
5 case(enable)
6 1'bz : $display ("enable is floating");
7 1'bx : $display ("enable is unknown");
8 default : $display ("enable is %b",enable);
9 endcase
10
11 endmodule
Special versions of the case statement allow the x ad z logic values to be used as "don't care":
• casez : Treats z as don't care.
• casex : Treats x and z as don't care.
Try ME.
1 module casez_example();
2 reg [3:0] opcode;
3 reg [1:0] a,b,c;
4 reg [1:0] out;
5
6 always @ (opcode or a or b or c)
7 casez(opcode)
8 4'b1zzx : begin // Don't care about lower 2:1 bit, bit 0 match with x
9 out = a;
10 $display("@%0dns 4'b1zzx is selected, opcode %b",$time,opcode);
11 end
12 4'b01?? : begin
13 out = b; // bit 1:0 is don't care
14 $display("@%0dns 4'b01?? is selected, opcode %b",$time,opcode);
15 end
16 4'b001? : begin // bit 0 is don't care
17 out = c;
18 $display("@%0dns 4'b001? is selected, opcode %b",$time,opcode);
19 end
20 default : begin
21 $display("@%0dns default is selected, opcode %b",$time,opcode);
22 end
23 endcase
24
25 // Testbench code goes here
26 always #2 a = $random;
27 always #2 b = $random;
28 always #2 c = $random;
29
30 initial begin
31 opcode = 0;
32 #2 opcode = 4'b101x;
33 #2 opcode = 4'b0101;
34 #2 opcode = 4'b0010;
35 #2 opcode = 4'b0000;
36 #2 $finish;
37 end
38
39 endmodule
Try me 2:
1 module casex_example();
2 reg [3:0] opcode;
3 reg [1:0] a,b,c;
4 reg [1:0] out;
5
6 always @ (opcode or a or b or c)
7 casex(opcode)
8 4'b1zzx : begin // Don't care 2:0 bits
9 out = a;
10 $display("@%0dns 4'b1zzx is selected, opcode %b",$time,opcode);
11 end
12 4'b01?? : begin // bit 1:0 is don't care
13 out = b;
14 $display("@%0dns 4'b01?? is selected, opcode %b",$time,opcode);
15 end
16 4'b001? : begin // bit 0 is don't care
17 out = c;
18 $display("@%0dns 4'b001? is selected, opcode %b",$time,opcode);
19 end
20 default : begin
21 $display("@%0dns default is selected, opcode %b",$time,opcode);
22 end
23 endcase
24
25 // Testbench code goes here
26 always #2 a = $random;
27 always #2 b = $random;
28 always #2 c = $random;
29
30 initial begin
31 opcode = 0;
32 #2 opcode = 4'b101x;
33 #2 opcode = 4'b0101;
34 #2 opcode = 4'b0010;
35 #2 opcode = 4'b0000;
36 #2 $finish;
37 end
38
39 endmodule
To avoid confusion and to see the process on those three, let’s have another one.
1 module case_compare;
2
3 reg sel;
4
5 initial begin
6 #1 $display ("\n Driving 0");
7 sel = 0;
8 #1 $display ("\n Driving 1");
9 sel = 1;
10 #1 $display ("\n Driving x");
11 sel = 1'bx;
12 #1 $display ("\n Driving z");
13 sel = 1'bz;
14 #1 $finish;
15 end
16
17 always @ (sel)
18 case (sel)
19 1'b0 : $display("Normal : Logic 0 on sel");
20 1'b1 : $display("Normal : Logic 1 on sel");
Driving 0
Normal : Logic 0 on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
Driving 1
Normal : Logic 1 on sel
CASEX : Logic 1 on sel
CASEZ : Logic 1 on sel
Driving x
Normal : Logic x on sel
CASEX : Logic 0 on sel
CASEZ : Logic x on sel
Driving z
Normal : Logic z on sel
CASEX : Logic 0 on sel
CASEZ : Logic 0 on sel
The case statement starts with a case or casex or casez keyword followed by the case expression (in
parenthesis) and case items or default statement. It ends with the endcase keyword. The default
statement is optional and should be used only once. A case item contains a list of one or more case
item expressions, separated by comma, and the case item statement. The case item expression and
the case item statement should be separated by a colon.
During the evaluation of the case statement, all case item expressions are evaluated and compared in
the order in which they are given. If the first case item expression matches the case expression, then
the statement which is associated with that expression is executed and the execution of the case
statement is terminated. If comparison fails, then the next case item expression is evaluated and
compared with the case expression. If all comparisons fail and the default section is given, then its
statements are executed. Otherwise none of the case items will be executed.
Both case expression and case item expressions should have the same bit length. None of the
expressions are required to be a constant expression.
The case expression comparison is effective when all compared bits are identical. Therefore, special
types of case statement are provided, which can contain don't-care values in the case expression and
in the case item expression. These statements can be used in the same way as the case statement,
but they begin with the keywords casex and casez.
The casez statement treats high-impedance (z) values as don't-care values and the casex statement
treats high-impedance and unknown (x) values as don't care values. If any of the bits in the case
expression or case item expression is a don't-care value then that bit position will be ignored.
The don't-care value can be also specified by the question mark (?), which is equal to z value.
Example 1
If the address value is 2'b00 then statement1 will be executed. Statement2 is executed when address
value equals 2'b01 or 2'b10. Otherwise statement3 is executed.
Example 2
reg a;
case (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
In Example 2, the statements will be executed depending on the value of the 'a' variable (if a = 1'b0
then statement1 will be executed, etc). If we assign a question mark (?) to the 'a' variable, then
statement4 will be executed because the syntax concerning numbers defines the question mark as
equal to the z value.
Example 3
reg a;
casez (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
If value of variable 'a' is 1'b0 or 1'b1 or 1'bx then statement1, statement2 or statement3 will be executed
respectively. If 'a' equals 1'bz or 1'b? then statement1 will be executed because the casez statement
treats z and ? as the don't-care values. Statement4 will never be executed because only the first case
item, which matches with the case expression, is executed.
Example 4
reg a;
casex (a)
1'b0 : statement1;
1'b1 : statement2;
1'bx : statement3;
1'bz : statement4;
endcase
If variable 'a' is 1'b0 or 1'b1 then statement1 and statement2 will be executed respectively. If 'a' equals
1'bx or 1'bz or 1'b? then statement1 will be executed (x, z and ? are don't care values for the casex
statement). Statement3 and statement4 will never be executed.
Example 5
reg a;
case (1'b1)
a : statement1;
endcase
The case expression can be a constant expression. In Example 5, statement1 will be executed only if
'a' is equal to 1'b1.
V. REFERENCES: Roth, C.H. Jr. And John, L. K. (2018). Digital Systems Design Using VHDL (3rd
ed.). Texas, USA: Cengage Unlimited