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 L. 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
24 // - Port to SDL [DONE]
26 // - Weed out unneeded functions [DONE]
28 // - 128K IIe related stuff
29 // - State loading/saving
47 #include "applevideo.h"
53 #include "gui/window.h"
54 #include "gui/draggablewindow2.h"
55 #include "gui/textedit.h"
57 // Debug and misc. defines
59 #define THREADED_65C02
60 #define CPU_THREAD_OVERFLOW_COMPENSATION
62 //#define CPU_CLOCK_CHECKING
63 //#define THREAD_DEBUGGING
64 #define SOFT_SWITCH_DEBUGGING
68 uint8_t ram[0x10000], rom[0x10000]; // RAM & ROM spaces
69 uint8_t ram2[0x10000]; // Auxillary RAM
70 uint8_t diskRom[0x100]; // Disk ROM space
71 V65C02REGS mainCPU; // v65C02 execution context
72 uint8_t appleType = APPLE_TYPE_IIE;
73 FloppyDrive floppyDrive;
77 static uint8_t lastKeyPressed = 0;
78 static bool keyDown = false;
79 static bool openAppleDown = false;
80 static bool closedAppleDown = false;
81 static bool store80Mode = false;
82 static bool vbl = false;
83 static bool slotCXROM = false;
84 static bool slotC3ROM = false;
85 static bool ramrd = false;
86 static bool ramwrt = false;
87 static bool altzp = false;
88 static bool ioudis = true;
91 //static FloppyDrive floppyDrive;
93 enum { LC_BANK_1, LC_BANK_2 };
95 static uint8_t visibleBank = LC_BANK_1;
96 static bool readRAM = false;
97 static bool writeRAM = false;
99 static bool running = true; // Machine running state flag...
100 static uint32_t startTicks;
101 static bool pauseMode = false;
103 static GUI * gui = NULL;
105 // Local functions (technically, they're global...)
107 bool LoadImg(char * filename, uint8_t * ram, int size);
108 uint8_t RdMem(uint16_t addr);
109 void WrMem(uint16_t addr, uint8_t b);
110 static void SaveApple2State(const char * filename);
111 static bool LoadApple2State(const char * filename);
113 // Local timer callback functions
115 static void FrameCallback(void);
116 static void BlinkTimer(void);
118 #ifdef THREADED_65C02
119 // Test of threaded execution of 6502
120 static SDL_Thread * cpuThread = NULL;
121 //static SDL_mutex * cpuMutex = NULL;
122 static SDL_cond * cpuCond = NULL;
123 static SDL_sem * mainSem = NULL;
124 static bool cpuFinished = false;
125 static bool cpuSleep = false;
128 // NB: Apple //e Manual sez 6502 is running @ 1,022,727 Hz
130 // Let's try a thread...
132 Here's how it works: Execute 1 frame's worth, then sleep.
133 Other stuff wakes it up
135 int CPUThreadFunc(void * data)
137 // Mutex must be locked for conditional to work...
138 // Also, must be created in the thread that uses it...
139 SDL_mutex * cpuMutex = SDL_CreateMutex();
141 // decrement mainSem...
142 //SDL_SemWait(mainSem);
143 #ifdef CPU_THREAD_OVERFLOW_COMPENSATION
144 float overflow = 0.0;
150 SDL_CondWait(cpuCond, cpuMutex);
152 // decrement mainSem...
153 #ifdef THREAD_DEBUGGING
154 WriteLog("CPU: SDL_SemWait(mainSem);\n");
156 SDL_SemWait(mainSem);
158 // There are exactly 800 slices of 21.333 cycles per frame, so it works out
161 uint32_t cycles = 17066;
162 #ifdef CPU_THREAD_OVERFLOW_COMPENSATION
163 // ODD! It's closer *without* this overflow compensation. ??? WHY ???
164 overflow += 0.666666667;
173 #ifdef THREAD_DEBUGGING
174 WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
176 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!
178 // Adjust the sound routine's last cycle toggled time base
179 // Also, since we're finished executing, .clock is now valid
180 #ifdef THREAD_DEBUGGING
181 WriteLog("CPU: AdjustLastToggleCycles(mainCPU.clock);\n");
183 AdjustLastToggleCycles(mainCPU.clock);
185 #ifdef THREAD_DEBUGGING
186 WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
188 for(int i=0; i<800; i++)
190 uint32_t cycles = 21;
191 overflow += 0.333333334;
199 Execute65C02(&mainCPU, cycles);
200 WriteSampleToBuffer();
202 // Dunno if this is correct (seems to be close enough)...
203 vbl = (i < 670 ? true : false);
207 //WriteLog("CPUThread: Supposedly end of frame...\n");
209 #ifdef THREAD_DEBUGGING
210 WriteLog("CPU: SDL_mutexP(cpuMutex);\n");
212 SDL_mutexP(cpuMutex);
214 if (SDL_CondWait(cpuCond, cpuMutex) != 0)
216 printf("SDL_CondWait != 0! (Error: '%s')\n", SDL_GetError());
220 // increment mainSem...
221 #ifdef THREAD_DEBUGGING
222 WriteLog("CPU: SDL_SemPost(mainSem);\n");
224 SDL_SemPost(mainSem);
225 // SDL_CondSignal(mainCond); // In case something is waiting on us...
227 #ifdef THREAD_DEBUGGING
228 WriteLog("CPU: SDL_CondWait(cpuCond, cpuMutex);\n");
230 SDL_CondWait(cpuCond, cpuMutex);
233 #ifdef THREAD_DEBUGGING
234 WriteLog("CPU: SDL_mutexV(cpuMutex);\n");
236 SDL_mutexV(cpuMutex);
238 while (!cpuFinished);
240 SDL_DestroyMutex(cpuMutex);
249 Element * TestWindow(void)
251 Element * win = new DraggableWindow2(10, 10, 128, 128);
252 // ((DraggableWindow *)win)->AddElement(new TextEdit(4, 16, 92, 0, "u2prog.dsk", win));
258 Element * QuitEmulator(void)
268 Small Apple II memory map:
270 $C010 - Clear bit 7 of keyboard data ($C000)
271 $C030 - Toggle speaker diaphragm
273 $C054 - Select page 1
274 $C056 - Select lo-res
275 $C058 - Set annuciator-0 output to 0
276 $C05A - Set annuciator-0 output to 0
277 $C05D - Set annuciator-0 output to 1
278 $C05F - Set annuciator-0 output to 1
279 $C0E0 - Disk control stepper ($C0E0-7)
280 $C0E9 - Disk control motor (on)
281 $C0EA - Disk enable (drive 1)
283 $C0EE - Disk set read mode
287 // V65C02 read/write memory functions
290 uint8_t RdMem(uint16_t addr)
295 if (addr >= 0xC000 && addr <= 0xC0FF)
296 WriteLog("\n*** Read at I/O address %04X\n", addr);
299 if (addr >= 0xC080 && addr <= 0xC08F)
300 WriteLog("\n*** Read at I/O address %04X\n", addr);
303 if ((addr & 0xFFF0) == 0xC000) // Read $C000-$C00F
305 return lastKeyPressed | (keyDown ? 0x80 : 0x00);
307 // else if ((addr & 0xFFF8) == 0xC010) // Read $C010-$C01F
308 else if (addr == 0xC010)
310 //This is bogus: keyDown is set to false, so return val NEVER is set...
312 //Also, this is IIe/IIc only...!
313 uint8_t retVal = lastKeyPressed | (keyDown ? 0x80 : 0x00);
317 // These are //e locations
318 else if (addr == 0xC011)
320 #ifdef SOFT_SWITCH_DEBUGGING
321 WriteLog("RDBANK2 (read)\n");
323 return (visibleBank == LC_BANK_2 ? 0x80 : 0x00);
325 else if (addr == 0xC012)
327 #ifdef SOFT_SWITCH_DEBUGGING
328 WriteLog("RDLCRAM (read)\n");
330 return (readRAM ? 0x80 : 0x00);
332 else if (addr == 0xC013)
334 #ifdef SOFT_SWITCH_DEBUGGING
335 WriteLog("RAMRD (read)\n");
337 return (ramrd ? 0x80 : 0x00);
339 else if (addr == 0xC014)
341 #ifdef SOFT_SWITCH_DEBUGGING
342 WriteLog("RAMWRT (read)\n");
344 return (ramwrt ? 0x80 : 0x00);
346 else if (addr == 0xC015)
348 #ifdef SOFT_SWITCH_DEBUGGING
349 WriteLog("SLOTCXROM (read)\n");
351 return (slotCXROM ? 0x80 : 0x00);
353 else if (addr == 0xC016)
355 #ifdef SOFT_SWITCH_DEBUGGING
356 WriteLog("ALTZP (read)\n");
358 return (altzp ? 0x80 : 0x00);
360 else if (addr == 0xC017)
362 #ifdef SOFT_SWITCH_DEBUGGING
363 WriteLog("SLOTC3ROM (read)\n");
365 return (slotC3ROM ? 0x80 : 0x00);
367 else if (addr == 0xC018)
369 #ifdef SOFT_SWITCH_DEBUGGING
370 WriteLog("80STORE (read)\n");
372 return (store80Mode ? 0x80 : 0x00);
374 else if (addr == 0xC019)
376 #ifdef SOFT_SWITCH_DEBUGGING
377 WriteLog("VBL (read)\n");
379 // NB: The doco suggests that this signal goes LOW when in the VBI.
380 // Which means that we need to control this by counting lines somewhere.
381 return (vbl ? 0x80 : 0x00);
383 else if (addr == 0xC01A)
385 #ifdef SOFT_SWITCH_DEBUGGING
386 WriteLog("TEXT (read)\n");
388 return (textMode ? 0x80 : 0x00);
390 else if (addr == 0xC01B)
392 #ifdef SOFT_SWITCH_DEBUGGING
393 WriteLog("MIXED (read)\n");
395 return (mixedMode ? 0x80 : 0x00);
397 else if (addr == 0xC01C)
399 #ifdef SOFT_SWITCH_DEBUGGING
400 WriteLog("PAGE2 (read)\n");
402 return (displayPage2 ? 0x80 : 0x00);
404 else if (addr == 0xC01D)
406 #ifdef SOFT_SWITCH_DEBUGGING
407 WriteLog("HIRES (read)\n");
409 return (hiRes ? 0x80 : 0x00);
411 else if (addr == 0xC01E)
413 #ifdef SOFT_SWITCH_DEBUGGING
414 WriteLog("ALTCHARSET (read)\n");
416 return (alternateCharset ? 0x80 : 0x00);
418 else if (addr == 0xC01F)
420 #ifdef SOFT_SWITCH_DEBUGGING
421 WriteLog("80COL (read)\n");
423 return (col80Mode ? 0x80 : 0x00);
425 else if ((addr & 0xFFF0) == 0xC030) // Read $C030-$C03F
428 //should it return something else here???
431 else if (addr == 0xC050) // Read $C050
433 #ifdef SOFT_SWITCH_DEBUGGING
434 WriteLog("TEXT off (read)\n");
438 else if (addr == 0xC051) // Read $C051
440 #ifdef SOFT_SWITCH_DEBUGGING
441 WriteLog("TEXT on (read)\n");
445 else if (addr == 0xC052) // Read $C052
447 #ifdef SOFT_SWITCH_DEBUGGING
448 WriteLog("MIXED off (read)\n");
452 else if (addr == 0xC053) // Read $C053
454 #ifdef SOFT_SWITCH_DEBUGGING
455 WriteLog("MIXED on (read)\n");
459 else if (addr == 0xC054) // Read $C054
461 #ifdef SOFT_SWITCH_DEBUGGING
462 WriteLog("PAGE2 off (read)\n");
464 displayPage2 = false;
466 else if (addr == 0xC055) // Read $C055
468 #ifdef SOFT_SWITCH_DEBUGGING
469 WriteLog("PAGE2 on (read)\n");
473 else if (addr == 0xC056) // Read $C056
475 #ifdef SOFT_SWITCH_DEBUGGING
476 WriteLog("HIRES off (read)\n");
480 else if (addr == 0xC057) // Read $C057
482 #ifdef SOFT_SWITCH_DEBUGGING
483 WriteLog("HIRES on (read)\n");
487 else if (addr == 0xC05E)
489 #ifdef SOFT_SWITCH_DEBUGGING
490 WriteLog("DHIRES on (read)\n");
495 else if (addr == 0xC05F)
497 #ifdef SOFT_SWITCH_DEBUGGING
498 WriteLog("DHIRES off (read)\n");
503 else if (addr == 0xC061) // Read $C061
505 // Open Apple key (or push button 0)
506 return (openAppleDown ? 0x80 : 0x00);
508 else if (addr == 0xC062) // Read $C062
510 // Open Apple key (or push button 0)
511 return (closedAppleDown ? 0x80 : 0x00);
513 // The way the paddles work is that a strobe is written (or read) to $C070,
514 // then software counts down the time that it takes for the paddle outputs
515 // to have bit 7 return to 0. If there are no paddles connected, bit 7
517 else if (addr == 0xC064) // Paddles 0-3
521 else if (addr == 0xC065)
525 else if (addr == 0xC066)
529 else if (addr == 0xC067)
533 else if (addr == 0xC07E)
535 #ifdef SOFT_SWITCH_DEBUGGING
536 WriteLog("IOUDIS (read)\n");
538 return (ioudis ? 0x80 : 0x00);
540 else if (addr == 0xC07F)
542 #ifdef SOFT_SWITCH_DEBUGGING
543 WriteLog("DHIRES (read)\n");
545 return (dhires ? 0x80 : 0x00);
548 //Note that this is a kludge: The $D000-$DFFF 4K space is shared (since $C000-$CFFF is
549 //memory mapped) between TWO banks, and that that $E000-$FFFF RAM space is a single bank.
550 //[SHOULD BE FIXED NOW]
551 //OK! This switch selects bank 2 of the 4K bank at $D000-$DFFF. One access makes it
552 //visible, two makes it R/W.
587 else if ((addr & 0xFFFB) == 0xC080)
590 WriteLog("LC(R): $C080 49280 OECG R Read RAM bank 2; no write\n");
592 //$C080 49280 OECG R Read RAM bank 2; no write
593 visibleBank = LC_BANK_2;
597 else if ((addr & 0xFFFB) == 0xC081)
600 WriteLog("LC(R): $C081 49281 OECG RR Read ROM; write RAM bank 2\n");
602 //$C081 49281 ROMIN OECG RR Read ROM; write RAM bank 2
603 visibleBank = LC_BANK_2;
607 else if ((addr & 0xFFFB) == 0xC082)
610 WriteLog("LC(R): $C082 49282 OECG R Read ROM; no write\n");
612 //$C082 49282 OECG R Read ROM; no write
613 visibleBank = LC_BANK_2;
617 else if ((addr & 0xFFFB) == 0xC083)
620 WriteLog("LC(R): $C083 49283 OECG RR Read/Write RAM bank 2\n");
622 //$C083 49283 LCBANK2 OECG RR Read/write RAM bank 2
623 visibleBank = LC_BANK_2;
627 else if ((addr & 0xFFFB) == 0xC088)
630 WriteLog("LC(R): $%04X 49288 OECG R Read RAM bank 1; no write\n", addr);
632 //$C088 49288 OECG R Read RAM bank 1; no write
633 visibleBank = LC_BANK_1;
636 //Hm. Some stuff seems to want this.
637 //nope, was looking at $C0E8... return 0xFF;
639 else if ((addr & 0xFFFB) == 0xC089)
642 WriteLog("LC(R): $C089 49289 OECG RR Read ROM; write RAM bank 1\n");
644 //$C089 49289 OECG RR Read ROM; write RAM bank 1
645 visibleBank = LC_BANK_1;
649 else if ((addr & 0xFFFB) == 0xC08A)
652 WriteLog("LC(R): $C08A 49290 OECG R Read ROM; no write\n");
654 //$C08A 49290 OECG R Read ROM; no write
655 visibleBank = LC_BANK_1;
659 else if ((addr & 0xFFFB) == 0xC08B)
662 WriteLog("LC(R): $C08B 49291 OECG RR Read/Write RAM bank 1\n");
664 //$C08B 49291 OECG RR Read/write RAM bank 1
665 visibleBank = LC_BANK_1;
669 else if ((addr & 0xFFF8) == 0xC0E0)
671 floppyDrive.ControlStepper(addr & 0x07);
673 else if ((addr & 0xFFFE) == 0xC0E8)
675 floppyDrive.ControlMotor(addr & 0x01);
677 else if ((addr & 0xFFFE) == 0xC0EA)
679 floppyDrive.DriveEnable(addr & 0x01);
681 else if (addr == 0xC0EC)
683 return floppyDrive.ReadWrite();
685 else if (addr == 0xC0ED)
687 return floppyDrive.GetLatchValue();
689 else if (addr == 0xC0EE)
691 floppyDrive.SetReadMode();
693 else if (addr == 0xC0EF)
695 floppyDrive.SetWriteMode();
698 //#define LC_DEBUGGING
700 bool showpath = false;
701 if (addr >= 0xD000 && addr <= 0xD00F)
705 if (addr >= 0xC100 && addr <= 0xC7FF) // The $C000-$CFFF block is *never* RAM
707 // Looks like the ][e ref manual got this one wrong: slotCXROM set should mean
708 // use internal ROM, NOT slot ROM. :-/
709 // (fixed now, by setting the switch correctly in the write mem section :-P)
715 if (addr >= 0xC100 && addr <= 0xC1FF)
716 b = parallelROM[addr & 0xFF];
717 else if (addr >= 0xC600 && addr <= 0xC6FF)
718 b = diskROM[addr & 0xFF];
719 else if (addr >= 0xC300 && addr <= 0xC3FF && !slotC3ROM)
727 WriteLog("b is from $C100-$CFFF block...\n");
730 else if (addr >= 0xC800 && addr <= 0xCFFF) // 2K peripheral or OS ROM
734 else if (addr >= 0xD000)
738 if (addr <= 0xDFFF && visibleBank == LC_BANK_1)
742 // b = ram[addr - 0x1000];
743 b = (altzp ? ram2[addr - 0x1000] : ram[addr - 0x1000]);
746 WriteLog("b is from LC bank #1 (ram[addr - 0x1000])...\n");
754 b = (altzp ? ram2[addr] : ram[addr]);
757 WriteLog("b is from LC bank #2 (ram[addr])...\n");
768 WriteLog("b is from LC ROM (rom[addr])...\n");
774 // 80STORE only works for WRITING, not READING!
776 // Check for 80STORE mode (STORE80 takes precedence over RAMRD/WRT)...
777 if ((((addr >= 0x0400) && (addr <= 0x07FF)) || ((addr >= 0x2000) && (addr <= 0x3FFF))) && store80Mode)
788 // Finally, check for auxillary/altzp write switches
790 b = (altzp ? ram2[addr] : ram[addr]);
792 b = (ramrd ? ram2[addr] : ram[addr]);
795 WriteLog("b is from ram[addr]...\n");
800 if (addr >= 0xD000 && addr <= 0xD00F)
802 WriteLog("*** Read from $%04X: $%02X (readRAM=%s, PC=%04X, ram$D000=%02X)\n", addr, b, (readRAM ? "T" : "F"), mainCPU.pc, ram[0xC000]);
811 APPENDIX F Assembly Language Program Listings
817 5 * ;ADDRESSES FOR FIRST 6522
818 6 ORB EQU $C400 ;PORT B
819 7 ORA EQU $C401 ;PORT A
820 8 DDRB EQU $C402 ;DATA DIRECTION REGISTER (A)
821 9 DDRA EQU $C403 ;DATA DIRECTION REGISTER (B)
822 10 * ;ADDRESSES FOR SECOND 6522
823 11 ORB2 EQU $C480 ;PORT B
824 12 ORA2 EQU $C481 ;PORT A
825 13 DDRB2 EQU $C482 ;DATA DIRECTION REGISTER (B)
826 14 DDRA2 EQU $C483 ;DATA DIRECTION REGISTER (A)
828 void WrMem(uint16_t addr, uint8_t b)
831 //extern V6809REGS regs;
832 //if (addr >= 0xC800 && addr <= 0xCBFE)
833 //if (addr == 0xC80F || addr == 0xC80D)
834 // WriteLog("WrMem: Writing address %04X with %02X [PC=%04X, $CB00=%02X]\n", addr, b, regs.pc, gram[0xCB00]);//*/
837 if (addr >= 0xC000 && addr <= 0xC0FF)
838 WriteLog("\n*** Write at I/O address %04X\n", addr);
841 Check the BIKO version on Asimov to see if it's been cracked or not...
843 7F3D: 29 07 AND #$07 [PC=7F3F, SP=01EA, CC=---B-I--, A=01, X=4B, Y=00]
844 7F3F: C9 06 CMP #$06 [PC=7F41, SP=01EA, CC=N--B-I--, A=01, X=4B, Y=00]
845 7F41: 90 03 BCC $7F46 [PC=7F46, SP=01EA, CC=N--B-I--, A=01, X=4B, Y=00]
846 [7F43: 4C 83 7E JMP $7E83] <- Skipped over... (Prints "THANK YOU VERY MUCH!")
847 7F46: AA TAX [PC=7F47, SP=01EA, CC=---B-I--, A=01, X=01, Y=00]
849 ; INX here *ensures* 1 - 6!!! BUG!!!
850 ; Or is it? Could this be part of a braindead copy protection scheme? It's
851 ; awfully close to NOP ($EA)...
852 ; Nothing else touches it once it's been written... Hmm...
854 7F47: E8 INX [PC=7F48, SP=01EA, CC=---B-I--, A=01, X=02, Y=00]
855 7F48: F8 SED [PC=7F49, SP=01EA, CC=---BDI--, A=01, X=02, Y=00]
856 7F49: 18 CLC [PC=7F4A, SP=01EA, CC=---BDI--, A=01, X=02, Y=00]
857 7F4A: BD 15 4E LDA $4E15,X [PC=7F4D, SP=01EA, CC=---BDI--, A=15, X=02, Y=00]
860 ; 4E15: 25 25 15 15 10 20
861 ; 4E1B: 03 41 99 99 01 00 12
864 7F4D: 65 FC ADC $FC [PC=7F4F, SP=01EA, CC=---BDI--, A=16, X=02, Y=00]
865 7F4F: 65 FC ADC $FC [PC=7F51, SP=01EA, CC=---BDI--, A=17, X=02, Y=00]
866 7F51: 65 FC ADC $FC [PC=7F53, SP=01EA, CC=---BDI--, A=18, X=02, Y=00]
867 7F53: 65 FC ADC $FC [PC=7F55, SP=01EA, CC=---BDI--, A=19, X=02, Y=00]
869 ; NO checking is done on the raised stat! Aarrrgggghhhhh!
871 7F55: 9D 15 4E STA $4E15,X [PC=7F58, SP=01EA, CC=---BDI--, A=19, X=02, Y=00]
872 7F58: D8 CLD [PC=7F59, SP=01EA, CC=---B-I--, A=19, X=02, Y=00]
874 ; Print "ALAKAZAM!" and so on...
876 7F59: 20 2C 40 JSR $402C [PC=402C, SP=01E8, CC=---B-I--, A=19, X=02, Y=00]
880 WriteLog("\n*** Byte %02X written at address %04X\n", b, addr);
883 I think this is IIc/IIe only...
885 CLR80STORE=$C000 ;80STORE Off- disable 80-column memory mapping (Write)
886 SET80STORE=$C001 ;80STORE On- enable 80-column memory mapping (WR-only)
888 CLRAUXRD = $C002 ;read from main 48K (WR-only)
889 SETAUXRD = $C003 ;read from aux/alt 48K (WR-only)
891 CLRAUXWR = $C004 ;write to main 48K (WR-only)
892 SETAUXWR = $C005 ;write to aux/alt 48K (WR-only)
894 CLRCXROM = $C006 ;use ROM on cards (WR-only)
895 SETCXROM = $C007 ;use internal ROM (WR-only)
897 CLRAUXZP = $C008 ;use main zero page, stack, & LC (WR-only)
898 SETAUXZP = $C009 ;use alt zero page, stack, & LC (WR-only)
900 CLRC3ROM = $C00A ;use internal Slot 3 ROM (WR-only)
901 SETC3ROM = $C00B ;use external Slot 3 ROM (WR-only)
903 CLR80VID = $C00C ;disable 80-column display mode (WR-only)
904 SET80VID = $C00D ;enable 80-column display mode (WR-only)
906 CLRALTCH = $C00E ;use main char set- norm LC, Flash UC (WR-only)
907 SETALTCH = $C00F ;use alt char set- norm inverse, LC; no Flash (WR-only)
911 #ifdef SOFT_SWITCH_DEBUGGING
912 WriteLog("80STORE off (write)\n");
916 else if (addr == 0xC001)
918 #ifdef SOFT_SWITCH_DEBUGGING
919 WriteLog("80STORE on (write)\n");
923 else if (addr == 0xC002)
925 #ifdef SOFT_SWITCH_DEBUGGING
926 WriteLog("RAMRD off (write)\n");
930 else if (addr == 0xC003)
932 #ifdef SOFT_SWITCH_DEBUGGING
933 WriteLog("RAMRD on (write)\n");
937 else if (addr == 0xC004)
939 #ifdef SOFT_SWITCH_DEBUGGING
940 WriteLog("RAMWRT off (write)\n");
944 else if (addr == 0xC005)
946 #ifdef SOFT_SWITCH_DEBUGGING
947 WriteLog("RAMWRT on (write)\n");
951 else if (addr == 0xC006)
953 // This is the only soft switch that breaks the usual convention.
954 #ifdef SOFT_SWITCH_DEBUGGING
955 WriteLog("SLOTCXROM on (write)\n");
959 else if (addr == 0xC007)
961 #ifdef SOFT_SWITCH_DEBUGGING
962 WriteLog("SLOTCXROM off (write)\n");
966 else if (addr == 0xC008)
968 #ifdef SOFT_SWITCH_DEBUGGING
969 WriteLog("ALTZP off (write)\n");
973 else if (addr == 0xC009)
975 #ifdef SOFT_SWITCH_DEBUGGING
976 WriteLog("ALTZP on (write)\n");
980 else if (addr == 0xC00A)
982 #ifdef SOFT_SWITCH_DEBUGGING
983 WriteLog("SLOTC3ROM off (write)\n");
987 else if (addr == 0xC00B)
989 #ifdef SOFT_SWITCH_DEBUGGING
990 WriteLog("SLOTC3ROM on (write)\n");
994 else if (addr == 0xC00C)
996 #ifdef SOFT_SWITCH_DEBUGGING
997 WriteLog("80COL off (write)\n");
1001 else if (addr == 0xC00D)
1003 #ifdef SOFT_SWITCH_DEBUGGING
1004 WriteLog("80COL on (write)\n");
1008 else if (addr == 0xC00E)
1010 #ifdef SOFT_SWITCH_DEBUGGING
1011 WriteLog("ALTCHARSET off (write)\n");
1013 alternateCharset = false;
1015 else if (addr == 0xC00F)
1017 #ifdef SOFT_SWITCH_DEBUGGING
1018 WriteLog("ALTCHARSET on (write)\n");
1020 alternateCharset = true;
1022 else if ((addr & 0xFFF0) == 0xC010) // Keyboard strobe
1024 //Actually, according to the A2 ref, this should do nothing since a write
1025 //is immediately preceded by a read leaving it in the same state it was...
1026 //But leaving this out seems to fuck up the key handling of some games...
1029 else if ((addr & 0xFFF0) == 0xC030) // Read $C030-$C03F
1031 //Likewise, the speaker is supposed to do nothing if you write to it, and
1032 //for the same reason. But without this, you get no sound in David's
1036 else if (addr == 0xC050)
1038 #ifdef SOFT_SWITCH_DEBUGGING
1039 WriteLog("TEXT off (write)\n");
1043 else if (addr == 0xC051)
1045 #ifdef SOFT_SWITCH_DEBUGGING
1046 WriteLog("TEXT on (write)\n");
1050 else if (addr == 0xC052)
1052 #ifdef SOFT_SWITCH_DEBUGGING
1053 WriteLog("MIXED off (write)\n");
1057 else if (addr == 0xC053)
1059 #ifdef SOFT_SWITCH_DEBUGGING
1060 WriteLog("MIXED on (write)\n");
1064 else if (addr == 0xC054)
1066 #ifdef SOFT_SWITCH_DEBUGGING
1067 WriteLog("PAGE2 off (write)\n");
1069 displayPage2 = false;
1071 else if (addr == 0xC055)
1073 #ifdef SOFT_SWITCH_DEBUGGING
1074 WriteLog("PAGE2 on (write)\n");
1076 displayPage2 = true;
1078 else if (addr == 0xC056)
1080 #ifdef SOFT_SWITCH_DEBUGGING
1081 WriteLog("HIRES off (write)\n");
1085 else if (addr == 0xC057)
1087 #ifdef SOFT_SWITCH_DEBUGGING
1088 WriteLog("HIRES on (write)\n");
1092 else if (addr == 0xC05E)
1094 #ifdef SOFT_SWITCH_DEBUGGING
1095 WriteLog("DHIRES on (write)\n");
1100 //static int goDumpDis = 0;
1102 //if (goDumpDis == 2)
1105 else if (addr == 0xC05F)
1107 #ifdef SOFT_SWITCH_DEBUGGING
1108 WriteLog("DHIRES off (write)\n");
1113 else if (addr == 0xC07E)
1115 #ifdef SOFT_SWITCH_DEBUGGING
1116 WriteLog("IOUDIS on (write)\n");
1120 else if (addr == 0xC07F)
1122 #ifdef SOFT_SWITCH_DEBUGGING
1123 WriteLog("IOUDIS off (write)\n");
1127 else if ((addr & 0xFFFB) == 0xC080)
1130 WriteLog("LC(R): $C080 49280 OECG R Read RAM bank 2; no write\n");
1132 //$C080 49280 OECG R Read RAM bank 2; no write
1133 visibleBank = LC_BANK_2;
1137 else if ((addr & 0xFFFB) == 0xC081)
1140 WriteLog("LC(R): $C081 49281 OECG RR Read ROM; write RAM bank 2\n");
1142 //$C081 49281 ROMIN OECG RR Read ROM; write RAM bank 2
1143 visibleBank = LC_BANK_2;
1147 else if ((addr & 0xFFFB) == 0xC082)
1150 WriteLog("LC(R): $C082 49282 OECG R Read ROM; no write\n");
1152 //$C082 49282 OECG R Read ROM; no write
1153 visibleBank = LC_BANK_2;
1157 else if ((addr & 0xFFFB) == 0xC083)
1160 WriteLog("LC(R): $C083 49283 OECG RR Read/Write RAM bank 2\n");
1162 //$C083 49283 LCBANK2 OECG RR Read/write RAM bank 2
1163 visibleBank = LC_BANK_2;
1167 else if ((addr & 0xFFFB) == 0xC088)
1170 WriteLog("LC(R): $C088 49288 OECG R Read RAM bank 1; no write\n");
1172 //$C088 49288 OECG R Read RAM bank 1; no write
1173 visibleBank = LC_BANK_1;
1177 else if ((addr & 0xFFFB) == 0xC089)
1180 WriteLog("LC(R): $C089 49289 OECG RR Read ROM; write RAM bank 1\n");
1182 //$C089 49289 OECG RR Read ROM; write RAM bank 1
1183 visibleBank = LC_BANK_1;
1187 else if ((addr & 0xFFFB) == 0xC08A)
1190 WriteLog("LC(R): $C08A 49290 OECG R Read ROM; no write\n");
1192 //$C08A 49290 OECG R Read ROM; no write
1193 visibleBank = LC_BANK_1;
1197 else if ((addr & 0xFFFB) == 0xC08B)
1200 WriteLog("LC(R): $C08B 49291 OECG RR Read/Write RAM bank 1\n");
1202 //$C08B 49291 OECG RR Read/write RAM bank 1
1203 visibleBank = LC_BANK_1;
1207 //This is determined by which slot it is in--this assumes slot 6. !!! FIX !!!
1208 else if ((addr & 0xFFF8) == 0xC0E0)
1210 floppyDrive.ControlStepper(addr & 0x07);
1212 else if ((addr & 0xFFFE) == 0xC0E8)
1214 floppyDrive.ControlMotor(addr & 0x01);
1216 else if ((addr & 0xFFFE) == 0xC0EA)
1218 floppyDrive.DriveEnable(addr & 0x01);
1220 else if (addr == 0xC0EC)
1222 //change this to Write()? (and the other to Read()?) Dunno. Seems to work OK, but still...
1224 floppyDrive.ReadWrite();
1226 else if (addr == 0xC0ED)
1228 floppyDrive.SetLatchValue(b);
1230 else if (addr == 0xC0EE)
1232 floppyDrive.SetReadMode();
1234 else if (addr == 0xC0EF)
1236 floppyDrive.SetWriteMode();
1238 //Still need to add missing I/O switches here...
1240 //DEEE: BD 10 BF LDA $BF10,X [PC=DEF1, SP=01F4, CC=--.B-IZ-, A=00, X=0C, Y=07]
1242 if (addr >= 0xD000 && addr <= 0xD00F)
1244 WriteLog("*** Write to $%04X: $%02X (writeRAM=%s, PC=%04X, ram$D000=%02X)\n", addr, b, (writeRAM ? "T" : "F"), mainCPU.pc, ram[0xC000]);
1247 if (addr >= 0xC000 && addr <= 0xCFFF)
1248 return; // Protect LC bank #1 from being written to!
1255 if (addr <= 0xDFFF && visibleBank == LC_BANK_1)
1256 ram[addr - 0x1000] = b;
1260 if (addr <= 0xDFFF && visibleBank == LC_BANK_1)
1263 ram2[addr - 0x1000] = b;
1265 ram[addr - 0x1000] = b;
1280 // Check for 80STORE mode (STORE80 takes precedence over RAMRD/WRT)...
1281 if ((((addr >= 0x0400) && (addr <= 0x07FF)) || ((addr >= 0x2000) && (addr <= 0x3FFF))) && store80Mode)
1291 // Finally, check for auxillary/altzp write switches
1304 // if (addr < 0x0200 || addr >= 0xD000)
1308 WriteLog("Write $38: $%02X\n", b);
1309 else if (addr == 0x39)
1310 WriteLog("Write $39: $%02X\n", b);
1329 // Load a file into RAM/ROM image space
1331 bool LoadImg(char * filename, uint8_t * ram, int size)
1333 FILE * fp = fopen(filename, "rb");
1338 fread(ram, 1, size, fp);
1345 static void SaveApple2State(const char * filename)
1350 static bool LoadApple2State(const char * filename)
1356 #ifdef CPU_CLOCK_CHECKING
1357 uint8_t counter = 0;
1358 uint32_t totalCPU = 0;
1359 uint64_t lastClock = 0;
1364 int main(int /*argc*/, char * /*argv*/[])
1366 InitLog("./apple2.log");
1368 srand(time(NULL)); // Initialize RNG
1371 memset(ram, 0, 0x10000);
1372 memset(rom, 0, 0x10000);
1373 memset(ram2, 0, 0x10000);
1375 // Set up V65C02 execution context
1376 memset(&mainCPU, 0, sizeof(V65C02REGS));
1377 mainCPU.RdMem = RdMem;
1378 mainCPU.WrMem = WrMem;
1379 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
1381 // alternateCharset = true;
1382 // if (!LoadImg(settings.BIOSPath, rom + 0xD000, 0x3000))
1383 if (!LoadImg(settings.BIOSPath, rom + 0xC000, 0x4000))
1385 WriteLog("Could not open file '%s'!\n", settings.BIOSPath);
1389 //This is now included...
1390 /* if (!LoadImg(settings.diskPath, diskRom, 0x100))
1392 WriteLog("Could not open file '%s'!\nDisk II will be unavailable!\n", settings.diskPath);
1396 //Load up disk image from config file (for now)...
1397 floppyDrive.LoadImage(settings.diskImagePath1, 0);
1398 floppyDrive.LoadImage(settings.diskImagePath2, 1);
1399 // floppyDrive.LoadImage("./disks/temp.nib", 1); // Load temp .nib file into second drive...
1401 //Kill the DOS ROM in slot 6 for now...
1403 // memcpy(rom + 0xC600, diskROM, 0x100);
1404 // memcpy(rom + 0xC700, diskROM, 0x100); // Slot 7???
1406 WriteLog("About to initialize video...\n");
1410 std::cout << "Aborting!" << std::endl;
1414 // Have to do this *after* video init but *before* sound init...!
1415 //Shouldn't be necessary since we're not doing emulation in the ISR...
1416 if (settings.autoStateSaving)
1418 // Load last state from file...
1419 if (!LoadApple2State(settings.autoStatePath))
1420 WriteLog("Unable to use Apple2 state file \"%s\"!\n", settings.autoStatePath);
1426 if (!LoadImg("./BT1_6502_RAM_SPACE.bin", ram, 0x10000))
1428 cout << "Couldn't load state file!" << endl;
1429 cout << "Aborting!!" << endl;
1434 //-- -- -- -- ----- -----
1435 //00 75 3B 53 FD 01 41 44
1437 mainCPU.cpuFlags = 0;
1443 mainCPU.pc = 0x4441;
1447 displayPage2 = false;
1454 memcpy(ram + 0xD000, ram + 0xC000, 0x1000);
1457 WriteLog("About to initialize audio...\n");
1459 //nope SDL_EnableUNICODE(1); // Needed to do key translation shit
1461 // gui = new GUI(surface); // Set up the GUI system object...
1462 // gui = new GUI(mainSurface); // Set up the GUI system object...
1463 // SDL 2... this will likely cause Apple 2 to crash
1464 // gui = new GUI(NULL); // Set up the GUI system object...
1466 gui->AddMenuTitle("Apple2");
1467 gui->AddMenuItem("Test!", TestWindow/*, hotkey*/);
1468 gui->AddMenuItem("");
1469 gui->AddMenuItem("Quit", QuitEmulator, SDLK_q);
1470 gui->CommitItemsToMenu();
1473 SetupBlurTable(); // Set up the color TV emulation blur table
1474 running = true; // Set running status...
1476 InitializeEventList(); // Clear the event list before we use it...
1477 SetCallbackTime(FrameCallback, 16666.66666667); // Set frame to fire at 1/60 s interval
1478 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 s intervals
1479 startTicks = SDL_GetTicks();
1481 #ifdef THREADED_65C02
1482 cpuCond = SDL_CreateCond();
1483 mainSem = SDL_CreateSemaphore(1);
1484 cpuThread = SDL_CreateThread(CPUThreadFunc, NULL, NULL);
1485 //Hmm... CPU does POST (+1), wait, then WAIT (-1)
1486 // SDL_sem * mainMutex = SDL_CreateMutex();
1489 WriteLog("Entering main loop...\n");
1492 double timeToNextEvent = GetTimeToNextEvent();
1493 #ifndef THREADED_65C02
1494 Execute65C02(&mainCPU, USEC_TO_M6502_CYCLES(timeToNextEvent));
1496 //We MUST remove a frame's worth of time in order for the CPU to function... !!! FIX !!!
1497 //(Fix so that this is not a requirement!)
1498 //Fixed, but mainCPU.clock is destroyed in the bargain. Oh well.
1499 // mainCPU.clock -= USEC_TO_M6502_CYCLES(timeToNextEvent);
1501 #ifdef CPU_CLOCK_CHECKING
1502 #ifndef THREADED_65C02
1503 totalCPU += USEC_TO_M6502_CYCLES(timeToNextEvent);
1506 // Handle CPU time delta for sound...
1507 //Don't need this anymore now that we use absolute time...
1508 // AddToSoundTimeBase(USEC_TO_M6502_CYCLES(timeToNextEvent));
1512 #ifdef THREADED_65C02
1513 WriteLog("Main: cpuFinished = true;\n");
1515 //#warning "If sound thread is behind, CPU thread will never wake up... !!! FIX !!!" [DONE]
1516 //What to do? How do you know when the CPU is sleeping???
1517 //USE A CONDITIONAL!!! OF COURSE!!!!!!11!11!11!!!1!
1519 SDL_mutexP(mainMutex);
1520 SDL_CondWait(mainCond, mainMutex); // Wait for CPU thread to get to signal point...
1521 SDL_mutexV(mainMutex);
1523 //Nope, use a semaphore...
1524 WriteLog("Main: SDL_SemWait(mainSem);\n");
1525 SDL_SemWait(mainSem);//should lock until CPU thread is waiting...
1528 WriteLog("Main: SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up\n");
1529 SDL_CondSignal(cpuCond);//thread is probably asleep, wake it up
1530 WriteLog("Main: SDL_WaitThread(cpuThread, NULL);\n");
1531 SDL_WaitThread(cpuThread, NULL);
1532 //nowok:SDL_WaitThread(CPUThreadFunc, NULL);
1533 WriteLog("Main: SDL_DestroyCond(cpuCond);\n");
1534 SDL_DestroyCond(cpuCond);
1536 //SDL_DestroyMutex(mainMutex);
1537 SDL_DestroySemaphore(mainSem);
1540 if (settings.autoStateSaving)
1542 // Save state here...
1543 SaveApple2State(settings.autoStatePath);
1545 floppyDrive.SaveImage();
1561 -----------------------
1562 space $A0 $A0 $A0 $A0 No xlation
1563 RETURN $8D $8D $8D $8D No xlation
1564 0 $B0 $B0 $B0 $B0 Need to screen shift+0 (?)
1565 1! $B1 $B1 $A1 $A1 No xlation
1566 2" $B2 $B2 $A2 $A2 No xlation
1567 3# $B3 $B3 $A3 $A3 No xlation
1568 4$ $B4 $B4 $A4 $A4 No xlation
1569 5% $B5 $B5 $A5 $A5 No xlation
1570 6& $B6 $B6 $A6 $A6 No xlation
1571 7' $B7 $B7 $A7 $A7 No xlation
1572 8( $B8 $B8 $A8 $A8 No xlation
1573 9) $B9 $B9 $A9 $A9 No xlation
1574 :* $BA $BA $AA $AA No xlation
1575 ;+ $BB $BB $AB $AB No xlation
1576 ,< $AC $AC $BC $BC No xlation
1577 -= $AD $AD $BD $BD No xlation
1578 .> $AE $AE $BE $BE No xlation
1579 /? $AF $AF $BF $BF No xlation
1592 M $CD $8D $DD $9D -> ODD
1593 N^ $CE $8E $DE $9E -> ODD
1595 P@ $D0 $90 $C0 $80 Need to xlate CTL+SHFT+P & SHFT+P (?)
1608 ESC $9B $9B $9B $9B No xlation
1611 //static uint64_t lastCPUCycles = 0;
1612 static uint32_t frameCount = 0;
1613 static void FrameCallback(void)
1617 while (SDL_PollEvent(&event))
1622 //Need to do some key translation here, and screen out non-apple keys as well...
1623 //(really, could do it all in SDL_KEYDOWN, would just have to get symbols &
1624 // everything else done separately. this is slightly easier. :-P)
1625 // if (event.key.keysym.sym == SDLK_TAB) // Prelim key screening...
1626 if (event.edit.text[0] == '\t') // Prelim key screening...
1629 lastKeyPressed = event.edit.text[0];
1632 //kludge: should have a caps lock thingy here...
1633 //or all uppercase for ][+...
1634 // if (lastKeyPressed >= 'a' && lastKeyPressed <='z')
1635 // lastKeyPressed &= 0xDF; // Convert to upper case...
1639 // CTRL+RESET key emulation (mapped to CTRL+`)
1640 // This doesn't work...
1641 // if (event.key.keysym.sym == SDLK_BREAK && (event.key.keysym.mod & KMOD_CTRL))
1642 // if (event.key.keysym.sym == SDLK_PAUSE && (event.key.keysym.mod & KMOD_CTRL))
1643 if (event.key.keysym.sym == SDLK_BACKQUOTE && (event.key.keysym.mod & KMOD_CTRL))
1644 //NOTE that this shouldn't take place until the key is lifted... !!! FIX !!!
1645 //ALSO it seems to leave the machine in an inconsistent state vis-a-vis the language card...
1646 mainCPU.cpuFlags |= V65C02_ASSERT_LINE_RESET;
1648 if (event.key.keysym.sym == SDLK_RIGHT)
1649 lastKeyPressed = 0x15, keyDown = true;
1650 else if (event.key.keysym.sym == SDLK_LEFT)
1651 lastKeyPressed = 0x08, keyDown = true;
1652 else if (event.key.keysym.sym == SDLK_UP)
1653 lastKeyPressed = 0x0B, keyDown = true;
1654 else if (event.key.keysym.sym == SDLK_DOWN)
1655 lastKeyPressed = 0x0A, keyDown = true;
1656 else if (event.key.keysym.sym == SDLK_RETURN)
1657 lastKeyPressed = 0x0D, keyDown = true;
1658 else if (event.key.keysym.sym == SDLK_ESCAPE)
1659 lastKeyPressed = 0x1B, keyDown = true;
1660 else if (event.key.keysym.sym == SDLK_BACKSPACE)
1661 lastKeyPressed = 0x7F, keyDown = true;
1663 // Fix CTRL+key combo...
1664 if (event.key.keysym.mod & KMOD_CTRL)
1666 if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
1668 lastKeyPressed = (event.key.keysym.sym - SDLK_a) + 1;
1670 //printf("Key combo pressed: CTRL+%c\n", lastKeyPressed + 0x40);
1674 // Use ALT+Q to exit, as well as the usual window decoration method
1675 if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod & KMOD_ALT))
1678 if (event.key.keysym.sym == SDLK_PAUSE)
1680 pauseMode = !pauseMode;
1685 SpawnMessage("*** PAUSED ***");
1690 SpawnMessage("*** RESUME ***");
1694 // Paddle buttons 0 & 1
1695 if (event.key.keysym.sym == SDLK_INSERT)
1696 openAppleDown = true;
1697 if (event.key.keysym.sym == SDLK_PAGEUP)
1698 closedAppleDown = true;
1700 if (event.key.keysym.sym == SDLK_F11)
1701 dumpDis = !dumpDis; // Toggle the disassembly process
1702 // else if (event.key.keysym.sym == SDLK_F11)
1703 // floppyDrive.LoadImage("./disks/bt1_char.dsk");//Kludge to load char disk...
1704 else if (event.key.keysym.sym == SDLK_F9)
1706 floppyDrive.CreateBlankImage();
1707 // SpawnMessage("Image cleared...");
1709 else if (event.key.keysym.sym == SDLK_F10)
1711 floppyDrive.SwapImages();
1712 // SpawnMessage("Image swapped...");
1715 if (event.key.keysym.sym == SDLK_F2)// Toggle the palette
1717 else if (event.key.keysym.sym == SDLK_F3)// Cycle through screen types
1720 // if (event.key.keysym.sym == SDLK_F5) // Temp GUI launch key
1721 if (event.key.keysym.sym == SDLK_F1) // GUI launch key
1722 //NOTE: Should parse the output to determine whether or not the user requested
1723 // to quit completely... !!! FIX !!!
1726 if (event.key.keysym.sym == SDLK_F5)
1729 char volStr[19] = "[****************]";
1730 // volStr[GetVolume()] = 0;
1731 for(int i=GetVolume(); i<16; i++)
1732 volStr[1 + i] = '-';
1733 SpawnMessage("Volume: %s", volStr);
1735 else if (event.key.keysym.sym == SDLK_F6)
1738 char volStr[19] = "[****************]";
1739 // volStr[GetVolume()] = 0;
1740 for(int i=GetVolume(); i<16; i++)
1741 volStr[1 + i] = '-';
1742 SpawnMessage("Volume: %s", volStr);
1745 static bool fullscreenDebounce = false;
1747 if (event.key.keysym.sym == SDLK_F12)
1749 if (!fullscreenDebounce)
1752 fullscreenDebounce = true;
1759 if (event.key.keysym.sym == SDLK_F12)
1760 fullscreenDebounce = false;
1762 // Paddle buttons 0 & 1
1763 if (event.key.keysym.sym == SDLK_INSERT)
1764 openAppleDown = false;
1765 if (event.key.keysym.sym == SDLK_PAGEUP)
1766 closedAppleDown = false;
1768 // if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
1777 //#warning "!!! Taking MAJOR time hit with the video frame rendering !!!"
1779 SetCallbackTime(FrameCallback, 16666.66666667);
1781 #ifdef CPU_CLOCK_CHECKING
1782 //We know it's stopped, so we can get away with this...
1786 uint64_t clock = GetCurrentV65C02Clock();
1787 //totalCPU += (uint32_t)(clock - lastClock);
1789 printf("Executed %u cycles...\n", (uint32_t)(clock - lastClock));
1795 //Instead of this, we should yield remaining time to other processes... !!! FIX !!! [DONE]
1798 //Actually, slows things down too much...
1800 // while (SDL_GetTicks() - startTicks < 16); // Wait for next frame...
1802 // This is the problem: If you set the interval to 16, it runs faster than
1803 // 1/60s per frame. If you set it to 17, it runs slower. What we need is to
1804 // have it do 16 for one frame, then 17 for two others. Then it should average
1805 // out to 1/60s per frame every 3 frames.
1806 frameCount = (frameCount + 1) % 3;
1808 uint32_t waitFrameTime = 17 - (frameCount == 0 ? 1 : 0);
1810 while (SDL_GetTicks() - startTicks < waitFrameTime)
1811 SDL_Delay(1); // Wait for next frame...
1813 startTicks = SDL_GetTicks();
1815 uint64_t cpuCycles = GetCurrentV65C02Clock();
1816 uint32_t cyclesBurned = (uint32_t)(cpuCycles - lastCPUCycles);
1817 WriteLog("FrameCallback: used %i cycles\n", cyclesBurned);
1818 lastCPUCycles = cpuCycles;
1821 //let's wait, then signal...
1822 //works longer, but then still falls behind...
1823 #ifdef THREADED_65C02
1825 SDL_CondSignal(cpuCond);//OK, let the CPU go another frame...
1830 static void BlinkTimer(void)
1833 SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 sec intervals
1838 Next problem is this: How to have events occur and synchronize with the rest
1841 o Have the CPU thread manage the timer mechanism? (need to have a method of carrying
1842 remainder CPU cycles over...)
1844 One way would be to use a fractional accumulator, then subtract 1 every
1845 time it overflows. Like so:
1847 double overflow = 0;
1851 Execute6808(&soundCPU, time);
1852 overflow += 0.289115646;