Priority with casez¶
Introduction of casez¶
Build a priority encoder for 8-bit inputs. Given an 8-bit vector, the output should report the first (least significant) bit in the vector that is 1
. Report zero if the input vector has no bits that are high. For example, the input 8'b10010000
should output 3'd4
, because bit[4] is first bit that is high.
From the previous exercise (always_case2), there would be 256 cases in the case statement. We can reduce this (down to 9 cases) if the case items in the case statement supported don't-care bits. This is what casez
is for: It treats bits that have the value z
as don't-care in the comparison.
For example, this would implement the 4-input priority encoder from the previous exercise:
Verilog | |
---|---|
A case statement behaves as though each item is checked sequentially (in reality, a big combinational logic function). Notice how there are certain inputs (e.g., 4'b1111
) that will match more than one case item. The first match is chosen (so 4'b1111
matches the first item, out = 0
, but not any of the later ones).
- There is also a similar
casex
that treats bothx
andz
as don't-care. I don't see much purpose to using it overcasez
. - The digit
?
is a synonym(同义词) forz
. so2'bz0
is the same as2'b?0
It may be less error-prone to explicitly specify the priority behaviour rather than rely on the ordering of the case items. For example, the following will still behave the same way if some of the case items were reordered, because any bit pattern can only match at most one case item:
Verilog | |
---|---|
Practice¶
Problem statement¶
Build a priority encoder for 8-bit inputs. Given an 8-bit vector, the output should report the first (least significant) bit in the vector that is
1
. Report zero if the input vector has no bits that are high. For example, the input8'b10010000
should output3'd4
, because bit[4] is first bit that is high.
Verilog | |
---|---|
Solution¶
- Notice that you should use
casez (input)
if you want to use casez statement. The letter z is neccessary.