---------------------------------------------------------------------
--ENSC 151 - VHDL Example for Assignment 3                                                                                      
--------------------------------------------------------------------
--Author(s): Bill De Vries, Arash Haidari-Khabbaz                                                                                      
--Date:          1999-02-26                                                                                               
--Version:   0.0                                                                                                        
--Revisions: 0.0 Module Created                                                                         
--------------------------------------------------------------------
--Description:                                                                                                          
--~~~~~~~~~~~~                                                                                                                  
--This program replaces the pat_test graphical design file that         
--connects the /IRQ pin on the HC12 to the four switches on the Altera
--board. It also connects the switches to four pins of PORTA. All        
--control functionality is also included. At thesame time it will send 
-- a frequency of about 2KHz to the headphones if any of the push buttons
-- are pressed.                                               
--------------------------------------------------------------------

LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;

ENTITY a3 IS
        PORT
        (
                clk : IN std_logic;  --100 KHz clock                
				pa  : IN STD_LOGIC_VECTOR(2 DOWNTO 0);
--Port A0, A1, A2 coming into FPGA
                lcde  :OUT  STD_LOGIC; --LCD enable signal
				lcdrw :OUT  STD_LOGIC; --LCD Read/Write
signal
				lcdrs :OUT  STD_LOGIC; --LCD RS control
line

                pb    :IN   STD_LOGIC_VECTOR(7 DOWNTO 0); --Port B coming
into FPGA 
                lcd   :OUT  STD_LOGIC_VECTOR(7 DOWNTO 0); --Data going to
LCDfrom FPGA
                
                buttons :IN  STD_LOGIC_VECTOR(3 DOWNTO 0);-- PUSH Buttons  
                led   :OUT   STD_LOGIC;   --LED turning on with push
buttons               
                paout :OUT   STD_LOGIC_VECTOR(3 DOWNTO 0); --The push
buttons going to port A
                                                           --individually
through FPGA
    
                peh    :IN   STD_LOGIC_VECTOR(5 DOWNTO 0); -- PORT E high
bits (7 to 2)
                gio    :OUT  STD_LOGIC_VECTOR(5 DOWNTO 0); --Connected to
PEH through FPGA
				LeftHp, RightHp :OUT  std_logic; --Left
and right head phones
                irq    :OUT  STD_LOGIC;
                xirq   :OUT  STD_LOGIC
        );
END a3;

ARCHITECTURE foo OF a3 IS

signal sysaclr : std_logic; --Asynchronous clear bit for the counter
signal sys : std_logic_vector(13 downto 0); --Used to store the output of
the counter

-- This counter is in the altera library. It has more functions than being
used.
--Check the MAXPLUS II help for further information
COMPONENT lpm_counter
   GENERIC (LPM_WIDTH: POSITIVE); --Counter width
   PORT (clock: IN STD_LOGIC; --Counter clock pin
		aclr: IN STD_LOGIC := '0';  --Counter Asynchronous clear
bit
		 q: OUT STD_LOGIC_VECTOR(LPM_WIDTH-1 DOWNTO 0)); --Counter
output
END COMPONENT;


BEGIN

sysaclr <= '0';
 
--An instance of the counter is now being defined
sysck: lpm_counter
	GENERIC MAP (LPM_WIDTH => 14) --Counter width is set to 14. Change
it as it suits.
	
    --Counter clock is connected to the FPGA 100KHz clock
	PORT MAP (clock => clk, aclr => sysaclr, q => sys);

--This process is responsible for sending a square wave signal with a
frequency  of
-- 100KHz/32 to the head phones if any push button is pressed
    
sound: process(buttons) begin
     
        --The following if statement checks for pressed push buttons
        if ((buttons = "0111") or (buttons = "1110") or (buttons = "1011")
or (buttons = "1101")) then
           --If a button is pressed the frequency is sent to the headpohne
pins
           LeftHp <= sys(4); 
           RightHp <= sys(4);
        else
           --If no button is pressed it makes sure that no signal is sent
to the headphones
           LeftHp <= '0';     
           RightHp <= '0';
        end if;
end process;

        lcd <= pb;                              --PortB connects to LCD
data bus. 
        gio <= peh;                             --Port E high connects to
GIO pins. 
        lcdrs <= pa(0);                         --LCD register select. 
        lcdrw <= pa(1);                         --LCD read/write. 
        lcde  <= pa(2);                         --LCD enable.  
        paout <= buttons;                       --Connect the switches to
4 pins of PORTA 
        xirq  <= '1';

interrupt: process(buttons) begin
        if ((buttons = "0111") or (buttons = "1110") or (buttons = "1011")
or (buttons = "1101")) then
                irq <= '0';
                led <= '0';
    else
                irq <= '1';
                led <= '1';
        end if;
end process;

END foo;




