Tuesday, November 17, 2009

Make an NES cartridge and learn a little about 6502 assembly language programming!

[I will be adding some pictures to illustrate some of the steps below]


NEW!
How to make Your Own Super Synth Drums Cartridge for The Vintage Nintendo NES

I am creating a tutorial (work in progress) showing how to make your own NES cartridge, particularly the Super Synth Drums Cart. You can use this info to create your own programs, games, or your own music cartridges.

You will recycle an old NES cartridge, remove the original program chip and replace it with one that you have programmed yourself. You will need a "programmer", a piece of hardware that you will attach to your PC or Mac via a USB cable. A Willem EEPROM Programmer such as this one:
http://www.mcumall.com They also sell their programmers on ebay.

This chip programmer is inexpensive and comes with software to make programming your chips very easy. Get the USB model. The programmer writes the code to your chip. You will then solder your newly programmed chip onto the cartridge circuit board. (The new chip you've programmed replaces the PRG chip of the old NES cart you're using. When you open the cart you will see two chips marked CHR and PRG. For this tutorial you will only need to remove and replace the PRG chip. (See the link below to nesdev/Parodius, or see NESWorld.com to learn more about NES 6502 programming and about the NES hardware.)

Get started!

You must obtain an old NES cartridge that is of the type "NROM". Nintendo made many different types of circuit boards for their cartridges during production. You can get more info from the guys at http://nesdev.parodius.com.

There is a load of info there and also a very helpful message forum. They used to have a list of all of the NES carts and a description of each cart type, but it seems it has been removed or just moved.

Luckily I printed the list out last year when I started making my own carts. Here is a short list of some of the carts I used for the Super Synth Drums cart. (Please note: I always tried to use the really crappy NES carts that were made, never good ones like Super Mario Bros.)

Some of the real crappy games made with the NROM circuit board are:

1. Tag Team Wrestling
2. Golf
3. Volleyball
4. Baseball
5. Hydlide
6. Ice Hockey
7. Mach Rider
8. Pinball
9. Soccer
10. Spelunker

I bought all of my carts on ebay, usually for about $2.00 each.

Removing the old PRG chip!

You will also need the NES cart "security" screwdriver tool such as this one: http://www.nintendorepairshop.com
It looks just like a regular screwdriver with a special tip used for removing the screws Nintendo used on some of their carts. You can find this tool on ebay, or several NES stores on the web. Some NES carts only use regular slotted screws which can be removed with a small flathead screwdriver. You will also need a soldering iron and small wire nippers/clippers.

Remove the screws from the cart case, open and remove the circuit board. What follows is a description of my technique for removing the old PRG chip. You will need small wire nippers or a similar wire cutting tool to clip the pins of the PRG chip. I only clip the row of pins that faces the top edge of the circuit board, the edge where you will see the word NROM printed. Once that top row of pins are clipped I bend the PRG chip back and forth until the lower rows of pins are broken off. I have personally found this technique to be the quickest and most efficient way of removing the PRG chip.

Now flip the circuit board over and with a soldering iron heat each solder hole one at a time and tap the circuit board against your work table to knock out the remnant of the broken/clipped pin. I found that by quickly tapping the board on an angle after heating the solder hole produced the quickest results for removing the clipped/broken pins. Do this for each solder hole until all of the pin remnants are removed. You will sometimes have to use a needle nose pliers to remove a stuck pin remnant.

If any holes are still clogged with solder you can now use a "solder removal braid", available in small rolls from Radio Shack, etc. This stuff is just a braid of copper wire which when placed on top of a solder hole and then heated with a solder iron it will absorb the melted solder and unclog the solder hole. *I have also found that by heating the solder hole with the soldering iron and tapping it on my workbench worked very well. You might have to do both; heat solder hole and tap, and/or use the solder braid. I very rarely used the solder braid, but it does come in good use once in awhile.

Types of chips to use for your ROM!

I will refer you to this thread on the nesdev/parodius forum where the types of chips used were discussed: http://nesdev.parodius.com/bbs/viewtopic.php?t=3898

I learned a lot from the people of that forum. The above thread was started by me last year when I was just learning about this stuff.

There are a variety of chips that you can use to replace the original PRG chip in your cart. Price and availability were the criteria for the chips I used and I found that the SST 27SF512 chips worked great. The numbers "512" on the chip means that it has a memory capacity of 512 kilobits. Note the word "bits", not "bytes". Divide 512 by 8 to determine its memory capacity in "kilo bytes, kB". So the chips I've been using (SST 27SF512) had a memory capacity of 64 kilobytes or 64 kB. This is important to understand if you are creating your own ROM to program your chip with.

NOTE: As another example, the chips I used (the SST 27SF512) were 64 kB (kilobytes). The last three numbers in the name of the chip are key to knowing it's particular memory capacity; 512. 512 means 512 kilobits, (kilo bits instead of kilo bytes). Chip makers print the chips capacity on the chip in kilo-bits instead of kilo-bytes. There are 8 bits in one byte, so to determine my chip's memory capacity in kilo-bytes I simply divide the number 512 by 8, which equals 64. That is 64 kilobytes.


Using an Assembler!

When I first "assembled" the 6502 assembly code for the Super Synth Drums cart (I used NESASM to assemble my code) the resulting .NES file (ROM) was 16 kB. I couldn't just write that (or "burn") to my chip. I had to open the .NES code in a HEX editor called HxD and delete the first 16 bytes of the HEX code. The first 16 bytes contained the "iNES header" that was created by the NESASM assembler. If you leave that header in your code before burning it to your chip it will not run on a real NES, *however the header is needed if you only wish to run your ROM in an emulator such as Nintendulator, on your PC. (Please see the forums at nesdev.parodius or NESWorld.com to learn more about using the various NES assemblers if you wish to write your own NES assembly code and compile to a ROM.) Wikipedia info on Assemblers: http://en.wikipedia.org/wiki/Assembly_language#Assembler

The process of writing a program in 6502 assembly language and then having that assembly language program converted into a ROM (the ROM is the .nes file containing HEX code) goes like this:

1. You first write your assembly language program using a text editor. Here is my code for the Super Synth Drums cart in .txt format: Super Synth Drums Assembly Language Code

(It is very simple code that I was planning on adding to, expanding, but I have been getting into other stuff recently. If anyone would like to modify the code or add to it please be my guest. Please let me know if you do modify or add to it and perhaps we can make it available here for downloading.)

2. Use an "assembler", which is just a piece of software (i.e. NESASM) that converts your assembly language program into the .nes file HEX code. This .nes file produced by the assembler contains Hex code and is also called a "ROM".

If you use an assembler for creating an NES ROM like the one I used called NESASM, the resulting file that is produced will have a .nes file extension and if you looked at this code in a text editor you will now see some very weird looking characters instead of assembly language. The assembler converted the assembly language code into HEX code, this is what we refer to as the "ROM" and it has a .nes file extension just as the SuperSynthDrums.nes ROM does.


Editing your ROM's HEX code!

After the conversion of your assembly language code into HEX code by the assembler (NESASM), the chip programmer you use to program your chip converts that HEX code into binary code (a bunch of 1's and 0's) before burning the chip. But you won't see any binary code unless you wanted to, you will just see the HEX code. If you open the .nes file in a HEX editor such as HxD you will see the proper HEX code which makes up the .nes file, and you can now edit the HEX code. Not only is this necessary when creating your own NES cartridges but it is also used in "ROM Hacking". If interested, please see "ROM Hacking" here:
http://en.wikipedia.org/wiki/ROM_hacking

NOTE: I will be doing some editing over the course of writing this tutorial. If there are any other 6502/NES enthusiasts/hobbyists out there who happen to read any of this and can suggest changes or clarifications that might need to be made please feel free to e-mail me and let me know.

Check out this info about the NES assembler that I used called NESASM. It is .pdf http://www.nespowerpak.com/nesasm/nesasm.pdf

I also found this guy's tutorial to be helpful, but please note that he did make some mistakes I believe in some of his code examples, nonetheless, I did get some good info from what he wrote about the NES and 6502 assembly language for the NES. http://patater.com/gbaguy/nesasm.htm


More about chip memory capacity and using a HEX editor!

So when I originally "assembled" my assembly code using NESASM the resulting file that was produced was a 16 kB .nes file containing HEX code. I edited this HEX code in HxD by removing the "iNES Header" (i.e. the first 16 bytes of code). I then had to copy the entire HEX code (in HxD's editing window) and paste it 3 times to create a file that was now 64 kB, (the original Hex code was 16 kB, plus pasting that code 3X equals 64 kB), because that happened to be my chip's memory capacity; the SST 27SF512 chip I used to replace the cart's original PRG chip. In other words, I had to increase the size of my ROM file (the .nes HEX code) to match the size of my chip's memory. If my chip's memory capacity was only 16 kB then I would not have had to copy and paste my HEX code 3 times, I would have just deleted the iNES header (the first 16 bytes of Hex code in my .nes ROM) and then burn it. Now I can burn this ROM into my chip. You must "fill up" your chips memory capacity, whatever that may be, in order for it to run on a real NES.

Another chip you can use is the W27E257 (32 kB memory capacity) available from MCUmall.com, or the AT29C256 (also 32 kB) available from Futurlec.com. You can also find suppliers selling these chips on ebay. If using a different chip please make note of its memory capacity.

If you're not interested in writing your own 6502 assembly language code then don't worry if you have no idea what I'm talking about here concerning assemblers such as NESASM. If you're only interested in burning the Super Synth Drums ROM to your chip you don't have to worry about any of this HEX editing stuff either, because the format of the code I will provide for downloading here will be ready for programming (burning) onto your chip. You do have to know your particular chip's memory capacity in either case so that you will know which Super Synth Drums file to download here.

As I have mentioned above, if you are using a different chip which has a different memory capacity other than that of the SST 27SF512 (i.e. 64 kB) you will need to edit the HEX code of the ROM file using a HEX editor such as HxD to suit your particular chip's memory capacity before programming it. (If your chips memory capcity is 32 kB and your .nes ROM file is 16 kB you will need to use a HEX editor to copy and paste the code 1X, to equal a total of 32 kB before burning it to your chip.) Even if your chip's capacity is only 16 kB and your .nes ROM file is also 16 kB you will still need to use a HEX editor to remove the first 16 bytes of code (the NES header) before burning the ROM into your chip. (If using an NES ROM in an emulator on your PC or Mac you won't need to do any of this. You would just assemble your 6502 code using NESASM and start playing using Nintendulator or a similar emulator. Please note that not all emulators are 100% accurate when recreating the functionality of the NES. I have found Nintendulator to be the most accurate for use on a Windows PC.)

NOTE: What we're calling a "ROM" here (Read Only Memory) is really a file with a .nes file extension that is made of HEX code. We will refer to this HEX code file as a ROM. (Read more about ROM's here: http://en.wikipedia.org/wiki/Read-only_memory). Personally when I think of a ROM I think of a memory chip with DATA in it, which is what your chip is after you program it. However, many people refer to the .nes files (which are made of HEX code) that are floating around the internet as "ROMs". I use both the words "HEX code" and "ROM" to refer to the .nes file.

I will make a few different downloads available for different types of chip memory capacities. You can read more about this chip stuff at the nesdev.parodius forum or at the NESWorld.com developers forum.

Recap:

1. Get an old crappy NES cart
2. Remove its PRG chip
3. Write your own 6502 assembly language program and "assemble" it using an "assembler" such as NESASM. The resulting file produced by the assembler is now called a ROM and is made of HEX code. *Or use an NES ROM (.nes file) from the internet, and if needed edit your ROM's HEX code as follows.
4. Edit your HEX code using a HEX editor such as HxD. Editing your ROM's HEX code means; A. Removing (deleating) the first 16 BYTES of HEX code at the very top of your code. That is the "iNES Header", it must be removed before burning your ROM to a chip, *but the iNES header should be left in tact if you're using your ROM in an emulator (such as Nintendulator) on your PC or Mac. B. If necessary, copying and pasting your HEX code in the HEX editor (HxD) to increase the file size and fill the memory capacity of the chip you are using when burning it.
5. Burning the edited HEX code (i.e. the .nes file, or ROM) to your chip using a chip programmer such as the Willem USB programmer and it's software.
6. Soldering your chip onto the NES cart's circuit board and reassembling the cart case, etc.


Using the Willem USB programmer!

After editing your .nes ROM Hex code with a HEX editor the procedure for programming (burning) your chip will differ depending on the chip programmer you're using. The Willem USB Programmer that I mentioned above is very easy to use. If anyone using that programmer and its software (GQ USB Programmer software) needs any help just e-mail me.

*As I mentioned, I will be making the ROM for the Super Synth Drums ready for burning into a few different chip sizes (memory capacities). So you will just have to choose the correct .nes ROM file that will fit into your particular chip (i.e. 32 kB, 64 kB, etc.) and then burn that right into your chip. You won't have to edit the ROM's HEX code unless you are trying to burn someone elses ROM that still has the NES header in it's code, or if the size of the ROM file doesn't match the memory capacity of your chip, or if you are burning a ROM you have programmed yourself.

The actual programming (burning) of the chip using a hardware programmer such as the Willem USB Programmer is a very easy process and I will be adding some info about how to use the Willem USB Programmer. Additional help can be found at nesdev.parodius.com/bbs or at NESWorld.com

*If anyone is reading this tutorial and would like me to add more info, pics, video please e-mail and let me know!

3 comments:

Meng Qi said...

Very cool! It's great that you share it! But I'd rather buy it from you if I want to use it.

-----

Do you still make the Lunarmodule or solarmodule? I was looking for demos for it but I didn't find any.

I do like your cooking blog. Check out my blog :
http://mengqimusic.blogspot.com

Best luck for you.
/M.Q.

ElectroKraft said...

Hi Meng, yes still making both the LunarModule and SolarModules.

Let me know any time you might have any questions.

(Will check out your blog now!)

Thanks again.

Unknown said...

Great info man. Any chance you still sell the Super Synth Drum cartridge? I'm pretty sure if I tried to do this myself it would not turn out so well...