Forum Discussion
VHDL integers initialization and FPGAs
I've started coding in VHDL just less than a year ago - unexperienced student warning!
My question is about integer initalization. I own an Altera DE1 board and I've also got some experience with Xilinx Spartan 3. With both deviced I've noticed a strange thing about integers. I must initialize an integer to some value at declaration level (i.e. signal int : integer range bla to blabla := 0; ), because otherwise my program may freeze or some other voodoo might appear. Usually I use integer signals for counters (UART interface for instance). Have you ever encountered this before ? I guess I'm configuring/doing something wrong as I know that initialization at declaration level doesn't (or shouldn't?) affect the program after synthesis.16 Replies
- Altera_Forum
Honored Contributor
OK, thanks a lot for clearing this up :D
I will definitely check out that safe state machine option. Now I'll try to capture that glitch in SignalTap somehow... I'm not really familiar with state-based trigger flow so I might as well educate myself about it too. - Altera_Forum
Honored Contributor
--- Quote Start --- what is the best way to prevent this from happening? Synchronize the async. inputs feeded into the FSM ? --- Quote End --- Yes, that's the required method, as already mentioned. In those cases, where a safe timing can't be guaranteed, e.g because the clock input may have glitches, the safe state machine option prevents freezing. See the Quartus software handbook for details. I was just aware, that my previous statement about default initialization of integers has been incorrect. As Tricky said, integers are initialized to the leftmost value of the range, also in Quartus synthesis. Thus to or downto definition makes an important difference. - Altera_Forum
Honored Contributor
--- Quote Start --- Yes, it's the most likely explanation. In case, state would show an illegal bit combination, it can be easily detected in simulation or with signal tap. The probability of dropping into an illegal state with asynchronous inputs also depends on arbitrary design timing details, it may change with design modifications without a clear relation. --- Quote End --- I see, so what is the best way to prevent this from happening? Synchronize the async. inputs feeded into the FSM ? - Altera_Forum
Honored Contributor
--- Quote Start --- You should/must not take this asynchronous signal as an input to your state machine. This can cause the described freeze. --- Quote End --- Yes, it's the most likely explanation. In case, state would show an illegal bit combination, it can be easily detected in simulation or with signal tap. The probability of dropping into an illegal state with asynchronous inputs also depends on arbitrary design timing details, it may change with design modifications without a clear relation. - Altera_Forum
Honored Contributor
hjs (http://www.alteraforum.com/forum/member.php?u=4216), oh I didn't know that, thanks! So passing it through DFFs is the way to go, I guess?
tricky (http://www.alteraforum.com/forum/member.php?u=7270), yep, in ActiveHDL you have the option to manipulate signals and set them to any (possible) value you want in any given period of time. Usually I find that easier to work with than with a testbench... - Altera_Forum
Honored Contributor
How did you test it without a testbench? did you use a waveform file?
- Altera_Forum
Honored Contributor
An other point to take care of : I suppose RX is asynchronous to the clock. You should/must not take this asynchronous signal as an input to your state machine. This can cause the described freeze.
- Altera_Forum
Honored Contributor
I simulated using ActiveHDL, so there's no testbench code since I didn't need it. I could write one if you'd like.
Maybe you'd rather like to see simulation results? Edit: ha, this is interesting. I just tested it again using 'to' instead of 'downto' in the declaration (without the := 0 initialization), and it also worked fine. So I guess it's a compiler related issue? - Altera_Forum
Honored Contributor
how about writing a testbench and simulating the design?
- Altera_Forum
Honored Contributor
FvM, I'm just used to use downto. No special purpose. I'll try it with 'to'.
Tricky, I have no idea how to capture the exact moment when it freezes. I have monitored a signal (using SignalTap) which shows when the receiver has a new byte and I saw that after it freezes, that signal never goes up as it should and no new data appears on the data bus. I have no idea how to capture the exact moment of the glitch. Maybe you have an idea how to do it using a testbench and/or SignalTap? By the way thanks for the help and fast replies, guys!