]> Shamusworld >> Repos - thunder/blobdiff - src/thunder.cpp
Changed main emu code to use preallocated arrays
[thunder] / src / thunder.cpp
index fde0f3ef48289d66db1b384421d113357cab70ac..3a9d8ae3ae84a67965ffd918b0552425e3c32164 100755 (executable)
@@ -95,10 +95,22 @@ using namespace std;                                                                        // Yes!
 
 SDL_Surface * screen;
 
-uint8 * gram, * grom;                // Allocate RAM & ROM pointers
-uint8 * gram1, * gram2, * grom1, * grom2;  // Actual memory
-uint8 * grom3, * grom4, * data_rom, * spr_rom, * voice_rom;
-uint8 * 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;
 
@@ -126,6 +138,8 @@ uint8 * fm_adrs[14];
 fstream tr;    // Tracelog hook
 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,
   0, 0, 5, 5, 0, 0, 4, 4, 0, 5, 8, 0, 8, 5, 6, 6,
@@ -925,23 +939,8 @@ int main(int argc, char * argv[])
 {
        InitLog("thunder.log");
 
-/*  extern uint16 cpu1.pc, cpu1.s, cpu1.u, cpu1.x, cpu1.y;            // Pull in vars from '6809.cpp'
-  extern uint8 cpu1.a, cpu1.b, cpu1.cc, cpu1.dp;
-  extern long iclock;
-  extern bool illegal;
-  extern uint16 cpu2.pc, cpu2.s, cpu2.u, cpu2.x, cpu2.y;       // Pull in vars from '6809B.cpp'
-  extern uint8 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...
@@ -954,35 +953,33 @@ int main(int argc, char * argv[])
   uint16 fire_debounce = 0;           // Fire button debounce counter
 //  bool refresh2 = true;             // Default to 60 Hz...
   uint8 x;                           // General placeholder...
-  bool active = true;               // Program running flag
+  bool active = true;                                          // Program running flag
 
-//  SDL_Surface * screen = NULL;      // SDL screen pointer
-  SDL_Event event;                  // SDL "event"
-//  int keyPressed;                   // SDL key pressed...
-  uint8 keys[256];                   // Keyboard "switch-like" buffer
-  extern uint8 palette[768];         // Screen physical palette
+  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 v"THUNDER_VERSION" ";
-  cout << "by James Hammmmons" << endl;
-  cout << "Serial #20090723 / Prerelease" << endl;
-  cout << "(C) 2003, 2009 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 free software. 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 v"THUNDER_VERSION" ", "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 uint8[0x10000];
@@ -1015,6 +1012,7 @@ int main(int argc, char * argv[])
   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
 
@@ -1332,52 +1330,41 @@ WriteLog("About to enter main loop...\n");
 
                                // SDL key handling...
 
-#warning "MAKE REPOSITORY FOR THIS THING! !!! FIX !!!"
-#warning "AND USE THE OLD SOURCE FOR THE BASE!!!"
-#warning "KEYS ARE FUCKED UP! !!! FIX !!!"
-//                             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
@@ -1387,13 +1374,13 @@ WriteLog("About to enter main loop...\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);
                                                SnapPCX(screen);
                                                debounce = 10;
                                        }
-                                       if (keys[0x0F])                      // Tab active/deactivate GUI
+                                       if (keys[SDLK_TAB])                      // Tab active/deactivate GUI
                                        {
                                                if (ShowGUI())
                                                        DeactivateGUI();
@@ -1403,47 +1390,47 @@ WriteLog("About to enter main loop...\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
                                {
                                        uint8 retval = UserSelectedSomething();
                                        if (retval == EXIT)
@@ -1454,60 +1441,57 @@ WriteLog("About to enter main loop...\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);
@@ -1517,7 +1501,8 @@ WriteLog("About to enter main loop...\n");
 //                             if (enable_cpu)
                                if (true)
                                {
-/*//                                   if (irqGoA)
+#if 0
+//                                     if (irqGoA)
                                                cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
 
                                        Execute6809(&cpu1, 25000);
@@ -1528,19 +1513,21 @@ WriteLog("About to enter main loop...\n");
 
                                        Execute6809(&cpu2, 25000);
                                        cpu2.clock -= 25000;                            // Remove 25K ticks from clock (in case it overflowed)//*/
-
-//                                     cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
-//                                     cpu2.cpuFlags |= V6809_ASSERT_LINE_IRQ;
-                                       while (cpu1.clock < 25000)
+#else
+                                       cpu1.cpuFlags |= V6809_ASSERT_LINE_IRQ;
+                                       cpu2.cpuFlags |= V6809_ASSERT_LINE_IRQ;
+//                                     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)//*/
-
+#endif
                                } // END: enable_cpu
 
 //        if (refresh_++ == 1)                // 30 Hz...
@@ -1551,6 +1538,7 @@ WriteLog("About to enter main loop...\n");
 //            BlitChar(screen, chr_rom, gram1);
 //          refresh_ = (refresh2 ? 1 : 0);    // 60/30 Hz...
 //        }
+
 //temp, for testing...
 BlitChar(screen, chr_rom, gram1);
 
@@ -1666,8 +1654,9 @@ BlitChar(screen, chr_rom, gram1);
                        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;
@@ -1678,16 +1667,16 @@ BlitChar(screen, chr_rom, gram1);
        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;