HDL Counter with programmable limit

6 views (last 30 days)
Hello,
I am using Simulink/HDL Coder to design a system. I use the HDL Counter block a lot. As I learn more, I realize that the HDL Counter doesn't quite do everything I need.
In particular, I'd like to be able to programmatically set the "Count to" value (for a Count limited counter) depending on the system configuration. For example: say I have a signal vector that is Nsamples long. Depending on the system configuration, Nsamples could be 1000 or it could be 1500 or any arbitrary value in that range. My system needs to count out those samples and do processing that depends on the length. But since the counter limit is set via a mask parameter, it gets compiled in as a constant and cannot be changed at runtime. The generated HDL code shows that this is a constant compiled into the source, hence not at all easily settable.
I would be happy if this could be set once on startup from a register - the counter limit would never need to change again, and a system reconfig would always involve a reset/reload of the FPGA. To change the value with the current HDL Counter, I will need to change it in the design and resynthesize the whole system, not to mention test it to make sure it still works. Is there some other block out there I'm missing? Or, should I go and design my own?
Suggestions welcome. Thanks,
Charles

Accepted Answer

Ryan Baird
Ryan Baird on 7 Mar 2023
Would a free running counter with a reset port connected to a comparison work for your use case?
  1 Comment
Charles
Charles on 9 Mar 2023
Hi Ryan,
That's a great idea - I don't use the reset port often and tend to forget about it. What's missing is that I do use the enable port often, and simply adding that to the system does not produce identical timing behavior. So I added an AND gate as in the system below showing two counters, the upper one programmable, the lower configured in the mask:
Luckily this worked and produced the same timing for the count_hit and programmable_count signals as for the signals from the stock counter. Do you see anything I should be thinking about?
Thanks,
Charles

Sign in to comment.

More Answers (0)

Categories

Find more on Code Generation in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!