If I've got an array of 9 wires, is there an easy way to make a new wire that is high if exactly one of the 9 is high? I know I could do
wire[8:0] data;
wire exactlyOneActive;
assign exactlyOneActive = (data[0] & !data[1] & !data[2] ...) |
(!data[0] & data[1] & !data[2] ...) |
(!data[0] & !data[1] & data[2] ...) |
...etc
but, yuck, right? Especially since the nine wires will probably be 25 at some point. Any better way to do this, maybe using generate
? It has to be synthesizable too.
assign zeroOrOnehot = ~|(data & (data-1));
assign atLeastOneBitSet = |data;
assign exactlyOneActive = zeroOrOnehot & atLeastOneBitSet;
Regards - Cliff Cummings - Verilog & SystemVerilog Guru
This should be a pretty efficient design.
wire[8:0] data;
wire exactly_one_active;
//Total is log2_ceiling of data bits wide
// Remove binary weighting
wire [3:0] total = data[8] + data[7] ... + data[0];
assign exactly_one_active = (total == 4'b1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With