]> Shamusworld >> Repos - thunder/blobdiff - src/thunder.cpp
Changed main emu code to use preallocated arrays
[thunder] / src / thunder.cpp
index 6413b4c239a59934fed4bc5e2b8372261616e123..3a9d8ae3ae84a67965ffd918b0552425e3c32164 100755 (executable)
@@ -1,14 +1,17 @@
 //
-// Thunder: A Rolling Thunder Emulator w/6809 debugger v0.40
-// (Last build: 3/6/2004)
+// Thunder: A Rolling Thunder Emulator w/6809 debugger
 //
 // by James L. Hammons
+// (c) 2004, 2009 Underground Software
 //
-// (c) 2004 Underground Software
+// JLH = James L. Hammons <jlhamm@acm.org>
+//
+// WHO  WHEN        WHAT
+// ---  ----------  ------------------------------------------------------------
+// JLH  07/23/2009  Added changelog ;-)
 //
 
-#define THUNDER_VERSION       "0.4.0"
-#define THUNDER_BETA_VERSION  "6"
+#define THUNDER_VERSION       "0.9.9"
 
 #include <iostream>
 #include <iomanip>
@@ -17,7 +20,8 @@
 #include <new>
 #include <stdio.h>
 #include <stdlib.h>
-#include <conio.h>                     // For getch()
+//#include <conio.h>                   // For getch()
+#include <curses.h>                    // For getch()
 #include <time.h>
 #include "SDL.h"                                                                               // Get yer SDL out...!
 #include "types.h"
@@ -28,7 +32,7 @@
 
 using namespace std;                                                                   // Yes!
 
-/*
+#if 0
 #define ROM1  "RT3-1B.ROM"
 #define ROM2  "RT3-2B.ROM"
 #define ROM3  "RT3-3.ROM"
@@ -56,7 +60,7 @@ using namespace std;                                                                  // Yes!
 #define PROM3 "RT1-3.BIN"
 #define PROM4 "RT1-4.BIN"
 #define PROM5 "RT1-5.BIN"
-*/
+#else
 #define ROM1   "rt3-1b.9c"
 #define ROM2   "rt3-2b.12c"
 #define ROM3   "rt3-3.12d"
@@ -85,41 +89,56 @@ using namespace std;                                                                        // Yes!
 #define PROM4  "mb7138h.6v"
 #define PROM5  "mb7112e.6u"
 #define MCUROM "rt1-mcu.bin"
+#endif
 
 // Global defines
 
 SDL_Surface * screen;
 
-BYTE * gram, * grom;                // Allocate RAM & ROM pointers
-BYTE * gram1, * gram2, * grom1, * grom2;  // Actual memory
-BYTE * grom3, * grom4, * data_rom, * spr_rom, * voice_rom;
-BYTE * chr_rom;                     // Character ROM pointer
+uint8 * gram, * grom;                                                  // Allocate RAM & ROM pointers
+uint8 gram1[0x10000], gram2[0x10000], grom1[0x10000], grom2[0x10000];  // Actual memory
+uint8 grom3[0x8000], grom4[0x8000], data_rom[0x40000], spr_rom[0x80000], voice_rom[0x20000];
+uint8 chr_rom[0x60000];                                                        // Character ROM pointer
+/*
+  gram1 = new uint8[0x10000];
+  grom1 = new uint8[0x10000];
+  gram2 = new uint8[0x10000];
+  grom2 = new uint8[0x10000];
+  chr_rom = new uint8[0x60000];
+  grom3 = new uint8[0x8000];
+  grom4 = new uint8[0x8000];
+  data_rom = new uint8[0x40000];
+  spr_rom = new uint8[0x80000];
+  voice_rom = new uint8[0x20000];
+*/
 
 V6809REGS cpu1, cpu2;
 
-bool trace = false;                 // ditto...
+bool trace1 = false;                 // ditto...
 bool looking_at_rom = true;         // true = R1, false = R2
-DWORD banksw1, banksw2;             // Bank switch addresses
-WORD game_over_switch;              // Game over delay
-WORD dpc;                           // Debug pc reg...
+uint32 banksw1, banksw2;             // Bank switch addresses
+uint16 game_over_switch;              // Game over delay
+uint16 dpc;                           // Debug pc reg...
 bool show_scr = true;            // Whether or not to show background
 bool enable_cpu = true;          // Whether or not to enable CPUs
 bool irqGoA = true;              // IRQ switch for CPU #1
 bool irqGoB = true;              // IRQ switch for CPU #2
 
-WORD refresh = 0;                // Crappy global screen stuff...
+uint16 refresh_ = 0;                // Crappy global screen stuff...
 bool refresh2 = true;
 
-DWORD psg_lens[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-BYTE * psg_adrs[16];
-DWORD voc_lens[32] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+uint32 psg_lens[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+uint8 * psg_adrs[16];
+uint32 voc_lens[32] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-BYTE * voc_adrs[32];
-DWORD fm_lens[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
-BYTE * fm_adrs[14];
+uint8 * voc_adrs[32];
+uint32 fm_lens[14] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+uint8 * fm_adrs[14];
 
 fstream tr;    // Tracelog hook
-WORD    pcx;   // Where we at?
+uint16    pcx;   // Where we at?
+
+static uint8 * keys;                                                   // SDL raw keyboard matrix
 
 static char op_mat1[256] = {
   1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
@@ -276,36 +295,12 @@ tregs[16][3] = {
   "A",  "B", "CC", "DP", "??", "??", "??", "??" },
 iregs[4][2] = {"X", "Y", "U", "S" };
 
-//
-// Fetch a byte out of memory
-//
-/*BYTE Fetch()
-{
-//  extern WORD sr, ur, xr, yr;            // Needed for tracelog
-  extern WORD pcr;
-  BYTE b;
-
-  b = (pcr < 0x8000 ? gram1[pcr] : grom1[pcr]);
-  pcr++;
-
-  return b;
-}*/
-
-//
-// Fetch a word out of memory (little endian format)
-//
-/*WORD FetchW()
-{
-  WORD w = Fetch() << 8;  w |= Fetch();
-  return w;
-}*/
-
 //
 // Read a byte from memory (without touching PC. Not a Fetch!)
 //
-BYTE RdMem(WORD addr)
+uint8 RdMem(uint16 addr)
 {
-       BYTE b;
+       uint8 b;
 
        if (addr < 0x8000)
        {
@@ -323,18 +318,18 @@ BYTE RdMem(WORD addr)
 //
 // Write a byte to memory
 //
-void WrMem(WORD addr, BYTE b)
+void WrMem(uint16 addr, uint8 b)
 {
        extern bool charbase;                                                           // Needed for screen. Extern it in it??
-  //extern WORD sr, ur, xr, yr;            // Needed for tracelog
-  //extern WORD pcr;
+  //extern uint16 sr, ur, xr, yr;            // Needed for tracelog
+  //extern uint16 pcr;
 /*  if ((addr>0x40FF) && (addr<0x4390))
   {
     tr << hex << addr << ":" << (int)b;
     //for(int i=0; i<32; i++)
     //{
     //  if (gram1[0x4400+i]<0x10)  tr << "0";
-    //  tr << hex << (WORD)gram1[0x4400+i] << " ";
+    //  tr << hex << (uint16)gram1[0x4400+i] << " ";
     //}
     tr << endl;
   }//*/
@@ -344,7 +339,7 @@ void WrMem(WORD addr, BYTE b)
        if (addr == 0x6400)
                SpawnSound(GAMESOUND, gram1[0x6600], 1);                // Do voice chan 2
        if (addr == 0x6800)
-               banksw1 = (DWORD)b << 13;                                               // Set char data bankswitch base address
+               banksw1 = (uint32)b << 13;                                              // Set char data bankswitch base address
        if (addr > 0x4284 && addr < 0x42A5 && b)
                SpawnSound(PSGSOUND, addr - 0x4285);                    // Do PSG sound on chans 2, 3
        if (addr == 0x4380)
@@ -361,57 +356,23 @@ void WrMem(WORD addr, BYTE b)
                charbase = true;                                                                // Char banksw2
        if (addr == 0x8400)                                                                     // Frame go strobe? VBlank acknowledge?
        {
-               if (refresh++ == 1)                                                             // 30 Hz...
+               if (refresh_++ == 1)                                                            // 30 Hz...
                {
                        BlitChar(screen, chr_rom, gram1);
-                       refresh = (refresh2 ? 1 : 0);                           // 60/30 Hz...
+                       refresh_ = (refresh2 ? 1 : 0);                          // 60/30 Hz...
                }
 //             irqGoA = true; // Will this work??? no...
                cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;//wil wok???
        }
 }
 
-//
-// Fetch a byte out of memory (2nd processor)
-//
-/*BYTE FetchB()
-{
-//  extern WORD sr, ur, xr, yr;            // Needed for tracelog
-  extern WORD pcrB;
-  BYTE b;
-
-  if (pcrB<0x8000)
-  {
-    if (pcrB<0x2000)
-      b = gram1[pcrB+0x4000];
-    if ((pcrB>0x1FFF) && (pcrB<0x6000))
-      b = gram1[pcrB-0x2000];
-    if (pcrB>0x5FFF)
-      b = gram1[pcrB];
-  }
-  else
-    b = grom2[pcrB];
-  pcrB++;
-
-  return b;
-}
-
-//
-// Fetch a word out of memory (little endian format) (2nd processor)
-//
-WORD FetchWB()
-{
-  WORD w = FetchB() << 8;  w |= FetchB();
-  return w;
-}*/
-
 //
 // Read a byte from memory (without touching PC. Not a Fetch!) (2nd processor)
 //
-BYTE RdMemB(WORD addr)
+uint8 RdMemB(uint16 addr)
 {
-//  extern WORD cpu2.s, cpu2.u, cpu2.x, cpu2.y;            // Needed for tracelog
-       BYTE b;
+//  extern uint16 cpu2.s, cpu2.u, cpu2.x, cpu2.y;            // Needed for tracelog
+       uint8 b;
 
        if (addr < 0x8000)
        {
@@ -438,18 +399,18 @@ BYTE RdMemB(WORD addr)
 //
 // Write a byte to memory (2nd processor)
 //
-void WrMemB(WORD addr, BYTE b)
+void WrMemB(uint16 addr, uint8 b)
 {
        extern bool charbase;
-  //extern WORD sr, ur, xr, yr;            // Needed for tracelog
-  //extern WORD pcr;
+  //extern uint16 sr, ur, xr, yr;            // Needed for tracelog
+  //extern uint16 pcr;
 /*  if ((addr>0x00FF) && (addr<0x0390))
   {
     tr << hex << addr << ":" << (int)b;
     //for(int i=0; i<32; i++)
     //{
     //  if (gram1[0x4400+i]<0x10)  tr << "0";
-    //  tr << hex << (WORD)gram1[0x4400+i] << " ";
+    //  tr << hex << (uint16)gram1[0x4400+i] << " ";
     //}
     tr << endl;
   }//*/
@@ -464,7 +425,7 @@ void WrMemB(WORD addr, BYTE b)
        if (addr > 0x0284 && addr < 0x02A5 && b)
                SpawnSound(PSGSOUND, addr - 0x0285);                    // Do PSG sound on chans 2, 3
        if (addr == 0xD803)
-               banksw2 = (DWORD)(b & 0x03) << 13;                              // Set sprite data bank switch
+               banksw2 = (uint32)(b & 0x03) << 13;                             // Set sprite data bank switch
        if (addr == 0x0380)
        {
                SpawnSound(FMSOUND, b);                                                 // Do FM sound on chan 4
@@ -485,9 +446,9 @@ void WrMemB(WORD addr, BYTE b)
 //
 // Display bytes in mem in hex
 //
-void DisplayBytes(WORD src, unsigned long dst)
+void DisplayBytes(uint16 src, unsigned long dst)
 {
-  BYTE cnt;
+  uint8 cnt;
   unsigned long i;
 
   printf("%04X: ", src);
@@ -495,7 +456,7 @@ void DisplayBytes(WORD src, unsigned long dst)
   if (src > dst)  dst += 0x10000;        // That should fix the FFFF bug...
   for(i=src; i<dst; i++)
   {
-    printf("%02X ", (BYTE)(looking_at_rom ? RdMem(i) : RdMemB(i)));
+    printf("%02X ", (uint8)(looking_at_rom ? RdMem(i) : RdMemB(i)));
     cnt++;                               // Bump counter...
   }
   for(i=cnt; i<5; i++)                   // Pad the leftover spaces...
@@ -505,28 +466,28 @@ void DisplayBytes(WORD src, unsigned long dst)
 }
 
 // temp crap...
-BYTE Fetch(void) { return 0; }
-WORD FetchW(void) { return 0; }
-BYTE FetchB(void) { return 0; }
-WORD FetchWB(void) { return 0; }
+uint8 Fetch(void) { return 0; }
+uint16 FetchW(void) { return 0; }
+uint8 FetchB(void) { return 0; }
+uint16 FetchWB(void) { return 0; }
 
 //
 // Decode a 6809 instruction at 'addr'
 //
 void Decode_6809()
 {
-  BYTE (* DFetch)();           // Decode Fetch() pointer...
-  WORD (* DFetchW)();          // Decode FetchW() pointer...
+  uint8 (* DFetch)();           // Decode Fetch() pointer...
+  uint16 (* DFetchW)();          // Decode FetchW() pointer...
   DFetch  = (looking_at_rom ? Fetch : FetchB);
   DFetchW = (looking_at_rom ? FetchW : FetchWB);
 
-/*  extern*/ WORD pcr, pcrB;              // Pull in 'pcr' from '6809.cpp'
-  WORD pc_save = pcr, pcB_save = pcrB;
+/*  extern*/ uint16 pcr, pcrB;              // Pull in 'pcr' from '6809.cpp'
+  uint16 pc_save = pcr, pcB_save = pcrB;
   pcr  = dpc;  pcrB = dpc;
-  BYTE opcode = DFetch();             // Get the opcode ('fetch' cycle)
-  BYTE opcode2, operand;
-  WORD loperand;
-  BYTE admode = op_mat1[opcode];     // addressing mode
+  uint8 opcode = DFetch();             // Get the opcode ('fetch' cycle)
+  uint8 opcode2, operand;
+  uint16 loperand;
+  uint8 admode = op_mat1[opcode];     // addressing mode
   char outbuf[80], mnem[6], tmp[30];
 
   strcpy(mnem, mnemonics[opcode]);  // Copy page 1 opcode
@@ -556,13 +517,13 @@ void Decode_6809()
       break; }
     case 3:  // Relative
     { operand = DFetch();   // Get offset
-      WORD tmpc = (looking_at_rom ? pcr : pcrB);
-      sprintf(outbuf, "%s $%04X", mnem, tmpc+(SWORD)(SBYTE)operand);
+      uint16 tmpc = (looking_at_rom ? pcr : pcrB);
+      sprintf(outbuf, "%s $%04X", mnem, tmpc+(int16)(int8)operand);
       break; }
     case 4:  // Long Relative
     { loperand = DFetchW(); // Get long offset
-      WORD tmpc = (looking_at_rom ? pcr : pcrB);
-      sprintf(outbuf, "%s $%04X", mnem, tmpc+(SWORD)loperand);
+      uint16 tmpc = (looking_at_rom ? pcr : pcrB);
+      sprintf(outbuf, "%s $%04X", mnem, tmpc+(int16)loperand);
       break; }
     case 5:  // Inherent
     { sprintf(outbuf, "%s ", mnem);
@@ -583,16 +544,16 @@ void Decode_6809()
         if (operand&0x10)  strcat(tmp, "X ");
         if (operand&0x20)  strcat(tmp, "Y ");
         if (operand&0x40)  (((opcode==0x34)||(opcode==0x35))
-                       ? strcat(tmp, "U ") : strcat(tmp, "S "));                                  
+                       ? strcat(tmp, "U ") : strcat(tmp, "S "));
         if (operand&0x80)  strcat(tmp, "PC");
       }
       sprintf(outbuf, "%s %s", mnem, tmp);
       break; }
     case 7:  // Indexed (the tough one!)
     { operand = DFetch();   // Get IDX byte
-      BYTE reg = ((operand & 0x60) >> 5), idxind = ((operand & 0x10) >> 4),
+      uint8 reg = ((operand & 0x60) >> 5), idxind = ((operand & 0x10) >> 4),
            lo_nyb = (operand & 0x0F),  boff;
-      WORD woff;
+      uint16 woff;
 
       strcpy(tmp, "??");
       if (!(operand & 0x80))      // Hi bit set? Then decode 4 bit offset
@@ -614,17 +575,17 @@ void Decode_6809()
             case 8:
             { boff = DFetch();  sprintf(tmp, "($%02X,%s)", boff,
                                        iregs[reg]);  break; }
-            case 9:   
+            case 9:
             { woff = DFetchW();  sprintf(tmp, "($%04X,%s)", woff,
                                         iregs[reg]);  break; }
             case 11:  sprintf(tmp, "(D,%s)", iregs[reg]);  break;
             case 12:
             { boff = DFetch();  sprintf(tmp, "($%02X,PC)", boff);  break; }
-            case 13:  
+            case 13:
             { woff = DFetchW();  sprintf(tmp, "($%04X,PC)", woff);  break; }
             case 15:
             { woff = DFetchW();  sprintf(tmp, "[$%04X]", woff);  break; }
-            default:  strcpy(tmp, "??"); 
+            default:  strcpy(tmp, "??");
           }
         }
         else
@@ -641,15 +602,15 @@ void Decode_6809()
             case 8:
             { boff = DFetch();  sprintf(tmp, "($%02X),%s", boff,
                                        iregs[reg]);  break; }
-            case 9:   
+            case 9:
             { woff = DFetchW();  sprintf(tmp, "($%04X),%s", woff,
                                        iregs[reg]);  break; }
             case 11:  sprintf(tmp, "(D),%s", iregs[reg]);  break;
             case 12:
             { boff = DFetch();  sprintf(tmp, "($%02X),PC", boff);  break; }
-            case 13:  
+            case 13:
             { woff = DFetchW();  sprintf(tmp, "($%04X),PC", woff);  break; }
-            default:  strcpy(tmp, "??"); 
+            default:  strcpy(tmp, "??");
           }
         }
       }
@@ -673,23 +634,23 @@ void Decode_6809()
 //
 // Convert hex to dec
 //
-WORD htod(char *str)
+uint16 htod(char *str)
 {
-  WORD value = 0;
+  uint16 value = 0;
   int len = strlen(str);
 
   for(int i=0; i<len; i++)
   {
     if (str[i]>='0' && str[i]<='9')
-    { 
+    {
       value = (value<<4) | (unsigned)(str[i]-'0');
     }
     if (str[i]>='a' && str[i]<='f')
-    { 
+    {
       value = (value<<4) | (unsigned)(str[i]-'a')+10;
     }
     if (str[i]>='A' && str[i]<='F')
-    { 
+    {
       value = (value<<4) | (unsigned)(str[i]-'A')+10;
     }
   }
@@ -699,14 +660,14 @@ WORD htod(char *str)
 //
 // Load 32K file into ROM image space
 //
-bool Load32KImg(char * filename, WORD address)
+bool Load32KImg(char * filename, uint16 address)
 {
   ifstream ff;
   char ch;
 
   ff.open(filename, ios::binary | ios::in);  // Open 'da file...
   if (ff)
-  {                                          
+  {
     for(long i=0; i<32768; i++)                // Read it in...
     {
       ff.get(ch);
@@ -721,7 +682,7 @@ bool Load32KImg(char * filename, WORD address)
 // Generic Load file into image space
 // (No error checking performed!  Responsibility of caller!)
 //
-bool LoadImg(char * filename, BYTE * mem, DWORD address, DWORD length)
+bool LoadImg(const char * filename, uint8 * mem, uint32 address, uint32 length)
 {
   ifstream ff;
   char path[80];
@@ -732,8 +693,8 @@ bool LoadImg(char * filename, BYTE * mem, DWORD address, DWORD length)
 //  ff.open(filename, ios::binary | ios::in);  // Open 'da file...
   ff.open(path, ios::binary | ios::in);       // Open 'da file...
   if (ff)
-  {                                          
-    for(DWORD i=0; i<length; i++)             // Read it in...
+  {
+    for(uint32 i=0; i<length; i++)             // Read it in...
     {
       ff.get(ch);
       mem[address+i] = ch;
@@ -749,11 +710,11 @@ bool LoadImg(char * filename, BYTE * mem, DWORD address, DWORD length)
 bool ReadColorPROMs(void)
 {
   fstream ff1, ff2;
-//  BYTE ch;
+//  uint8 ch;
   char ch;
-  extern BYTE palette[768];     // Screen physical palette
-  extern BYTE ccolor[256][8];   // Character color PROM values
-  extern BYTE scolor[128][16];  // Sprite color PROM values
+  extern uint8 palette[768];     // Screen physical palette
+  extern uint8 ccolor[256][8];   // Character color PROM values
+  extern uint8 scolor[128][16];  // Sprite color PROM values
 
   ff1.open("./ROMs/"PROM3, ios::binary|ios::in);
   if (ff1)
@@ -763,7 +724,7 @@ bool ReadColorPROMs(void)
       for(int j=0; j<8; j++)
       {
         ff1.get(ch);
-        ccolor[i][j] = (BYTE)ch;
+        ccolor[i][j] = (uint8)ch;
       }
     }
     ff1.close();
@@ -776,7 +737,7 @@ bool ReadColorPROMs(void)
       for(int j=0; j<16; j++)
       {
         ff1.get(ch);
-        scolor[i][j] = (BYTE)ch;
+        scolor[i][j] = (uint8)ch;
       }
     }
     ff1.close();
@@ -789,10 +750,10 @@ bool ReadColorPROMs(void)
     for(int i=0; i<768; i+=3)
     {
       ff1.get(ch);
-      palette[i]   = (BYTE)(ch&0x0F);
-      palette[i+1] = (BYTE)(ch>>4);
+      palette[i]   = (uint8)(ch&0x0F);
+      palette[i+1] = (uint8)(ch>>4);
       ff2.get(ch);
-      palette[i+2] = (BYTE)ch;
+      palette[i+2] = (uint8)ch;
     }
 
     // Do palette stretching here... I.e. add 0 to hinyb 0, 1 to hinyb 1, etc.
@@ -811,7 +772,7 @@ bool ReadColorPROMs(void)
 //
 bool UnpackFonts(void)
 {
-//  BYTE b1, b2, b3;
+//  uint8 b1, b2, b3;
   char b1, b2, b3;
   fstream f1, f2;
   //0x4000 $800 chars
@@ -863,27 +824,27 @@ bool UnpackFonts(void)
 //
 // Get length of sample from WAV format
 //
-DWORD GetWAVLength(fstream &file)
+uint32 GetWAVLength(fstream &file)
 {
        char ch;
-       DWORD len;
+       uint32 len;
 
        file.ignore(16);                                                                        // Skip header BS
 
        for(int i=0; i<2; i++)
        {
-               file.get(ch);  len = (int)(BYTE)ch;
-               file.get(ch);  len |= (int)(BYTE)ch << 8;
-               file.get(ch);  len |= (int)(BYTE)ch << 16;
-               file.get(ch);  len |= (int)(BYTE)ch << 24;
+               file.get(ch);  len = (int)(uint8)ch;
+               file.get(ch);  len |= (int)(uint8)ch << 8;
+               file.get(ch);  len |= (int)(uint8)ch << 16;
+               file.get(ch);  len |= (int)(uint8)ch << 24;
 
                file.ignore(len + 4);                                                   // Skip intermediate data
        }
 
-       file.get(ch);  len = (int)(BYTE)ch;                                     // & finally get length of data
-       file.get(ch);  len |= (int)(BYTE)ch << 8;
-       file.get(ch);  len |= (int)(BYTE)ch << 16;
-       file.get(ch);  len |= (int)(BYTE)ch << 24;
+       file.get(ch);  len = (int)(uint8)ch;                                    // & finally get length of data
+       file.get(ch);  len |= (int)(uint8)ch << 8;
+       file.get(ch);  len |= (int)(uint8)ch << 16;
+       file.get(ch);  len |= (int)(uint8)ch << 24;
 
        return len;
 }
@@ -895,7 +856,7 @@ void LoadPSGs(void)
 {
        char file[40];
        char ch;
-       DWORD len;
+       uint32 len;
 
        for(int i=0; i<16; i++)
        {
@@ -910,7 +871,7 @@ void LoadPSGs(void)
                {
                        len = GetWAVLength(fp);                                         // Get WAV data length...
 
-                       psg_adrs[i] = new BYTE[len];                            // Attempt to allocate space...
+                       psg_adrs[i] = new uint8[len];                           // Attempt to allocate space...
 
                        if (psg_adrs[i] != NULL)
                        {
@@ -936,7 +897,7 @@ void LoadFMs(void)
 {
        char file[200];
        char ch;
-       DWORD len;
+       uint32 len;
 
        for(int i=0; i<14; i++)
        {
@@ -951,7 +912,7 @@ void LoadFMs(void)
                {
                        len = GetWAVLength(fp);                                         // Get WAV length...
 
-                       fm_adrs[i] = new BYTE[len];                                     // Attempt to allocate space...
+                       fm_adrs[i] = new uint8[len];                                    // Attempt to allocate space...
 
                        if (fm_adrs[i] != NULL)
                        {
@@ -978,96 +939,80 @@ int main(int argc, char * argv[])
 {
        InitLog("thunder.log");
 
-/*  extern WORD cpu1.pc, cpu1.s, cpu1.u, cpu1.x, cpu1.y;            // Pull in vars from '6809.cpp'
-  extern BYTE cpu1.a, cpu1.b, cpu1.cc, cpu1.dp;
-  extern long iclock;
-  extern bool illegal;
-  extern WORD cpu2.pc, cpu2.s, cpu2.u, cpu2.x, cpu2.y;       // Pull in vars from '6809B.cpp'
-  extern BYTE cpu2.a, cpu2.b, cpu2.cc, cpu2.dp;
-  extern long iclockB;
-  extern bool illegalB;
-  extern void (* exec_op0[256])();   // Array of page zero opcode functions...
-  extern void (* exec_op1[256])();   // Array of page one opcode functions...
-  extern void (* exec_op2[256])();   // Array of page two opcode functions...
-  extern void (* exec_op0B[256])();  // Array of page zero opcode functions...
-  extern void (* exec_op1B[256])();  // Array of page one opcode functions...
-  extern void (* exec_op2B[256])();  // Array of page two opcode functions...*/
-  extern bool charbase;                                                                        // From 'SCREEN.CPP'
-//  extern unsigned int vesa_memptr;
-  charbase = false;
+       extern bool charbase;                                           // From 'SCREEN.CPP'
+       charbase = false;
 
   char lbuff[80];
   fstream ff;                       // Declare fstream without file hooks...
   bool brk = false, brk2 = false;   // Breakpoint set flag
-  WORD brkpnt, brkpnt2;             // Where the breakpoint is...
+  uint16 brkpnt, brkpnt2;             // Where the breakpoint is...
   bool running;                     // CPU running state flag...
   bool self_test = false;           // Self-test switch
   bool scr_type = false;            // false=chars, true=pixels
-  WORD debounce = 0;                // Key de-bounce counter
-  WORD fire_debounce = 0;           // Fire button debounce counter
+  uint16 debounce = 0;                // Key de-bounce counter
+  uint16 fire_debounce = 0;           // Fire button debounce counter
 //  bool refresh2 = true;             // Default to 60 Hz...
-  BYTE x;                           // General placeholder...
-  bool active = true;               // Program running flag
+  uint8 x;                           // General placeholder...
+  bool active = true;                                          // Program running flag
 
-//  SDL_Surface * screen = NULL;      // SDL screen pointer
-  SDL_Event event;                  // SDL "event"
-//  int keyPressed;                   // SDL key pressed...
-  BYTE keys[256];                   // Keyboard "switch-like" buffer
-  extern BYTE palette[768];         // Screen physical palette
-  DWORD ticks, oldTicks;
+  SDL_Event event;                                                             // SDL "event"
+  extern uint8 palette[768];                                   // Screen physical palette
+  uint32 ticks, oldTicks;
 
-//  tr.open("exe.log", ios::binary | ios::out); // Tracelog
+       cout << endl << "THUNDER v"THUNDER_VERSION" ";
+       cout << "by James Hammons" << endl;
+       cout << "Serial #20090723 / Prerelease" << endl;
+       cout << "(C) 2003, 2009 Underground Software" << endl << endl;
 
-  cout << endl << "THUNDER beta "THUNDER_BETA_VERSION" ";
-  cout << "by Jimmy Hamm <jlhamm@acm.org>" << endl;
-  cout << "Serial #20030313 / Prerelease" << endl;
-  cout << "(C) 2003 Underground Software" << endl << endl;
+       cout << "This emulator is free software. If you paid for it you were RIPPED OFF"
+               << endl << endl;
 
-  cout << "This emulator is freeware.  If you paid for it you were RIPPED OFF"
-       << endl << endl;
+       cout << "Initializing SDL..." << endl;
 
-  cout << "Initializing SDL..." << endl;
+       if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
+       {
+               cout << "Couldn't initialize SDL: " << SDL_GetError() << endl;
+               return -1;
+       }
 
-  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
-  {
-    cout << "Couldn't initialize SDL: " << SDL_GetError() << endl;
-    return -1;
-  }
+       SDL_WM_SetCaption("Thunder v"THUNDER_VERSION" ", "Thunder");
 
-  SDL_WM_SetCaption("Thunder Beta 6", "Thunder");
+       keys = SDL_GetKeyState(NULL);                           // Get the SDL keyboard matrix
 
+#if 0
   cout << "Allocating memory..." << endl;
 //Does this anyway...  set_new_handler(0);    // Make 'new' return NULL on failure...
-  gram1 = new BYTE[0x10000];
+  gram1 = new uint8[0x10000];
   if (gram1 == NULL)  { cout << "Could not allocate RAM space #1!" << endl
                       << "Aborting!" << endl;  return -1; }
-  grom1 = new BYTE[0x10000];
+  grom1 = new uint8[0x10000];
   if (grom1 == NULL)  { cout << "Could not allocate ROM space #1!" << endl
                       << "Aborting!" << endl;  return -1; }
-  gram2 = new BYTE[0x10000];
+  gram2 = new uint8[0x10000];
   if (gram2 == NULL)  { cout << "Could not allocate RAM space #2!" << endl
                       << "Aborting!" << endl;  return -1; }
-  grom2 = new BYTE[0x10000];
+  grom2 = new uint8[0x10000];
   if (grom2 == NULL)  { cout << "Could not allocate ROM space #2!" << endl
                       << "Aborting!" << endl;  return -1; }
-  chr_rom = new BYTE[0x60000];
+  chr_rom = new uint8[0x60000];
   if (chr_rom == NULL)  { cout << "Could not allocate character RAM!" << endl
                       << "Aborting!" << endl;  return -1; }
-  grom3 = new BYTE[0x8000];
+  grom3 = new uint8[0x8000];
   if (grom3 == NULL)  { cout << "Could not allocate ROM space #4!" << endl
                       << "Aborting!" << endl;  return -1; }
-  grom4 = new BYTE[0x8000];
+  grom4 = new uint8[0x8000];
   if (grom4 == NULL)  { cout << "Could not allocate ROM space #5!" << endl
                       << "Aborting!" << endl;  return -1; }
-  data_rom = new BYTE[0x40000];
+  data_rom = new uint8[0x40000];
   if (data_rom == NULL)  { cout << "Could not allocate ROM level data!" << endl
                       << "Aborting!" << endl;  return -1; }
-  spr_rom = new BYTE[0x80000];
+  spr_rom = new uint8[0x80000];
   if (spr_rom == NULL)  { cout << "Could not allocate ROM sprite data!" << endl
                       << "Aborting!" << endl;  return -1; }
-  voice_rom = new BYTE[0x20000];
+  voice_rom = new uint8[0x20000];
   if (voice_rom == NULL)  { cout << "Could not allocate ROM voice data!" << endl
                       << "Aborting!" << endl;  return -1; }
+#endif
 
   gram = gram1;  grom = grom1;           // Needed only for debugger
 
@@ -1137,18 +1082,19 @@ int main(int argc, char * argv[])
 
   if (!LoadImg(ROM22, voice_rom, 0x10000, 0x10000))  // Load 22nd ROM
   { cout << "Could not open file '" << ROM22 << "'!" << endl;  return -1; }
-     
+
   if (!UnpackFonts())                         // Load 5, 6, 7, 8th ROMs
   {
     cout << "Could not open font files!" << endl;
     return -1;
   }
 
-       LoadPSGs();                                                                                     // Load samples if they're there...
+       // Load samples if they're there...
+       LoadPSGs();
        LoadFMs();
 
   // Quick 'n' Dirty voice dump (sound 0x0E)
-/*  DWORD adc = (voice_rom[26]<<8) | voice_rom[27];
+/*  uint32 adc = (voice_rom[26]<<8) | voice_rom[27];
   bool doneWitIt = false;
   int crh = 0;
   while (!doneWitIt)
@@ -1163,12 +1109,12 @@ int main(int argc, char * argv[])
 
        // Set up V6809 execution contexts
 
-       memset(&cpu1, sizeof(V6809REGS), 0);
+       memset(&cpu1, 0, sizeof(V6809REGS));
        cpu1.RdMem = RdMem;
        cpu1.WrMem = WrMem;
        cpu1.cpuFlags |= V6809_ASSERT_LINE_RESET;
 
-       memset(&cpu2, sizeof(V6809REGS), 0);
+       memset(&cpu2, 0, sizeof(V6809REGS));
        cpu2.RdMem = RdMemB;
        cpu2.WrMem = WrMemB;
        cpu2.cpuFlags |= V6809_ASSERT_LINE_RESET;
@@ -1195,8 +1141,8 @@ WriteLog("About to go to the main loop...\n");
                                lbuff[0] = 32;
                                dpc = htod(lbuff);
                        }
-                       printf("%04X: ", dpc); 
-                       WORD pc_save = cpu1.pc, pcB_save = cpu2.pc;
+                       printf("%04X: ", dpc);
+                       uint16 pc_save = cpu1.pc, pcB_save = cpu2.pc;
                        cpu1.pc = dpc;  cpu2.pc = dpc;
                        for(int i=0; i<16; i++)
                                printf("%02X ", (looking_at_rom ? Fetch() : FetchB()));
@@ -1204,7 +1150,7 @@ WriteLog("About to go to the main loop...\n");
                        cpu1.pc = dpc;  cpu2.pc = dpc;
                        for(int i=0; i<16; i++)
                        {
-                               BYTE a = (looking_at_rom ? Fetch() : FetchB());
+                               uint8 a = (looking_at_rom ? Fetch() : FetchB());
                                if (a<10)             cout << (char)(a+48);
                                if ((a>9) && (a<37))  cout << (char)(a+55);
                                if (a>36)             cout << ".";
@@ -1221,7 +1167,7 @@ WriteLog("About to go to the main loop...\n");
                                dpc = htod(lbuff);
                        }
                        printf("%04X: ", dpc);
-                       for(int i=0; i<16; i++)  printf("%02X ", (BYTE)gram[dpc++]);
+                       for(int i=0; i<16; i++)  printf("%02X ", (uint8)gram[dpc++]);
                        cout << endl;
                }
                else if (lbuff[0] == 'l')
@@ -1265,11 +1211,11 @@ WriteLog("About to go to the main loop...\n");
                else if ((lbuff[0] == 'r') || (lbuff[0] == 'c')) // Run/continue...
                {
 WriteLog("Executing 'run' command...\n");
-                       DWORD my_clock = 0;
+                       uint32 my_clock = 0;
                        running = true;                                                         // Set running status...
-                       trace = false;
+                       trace1 = false;
                        SetRefreshRate(refresh2);                                       // Tell GUI our refresh rate
-      //for(WORD i=0; i<0x8000; i++)  gram2[i] = grom3[i]; //Temp
+      //for(uint16 i=0; i<0x8000; i++)  gram2[i] = grom3[i]; //Temp
 
                        if (lbuff[0] == 'r')                                            // If run, then reset CPUs
                        {
@@ -1291,7 +1237,7 @@ WriteLog("Executing secondary 'run' command...\n");
         else  cpu1.cc = 0xFF;                         // Set CC register
 
         cpu2.pc = ((grom2[0xFFFE]<<8) | grom2[0xFFFF]); // Reset 6809 #2
-        cpu2.cc = 0xFF;                              // Set CC register 
+        cpu2.cc = 0xFF;                              // Set CC register
         while(iclock < 8000)  // was 17000, 20000, 5000
         {
           Execute6809(&cpu1, 1);  Execute6809(&cpu2, 1);
@@ -1322,13 +1268,29 @@ WriteLog("About to set up screen...\n");
                                colors[i].g = palette[i*3+1];
                                colors[i].b = palette[i*3+2];
                        }
-                       SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256);
+                       SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL, colors, 0, 256);
+
+#if 1
+       // This confirms that we're getting video to the screen...
+       SDL_LockSurface(screen);
+
+       uint8 pixel = 0;
+       uint8 * pixels = (uint8 *)(screen->pixels);
+
+       for(uint32 y=0; y<480; y++)
+               for(uint32 x=0; x<640; x++)
+                       pixels[(y * 640) + x] = pixel++;
+
+       SDL_UnlockSurface(screen);
+       SDL_UpdateRect(screen, 0, 0, 0, 0);
+#endif
 
                        for(int i=0; i<256; i++)
                                keys[i] = 0;                            // Clear keyboard buffer...
 
                        oldTicks = SDL_GetTicks();
 
+WriteLog("About to set up audio...\n");
                        // This crap SHOULD be in sound.cpp (not yet created)...
                        SDL_AudioSpec desired, obtained;
                        desired.freq = 22050;
@@ -1345,6 +1307,7 @@ WriteLog("About to set up screen...\n");
                        }
                        SDL_PauseAudio(0);                                                      // Get that audio going!
 
+WriteLog("About to enter main loop...\n");
                        while (running)
                        {
                                HandleGUIDebounce();                                    // Debounce GUI keys
@@ -1367,49 +1330,41 @@ WriteLog("About to set up screen...\n");
 
                                // SDL key handling...
 
-//                             keyPressed = 0;                                                 // Reset keypress
-                               while (SDL_PollEvent(&event) != 0)              // Bleed out all pending key events...
-                               {
-                                       if (event.type == SDL_KEYDOWN)
-                                               keys[event.key.keysym.scancode] = 1;
-                                       if (event.type == SDL_KEYUP)
-                                               keys[event.key.keysym.scancode] = 0;
-                               }
+                               SDL_PumpEvents();                               // Force key events into the buffer.
 
-//        {
-                               if (keys[0x01])
+                               if (keys[SDLK_ESCAPE])
                                        running = false;                     // ESC to exit...
 
                                if (debounce)
                                        debounce--;                          // Debounce toggle keys...
                                else
                                {
-                                       if (keys[0x3B])
+                                       if (keys[SDLK_F1])
                                        {
                                                self_test = !self_test;            // Self-test (F1-toggle)
                                                debounce = 10;                     // Key debounce value...
                                        }
-                                       if (keys[0x3C])
+                                       if (keys[SDLK_F2])
                                        {
                                                gram1[0x4268] = 1;                 // Video test (F2)
                                                debounce = 10;                     // Key debounce value...
                                        }
-                                       if (keys[0x58])
+                                       if (keys[SDLK_F12])
                                        {
                                                scr_type = !scr_type;              // Toggle screen (F12)
                                                debounce = 10;                     // Key debounce value...
                                        }
-                                       if (keys[0x3D])
+                                       if (keys[SDLK_F3])
                                        {
                                                show_scr = !show_scr;              // Toggle bkgrnd (F3)
                                                debounce = 10;
                                        }
-                                       if (keys[0x40])
+                                       if (keys[SDLK_F6])
                                        {
                                                enable_cpu = !enable_cpu;          // Toggle CPUs (F6)
                                                debounce = 10;
                                        }
-                                       if (keys[0x3F])
+                                       if (keys[SDLK_F5])
                                        {
                                                refresh2 = !refresh2;             // Toggle 30/60Hz (F5)
                                                SetRefreshRate(refresh2);         // Inform GUI of refresh
@@ -1419,13 +1374,13 @@ WriteLog("About to set up screen...\n");
                                                        SpawnMsg(M30FPS);
                                                debounce = 10;                    // Key debounce value...
                                        }
-                                       if (keys[0x3E])                      // Do PCX snapshot (F4)
+                                       if (keys[SDLK_F4])                      // Do PCX snapshot (F4)
                                        {
-                                               SpawnSound(USERSOUND, SCAMERA);     
+                                               SpawnSound(USERSOUND, SCAMERA);
                                                SnapPCX(screen);
                                                debounce = 10;
                                        }
-                                       if (keys[0x0F])                      // Tab active/deactivate GUI
+                                       if (keys[SDLK_TAB])                      // Tab active/deactivate GUI
                                        {
                                                if (ShowGUI())
                                                        DeactivateGUI();
@@ -1435,49 +1390,49 @@ WriteLog("About to set up screen...\n");
                                        }
                                }
                                //if (keys[0x3E])  gram1[0x4247] = 1;  // Screen hold DS (F4)
-                               if (keys[77+128])                                               // Right arrow
+                               if (keys[SDLK_RIGHT])                                           // Right arrow
                                {
                                        if (ShowGUI())
                                                SelectRight();                     // If GUI active...
                                        else
                                        {
-                                               if (!keys[75+128])                     // Disallow opposite directions @ same time
+                                               if (!keys[SDLK_LEFT])                     // Disallow opposite directions @ same time
                                                        gram1[0x427F] = 1;               // Stick right
                                        }
                                }
-                               if (keys[75+128])
+                               if (keys[SDLK_LEFT])
                                {
                                        if (ShowGUI())
                                                SelectLeft();                      // If GUI active...
                                        else
                                        {
-                                               if (!keys[77+128])                     // Disallow opposite directions@same time
+                                               if (!keys[SDLK_RIGHT])                     // Disallow opposite directions@same time
                                                gram1[0x4281] = 1;               // Left arrow
                                        }
                                }
-                               if (keys[72+128])
+                               if (keys[SDLK_UP])
                                {
                                        if (ShowGUI())
                                                SelectUp();                        // If GUI active...
                                        else
                                        {
-                                               if (!keys[80+128])                     // Disallow opposite directions@same time
+                                               if (!keys[SDLK_DOWN])                     // Disallow opposite directions@same time
                                                        gram1[0x427B] = 1;               // Up arrow
                                        }
                                }
-                               if (keys[80+128])
+                               if (keys[SDLK_DOWN])
                                {
                                        if (ShowGUI())
                                                SelectDown();                                   // If GUI active...
                                        else
                                        {
-                                               if (!keys[72+128])                              // Disallow opposite directions@same time
+                                               if (!keys[SDLK_UP])                             // Disallow opposite directions@same time
                                                        gram1[0x427D] = 1;                      // Down arrow
                                        }
                                }
-                               if (keys[28])                                                   // Return
+                               if (keys[SDLK_RETURN])                                                  // Return
                                {
-                                       BYTE retval = UserSelectedSomething();
+                                       uint8 retval = UserSelectedSomething();
                                        if (retval == EXIT)
                                                running = false;
                                        if (retval == REFRESH)
@@ -1486,68 +1441,68 @@ WriteLog("About to set up screen...\n");
                                                SetRefreshRate(refresh2);
                                        }
                                }
-                               if (keys[0x02])
-                                       gram1[0x427A] = 1;                                      // (1)
-                               if (keys[0x03])
-                                       gram1[0x427C] = 1;                                      // (2)
-                               if (keys[0x04])
-                                       gram1[0x427E] = 1;                                      // (3)
-                               if (keys[0x06])
-                                       gram1[0x4280] = 1;                                      // (5)
-                               if (keys[0x10]|keys[29])
-                                       gram1[0x4276] = 1;                                      // (Q)  Jump
-                               if (keys[0x11])
-                                       gram1[0x426A] = 1;                                      // (W)
+                               if (keys[SDLK_1])
+                                       gram1[0x427A] = 1;                      // (1)
+                               if (keys[SDLK_2])
+                                       gram1[0x427C] = 1;                      // (2)
+                               if (keys[SDLK_3])
+                                       gram1[0x427E] = 1;                      // (3)
+                               if (keys[SDLK_5])
+                                       gram1[0x4280] = 1;                      // (5)
+                               if (keys[SDLK_q] | keys[29])
+                                       gram1[0x4276] = 1;                      // (Q)  Jump
+                               if (keys[SDLK_w])
+                                       gram1[0x426A] = 1;                      // (W)
                                if (fire_debounce)
                                        fire_debounce--;
-                               if (keys[0x12]|keys[56])                                // (E) Fire
+                               if (keys[SDLK_e] | keys[56])    // (E) Fire
                                {
                                        if (!fire_debounce)
                                        {
                                                gram1[0x4278] = 1;
+
                                                if (gram1[0x3F08] == 0xFF)              // Ugly kludge for debouncing gun
-                                               {
                                                        fire_debounce = 8;
-                                               }
                                                else
-                                               {
                                                        fire_debounce = 2;
-                                               }
                                        }
                                }
-                               if (keys[0x13])
-                                       gram1[0x426C] = 1;                                      // (R)
-                               if (keys[0x14])
-                                       gram1[0x4262] = 1;                                      // (T)
-                               if (keys[0x15])
-                                       gram1[0x4260] = 1;                                      // (Y)
-                               if (keys[0x44])
-                                       gram1[0x41A5]++;                                        // Coin? (F10)
-                               if (keys[0x2C])
-                                       gram1[0x4189]++;                                        // ? (Z) credits l dig
-                               if (keys[0x2D])
-                                       gram1[0x418A]++;                                        // ? (X) credits r dig
-                               if (keys[0x2E])
-                                       gram1[0x418C]++;                                        // ? (C) Start
-                               if (keys[0x2F])
-                                       gram1[0x418D]++;                                        // ? (V)
-                               if (keys[0x41])
-                                       SpawnSound(USERSOUND, 0);                       // Do user sound (F7)
-//                             if (keys[0x42])
+                               if (keys[SDLK_r])
+                                       gram1[0x426C] = 1;                      // (R)
+                               if (keys[SDLK_t])
+                                       gram1[0x4262] = 1;                      // (T)
+                               if (keys[SDLK_y])
+                                       gram1[0x4260] = 1;                      // (Y)
+                               if (keys[SDLK_F10])
+                                       gram1[0x41A5]++;                        // Coin? (F10)
+                               if (keys[SDLK_z])
+                                       gram1[0x4189]++;                        // ? (Z) credits l dig
+                               if (keys[SDLK_x])
+                                       gram1[0x418A]++;                        // ? (X) credits r dig
+                               if (keys[SDLK_c])
+                                       gram1[0x418C]++;                        // ? (C) Start
+                               if (keys[SDLK_v])
+                                       gram1[0x418D]++;                        // ? (V)
+                               if (keys[SDLK_F7])
+                                       SpawnSound(USERSOUND, 0);       // Do user sound (F7)
+//                             if (keys[SDLK_F8])
 //                             {
-//                                     gram1[0x4380] = 0;                                      // (F8) kill music (this worx)
-//                                     charbase = false;                                       // Switch chars out...
+//                                     gram1[0x4380] = 0;                      // (F8) kill music (this worx)
+//                                     charbase = false;                       // Switch chars out...
 //                             }
-//                             if (keys[0x43])  gram1[0x4285] = 1;             // (F9) strobe unknown loc
-                               if (keys[0x45])                                                 // (F11)
+//                             if (keys[SDLK_F9])  gram1[0x4285] = 1;          // (F9) strobe unknown loc
+                               if (keys[SDLK_F11])                             // (F11)
                                {
                                        Execute6809(&cpu1, 10);
                                        Execute6809(&cpu2, 10);
                                }
 //                     }
-                               if (enable_cpu)
+
+//                             if (enable_cpu)
+                               if (true)
                                {
-/*//                                   if (irqGoA)
+#if 0
+//                                     if (irqGoA)
                                                cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
 
                                        Execute6809(&cpu1, 25000);
@@ -1558,126 +1513,41 @@ WriteLog("About to set up screen...\n");
 
                                        Execute6809(&cpu2, 25000);
                                        cpu2.clock -= 25000;                            // Remove 25K ticks from clock (in case it overflowed)//*/
-
+#else
                                        cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
                                        cpu2.cpuFlags |= V6809_ASSERT_LINE_IRQ;
-                                       while (cpu1.clock < 25000)
+//                                     while (cpu1.clock < 25000)
+                                       for(uint32 i=0; i<250; i++)
                                        {
                                                // Gay, but what are ya gonna do?
-                                               Execute6809(&cpu1, 5);
-                                               Execute6809(&cpu2, 5);
+                                               // There's better ways, such as keeping track of when slave writes to master, etc...
+                                               Execute6809(&cpu1, 100);
+                                               Execute6809(&cpu2, 100);
                                        }
+
                                        cpu1.clock -= 25000;                            // Remove 25K ticks from clock (in case it overflowed)
                                        cpu2.clock -= 25000;                            // Remove 25K ticks from clock (in case it overflowed)//*/
-
-/*                                     while (my_clock < 25000)   // Cycles in 60th of a sec at 1.5 Mhz
-                                       {
-                                               // pcx = cpu1.pc;  // Tracelog stuff
-                                               if (brk && (cpu1.pc == brkpnt))
-                                               {
-                                                       running = false;
-                                                       break;
-                                               }
-                                               if (brk2 && (cpu2.pc == brkpnt2))
-                                               {
-                                                       running = false;
-                                                       break;
-                                               }
-                                               DWORD prev = iclock;
-
-                                               BYTE opcode = Fetch();                                 // Get the opcode
-                                               if (opcode == 0x10)
-                                               {
-                                                       exec_op1[Fetch()]();
-                                                       goto TEXE_NEXT;
-                                               }
-                                               if (opcode == 0x11)
-                                               {
-                                                       exec_op2[Fetch()]();
-                                                       goto TEXE_NEXT;
-                                               }
-                                               exec_op0[opcode]();
-TEXE_NEXT:
-
-                                               if (iclock > 25000)
-                                               {
-                                                       iclock = 0;
-//          if (irqGoA)
-//          {
-                                                       if ((!(cpu1.cc&0x10)) && irqGoA)
-                                                       {
-                                                               irqGoA = false;
-//            if (!(cpu1.cc&0x10))                // Process an interrupt?
-//            {
-                                                               cpu1.cc |= 0x80;                  // Set E
-                                                               WrMem(--cpu1.s, cpu1.pc&0xFF);  WrMem(--cpu1.s, cpu1.pc>>8);  // Save all regs...
-                                                               WrMem(--cpu1.s, cpu1.u&0xFF);   WrMem(--cpu1.s, cpu1.u>>8);
-                                                               WrMem(--cpu1.s, cpu1.y&0xFF);   WrMem(--cpu1.s, cpu1.y>>8);
-                                                               WrMem(--cpu1.s, cpu1.x&0xFF);   WrMem(--cpu1.s, cpu1.x>>8);
-                                                               WrMem(--cpu1.s, cpu1.dp);       WrMem(--cpu1.s, cpu1.b);
-                                                               WrMem(--cpu1.s, cpu1.a);        WrMem(--cpu1.s, cpu1.cc);
-                                                               cpu1.cc |= 0x50;         // Set F,I
-                                                               cpu1.pc = (grom1[0xFFF8]<<8) | grom1[0xFFF9];  // And do it!
-                                                               // Need to find out why RdMem is returning wrong values here...
-                                                       }
-                                               }
-//        }//irgGoA
-                                               if (iclock > prev)
-                                               my_clock += (iclock - prev); // Increment my_clock
-
-                                               // pcx = cpu2.pc;  // Tracelog stuff
-                                               opcode = FetchB();                                 // Get the opcode
-                                               if (opcode == 0x10)
-                                               {
-                                                       exec_op1B[FetchB()]();
-                                                       goto TEXE_NEXT2;
-                                               }
-                                               if (opcode == 0x11)
-                                               {
-                                                       exec_op2B[FetchB()]();
-                                                       goto TEXE_NEXT2;
-                                               }
-                                               exec_op0B[opcode]();
-TEXE_NEXT2:
-         
-                                               if (iclockB > 25000)//24550)  // Slightly faster IRQs for SUB processor 
-                                               {
-                                                       iclockB = 0;
-//          if (irqGoB)
-//          {
-                                                       if ((!(cpu2.cc&0x10)) && irqGoB)
-                                                       {
-                                                               irqGoB = false;
-//            if (!(cpu2.cc&0x10))               // Process an interrupt?
-//            {
-                                                               cpu2.cc |= 0x80;                   // Set E
-                                                               WrMemB(--cpu2.s, cpu2.pc&0xFF);  WrMemB(--cpu2.s, cpu2.pc>>8);  // Save all regs...
-                                                               WrMemB(--cpu2.s, cpu2.u&0xFF);   WrMemB(--cpu2.s, cpu2.u>>8);
-                                                               WrMemB(--cpu2.s, cpu2.y&0xFF);   WrMemB(--cpu2.s, cpu2.y>>8);
-                                                               WrMemB(--cpu2.s, cpu2.x&0xFF);   WrMemB(--cpu2.s, cpu2.x>>8);
-                                                               WrMemB(--cpu2.s, cpu2.dp);       WrMemB(--cpu2.s, cpu2.b);
-                                                               WrMemB(--cpu2.s, cpu2.a);        WrMemB(--cpu2.s, cpu2.cc);
-                                                               cpu2.cc |= 0x50;                   // Set F,I
-                                                               cpu2.pc = (grom2[0xFFF8]<<8) | grom2[0xFFF9];  // And do it!
-                                                       }
-                                               }
-//        } // irqGoB
-        // if (brk && (cpu1.pc == brkpnt))  running = false;  // Stop running!
-                                       }
-                                       my_clock -= 25000;                 // Remove frame count from iclock*/
+#endif
                                } // END: enable_cpu
-//        if (refresh++ == 1)                // 30 Hz...
+
+//        if (refresh_++ == 1)                // 30 Hz...
 //        {
 //          if (scr_type)
 //            BlitWilliamsScreen(gram1);     // Display the screen...
 //          else
 //            BlitChar(screen, chr_rom, gram1);
-//          refresh = (refresh2 ? 1 : 0);    // 60/30 Hz...
+//          refresh_ = (refresh2 ? 1 : 0);    // 60/30 Hz...
 //        }
 
+//temp, for testing...
+BlitChar(screen, chr_rom, gram1);
+
                                // Speed throttling happens here...
-                               while (SDL_GetTicks() - oldTicks < 16);  // Actually, it's 16.66... Need to account for that somehow
+                               while (SDL_GetTicks() - oldTicks < 16)  // Actually, it's 16.66... Need to account for that somehow
+                                       SDL_Delay(1);                           // Release our timeslice...
+
                                oldTicks = SDL_GetTicks();
+//cout << "Finished frame..." << endl;
                        }
 
 //      Stop_audio_output();
@@ -1758,11 +1628,11 @@ TEXE_NEXT2:
 
       printf("%04X: ", dpc);       // Show data found...
       for(int i=0; i<16; i++)
-        printf("%02X ", gram1[(WORD)(dpc+i)]);
+        printf("%02X ", gram1[(uint16)(dpc+i)]);
       cout << " ";
       for(int i=0; i<16; i++)
       {
-        BYTE a = gram1[dpc++];
+        uint8 a = gram1[dpc++];
         if (a<10)
           cout << (char)(a+48);
         if ((a>9) && (a<37))
@@ -1784,8 +1654,9 @@ TEXE_NEXT2:
                        active = false; //break;  // Quit
        }
 
-       SDL_Quit();                                                                                     // Shut down SDL
+       SDL_Quit();                                                                     // Shut down SDL
 
+#if 0
        delete[] gram1;                                                                         // Deallocate RAM & ROM spaces
        delete[] grom1;
        delete[] gram2;
@@ -1796,16 +1667,16 @@ TEXE_NEXT2:
        delete[] data_rom;
        delete[] spr_rom;
        delete[] voice_rom;
+#endif
 
        for(int i=0; i<16; i++)
-               if (psg_adrs[i] != NULL)
-                       delete[] psg_adrs[i];                                           // Deallocate if loaded
+               if (psg_adrs[i])
+                       delete[] psg_adrs[i];                           // Deallocate if loaded
 
        for(int i=0; i<14; i++)
-               if (fm_adrs[i] != NULL)
-                       delete[] fm_adrs[i];                                            // Deallocate if loaded
+               if (fm_adrs[i])
+                       delete[] fm_adrs[i];                            // Deallocate if loaded
 
-//     tr.close();                                                                                     // Close tracelog
        LogDone();
 
        return 1;