CPU频率和上升沿计数
CPU frequency and rising edge count
我对 CPU 时钟速度感到困惑。
我以为看完这个 YouTube video, and reading this web page 后我对它有了深刻的理解,但是当我回到 Pluralsight 上的 VHDL 教程时(link 没有给出,因为它不是免费的)谈论时钟速度,我不确定。
请考虑以下代码,它被设计成一个非常简单的计时器:
entity boardio is
port (
clock_50: in bit;
hex0 : out bit_vector(0 to 6);
hex1 : out bit_vector(0 to 6);
hex2 : out bit_vector(0 to 6);
hex3 : out bit_vector(0 to 6);
key : in bit_vector(0 to 3);
sw: bit_vector(0 to 9);
ledr: buffer bit_vector (0 to 9)
);
end boardio;
architecture arch of boardio is
signal count : integer := 1; -- not a variable!
signal clock_1hz : bit := '0';
signal mins, secs : integer range 0 to 59 := 0;
function hex_digit(x:integer; constant hide_zero:boolean := false) return bit_vector is
begin
case x is
when 0 =>
if hide_zero then
return "1111111";
else
return "0000001";
end if;
when 1 => return "1001111";
when 2 => return "0010010";
when 3 => return "0000110";
when 4 => return "1001100";
when 5 => return "0100100";
when 6 => return "0100000";
when 7 => return "0001111";
when 8 => return "0000000";
when 9 => return "0000100";
when others => return "1111111";
end case;
end function;
begin
-- 1hz clock
-- ****************************************************
process(clock_50)
begin
if (clock_50'event and clock_50 = '1') then
count <= count + 1;
if (count = 25000000) then -- half the rate
clock_1hz <= not clock_1hz;
count <= 1;
end if;
end if;
end process;
-- ****************************************************
process(clock_1hz, key)
begin
-- update # of seconds
if (clock_1hz'event and clock_1hz = '1') then
if (key(0) = '0') then
secs <= 0;
mins <= 0;
else
if (secs = 59) then
mins <= (mins + 1) rem 60;
end if;
secs <= (secs + 1) rem 60;
end if;
end if;
end process;
process(clock_1hz)
begin
hex0 <= hex_digit(secs rem 10);
hex1 <= hex_digit(secs / 10, true);
hex2 <= hex_digit(mins rem 10, mins = 0);
hex3 <= hex_digit(mins / 10, true);
end process;
end arch;
我已经发布了所有代码,因此每个人都有完整的上下文,但我真正感兴趣的是我用星号表示的 1hz clock
过程。
这表明在 50MHz 时钟的一秒内,一秒内将有 250,000,000 个上升沿。但是,我 link 编辑的视频和网页表明,对于 1hz 时钟,一秒内将有一个上升沿和一个下降沿,因此对于 50Mhz 时钟,每个上升沿和下降沿将有 500,000,000 个。
有人可以澄清 CPU 频率在上升沿和下降沿方面的实际含义,以及 'tick' 吗?图表将不胜感激,因为我不再确定 link 上的图表是否正确...
x Hz 的频率意味着每秒有 x 个完整的信号周期。如果您有一个典型的矩形时钟信号,这意味着每秒 x 个上升沿和 x 个下降沿。在 1 Hz 时钟上,每秒有一个上升沿和一个下降沿,给出一个完整的周期。在 50 MHz 时钟(5000 万赫兹)上,您有 5000 万个上升沿和下降沿。
为了从 50 MHz 时钟生成 1 Hz 时钟,您需要将时钟周期减少 5000 万倍。但是,由于每个时钟周期都包含一个上升沿和一个下降沿,因此您需要每个周期更改两次输出时钟信号(即每秒两次)。因此,您示例中的计数器计数为 2500 万(不是您编写的 2.5 亿,仔细查看代码!),然后反转输出信号。这每秒产生一个下降沿和一个上升沿,每个相隔半秒,这正是您通常想要的 - 一个每秒重复一次并在 'on' 状态和 'off' 之间平均分配的信号-状态,称为 50% 占空比。您的时钟确实不需要 50% 的占空比才能正常工作,因为时钟逻辑使用边沿而不是时钟状态,但是如果时钟的 'on' 或 'off' 脉冲太短,逻辑可能无法正确检测到它并出现错误。
一个滴答是一个时钟周期,因此一个 50 MHz 的时钟每秒将产生 5000 万个滴答。
我对 CPU 时钟速度感到困惑。
我以为看完这个 YouTube video, and reading this web page 后我对它有了深刻的理解,但是当我回到 Pluralsight 上的 VHDL 教程时(link 没有给出,因为它不是免费的)谈论时钟速度,我不确定。
请考虑以下代码,它被设计成一个非常简单的计时器:
entity boardio is
port (
clock_50: in bit;
hex0 : out bit_vector(0 to 6);
hex1 : out bit_vector(0 to 6);
hex2 : out bit_vector(0 to 6);
hex3 : out bit_vector(0 to 6);
key : in bit_vector(0 to 3);
sw: bit_vector(0 to 9);
ledr: buffer bit_vector (0 to 9)
);
end boardio;
architecture arch of boardio is
signal count : integer := 1; -- not a variable!
signal clock_1hz : bit := '0';
signal mins, secs : integer range 0 to 59 := 0;
function hex_digit(x:integer; constant hide_zero:boolean := false) return bit_vector is
begin
case x is
when 0 =>
if hide_zero then
return "1111111";
else
return "0000001";
end if;
when 1 => return "1001111";
when 2 => return "0010010";
when 3 => return "0000110";
when 4 => return "1001100";
when 5 => return "0100100";
when 6 => return "0100000";
when 7 => return "0001111";
when 8 => return "0000000";
when 9 => return "0000100";
when others => return "1111111";
end case;
end function;
begin
-- 1hz clock
-- ****************************************************
process(clock_50)
begin
if (clock_50'event and clock_50 = '1') then
count <= count + 1;
if (count = 25000000) then -- half the rate
clock_1hz <= not clock_1hz;
count <= 1;
end if;
end if;
end process;
-- ****************************************************
process(clock_1hz, key)
begin
-- update # of seconds
if (clock_1hz'event and clock_1hz = '1') then
if (key(0) = '0') then
secs <= 0;
mins <= 0;
else
if (secs = 59) then
mins <= (mins + 1) rem 60;
end if;
secs <= (secs + 1) rem 60;
end if;
end if;
end process;
process(clock_1hz)
begin
hex0 <= hex_digit(secs rem 10);
hex1 <= hex_digit(secs / 10, true);
hex2 <= hex_digit(mins rem 10, mins = 0);
hex3 <= hex_digit(mins / 10, true);
end process;
end arch;
我已经发布了所有代码,因此每个人都有完整的上下文,但我真正感兴趣的是我用星号表示的 1hz clock
过程。
这表明在 50MHz 时钟的一秒内,一秒内将有 250,000,000 个上升沿。但是,我 link 编辑的视频和网页表明,对于 1hz 时钟,一秒内将有一个上升沿和一个下降沿,因此对于 50Mhz 时钟,每个上升沿和下降沿将有 500,000,000 个。
有人可以澄清 CPU 频率在上升沿和下降沿方面的实际含义,以及 'tick' 吗?图表将不胜感激,因为我不再确定 link 上的图表是否正确...
x Hz 的频率意味着每秒有 x 个完整的信号周期。如果您有一个典型的矩形时钟信号,这意味着每秒 x 个上升沿和 x 个下降沿。在 1 Hz 时钟上,每秒有一个上升沿和一个下降沿,给出一个完整的周期。在 50 MHz 时钟(5000 万赫兹)上,您有 5000 万个上升沿和下降沿。
为了从 50 MHz 时钟生成 1 Hz 时钟,您需要将时钟周期减少 5000 万倍。但是,由于每个时钟周期都包含一个上升沿和一个下降沿,因此您需要每个周期更改两次输出时钟信号(即每秒两次)。因此,您示例中的计数器计数为 2500 万(不是您编写的 2.5 亿,仔细查看代码!),然后反转输出信号。这每秒产生一个下降沿和一个上升沿,每个相隔半秒,这正是您通常想要的 - 一个每秒重复一次并在 'on' 状态和 'off' 之间平均分配的信号-状态,称为 50% 占空比。您的时钟确实不需要 50% 的占空比才能正常工作,因为时钟逻辑使用边沿而不是时钟状态,但是如果时钟的 'on' 或 'off' 脉冲太短,逻辑可能无法正确检测到它并出现错误。
一个滴答是一个时钟周期,因此一个 50 MHz 的时钟每秒将产生 5000 万个滴答。