
The indirect form using a pointer requires only four bytes (for offsets -128.127 to the pointer), and is only four bytes. On x86, addressing uses the 32-bit immediate form, which requires five bytes, so that any relocation can be done correctly. Aside from using one register to hold the pointer, there is just no difference at all. Pointer-to-structure references use a base register, often %rdi if specified as first parameter to a function. Global variables are addressed using %rip. If using a global gives a performance advantage (which I'm not sure it does actually)I'm pretty sure it does not give a meaningful performance advantage.
#X86 asm 6502 emulator code#
I suspect it is a psychological effect that the types of data structures you use, also affect the patterns of code you create.) (I don't know why, but whenever global variables are used a lot in C, the code also tends to become spaghetti. In fact, I think it tends to push your mindset towards modularity, and yield better organized, more readable code. Your functions will pass the pointer to the state all over, and you may need to add fields to describe at least some of the emulator internal state, but in my experience, those are not downsides. There is very few downsides to this approach. Some have grown warts afterwards to support multiple interpreters in one process, but those tend to be fragile and prone to leakage.) That means you can't have more than one independent interpreter state in one process. Instead of using a separate state variable, most scripting languages just use global variables. (Most embedded scripting languages have a very similar problem, Lua being the exception that comes to mind. Also, you only need to provide the one pointer to the state structure to your functions.

Since the mos_6502_t structure contains the entire state of the emulated machine, you can emulate more than one in a single process.


The code is then not dependent on host byte order, and should be much easier to write. #define PC_HIGH(stateptr) ((stateptr)->pc / 256U)For the different addressing modes, define macros similar to above. #define PC_LOW(stateptr) ((stateptr)->pc & 255U)
