Advertisement
Guest User

Untitled

a guest
Sep 28th, 2011
1,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 11.05 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_ARITH.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  5.  
  6. ---- Uncomment the following library declaration if instantiating
  7. ---- any Xilinx primitives in this code.
  8. --library UNISIM;
  9. --use UNISIM.VComponents.all;
  10.  
  11. entity vga_text is
  12.     Port ( clk : in  STD_LOGIC;
  13.            iowr : in  STD_LOGIC;
  14.            addr : in  STD_LOGIC_VECTOR (31 downto 0);
  15.            data : in  STD_LOGIC_VECTOR (31 downto 0);
  16.            r : out  STD_LOGIC;
  17.            g : out  STD_LOGIC;
  18.            b : out  STD_LOGIC;
  19.            vga_blank : out  STD_LOGIC;
  20.            vsync : out  STD_LOGIC;
  21.            hsync : out  STD_LOGIC;
  22.         dout: out std_logic_vector(31 downto 0));
  23. end vga_text;
  24.  
  25. architecture Behavioral of vga_text is
  26.  
  27. -- Symbols format
  28. --   7   6   5   4   3   2   1   0
  29. --  15  14  13  12  11  10   9   8
  30. --  23  22  21  20  19  18  17  16
  31. --  31  30  29  28  27  26  25  24
  32. --  39  38  37  36  35  34  33  32
  33. -- ...
  34. -- 127 126 125 124 123 122 121 120
  35. signal SAddrA: std_logic_vector(7 downto 0);
  36. signal SAddrRead, SAddrWrite: integer range 0 to 255;
  37. signal SDataA, SDinA: std_logic_vector(127 downto 0);
  38. signal SWeA: std_logic;
  39.  
  40. type TScreen is array (0 to 1999) of std_logic_vector(17 downto 0);
  41. signal Screen: TScreen;
  42. signal scrAddrA, scrAddrB: integer range 0 to 1999;
  43. signal scrDataA, scrDataB, scrDinB: std_logic_vector(17 downto 0);
  44. signal scrWeB: std_logic;
  45.  
  46. type TColor is array (0 to 31) of std_logic_vector(23 downto 0);
  47. signal PalleteColor: TColor :=
  48. (
  49.  0 => x"000000",
  50.  1 => x"0000FF",
  51.  2 => x"00FF00",
  52.  3 => x"00FFFF",
  53.  4 => x"FF0000",
  54.  5 => x"FF00FF",
  55.  6 => x"FFFF00",
  56.  7 => x"FFFFFF",
  57.  others => x"FFFFFF"
  58. );
  59. signal PalleteAddr: integer range 0 to 31;
  60.  
  61. signal VCounter : integer range 0 to 520 := 0;
  62. signal HCounter : integer range 0 to 800 := 0;
  63. signal div : std_logic := '0';
  64.  
  65. signal scrX, scrX_new: std_logic_vector(9 downto 0);
  66. signal scrY, scrY_new: std_logic_vector(9 downto 0);
  67. signal textX: integer range 0 to 79;
  68. signal textX2: integer range 0 to 79;
  69. signal textY: integer range 0 to 24;
  70. signal SymbolPoint: integer range 0 to 127;
  71.  
  72. signal curColor: std_logic_vector(23 downto 0);
  73.  
  74. signal r_in, g_in, b_in: std_logic_vector(7 downto 0);
  75.  
  76. signal GotoX: integer range 0 to 79;
  77. signal GotoY: integer range 0 to 24;
  78. signal Color, BgColor: std_logic_vector(4 downto 0);
  79. signal isLowLevel: std_logic;
  80.  
  81. signal scrAddrBLowLevel, scrAddrBNormal: integer range 0 to 1999;
  82. signal scrDinBLowLevel,  scrDinBNormal:  std_logic_vector(17 downto 0);
  83. signal scrWebLowLevel,   scrWebNormal:   std_logic;
  84.  
  85. signal scrData: std_logic_vector(17 downto 0);
  86.  
  87. signal SymbolX: std_logic_vector(2 downto 0);
  88. signal SymbolY: std_logic_vector(3 downto 0);
  89.  
  90. signal selector: std_logic_vector(31 downto 0);
  91. signal isCaretPos: std_logic;
  92. signal isLowLinePosB, isLowLinePos: std_logic;
  93. signal isVertLinePosB, isVertLinePos: std_logic;
  94.  
  95. -- cursor settings
  96. signal isLowLine: std_logic := '1';
  97. signal isBlackSquare: std_logic := '0';
  98. signal isBlinking: std_logic := '1';
  99. signal isVertLine: std_logic := '0';
  100.  
  101. signal timeCount: std_logic_vector(31 downto 0) := (others => '0');
  102. signal blinkTotal: std_logic_vector(31 downto 0) := conv_std_logic_vector(50000000, 32);
  103. signal blinkOff: std_logic_vector(31 downto 0) := conv_std_logic_vector(25000000, 32);
  104. signal caretColor: std_logic_vector(23 downto 0) := x"00FF00";
  105.  
  106. signal NewSymbol: std_logic_vector(127 downto 0);
  107.  
  108. component vga640_symbols
  109.   port (
  110.   addr: IN std_logic_VECTOR(7 downto 0);
  111.   clk: IN std_logic;
  112.   din: IN std_logic_VECTOR(127 downto 0);
  113.   dout: OUT std_logic_VECTOR(127 downto 0);
  114.   we: IN std_logic);
  115. end component;
  116.  
  117. begin
  118.  
  119. process(clk)
  120. begin
  121.   if rising_edge(clk) then
  122.     if timeCount > blinkTotal then
  123.       timeCount <= (others => '0');
  124.     else
  125.       timeCount <= timeCount + 1;
  126.     end if;
  127.   end if;
  128. end process;
  129.  
  130. vga640_symbols_0: vga640_symbols
  131. Port map(
  132.  addr => SAddrA,
  133.  clk => clk,
  134.  din => NewSymbol,
  135.  dout => SDataA,
  136.  we => SWeA
  137. );
  138. SWeA <= '1' when conv_integer(addr) = 337 and iowr = '1' else '0';
  139. SAddrA <= conv_std_logic_vector(SAddrWrite, 8) when SWeA = '1' else conv_std_logic_vector(SAddrRead, 8);
  140.  
  141. process(clk)
  142. begin
  143.   if rising_edge(clk) then
  144.     if scrWeB = '1' then
  145.       Screen(scrAddrB) <= scrDinB;
  146.     end if;
  147.     scrDataA <= Screen(scrAddrA);
  148.     scrDataB <= Screen(scrAddrB);
  149.   end if;
  150. end process;
  151. scrDinB  <= scrDinBLowLevel  when isLowLevel = '1' else scrDinBNormal;
  152. scrWeb   <= scrWeBLowLevel   when isLowLevel = '1' else scrWebNormal;
  153. scrAddrB <= scrAddrBLowLevel when isLowLevel = '1' else scrAddrBNormal;
  154.  
  155. scrDinBLowLevel <= data(17 downto 0);
  156. scrWeBLowLevel  <= '1' when (conv_integer(addr) = 300 or conv_integer(addr) = 309) and iowr = '1' else '0';
  157.  
  158. scrAddrBNormal <= GotoY * 80 + GotoX;
  159. scrDiNBNormal  <= BgColor & Color & data(7 downto 0);
  160. scrWebNormal   <= '1' when conv_integer(addr) = 306 and iowr = '1' else '0';
  161.  
  162. process(clk)
  163. begin
  164.   if rising_edge(clk) then
  165.     div <= not(div);
  166.     if div = '1' then
  167.       if HCounter = 799 then
  168.         HCounter <= 0;
  169.         if VCounter = 520 then
  170.           VCounter <= 0;
  171.         else
  172.           VCounter <= VCounter + 1;
  173.         end if;
  174.       else
  175.         HCounter <= HCounter + 1;
  176.       end if;
  177.     end if;
  178.   end if;
  179. end process;
  180.  
  181. process(clk)
  182. begin
  183.   if rising_edge(clk) then
  184.    if div = '1' then
  185.     -- 1
  186.     scrX <= conv_std_logic_vector(HCounter - 139, 10);
  187.     scrY <= conv_std_logic_vector(VCounter - 70 , 10);
  188.    
  189.     -- 2
  190.     textX <= conv_integer(scrX(9 downto 3));
  191.     textY <= conv_integer(scrY(9 downto 4));
  192.     SymbolX <= scrX(2 downto 0) - 1;
  193.     SymbolY <= scrY(3 downto 0);
  194.    
  195.     -- 3
  196.     if SymbolY > 13 then
  197.       isLowLinePosB <= '1';
  198.     else
  199.       isLowLinePosB <= '0';
  200.     end if;
  201.     if SymbolX < 2 then
  202.       isVertLinePosB <= '1';
  203.     else
  204.       isVertLinePosB <= '0';
  205.     end if;
  206.     scrAddrA <= textY * 80 + textX;
  207.     SymbolPoint <= conv_integer(SymbolY) * 8 + 7 - conv_integer(SymbolX);
  208.    
  209.     -- 4        
  210.     SAddrRead <= conv_integer(scrDataA(7 downto 0));
  211.     scrData <= scrDataA;
  212.     isLowLinePos <= isLowLinePosB;
  213.     isVertLinePos <= isVertLinePosB;
  214.     if scrAddrA = GotoY * 80 + GotoX then
  215.       isCaretPos <= '1';
  216.     else
  217.       isCaretPos <= '0';
  218.     end if;
  219.    
  220.     -- 5
  221.     if (VCounter > 69) and (VCounter < 470) then
  222.       if isCaretPos = '1' then        
  223.         if (isBlackSquare = '1' and isBlinking = '0') or
  224.           (isBlackSquare = '1' and isBlinking = '1' and timeCount < blinkOff) or
  225.           (isVertLine = '1' and isVertLinePos = '1' and isBlinking = '1' and timeCount < blinkOff) or
  226.           (isLowLine = '1' and isLowLinePos = '1' and isBlinking = '1' and timeCount < blinkOff) then
  227.           curColor <= caretColor;
  228.         else
  229.           if SDataA(SymbolPoint) = '1' then
  230.             curColor <= PalleteColor(conv_integer(scrData(12 downto 8)));
  231.           else
  232.             curColor <= PalleteColor(conv_integer(scrData(17 downto 13)));
  233.           end if;
  234.         end if;          
  235.       else
  236.         if SDataA(SymbolPoint) = '1' then
  237.           curColor <= PalleteColor(conv_integer(scrData(12 downto 8)));
  238.         else
  239.           curColor <= PalleteColor(conv_integer(scrData(17 downto 13)));
  240.         end if;
  241.       end if;
  242.     else
  243.       curColor <= x"000000";
  244.     end if;
  245.    end if;
  246.   end if;
  247. end process;
  248.  
  249. process(clk)
  250. begin
  251.   if rising_edge(clk) then
  252.     if conv_integer(addr) = 301 and iowr = '1' then scrAddrBLowLevel <= conv_integer(data(10 downto 0)); end if;
  253.     if conv_integer(addr) = 302 and iowr = '1' then
  254.       if conv_integer(data) > 79 then
  255.         GotoX <= 0;
  256.       else
  257.         GotoX <= conv_integer(data);
  258.       end if;
  259.     end if;
  260.     if conv_integer(addr) = 303 and iowr = '1' then
  261.       if conv_integer(data) > 24 then
  262.         GotoY <= 0;
  263.       else
  264.         GotoY <= conv_integer(data);
  265.       end if;
  266.     end if;
  267.     if conv_integer(addr) = 304 and iowr = '1' then Color <= data(4 downto 0); end if;
  268.     if conv_integer(addr) = 305 and iowr = '1' then BgColor <= data(4 downto 0); end if;
  269.     if conv_integer(addr) = 306 and iowr = '1' then
  270.       if GotoX = 79 then
  271.         if GotoY = 24 then
  272.           GotoY <= 0;
  273.         else
  274.           GotoY <= GotoY + 1;
  275.         end if;
  276.         GotoX <= 0;
  277.       else
  278.         GotoX <= GotoX + 1;
  279.       end if;
  280.     end if;
  281.     if conv_integer(addr) = 307 and iowr = '1' then isLowLevel <= data(0); end if;
  282.     if conv_integer(addr) = 308 and iowr = '1' then
  283.       GotoX <= 0;
  284.       if GotoY = 24 then
  285.         GotoY <= 0;
  286.       else
  287.         GotoY <= GotoY + 1;
  288.       end if;
  289.     end if;
  290.     if conv_integer(addr) = 309 and iowr = '1' then
  291.       if scrAddrBLowLevel = 1999 then
  292.         scrAddrBLowLevel <= 0;
  293.       else
  294.         scrAddrBLowLevel <= scrAddrBLowLevel + 1;
  295.       end if;
  296.     end if;
  297.     if conv_integer(addr) = 310 and iowr = '1' then
  298.       isLowLine <= data(0);
  299.       isBlackSquare <= data(1);
  300.       isBlinking <= data(2);      
  301.       isVertLine <= data(3);
  302.     end if;
  303.     if conv_integer(addr) = 311 and iowr = '1' then
  304.       isLowLine <= data(0);
  305.     end if;
  306.     if conv_integer(addr) = 312 and iowr = '1' then
  307.       isBlackSquare <= data(0);
  308.     end if;
  309.     if conv_integer(addr) = 313 and iowr = '1' then
  310.       isBlinking <= data(0);
  311.     end if;
  312.     if conv_integer(addr) = 314 and iowr = '1' then
  313.       isVertLine <= data(0);
  314.     end if;
  315.     if conv_integer(addr) = 320 and iowr = '1' then
  316.       caretColor <= data(23 downto 0);
  317.     end if;
  318.     if conv_integer(addr) = 321 and iowr = '1' then
  319.       blinkTotal <= data;
  320.     end if;
  321.     if conv_integer(addr) = 322 and iowr = '1' then
  322.       blinkOff <= data;
  323.     end if;
  324.    
  325.     if conv_integer(addr) = 330 and iowr = '1' then
  326.       PalleteAddr <= conv_integer(data(4 downto 0));      
  327.     end if;
  328.     if conv_integer(addr) = 331 and iowr = '1' then
  329.       PalleteColor(PalleteAddr) <= data(23 downto 0);
  330.     end if;
  331.    
  332.     if conv_integer(addr) = 335 and iowr = '1' then
  333.       SAddrWrite <= conv_integer(data(7 downto 0));
  334.     end if;    
  335.     -- 337 - for write symbol
  336.     for i in 0 to 3 loop
  337.       if conv_integer(addr) = 340 + i and iowr = '1' then
  338.         NewSymbol((i + 1) * 32 - 1 downto i * 32) <= data(31 downto 0);
  339.       end if;
  340.     end loop;
  341.   end if;
  342. end process;
  343.  
  344. b_in <= curColor(23 downto 16) when ((HCounter > 143) and (HCounter < 784) and (VCounter > 30) and (VCounter < 511)) else "00000000";
  345. g_in <= curColor(15 downto  8) when ((HCounter > 143) and (HCounter < 784) and (VCounter > 30) and (VCounter < 511)) else "00000000";
  346. r_in <= curColor( 7 downto  0) when ((HCounter > 143) and (HCounter < 784) and (VCounter > 30) and (VCounter < 511)) else "00000000";
  347.  
  348. b <= b_in(7);
  349. g <= g_in(7);
  350. r <= r_in(7);
  351.  
  352. vsync <= '0' when VCounter <  2 else '1';
  353. hsync <= '0' when HCounter < 96 else '1';
  354.  
  355. vga_blank <= '1' when ((HCounter > 143) and (HCounter < 784) and (VCounter > 30) and (VCounter < 511)) else '0';
  356.  
  357. with conv_integer(addr) select
  358.   dout <= "00000000000000" & scrDataB when 300,
  359.           (others => 'Z') when others;
  360.  
  361. end Behavioral;
  362.  
  363.  
  364.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement