kamilosxd678

ISP Lab 5B

Jun 2nd, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 6.61 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. -- Company:
  3. -- Engineer:
  4. --
  5. -- Create Date:    08:36:32 05/28/2013
  6. -- Design Name:
  7. -- Module Name:    main - Behavioral
  8. -- Project Name:
  9. -- Target Devices:
  10. -- Tool versions:
  11. -- Description:
  12. --
  13. -- Dependencies:
  14. --
  15. -- Revision:
  16. -- Revision 0.01 - File Created
  17. -- Additional Comments:
  18. --
  19. ----------------------------------------------------------------------------------
  20. library IEEE;
  21. use IEEE.STD_LOGIC_1164.ALL;
  22. use IEEE.STD_LOGIC_ARITH.ALL;
  23. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  24. use IEEE.NUMERIC_STD.ALL;
  25.  
  26. ---- Uncomment the following library declaration if instantiating
  27. ---- any Xilinx primitives in this code.
  28. --library UNISIM;
  29. --use UNISIM.VComponents.all;
  30.  
  31. entity main is
  32.     Port ( clk_i : in  STD_LOGIC;
  33.            rgb_o : out STD_LOGIC_VECTOR(2 downto 0);
  34.               hsync_o : out STD_LOGIC;
  35.               vsync_o : out STD_LOGIC;
  36.               sw_i : in STD_LOGIC_VECTOR(7 downto 0);
  37.               btn_i : in STD_LOGIC_VECTOR(3 downto 0));
  38. end main;
  39.  
  40. architecture Structural of main is
  41.  
  42. component memory
  43.     port (
  44.     clka: IN std_logic;
  45.     dina: IN std_logic_VECTOR(0 downto 0);
  46.     addra: IN std_logic_VECTOR(17 downto 0);
  47.     wea: IN std_logic_VECTOR(0 downto 0);
  48.     clkb: IN std_logic;
  49.     addrb: IN std_logic_VECTOR(17 downto 0);
  50.     doutb: OUT std_logic_VECTOR(0 downto 0));
  51. end component;
  52.  
  53. component sinus IS
  54.    PORT (
  55.       DATA : IN STD_LOGIC_VECTOR(15 DOWNTO 0);
  56.       WE : IN STD_LOGIC;
  57.       A : IN STD_LOGIC_VECTOR(4 DOWNTO 0);
  58.       CLK : IN STD_LOGIC;
  59.       SCLR : IN STD_LOGIC;
  60.       CHANNEL : OUT STD_LOGIC_VECTOR(0 DOWNTO 0);
  61.       RDY : OUT STD_LOGIC;
  62.       SINE : OUT STD_LOGIC_VECTOR(10 DOWNTO 0)
  63. );
  64. END component;
  65.  
  66. component VGAsync is
  67.      Port ( clk_i : in STD_LOGIC;
  68.                 hsync_o : out STD_LOGIC;
  69.                 vsync_o : out STD_LOGIC;
  70.                 pixelX : out integer range 640 downto 0;
  71.                 pixelY : out integer range 480 downto 0);
  72. end component;
  73.  
  74. signal lowClk : std_logic := '0';
  75.  
  76. signal addrW : std_logic_vector(17 downto 0);
  77. signal addrR : std_logic_vector(17 downto 0);
  78. signal x : std_logic;
  79. signal y : std_logic;
  80. signal memRead : std_logic_vector(0 downto 0);
  81. signal memWrite : std_logic_vector(0 downto 0);
  82. signal writeEnable : std_logic_vector(0 downto 0);
  83. signal pixelX : integer range 640 downto 0;
  84. signal pixelY : integer range 480 downto 0;
  85. signal pixAddr : integer range 147456 downto 0;
  86.  
  87. signal singenData : std_logic_vector(15 downto 0);
  88. signal addrSel : std_logic_vector(4 downto 0);
  89. signal wrEnable : std_logic := '0';
  90. signal resetGen : std_logic := '0';
  91.  
  92. signal channel : std_logic_vector(0 downto 0);
  93. signal genRdy : std_logic;
  94. signal sinOut : std_logic_vector(10 downto 0);
  95.  
  96. constant PIXEL : std_logic_vector(2 downto 0) := "111";
  97. constant BACKGROUND : std_logic_vector(2 downto 0) := "000";
  98.  
  99. signal Xoffset : integer range 640 downto 0 := 128;
  100. signal Yoffset : integer range 480 downto 0 := 48;
  101.  
  102. signal clearScr : std_logic := '1';
  103.  
  104. signal rstBtn : std_logic := '0'; --nowa zmienna
  105.  
  106. begin
  107.  
  108. videoMem : memory port map(
  109.     clka => clk_i,
  110.     dina => memWrite,
  111.     addra => addrW,
  112.     wea => writeEnable,
  113.     clkb => clk_i,
  114.     addrb => addrR,
  115.     doutb => memRead );
  116.    
  117. generator : sinus PORT map(
  118.       DATA => singenData,   -- data input to the gen.
  119.       WE => wrEnable, -- write enable - read from DATA into registers PINC or POFF
  120.       A => addrSel, -- address select for DATA to write to when WE
  121.         -- 00000 to 01111 phase increment for ch. 0 through 15 (PINC)
  122.         -- 10000 to 11111 phase offset for ch. 0 through 15 (POFF)
  123.       CLK => clk_i, -- clock
  124.       SCLR => resetGen, -- synchronous clear
  125.       CHANNEL => channel, -- shows which channel is currently displayed
  126.       RDY => genRdy, -- indicates that the data is ready to read
  127.       SINE => sinOut -- output sinus data
  128. );
  129.  
  130. pushButtons : process(clk_i)
  131. begin
  132.     if rising_edge(clk_i) then -- ponizej buttony maja byc porownywane do 1 a nie do 0
  133.         if btn_i(0)='1' then
  134.             singenData(15 downto 0) <= "00000000"&sw_i;
  135.             addrSel <= "00000";
  136.             wrEnable <= '1';
  137.         elsif btn_i(1)='1' then
  138.             singenData(`5 downto 0) <= "00000000"&sw_i;
  139.             addrSel <= "00001";
  140.             wrEnable <= '1';
  141.         elsif btn_i(2)='1' then
  142.             singenData(15 downto 0) <= sw_i&"00000000";
  143.             addrSel <= "10000";
  144.             wrEnable <= '1';
  145.         else
  146.             wrEnable <= '0';
  147.         end if;
  148.     end if;
  149. end process;
  150.  
  151. writeRGB : process(pixelX, pixelY)
  152.     variable tmpXoffset : integer range 640 downto 0 := 0;
  153.     variable tmpYoffset : integer range 480 downto 0 := 0;
  154. begin
  155.     tmpXoffset := Xoffset + 12;
  156.     tmpYoffset := Yoffset + 7;
  157.  
  158.     if (pixelX >= tmpXoffset) and (pixelY >= tmpYoffset) and (pixelX < tmpXoffset + 384) then
  159.         pixAddr <= (pixelX-tmpXoffset) + ((pixelY-tmpYoffset)*384); -- a tutaj +2
  160.         addrR <= std_logic_vector(to_unsigned(pixAddr, 18));
  161.         if memRead(0)='1' then
  162.             rgb_o <= PIXEL;
  163.         else
  164.             rgb_o <= BACKGROUND;
  165.         end if;
  166.     else
  167.         rgb_o <= BACKGROUND;
  168.     end if;
  169. end process;
  170.  
  171. setMemory : process (clk_i)
  172.     variable Xmemory : integer range 386 downto 0 := 386;
  173.     variable Ymemory : integer range 386 downto 0 := 386;
  174.     variable wrtAddr : integer range 147456 downto 0 := 0;
  175. begin
  176.     if rising_edge(clk_i) then
  177.         if btn_i(3)='1' then --nowy if do wyeliminowania wyscigow na buttonie 3
  178.             rstBtn <= '1'
  179.         end if;
  180.         if rstBtn = '1' then
  181.             Xmemory := 0;
  182.             Ymemory := 0;
  183.             clearScr <= '1';
  184.             resetGen <= '1';
  185.             rstBtn <= '0';
  186.         elsif clearScr='1' then
  187.             if resetGen='1' then
  188.                 resetGen <= '0';
  189.             end if;
  190.             if lowClk='1' then
  191.                 writeEnable(0) <= '0';
  192.             elsif Xmemory<385 and Ymemory<385 then
  193.                 wrtAddr := (Xmemory) + ((Ymemory)*384);
  194.                 memWrite(0) <= '0';
  195.                 addrW <= std_logic_vector(to_unsigned(wrtAddr, 18));
  196.                 writeEnable(0) <= '1';
  197.                 Xmemory := Xmemory + 1;
  198.                 if Xmemory=385 then
  199.                     Ymemory := Ymemory + 1;
  200.                     Xmemory := 0;
  201.                 end if;
  202.             elsif Ymemory=385 then
  203.                 clearScr <= '0';
  204.             end if;
  205.         else
  206.             if writeEnable(0)='1' then
  207.                 writeEnable(0) <= '0';
  208.             end if;
  209.             if genRdy='1' then
  210.                 if channel(0)='0' then
  211.                     Xmemory := conv_integer(sinOut)*3/16 + 192;
  212.                 else
  213.                     Ymemory := conv_integer(sinOut)*3/16 + 192;
  214.                     wrtAddr := (Xmemory) + ((Ymemory)*384);
  215.                     memWrite(0) <= '1';
  216.                     addrW <= std_logic_vector(to_unsigned(wrtAddr, 18));
  217.                     writeEnable(0) <= '1';
  218.                 end if;
  219.             end if;
  220.         end if;
  221.     end if;
  222. end process;
  223.    
  224. monitor_sync : VGAsync port map(
  225.     clk_i => lowClk,
  226.     hsync_o => hsync_o,
  227.     vsync_o => vsync_o,
  228.     pixelX => pixelX,
  229.     pixelY => pixelY );
  230.    
  231. clockDivider : process(clk_i)
  232. begin
  233.     if rising_edge(clk_i) then
  234.         if lowClk='0' then
  235.             lowClk <= '1';
  236.         else
  237.             lowClk <= '0';
  238.         end if;
  239.     end if;
  240. end process;
  241.  
  242. end Structural;
Advertisement
Add Comment
Please, Sign In to add comment