BRK
From Wikipedia, the free encyclopedia
In 6502 assembly language programming, BRK is an opcode that causes a software interrupt or trap. The generalized actions of BRK are as follows:
- The B bit in the processor status register is set to indicate that a BRK instruction is being processed.
- The program counter is incremented by two and then pushed on to the stack, most significant byte first.
- The status register is pushed on to the stack.
- Maskable interrupts (IRQ) are disabled by setting the I bit in the status register.
- The program counter is loaded with the address stored in memory address 0xFFFE (LSB) and 0xFFFF (MSB—the 6502 is little endian), and execution is continued at that point.
Note that neither the accumulator or index registers are pushed on to the stack. Also, the vector (0xFFFE) through which the processor jumps in response to BRK is the same as that for an interrupt request, requiring that the IRQ service routine get a copy of the status register from the stack to check the B bit so as to determine whether the interrupt was hardware or software.
In the original 6502, the simultaneous assertion of a hardware interrupt line and execution of BRK was not accounted for in the design—the BRK instruction will be ignored in such a case. Also, the status of the decimal mode flag in the processor status register was preserved, which could potentially result in a difficult to locate bug in the interrupt handler if decimal mode happened to be enabled at the time of BRK being executed. This was fixed in the CMOS versions of the processor.