Fixed cycle delay

From NESdev Wiki
Revision as of 00:22, 14 March 2016 by Bisqwit (talk | contribs) (Another reformatting)
Jump to navigationJump to search

Delay code

Shortest possible CPU code that creates N cycles of delay, depending on constraints.

All branch instructions assume that no page wrap occurs.

Explanations on the requirements:

  • @zp_temp means you have a zeropage address that you can write random data into.
  • @rts12 means you know a memory address that contains byte $60 (RTS).
  • @rts14 means you know a memory address that contains a harmless 2-cycle instruction that fits your constraints (such as CLC, LDA #0, or NOP), followed by RTS.
  • @rts15 means you know a memory address that contains a JMP that jumps to another location that contains RTS.
  • Alternatively, @rts15 means you know a memory address that contains a harmless 3-cycle instruction that fits your constraints (such as LDA $00), followed by RTS.
  • @A, @X, @Y are local labels.

2 cycles

1 bytes
NOP
Clobbers nothing, requires nothing


3 cycles

1 bytes
PHA
Clobbers S
2 bytes
LDA $00
Clobbers Z, N and A
BIT $00
Clobbers Z, N and V
STA @zptemp
Requires @zptemp
NOP $00
Requires support for unofficial opcodes
3 bytes
JMP *+3
Clobbers nothing, requires nothing


4 cycles

1 bytes
PLA
Clobbers Z, N, A and S
2 bytes
NOP
NOP
Clobbers nothing, requires nothing


5 cycles

2 bytes
NOP
PHA
Clobbers S
3 bytes
NOP
LDA $00
Clobbers Z, N and A
NOP
BIT $00
Clobbers Z, N and V
NOP
STA @zptemp
Requires @zptemp
NOP
NOP $00
Requires support for unofficial opcodes
4 bytes
NOP
JMP *+3
Clobbers nothing, requires nothing


6 cycles

2 bytes
PHA
PHA
Clobbers S
3 bytes
NOP
NOP
NOP
Clobbers nothing, requires nothing


7 cycles

2 bytes
PHP
PLP
Clobbers nothing, requires nothing


8 cycles

2 bytes
PLA
PLA
Clobbers Z, N, A and S
3 bytes
TSX
PLA
TXS
Clobbers Z, N, A and X
NOP
PHA
PHA
Clobbers S
4 bytes
NOP
NOP
NOP
NOP
Clobbers nothing, requires nothing


9 cycles

3 bytes
NOP
PHP
PLP
Clobbers nothing, requires nothing


10 cycles

3 bytes
PHA
PHP
PLP
Clobbers S
4 bytes
PHP
BIT $00
PLP
Clobbers nothing, requires nothing


11 cycles

3 bytes
PHA
PLA
PLA
Clobbers Z, N, A and S
4 bytes
NOP
NOP
PHP
PLP
Clobbers nothing, requires nothing


12 cycles

3 bytes
JSR @rts12
Requires @rts12
PLA
PLA
PLA
Clobbers Z, N, A and S
4 bytes
ROL $00,X
ROR $00,X
Clobbers Z and N
NOP
PHA
PHP
PLP
Clobbers S
5 bytes
NOP
PHP
BIT $00
PLP
Clobbers nothing, requires nothing


13 cycles

4 bytes
PHA
PHA
PHP
PLP
Clobbers S
5 bytes
NOP
NOP
NOP
PHP
PLP
Clobbers nothing, requires nothing


14 cycles

3 bytes
JSR @rts14
Requires @rts14
4 bytes
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


15 cycles

3 bytes
JSR @rts15
Requires @rts15
4 bytes
PHA
JSR @rts12
Clobbers S and requires @rts12
PHA
PLA
PLA
PLA
Clobbers Z, N, A and S
5 bytes
LDA $00
JSR @rts12
Clobbers Z, N and A and requires @rts12
PHA
TSX
PLA
TXS
PLA
Clobbers Z, N and X
PHP
TSX
PLA
TXS
PLP
Clobbers A and X
BIT $00
JSR @rts12
Clobbers Z, N and V and requires @rts12
STA @zptemp
JSR @rts12
Requires @zptemp and @rts12
NOP
PHA
PHA
PHP
PLP
Clobbers S
NOP $00
JSR @rts12
Requires support for unofficial opcodes and @rts12
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
6 bytes
NOP
NOP
NOP
NOP
PHP
PLP
Clobbers nothing, requires nothing


16 cycles

4 bytes
NOP
JSR @rts14
Requires @rts14
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
5 bytes
NOP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


17 cycles

4 bytes
NOP
JSR @rts15
Requires @rts15
PHA
JSR @rts14
Clobbers S and requires @rts14
5 bytes
LDA $00
JSR @rts14
Clobbers Z, N and A and requires @rts14
BIT $00
JSR @rts14
Clobbers Z, N and V and requires @rts14
STA @zptemp
JSR @rts14
Requires @zptemp and @rts14
PHA
PHP
PLP
PHP
PLP
Clobbers S
NOP $00
JSR @rts14
Requires support for unofficial opcodes and @rts14
6 bytes
PHP
PLP
PHP
BIT $00
PLP
Clobbers nothing, requires nothing


18 cycles

4 bytes
PHA
JSR @rts15
Clobbers S and requires @rts15
PLA
JSR @rts14
Clobbers Z, N, A and S and requires @rts14
5 bytes
LDA $00
JSR @rts15
Clobbers Z, N and A and requires @rts15
BIT $00
JSR @rts15
Clobbers Z, N and V and requires @rts15
NOP
NOP
JSR @rts14
Requires @rts14
STA @zptemp
JSR @rts15
Requires @zptemp and @rts15
PHA
PHA
JSR @rts12
Clobbers S and requires @rts12
NOP
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
NOP $00
JSR @rts15
Requires support for unofficial opcodes and @rts15
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA ;hides 'NOP'
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA ;hides 'NOP'
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes
6 bytes
NOP
NOP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


19 cycles

4 bytes
PLA
JSR @rts15
Clobbers Z, N, A and S and requires @rts15
5 bytes
PHP
PLP
JSR @rts12
Requires @rts12
NOP
NOP
JSR @rts15
Requires @rts15
PHA
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
NOP
PHA
JSR @rts14
Clobbers S and requires @rts14
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes
6 bytes
PHP
ROL $00,X
ROR $00,X
PLP
Clobbers nothing, requires nothing


20 cycles

5 bytes
NOP
PHA
JSR @rts15
Clobbers S and requires @rts15
PLA
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
PHA
PHA
JSR @rts14
Clobbers S and requires @rts14
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes
6 bytes
NOP
LDA $00
JSR @rts15
Clobbers Z, N and A and requires @rts15
TSX
PLA
PLA
PLA
PLA
TXS
Clobbers Z, N, A and X
NOP
BIT $00
JSR @rts15
Clobbers Z, N and V and requires @rts15
NOP
NOP
NOP
JSR @rts14
Requires @rts14
NOP
STA @zptemp
JSR @rts15
Requires @zptemp and @rts15
PHA
PHA
PHP
PLP
PHP
PLP
Clobbers S
NOP
NOP $00
JSR @rts15
Requires support for unofficial opcodes and @rts15
7 bytes
NOP
NOP
NOP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


21 cycles

5 bytes
LDY #4
@Y:
DEY
BNE @Y
Clobbers Z, N and Y
LDX #4
@X:
DEX
BNE @X
Clobbers Z, N and X
PHP
PLP
JSR @rts14
Requires @rts14
PHA
PHA
JSR @rts15
Clobbers S and requires @rts15
6 bytes
PHP
PLP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


22 cycles

5 bytes
PHP
PLP
JSR @rts15
Requires @rts15
PLA
PLA
JSR @rts14
Clobbers Z, N, A and S and requires @rts14
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes and @zptemp
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes and @zptemp
6 bytes
LDY #3
@Y:
NOP
DEY
BNE @Y
Clobbers Z, N and Y
LDX #3
@X:
NOP
DEX
BNE @X
Clobbers Z, N and X
PHA
PHP
PLP
JSR @rts12
Clobbers S and requires @rts12
NOP
PLA
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
NOP
PHA
PHA
JSR @rts14
Clobbers S and requires @rts14
7 bytes
PHP
BIT $00
PLP
JSR @rts12
Requires @rts12
PHP
PHA
TSX
PLA
TXS
PLA
PLP
Clobbers X
NOP
NOP
NOP
NOP
JSR @rts14
Requires @rts14
NOP
PHA
PHA
PHP
PLP
PHP
PLP
Clobbers S
PHP
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP
DEY
BMI @Y
PLP
Clobbers Y and requires support for unofficial opcodes
SEC
LDA #130
@A = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
SBC #1
BMI @A
Clobbers C, Z, N and A and requires support for unofficial opcodes
8 bytes
NOP
NOP
NOP
NOP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


23 cycles

5 bytes
PLA
PLA
JSR @rts15
Clobbers Z, N, A and S and requires @rts15
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $4848 ;hides two 'PHA's
DEX
BMI @X
Clobbers Z, N, X and S and requires support for unofficial opcodes
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $4848 ;hides two 'PHA's
DEY
BMI @Y
Clobbers Z, N, Y and S and requires support for unofficial opcodes
6 bytes
TSX
PLA
TXS
JSR @rts15
Clobbers Z, N, A and X and requires @rts15
NOP
PHP
PLP
JSR @rts14
Requires @rts14
NOP
PHA
PHA
JSR @rts15
Clobbers S and requires @rts15
PHA
PLA
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
NOP
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
NOP
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes
7 bytes
NOP
PHP
PLP
PHP
PLP
PHP
PLP
Clobbers nothing, requires nothing


24 cycles

5 bytes
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $2808 ;hides 'PHP' and 'PLP'; reads from $2000
DEY
BMI @Y
Clobbers Z, N and Y and requires support for unofficial opcodes
LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $2808 ;hides 'PHP' and 'PLP'; reads from $2000
DEX
BMI @X
Clobbers Z, N and X and requires support for unofficial opcodes
6 bytes
JSR @rts12
JSR @rts12
Requires @rts12
NOP
PHP
PLP
JSR @rts15
Requires @rts15
PLA
PLA
PLA
PLA
PLA
PLA
Clobbers Z, N, A and S
PHA
PHP
PLP
JSR @rts14
Clobbers S and requires @rts14
7 bytes
TSX
PLA
PLA
PLA
PLA
PLA
TXS
Clobbers Z, N, A and X
PHP
BIT $00
PLP
JSR @rts14
Requires @rts14
PHA
PHP
PLP
PHP
PLP
PHP
PLP
Clobbers S
SEC
LDA #130
@A = * - 1
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
SBC #1
BMI @A
Clobbers C, Z, N and A and requires support for unofficial opcodes and @zptemp
8 bytes
PHP
PLP
PHP
PLP
PHP
BIT $00
PLP
Clobbers nothing, requires nothing

More

Bisqwit's 6502 delay_n macro set for ca65: http://bisqwit.iki.fi/src/6502-inline_delay.7z