emulation - 6502 and little-endian conversion -
for fun i'm implementing nes emulator. i'm reading through documentation 6502 cpu , i'm little confused.
i've seen documentation stating because 6502 little-endian when using absolute addressing mode need swap bytes. i'm writing on x86 machine little-endian, don't understand why couldn't cast uint16_t*, dereference that, , let compiler work out details.
i've written simple tests in google test , seem agree me.
// implementation of read16 #define read16(addr) (*(uint16_t*)addr) test(memmacro, read16) { uint8_t arr[] = {0xff,0xcc}; uint8_t *mem = (&arr[0]); expect_eq(0xccff, read16(mem)); }
this passes, appears supposition correct, thought i'd ask more experience i.
is correct pulling out operand in 6502 absolute addressing mode? possibly missing something?
it work simple cases on little-endian systems, tying implementation feels unnecessary when corresponding portable implementation simple. sticking macro, instead:
#define read16(addr) (addr[0] + (addr[1] << 8))
(just pedantic, should make sure addr[1]
can't out-of-bounds, , need add more parentheses if addr
complex expression.)
however, keep developing emulator, find it's natural use pair of general-purpose read_mem()
, write_mem()
functions operate on single bytes. remember address space split multiple regions (ram, rom, , memory-mapped registers ppu , apu), having e.g. single array index won't work well. fact memory regions can remapped mappers complicates things. (you won't have worry simple games though -- recommend starting donkey kong.)
what need figure out region or memory-mapped register address belongs inside read_mem()
, write_mem()
functions (this called address decoding), , right thing address.
returning original question, fact you'll end using read_mem()
read individual bytes of address anyway means uint16_t
casting trickery less useful. simplest , robust approach w.r.t. handling corner cases, , every emulator i've seen in practice (nestopia, nintendulator, , fceux).
in case you've missed it, #nesdev channel on efnet active , resource way. assume you're familiar nesdev wiki. :)
i've been working on emulator can found here.
Comments
Post a Comment