How do I put ram, rom, and perepheral registers in one memory map?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
overlordmanny
Posts: 2
Joined: Tue Dec 04, 2018 8:54 am

How do I put ram, rom, and perepheral registers in one memory map?

Post by overlordmanny » Thu Feb 04, 2021 9:59 pm

I'm a fairly noob programmer. I write databases for a living but this is a different kind of thing. This is purely for my entertainment and learning. I have no aspirations for anything. What I have is not an NES emulator, but I may go that route eventually. I'm writing a 6502 emulator in C++ and it's running code fine but I have some issues I'd like to sort out before I go much farther. Currently I'm only using ram and rom. How can I better join the ram and rom (and one day peripheral registers) into one 64K memory space. I'm currently copying a "ram" array of 56K and "rom" array of 8K that is loaded from a file into a 64K "memory" array that the CPU then accesses. The problems with this is that the "rom" is then clobberable if errant code ever tries to write to it and if I ever memory map hardware registers I won't be able to edit them without re-copying the array constantly. Is there a way to have the large array have portions that are just pointers to other arrays? Or is there a better way entirely.

I'm sure I'm doing this wrong.

User avatar
tokumaru
Posts: 11998
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How do I put ram, rom, and perepheral registers in one memory map?

Post by tokumaru » Thu Feb 04, 2021 11:17 pm

I never wrote an emulator myself, but I think that a common approach is to hide all memory accesses behind a pair of functions. In a real system, the CPU has no idea what's mapped where in the memory map, and it really doesn't care, it just makes read and write requests, so you can basically do the same thing with the emulated CPU and let it interface with the memory map exclusively through a pair of read/write functions. Then, inside these functions, you can decode the address as necessary, accessing your RAM and ROM arrays, blocking writes to ROM, triggering special actions for memory-mapped registers, and so on.

coto
Posts: 55
Joined: Wed Mar 06, 2019 6:00 pm
Location: Chile

Re: How do I put ram, rom, and perepheral registers in one memory map?

Post by coto » Fri Feb 05, 2021 7:08 am

overlordmanny wrote:
Thu Feb 04, 2021 9:59 pm
I'm a fairly noob programmer. I write databases for a living but this is a different kind of thing. This is purely for my entertainment and learning. I have no aspirations for anything. What I have is not an NES emulator, but I may go that route eventually. I'm writing a 6502 emulator in C++ and it's running code fine but I have some issues I'd like to sort out before I go much farther. Currently I'm only using ram and rom. How can I better join the ram and rom (and one day peripheral registers) into one 64K memory space. I'm currently copying a "ram" array of 56K and "rom" array of 8K that is loaded from a file into a 64K "memory" array that the CPU then accesses. The problems with this is that the "rom" is then clobberable if errant code ever tries to write to it and if I ever memory map hardware registers I won't be able to edit them without re-copying the array constantly. Is there a way to have the large array have portions that are just pointers to other arrays? Or is there a better way entirely.

I'm sure I'm doing this wrong.

Try reading this:

It's a functional ARMv4 (gba) emu, does write stuff to screen and perform basic drawing of routines, it was an experiment to understand hardware better (well i've known hardware since the age of 4 but this was done to understand the connection between circuitry and game programs):
https://bitbucket.org/Coto88/armv4core/src

Post Reply