1

How to create balanced logic (case) instead of priority (if .. else) for the following case?

I know I could get balanced logic when I change following priority logic

if(a == 2'b00) out = 1;
else if(a == 2'b01) out = 4'b0010;
else if(a == 2'b10) out = 4'b0100;
else if(a == 2'b11) out = 4'b1000;

To

case(a)
    2'b00: out = 1;
    2'b01: out = 2;
    2'b10: out = 4;
    2'b11: out = 8;
endcase

But how can I get balanced logic if the priority logic is this?

if(a) out = 1;
else if(b) out = 4'b0010;
else if(c) out = 4'b0100;
else if(d) out = 4'b1000;
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Steve
  • 607
  • 7
  • 8

1 Answers1

3

How about:

case (1'b1)

  a:out = 1;
  b:out = 4'b0010;
  c:out = 4'b0100;
  d:out = 4'b1000;

endcase

See here for concerns about priority.

Community
  • 1
  • 1
Ari
  • 7,251
  • 11
  • 40
  • 70
  • So is a, b, c, d could be any bit? – Steve Jun 26 '14 at 00:04
  • 1
    @Regan: I assumed they are 1-bit long. You could have a case where a, b, c, d are longer. In that case, they should be at least as long as the case value. – Ari Jun 26 '14 at 17:36