brainwagon "There is much pleasure in useless knowledge." — Bertrand Russell

7Jul/112

A basic simulator for Caxton Foster’s “Blue” Architecture…

Yep, been spending some time thinking about homebrew computer architectures. I've also been reading Gordon Bell's Computer Engineering, pondering some of the older and earlier computing architectures. Caxton Foster's Computer Architecture describes a simple computer architecture called "Blue", which has only 16 instructions, and uses only direct addressing. It's a pretty primitive machine (more like a PDP-8's out of work cousin), but in 30 pages, Foster describes pretty much the complete circuitry needed to implement the machine. I thought I'd write a simple behavoral simulator just to play with, and to see how much of a pain it is to program (answer: pretty big). For now, here's the code. More later.

#include <stdio.h>
#include <stdlib.h>

/*  _    _          
 * | |__| |___  ___ 
 * | '_ \ / _ \/ _ \
 * |_.__/_\___/\___/
 *                  
 * An implementation of the simplest version of Caxton Foster's "Blue" 
 * machine as described in his book _Computer Architecture_, Third
 * Edition, Copyright 1985.   Just meant to stimulate some experimentation.
 * 
 * The architecture described is _very_ simple, consisting of just sixteen
 * instructions, a single accumulator, no CPU flags like carry or overflow,
 * and only direct addressing.  This makes programming pretty annoying, 
 * virtually requiring self-modifying code to do anything non-trivial.
 * But in 30 pages, Foster presents pretty much the entire architecture, 
 * which made me spend 15 minutes to code up a simple behavioral simulator 
 * and play around with it.
 */

typedef unsigned short Word ;

#define MEMSIZE         4096

Word M[MEMSIZE] = {
        0x600C,
        0x100B,
        0x9004,
        0x0000,
        0x700C,
        0x6008,
        0x100B,
        0x7008,
        0x600C,
        0xC000,
        0xA000,
        1,
        -13,
        'H',
        'e',
        'l',
        'l',
        'o',
        ' ',
        'W',
        'o',
        'r',
        'l',
        'd',
        '!',
        '\n'
} ;

Word A ;
Word Z ;
Word MAR ;
Word MBR ;
Word PC ;
Word IR ;
Word CS ;

void
fetch()
{
    Word t ;

    /* fprintf(stderr, "... 0x%04x ", PC) ; */
    IR = M[PC] ;
    PC ++ ;
    PC &= (MEMSIZE-1) ;
}

void
execute()
{
    int op = IR >> 12 ;
    int addr = IR & 0xfff ;

    /* fprintf(stderr, "%0x %03x\n", op, addr) ; */

    switch (op) {
    case 0:             /* HLT */
        fprintf(stderr, "\n... bloo executed HLT.\n") ;
        exit(0) ;
    case 1:             /* ADD */
        A += M[addr] ;
        break ;
    case 2:             /* XOR */
        A ^= M[addr] ;
        break ;
    case 3:             /* AND */
        A &= M[addr] ;
        break ;
    case 4:             /* OR */
        A |= M[addr] ;
        break ;
    case 5:             /* NOT */
        A ^= 0xFFFF ;
        break ;
    case 6:             /* LDA */
        A = M[addr] ;
        break ;
    case 7:             /* STA */
        M[addr] = A ;
        break ;
    case 8:             /* SRJ */
        A = PC & 0xFFF ;
        PC = addr ;
        break ;
    case 9:             /* JMA */
        if (A&0x8000)
            PC = addr ;
        break ;
    case 0xA:           /* JMP */
        PC = addr ;
        break ;
    case 0xB:           /* INP */
        A = getchar() ;
        break ;
    case 0xC:           /* OUT */
        putchar(A) ;
        fflush(stdout) ;
        break ;
    case 0xD:           /* RAL */
        if (A & 0x8000)
            A = (A << 1) | 1 ;
        else
            A = (A << 1) ;
        break ;
    case 0xE:           /* CSA */
        A = CS ;
        break ;
    case 0xF:           /* NOP */
        break ;
    }
}

int
main(int argc, char *argv[])
{
    for (;;) {
        fetch() ;
        execute() ;
    }    
    return 0 ;
}

Comments (2) Trackbacks (0)
  1. Very cool. Check out my much expanded version of Blue in an FPGA at Opencores:

    http://opencores.org/project,blue

  2. Hi, I’ve been searching to download this book everywhere. Would you tell if me where you found it.?


Leave a comment

No trackbacks yet.