Friday 23 August 2013

Arduino Mega Drive Rom Dumper (1 of ?)

You may remember at the start of the year I posted this entry on Inside Sega Megadrive Carts where I spoke briefly about an awesome project I saw on insideGadgets.

In the project Alex created an Arduino sketch that was capable of dumping the ROM data from a GameBoy cartridge as well as being able to read and write the data from RAM. This inspired me to get my own Arduino with the aim of one day creating my own dumper for the Sega Megadrive system. Well today is that day!

Ok not exactly today but today is at least the start of it!

In case you can't remember the project here are all the relevant posts from his blog, consolidated into one handy list:

Hopefully by this point you've read through everything especially the guide linked to in the first post. I know you have to download it as a zip which is a bit of a pain but it is well worth it as Alex's guide really builds upon knowledge you'll gain there. (Edit - if you missed it you can download it here thanks to Alex for permission to share)

When reading through the part on reading the ROM data you'll see we also need to use shift registers to allow us to access all the possible addresses due to the Arduino's limit on digital ports. Despite owning a couple (they came with my starter kit) I didn't know how to use these so had a read and work through these tutorials to help me learn:
Really the shift register isn't as scary as the diagrams make it out to be, all you do is fill it up with the data you want to send out and then release the data. Once you know what pins do what it should all make sense and be a lot less intimidating.

Since the Megadrive uses a 23 bit address register (more of this in a minute) we will dedicate 3 shift registers to taking care of addresses. We can also daisy chain our registers as they do in Project 15 so that we have all the other ports free on the Arduino.

Ok, let's finish this post off by setting up a little circuit that will let us test if the addressing is being done right. First a little explanation on what addressing and address registers are.

Basically the ROM is like an array of bytes in programming and just like an array each memory space for a byte on the ROM has a unique number identifying it. In an array this number is known as an index but in this context it is known as an address. An address register is a part of the ROM that is used to tell the ROM which address (i.e. index) we are interested in when we send it a command. As mentioned the Megadrive has a 23 bit address register which means the range of addresses we can have is from 0 to the biggest number possible with 23 bits, 8388607, each of which holds 1 byte of data.

We'll get into more details about how to access the address register in the next post but essentially we send the address to the register by connecting the pins from our shift registers to the address pins on the ROM. For now we will connect them to some LEDs to check the correct values are being sent. Unfortunatly I only have 2 shift registers just now so I can only set up a demo that can simulate a 16 bit register but I'll get some more soon.

Connect up Project 15 from the above source if you haven't already and make the following couple of changes to the code:

The main loop:
void loop() {
//count from 0 to 255
//for (int i = 0; i < 255; i++) {  
  for (unsigned int i = 0; i < 65536; i++) {  
    digitalWrite(latchPin, LOW); //set latchPin low to allow data flow
    shiftOut(i);
//shiftOut(255-i);
    digitalWrite(latchPin, HIGH); //set latchPin to high to lock and send data
    delay(10);
  }
}

and change this loop of the shiftOut function from <=7 to < 16

// for each bit in dataOut send out a bit
  for (int i = 0; i < 16; i++) {

You can also increase the delay in the main loop from 10 to 1000 to see things clearer.

Compile and run the sketch. Hopefully you'll see something like this:

16bit Address Register made from 2 8bit Shift Registers


You should see the LEDs lighting up as a binary counter with the right most red LED being the least significant bit (i.e the number 1). Once all the red ones are lit, which will be when i = 255, the green ones start lighting with the right most green LED being equal to 256. This shows that as we send the addresses from 0 to 65535 they are being sent correctly from the shift registers.

And that's it for now. Be sure to read everything I've linked to and, if you can, keep that project set up on your Arduino, we'll use it on the next post.

Any questions please ask otherwise happy computing!

No comments:

Post a Comment