]> Shamusworld >> Repos - apple2/blobdiff - src/apple2.cpp
Added infrastructure to handle write protecting floppy disks
[apple2] / src / apple2.cpp
index 7621a2e679edfefe1fb8393a5f59ebacf3b23678..fa7323a6d217adb2a8ddfb32a6a87b32c0432409 100755 (executable)
@@ -14,7 +14,7 @@
 // WHO  WHEN        WHAT
 // ---  ----------  ------------------------------------------------------------
 // JLH  11/12/2005  Initial port to SDL
-// JLH  11/18/2005  Wired up graphic soft switches 
+// JLH  11/18/2005  Wired up graphic soft switches
 // JLH  12/02/2005  Setup timer subsystem for more accurate time keeping
 // JLH  12/12/2005  Added preliminary state saving support
 //
 
 #include "gui/gui.h"
 #include "gui/window.h"
-#include "gui/draggablewindow.h"
+#include "gui/draggablewindow2.h"
 #include "gui/textedit.h"
 
-using namespace std;
+//using namespace std;
 
 // Global variables
 
@@ -62,13 +62,14 @@ uint8 ram[0x10000], rom[0x10000];                           // RAM & ROM spaces
 uint8 diskRom[0x100];                                                  // Disk ROM space
 V65C02REGS mainCPU;
 uint8 appleType = APPLE_TYPE_II;
+FloppyDrive floppyDrive;
 
 // Local variables
 
 static uint8 lastKeyPressed = 0;
 static bool keyDown = false;
 
-static FloppyDrive floppyDrive;
+//static FloppyDrive floppyDrive;
 
 enum { LC_BANK_1, LC_BANK_2 };
 
@@ -98,7 +99,7 @@ static void BlinkTimer(void);
 
 Element * TestWindow(void)
 {
-       Element * win = new DraggableWindow(10, 10, 128, 128);
+       Element * win = new DraggableWindow2(10, 10, 128, 128);
 //     ((DraggableWindow *)win)->AddElement(new TextEdit(4, 16, 92, 0, "u2prog.dsk", win));
 
        return win;
@@ -163,6 +164,23 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFF0) == 0xC030)
        {
+/*
+This is problematic, mainly because the v65C02 removes actual cycles run after each call.
+Therefore, we don't really have a reliable method of sending a timestamp to the sound routine.
+How to fix?
+
+What we need to send is a delta T value but related to the IRQ buffer routine. E.g., if the buffer
+hasn't had any changes in it then we just fill it with the last sample value and are done. Then
+we need to adjust our delta T accordingly. What we could do is keep a running total of time since the
+last change and adjust it accordingly, i.e., whenever a sound IRQ happens.
+How to keep track?
+
+Have deltaT somewhere. Then, whenever there's a toggle, backfill buffer with last spkr state and reset
+deltaT to zero. In the sound IRQ, if deltaT > buffer size, then subtract buffer size from deltaT. (?)
+
+
+
+*/
                ToggleSpeaker(GetCurrentV65C02Clock());
 //should it return something else here???
                return 0x00;
@@ -206,8 +224,45 @@ if (addr >= 0xC080 && addr <= 0xC08F)
 //OK! This switch selects bank 2 of the 4K bank at $D000-$DFFF. One access makes it
 //visible, two makes it R/W.
 
+/*
+301  LDA $E000
+304  PHA
+305  LDA $C081
+308  PLA
+309  PHA
+30A  CMP $E000
+30D  BNE $332
+30F  LDA $C083
+312  LDA $C083
+315  LDA #$A5
+317  STA $D000
+31A  CMP $D000
+31D  BNE $332
+31F  LSR A
+320  STA $D000
+323  CMP $D000
+326  BNE $332
+328  LDA $C081
+32B  LDA $C081
+32E  LDA #$01
+330  BNE $334
+332  LDA #$00
+334  STA $300
+337  PLA
+338  CMP $E000
+33B  BEQ $340
+33D  LDA $C080
+340  RTS
+
+A = PEEK($C082)
+*/
+
+//#define DEBUG_LC
        else if ((addr & 0xFFFB) == 0xC080)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C080 49280 OECG R Read RAM bank 2; no write\n");
+#endif
 //$C080 49280              OECG  R   Read RAM bank 2; no write
                visibleBank = LC_BANK_2;
                readRAM = true;
@@ -215,6 +270,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC081)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C081 49281 OECG RR Read ROM; write RAM bank 2\n");
+#endif
 //$C081 49281 ROMIN        OECG  RR  Read ROM; write RAM bank 2
                visibleBank = LC_BANK_2;
                readRAM = false;
@@ -222,6 +280,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC082)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C082 49282 OECG R Read ROM; no write\n");
+#endif
 //$C082 49282              OECG  R   Read ROM; no write
                visibleBank = LC_BANK_2;
                readRAM = false;
@@ -229,6 +290,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC083)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C083 49283 OECG RR Read/Write RAM bank 2\n");
+#endif
 //$C083 49283 LCBANK2      OECG  RR  Read/write RAM bank 2
                visibleBank = LC_BANK_2;
                readRAM = true;
@@ -236,13 +300,21 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC088)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $%04X 49288 OECG R Read RAM bank 1; no write\n", addr);
+#endif
 //$C088 49288              OECG  R   Read RAM bank 1; no write
                visibleBank = LC_BANK_1;
                readRAM = true;
                writeRAM = false;
+//Hm. Some stuff seems to want this.
+//nope, was looking at $C0E8... return 0xFF;
        }
        else if ((addr & 0xFFFB) == 0xC089)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C089 49289 OECG RR Read ROM; write RAM bank 1\n");
+#endif
 //$C089 49289              OECG  RR  Read ROM; write RAM bank 1
                visibleBank = LC_BANK_1;
                readRAM = false;
@@ -250,6 +322,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC08A)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C08A 49290 OECG R Read ROM; no write\n");
+#endif
 //$C08A 49290              OECG  R   Read ROM; no write
                visibleBank = LC_BANK_1;
                readRAM = false;
@@ -257,6 +332,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        }
        else if ((addr & 0xFFFB) == 0xC08B)
        {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C08B 49291 OECG RR Read/Write RAM bank 1\n");
+#endif
 //$C08B 49291              OECG  RR  Read/write RAM bank 1
                visibleBank = LC_BANK_1;
                readRAM = true;
@@ -277,6 +355,9 @@ if (addr >= 0xC080 && addr <= 0xC08F)
        else if (addr == 0xC0EC)
        {
                return floppyDrive.ReadWrite();
+//Hm, some stuff is looking at the return value. Dunno what it *should* be...
+// OK, it's from the ReadWrite routine...
+//return 0xFF;
        }
        else if (addr == 0xC0ED)
        {
@@ -291,23 +372,76 @@ if (addr >= 0xC080 && addr <= 0xC08F)
                floppyDrive.SetWriteMode();
        }
 
+//#define LC_DEBUGGING
+#ifdef LC_DEBUGGING
+bool showpath = false;
+if (addr >= 0xD000 && addr <= 0xD00F)
+       showpath = true;
+#endif
 //This sux...
        if (addr >= 0xC100 && addr <= 0xCFFF)   // The $C000-$CFFF block is *never* RAM
+#ifdef LC_DEBUGGING
+       {
+#endif
                b = rom[addr];
+#ifdef LC_DEBUGGING
+if (showpath)
+       WriteLog("b is from $C100-$CFFF block...\n");
+       }
+#endif
        else if (addr >= 0xD000)
        {
                if (readRAM)
                {
                        if (addr <= 0xDFFF && visibleBank == LC_BANK_1)
+#ifdef LC_DEBUGGING
+       {
+#endif
                                b = ram[addr - 0x1000];
+#ifdef LC_DEBUGGING
+if (showpath)
+       WriteLog("b is from LC bank #1 (ram[addr - 0x1000])...\n");
+       }
+#endif
                        else
+#ifdef LC_DEBUGGING
+       {
+#endif
                                b = ram[addr];
+#ifdef LC_DEBUGGING
+if (showpath)
+       WriteLog("b is from LC bank #2 (ram[addr])...\n");
+       }
+#endif
                }
                else
+#ifdef LC_DEBUGGING
+       {
+#endif
                        b = rom[addr];
+#ifdef LC_DEBUGGING
+if (showpath)
+       WriteLog("b is from LC ROM (rom[addr])...\n");
+       }
+#endif
        }
        else
+#ifdef LC_DEBUGGING
+       {
+#endif
                b = ram[addr];
+#ifdef LC_DEBUGGING
+if (showpath)
+       WriteLog("b is from ram[addr]...\n");
+       }
+#endif
+
+#ifdef LC_DEBUGGING
+if (addr >= 0xD000 && addr <= 0xD00F)
+{
+       WriteLog("*** Read from $%04X: $%02X (readRAM=%s, PC=%04X, ram$D000=%02X)\n", addr, b, (readRAM ? "T" : "F"), mainCPU.pc, ram[0xC000]);
+}
+#endif
 
        return b;
 }
@@ -316,28 +450,20 @@ if (addr >= 0xC080 && addr <= 0xC08F)
 A-9 (Mockingboard)
 APPENDIX F Assembly Language Program Listings
 
-1      *PRIMARY ROUTINES
-2      *FOR SLOT 4
-3      *
-4                      ORG     $9000
-5      *                               ;ADDRESSES
-                                       FOR FIRST
-                                       6522
-6      ORB             EQU     $C400           ;PORT B
-7      ORA             EQU     $C401           ;PORT A
-8      DDRB            EQU     $C402           ;DATA DIRECTION
-                                                       REGISTER (A)
-9      DDRA            EQU     $C403           ;DATA DIRECTION
-                                                       REGISTER (B)
-10     *                                       ;ADDRESSES
-                                                       FOR SECOND
-                                                       6522
-11     ORB2            EQU     $C480           ;PORT B
-12     ORA2            EQU     $C481           ;PORT A
-13     DDRB2   EQU     $C482           ;DATA DIRECTION
-                                                       REGISTER (B)
-14     DDRA2   EQU     $C483           ;DATA DIRECTION
-                                                       REGISTER (A)
+       1       *PRIMARY ROUTINES
+       2       *FOR SLOT 4
+       3       *
+       4                       ORG     $9000
+       5       *                               ;ADDRESSES FOR FIRST 6522
+       6       ORB             EQU     $C400           ;PORT B
+       7       ORA             EQU     $C401           ;PORT A
+       8       DDRB            EQU     $C402           ;DATA DIRECTION REGISTER (A)
+       9       DDRA            EQU     $C403           ;DATA DIRECTION REGISTER (B)
+       10      *                                       ;ADDRESSES FOR SECOND 6522
+       11      ORB2            EQU     $C480           ;PORT B
+       12      ORA2            EQU     $C481           ;PORT A
+       13      DDRB2   EQU     $C482           ;DATA DIRECTION REGISTER (B)
+       14      DDRA2   EQU     $C483           ;DATA DIRECTION REGISTER (A)
 */
 void WrMem(uint16 addr, uint8 b)
 {
@@ -428,6 +554,9 @@ SETALTCH = $C00F ;use alt char set- norm inverse, LC; no Flash (WR-only)
        }
        else if ((addr & 0xFFF0) == 0xC010)             // Keyboard strobe
        {
+//Actually, according to the A2 ref, this should do nothing since a write
+//is immediately preceded by a read leaving it in the same state it was...
+//But leaving this out seems to fuck up the key handling of some games...
                keyDown = false;
        }
        else if (addr == 0xC050)
@@ -462,6 +591,87 @@ SETALTCH = $C00F ;use alt char set- norm inverse, LC; no Flash (WR-only)
        {
                hiRes = true;
        }
+       else if ((addr & 0xFFFB) == 0xC080)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C080 49280 OECG R Read RAM bank 2; no write\n");
+#endif
+//$C080 49280              OECG  R   Read RAM bank 2; no write
+               visibleBank = LC_BANK_2;
+               readRAM = true;
+               writeRAM = false;
+       }
+       else if ((addr & 0xFFFB) == 0xC081)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C081 49281 OECG RR Read ROM; write RAM bank 2\n");
+#endif
+//$C081 49281 ROMIN        OECG  RR  Read ROM; write RAM bank 2
+               visibleBank = LC_BANK_2;
+               readRAM = false;
+               writeRAM = true;
+       }
+       else if ((addr & 0xFFFB) == 0xC082)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C082 49282 OECG R Read ROM; no write\n");
+#endif
+//$C082 49282              OECG  R   Read ROM; no write
+               visibleBank = LC_BANK_2;
+               readRAM = false;
+               writeRAM = false;
+       }
+       else if ((addr & 0xFFFB) == 0xC083)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C083 49283 OECG RR Read/Write RAM bank 2\n");
+#endif
+//$C083 49283 LCBANK2      OECG  RR  Read/write RAM bank 2
+               visibleBank = LC_BANK_2;
+               readRAM = true;
+               writeRAM = true;
+       }
+       else if ((addr & 0xFFFB) == 0xC088)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C088 49288 OECG R Read RAM bank 1; no write\n");
+#endif
+//$C088 49288              OECG  R   Read RAM bank 1; no write
+               visibleBank = LC_BANK_1;
+               readRAM = true;
+               writeRAM = false;
+       }
+       else if ((addr & 0xFFFB) == 0xC089)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C089 49289 OECG RR Read ROM; write RAM bank 1\n");
+#endif
+//$C089 49289              OECG  RR  Read ROM; write RAM bank 1
+               visibleBank = LC_BANK_1;
+               readRAM = false;
+               writeRAM = true;
+       }
+       else if ((addr & 0xFFFB) == 0xC08A)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C08A 49290 OECG R Read ROM; no write\n");
+#endif
+//$C08A 49290              OECG  R   Read ROM; no write
+               visibleBank = LC_BANK_1;
+               readRAM = false;
+               writeRAM = false;
+       }
+       else if ((addr & 0xFFFB) == 0xC08B)
+       {
+#ifdef DEBUG_LC
+WriteLog("LC(R): $C08B 49291 OECG RR Read/Write RAM bank 1\n");
+#endif
+//$C08B 49291              OECG  RR  Read/write RAM bank 1
+               visibleBank = LC_BANK_1;
+               readRAM = true;
+               writeRAM = true;
+       }
+//This is determined by which slot it is in--this assumes slot 6. !!! FIX !!!
        else if ((addr & 0xFFF8) == 0xC0E0)
        {
                floppyDrive.ControlStepper(addr & 0x07);
@@ -493,6 +703,16 @@ SETALTCH = $C00F ;use alt char set- norm inverse, LC; no Flash (WR-only)
        }
 //Still need to add missing I/O switches here...
 
+//DEEE: BD 10 BF       LDA   $BF10,X    [PC=DEF1, SP=01F4, CC=--.B-IZ-, A=00, X=0C, Y=07]
+#if 0
+if (addr >= 0xD000 && addr <= 0xD00F)
+{
+       WriteLog("*** Write to $%04X: $%02X (writeRAM=%s, PC=%04X, ram$D000=%02X)\n", addr, b, (writeRAM ? "T" : "F"), mainCPU.pc, ram[0xC000]);
+}
+#endif
+       if (addr >= 0xC000 && addr <= 0xCFFF)
+               return; // Protect LC bank #1 from being written to!
+
        if (addr >= 0xD000)
        {
                if (writeRAM)
@@ -579,7 +799,7 @@ int main(int /*argc*/, char * /*argv*/[])
        WriteLog("About to initialize video...\n");
        if (!InitVideo())
        {
-               cout << "Aborting!" << endl;
+               std::cout << "Aborting!" << std::endl;
                return -1;
        }
 
@@ -630,12 +850,15 @@ memcpy(ram + 0xD000, ram + 0xC000, 0x1000);
        SoundInit();
        SDL_EnableUNICODE(1);                                           // Needed to do key translation shit
 
-       gui = new GUI(surface);                                         // Set up the GUI system object...
+//     gui = new GUI(surface);                                         // Set up the GUI system object...
+       gui = new GUI(mainSurface);                                     // Set up the GUI system object...
+#if 0
        gui->AddMenuTitle("Apple2");
        gui->AddMenuItem("Test!", TestWindow/*, hotkey*/);
        gui->AddMenuItem("");
        gui->AddMenuItem("Quit", QuitEmulator, SDLK_q);
        gui->CommitItemsToMenu();
+#endif
 
        SetupBlurTable();                                                       // Set up the color TV emulation blur table
        running = true;                                                         // Set running status...
@@ -652,7 +875,10 @@ memcpy(ram + 0xD000, ram + 0xC000, 0x1000);
                Execute65C02(&mainCPU, USEC_TO_M6502_CYCLES(timeToNextEvent));
 //We MUST remove a frame's worth of time in order for the CPU to function... !!! FIX !!!
 //(Fix so that this is not a requirement!)
-               mainCPU.clock -= USEC_TO_M6502_CYCLES(timeToNextEvent);
+//Fixed, but mainCPU.clock is destroyed in the bargain. Oh well.
+//             mainCPU.clock -= USEC_TO_M6502_CYCLES(timeToNextEvent);
+               // Handle CPU time delta for sound...
+               AddToSoundTimeBase(USEC_TO_M6502_CYCLES(timeToNextEvent));
                HandleNextEvent();
        }
 
@@ -799,10 +1025,13 @@ else if (event.key.keysym.sym == SDLK_F10)
                }
        }
 
-       HandleSoundAtFrameEdge();                                       // Sound stuff... (ick)
+//ick. HandleSoundAtFrameEdge();                                       // Sound stuff... (ick)
        RenderVideoFrame();
        SetCallbackTime(FrameCallback, 16666.66666667);
 
+//Instead of this, we should yield remaining time to other processes... !!! FIX !!!
+//lessee...
+//nope. SDL_Delay(10);
        while (SDL_GetTicks() - startTicks < 16);       // Wait for next frame...
        startTicks = SDL_GetTicks();
 }