2 // Apple 2 SDL Portable Apple Emulator
5 // (C) 2005 Underground Software
7 // Loosely based on AppleWin by Tom Charlesworth which was based on AppleWin by
8 // Oliver Schmidt which was based on AppleWin by Michael O'Brien. :-) Parts are
9 // also derived from ApplePC. Too bad it was closed source--it could have been
10 // *the* premier Apple II emulator out there.
12 // JLH = James Hammons <jlhamm@acm.org>
15 // --- ---------- ------------------------------------------------------------
16 // JLH 11/12/2005 Initial port to SDL
17 // JLH 11/18/2005 Wired up graphic soft switches
18 // JLH 12/02/2005 Setup timer subsystem for more accurate time keeping
19 // JLH 12/12/2005 Added preliminary state saving support
20 // JLH 09/24/2013 Added //e support
25 // - Port to SDL [DONE]
27 // - Weed out unneeded functions [DONE]
29 // - 128K IIe related stuff [DONE]
30 // - State loading/saving
48 #include "applevideo.h"
55 #include "gui/window.h"
56 #include "gui/draggablewindow2.h"
57 #include "gui/textedit.h"
59 // Debug and misc. defines
61 #define THREADED_65C02
62 #define CPU_THREAD_OVERFLOW_COMPENSATION
64 //#define CPU_CLOCK_CHECKING
65 //#define THREAD_DEBUGGING
66 #define SOFT_SWITCH_DEBUGGING
70 uint8_t ram[0x10000], rom[0x10000]; // RAM & ROM spaces
71 uint8_t ram2[0x10000]; // Auxillary RAM
72 //uint8_t diskRom[0x100]; // Disk ROM space
73 V65C02REGS mainCPU; // v65C02 execution context
74 uint8_t appleType = APPLE_TYPE_IIE;
75 FloppyDrive floppyDrive;
79 uint8_t lastKeyPressed = 0;
81 bool openAppleDown = false;
82 bool closedAppleDown = false;
83 bool store80Mode = false;
85 bool slotCXROM = false;
86 bool slotC3ROM = false;
93 static bool running = true; // Machine running state flag...
94 static uint32_t startTicks;
95 static bool pauseMode = false;
96 static bool fullscreenDebounce = false;
97 static bool capsLock = false;
98 static bool capsLockDebounce = false;
99 //static GUI * gui = NULL;
101 // Local functions (technically, they're global...)
103 bool LoadImg(char * filename, uint8_t * ram, int size);
104 uint8_t RdMem(uint16_t addr);
105 void WrMem(uint16_t addr, uint8_t b);
106 static void SaveApple2State(const char * filename);
107 static bool LoadApple2State(const char * filename);
109 // Local timer callback functions
111 static void FrameCallback(void);
112 static void BlinkTimer(void);
114 #ifdef THREADED_65C02
115 // Test of threaded execution of 6502
116 static SDL_Thread * cpuThread = NULL;
117 //static SDL_mutex * cpuMutex = NULL;
118 static SDL_cond * cpuCond = NULL;
119 static SDL_sem * mainSem = NULL;
120 static bool cpuFinished = false;
121 static bool cpuSleep = false;
123 // NB: Apple //e Manual sez 6502 is running @ 1,022,727 Hz
125 // Let's try a thread...
127 Here's how it works: Execute 1 frame's worth, then sleep.
128 Other stuff wakes it up
130 int CPUThreadFunc(void * data)
132 // Mutex must be locked for conditional to work...
133 // Also, must be created in the thread that uses it...
134 SDL_mutex * cpuMutex = SDL_CreateMutex();
136 // decrement mainSem...
137 //SDL_SemWait(mainSem);
138 #ifdef CPU_THREAD_OVERFLOW_COMPENSATION
139 float overflow = 0.0;
145 SDL_CondWait(cpuCond, cpuMutex);
147 // decrement mainSem...
148 #ifdef THREAD_DEBUGGING
149 WriteLog("CPU: SDL_SemWait(mainSem);\n");
151 SDL_SemWait(mainSem);
153 // There are exactly 800 slices of 21.333 cycles per frame, so it works out
156 uint32_t cycles = 17066;
157 #ifdef CPU_THREAD_OVERFLOW_COMPENSATION
158 // ODD! It's closer *without* this overflow compensation. ??? WHY ???
159 overflow += 0.666666667;
168 #ifdef THREAD_DEBUGGING
169 WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
171 Execute65C02(&mainCPU, cycles); // how much? 1 frame (after 1 s, off by 40 cycles) not any more--it's off by as much as 240 now!
173 // Adjust the sound routine's last cycle toggled time base
174 // Also, since we're finished executing, .clock is now valid
175 #ifdef THREAD_DEBUGGING
176 WriteLog("CPU: AdjustLastToggleCycles(mainCPU.clock);\n");
178 AdjustLastToggleCycles(mainCPU.clock);
180 #ifdef THREAD_DEBUGGING
181 WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
183 for(int i=0; i<800; i++)
185 uint32_t cycles = 21;
186 overflow += 0.333333334;
194 Execute65C02(&mainCPU, cycles);
195 WriteSampleToBuffer();
197 // Dunno if this is correct (seems to be close enough)...
198 vbl = (i < 670 ? true : false);
202 #ifdef THREAD_DEBUGGING
203 WriteLog("CPU: SDL_mutexP(cpuMutex);\n");
205 SDL_mutexP(cpuMutex);
206 // increment mainSem...
207 #ifdef THREAD_DEBUGGING
208 WriteLog("CPU: SDL_SemPost(mainSem);\n");
210 SDL_SemPost(mainSem);
211 #ifdef THREAD_DEBUGGING
212 WriteLog("CPU: SDL_CondWait(cpuCond, cpuMutex);\n");
214 SDL_CondWait(cpuCond, cpuMutex);
216 #ifdef THREAD_DEBUGGING
217 WriteLog("CPU: SDL_mutexV(cpuMutex);\n");
219 SDL_mutexV(cpuMutex);
221 while (!cpuFinished);
223 SDL_DestroyMutex(cpuMutex);
233 Element * TestWindow(void)
235 Element * win = new DraggableWindow2(10, 10, 128, 128);
236 // ((DraggableWindow *)win)->AddElement(new TextEdit(4, 16, 92, 0, "u2prog.dsk", win));
242 Element * QuitEmulator(void)
253 // Load a file into RAM/ROM image space
255 bool LoadImg(char * filename, uint8_t * ram, int size)
257 FILE * fp = fopen(filename, "rb");
262 fread(ram, 1, size, fp);
269 static void SaveApple2State(const char * filename)
274 static bool LoadApple2State(const char * filename)
280 #ifdef CPU_CLOCK_CHECKING
282 uint32_t totalCPU = 0;
283 uint64_t lastClock = 0;
288 int main(int /*argc*/, char * /*argv*/[])
290 InitLog("./apple2.log");
292 srand(time(NULL)); // Initialize RNG
295 memset(ram, 0, 0x10000);
296 memset(rom, 0, 0x10000);
297 memset(ram2, 0, 0x10000);
303 // Set up V65C02 execution context
304 memset(&mainCPU, 0, sizeof(V65C02REGS));
305 mainCPU.RdMem = AppleReadMem;
306 mainCPU.WrMem = AppleWriteMem;
308 mainCPU.RdMem = RdMem;
309 mainCPU.WrMem = WrMem;
311 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
313 // alternateCharset = true;
314 // if (!LoadImg(settings.BIOSPath, rom + 0xD000, 0x3000))
315 if (!LoadImg(settings.BIOSPath, rom + 0xC000, 0x4000))
317 WriteLog("Could not open file '%s'!\n", settings.BIOSPath);
321 //Load up disk image from config file (for now)...
322 floppyDrive.LoadImage(settings.diskImagePath1, 0);
323 floppyDrive.LoadImage(settings.diskImagePath2, 1);
324 // floppyDrive.LoadImage("./disks/temp.nib", 1); // Load temp .nib file into second drive...
326 WriteLog("About to initialize video...\n");
330 std::cout << "Aborting!" << std::endl;
334 GUI2::Init(sdlRenderer);
336 // Have to do this *after* video init but *before* sound init...!
337 //Shouldn't be necessary since we're not doing emulation in the ISR...
338 if (settings.autoStateSaving)
340 // Load last state from file...
341 if (!LoadApple2State(settings.autoStatePath))
342 WriteLog("Unable to use Apple2 state file \"%s\"!\n", settings.autoStatePath);
348 if (!LoadImg("./BT1_6502_RAM_SPACE.bin", ram, 0x10000))
350 cout << "Couldn't load state file!" << endl;
351 cout << "Aborting!!" << endl;
356 //-- -- -- -- ----- -----
357 //00 75 3B 53 FD 01 41 44
359 mainCPU.cpuFlags = 0;
369 displayPage2 = false;
376 memcpy(ram + 0xD000, ram + 0xC000, 0x1000);
379 WriteLog("About to initialize audio...\n");
381 //nope SDL_EnableUNICODE(1); // Needed to do key translation shit
383 // gui = new GUI(surface); // Set up the GUI system object...
384 // gui = new GUI(mainSurface); // Set up the GUI system object...
385 // SDL 2... this will likely cause Apple 2 to crash
386 // gui = new GUI(NULL); // Set up the GUI system object...
388 gui->AddMenuTitle("Apple2");
389 gui->AddMenuItem("Test!", TestWindow/*, hotkey*/);
390 gui->AddMenuItem("");
391 gui->AddMenuItem("Quit", QuitEmulator, SDLK_q);
392 gui->CommitItemsToMenu();
395 SetupBlurTable(); // Set up the color TV emulation blur table
396 running = true; // Set running status...
398 InitializeEventList(); // Clear the event list before we use it...
399 SetCallbackTime(FrameCallback, 16666.66666667); // Set frame to fire at 1/60 s interval
400 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 s intervals
401 startTicks = SDL_GetTicks();
403 #ifdef THREADED_65C02
404 cpuCond = SDL_CreateCond();
405 mainSem = SDL_CreateSemaphore(1);
406 cpuThread = SDL_CreateThread(CPUThreadFunc, NULL, NULL);
407 //Hmm... CPU does POST (+1), wait, then WAIT (-1)
408 // SDL_sem * mainMutex = SDL_CreateMutex();
411 WriteLog("Entering main loop...\n");
415 double timeToNextEvent = GetTimeToNextEvent();
416 #ifndef THREADED_65C02
417 Execute65C02(&mainCPU, USEC_TO_M6502_CYCLES(timeToNextEvent));
419 //We MUST remove a frame's worth of time in order for the CPU to function... !!! FIX !!!
420 //(Fix so that this is not a requirement!)
421 //Fixed, but mainCPU.clock is destroyed in the bargain. Oh well.
422 // mainCPU.clock -= USEC_TO_M6502_CYCLES(timeToNextEvent);
424 #ifdef CPU_CLOCK_CHECKING
425 #ifndef THREADED_65C02
426 totalCPU += USEC_TO_M6502_CYCLES(timeToNextEvent);
432 #ifdef THREADED_65C02
433 WriteLog("Main: cpuFinished = true;\n");
435 //#warning "If sound thread is behind, CPU thread will never wake up... !!! FIX !!!" [DONE]
436 //What to do? How do you know when the CPU is sleeping???
437 //USE A CONDITIONAL!!! OF COURSE!!!!!!11!11!11!!!1!
439 SDL_mutexP(mainMutex);
440 SDL_CondWait(mainCond, mainMutex); // Wait for CPU thread to get to signal point...
441 SDL_mutexV(mainMutex);
443 //Nope, use a semaphore...
444 WriteLog("Main: SDL_SemWait(mainSem);\n");
445 SDL_SemWait(mainSem);//should lock until CPU thread is waiting...
448 WriteLog("Main: SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up\n");
449 SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up
450 WriteLog("Main: SDL_WaitThread(cpuThread, NULL);\n");
451 SDL_WaitThread(cpuThread, NULL);
452 //nowok:SDL_WaitThread(CPUThreadFunc, NULL);
453 WriteLog("Main: SDL_DestroyCond(cpuCond);\n");
454 SDL_DestroyCond(cpuCond);
456 //SDL_DestroyMutex(mainMutex);
457 SDL_DestroySemaphore(mainSem);
460 if (settings.autoStateSaving)
462 // Save state here...
463 SaveApple2State(settings.autoStatePath);
465 floppyDrive.SaveImage(0);
466 floppyDrive.SaveImage(1);
482 -----------------------
483 space $A0 $A0 $A0 $A0 No xlation
484 RETURN $8D $8D $8D $8D No xlation
485 0 $B0 $B0 $B0 $B0 Need to screen shift+0 (?)
486 1! $B1 $B1 $A1 $A1 No xlation
487 2" $B2 $B2 $A2 $A2 No xlation
488 3# $B3 $B3 $A3 $A3 No xlation
489 4$ $B4 $B4 $A4 $A4 No xlation
490 5% $B5 $B5 $A5 $A5 No xlation
491 6& $B6 $B6 $A6 $A6 No xlation
492 7' $B7 $B7 $A7 $A7 No xlation
493 8( $B8 $B8 $A8 $A8 No xlation
494 9) $B9 $B9 $A9 $A9 No xlation
495 :* $BA $BA $AA $AA No xlation
496 ;+ $BB $BB $AB $AB No xlation
497 ,< $AC $AC $BC $BC No xlation
498 -= $AD $AD $BD $BD No xlation
499 .> $AE $AE $BE $BE No xlation
500 /? $AF $AF $BF $BF No xlation
513 M $CD $8D $DD $9D -> ODD
514 N^ $CE $8E $DE $9E -> ODD
516 P@ $D0 $90 $C0 $80 Need to xlate CTL+SHFT+P & SHFT+P (?)
529 ESC $9B $9B $9B $9B No xlation
532 //static uint64_t lastCPUCycles = 0;
533 static uint32_t frameCount = 0;
534 static void FrameCallback(void)
538 while (SDL_PollEvent(&event))
542 // Problem with using SDL_TEXTINPUT is that it causes key delay. :-/
545 //Need to do some key translation here, and screen out non-apple keys as well...
546 //(really, could do it all in SDL_KEYDOWN, would just have to get symbols &
547 // everything else done separately. this is slightly easier. :-P)
548 // if (event.key.keysym.sym == SDLK_TAB) // Prelim key screening...
549 if (event.edit.text[0] == '\t') // Prelim key screening...
552 lastKeyPressed = event.edit.text[0];
555 //kludge: should have a caps lock thingy here...
556 //or all uppercase for ][+...
557 // if (lastKeyPressed >= 'a' && lastKeyPressed <='z')
558 // lastKeyPressed &= 0xDF; // Convert to upper case...
563 // Use ALT+Q to exit, as well as the usual window decoration method
564 if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod & KMOD_ALT))
567 // CTRL+RESET key emulation (mapped to CTRL+`)
568 // This doesn't work...
569 // if (event.key.keysym.sym == SDLK_BREAK && (event.key.keysym.mod & KMOD_CTRL))
570 // if (event.key.keysym.sym == SDLK_PAUSE && (event.key.keysym.mod & KMOD_CTRL))
571 if (event.key.keysym.sym == SDLK_BACKQUOTE && (event.key.keysym.mod & KMOD_CTRL))
573 //NOTE that this shouldn't take place until the key is lifted... !!! FIX !!!
574 //ALSO it seems to leave the machine in an inconsistent state vis-a-vis the language card...
575 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
579 if (event.key.keysym.sym == SDLK_RIGHT)
580 lastKeyPressed = 0x15, keyDown = true;
581 else if (event.key.keysym.sym == SDLK_LEFT)
582 lastKeyPressed = 0x08, keyDown = true;
583 else if (event.key.keysym.sym == SDLK_UP)
584 lastKeyPressed = 0x0B, keyDown = true;
585 else if (event.key.keysym.sym == SDLK_DOWN)
586 lastKeyPressed = 0x0A, keyDown = true;
587 else if (event.key.keysym.sym == SDLK_RETURN)
588 lastKeyPressed = 0x0D, keyDown = true;
589 else if (event.key.keysym.sym == SDLK_ESCAPE)
590 lastKeyPressed = 0x1B, keyDown = true;
591 else if (event.key.keysym.sym == SDLK_BACKSPACE)
592 lastKeyPressed = 0x7F, keyDown = true;
594 // Fix CTRL+key combo...
595 if (event.key.keysym.mod & KMOD_CTRL)
597 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
599 lastKeyPressed = (event.key.keysym.sym - SDLK_a) + 1;
601 //printf("Key combo pressed: CTRL+%c\n", lastKeyPressed + 0x40);
607 // Fix SHIFT+key combo...
608 if (event.key.keysym.mod & KMOD_SHIFT)
610 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
612 lastKeyPressed = (event.key.keysym.sym - SDLK_a) + 0x41;
614 //printf("Key combo pressed: CTRL+%c\n", lastKeyPressed + 0x40);
617 else if (event.key.keysym.sym == SDLK_1)
619 lastKeyPressed = '!';
623 else if (event.key.keysym.sym == SDLK_2)
625 lastKeyPressed = '@';
629 else if (event.key.keysym.sym == SDLK_3)
631 lastKeyPressed = '#';
635 else if (event.key.keysym.sym == SDLK_3)
637 lastKeyPressed = '#';
641 else if (event.key.keysym.sym == SDLK_4)
643 lastKeyPressed = '$';
647 else if (event.key.keysym.sym == SDLK_5)
649 lastKeyPressed = '%';
653 else if (event.key.keysym.sym == SDLK_6)
655 lastKeyPressed = '^';
659 else if (event.key.keysym.sym == SDLK_7)
661 lastKeyPressed = '&';
665 else if (event.key.keysym.sym == SDLK_8)
667 lastKeyPressed = '*';
671 else if (event.key.keysym.sym == SDLK_9)
673 lastKeyPressed = '(';
677 else if (event.key.keysym.sym == SDLK_0)
679 lastKeyPressed = ')';
683 else if (event.key.keysym.sym == SDLK_MINUS)
685 lastKeyPressed = '_';
689 else if (event.key.keysym.sym == SDLK_EQUALS)
691 lastKeyPressed = '+';
695 else if (event.key.keysym.sym == SDLK_LEFTBRACKET)
697 lastKeyPressed = '{';
701 else if (event.key.keysym.sym == SDLK_RIGHTBRACKET)
703 lastKeyPressed = '}';
707 else if (event.key.keysym.sym == SDLK_BACKSLASH)
709 lastKeyPressed = '|';
713 else if (event.key.keysym.sym == SDLK_SEMICOLON)
715 lastKeyPressed = ':';
719 else if (event.key.keysym.sym == SDLK_QUOTE)
721 lastKeyPressed = '"';
725 else if (event.key.keysym.sym == SDLK_COMMA)
727 lastKeyPressed = '<';
731 else if (event.key.keysym.sym == SDLK_PERIOD)
733 lastKeyPressed = '>';
737 else if (event.key.keysym.sym == SDLK_SLASH)
739 lastKeyPressed = '?';
743 else if (event.key.keysym.sym == SDLK_BACKQUOTE)
745 lastKeyPressed = '~';
753 if (event.key.keysym.sym >= SDLK_SPACE && event.key.keysym.sym <= SDLK_z)
755 lastKeyPressed = event.key.keysym.sym;
758 // Check for Caps Lock key...
759 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z && capsLock)
760 lastKeyPressed -= 0x20;
765 if (event.key.keysym.sym == SDLK_PAUSE)
767 pauseMode = !pauseMode;
772 SpawnMessage("*** PAUSED ***");
777 SpawnMessage("*** RESUME ***");
781 // Paddle buttons 0 & 1
782 if (event.key.keysym.sym == SDLK_INSERT)
783 openAppleDown = true;
784 if (event.key.keysym.sym == SDLK_PAGEUP)
785 closedAppleDown = true;
787 if (event.key.keysym.sym == SDLK_F11)
788 dumpDis = !dumpDis; // Toggle the disassembly process
789 // else if (event.key.keysym.sym == SDLK_F11)
790 // floppyDrive.LoadImage("./disks/bt1_char.dsk");//Kludge to load char disk...
791 else if (event.key.keysym.sym == SDLK_F9)
793 floppyDrive.CreateBlankImage(0);
794 // SpawnMessage("Image cleared...");
796 else if (event.key.keysym.sym == SDLK_F10)
798 floppyDrive.SwapImages();
799 // SpawnMessage("Image swapped...");
802 if (event.key.keysym.sym == SDLK_F2)// Toggle the palette
804 else if (event.key.keysym.sym == SDLK_F3)// Cycle through screen types
807 // GUI is no longer launched this way...
809 // if (event.key.keysym.sym == SDLK_F5) // Temp GUI launch key
810 if (event.key.keysym.sym == SDLK_F1) // GUI launch key
811 //NOTE: Should parse the output to determine whether or not the user requested
812 // to quit completely... !!! FIX !!!
816 if (event.key.keysym.sym == SDLK_F5)
819 char volStr[19] = "[****************]";
820 // volStr[GetVolume()] = 0;
821 for(int i=GetVolume(); i<16; i++)
823 SpawnMessage("Volume: %s", volStr);
825 else if (event.key.keysym.sym == SDLK_F6)
828 char volStr[19] = "[****************]";
829 // volStr[GetVolume()] = 0;
830 for(int i=GetVolume(); i<16; i++)
832 SpawnMessage("Volume: %s", volStr);
835 if (event.key.keysym.sym == SDLK_F12)
837 if (!fullscreenDebounce)
840 fullscreenDebounce = true;
844 if (event.key.keysym.sym == SDLK_CAPSLOCK)
846 if (!capsLockDebounce)
848 capsLock = !capsLock;
849 capsLockDebounce = true;
855 if (event.key.keysym.sym == SDLK_F12)
856 fullscreenDebounce = false;
857 if (event.key.keysym.sym == SDLK_CAPSLOCK)
858 capsLockDebounce = false;
860 // Paddle buttons 0 & 1
861 if (event.key.keysym.sym == SDLK_INSERT)
862 openAppleDown = false;
863 if (event.key.keysym.sym == SDLK_PAGEUP)
864 closedAppleDown = false;
866 // if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
870 case SDL_MOUSEBUTTONDOWN:
871 GUI2::MouseDown(event.motion.x, event.motion.y, event.motion.state);
873 case SDL_MOUSEBUTTONUP:
874 GUI2::MouseUp(event.motion.x, event.motion.y, event.motion.state);
876 case SDL_MOUSEMOTION:
877 GUI2::MouseMove(event.motion.x, event.motion.y, event.motion.state);
879 case SDL_WINDOWEVENT:
880 if (event.window.event == SDL_WINDOWEVENT_LEAVE)
881 GUI2::MouseMove(0, 0, 0);
889 //#warning "!!! Taking MAJOR time hit with the video frame rendering !!!"
891 RenderScreenBuffer();
892 GUI2::Render(sdlRenderer);
893 SDL_RenderPresent(sdlRenderer);
894 SetCallbackTime(FrameCallback, 16666.66666667);
896 #ifdef CPU_CLOCK_CHECKING
897 //We know it's stopped, so we can get away with this...
901 uint64_t clock = GetCurrentV65C02Clock();
902 //totalCPU += (uint32_t)(clock - lastClock);
904 printf("Executed %u cycles...\n", (uint32_t)(clock - lastClock));
910 //Instead of this, we should yield remaining time to other processes... !!! FIX !!! [DONE]
913 //Actually, slows things down too much...
915 // while (SDL_GetTicks() - startTicks < 16); // Wait for next frame...
917 // This is the problem: If you set the interval to 16, it runs faster than
918 // 1/60s per frame. If you set it to 17, it runs slower. What we need is to
919 // have it do 16 for one frame, then 17 for two others. Then it should average
920 // out to 1/60s per frame every 3 frames.
921 frameCount = (frameCount + 1) % 3;
922 uint32_t waitFrameTime = 17 - (frameCount == 0 ? 1 : 0);
924 while (SDL_GetTicks() - startTicks < waitFrameTime)
925 SDL_Delay(1); // Wait for next frame...
927 startTicks = SDL_GetTicks();
929 uint64_t cpuCycles = GetCurrentV65C02Clock();
930 uint32_t cyclesBurned = (uint32_t)(cpuCycles - lastCPUCycles);
931 WriteLog("FrameCallback: used %i cycles\n", cyclesBurned);
932 lastCPUCycles = cpuCycles;
935 //let's wait, then signal...
936 //works longer, but then still falls behind...
937 #ifdef THREADED_65C02
939 SDL_CondSignal(cpuCond);//OK, let the CPU go another frame...
944 static void BlinkTimer(void)
947 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 sec intervals
952 Next problem is this: How to have events occur and synchronize with the rest
955 o Have the CPU thread manage the timer mechanism? (need to have a method of carrying
956 remainder CPU cycles over...)
958 One way would be to use a fractional accumulator, then subtract 1 every
959 time it overflows. Like so:
965 Execute6808(&soundCPU, time);
966 overflow += 0.289115646;