#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

//------- New Memory ------
#define IVTStart 512                            //512=200h
#define IOMapStart    IVTStart+DataSize*256     //512+8*256=2560=A00h
#define KernelCode    00100A00h                //2560+1MB =100A00h
#define KernelData    00200A00h                //100A00h+1MB =200A00h

//#define KernelStack   00240A00h                //200A00h+256KB =240A00h
//#define KernelHeap    00260800h                //240A00h+1FE00(130560B) =260800h
#define KernelHeap    00240A00h
#define KernelStack   002F3DB0h
#define SysCall       002F3DB8h                //~ + 603576B =2F3DB8h

/* Task 1 Physical Address */
#define Task1Start    00413FB8h                //~ + 1180160B = 413FB8h
#define Task1Data     0045EFB8h                
#define Task1Heap     004847B8h                
#define Task1Stack    004B67B8h                
#define Task1Frame    004D6598h
#define Task1ID         2
#define Task1Priority   8

/* Task 2 Physical Address */
#define Task2Start    004D65B8h                //
#define Task2Data     005215B8h                
#define Task2Heap     00546DB8h                
#define Task2Stack    00578DB8h                
#define Task2Frame    00598B98h
#define Task2ID         3
#define Task2Priority   7

/* Task 3 Physical Address */
#define Task3Start    00598BB8h                //
#define Task3Data     005E3BB8h                
#define Task3Heap     006093B8h                
#define Task3Stack    0063B3B8h                
#define Task3Frame    0065B198h
#define Task3ID         4
#define Task3Priority   6

/* Task 4 Physical Address */
#define Task4Start    0065B1B8h                //
#define Task4Data     006A61B8h                
#define Task4Heap     006CB9B8h                
#define Task4Stack    006FD9B8h                
#define Task4Frame    0071D798h
#define Task4ID         5
#define Task4Priority   5

/* Task 5 Physical Address */
#define Task5Start    0071D7B8h                //
#define Task5Data     007687B8h                
#define Task5Heap     0078DFB8h                
#define Task5Stack    007BFFB8h                
#define Task5Frame    007DFD98h
#define Task5ID         6
#define Task5Priority   4

/* Final Boundary */
#define Task6Start    007DFDB8h                //

/* Idle task and Other task */
#define IdleTaskID      1
#define IdleTaskP       1
#define SmallTask1ID    7   
#define SmallTask1P     3
#define SmallTask2ID    8   
#define SmallTask2P     2


//-------------
/* Dynamic loading related informations */
#define TEXTSIZE      0004B000h
#define DATASIZE      00025800h

// assumed ROM start address
#define LMA_Start     40000000h

#define task1textLMA   LMA_Start
#define linkinfo1      (LMA_Start+TEXTSIZE)
#define task1dataLMA   (linkinfo1+DataSize*2)

#define task2textLMA   (task1dataLMA+DATASIZE)
#define linkinfo2      (task2textLMA+TEXTSIZE)
#define task2dataLMA   (linkinfo2+DataSize*2)

#define task3textLMA   (task2dataLMA+DATASIZE)
#define linkinfo3      (task3textLMA+TEXTSIZE)
#define task3dataLMA   (linkinfo3+DataSize*2)

#define task4textLMA   (task3dataLMA+DATASIZE)
#define linkinfo4      (task4textLMA+TEXTSIZE)
#define task4dataLMA   (linkinfo4+DataSize*2)

#define task5textLMA   (task4dataLMA+DATASIZE)
#define linkinfo5      (task5textLMA+TEXTSIZE)
#define task5dataLMA   (linkinfo5+DataSize*2)

//-------------


//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 KernelData //(SystemCallSize*DataSize) //160=A0h, the same with the IntEndAddress

#define TaskIndexStart   00200B10h //KernelStructStart+110h //TaskStructSize  //160+272=432=1B0h
#define TaskStructStart  00200B38h //TaskIndexStart+28h    //(MaxTask*DataSize) //432+5*8=472=1D8h
#define CurTask  00201088h // TaskStructStart+550h   		    //472+1360=1832=728h
//#define StackTmp CurTask+DataSize             //1840=730h
#define StackTmp 00201090h//CurTask+8h             //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 StackTmpEnd (StackTmp + DataSize*MaxTask)
//#define StackTmpEnd 002010B8h

#define StackStart (KernelStack) //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  

  P0 store.8 Rn, R29, 0
  P0 subi R29, R29, DataSize
  endm
  
pop macro Rn
  //P0 load.8 Rn, R29, -DataSize
  //P0 subi R29, R29, DataSize
 
  P0 load.8 Rn, R29, DataSize
  P0 addi 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
  
  P0  store.8 R0, Rn, location+DataSize*32
  P0  store.8 R0, Rn, location+DataSize*33
  
  endm
  
SaveReg macro Rn, location
  push R30
      
  //P0  store.8 R0, Rn, location             //kernel priority
  lea R30, location    
  //P0 ori R30, R0, location    
  
  P0  store.8 R0, R30, 0            //current task = NULL
  
  //P0  store.8 R1, Rn, location+DataSize*1
  //lea R30, location+DataSize*1    
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*1    
  
  P0  store.8 R1, R30, 0            //current task = NULL
  
  //P0  store.8 R2, Rn, location+DataSize*2
  //lea R30, location+DataSize*2
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*3
  P0  store.8 R2, R30, 0            //current task = NULL
  //P0  store.8 R3, Rn, location+DataSize*3
  //lea R30, location+DataSize*3    
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*3    
  P0  store.8 R3, R30, 0            //current task = NULL
  //P0  store.8 R4, Rn, location+DataSize*4
  //lea R30, location+DataSize*4
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*4    
  P0  store.8 R4, R30, 0            //current task = NULL
  //P0  store.8 R5, Rn, location+DataSize*5
  //lea R30, location+DataSize*5
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*5    
  P0  store.8 R5, R30, 0            //current task = NULL
  //P0  store.8 R6, Rn, location+DataSize*6
  //lea R30, location+DataSize*6
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*6    
  P0  store.8 R6, R30, 0            //current task = NULL
  //P0  store.8 R7, Rn, location+DataSize*7
  //lea R30, location+DataSize*7
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*7    
  P0  store.8 R7, R30, 0            //current task = NULL
  //P0  store.8 R8, Rn, location+DataSize*8
  //lea R30, location+DataSize*8
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*8
  P0  store.8 R8, R30, 0
  //P0  store.8 R9, Rn, location+DataSize*9 
  //lea R30, location+DataSize*9
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*9
  P0  store.8 R9, R30, 0
  
  //P0  store.8 R10, Rn, location+DataSize*10
  //lea R30, location+DataSize*10
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*10
  P0  store.8 R10, R30, 0
  //P0  store.8 R11, Rn, location+DataSize*11
  //lea R30, location+DataSize*11
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*11
  P0  store.8 R11, R30, 0
  //P0  store.8 R12, Rn, location+DataSize*12
  //lea R30, location+DataSize*12
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*12
  P0  store.8 R12, R30, 0
  //P0  store.8 R13, Rn, location+DataSize*13
  //lea R30, location+DataSize*13
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*13
  P0  store.8 R13, R30, 0
  //P0  store.8 R14, Rn, location+DataSize*14
  //lea R30, location+DataSize*14
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*14
  P0  store.8 R14, R30, 0
  //P0  store.8 R15, Rn, location+DataSize*15
  //lea R30, location+DataSize*15
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*15
  P0  store.8 R15, R30, 0
  //P0  store.8 R16, Rn, location+DataSize*16
  //lea R30, location+DataSize*16
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*16
  P0  store.8 R16, R30, 0
  //P0  store.8 R17, Rn, location+DataSize*17
  //lea R30, location+DataSize*17
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*17
  P0  store.8 R17, R30, 0
  //P0  store.8 R18, Rn, location+DataSize*18
  //lea R30, location+DataSize*18
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*18
  P0  store.8 R18, R30, 0
  //P0  store.8 R19, Rn, location+DataSize*19
  //lea R30, location+DataSize*19
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*19
  P0  store.8 R19, R30, 0
  //P0  store.8 R20, Rn, location+DataSize*20
  //lea R30, location+DataSize*20
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*20
  P0  store.8 R20, R30, 0
  //P0  store.8 R21, Rn, location+DataSize*21
  //lea R30, location+DataSize*21
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*21
  P0  store.8 R21, R30, 0
  //P0  store.8 R22, Rn, location+DataSize*22
  //lea R30, location+DataSize*22
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*22
  P0  store.8 R22, R30, 0
  //P0  store.8 R23, Rn, location+DataSize*23
  //lea R30, location+DataSize*23
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*23
  P0  store.8 R23, R30, 0
  //P0  store.8 R24, Rn, location+DataSize*24
  //lea R30, location+DataSize*24
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*24
  P0  store.8 R24, R30, 0
  //P0  store.8 R25, Rn, location+DataSize*25
  //lea R30, location+DataSize*25
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*25
  P0  store.8 R25, R30, 0
  //P0  store.8 R26, Rn, location+DataSize*26
  //lea R30, location+DataSize*26
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*26
  P0  store.8 R26, R30, 0
  //P0  store.8 R27, Rn, location+DataSize*27
  //lea R30, location+DataSize*27
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*27
  P0  store.8 R27, R30, 0
  //P0  store.8 R28, Rn, location+DataSize*28
  //lea R30, location+DataSize*28
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*28
  P0  store.8 R28, R30, 0
  //P0  store.8 R29, Rn, location+DataSize*29
  //lea R30, location+DataSize*29
  P0 addi R30, R30, DataSize
  //P0 ori R30, R0, location+DataSize*29
  P0  store.8 R29, R30, 0
  pop R30
  push R20
  //P0  store.8 R30, Rn, location+DataSize*30
  //lea R20, location+DataSize*30
  P0 addi R20, R20, DataSize
  //P0 ori R20, R0, location+DataSize*30
  P0  store.8 R30, R20, 0
  //P0  store.8 R31, Rn, location+DataSize*31
  //lea R20, location+DataSize*31
  P0 addi R20, R20, DataSize
  //P0 ori R20, R0, location+DataSize*31
  P0  store.8 R31, R20, 0
  pop R20
  
  endm

LoadKernelData macro
  //push R20
  //P0 load.8 R1, R0, KernelStructStart+DataSize*1
  lea R20, KernelStructStart
  P0 addi R20, R20, DataSize  
  //P0 ori R29, R0, KernelStructStart+DataSize*1
  P0 loadx.8 R1, R0, R20
  //P0 load.8 R2, R0, KernelStructStart+DataSize*2
  //lea R29, KernelStructStart+DataSize*2
  P0 addi R20, R20, DataSize//*2
  //P0 ori R29, R0, KernelStructStart+DataSize*2
  P0 loadx.8 R2, R0, R20
  //P0 load.8 R3, R0, KernelStructStart+DataSize*3
  //lea R29, KernelStructStart+DataSize*3
  P0 addi R20, R20, DataSize//*3
  //P0 ori R29, R0, KernelStructStart+DataSize*3
  P0 loadx.8 R3, R0, R20
  //P0 load.8 R4, R0, KernelStructStart+DataSize*4
  //lea R29, KernelStructStart+DataSize*4
  P0 addi R20, R20, DataSize//*4
  //P0 ori R29, R0, KernelStructStart+DataSize*4
  P0 loadx.8 R4, R0, R20
  //P0 load.8 R5, R0, KernelStructStart+DataSize*5
  //lea R29, KernelStructStart+DataSize*5
  P0 addi R20, R20, DataSize//*5
  //P0 ori R29, R0, KernelStructStart+DataSize*5
  P0 loadx.8 R5, R0, R20
  
  //P0 load.8 R6, R0, KernelStructStart+DataSize*6
  //lea R29, KernelStructStart+DataSize*6
  P0 addi R20, R20, DataSize //*6
  //P0 ori R29, R0, KernelStructStart+DataSize*6
  P0 loadx.8 R6, R0, R20
  //P0 load.8 R7, R0, KernelStructStart+DataSize*7
  //lea R29, KernelStructStart+DataSize*7
  P0 addi R20, R20, DataSize //*1
  //P0 ori R29, R0, KernelStructStart+DataSize*7
  P0 loadx.8 R7, R0, R20
  //P0 load.8 R8, R0, KernelStructStart+DataSize*8
  //lea R29, KernelStructStart+DataSize*8
  P0 addi R20, R20, DataSize //*1
  //P0 ori R29, R0, KernelStructStart+DataSize*8
  P0 loadx.8 R8, R0, R20
  //P0 load.8 R9, R0, KernelStructStart+DataSize*9
  //lea R29, KernelStructStart+DataSize*9
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*9
  P0 loadx.8 R9, R0, R20
  //P0 load.8 R10, R0, KernelStructStart+DataSize*10
  //lea R29, KernelStructStart+DataSize*10
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*10
  P0 loadx.8 R10, R0, R20
  //P0 load.8 R11, R0, KernelStructStart+DataSize*11
  //lea R29, KernelStructStart+DataSize*11
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*11
  P0 loadx.8 R11, R0, R20
  //P0 load.8 R12, R0, KernelStructStart+DataSize*12
  //lea R29, KernelStructStart+DataSize*12
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*12
  P0 loadx.8 R12, R0, R20
  //P0 load.8 R13, R0, KernelStructStart+DataSize*13
  //lea R29, KernelStructStart+DataSize*13
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*13
  P0 loadx.8 R13, R0, R20
  //P0 load.8 R14, R0, KernelStructStart+DataSize*14
  //lea R29, KernelStructStart+DataSize*14
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*14
  P0 loadx.8 R14, R0, R20
  //P0 load.8 R15, R0, KernelStructStart+DataSize*15
  //lea R29, KernelStructStart+DataSize*15
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*15
  P0 loadx.8 R15, R0, R20
  //P0 load.8 R16, R0, KernelStructStart+DataSize*16
  //lea R29, KernelStructStart+DataSize*16
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*16
  P0 loadx.8 R16, R0, R20
  //P0 load.8 R17, R0, KernelStructStart+DataSize*17
  //lea R29, KernelStructStart+DataSize*17
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*17
  P0 loadx.8 R17, R0, R20
  //P0 load.8 R18, R0, KernelStructStart+DataSize*18
  //lea R29, KernelStructStart+DataSize*18
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*18
  P0 loadx.8 R18, R0, R20
  //P0 load.8 R19, R0, KernelStructStart+DataSize*19
  //lea R29, KernelStructStart+DataSize*19
  P0 addi R20, R20, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*19
  P0 loadx.8 R19, R0, R20
  //P0 load.8 R20, R0, KernelStructStart+DataSize*20
  //lea R29, KernelStructStart+DataSize*20
  P0 addi R20, R20, DataSize 
  
  push R18
  push R20
  pop R18
  
  //P0 ori R29, R0, KernelStructStart+DataSize*20
  P0 loadx.8 R20, R0, R18
  //P0 load.8 R21, R0, KernelStructStart+DataSize*21
  //lea R29, KernelStructStart+DataSize*21
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*21
  P0 loadx.8 R21, R0, R18
  //P0 load.8 R22, R0, KernelStructStart+DataSize*22
  //lea R29, KernelStructStart+DataSize*22
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*22
  P0 loadx.8 R22, R0, R18
  //P0 load.8 R23, R0, KernelStructStart+DataSize*23
  //lea R29, KernelStructStart+DataSize*23
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*23
  P0 loadx.8 R23, R0, R18
  //P0 load.8 R24, R0, KernelStructStart+DataSize*24
  //lea R29, KernelStructStart+DataSize*24
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*24
  P0 loadx.8 R24, R0, R18
  //P0 load.8 R25, R0, KernelStructStart+DataSize*25
  //lea R29, KernelStructStart+DataSize*25
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*25
  P0 loadx.8 R25, R0, R18
  //P0 load.8 R26, R0, KernelStructStart+DataSize*26
  //lea R29, KernelStructStart+DataSize*26
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*26
  P0 loadx.8 R26, R0, R18
  //P0 load.8 R27, R0, KernelStructStart+DataSize*27
  //lea R29, KernelStructStart+DataSize*27
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*27
  P0 loadx.8 R27, R0, R18
  //P0 load.8 R28, R0, KernelStructStart+DataSize*28
  //lea R29, KernelStructStart+DataSize*28
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*28
  P0 loadx.8 R28, R0, R18

//P0 load.8 R29, R0, KernelStructStart+DataSize*29
  
  //P0 load.8 R30, R0, KernelStructStart+DataSize*30
  //lea R29, KernelStructStart+DataSize*30
  P0 addi R18, R18, DataSize 
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*30
  P0 loadx.8 R30, R0, R18
  //P0 load.8 R31, R0, KernelStructStart+DataSize*31
  //lea R29, KernelStructStart+DataSize*31
  P0 addi R18, R18, DataSize 
  //P0 ori R29, R0, KernelStructStart+DataSize*31
  P0 loadx.8 R31, R0, R18
  
  pop R18
  endm
  
LoadTaskData macro
  
  lea R25, CurTask      
  P0  load.8 R9, R25, 0					      //R9 = task struct index     
  P0  load.8 R28, R9, 0

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

  P0 load.8 R26, R28, DataSize*32        //task reti
  P0 load.8 R25, R28, DataSize*33        //task flag

  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
  */
  










