Author Topic: VERILOG: Equality test with don't cares  (Read 3735 times)

0 Members and 1 Guest are viewing this topic.

Offline AussieBruceTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: au
VERILOG: Equality test with don't cares
« on: May 14, 2022, 05:10:11 am »
Hello,  I’m having trouble getting a simple frequency dividing loop to work. Here’s a minimal test fragment, if the comparison wire has any don’t care bits in it the test fails, if I don’t include them it works fine. I’m obviously specifying the don’t cares in the wrong way, can anyone help?

In case it matters, IDE is Quartus-II.  Thanks in advance

reg [24:0] TBdiv;
reg LEDreg ;

 wire Roll = (TBdiv ==  25'b1111111111111111111111111) ;  // This works
 wire Roll = (TBdiv ==  25'b111111111111111111x111111) ;  // This doesn't

   always @ (posedge clk)
      begin
      if (Roll)
         begin
         LEDreg <= ~LEDreg  ;  // Test LED
         TBdiv <= 25'd0 ;
         end
      else
         begin
         TBdiv <= TBdiv + 25'd1 ;
         end
      end
            
assign LED = LEDreg ;
 

Offline Foxxz

  • Regular Contributor
  • *
  • Posts: 125
  • Country: us
Re: VERILOG: Equality test with don't cares
« Reply #1 on: May 14, 2022, 05:25:00 am »
What about something like...

wire Roll = (TBdiv{24:7} == 18'b111111111111111111) && (TBdiv{5:0} == 6'b111111);

This might not be exact I'm a little rusty. But basically cutting out the don't care bit by splitting up the comparison

wire Roll = (TBdiv ==  25'b111111111111111111?111111);

I think this is also valid
« Last Edit: May 14, 2022, 05:36:11 am by Foxxz »
 
The following users thanked this post: AussieBruce

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8089
  • Country: ca
Re: VERILOG: Equality test with don't cares
« Reply #2 on: May 14, 2022, 05:35:55 am »
Try :
wire Roll = (TBdiv ==  25'b111111111111111111?111111) ;

Please double check in your simulator.  I'm a bit rusty in this area.

When placing an 'x', this means your source reg needs to be unknown or un-initialized 'x' to return a high value.  This does not tend to work in FPGA hardware but only in simulators.
« Last Edit: May 14, 2022, 11:46:01 pm by BrianHG »
 
The following users thanked this post: AussieBruce

Offline AussieBruceTopic starter

  • Regular Contributor
  • *
  • Posts: 61
  • Country: au
Re: VERILOG: Equality test with don't cares
« Reply #3 on: May 14, 2022, 07:36:46 am »
Thanks for the comments folks. Since then I’ve done some more testing as well as googling, and now realise that I’ve stumbled on a can of worms. All I was trying to do was to reduce the footprint of the wire by anding only the bit positions that indicate that the target has been reached. I would have thought having convenient syntax available for that function would be a nobrainer, whereas in fact it seems that there are issues all over the place with the notion of ‘don’t care’.

It’s probably good that all this has come to light, up to now Verilog has looked pretty unbreakable. If I ever come across a ‘what would you like added to Verilog?’ website I’ll add that to it, although I have no doubt that it would have already been suggested. Cheers
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4279
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: VERILOG: Equality test with don't cares
« Reply #4 on: May 14, 2022, 08:22:54 am »
I'd implement this with a bit mask, ie.

if (value AND mask) = (target AND mask) then <stuff>

This doesn't add any extra logic to the final gate list, because if the mask is fixed, the results of the AND operations are known at compile time - either the original bit value or zero, neither of which requires any additional logic to derive.

The tests for "does 0 equal 0" can also be omitted and will optimise out, leaving behind just the comparison between the bits you're interested in.
 
The following users thanked this post: Someone

Online Someone

  • Super Contributor
  • ***
  • Posts: 4961
  • Country: au
    • send complaints here
Re: VERILOG: Equality test with don't cares
« Reply #5 on: May 14, 2022, 11:57:34 am »
if (value AND mask) = (target AND mask) then <stuff>
Verilog code golf:
&((value ~^ target) | ~mask)

... and to continue the holy wars, VHDL has a function: std_match
which is apparently now also an operator: ?=
 

Offline emece67

  • Frequent Contributor
  • **
  • !
  • Posts: 614
  • Country: 00
Re: VERILOG: Equality test with don't cares
« Reply #6 on: May 14, 2022, 01:04:07 pm »
.
« Last Edit: August 19, 2022, 05:29:39 pm by emece67 »
 
The following users thanked this post: BrianHG

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9935
  • Country: us
Re: VERILOG: Equality test with don't cares
« Reply #7 on: May 14, 2022, 02:48:34 pm »
They should add a 'Maybe' operator to go along with these.  Perhaps expand the concept to include a  'Once in a while' operator.

I'm getting old...

 

Online BrianHG

  • Super Contributor
  • ***
  • Posts: 8089
  • Country: ca
Re: VERILOG: Equality test with don't cares
« Reply #8 on: May 14, 2022, 02:57:28 pm »
They should add a 'Maybe' operator to go along with these.  Perhaps expand the concept to include a  'Once in a while' operator.

I'm getting old...
Well, the triple '=', or the === and !== operators exist...
 

Offline josuah

  • Regular Contributor
  • *
  • Posts: 119
  • Country: fr
    • josuah.net
Re: VERILOG: Equality test with don't cares
« Reply #9 on: May 18, 2022, 11:58:43 am »
Would it not work to slice TBdiv to exclude the bit you do not care?

Foxxz already proposed:

Code: [Select]
if (TBdiv[24:7] == 18'b111111111111111111 && TBdiv[5:0] == 6'b111111)
...

This is slightly more compact (but not too much):

Code: [Select]
if ({ TBdiv[24:7], TBdiv[5:0] } == 24'b111111111111111111111111)
...

This 9'b1111?1111 syntax is nifty! I did not know about it...
From the standard:

Quote
3.5.1 Integer constants
[...]
When used in a number, the question-mark (?) character is a Verilog HDL alternative for the z character. It
sets 4 bits to the high-impedance value in hexadecimal numbers, 3 bits in octal, and 1 bit in binary. The
question mark can be used to enhance readability in cases where the high-impedance value is a do-not-care
condition.
[...]
16'sd?  // the same as 16'sbz
 

Offline free_electron

  • Super Contributor
  • ***
  • Posts: 8550
  • Country: us
    • SiliconValleyGarage
Re: VERILOG: Equality test with don't cares
« Reply #10 on: May 18, 2022, 01:27:30 pm »
logically OR incoming data with a mask. Set bit to 1 if don't care. now you can reduce to bitwise and
Professional Electron Wrangler.
Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf