Monday, November 26, 2012

Hello World the Hard Way

Well it looks like my love of the original Commodore 64 has taken me somewhere unexpected.   Somewhere I thought I would never be.   The world of software emulation.  The picture you see here is from my current project.   I'm calling it SASM64.  It stands for Semi Abstract State machine, and is a functional Commodore 64 emulator.

I like to call this hello world the hard way, as I had to write an entire environment for the hello world program before I ran it.   Hello world isn't actual the first program this emulator ran.  Due to some bugs during development I could not get strings to work.   So the first program that ran correctly was 10 print 5+3.

Before depart to far here and start telling antic dotes that only interest me, I'll start by telling you why this project was started and what the design goals were.

The SASM64 project began as an extension of my desire to join the world of homebrew CPU.  The Magic-1 is a wonderful example, and one of the most complete homebrew CPU projects I've seen.  Just to be clear here were not talking about building a computer from scratch.   Were talking about building a computer without a microprocessor.    The goal is to build the processor, not just a computer.    I've made lots of plan and sketches but nothing really has come from my project yet.

A few weeks ago while thinking about the homebrew CPU concept I decided to undertake an intermediate step that would advance my understanding of how to build a CPU.  A CPU is is made up of many state machines, and I don't really have a clear picture in my head of how many and what type of state machines will need to be designed for a CPU to work.  So I set out to write a software CPU core just to see what was involved.  I also decided to go with a known processor so I would have a code base to work from.

So I began my software core project with somewhat different goals than what I imagine to be the typical software Emulator Author.   Instead of tyring to optimize the code (though there is some that entered out of necessity) the code is written to simulate multiple state machines.    Thus the name SM in the title.    Ultimately I don't have the detail of a physical state machine, my software state machines are somewhat abstracted from the real state machines.    So I set out with the following Goals
  • Design A functional Software 6502 Core
  • Design it using multiple state machines
  • Design it in a human friendly language (I chose C# as I am familiar with it)
  • Design enough functionality that it can run Basic using an existing ROM.
I reached those goals within about two or three weeks of starting the project.   Its first existence was in a very ugly Windows forms Application.  I was able to get it running the Commodore 64 Kernel, and Basic ROMS and execute basic program.    It has turned into such a rewarding project that I was unable to stop there.    I started a new XNA project and dropped the code in, added a limited Text mode video driver, some tolls to drop PRG files into memory (no floppy emulation yet), and most recently add a from scratch Software SID emulator.  With a bit of hacking at it It now is capable of playing SID files.

This project not been without pitfalls, many of them around incorrect, and incomplete documentation.  Some of them directly related to the fact I am not, nor have ever claimed to be a programmer.    There was a lot of making it up as I go along.   My plan is to tear myself away from this project for a while and document the process, and share the problems and solutions I've passed along the way.


Stay Tuned....