#include <shared/typedefs.h>

#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <exec/libraries.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>

__asm void (*own_StackSwap)(register __A0 struct StackSwapStruct *newstack,register __A6 struct Library *library);

struct StackSwapStruct stackswapstruct;

int SetNewStack(ulong size)
{
   if(!(stackswapstruct.stk_Lower = AllocMem(STACKSIZE,MEMF_PUBLIC)))
      return(FALSE);

   stackswapstruct.stk_Upper   = (ULONG)stackswapstruct.stk_Lower + STACKSIZE;
   stackswapstruct.stk_Pointer = (APTR)stackswapstruct.stk_Upper;

   /* This trick is needed, otherwise DICE will use stack when swapping... */

   own_StackSwap=(void *)((ULONG)SysBase-(ULONG)732);
   (*own_StackSwap)(&stackswapstruct,(struct Library *)SysBase);

   return(TRUE);
}

void RestoreStack(void)
{
   /* This trick is needed, otherwise DICE will use stack... */

   own_StackSwap=(void *)((ULONG)SysBase-(ULONG)732);
   (*own_StackSwap)(&stackswapstruct,(struct Library *)SysBase);

   FreeMem(stackswapstruct.stk_Lower,stackswapstruct.stk_Upper-stackswapstruct.Lower);

   Printf("%lu\n",stackswapstruct.stk_Upper-stackswapstruct.Lower);
}
