I am sure that there is already a lot of nicer and shorter code for this. But the code seems to do the job and allows for memory dumps that are a easier to read.
Maybe this is useful for someone. Comments and suggestions for improvement are very welcome.
Code:
; input:
; A opcode
; changed:
; A
; output
; X instruction size
; remarks:
; - valid for newer 65c02 with BBR, BBS, RMB and SMB instructions
; see also:
; - https://llx.com/Neil/a2/opcodes.html
; - http://www.6502.org/tutorials/65c02opcodes.html
instruction_size:
ldx #3
eor #$0c
bit #$0c
beq @three ; xC, xD, xE, xF
; x0 .. xB
eor #$08
bit #$0c
beq @two ; x4, x5, x6, x7
; x0 .. x3, x8 .. xF
eor #$03
bit #$03
beq @one ; x3, xB
; x0 .. x2, x8 .. xA
bit #$08
beq @x0_2 ; x0 .. x2
; x8 .. xA
bit #$01
bne @one ; x8, xA
; x9
bit #$10
beq @two ; 09, 29, ..., e9
bne @three ; 19, 39, ..., f9
; x0 .. x2
@x0_2: eor #$03
bit #$03
bne @two ; x1, x2
; x0
bit #$80
bne @two ; 80, 90, .., F0
; 00, 10, ..., 70
bit #$10
bne @two ; 10, 30, 50, 70
; 00, 20, 40, 60
cmp #$24
beq @three ; 20
; 00, 40, 60
@one: dex
@two: dex
@three: rts
Example dump using the code:
Code:
.i f80d
.: F80D 48
.: F80E DA
.: F80F BA
.: F810 BD 03 01
.: F813 29 10
.: F815 D0 03
.: F817 6C 02 02
.: F81A 6C 04 02
update (2023-12-07):
As BB8 pointed out (
http://forum.6502.org/viewtopic.php?f=2&t=7845&p=104965#p104946), result 1 for x7 and xF is no longer correct for current 65c02. I have adapted the code accordingly.