Dvorak wrote:
Interesting that the BRK instruction increments PC by 2. I was wondering why PC was never at the BRK point itself! This will help me a decent amount. Would you happen to know why the BRK instruction increments PC?
The reason is mostly historical and had to due with
BRK's dual use as a means of hooking in patches in PROMs in early systems and as a means of invoking operating system services, analogous to
INT <int number> in x86 assembly language.
Quote:
It seems like BRK is half of what I want (convenience-wise), but it will do. I was wanting something which would pause the code, then allow me to step through it using VICE's monitor, but I'm becoming uncertain as to whether or not something like that actually exists. Thanks!
VICE, being software, cannot single-step a program. If you ever decide to try your hand at building a 6502 contraption you can incorporate the circuitry needed to single-step under hardware control.
Quote:
edit; found this on a 6502 instruction set sheet: "BRK causes a non-maskable interrupt and increments the program counter by one. Therefore an RTI will go to the address of the BRK +2 so that BRK may be used to replace a two-byte instruction for debugging and the subsequent RTI will be correct. " I take it this has something to do with the stack?
When
BRK is executed, the 6502 will push the program counter (PC) and status register (SR) to the stack (located in the range $0100-$01FF) and then jump through the hardware vector at $FFFE-$FFFF.
RTI will reverse the stack operations, reloading PC with the address that was pushed and restore the stack copy of SR as well. Since the execution of
BRK double-increments PC, the return address is the address of the
BRK instruction plus two.
Worthy of note is that the $FFFE-$FFFF vector is the same one taken when a hardware interrupt request (IRQ) occurs. The 6502 indicates the type of interrupt by setting the B bit in the stack copy SR if the interrupt was caused by a
BRK instruction, or by clearing the B bit if the interrupt was caused by an IRQ. Hence the type of interrupt has to be determined in software by fetching a copy of SR from the stack and masking for the B bit. The B bit is meaningless otherwise.