All 6502 instructions with their actions completely unrolled so you don't have to pick through detailed documentation to understand what they do or how to emulate them accurately. There is The Spreadsheet and The Github Repo.
How it works
For each instruction there is a set of expressions for the necessary memory reads, register updates, flag updates, and memory writes which are expressed in a simple, reasonably language agnostic manner by just using the symbols + - << >> & | ()
.
Self-contained emulation functions in a variety of languages are generated by the spreadsheet in columns which can be copy/pasted into your own code.
Making an Emulator or Disassembler
Each row can be turned into a small block of code which processes that particular instruction.
The basic flow of the generated code for an emulator would be something like this. After setting up the initial memory, register variables, and PC pointing to the start of the program:
-
Get byte at PC; this is the instruction
-
Use a set of generated if statements which check for each possible byte value, and if that's the current byte then do the following:
-
Do memory fetches for this instruction (if any)
-
Calculate updated values for registers and flags (if any)
-
Do memory writes (if any)
-
Update register and flag variables with the new values (including PC)
-
-
Loop around to get the next instruction or halt if PC has not changed value
Reversibility
The spreadsheet also details for each instruction what data needs to be saved and what register, flag and memory changes need to be made to roll back the instruction.
This is the information which would otherwise be trashed by the instruction.
If you incorporate the recording of these values into your emulator as it executes, then you gain the ability to step backwards through the previously executed instructions when debugging.
Keep It Simple
If you don't want to mess around with code generation, your 6502-enabled program can just have the spreadsheet itself in memory - probably broken down into an array of lines - and then do the string processing of the row on the fly to perform the operation or disassemble the instruction.
The expressions for the updates to registers etc. are few enough that you can check for each one as a string specifically and perform the equivalent calculation in code.
Future Work
If I get time I'll make an example emulator, and from there it is making the codegen routine and generated code in a bunch of languages.
Oh, and checking if the spreadsheet is actually correct!