I'm starting out with an NES as the version of a 6502 I'm emulating. There are onyl small tweaks here and there to make it run as an Atari 2600, or a Sega Master System (at least in terms of the CPU each uses, as they're all variants of the standard 6502)
The NES uses 2k or ram, so that's what I've implemented here. My goal is to have built in debugging much like the NES emulator Mesen, thus trying to push what is viewable in realtime... that said, I'm most likely going to move to a page by page view instead of all at once, both for efficiency's sake as well as what I have showing on the screen in general with regards to layout, modules, etc.
Most 6502 setups generally start their code running at BusAddress $8000, the last 6 bytes of the program are typically reserved as Vector addresses which tell the system where to jump to for NMI, IRQ/BRK, and RESET/POWER ON.
As far as how the CPU interacts with RAM, the only real caveat is that the Stack is always ranging from addresses $0100 to $01FF, with the pointer decrementing when values are pushed to the Stack, and incrementing when pulled. The Stack pointer always wraps with the page its on. So decrementing beyond $0100 will jump it back to $01FF, etc
I currently have all the opcodes in place. I have verified (to the best of my ability with the emulator in its current state) all of the legal opcodes and their addressing modes. The illegal ones are in and functional (with the exception of illegal opcodes marked unstable... the CPU will read and execute them, but as far as the emulator is concerned, they're the same as a NOP) but not verified yet.
I've been having ChatGPT make small 6502 programs for testing (sure is fun having to double check its work all the time lol it gets addressing modes confused regularily), as well as some small programs from a tutorial series on NES programming I purchased from Pikuma.com about a year ago.
I've done everything in events so far, as I've mentioned not knowing any JS yet... that said, someone kindly posted a JS example in a different post of mine regarding drawing pixels to the Canvas object, and I have modified that enough to generate a noise image at 60fps (when RAM isn't updating) when the emulator is "off" and showing a solid pre-programmed BG color when "on" (none of this is can actually be affected by a 6502 program...YET!)
For the bitwise operators, I actually built my own DECtoHEX, DECtoBIN, BINtoDEC, BINtoHEX, HEXtoBIN, and HEXtoDEC funations BEFORE realizing there were getbit() setbit() togglebit() already built into C3... So I'm in the process of converting those over. Until now I've been using a lot of tokenat( binString, targetBit, "" ) which I assume is slow given that its dealing with strings and text comparisons.
I haven't benchmarked anything yet, as I'm still in the "does it all work properly" and the "how can I make what I've got the most effective/efficient" stages... though the goal is to be able to load and run 6502 programs at full speed, including NES roms for this particular iteration.
One of my project files is a 2D array of the actual instruction layout of the 6502 chip as seen at https://www.masswerk.at/6502/6502_instruction_set.html, so using my HEXtoDEC function, I can:
- read a Hex byte from the program, and directly reference the Opcode, Addressing Mode, and expected byte count
- collect the value of the data bytes (either 0, 1 or 2) to send to the Opcode Function
- execute the Opcode function, which then calls the addressing mode function (this sends back Memory and Address16 values back to the Opcode function), which then completes the operation, which then either sets the program counter manually (such as for Branches, Jumps, Interrupts, etc) or increments it normally through the program.
* I haven't included the cycle values yet, but its on my TODO list, to try and get it accurate enough for playing games and running programs and having them behave the way they would on real hardware, at least as far as the user experience is concerned.
I do recognize the need to move probably most if not all of this to JS, I've just zero experience aside form the snippet I'm using for my little display element, and will likely need to either find some really good tutorials (ideally relating to JS in C3 specifically) or see if I can find/make a friend who might help guide me. As prior to Construct apps the only actual programming experience I have is some Visual Basic from the late 90s... I'm better with 6502 than I am with any "modern" languages, in that 6502 is all I've ever tried to learn lol
And yes... TONS of events... This project is definitely NOT for the free version...
The eventual goal for this project (assuming I can get the emulator fast enough to run at 60fps during full playback with debugging, etc) is to create a 6502 SDK, which includes:
- code editor (I've already prototyped this to the point where I will likely start working on it as my next step)
- emulator for in-app testing/playing/debugging.
- chr editor (akin to Aseprite as far as making tiles and sprite graphics goes)
- sound editing (I have actually done a little bit of playing with WebAudio in JS, and between that and the WebMIDI plugin, intend on making a very basic sound and music editor... I've spent years using both Modplug Tracker and FL Studio for all my sound and music production, so replicating those features is a task, but not an unfamiliar one)
This is intended to only support nrom format intitially. As far as NES stuff goes, mappers and whatnot might be considered later... I think I've got more than enough on my plate with this currently lol