#define	EnableIntr	0x20
#define	InsertTask	0x21
#define	RemoveTask	0x22
#define	SystemCallTest	0x23



//define variable

#define SystemCallSize 20  // 20*2 system call
#define KernelPredicate (8-1)    //(0~7)
#define	DataSize	8	//2^8=256=32*8
#define	MaxTask	5

//define data struct size
#define	TaskStructSize	(31+3)*DataSize //272=110h, order:Priority, R1,R2,...,R31,reti,flag
#define	MaxTaskSize TaskStructSize*MaxTask      //272*5=1360=550h         2006.05.17

//define boundary
#define IntEndAddress    SystemCallSize*2*4  //160=A0h, interrupt end address, start from address[0], 34 System call
#define KernelStructStart (SystemCallSize*DataSize) //160=A0h, the same with the IntEndAddress
#define TaskIndexStart KernelStructStart+TaskStructSize  //160+272=432=1B0h
#define TaskStructStart TaskIndexStart+(MaxTask*DataSize) //432+5*8=472=1D8h
#define CurTask TaskStructStart+MaxTaskSize  //472+1360=1832=728h
#define StackTmp CurTask+DataSize            //1840=730h
//#define StackTmp2 CurTask+DataSize*2         //1848=738h
//#define StackTmp3 CurTask+DataSize*3         //1856=740h
//#define StackTmp4 CurTask+DataSize*4         //1864=748h
//#define StackTmp5 CurTask+DataSize*5			   //1872=750h
#define StackStart StackTmp+(MaxTask*DataSize)

#define TaskCodeStart 4000h

/*          System Call         
   R29 as the stack top

*/

#define NR_syscalls 16


//all macros
ResetClock macro 
  P0 WriteSFR.and	sfr_intrctrl, 0xFE        //disable interrupt
  P0 WriteSFR.set sfr_T0, 0
  //P0 WriteSFR.set sfr_TC0, 0x03E8         //timer0 count 0 to 65535 (655350 ns)
  P0 WriteSFR.set sfr_TC0, 0x07D0     			//timer0 count 0 to 65535 (655350 ns)
  P0 WriteSFR.set	sfr_intren,0x05     			//enable interrupt 0x10 and 0x12 
  P0 WriteSFR.set sfr_intrpriority, 0x04    //set interrupt priority, key:high, timer:low    
  endm

lea	macro	Rn,lab
	loadi.z.0	Rn,lab&0xFFFF
	loadi.k.1	Rn,lab>>16
	endm
	
push macro Rn
  P0 store.8 Rn, R29, 0
  P0 addi R29, R29, DataSize
  endm
  
pop macro Rn
  P0 load.8 Rn, R29, -DataSize
  P0 subi R29, R29, DataSize
  endm

jmpop	macro	Rn,target,locate	
	loadi.z.0	Rn,target-(locate+4)	//4<=target-locate<65536
	excheck.2	Rn,Rn,Rn
	excheck.1	Rn,Rn,Rn
	endm


DumpRegister macro
  P0 WriteSFR sfr_debugport, R1 
  P0 WriteSFR sfr_debugport, R2 
  P0 WriteSFR sfr_debugport, R3 
  P0 WriteSFR sfr_debugport, R4 
  P0 WriteSFR sfr_debugport, R5 
  P0 WriteSFR sfr_debugport, R6 
  P0 WriteSFR sfr_debugport, R7 
  P0 WriteSFR sfr_debugport, R8 
  P0 WriteSFR sfr_debugport, R9 
  P0 WriteSFR sfr_debugport, R10 
  P0 WriteSFR sfr_debugport, R11 
  P0 WriteSFR sfr_debugport, R12 
  P0 WriteSFR sfr_debugport, R13 
  P0 WriteSFR sfr_debugport, R14 
  P0 WriteSFR sfr_debugport, R15 
  P0 WriteSFR sfr_debugport, R16 
  P0 WriteSFR sfr_debugport, R17 
  P0 WriteSFR sfr_debugport, R18 
  P0 WriteSFR sfr_debugport, R19 
  P0 WriteSFR sfr_debugport, R20 
  P0 WriteSFR sfr_debugport, R21 
  P0 WriteSFR sfr_debugport, R22 
  P0 WriteSFR sfr_debugport, R23 
  P0 WriteSFR sfr_debugport, R24 
  P0 WriteSFR sfr_debugport, R25 
  P0 WriteSFR sfr_debugport, R26 
  P0 WriteSFR sfr_debugport, R27 
  P0 WriteSFR sfr_debugport, R28 
  P0 WriteSFR sfr_debugport, R29 
  P0 WriteSFR sfr_debugport, R30 
  P0 WriteSFR sfr_debugport, R31 
  endm

ResetReg macro
  P0  ori R1, R0, 0
  P0  ori R2, R0, 0
  P0  ori R3, R0, 0
  P0  ori R4, R0, 0
  P0  ori R5, R0, 0
  P0  ori R6, R0, 0
  P0  ori R7, R0, 0
  P0  ori R8, R0, 0
  P0  ori R9, R0, 0
  P0  ori R10, R0, 0
  P0  ori R11, R0, 0
  P0  ori R12, R0, 0
  P0  ori R13, R0, 0
  P0  ori R14, R0, 0
  P0  ori R15, R0, 0
  P0  ori R16, R0, 0
  P0  ori R17, R0, 0
  P0  ori R18, R0, 0
  P0  ori R19, R0, 0
  P0  ori R20, R0, 0
  P0  ori R21, R0, 0
  P0  ori R22, R0, 0
  P0  ori R23, R0, 0
  P0  ori R24, R0, 0
  P0  ori R25, R0, 0
  P0  ori R26, R0, 0
  P0  ori R27, R0, 0
  P0  ori R28, R0, 0
  P0  ori R29, R0, 0
  P0  ori R30, R0, 0
  P0  ori R31, R0, 0
  endm
  
InitReg macro Rn, location
  P0  store.8 R0, Rn, location             
  P0  store.8 R0, Rn, location+DataSize*1
  P0  store.8 R0, Rn, location+DataSize*2
  P0  store.8 R0, Rn, location+DataSize*3
  P0  store.8 R0, Rn, location+DataSize*4
  P0  store.8 R0, Rn, location+DataSize*5
  P0  store.8 R0, Rn, location+DataSize*6
  P0  store.8 R0, Rn, location+DataSize*7
  P0  store.8 R0, Rn, location+DataSize*8
  P0  store.8 R0, Rn, location+DataSize*9 
  P0  store.8 R0, Rn, location+DataSize*10
  P0  store.8 R0, Rn, location+DataSize*11
  P0  store.8 R0, Rn, location+DataSize*12
  P0  store.8 R0, Rn, location+DataSize*13
  P0  store.8 R0, Rn, location+DataSize*14
  P0  store.8 R0, Rn, location+DataSize*15
  P0  store.8 R0, Rn, location+DataSize*16
  P0  store.8 R0, Rn, location+DataSize*17
  P0  store.8 R0, Rn, location+DataSize*18
  P0  store.8 R0, Rn, location+DataSize*19
  P0  store.8 R0, Rn, location+DataSize*20
  P0  store.8 R0, Rn, location+DataSize*21
  P0  store.8 R0, Rn, location+DataSize*22
  P0  store.8 R0, Rn, location+DataSize*23
  P0  store.8 R0, Rn, location+DataSize*24
  P0  store.8 R0, Rn, location+DataSize*25
  P0  store.8 R0, Rn, location+DataSize*26
  P0  store.8 R0, Rn, location+DataSize*27
  P0  store.8 R0, Rn, location+DataSize*28
  P0  store.8 R0, Rn, location+DataSize*29
  P0  store.8 R0, Rn, location+DataSize*30
  P0  store.8 R0, Rn, location+DataSize*31
  endm
  
SaveReg macro Rn, location
  P0  store.8 R0, Rn, location             //kernel priority
  P0  store.8 R1, Rn, location+DataSize*1
  P0  store.8 R2, Rn, location+DataSize*2
  P0  store.8 R3, Rn, location+DataSize*3
  P0  store.8 R4, Rn, location+DataSize*4
  P0  store.8 R5, Rn, location+DataSize*5
  P0  store.8 R6, Rn, location+DataSize*6
  P0  store.8 R7, Rn, location+DataSize*7
  P0  store.8 R8, Rn, location+DataSize*8
  P0  store.8 R9, Rn, location+DataSize*9 
  P0  store.8 R10, Rn, location+DataSize*10
  P0  store.8 R11, Rn, location+DataSize*11
  P0  store.8 R12, Rn, location+DataSize*12
  P0  store.8 R13, Rn, location+DataSize*13
  P0  store.8 R14, Rn, location+DataSize*14
  P0  store.8 R15, Rn, location+DataSize*15
  P0  store.8 R16, Rn, location+DataSize*16
  P0  store.8 R17, Rn, location+DataSize*17
  P0  store.8 R18, Rn, location+DataSize*18
  P0  store.8 R19, Rn, location+DataSize*19
  P0  store.8 R20, Rn, location+DataSize*20
  P0  store.8 R21, Rn, location+DataSize*21
  P0  store.8 R22, Rn, location+DataSize*22
  P0  store.8 R23, Rn, location+DataSize*23
  P0  store.8 R24, Rn, location+DataSize*24
  P0  store.8 R25, Rn, location+DataSize*25
  P0  store.8 R26, Rn, location+DataSize*26
  P0  store.8 R27, Rn, location+DataSize*27
  P0  store.8 R28, Rn, location+DataSize*28
  P0  store.8 R29, Rn, location+DataSize*29
  P0  store.8 R30, Rn, location+DataSize*30
  P0  store.8 R31, Rn, location+DataSize*31
  endm

LoadKernelData macro
  P0 load.8 R1, R0, KernelStructStart+DataSize*1
  P0 load.8 R2, R0, KernelStructStart+DataSize*2
  P0 load.8 R3, R0, KernelStructStart+DataSize*3
  P0 load.8 R4, R0, KernelStructStart+DataSize*4
  P0 load.8 R5, R0, KernelStructStart+DataSize*5
  P0 load.8 R6, R0, KernelStructStart+DataSize*6
  P0 load.8 R7, R0, KernelStructStart+DataSize*7
  P0 load.8 R8, R0, KernelStructStart+DataSize*8
  P0 load.8 R9, R0, KernelStructStart+DataSize*9
  P0 load.8 R10, R0, KernelStructStart+DataSize*10
  P0 load.8 R11, R0, KernelStructStart+DataSize*11
  P0 load.8 R12, R0, KernelStructStart+DataSize*12
  P0 load.8 R13, R0, KernelStructStart+DataSize*13
  P0 load.8 R14, R0, KernelStructStart+DataSize*14
  P0 load.8 R15, R0, KernelStructStart+DataSize*15
  P0 load.8 R16, R0, KernelStructStart+DataSize*16
  P0 load.8 R17, R0, KernelStructStart+DataSize*17
  P0 load.8 R18, R0, KernelStructStart+DataSize*18
  P0 load.8 R19, R0, KernelStructStart+DataSize*19
  P0 load.8 R20, R0, KernelStructStart+DataSize*20
  P0 load.8 R21, R0, KernelStructStart+DataSize*21
  P0 load.8 R22, R0, KernelStructStart+DataSize*22
  P0 load.8 R23, R0, KernelStructStart+DataSize*23
  P0 load.8 R24, R0, KernelStructStart+DataSize*24
  P0 load.8 R25, R0, KernelStructStart+DataSize*25
  P0 load.8 R26, R0, KernelStructStart+DataSize*26
  P0 load.8 R27, R0, KernelStructStart+DataSize*27
  P0 load.8 R28, R0, KernelStructStart+DataSize*28
//P0 load.8 R29, R0, KernelStructStart+DataSize*29
  P0 load.8 R30, R0, KernelStructStart+DataSize*30
  P0 load.8 R31, R0, KernelStructStart+DataSize*31
  endm
  
LoadTaskData macro
  P0  load.8 R9, R0, CurTask        //R9 = task struct index
  P0  load.8 R30, R9, 0

  //load Task data
  P0 load.8 R1, R30, DataSize*1
  P0 load.8 R2, R30, DataSize*2
  P0 load.8 R3, R30, DataSize*3
  P0 load.8 R4, R30, DataSize*4
  P0 load.8 R5, R30, DataSize*5
  P0 load.8 R6, R30, DataSize*6
  P0 load.8 R7, R30, DataSize*7
  P0 load.8 R8, R30, DataSize*8
  P0 load.8 R9, R30, DataSize*9
  P0 load.8 R10, R30, DataSize*10
  P0 load.8 R11, R30, DataSize*11
  P0 load.8 R12, R30, DataSize*12
  P0 load.8 R13, R30, DataSize*13
  P0 load.8 R14, R30, DataSize*14
  P0 load.8 R15, R30, DataSize*15
  P0 load.8 R16, R30, DataSize*16
  P0 load.8 R17, R30, DataSize*17
  P0 load.8 R18, R30, DataSize*18
  P0 load.8 R19, R30, DataSize*19
  P0 load.8 R20, R30, DataSize*20
  P0 load.8 R21, R30, DataSize*21
  P0 load.8 R22, R30, DataSize*22
  P0 load.8 R23, R30, DataSize*23
//  P0 load.8 R24, R30, DataSize*24
  P0 load.8 R25, R30, DataSize*25
  P0 load.8 R26, R30, DataSize*26
  P0 load.8 R27, R30, DataSize*27
  P0 load.8 R28, R30, DataSize*28
//P0 load.8 R29, R30, DataSize*29
//P0 load.8 R30, R30, DataSize*30
  P0 load.8 R31, R30, DataSize*31
  
  P0 load.8 R26, R30, DataSize*32

  endm

SAVE_ALL macro 
  P0 store.8 R1, R29, 0 
  P0 store.8 R2, R29, DataSize*1 
  P0 store.8 R3, R29, DataSize*2 
  P0 store.8 R4, R29, DataSize*3 
  P0 store.8 R5, R29, DataSize*4 
  P0 store.8 R6, R29, DataSize*5 
  P0 store.8 R7, R29, DataSize*6 
  P0 store.8 R8, R29, DataSize*7 
  P0 store.8 R9, R29, DataSize*8 
  P0 store.8 R10, R29, DataSize*9 
  P0 store.8 R11, R29, DataSize*10 
  P0 store.8 R12, R29, DataSize*11 
  P0 store.8 R13, R29, DataSize*12 
  P0 store.8 R14, R29, DataSize*13 
  P0 store.8 R15, R29, DataSize*14 
  P0 store.8 R16, R29, DataSize*15 
  P0 store.8 R17, R29, DataSize*16 
  P0 store.8 R18, R29, DataSize*17 
  P0 store.8 R19, R29, DataSize*18 
  P0 store.8 R20, R29, DataSize*19 
  P0 store.8 R21, R29, DataSize*20 
  P0 store.8 R22, R29, DataSize*21 
  P0 store.8 R23, R29, DataSize*22 
  P0 store.8 R24, R29, DataSize*23 
  P0 store.8 R25, R29, DataSize*24 
  P0 store.8 R26, R29, DataSize*25 
  P0 store.8 R27, R29, DataSize*26 
  P0 store.8 R28, R29, DataSize*27 
  P0 store.8 R29, R29, DataSize*28 
  P0 store.8 R30, R29, DataSize*29 
  P0 store.8 R31, R29, DataSize*30 
  P0 addi    R29, R0, DataSize*31
  endm
//#define RESTORE_ALL
/*  subi   R29, R0, DataSize*31 \
  load.8 R1, R29, 0 \
  load.8 R2, R29, DataSize*1 \
  load.8 R3, R29, DataSize*2 \
  load.8 R4, R29, DataSize*3 \
  load.8 R5, R29, DataSize*4 \
  load.8 R6, R29, DataSize*5 \
  load.8 R7, R29, DataSize*6 \
  load.8 R8, R29, DataSize*7 \
  load.8 R9, R29, DataSize*8 \
  load.8 R10, R29, DataSize*9 \
  load.8 R11, R29, DataSize*10 \
  load.8 R12, R29, DataSize*11 \
  load.8 R13, R29, DataSize*12 \
  load.8 R14, R29, DataSize*13 \
  load.8 R15, R29, DataSize*14 \
  load.8 R16, R29, DataSize*15 \
  load.8 R17, R29, DataSize*16 \
  load.8 R18, R29, DataSize*17 \
  load.8 R19, R29, DataSize*18 \
  load.8 R20, R29, DataSize*19 \
  load.8 R21, R29, DataSize*20 \
  load.8 R22, R29, DataSize*21 \
  load.8 R23, R29, DataSize*22 \
  load.8 R24, R29, DataSize*23 \
  load.8 R25, R29, DataSize*24 \
  load.8 R26, R29, DataSize*25 \
  load.8 R27, R29, DataSize*26 \
  load.8 R28, R29, DataSize*27 \
//  load.8 R29, R29, DataSize*28 \
  load.8 R30, R29, DataSize*29 \
  load.8 R31, R29, DataSize*30
  */
  










