⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 top.vhd

📁 音乐编辑与播放设计
💻 VHD
字号:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

--  Uncomment the following lines to use the declarations that are
--  provided for instantiating Xilinx primitive components.
--library UNISIM;
--use UNISIM.VComponents.all;

entity top is
    Port (sysclk:in std_logic;
	       tonekey:in std_logic_vector(6 downto 0);
			  musickey:in std_logic;
			 spkout0:out std_logic;
	       spkout1:out std_logic);
end top;

architecture Behavioral of top is
signal addr:std_logic_vector(23 downto 0);
signal divclk_counter:integer range 0 to 24999999;
signal musicclk:std_logic;
signal musickeyshiftbuf:std_logic_vector(31 downto 0);
signal musicno:std_logic_vector(1 downto 0);


signal step:std_logic_vector(9 downto 0);
signal tonecode:std_logic_vector(7 downto 0);
signal tonestep:std_logic_vector(9 downto 0);
signal codeaddr1:integer range 0 to 256; 
signal codeaddr2:integer range 0 to 447; 
signal codeaddr3:integer range 0 to 495; 



component musicdec is
Port ( tonecode : in std_logic_vector(7 downto 0);
       tonestep : out std_logic_vector(9 downto 0));
end component;

type state is (idle,playtone,playmusic);
signal prestate:state;


type datatype1 is array (0 to 255) of std_logic_vector(7 downto 0);
--music:two tigers
constant music1 : datatype1 :=(x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"00",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",x"0b",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"00",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0d",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0b",x"0b",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0d",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0b",x"0b",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"00",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"00",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"00");

type datatype2 is array (0 to 447) of std_logic_vector(7 downto 0);
--music:mother's kiss
constant music2 : datatype2 :=(
						x"08",x"08",x"08",x"08",x"08",x"08",x"09",x"09",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"09",x"09",
						x"08",x"08",x"08",x"08",x"08",x"08",x"06",x"06",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",
						x"08",x"08",x"08",x"08",x"00",x"00",x"09",x"09",x"0a",x"0a",x"0a",x"0a",x"09",x"09",x"08",x"08",
						x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"08",x"08",x"08",x"08",x"00",x"00",x"09",x"09",x"0a",x"0a",x"0a",x"00",x"0a",x"00",x"09",x"09",
						x"09",x"09",x"09",x"09",x"09",x"09",x"08",x"08",x"06",x"06",x"06",x"06",x"00",x"00",x"06",x"06",
						x"09",x"09",x"09",x"09",x"00",x"00",x"08",x"08",x"08",x"08",x"08",x"08",x"06",x"06",x"06",x"06",
						x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"00",
						x"05",x"05",x"05",x"05",x"06",x"00",x"06",x"00",x"08",x"08",x"08",x"00",x"08",x"08",x"08",x"00",
						x"09",x"09",x"09",x"00",x"09",x"09",x"08",x"08",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"09",x"09",x"09",x"09",x"00",x"00",x"0a",x"0a",x"09",x"09",x"09",x"09",x"08",x"08",x"08",x"08",
						x"08",x"08",x"09",x"08",x"06",x"06",x"05",x"05",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"05",x"05",x"05",x"05",x"00",x"00",x"06",x"06",x"08",x"08",x"08",x"08",x"09",x"09",x"0a",x"0a",
						x"0c",x"0c",x"0c",x"0c",x"09",x"09",x"0a",x"0a",x"08",x"08",x"09",x"08",x"06",x"06",x"06",x"06",
						x"06",x"06",x"06",x"06",x"09",x"09",x"09",x"09",x"07",x"07",x"07",x"07",x"06",x"06",x"05",x"05",
						x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",
						x"0c",x"00",x"0c",x"0c",x"0c",x"00",x"0c",x"00",x"0c",x"0c",x"0c",x"0c",x"0a",x"0a",x"0c",x"0c",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0c",x"0c",x"0a",x"0a",x"0a",x"0a",
						x"0d",x"00",x"0d",x"00",x"0d",x"00",x"0d",x"00",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0a",x"0a",
						x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"0d",x"0d",x"0d",x"0d",x"0c",x"0c",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0a",x"0a",x"0a",x"0a",
						x"09",x"09",x"0a",x"0a",x"09",x"09",x"08",x"08",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"09",x"00",x"09",x"00",x"09",x"00",x"09",x"00",x"08",x"08",x"08",x"08",x"06",x"06",x"06",x"06",
						x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",
						x"0d",x"0d",x"0d",x"0d",x"0c",x"0c",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0a",x"0a",x"0a",x"0a",
						x"09",x"09",x"0a",x"0a",x"09",x"09",x"08",x"08",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"09",x"00",x"09",x"00",x"09",x"00",x"09",x"00",x"08",x"08",x"08",x"08",x"06",x"06",x"06",x"06",
						x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05",x"05");


type datatype3 is array (0 to 495) of std_logic_vector(7 downto 0);
--music3:let's boat...
constant music3 : datatype3 :=(
						x"00",x"00",x"00",x"00",x"06",x"06",x"06",x"06",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"00",x"00",x"00",x"00",x"0c",x"0c",x"0c",x"0c",
						x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"00",x"00",x"00",x"00",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"0a",x"0a",x"0a",x"0a",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"00",x"00",x"00",x"00",x"0c",x"0c",x"0c",x"0c",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"00",x"0a",x"0a",x"0a",x"0a",x"0c",x"0c",x"0c",x"0c",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"00",x"00",x"00",x"00",x"0d",x"0d",x"0d",x"0d",
						x"0f",x"0f",x"0f",x"0f",x"0e",x"0e",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0d",x"0d",x"0d",x"0d",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"00",x"00",x"00",x"00",x"0c",x"0c",x"0c",x"0c",
						x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"08",x"06",x"06",x"06",x"06",x"06",x"06",x"06",x"06",
						x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"0a",x"0a",x"0a",x"0a",x"0d",x"0d",x"0d",x"0d",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",
						x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"0c",x"00",x"00",x"00",x"00",x"00",x"00",x"00",x"00",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"00",x"00",x"00",x"00",x"0d",x"0d",x"0d",x"0d",
						x"0c",x"0c",x"0c",x"0c",x"0b",x"0b",x"0b",x"0b",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",
						x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"00",x"00",x"00",x"00",x"0c",x"0c",x"0c",x"0c",
						x"06",x"06",x"06",x"06",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",x"09",x"09",x"09",x"09",
						x"00",x"00",x"00",x"00",x"00",x"00",x"00",x"00",x"08",x"08",x"08",x"08",x"09",x"09",x"09",x"09",
						x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0a",x"0c",x"0c",x"0c",x"0c",x"00",x"00",x"0c",x"0c",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0f",x"0f",x"0f",x"0f",x"0f",x"0f",x"0f",x"0f",
						x"0e",x"0e",x"0e",x"0e",x"0d",x"0d",x"0d",x"0d",x"0c",x"0c",x"0c",x"0c",x"0a",x"0a",x"0a",x"0a",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",
						x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",x"0d",
						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");


begin

U0:musicdec	port map(tonecode=>tonecode,tonestep=>tonestep);


spkout1<=addr(23);
spkout0<=addr(23);
step<=tonestep;

process(sysclk)
begin
  if sysclk'event and sysclk='1' then
    addr<=addr+step;
	 if divclk_counter=1799999 then
	   musicclk<=not musicclk;
		divclk_counter<=0;
	 else
	   divclk_counter<=divclk_counter+1;
	 end if;

    musickeyshiftbuf<=musickey&musickeyshiftbuf(31 downto 1);
    if musickeyshiftbuf=x"00000001" then
      musicno<=musicno+1;
    end if;

  end if;
end process;

state_machine: process(musicclk)
begin
  if musicclk'event and musicclk='1' then
  case prestate is
    when idle=>
	 		if musickey='0' then 
				tonecode<=x"00";
			   prestate<=playmusic;			   
			 elsif tonekey/="1111111"  then
				tonecode<=x"00";
            prestate<=playtone;
			 else
			   tonecode<=x"00";
				codeaddr1<=0;
			   codeaddr2<=0;
				codeaddr3<=0;
			   prestate<=idle;
          end if;
					 
    when playtone=>
	             if tonekey/="1111111" then
					   case tonekey is 
						  when "1111110"=> tonecode<=x"08";
						  when "1111101"=> tonecode<=x"09";
						  when "1111011"=> tonecode<=x"0a";
						  when "1110111"=> tonecode<=x"0b";
						  when "1101111"=> tonecode<=x"0c";
						  when "1011111"=> tonecode<=x"0d";
						  when "0111111"=> tonecode<=x"0e";
						  when others=> tonecode<=x"00";
						end case;
						prestate<=playtone;
                else
					   tonecode<=x"00";
						prestate<=idle; 
					 end if;
    when playmusic=>
	        if tonekey/="1111111" then
				prestate<=playtone;
			   else					     
				case musicno is
				  when "00" =>	tonecode<=music1(codeaddr1);
							if codeaddr1=255 then
						  	  codeaddr1<=0;
							else
						  	  codeaddr1<=codeaddr1+1;
							end if;
							codeaddr2<=0;
							codeaddr3<=0;
				  when "01" => tonecode<=music2(codeaddr2);
							if codeaddr2=447 then
						  	  codeaddr2<=0;
							else
						  	  codeaddr2<=codeaddr2+1;
							end if;
							codeaddr1<=0;
							codeaddr3<=0;
						   
				  when "10" => tonecode<=music3(codeaddr3);
							if codeaddr3=495 then
						  	  codeaddr3<=0;
							else
						  	  codeaddr3<=codeaddr3+1;
							end if;
							codeaddr2<=0;
							codeaddr1<=0;
              when "11"=> prestate<=idle;
				         tonecode<=x"00";
				         codeaddr1<=0;
							codeaddr2<=0;
							codeaddr3<=0;
				  when others=>null;
				end case;

    		      if tonecode/=x"ff" then  
				    prestate<=playmusic;
				  else
				    prestate<=idle;
				  end if;
			   end if;
	when others=> null;
   end case;
  end if;                  



end process;





end Behavioral;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -