llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
269 stars 56 forks source link

A library should be written to save and restore BASIC ZP on `commodore` #343

Open mysterymath opened 3 months ago

mysterymath commented 3 months ago

For context, see #341 and #315.

Our current commodore targets clobber BASIC ZP. This does seem a reasonable default behavior for a C program: it's like BASIC, but unlike the KERNAL, so it should clobber BASIC but preserve the KERNAL. The commodore targets used to exit by returning to a possibly broken BASIC, but #341 changes them to infinitely loop instead.

It may still be desirable to be able to return to BASIC. This would either require not using the BASIC area, or saving and restoring the BASIC ZP to a buffer. The latter seems more appropriate for a C program. Accordingly, we should see if there's a way to create a library, includable with -lsave-basic, that can save and restore ZP before and after main, across the various commodore targets.

asiekierka commented 3 months ago

I agree. One small side note - perhaps the more appropriate behavior for a C= program would be to jump to the reset vector, as opposed to infinitely looping?

mysterymath commented 3 months ago

I agree. One small side note - perhaps the more appropriate behavior for a C= program would be to jump to the reset vector, as opposed to infinitely looping?

This came up in discussion, and it's only a slight preference on my part. The benefit of warm restart is that the machine is interactive on exit without a hardware reboot; the disadvantage is that any output on screen is cleared. Metaphorically, there isn't really an environment to return to if BASIC is replaced, so looping is vaguely akin to a kernel panic or exit, as it retains a message indicating what happened. This should also just be a question of defaults; it should already be relatively straightforward to provide reset on exit behavior (perhaps this would also make a good library in the SDK?).

johnwbyrd commented 3 months ago

This may be too obvious to write, but it seems natural that more platforms than Commodore could benefit from having the option to save and restore zero page. Seems like the sort of thing you might want to opt into, per platform or per project.

Personally, I feel that graceful exit should be the default on all platforms, but reasonable people may disagree.

mysterymath commented 3 months ago

If saving/restoring a ZP region can guarantee a clean exit, then:

That limits the desired applicability of the technique somewhat. It's not particularly applicable on Atari DOS programs, for example, since the program may continually interact with the DOS, and the DUP (menu) can be freely overwritten, since the DOS detects this and reloads it on exit.