Signature byte: Difference between revisions

From SNESdev Wiki
Jump to navigationJump to search
(some people think BRK is a one-byte instruction)
(→‎Assemblers: documenting changes to ca65)
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
In 65x parlance, a '''signature byte''' is the second byte of certain two-byte instructions, including:
In 65x parlance, a '''signature byte''' is the byte that follows certain instructions, including:


* BRK
* <tt>BRK</tt>
* COP
* <tt>COP</tt>
* WDM
* <tt>WDM</tt>


When a programmer has sprinkled many BRKs throughout a large codebase, it can be difficult to tell which BRK breakpoint has been tripped.  That is why it can be useful to specify a unique signature byte after BRK, allowing up to 256 unique breakpoints.
Each of these instructions will normally advance the PC by two bytes, even though the hardware does not make any direct use of the second "operand" byte.


For co-processor empowerment, the signature byte can be used to specify which command the co-processor should run.
For this reason, these instructions have been treated both as one-byte<ref>Eyes, David, &amp; Lichty, Ron. ''Programming the 65816 Including the 6502, 65C02, and 65802'' (2015th ed.).  Page 436.  Prentice Hall Press.  New York, New York.</ref> and two-byte<ref>''Western Design Center W65C816S 8/16–bit Microprocessor Datasheet.'' Section 7.22 BRK Instruction, page 53.  (2018, November 9). Retrieved February 25, 2023, from https://www.westerndesigncenter.com/wdc/documentation/w65c816s.pdf </ref> instructions in various reference documents and assemblers.


The signature byte following WDM was originally intended to extend the 65c816's opcode space and allow for up to 256 more unique instructions, but this was never implemented.  As of 2023 when using Mesen for SNES homebrew, WDM can be used as an even simpler (because it does not need a handler routine) breakpoint by checking the "Break on.." WDM checkbox in the debugger.
== BRK and COP ==


Some literature (such as the Eyes & Lichty manual) describes BRK as a one-byte instruction because some assemblers do not require the programmer to specify the signature byte. This way of thinking about BRK has the disadvantage of needing to pad the following byte with a dummy value, or remember that the program counter was incremented by two before returning from an interrupt.
Both of these instructions generate a software interrupt that will be handled by a routine designated in the [[CPU vectors|CPU vector table]].
* <tt>COP</tt> has a vector at $FFE4.
* <tt>BRK</tt> has a vector at $FFE6.
 
Stack contents for handler:
  $00, S - (empty, current stack pointer)
  $01, S - P status byte
  $02, S - return address low (BRK/COP PC + 2)
  $03, S - return address high
  $04, S - return bank K
 
The <tt>COP</tt> instruction was originally intended for use with a co-processor, for which the signature byte could indicate a command to send to the co-processor. However, there is no hardware to support this usage on the SNES, and it is simply a second software interrupt, equivalent to BRK.
 
=== Without Signature ===
 
If the signature byte is not needed, a BRK or COP handler may wish to decrement the return address on the stack before <tt>RTI</tt>, returning as if it were a one-byte instruction.
 
=== With Signature ===
 
A software response to <tt>BRK</tt> or <tt>COP</tt> may use the return address on the stack to deduce the location of the operand byte and inspect it.
 
This might be used for error codes, or as a compact system call dispatch.
 
== WDM ==
 
The <tt>WDM</tt> instruction was reserved for future use, but was ultimately left unused. It is simply a 2-byte alternative to <tt>NOP</tt>.
 
Mesen's debugger provides a break-on-WDM instruction which can make it convenient as an emulator-only breakpoint.
 
== Assemblers ==
 
There is no standard for how assemblers treat BRK or COP. If BRK emits only 1 byte, a signature byte can be added manually with a data byte following.
 
* ca65 an optional signature for BRK<ref>[https://github.com/cc65/cc65/commit/d13d068e71fb7cc08734e2c17e67e83f48d28d77 cc65 github commit d13d068] 2018-08-16 - 65C816-only BRK optional parameter</ref> and COP<ref>[https://github.com/cc65/cc65/pull/2010 cc65 github PR 2010] 2023-03-04 - BRK, COP optional parameters, optional immediate, all CPUs</ref>, allowing either 1 or 2 bytes. WDM always require the signature byte.
* wla-dx always emits 2 bytes for BRK, COP and WDM. The signature byte defaults to 0 if not given.
* asar always emits 2 bytes for BRK, COP and WDM. The signature byte defaults to 0 if not given.
 
== Notes ==
 
* Though the 65C816 has no unused opcodes, on the 6502 many were left open with unspecified behaviour. This allowed the use of "unofficial" illegal opcodes, including several <tt>NOP</tt> variants with an unused signature byte. See: [//www.nesdev.org/wiki/CPU_unofficial_opcodes NESDev: CPU unofficial opcodes]
 
== References ==
<References/>

Latest revision as of 22:51, 7 March 2023

In 65x parlance, a signature byte is the byte that follows certain instructions, including:

  • BRK
  • COP
  • WDM

Each of these instructions will normally advance the PC by two bytes, even though the hardware does not make any direct use of the second "operand" byte.

For this reason, these instructions have been treated both as one-byte[1] and two-byte[2] instructions in various reference documents and assemblers.

BRK and COP

Both of these instructions generate a software interrupt that will be handled by a routine designated in the CPU vector table.

  • COP has a vector at $FFE4.
  • BRK has a vector at $FFE6.

Stack contents for handler:

 $00, S - (empty, current stack pointer)
 $01, S - P status byte
 $02, S - return address low (BRK/COP PC + 2)
 $03, S - return address high
 $04, S - return bank K

The COP instruction was originally intended for use with a co-processor, for which the signature byte could indicate a command to send to the co-processor. However, there is no hardware to support this usage on the SNES, and it is simply a second software interrupt, equivalent to BRK.

Without Signature

If the signature byte is not needed, a BRK or COP handler may wish to decrement the return address on the stack before RTI, returning as if it were a one-byte instruction.

With Signature

A software response to BRK or COP may use the return address on the stack to deduce the location of the operand byte and inspect it.

This might be used for error codes, or as a compact system call dispatch.

WDM

The WDM instruction was reserved for future use, but was ultimately left unused. It is simply a 2-byte alternative to NOP.

Mesen's debugger provides a break-on-WDM instruction which can make it convenient as an emulator-only breakpoint.

Assemblers

There is no standard for how assemblers treat BRK or COP. If BRK emits only 1 byte, a signature byte can be added manually with a data byte following.

  • ca65 an optional signature for BRK[3] and COP[4], allowing either 1 or 2 bytes. WDM always require the signature byte.
  • wla-dx always emits 2 bytes for BRK, COP and WDM. The signature byte defaults to 0 if not given.
  • asar always emits 2 bytes for BRK, COP and WDM. The signature byte defaults to 0 if not given.

Notes

  • Though the 65C816 has no unused opcodes, on the 6502 many were left open with unspecified behaviour. This allowed the use of "unofficial" illegal opcodes, including several NOP variants with an unused signature byte. See: NESDev: CPU unofficial opcodes

References

  1. Eyes, David, & Lichty, Ron. Programming the 65816 Including the 6502, 65C02, and 65802 (2015th ed.). Page 436. Prentice Hall Press. New York, New York.
  2. Western Design Center W65C816S 8/16–bit Microprocessor Datasheet. Section 7.22 BRK Instruction, page 53. (2018, November 9). Retrieved February 25, 2023, from https://www.westerndesigncenter.com/wdc/documentation/w65c816s.pdf
  3. cc65 github commit d13d068 2018-08-16 - 65C816-only BRK optional parameter
  4. cc65 github PR 2010 2023-03-04 - BRK, COP optional parameters, optional immediate, all CPUs