2 // Apple 2 SDL Portable Apple Emulator
5 // © 2014 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"
56 // Debug and misc. defines
58 #define THREADED_65C02
59 #define CPU_THREAD_OVERFLOW_COMPENSATION
61 //#define CPU_CLOCK_CHECKING
62 //#define THREAD_DEBUGGING
63 #define SOFT_SWITCH_DEBUGGING
67 uint8_t ram[0x10000], rom[0x10000]; // RAM & ROM spaces
68 uint8_t ram2[0x10000]; // Auxillary RAM
69 //uint8_t diskRom[0x100]; // Disk ROM space
70 V65C02REGS mainCPU; // v65C02 execution context
71 uint8_t appleType = APPLE_TYPE_IIE;
72 FloppyDrive floppyDrive;
73 //bool powerOnState = true; // Virtual power switch
74 bool powerStateChangeRequested = false;
78 uint8_t lastKeyPressed = 0;
80 bool openAppleDown = false;
81 bool closedAppleDown = false;
82 bool store80Mode = false;
84 bool slotCXROM = false;
85 bool slotC3ROM = false;
91 // Language card state (ROM read, no write)
92 uint8_t lcState = 0x02;
94 static bool running = true; // Machine running state flag...
95 static uint32_t startTicks;
96 static bool pauseMode = false;
97 static bool fullscreenDebounce = false;
98 static bool capsLock = false;
99 static bool capsLockDebounce = false;
101 // Local functions (technically, they're global...)
103 bool LoadImg(char * filename, uint8_t * ram, int size);
104 static void SaveApple2State(const char * filename);
105 static bool LoadApple2State(const char * filename);
106 static void ResetApple2State(void);
108 // Local timer callback functions
110 static void FrameCallback(void);
111 static void BlinkTimer(void);
113 #ifdef THREADED_65C02
114 // Test of threaded execution of 6502
115 static SDL_Thread * cpuThread = NULL;
116 //static SDL_mutex * cpuMutex = NULL;
117 static SDL_cond * cpuCond = NULL;
118 static SDL_sem * mainSem = NULL;
119 static bool cpuFinished = false;
120 //static bool cpuSleep = false;
122 // NB: Apple //e Manual sez 6502 is running @ 1,022,727 Hz
124 // Let's try a thread...
126 Here's how it works: Execute 1 frame's worth, then sleep.
127 Other stuff wakes it up
129 int CPUThreadFunc(void * data)
131 // Mutex must be locked for conditional to work...
132 // Also, must be created in the thread that uses it...
133 SDL_mutex * cpuMutex = SDL_CreateMutex();
135 // decrement mainSem...
136 //SDL_SemWait(mainSem);
137 #ifdef CPU_THREAD_OVERFLOW_COMPENSATION
138 float overflow = 0.0;
143 // This is never set to true anywhere...
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
155 #ifdef THREAD_DEBUGGING
156 WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
158 for(int i=0; i<800; i++)
160 uint32_t cycles = 21;
161 overflow += 0.333333334;
169 Execute65C02(&mainCPU, cycles);
170 WriteSampleToBuffer();
172 // Dunno if this is correct (seems to be close enough)...
173 vbl = (i < 670 ? true : false);
176 #ifdef THREAD_DEBUGGING
177 WriteLog("CPU: SDL_mutexP(cpuMutex);\n");
179 SDL_mutexP(cpuMutex);
180 // increment mainSem...
181 #ifdef THREAD_DEBUGGING
182 WriteLog("CPU: SDL_SemPost(mainSem);\n");
184 SDL_SemPost(mainSem);
185 #ifdef THREAD_DEBUGGING
186 WriteLog("CPU: SDL_CondWait(cpuCond, cpuMutex);\n");
188 SDL_CondWait(cpuCond, cpuMutex);
190 #ifdef THREAD_DEBUGGING
191 WriteLog("CPU: SDL_mutexV(cpuMutex);\n");
193 SDL_mutexV(cpuMutex);
195 while (!cpuFinished);
197 SDL_DestroyMutex(cpuMutex);
206 // Request a change in the power state of the emulated Apple
208 void SetPowerState(void)
210 // powerOnState = state;
211 // pauseMode = !state;
215 //printf("Turning on...\n");
216 // Transitioning from OFF to ON
217 // mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
222 //printf("Turning off...\n");
226 powerStateChangeRequested = true;
232 // Load a file into RAM/ROM image space
234 bool LoadImg(char * filename, uint8_t * ram, int size)
236 FILE * fp = fopen(filename, "rb");
241 fread(ram, 1, size, fp);
248 const uint8_t stateHeader[19] = "APPLE2SAVESTATE1.0";
249 static void SaveApple2State(const char * filename)
251 WriteLog("Main: Saving Apple2 state...\n");
252 FILE * file = fopen(filename, "wb");
256 WriteLog("Could not open file \"%s\" for writing!\n", filename);
261 fwrite(stateHeader, 1, 18, file);
263 // Write out CPU state
264 fwrite(&mainCPU, 1, sizeof(mainCPU), file);
266 // Write out main memory
267 fwrite(ram, 1, 0x10000, file);
268 fwrite(ram2, 1, 0x10000, file);
270 // Write out state variables
271 fputc((uint8_t)keyDown, file);
272 fputc((uint8_t)openAppleDown, file);
273 fputc((uint8_t)closedAppleDown, file);
274 fputc((uint8_t)store80Mode, file);
275 fputc((uint8_t)vbl, file);
276 fputc((uint8_t)slotCXROM, file);
277 fputc((uint8_t)slotC3ROM, file);
278 fputc((uint8_t)ramrd, file);
279 fputc((uint8_t)ramwrt, file);
280 fputc((uint8_t)altzp, file);
281 fputc((uint8_t)ioudis, file);
282 fputc((uint8_t)dhires, file);
283 fputc((uint8_t)flash, file);
284 fputc((uint8_t)textMode, file);
285 fputc((uint8_t)mixedMode, file);
286 fputc((uint8_t)displayPage2, file);
287 fputc((uint8_t)hiRes, file);
288 fputc((uint8_t)alternateCharset, file);
289 fputc((uint8_t)col80Mode, file);
290 fputc(lcState, file);
292 // Write out floppy state
293 floppyDrive.SaveState(file);
298 static bool LoadApple2State(const char * filename)
300 WriteLog("Main: Loading Apple2 state...\n");
301 FILE * file = fopen(filename, "rb");
305 WriteLog("Could not open file \"%s\" for reading!\n", filename);
310 fread(buffer, 1, 18, file);
313 if (memcmp(buffer, stateHeader, 18) != 0)
316 WriteLog("File \"%s\" is not a valid Apple2 save state file!\n", filename);
321 fread(&mainCPU, 1, sizeof(mainCPU), file);
324 fread(ram, 1, 0x10000, file);
325 fread(ram2, 1, 0x10000, file);
327 // Read in state variables
328 keyDown = (bool)fgetc(file);
329 openAppleDown = (bool)fgetc(file);
330 closedAppleDown = (bool)fgetc(file);
331 store80Mode = (bool)fgetc(file);
332 vbl = (bool)fgetc(file);
333 slotCXROM = (bool)fgetc(file);
334 slotC3ROM = (bool)fgetc(file);
335 ramrd = (bool)fgetc(file);
336 ramwrt = (bool)fgetc(file);
337 altzp = (bool)fgetc(file);
338 ioudis = (bool)fgetc(file);
339 dhires = (bool)fgetc(file);
340 flash = (bool)fgetc(file);
341 textMode = (bool)fgetc(file);
342 mixedMode = (bool)fgetc(file);
343 displayPage2 = (bool)fgetc(file);
344 hiRes = (bool)fgetc(file);
345 alternateCharset = (bool)fgetc(file);
346 col80Mode = (bool)fgetc(file);
347 lcState = fgetc(file);
349 // Read in floppy state
350 floppyDrive.LoadState(file);
354 // Make sure things are in a sane state before execution :-P
355 mainCPU.RdMem = AppleReadMem;
356 mainCPU.WrMem = AppleWriteMem;
363 static void ResetApple2State(void)
366 openAppleDown = false;
367 closedAppleDown = false;
378 // SwitchLC(); // Make sure MMU is in sane state
379 //NOPE, does nothing SetupAddressMap();
382 // Without this, you can wedge the system :-/
383 memset(ram, 0, 0x10000);
384 // memset(ram2, 0, 0x10000);
385 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
389 #ifdef CPU_CLOCK_CHECKING
391 uint32_t totalCPU = 0;
392 uint64_t lastClock = 0;
397 int main(int /*argc*/, char * /*argv*/[])
399 InitLog("./apple2.log");
401 srand(time(NULL)); // Initialize RNG
404 memset(ram, 0, 0x10000);
405 memset(rom, 0, 0x10000);
406 memset(ram2, 0, 0x10000);
412 // Set up V65C02 execution context
413 memset(&mainCPU, 0, sizeof(V65C02REGS));
414 mainCPU.RdMem = AppleReadMem;
415 mainCPU.WrMem = AppleWriteMem;
416 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
418 // alternateCharset = true;
419 // if (!LoadImg(settings.BIOSPath, rom + 0xD000, 0x3000))
420 if (!LoadImg(settings.BIOSPath, rom + 0xC000, 0x4000))
422 WriteLog("Could not open file '%s'!\n", settings.BIOSPath);
426 //Load up disk image from config file (for now)...
427 floppyDrive.LoadImage(settings.diskImagePath1, 0);
428 floppyDrive.LoadImage(settings.diskImagePath2, 1);
430 WriteLog("About to initialize video...\n");
434 std::cout << "Aborting!" << std::endl;
438 GUI::Init(sdlRenderer);
440 // Have to do this *after* video init but *before* sound init...!
441 //Shouldn't be necessary since we're not doing emulation in the ISR...
442 if (settings.autoStateSaving)
444 // Load last state from file...
445 if (!LoadApple2State(settings.autoStatePath))
446 WriteLog("Unable to use Apple2 state file \"%s\"!\n", settings.autoStatePath);
449 WriteLog("About to initialize audio...\n");
451 SetupBlurTable(); // Set up the color TV emulation blur table
452 running = true; // Set running status...
453 InitializeEventList(); // Clear the event list before we use it...
454 SetCallbackTime(FrameCallback, 16666.66666667); // Set frame to fire at 1/60 s interval
455 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 s intervals
456 startTicks = SDL_GetTicks();
458 #ifdef THREADED_65C02
459 cpuCond = SDL_CreateCond();
460 mainSem = SDL_CreateSemaphore(1);
461 cpuThread = SDL_CreateThread(CPUThreadFunc, NULL, NULL);
462 //Hmm... CPU does POST (+1), wait, then WAIT (-1)
463 // SDL_sem * mainMutex = SDL_CreateMutex();
466 WriteLog("Entering main loop...\n");
470 double timeToNextEvent = GetTimeToNextEvent();
471 #ifndef THREADED_65C02
472 Execute65C02(&mainCPU, USEC_TO_M6502_CYCLES(timeToNextEvent));
475 #ifdef CPU_CLOCK_CHECKING
476 #ifndef THREADED_65C02
477 totalCPU += USEC_TO_M6502_CYCLES(timeToNextEvent);
483 #ifdef THREADED_65C02
484 WriteLog("Main: cpuFinished = true;\n");
486 //#warning "If sound thread is behind, CPU thread will never wake up... !!! FIX !!!" [DONE]
487 //What to do? How do you know when the CPU is sleeping???
488 //USE A CONDITIONAL!!! OF COURSE!!!!!!11!11!11!!!1!
489 //Nope, use a semaphore...
490 WriteLog("Main: SDL_SemWait(mainSem);\n");
491 // Only do this if NOT in power off/emulation paused mode!
493 // Should lock until CPU thread is waiting...
494 SDL_SemWait(mainSem);
497 WriteLog("Main: SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up\n");
498 SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up
499 WriteLog("Main: SDL_WaitThread(cpuThread, NULL);\n");
500 SDL_WaitThread(cpuThread, NULL);
501 //nowok:SDL_WaitThread(CPUThreadFunc, NULL);
502 WriteLog("Main: SDL_DestroyCond(cpuCond);\n");
503 SDL_DestroyCond(cpuCond);
504 SDL_DestroySemaphore(mainSem);
506 if (settings.autoStateSaving)
508 // Save state here...
509 SaveApple2State(settings.autoStatePath);
512 floppyDrive.SaveImage(0);
513 floppyDrive.SaveImage(1);
529 -----------------------
530 space $A0 $A0 $A0 $A0 No xlation
531 RETURN $8D $8D $8D $8D No xlation
532 0 $B0 $B0 $B0 $B0 Need to screen shift+0 (?)
533 1! $B1 $B1 $A1 $A1 No xlation
534 2" $B2 $B2 $A2 $A2 No xlation
535 3# $B3 $B3 $A3 $A3 No xlation
536 4$ $B4 $B4 $A4 $A4 No xlation
537 5% $B5 $B5 $A5 $A5 No xlation
538 6& $B6 $B6 $A6 $A6 No xlation
539 7' $B7 $B7 $A7 $A7 No xlation
540 8( $B8 $B8 $A8 $A8 No xlation
541 9) $B9 $B9 $A9 $A9 No xlation
542 :* $BA $BA $AA $AA No xlation
543 ;+ $BB $BB $AB $AB No xlation
544 ,< $AC $AC $BC $BC No xlation
545 -= $AD $AD $BD $BD No xlation
546 .> $AE $AE $BE $BE No xlation
547 /? $AF $AF $BF $BF No xlation
560 M $CD $8D $DD $9D -> ODD
561 N^ $CE $8E $DE $9E -> ODD
563 P@ $D0 $90 $C0 $80 Need to xlate CTL+SHFT+P & SHFT+P (?)
576 ESC $9B $9B $9B $9B No xlation
579 //static uint64_t lastCPUCycles = 0;
580 static uint32_t frameCount = 0;
581 static void FrameCallback(void)
585 while (SDL_PollEvent(&event))
589 // Problem with using SDL_TEXTINPUT is that it causes key delay. :-/
592 //Need to do some key translation here, and screen out non-apple keys as well...
593 //(really, could do it all in SDL_KEYDOWN, would just have to get symbols &
594 // everything else done separately. this is slightly easier. :-P)
595 // if (event.key.keysym.sym == SDLK_TAB) // Prelim key screening...
596 if (event.edit.text[0] == '\t') // Prelim key screening...
599 lastKeyPressed = event.edit.text[0];
602 //kludge: should have a caps lock thingy here...
603 //or all uppercase for ][+...
604 // if (lastKeyPressed >= 'a' && lastKeyPressed <='z')
605 // lastKeyPressed &= 0xDF; // Convert to upper case...
610 // Use ALT+Q to exit, as well as the usual window decoration method
611 if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod & KMOD_ALT))
614 // CTRL+RESET key emulation (mapped to CTRL+`)
615 // This doesn't work...
616 // if (event.key.keysym.sym == SDLK_BREAK && (event.key.keysym.mod & KMOD_CTRL))
617 // if (event.key.keysym.sym == SDLK_PAUSE && (event.key.keysym.mod & KMOD_CTRL))
618 if (event.key.keysym.sym == SDLK_BACKQUOTE && (event.key.keysym.mod & KMOD_CTRL))
620 //NOTE that this shouldn't take place until the key is lifted... !!! FIX !!!
621 //ALSO it seems to leave the machine in an inconsistent state vis-a-vis the language card...
622 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
626 if (event.key.keysym.sym == SDLK_RIGHT)
627 lastKeyPressed = 0x15, keyDown = true;
628 else if (event.key.keysym.sym == SDLK_LEFT)
629 lastKeyPressed = 0x08, keyDown = true;
630 else if (event.key.keysym.sym == SDLK_UP)
631 lastKeyPressed = 0x0B, keyDown = true;
632 else if (event.key.keysym.sym == SDLK_DOWN)
633 lastKeyPressed = 0x0A, keyDown = true;
634 else if (event.key.keysym.sym == SDLK_RETURN)
635 lastKeyPressed = 0x0D, keyDown = true;
636 else if (event.key.keysym.sym == SDLK_ESCAPE)
637 lastKeyPressed = 0x1B, keyDown = true;
638 else if (event.key.keysym.sym == SDLK_BACKSPACE)
639 lastKeyPressed = 0x7F, keyDown = true;
641 // Fix CTRL+key combo...
642 if (event.key.keysym.mod & KMOD_CTRL)
644 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
646 lastKeyPressed = (event.key.keysym.sym - SDLK_a) + 1;
648 //printf("Key combo pressed: CTRL+%c\n", lastKeyPressed + 0x40);
654 // Fix SHIFT+key combo...
655 if (event.key.keysym.mod & KMOD_SHIFT)
657 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
659 lastKeyPressed = (event.key.keysym.sym - SDLK_a) + 0x41;
661 //printf("Key combo pressed: CTRL+%c\n", lastKeyPressed + 0x40);
664 else if (event.key.keysym.sym == SDLK_1)
666 lastKeyPressed = '!';
670 else if (event.key.keysym.sym == SDLK_2)
672 lastKeyPressed = '@';
676 else if (event.key.keysym.sym == SDLK_3)
678 lastKeyPressed = '#';
682 else if (event.key.keysym.sym == SDLK_3)
684 lastKeyPressed = '#';
688 else if (event.key.keysym.sym == SDLK_4)
690 lastKeyPressed = '$';
694 else if (event.key.keysym.sym == SDLK_5)
696 lastKeyPressed = '%';
700 else if (event.key.keysym.sym == SDLK_6)
702 lastKeyPressed = '^';
706 else if (event.key.keysym.sym == SDLK_7)
708 lastKeyPressed = '&';
712 else if (event.key.keysym.sym == SDLK_8)
714 lastKeyPressed = '*';
718 else if (event.key.keysym.sym == SDLK_9)
720 lastKeyPressed = '(';
724 else if (event.key.keysym.sym == SDLK_0)
726 lastKeyPressed = ')';
730 else if (event.key.keysym.sym == SDLK_MINUS)
732 lastKeyPressed = '_';
736 else if (event.key.keysym.sym == SDLK_EQUALS)
738 lastKeyPressed = '+';
742 else if (event.key.keysym.sym == SDLK_LEFTBRACKET)
744 lastKeyPressed = '{';
748 else if (event.key.keysym.sym == SDLK_RIGHTBRACKET)
750 lastKeyPressed = '}';
754 else if (event.key.keysym.sym == SDLK_BACKSLASH)
756 lastKeyPressed = '|';
760 else if (event.key.keysym.sym == SDLK_SEMICOLON)
762 lastKeyPressed = ':';
766 else if (event.key.keysym.sym == SDLK_QUOTE)
768 lastKeyPressed = '"';
772 else if (event.key.keysym.sym == SDLK_COMMA)
774 lastKeyPressed = '<';
778 else if (event.key.keysym.sym == SDLK_PERIOD)
780 lastKeyPressed = '>';
784 else if (event.key.keysym.sym == SDLK_SLASH)
786 lastKeyPressed = '?';
790 else if (event.key.keysym.sym == SDLK_BACKQUOTE)
792 lastKeyPressed = '~';
800 if (event.key.keysym.sym >= SDLK_SPACE && event.key.keysym.sym <= SDLK_z)
802 lastKeyPressed = event.key.keysym.sym;
805 // Check for Caps Lock key...
806 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z && capsLock)
807 lastKeyPressed -= 0x20;
812 if (event.key.keysym.sym == SDLK_PAUSE)
814 pauseMode = !pauseMode;
819 SpawnMessage("*** PAUSED ***");
824 SpawnMessage("*** RESUME ***");
828 // Paddle buttons 0 & 1
829 if (event.key.keysym.sym == SDLK_INSERT)
830 openAppleDown = true;
831 if (event.key.keysym.sym == SDLK_PAGEUP)
832 closedAppleDown = true;
834 if (event.key.keysym.sym == SDLK_F11)
835 dumpDis = !dumpDis; // Toggle the disassembly process
837 /*else if (event.key.keysym.sym == SDLK_F9)
839 floppyDrive.CreateBlankImage(0);
840 // SpawnMessage("Image cleared...");
842 /*else if (event.key.keysym.sym == SDLK_F10)
844 floppyDrive.SwapImages();
845 // SpawnMessage("Image swapped...");
848 if (event.key.keysym.sym == SDLK_F2)// Toggle the palette
850 else if (event.key.keysym.sym == SDLK_F3)// Cycle through screen types
852 else if (event.key.keysym.sym == SDLK_F5)
855 char volStr[19] = "[****************]";
856 // volStr[GetVolume()] = 0;
857 for(int i=GetVolume(); i<16; i++)
859 SpawnMessage("Volume: %s", volStr);
861 else if (event.key.keysym.sym == SDLK_F6)
864 char volStr[19] = "[****************]";
865 // volStr[GetVolume()] = 0;
866 for(int i=GetVolume(); i<16; i++)
868 SpawnMessage("Volume: %s", volStr);
870 else if (event.key.keysym.sym == SDLK_F12)
872 if (!fullscreenDebounce)
875 fullscreenDebounce = true;
879 if (event.key.keysym.sym == SDLK_CAPSLOCK)
881 if (!capsLockDebounce)
883 capsLock = !capsLock;
884 capsLockDebounce = true;
890 if (event.key.keysym.sym == SDLK_F12)
891 fullscreenDebounce = false;
892 if (event.key.keysym.sym == SDLK_CAPSLOCK)
893 capsLockDebounce = false;
895 // Paddle buttons 0 & 1
896 if (event.key.keysym.sym == SDLK_INSERT)
897 openAppleDown = false;
898 if (event.key.keysym.sym == SDLK_PAGEUP)
899 closedAppleDown = false;
901 // if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
905 case SDL_MOUSEBUTTONDOWN:
906 GUI::MouseDown(event.motion.x, event.motion.y, event.motion.state);
908 case SDL_MOUSEBUTTONUP:
909 GUI::MouseUp(event.motion.x, event.motion.y, event.motion.state);
911 case SDL_MOUSEMOTION:
912 GUI::MouseMove(event.motion.x, event.motion.y, event.motion.state);
914 case SDL_WINDOWEVENT:
915 if (event.window.event == SDL_WINDOWEVENT_LEAVE)
916 GUI::MouseMove(0, 0, 0);
924 // Handle power request from the GUI
925 if (powerStateChangeRequested)
927 if (GUI::powerOnState)
931 // Unlock the CPU thread...
932 SDL_SemPost(mainSem);
937 // Should lock until CPU thread is waiting...
938 SDL_SemWait(mainSem);
943 powerStateChangeRequested = false;
946 //#warning "!!! Taking MAJOR time hit with the video frame rendering !!!"
952 RenderScreenBuffer();
953 GUI::Render(sdlRenderer);
954 SDL_RenderPresent(sdlRenderer);
955 SetCallbackTime(FrameCallback, 16666.66666667);
957 #ifdef CPU_CLOCK_CHECKING
958 //We know it's stopped, so we can get away with this...
962 uint64_t clock = GetCurrentV65C02Clock();
963 //totalCPU += (uint32_t)(clock - lastClock);
965 printf("Executed %u cycles...\n", (uint32_t)(clock - lastClock));
972 // This is the problem: If you set the interval to 16, it runs faster than
973 // 1/60s per frame. If you set it to 17, it runs slower. What we need is to
974 // have it do 16 for one frame, then 17 for two others. Then it should average
975 // out to 1/60s per frame every 3 frames.
976 frameCount = (frameCount + 1) % 3;
977 uint32_t waitFrameTime = 17 - (frameCount == 0 ? 1 : 0);
979 // Wait for next frame...
980 while (SDL_GetTicks() - startTicks < waitFrameTime)
983 startTicks = SDL_GetTicks();
985 uint64_t cpuCycles = GetCurrentV65C02Clock();
986 uint32_t cyclesBurned = (uint32_t)(cpuCycles - lastCPUCycles);
987 WriteLog("FrameCallback: used %i cycles\n", cyclesBurned);
988 lastCPUCycles = cpuCycles
991 //let's wait, then signal...
992 //works longer, but then still falls behind...
993 #ifdef THREADED_65C02
995 SDL_CondSignal(cpuCond);//OK, let the CPU go another frame...
1000 static void BlinkTimer(void)
1003 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 sec intervals
1008 Next problem is this: How to have events occur and synchronize with the rest
1011 o Have the CPU thread manage the timer mechanism? (need to have a method of carrying
1012 remainder CPU cycles over...)
1014 One way would be to use a fractional accumulator, then subtract 1 every
1015 time it overflows. Like so:
1017 double overflow = 0;
1021 Execute6808(&soundCPU, time);
1022 overflow += 0.289115646;