Author Topic: Incrementing a counter in Verilog  (Read 1609 times)

0 Members and 1 Guest are viewing this topic.

Online MitiTopic starter

  • Super Contributor
  • ***
  • Posts: 1354
  • Country: ca
Incrementing a counter in Verilog
« on: March 16, 2021, 10:48:27 pm »
Looking through code examples in Verilog I found multiple ways to increment a counter and I wonder if there's any difference.

Code: [Select]
reg [19:0] counter;

// Method 1
if (condition) counter <= counter + 1'b1;

// Method 2
if (condition) counter <= counter + 20'b1;

// Method 3
if (condition) counter <= counter + 20'd1;

// Method 4
if (condition) counter <= counter + 1;

All seem to work but what are the dangers?

Cheers,
Miti
Fear does not stop death, it stops life.
 

Offline asmi

  • Super Contributor
  • ***
  • Posts: 2794
  • Country: ca
Re: Incrementing a counter in Verilog
« Reply #1 on: March 16, 2021, 11:38:34 pm »
They are equivalent, and will infer the same logic. The only thing is some synthesizers will issue a warning for the last method to the effect of "truncating number", as they treat dimensionless numbers as 32 bit values, and so it will need to chop off higher bits.

Online MitiTopic starter

  • Super Contributor
  • ***
  • Posts: 1354
  • Country: ca
Re: Incrementing a counter in Verilog
« Reply #2 on: March 17, 2021, 03:16:41 am »
Thanks asmi!
Fear does not stop death, it stops life.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf