Before I can start emulating, I need to make a ROM / RAM area or memory buffer to store the ROMs and provide the system with RAM.
And before I can do that, I need to know the Space Invaders memory map.
Space Invaders has four x 2 kB roms:
invaders.e
invaders.f
invaders.g
invaders.h
This rom set is widely available on the internet. I googled for websites supplying MAME 0.36 romsets. If you end up with a romset that has four x 2 kB roms of a different naming convention, you may be able to work out which rom corresponds to the naming convention above, or just find an older set.
Space invaders then has 8 kB of RAM; 1 kB of general purpose RAM and 7 kB of video RAM. It then also has a RAM mirror of a further 8 kB.
The processor addresses these ROMs and RAM as a single 16 kB entity so the memory map in Hex goes:
invaders.h 2 kB 0000 to 07ff
invaders.g 2 kB 0800 to 0fff
invaders.f 2 kB 1000 to 17ff
invaders.e 2 kB 1800 to 1fff
RAM 1 kB 2000 to 23ff
Video RAM 7 kB 2400 to 3fff
Note that already we can start to gather info for the processor emulator. 16 kB cannot be addressed in 8 bits / 1 byte. The 8080 processor is an 8 bit processor, but must use 16 bit memory addressing, or memory addressing spread across two bytes.
Also note that Space Invaders screen resolution is 256 x 224 in black and white, so one bit per pixel. 256 x 224 is 57344 bits or 7168 bytes or 7 kB. This makes for a nice easy bit streaming screen interpreter hopefully.
Two additional important notes to add here:
Note 1. Some Space Invaders Emulation sites state that there is a "RAM Mirror" above memory address 3fff which seems to be frequently misunderstood as to what it is. A RAM mirror isn't an extra area of RAM. It simply means that because of how the memory pins on the processor work, memory addresses above 3fff may be requested by the processor. If the requested memory address is above the stated memory limit of 3fff, then the real memory address is (Address-3fff). I am doubtful whether this situation ever comes up though. Several people have stated that their emulators don't request addresses above 3fff. Several others have stated that an emulator won't work unless you allow for the RAM mirror. I havent seen any need to take this into account in my emulator. Just keep it in mind.
Note 2. Building in error checks as you write the emulator is always a good idea. A good error check is that whenever the processor writes to memory, check to see that the address that its writing to is in the RAM area and not the ROM area. Another good check is to ensure that the program counter doesn't go up beyond the program area. If either of these checks fail, then its a good indicator that there is errors in your code.
Now that I have the memory map details, I can start making the memory buffer.
I have two choices here; Array or Databank. Both should be pretty straight forward to implement. Set up an array or databank with 16384 slots and allow one byte in each slot. The only real question is, which would be faster?
DEV 1 - Program to test the speed of writing to and reading from an array vs writing to and reading from a databank.
This was a nice easy dev program that checks the time in ms, writes a fixed value to each of the 16384 slots in the array, and then reads each of them back out. On completion check the time is ms again and see how long its taken.
Repeat the process for the databank and then compare which one was faster.
I let this run for several thousand cycles capturing the average time taken for each one, and was slightly disappointed to find that both methods take pretty much exactly the same amount of time.. (rolls eyes) and so after literally three seconds of consideration, I went with the array option as the coding to read and write from array slots is shorter.
I have a feeling that this will by far be the easiest part of this emulator.
I have made a single dimension array with 16384 spaces. I have loaded up the Space Invaders roms into first part of the array in the correct order, and left the rest of the array empty... And that's about it!
One last important thing to know. What memory location do you start running the Space Invaders game from?
While this isn't always the case with 8080 code, conveniently Space Invaders starts running from memory location 0000.