Project

General

Profile

1031 markw
---------------------------------------------------------------------------
-- (c) 2020 mark watson
-- I am happy for anyone to use this for non-commercial use.
-- If my vhdl files are used commercially or otherwise sold,
-- please contact me for explicit permission at scrameta (gmail).
-- This applies for source and binary form and derived works.
---------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.all;
use ieee.numeric_std.all;

1036 markw
ENTITY PSG_envelope IS
1031 markw
PORT
(
CLK : IN STD_LOGIC;
RESET_N : IN STD_LOGIC;
ENABLE : IN STD_LOGIC;
1038 markw
STEP32 : IN STD_LOGIC;
1031 markw
COUNT_RESET : IN STD_LOGIC;
SHAPE : IN STD_LOGIC_VECTOR(3 downto 0);
PERIOD : IN STD_LOGIC_VECTOR(15 downto 0);

1038 markw
ENVELOPE : OUT STD_LOGIC_VECTOR(4 downto 0)
1031 markw
);
1036 markw
END PSG_envelope;
1031 markw
1036 markw
ARCHITECTURE vhdl OF PSG_envelope IS
1038 markw
signal envelope_reg: std_logic_vector(4 downto 0);
signal envelope_next: std_logic_vector(4 downto 0);
1031 markw
1038 markw
signal count_reg: unsigned(5 downto 0);
signal count_next: unsigned(5 downto 0);
1032 markw
1031 markw
signal envelope_tick: std_logic;
BEGIN
1038 markw
-- register
process(clk, reset_n)
begin
if (reset_n = '0') then
count_reg <= (others=>'0');
envelope_reg <= (others=>'0');
elsif (clk'event and clk='1') then
count_reg <= count_next;
envelope_reg <= envelope_next;
end if;
end process;

envelope_ticker : entity work.PSG_freqdiv
GENERIC MAP
(
bits => 16
)
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,
1047 markw
SYNC_RESET => count_reset,
1031 markw
1038 markw
BIT_OUT => envelope_tick,

THRESHOLD => unsigned(PERIOD)
);

-- next state
1064 markw
process(count_reg,shape,envelope_reg,envelope_tick,count_reset)
1038 markw
variable continue : std_logic;
variable attack : std_logic;
variable alternate : std_logic;
variable hold : std_logic;

variable tmprep : std_logic_vector(4 downto 0);
begin
count_next <= count_reg;
envelope_next <= envelope_reg;

continue := shape(3);
attack := shape(2);
alternate := shape(1);
hold := shape(0);

if (count_reset='1') then
count_next <= (others=>'0');
else
if (envelope_tick='1') then
count_next <= count_reg+1;
if ((hold and count_reg(5))='1') then
envelope_next <= (others=>alternate xor attack);
else
tmprep := (others=>(count_reg(5) and alternate) xnor attack);
envelope_next <= std_logic_vector(count_reg(4 downto 0)) xor tmprep;
end if;

if (((hold or not(continue)) and count_reg(5))='1') then
if (continue='0') then
envelope_next <= (others=>'0');
end if;
count_next(5) <= '1';
end if;
end if;
end if;

end process;

-- output
envelope <= envelope_reg(4 downto 1)&(envelope_reg(0) and STEP32);

1031 markw
END vhdl;