BRK - Software Break

Perform a software break

BRK forces a software interrupt. It is unaffected by the i interrupt disable flag.

The BRK instruction is a single byte instruction. However, when it is invoked the Program Counter is incremented by 2. This allows for a one-byte signature value indicating which break caused the interrupt.

Even if the signature byte is not required, it must either be there or the RTI instruction which returns control to the caller must manually decrement the return address. As this can be tricky, most operating systems require BRK to take up 2 bytes in memory.

6502, 65C02 & Emulation Mode (e=1)

The program counter is incremented by two & pushed onto the stack. The status register (with b break flag set) is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program counter is loaded with the interrupt vector at &FFFE-&FFFF.

It's up to the interrupt handler pointed to by (&FFFE) to test the b flag to determine if the interrupt was from a software (BRK) rather than a hardware (IRQ) interrupt.

 1.handler PLA       ; copy status from stack
 2         PHA       ; but don't remove it else RTI will break
 3
 4         AND #&10  ; check B flag
 5         BNE isBrk ; call break handler
 6
 7.isIRQ             ; hardware handler here
 8         RTI       ; exit hardware handler
 9
10.isBrk             ; break handler here
11         RTI       ; exit BRK handler

65802/65816 Native Mode (e=0)

The program bank register is pushed onto the stack. The program counter is incremented by two & pushed onto the stack. The status register is pushed onto the stack. The interrupt disable flag is then set, disabling interrupts. The program counter is loaded with the break vector at &00FFE6-&00FFE7.

Decimal Mode

On the 6502 the decimal d flag is not modified after a BRK is executed. On the 65C02 & 65816 the decimal d flag is reset to 0.

Flags Affected
Flags
---bdi--
bValue of P register on the stack is set
dOn 65C02, 65816 in emulation mode (e=1) reset to 0 for binary arithmetic, unchanged on 6502
iset to disable hardware IRQ interrupts
Instructions
SyntaxOpcode Available on: # of # of Addressing Mode
(hex) 6502 65C02 65816 bytes cycles
BRK 00 x x x 21 72 Stack Interrupt

Notes:

  1. BRK is 1 byte but program counter is incremented by 2 allowing for an optional parameter
  2. 65816: Add 1 cycle in 6502 emulation mode (e=1)

BRK on the BBC Micro & Acorn Electron

Example of BRK on the BBC Micro or Acorn Electron

Last modified November 5, 2021: Add instruction categories (6b74ff9)