Fixed cycle delay: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(Let's test a rewriting of the page.)
(Another reformatting)
Line 11: Line 11:
* @rts15 means you know a memory address that contains a <code>JMP</code> that jumps to another location that contains <code>RTS</code>.
* @rts15 means you know a memory address that contains a <code>JMP</code> that jumps to another location that contains <code>RTS</code>.
* Alternatively, @rts15 means you know a memory address that contains a harmless 3-cycle instruction that fits your constraints (such as <code>LDA $00</code>), followed by <code>RTS</code>.
* Alternatively, @rts15 means you know a memory address that contains a harmless 3-cycle instruction that fits your constraints (such as <code>LDA $00</code>), followed by <code>RTS</code>.
 
* @A, @X, @Y are local labels.
=== 0 cycles ===
0 bytes
 
=== 1 cycle ===
Impossible


=== 2 cycles ===
=== 2 cycles ===
==== Canonical (1 bytes) ====
{| class="wikitable"
<pre>NOP</pre>
!colspan="2"|1 bytes
|-
|<pre>NOP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 3 cycles ===
=== 3 cycles ===
==== 1 bytes ====
{| class="wikitable"
''If you can clobber S'':<br>
!colspan="2"|1 bytes
<pre>PHA</pre>
|-
 
|<pre>PHA</pre>
==== 2 bytes ====
|Clobbers S
''If you can clobber Z, N and A'':<br>
|-
<pre>LDA $00</pre>
!colspan="2"|2 bytes
 
|-
''If you can clobber Z, N and V'':<br>
|<pre>LDA $00</pre>
<pre>BIT $00</pre>
|Clobbers Z, N and A
 
|-
''If you have @zptemp'':<br>
|<pre>BIT $00</pre>
<pre>STA @zptemp</pre>
|Clobbers Z, N and V
 
|-
''If you can use unofficial opcodes'':<br>
|<pre>STA @zptemp</pre>
<pre>NOP $00</pre>
|Requires @zptemp
 
|-
==== Canonical (3 bytes) ====
|<pre>NOP $00</pre>
<pre>JMP *+3</pre>
|Requires support for unofficial opcodes
|-
!colspan="2"|3 bytes
|-
|<pre>JMP *+3</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 4 cycles ===
=== 4 cycles ===
==== 1 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S'':<br>
!colspan="2"|1 bytes
<pre>PLA</pre>
|-
 
|<pre>PLA</pre>
==== Synthetic (2 bytes) ====
|Clobbers Z, N, A and S
{2-cycle delay} + {2-cycle delay}
|-
!colspan="2"|2 bytes
|-
|<pre>NOP
NOP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 5 cycles ===
=== 5 cycles ===
==== Synthetic (2&mdash;4 bytes) ====
{| class="wikitable"
{2-cycle delay} + {3-cycle delay}
!colspan="2"|2 bytes
|-
|<pre>NOP
PHA</pre>
|Clobbers S
|-
!colspan="2"|3 bytes
|-
|<pre>NOP
LDA $00</pre>
|Clobbers Z, N and A
|-
|<pre>NOP
BIT $00</pre>
|Clobbers Z, N and V
|-
|<pre>NOP
STA @zptemp</pre>
|Requires @zptemp
|-
|<pre>NOP
NOP $00</pre>
|Requires support for unofficial opcodes
|-
!colspan="2"|4 bytes
|-
|<pre>NOP
JMP *+3</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 6 cycles ===
=== 6 cycles ===
==== 2 bytes ====
{| class="wikitable"
''If you can clobber S'':<br>
!colspan="2"|2 bytes
<pre>PHA
|-
|<pre>PHA
PHA</pre>
PHA</pre>
 
|Clobbers S
==== Synthetic (3 bytes) ====
|-
{2-cycle delay} + {4-cycle delay}
!colspan="2"|3 bytes
|-
|<pre>NOP
NOP
NOP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 7 cycles ===
=== 7 cycles ===
==== Canonical (2 bytes) ====
{| class="wikitable"
<pre>PHP
!colspan="2"|2 bytes
|-
|<pre>PHP
PLP</pre>
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 8 cycles ===
=== 8 cycles ===
==== 2 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S'':<br>
!colspan="2"|2 bytes
<pre>PLA
|-
|<pre>PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== 3 bytes ====
|-
''If you can clobber Z, N, A and X'':<br>
!colspan="2"|3 bytes
<pre>TSX
|-
|<pre>TSX
PLA
PLA
TXS</pre>
TXS</pre>
 
|Clobbers Z, N, A and X
==== Synthetic (3&mdash;4 bytes) ====
|-
{2-cycle delay} + {6-cycle delay}
|<pre>NOP
PHA
PHA</pre>
|Clobbers S
|-
!colspan="2"|4 bytes
|-
|<pre>NOP
NOP
NOP
NOP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 9 cycles ===
=== 9 cycles ===
==== Synthetic (3 bytes) ====
{| class="wikitable"
{2-cycle delay} + {7-cycle delay}
!colspan="2"|3 bytes
|-
|<pre>NOP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 10 cycles ===
=== 10 cycles ===
==== 3 bytes ====
{| class="wikitable"
''If you can clobber S'':<br>
!colspan="2"|3 bytes
<pre>PHA
|-
|<pre>PHA
PHP
PHP
PLP</pre>
PLP</pre>
 
|Clobbers S
==== 4 bytes ====
|-
''Clobbers nothing, requires nothing'':<br>
!colspan="2"|4 bytes
<pre>PHP
|-
|<pre>PHP
BIT $00
BIT $00
PLP</pre>
PLP</pre>
 
|Clobbers nothing, requires nothing
|-
|}




=== 11 cycles ===
=== 11 cycles ===
==== 3 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S'':<br>
!colspan="2"|3 bytes
<pre>PHA
|-
|<pre>PHA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== Synthetic (4 bytes) ====
|-
{2-cycle delay} + {9-cycle delay}
!colspan="2"|4 bytes
|-
|<pre>NOP
NOP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 12 cycles ===
=== 12 cycles ===
==== 3 bytes ====
{| class="wikitable"
''If you have @rts12'':<br>
!colspan="2"|3 bytes
<pre>JSR @rts12</pre>
|-
 
|<pre>JSR @rts12</pre>
''If you can clobber Z, N, A and S'':<br>
|Requires @rts12
<pre>PLA
|-
|<pre>PLA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== 4 bytes ====
|-
''If you can clobber Z and N'':<br>
!colspan="2"|4 bytes
<pre>ROL $00,X
|-
|<pre>ROL $00,X
ROR $00,X</pre>
ROR $00,X</pre>
 
|Clobbers Z and N
==== Synthetic (4&mdash;5 bytes) ====
|-
{2-cycle delay} + {10-cycle delay}
|<pre>NOP
PHA
PHP
PLP</pre>
|Clobbers S
|-
!colspan="2"|5 bytes
|-
|<pre>NOP
PHP
BIT $00
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 13 cycles ===
=== 13 cycles ===
==== 4 bytes ====
{| class="wikitable"
''If you can clobber S'':<br>
!colspan="2"|4 bytes
<pre>PHA
|-
|<pre>PHA
PHA
PHA
PHP
PHP
PLP</pre>
PLP</pre>
 
|Clobbers S
==== Synthetic (5 bytes) ====
|-
{2-cycle delay} + {11-cycle delay}
!colspan="2"|5 bytes
|-
|<pre>NOP
NOP
NOP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 14 cycles ===
=== 14 cycles ===
==== 3 bytes ====
{| class="wikitable"
''If you have @rts14'':<br>
!colspan="2"|3 bytes
<pre>JSR @rts14</pre>
|-
 
|<pre>JSR @rts14</pre>
==== Synthetic (4 bytes) ====
|Requires @rts14
{7-cycle delay} + {7-cycle delay}
|-
!colspan="2"|4 bytes
|-
|<pre>PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 15 cycles ===
=== 15 cycles ===
==== 3 bytes ====
{| class="wikitable"
''If you have @rts15'':<br>
!colspan="2"|3 bytes
<pre>JSR @rts15</pre>
|-
 
|<pre>JSR @rts15</pre>
==== 4 bytes ====
|Requires @rts15
''If you can clobber S and you have @rts12'':<br>
|-
<pre>PHA
!colspan="2"|4 bytes
|-
|<pre>PHA
JSR @rts12</pre>
JSR @rts12</pre>
 
|Clobbers S and requires @rts12
''If you can clobber Z, N, A and S'':<br>
|-
<pre>PHA
|<pre>PHA
PLA
PLA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== 5 bytes ====
|-
''If you can clobber Z, N and A and you have @rts12'':<br>
!colspan="2"|5 bytes
<pre>LDA $00
|-
|<pre>LDA $00
JSR @rts12</pre>
JSR @rts12</pre>
 
|Clobbers Z, N and A and requires @rts12
''If you can clobber Z, N and X'':<br>
|-
<pre>PHA
|<pre>PHA
TSX
TSX
PLA
PLA
TXS
TXS
PLA</pre>
PLA</pre>
 
|Clobbers Z, N and X
''If you can clobber A and X'':<br>
|-
<pre>PHP
|<pre>PHP
TSX
TSX
PLA
PLA
TXS
TXS
PLP</pre>
PLP</pre>
 
|Clobbers A and X
''If you can clobber Z, N and V and you have @rts12'':<br>
|-
<pre>BIT $00
|<pre>BIT $00
JSR @rts12</pre>
JSR @rts12</pre>
 
|Clobbers Z, N and V and requires @rts12
''If you have @zptemp and you have @rts12'':<br>
|-
<pre>STA @zptemp
|<pre>STA @zptemp
JSR @rts12</pre>
JSR @rts12</pre>
 
|Requires @zptemp and @rts12
''If you can use unofficial opcodes and you have @rts12'':<br>
|-
<pre>NOP $00
|<pre>NOP
PHA
PHA
PHP
PLP</pre>
|Clobbers S
|-
|<pre>NOP $00
JSR @rts12</pre>
JSR @rts12</pre>
 
|Requires support for unofficial opcodes and @rts12
==== Synthetic (5&mdash;6 bytes) ====
|-
{2-cycle delay} + {13-cycle delay}
|<pre>LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>NOP
NOP
NOP
NOP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 16 cycles ===
=== 16 cycles ===
==== 4 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S'':<br>
!colspan="2"|4 bytes
<pre>PLA
|-
|<pre>NOP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>PLA
PLA
PLA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== Synthetic (4&mdash;5 bytes) ====
|-
{2-cycle delay} + {14-cycle delay}
!colspan="2"|5 bytes
|-
|<pre>NOP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 17 cycles ===
=== 17 cycles ===
==== 4 bytes ====
{| class="wikitable"
''If you have @rts15'':<br>
!colspan="2"|4 bytes
<pre>NOP
|-
|<pre>NOP
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires @rts15
''If you can clobber S and you have @rts14'':<br>
|-
<pre>PHA
|<pre>PHA
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers S and requires @rts14
==== 5 bytes ====
|-
''If you can clobber Z, N and A and you have @rts14'':<br>
!colspan="2"|5 bytes
<pre>LDA $00
|-
|<pre>LDA $00
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers Z, N and A and requires @rts14
''If you can clobber Z, N and V and you have @rts14'':<br>
|-
<pre>BIT $00
|<pre>BIT $00
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers Z, N and V and requires @rts14
''If you have @zptemp and you have @rts14'':<br>
|-
<pre>STA @zptemp
|<pre>STA @zptemp
JSR @rts14</pre>
JSR @rts14</pre>
 
|Requires @zptemp and @rts14
''If you can clobber S'':<br>
|-
<pre>PHA
|<pre>PHA
PHP
PHP
PLP
PLP
PHP
PHP
PLP</pre>
PLP</pre>
 
|Clobbers S
''If you can use unofficial opcodes and you have @rts14'':<br>
|-
<pre>NOP $00
|<pre>NOP $00
JSR @rts14</pre>
JSR @rts14</pre>
 
|Requires support for unofficial opcodes and @rts14
==== Synthetic (6 bytes) ====
|-
{7-cycle delay} + {10-cycle delay}
!colspan="2"|6 bytes
|-
|<pre>PHP
PLP
PHP
BIT $00
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 18 cycles ===
=== 18 cycles ===
==== 4 bytes ====
{| class="wikitable"
''If you can clobber S and you have @rts15'':<br>
!colspan="2"|4 bytes
<pre>PHA
|-
|<pre>PHA
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers S and requires @rts15
''If you can clobber Z, N, A and S and you have @rts14'':<br>
|-
<pre>PLA
|<pre>PLA
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers Z, N, A and S and requires @rts14
==== 5 bytes ====
|-
''If you can clobber Z, N and A and you have @rts15'':<br>
!colspan="2"|5 bytes
<pre>LDA $00
|-
|<pre>LDA $00
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers Z, N and A and requires @rts15
''If you can clobber Z, N and V and you have @rts15'':<br>
|-
<pre>BIT $00
|<pre>BIT $00
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers Z, N and V and requires @rts15
''If you have @zptemp and you have @rts15'':<br>
|-
<pre>STA @zptemp
|<pre>NOP
NOP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>STA @zptemp
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires @zptemp and @rts15
''If you can clobber S and you have @rts12'':<br>
|-
<pre>PHA
|<pre>PHA
PHA
PHA
JSR @rts12</pre>
JSR @rts12</pre>
 
|Clobbers S and requires @rts12
''If you can use unofficial opcodes and you have @rts15'':<br>
|-
<pre>NOP $00
|<pre>NOP
PLA
PLA
PLA
PLA</pre>
|Clobbers Z, N, A and S
|-
|<pre>NOP $00
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires support for unofficial opcodes and @rts15
==== Synthetic (5&mdash;6 bytes) ====
|-
{2-cycle delay} + {16-cycle delay}
|<pre>LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA ;hides 'NOP'
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA ;hides 'NOP'
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>NOP
NOP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 19 cycles ===
=== 19 cycles ===
==== 4 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S and you have @rts15'':<br>
!colspan="2"|4 bytes
<pre>PLA
|-
|<pre>PLA
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers Z, N, A and S and requires @rts15
==== 5 bytes ====
|-
''If you have @rts12'':<br>
!colspan="2"|5 bytes
<pre>PHP
|-
|<pre>PHP
PLP
PLP
JSR @rts12</pre>
JSR @rts12</pre>
 
|Requires @rts12
''If you have @rts15'':<br>
|-
<pre>NOP
|<pre>NOP
NOP
NOP
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires @rts15
''If you can clobber Z, N, A and S'':<br>
|-
<pre>PHA
|<pre>PHA
PLA
PLA
PLA
PLA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
''If you can clobber S and you have @rts14'':<br>
|-
<pre>NOP
|<pre>NOP
PHA
PHA
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers S and requires @rts14
==== 6 bytes ====
|-
''Clobbers nothing, requires nothing'':<br>
|<pre>LDY #130
<pre>PHP
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>PHP
ROL $00,X
ROL $00,X
ROR $00,X
ROR $00,X
PLP</pre>
PLP</pre>
 
|Clobbers nothing, requires nothing
|-
|}




=== 20 cycles ===
=== 20 cycles ===
==== 5 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S'':<br>
!colspan="2"|5 bytes
<pre>PLA
|-
|<pre>NOP
PHA
JSR @rts15</pre>
|Clobbers S and requires @rts15
|-
|<pre>PLA
PLA
PLA
PLA
PLA
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
''If you can clobber S and you have @rts14'':<br>
|-
<pre>PHA
|<pre>PHA
PHA
PHA
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers S and requires @rts14
==== 6 bytes ====
|-
''If you can clobber Z, N, A and X'':<br>
|<pre>LDY #130
<pre>TSX
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>NOP
LDA $00
JSR @rts15</pre>
|Clobbers Z, N and A and requires @rts15
|-
|<pre>TSX
PLA
PLA
PLA
PLA
Line 359: Line 628:
PLA
PLA
TXS</pre>
TXS</pre>
 
|Clobbers Z, N, A and X
''If you can clobber S'':<br>
|-
<pre>PHA
|<pre>NOP
BIT $00
JSR @rts15</pre>
|Clobbers Z, N and V and requires @rts15
|-
|<pre>NOP
NOP
NOP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>NOP
STA @zptemp
JSR @rts15</pre>
|Requires @zptemp and @rts15
|-
|<pre>PHA
PHA
PHA
PHP
PHP
Line 367: Line 652:
PHP
PHP
PLP</pre>
PLP</pre>
 
|Clobbers S
==== Synthetic (5&mdash;7 bytes) ====
|-
{2-cycle delay} + {18-cycle delay}
|<pre>NOP
NOP $00
JSR @rts15</pre>
|Requires support for unofficial opcodes and @rts15
|-
!colspan="2"|7 bytes
|-
|<pre>NOP
NOP
NOP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 21 cycles ===
=== 21 cycles ===
==== 5 bytes ====
{| class="wikitable"
''If you can clobber Z, N and Y'':<br>
!colspan="2"|5 bytes
<pre>LDY #4
|-
@D:
|<pre>LDY #4
@Y:
DEY
DEY
BNE @D</pre>
BNE @Y</pre>
 
|Clobbers Z, N and Y
''If you can clobber Z, N and X'':<br>
|-
<pre>LDX #4
|<pre>LDX #4
@D:
@X:
DEX
DEX
BNE @D</pre>
BNE @X</pre>
 
|Clobbers Z, N and X
''If you can clobber S and you have @rts15'':<br>
|-
<pre>PHA
|<pre>PHP
PLP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>PHA
PHA
PHA
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers S and requires @rts15
==== Synthetic (5&mdash;6 bytes) ====
|-
{7-cycle delay} + {14-cycle delay}
!colspan="2"|6 bytes
|-
|<pre>PHP
PLP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 22 cycles ===
=== 22 cycles ===
==== 5 bytes ====
{| class="wikitable"
''If you have @rts15'':<br>
!colspan="2"|5 bytes
<pre>PHP
|-
|<pre>PHP
PLP
PLP
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires @rts15
''If you can clobber Z, N, A and S and you have @rts14'':<br>
|-
<pre>PLA
|<pre>PLA
PLA
PLA
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers Z, N, A and S and requires @rts14
==== 6 bytes ====
|-
''If you can clobber Z, N and Y'':<br>
|<pre>LDY #130
<pre>LDY #3
@Y = * - 1
@D:
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes and @zptemp
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes and @zptemp
|-
!colspan="2"|6 bytes
|-
|<pre>LDY #3
@Y:
NOP
NOP
DEY
DEY
BNE @D</pre>
BNE @Y</pre>
 
|Clobbers Z, N and Y
''If you can clobber Z, N and X'':<br>
|-
<pre>LDX #3
|<pre>LDX #3
@D:
@X:
NOP
NOP
DEX
DEX
BNE @D</pre>
BNE @X</pre>
 
|Clobbers Z, N and X
''If you can clobber S and you have @rts12'':<br>
|-
<pre>PHA
|<pre>PHA
PHP
PHP
PLP
PLP
JSR @rts12</pre>
JSR @rts12</pre>
 
|Clobbers S and requires @rts12
==== 7 bytes ====
|-
''If you have @rts12'':<br>
|<pre>NOP
<pre>PHP
PLA
PLA
PLA
PLA
PLA</pre>
|Clobbers Z, N, A and S
|-
|<pre>NOP
PHA
PHA
JSR @rts14</pre>
|Clobbers S and requires @rts14
|-
!colspan="2"|7 bytes
|-
|<pre>PHP
BIT $00
BIT $00
PLP
PLP
JSR @rts12</pre>
JSR @rts12</pre>
 
|Requires @rts12
''If you can clobber X'':<br>
|-
<pre>PHP
|<pre>PHP
PHA
PHA
TSX
TSX
Line 443: Line 793:
PLA
PLA
PLP</pre>
PLP</pre>
 
|Clobbers X
==== Synthetic (6&mdash;8 bytes) ====
|-
{2-cycle delay} + {20-cycle delay}
|<pre>NOP
NOP
NOP
NOP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>NOP
PHA
PHA
PHP
PLP
PHP
PLP</pre>
|Clobbers S
|-
|<pre>PHP
LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP
DEY
BMI @Y
PLP</pre>
|Clobbers Y and requires support for unofficial opcodes
|-
|<pre>SEC
LDA #130
@A = * - 1
; ^ Hides !NOP #imm
NOP $EA44 ;hides 'NOP zp'
SBC #1
BMI @A</pre>
|Clobbers C, Z, N and A and requires support for unofficial opcodes
|-
!colspan="2"|8 bytes
|-
|<pre>NOP
NOP
NOP
NOP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 23 cycles ===
=== 23 cycles ===
==== 5 bytes ====
{| class="wikitable"
''If you can clobber Z, N, A and S and you have @rts15'':<br>
!colspan="2"|5 bytes
<pre>PLA
|-
|<pre>PLA
PLA
PLA
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers Z, N, A and S and requires @rts15
==== 6 bytes ====
|-
''If you can clobber Z, N, A and X and you have @rts15'':<br>
|<pre>LDX #130
<pre>TSX
@X = * - 1
; ^ Hides !NOP #imm
NOP $4848 ;hides two 'PHA's
DEX
BMI @X</pre>
|Clobbers Z, N, X and S and requires support for unofficial opcodes
|-
|<pre>LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $4848 ;hides two 'PHA's
DEY
BMI @Y</pre>
|Clobbers Z, N, Y and S and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>TSX
PLA
PLA
TXS
TXS
JSR @rts15</pre>
JSR @rts15</pre>
 
|Clobbers Z, N, A and X and requires @rts15
''If you can clobber Z, N, A and S'':<br>
|-
<pre>PHA
|<pre>NOP
PHP
PLP
JSR @rts14</pre>
|Requires @rts14
|-
|<pre>NOP
PHA
PHA
JSR @rts15</pre>
|Clobbers S and requires @rts15
|-
|<pre>PHA
PLA
PLA
PLA
PLA
Line 469: Line 896:
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
==== Synthetic (6&mdash;7 bytes) ====
|-
{2-cycle delay} + {21-cycle delay}
|<pre>LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
NOP
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $EA,X ;hides 'NOP'
NOP
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes
|-
!colspan="2"|7 bytes
|-
|<pre>NOP
PHP
PLP
PHP
PLP
PHP
PLP</pre>
|Clobbers nothing, requires nothing
|-
|}




=== 24 cycles ===
=== 24 cycles ===
==== 6 bytes ====
{| class="wikitable"
''If you have @rts12'':<br>
!colspan="2"|5 bytes
<pre>JSR @rts12
|-
|<pre>LDY #130
@Y = * - 1
; ^ Hides !NOP #imm
NOP $2808 ;hides 'PHP' and 'PLP'; reads from $2000
DEY
BMI @Y</pre>
|Clobbers Z, N and Y and requires support for unofficial opcodes
|-
|<pre>LDX #130
@X = * - 1
; ^ Hides !NOP #imm
NOP $2808 ;hides 'PHP' and 'PLP'; reads from $2000
DEX
BMI @X</pre>
|Clobbers Z, N and X and requires support for unofficial opcodes
|-
!colspan="2"|6 bytes
|-
|<pre>JSR @rts12
JSR @rts12</pre>
JSR @rts12</pre>
 
|Requires @rts12
''If you have @rts15'':<br>
|-
<pre>NOP
|<pre>NOP
PHP
PHP
PLP
PLP
JSR @rts15</pre>
JSR @rts15</pre>
 
|Requires @rts15
''If you can clobber Z, N, A and S'':<br>
|-
<pre>PLA
|<pre>PLA
PLA
PLA
PLA
PLA
Line 493: Line 968:
PLA
PLA
PLA</pre>
PLA</pre>
 
|Clobbers Z, N, A and S
''If you can clobber S and you have @rts14'':<br>
|-
<pre>PHA
|<pre>PHA
PHP
PHP
PLP
PLP
JSR @rts14</pre>
JSR @rts14</pre>
 
|Clobbers S and requires @rts14
==== 7 bytes ====
|-
''If you can clobber Z, N, A and X'':<br>
!colspan="2"|7 bytes
<pre>TSX
|-
|<pre>TSX
PLA
PLA
PLA
PLA
Line 509: Line 985:
PLA
PLA
TXS</pre>
TXS</pre>
 
|Clobbers Z, N, A and X
''If you have @rts14'':<br>
|-
<pre>PHP
|<pre>PHP
BIT $00
BIT $00
PLP
PLP
JSR @rts14</pre>
JSR @rts14</pre>
 
|Requires @rts14
''If you can clobber S'':<br>
|-
<pre>PHA
|<pre>PHA
PHP
PLP
PHP
PLP
PHP
PHP
PLP</pre>
|Clobbers S
|-
|<pre>SEC
LDA #130
@A = * - 1
; ^ Hides !NOP #imm
.byte $0C,$E6,@zp_temp ;NOP abs; hides 'INC @zp_temp'
SBC #1
BMI @A</pre>
|Clobbers C, Z, N and A and requires support for unofficial opcodes and @zptemp
|-
!colspan="2"|8 bytes
|-
|<pre>PHP
PLP
PLP
PHP
PHP
PLP
PLP
PHP
PHP
BIT $00
PLP</pre>
PLP</pre>
 
|Clobbers nothing, requires nothing
==== Synthetic (8 bytes) ====
|-
{7-cycle delay} + {17-cycle delay}
|}


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

Revision as of 00:22, 14 March 2016

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