Project

General

Profile

1043 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;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.STD_LOGIC_MISC.all;

LIBRARY work;

ENTITY SID_top IS
1253 markw
GENERIC
(
wave_base: std_logic_vector(16 downto 0)
);
1043 markw
PORT
(
1058 markw
CLK : in std_logic; -- >1MHz, ideally higher for more accurate filter (as long as timing met)
1043 markw
RESET_N : in std_logic;

1058 markw
ENABLE : in std_logic; -- Typically ~1MHz
1043 markw
ADDR : in std_logic_vector(4 downto 0);
1324 markw
READ_ENABLE : in std_logic;
1043 markw
WRITE_ENABLE : in std_logic;
1071 markw
1324 markw
POT_X : in std_logic;
POT_Y : in std_logic;
POT_RESET : out std_logic;
1043 markw
DI : in std_logic_vector(7 downto 0);
DO : out std_logic_vector(7 downto 0);
1273 markw
DRIVE_DO : out std_logic;
1043 markw
1073 markw
AUDIO : out std_logic_vector(15 downto 0);

DEBUG_WV1 : out unsigned(11 downto 0);
DEBUG_EV1 : out unsigned(7 downto 0);
1178 markw
DEBUG_AM1 : out signed(15 downto 0);

1324 markw
sidtype : in std_logic; -- 0=8580 filter, 1=6581 filter
EXT : in std_logic_vector(1 downto 0); -- 00=GND,01=digifix,10=ADC
EXT_ADC : in unsigned(15 downto 0);
1208 markw
1253 markw
rom_addr : out std_logic_vector(16 downto 0);
1208 markw
rom_data : in std_logic_vector(31 downto 0);
rom_request : out std_logic;
1247 markw
rom_ready : in std_logic;

1262 markw
FILTER_BP_OUT : out signed(17 downto 8);
FILTER_HP_OUT : out signed(17 downto 8);
FILTER_F_OUT : out std_logic_vector(12 downto 0);
FILTER_F_BP : in std_logic_vector(12 downto 0);
FILTER_F_HP : in std_logic_vector(12 downto 0)
1043 markw
);
END SID_top;

ARCHITECTURE vhdl OF SID_top IS
-- frequency (added to 24 bit osc on each tick)
signal freq_adj_channel_a_reg : std_logic_vector(15 downto 0);
signal freq_adj_channel_a_next : std_logic_vector(15 downto 0);
signal freq_adj_channel_b_reg : std_logic_vector(15 downto 0);
signal freq_adj_channel_b_next : std_logic_vector(15 downto 0);
signal freq_adj_channel_c_reg : std_logic_vector(15 downto 0);
signal freq_adj_channel_c_next : std_logic_vector(15 downto 0);

-- pulse waveform duty cycle
signal pulse_width_channel_a_reg : std_logic_vector(11 downto 0);
signal pulse_width_channel_a_next : std_logic_vector(11 downto 0);
signal pulse_width_channel_b_reg : std_logic_vector(11 downto 0);
signal pulse_width_channel_b_next : std_logic_vector(11 downto 0);
signal pulse_width_channel_c_reg : std_logic_vector(11 downto 0);
signal pulse_width_channel_c_next : std_logic_vector(11 downto 0);

-- waveform
signal waveselect_a_reg : std_logic_vector(3 downto 0);
signal waveselect_a_next : std_logic_vector(3 downto 0);
signal waveselect_b_reg : std_logic_vector(3 downto 0);
signal waveselect_b_next : std_logic_vector(3 downto 0);
signal waveselect_c_reg : std_logic_vector(3 downto 0);
signal waveselect_c_next : std_logic_vector(3 downto 0);

--ring mod, sync, gate
signal control_a_reg : std_logic_vector(3 downto 0);
signal control_a_next : std_logic_vector(3 downto 0);
signal control_b_reg : std_logic_vector(3 downto 0);
signal control_b_next : std_logic_vector(3 downto 0);
signal control_c_reg : std_logic_vector(3 downto 0);
signal control_c_next : std_logic_vector(3 downto 0);

-- ADSR envelope - attack/decay/sustain/release
signal envelope_attack_a_reg : std_logic_vector(3 downto 0);
signal envelope_attack_a_next : std_logic_vector(3 downto 0);
signal envelope_attack_b_reg : std_logic_vector(3 downto 0);
signal envelope_attack_b_next : std_logic_vector(3 downto 0);
signal envelope_attack_c_reg : std_logic_vector(3 downto 0);
signal envelope_attack_c_next : std_logic_vector(3 downto 0);

signal envelope_decay_a_reg : std_logic_vector(3 downto 0);
signal envelope_decay_a_next : std_logic_vector(3 downto 0);
signal envelope_decay_b_reg : std_logic_vector(3 downto 0);
signal envelope_decay_b_next : std_logic_vector(3 downto 0);
signal envelope_decay_c_reg : std_logic_vector(3 downto 0);
signal envelope_decay_c_next : std_logic_vector(3 downto 0);

signal envelope_sustain_a_reg : std_logic_vector(3 downto 0);
signal envelope_sustain_a_next : std_logic_vector(3 downto 0);
signal envelope_sustain_b_reg : std_logic_vector(3 downto 0);
signal envelope_sustain_b_next : std_logic_vector(3 downto 0);
signal envelope_sustain_c_reg : std_logic_vector(3 downto 0);
signal envelope_sustain_c_next : std_logic_vector(3 downto 0);

signal envelope_release_a_reg : std_logic_vector(3 downto 0);
signal envelope_release_a_next : std_logic_vector(3 downto 0);
signal envelope_release_b_reg : std_logic_vector(3 downto 0);
signal envelope_release_b_next : std_logic_vector(3 downto 0);
signal envelope_release_c_reg : std_logic_vector(3 downto 0);
signal envelope_release_c_next : std_logic_vector(3 downto 0);

-- state variable filter params
1178 markw
signal statevariable_fcutoff_reg : std_logic_vector(10 downto 0); --30Hz to 12KHz, linear (or rather not, read from rom...)
1043 markw
signal statevariable_fcutoff_next : std_logic_vector(10 downto 0);
1262 markw
signal statevariable_F_reg : std_logic_vector(12 downto 0); -- F computed from fcutoff -> or read from ram : 0.21 fixed point
signal statevariable_F_next : std_logic_vector(12 downto 0); -- see example computation at start of filter.vhdl
1178 markw
signal statevariable_f_changed : std_logic;
1213 markw
signal statevariable_f_dirty_next : std_logic;
signal statevariable_f_dirty_reg : std_logic;
1283 markw
signal rom_state_reg : std_logic_vector(2 downto 0);
signal rom_state_next : std_logic_vector(2 downto 0);
constant rom_state_init : std_logic_vector(2 downto 0) := "000";
constant rom_state_romrequest_statevariable_f : std_logic_vector(2 downto 0) := "001";
constant rom_state_romrequest_wave_a : std_logic_vector(2 downto 0) := "010";
constant rom_state_romrequest_wave_b : std_logic_vector(2 downto 0) := "011";
constant rom_state_romrequest_wave_c : std_logic_vector(2 downto 0) := "100";
constant rom_state_romrequest_statevariable_q : std_logic_vector(2 downto 0) := "101";
1217 markw
signal statevariable_q_changed : std_logic;
signal statevariable_q_dirty_next : std_logic;
signal statevariable_q_dirty_reg : std_logic;
1043 markw
signal statevariable_Q_reg : std_logic_vector(3 downto 0); --resonance
signal statevariable_Q_next : std_logic_vector(3 downto 0);
1217 markw
signal statevariable_1q_reg : signed(17 downto 0); --resonance
signal statevariable_1q_next : signed(17 downto 0);
1213 markw
1217 markw
signal rom_addr_mux : std_logic_vector(2 downto 0);
1213 markw
signal rom_data_word : std_logic_vector(15 downto 0);
1043 markw
1213 markw
signal rom_osc : std_logic_vector(10 downto 0);
signal rom_high_word : std_logic;
signal rom_wave_3bit : std_logic_vector(2 downto 0);
signal rom_wave_2bit : std_logic_vector(1 downto 0);

signal wavegen_data_needed : std_logic_vector(2 downto 0);
signal wavegen_data_ready : std_logic_vector(2 downto 0);

1043 markw
-- which channels are filtered?
1284 markw
signal filter_en_reg : std_logic_vector(3 downto 0);
signal filter_en_next : std_logic_vector(3 downto 0);
1043 markw
-- which filters are we using?
signal filter_sel_reg : std_logic_vector(2 downto 0); --hp/bp/lp
signal filter_sel_next : std_logic_vector(2 downto 0);

1058 markw
-- allow ch3 to be silent (direct audio path), if using it for modulation
1043 markw
signal ch3silent_reg : std_logic;
signal ch3silent_next : std_logic;

-- overall volume
signal vol_reg : std_logic_vector(3 downto 0);
signal vol_next : std_logic_vector(3 downto 0);

-- op regs
signal addr_decoded : std_logic_vector(31 downto 0);

signal audio_reg: std_logic_vector(15 downto 0);
1056 markw
-- osc regs
signal osc_a_reg : std_logic_vector(11 downto 0);
signal osc_b_reg : std_logic_vector(11 downto 0);
1062 markw
signal osc_c_reg : std_logic_vector(11 downto 0);
1056 markw
signal osc_a_lfsr_enable : std_logic;
signal osc_b_lfsr_enable : std_logic;
signal osc_c_lfsr_enable : std_logic;
1062 markw
signal sync_a : std_logic;
signal sync_b : std_logic;
signal sync_c : std_logic;
signal osc_a_sync_out : std_logic;
signal osc_b_sync_out : std_logic;
signal osc_c_sync_out : std_logic;
1213 markw
signal osc_a_changing : std_logic;
signal osc_b_changing : std_logic;
signal osc_c_changing : std_logic;
1062 markw
-- wavegen regs
signal wave_a_reg : std_logic_vector(11 downto 0);
signal wave_b_reg : std_logic_vector(11 downto 0);
signal wave_c_reg : std_logic_vector(11 downto 0);

-- envelope regs
signal envelope_a_reg : std_logic_vector(7 downto 0);
signal envelope_b_reg : std_logic_vector(7 downto 0);
signal envelope_c_reg : std_logic_vector(7 downto 0);
1266 markw
signal delay_lfsr_a : std_logic_vector(14 downto 0);
signal delay_lfsr_b : std_logic_vector(14 downto 0);
signal delay_lfsr_c : std_logic_vector(14 downto 0);
signal tapkey_a : std_logic_vector(3 downto 0);
signal tapkey_b : std_logic_vector(3 downto 0);
signal tapkey_c : std_logic_vector(3 downto 0);
signal tapmatches : std_logic_vector(2 downto 0);
1062 markw
-- amplitude modulator
1073 markw
signal channel_a_modulated : signed(15 downto 0);
signal channel_b_modulated : signed(15 downto 0);
signal channel_c_modulated : signed(15 downto 0);
1324 markw
signal channel_d : signed(15 downto 0);
1062 markw
-- prefilter
1073 markw
signal channel_prefilter : signed(15 downto 0);
signal channel_directsum : signed(15 downto 0);
1062 markw
-- filter
1078 markw
signal filter_lp : signed(17 downto 0); -- extra bit due to Jammer causing filter to clip
signal filter_bp : signed(17 downto 0);
signal filter_hp : signed(17 downto 0);
1324 markw
-- paddles
signal potx_reg : std_logic_vector(7 downto 0);
signal potx_next : std_logic_vector(7 downto 0);
signal poty_reg : std_logic_vector(7 downto 0);
signal poty_next : std_logic_vector(7 downto 0);
signal potcount_reg : std_logic_vector(8 downto 0);
signal potcount_next : std_logic_vector(8 downto 0);
signal potread_x_reg : std_logic;
signal potread_y_reg : std_logic;
signal potread_x_next : std_logic;
signal potread_y_next : std_logic;

-- do internal bus
signal do_out_next : std_logic_vector(7 downto 0);
signal do_out_reg : std_logic_vector(7 downto 0);
signal reset_readcount : std_logic;
signal readcount_reg : unsigned(15 downto 0);
signal readcount_next : unsigned(15 downto 0);
1043 markw
BEGIN
process(clk,reset_n)
begin
if (reset_n='0') then
freq_adj_channel_a_reg <= (others=>'0');
freq_adj_channel_b_reg <= (others=>'0');
freq_adj_channel_c_reg <= (others=>'0');
pulse_width_channel_a_reg <= (others=>'0');
pulse_width_channel_b_reg <= (others=>'0');
pulse_width_channel_c_reg <= (others=>'0');
waveselect_a_reg <= (others=>'0');
waveselect_b_reg <= (others=>'0');
waveselect_c_reg <= (others=>'0');
control_a_reg <= (others=>'0');
control_b_reg <= (others=>'0');
control_c_reg <= (others=>'0');
envelope_attack_a_reg <= (others=>'0');
envelope_attack_b_reg <= (others=>'0');
envelope_attack_c_reg <= (others=>'0');
envelope_decay_a_reg <= (others=>'0');
envelope_decay_b_reg <= (others=>'0');
envelope_decay_c_reg <= (others=>'0');
envelope_sustain_a_reg <= (others=>'0');
envelope_sustain_b_reg <= (others=>'0');
envelope_sustain_c_reg <= (others=>'0');
envelope_release_a_reg <= (others=>'0');
envelope_release_b_reg <= (others=>'0');
envelope_release_c_reg <= (others=>'0');
statevariable_fcutoff_reg <= (others=>'0');
1178 markw
statevariable_F_reg <= (others=>'0');
statevariable_Q_reg <= (others=>'0');
1217 markw
statevariable_1q_reg <= (others=>'0');
1209 markw
rom_state_reg <= rom_state_init;
1043 markw
filter_en_reg <= (others=>'0');
filter_sel_reg <= (others=>'0');
1062 markw
ch3silent_reg <= '0';
1043 markw
vol_reg <= (others=>'0');
1213 markw
statevariable_f_dirty_reg <= '1';
1217 markw
statevariable_q_dirty_reg <= '1';
1324 markw
potx_reg <= (others=>'0');
poty_reg <= (others=>'0');
potread_x_reg <= '0';
potread_y_reg <= '0';
potcount_reg <= (others=>'0');
do_out_reg <= (others=>'0');
readcount_reg <= (others=>'0');
1043 markw
elsif (clk'event and clk='1') then
freq_adj_channel_a_reg <= freq_adj_channel_a_next;
freq_adj_channel_b_reg <= freq_adj_channel_b_next;
freq_adj_channel_c_reg <= freq_adj_channel_c_next;
pulse_width_channel_a_reg <= pulse_width_channel_a_next;
pulse_width_channel_b_reg <= pulse_width_channel_b_next;
pulse_width_channel_c_reg <= pulse_width_channel_c_next;
waveselect_a_reg <= waveselect_a_next;
waveselect_b_reg <= waveselect_b_next;
waveselect_c_reg <= waveselect_c_next;
control_a_reg <= control_a_next;
control_b_reg <= control_b_next;
control_c_reg <= control_c_next;
envelope_attack_a_reg <= envelope_attack_a_next;
envelope_attack_b_reg <= envelope_attack_b_next;
envelope_attack_c_reg <= envelope_attack_c_next;
envelope_decay_a_reg <= envelope_decay_a_next;
envelope_decay_b_reg <= envelope_decay_b_next;
envelope_decay_c_reg <= envelope_decay_c_next;
envelope_sustain_a_reg <= envelope_sustain_a_next;
envelope_sustain_b_reg <= envelope_sustain_b_next;
envelope_sustain_c_reg <= envelope_sustain_c_next;
envelope_release_a_reg <= envelope_release_a_next;
envelope_release_b_reg <= envelope_release_b_next;
envelope_release_c_reg <= envelope_release_c_next;
statevariable_fcutoff_reg <= statevariable_fcutoff_next;
1178 markw
statevariable_F_reg <= statevariable_F_next;
1043 markw
statevariable_Q_Reg <= statevariable_Q_next;
1217 markw
statevariable_1q_reg <= statevariable_1q_next;
1209 markw
rom_state_reg <= rom_state_next;
1043 markw
filter_en_reg <= filter_en_next;
filter_sel_reg <= filter_sel_next;
ch3silent_reg <= ch3silent_next;
vol_reg <= vol_next;
1213 markw
statevariable_f_dirty_reg <= statevariable_f_dirty_next;
1217 markw
statevariable_q_dirty_reg <= statevariable_q_dirty_next;
1324 markw
potx_reg <= potx_next;
poty_reg <= poty_next;
potread_x_reg <= potread_x_next;
potread_y_reg <= potread_y_next;
potcount_reg <= potcount_next;
do_out_reg <= do_out_next;
readcount_reg <= readcount_next;
1043 markw
end if;
end process;

decode_addr1 : entity work.complete_address_decoder
generic map(width=>5)
port map (addr_in=>ADDR(4 downto 0), addr_decoded=>addr_decoded);

process(addr_decoded,write_enable,di,
freq_adj_channel_a_reg,
freq_adj_channel_b_reg,
freq_adj_channel_c_reg,
pulse_width_channel_a_reg,
pulse_width_channel_b_reg,
pulse_width_channel_c_reg,
waveselect_a_reg,
waveselect_b_reg,
waveselect_c_reg,
control_a_reg,
control_b_reg,
control_c_reg,
envelope_attack_a_reg,
envelope_attack_b_reg,
envelope_attack_c_reg,
envelope_decay_a_reg,
envelope_decay_b_reg,
envelope_decay_c_reg,
envelope_sustain_a_reg,
envelope_sustain_b_reg,
envelope_sustain_c_reg,
envelope_release_a_reg,
envelope_release_b_reg,
envelope_release_c_reg,
statevariable_fcutoff_reg,
statevariable_Q_Reg,
filter_en_reg,
filter_sel_reg,
ch3silent_reg,
vol_reg
)
begin
freq_adj_channel_a_next <= freq_adj_channel_a_reg;
freq_adj_channel_b_next <= freq_adj_channel_b_reg;
freq_adj_channel_c_next <= freq_adj_channel_c_reg;
pulse_width_channel_a_next <= pulse_width_channel_a_reg;
pulse_width_channel_b_next <= pulse_width_channel_b_reg;
pulse_width_channel_c_next <= pulse_width_channel_c_reg;
waveselect_a_next <= waveselect_a_reg;
waveselect_b_next <= waveselect_b_reg;
waveselect_c_next <= waveselect_c_reg;
control_a_next <= control_a_reg;
control_b_next <= control_b_reg;
control_c_next <= control_c_reg;
envelope_attack_a_next <= envelope_attack_a_reg;
envelope_attack_b_next <= envelope_attack_b_reg;
envelope_attack_c_next <= envelope_attack_c_reg;
envelope_decay_a_next <= envelope_decay_a_reg;
envelope_decay_b_next <= envelope_decay_b_reg;
envelope_decay_c_next <= envelope_decay_c_reg;
envelope_sustain_a_next <= envelope_sustain_a_reg;
envelope_sustain_b_next <= envelope_sustain_b_reg;
envelope_sustain_c_next <= envelope_sustain_c_reg;
envelope_release_a_next <= envelope_release_a_reg;
envelope_release_b_next <= envelope_release_b_reg;
envelope_release_c_next <= envelope_release_c_reg;
statevariable_fcutoff_next <= statevariable_fcutoff_reg;
1065 markw
statevariable_Q_next <= statevariable_Q_reg;
1043 markw
filter_en_next <= filter_en_reg;
filter_sel_next <= filter_sel_reg;
ch3silent_next <= ch3silent_reg;
vol_next <= vol_reg;
1178 markw
statevariable_f_changed <= '0';
1217 markw
statevariable_q_changed <= '0';
1043 markw
if (write_enable='1') then
--ch a
if (addr_decoded(0)='1') then
freq_adj_channel_a_next(7 downto 0) <= di;
end if;
if (addr_decoded(1)='1') then
freq_adj_channel_a_next(15 downto 8) <= di;
end if;
if (addr_decoded(2)='1') then
pulse_width_channel_a_next(7 downto 0) <= di;
end if;
if (addr_decoded(3)='1') then
pulse_width_channel_a_next(11 downto 8) <= di(3 downto 0);
end if;
if (addr_decoded(4)='1') then
control_a_next <= di(3 downto 0);
waveselect_a_next <= di(7 downto 4);
end if;
if (addr_decoded(5)='1') then
envelope_attack_a_next <= di(7 downto 4);
envelope_decay_a_next <= di(3 downto 0);
end if;
if (addr_decoded(6)='1') then
envelope_sustain_a_next <= di(7 downto 4);
envelope_release_a_next <= di(3 downto 0);
end if;

--ch b
if (addr_decoded(7)='1') then
freq_adj_channel_b_next(7 downto 0) <= di;
end if;
if (addr_decoded(8)='1') then
freq_adj_channel_b_next(15 downto 8) <= di;
end if;
if (addr_decoded(9)='1') then
pulse_width_channel_b_next(7 downto 0) <= di;
end if;
if (addr_decoded(10)='1') then
pulse_width_channel_b_next(11 downto 8) <= di(3 downto 0);
end if;
if (addr_decoded(11)='1') then
control_b_next <= di(3 downto 0);
waveselect_b_next <= di(7 downto 4);
end if;
if (addr_decoded(12)='1') then
envelope_attack_b_next <= di(7 downto 4);
envelope_decay_b_next <= di(3 downto 0);
end if;
if (addr_decoded(13)='1') then
envelope_sustain_b_next <= di(7 downto 4);
envelope_release_b_next <= di(3 downto 0);
end if;

--ch c
if (addr_decoded(14)='1') then
freq_adj_channel_c_next(7 downto 0) <= di;
end if;
if (addr_decoded(15)='1') then
freq_adj_channel_c_next(15 downto 8) <= di;
end if;
if (addr_decoded(16)='1') then
pulse_width_channel_c_next(7 downto 0) <= di;
end if;
if (addr_decoded(17)='1') then
pulse_width_channel_c_next(11 downto 8) <= di(3 downto 0);
end if;
if (addr_decoded(18)='1') then
control_c_next <= di(3 downto 0);
waveselect_c_next <= di(7 downto 4);
end if;
if (addr_decoded(19)='1') then
envelope_attack_c_next <= di(7 downto 4);
envelope_decay_c_next <= di(3 downto 0);
end if;
if (addr_decoded(20)='1') then
envelope_sustain_c_next <= di(7 downto 4);
envelope_release_c_next <= di(3 downto 0);
end if;

--filter
if (addr_decoded(21)='1') then
statevariable_fcutoff_next(2 downto 0) <= di(2 downto 0);
1178 markw
statevariable_f_changed <= '1';
1043 markw
end if;
if (addr_decoded(22)='1') then
statevariable_fcutoff_next(10 downto 3) <= di;
1178 markw
statevariable_f_changed <= '1';
1043 markw
end if;
if (addr_decoded(23)='1') then
statevariable_Q_next <= di(7 downto 4);
1217 markw
statevariable_q_changed <= '1';
1284 markw
filter_en_next <= di(3 downto 0);
1043 markw
end if;
if (addr_decoded(24)='1') then
1062 markw
ch3silent_next <= di(7);
1043 markw
filter_sel_next <= di(6 downto 4);
vol_next <= di(3 downto 0);
end if;
end if;
end process;

1324 markw
process(addr,addr_decoded,
do_out_reg,do_out_next,
read_enable,
1043 markw
wave_c_reg,
1071 markw
envelope_c_reg,
1324 markw
potx_reg,
poty_reg,
readcount_reg
1043 markw
)
begin
1324 markw
drive_do <= '1';
--ADDR(4) and ADDR(3) and or_reduce(ADDR(2 downto 0));
do_out_next <= do_out_reg;

reset_readcount <= '0';

if (read_enable='1') then
if (addr_decoded(25)='1') then
do_out_next <= potx_reg;
reset_readcount <= '1';
end if;
if (addr_decoded(26)='1') then
do_out_next <= poty_reg;
reset_readcount <= '1';
end if;
if (addr_decoded(27)='1') then
do_out_next <= wave_c_reg(11 downto 4);
reset_readcount <= '1';
end if;
if (addr_decoded(28)='1') then
do_out_next <= envelope_c_reg;
reset_readcount <= '1';
end if;
else
if (or_reduce(std_logic_vector(readcount_reg))='0') then
do_out_next <= (others=>'0');
end if;
1071 markw
end if;
1324 markw
do <= do_out_next;
1043 markw
end process;

1331 markw
process(readcount_reg,enable,reset_readcount,sidtype)
1324 markw
begin
readcount_next <= readcount_reg;

if (reset_readcount='1') then
if (sidtype ='1') then --6581
readcount_next <=to_unsigned(8000,16);
else
readcount_next <=to_unsigned(65535,16);
end if;
else
if (enable='1') then
readcount_next <=readcount_reg-1;
end if;
end if;
end process;

1043 markw
-- osc a
osc_a : entity work.SID_oscillator
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1056 markw
TEST => control_a_reg(3),
1062 markw
LFSR_ENABLE => osc_a_lfsr_enable,
1213 markw
CHANGING => osc_a_changing,
1043 markw
BITS_OUT => osc_a_reg,

SYNC_IN => sync_a,
SYNC_OUT => osc_a_sync_out,

ADJ => freq_adj_channel_a_reg
);
sync_a <= control_a_reg(1) and osc_c_sync_out;

-- osc b
osc_b : entity work.SID_oscillator
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1056 markw
TEST => control_b_reg(3),
1062 markw
LFSR_ENABLE => osc_b_lfsr_enable,
1213 markw
CHANGING => osc_b_changing,
1043 markw
BITS_OUT => osc_b_reg,

SYNC_IN => sync_b,
SYNC_OUT => osc_b_sync_out,

ADJ => freq_adj_channel_b_reg
);
sync_b <= control_b_reg(1) and osc_a_sync_out;

-- osc c
osc_c : entity work.SID_oscillator
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1056 markw
TEST => control_c_reg(3),
1062 markw
LFSR_ENABLE => osc_c_lfsr_enable,
1213 markw
CHANGING => osc_c_changing,
1043 markw
BITS_OUT => osc_c_reg,

SYNC_IN => sync_c,
SYNC_OUT => osc_c_sync_out,

ADJ => freq_adj_channel_c_reg
);
sync_c <= control_c_reg(1) and osc_b_sync_out;

1056 markw
--wave generator
wavegen_a : entity work.SID_wavegen
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
1284 markw
ENABLE => enable,
1043 markw
1213 markw
CHANGING => osc_a_changing,

1324 markw
DELAYSAWTOOTH => not(sidtype),
1056 markw
RINGMOD => control_a_reg(2),
RINGMOD_OSC_MSB => osc_c_reg(11),
1274 markw
TEST => control_a_next(3),
LFSR_ENABLE => (osc_a_lfsr_enable or (control_a_reg(3) xor control_a_next(3)) or (control_a_reg(3) and enable)),
1056 markw
OSC_IN => osc_a_reg,
1062 markw
PULSE_WIDTH_IN => pulse_width_channel_a_reg,
1043 markw
1056 markw
WAVESELECT_IN => waveselect_a_reg,
1043 markw
1213 markw
WAVE_DATA_NEEDED => wavegen_data_needed(0),
WAVE_DATA_READY => wavegen_data_ready(0),
WAVE_DATA => rom_data_word(11 downto 0),

1057 markw
WAVE_OUT => wave_a_reg
1043 markw
);
1056 markw
wavegen_b : entity work.SID_wavegen
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
1284 markw
ENABLE => enable,
1056 markw
1213 markw
CHANGING => osc_b_changing,

1324 markw
DELAYSAWTOOTH => not(sidtype),
1056 markw
RINGMOD => control_b_reg(2),
RINGMOD_OSC_MSB => osc_a_reg(11),
1274 markw
TEST => control_b_next(3),
LFSR_ENABLE => (osc_b_lfsr_enable or (control_b_reg(3) xor control_b_next(3)) or (control_b_reg(3) and enable)),
1056 markw
OSC_IN => osc_b_reg,
1062 markw
PULSE_WIDTH_IN => pulse_width_channel_b_reg,
1056 markw
WAVESELECT_IN => waveselect_b_reg,

1213 markw
WAVE_DATA_NEEDED => wavegen_data_needed(1),
WAVE_DATA_READY => wavegen_data_ready(1),
WAVE_DATA => rom_data_word(11 downto 0),

1057 markw
WAVE_OUT => wave_b_reg
1056 markw
);
wavegen_c : entity work.SID_wavegen
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
1284 markw
ENABLE => enable,
1056 markw
1213 markw
CHANGING => osc_c_changing,

1324 markw
DELAYSAWTOOTH => not(sidtype),
1056 markw
RINGMOD => control_c_reg(2),
RINGMOD_OSC_MSB => osc_b_reg(11),
1274 markw
TEST => control_c_next(3),
LFSR_ENABLE => (osc_c_lfsr_enable or (control_c_reg(3) xor control_c_next(3)) or (control_c_reg(3) and enable)),
1056 markw
OSC_IN => osc_c_reg,
1062 markw
PULSE_WIDTH_IN => pulse_width_channel_c_reg,
1056 markw
WAVESELECT_IN => waveselect_c_reg,

1213 markw
WAVE_DATA_NEEDED => wavegen_data_needed(2),
WAVE_DATA_READY => wavegen_data_ready(2),
WAVE_DATA => rom_data_word(11 downto 0),

1057 markw
WAVE_OUT => wave_c_reg
1056 markw
);

1057 markw
-- envelope
envelope_a : entity work.SID_envelope
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,
1057 markw
1266 markw
TAPMATCH => tapmatches(0),

1057 markw
ATTACK => envelope_attack_a_reg,
SUSTAIN => envelope_sustain_a_reg,
DECAY => envelope_decay_a_reg,
RELEASE_IN => envelope_release_a_reg,

GATE => control_a_reg(0),
1043 markw
1266 markw
ENVELOPE => envelope_a_reg,
DELAY_LFSR => delay_lfsr_a,
TAPKEY => tapkey_a
1057 markw
);

envelope_b : entity work.SID_envelope
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,
1057 markw
1266 markw
TAPMATCH => tapmatches(1),

1057 markw
ATTACK => envelope_attack_b_reg,
SUSTAIN => envelope_sustain_b_reg,
DECAY => envelope_decay_b_reg,
RELEASE_IN => envelope_release_b_reg,

GATE => control_b_reg(0),
1043 markw
1266 markw
ENVELOPE => envelope_b_reg,
DELAY_LFSR => delay_lfsr_b,
TAPKEY => tapkey_b
1057 markw
);

envelope_c : entity work.SID_envelope
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,
1057 markw
1266 markw
TAPMATCH => tapmatches(2),

1057 markw
ATTACK => envelope_attack_c_reg,
SUSTAIN => envelope_sustain_c_reg,
DECAY => envelope_decay_c_reg,
RELEASE_IN => envelope_release_c_reg,

GATE => control_c_reg(0),
1043 markw
1266 markw
ENVELOPE => envelope_c_reg,
DELAY_LFSR => delay_lfsr_c,
TAPKEY => tapkey_c
1043 markw
);

1266 markw
envelope_tapmatcher : entity work.SID_envelope_tapmatch
PORT MAP
(
CLK => clk,
RESET_N => reset_n,

DELAY_LFSR1 => delay_lfsr_a,
DELAY_LFSR2 => delay_lfsr_b,
DELAY_LFSR3 => delay_lfsr_c,

TAPKEY1 => tapkey_a,
TAPKEY2 => tapkey_b,
TAPKEY3 => tapkey_c,

TAPMATCHES => tapmatches
);

1043 markw
-- volume
1058 markw
vol_a : entity work.SID_amplitudeModulator
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1057 markw
WAVE => wave_a_reg,
ENVELOPE => envelope_a_reg,
1043 markw
1062 markw
MODULATED => channel_a_modulated
1043 markw
);
1057 markw
1058 markw
vol_b : entity work.SID_amplitudeModulator
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1057 markw
WAVE => wave_b_reg,
ENVELOPE => envelope_b_reg,
1043 markw
1062 markw
MODULATED => channel_b_modulated
1043 markw
);
1057 markw
1058 markw
vol_c : entity work.SID_amplitudeModulator
1043 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1057 markw
WAVE => wave_c_reg,
ENVELOPE => envelope_c_reg,
1043 markw
1062 markw
MODULATED => channel_c_modulated
1043 markw
);
1057 markw
1062 markw
prefilter: entity work.SID_preFilterSum
1058 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,
ENABLE => enable,

1324 markw
BIAS_CHANNEL => sidtype,
1284 markw
1062 markw
CHANNEL_A => channel_a_modulated,
CHANNEL_B => channel_b_modulated,
CHANNEL_C => channel_c_modulated,
CHANNEL_C_CUTDIRECT => ch3silent_reg,
1324 markw
CHANNEL_D => channel_d,
1058 markw
FILTER_EN => filter_en_reg,
1059 markw
PREFILTER_OUT => channel_prefilter,
DIRECT_OUT => channel_directsum
1058 markw
);

1217 markw
process(statevariable_F_reg, statevariable_1q_reg,
1213 markw
rom_state_reg,
statevariable_f_changed, statevariable_f_dirty_reg,
1217 markw
statevariable_q_changed, statevariable_q_dirty_reg,
1213 markw
rom_data, rom_data_word, rom_high_word, rom_ready,
wavegen_data_needed)
1178 markw
begin
1213 markw
statevariable_f_dirty_next <= statevariable_f_dirty_reg or statevariable_f_changed;
1178 markw
statevariable_F_next <= statevariable_F_reg;
1217 markw
statevariable_q_dirty_next <= statevariable_q_dirty_reg or statevariable_q_changed;
statevariable_1q_next <= statevariable_1q_reg;
1209 markw
rom_state_next <= rom_state_reg;
1208 markw
rom_request <= '0';
1178 markw
1217 markw
rom_addr_mux <= "000";
1213 markw
wavegen_data_ready <= (others=>'0');
1210 markw
1213 markw
if (rom_high_word='0') then
rom_data_word <= rom_data(15 downto 0);
else
rom_data_word <= rom_data(31 downto 16);
end if;

1209 markw
case rom_state_reg is
when rom_state_init =>
1213 markw
if (statevariable_f_dirty_reg='1') then
1209 markw
rom_state_next <= rom_state_romrequest_statevariable_f;
1178 markw
end if;
1217 markw
if (statevariable_q_dirty_reg='1') then
rom_state_next <= rom_state_romrequest_statevariable_q;
end if;
1213 markw
if (wavegen_data_needed(0)='1') then
rom_state_next <= rom_state_romrequest_wave_a;
end if;
if (wavegen_data_needed(1)='1') then
rom_state_next <= rom_state_romrequest_wave_b;
end if;
if (wavegen_data_needed(2)='1') then
rom_state_next <= rom_state_romrequest_wave_c;
end if;
1209 markw
when rom_state_romrequest_statevariable_f =>
1208 markw
rom_request <= '1';
1217 markw
rom_addr_mux <= "000";
1208 markw
if (rom_ready = '1') then
1213 markw
statevariable_f_dirty_next <= '0';
1262 markw
statevariable_F_next <= rom_data_word(12 downto 0);
1209 markw
rom_state_next <= rom_state_init;
1178 markw
end if;
1213 markw
when rom_state_romrequest_wave_a =>
rom_request <= '1';
1217 markw
rom_addr_mux <= "001";
1213 markw
wavegen_data_ready(0) <= rom_ready;
if (rom_ready = '1') then
rom_state_next <= rom_state_init;
end if;
when rom_state_romrequest_wave_b =>
rom_request <= '1';
1217 markw
rom_addr_mux <= "010";
1213 markw
wavegen_data_ready(1) <= rom_ready;
if (rom_ready = '1') then
rom_state_next <= rom_state_init;
end if;
when rom_state_romrequest_wave_c =>
rom_request <= '1';
1217 markw
rom_addr_mux <= "011";
1213 markw
wavegen_data_ready(2) <= rom_ready;
if (rom_ready = '1') then
rom_state_next <= rom_state_init;
end if;
1217 markw
when rom_state_romrequest_statevariable_q =>
rom_request <= '1';
rom_addr_mux <= "100";
if (rom_ready = '1') then
statevariable_q_dirty_next <= '0';
statevariable_1q_next <= signed(rom_data(17 downto 0));
rom_state_next <= rom_state_init;
end if;
1178 markw
when others =>
1209 markw
rom_state_next <= rom_state_init;
1178 markw
end case;
end process;

1210 markw
process(rom_addr_mux,
sidtype,
1213 markw
statevariable_fcutoff_reg,
1217 markw
statevariable_q_reg,
1213 markw
osc_a_reg,osc_b_reg,osc_c_reg,
waveselect_a_reg,waveselect_b_reg,waveselect_c_reg,
rom_wave_2bit,rom_wave_3bit,
rom_osc,rom_high_word)

1253 markw
variable rom_wave_addr: std_logic_vector(16 downto 0);
1324 markw
variable sidtype2: std_logic_vector(0 downto 0);
1210 markw
begin
rom_addr <= (others=>'0');
1213 markw
rom_high_word <= '0';
rom_wave_2bit <= (others=>'0');
rom_wave_3bit <= (others=>'0');
rom_osc <= (others=>'0');
1210 markw
1324 markw
sidtype2(0) := sidtype;

1213 markw
case rom_wave_3bit is
1324 markw
when "011" => -- ST
1213 markw
rom_wave_2bit <= "00";
1324 markw
when "101" => --P T
1213 markw
rom_wave_2bit <= "01";
1324 markw
when "110" => --PS
1213 markw
rom_wave_2bit <= "10";
1324 markw
when "111" => --PST
1213 markw
rom_wave_2bit <= "11";
when others =>
end case;


1324 markw
rom_wave_addr := std_logic_vector(unsigned(wave_base)+resize(unsigned(sidtype2(0 downto 0)&rom_wave_2bit&rom_osc),17)); --1:2:11
1213 markw
1210 markw
case rom_addr_mux is
1217 markw
when "000" =>
1324 markw
rom_addr <= "00000"&std_logic_vector(unsigned('0'&sidtype2(0 downto 0))+1)&statevariable_fcutoff_reg(10 downto 1);
1213 markw
rom_high_word <= statevariable_fcutoff_reg(0);
1217 markw
when "001" =>
1213 markw
rom_osc <= osc_a_reg(11 downto 1);
rom_wave_3bit <= waveselect_a_reg(2 downto 0);
rom_addr <= rom_wave_addr;
rom_high_word <= osc_a_reg(0);
1217 markw
when "010" =>
1213 markw
rom_osc <= osc_b_reg(11 downto 1);
rom_wave_3bit <= waveselect_b_reg(2 downto 0);
rom_addr <= rom_wave_addr;
rom_high_word <= osc_b_reg(0);
1217 markw
when "011" =>
1213 markw
rom_osc <= osc_c_reg(11 downto 1);
rom_wave_3bit <= waveselect_c_reg(2 downto 0);
rom_addr <= rom_wave_addr;
rom_high_word <= osc_c_reg(0);
1217 markw
when "100" =>
1324 markw
rom_addr <= "000000010000"&sidtype2(0 downto 0)&statevariable_q_reg;
1210 markw
when others =>
end case;

end process;

1058 markw
variable_state_filter : entity work.SID_filter
PORT MAP
(
CLK => clk,
RESET_N => reset_n,

1062 markw
INPUT => channel_prefilter,
1059 markw
1324 markw
SIDTYPE => sidtype,
1265 markw
1059 markw
LOWPASS => filter_lp,
BANDPASS => filter_bp,
HIGHPASS => filter_hp,

1178 markw
--CUTOFF_FREQUENCY => statevariable_fcutoff_reg,
1247 markw
F_BP => unsigned(filter_f_bp),
F_HP => unsigned(filter_f_hp),
1217 markw
Q => statevariable_1q_reg
1058 markw
);

1062 markw
postfilter: entity work.SID_postFilterSum
1059 markw
PORT MAP
(
CLK => clk,
RESET_N => reset_n,

DIRECT => channel_directsum,

FILTER_LP => filter_lp,
FILTER_BP => filter_bp,
FILTER_HP => filter_hp,
FILTER_SEL => filter_sel_reg,

1062 markw
VOLUME => vol_reg,
1059 markw
1061 markw
CHANNEL_OUT => audio_reg
1059 markw
);

1324 markw
-- paddles
process (potx_reg,poty_reg,pot_x,pot_y,potcount_reg,enable,potread_x_reg,potread_y_reg)
begin
potx_next <= potx_reg;
poty_next <= poty_reg;
potread_x_next <= potread_x_reg and not(potcount_reg(8));
potread_y_next <= potread_y_reg and not(potcount_reg(8));
potcount_next <= potcount_reg;

pot_reset <= potcount_reg(8);
if (enable='1') then
potcount_next <= std_logic_vector(unsigned(potcount_reg)+1);
if ((pot_x='1' or potcount_reg="011111111") and potread_x_reg='0') then
potx_next <= potcount_reg(7 downto 0);
potread_x_next <= '1';
end if;
if ((pot_y='1' or potcount_reg="011111111") and potread_y_reg='0') then
poty_next <= potcount_reg(7 downto 0);
potread_y_next <= '1';
end if;
end if;

end process;

-- ext audio
process(ext_adc,ext)
begin
--EXT : in std_logic_vector(1 downto 0); -- 00=GND,01=digifix,10=ADC
--EXT_ADC : in unsigned(7 downto 0);
channel_d <= to_signed(0,16);

case EXT is
when "01" =>
channel_d <= signed("000"&ext&"00000000000");
when "10" =>
channel_d <= signed(not(ext_adc(15))&ext_adc(14 downto 0));
when others=>
end case;
end process;
1061 markw
1057 markw
--------------------------------
-- TODO
1071 markw
-- 1) DONE:check above works!
1247 markw
-- 2) DONE: wave combinations need to read flash
1057 markw
-- 3) DONE:envelope/gate
1058 markw
-- 4) DONE:amplitude modulation
1061 markw
-- 5) DONE:filter on/off
-- 5) DONE:filter (state variable as per info found)
-- 6) DONE:volume
1071 markw
-- 7) DONE: read registers: pot, osc3 etc
1247 markw
-- 8) 6581! buggy_variable_state_filter : entity work.SID_filter
-- ref: https://bel.fi/alankila/c64-sw/index-cpp.html
-- see distortion section
1057 markw
--------------------------------
1043 markw
--outputs
AUDIO <= audio_reg;
1073 markw
DEBUG_EV1 <= unsigned(envelope_a_reg);
DEBUG_WV1 <= unsigned(wave_a_reg);
DEBUG_AM1 <= channel_a_modulated;
1178 markw
1262 markw
FILTER_BP_OUT <= filter_bp(17 downto 8);
FILTER_HP_OUT <= filter_hp(17 downto 8);
1247 markw
FILTER_F_OUT <= statevariable_F_reg;

1043 markw
end vhdl;