what is the difference between casex
and casez
in Verilog
?
I have searched about it and find this sentence :
casez treats all z values in the case alternatives or the case expression as don't cares. All bit positions with z can also represented by ? in that position.
casex treats all x and z values in the case item or the case expression as don't
cares.
for example , what is the difference between first one and second one:
1-
casez (instr)
7'b1zzzzzzz: // arithmetic
7'b01zzzzzz: // load-reg
7'b00zzzzzz: // store-reg
endcase
2-
casex (instr)
7'b1zxxxxzz: // arithmetic
7'b01zzxxxx: // load-reg
7'b00xxxzzz: // store-reg
endcase
The Verilog Language Reference Manual (now replaced by the SystemVerilog LRM) explains this in great detail. The key difference is when the case expression
instr
contains x or z values. Remember that bothcasex
andcasez
look at both the case item and the case expression for x and z values. We call this a symmetric comparison as don't care values can show up in either place.So if
instr
was all x's, none of the items in thecasez
would match, but ALL of the items in thecasex
would match, and a simulator would pick the first item. Similarly, ifinstr
were all z's, then ALL items would match. I considercasex
a useless construct.SystemVerilog replaces both statements with a
case() inside
statement. It uses an asymmetric comparison operator==?
that only treats an x or z in the case item as a don't care.