Tutorial 1 – C Programming for 6502 (8 bit) CPU


6502 CPUs were a massive success back in the 80s and early 90s. The 6502 CPU is 8-bit only but there were many many successfully games written for this platform, e.g. Super Mario.

6502-super-mario3 Tutorial 1 - C Programming for 6502 (8 bit) CPU 6502 8 bit assembly language c / c++ code interpreter / compiler Nintendo Entertainment System programming languages

Despite limited computing resources (such as memory and processing speed),  the Super Mario 3 (shown above, in a NES emulator) was a great success and it has brought huge profit to Nintendo.

The Nintendo Entertainment System (Family Computer, or Console) was very popular in the last decade and even by today, you can still find their existence everywhere. The NES had given many of us (especially the ones born in 1980s) great memory for our childhood.

6502-family-computer Tutorial 1 - C Programming for 6502 (8 bit) CPU 6502 8 bit assembly language c / c++ code interpreter / compiler Nintendo Entertainment System programming languages

Today, the NES FC (Family Computer/Console) has been out of market for many years. We have 64-bit processors and you can see how time flies.

Is 6502 programming dead? I don’t think so, for multiple reasons: First, you can familiar with computer/architecture with 6502 programming (especially 6502 assembly). Second, you can write some simple NES games for fun. Yes, some of the 6502 FC (such as this one) have even supported 1.44M floppy disks (so you can copy games and play it on TV if you have a console).

So, let’s kick-off this tutorial series by writing a ‘Hello World’ using 6502 C Compiler. You would need to download a 6502 C Compiler from (www.cc65.org)

Also, you need to have a NES emulator, which is very popular and there are many downloadable (google it). The NES emulator allows you to run games  on modern PCs. The files loaded into the emulator are *.nes.

After you install the C compiler for 6502 CPU, you will find a bin folder (such as C:\Program Files\cc65\bin) where there are many compiler executables. The file cl65.exe is the compiler to use. Let’s write a simple 6502 C program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "conio.h"
 
void main()
{
    unsigned int s = 0;
    int i = 0;
 
    cprintf("6502-C Compiler. \r\nhttp://HelloACM.com\r\n");
 
    cprintf("Size Of int = %d\r\n", sizeof(int));
    cprintf("Size Of char = %d\r\n", sizeof(char));
    cprintf("Size Of float = %d\r\n", sizeof(float));
    cprintf("Size Of double = %d\r\n", sizeof(double));
    cprintf("Size Of short = %d\r\n", sizeof(short));
 
    for (i = 0; i <= 100; i ++) {
        s += i;
    }
    cprintf("Sum = %d\r\n", s);
 
    for(;;); // loop forever, never ends
}
#include "conio.h"

void main()
{
	unsigned int s = 0;
	int i = 0;

	cprintf("6502-C Compiler. \r\nhttp://HelloACM.com\r\n");

	cprintf("Size Of int = %d\r\n", sizeof(int));
	cprintf("Size Of char = %d\r\n", sizeof(char));
	cprintf("Size Of float = %d\r\n", sizeof(float));
	cprintf("Size Of double = %d\r\n", sizeof(double));
	cprintf("Size Of short = %d\r\n", sizeof(short));

	for (i = 0; i <= 100; i ++) {
		s += i;
	}
	cprintf("Sum = %d\r\n", s);

	for(;;); // loop forever, never ends
}

Instead of normal C printf, we use cprintf in 6502 programming. All other C syntax stay the same. However, the data types are a bit different. For int type, it is not 4 byte but 2 byte on 6502 platforms.

We save this file as test.c and compile it under the command shell.

6502-cl65.exe Tutorial 1 - C Programming for 6502 (8 bit) CPU 6502 8 bit assembly language c / c++ code interpreter / compiler Nintendo Entertainment System programming languages

The -t switch tells what platform is targeted. The -o switch specifies the output binary file name. After this, if it compiles with no errors, the test.nes will be generated, and we can load it into the NES emulator.

6502-cl65.exe-nes Tutorial 1 - C Programming for 6502 (8 bit) CPU 6502 8 bit assembly language c / c++ code interpreter / compiler Nintendo Entertainment System programming languages

Writing a NES program isn’t that difficult, huh? We will surely achieve more in the next tutorial.

We use a endless loop at the end of program to pause the screen, otherwise, most emulator will terminate but no messages will be shown (frames run out)

To change to a new line for print, we need to use \r\n (carriage return and line feed) instead of a single \n.

The sample NES file can be downloaded at locally.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
a WordPress rating system
796 words
Last Post: Microsoft Excel Tricks: Remove Unit from Cell Values
Next Post: Tutorial 2 - C Programming in 6502

The Permanent URL is: Tutorial 1 – C Programming for 6502 (8 bit) CPU

2 Comments

  1. Kirill
  2. sugar

Leave a Reply