dschmenk / PLASMA

Proto Language AsSeMbler for All (formerly Apple)
MIT License
191 stars 26 forks source link

Is there a documented reentry point for command interpreter? #46

Closed leanto closed 6 years ago

leanto commented 6 years ago

I'm trying to write a program that calls into various AppleSoft routines and I'm constantly crashing into the monitor because I've screwed something up. Until now, I've just been rebooting after each crash, but I figure that there has to be a better way. I've looked through cmd.pla and, as far as I can tell, there is no way to call back into it from the monitor.

From the design section of the README, it looks like if the main loop in cmd.pla were a function, it would be callable. If that address were stored in a convenient location, like the zero page, it would make finding it easier.

Am I missing something?

Thanks!

dschmenk commented 6 years ago

Hi leanto-

Calling AppleSoft from PLASMA requires checking which zero page locations are used. The HiRes routines in particular use locations where PLASMA puts its evaluation stack. For those routines you will have to save/restore the ZP locations.

There isn't really a way to call back into PLASMA from the monitor once it's crashed. However, check out: https://github.com/dschmenk/PLASMA/blob/master/src/samplesrc/mon.pla Maybe that can work for you (hooks the CTRL-Y vector to return from the monitor).

leanto commented 6 years ago

Thanks a bunch for the response. That clears up two problems at the same time. :-)

My understanding now is that, on the Apple ][, PLASMA uses these locations (from plvmzp.inc):

Location Use
$00 - $05 unused by Plasma
$06 - $07 SRC - Temporarily used in the memcpy routine as the source address. (In PLASMA for the Apple I, this is called freemem and is used during initialization to pass the start of the heap to the command interpreter.)
$07 - $08 DST - Temporarily used in the memcpy routine as the destination address.
$09 - $BD unused by PLASMA
$BE - $BF ESGUARD - Stack guard? Seems to be unused directly.
$C0 - $CF ESTKL Evaluation stack low byte; together with ESTKH, forms a 16-bit wide stack indexed by the X register
$D0 - $DF ESTKH Evaluation stack high byte
$E0 VMZP - ?? I think this is supposed to mark the start of the zero page locations used by the VM, but it's unclear since many addresses before are also used.
$E0 - E1 IFP - Interpreter frame pointer (initialized to #$8000 on the Apple I, not sure on Apple ][.)
$E2 - E3 PP - Param pointer
$E4 IPY - Instruction pointer offset (Y register) temporary storage
$E5 ESP - Stack pointer (X register) temporary storage
$E6 JMPTMP - Always #$4C, a JMP instruction, allowing JSR JMPTMP to dispatch to the location in TMP, usually as a subroutine.
$E7 - $E8 TMP - A 16-bit temporary register.
$E9 DVSIGN (Apple I) - Sign of dividend after division
$EA DVSIGN (Apple ][ 64K) - Sign of dividend after division
$EF - FF PAGE0 - Interpreter bytecode inner loop. The following locations are part of the loop.
$EF DROP - Drops one frame of the evaluation stack, then interprets the next opcode.
$F0 NEXTOP - Interprets the next opcode.
$F1 FETCHOP - Fetch the current opcode.
$F2 - $F3 IP - Instruction pointer for PLASMA interpreter
$F4 - $F6 part of zero page routine
$F7 OPIDX - location of the lower byte of the address in JMP (OPTBL), used to dispatch into the interpreter
$F8 OPPAGE - location of the high byte of the address in JMP (OPTBL).
$F9 - $FF part of the zero page routine, but currently unused

Note that I think the comment in line 1098 of plvm01.s and line 288 of plvm02.s. have the values off by two (or I am misunderstanding something). FETCHOP should be at $F1 and IP should be at $F2 - F3.

BTW, if these notes seem mostly right, I'd be happy to copy them to a wiki page.

dschmenk commented 6 years ago

Great work! I went in and fixed the comments in the code - I forgot to update them when code golfing found a way to improve that sequence. This would be a fantastic addition to the Wiki, just make sure to tag it as the Apple ZP map. The C64 and Beeb will have different values.

Thanks!

Dave...