Creating a virtual 6502 chip Dec 2015

Ok, we’ve seen Tiny BASIC, and how it turned a µC into a complete, albeit limited, programming environment. It took a mere 17 KB of flash memory. Now watch this:

[emu6502] 10496 bytes

6502 EhBASIC [C]old/[W]arm ?

Memory size ? 

15615 Bytes free

Enhanced BASIC 2.22

Ready

The code running on the Hy-Tiny STM32F103 uses some more resources, in particular because now the interpreter context grabs 16 KB RAM for emulation:

Sketch uses 24,660 bytes (18%) of program storage space.
Global variables use 18,040 bytes of dynamic memory.

Let’s try a few things again:

PRINT 1+2, 1/3, SIN(PI/4)
 3             .333333       .707107

Ready

So by using 7 KB more flash, we now have a substantially more complete implementation of BASIC. In fact, what we’re running here is a port of this to the STM32 platform:

MOS 6502AD 4585 top

There’s something strange going on here: our STM32F103 µC is running a C/C++ program (compiled via the Arduino IDE), which emulates a Mostek 6502 chip from the 1970’s, and then runs a BASIC implementation for the 6502 on it. Here’s the main sketch, in essence:

extern "C" {
    void exec6502(int32_t tickcount);
    int reset6502();

    void serout(uint8_t val) {
        Serial.write(val);
    }

    uint8_t getkey() {
        return Serial.available() ? Serial.read() : 0;
    }
}

void setup () {
    Serial.begin(115200);
    reset6502();
}

void loop () {
    exec6502(1000);
}

The actual emulator code is over 1400 lines of C, but still it’s all fairly small. In addition to code, the sketch also includes a 10 KB ROM image of the BASIC-in-6502 implementation.

In other words, the breakdown of this “emu6502” demo is as follows:

We do pay a price for this double level of interpretation, though:

LIST

10 FOR I=1 TO 10000:NEXT

Ready
RUN

Ready

That second “Ready” message appears after ≈ 10 seconds. So each iteration through the above loop in BASIC takes around 1 ms. It’s not very efficient. Then again, quite amazing that this is even possible - comparing today’s µC with what was used in the 1970’s.

Let’s continue on this thought a bit more. Just because we can…

Weblog © Jean-Claude Wippler. Generated by Hugo.