S
Shannon
Since everyone has been so helpful lately I thought I'd press my luck
with a nagging problem I keep running into.
Many times in a syncronous domain I end up with some "enable" pulse
that is exactly one clock wide. Let's say I want to use that enable
to start a counter for some reason. I want the counter to start
counting when enable is true but keep counting until it's done. Let's
say it's a down counter counting down to zero.
I guess my question seems simple: How do I "latch" this enable
pulse? It seems like anything I do is in danger of a race condition.
If everything is syncronous, I can't "act" on anything until the next
clock cycle after the front edge of "enable". But exactly at that
moment, enable is going away! Seems scary.
Mike Treseler once posted the following code to "stretch" a one-clock-
wide pulse to two clocks wide:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity stretch is
port (
clk : in std_logic;
reset : in std_logic;
trig : in std_logic;
pulse : out std_logic
);
end stretch;
architecture synth of stretch is
begin
process (reset, clk) is
variable last_trig : std_logic;
begin
if reset = '0' then
pulse <= '0';
elsif clk'event and clk = '1' then
pulse <= trig or last_trig; -- pulse for 10,11,or 01
last_trig := trig; -- save trig
end if;
end process;
end synth;
Doesn't this suffer the same problem? "last_trig" is changing state
right when "trig" is going away. Same for pulse: pulse wants to
change state right as last_trig is going away.
I'm sure the code works but it's the same problem I have. It just
seems scary.
Any ideas?
Shannon
with a nagging problem I keep running into.
Many times in a syncronous domain I end up with some "enable" pulse
that is exactly one clock wide. Let's say I want to use that enable
to start a counter for some reason. I want the counter to start
counting when enable is true but keep counting until it's done. Let's
say it's a down counter counting down to zero.
I guess my question seems simple: How do I "latch" this enable
pulse? It seems like anything I do is in danger of a race condition.
If everything is syncronous, I can't "act" on anything until the next
clock cycle after the front edge of "enable". But exactly at that
moment, enable is going away! Seems scary.
Mike Treseler once posted the following code to "stretch" a one-clock-
wide pulse to two clocks wide:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity stretch is
port (
clk : in std_logic;
reset : in std_logic;
trig : in std_logic;
pulse : out std_logic
);
end stretch;
architecture synth of stretch is
begin
process (reset, clk) is
variable last_trig : std_logic;
begin
if reset = '0' then
pulse <= '0';
elsif clk'event and clk = '1' then
pulse <= trig or last_trig; -- pulse for 10,11,or 01
last_trig := trig; -- save trig
end if;
end process;
end synth;
Doesn't this suffer the same problem? "last_trig" is changing state
right when "trig" is going away. Same for pulse: pulse wants to
change state right as last_trig is going away.
I'm sure the code works but it's the same problem I have. It just
seems scary.
Any ideas?
Shannon