AND Immediate, opcode $29, takes 2 or 3 cycles?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

AND Immediate, opcode $29, takes 2 or 3 cycles?

Post 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.
Last edited by Petruza on Thu Jun 26, 2014 8:34 pm, edited 3 times in total.
lidnariq
Posts: 10677
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?

Post by lidnariq »

Opcode $29 is not LDA. (It's AND)
User avatar
Movax12
Posts: 529
Joined: Sun Jan 02, 2011 11:50 am

Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?

Post 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.
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?

Post 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.
lidnariq
Posts: 10677
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: LDA Immediate, opcode $29, takes 2 or 3 cycles?

Post by lidnariq »

Also, where exactly in FCEUX's source do you think it says AND #imm takes 3 cycles?
User avatar
Petruza
Posts: 311
Joined: Mon Dec 22, 2008 10:45 pm
Location: Argentina

Re: AND Immediate, opcode $29, takes 2 or 3 cycles?

Post by Petruza »

lidnariq wrote:Opcode $29 is not LDA. (It's AND)
You're right, I thought of AND but wrote LDA.
Movax12 wrote:I have no reason to doubt this favorite page for 6502 instructions: http://www.obelisk.demon.co.uk/6502/reference.html
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
Bregalad wrote:... according to this. : http://www.6502.org/tutorials/6502opcodes.html
However this page contains at least 1 error : and $xx is 3 cycles and not 2...
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.
Sik
Posts: 1589
Joined: Thu Aug 12, 2010 3:43 am

Re: AND Immediate, opcode $29, takes 2 or 3 cycles?

Post 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.
User avatar
koitsu
Posts: 4203
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: AND Immediate, opcode $29, takes 2 or 3 cycles?

Post 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.
Post Reply