Building a 65816 Emulator
Updated: Apr 30, 2022
(Latest video posted at the bottom.)
While I'm waiting for my 65816 system PCBs to come in, I thought I would see how far I can get with building a 65816 emulator. So far, I am loading a ROM binary (the one I used to drive a bar graph through a VIA and drive my video circuit with a basic colored-bar pattern). Once the ROM is loaded, I jump to the reset vector and read the address, then jump to the address and read the first two bytes. That's not much yet, but it's a start.
Ultimately, I would like to be able to emulate much of my system, including ROM, RAM, extended RAM, VIA output, and video output -- all with their addressing that matches my build. I'd like to load the exact ROM binary I put into my 65816 system and have the emulator run it. I'm not sure if I'll get to audio; we'll see how it goes.
I have so much to learn about the 65816. I think this is going to be an entertaining way to do it!
I am watching Dave Poo's video series 6502 CPU Emulator in C++. His GitHub: davepoo/6502Emulator: Learning how a CPU works by emulating one.
Update: 9/256 Opcodes
I have my first nine opcodes added. With this, I have added a virtual video output with memory-mapped I/O and a VIA with virtual bar graphs. RAM, ROM, and Extended RAM are also working, all mirroring my actual hardware configuration and address decoding. An actual ROM binary file is loaded and running.
Update 29/256 Opcodes
I can now manage the video memory in the emulator. I added 20 opcodes to support the same code used in 65816+VGA Progress Update.
Update 36/256 Opcodes
I now have initial support for native mode and 16-bit support. The test ROM fills the video output with colored bars. I added an option to suspend logging, which allows the "processor" to run faster and output video faster. Playback speed has not been altered. An additional resource I have found helpful is andrew-jacobs/emu816: A C++ based 65C816 Emulator (github.com).
Update 256/256 Opcodes
I now have all 256 opcodes supported. I ended up porting much of Andrew Jacobs's 65816 emulator (C++) over to C#. Next on my list are interrupts, so that I can emulate interrupts from the VIA and process keyboard input. Thanks to u/visrealm for some coding pointers! Another nice reference: visrealm/vrEmu6502: 6502/65C02 emulator library (C99) (github.com).
Break, Step, and Memory Viewer Support
I added support to break and step one operation at a time. I also added a memory viewer, allowing me to view complete memory of all memory-mapped devices.
Updated Video Output Rendering
I replaced the video output code (GDI+ to the form) with a frame buffer and a PictureBox. It's feeling much better now.
Clock Equivalent and Moving Video Output
1602 LCD via VIA
I have started basic emulation of a VIA and a 1602 LCD connected to that VIA. Output is generated from assembly ROM, using the exact same code as I use to control physical VIA and 1602 LCD hardware.
Keyboard Input (to 65816) and Interrupts
I can now capture keyboard input, convert it to scan codes, write it to the VIA, trigger an interrupt, and have the 65816 take it from there. The 65816 has a reset vector to code that captures the scan codes from the VIA, converts them to ASCII values, then writes back to the VIA to update the virtual 1602 LCD. The ROM used in the video below should run as-is on my 65816 PC hardware; I will test it in the coming weeks. All code, including the assembly I used for the video, is on my GitHub. The assembly used in the video is available here.
Debug Labels, Additional Virtual Devices, and Mouse Capture
Adding a Sound Card Emulator
See Emulator^2 -- Sound Card Emulator.
When Time Permits
Some things I might add/change, when time permits:
Add conditional breakpoint support.
Continue to improve processor interrupt support and keyboard handler.
Posted to rehsd/Emul816or (github.com).