gclk の完全な 0 ~ 15 カウントが出力 pwm 波の 1 周期であると仮定すると、pwmin はデューティ サイクルを反映します。カウントが 15+1 から 0 に減少すると、最初に bufferreg 値が gclk の立ち上がりエッジで pwmin 値を取得し、連続する各クロック エッジで countreg 値をチェックし、それに基づいて pwmout を提供します。
entity pwm is
port(gclk: in std_logic;
reset: in std_logic;
pwmin: in std_logic_vector(3 downto 0); --input reg to reflect the duty cycle
pwmout: out std_logic );
end pwm;
architecture Behavioral of pwm is
signal bufferreg,countreg: unsigned(3 downto 0);
signal count: integer:=16; -- count value for the one full cycle of PWM
begin
process(gclk, reset, pwmin)
begin
case reset is --asynchronous reset
when '0' =>
loop1: for count in 16 downto 0 loop --count is 15+1
if (rising_edge(gclk)) then --the buffer reg is loaded athe first clock edge
if (count=16) then
bufferreg<=unsigned(pwmin);
countreg<="0000";
else
if (countreg<=bufferreg) then
pwmout<='1'; --output high for on period
elsif (countreg>bufferreg) then
pwmout<='0'; --output low for off period
end if;
countreg<=countreg+1 --updating of countreg
end if;
end if;
--next;
end loop loop1;
when others =>
end case;
end process;
end Behavioral;