Subject: Re: ORCA/Pascal Question
From: [email-masked] (Neil Parker)
Date: Fri, 2 Oct 92 01:06:35 GMT
Newsgroups: comp.sys.apple2

In article <[email-masked]> [email-masked] (Theo Schneider) writes:
>Hi there,
>i has an problem with an Assembler Routine in ORCA/Pascal.
> 
>The Routine is in short
> 
>theRout  start
>         sub
> 
>         emulation           ;Macro to set to emulation mode
>         native long         ;Macro to set to native mode
> 
>         ret
>         end
> 
>In the Pascal source the Routine is call with :
> 
>procedure theRout; extern
> 
>begin
>  theRout;
>end.
> 
>The Macros are from 'M16.Util', so i think there are correct.
> 
>Now when i execute my small programm it crash, when i remove the
>emulation and the native macro all work fine.
> 
>Can anybody explain me how can i go to emulation mode and back without
>an crash ??
> 
>Thank's for any help

Going into emulation mode safely requires a lot more than just setting the
CPU's e-bit.  The first and biggest problem is that when you go into
emulation mode, the stack pointer is forced into the $100-$1FF range, no
matter where is was previously, and the native-mode stack pointer is not
automatically saved anywhere.  You need to save the native-mode stack
pointer yourself before going into emulation mode, and restore it after you
come back.

Second, unless your native-mode code is already running in bank 0, you need
to disable interrupts immediately before switching to emulation mode, and
turn them back on once you're running in bank 0.  This is because
emulation-mode interrupts assume that your code is in bank 0, and if one
occurs while you're in emulation mode in some other bank, all heck will
break loose when the interrupt handler tries to return to non-existant bank
0 code.

The _Apple_IIGS_Firmware_Reference_ includes a sample routine that takes
care of these concerns.

Have you considered simply loading your emulation-mode code into bank 0
and calling it with FWEntry?

               - Neil Parker
--
Neil Parker                 No cute ASCII art...no cute quote...no cute
[email-masked]     disclaimer...no deposit, no return...
[email-masked]   (This space intentionally left blank:           )