In the following code, i have the line row_count <= row_count + 1;
, and the next line I use row_count
to access an array, col_mask <= display(conv_integer(row_count));
. My expectation was that when the if block was triggered, row_count
would be incremented and that new value would be used to access the array. However, when I run the code, the value retrieved from the array is 1 behind where I expect it, showing that the array is indexed with the current value of row_count
before it is incremented.
My question is, why is that? I easily compensated for this by starting my counter at 1 instead of zero, but I'm very curious as to why it behaves this way.
if col = "10000000" then
-- Left rotate row
row <= row(6 downto 0) & row(7);
-- increment row count
row_count <= row_count + 1;
-- get column mask from current position in array
col_mask <= display(conv_integer(row_count));
end if;
row_count is a signal (visible because of the "<=" assignement). signals are always updated at the end of the process. therefore, if you update a signal, the updated value is only available the next time the process is triggered (e.g. on next rising clock edge).
if you need to have immediate access to updated values, you can implement row_count as a variable. variables are updated immediately and assigned with ":=". however, note that variables are only visible within the process.