diff --git a/files.qip b/files.qip index 1425d95..2a2c8e1 100644 --- a/files.qip +++ b/files.qip @@ -27,7 +27,12 @@ set_global_assignment -name SYSTEMVERILOG_FILE rtl/Auto_Run.sv set_global_assignment -name SYSTEMVERILOG_FILE rtl/ps2_mouse.v set_global_assignment -name SYSTEMVERILOG_FILE rtl/sdram/sdram2.sv set_global_assignment -name SYSTEMVERILOG_FILE rtl/wd1793.sv +set_global_assignment -name SYSTEMVERILOG_FILE rtl/music_speech.sv +set_global_assignment -name SYSTEMVERILOG_FILE rtl/sp0256.vhd +set_global_assignment -name SYSTEMVERILOG_FILE rtl/ym2149_audio.vhd +set_global_assignment -name SYSTEMVERILOG_FILE rtl/Music_Speech_SOC.sv set_global_assignment -name SYSTEMVERILOG_FILE CoCo3.sv +set_global_assignment -name VHDL_FILE rtl/sram/CoCo_ram_6116.vhd set_global_assignment -name VHDL_FILE rtl/sram/coco_vid_ram_buf.vhd set_global_assignment -name SYSTEMVERILOG_FILE rtl/coco_mem_fetch.sv set_global_assignment -name VERILOG_FILE rtl/EE_Cold_Bt.v diff --git a/rtl/Music_Speech_SOC.sv b/rtl/Music_Speech_SOC.sv new file mode 100644 index 0000000..90be1ff --- /dev/null +++ b/rtl/Music_Speech_SOC.sv @@ -0,0 +1,95 @@ +//////////////////////////////////////////////////////////////////////////////// +// Project Name: CoCo3FPGA Version 5.x.x +// File Name: Music_Speech.sv +// +// CoCo3 in an FPGA +// +//////////////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////////////// +// +// Color Computer 3 compatible system on a chip +// +// +// Copyright (c) 2008 Gary Becker (gary_l_becker@yahoo.com) +// +// All rights reserved +// +// Redistribution and use in source and synthezised forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// Redistributions in synthesized form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Please report bugs to the author, but before you do so, please +// make sure that this is not a derivative work and that +// you have the latest version of this file. +// +// +//////////////////////////////////////////////////////////////////////////////// +// Stan Hodge +// +// Music_Speech.sv by Stan Hodge 02/16/24 +//////////////////////////////////////////////////////////////////////////////// + +module Music_Speech_SOC( +input RESET_N, +input CLKIN, +input CLK_1_78, + +input INT1_N, +input INT3_N, + +output [7:0] PORT_A, +output [7:0] PORT_B, +output [7:0] PORT_C, +output [7:0] PORT_D_OUT, +input [7:0] PORT_D_IN +); + +////////////////////////////////////////////////////////////////////////////////// +// 09 Processor + +wire VMA, RW_N, HALT, CPU_IRQ_N, CPU_FIRQ_N, NMI_09; +wire [15:0] ADDRESS; +wire [7:0] DATA_IN, DATA_OUT; + +// CPU section copyrighted by John Kent +cpu09 GLBCPU09( + .clk(CLKIN), + .rst(!RESET_N), + .vma(VMA), + .addr(ADDRESS), + .rw(RW_N), + .data_in(DATA_IN), + .data_out(DATA_OUT), + .halt(HALT), + .hold(hold | ~cpu_ena), // ??? + .irq(!CPU_IRQ_N), + .firq(!CPU_FIRQ_N), + .nmi(NMI_09) +); + + + +endmodule diff --git a/rtl/SP0256/compressor.sv b/rtl/SP0256/compressor.sv new file mode 100644 index 0000000..3289b45 --- /dev/null +++ b/rtl/SP0256/compressor.sv @@ -0,0 +1,66 @@ +//============================================================================ +// Audio compressor (signed samples) +// +// Copyright (C) 2018 Sorgelig +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 2 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +//============================================================================ + +module compressor +( + input clk, + input [9:0] din, + output [8:0] dout +); + +always @(posedge clk) dout <= {din[9], din[9] ? ~tbl[~din[8:0]] : tbl[din[8:0]]}; + +wire [7:0] tbl[0:511] = +'{ + 8'h00, 8'h01, 8'h02, 8'h03, 8'h04, 8'h06, 8'h07, 8'h08, 8'h09, 8'h0B, 8'h0C, 8'h0D, 8'h0E, 8'h0F, 8'h11, 8'h12, + 8'h13, 8'h14, 8'h16, 8'h17, 8'h18, 8'h19, 8'h1B, 8'h1C, 8'h1D, 8'h1E, 8'h1F, 8'h21, 8'h22, 8'h23, 8'h24, 8'h25, + 8'h27, 8'h28, 8'h29, 8'h2A, 8'h2C, 8'h2D, 8'h2E, 8'h2F, 8'h30, 8'h32, 8'h33, 8'h34, 8'h35, 8'h36, 8'h37, 8'h39, + 8'h3A, 8'h3B, 8'h3C, 8'h3D, 8'h3F, 8'h40, 8'h41, 8'h42, 8'h43, 8'h44, 8'h46, 8'h47, 8'h48, 8'h49, 8'h4A, 8'h4B, + 8'h4D, 8'h4E, 8'h4F, 8'h50, 8'h51, 8'h52, 8'h53, 8'h55, 8'h56, 8'h57, 8'h58, 8'h59, 8'h5A, 8'h5B, 8'h5C, 8'h5E, + 8'h5F, 8'h60, 8'h61, 8'h62, 8'h63, 8'h64, 8'h65, 8'h66, 8'h67, 8'h69, 8'h6A, 8'h6B, 8'h6C, 8'h6D, 8'h6E, 8'h6F, + 8'h70, 8'h71, 8'h72, 8'h73, 8'h74, 8'h75, 8'h76, 8'h77, 8'h78, 8'h79, 8'h7A, 8'h7B, 8'h7C, 8'h7D, 8'h7E, 8'h7F, + 8'h80, 8'h81, 8'h82, 8'h83, 8'h84, 8'h85, 8'h86, 8'h87, 8'h88, 8'h89, 8'h8A, 8'h8B, 8'h8C, 8'h8D, 8'h8E, 8'h8F, + 8'h90, 8'h91, 8'h92, 8'h93, 8'h94, 8'h95, 8'h96, 8'h96, 8'h97, 8'h98, 8'h99, 8'h9A, 8'h9B, 8'h9C, 8'h9D, 8'h9E, + 8'h9E, 8'h9F, 8'hA0, 8'hA1, 8'hA2, 8'hA3, 8'hA4, 8'hA4, 8'hA5, 8'hA6, 8'hA7, 8'hA8, 8'hA9, 8'hA9, 8'hAA, 8'hAB, + 8'hAC, 8'hAD, 8'hAD, 8'hAE, 8'hAF, 8'hB0, 8'hB1, 8'hB1, 8'hB2, 8'hB3, 8'hB4, 8'hB4, 8'hB5, 8'hB6, 8'hB7, 8'hB7, + 8'hB8, 8'hB9, 8'hB9, 8'hBA, 8'hBB, 8'hBC, 8'hBC, 8'hBD, 8'hBE, 8'hBE, 8'hBF, 8'hC0, 8'hC0, 8'hC1, 8'hC2, 8'hC2, + 8'hC3, 8'hC4, 8'hC4, 8'hC5, 8'hC6, 8'hC6, 8'hC7, 8'hC8, 8'hC8, 8'hC9, 8'hC9, 8'hCA, 8'hCB, 8'hCB, 8'hCC, 8'hCC, + 8'hCD, 8'hCE, 8'hCE, 8'hCF, 8'hCF, 8'hD0, 8'hD0, 8'hD1, 8'hD2, 8'hD2, 8'hD3, 8'hD3, 8'hD4, 8'hD4, 8'hD5, 8'hD5, + 8'hD6, 8'hD6, 8'hD7, 8'hD7, 8'hD8, 8'hD8, 8'hD9, 8'hD9, 8'hDA, 8'hDA, 8'hDB, 8'hDB, 8'hDC, 8'hDC, 8'hDD, 8'hDD, + 8'hDE, 8'hDE, 8'hDE, 8'hDF, 8'hDF, 8'hE0, 8'hE0, 8'hE1, 8'hE1, 8'hE1, 8'hE2, 8'hE2, 8'hE3, 8'hE3, 8'hE3, 8'hE4, + 8'hE4, 8'hE5, 8'hE5, 8'hE5, 8'hE6, 8'hE6, 8'hE6, 8'hE7, 8'hE7, 8'hE7, 8'hE8, 8'hE8, 8'hE9, 8'hE9, 8'hE9, 8'hEA, + 8'hEA, 8'hEA, 8'hEA, 8'hEB, 8'hEB, 8'hEB, 8'hEC, 8'hEC, 8'hEC, 8'hED, 8'hED, 8'hED, 8'hEE, 8'hEE, 8'hEE, 8'hEE, + 8'hEF, 8'hEF, 8'hEF, 8'hEF, 8'hF0, 8'hF0, 8'hF0, 8'hF0, 8'hF1, 8'hF1, 8'hF1, 8'hF1, 8'hF2, 8'hF2, 8'hF2, 8'hF2, + 8'hF3, 8'hF3, 8'hF3, 8'hF3, 8'hF3, 8'hF4, 8'hF4, 8'hF4, 8'hF4, 8'hF4, 8'hF5, 8'hF5, 8'hF5, 8'hF5, 8'hF5, 8'hF6, + 8'hF6, 8'hF6, 8'hF6, 8'hF6, 8'hF6, 8'hF7, 8'hF7, 8'hF7, 8'hF7, 8'hF7, 8'hF7, 8'hF8, 8'hF8, 8'hF8, 8'hF8, 8'hF8, + 8'hF8, 8'hF8, 8'hF9, 8'hF9, 8'hF9, 8'hF9, 8'hF9, 8'hF9, 8'hF9, 8'hF9, 8'hFA, 8'hFA, 8'hFA, 8'hFA, 8'hFA, 8'hFA, + 8'hFA, 8'hFA, 8'hFA, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFB, 8'hFC, 8'hFC, + 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFC, 8'hFD, 8'hFD, 8'hFD, + 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, 8'hFD, + 8'hFD, 8'hFD, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, + 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFE, 8'hFF +}; + +endmodule diff --git a/rtl/SP0256/sp0256.vhd b/rtl/SP0256/sp0256.vhd new file mode 100644 index 0000000..b6dbe12 --- /dev/null +++ b/rtl/SP0256/sp0256.vhd @@ -0,0 +1,444 @@ +--------------------------------------------------------------------------------- +-- sp0256 by Dar (darfpga@aol.fr) (14/04/2018) +-- http://darfpga.blogspot.fr +--------------------------------------------------------------------------------- +-- Educational use only +-- Do not redistribute synthetized file with roms +-- Do not redistribute roms whatever the form +-- Use at your own risk +--------------------------------------------------------------------------------- +-- +-- SP0256-al2 prom decoding scheme and speech synthesis algorithm are from : +-- +-- Copyright Joseph Zbiciak, all rights reserved. +-- Copyright tim lindner, all rights reserved. +-- +-- See C source code and license in sp0256.c from MAME source +-- +-- VHDL code is by Dar. +-- +--------------------------------------------------------------------------------- +-- +-- One allophone is made of N parts (called here after lines), each part has a +-- 16 bytes descriptor. One descriptor (for one part) contains one repeat value +-- one amplitude value, one period value and 2x6 filtering coefficients. +-- +-- for line_cnt from 0 to nb_line-1 (part) +-- for line_rpt from 0 to line_rpt-1 (repeat) +-- for per_cnt from 0 to line_per-1 (period) +-- produce 1 sample +-- +-- One sample is the output of the 6 stages filter. Each filter stage is fed by +-- the output of the previous stage, the first stage is fed by the source sample +-- +-- when line_per != 0 source sample is set to amplitude value only once at the +-- begin of each repeat (per_cnt==0) then source sample is set to 0 +-- +-- when line_per == 0 source sample is set to amplitude value only at the begin +-- of each repeat (per_cnt==0) then source sample sign is toggled (+/-) when then +-- random noise generator lsb equal 1. In that case actual line_per is set to 64 +-- +-- +-- Sound sample frequency is 10kHz. I make a 25 stages linear state machine +-- running at 250kHz that produce one sound sample per cycle. +-- +-- As long as one allophones is available the state machine runs permanently and +-- there is zero latency between allophones. +-- +-- During one (each) cycle the state machine: +-- +-- - fetch new allophone or go on with current one if not finished +-- - get allophone first line descriptor address from rom entry table +-- - get allophone nb_line from rom entry table and jump to first line address +-- - get allophone line_rpt from rom current line descriptor +-- - get allophone amplitude from rom current line descriptor +-- manage source amplitude, reset filter if needed +-- - get allophone line_per from rom current line descriptor +-- - address filter coefficients F/B within rom current line descriptor, +-- feed filter input, update filter state with computation output +-- - rescale last filter stage output to audio output +-- - manage per_cnt, rpt_cnt, line_cnt and random noise generator +-- +-- Filter computation: +-- +-- Filter coefficients F or B index is get from rom current line descriptor +-- (address managed by state machine), value is converted thru coeff_array +-- table. Coefficient index has a sign bit to be managed: +-- +-- if index sign bit = 0, filter coefficient <= -coeff_array(index) +-- if index sign bit = 1, filter coefficient <= coeff_array(-index) +-- +-- During one state machine cycle each filter is updated once. +-- One filter update require two state machine steps: +-- +-- step 1 +-- sum_in1 <= filter input +-- sum_in2 <= filter coefficient F * filter state z1 / 256 +-- sum_out <= sum_in1 + sum_in2 +-- step 2 +-- sum_in1 <= sum_out +-- sum_in2 <= filter coefficient B * filter state z2 / 512 +-- sum_out <= sum_in1 + sum_in2 +-- filter state z1 <= sum_in1 + sum_in2 +-- filter state z2 <= filter state z1 +-- +-- (sum_out will be limited to -32768/+32767) +-- +-- Audio output scaling to 10bits unsigned: +-- +-- what : +-- Last filter output is limited to -8192/+8191 +-- Then divided by 16 => -512/+511 +-- Then offset by 512 => 0/1023 +-- +-- how: +-- if X > 8191, Y <= 1023 +-- elsif X < -8192, Y <= 0 +-- else Y <= (X/16)+512 +-- +--------------------------------------------------------------------------------- + +library ieee; +use ieee.std_logic_1164.ALL; +use ieee.std_logic_unsigned.all; +use ieee.numeric_std.all; + +entity sp0256 is +port +( + clock : in std_logic; + ce : in std_logic; + reset : in std_logic; + + input_rdy : out std_logic; + allophone : in std_logic_vector(5 downto 0); + trig_allophone : in std_logic; + + audio_out : out signed(9 downto 0) +); +end sp0256; + +architecture syn of sp0256 is + + signal rom_addr : std_logic_vector(11 downto 0); + signal rom_do : std_logic_vector( 7 downto 0); + + signal stage : integer range 0 to 24; -- stage counter 0-24; + + signal allo_entry : std_logic_vector(7 downto 0); + signal allo_addr_lsb, allo_addr_msb : std_logic_vector(7 downto 0); + signal allo_nb_line : std_logic_vector(7 downto 0); + signal line_rpt, line_per : std_logic_vector(7 downto 0); + signal line_amp_lsb, line_amp_msb : std_logic_vector(7 downto 0); + + signal amp, filter, coeff : signed(15 downto 0); + signal sum_in2 : signed(31 downto 0); + signal sum_in1,sum_out_ul : signed(15 downto 0); + signal sum_out : signed(15 downto 0); + signal divider : std_logic; + signal audio : signed(9 downto 0); + signal audio_int : signed(8 downto 0); + + signal is_noise : std_logic; + signal noise_rng : std_logic_vector(15 downto 0) := X"0001"; + + signal f0_z1,f0_z2 : signed(15 downto 0); + signal f1_z1,f1_z2 : signed(15 downto 0); + signal f2_z1,f2_z2 : signed(15 downto 0); + signal f3_z1,f3_z2 : signed(15 downto 0); + signal f4_z1,f4_z2 : signed(15 downto 0); + signal f5_z1,f5_z2 : signed(15 downto 0); + + signal input_rdy_in : std_logic; + signal sound_on : std_logic := '0'; + signal trig_allophone_r : std_logic; + signal line_cnt, rpt_cnt, per_cnt : std_logic_vector(7 downto 0); + + signal coeff_idx : std_logic_vector(6 downto 0); + + type coeff_array_t is array(0 to 127) of integer range 0 to 511; + constant coeff_array : coeff_array_t := ( + 0, 9, 17, 25, 33, 41, 49, 57, + 65, 73, 81, 89, 97, 105, 113, 121, + 129, 137, 145, 153, 161, 169, 177, 185, + 193, 201, 209, 217, 225, 233, 241, 249, + 257, 265, 273, 281, 289, 297, 301, 305, + 309, 313, 317, 321, 325, 329, 333, 337, + 341, 345, 349, 353, 357, 361, 365, 369, + 373, 377, 381, 385, 389, 393, 397, 401, + 405, 409, 413, 417, 421, 425, 427, 429, + 431, 433, 435, 437, 439, 441, 443, 445, + 447, 449, 451, 453, 455, 457, 459, 461, + 463, 465, 467, 469, 471, 473, 475, 477, + 479, 481, 482, 483, 484, 485, 486, 487, + 488, 489, 490, 491, 492, 493, 494, 495, + 496, 497, 498, 499, 500, 501, 502, 503, + 504, 505, 506, 507, 508, 509, 510, 511); + + component compressor is port + ( + clk : in std_logic; + din : in signed(9 downto 0); + dout : out signed(8 downto 0) + ); + end component; + +begin + +input_rdy <= input_rdy_in; + +-- stage counter : Fs=250k/25 = 10kHz +process (clock, reset) + begin + if reset='1' then + stage <= 0; + else + if rising_edge(clock) then + if ce = '1' then + if stage >= 24 then + stage <= 0; + else + stage <= stage + 1; + end if; + end if; + end if; + end if; +end process; + +process (clock, reset) + begin + if reset='1' then + input_rdy_in <= '1'; + sound_on <= '0'; + noise_rng <= X"0001"; + else + if rising_edge(clock) then if ce = '1' then + + trig_allophone_r <= trig_allophone; + if trig_allophone = '1' and trig_allophone_r = '0' then + input_rdy_in <= '0'; + end if; + + if sound_on = '0' then + + if stage = 0 and input_rdy_in = '0' then + allo_entry <= allophone*"11"; + rom_addr <= X"0"&(allophone*"11"); + line_cnt <= (others => '0'); + rpt_cnt <= (others => '0'); + per_cnt <= (others => '0'); + sound_on <= '1'; + input_rdy_in <= '1'; + end if; + + else -- sound is on + + case stage is + when 0 => + rom_addr <= X"0"&allo_entry; + when 1 => + --allo_addr_msb <= rom_do; + rom_addr <= rom_addr + '1'; + when 2 => + allo_addr_lsb <= rom_do; + rom_addr <= rom_addr + '1'; + when 3 => + allo_nb_line <= rom_do - '1'; + rom_addr <= (allo_addr_lsb +line_cnt) & X"0"; + when 4 => + line_rpt <= rom_do - '1'; + rom_addr <= rom_addr + '1'; + when 5 => + line_amp_msb <= rom_do; + rom_addr <= rom_addr + '1'; + when 6 => + + if per_cnt = X"00" then + amp <= signed(line_amp_msb & rom_do); + else + if is_noise = '1' then + if noise_rng(0) = '1' then + amp <= -amp; + end if; + else + amp <= (others => '0'); + end if; + end if; + + if per_cnt = X"00"then + f0_z1 <= (others => '0'); f0_z2 <= (others => '0'); + f1_z1 <= (others => '0'); f1_z2 <= (others => '0'); + f2_z1 <= (others => '0'); f2_z2 <= (others => '0'); + f3_z1 <= (others => '0'); f3_z2 <= (others => '0'); + f4_z1 <= (others => '0'); f4_z2 <= (others => '0'); + f5_z1 <= (others => '0'); f5_z2 <= (others => '0'); + end if; + + rom_addr <= rom_addr + '1'; + + when 7 => + if rom_do = X"00" then + line_per <= X"40"; + is_noise <= '1'; + else + line_per <= rom_do - '1'; + is_noise <= '0'; + end if; + sum_in1 <= amp; + filter <= f0_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 8 => + sum_in1 <= sum_out; + filter <= f0_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 9 => + f0_z1 <= sum_out; + f0_z2 <= f0_z1; + sum_in1 <= sum_out; + filter <= f1_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 10 => + sum_in1 <= sum_out; + filter <= f1_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 11 => + f1_z1 <= sum_out; + f1_z2 <= f1_z1; + sum_in1 <= sum_out; + filter <= f2_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 12 => + sum_in1 <= sum_out; + filter <= f2_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 13 => + f2_z1 <= sum_out; + f2_z2 <= f2_z1; + sum_in1 <= sum_out; + filter <= f3_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 14 => + sum_in1 <= sum_out; + filter <= f3_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 15 => + f3_z1 <= sum_out; + f3_z2 <= f3_z1; + sum_in1 <= sum_out; + filter <= f4_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 16 => + sum_in1 <= sum_out; + filter <= f4_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 17 => + f4_z1 <= sum_out; + f4_z2 <= f4_z1; + sum_in1 <= sum_out; + filter <= f5_z1; + divider <= '0'; + rom_addr <= rom_addr + '1'; + when 18 => + sum_in1 <= sum_out; + filter <= f5_z2; + divider <= '1'; + rom_addr <= rom_addr + '1'; + + when 19 => + f5_z1 <= sum_out; + f5_z2 <= f5_z1; + + if sum_out > 510*16 then + audio <= to_signed(511,10); + elsif sum_out < -511*16 then + audio <= to_signed(-512,10); + else + audio <= sum_out(13 downto 4); + end if; + + when 20 => + if per_cnt >= line_per then + per_cnt <= (others => '0'); + if rpt_cnt >= line_rpt then + rpt_cnt <= (others => '0'); + if line_cnt >= allo_nb_line then + line_cnt <= (others => '0'); + sound_on <= '0'; + else + line_cnt <= line_cnt + '1'; + end if; + is_noise <= '0'; + else + rpt_cnt <= rpt_cnt + '1'; + end if; + else + per_cnt <= per_cnt + '1'; + end if; + + if noise_rng(0) = '1' then + noise_rng <= ('0' & noise_rng(15 downto 1) ) xor X"4001"; + else + noise_rng <= '0' & noise_rng(15 downto 1); + end if; + + when others => null; + end case; + + end if; + + end if; end if; + end if; +end process; + +compr: compressor +port map +( + clk => clock, + din => audio, + dout => audio_int +); + +audio_out <= audio_int(8) & audio_int; + +-- filter computation +coeff_idx <= rom_do(6 downto 0) when rom_do(7)='0' else + not(rom_do(6 downto 0)) + '1'; + +coeff <= -to_signed(coeff_array(to_integer(unsigned(coeff_idx))),16) when rom_do(7)='0' else + to_signed(coeff_array(to_integer(unsigned(coeff_idx))),16); + +sum_in2 <= (filter * coeff) / 256 when divider = '0' else + (filter * coeff) / 512 ; + +sum_out_ul <= sum_in1 + sum_in2(15 downto 0); + +sum_out <= to_signed( 32767,16) when sum_out_ul > 32767 else + to_signed(-32768,16) when sum_out_ul < -32768 else + sum_out_ul; + + +-- sp0256-al2 prom (decoded) +sp0256_al2_decoded : entity work.sp0256_al2_decoded +port map( + clk => clock, + addr => rom_addr, + data => rom_do +); + + +end syn; diff --git a/rtl/SP0256/sp0256_al2_decoded.vhd b/rtl/SP0256/sp0256_al2_decoded.vhd new file mode 100644 index 0000000..c01ad89 --- /dev/null +++ b/rtl/SP0256/sp0256_al2_decoded.vhd @@ -0,0 +1,373 @@ +--------------------------------------------------------------------------------- +-- sp0256 by Dar (darfpga@aol.fr) (14/04/2018) +-- http://darfpga.blogspot.fr +--------------------------------------------------------------------------------- +-- Educational use only +-- Do not redistribute synthetized file with roms +-- Do not redistribute roms whatever the form +-- Use at your own risk +--------------------------------------------------------------------------------- +-- +-- SP0256-al2 prom decoding scheme and speech synthesis algorithm are from : +-- +-- Copyright Joseph Zbiciak, all rights reserved. +-- Copyright tim lindner, all rights reserved. +-- +-- See C source code and license in sp0256.c from MAME source +-- +-- VHDL code is by Dar. +-- +--------------------------------------------------------------------------------- +-- Original sp0256 prom is bit compressed. I used sp0256.c from MAME to produce +-- an uncompressed and more easy format to be used in FPGA. +-- +-- Original prom is 2K, uncompressed prom is 4k. +-- +-- Uncompressed format is : +-- +-- 64 entries table of 3 bytes/entry : adr_msb, adr_lsb, nb_line (nb parts) +-- (1 entry per allophone) +-- +-- Each allophone is made of #nb_line parts +-- +-- Each part is described by 16 bytes (one line) : +-- +-- rpt : repeat nb +-- amp_msb, amp_lsb : amplitude +-- per : periode (number of sample to be produced for each rpt) +-- F0|B0|F1|B1|F2|B2|F3|B3|F4|B4|F5|B5 : filtering coefficients +-- +--------------------------------------------------------------------------------- +library ieee; +use ieee.std_logic_1164.all,ieee.numeric_std.all; + +entity sp0256_al2_decoded is +port ( + clk : in std_logic; + addr : in std_logic_vector(11 downto 0); + data : out std_logic_vector(7 downto 0) +); +end entity; + +architecture prom of sp0256_al2_decoded is + type rom is array(0 to 4095) of std_logic_vector(7 downto 0); + signal rom_data: rom := ( +-- adr_msb|adr_lsb|nb_line +X"00",X"0C",X"01", -- #00 : 000-002 +X"00",X"0D",X"01", -- #01 : 003-005 +X"00",X"0E",X"01", -- #02 : 006-008 +X"00",X"0F",X"01", -- #03 : 009-00B +X"00",X"10",X"01", -- #04 : 00C-00E +X"00",X"11",X"0A", -- #05 : 00F-011 +X"00",X"1B",X"0A", -- #06 : 012-014 +X"00",X"25",X"01", -- #07 : 015-017 +X"00",X"26",X"03", -- #08 : 018-01A +X"00",X"29",X"03", -- #09 : 01B-01D +X"00",X"2C",X"03", -- #0A : 01E-020 +X"00",X"2F",X"03", -- #0B : 021-023 +X"00",X"32",X"01", -- #0C : 024-026 +X"00",X"33",X"04", -- #0D : 027-029 +X"00",X"37",X"06", -- #0E : 02A-02C +X"00",X"3D",X"01", -- #0F : 02D-02F +X"00",X"3E",X"04", -- #10 : 030-032 +X"00",X"42",X"03", -- #11 : 033-035 +X"00",X"45",X"02", -- #12 : 036-038 +X"00",X"47",X"06", -- #13 : 039-03B +X"00",X"4D",X"08", -- #14 : 03C-03E +X"00",X"55",X"02", -- #15 : 03F-041 +X"00",X"57",X"03", -- #16 : 042-044 +X"00",X"5A",X"01", -- #17 : 045-047 +X"00",X"5B",X"03", -- #18 : 048-04A +X"00",X"5E",X"06", -- #19 : 04B-04D +X"00",X"64",X"01", -- #1A : 04E-050 +X"00",X"65",X"03", -- #1B : 051-053 +X"00",X"68",X"02", -- #1C : 054-056 +X"00",X"6A",X"01", -- #1D : 057-059 +X"00",X"6B",X"01", -- #1E : 05A-05C +X"00",X"6C",X"03", -- #1F : 05D-05F +X"00",X"6F",X"05", -- #20 : 060-062 +X"00",X"74",X"03", -- #21 : 063-065 +X"00",X"77",X"03", -- #22 : 066-068 +X"00",X"7A",X"03", -- #23 : 069-06B +X"00",X"7D",X"03", -- #24 : 06C-06E +X"00",X"80",X"04", -- #25 : 06F-071 +X"00",X"84",X"02", -- #26 : 072-074 +X"00",X"86",X"03", -- #27 : 075-077 +X"00",X"89",X"01", -- #28 : 078-07A +X"00",X"8A",X"03", -- #29 : 07B-07D +X"00",X"8D",X"04", -- #2A : 07E-080 +X"00",X"91",X"03", -- #2B : 081-083 +X"00",X"94",X"04", -- #2C : 084-086 +X"00",X"98",X"03", -- #2D : 087-089 +X"00",X"9B",X"03", -- #2E : 08A-08C +X"00",X"9E",X"0D", -- #2F : 08D-08F +X"00",X"AB",X"04", -- #30 : 090-092 +X"00",X"AF",X"04", -- #31 : 093-095 +X"00",X"B3",X"03", -- #32 : 096-098 +X"00",X"B6",X"09", -- #33 : 099-09B +X"00",X"BF",X"0A", -- #34 : 09C-09E +X"00",X"C9",X"06", -- #35 : 09F-0A1 +X"00",X"CF",X"02", -- #36 : 0A2-0A4 +X"00",X"D1",X"01", -- #37 : 0A5-0A7 +X"00",X"D2",X"06", -- #38 : 0A8-0AA +X"00",X"D8",X"02", -- #39 : 0AB-0AD +X"00",X"DA",X"09", -- #3A : 0AE-0B0 +X"00",X"E3",X"09", -- #3B : 0B1-0B3 +X"00",X"EC",X"08", -- #3C : 0B4-0B6 +X"00",X"F4",X"03", -- #3D : 0B7-0B9 +X"00",X"F7",X"03", -- #3E : 0BA-0BC +X"00",X"FA",X"03", -- #3F : 0BD-0BF +-- rpt|amp_msb mp_lsb|per|F0|B0|F1|B1|F2|B2|F3|B3|F4|B4|F5|B5 +X"01",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #00.0 : 0C0-0CF +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #01.0 : 0D0-0DF +X"07",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #02.0 : 0E0-0EF +X"0F",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #03.0 : 0F0-0FF +X"1F",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #04.0 : 100-10F +X"03",X"01",X"40",X"5B",X"A0",X"60",X"B8",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.0 : 110-11F +X"03",X"00",X"C0",X"5B",X"A0",X"60",X"B0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.1 : 120-12F +X"01",X"00",X"50",X"5B",X"A0",X"60",X"A8",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.2 : 130-13F +X"03",X"00",X"80",X"5B",X"A0",X"60",X"B0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.3 : 140-14F +X"01",X"00",X"70",X"5B",X"A0",X"60",X"B0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.4 : 150-15F +X"04",X"00",X"A0",X"5B",X"A0",X"60",X"B8",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.5 : 160-16F +X"02",X"01",X"C0",X"5B",X"A8",X"60",X"C0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.6 : 170-17F +X"04",X"02",X"80",X"5B",X"A8",X"60",X"D0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.7 : 180-18F +X"02",X"02",X"80",X"5B",X"A8",X"60",X"E0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.8 : 190-19F +X"09",X"01",X"00",X"5B",X"A0",X"60",X"E8",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #05.9 : 1A0-1AF +X"01",X"03",X"00",X"5B",X"B0",X"70",X"C8",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.0 : 1B0-1BF +X"01",X"02",X"80",X"5B",X"C8",X"70",X"B0",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.1 : 1C0-1CF +X"02",X"03",X"00",X"5B",X"C8",X"70",X"B0",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.2 : 1D0-1DF +X"02",X"02",X"80",X"5B",X"B0",X"70",X"C8",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.3 : 1E0-1EF +X"02",X"02",X"00",X"5B",X"C8",X"70",X"F8",X"70",X"B8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.4 : 1F0-1FF +X"02",X"02",X"80",X"5B",X"B0",X"70",X"D0",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.5 : 200-20F +X"03",X"03",X"80",X"5B",X"B0",X"70",X"F8",X"70",X"E0",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.6 : 210-21F +X"01",X"03",X"00",X"5B",X"A8",X"70",X"E0",X"70",X"F8",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.7 : 220-22F +X"03",X"02",X"80",X"5B",X"A0",X"70",X"E8",X"70",X"00",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.8 : 230-23F +X"02",X"01",X"00",X"5B",X"A0",X"70",X"F0",X"70",X"00",X"60",X"18",X"50",X"3C",X"44",X"00",X"00", -- #06.9 : 240-24F +X"06",X"06",X"00",X"5B",X"00",X"50",X"28",X"50",X"40",X"50",X"F8",X"10",X"E8",X"58",X"AA",X"64", -- #07.0 : 250-25F +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #08.0 : 260-26F +X"03",X"00",X"28",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"38",X"40",X"FC",X"04",X"CB",X"68", -- #08.1 : 270-27F +X"05",X"00",X"20",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"18",X"20",X"40",X"C5",X"62", -- #08.2 : 280-28F +X"0D",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #09.0 : 290-29F +X"04",X"00",X"14",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"40",X"F4",X"2C",X"BF",X"3B", -- #09.1 : 2A0-2AF +X"06",X"00",X"20",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"50",X"F8",X"44",X"CF",X"3A", -- #09.2 : 2B0-2BF +X"04",X"00",X"50",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"20",X"18",X"42",X"FA",X"30", -- #0A.0 : 2C0-2CF +X"04",X"01",X"C0",X"5B",X"18",X"60",X"30",X"50",X"38",X"20",X"00",X"10",X"00",X"60",X"E6",X"38", -- #0A.1 : 2D0-2DF +X"04",X"01",X"40",X"5B",X"18",X"50",X"28",X"40",X"40",X"30",X"FC",X"68",X"E8",X"52",X"DD",X"16", -- #0A.2 : 2E0-2EF +X"07",X"00",X"38",X"5B",X"00",X"30",X"18",X"20",X"38",X"40",X"FC",X"60",X"E0",X"2A",X"A1",X"54", -- #0B.0 : 2F0-2FF +X"03",X"00",X"50",X"5B",X"08",X"30",X"20",X"30",X"20",X"10",X"FC",X"68",X"E8",X"1C",X"A2",X"50", -- #0B.1 : 300-30F +X"09",X"00",X"60",X"5B",X"08",X"30",X"20",X"40",X"20",X"10",X"FC",X"68",X"E0",X"24",X"9B",X"5B", -- #0B.2 : 310-31F +X"05",X"06",X"00",X"5B",X"00",X"50",X"10",X"20",X"30",X"50",X"E8",X"60",X"34",X"1E",X"A0",X"6E", -- #0C.0 : 320-32F +X"05",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #0D.0 : 330-33F +X"03",X"00",X"A0",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"34",X"28",X"1C",X"4E",X"F4",X"21", -- #0D.1 : 340-34F +X"04",X"00",X"50",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"30",X"20",X"1C",X"44",X"EA",X"53", -- #0D.2 : 350-35F +X"03",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"18",X"1C",X"46",X"E7",X"5A", -- #0D.3 : 360-36F +X"05",X"00",X"1C",X"5B",X"98",X"60",X"C0",X"60",X"E0",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.0 : 370-37F +X"03",X"00",X"50",X"5B",X"98",X"60",X"C0",X"60",X"E8",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.1 : 380-38F +X"01",X"00",X"C0",X"5B",X"98",X"60",X"C0",X"60",X"E8",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.2 : 390-39F +X"02",X"02",X"80",X"5B",X"A0",X"60",X"C0",X"60",X"F0",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.3 : 3A0-3AF +X"01",X"01",X"C0",X"5B",X"A0",X"60",X"B8",X"60",X"F0",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.4 : 3B0-3BF +X"02",X"02",X"00",X"5B",X"A0",X"60",X"B8",X"60",X"F0",X"50",X"0C",X"40",X"20",X"3C",X"32",X"2B", -- #0E.5 : 3C0-3CF +X"06",X"06",X"00",X"5B",X"F0",X"50",X"18",X"20",X"28",X"60",X"D0",X"60",X"28",X"18",X"A8",X"61", -- #0F.0 : 3D0-3DF +X"06",X"00",X"A0",X"5B",X"F0",X"50",X"C0",X"30",X"E0",X"20",X"00",X"10",X"20",X"44",X"00",X"00", -- #10.0 : 3E0-3EF +X"05",X"00",X"C0",X"5B",X"F0",X"50",X"C0",X"30",X"E0",X"20",X"00",X"10",X"20",X"44",X"00",X"00", -- #10.1 : 3F0-3FF +X"05",X"00",X"E0",X"5B",X"F0",X"50",X"E0",X"30",X"C8",X"20",X"00",X"10",X"20",X"44",X"00",X"00", -- #10.2 : 400-40F +X"04",X"01",X"40",X"5B",X"F0",X"50",X"E0",X"30",X"C8",X"20",X"00",X"10",X"20",X"44",X"00",X"00", -- #10.3 : 410-41F +X"06",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #11.0 : 420-42F +X"01",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"40",X"48",X"10",X"40",X"DF",X"3F", -- #11.1 : 430-43F +X"05",X"00",X"28",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"18",X"28",X"40",X"E8",X"3F", -- #11.2 : 440-44F +X"0C",X"00",X"A0",X"5B",X"08",X"30",X"18",X"30",X"38",X"40",X"EC",X"30",X"D4",X"2E",X"CB",X"20", -- #12.0 : 450-45F +X"03",X"02",X"80",X"5B",X"00",X"60",X"18",X"50",X"38",X"50",X"F4",X"08",X"DC",X"5E",X"A1",X"5C", -- #12.1 : 460-46F +X"03",X"06",X"00",X"5B",X"08",X"00",X"28",X"40",X"38",X"40",X"04",X"60",X"F0",X"66",X"A1",X"5A", -- #13.0 : 470-47F +X"03",X"06",X"00",X"5B",X"28",X"40",X"38",X"40",X"08",X"00",X"04",X"50",X"F4",X"62",X"A4",X"57", -- #13.1 : 480-48F +X"02",X"06",X"00",X"5B",X"08",X"10",X"28",X"50",X"38",X"40",X"04",X"48",X"F4",X"64",X"9A",X"62", -- #13.2 : 490-49F +X"03",X"06",X"00",X"5B",X"08",X"20",X"28",X"50",X"38",X"40",X"04",X"38",X"F8",X"60",X"96",X"66", -- #13.3 : 4A0-4AF +X"04",X"04",X"00",X"5B",X"08",X"30",X"28",X"50",X"38",X"50",X"FC",X"20",X"F8",X"62",X"92",X"6E", -- #13.4 : 4B0-4BF +X"04",X"01",X"40",X"5B",X"08",X"40",X"28",X"40",X"38",X"40",X"F8",X"60",X"F4",X"0C",X"90",X"71", -- #13.5 : 4C0-4CF +X"03",X"03",X"00",X"5B",X"A0",X"70",X"E8",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.0 : 4D0-4DF +X"01",X"03",X"00",X"5B",X"A0",X"70",X"F0",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.1 : 4E0-4EF +X"03",X"02",X"80",X"5B",X"A0",X"70",X"F0",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.2 : 4F0-4FF +X"01",X"01",X"C0",X"5B",X"98",X"70",X"F8",X"60",X"F8",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.3 : 500-50F +X"05",X"01",X"C0",X"5B",X"98",X"70",X"F8",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.4 : 510-51F +X"01",X"01",X"C0",X"5B",X"98",X"70",X"F8",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.5 : 520-52F +X"06",X"00",X"A0",X"5B",X"90",X"70",X"F8",X"60",X"00",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.6 : 530-53F +X"02",X"00",X"70",X"5B",X"90",X"70",X"F8",X"60",X"F8",X"20",X"04",X"30",X"28",X"52",X"00",X"00", -- #14.7 : 540-54F +X"03",X"02",X"80",X"5B",X"90",X"70",X"E0",X"70",X"00",X"60",X"10",X"10",X"1C",X"56",X"3A",X"49", -- #15.0 : 550-55F +X"02",X"02",X"00",X"5B",X"98",X"70",X"E0",X"70",X"00",X"60",X"10",X"10",X"1C",X"56",X"3A",X"49", -- #15.1 : 560-56F +X"03",X"05",X"00",X"5B",X"98",X"60",X"F0",X"60",X"D8",X"60",X"1C",X"58",X"24",X"4E",X"39",X"2C", -- #16.0 : 570-57F +X"02",X"05",X"00",X"5B",X"D0",X"60",X"F0",X"60",X"A0",X"60",X"1C",X"58",X"24",X"4E",X"39",X"2C", -- #16.1 : 580-58F +X"02",X"04",X"00",X"5B",X"F0",X"60",X"C8",X"60",X"A0",X"60",X"1C",X"58",X"24",X"4E",X"39",X"2C", -- #16.2 : 590-59F +X"08",X"04",X"00",X"5B",X"20",X"50",X"18",X"20",X"40",X"30",X"F8",X"60",X"C0",X"60",X"B2",X"5A", -- #17.0 : 5A0-5AF +X"03",X"05",X"00",X"5B",X"C8",X"60",X"B8",X"60",X"F8",X"60",X"14",X"28",X"1C",X"36",X"44",X"41", -- #18.0 : 5B0-5BF +X"02",X"05",X"00",X"5B",X"C8",X"60",X"B8",X"60",X"F8",X"60",X"14",X"28",X"1C",X"36",X"44",X"41", -- #18.1 : 5C0-5CF +X"02",X"05",X"00",X"5B",X"B8",X"60",X"C8",X"60",X"F8",X"60",X"14",X"28",X"1C",X"36",X"44",X"41", -- #18.2 : 5D0-5DF +X"03",X"01",X"C0",X"5B",X"10",X"50",X"28",X"40",X"30",X"30",X"08",X"30",X"F0",X"64",X"8E",X"70", -- #19.0 : 5E0-5EF +X"02",X"01",X"80",X"5B",X"10",X"50",X"28",X"50",X"38",X"30",X"04",X"28",X"F4",X"5E",X"8C",X"76", -- #19.1 : 5F0-5FF +X"04",X"02",X"80",X"5B",X"08",X"30",X"28",X"40",X"30",X"20",X"08",X"40",X"F4",X"5C",X"90",X"70", -- #19.2 : 600-60F +X"01",X"04",X"00",X"5B",X"10",X"30",X"20",X"50",X"38",X"40",X"00",X"38",X"F0",X"5A",X"95",X"69", -- #19.3 : 610-61F +X"03",X"07",X"00",X"5B",X"20",X"40",X"28",X"40",X"18",X"10",X"00",X"48",X"F0",X"60",X"98",X"69", -- #19.4 : 620-62F +X"01",X"06",X"00",X"5B",X"18",X"30",X"28",X"40",X"20",X"10",X"00",X"48",X"F0",X"5E",X"9C",X"68", -- #19.5 : 630-63F +X"09",X"03",X"00",X"5B",X"F8",X"40",X"20",X"50",X"30",X"50",X"F0",X"20",X"DC",X"4C",X"AE",X"66", -- #1A.0 : 640-64F +X"06",X"00",X"1C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"28",X"08",X"4E",X"EB",X"0C", -- #1B.0 : 650-65F +X"06",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"34",X"38",X"0C",X"3E",X"F1",X"1A", -- #1B.1 : 660-66F +X"02",X"00",X"14",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"30",X"E8",X"F2",X"03",X"3F", -- #1B.2 : 670-67F +X"03",X"00",X"04",X"5B",X"F8",X"30",X"18",X"40",X"40",X"30",X"E4",X"20",X"18",X"04",X"88",X"76", -- #1C.0 : 680-68F +X"01",X"03",X"00",X"5B",X"20",X"50",X"18",X"10",X"30",X"40",X"F8",X"78",X"E0",X"74",X"98",X"6C", -- #1C.1 : 690-69F +X"14",X"00",X"28",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"30",X"30",X"10",X"24",X"E5",X"3B", -- #1D.0 : 6A0-6AF +X"08",X"02",X"80",X"5B",X"F8",X"70",X"20",X"50",X"28",X"50",X"C0",X"68",X"18",X"06",X"A4",X"5E", -- #1E.0 : 6B0-6BF +X"08",X"02",X"80",X"5B",X"F0",X"60",X"18",X"60",X"20",X"40",X"D8",X"60",X"20",X"0C",X"9D",X"61", -- #1F.0 : 6C0-6CF +X"04",X"03",X"00",X"5B",X"18",X"50",X"20",X"50",X"38",X"30",X"F0",X"60",X"D0",X"66",X"A6",X"53", -- #1F.1 : 6D0-6DF +X"07",X"02",X"00",X"5B",X"18",X"60",X"28",X"50",X"40",X"40",X"F0",X"60",X"C8",X"62",X"9D",X"61", -- #1F.2 : 6E0-6EF +X"08",X"01",X"C0",X"5B",X"B8",X"60",X"D0",X"60",X"F8",X"50",X"0C",X"20",X"20",X"44",X"00",X"00", -- #20.0 : 6F0-6FF +X"05",X"01",X"00",X"5B",X"B0",X"60",X"C8",X"60",X"F8",X"50",X"0C",X"20",X"20",X"44",X"00",X"00", -- #20.1 : 700-70F +X"04",X"00",X"A0",X"5B",X"B0",X"60",X"C0",X"60",X"F8",X"50",X"0C",X"20",X"20",X"44",X"00",X"00", -- #20.2 : 710-71F +X"04",X"00",X"80",X"5B",X"A8",X"60",X"C0",X"60",X"00",X"50",X"0C",X"20",X"20",X"44",X"00",X"00", -- #20.3 : 720-72F +X"07",X"00",X"28",X"5B",X"A0",X"60",X"B8",X"60",X"00",X"50",X"0C",X"20",X"20",X"44",X"00",X"00", -- #20.4 : 730-73F +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #21.0 : 740-74F +X"03",X"00",X"38",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"20",X"14",X"32",X"E7",X"23", -- #21.1 : 750-75F +X"03",X"06",X"00",X"5B",X"18",X"50",X"20",X"50",X"40",X"30",X"F8",X"60",X"E4",X"72",X"9D",X"5D", -- #21.2 : 760-76F +X"08",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #22.0 : 770-77F +X"05",X"00",X"38",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"30",X"48",X"F4",X"42",X"EA",X"2A", -- #22.1 : 780-78F +X"03",X"01",X"40",X"5B",X"20",X"30",X"30",X"50",X"18",X"10",X"F8",X"68",X"E8",X"58",X"9C",X"68", -- #22.2 : 790-79F +X"06",X"00",X"20",X"5B",X"08",X"30",X"28",X"50",X"50",X"40",X"F0",X"48",X"C8",X"68",X"A8",X"47", -- #23.0 : 7A0-7AF +X"06",X"00",X"E0",X"5B",X"10",X"40",X"28",X"40",X"50",X"50",X"F0",X"40",X"D0",X"1A",X"DE",X"3A", -- #23.1 : 7B0-7BF +X"02",X"01",X"40",X"5B",X"F0",X"60",X"18",X"40",X"20",X"40",X"D0",X"68",X"2C",X"18",X"C7",X"25", -- #23.2 : 7C0-7CF +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #24.0 : 7D0-7DF +X"03",X"00",X"20",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"14",X"30",X"FC",X"F0",X"FB",X"41", -- #24.1 : 7E0-7EF +X"03",X"03",X"80",X"5B",X"10",X"40",X"20",X"60",X"38",X"30",X"F8",X"30",X"F4",X"5E",X"A1",X"5C", -- #24.2 : 7F0-7FF +X"06",X"00",X"10",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"1C",X"50",X"0C",X"0E",X"FB",X"5C", -- #25.0 : 800-80F +X"03",X"00",X"60",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"18",X"18",X"42",X"FA",X"56", -- #25.1 : 810-81F +X"12",X"00",X"60",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"18",X"18",X"4A",X"FC",X"5B", -- #25.2 : 820-82F +X"04",X"00",X"50",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"18",X"10",X"2E",X"F8",X"4C", -- #25.3 : 830-83F +X"07",X"01",X"40",X"5B",X"C0",X"30",X"E0",X"40",X"F8",X"60",X"14",X"58",X"24",X"48",X"00",X"00", -- #26.0 : 840-84F +X"0B",X"00",X"80",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"42",X"FE",X"3F", -- #26.1 : 850-85F +X"02",X"00",X"38",X"5B",X"08",X"40",X"20",X"30",X"38",X"30",X"DC",X"48",X"BC",X"58",X"A4",X"54", -- #27.0 : 860-86F +X"03",X"00",X"60",X"5B",X"D0",X"60",X"10",X"70",X"20",X"30",X"BC",X"60",X"1C",X"08",X"A1",X"5D", -- #27.1 : 870-87F +X"04",X"00",X"A0",X"5B",X"D0",X"50",X"10",X"60",X"18",X"50",X"BC",X"60",X"20",X"0E",X"A3",X"5C", -- #27.2 : 880-88F +X"11",X"00",X"38",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"38",X"04",X"28",X"E3",X"27", -- #28.0 : 890-89F +X"12",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #29.0 : 8A0-8AF +X"01",X"00",X"38",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"34",X"30",X"04",X"12",X"DE",X"56", -- #29.1 : 8B0-8BF +X"02",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"2C",X"28",X"F4",X"1E",X"DC",X"49", -- #29.2 : 8C0-8CF +X"07",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #2A.0 : 8D0-8DF +X"01",X"00",X"A0",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"30",X"38",X"0C",X"62",X"E2",X"37", -- #2A.1 : 8E0-8EF +X"01",X"00",X"1C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"2C",X"20",X"10",X"36",X"F9",X"44", -- #2A.2 : 8F0-8FF +X"09",X"00",X"50",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"2C",X"28",X"0C",X"44",X"E7",X"1B", -- #2A.3 : 900-90F +X"06",X"04",X"00",X"5B",X"20",X"50",X"40",X"60",X"58",X"50",X"00",X"58",X"DC",X"54",X"A7",X"4D", -- #2B.0 : 910-91F +X"09",X"00",X"60",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"44",X"58",X"EC",X"00",X"21",X"30", -- #2B.1 : 920-92F +X"04",X"05",X"00",X"5B",X"20",X"50",X"40",X"50",X"48",X"40",X"00",X"40",X"DC",X"60",X"B9",X"37", -- #2B.2 : 930-93F +X"05",X"03",X"80",X"5B",X"D8",X"40",X"B8",X"40",X"F8",X"40",X"04",X"58",X"2C",X"3C",X"55",X"5A", -- #2C.0 : 940-94F +X"03",X"02",X"00",X"5B",X"F0",X"40",X"B0",X"40",X"D8",X"40",X"04",X"58",X"2C",X"3C",X"55",X"5A", -- #2C.1 : 950-95F +X"06",X"01",X"C0",X"5B",X"B0",X"40",X"F0",X"40",X"D8",X"40",X"04",X"58",X"2C",X"3C",X"55",X"5A", -- #2C.2 : 960-96F +X"08",X"01",X"40",X"5B",X"F0",X"40",X"B0",X"40",X"D0",X"40",X"04",X"58",X"2C",X"3C",X"55",X"5A", -- #2C.3 : 970-97F +X"03",X"00",X"C0",X"5B",X"98",X"60",X"C8",X"60",X"08",X"40",X"18",X"30",X"28",X"44",X"2E",X"25", -- #2D.0 : 980-98F +X"03",X"02",X"80",X"5B",X"A0",X"60",X"C8",X"60",X"08",X"40",X"18",X"30",X"28",X"44",X"2E",X"25", -- #2D.1 : 990-99F +X"03",X"02",X"00",X"5B",X"A0",X"60",X"C0",X"60",X"00",X"40",X"18",X"30",X"28",X"44",X"2E",X"25", -- #2D.2 : 9A0-9AF +X"05",X"00",X"28",X"5B",X"F8",X"20",X"18",X"40",X"28",X"30",X"C0",X"38",X"10",X"F8",X"A0",X"5A", -- #2E.0 : 9B0-9BF +X"05",X"00",X"38",X"5B",X"08",X"30",X"20",X"60",X"38",X"40",X"FC",X"08",X"B0",X"50",X"9E",X"5F", -- #2E.1 : 9C0-9CF +X"06",X"00",X"50",X"5B",X"00",X"50",X"18",X"60",X"20",X"50",X"A8",X"70",X"14",X"02",X"93",X"72", -- #2E.2 : 9D0-9DF +X"03",X"02",X"80",X"5B",X"A0",X"60",X"E8",X"60",X"F8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.0 : 9E0-9EF +X"02",X"02",X"80",X"5B",X"A0",X"60",X"E8",X"60",X"F8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.1 : 9F0-9FF +X"02",X"01",X"C0",X"5B",X"A0",X"60",X"E8",X"60",X"F0",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.2 : A00-A0F +X"02",X"02",X"80",X"5B",X"A0",X"60",X"E0",X"60",X"F0",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.3 : A10-A1F +X"02",X"03",X"00",X"5B",X"A8",X"60",X"E0",X"60",X"F0",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.4 : A20-A2F +X"02",X"02",X"80",X"5B",X"A8",X"60",X"D8",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.5 : A30-A3F +X"03",X"02",X"00",X"5B",X"A8",X"60",X"D8",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.6 : A40-A4F +X"01",X"01",X"C0",X"5B",X"A8",X"60",X"D8",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.7 : A50-A5F +X"03",X"00",X"E0",X"5B",X"A8",X"60",X"D0",X"60",X"E0",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.8 : A60-A6F +X"01",X"00",X"80",X"5B",X"A0",X"60",X"D0",X"60",X"E0",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.9 : A70-A7F +X"02",X"00",X"C0",X"5B",X"A8",X"60",X"D0",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.A : A80-A8F +X"01",X"00",X"70",X"5B",X"A8",X"60",X"D0",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.B : A90-A9F +X"03",X"00",X"50",X"5B",X"A8",X"60",X"D8",X"60",X"E8",X"60",X"00",X"10",X"1C",X"52",X"42",X"59", -- #2F.C : AA0-AAF +X"06",X"00",X"0C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"20",X"FC",X"1C",X"D2",X"28", -- #30.0 : AB0-ABF +X"0B",X"00",X"0C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"24",X"38",X"04",X"18",X"B4",X"55", -- #30.1 : AC0-ACF +X"02",X"00",X"C0",X"5B",X"F8",X"40",X"30",X"70",X"20",X"20",X"C4",X"38",X"10",X"FC",X"A3",X"5D", -- #30.2 : AD0-ADF +X"02",X"01",X"40",X"5B",X"F8",X"60",X"28",X"50",X"28",X"50",X"B0",X"68",X"1C",X"06",X"A3",X"58", -- #30.3 : AE0-AEF +X"03",X"01",X"C0",X"5B",X"10",X"50",X"28",X"40",X"30",X"30",X"08",X"30",X"F0",X"64",X"8E",X"70", -- #31.0 : AF0-AFF +X"02",X"01",X"80",X"5B",X"10",X"50",X"28",X"50",X"38",X"30",X"04",X"28",X"F4",X"5E",X"8C",X"76", -- #31.1 : B00-B0F +X"04",X"02",X"80",X"5B",X"08",X"30",X"28",X"40",X"30",X"20",X"08",X"40",X"F4",X"5C",X"90",X"70", -- #31.2 : B10-B1F +X"01",X"04",X"00",X"5B",X"10",X"30",X"20",X"50",X"38",X"40",X"00",X"38",X"F0",X"5A",X"95",X"69", -- #31.3 : B20-B2F +X"05",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #32.0 : B30-B3F +X"05",X"00",X"14",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"28",X"40",X"08",X"4E",X"E4",X"19", -- #32.1 : B40-B4F +X"0D",X"00",X"50",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"34",X"50",X"10",X"66",X"F6",X"49", -- #32.2 : B50-B5F +X"01",X"02",X"80",X"5B",X"18",X"40",X"20",X"40",X"30",X"20",X"E4",X"60",X"CC",X"68",X"A5",X"5F", -- #33.0 : B60-B6F +X"02",X"02",X"80",X"5B",X"18",X"40",X"20",X"40",X"28",X"10",X"E4",X"58",X"D0",X"60",X"A5",X"5F", -- #33.1 : B70-B7F +X"01",X"02",X"80",X"5B",X"E0",X"60",X"18",X"40",X"20",X"40",X"D0",X"60",X"24",X"12",X"A8",X"5B", -- #33.2 : B80-B8F +X"01",X"02",X"00",X"5B",X"E0",X"60",X"18",X"50",X"28",X"40",X"D0",X"60",X"20",X"0A",X"A3",X"63", -- #33.3 : B90-B9F +X"01",X"01",X"C0",X"5B",X"E0",X"60",X"18",X"60",X"28",X"40",X"D0",X"68",X"20",X"0A",X"A5",X"61", -- #33.4 : BA0-BAF +X"02",X"02",X"00",X"5B",X"E0",X"60",X"18",X"50",X"28",X"40",X"D0",X"60",X"24",X"12",X"A2",X"69", -- #33.5 : BB0-BBF +X"01",X"01",X"40",X"5B",X"E0",X"60",X"18",X"60",X"28",X"40",X"D4",X"68",X"1C",X"0A",X"A0",X"6E", -- #33.6 : BC0-BCF +X"02",X"00",X"E0",X"5B",X"18",X"60",X"20",X"20",X"20",X"10",X"E0",X"60",X"D4",X"68",X"A0",X"6E", -- #33.7 : BD0-BDF +X"01",X"01",X"00",X"5B",X"18",X"60",X"18",X"20",X"30",X"20",X"E4",X"60",X"D4",X"6A",X"A3",X"63", -- #33.8 : BE0-BEF +X"02",X"02",X"80",X"5B",X"18",X"40",X"20",X"40",X"30",X"20",X"E4",X"60",X"CC",X"68",X"A5",X"5F", -- #34.0 : BF0-BFF +X"02",X"02",X"80",X"5B",X"18",X"40",X"20",X"40",X"28",X"10",X"E4",X"58",X"D0",X"60",X"A5",X"5F", -- #34.1 : C00-C0F +X"02",X"02",X"00",X"5B",X"E0",X"60",X"18",X"40",X"28",X"40",X"D0",X"60",X"24",X"10",X"A9",X"59", -- #34.2 : C10-C1F +X"02",X"02",X"00",X"5B",X"E0",X"60",X"18",X"40",X"20",X"40",X"D0",X"60",X"24",X"12",X"A8",X"5B", -- #34.3 : C20-C2F +X"02",X"01",X"C0",X"5B",X"E0",X"60",X"18",X"50",X"28",X"40",X"D0",X"60",X"20",X"0A",X"A3",X"63", -- #34.4 : C30-C3F +X"02",X"01",X"C0",X"5B",X"E0",X"60",X"18",X"60",X"28",X"40",X"D0",X"68",X"20",X"0A",X"A5",X"61", -- #34.5 : C40-C4F +X"02",X"01",X"C0",X"5B",X"E0",X"60",X"18",X"50",X"28",X"40",X"D0",X"60",X"24",X"12",X"A2",X"69", -- #34.6 : C50-C5F +X"03",X"01",X"40",X"5B",X"E0",X"60",X"18",X"60",X"28",X"40",X"D4",X"68",X"1C",X"0A",X"A0",X"6E", -- #34.7 : C60-C6F +X"03",X"00",X"C0",X"5B",X"18",X"60",X"20",X"20",X"20",X"10",X"E0",X"60",X"D4",X"68",X"A0",X"6E", -- #34.8 : C70-C7F +X"03",X"00",X"E0",X"5B",X"18",X"60",X"18",X"20",X"30",X"20",X"E4",X"60",X"D4",X"6A",X"A3",X"63", -- #34.9 : C80-C8F +X"03",X"02",X"80",X"5B",X"A8",X"60",X"C0",X"60",X"F8",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.0 : C90-C9F +X"03",X"02",X"00",X"5B",X"A8",X"60",X"C0",X"60",X"00",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.1 : CA0-CAF +X"01",X"01",X"C0",X"5B",X"A8",X"60",X"C0",X"60",X"00",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.2 : CB0-CBF +X"05",X"00",X"C0",X"5B",X"A0",X"60",X"B8",X"60",X"00",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.3 : CC0-CCF +X"01",X"00",X"C0",X"5B",X"A0",X"60",X"B8",X"60",X"00",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.4 : CD0-CDF +X"06",X"00",X"70",X"5B",X"A0",X"60",X"B8",X"60",X"00",X"60",X"40",X"60",X"24",X"60",X"00",X"00", -- #35.5 : CE0-CEF +X"11",X"00",X"A0",X"5B",X"08",X"30",X"18",X"30",X"38",X"40",X"EC",X"30",X"D4",X"2E",X"CB",X"20", -- #36.0 : CF0-CFF +X"03",X"02",X"80",X"5B",X"00",X"60",X"18",X"50",X"38",X"50",X"F4",X"08",X"DC",X"5E",X"A1",X"5C", -- #36.1 : D00-D0F +X"0A",X"00",X"70",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"48",X"40",X"20",X"38",X"E3",X"29", -- #37.0 : D10-D1F +X"04",X"00",X"A0",X"5B",X"00",X"30",X"18",X"20",X"38",X"40",X"FC",X"60",X"E0",X"2A",X"A1",X"54", -- #38.0 : D20-D2F +X"02",X"00",X"C0",X"5B",X"08",X"30",X"20",X"30",X"20",X"10",X"FC",X"68",X"E8",X"1C",X"A2",X"50", -- #38.1 : D30-D3F +X"02",X"00",X"C0",X"5B",X"08",X"30",X"20",X"40",X"20",X"10",X"FC",X"68",X"E0",X"24",X"9B",X"5B", -- #38.2 : D40-D4F +X"03",X"06",X"00",X"5B",X"10",X"40",X"18",X"40",X"30",X"30",X"F8",X"68",X"DC",X"62",X"A8",X"50", -- #38.3 : D50-D5F +X"01",X"06",X"00",X"5B",X"10",X"20",X"18",X"40",X"30",X"40",X"F8",X"68",X"D4",X"6E",X"AC",X"55", -- #38.4 : D60-D6F +X"03",X"06",X"00",X"5B",X"20",X"50",X"18",X"10",X"30",X"40",X"F8",X"70",X"D0",X"6A",X"AC",X"58", -- #38.5 : D70-D7F +X"0E",X"00",X"0C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"30",X"00",X"1E",X"C1",X"48", -- #39.0 : D80-D8F +X"04",X"00",X"C0",X"5B",X"00",X"40",X"20",X"60",X"38",X"50",X"E8",X"18",X"E0",X"0A",X"C2",X"49", -- #39.1 : D90-D9F +X"02",X"00",X"60",X"5B",X"A0",X"60",X"A8",X"60",X"F8",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.0 : DA0-DAF +X"01",X"00",X"40",X"5B",X"A0",X"60",X"A8",X"60",X"F0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.1 : DB0-DBF +X"03",X"00",X"50",X"5B",X"A0",X"60",X"A8",X"60",X"F0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.2 : DC0-DCF +X"03",X"00",X"E0",X"5B",X"A0",X"60",X"B0",X"60",X"F0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.3 : DD0-DDF +X"05",X"01",X"40",X"5B",X"A0",X"60",X"C0",X"60",X"E8",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.4 : DE0-DEF +X"04",X"01",X"C0",X"5B",X"A8",X"60",X"C8",X"60",X"E0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.5 : DF0-DFF +X"04",X"01",X"C0",X"5B",X"A8",X"60",X"D0",X"60",X"E0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.6 : E00-E0F +X"01",X"00",X"E0",X"5B",X"A0",X"60",X"D0",X"60",X"E0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.7 : E10-E1F +X"03",X"00",X"60",X"5B",X"A0",X"60",X"D0",X"60",X"E0",X"60",X"10",X"20",X"20",X"60",X"37",X"57", -- #3A.8 : E20-E2F +X"02",X"01",X"00",X"5B",X"18",X"60",X"28",X"40",X"10",X"00",X"F0",X"48",X"BC",X"64",X"AC",X"59", -- #3B.0 : E30-E3F +X"02",X"01",X"40",X"5B",X"10",X"60",X"20",X"40",X"18",X"10",X"F0",X"48",X"BC",X"62",X"AF",X"56", -- #3B.1 : E40-E4F +X"02",X"01",X"80",X"5B",X"10",X"50",X"20",X"30",X"20",X"10",X"EC",X"48",X"C0",X"5E",X"B0",X"56", -- #3B.2 : E50-E5F +X"02",X"01",X"40",X"5B",X"F0",X"50",X"10",X"60",X"20",X"50",X"C0",X"60",X"10",X"02",X"B6",X"51", -- #3B.3 : E60-E6F +X"02",X"01",X"40",X"5B",X"E8",X"60",X"18",X"60",X"28",X"50",X"C4",X"60",X"04",X"00",X"B8",X"4A", -- #3B.4 : E70-E7F +X"03",X"01",X"40",X"5B",X"18",X"60",X"10",X"00",X"20",X"30",X"E8",X"58",X"C8",X"62",X"B5",X"54", -- #3B.5 : E80-E8F +X"03",X"00",X"E0",X"5B",X"00",X"00",X"18",X"60",X"30",X"50",X"E4",X"60",X"CC",X"58",X"B5",X"56", -- #3B.6 : E90-E9F +X"03",X"00",X"C0",X"5B",X"00",X"00",X"18",X"60",X"28",X"40",X"E4",X"60",X"D0",X"5C",X"B2",X"59", -- #3B.7 : EA0-EAF +X"03",X"00",X"80",X"5B",X"E8",X"60",X"10",X"60",X"20",X"20",X"CC",X"60",X"04",X"FC",X"B2",X"55", -- #3B.8 : EB0-EBF +X"03",X"03",X"80",X"5B",X"90",X"70",X"F0",X"50",X"00",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.0 : EC0-ECF +X"02",X"04",X"00",X"5B",X"90",X"70",X"F8",X"50",X"00",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.1 : ED0-EDF +X"03",X"05",X"00",X"5B",X"90",X"70",X"F8",X"50",X"00",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.2 : EE0-EEF +X"02",X"02",X"80",X"5B",X"90",X"70",X"F0",X"50",X"00",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.3 : EF0-EFF +X"03",X"02",X"80",X"5B",X"98",X"70",X"E8",X"50",X"F8",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.4 : F00-F0F +X"04",X"01",X"80",X"5B",X"A0",X"70",X"E0",X"50",X"F0",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.5 : F10-F1F +X"05",X"01",X"80",X"5B",X"A0",X"70",X"D8",X"50",X"F0",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.6 : F20-F2F +X"05",X"01",X"40",X"5B",X"A0",X"70",X"D8",X"50",X"F0",X"30",X"04",X"18",X"28",X"4E",X"39",X"41", -- #3C.7 : F30-F3F +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #3D.0 : F40-F4F +X"04",X"00",X"1C",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"38",X"E0",X"40",X"E6",X"1C", -- #3D.1 : F50-F5F +X"02",X"01",X"80",X"5B",X"F8",X"40",X"18",X"60",X"38",X"50",X"F8",X"18",X"D8",X"5C",X"A1",X"70", -- #3D.2 : F60-F6F +X"05",X"03",X"00",X"5B",X"18",X"40",X"28",X"50",X"20",X"20",X"FC",X"68",X"C4",X"60",X"A3",X"64", -- #3E.0 : F70-F7F +X"03",X"01",X"80",X"5B",X"10",X"10",X"28",X"60",X"38",X"40",X"FC",X"60",X"C0",X"62",X"A3",X"60", -- #3E.1 : F80-F8F +X"07",X"01",X"00",X"5B",X"28",X"60",X"18",X"10",X"30",X"30",X"00",X"60",X"C0",X"60",X"A0",X"68", -- #3E.2 : F90-F9F +X"04",X"00",X"00",X"40",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- #3F.0 : FA0-FAF +X"01",X"00",X"18",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"20",X"30",X"FC",X"24",X"DE",X"43", -- #3F.1 : FB0-FBF +X"02",X"05",X"00",X"5B",X"20",X"50",X"30",X"40",X"18",X"10",X"F8",X"68",X"E0",X"6E",X"99",X"6C", -- #3F.2 : FC0-FCF +X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- : FD0-FDF +X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00", -- : FE0-FEF +X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00",X"00" -- : FF0-FFF +); +begin +process(clk) +begin + if rising_edge(clk) then + data <= rom_data(to_integer(unsigned(addr))); + end if; +end process; +end architecture; diff --git a/rtl/YM_2149/ym2149_audio.vhd b/rtl/YM_2149/ym2149_audio.vhd new file mode 100644 index 0000000..fd37c00 --- /dev/null +++ b/rtl/YM_2149/ym2149_audio.vhd @@ -0,0 +1,1192 @@ +-- +-- YM-2149 / AY-3-8910 Complex Sound Generator +-- Matthew Hagerty +-- June 2020 +-- https://dnotq.io +-- https://github.com/dnotq/ym2149_audio +-- + +-- Released under the 3-Clause BSD License: +-- +-- Copyright 2020 Matthew Hagerty (matthew dnotq io) +-- +-- Redistribution and use in source and binary forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- +-- 1. Redistributions of source code must retain the above copyright notice, +-- this list of conditions and the following disclaimer. +-- +-- 2. Redistributions in binary form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- 3. Neither the name of the copyright holder nor the names of its +-- contributors may be used to endorse or promote products derived from this +-- software without specific prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +-- POSSIBILITY OF SUCH DAMAGE. + +-- +-- This core is based mostly on the YM-2149 Programmable Sound Generator (PSG), +-- which is an upgraded version of the AY-3-8913 PSG. The main changes in the +-- YM-2149 over the AY-3-8910 are: +-- +-- * 5-bit envelope shape counter for smoother volume ramping, with 1.5dB +-- logarithmic steps. +-- * simplified host interface, i.e. no BC2 input. +-- * option to divide the input clock in half prior to further dividing. +-- +-- +-- TODO: Currently the two external 8-bit I/O ports are not implemented. +-- Their implementation should be easy enough, so it will probably happen in +-- the future when a system that uses the I/O is implemented (they would need +-- to be tested, and the SoC that this core was initially written for does not +-- use the PSG's I/O ports). + +-- A huge amount of effort has gone into making this core as accurate as +-- possible to the real IC, while at the same time making it usable in all- +-- digital SoC designs, i.e. retro-computer and game systems, etc. Design +-- elements from the real IC were used and implemented when possible, with any +-- work-around or changes noted along with the reasons. +-- +-- Synthesized and FPGA proven: +-- +-- * Xilinx Spartan-6 LX16, SoC 25.0MHz system clock, 3.58MHz clock-enable. +-- +-- +-- References: +-- +-- * The AY-3-8910 die-shot and reverse engineered schematic. This was the +-- most beneficial reference and greatly appreciated! Removes any questions +-- about what the real IC does. +-- https://github.com/lvd2/ay-3-8910_reverse_engineered +-- * The YM-2149 and AY-3-8910 datasheets. +-- * Real YM-2149 IC. +-- * Chip quirks, use, and abuse details from friends and retro enthusiasts. +-- +-- +-- Generates: +-- +-- * Unsigned 12-bit output for each channel. +-- * Unsigned 14-bit summation of the channels. +-- * Signed 14-bit PCM summation of the channels, with each channel +-- converted to -/+ zero-centered level or -/+ full-range level. +-- +-- The tone counters are period-limited to prevent the very high frequency +-- outputs that the original IC is capable of producing. Frequencies above +-- 20KHz cause problems in all-digital systems with sampling rates around +-- 44.1KHz to 48KHz. The primary use of these high frequencies was as a +-- carrier for amplitude modulated (AM) audio. The high frequency would be +-- filtered out by external electronics, leaving only the low frequency audio. +-- +-- When the tone counters are limited, the normal square-wave is set to always +-- output a '1', but the amplitude can still be changed, which allows the A.M. +-- technique to still work in a digital Soc. +-- + +-- Clock, Clock-enable, and Sel. +-- +-- The clk_i input should be the full-speed clock of the system, and +-- en_clk_psg_i must be a strobe (single clk_i tick) at the PSG operating +-- frequency. +-- +-- If the sel_n_i input is used to divide the en_clk_psg_i strobe, the host +-- interface will continue to operate with the non-divided en_clk_psg_i strobe. +-- +-- sel_n_i : 0 1 +-- en_clk_psg_i max : 4.0MHz 2.0MHz +-- +-- This core can use a much faster clock-enable than the original PSG, however +-- the clock-enable frequency affects the output audio frequency directly. If +-- producing sounds similar to the original ICs is desired, then using an +-- accurate input clock-enable is important. +-- +-- In circuits where these PSG ICs were used, the clocks tend to be derived +-- from division of the video clock frequency, and generally fell very close to +-- a frequency of 1.78MHz. +-- +-- Internally the PSG divides the clock by 3 or 4 to obtain a typical internal +-- operating frequency of around 447KHz. Some PSGs take this lower frequency +-- directly, but most do clock division internally to reduce the external +-- components needed by a designed using the PSG IC. +-- +-- Common frequencies found in retro systems: +-- +-- Crystal Pixel CPU PSG other PSG +-- 10.738MHz /2 = 5.369MHz /3 = 3.579MHz /6 = 1.789MHz /24 = 447.437KHz +-- 14.318MHz /2 = 7.159MHz /4 = 3.579MHz /8 = 1.789MHz +-- 21.477MHz /2 =10.738MHz /6 = 3.579MHz /12 = 1.789MHz +-- +-- A pixel clock of 5.369MHz is common for NTSC, and 7.159MHz is common for +-- standard resolution arcade games. +-- + +-- Basic host interface, uses the simplified form where BC2 is not used or +-- exposed externally (AY-3-8913). +-- +-- BDIR BC1 State +-- 0 0 Inactive +-- 0 1 Read from PSG +-- 1 0 Write to PSG +-- 1 1 Latch address +-- + +-- +-- Version history: +-- +-- June 28 2020 +-- V1.0. Release. SoC tested. +-- + + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +entity ym2149_audio is +generic -- Non-custom PSG address mask is 0000. + ( ADDRESS_G : std_logic_vector(3 downto 0) := x"0" +); +port + ( clk_i : in std_logic -- system clock + ; en_clk_psg_i : in std_logic -- PSG clock enable + ; sel_n_i : in std_logic -- divide select, 0=clock-enable/2 + ; reset_n_i : in std_logic -- active low + ; bc_i : in std_logic -- bus control + ; bdir_i : in std_logic -- bus direction + ; data_i : in std_logic_vector(7 downto 0) + ; data_r_o : out std_logic_vector(7 downto 0) -- registered output data + ; ch_a_o : out unsigned(11 downto 0) + ; ch_b_o : out unsigned(11 downto 0) + ; ch_c_o : out unsigned(11 downto 0) + ; mix_audio_o : out unsigned(13 downto 0) + ; pcm14s_o : out unsigned(13 downto 0) +); +end ym2149_audio; + +architecture rtl of ym2149_audio is + + -- Registered inputs. + signal reg_addr_r : unsigned(3 downto 0) := x"0"; + signal reg_addr_x : unsigned(3 downto 0); + signal data_i_r : std_logic_vector(7 downto 0) := x"00"; + signal data_o_r : std_logic_vector(7 downto 0) := x"00"; + signal busctl_r : std_logic_vector(1 downto 0) := "00"; + signal sel_n_r : std_logic := '1'; + signal sel_ff_r : std_logic := '1'; + signal sel_ff_x : std_logic; + signal en_int_clk_psg_s : std_logic; + + -- Register file, should infer to discrete flip-flops. + type reg_file_type is array (0 to 15) of std_logic_vector(7 downto 0); + signal reg_file_ar : reg_file_type := (others => (others => '0')); + + signal en_data_rd_s : std_logic; + signal en_data_wr_s : std_logic; + + -- Channel DAC levels. + signal ch_a_level_r : unsigned(11 downto 0) := (others => '0'); + signal ch_a_level_x : unsigned(11 downto 0); + signal ch_b_level_r : unsigned(11 downto 0) := (others => '0'); + signal ch_b_level_x : unsigned(11 downto 0); + signal ch_c_level_r : unsigned(11 downto 0) := (others => '0'); + signal ch_c_level_x : unsigned(11 downto 0); + + -- Added init value to channel period conbinatorial signals to keep + -- simulation from complaining about metavalues at 0ps. + + -- Register-name signals. + signal ch_a_period_s : unsigned(11 downto 0) := (others => '0'); + signal ch_a_tone_en_n_s : std_logic; + signal ch_a_noise_en_n_s : std_logic; + signal ch_a_mode_s : std_logic; + + signal ch_b_period_s : unsigned(11 downto 0) := (others => '0'); + signal ch_b_tone_en_n_s : std_logic; + signal ch_b_noise_en_n_s : std_logic; + signal ch_b_mode_s : std_logic; + + signal ch_c_period_s : unsigned(11 downto 0) := (others => '0'); + signal ch_c_tone_en_n_s : std_logic; + signal ch_c_noise_en_n_s : std_logic; + signal ch_c_mode_s : std_logic; + + signal noise_period_s : unsigned( 4 downto 0) := (others => '0'); + + signal env_period_s : unsigned(15 downto 0); + signal env_continue_s : std_logic; + signal env_attack_s : std_logic; + signal env_alternate_s : std_logic; + signal env_hold_s : std_logic; + + -- Clock conditioning counters and enables. + signal clk_div8_r : unsigned(2 downto 0) := "000"; + signal clk_div8_x : unsigned(2 downto 0); + signal en_cnt_r : std_logic := '0'; + signal en_cnt_x : std_logic; + + -- Channel tone and noise counters. + signal ch_a_cnt_r : unsigned(11 downto 0) := (others => '0'); + signal ch_a_cnt_x : unsigned(11 downto 0); + signal flatline_a_s : std_logic; + signal tone_a_r : std_logic := '1'; + signal tone_a_x : std_logic; + + signal ch_b_cnt_r : unsigned(11 downto 0) := (others => '0'); + signal ch_b_cnt_x : unsigned(11 downto 0); + signal flatline_b_s : std_logic; + signal tone_b_r : std_logic := '1'; + signal tone_b_x : std_logic; + + signal ch_c_cnt_r : unsigned(11 downto 0) := (others => '0'); + signal ch_c_cnt_x : unsigned(11 downto 0); + signal flatline_c_s : std_logic; + signal tone_c_r : std_logic := '1'; + signal tone_c_x : std_logic; + + signal noise_cnt_r : unsigned(4 downto 0) := (others => '0'); + signal noise_cnt_x : unsigned(4 downto 0); + signal flatline_n_s : std_logic; + signal noise_ff_r : std_logic := '1'; + signal noise_ff_x : std_logic; + + -- 17-bit Noise LFSR. + signal noise_lfsr_r : std_logic_vector(16 downto 0) := b"1_0000_0000_0000_0000"; + signal noise_lfsr_x : std_logic_vector(16 downto 0); + signal noise_fb_s : std_logic; + signal noise_s : std_logic; + + -- Tone and noise mixer. + signal mix_a_s : std_logic; + signal mix_b_s : std_logic; + signal mix_c_s : std_logic; + + -- Envelope counter. + signal env_shape_wr_r : std_logic := '0'; + signal env_shape_wr_x : std_logic; + signal env_rst_s : std_logic; + signal env_cnt_r : unsigned(15 downto 0) := (others => '0'); + signal env_cnt_x : unsigned(15 downto 0); + signal env_ff_r : std_logic := '1'; + signal env_ff_x : std_logic; + + signal shape_cnt_r : unsigned(4 downto 0) := (others => '0'); + signal shape_cnt_x : unsigned(4 downto 0); + + signal continue_ff_r : std_logic := '1'; + signal continue_ff_x : std_logic; + signal attack_ff_r : std_logic := '0'; + signal attack_ff_x : std_logic; + signal hold_ff_r : std_logic := '0'; + signal hold_ff_x : std_logic; + + -- Added init value to env_out_s to keep simulation from complaining + -- about metavalues at 0ps. + signal env_sel_s : std_logic; + signal env_out_s : unsigned(4 downto 0) := (others => '0'); + + -- Amplitude control. + signal level_a_s : unsigned(11 downto 0); + signal level_b_s : unsigned(11 downto 0); + signal level_c_s : unsigned(11 downto 0); + + -- DAC. + signal dac_a_r : unsigned(11 downto 0) := (others => '0'); + signal dac_b_r : unsigned(11 downto 0) := (others => '0'); + signal dac_c_r : unsigned(11 downto 0) := (others => '0'); + signal sum_audio_r : unsigned(13 downto 0) := (others => '0'); + + -- Digital to Analogue Output-level lookup table ROM. + -- + -- The DAC is implemented with tuned NMOS transistors in the real IC. For + -- the FPGA version, the 4-bit register level or 5-bit output from the + -- envelop is used as an index into a calculated table of values that + -- represent the equivalent voltage. + -- + -- The output scale is amplitude logarithmic. + -- + -- ln10 = Natural logarithm of 10, ~= 2.302585 + -- amp = Amplitude in voltage, 0.2, 1.45, etc. + -- dB = decibel value in dB, -1.5, -3, etc. + -- + -- dB = 20 * log(amp) / ln10 + -- amp = 10 ^ (dB / 20) + -- + -- -1.5dB = 0.8413951416 + -- -2.0dB = 0.7943282347 + -- -3.0dB = 0.7079457843 + -- + -- The datasheet defines a normalize 0V to 1V graph and oscilloscope photo + -- of the output curve. The AY-3-8910 has 16-steps that are -3.0dB apart, + -- and the YM-2149 has 32-steps that are -1.5dB apart. + -- + -- 1V reference values based on the datasheet: + -- + -- 1.0000, 0.8414, 0.7079, 0.5957, 0.5012, 0.4217, 0.3548, 0.2985, + -- 0.2512, 0.2113, 0.1778, 0.1496, 0.1259, 0.1059, 0.0891, 0.0750, + -- 0.0631, 0.0531, 0.0447, 0.0376, 0.0316, 0.0266, 0.0224, 0.0188, + -- 0.0158, 0.0133, 0.0112, 0.0094, 0.0079, 0.0067, 0.0056, 0.0047 + -- + -- A 10-bit number (0..1023) can support a scaled version of the reference + -- list without having any repeating values. Using 9-bits is close, and + -- 8-bit values have too many duplicate values at the bottom-end to make for + -- a nice curve. Duplicate values means several volume levels produce the + -- same output level, and is not accurate. + -- + -- Using a 12-bit output value means the three channels can be summed into a + -- 14-bit value without overflow, and leaves room for adjustments if + -- converting to something like 16-bit PCM. The 12-bit values also provide + -- a nicer curve, and are easier to initialize in VHDL. + -- + -- The lowest volume level needs to go to 0 in a digital SoC to prevent + -- noise that would be filtered in a real system with external electronics. + + -- Added init value to dac_reg_bit0_s to keep simulation from complaining + -- about metavalues at 0ps. + signal dac_reg_bit0_s : std_logic := '0'; + signal dac_reg_level_s : unsigned(11 downto 0); + signal dac_env_level_s : unsigned(11 downto 0); + + -- Output-level lookup table ROM. + type dacrom_type is array (0 to 31) of unsigned(11 downto 0); + signal dacrom_ar : dacrom_type := ( + -- 19, 23, 27, 33, 39, 46, 55, 65, + -- 77, 92, 109, 129, 154, 183, 217, 258, + -- 307, 365, 434, 516, 613, 728, 865,1029, + -- 1223,1453,1727,2052,2439,2899,3446,4095 + x"000",x"017",x"01B",x"021",x"027",x"02E",x"037",x"041", + x"04D",x"05C",x"06D",x"081",x"09A",x"0B7",x"0D9",x"102", + x"133",x"16D",x"1B2",x"204",x"265",x"2D8",x"361",x"405", + x"4C7",x"5AD",x"6BF",x"804",x"987",x"B53",x"D76",x"FFF"); + + -- PCM signed 14-bit. + signal sign_a_r : unsigned(11 downto 0) := (others => '0'); + signal sign_a_x : unsigned(11 downto 0); + signal level_a_env_s : unsigned(11 downto 0); + signal sign_b_r : unsigned(11 downto 0) := (others => '0'); + signal sign_b_x : unsigned(11 downto 0); + signal level_b_env_s : unsigned(11 downto 0); + signal sign_c_r : unsigned(11 downto 0) := (others => '0'); + signal sign_c_x : unsigned(11 downto 0); + signal level_c_env_s : unsigned(11 downto 0); + + signal pcm14s_r : unsigned(13 downto 0) := (others => '0'); + +begin + + -- Register the input data at the full clock rate. + process ( clk_i ) begin if rising_edge(clk_i) then + sel_n_r <= sel_n_i; + busctl_r <= bdir_i & bc_i; + data_i_r <= data_i; + end if; end process; + + -- Registered data output. + data_r_o <= data_o_r; + + + -- ----------------------------------------------------------------------- + -- + -- External bus interface and register file. + -- + -- BDIR BC State + -- 0 0 Inactive + -- 0 1 Read from PSG register + -- 1 0 Write to PSG register + -- 1 1 Latch address + -- + -- A generic register file is used here instead of specific registers and + -- decode logic. However, the real IC only has registers of exact size and + -- performs decode and generates enable signals. + -- + + process + ( bdir_i, bc_i, busctl_r + , data_i_r, reg_addr_r + ) begin + + reg_addr_x <= reg_addr_r; + en_data_rd_s <= '0'; + en_data_wr_s <= '0'; + + case ( busctl_r ) is + when "00" => -- inactive. + null; + when "01" => -- read from PSG register. + en_data_rd_s <= '1'; + when "10" => -- write to PSG register. + en_data_wr_s <= '1'; + -- "11" + when others => -- latch register address. + -- The PSG is factory set to a high-address mask of "0000", unless the + -- PSG was special ordered with a custom-masked address. + if data_i_r(7 downto 4) = ADDRESS_G then + reg_addr_x <= unsigned(data_i_r(3 downto 0)); + end if; + end case; + end process; + + -- Decode envelope shape register writes since they cause a reset of the + -- envelope counter and state machine. The enable lasts as long as the + -- write cycle, which is accurate to the real IC. It is registered to + -- prevent long combinatorial paths from the write enable. + env_shape_wr_x <= en_data_wr_s when reg_addr_r = x"D" else '0'; + + + -- The output-level is converted to the equivalent DAC value and stored as + -- as the look-up result, rather than the 4-bit level index. This allows + -- sharing of the ROM lookup table, and uses less FPGA resources. + dac_reg_bit0_s <= '0' when data_i_r(3 downto 0) = "0000" else '1'; + dac_reg_level_s <= dacrom_ar(to_integer(unsigned(data_i_r(3 downto 0) & dac_reg_bit0_s))); + + ch_a_level_x <= dac_reg_level_s when reg_addr_r = x"8" else ch_a_level_r; + ch_b_level_x <= dac_reg_level_s when reg_addr_r = x"9" else ch_b_level_r; + ch_c_level_x <= dac_reg_level_s when reg_addr_r = x"A" else ch_c_level_r; + + + -- Registers. + -- + -- 7654 3210 + -- R0 PPPP PPPP Channel A tone period 7..0. + -- R1 ---- PPPP Channel A tone period 11..8. + -- R2 PPPP PPPP Channel B tone period 7..0. + -- R3 ---- PPPP Channel B tone period 11..8. + -- R4 PPPP PPPP Channel C tone period 7..0. + -- R5 ---- PPPP Channel C tone period 11..8. + -- R6 ---P PPPP Noise shift period. + -- R7 I--- ---- I/O Port B IN_n/OUT control. + -- -I-- ---- I/O Port A IN_n/OUT control. + -- --C- ---- Mix Noise with Channel C, active low. + -- ---B ---- Mix Noise with Channel B, active low. + -- ---- A--- Mix Noise with Channel A, active low. + -- ---- -C-- Enable Channel C, active low. + -- ---- --B- Enable Channel B, active low. + -- ---- ---A Enable Channel A, active low. + -- R8 ---M ---- Channel A Mode, 0=level, 1=envelope. + -- ---- LLLL Channel A Level. + -- R9 ---M ---- Channel B Mode, 0=level, 1=envelope. + -- ---- LLLL Channel B Level. + -- R10 ---M ---- Channel C Mode, 0=level, 1=envelope. + -- ---- LLLL Channel C Level. + -- R11 PPPP PPPP Envelope period 7..0. + -- R12 PPPP PPPP Envelope period 15..8. + -- R13 ---- C--- Envelope shape "Continue" control. + -- ---- -A-- Envelope shape "Attack" control. + -- ---- --A- Envelope shape "Alternate" control. + -- ---- ---H Envelope shape "Hole" control. + -- R14 DDDD DDDD I/O port A data. + -- R15 DDDD DDDD I/O port B data. + + process + ( clk_i, en_clk_psg_i, reset_n_i + ) begin + if rising_edge(clk_i) then + if reset_n_i = '0' then + + reg_addr_r <= (others => '0'); + data_o_r <= (others => '0'); + env_shape_wr_r <= '0'; + ch_a_level_r <= x"800"; -- Default to half-range for 0 level. + ch_b_level_r <= x"800"; -- Default to half-range for 0 level. + ch_c_level_r <= x"800"; -- Default to half-range for 0 level. + + reg_file_ar(0) <= x"00"; + reg_file_ar(1) <= x"00"; + reg_file_ar(2) <= x"00"; + reg_file_ar(3) <= x"00"; + reg_file_ar(4) <= x"00"; + reg_file_ar(5) <= x"00"; + reg_file_ar(6) <= x"00"; + reg_file_ar(7) <= x"00"; + reg_file_ar(8) <= x"00"; + reg_file_ar(9) <= x"00"; + reg_file_ar(10) <= x"00"; + reg_file_ar(11) <= x"00"; + reg_file_ar(12) <= x"00"; + reg_file_ar(13) <= x"00"; + reg_file_ar(14) <= x"00"; + reg_file_ar(15) <= x"00"; + + elsif en_clk_psg_i = '1' then + + reg_addr_r <= reg_addr_x; + env_shape_wr_r <= env_shape_wr_x; + + if en_data_rd_s = '1' then + data_o_r <= reg_file_ar(to_integer(reg_addr_r)); + elsif en_data_wr_s = '1' then + reg_file_ar(to_integer(reg_addr_r)) <= data_i_r; + + ch_a_level_r <= ch_a_level_x; + ch_b_level_r <= ch_b_level_x; + ch_c_level_r <= ch_c_level_x; + end if; + + end if; + end if; + end process; + + + -- Register file name mapping. Convenience for this implementation. The + -- real IC does not have full 16x8-bit registers. Confirmed via die-shots. + ch_a_period_s <= unsigned(reg_file_ar(1)(3 downto 0)) & unsigned(reg_file_ar(0)); + ch_a_tone_en_n_s <= reg_file_ar(7)(0); + ch_a_noise_en_n_s <= reg_file_ar(7)(3); + ch_a_mode_s <= reg_file_ar(8)(4); + + ch_b_period_s <= unsigned(reg_file_ar(3)(3 downto 0)) & unsigned(reg_file_ar(2)); + ch_b_tone_en_n_s <= reg_file_ar(7)(1); + ch_b_noise_en_n_s <= reg_file_ar(7)(4); + ch_b_mode_s <= reg_file_ar(9)(4); + + ch_c_period_s <= unsigned(reg_file_ar(5)(3 downto 0)) & unsigned(reg_file_ar(4)); + ch_c_tone_en_n_s <= reg_file_ar(7)(2); + ch_c_noise_en_n_s <= reg_file_ar(7)(5); + ch_c_mode_s <= reg_file_ar(10)(4); + + noise_period_s <= unsigned(reg_file_ar(6)(4 downto 0)); + + env_period_s <= unsigned(reg_file_ar(12)) & unsigned(reg_file_ar(11)); + env_continue_s <= reg_file_ar(13)(3); + env_attack_s <= reg_file_ar(13)(2); + env_alternate_s <= reg_file_ar(13)(1); + env_hold_s <= reg_file_ar(13)(0); + + -- TODO implement I/O registers. + + + -- ----------------------------------------------------------------------- + -- + -- Clock conditioning. + -- + + -- Divide the input clock enable. + sel_ff_x <= not sel_ff_r; + + -- Select the clock enable based on the sel_n_i input. + en_int_clk_psg_s <= en_clk_psg_i and sel_ff_r when sel_n_r = '0' else en_clk_psg_i; + + process ( clk_i, en_clk_psg_i ) begin + if rising_edge(clk_i) then + if en_clk_psg_i = '1' then + sel_ff_r <= sel_ff_x; + end if; + end if; end process; + + + -- Reduce the input clock to provide the divide by eight clock-phases, count + -- enable, and count reset. + clk_div8_x <= clk_div8_r + 1; + + -- The real IC counts on the 4th divider count (100), so do the same with + -- the enable (which is active during the next state, thus 3 instead of 4). + en_cnt_x <= '1' when clk_div8_r = 3 else '0'; + + process + ( clk_i, en_int_clk_psg_s, reset_n_i + ) begin + if rising_edge(clk_i) then + if reset_n_i = '0' then + clk_div8_r <= (others => '0'); + en_cnt_r <= '0'; + elsif en_int_clk_psg_s = '1' then + clk_div8_r <= clk_div8_x; + en_cnt_r <= en_cnt_x; + end if; + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Channel tone counters. The counters *always* count unless in reset. + -- + + -- Timing for 0 and 1 period-count, showing why they are the same. The real + -- IC counts on the 4-count of the clock divider, so this implementation + -- does the same. In the real IC, the >= reset and count enable happen in + -- the same 4-count cycle due to the asynchronous nature of the IC. In a + -- synchronous FPGA design, the >= reset is performed 1-cycle prior to the + -- count enable. + -- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + -- _/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_ PSG clock (input clock enable) + -- _______________ _______________ + -- ___3___4_/ 5 6 7 0 \_1___2___3___4_/ 5 6 7 0 \_1___2___3___4_ divided clock count + -- ___ ___ ___ + -- _/ \___________________________/ \___________________________/ \___ >= enable (resets count to 0) + -- ___ ___ ___ + -- _____/ \___________________________/ \___________________________/ count enable + -- _____ ___ ___________________________ ___ ___________________________ ___ + -- _____X_0_X______________1____________X_0_X______________1____________X_0_ tone counter + -- _____ _______________________________ + -- \_______________________________/ \___ tone flip-flop + + -- The tone counter >= reset strobe must be enabled prior to the count enable + -- so the tone counter is only a zero-count for *one input clock cycle*, and + -- not the count-enable cycle (i.e. input clock divided by 8). + -- + -- This is the reason why a period-count of zero is the same as a period- + -- count of one. The time the counter has a zero value is merged with the + -- last 8-cycle period where the >= condition is detected. + -- + -- In the real IC the reset is asynchronous combinatorial logic (as is the + -- whole IC). For the FPGA, combinatorial look-ahead is used. + -- + -- The channel counters in the real IC perform a greater-than or equal-to + -- comparison to toggle the tone output flip-flop. + -- + -- Loading new values into the registers does NOT affect the counter or tone + -- flip-flop in any way directly. A new register value many cause the >= + -- comparison be become true immediately, which will cause the tone flip- + -- flop to *toggle*, but does not guarantee the high or low state of the + -- flip-flop. + -- + -- The *only* way to set the tone flip-flop to a known state is via the + -- external chip reset, in which case the flip-flop will be set ('1'). + -- + -- A chip-accurate counter implementation in C might look like this: + -- + -- { + -- if ( counter >= period ) { + -- tone = !tone; + -- counter = 0; + -- } + -- + -- counter++; + -- } + -- + -- With the period work-around described below: + -- + -- { + -- if ( counter >= period ) + -- { + -- if ( period < 6 ) { + -- tone = 1; + -- } else { + -- tone = !tone; + -- } + -- + -- counter = 0; + -- } + -- + -- counter++; + -- } + + -- High frequency noise problem. + -- + -- With a typical 1.78MHz input clock, the divide by eight produces a + -- 223.72KHz clock into the tone counters. With small period counts, + -- frequencies *well over* the human hearing range can be produced. + -- + -- The problem with the frequencies over 20KHz is, in a digital SoC the high + -- frequencies do not filter out like they do when the output is connected + -- to an external low-pass filter and inductive load (speaker), like they + -- are with the real IC. + -- + -- In an all digital system with digital audio, the generated frequencies + -- should never be more than the Nyquist frequency (twice the sample rate). + -- Typical sample rates are 44KHz or 48KHz, so any frequency over 20KHz + -- should not be output (and is not audible to a human anyway). + -- + -- The work-around here is to flat-line the toggle flip-flop for any tone + -- counter with a period that produces a frequency over the Nyquist rate. + -- This change still allows the technique of modulating the output with + -- rapid volume level changes. + -- + -- Based on a typical PSG clock of 1.78MHz for a Z80-based system, the + -- period counts that cause frequencies above 20KHz are: + -- + -- f = CLK / (16 * Count) + -- + -- Clock 1,789,772Hz 55.873ns + -- + -- Count Frequency Period + -- 0 111860.78Hz 8.9396us * same as a count of 1, see above. + -- 1 111860.78Hz 8.9396us + -- 2 55930.39Hz 17.8793us + -- 3 37286.92Hz 26.8190us + -- 4 27965.19Hz 35.7587us + -- 5 22372.15Hz 44.6984us + -- ---------------------------- + -- 6 18643.46Hz 53.6381us First audible count. + + + -- A channel counter and tone flip-flop. + ch_a_cnt_x <= + -- Reset uses counter next-state look-ahead. + (others => '0') when (en_cnt_x = '1' and ch_a_cnt_r >= ch_a_period_s) else + -- Counting uses current-state. + ch_a_cnt_r + 1 when en_cnt_r = '1' else + ch_a_cnt_r; + + flatline_a_s <= '1' when ch_a_period_s < 6 else '0'; + + tone_a_x <= + -- Flat-line the output for counts that produce frequencies > 20KHz. + '1' when flatline_a_s = '1' else + -- Toggle uses counter next-state look-ahead, same condition as reset. + not tone_a_r when (en_cnt_x = '1' and ch_a_cnt_r >= ch_a_period_s) else + tone_a_r; + + -- B channel counter and tone flip-flop. + ch_b_cnt_x <= + (others => '0') when (en_cnt_x = '1' and ch_b_cnt_r >= ch_b_period_s) else + ch_b_cnt_r + 1 when en_cnt_r = '1' else + ch_b_cnt_r; + + flatline_b_s <= '1' when ch_b_period_s < 6 else '0'; + + tone_b_x <= + '1' when flatline_b_s = '1' else + not tone_b_r when (en_cnt_x = '1' and ch_b_cnt_r >= ch_b_period_s) else + tone_b_r; + + -- C channel counter and tone flip-flop. + ch_c_cnt_x <= + (others => '0') when (en_cnt_x = '1' and ch_c_cnt_r >= ch_c_period_s) else + ch_c_cnt_r + 1 when en_cnt_r = '1' else + ch_c_cnt_r; + + flatline_c_s <= '1' when ch_c_period_s < 6 else '0'; + + tone_c_x <= + '1' when flatline_c_s = '1' else + not tone_c_r when (en_cnt_x = '1' and ch_c_cnt_r >= ch_c_period_s) else + tone_c_r; + + + process + ( clk_i, en_int_clk_psg_s, reset_n_i + ) begin + if rising_edge(clk_i) then + + if reset_n_i = '0' then + ch_a_cnt_r <= (others => '0'); + tone_a_r <= '1'; -- Verified resets to '1' in the real IC. + + ch_b_cnt_r <= (others => '0'); + tone_b_r <= '1'; -- Verified resets to '1' in the real IC. + + ch_c_cnt_r <= (others => '0'); + tone_c_r <= '1'; -- Verified resets to '1' in the real IC. + + elsif en_int_clk_psg_s = '1' then + ch_a_cnt_r <= ch_a_cnt_x; + tone_a_r <= tone_a_x; + + ch_b_cnt_r <= ch_b_cnt_x; + tone_b_r <= tone_b_x; + + ch_c_cnt_r <= ch_c_cnt_x; + tone_c_r <= tone_c_x; + end if; + + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Noise period counter. Identical to the tone counters, only shorter, and + -- the toggle flip-flop output of the counter is used as the clock input to + -- the LFSR. Thus, the noise counter effectively divides the output shift + -- rate in half one additional time. + -- + -- With the lowest period counter of 0 or 1 (both values produce the same + -- count), the LFSR will output a bit at the rate of one-bit for every 16 + -- main clock cycles, maximum. + -- + -- The noise counter has the same high frequency problem for digital systems + -- as the tone counters, but the noise counter has one extra flip-flop + -- before the counter, so its minimum counter will be one lower than the + -- tone counter minimum. + -- + -- PSG + -- Clk 3,579,545Hz 2.793651ns + -- + -- Cnt Frequency Period + -- 0 55930.39062Hz 17.87936us * same as a count of 1, see above. + -- 1 55930.39062Hz 17.87936us + -- 2 37286.92708Hz 26.81905us + -- 3 27965.19531Hz 35.75873us + -- 4 22372.15625Hz 44.69841us + -- ------------------------------- + -- 5 18643.46354Hz 53.63810us First audible count. + + noise_cnt_x <= + -- Reset uses counter next-state look-ahead. + (others => '0') when (en_cnt_x = '1' and noise_cnt_r >= noise_period_s) else + -- Counting uses current-state. + noise_cnt_r + 1 when en_cnt_r = '1' else + noise_cnt_r; + + flatline_n_s <= '1' when noise_period_s < 5 else '0'; + + noise_ff_x <= + -- Flat-line the output for counts that produce frequencies > 20KHz. + '1' when flatline_n_s = '1' else + -- Toggle uses counter next-state look-ahead, same condition as reset. + not noise_ff_r when (en_cnt_x = '1' and noise_cnt_r >= noise_period_s) else + noise_ff_r; + + + -- Noise 17-bit right-shift LFSR with taps at 0 and 3, LS-bit is the output. + -- Verified against the reverse engineered 8910 die-shot. Reset loads the + -- LFSR with 0x10000 to prevent lock-up. + noise_fb_s <= noise_lfsr_r(3) xor noise_lfsr_r(0); + noise_lfsr_x <= noise_fb_s & noise_lfsr_r(16 downto 1); + noise_s <= noise_lfsr_r(0); + + + process + ( clk_i, en_int_clk_psg_s, reset_n_i + ) begin + if rising_edge(clk_i) then + + if reset_n_i = '0' then + noise_cnt_r <= (others => '0'); + noise_ff_r <= '1'; -- Verified resets to '1' in the real IC. + noise_lfsr_r <= b"1_0000_0000_0000_0000"; + + elsif en_int_clk_psg_s = '1' then + noise_cnt_r <= noise_cnt_x; + noise_ff_r <= noise_ff_x; + -- Look-ahead rising-edge detect the noise flip-flop. + if noise_ff_r = '0' and noise_ff_x = '1' then + noise_lfsr_r <= noise_lfsr_x; + end if; + end if; + + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Tone and Noise mixer. + -- + -- The output of the tone and noise section is a '0' or '1' for each channel + -- and the noise. Each tone bit is optionally mixed with the noise bit. + -- The final bit is used to determine if the amplitude is used or not. + + -- Tone and noise mixer. The enables are active low, so if the channel and + -- noise are disabled, the output will be a '1', not a '0'. This allows a + -- disabled channel to still be amplitude modulated by changing the volume. + + mix_a_s <= (ch_a_tone_en_n_s or tone_a_r) and (ch_a_noise_en_n_s or noise_s); + mix_b_s <= (ch_b_tone_en_n_s or tone_b_r) and (ch_b_noise_en_n_s or noise_s); + mix_c_s <= (ch_c_tone_en_n_s or tone_c_r) and (ch_c_noise_en_n_s or noise_s); + + + -- ----------------------------------------------------------------------- + -- + -- Envelope counter. Works the same as the noise counter, with the envelope + -- period-counter clocking the envelope shape-counter. The YM-2149 has a + -- 5-bit shape counter, but it counts twice as fast so it produces smoother + -- ramping of the envelope in the same time (one cycle of the envelope + -- period). + -- + -- When the envelope-period is 0 or 1, the AY-3-8910 shape-counter counts at + -- one-count for every 16 main-clock cycles, and the YM-2149 counts twice in + -- the same 16 cycles. + -- + -- Unlike the other counters, the envelope period-counter, shape-counter, + -- and shape FSM are reset when the shape register is written, as well as + -- the global reset. + + env_cnt_x <= + -- Reset uses counter next-state look-ahead. + (others => '0') when (en_cnt_x = '1' and env_cnt_r >= env_period_s) else + -- Counting uses current-state. + env_cnt_r + 1 when en_cnt_r = '1' else + env_cnt_r; + + env_ff_x <= + -- Toggle uses counter next-state look-ahead, same condition as reset. + not env_ff_r when (en_cnt_x = '1' and env_cnt_r >= env_period_s) else + env_ff_r; + + -- The envelope reset is active during the global reset input as well as + -- when the envelope shape register is written. + env_rst_s <= (not reset_n_i) or env_shape_wr_r; + + -- Shape counter. Hold forces the shape counter to a "set" state. + -- Look-ahead on the hold flip-flop to prevent counter roll-over. + shape_cnt_x <= (others => '1') when hold_ff_x = '1' else shape_cnt_r + 1; + + + process + ( clk_i, en_int_clk_psg_s, env_rst_s + , hold_ff_r, env_ff_r, env_ff_x + ) begin + if rising_edge(clk_i) then + + -- ** NOTE: This reset is active high. + if env_rst_s = '1' then + env_cnt_r <= (others => '0'); + env_ff_r <= '1'; -- Verified resets to '1' in the real IC. + shape_cnt_r <= (others => '0'); + + elsif en_int_clk_psg_s = '1' then + env_cnt_r <= env_cnt_x; + env_ff_r <= env_ff_x; + + -- Look-ahead edge detect the envelope flip-flop to provide two + -- counts in the same number of cycles. Hold inhibits the shape + -- counter's clock input. + if hold_ff_r = '0' and env_ff_r /= env_ff_x then + shape_cnt_r <= shape_cnt_x; + end if; + end if; + + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Envelope FSM. Four envelope control bits determine how the shape + -- counter operates after it has counted from 0 to 31. + -- + -- The envelope waveform is used as an amplitude control for use with the + -- tone channels. + -- + -- Register 15 Envelope Shape Control + -- + -- +------- Continue + -- | +----- Attack + -- | | +--- Alternate + -- | | | +- Hold + -- 3 2 1 0 + -- ------- + -- \ + -- 0 0 X X \______________ + -- + -- /| + -- 0 1 X X / |_____________ + -- + -- \ |\ |\ |\ |\ |\ + -- 1 0 0 0 \| \| \| \| \| \ + -- + -- \ + -- 1 0 0 1 \______________ + -- + -- \ /\ /\ /\ / + -- 1 0 1 0 \/ \/ \/ \/ + -- _____________ + -- \ | + -- 1 0 1 1 \| + -- + -- /| /| /| /| /| /| + -- 1 1 0 0 / |/ |/ |/ |/ |/ | + -- _______________ + -- / + -- 1 1 0 1 / + -- + -- /\ /\ /\ /\ + -- 1 1 1 0 / \/ \/ \/ \ + -- + -- /| + -- 1 1 1 1 / |______________ + + -- Continue is rather useless, since the envelopes created by using it are + -- duplicates of envelopes created with the other three control bits. + + -- "Continue" is a final envelope output select. After the shape counter + -- completes the first 0..31 count after being reset, if the continue bit + -- from the shape register is zero, the final envelope output will be zero. + continue_ff_x <= env_continue_s when shape_cnt_r = 31 else continue_ff_r; + + -- "Attack" is a selection between the regular or inverted shape count. + -- The real IC gates the ~q output from the attack flip-flop when the + -- attack register is '1'. + env_sel_s <= not attack_ff_r when env_attack_s = '1' else attack_ff_r; + + -- "Alternate" is an enable for toggling the Attack flip-flop. + attack_ff_x <= + not attack_ff_r when shape_cnt_r = 31 and env_alternate_s = '1' else + attack_ff_r; + + -- "Hold" sets ('1') all the flip-flops in the shape counter and inhibits it + -- it from counting. The only way to reset the hold is the global reset or + -- writing to the shape register (which issues the local envelope reset). + hold_ff_x <= env_hold_s when shape_cnt_r = 31 else hold_ff_r; + + env_out_s <= + (others => '0') when continue_ff_r = '0' else + not shape_cnt_r when env_sel_s = '0' else + shape_cnt_r; + + dac_env_level_s <= dacrom_ar(to_integer(env_out_s)); + + process + ( clk_i, en_int_clk_psg_s, env_rst_s, hold_ff_r + ) begin + if rising_edge(clk_i) then + + -- ** NOTE: This reset is active high. + if env_rst_s = '1' then + continue_ff_r <= '1'; + attack_ff_r <= '0'; + hold_ff_r <= '0'; + + elsif en_int_clk_psg_s = '1' then + + -- Look-ahead edge detect the envelope flip-flop to provide two + -- counts in the same number of cycles. Hold inhibits the shape + -- counter's clock input. + if hold_ff_r = '0' and env_ff_r /= env_ff_x then + continue_ff_r <= continue_ff_x; + attack_ff_r <= attack_ff_x; + hold_ff_r <= hold_ff_x; + end if; + end if; + + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Amplitude control. The amplitude of each tone channel is controlled + -- by one of two ways: + -- + -- 1. The 4-bit amplitude setting from the channel's amplitude register. + -- 2. The 5-bit amplitude from the envelope generator. + -- + -- The selection is controlled by a mode-bit in each channel's amplitude + -- register. + -- + -- Because the envelope amplitude is 5-bits vs the 4-bits of the amplitude + -- register, the envelope amplitude will produce smoother ramps by changing + -- the level in 1.5dB half steps. + -- + -- The output of the mixer takes precedence, otherwise the level vs. + -- envelope selection is used. + + + level_a_s <= + (others => '0') when mix_a_s = '0' else + ch_a_level_r when ch_a_mode_s = '0' else + dac_env_level_s; + + level_b_s <= + (others => '0') when mix_b_s = '0' else + ch_b_level_r when ch_b_mode_s = '0' else + dac_env_level_s; + + level_c_s <= + (others => '0') when mix_c_s = '0' else + ch_c_level_r when ch_c_mode_s = '0' else + dac_env_level_s; + + + -- ----------------------------------------------------------------------- + -- + -- Digital to Analogue Converter. + -- + + ch_a_o <= dac_a_r; + ch_b_o <= dac_b_r; + ch_c_o <= dac_c_r; + mix_audio_o <= sum_audio_r; + + process + ( clk_i, en_clk_psg_i + ) begin + if rising_edge(clk_i) then + if en_clk_psg_i = '1' then + + dac_a_r <= level_a_s; + dac_b_r <= level_b_s; + dac_c_r <= level_c_s; + + -- Convenience sum the audio channels. + sum_audio_r <= ("00" & dac_a_r) + ("00" & dac_b_r) + ("00" & dac_c_r); + end if; + end if; + end process; + + + -- ----------------------------------------------------------------------- + -- + -- Signed zero-centered 14-bit PCM. + -- + + -- Make a -/+ level value depending on the tone state. When the tone is + -- disabled, adjust the unsigned level-range to a signed-level range. + -- + -- signed_level = + -- level - (range / 2) when disabled + -- -(level / 2) when tone == 0 + -- (level / 2) when tone == 1 + + level_a_env_s <= ch_a_level_r when ch_a_mode_s = '0' else dac_env_level_s; + level_b_env_s <= ch_b_level_r when ch_b_mode_s = '0' else dac_env_level_s; + level_c_env_s <= ch_c_level_r when ch_c_mode_s = '0' else dac_env_level_s; + + sign_a_x <= + -- Make a flat-level into a full-range signed value. + level_a_env_s - x"800" when + -- Channel is disabled, produces a constant '1' for the channel. + ((ch_a_tone_en_n_s and ch_a_noise_en_n_s) or + -- Channel is flat-lined. + flatline_a_s or + -- Channel + noise enabled, but noise is flat-lined. + ((not ch_a_noise_en_n_s) and flatline_n_s)) = '1' else + + -- Otherwise, make signed range value from tone 0 or 1. + ("1" & (not level_a_env_s(11 downto 1))) + 1 when mix_a_s = '0' else + ("0" & level_a_env_s(11 downto 1)); + + sign_b_x <= + level_b_env_s - x"800" when + ((ch_b_tone_en_n_s and ch_b_noise_en_n_s) or + flatline_b_s or + ((not ch_b_noise_en_n_s) and flatline_n_s)) = '1' else + ("1" & (not level_b_env_s(11 downto 1))) + 1 when mix_b_s = '0' else + ("0" & level_b_env_s(11 downto 1)); + + sign_c_x <= + level_c_env_s - x"800" when + ((ch_c_tone_en_n_s and ch_c_noise_en_n_s) or + flatline_c_s or + ((not ch_c_noise_en_n_s) and flatline_n_s)) = '1' else + ("1" & (not level_c_env_s(11 downto 1))) + 1 when mix_c_s = '0' else + ("0" & level_c_env_s(11 downto 1)); + + + pcm14s_o <= pcm14s_r; + + + process + ( clk_i, en_clk_psg_i + ) begin + if rising_edge(clk_i) then + if en_clk_psg_i = '1' then + + sign_a_r <= sign_a_x; + sign_b_r <= sign_b_x; + sign_c_r <= sign_c_x; + + -- Sum to signed 14-bit. + pcm14s_r <= + (sign_a_r(11) & sign_a_r(11) & sign_a_r) + + (sign_b_r(11) & sign_b_r(11) & sign_b_r) + + (sign_c_r(11) & sign_c_r(11) & sign_c_r); + + end if; + end if; + end process; + +end rtl; diff --git a/rtl/coco3fpga.sv b/rtl/coco3fpga.sv index ed55e5a..e4021af 100644 --- a/rtl/coco3fpga.sv +++ b/rtl/coco3fpga.sv @@ -937,6 +937,9 @@ assign DATA_IN = (ADDRESS == 16'hFF74) ? {1'b0, GART_READ[22:16]}: (ADDRESS == 16'hFF75) ? { GART_READ[15:8]}: (ADDRESS == 16'hFF76) ? { GART_READ[7:0]}: +`ifdef CoCo3_enable_Music_Speech + (ADDRESS == 16'hFF7E) ? MUS_SPEECH_RD_DATA: +`endif (ADDRESS == 16'hFF7F) ? {2'b11, MPI_CTS, 2'b00, MPI_SCS}: // (ADDRESS == 16'hFF80) ? {CK_DONE_BUF[1], // CK_FAIL, @@ -1644,6 +1647,29 @@ sdc_top coco_sdc_top( .sd_buff_wr(sd_buff_wr) ); +//////////////////////////////////////////////////////////////// +// Speech and Music cart + +`ifdef CoCo3_enable_Music_Speech + +wire [7:0] MUS_SPEECH_RD_DATA; +wire [7:0] Mus_Spch_Audio; + +Music_Speech CoCo3_Music_Speech( + .RESET_N(RESET_N), + .CLK(clk_sys), + .CLK_EN(PH_2), + .CLK_1_78(clk_1_78), + .MPI_SCS(SWITCH[2:1]), + .ADDRESS(ADDRESS[15:0]), + .WRITE_DATA(DATA_OUT), + .READ_DATA(MUS_SPEECH_RD_DATA), + .RW_N(RW_N), + .AUDIO(Mus_Spch_Audio) +); + +`endif + reg cart_firq_enable; @@ -3312,7 +3338,12 @@ end // The code for the internal and Orchestra sound // Internal Sound generation + +`ifdef CoCo3_enable_Music_Speech +assign SOUND = {SBS, 7'b0000000} + {SOUND_DTOA, SOUND_DTOA[5:4]} + Mus_Spch_Audio; +`else assign SOUND = {SBS, 7'b0000000} + {SOUND_DTOA, SOUND_DTOA[5:4]}; +`endif assign SOUND_LEFT = {ORCH_LEFT, ORCH_LEFT} + {SOUND, SOUND}; assign SOUND_RIGHT = {ORCH_RIGHT, ORCH_RIGHT} + {SOUND, SOUND}; @@ -3519,7 +3550,7 @@ begin begin clk_1_78 <= 1'b0; clk_178_ctr <= clk_178_ctr + 1'b1; - if (clk_178_ctr == 5'd27) + if (clk_178_ctr == 5'd31) begin clk_1_78 <= 1'b1; end diff --git a/rtl/config.v b/rtl/config.v index 529b95a..4247929 100644 --- a/rtl/config.v +++ b/rtl/config.v @@ -80,3 +80,4 @@ `set_feature(CoCo3_disable_GART_in_GIMEX, FEATURE_6)// Disable GIMEX ram transfers [GIMEX detection in OS( EOU] +//`set_feature(CoCo3_enable_Music_Speech, FEATURE_7) // Enable Music and Speech cart diff --git a/rtl/music_speech.sv b/rtl/music_speech.sv new file mode 100644 index 0000000..876ca73 --- /dev/null +++ b/rtl/music_speech.sv @@ -0,0 +1,203 @@ +//////////////////////////////////////////////////////////////////////////////// +// Project Name: CoCo3FPGA Version 5.x.x +// File Name: Music_Speech.sv +// +// CoCo3 in an FPGA +// +//////////////////////////////////////////////////////////////////////////////// +// +//////////////////////////////////////////////////////////////////////////////// +// +// Color Computer 3 compatible system on a chip +// +// +// Copyright (c) 2008 Gary Becker (gary_l_becker@yahoo.com) +// +// All rights reserved +// +// Redistribution and use in source and synthezised forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// Redistributions in synthesized form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// Neither the name of the author nor the names of other contributors may +// be used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Please report bugs to the author, but before you do so, please +// make sure that this is not a derivative work and that +// you have the latest version of this file. +// +// +//////////////////////////////////////////////////////////////////////////////// +// Stan Hodge +// +// Music_Speech.sv by Stan Hodge 02/16/24 +//////////////////////////////////////////////////////////////////////////////// + +module Music_Speech( +input RESET_N, +input CLK, +input CLK_EN, +input CLK_1_78, + +input [1:0] MPI_SCS, // slot id +input [15:0] ADDRESS, // from 09 +input [7:0] WRITE_DATA, // from 09 + +output [7:0] READ_DATA, // to 09 +input RW_N, // from 09 +output [7:0] AUDIO // 8 bit audio output mixed from both Speech and Music chips +); + +/////////////////////////////////////////////////////// +// Host Interface + +localparam Soft_Reset_Adrs = 15'hff7d; +localparam Data_Write_Adrs = 15'hff7e; + +localparam SLOT_1 = 2'b00; // Nothing [RS-232) +localparam SLOT_2 = 2'b01; // SDC +localparam SLOT_3 = 2'b10; // Cartridge System +localparam SLOT_4 = 2'b11; // FDC + +localparam S_M_SLOT = SLOT_2; + +reg Soft_Reset; +wire System_Reset; + +// Soft Reset Bit +always @ (negedge CLK or negedge RESET_N) +begin + if(!RESET_N) + begin + Soft_Reset <= 1'b0; + end + else + begin + if (CLK_EN && !RW_N && (MPI_SCS == S_M_SLOT) && (ADDRESS == Soft_Reset_Adrs)) + begin + if (WRITE_DATA[0]) + Soft_Reset <= !Soft_Reset; + else + Soft_Reset <= 1'b0; + end + end +end + +assign System_Reset = RESET_N ^ Soft_Reset; + +// New data and INT +reg INT3_N; +reg [7:0] SOC_WR_DATA; + +always @ (negedge CLK or negedge Port_C[7]) +begin + if(!Port_C[7]) + begin + INT3_N <= 1'b1; + end + else + begin + if (CLK_EN && !RW_N && (MPI_SCS == S_M_SLOT) && (ADDRESS == Data_Write_Adrs)) + begin + SOC_WR_DATA <= WRITE_DATA; + INT3_N <= 1'b0; + end + end +end + + +////////////////////////////////////////////////////////////////////////////// +// Read Data + +wire Speech_Status; +wire Music_Status; + +assign READ_DATA = {INT3_N, Speech_Status, Music_Status, 5'b00000}; + + +///////////////////////////////////////////////////////////////////////////// +// SOC + +wire INT1_N; +wire [7:0] Port_A, Port_B, Port_C, Port_D_IN, Port_D_OUT; + +Music_Speech_SOC CoCo3_Music_Speech_SOC( + .RESET_N(RESET_N), + .CLKIN(CLK), + .CLK_1_78(CLK_1_78), + + .INT1_N(INT1_N), + .INT3_N(INT3_N), + .PORT_A(Port_A), + .PORT_B(Port_B), + .PORT_C(Port_C), + .PORT_D_OUT(Port_D_OUT), + .PORT_D_IN(Port_D_IN) +); + +///////////////////////////////////////////////////////////////////////////// +// SRAM Chip + +COCO_SRAM_6116 CoCo3_SRAM_6116( + .CLK(CLK), + .ADDR({Port_C[2:0], Port_B}), // Check + .R_N(Port_C[4]), // Check + .DATA_O(Port_D_IN), + .DATA_I(Port_D_OUT) +); + +///////////////////////////////////////////////////////////////////////////// +// Speech Chip + +sp0256 CoCo3_sp0256( + .clock(), + .ce(), + .reset(), + .input_rdy(), + .allophone(), + .trig_allophone(), + .audio_out() +); + + +///////////////////////////////////////////////////////////////////////////// +// Music Chip + +ym2149_audio CoCo3_ym2149_audio( + .clk_i(CLK), + .en_clk_psg_i(CLK_1_78), + .sel_n_i(1'b1), // No divide by 2 + .reset_n_i(RESET_N), + .bc_i(), + .bdir_i(), + .data_i(), + .data_r_o(), + +// .ch_a_o(), +// .ch_b_o(), +// .ch_c_o() + + .mix_audio_o(), //[13:0] +// .pcm14s_o() +); + +endmodule diff --git a/rtl/sram/CoCo_ram_6116.vhd b/rtl/sram/CoCo_ram_6116.vhd new file mode 100644 index 0000000..4eeb97d --- /dev/null +++ b/rtl/sram/CoCo_ram_6116.vhd @@ -0,0 +1,102 @@ +--////////////////////////////////////////////////////////////////////////////// +-- Project Name: CoCo3FPGA Version 4.0 +-- File Name: CoCo_ram_6116.vhd +-- +-- CoCo3 in an FPGA +-- +-- Revision: 4.0 07/10/16 +--////////////////////////////////////////////////////////////////////////////// +-- +-- Color Computer 3 compatible system on a chip +-- +-- Version : 5.0.x.x +-- +-- Copyright (c) 2008 Gary Becker (gary_l_becker@yahoo.com) +-- +-- All rights reserved +-- +-- Redistribution and use in source and synthezised forms, with or without +-- modification, are permitted provided that the following conditions are met: +-- +-- Redistributions of source code must retain the above copyright notice, +-- this list of conditions and the following disclaimer. +-- +-- Redistributions in synthesized form must reproduce the above copyright +-- notice, this list of conditions and the following disclaimer in the +-- documentation and/or other materials provided with the distribution. +-- +-- Neither the name of the author nor the names of other contributors may +-- be used to endorse or promote products derived from this software without +-- specific prior written permission. +-- +-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE +-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +-- POSSIBILITY OF SUCH DAMAGE. +-- +-- Please report bugs to the author, but before you do so, please +-- make sure that this is not a derivative work and that +-- you have the latest version of this file. +-- +-- The latest version of this file can be found at: +-- http://groups.yahoo.com/group/CoCo3FPGA +-- +-- File history : +-- +-- 1.0 Full Release +-- 2.0 Partial Release +-- 3.0 Full Release +-- 3.0.0.1 Update to fix DoD interrupt issue +--////////////////////////////////////////////////////////////////////////////// +-- Gary Becker +-- gary_L_becker@yahoo.com +--////////////////////////////////////////////////////////////////////////////// +--////////////////////////////////////////////////////////////////////////////// +--//////////////////////////////////////////////////////////////////////////////// +--// MISTer Conversion by Stan Hodge and Alan Steremberg (& Gary Becker) +--// stan.pda@gmail.com +--// +--//////////////////////////////////////////////////////////////////////////////// + + +library ieee; + use ieee.std_logic_1164.all; + use ieee.std_logic_unsigned.all; + use ieee.numeric_std.all; + +entity COCO_SRAM_6116 is + port ( + CLK : in std_logic; + ADDR : in std_logic_vector(10 downto 0); + R_W : in std_logic; + DATA_I : in std_logic_vector(7 downto 0); + DATA_O : out std_logic_vector(7 downto 0) + ); +end; + +architecture RTL of COCO_SRAM_6116 is + + type SRAM_ARRAY is array(0 to 2047) of std_logic_vector(7 downto 0); + signal SRAM : SRAM_ARRAY; + +begin + + P_SRAM : process(CLK,ADDR,R_W,DATA_I) + begin + if CLK'event and CLK='1' then + if R_W = '0' then + SRAM(to_integer(unsigned(ADDR))) <= DATA_I; + end if; + end if; + end process; + + DATA_O <= SRAM(to_integer(unsigned(ADDR))); + +end RTL; \ No newline at end of file