Fixed cycle delay: Difference between revisions
Rainwarrior (talk | contribs) m (→10 cycles: consistent formatting (capitals)) |
(Let's test a rewriting of the page.) |
||
Line 4: | Line 4: | ||
All branch instructions assume that no page wrap occurs. | 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 (<code>RTS</code>). | |||
* @rts14 means you know a memory address that contains a harmless 2-cycle instruction that fits your constraints (such as <code>CLC</code>, <code>LDA #0</code>, or <code>NOP</code>), followed by <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>. | |||
=== 0 cycles === | === 0 cycles === | ||
Line 12: | Line 19: | ||
=== 2 cycles === | === 2 cycles === | ||
1 | ==== Canonical (1 bytes) ==== | ||
<pre>NOP</pre> | |||
=== 3 cycles === | === 3 cycles === | ||
==== 1 bytes ==== | |||
''If you can clobber S'':<br> | |||
<pre>PHA</pre> | |||
If you can clobber | ==== 2 bytes ==== | ||
''If you can clobber Z, N and A'':<br> | |||
<pre>LDA $00</pre> | |||
If | ''If you can clobber Z, N and V'':<br> | ||
<pre>BIT $00</pre> | |||
If you | ''If you have @zptemp'':<br> | ||
<pre>STA @zptemp</pre> | |||
If you can | ''If you can use unofficial opcodes'':<br> | ||
<pre>NOP $00</pre> | |||
==== Canonical (3 bytes) ==== | |||
<pre>JMP *+3</pre> | |||
=== 4 cycles === | === 4 cycles === | ||
If you can clobber Z, N, S | ==== 1 bytes ==== | ||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PLA</pre> | |||
==== Synthetic (2 bytes) ==== | |||
2 bytes | {2-cycle delay} + {2-cycle delay} | ||
=== 5 cycles === | === 5 cycles === | ||
==== Synthetic (2—4 bytes) ==== | |||
{2-cycle delay} + {3-cycle delay} | |||
=== 6 cycles === | === 6 cycles === | ||
==== 2 bytes ==== | |||
''If you can clobber S'':<br> | |||
<pre>PHA | |||
PHA</pre> | |||
==== Synthetic (3 bytes) ==== | |||
{2-cycle delay} + {4-cycle delay} | |||
=== 7 cycles === | === 7 cycles === | ||
2 bytes | ==== Canonical (2 bytes) ==== | ||
PLP</ | <pre>PHP | ||
PLP</pre> | |||
=== 8 cycles === | === 8 cycles === | ||
If you can clobber Z, N, S | ==== 2 bytes ==== | ||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PLA | |||
PLA</pre> | |||
If you can clobber Z, N, X | ==== 3 bytes ==== | ||
''If you can clobber Z, N, A and X'':<br> | |||
PLA | <pre>TSX | ||
TXS</ | PLA | ||
TXS</pre> | |||
==== Synthetic (3—4 bytes) ==== | |||
{2-cycle delay} + {6-cycle delay} | |||
=== 9 cycles === | === 9 cycles === | ||
3 bytes | ==== Synthetic (3 bytes) ==== | ||
{2-cycle delay} + {7-cycle delay} | |||
=== 10 cycles === | === 10 cycles === | ||
==== 3 bytes ==== | |||
If you can clobber S:<br> | ''If you can clobber S'':<br> | ||
<pre>PHA | |||
PHP | PHP | ||
PLP</ | PLP</pre> | ||
==== 4 bytes ==== | |||
''Clobbers nothing, requires nothing'':<br> | |||
<pre>PHP | |||
BIT $00 | BIT $00 | ||
PLP</ | PLP</pre> | ||
=== 11 cycles === | === 11 cycles === | ||
==== 3 bytes ==== | |||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PHA | |||
PLA | |||
PLA</pre> | |||
==== Synthetic (4 bytes) ==== | |||
{2-cycle delay} + {9-cycle delay} | |||
=== 12 cycles === | === 12 cycles === | ||
If you | ==== 3 bytes ==== | ||
''If you have @rts12'':<br> | |||
<pre>JSR @rts12</pre> | |||
If you can clobber Z, N, S | ''If you can clobber Z, N, A and S'':<br> | ||
<pre>PLA | |||
PLA | |||
PLA</pre> | |||
If you can clobber Z and N | ==== 4 bytes ==== | ||
''If you can clobber Z and N'':<br> | |||
ROR $00,X</ | <pre>ROL $00,X | ||
ROR $00,X</pre> | |||
==== Synthetic (4—5 bytes) ==== | |||
{2-cycle delay} + {10-cycle delay} | |||
=== 13 cycles === | === 13 cycles === | ||
5 bytes | ==== 4 bytes ==== | ||
''If you can clobber S'':<br> | |||
<pre>PHA | |||
PHA | |||
PHP | |||
PLP</pre> | |||
==== Synthetic (5 bytes) ==== | |||
{2-cycle delay} + {11-cycle delay} | |||
=== 14 cycles === | === 14 cycles === | ||
If you | ==== 3 bytes ==== | ||
''If you have @rts14'':<br> | |||
<pre>JSR @rts14</pre> | |||
==== Synthetic (4 bytes) ==== | |||
{7-cycle delay} + {7-cycle delay} | |||
=== 15 cycles === | === 15 cycles === | ||
If you | ==== 3 bytes ==== | ||
''If you have @rts15'':<br> | |||
<pre>JSR @rts15</pre> | |||
If you | ==== 4 bytes ==== | ||
''If you can clobber S and you have @rts12'':<br> | |||
<pre>PHA | |||
JSR @rts12</pre> | |||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PHA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
==== 5 bytes ==== | |||
''If you can clobber Z, N and A and you have @rts12'':<br> | |||
<pre>LDA $00 | |||
JSR @rts12</pre> | |||
''If you can clobber Z, N and X'':<br> | |||
<pre>PHA | |||
TSX | |||
PLA | |||
TXS | |||
PLA</pre> | |||
''If you can clobber A and X'':<br> | |||
<pre>PHP | |||
TSX | |||
PLA | |||
TXS | |||
PLP</pre> | |||
''If you can clobber Z, N and V and you have @rts12'':<br> | |||
<pre>BIT $00 | |||
JSR @rts12</pre> | |||
''If you have @zptemp and you have @rts12'':<br> | |||
<pre>STA @zptemp | |||
JSR @rts12</pre> | |||
''If you can use unofficial opcodes and you have @rts12'':<br> | |||
<pre>NOP $00 | |||
JSR @rts12</pre> | |||
==== Synthetic (5—6 bytes) ==== | |||
{2-cycle delay} + {13-cycle delay} | |||
=== 16 cycles === | === 16 cycles === | ||
If you can clobber Z, N, S | ==== 4 bytes ==== | ||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PLA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
==== Synthetic (4—5 bytes) ==== | |||
{2-cycle delay} + {14-cycle delay} | |||
=== 17 cycles === | === 17 cycles === | ||
If | ==== 4 bytes ==== | ||
''If you have @rts15'':<br> | |||
<pre>NOP | |||
JSR @rts15</pre> | |||
''If you can clobber S and you have @rts14'':<br> | |||
<pre>PHA | |||
JSR @rts14</pre> | |||
==== 5 bytes ==== | |||
''If you can clobber Z, N and A and you have @rts14'':<br> | |||
<pre>LDA $00 | |||
JSR @rts14</pre> | |||
''If you can clobber Z, N and V and you have @rts14'':<br> | |||
<pre>BIT $00 | |||
JSR @rts14</pre> | |||
''If you have @zptemp and you have @rts14'':<br> | |||
<pre>STA @zptemp | |||
JSR @rts14</pre> | |||
''If you can clobber S'':<br> | |||
<pre>PHA | |||
PHP | |||
PLP | |||
PHP | |||
PLP</pre> | |||
''If you can use unofficial opcodes and you have @rts14'':<br> | |||
<pre>NOP $00 | |||
JSR @rts14</pre> | |||
==== Synthetic (6 bytes) ==== | |||
{7-cycle delay} + {10-cycle delay} | |||
=== 18 cycles === | === 18 cycles === | ||
==== 4 bytes ==== | |||
''If you can clobber S and you have @rts15'':<br> | |||
<pre>PHA | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N, A and S and you have @rts14'':<br> | |||
<pre>PLA | |||
JSR @rts14</pre> | |||
==== 5 bytes ==== | |||
''If you can clobber Z, N and A and you have @rts15'':<br> | |||
<pre>LDA $00 | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N and V and you have @rts15'':<br> | |||
<pre>BIT $00 | |||
JSR @rts15</pre> | |||
''If you have @zptemp and you have @rts15'':<br> | |||
<pre>STA @zptemp | |||
JSR @rts15</pre> | |||
''If you can clobber S and you have @rts12'':<br> | |||
<pre>PHA | |||
PHA | |||
JSR @rts12</pre> | |||
''If you can use unofficial opcodes and you have @rts15'':<br> | |||
<pre>NOP $00 | |||
JSR @rts15</pre> | |||
==== Synthetic (5—6 bytes) ==== | |||
{2-cycle delay} + {16-cycle delay} | |||
=== 19 cycles === | === 19 cycles === | ||
==== 4 bytes ==== | |||
''If you can clobber Z, N, A and S and you have @rts15'':<br> | |||
<pre>PLA | |||
JSR @rts15</pre> | |||
==== 5 bytes ==== | |||
''If you have @rts12'':<br> | |||
<pre>PHP | |||
PLP | |||
JSR @rts12</pre> | |||
''If you have @rts15'':<br> | |||
<pre>NOP | |||
NOP | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PHA | |||
PLA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
''If you can clobber S and you have @rts14'':<br> | |||
<pre>NOP | |||
PHA | |||
JSR @rts14</pre> | |||
==== 6 bytes ==== | |||
''Clobbers nothing, requires nothing'':<br> | |||
<pre>PHP | |||
ROL $00,X | |||
ROR $00,X | |||
PLP</pre> | |||
=== 20 cycles === | === 20 cycles === | ||
If you can clobber Z, N, S | ==== 5 bytes ==== | ||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PLA | |||
PLA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
If | ''If you can clobber S and you have @rts14'':<br> | ||
6 bytes: { | <pre>PHA | ||
PHA | |||
JSR @rts14</pre> | |||
==== 6 bytes ==== | |||
''If you can clobber Z, N, A and X'':<br> | |||
<pre>TSX | |||
PLA | |||
PLA | |||
PLA | |||
PLA | |||
TXS</pre> | |||
''If you can clobber S'':<br> | |||
<pre>PHA | |||
PHA | |||
PHP | |||
PLP | |||
PHP | |||
PLP</pre> | |||
==== Synthetic (5—7 bytes) ==== | |||
{2-cycle delay} + {18-cycle delay} | |||
=== 21 cycles === | === 21 cycles === | ||
If | ==== 5 bytes ==== | ||
''If you can clobber Z, N and Y'':<br> | |||
<pre>LDY #4 | |||
@D: | |||
DEY | |||
BNE @D</pre> | |||
''If you can clobber Z, N and X'':<br> | |||
<pre>LDX #4 | |||
@D: | |||
DEX | |||
BNE @D</pre> | |||
''If you can clobber S and you have @rts15'':<br> | |||
<pre>PHA | |||
PHA | |||
JSR @rts15</pre> | |||
==== Synthetic (5—6 bytes) ==== | |||
5 bytes | {7-cycle delay} + {14-cycle delay} | ||
=== 22 cycles === | === 22 cycles === | ||
==== 5 bytes ==== | |||
''If you have @rts15'':<br> | |||
<pre>PHP | |||
PLP | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N, A and S and you have @rts14'':<br> | |||
<pre>PLA | |||
PLA | |||
JSR @rts14</pre> | |||
==== 6 bytes ==== | |||
''If you can clobber Z, N and Y'':<br> | |||
<pre>LDY #3 | |||
@D: | |||
NOP | |||
DEY | |||
BNE @D</pre> | |||
''If you can clobber Z, N and X'':<br> | |||
<pre>LDX #3 | |||
@D: | |||
NOP | |||
DEX | |||
BNE @D</pre> | |||
''If you can clobber S and you have @rts12'':<br> | |||
<pre>PHA | |||
PHP | |||
PLP | |||
JSR @rts12</pre> | |||
==== 7 bytes ==== | |||
''If you have @rts12'':<br> | |||
<pre>PHP | |||
BIT $00 | |||
PLP | |||
JSR @rts12</pre> | |||
If you can clobber | ''If you can clobber X'':<br> | ||
<pre>PHP | |||
PHA | |||
TSX | |||
PLA | |||
TXS | |||
PLA | |||
PLP</pre> | |||
==== Synthetic (6—8 bytes) ==== | |||
{2-cycle delay} + {20-cycle delay} | |||
=== 23 cycles === | === 23 cycles === | ||
If | ==== 5 bytes ==== | ||
''If you can clobber Z, N, A and S and you have @rts15'':<br> | |||
<pre>PLA | |||
PLA | |||
JSR @rts15</pre> | |||
If | ==== 6 bytes ==== | ||
6 bytes | ''If you can clobber Z, N, A and X and you have @rts15'':<br> | ||
<pre>TSX | |||
PLA | |||
TXS | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PHA | |||
PLA | |||
PLA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
==== Synthetic (6—7 bytes) ==== | |||
{2-cycle delay} + {21-cycle delay} | |||
=== 24 cycles === | === 24 cycles === | ||
If you | ==== 6 bytes ==== | ||
''If you have @rts12'':<br> | |||
<pre>JSR @rts12 | |||
JSR @rts12</pre> | |||
If | ''If you have @rts15'':<br> | ||
<pre>NOP | |||
PHP | |||
PLP | |||
JSR @rts15</pre> | |||
''If you can clobber Z, N, A and S'':<br> | |||
<pre>PLA | |||
PLA | |||
PLA | |||
PLA | |||
PLA | |||
PLA</pre> | |||
''If you can clobber S and you have @rts14'':<br> | |||
<pre>PHA | |||
PHP | |||
PLP | |||
JSR @rts14</pre> | |||
==== 7 bytes ==== | |||
''If you can clobber Z, N, A and X'':<br> | |||
<pre>TSX | |||
PLA | |||
PLA | |||
PLA | |||
PLA | |||
PLA | |||
TXS</pre> | |||
''If you have @rts14'':<br> | |||
<pre>PHP | |||
BIT $00 | |||
PLP | |||
JSR @rts14</pre> | |||
''If you can clobber S'':<br> | |||
<pre>PHA | |||
PHP | |||
PLP | |||
PHP | |||
PLP | |||
PHP | |||
PLP</pre> | |||
==== 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 22:01, 13 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
, orNOP
), followed byRTS
. - @rts15 means you know a memory address that contains a
JMP
that jumps to another location that containsRTS
. - Alternatively, @rts15 means you know a memory address that contains a harmless 3-cycle instruction that fits your constraints (such as
LDA $00
), followed byRTS
.
0 cycles
0 bytes
1 cycle
Impossible
2 cycles
Canonical (1 bytes)
NOP
3 cycles
1 bytes
If you can clobber S:
PHA
2 bytes
If you can clobber Z, N and A:
LDA $00
If you can clobber Z, N and V:
BIT $00
If you have @zptemp:
STA @zptemp
If you can use unofficial opcodes:
NOP $00
Canonical (3 bytes)
JMP *+3
4 cycles
1 bytes
If you can clobber Z, N, A and S:
PLA
Synthetic (2 bytes)
{2-cycle delay} + {2-cycle delay}
5 cycles
Synthetic (2—4 bytes)
{2-cycle delay} + {3-cycle delay}
6 cycles
2 bytes
If you can clobber S:
PHA PHA
Synthetic (3 bytes)
{2-cycle delay} + {4-cycle delay}
7 cycles
Canonical (2 bytes)
PHP PLP
8 cycles
2 bytes
If you can clobber Z, N, A and S:
PLA PLA
3 bytes
If you can clobber Z, N, A and X:
TSX PLA TXS
Synthetic (3—4 bytes)
{2-cycle delay} + {6-cycle delay}
9 cycles
Synthetic (3 bytes)
{2-cycle delay} + {7-cycle delay}
10 cycles
3 bytes
If you can clobber S:
PHA PHP PLP
4 bytes
Clobbers nothing, requires nothing:
PHP BIT $00 PLP
11 cycles
3 bytes
If you can clobber Z, N, A and S:
PHA PLA PLA
Synthetic (4 bytes)
{2-cycle delay} + {9-cycle delay}
12 cycles
3 bytes
If you have @rts12:
JSR @rts12
If you can clobber Z, N, A and S:
PLA PLA PLA
4 bytes
If you can clobber Z and N:
ROL $00,X ROR $00,X
Synthetic (4—5 bytes)
{2-cycle delay} + {10-cycle delay}
13 cycles
4 bytes
If you can clobber S:
PHA PHA PHP PLP
Synthetic (5 bytes)
{2-cycle delay} + {11-cycle delay}
14 cycles
3 bytes
If you have @rts14:
JSR @rts14
Synthetic (4 bytes)
{7-cycle delay} + {7-cycle delay}
15 cycles
3 bytes
If you have @rts15:
JSR @rts15
4 bytes
If you can clobber S and you have @rts12:
PHA JSR @rts12
If you can clobber Z, N, A and S:
PHA PLA PLA PLA
5 bytes
If you can clobber Z, N and A and you have @rts12:
LDA $00 JSR @rts12
If you can clobber Z, N and X:
PHA TSX PLA TXS PLA
If you can clobber A and X:
PHP TSX PLA TXS PLP
If you can clobber Z, N and V and you have @rts12:
BIT $00 JSR @rts12
If you have @zptemp and you have @rts12:
STA @zptemp JSR @rts12
If you can use unofficial opcodes and you have @rts12:
NOP $00 JSR @rts12
Synthetic (5—6 bytes)
{2-cycle delay} + {13-cycle delay}
16 cycles
4 bytes
If you can clobber Z, N, A and S:
PLA PLA PLA PLA
Synthetic (4—5 bytes)
{2-cycle delay} + {14-cycle delay}
17 cycles
4 bytes
If you have @rts15:
NOP JSR @rts15
If you can clobber S and you have @rts14:
PHA JSR @rts14
5 bytes
If you can clobber Z, N and A and you have @rts14:
LDA $00 JSR @rts14
If you can clobber Z, N and V and you have @rts14:
BIT $00 JSR @rts14
If you have @zptemp and you have @rts14:
STA @zptemp JSR @rts14
If you can clobber S:
PHA PHP PLP PHP PLP
If you can use unofficial opcodes and you have @rts14:
NOP $00 JSR @rts14
Synthetic (6 bytes)
{7-cycle delay} + {10-cycle delay}
18 cycles
4 bytes
If you can clobber S and you have @rts15:
PHA JSR @rts15
If you can clobber Z, N, A and S and you have @rts14:
PLA JSR @rts14
5 bytes
If you can clobber Z, N and A and you have @rts15:
LDA $00 JSR @rts15
If you can clobber Z, N and V and you have @rts15:
BIT $00 JSR @rts15
If you have @zptemp and you have @rts15:
STA @zptemp JSR @rts15
If you can clobber S and you have @rts12:
PHA PHA JSR @rts12
If you can use unofficial opcodes and you have @rts15:
NOP $00 JSR @rts15
Synthetic (5—6 bytes)
{2-cycle delay} + {16-cycle delay}
19 cycles
4 bytes
If you can clobber Z, N, A and S and you have @rts15:
PLA JSR @rts15
5 bytes
If you have @rts12:
PHP PLP JSR @rts12
If you have @rts15:
NOP NOP JSR @rts15
If you can clobber Z, N, A and S:
PHA PLA PLA PLA PLA
If you can clobber S and you have @rts14:
NOP PHA JSR @rts14
6 bytes
Clobbers nothing, requires nothing:
PHP ROL $00,X ROR $00,X PLP
20 cycles
5 bytes
If you can clobber Z, N, A and S:
PLA PLA PLA PLA PLA
If you can clobber S and you have @rts14:
PHA PHA JSR @rts14
6 bytes
If you can clobber Z, N, A and X:
TSX PLA PLA PLA PLA TXS
If you can clobber S:
PHA PHA PHP PLP PHP PLP
Synthetic (5—7 bytes)
{2-cycle delay} + {18-cycle delay}
21 cycles
5 bytes
If you can clobber Z, N and Y:
LDY #4 @D: DEY BNE @D
If you can clobber Z, N and X:
LDX #4 @D: DEX BNE @D
If you can clobber S and you have @rts15:
PHA PHA JSR @rts15
Synthetic (5—6 bytes)
{7-cycle delay} + {14-cycle delay}
22 cycles
5 bytes
If you have @rts15:
PHP PLP JSR @rts15
If you can clobber Z, N, A and S and you have @rts14:
PLA PLA JSR @rts14
6 bytes
If you can clobber Z, N and Y:
LDY #3 @D: NOP DEY BNE @D
If you can clobber Z, N and X:
LDX #3 @D: NOP DEX BNE @D
If you can clobber S and you have @rts12:
PHA PHP PLP JSR @rts12
7 bytes
If you have @rts12:
PHP BIT $00 PLP JSR @rts12
If you can clobber X:
PHP PHA TSX PLA TXS PLA PLP
Synthetic (6—8 bytes)
{2-cycle delay} + {20-cycle delay}
23 cycles
5 bytes
If you can clobber Z, N, A and S and you have @rts15:
PLA PLA JSR @rts15
6 bytes
If you can clobber Z, N, A and X and you have @rts15:
TSX PLA TXS JSR @rts15
If you can clobber Z, N, A and S:
PHA PLA PLA PLA PLA PLA
Synthetic (6—7 bytes)
{2-cycle delay} + {21-cycle delay}
24 cycles
6 bytes
If you have @rts12:
JSR @rts12 JSR @rts12
If you have @rts15:
NOP PHP PLP JSR @rts15
If you can clobber Z, N, A and S:
PLA PLA PLA PLA PLA PLA
If you can clobber S and you have @rts14:
PHA PHP PLP JSR @rts14
7 bytes
If you can clobber Z, N, A and X:
TSX PLA PLA PLA PLA PLA TXS
If you have @rts14:
PHP BIT $00 PLP JSR @rts14
If you can clobber S:
PHA PHP PLP PHP PLP PHP PLP
Synthetic (8 bytes)
{7-cycle delay} + {17-cycle delay}
More
Bisqwit's 6502 delay_n macro set for ca65: http://bisqwit.iki.fi/src/6502-inline_delay.7z