BRKing a program without clearing the screen (C64)

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
DanielS
Posts: 43
Joined: 12 Aug 2020

BRKing a program without clearing the screen (C64)

Post 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?
Last edited by DanielS on Wed Sep 09, 2020 12:04 am, edited 1 time in total.
John West
Posts: 383
Joined: 03 Sep 2002

Re: BRKing a program without clearing the screen

Post 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
DanielS
Posts: 43
Joined: 12 Aug 2020

Re: BRKing a program without clearing the screen (C64)

Post by DanielS »

Sorry John. I edited it. Thanks for your answer.
White Flame
Posts: 704
Joined: 24 Jul 2012

Re: BRKing a program without clearing the screen (C64)

Post 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.
Post Reply