NORCAL: A C compiler for the 6502

These are my project notes for NORCAL: a C compiler that targets the NES, with its 6502-like CPU.


Getting started

January 15, 2019

I am setting up my toolchain: compiler, simulator, test harness.


Automated, simulated tests

January 17, 2019

For running automated tests, I've put together a very simple "headless" NES simulator. I took an open source 6502 CPU simulator and configured its memory map to match the NES. To specify the program, you give the sim a standard iNES file, just like any other NES emulator. This won't work for testing any graphical or special-purpose hardware -- only the CPU, RAM, and ROM. This should be enough to test all compiler features.


Design priorities

January 18, 2019

These are my priorities for the compiler:

  1. Simple to implement
  2. Optimized for the 6502 architecture
  3. Consistent with standard C (for usability)

Obviously, they will often conflict.


Starting on parsing

January 22, 2019

Parsing isn't interesting, but I need to be able to parse source code in order to run automated tests. For now, to allow me to use a very simple (and quick to implement) parser, programs will be written in a simple S-expression syntax. Hopefully I can get this parser working in just a couple days so that I can move onto code generation.


The provisional parser is complete

January 23, 2019

As I hoped, the simple parser was easy to implement. I've moved on to generating code for loading and storing values through pointers. For example:

*0x6000 = 123;
*0x6000 = *0x6000;

Simple, untyped assignment expressions.

These kind of expressions won't work once the compiler starts enforcing C's type system, but for now they are useful test cases.


S-expressions are out, real C grammar is in

February 4, 2019

It didn't take long for me to get tired of the temporary syntax, and it didn't extend well to handling blocks and function declarations. I've now written a lexer and parser that will parse legitimate C grammar.


Future topics


If you have comments, questions, or suggestions regarding this topic, please email me at holmak+blog@gmail.com!