I am new to FPGA/HDL in general.
While writing code, I often find myself having to repeat code. As a long time programmer I don’t like having to do this.
For example, say I have a sequential block, and it has an internal signal and an external signal.
I want these two signals to be in sync with each other, so they are updated to a “new” value at the same time. An example of this is a block which uses an accumulator to compute a box filter.
At each clock I do something like
accumulator.next = accumulator + data_in - oldest_value
output.next = (accumulator + data_in - oldest_value) // number_samples
In other words, the output is always equal to the accumulator divided by another number.
Is the preferred way to put the output.next into a combinatorial block, rather than a sequential block? I would still like for the output to be registered at the clock edge, and I am not sure if a combinatorial block would allow that.
Are there also other timing implications if the output signal is moved to a combinatorial block?
Is there a way to put the repeated code into some kind of symbolic variable so it can be used, i.e.
temp.next = accumulator + data_in - oldest_value
accumulator.next = temp
output.next = temp // number_samples
?
Another example:
I have a counter which I want to usually count up by 1 on every clock. But sometimes I want to set it back to zero, or subtract some value from it.
The control logic (if statements) are fairly complex, and so I have to manually go through all branches of all my if statements and check that every branch updates the value of the counter.
eg
# This example is contrived - in my real code there is
# no simple boolean simplification
if condA:
if condB:
counter.next = counter + 1
else:
counter.next = 0
else:
if condC:
counter.next = counter - value
else:
counter.next = counter + 1
Is there some way to again use a temporary ‘variable’ to store the next value, or am I thinking too procedurally here?
Eg, is something like this possible:
temp = counter + 1
if condA:
if not condB:
temp = 0
else:
if condC:
temp = counter - value
counter.next = temp
In other words, is there an easy way to have a default action (increment the counter) which gets applied, unless one of the other new values is applied?
Is there any good user manual / tutorial / documentation where I can learn about how to structure my code to avoid repeating myself and also searching for missing logic branches when updating my counter etc?
Thanks