Page 1 of 1
BRKing a program without clearing the screen (C64)
Posted: Tue Sep 08, 2020 10:16 pm
by DanielS
I'm putting in a few BRK instructions here and there for debugging. What I want to do is print a debug message to the screen and then exit the program. However, brk clears the screen. How can I just drop out of my program and leave the screen contents intact? Re-vector the interrupt routine maybe?
Re: BRKing a program without clearing the screen
Posted: Tue Sep 08, 2020 11:09 pm
by John West
This is completely dependent on the machine you're running your code on. Commodore 64 again? Please put this in the subject, or at least in the post. My psychic powers have limits.
BRK is a software-generated interrupt. The C64's interrupt handler starts at $FF48. The first thing it does after pushing registers is look at the B flag. If it's clear it jumps through the vector CINV at $0314. If it's set (because we got here from a BRK instruction) it jumps through the vector CBINV at $0316. Point CBINV at your handler, and you can do whatever you like. Your handler will have to pop the registers before returning, and there's a handy snippet of code at $EA81 that will do it for you.
But doing that will return to your program. If you wanted to warm-start BASIC without clearing the screen, JMP ($A002) will probably do it. If you've changed any VIC registers JSR PANIC ($E5A0) will reset them.
I'm using the very useful KERNAL and BASIC source code posted by Michael Steil at
https://github.com/mist64/c64ref/blob/m ... sm_cbm.txt and
https://github.com/mist64/c64ref/blob/m ... asm_ms.txt
Re: BRKing a program without clearing the screen (C64)
Posted: Wed Sep 09, 2020 12:05 am
by DanielS
Sorry John. I edited it. Thanks for your answer.
Re: BRKing a program without clearing the screen (C64)
Posted: Wed Sep 09, 2020 5:24 am
by White Flame
Alternatively, if you use SYS from BASIC to jump into your asm program, then you can RTS out of it cleanly (assuming that you didn't muck up BASIC's zeropage variables or bank the ROMs out or anything). BASIC will still print a blank like and the READY. prompt once it returns, so if you're on the bottom of the screen it might scroll a few lines.
If you're nested somewhere inside your own subroutines and want to exit this way, you could save the stack pointer on entry with TSX, STX <somewhere>, then restore the stack pointer with TXS and RTS.