Page 1 of 1
AND Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 4:42 pm
by Petruza
Marat fayzullin's emulator source code says 2.
FCEUX source code says 3.
I have an old note on my emu source saying Bregalad said it takes 3 cycles.
But according to
http://nesdev.com/6502_cpu.txt, all immediates take 2 cycles.
So what is it, 2 or 3?
Edit: it's 2 cycles, not 3.
Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 5:04 pm
by lidnariq
Opcode $29 is not LDA. (It's AND)
Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 5:17 pm
by Movax12
I have no reason to doubt this favorite page for 6502 instructions:
http://www.obelisk.demon.co.uk/6502/reference.html
It says 2 cycles for LDA Immediate. In fact, all immediate instructions are 2 cycles.
Would be nice if that page also had a section on illegal/undocumented/unofficial opcodes.
Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 6:57 pm
by tokumaru
Why is this a poll? The amount of cycles a 6502 instruction takes to execute is a fact, and there's only one correct answer. There are no opinions here.
Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 7:20 pm
by lidnariq
Also, where exactly in FCEUX's source do you think it says AND #imm takes 3 cycles?
Re: AND Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 8:33 pm
by Petruza
lidnariq wrote:Opcode $29 is not LDA. (It's AND)
You're right, I thought of AND but wrote LDA.
Me neither, in fact I based my CPU emulator on that page.
lidnariq wrote:Also, where exactly in FCEUX's source do you think it says AND #imm takes 3 cycles?
Now that I reviewed FCEUX's source, it says 2.
I had the cycle table copied from FCEUX, but I must have changed the 2 to 3 after I read in this forum that it was so (I commented in my code that Bregalad said that, but I could be wrong about that too) so I atributed the 3 to FCEUX, but it was actually me.
Mistery solved, thanks guys.
TL;DR: it's 2 cycles, not 3.
Edit: this is the post I'm talking about:
viewtopic.php?t=6183#p58516
Although now that I read it, by "and $xx" he might be referring to zero page, and not immediate, but AND zero page IS actually 3 cycles on that page.
Re: AND Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Thu Jun 26, 2014 10:47 pm
by Sik
I think I know where the confusion comes from: internally, the operation does indeed take three cycles, but the last cycle overlaps with fetching the next opcode, so effectively it behaves like it takes two cycles. I recall reading this in some C64 documentation.
Re: AND Immediate, opcode $29, takes 2 or 3 cycles?
Posted: Fri Jun 27, 2014 10:33 am
by koitsu
OP has already figured out the answer, but I thought I'd write something coherent/definitive as a summary:
- Opcode $29, AND # (AND immediate) -- takes 2 cycles. I don't care what other documentation says; there are 6502 docs all over the place that have things wrong/typos/snakes in the grass. On the 6502 it's a 2-cycle opcode. If you're writing an emulator, or writing timing-sensitive code, it's 2 cycles.
- Opcode $A9, LDA # (LDA immediate) -- because it was mentioned earlier in the thread erroneously and I want to clarify -- takes 2 cycles.