User:Troy martin/Emulators

From OSDev Wiki
Jump to: navigation, search

Written in C

8-bit Outline

#include <stdio.h>
#include <time.h>
#define LDX 0x01
#define LDY 0x02
#define IOX 0x10
#define HLT 0xFF

unsigned int ip=0, x=0, y=0, acc=0;
unsigned char memory[256] = {LDX,0x01,LDY,0x06,IOX,HLT,'M','y',' ','e','m','u','l','a','t','o','r',' ','w','o','r','k','s','!','\0'};

int slp(unsigned int mseconds) /* sleep: can be used for setting processor limits */
{
  clock_t goal = mseconds + clock();
  while (goal > clock());
  return 0;
}

void ldx(unsigned int xdata)
{
  x=xdata;
}

void ldy(unsigned int ydata)
{
  y=ydata;
}

void iox()
{
  switch(x)
  {
    case 0x01:
    printf("%s",memory+y);
    break;
  }
}

int main(void)
{
  int CPUIsRunning = 1;
  float memkb=sizeof(memory);
  printf("My Emulator 1.0\n%d bytes (%f KB) of memory.\n\n",sizeof(memory),(memkb/1024));
  while(CPUIsRunning==1)
  {
    //slp(100);
    switch(memory[ip])
    {
      case 0x01:
        ldx(memory[ip+1]);
        ip+=2;
        break;
      case 0x02:
        ldy(memory[ip+1]);
        ip+=2;
        break;
      case 0x10:
        iox();
        ip+=1;
        break;
      case 0xFF:
        printf("\n\nEmulator halted by opcode 0xFF (instruction HLT).\n");
        system("pause");
        CPUIsRunning=0;
        break;
      default:
        printf("\n\nEmulator halted by illegal opcode.\n");
        system("pause");
        CPUIsRunning=0;
        break;
    }
  }
  return 0;
}

16-bit

I'll post code or a link to pastebin'd code when it's more complete.

Personal tools
Namespaces
Variants
Actions
Navigation
About
Toolbox