[comp.sys.mac.programmer] inits & pascal & variables

kpottie@icarus.cs.kuleuven.ac.be (Pottie Karl) (01/31/91)

I recently found this file for writing simple inits in Pascal. It worked
fine till I tried to use variables


unit install;
interface
 procedure main;
implementation
 const
  YourTrapGoesHere = $A9E9;
 type
  long_t = array[0..0] of longint;
  long_ptr = ^long_t;
  long_hdl = ^long_ptr;
 procedure main;
  var
   beep: Handle;
   long: long_hdl;
   addr: longint;
 begin
  beep := Get1Resource('CODE', 128);
  DetachResource(beep);
  long := long_hdl(beep);
  addr := GetTrapAddress(YourTrapGoesHere);
  long^^[5] := $2F3C; { MOVE.L <addr>,-(A7) }
  long^^[6] := addr;
  SetTrapAddress(longint(beep^) + 22, YourTrapGoesHere)
 end;

end.

unit beep;
interface
        procedure main;
implementation
        procedure main;
                begin
                sysbeep(1);
                
        end;
end.


This all works fine until I try to use variables, like here:

unit beep;
interface
        procedure main;
implementation
        procedure main;
                var
                        ourParam: ParmBlkPtr;
        begin
                ourParam^.ioVRefNum := 1;

                if PBMountvol(ourParam) = noMacDskErr then
                        begin
                                SysBeep(1);
                        end
        end;
end.


How can I use variables in an init in Pascal ??

martin@cs.uchicago.edu (Charles Martin) (02/01/91)

kpottie@icarus (Pottie Karl) writes:
>I recently found this file for writing simple inits in Pascal. It worked
>fine till I tried to use variables

How odd, to see one's own code reappear after many months... that bit
of code I wrote before is buggy.  Basically, the offsets were
incorrect, which ended up overwriting the instructions that put the
address of the code into A0 and ToolScratch (which you wouldn't miss
in Pascal).  Probably also overwrote the jump to main, which is why
you crash as soon as you do anything exciting.  Here's another try.

Note: this is meant to be used with THINK Pascal code resources.
THINK Pascal adds a default header that we will mangle to make our
head patch.  All we do is push the real trap address on the stack
before entering our code segment, so the RTS puts us in the real trap
with the same arguments.

Modulo error checking, N...TrapAddress, and etc., here is the installer:

unit install;
interface
procedure main;
implementation
const trap = <...>;
type
    code_hdl = ^code_ptr;
    code_ptr = ^code_t;
    code_t = array [0..0] of integer;
procedure main;
var code: code_hdl;
    addr: longint;
begin
    code := code_hdl (Get1Resource ('CODE',128));
    DetachResource (Handle(code));
    addr := GetTrapAddress (trap);
    code^^ [5] := $2F3C; { MOVE.L #<addr>,-(A7) }
    code^^ [6] := HiWord (addr);
    code^^ [7] := LoWord (addr);
    SetTrapAddress (longint(code^)+10, trap)
end;
end.

Hope this helps!

Charles Martin // martin@cs.uchicago.edu