#include "AEE.h"
#if !defined(AEEBRIDLE_H_)
#define AEEBRIDLE_H_

/*======================================================
FILE:  AEEBridle.h

SERVICES:  BREW Memory Protection 

GENERAL DESCRIPTION:


        Copyright © 2003 QUALCOMM Incorporated.
               All Rights Reserved.
            QUALCOMM Proprietary/GTDR
=====================================================*/

#include <stdarg.h>
#include "AEEComdef.h"
#include "AEEHandle.h"
#include "AEEIOCtl.h"
#include "AEEkCB.h"
#include "AEEError.h"

#define BADSWI                        0
#define SPLSWI                        1

#define BRIDLE_TYPE_OFFSET           28
#define BRIDLE_FUNCTION_OFFSET       20
#define BRIDLE_SUBSYS_MAX            500

//Processor mode mask of the function that called BRIDLE_SWI
#define BRIDLE_FROM_UNPRIV            0x00000000
#define BRIDLE_FROM_PRIV              0xF0000000

#define BRIDLE_USR_MODE               0
#define BRIDLE_SVC_MODE               0x03
#define BRIDLE_SYS_MODE               0x0f


#define BRIDLE_NO_ACCESS              1
#define BRIDLE_READ_ACCESS            2
#define BRIDLE_WRITE_ACCESS           3
#define BRIDLE_READ_STRING_ACCESS     4

/*========================================================================
The BRIDLE system call number (control number) that gets passed on to the 
SWI Handler is a 32-bit entity whose structure is given below:

     ------------------------------------------
    |        |          |                      |
    | Type   | Function |  Subsys No.          |
    |(4 bits)| (8 bits) |  (20 bits)           |
    |        |          |                      |
     ------------------------------------------


==========================================================================*/
// Constructs the BRIDLE system call number, control number, or "swino"
#define BRIDLE_SYSCALL_NO(subsysno,fnno) ((((fnno) << BRIDLE_FUNCTION_OFFSET) | \
                                           (subsysno)) & 0x3FFFFFFF)

//Gets the 8-bit function no. from the BRIDLE Control number
#define BRIDLE_GET_FNNO(swino)       (((swino) >> BRIDLE_FUNCTION_OFFSET) & 0xff)

//Gets the 8-bit function no. from the BRIDLE Control number
#define BRIDLE_GET_SUBSYSNO(swino)    ((swino) & ((1<<BRIDLE_FUNCTION_OFFSET)-1))

//Gets the 4-bit processor mode of the function that called BRIDLE_SYSCALL
#define BRIDLE_GET_REQ_MODE(swino)   (((swino) >> BRIDLE_TYPE_OFFSET) & 0x0f)                                       


//Protoype for a module-level SWI handler for BRIDLE. 
//The first argument is always the BRIDLE Control number, and the remaining 4 arguments represent
//the arguments p1, p2, p3, p4 passed to BRIDLE_SWI. The 5th argument swi in BRIDLE_SWI
//becomes the first argument (arg1) in pSWIModuleHandler. The arguments p1-p4 map to arg2-arg5. 
typedef uint32 (*PFNSWIHANDLER)(uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5);
                                   
#if defined(__cplusplus)
extern "C" {
#endif

extern AEE_EXPORTS boolean  bridle_IsEnabled(void);
extern AEE_EXPORTS boolean  bridle_CheckMode(int mode);
extern AEE_EXPORTS boolean  bridle_IsInUserCall(void);

extern AEE_EXPORTS uint32   bridle_UserCall(uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, void (*fn)(void));
extern AEE_EXPORTS uint32   bridle_SystemCall(uint32 parm1, uint32 parm2, uint32 parm3, uint32 parm4, uint32 swino);
extern AEE_EXPORTS int      bridle_CheckAccess(void *pBuf, int len, int aType, uint32 swino);

#if defined(__cplusplus)
}
#endif

#define BRIDLE_SYSCALL(p1, p2, p3, p4, swino) bridle_SystemCall((uint32)(p1), (uint32)(p2), (uint32)(p3), (uint32)(p4), (uint32)(swino))


// old-style 
#define BRIDLE_MAKE_SWI(subsysno,fnno) BRIDLE_SYSCALL_NO(subsysno,fnno)
#define BRIDLE_SWI(p1, p2, p3, p4, swino) BRIDLE_SYSCALL(p1, p2, p3, p4, swino)

#define BRIDLE_IS_ENABLED()                     bridle_IsEnabled()

#define BRIDLE_IS_IN_USR_MODE()                 bridle_CheckMode(BRIDLE_USR_MODE)
#define BRIDLE_IS_IN_SVC_MODE()                 bridle_CheckMode(BRIDLE_SVC_MODE)
#define BRIDLE_IS_IN_SYS_MODE()                 bridle_CheckMode(BRIDLE_SYS_MODE)

// verify no access from user mode
#define BRIDLE_CHECK_NOACCESS(swi, p, len)      bridle_CheckAccess((void *)(p),(int)(len), BRIDLE_NO_ACCESS, (swi))

// verify read access from user mode
#define BRIDLE_CHECK_READACCESS(swi, p, len)    bridle_CheckAccess((void *)(p),(int)(len), BRIDLE_READ_ACCESS, (swi))

// verify write access from user mode
#define BRIDLE_CHECK_WRITEACCESS(swi, p, len)   bridle_CheckAccess((void *)(p),(int)(len), BRIDLE_WRITE_ACCESS, (swi))

// verify read access from user mode, if pointer is non-NULL
#define BRIDLE_CHECK_READACCESS_NZ(swi, p, len) (p==0? SUCCESS: bridle_CheckAccess((void *)(p),(int)(len), BRIDLE_READ_ACCESS, (swi)))

// verify read access from user mode, if pointer is non-NULL
#define BRIDLE_CHECK_WRITEACCESS_NZ(swi, p, len)(p==0? SUCCESS: bridle_CheckAccess((void *)(p),(int)(len), BRIDLE_WRITE_ACCESS, (swi)))

// don't verify access
#define BRIDLE_CHECK_NONE(swi, p, len)          bridle_Check_None()
static __inline int bridle_Check_None( )
{
   return SUCCESS;
}

// verify IOCtl (see AEEIOCtl.h)
#define BRIDLE_CHECK_IOCTL(swi, opt, p)         ( (BREWIOCTL_DIR((opt)) == BREWIOCTLD_R)?BRIDLE_CHECK_READACCESS(swi,(p),BREWIOCTL_SIZE(opt)): \
                                                   ((BREWIOCTL_DIR((opt)) == BREWIOCTLD_RW)?BRIDLE_CHECK_WRITEACCESS(swi,(p),BREWIOCTL_SIZE(opt)):SUCCESS) )

// verify a string for read access
#define BRIDLE_CHECK_READ_STRING_ACCESS(swi, p, unused) bridle_CheckAccess((void *)(p),0, BRIDLE_READ_STRING_ACCESS, (swi))

// verify that a specific value was passed, for example a specific callback pointer
#define BRIDLE_CHECK_VALUE(swi, v, V) \
   (((uint32)(v) == (uint32)(V)) ? SUCCESS : BRIDLE_CHECK_WRITEACCESS((swi), 0, 4))

// verify that a specific value was passed, for example a specific non-NULL callback pointer
#define BRIDLE_CHECK_VALUE_NZ(swi, v, V) ((v)==0 ? SUCCESS : BRIDLE_CHECK_VALUE(swi, v, V))

// This macro verifies that an AEEHandle (h) refers to a valid entry
// in the AEEInstanceList (pIL), and if so converts it to the
// corresponding OEMINSTANCE.
#define BRIDLE_CHECK_HANDLE(swi, h, pIL) bridle_Check_Handle((uint32) swi, (uint32*) &h, (uint32) pIL)
static __inline int bridle_Check_Handle(  uint32 swi, 
                                          uint32 *h, 
                                          uint32 pIL)
{
   return ((TRUE == AEEHandle_From((AEEInstanceList *) pIL, (uint32)(*h), (OEMINSTANCE*)h)) ? SUCCESS : BRIDLE_CHECK_WRITEACCESS((swi), 0, 4));
}

// This macro verifies that an AEEkCB (kcb) refers to a valid handle,
// and if so converts it to the corresponding AEECallback*.
#define BRIDLE_CHECK_AEEKCB(swi, kcb, unused) bridle_Check_AEEKCB((uint32) swi, (hAEEkCB*)&kcb)
static __inline int bridle_Check_AEEKCB(  uint32 swi, 
                                          hAEEkCB *kcb)
{
   return ((SUCCESS == AEEkCB_ToAEECallback((*kcb), (AEECallback**)kcb)) ? SUCCESS : BRIDLE_CHECK_WRITEACCESS((swi), 0, 4));
}


// These are degenerate now, but will deal with virtual memory in bridle phase II
#define COPY_TO_USER(to, from, len)          MEMCPY(to, from, len)
#define COPY_FROM_USER(to, from, len)        MEMCPY(to, from, len)

// Check the user pointer for access, and then copy
#define BRIDLE_COPY_TO_USER(to, from, len)   BRIDLE_CHECK_WRITEACCESS(BRIDLE_FROM_UNPRIV, to, len); \
                                             COPY_TO_USER(to, from, len)
#define BRIDLE_COPY_FROM_USER(to, from, len) BRIDLE_CHECK_READACCESS(BRIDLE_FROM_UNPRIV, from, len); \
                                             COPY_FROM_USER(to, from, len)

// A funtion pointer is unsafe if 1) bridle is on, 2) we are in supervisor
// mode, and 3) the function pointer is user-writeable
#define BRIDLE_UNSAFE_PFN(pfn) (bridle_IsEnabled() && \
                                BRIDLE_IS_IN_SVC_MODE() && \
                                (SUCCESS == BRIDLE_CHECK_WRITEACCESS(0, pfn, 4)))

// ASSERT that a function pointer is safe
#define BRIDLE_ASSERT_SAFE_PFN(pfn) \
   BRIDLE_UNSAFE_PFN(pfn) ? BRIDLE_CHECK_WRITEACCESS(BRIDLE_FROM_UNPRIV, 0, 4) : SUCCESS

static __inline uint32 bridle_generic_swihandler(uint32 arg1, 
                                                 uint32 arg2, 
                                                 uint32 arg3, 
                                                 uint32 arg4, 
                                                 uint32 arg5,
                                                 uint32 maxfnnum, 
                                                 const PFNSWIHANDLER ptrTable[])
{
   uint32 fn_no; 
   
   fn_no = (arg1 >>BRIDLE_FUNCTION_OFFSET) & 0xff;
   if(fn_no < maxfnnum) {
      return ptrTable[fn_no](arg1, arg2, arg3, arg4, arg5);
   }
   return 0;
}

// useful for packing more than four parameters into four parameters to traverse the SWI
static __inline uint32 packbridle(uint32 swi, uint32 a1, uint32 a2, uint32 a3, int x, ...)
{
   va_list ap;
   int i;
   uint32 pu[16];

   va_start(ap, x);
   for (i = 0; i < x; ++i) {
      pu[i] = va_arg(ap, uint32);
   }
   va_end(ap);

   return bridle_SystemCall(a1, a2, a3, (uint32)pu, swi);
} 




//
// PACKBRIDLEX_* macros - generates an inline for the user mode invocation of the SWI
//
// PACKBRIDLEX_RET(rt,f,t1,...,tn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    rt - return type (e.g. int32)
//    f  - system function (e.g. OEMSocket_Open)
//    t1 - type of first arg (e.g. byte)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_SOCKET)
//
// PACKBRIDLEX_VOID(f,t1,...,tn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    f  - system function (e.g. OEMFS_RegRmtAccessChk)
//    t1 - type of first arg (e.g. byte)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_FORCE_DORMANCY)
//
// NOTE: If more macros are added to support greater than 16 parameters, you
//       must also visit the declaration of pu in the packbridle() function above.
//       This array must be enlarged to support the additional parameters.
//
#define PACKBRIDLE0_RET(rt,f,i,n) \
static __inline rt packbridle_##f(void) \
{ \
   return (rt)BRIDLE_SYSCALL(0,0,0,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE0_VOID(f,i,n) \
static __inline void packbridle_##f(void) \
{ \
   (void)BRIDLE_SYSCALL(0,0,0,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE1_RET(rt,f,t1,i,n) \
static __inline rt packbridle_##f(t1 a1) \
{ \
   return (rt)BRIDLE_SYSCALL((uint32)a1,0,0,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE1_VOID(f,t1,i,n) \
static __inline void packbridle_##f(t1 a1) \
{ \
   (void)BRIDLE_SYSCALL((uint32)a1,0,0,0,BRIDLE_SYSCALL_NO(i,n)); \
}
#define PACKBRIDLE2_RET(rt,f,t1,t2,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2) \
{ \
   return (rt)BRIDLE_SYSCALL((uint32)a1,(uint32)a2,0,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE2_VOID(f,t1,t2,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2) \
{ \
   (void)BRIDLE_SYSCALL((uint32)a1,(uint32)a2,0,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE3_RET(rt,f,t1,t2,t3,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3) \
{ \
   return (rt)BRIDLE_SYSCALL((uint32)a1,(uint32)a2,(uint32)a3,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE3_VOID(f,t1,t2,t3,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2,t3 a3) \
{ \
   (void)BRIDLE_SYSCALL((uint32)a1,(uint32)a2,(uint32)a3,0,BRIDLE_SYSCALL_NO(i,n)); \
} 
#define PACKBRIDLE4_RET(rt,f,t1,t2,t3,t4,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,1,(uint32)a4); \
} 
#define PACKBRIDLE4_VOID(f,t1,t2,t3,t4,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4) \
{ \
   (void)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,1,(uint32)a4); \
} 
#define PACKBRIDLE5_RET(rt,f,t1,t2,t3,t4,t5,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,2,(uint32)a4,(uint32)a5); \
} 
#define PACKBRIDLE5_VOID(f,t1,t2,t3,t4,t5,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5) \
{ \
   (void)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,2,(uint32)a4,(uint32)a5); \
} 
#define PACKBRIDLE6_RET(rt,f,t1,t2,t3,t4,t5,t6,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,3,(uint32)a4,(uint32)a5,(uint32)a6); \
} 
#define PACKBRIDLE6_VOID(f,t1,t2,t3,t4,t5,t6,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6) \
{ \
   (void)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,3,(uint32)a4,(uint32)a5,(uint32)a6); \
} 
#define PACKBRIDLE7_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,4,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7); \
} 
#define PACKBRIDLE7_VOID(f,t1,t2,t3,t4,t5,t6,t7,i,n) \
static __inline void packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7) \
{ \
   (void)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,4,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7); \
} 
#define PACKBRIDLE8_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,5,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8); \
} 
#define PACKBRIDLE8_VOID(f,t1,t2,t3,t4,t5,t6,t7,t8,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8) \
{ \
   (void)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,5,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8); \
} 
#define PACKBRIDLE9_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9) \
{ \
   return (rt)packbridle(BRIDLE_MAKE_SWI(i,n),(uint32)a1,(uint32)a2,(uint32)a3,6,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9); \
} 
#define PACKBRIDLE10_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10) \
{ \
   return (rt)packbridle(BRIDLE_MAKE_SWI(i,n),(uint32)a1,(uint32)a2,(uint32)a3,7,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10); \
} 
#define PACKBRIDLE11_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11) \
{ \
   return (rt)packbridle(BRIDLE_MAKE_SWI(i,n),(uint32)a1,(uint32)a2,(uint32)a3,8,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11); \
} 
#define PACKBRIDLE12_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11,t12 a12) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,9,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11,(uint32)a12); \
} 
#define PACKBRIDLE13_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11,t12 a12,t13 a13) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,10,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11,(uint32)a12,(uint32)a13); \
} 
#define PACKBRIDLE14_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11,t12 a12,t13 a13,t14 a14) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,11,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11,(uint32)a12,(uint32)a13,(uint32)a14); \
} 
#define PACKBRIDLE15_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11,t12 a12,t13 a13,t14 a14,t15 a15) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,12,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11,(uint32)a12,(uint32)a13,(uint32)a14,(uint32)a15); \
} 
#define PACKBRIDLE16_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,i,n) \
static __inline rt packbridle_##f(t1 a1,t2 a2,t3 a3,t4 a4,t5 a5,t6 a6,t7 a7,t8 a8,t9 a9,t10 a10,t11 a11,t12 a12,t13 a13,t14 a14,t15 a15,t16 a16) \
{ \
   return (rt)packbridle(BRIDLE_SYSCALL_NO(i,n),(uint32)a1,(uint32)a2,(uint32)a3,13,(uint32)a4,(uint32)a5,(uint32)a6,(uint32)a7,(uint32)a8,(uint32)a9,(uint32)a10,(uint32)a11,(uint32)a12,(uint32)a13,(uint32)a14,(uint32)a15,(uint32)a16); \
} 

//
// UNPACKBRIDLE_PARAM_X macros - used for accessing packed parameters
//   (also useful for validating a pointer whose length is specified by another parameter)
#define UNPACKBRIDLE_PARAM_1  (a1)
#define UNPACKBRIDLE_PARAM_2  (a2)
#define UNPACKBRIDLE_PARAM_3  (a3)
#define UNPACKBRIDLE_PARAM_4  (((uint32*)a4)[0])
#define UNPACKBRIDLE_PARAM_5  (((uint32*)a4)[1])
#define UNPACKBRIDLE_PARAM_6  (((uint32*)a4)[2])
#define UNPACKBRIDLE_PARAM_7  (((uint32*)a4)[3])
#define UNPACKBRIDLE_PARAM_8  (((uint32*)a4)[4])
#define UNPACKBRIDLE_PARAM_9  (((uint32*)a4)[5])
#define UNPACKBRIDLE_PARAM_10 (((uint32*)a4)[6])
#define UNPACKBRIDLE_PARAM_11 (((uint32*)a4)[7])
#define UNPACKBRIDLE_PARAM_12 (((uint32*)a4)[8])
#define UNPACKBRIDLE_PARAM_13 (((uint32*)a4)[9])
#define UNPACKBRIDLE_PARAM_14 (((uint32*)a4)[10])
#define UNPACKBRIDLE_PARAM_15 (((uint32*)a4)[11])
#define UNPACKBRIDLE_PARAM_16 (((uint32*)a4)[12])

//
// UNPACKBRIDLEX_* macros - generates an inline for the SWI to invoke
//   Note that there are very few cases in which these would need to
//   be used by themselves - instead use the BRIDLEX_* macros below.
//
// UNPACKBRIDLEX_RET(rt,f,t1,v1,n1,...,tn,vn,nn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    rt - return type (e.g. int32)
//    f  - system function (e.g. OEMSocket_Open)
//    t1 - type of first arg (e.g. byte)
//    v1 - first arg verification function (e.g. BRIDLE_CHECK_NONE)
//    n1 - first number of bytes to verify (e.g. 0)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    vn - nth arg verification function (e.g. BRIDLE_CHECK_WRITEACCESS)
//    nn - nth number of bytes to verify (e.g. sizeof(uint16))
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_SOCKET)
//
// UNPACKBRIDLEX_VOID(f,t1,...,tn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    f  - system function (e.g. OEMFS_RegRmtAccessChk)
//    t1 - type of first arg (e.g. byte)
//    v1 - first arg verification function (e.g. BRIDLE_CHECK_NONE)
//    n1 - first number of bytes to verify (e.g. 0)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    vn - nth arg verification function (e.g. BRIDLE_CHECK_WRITEACCESS)
//    nn - nth number of bytes to verify (e.g. sizeof(uint16))
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_FORCE_DORMANCY)
//
#define UNPACKBRIDLE0_RET(rt,f,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   return (uint32)f(); \
}
#define UNPACKBRIDLE0_VOID(f,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   f(); \
   return 0; \
}
#define UNPACKBRIDLE1_RET(rt,f,t1,v1,n1,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1); \
}
#define UNPACKBRIDLE1_VOID(f,t1,v1,n1,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   f((t1)UNPACKBRIDLE_PARAM_1); \
   return 0; \
}
#define UNPACKBRIDLE2_RET(rt,f,t1,v1,n1,t2,v2,n2,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2); \
}
#define UNPACKBRIDLE2_VOID(f,t1,v1,n1,t2,v2,n2,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2); \
   return 0; \
}
#define UNPACKBRIDLE3_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3); \
}
#define UNPACKBRIDLE3_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3); \
   return 0; \
}
#define UNPACKBRIDLE4_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4); \
}
#define UNPACKBRIDLE4_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
     (t4)UNPACKBRIDLE_PARAM_4); \
   return 0; \
}
#define UNPACKBRIDLE5_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5); \
}
#define UNPACKBRIDLE5_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
     (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5); \
   return 0; \
}
#define UNPACKBRIDLE6_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6); \
}
#define UNPACKBRIDLE6_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{  \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
     (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6); \
   return 0; \
}
#define UNPACKBRIDLE7_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7); \
}
#define UNPACKBRIDLE7_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
     (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
     (t7)UNPACKBRIDLE_PARAM_7); \
   return 0; \
}
#define UNPACKBRIDLE8_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8); \
}
#define UNPACKBRIDLE9_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9); \
}
#define UNPACKBRIDLE10_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10); \
}
#define UNPACKBRIDLE11_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11); \
}
#define UNPACKBRIDLE12_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   v12(swi,UNPACKBRIDLE_PARAM_12,n12); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11,(t12)UNPACKBRIDLE_PARAM_12); \
}
#define UNPACKBRIDLE13_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   v12(swi,UNPACKBRIDLE_PARAM_12,n12); \
   v13(swi,UNPACKBRIDLE_PARAM_13,n13); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11,(t12)UNPACKBRIDLE_PARAM_12, \
                    (t13)UNPACKBRIDLE_PARAM_13); \
}
#define UNPACKBRIDLE14_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   v12(swi,UNPACKBRIDLE_PARAM_12,n12); \
   v13(swi,UNPACKBRIDLE_PARAM_13,n13); \
   v14(swi,UNPACKBRIDLE_PARAM_14,n14); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11,(t12)UNPACKBRIDLE_PARAM_12, \
                    (t13)UNPACKBRIDLE_PARAM_13,(t14)UNPACKBRIDLE_PARAM_14); \
}
#define UNPACKBRIDLE15_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   v12(swi,UNPACKBRIDLE_PARAM_12,n12); \
   v13(swi,UNPACKBRIDLE_PARAM_13,n13); \
   v14(swi,UNPACKBRIDLE_PARAM_14,n14); \
   v15(swi,UNPACKBRIDLE_PARAM_15,n15); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11,(t12)UNPACKBRIDLE_PARAM_12, \
                    (t13)UNPACKBRIDLE_PARAM_13,(t14)UNPACKBRIDLE_PARAM_14,(t15)UNPACKBRIDLE_PARAM_15); \
}
#define UNPACKBRIDLE16_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,t16,v16,n16,i,n) \
static __inline uint32 unpackbridle_##f(uint32 swi,uint32 a1,uint32 a2,uint32 a3,uint32 a4) \
{ \
   v1(swi,UNPACKBRIDLE_PARAM_1,n1); \
   v2(swi,UNPACKBRIDLE_PARAM_2,n2); \
   v3(swi,UNPACKBRIDLE_PARAM_3,n3); \
   v4(swi,UNPACKBRIDLE_PARAM_4,n4); \
   v5(swi,UNPACKBRIDLE_PARAM_5,n5); \
   v6(swi,UNPACKBRIDLE_PARAM_6,n6); \
   v7(swi,UNPACKBRIDLE_PARAM_7,n7); \
   v8(swi,UNPACKBRIDLE_PARAM_8,n8); \
   v9(swi,UNPACKBRIDLE_PARAM_9,n9); \
   v10(swi,UNPACKBRIDLE_PARAM_10,n10); \
   v11(swi,UNPACKBRIDLE_PARAM_11,n11); \
   v12(swi,UNPACKBRIDLE_PARAM_12,n12); \
   v13(swi,UNPACKBRIDLE_PARAM_13,n13); \
   v14(swi,UNPACKBRIDLE_PARAM_14,n14); \
   v15(swi,UNPACKBRIDLE_PARAM_15,n15); \
   v16(swi,UNPACKBRIDLE_PARAM_16,n16); \
   return (uint32)f((t1)UNPACKBRIDLE_PARAM_1,(t2)UNPACKBRIDLE_PARAM_2,(t3)UNPACKBRIDLE_PARAM_3, \
                    (t4)UNPACKBRIDLE_PARAM_4,(t5)UNPACKBRIDLE_PARAM_5,(t6)UNPACKBRIDLE_PARAM_6, \
                    (t7)UNPACKBRIDLE_PARAM_7,(t8)UNPACKBRIDLE_PARAM_8,(t9)UNPACKBRIDLE_PARAM_9, \
                    (t10)UNPACKBRIDLE_PARAM_10,(t11)UNPACKBRIDLE_PARAM_11,(t12)UNPACKBRIDLE_PARAM_12, \
                    (t13)UNPACKBRIDLE_PARAM_13,(t14)UNPACKBRIDLE_PARAM_14,(t15)UNPACKBRIDLE_PARAM_15, \
                    (t16)UNPACKBRIDLE_PARAM_16); \
}


//
// BRIDLEX_* macros - generates both PACKBRIDLE and UNPACKBRIDLE inlines
//
// BRIDLEX_RET(rt,f,t1,v1,n1,...,tn,vn,nn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    rt - return type (e.g. int32)
//    f  - system function (e.g. OEMSocket_Open)
//    t1 - type of first arg (e.g. byte)
//    v1 - first arg verification function (e.g. BRIDLE_CHECK_NONE)
//    n1 - first number of bytes to verify (e.g. 0)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    vn - nth arg verification function (e.g. BRIDLE_CHECK_WRITEACCESS)
//    nn - nth number of bytes to verify (e.g. sizeof(uint16))
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_SOCKET)
//
// BRIDLEX_VOID(f,t1,...,tn,i,n)
//    X  - number of paramters to deal with (e.g. 4)
//    f  - system function (e.g. OEMFS_RegRmtAccessChk)
//    t1 - type of first arg (e.g. byte)
//    v1 - first arg verification function (e.g. BRIDLE_CHECK_NONE)
//    n1 - first number of bytes to verify (e.g. 0)
//    ...
//    tn - type of nth arg (e.g. uint16*)
//    vn - nth arg verification function (e.g. BRIDLE_CHECK_WRITEACCESS)
//    nn - nth number of bytes to verify (e.g. sizeof(uint16))
//    i  - swi number (e.g. bridle_sock_swi)
//    n  - swi index (e.g. BRIDLE_SOCK_FORCE_DORMANCY)
//
#define BRIDLE0_RET(rt,f,i,n) \
PACKBRIDLE0_RET(rt,f,i,n) \
UNPACKBRIDLE0_RET(rt,f,i,n) 

#define BRIDLE0_VOID(f,i,n) \
PACKBRIDLE0_VOID(f,i,n) \
UNPACKBRIDLE0_VOID(f,i,n)
 
#define BRIDLE1_RET(rt,f,t1,v1,n1,i,n) \
PACKBRIDLE1_RET(rt,f,t1,i,n) \
UNPACKBRIDLE1_RET(rt,f,t1,v1,n1,i,n) 

#define BRIDLE1_VOID(f,t1,v1,n1,i,n) \
PACKBRIDLE1_VOID(f,t1,i,n) \
UNPACKBRIDLE1_VOID(f,t1,v1,n1,i,n) 

#define BRIDLE2_RET(rt,f,t1,v1,n1,t2,v2,n2,i,n) \
PACKBRIDLE2_RET(rt,f,t1,t2,i,n) \
UNPACKBRIDLE2_RET(rt,f,t1,v1,n1,t2,v2,n2,i,n) 

#define BRIDLE2_VOID(f,t1,v1,n1,t2,v2,n2,i,n) \
PACKBRIDLE2_VOID(f,t1,t2,i,n) \
UNPACKBRIDLE2_VOID(f,t1,v1,n1,t2,v2,n2,i,n) 

#define BRIDLE3_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) \
PACKBRIDLE3_RET(rt,f,t1,t2,t3,i,n) \
UNPACKBRIDLE3_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) 

#define BRIDLE3_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) \
PACKBRIDLE3_VOID(f,t1,t2,t3,i,n) \
UNPACKBRIDLE3_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,i,n) 

#define BRIDLE4_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) \
PACKBRIDLE4_RET(rt,f,t1,t2,t3,t4,i,n) \
UNPACKBRIDLE4_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) 

#define BRIDLE4_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) \
PACKBRIDLE4_VOID(f,t1,t2,t3,t4,i,n) \
UNPACKBRIDLE4_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,i,n) 

#define BRIDLE5_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) \
PACKBRIDLE5_RET(rt,f,t1,t2,t3,t4,t5,i,n) \
UNPACKBRIDLE5_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) 

#define BRIDLE5_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) \
PACKBRIDLE5_VOID(f,t1,t2,t3,t4,t5,i,n) \
UNPACKBRIDLE5_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,i,n) 

#define BRIDLE6_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) \
PACKBRIDLE6_RET(rt,f,t1,t2,t3,t4,t5,t6,i,n) \
UNPACKBRIDLE6_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) 

#define BRIDLE6_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) \
PACKBRIDLE6_VOID(f,t1,t2,t3,t4,t5,t6,i,n) \
UNPACKBRIDLE6_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,i,n) 

#define BRIDLE7_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) \
PACKBRIDLE7_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,i,n) \
UNPACKBRIDLE7_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) 

#define BRIDLE7_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) \
PACKBRIDLE7_VOID(f,t1,t2,t3,t4,t5,t6,t7,i,n) \
UNPACKBRIDLE7_VOID(f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,i,n) 

#define BRIDLE8_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,i,n) \
PACKBRIDLE8_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,i,n) \
UNPACKBRIDLE8_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,i,n) 

#define BRIDLE9_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,i,n) \
PACKBRIDLE9_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,i,n) \
UNPACKBRIDLE9_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,i,n) 

#define BRIDLE10_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,i,n) \
PACKBRIDLE10_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,i,n) \
UNPACKBRIDLE10_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,i,n) 

#define BRIDLE11_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,i,n) \
PACKBRIDLE11_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,i,n) \
UNPACKBRIDLE11_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,i,n) 

#define BRIDLE12_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,i,n) \
PACKBRIDLE12_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,i,n) \
UNPACKBRIDLE12_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,i,n) 

#define BRIDLE13_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,i,n) \
PACKBRIDLE13_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,i,n) \
UNPACKBRIDLE13_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,i,n) 

#define BRIDLE14_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,i,n) \
PACKBRIDLE14_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,i,n) \
UNPACKBRIDLE14_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,i,n) 

#define BRIDLE15_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,i,n) \
PACKBRIDLE15_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,i,n) \
UNPACKBRIDLE15_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,i,n) 

#define BRIDLE16_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,t16,v16,n16,i,n) \
PACKBRIDLE16_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,i,n) \
UNPACKBRIDLE16_RET(rt,f,t1,v1,n1,t2,v2,n2,t3,v3,n3,t4,v4,n4,t5,v5,n5,t6,v6,n6,t7,v7,n7,t8,v8,n8,t9,v9,n9,t10,v10,n10,t11,v11,n11,t12,v12,n12,t13,v13,n13,t14,v14,n14,t15,v15,n15,t16,v16,n16,i,n) 


//
// BRIDLE_SUBSYS macros - useful for building typical bridle subsystems
//
//

// construct a constant name that's subsystem+function specific, to be stuck 
// in an enum { };, used for initializing the subsystem's SWI handler table,
// and naming sys calls
#define BRIDLE_SUBSYS_HANDLER_IDX(name, func) BRIDLE_##name##_##func

// declares a subsystem-specific number name
#define BRIDLE_SUBSYS_NO(name) g_bridle_##name##_subsys_no

// names the subsystem's handler table
#define BRIDLE_SUBSYS_HANDLER_TABLE(name) g_##name##_handlers

// declares name the subsystem's SWI handler table
#define BRIDLE_DECLARE_SUBSYS_HANDLER_TABLE(name) \
     static PFNSWIHANDLER BRIDLE_SUBSYS_HANDLER_TABLE(name)[BRIDLE_##name##_COUNT]

// the default implementation of the subsystem's top-level SWI handler
// would look like this
#define BRIDLE_DECLARE_GENERIC_SUBSYS_SWIHANDLER(name) \
static uint32 bridle_##name##_swihandler(uint32 swi, uint32 a1, uint32 a2, uint32 a3, uint32 a4)\
{\
   return bridle_generic_swihandler(swi, a1, a2, a3, a4, \
                                    ARRAY_SIZE(BRIDLE_SUBSYS_HANDLER_TABLE(name)), \
                                    BRIDLE_SUBSYS_HANDLER_TABLE(name));\
}

// initializes one entry in the subsystems SWI handler table
#define BRIDLE_INIT_SUBSYS_HANDLER_TABLE(name, func) \
   BRIDLE_SUBSYS_HANDLER_TABLE(name)[BRIDLE_SUBSYS_HANDLER_IDX(name,func)] = unpackbridle_##func;


// PACKBRIDLE_SUBSYS_X are just like PACKBRIDLEX_ macros, only without the 
// resulting subsystem replicode, assuming use above BRIDLE_SUBSYS_ macros
#define PACKBRIDLE_SUBSYS_0_RET(name,rt,f) \
   PACKBRIDLE0_RET(rt,f,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_0_VOID(name,f) \
   PACKBRIDLE0_VOID(f,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_1_RET(name,rt,f,t1) \
   PACKBRIDLE1_RET(rt,f,t1,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_1_VOID(name,f,t1) \
   PACKBRIDLE1_VOID(f,t1,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_2_RET(name,rt,f,t1,t2) \
   PACKBRIDLE2_RET(rt,f,t1,t2,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_2_VOID(name,f,t1,t2) \
   PACKBRIDLE2_VOID(f,t1,t2,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_3_RET(name,rt,f,t1,t2,t3) \
   PACKBRIDLE3_RET(rt,f,t1,t2,t3,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_3_VOID(name,f,t1,t2,t3) \
   PACKBRIDLE3_VOID(f,t1,t2,t3,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_4_RET(name,rt,f,t1,t2,t3,t4) \
   PACKBRIDLE4_RET(rt,f,t1,t2,t3,t4,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_4_VOID(name,f,t1,t2,t3,t4) \
   PACKBRIDLE4_VOID(f,t1,t2,t3,t4,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_5_RET(name,rt,f,t1,t2,t3,t4,t5) \
   PACKBRIDLE5_RET(rt,f,t1,t2,t3,t4,t5,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_5_VOID(name,f,t1,t2,t3,t4,t5) \
   PACKBRIDLE5_VOID(f,t1,t2,t3,t4,t5,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_6_RET(name,rt,f,t1,t2,t3,t4,t5,t6) \
   PACKBRIDLE6_RET(rt,f,t1,t2,t3,t4,t5,t6,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_6_VOID(name,f,t1,t2,t3,t4,t5,t6) \
   PACKBRIDLE6_VOID(f,t1,t2,t3,t4,t5,t6,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_7_RET(name,rt,f,t1,t2,t3,t4,t5,t6,t7) \
   PACKBRIDLE7_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_7_VOID(name,f,t1,t2,t3,t4,t5,t6,t7) \
   PACKBRIDLE7_VOID(f,t1,t2,t3,t4,t5,t6,t7,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_8_RET(name,rt,f,t1,t2,t3,t4,t5,t6,t7,t8) \
   PACKBRIDLE8_RET(rt,f,t1,t2,t3,t4,t5,t6,t7,t8,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define PACKBRIDLE_SUBSYS_8_VOID(name,f,t1,t2,t3,t4,t5,t6,t7,t8) \
   PACKBRIDLE8_VOID(f,t1,t2,t3,t4,t5,t6,t7,t8,BRIDLE_SUBSYS_NO(name),BRIDLE_SUBSYS_HANDLER_IDX(name,f))


// BRIDLE_SUBSYS_X are just like BRIDLEX_ macros, only without the 
//  resulting subsystem replicode, assuming use above BRIDLE_SUBSYS_ 
//  macros
#define BRIDLE_SUBSYS_0_RET(name, rt, f) \
   BRIDLE0_RET(rt, f, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_0_VOID(name, f) \
   BRIDLE0_VOID(f, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_1_RET(name, rt, f, t1, v1, n1) \
   BRIDLE1_RET(rt, f, t1, v1, n1, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_1_VOID(name, f, t1, v1, n1) \
   BRIDLE1_VOID(f, t1, v1, n1, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_2_RET(name, rt, f, t1, v1, n1, t2, v2, n2) \
   BRIDLE2_RET(rt, f, t1, v1, n1, t2, v2, n2, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_2_VOID(name, f, t1, v1, n1, t2, v2, n2) \
   BRIDLE2_VOID(f, t1, v1, n1, t2, v2, n2, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_3_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3) \
   BRIDLE3_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_3_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3) \
   BRIDLE3_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_4_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4) \
   BRIDLE4_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_4_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4) \
   BRIDLE4_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_5_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5) \
   BRIDLE5_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_5_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5) \
   BRIDLE5_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_6_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6) \
   BRIDLE6_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_6_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6) \
   BRIDLE6_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_7_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7) \
   BRIDLE7_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_7_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7) \
   BRIDLE7_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_8_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8) \
   BRIDLE8_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_8_VOID(name, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8) \
   BRIDLE8_VOID(f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_9_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9) \
   BRIDLE9_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_10_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10) \
   BRIDLE10_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_11_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11) \
   BRIDLE11_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_12_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12) \
   BRIDLE12_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12,BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_13_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11,  t12, v12, n12, t13, v13, n13) \
   BRIDLE13_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12, t13, v13, n13, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_14_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11,  t12, v12, n12, t13, v13, n13, t14, v14, n14) \
   BRIDLE14_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12, t13, v13, n13, t14, v14, n14, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_15_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11,  t12, v12, n12, t13, v13, n13, t14, v14, n14, t15, v15, n15) \
   BRIDLE15_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12, t13, v13, n13, t14, v14, n14, t15, v15, n15, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))

#define BRIDLE_SUBSYS_16_RET(name, rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11,  t12, v12, n12, t13, v13, n13, t14, v14, n14, t15, v15, n15, t16, v16, n16) \
   BRIDLE16_RET(rt, f, t1, v1, n1, t2, v2, n2, t3, v3, n3, t4, v4, n4, t5, v5, n5, t6, v6, n6, t7, v7, n7, t8, v8, n8, t9, v9, n9, t10, v10, n10, t11, v11, n11, t12, v12, n12, t13, v13, n13, t14, v14, n14, t15, v15, n15, t16, v16, n16, BRIDLE_SUBSYS_NO(name), BRIDLE_SUBSYS_HANDLER_IDX(name,f))


#define BRIDLE_USER_VOID_0(__f)                         \
   (0 ? __f() :                                         \
    (void)bridle_UserCall(0,                            \
                              0,                        \
                              0,                        \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_VOID_1(__f,__a1)                    \
   (0 ? __f(__a1) :                                     \
    (void)bridle_UserCall((uint32)__a1,                 \
                              0,                        \
                              0,                        \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_VOID_2(__f,__a1,__a2)               \
   (0 ? __f(__a1,__a2) :                                \
    (void)bridle_UserCall((uint32)__a1,                 \
                              (uint32)__a2,             \
                              0,                        \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_VOID_3(__f,__a1,__a2,__a3)          \
   (0 ? __f(__a1,__a2,__a3) :                           \
    (void)bridle_UserCall((uint32)__a1,                 \
                              (uint32)__a2,             \
                              (uint32)__a3,             \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_VOID_4(__f,__a1,__a2,__a3,__a4)     \
   (0 ? __f(__a1,__a2,__a3,__a4) :                      \
    (void)bridle_UserCall((uint32)__a1,                 \
                              (uint32)__a2,             \
                              (uint32)__a3,             \
                              (uint32)__a4,             \
                              (void (*)(void))__f))


#define BRIDLE_USER_RET_0(__rt,__f)                     \
   (0 ? __f() :                                         \
    (__rt)bridle_UserCall(0,                            \
                              0,                        \
                              0,                        \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_RET_1(__rt,__f,__a1)                \
   (0 ? __f(__a1) :                                     \
    (__rt)bridle_UserCall((uint32)__a1,                 \
                              0,                        \
                              0,                        \
                              0,                        \
                              (void (*)(void))__f))

#define BRIDLE_USER_RET_2(__rt,__f,__a1,__a2)            \
   (0 ? __f(__a1,__a2) :                                 \
    (__rt)bridle_UserCall((uint32)__a1,                  \
                              (uint32)__a2,              \
                              0,                         \
                              0,                         \
                              (void (*)(void))__f))

#define BRIDLE_USER_RET_3(__rt,__f,__a1,__a2,__a3)       \
   (0 ? __f(__a1,__a2,__a3) :                            \
    (__rt)bridle_UserCall((uint32)__a1,                  \
                              (uint32)__a2,              \
                              (uint32)__a3,              \
                              0,                         \
                              (void (*)(void))__f))

#define BRIDLE_USER_RET_4(__rt,__f,__a1,__a2,__a3,__a4)  \
   (0 ? __f(__a1,__a2,__a3,__a4) :                       \
    (__rt)bridle_UserCall((uint32)__a1,                  \
                              (uint32)__a2,              \
                              (uint32)__a3,              \
                              (uint32)__a4,              \
                              (void (*)(void))__f))




#endif //AEEBRIDLE_H_
