3

The question is about 6502 instructions that take an extra cycle when address calculation carries over into the high byte of the address. This affects addressing modes Absolute,X, Absolute,Y, Indirect,Y. I suppose it also affects the relative branches. I'm going to call such a cycle a penalty cycle (I have seen this term in various emulator source code, and there's no better words I can think of).

Going from this document, Many instructions take a penalty cycle to manipulate the high byte of the effective address, denoted by a + in the last column. Initially, I thought that all opcodes that needed to do this incurred a penalty cycle. But the general pattern seems to be that if it needs to write something to memory, then it doesn't incur a penalty cycle.

Why is this? Can the high byte of the effective address sometimes somehow be incremented for free? I don't see how, because the 6502 uses its ALU to generate the carry, and also to add the carry into the high byte.

7

The CPU needs to do an addition of the 2-byte address after the opcode and the 8-bit unsigned displacement from X or Y register.

Since 6502 addresses are always stored as little-endian, the CPU gets the lower byte first. During the time it reads the following higher byte, it simultaneously performs addition of the lower byte just read and the contents of the index register.

If that addition results in no carry, the CPU instantly knows it does not need to update the high byte of the address. Therefore the read cycle at the correct address follows (no penalty).

When there is carry, the 6502 needs another cycle to increment the high byte of the address. During that process, the 6502 does a dummy read cycle at the incorrect address, followed by the correct read cycle (penalty).

It is obvious that there should be no 'dummy' write cycles at incorrect addresses. Therefore the 6502 always takes a 'penalty' read cycle with an (probably) incorrect address before performing the final write.

Interesting enough, the little-endianness of the 6502 made such a cycle-saving possible. Were there big-endianness, like in the 6805, there would always be a penalty in index addressing, as it happens in the 6805 itself.

So, unlike, for example, the 8080 and Z80, here the endianness actually did matter.

| improve this answer | |
  • Sequenced processing of multi word values is the basic idea of why little endian storage is used at all. Machines that doe only use word size data (and a word sized memory interface) do not gain from inverting the sequence. – Raffzahn Jul 14 at 9:01
  • @Raffzahn if they only use word-sized data and a word-sized memory interface, is there a sequence? It would be like talking about the endianness of bits within each byte – user253751 Jul 14 at 15:27
  • @user253751: If a machine like the Intel 8088 performs "inc word [addr]", it will read the lower and upper bytes at the destination address, compute the new value, and then write both the lower and upper bytes without regard for whether both had actually changed. – supercat Jul 14 at 16:43
  • @supercat That's because the Intel 8088 doesn't only operate on word-sized data through a word-sized memory interface – user253751 Jul 14 at 17:15
  • @user253751 That's exactly the point why little endian only makes sense for an architecture working on multiple words. Everywhere else it doesn't matter, so big endian can be used easing at least dump reading :) – Raffzahn Jul 14 at 18:08
2

TL;DR: It's always included.


If you compare the timing of memory writing using Absolute,X (ASL, DEC, INC, LSR, ROL, ROR, STA) or Absolute,Y (STA) with their counterpart using Absolute (without indexing), you'll note that the additional cycle is always included. It's only 'optional' for reading instructions.

Example from the (original) table (abreviated):

AND (bitwise AND with accumulator)

MODE           SYNTAX       HEX LEN TIM
Zero Page     AND $44       $25  2   3
Zero Page,X   AND $44,X     $35  2   4
Absolute      AND $4400     $2D  3   4
Absolute,X    AND $4400,X   $3D  3   4+
Absolute,Y    AND $4400,Y   $39  3   4+

+ add 1 cycle if page boundary crossed


ASL (Arithmetic Shift Left)

MODE           SYNTAX       HEX LEN TIM
Zero Page     ASL $44       $06  2   5
Zero Page,X   ASL $44,X     $16  2   6
Absolute      ASL $4400     $0E  3   6
Absolute,X    ASL $4400,X   $1E  3   7

Making the page crossing cycle optional is an optimization only added to reading instructions, so they are the exception here.

| improve this answer | |
  • On the original 6502, the repeated read is optional when it would be the last cycle before the next instruction fetch. Read-modify-write instructions could have skipped the repeated read when there is a page crossing, but that would require having a means of skipping a state within the instruction. – supercat Jul 14 at 16:41
  • Why do you say the document hosted on 6502.org is the original? – OmarL Jul 15 at 19:00
  • @OmarL because, to my knowledge it is. – Raffzahn Jul 15 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.