I would like to latch a signal, however when I try to do so, I get a delay of one cycle, how can I avoid this?
myLatch: process(wclk, we) -- Can I ommit the we in the sensitivity list?
begin
if wclk'event and wclk = '1' then
lwe <= we;
end if;
end process;
However if I try this and look into the waves during simulation lwe is delayed by one cycle of wclk. All I want tp achieve is to sample we on the rising edge of wclk and keep it stable till the next rising edge. I then assign the latched signal to another entities port map which is defined in the architecture.
==============================================
Well I figured out that I have to omit the wclk'event to get a latch instead of a flip flop. This seems rather unintuitive to me. By simply shortening the time where I sample the signal to be latched I go from latch to flip flop. Can anyone explain why this is and where my perception is wrong. (I am a vhdl beginner)
First off, a few observations on the process you pasted above:
The signal we can be omitted from the sensitivity list because you have described a clocked process. The only signals required in the sensitivity list of a process like this are the clock and the asynchronous reset if you choose to use one (a synchronous reset would not need to be added to the sensitivity list).
Instead of using
if wclk'event and wclk = '1' thenyou should instead useif rising_edge(wclk) thenorif falling_edge(wclk) then, there’s a good blog post on the reasons why here.By omitting the
wclk'eventyou changed the process from a clocked process to a combinatorial process, like so:In a combinatorial process all inputs should be present in the sensitivity list, so you would be correct to have both
wclkandwein the list as they had an influence on the output. Normally you would ensure thatlweis assigned in all cases of your if statement to avoid inferring a latch, however this appears to be your intention in this case.Latches in general should be avoided, so if you find yourself needing one you should perhaps pause and consider your approach. Doulos have a couple of articles on latches here and here that you might find useful.
You stated that all you want to achieve is to sample
weon the rising edge ofwclkand keep it stable until the next rising edge. The process below will accomplish this:With this process,
lwewill be updated with the value ofweupon every rising edge ofwclkand it will remain valid for a single clock cycle.Let me know if this clears things up for you.