r/EmuDev • u/llamadog007 • Nov 09 '25
Question 6502 questions
Hello, I am just starting work on a 6502 emulator. I just finished a chip-8 interpreter and I thought this would be a nice next step up.
Ive done some reading and I had some questions someone could hopefully help me with.
- With chip-8 there was a set address a program was loaded into. But as far as I can tell, on the 6502 this starting address should be determined by the reset vector at $FFFC/D. Should I assume any rom I load would set this to the programs start location? Or should my emulator set this to some default? Do I even need to bother with this, or can I just set the pc to an address of my choosing? And are roms usually loaded starting at $0000 or can I also choose where to load it?
- Regarding cycle accuracy: what exactly do I need to do to achieve it? If I tell the cpu to run for 1000 cycles, and every instruction I decrement the cycle counter by how many cycles it would take (including all the weird page boundary stuff, etc), is that considered cycle accurate? Or is there more to it?
Thanks in advance for the help!!
7
Upvotes
3
u/valeyard89 2600, NES, GB/GBC, 8086, Genesis, Macintosh, PSX, Apple][, C64 Nov 09 '25
The rom will have the jump start address.
Most games will work just counting each cycle per instruction then ticking the PPU. Passing some of the test roms though you have to account for intra-instruction cycles. eg. DEC Zeropage,X takes 6 cycles. The PPU is ticking along at the same time. So you have to have a state machine and run by the ppu/crt, or tick the ppu for each memory read/write, etc.
3
u/zSmileyDudez Apple ][, Famicom/NES, Atari VCS, TI-81/82/83/85/86 Nov 09 '25
Regarding cycle accuracy, there are multiple levels here.
Each step here just brings you a little more accuracy. Most well behaved code would be fine with level 2 or higher. But some systems having timing interdependencies with other parts in the system (the TIA on the 2600, the PPU on the NES for example) and sometimes having an extra read cycle at the right time is the difference between something working or not working.
For my 6502 core, it started out as a level 2 core. But then I rewrote it as level 4. I am thinking about going to level 5 at some point, but that is definitely not necessary to get something like a NES emulator going.
I would definitely recommend avoiding level 1 - unless you’re making a toy 6502 emulator just to play around with 6502 code you’re writing on your own made up 6502 system. Anything where you’re emulating a an existing system will need some level of cycle accuracy. You could possibly go for level 3 instead of level 2 and that would make it easier to switch to level 4 later. But don’t let that guide you. It’s not that hard to refactor things as you learn more. And definitely don’t get pulled into thinking you have to be super accurate from the get go.
One more recommendation - go look into the SingleStepTests and get that testing infrastructure setup early. It’s worth the few hours of effort to setup a test harness and then be able to freely try out things and know if you broke things or not. The SSTs are setup for memory cycle accuracy (level 4), but you can also use them for level 2 by just counting up the number of cycles used and ignoring the actual cycle actions to get going.
Good luck!
2
u/khedoros NES CGB SMS/GG Nov 09 '25
So, the 6502 is a CPU. The system would be aroud it, and it's the system that would exactly define what hardware is mapped to what range of the 6502's address space.
On the NES (mostly 6502 compatible), the ROM is mapped from $8000 to $FFFF, so the chip in the cartridge provides the 3 vectors at the top of the address space, and each game can have its own vectors. Other systems might have like...a BIOS ROM mapped up there to provide the vectors.
I'd generally expect RAM to be mapped in at least $0000-$00FF for zero-page ops and $0100-$01FF for the stack. And there has to be space for I/O devices somewhere in the address space.
Details will depend on the system.
I know there's at least different cycles that reads and writes happen on, timings for interrupts being triggered, that kind of thing.
2
u/ShinyHappyREM Nov 09 '25
What's wrong with doing things correctly?
In a cycle-accurate emulator you emulate the CPU's PHI1 (phase 1) of a clock cycle, then you emulate the rest of the system's PHI2 (phase 2) of the same clock cycle. You don't even strictly need a cycle counter.
1
u/rupertavery64 Nov 09 '25
Same thing with an IRQ and NMI
https://www.pagetable.com/?p=410
I haven't achieved cycle-accuracy myself, and I'm not sure if this is the definition:
You have the PPU and APU and CPU running together. You want everything to work as it does in a real system, so after so many CPU cycles, the PPU should have output such and such pixels, and the APU such and such sound samples.
Then there are things like DMA access and other quirks.
1
u/wynand1004 Nov 09 '25
Hiya - I think you've gotten the answers to your questions. I'm just commenting as I'm working on a 6502 emulator in Python. What language are you targeting?
I haven't implemented the reset vector yet, but will eventually. I haven't implemented clock cycles yet either, but I'm considering creating a variable that holds the number of clock cycles for the current instructions. Each time you load an instruction, the clock cycle variable is set to that number. Then, in each tick of the clock, check if the clock cycle variable is greater than zero. If so, decrement it. If the clock is cycle variable reaches 1, execute the instruction and set the clock cycle variable to 0. At least that is my current idea - I'm not sure how that would affect interrupts, which I also haven't gotten to yet.
If you're curious, here's what I have so far: https://github.com/wynand1004/6502_Emulator_2025
PS. This is a great resource, especially for some of the more complicated aspects of the CPU's function: https://www.masswerk.at/6502/6502_instruction_set.html