CPU Quirks

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

beannaich
Posts: 207
Joined: Wed Mar 31, 2010 12:40 pm

CPU Quirks

Post by beannaich »

I am aware of a few CPU quirks, like the dummy write on INC. Is there a list somewhere of the other quirks, or can one of you walking NES information archives whip one up? Much appreciated! ;D
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

There's the error on wrap for the JMP Indirect instruction, you ask to do an indirect jmp at ($1FF) and it reads the 16-bit jump address from $1FF and $100 instead of $1FF and $200.

Then there's the extra cycle for when some instructions cross pages when adding X or Y.

Then there's the zeropage instructions which wrap back to the zeropage when you add a X to them instead of advancing to the $100 page.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
beannaich
Posts: 207
Joined: Wed Mar 31, 2010 12:40 pm

Post by beannaich »

Those are all good quirks, what about other dummy reads/writes? I'm trying to make my CPU not require a timing table, instead just adding cycles for read/write (and page boundary crossing where appropriate).
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

I think there are dummy reads for the wrong offset when there's a penalty cycle. Not 100% sure here, but I think when instructions like LDA xxxx,X cross to the next page, it will read from the incorrect page, then read from the correct page.
Wonder if this could be used to read both PPUSTAT and PPUDATA from one instruction?
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
beannaich
Posts: 207
Joined: Wed Mar 31, 2010 12:40 pm

Post by beannaich »

Alright, and am I correct that INC does a dummy write? Say you do INC $AA, it'll read the value at $AA, then write that value to $AA, increase then write to $AA again?

If so, does DEC do the same thing?
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

I think all the "read-modify-write" instructions (including shift instructions) do that.

What's bad is that some games want to see the dummy write, and not the real write afterwards. Good ol MMC1 games and INC xxxx to write FF as the dummy value.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

I couldn't find it right now, but I'm pretty sure someone in this board posted a link to a document that lists everything each instruction does every cycle, so it should contain all the dummy reads/writes. I'll update here if I can find it.
beannaich
Posts: 207
Joined: Wed Mar 31, 2010 12:40 pm

Post by beannaich »

It seems that this is a good idea then! There are a few things still confusing me, take for example LDA (xx,X) (Assume all reads/writes consume 1 CPU cycle):

Read Instruction (PC)
Read Zero Page Address (PC + 1)
Add X, with wrapping
Read Low Byte ($00)
Read High Byte ($20)
Read Data ($2000)

This sequence totals 5 cycles, but the actual instruction requires 6.. Where is the missing cycle coming from?

EDIT: That would be great tokumaru!
User avatar
tokumaru
Posts: 12106
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

I think this is the document. The last third of the document appears to detail several instructions.
User avatar
clueless
Posts: 496
Joined: Sun Sep 07, 2008 7:27 am
Location: Seatlle, WA, USA

Post by clueless »

The 6502.org forums have details about instruction execute and the PLA (programmable logic array, not to be confused with 'pull accumulator').

The following links mainly discuss how the 6502 handles to so called "illegal instructions". However, they also contain clues about what the 6502 does on each clock cycle while executing an instruction.

http://forum.6502.org/viewtopic.php?t=1406
http://www.pagetable.com/?p=39

Once this data is gathered, I think that it would make an excellent addition to the wiki.

EDIT:
http://www.geocities.jp/team_zero_three/FC/index_e.html
(search for first use of the word "dummy")

http://nesdev.com/bbs/viewtopic.php?p=11288#11288

EDIT #2: Found this on the geocities.jp page:
Although the FC/NES 6502 is not completely same as the general NMOS6502 chip, it has been verified that the FC/NES 6502 also has undocumented instructions and executes RMWW for RMW instructions.
beannaich
Posts: 207
Joined: Wed Mar 31, 2010 12:40 pm

Post by beannaich »

It would be so counter-intuitive, but how awesome would it be to do a cycle by cycle CPU emulation? Like LDA:

Code: Select all

int LDA(int cycles)
{
    static int ldaCycles=0;

    while (cycles > 0)
    {
        switch (ldaCycles)
        {
            case 0: // fetch address?
            ...
            case x: return (cycles - ldaCycles);
        }
    }

    return 0;
}
It would be a huge pain in the ass, but I think it would be neato!
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

I seem to remember Nintendulator emulates cycle by cycle like that.
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow »

tepples wrote:I seem to remember Nintendulator emulates cycle by cycle like that.
I didn't come to that conclusion looking at the source for v0.975. :?:
User avatar
Dwedit
Posts: 4470
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

Nestopia emulates the instructions cycle by cycle.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox »

NESICIDE wrote:
tepples wrote:I seem to remember Nintendulator emulates cycle by cycle like that.
I didn't come to that conclusion looking at the source for v0.975. :?:
I initially thought that too, but actually it does emulate cycle by cycle. Check the functions which handle different addressing modes (AM_xxx), every MemGet()/MemSet() increases cycle count.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Post Reply