Here's a version similar to Osi's which works for all 256 65c02 instructions. Very slightly shorter and I think a little faster than the original posting in the thread:
Code:
op_length:
; given an opcode in A, return its length in Y (stomps A)
pha
and #$f
tay
lda _lengths,y ; lookup the length
bmi _special ; $x0 and $x9 are special
ply ; discard the opcode
tay ; return the length
rts
_lengths: .byte $80,2,2,1,2,2,2,2, 1,$81,1,1,3,3,3,3
_special:
ldy #1 ; guess length 1
ror ; test bit 0: C=0 means $x0, C=1 means $x9
pla ; recover the opcode
bcs _x9
; for $x0 length is two except $20 (3), $40 (1), $60 (1)
bit #%10011111 ; is opcode 0/20/40/60 ?
bne _two
asl ; test bit 6 by shifting to sign bit
bmi _one ; bit 6 set means $40 or $60
; otherwise we have A=$40 if op was $20 and $0 otherwise
; shift right twice to reuse bit 4 test
lsr
lsr
_x9:
; for $x9, bit 4 set means 3 bytes, clear means 2
and #$10
beq _two
_three: iny
_two: iny
_one: rts