
#include "board.h"	
		
	SELECT_CPU		
	.text

#define NEW_VECTOR_BASE 0x200000
	
#define GLOBAL(symbol)  \
	.global symbol; \
symbol:	

#define VECTOR(nr, handler)                  \
	.org (nr*4);                         \
	_cpu_vector_##handler:               \
	.long _cpu_handler_##handler;

#define HANDLER(nr, handler)                         \
	GLOBAL(_cpu_handler_##handler);              \
	mov.l	er0, @-er7;  /* placeholder push */  \
	mov.l	er0, @-er7;  /* save er0         */  \
	mov.l   @(NEW_VECTOR_BASE + (nr * 4)), er0;  \
	mov.l	er0, @(4, er7); /* modify stack */   \
	mov.l	@er7+, er0;  /* restore er0     */  \
	rts
	
	/* the vector table */

	VECTOR(0, power_on_reset); 
	VECTOR(1, manual_reset); 

	VECTOR(5, trace); 
	VECTOR(6, direct_transition); 
	VECTOR(7, nmi); 

	VECTOR(8, trap0); 
	VECTOR(9, trap1); 
	VECTOR(10, trap2); 
	VECTOR(11, trap3); 

	VECTOR(16, irq0); 
	VECTOR(17, irq1); 
	VECTOR(18, irq2);
	VECTOR(19, irq3);
	VECTOR(20, irq4); 
	VECTOR(21, irq5); 
	VECTOR(22, irq6);
	VECTOR(23, irq7);

	VECTOR(24, swdtend);
	VECTOR(25, wovi0);
	VECTOR(26, cmi);
	VECTOR(27, pc_break);
	VECTOR(28, adi);
	VECTOR(29, wovi1);

	VECTOR(32, tgi0a);
	VECTOR(33, tgi0b);
	VECTOR(34, tgi0c);
	VECTOR(35, tgi0d);
	VECTOR(36, tgi0v);

	VECTOR(40, tgi1a);
	VECTOR(41, tgi1b);
	VECTOR(42, tgi1v);
	VECTOR(43, tgi1u);

	VECTOR(44, tgi2a);
	VECTOR(45, tgi2b);
	VECTOR(46, tgi2v);
	VECTOR(47, tgi2u);

	VECTOR(48, tgi3a);
	VECTOR(49, tgi3b);
	VECTOR(50, tgi3c);
	VECTOR(51, tgi3d);
	VECTOR(52, tgi3v);
	
	VECTOR(56, tgi4a);
	VECTOR(57, tgi4b);
	VECTOR(58, tgi4v);
	VECTOR(59, tgi4u);

	VECTOR(60, tgi5a);
	VECTOR(61, tgi5b);
	VECTOR(62, tgi5v);
	VECTOR(63, tgi5u);

	VECTOR(64, cmia0);
	VECTOR(65, cmib0);
	VECTOR(66, ovi0);

	VECTOR(68, cmia1);
	VECTOR(69, cmib1);
	VECTOR(70, ovi1);

	VECTOR(72, dend0a);
	VECTOR(73, dend0b);
	VECTOR(74, dend1a);
	VECTOR(75, dend1b);

	VECTOR(80, eri0);
	VECTOR(81, rxi0);
	VECTOR(82, txi0);
	VECTOR(83, tei0);

	VECTOR(84, eri1);
	VECTOR(85, rxi1);
	VECTOR(86, txi1);
	VECTOR(87, tei1);

	VECTOR(88, eri2);
	VECTOR(89, rxi2);
	VECTOR(90, txi2);
	VECTOR(91, tei2);

	VECTOR(92, cmia2);
	VECTOR(93, cmib2);
	VECTOR(94, ovi2);
	
	VECTOR(96, cmia3);
	VECTOR(97, cmib3);
	VECTOR(98, ovi3);
	
	VECTOR(100, iici0);
	VECTOR(101, ddcsw1);
	VECTOR(102, iici1);

	VECTOR(120, eri3);
	VECTOR(121, rxi3);
	VECTOR(122, txi3);
	VECTOR(123, tei3);

	VECTOR(124, eri4);
	VECTOR(125, rxi4);
	VECTOR(126, txi4);
	VECTOR(127, tei4);

	.globl start
start:	
_cpu_handler_power_on_reset:	

	mov.l	#STACK_TOP, er7

	jsr	board_init
	jsr	uart_init

	jmp	boot_start

	HANDLER(1, manual_reset); 

	HANDLER(5, trace); 
	HANDLER(6, direct_transition); 
	HANDLER(7, nmi); 

	HANDLER(8, trap0); 
	HANDLER(9, trap1); 
	HANDLER(10, trap2); 
	HANDLER(11, trap3); 

	HANDLER(16, irq0); 
	HANDLER(17, irq1); 
	HANDLER(18, irq2);
	HANDLER(19, irq3);
	HANDLER(20, irq4); 
	HANDLER(21, irq5); 
	HANDLER(22, irq6);
	HANDLER(23, irq7);

	HANDLER(24, swdtend);
	HANDLER(25, wovi0);
	HANDLER(26, cmi);
	HANDLER(27, pc_break);
	HANDLER(28, adi);
	HANDLER(29, wovi1);

	HANDLER(32, tgi0a);
	HANDLER(33, tgi0b);
	HANDLER(34, tgi0c);
	HANDLER(35, tgi0d);
	HANDLER(36, tgi0v);

	HANDLER(40, tgi1a);
	HANDLER(41, tgi1b);
	HANDLER(42, tgi1v);
	HANDLER(43, tgi1u);

	HANDLER(44, tgi2a);
	HANDLER(45, tgi2b);
	HANDLER(46, tgi2v);
	HANDLER(47, tgi2u);

	HANDLER(48, tgi3a);
	HANDLER(49, tgi3b);
	HANDLER(50, tgi3c);
	HANDLER(51, tgi3d);
	HANDLER(52, tgi3v);
	
	HANDLER(56, tgi4a);
	HANDLER(57, tgi4b);
	HANDLER(58, tgi4v);
	HANDLER(59, tgi4u);

	HANDLER(60, tgi5a);
	HANDLER(61, tgi5b);
	HANDLER(62, tgi5v);
	HANDLER(63, tgi5u);

	HANDLER(64, cmia0);
	HANDLER(65, cmib0);
	HANDLER(66, ovi0);

	HANDLER(68, cmia1);
	HANDLER(69, cmib1);
	HANDLER(70, ovi1);

	HANDLER(72, dend0a);
	HANDLER(73, dend0b);
	HANDLER(74, dend1a);
	HANDLER(75, dend1b);

	HANDLER(80, eri0);
	HANDLER(81, rxi0);
	HANDLER(82, txi0);
	HANDLER(83, tei0);

	HANDLER(84, eri1);
	HANDLER(85, rxi1);
	HANDLER(86, txi1);
	HANDLER(87, tei1);

	HANDLER(88, eri2);
	HANDLER(89, rxi2);
	HANDLER(90, txi2);
	HANDLER(91, tei2);

	HANDLER(92, cmia2);
	HANDLER(93, cmib2);
	HANDLER(94, ovi2);
	
	HANDLER(96, cmia3);
	HANDLER(97, cmib3);
	HANDLER(98, ovi3);
	
	HANDLER(100, iici0);
	HANDLER(101, ddcsw1);
	HANDLER(102, iici1);

	HANDLER(120, eri3);
	HANDLER(121, rxi3);
	HANDLER(122, txi3);
	HANDLER(123, tei3);

	HANDLER(124, eri4);
	HANDLER(125, rxi4);
	HANDLER(126, txi4);
	HANDLER(127, tei4);
