Captain's Phlog                     2020.02.07
______________________________________________

Okay so what follows is my first attempt at
re-learning C. I studied it in college for one
month back in 1989 and promptly forgot it all.
I'm having a blest!
______________________________________________



/* An variation on the Little Man Computer
* as created by D. Stuart Madnick in 1965. */

/* My version operates on 4K of hex mailboxes
* also allowing for more instructions (A-F).
* I have also added OUT4 whcih outputs the
* calculator value as an ASCII character.
*
* YET TO IMPLEMENT: INP */


#include <stdio.h>

/**********************
**********************
** PROGRAM EXAMPLES **
**********************
**********************/

// Hello World
int program[] = { 0x5005,       // 0: LDA [5] ("H")
               0x9004,         // 1: OUT4 (ASCII)
               0x5006,         // 2: LDA [6] ("i")
               0x9004,         // 3: OUT4 (ASCII)
               0x0000,         // 4: HLT
               0x0048,         // 5: DAT "H"
               0x0069 };       // 6: DAT "i"

// Countdown
// int program[] = { 0x5006,    // 0: LDA [6] (0xA)
//              0x9002,         // 1: OUT2 (INT)
//              0x2007,         // 2: SUB [7] (0x1)
//              0x7005,         // 3: BRZ 5
//              0x6001,         // 4: JMP 1
//              0x0,            // 5: HLT
//              0x000A,         // 6: DAT 0xA
//              0x0001 };       // 7: DAT 0x1


// VARIABLES
 int pc = 0x0;
 int ir = 0x0;
 int opcode = 0xFFFF;
 int mailbox = 0x0;
 int calculator = 0x41;


void main() {
 printf("\nThe Little Man Computer\n   - Emulator in C -\n\n");

 do {
   ir = program[pc];   // load instruction into IR
   opcode = ir>>12;    // tease out the Opcode
   mailbox = ir - ( opcode<<12 );      // tease out the Mailbox

   switch (opcode) {
     case 1:   // ADD
       calculator = calculator + program[mailbox];
       pc++;
       break;
     case 2:   // SUB
       calculator = calculator - program[mailbox];
       pc++;
       break;
     case 3:   // STA
       program[mailbox] = calculator;
       pc++;
       break;
     case 4:   // UNDEFINED
       break;
     case 5:   // LDA
       calculator = program[mailbox];
       pc++;
       break;
     case 6:   // JMP (BRA Unconditional)
       pc = mailbox;
       break;
     case 7:   // BRZ (BRA if Zero)
       if (calculator == 0) pc = mailbox;
         else pc++;
       break;
     case 8:   // BRP (BRA if >= Zero)
       if (calculator >= 0) pc = mailbox;
         else pc++;
       break;
     case 9:   // I/O
       if (mailbox == 0x2) {           // Output Numeric
         printf("%X", calculator); }
       else if (mailbox == 0x4) {      // Output Equivalent Char
         printf("%c", calculator); }
       pc++;
       break;
     default:  // Expansion for Opcodes 0xA - 0xF
       break; }

 } while (opcode != 0x0  /* Coffee Break */ );

   printf("\n\nCalculator: %X\n\n", calculator);       // exit
}