7 ms·
A new cycle-stepped 6502 CPU emulator
- eatonphil 7y agoThis is a great introduction to emulation in general. While I was trying to figure out how to write my first emulator I thought it would be simpler to start with a Game Boy Advance or an 8-bit processor like the 6502 or the PICO-8. After a few false starts it turned out to be simplest to write a simple x86 emulator since I already had all the tools locally (assemblers, disassemblers, compilers, etc.) and a working reference environment (my laptop). Here's the walkthrough I wrote for building an x86 emulator in JavaScript that you can use to run simple C programs compiled with GCC/Clang: http://notes.eatonphil.com/emulator-basics-a-stack-and-register-machine.html http://notes.eatonphil.com/emulator-basics-a-stack-and-regis... Now that I did this though I'm still trying to get into a GBA emulator.
- jsmolka 7y ago> I thought it would be simpler to start with a Game Boy Advance or an 8-bit processor like the 6502 The 8-bit processor will be much more approachable. I have worked on my GBA emulator for around one year now. Before that I tried the classic GB and its 8-bit CPU was so much easier to implement. The GBA's ARM7TDMI alone took me 3 months to complete, even with extensive testing [1]. [1] https://github.com/jsmolka/eggvance/tree/master/tests https://github.com/jsmolka/eggvance/tree/master/tests
- mrec 7y agoRetro emulators seem to be increasing in popularity lately. I've seen a few articles now covering the "how", but does anyone have pointers to something explaining the "why"? Is it a Zachtronics-style technical challenge? Specific nostalgia for the software that ran on these ancient platforms? More general nostalgia for the days when platform stacks were still thin and comprehensible [1]? Something else? [1] https://ptrthomas.files.wordpress.com/2006/06/jtrac-callstack1.png https://ptrthomas.files.wordpress.com/2006/06/jtrac-callstac...
- zeta0134 7y agoGiven how complex a successful emulator project tends to be, I expect the answer will be highly personal. In my case, it's a combination of nostalgia for the games I grew up playing, curiosity and respect for the developers who were working with such unusual constraints (by today's standards), and a strong desire to help preservationist efforts. I want to help ensure that future curious minds have a wide variety of tools available to play older software once the hardware becomes difficult to acquire. Well, that's the philosophical answer anyway; the more practical answer is that I simply enjoy the challenge, and nostalgia is a great spark to motivate the effort.
- Lerc 7y agoA bit of all of that I think. I'm making a fantasy console with an 8 bit instruction set and I'm having quite a bit of fun writing assembly for it myself. A few years ago I added a graphics mode to a DCPU-16 emulator and wrote a game of Pac-Man for it and it felt extremely liberating to write code that just does the job. I think that aspect could be achieved in other environments if you have a extremely thin API, That is, I think, one of the appeals of the PICO-8. While the Lua language is pretty powerful, the interface to the virtual hardware is thin enough that there is very little API wrangling. If you want it, build it. As for the technical challenge side of things, I have long wanted to write a Language like BASIC in 8-bit ASM. They squeezed a lot into 4 to 16 k back in the day, it would be fun to have a go myself.
- jdc 7y agoFor me it's the appeal of potentially mastering the _entire_ machine.
- ddingus 7y agoFully understanding is one reason. Another is to preserve experiences. There is a lot of great retro software out there. Yet another is development. Many older machines enjoy an active development scene. Emulation can help this along. Fun.
- byuu 7y agoFor me, the "why" is that I grew up playing RPGs, and around my early teens I discovered the west missed out on dozens of major games (even Dragon Quest 5 and Final Fantasy 5!) I taught myself programming and reverse engineering so that I could fan translate these games. After I finished a few of them, I became disheartened that my work had trouble running on real game consoles due to emulator inaccuracies. The folks writing emulators at the time took many shortcuts to run on the slower computers we had back then, but in spite of PCs getting faster, they didn't want to incur any speed hits or break any other fan translations relying on emulator bugs. So I switched focus and started writing my own emulators instead. I was filling a niche that at the time wasn't covered. These days, I mostly keep at it because it's fun, I like the games on these old systems, and I'd like to preserve the games for future generations as faithfully as possible. (if you're bored and would like a really in-depth answer, see https://byuu.org/about https://byuu.org/about for a full history.)
- cabaalis 7y agoI've been thinking of writing a 6502 emulator. My plan was to start with cycle-stepping. I'm purposefully not looking at other emulators or code :) I do have something I've been wondering, especially since I haven't yet written any complicated code yet for this plan. To emulate a 1mhz clock on a modern CPU, do you have to do things like check time deltas to "limit" the emulator speed? (If this was address in the article, know that I stopped reading when I saw code.. see above purpose)
- thristian 7y agoMost (game console) emulators don't try to limit the speed of actual CPU emulation directly, since that makes the code more complex, and makes testing slower, etc. Instead, the only time the emulated system speed is when it has to talk to humans - render a frame of video, generate audio samples, etc. So if you have a 60Hz monitor (as most people do), and your emulated CPU takes 180,000 cycles (or whatever) to render a video frame, then your emulator just runs at top speed until a video frame is available, waits for the "vertical synchronisation" signal from your graphics API, draws the frame with the graphics API, and repeats. Because of the delay introduced by vertical sync, your emulated CPU's average speed will be correct, even though the instantaneous speed is either "way too fast" or "zero".
- zeta0134 7y agoAdding on to this, in the context of a typical emulator, it's important that the other hardware be emulated mostly in lockstep with the CPU. Each time the 6502 reads or writes memory, the hardware registers it is talking to should be "caught up" so that things like video and audio signal timings work correctly. There are a bunch of different approaches to this depending on how accurate you want things to be, but the most straightforward is to just clock the components directly, one after the other. I have a function in RusticNES that clocks the CPU + APU, another that clocks the PPU, and since the PPU runs 3x as fast, a global clock function that looks like: pub fn cycle(&mut self) { cycle_cpu::run_one_clock(self); self.master_clock = self.master_clock + 12; // Three PPU clocks per every 1 CPU clock self.ppu.clock(&mut *self.mapper); self.ppu.clock(&mut *self.mapper); self.ppu.clock(&mut *self.mapper); self.apu.clock_apu(&mut *self.mapper); } This way, all of the emulated chips remain mostly synchronized with each other, but I don't bother to synchronize with the host until either the PPU is ready to draw a frame, or the APU has filled its audio buffer. This approach uses the CPU cycle as the boundary, but there are other approaches to consider. You could run an entire opcode and then "catch up" the rest of the hardware (maybe easier to manage CPU state), or run entire scanlines at once. (Maybe easier to manage PPU state at first? The NES's PPU is well understood, but very tricky to emulate correctly.)
- Swivekth18 7y agoI like how it's pretty much standard to expect emulators to have a WASM port running in the browser at full performance at this point. Atwood's law is 12 years old but still going strong.
- boomlinde 7y agoThis is really cool. I like the approach of exposing the pins as its API. Super flexible.
- djmips 7y agoI feel like with this approach and you are getting to a better place to convert the design into an HDL and running on an FPGA.