/* interrupt.h - HC12 interrupt handling support.
 * Copyright 2008 Colin D. Bennett <colin@gibibit.com>.
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *    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 3 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, see <http://www.gnu.org/licenses/>.
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * The HC12 interrupt vectors are handled by the D-Bug12 boot loader, which
 * chains interrupts to another RAM location, at 0x3E00.
 * 
 * Interrupt handlers are set at runtime by storing the address of the
 * interrupt handler at the appropriate RAM location.
 *
 * Portions by Navid Mohaghegh (MC9S912DP256 - 2006/12/13)
 * Modified by Colin Bennett (2008/04/18)
 */

#ifndef HC12_VECTORS12_H
#define HC12_VECTORS12_H

/*
 * Set the interrupt handler for the specified vector.
 * Example:
 *
 *   DECLARE_ISR(my_intr);
 *   void init()
 *   {
 *       SET_INTERRUPT_VECTOR(UserTimerOvf, my_intr);
 *       // You would then want to enable the specific interrupt.
 *       enable_interrupts();   // Enable global interrupt flag.
 *   }
 *   ISR(my_intr)
 *   {
 *       // This is the interrupt handler; do stuff here...
 *   }
 */
#define SET_INTERRUPT_VECTOR(vec, isr_ptr) \
	do { \
		vec = (uint16_t) isr_ptr; \
	} while (0)


#ifndef VECTOR_BASE
#define VECTOR_BASE 0x3E00
#endif

#define _VEC16(off)  (*(volatile uint16_t *)(VECTOR_BASE + off*2))

// TODO Give these names incorporating 'Vector'
#define UserRsrv0x80 _VEC16(0)
#define UserRsrv0x82 _VEC16(1)
#define UserRsrv0x84 _VEC16(2)
#define UserRsrv0x86 _VEC16(3)
#define UserRsrv0x88 _VEC16(4)
#define UserRsrv0x8a _VEC16(5)
#define UserPWMShDn _VEC16(6)
#define UserPortP _VEC16(7)
#define UserMSCAN4Tx _VEC16(8)
#define UserMSCAN4Rx _VEC16(9)
#define UserMSCAN4Errs _VEC16(10)
#define UserMSCAN4Wake _VEC16(11)
#define UserMSCAN3Tx _VEC16(12)
#define UserMSCAN3Rx _VEC16(13)
#define UserMSCAN3Errs _VEC16(14)
#define UserMSCAN3Wake _VEC16(15)
#define UserMSCAN2Tx _VEC16(16)
#define UserMSCAN2Rx _VEC16(17)
#define UserMSCAN2Errs _VEC16(18)
#define UserMSCAN2Wake _VEC16(19)
#define UserMSCAN1Tx _VEC16(20)
#define UserMSCAN1Rx _VEC16(21)
#define UserMSCAN1Errs _VEC16(22)
#define UserMSCAN1Wake _VEC16(23)
#define UserMSCAN0Tx _VEC16(24)
#define UserMSCAN0Rx _VEC16(25)
#define UserMSCAN0Errs _VEC16(26)
#define UserMSCAN0Wake _VEC16(27)
#define UserFlash _VEC16(28)
#define UserEEPROM _VEC16(29)
#define UserSPI2 _VEC16(30)
#define UserSPI1 _VEC16(31)
#define UserIIC _VEC16(32)
#define UserDLC _VEC16(33)
#define UserSCME _VEC16(34)
#define UserCRG _VEC16(35)
#define UserPAccBOv _VEC16(36)
#define UserModDwnCtr _VEC16(37)
#define UserPortH _VEC16(38)
#define UserPortJ _VEC16(39)
#define UserAtoD1 _VEC16(40)
#define UserAtoD0 _VEC16(41)
#define UserSCI1 _VEC16(42)
#define UserSCI0 _VEC16(43)
#define UserSPI0 _VEC16(44)
#define UserPAccEdge _VEC16(45)
#define UserPAccOvf _VEC16(46)
#define UserTimerOvf _VEC16(47)
#define UserTimerCh7 _VEC16(48)
#define UserTimerCh6 _VEC16(49)
#define UserTimerCh5 _VEC16(50)
#define UserTimerCh4 _VEC16(51)
#define UserTimerCh3 _VEC16(52)
#define UserTimerCh2 _VEC16(53)
#define UserTimerCh1 _VEC16(54)
#define UserTimerCh0 _VEC16(55)
#define UserRTI _VEC16(56)
#define UserIRQ _VEC16(57)
#define UserXIRQ _VEC16(58)
#define UserSWI _VEC16(59)
#define UserTrap _VEC16(60)

#endif
