This is my first question here and I hope that you're able to help me! I'm currently working on a GameBoy Emulator and want to write it in MASM, for handling the CPU instructions, I want to create an array with variables to make it easier for me to access.
Here is an example what I want to achieve:
assume esi:ptr CPU_CORE
REGISTER_A equ (([esi].registers.AF AND 0F0h) SAR 3h)
REGISTER_B equ (([esi].registers.BC AND 0F0h) SAR 3h)
REGISTER_C equ (([esi].registers.BC AND 0Fh))
PARAM_TABLE [Type?] REGISTER_A
[Type?] REGISTER_B
[Type?] REGISTER_C
assume esi:ptr NOTHING
and if I want to grab a value from the PARAM_TABLE it should work like this:
lea esi, PARAM_TABLE
mov ecx, 1h ; just as example for accessing REGISTER_B
mov eax, [esi+ecx*[TYPE?]]
;EAX now contains the value from the hi-byte of the BC register, so: (([esi].registers.BC AND 0F0h) SAR 3h)
So basically my idea is to create variables like REGISTER_A to make it easier to access. I hope you understand me. Maybe it would be possible to do this with macros?
So I answer my own question after tying out some things. Indeed you can create constants using registers like:
but you can only load it like:
Then i've figure out that you can just directly access the offsets of a structure (i don't know why i never have used it before.)
Since this works i've created a structure called PARAM which looks like:
And from that i've just created a parameter list for the LD R,R opcode. which looks like:
and a function to read from the register
This function loads the
CPU_CORE
intoESI
and thePARAM
intoEDI
, the pointer of thePARAM
gets added to theCPU_CORE
. After that the flags are getting tested, ifBIT 0
is set it reads from the CPU memory (as example:(HL)
), if it's not set it will just grab the value from the register. TheBIT 1
is for the size if it's set the function will read a 16-bit register (BC
,DE
,HL
) or an 8-bit register (B
,C
,D
,E
,H
,L
,A
).I hope you can understand what I've wrote. If you have any questions feel free to ask, this is just by far the "easiest" way for me to solve my problem.
If you're wondering why I've created the table: You can "decode" the opcodes for further information google "z80 decoding".