/Programming


Learn to develop things, get out of debt, find answers and take part in coding and programming challenges.


Members: 12
Join


Moderated by: mozzapp
up
1
up
PaulG 1796128363 [Programming] 0 comments
A cycle-accurate emulator doesn't count cycles better than a normal one. It gives up the assumption that an instruction is the unit of work, and almost everything that makes these projects hard follows from giving that up. A conventional emulator runs a loop: fetch an instruction, execute it, add its cost to a counter, then let the video and audio chips run for that many cycles to catch up. It works because most games don't notice. The CPU is treated as atomic and the rest of the machine as something that gets updated afterwards. Hardware has no such notion. A 6502 does one bus access per cycle, and an "instruction" is just the pattern of accesses that happens to come out of its decoder. Take an indexed load like `LDA $20FF,X` with `X = 3`. The CPU computes the low byte of the address first, and if the addition carries into the high byte it hasn't fixed the high byte yet. So it reads from the uncorrected address, throws the result away, and spends one more cycle reading from the right one. An emulator that reads the final address and adds five cycles gets the timing right and the behavior wrong, because that discarded read really happened on the bus. If it landed on a register with side effects, like a status register that clears a flag when read, the hardware changed state and your emulator didn't. No documentation says "this read is a bug you must reproduce", because the manual describes what the chip is meant to do. So the first cost is that the CPU core has to be written per bus cycle, with time passing at every memory access instead of once per instruction. That's slower and uglier, but it's the cheap part. The expensive part is the other chips. The NES PPU runs three dots for every CPU cycle. If a game writes a scroll register mid-scanline, the picture changes from that dot onwards. An emulator that renders the whole scanline once the CPU finishes its time slice shows the change at the wrong place, or not at all. Games that do this deliberately, for split-screen effects, are exactly the ones that break first. The Atari 2600 is the extreme case. It has no framebuffer, and the CPU feeds the video chip line by line while the beam is moving. There the picture is a product of CPU timing, and approximate timing doesn't give you a slightly wrong picture, it gives you a different one. The straightforward fix is lockstep: after every CPU cycle, advance every other component by its share of a cycle. It is correct by construction and it's slow, in a specific way. Switching components constantly wrecks locality. Nothing can be inlined across the boundary, and each chip's code has to be a state machine that can stop at any cycle and resume with its internal state intact. You stop writing "render a scanline" and start writing "what does the PPU do on dot 257 of this line", for every dot. The usual escape is catch-up scheduling. Each component keeps its own timestamp, and the CPU runs ahead until it touches something shared. At that moment the other component is advanced to the CPU's current time, and then the access happens. This recovers most of the speed, since nothing runs in lockstep unless it has to interact, but you've traded a performance problem for a correctness problem. You must define, for every interaction, what the other side's state was at time T. The CPU also has to know how long it can safely run, which means predicting when another chip will next raise an interrupt or steal the bus. When this goes wrong the emulator doesn't crash. It's off by one dot, the picture is right 99% of the time, and the bug lives in a timestamp that was updated one step too late. Some projects avoid the scheduling logic by giving each chip its own cooperative thread that yields on synchronization points, so the code reads like the hardware does. bsnes is the well-known example. You pay for it in context switches. Even with the architecture solved, the real obstacle is knowing what the hardware does. Behavior you need to match is mostly undocumented. Open bus is one example: read an unmapped address and you get whatever value was last on the data bus, which depends on what the previous cycle did. DMA is another: when a chip halts the CPU to copy memory, how many cycles the stall takes can depend on whether the CPU was on an odd or even cycle when it started. Nobody wrote that down as a feature. People found it by running test ROMs on real hardware, putting logic analyzers on buses, and in some cases by simulating the chip at transistor level from photographs of the die, as the Visual6502 project did. A cycle-accurate emulator is a reverse-engineered specification, and the specification is still being written. That also explains why debugging is so unpleasant. A one-cycle error in DMA alignment doesn't produce a wrong cycle count you can read off. It produces a sprite that glitches twenty frames later, or a game that hangs on one specific level. The failure is far from the cause, and unless you have a trace from hardware or from another accurate emulator to diff against, you're guessing which of thousands of cycles went wrong. Passing a test ROM proves one behavior is right and says nothing about the rest. Is it worth it? For playing most commercial games, probably not. A good approximate core runs them identically to the eye, on weaker hardware, with a fraction of the code. That objection is real, and anyone selling cycle accuracy as universally necessary is selling something. The case for it is narrower. Approximate emulators fix games one at a time: this title needs a hack, that one needs a timing tweak, and the list of per-game exceptions grows and never converges. A cycle-accurate model replaces the list with one description of the machine, and a game that nobody has tested yet works because the model is right, not because someone patched it. That matters most for the long tail: homebrew, demos that abuse the hardware on purpose, unreleased software, anything where preservation means "behaves like the original" and not "looks fine in the ten games people check". If that's not your target, the extra cost buys you nothing you'll see. If it is, there's no approximation of it.

A social news and discussion community