]> Shamusworld >> Repos - virtualjaguar/commitdiff
New timer based execution code
authorShamus Hammons <jlhamm@acm.org>
Fri, 4 Feb 2005 06:27:28 +0000 (06:27 +0000)
committerShamus Hammons <jlhamm@acm.org>
Fri, 4 Feb 2005 06:27:28 +0000 (06:27 +0000)
src/jaguar.cpp
src/jerry.cpp
src/tom.cpp

index d4ff90b90db1a037a81f77d852b1f81e39f209a8..d2817df12e65bd301980ee76bbd4bece70037121 100644 (file)
@@ -12,6 +12,9 @@
 #include "video.h"
 #include "settings.h"
 //#include "m68kdasmAG.h"
+#include "clock.h"
+#include <SDL.h>
+#include "SDL_opengl.h"
 
 #define CPU_DEBUG
 //Do this in makefile??? Yes! Could, but it's easier to define here...
@@ -620,7 +623,7 @@ uint32 jaguar_get_handler(uint32 i)
        return JaguarReadLong(i * 4);
 }
 
-uint32 jaguar_interrupt_handler_is_valid(uint32 i)
+uint32 jaguar_interrupt_handler_is_valid(uint32 i) // Debug use only...
 {
        uint32 handler = jaguar_get_handler(i);
        if (handler && (handler != 0xFFFFFFFF))
@@ -1045,8 +1048,13 @@ void jaguar_init(void)
        CDROMInit();
 }
 
+//New timer based code stuffola...
+void ScanlineCallback(void);
+void RenderCallback(void);
+extern uint32 * backbuffer;
 void jaguar_reset(void)
 {
+//NOTE: This causes a (virtual) crash if this is set in the config but not found... !!! FIX !!!
        if (vjs.useJaguarBIOS)
                memcpy(jaguar_mainRam, jaguar_bootRom, 8);
        else
@@ -1060,6 +1068,14 @@ void jaguar_reset(void)
        CDROMReset();
     m68k_pulse_reset();                                                                // Reset the 68000
        WriteLog("Jaguar: 68K reset. PC=%06X SP=%08X\n", m68k_get_reg(NULL, M68K_REG_PC), m68k_get_reg(NULL, M68K_REG_A7));
+
+       // New timer base code stuffola...
+       InitializeEventList();
+       TOMResetBackbuffer(backbuffer);
+//     SetCallbackTime(ScanlineCallback, 63.5555);
+       SetCallbackTime(ScanlineCallback, 31.77775);
+//     SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
+//     SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time
 }
 
 void jaguar_done(void)
@@ -1158,9 +1174,10 @@ void jaguar_done(void)
 //
 void JaguarExecute(uint32 * backbuffer, bool render)
 {
-       uint16 vp = TOMReadWord(0xF0003E) + 1;//Hmm. This is a WO register. Will work? Looks like. But wrong behavior!
-       uint16 vi = TOMReadWord(0xF0004E);//Another WO register...
+       uint16 vp = TOMReadWord(0xF0003E) + 1;
+       uint16 vi = TOMReadWord(0xF0004E);
 //Using WO registers is OK, since we're the ones controlling access--there's nothing wrong here! ;-)
+//Though we shouldn't be able to do it using TOMReadWord... !!! FIX !!!
 
 //     uint16 vdb = TOMReadWord(0xF00046);
 //Note: This is the *definite* end of the display, though VDE *might* be less than this...
@@ -1171,6 +1188,9 @@ void JaguarExecute(uint32 * backbuffer, bool render)
 //     uint16 vde = TOMReadWord(0xF00048);
 
        uint16 refreshRate = (vjs.hardwareTypeNTSC ? 60 : 50);
+//Not sure the above is correct, since the number of lines and timings given in the JTRM
+//seem to indicate the refresh rate is *half* the above...
+//     uint16 refreshRate = (vjs.hardwareTypeNTSC ? 30 : 25);
        // Should these be hardwired or read from VP? Yes, from VP!
        uint32 M68KCyclesPerScanline
                = (vjs.hardwareTypeNTSC ? M68K_CLOCK_RATE_NTSC : M68K_CLOCK_RATE_PAL) / (vp * refreshRate);
@@ -1251,3 +1271,100 @@ uint8 * GetRamPtr(void)
 {
        return jaguar_mainRam;
 }
+
+//
+// New Jaguar execution stack
+//
+
+void JaguarExecuteNew(void)
+{
+       extern bool finished, showGUI;
+       extern bool debounceRunKey;
+       // Pass a message to the "joystick" code to debounce the ESC key...
+       debounceRunKey = true;
+       finished = false;
+/*     InitializeEventList();
+       TOMResetBackbuffer(backbuffer);
+//     SetCallbackTime(ScanlineCallback, 63.5555);
+       SetCallbackTime(ScanlineCallback, 31.77775);
+//     SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
+//     SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time//*/
+//     uint8 * keystate = SDL_GetKeyState(NULL);
+
+       do
+       {
+               double timeToNextEvent = GetTimeToNextEvent();
+//WriteLog("JEN: Time to next event (%u) is %f usec (%u RISC cycles)...\n", nextEvent, timeToNextEvent, USEC_TO_RISC_CYCLES(timeToNextEvent));
+
+               m68k_execute(USEC_TO_M68K_CYCLES(timeToNextEvent));
+               gpu_exec(USEC_TO_RISC_CYCLES(timeToNextEvent));
+
+               if (vjs.DSPEnabled)
+               {
+                       if (vjs.usePipelinedDSP)
+                               DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent));        // Pipelined DSP execution (3 stage)...
+                       else
+                               DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent));          // Ordinary non-pipelined DSP
+               }
+
+               HandleNextEvent();
+
+//             if (keystate[SDLK_ESCAPE])
+//                     break;
+
+//         SDL_PumpEvents();   // Needed to keep the keystate current...
+       }
+       while (!finished);
+}
+
+void ScanlineCallback(void)
+{
+       uint16 vc = TOMReadWord(0xF00006);
+       uint16 vp = TOMReadWord(0xF0003E) + 1;
+       uint16 vi = TOMReadWord(0xF0004E);
+//     uint16 vbb = TOMReadWord(0xF00040);
+       vc++;
+
+       if (vc >= vp)
+               vc = 0;
+
+//WriteLog("SLC: Currently on line %u (VP=%u)...\n", vc, vp);
+       TOMWriteWord(0xF00006, vc);
+
+//This is a crappy kludge, but maybe it'll work for now...
+//Maybe it's not so bad, since the IRQ happens on a scanline boundary...
+       if (vc == vi && vc > 0 && tom_irq_enabled(IRQ_VBLANK))  // Time for Vertical Interrupt?
+       {
+               // We don't have to worry about autovectors & whatnot because the Jaguar
+               // tells you through its HW registers who sent the interrupt...
+               tom_set_pending_video_int();
+               m68k_set_irq(7);
+       }
+
+       TOMExecScanline(vc, true);
+
+//Change this to VBB???
+//Doesn't seem to matter (at least for Flip Out & I-War)
+       if (vc == 0)
+//     if (vc == vbb)
+       {
+joystick_exec();
+
+               RenderBackbuffer();
+               TOMResetBackbuffer(backbuffer);
+       }//*/
+
+//     if (vc == 0)
+//             TOMResetBackbuffer(backbuffer);
+
+//     SetCallbackTime(ScanlineCallback, 63.5555);
+       SetCallbackTime(ScanlineCallback, 31.77775);
+}
+
+void RenderCallback(void)
+{
+       RenderBackbuffer();
+       TOMResetBackbuffer(backbuffer);
+//     SetCallbackTime(RenderCallback, 33303.082);     // # Scanlines * scanline time
+       SetCallbackTime(RenderCallback, 16651.541);     // # Scanlines * scanline time
+}
index 10b11a2a839429af9de02a5933cc109bab215a41..d569dad7b5a95faafe2f475386c8fb37d0a42dcf 100644 (file)
 //     ------------------------------------------------------------
 
 //#include <math.h>
+#include "jaguar.h"
 #include "wavetable.h"
 #include "jerry.h"
+#include "clock.h"
 
+//Note that 44100 Hz requires samples every 22.675737 usec.
+#define NEW_TIMER_SYSTEM
 //#define JERRY_DEBUG
 
 /*static*/ uint8 * jerry_ram_8;
 
 uint8 analog_x, analog_y;
 
-static uint32 jerry_timer_1_prescaler;
-static uint32 jerry_timer_2_prescaler;
-static uint32 jerry_timer_1_divider;
-static uint32 jerry_timer_2_divider;
+static uint32 JERRYPIT1Prescaler;
+static uint32 JERRYPIT1Divider;
+static uint32 JERRYPIT2Prescaler;
+static uint32 JERRYPIT2Divider;
 static int32 jerry_timer_1_counter;
 static int32 jerry_timer_2_counter;
 
@@ -168,11 +172,22 @@ static int32 jerry_i2s_interrupt_timer = -1;
 uint32 jerryI2SCycles;
 uint32 jerryIntPending;
 
+// Private function prototypes
+
+void JERRYResetPIT1(void);
+void JERRYResetPIT2(void);
+void JERRYResetI2S(void);
+
+void JERRYPIT1Callback(void);
+void JERRYPIT2Callback(void);
+void JERRYI2SCallback(void);
+
 //This approach is probably wrong, since the timer is continuously counting down, though
 //it might only be a problem if the # of interrupts generated is greater than 1--the M68K's
 //timeslice should be running during that phase... (The DSP needs to be aware of this!)
 void jerry_i2s_exec(uint32 cycles)
 {
+#ifndef NEW_TIMER_SYSTEM
        extern uint16 serialMode;                                               // From DAC.CPP
        if (serialMode & 0x01)                                                  // INTERNAL flag (JERRY is master)
        {
@@ -224,9 +239,40 @@ void jerry_i2s_exec(uint32 cycles)
                        jerry_i2s_interrupt_timer += 602;
                }
        }
+#else
+       RemoveCallback(JERRYI2SCallback);
+       JERRYI2SCallback();
+#endif
+}
+
+void JERRYExecPIT(uint32 cycles)
+{
+//This is wrong too: Counters are *always* spinning! !!! FIX !!! [DONE]
+//     if (jerry_timer_1_counter)
+               jerry_timer_1_counter -= cycles;
+
+       if (jerry_timer_1_counter <= 0)
+       {
+//Also, it can generate a CPU interrupt as well... !!! FIX !!! or does it? Maybe it goes Timer->GPU->CPU?
+               DSPSetIRQLine(DSPIRQ_TIMER0, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
+//             JERRYResetPIT1();
+               jerry_timer_1_counter += (JERRYPIT1Prescaler + 1) * (JERRYPIT1Divider + 1);
+       }
+
+//This is wrong too: Counters are *always* spinning! !!! FIX !!! [DONE]
+//     if (jerry_timer_2_counter)
+               jerry_timer_2_counter -= cycles;
+
+       if (jerry_timer_2_counter <= 0)
+       {
+//Also, it can generate a CPU interrupt as well... !!! FIX !!! or does it? Maybe it goes Timer->GPU->CPU?
+               DSPSetIRQLine(DSPIRQ_TIMER1, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
+//             JERRYResetPIT2();
+               jerry_timer_2_counter += (JERRYPIT2Prescaler + 1) * (JERRYPIT2Divider + 1);
+       }
 }
 
-void jerry_reset_i2s_timer(void)
+void JERRYResetI2S(void)
 {
        //WriteLog("i2s: reseting\n");
 //This is really SCLK... !!! FIX !!!
@@ -234,85 +280,138 @@ void jerry_reset_i2s_timer(void)
        jerry_i2s_interrupt_timer = -1;
 }
 
-void jerry_reset_timer_1(void)
+void JERRYResetPIT1(void)
 {
-/*     if (!jerry_timer_1_prescaler || !jerry_timer_1_divider)
+#ifndef NEW_TIMER_SYSTEM
+/*     if (!JERRYPIT1Prescaler || !JERRYPIT1Divider)
                jerry_timer_1_counter = 0;
        else//*/
 //Small problem with this approach: Overflow if both are = $FFFF. !!! FIX !!!
-               jerry_timer_1_counter = (jerry_timer_1_prescaler + 1) * (jerry_timer_1_divider + 1);
+               jerry_timer_1_counter = (JERRYPIT1Prescaler + 1) * (JERRYPIT1Divider + 1);
 
 //     if (jerry_timer_1_counter)
 //             WriteLog("jerry: reseting timer 1 to 0x%.8x (%i)\n",jerry_timer_1_counter,jerry_timer_1_counter);
+
+#else
+       RemoveCallback(JERRYPIT1Callback);
+       double usecs = (float)(JERRYPIT1Prescaler + 1) * (float)(JERRYPIT1Divider + 1) * RISC_CYCLE_IN_USEC;
+       SetCallbackTime(JERRYPIT1Callback, usecs);
+#endif
 }
 
-void jerry_reset_timer_2(void)
+void JERRYResetPIT2(void)
 {
-/*     if (!jerry_timer_2_prescaler || !jerry_timer_2_divider)
+#ifndef NEW_TIMER_SYSTEM
+/*     if (!JERRYPIT2Prescaler || !JERRYPIT2Divider)
        {
                jerry_timer_2_counter = 0;
                return;
        }
        else//*/
-               jerry_timer_2_counter = (jerry_timer_2_prescaler + 1) * (jerry_timer_2_divider + 1);
+               jerry_timer_2_counter = (JERRYPIT2Prescaler + 1) * (JERRYPIT2Divider + 1);
 
 //     if (jerry_timer_2_counter)
 //             WriteLog("jerry: reseting timer 2 to 0x%.8x (%i)\n",jerry_timer_2_counter,jerry_timer_2_counter);
+
+#else
+       RemoveCallback(JERRYPIT2Callback);
+       double usecs = (float)(JERRYPIT2Prescaler + 1) * (float)(JERRYPIT2Divider + 1) * RISC_CYCLE_IN_USEC;
+       SetCallbackTime(JERRYPIT2Callback, usecs);
+#endif
 }
 
-void JERRYExecPIT(uint32 cycles)
+void JERRYPIT1Callback(void)
 {
-//This is wrong too: Counters are *always* spinning! !!! FIX !!! [DONE]
-//     if (jerry_timer_1_counter)
-               jerry_timer_1_counter -= cycles;
+       DSPSetIRQLine(DSPIRQ_TIMER0, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
+       JERRYResetPIT1();
+}
 
-       if (jerry_timer_1_counter <= 0)
-       {
-//Also, it can generate a CPU interrupt as well... !!! FIX !!! or does it? Maybe it goes Timer->GPU->CPU?
-               DSPSetIRQLine(DSPIRQ_TIMER0, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
-//             jerry_reset_timer_1();
-               jerry_timer_1_counter += (jerry_timer_1_prescaler + 1) * (jerry_timer_1_divider + 1);
-       }
+void JERRYPIT2Callback(void)
+{
+       DSPSetIRQLine(DSPIRQ_TIMER1, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
+       JERRYResetPIT2();
+}
 
-//This is wrong too: Counters are *always* spinning! !!! FIX !!! [DONE]
-//     if (jerry_timer_2_counter)
-               jerry_timer_2_counter -= cycles;
+void JERRYI2SCallback(void)
+{
+       // Why is it called this? Instead of SCLK? Shouldn't this be read from DAC.CPP???
+//Yes, it should. !!! FIX !!!
+       jerry_i2s_interrupt_divide &= 0xFF;
+       // We don't have to divide the RISC clock rate by this--the reason is a bit
+       // convoluted. Will put explanation here later...
+// What's needed here is to find the ratio of the frequency to the number of clock cycles
+// in one second. For example, if the sample rate is 44100, we divide the clock rate by
+// this: 26590906 / 44100 = 602 cycles.
+// Which means, every 602 cycles that go by we have to generate an interrupt.
+       jerryI2SCycles = 32 * (2 * (jerry_i2s_interrupt_divide + 1));
 
-       if (jerry_timer_2_counter <= 0)
+//This should be in this file with an extern reference in the header file so that
+//DAC.CPP can see it... !!! FIX !!!
+       extern uint16 serialMode;                                               // From DAC.CPP
+
+       if (serialMode & 0x01)                                                  // INTERNAL flag (JERRY is master)
        {
-//Also, it can generate a CPU interrupt as well... !!! FIX !!! or does it? Maybe it goes Timer->GPU->CPU?
-               DSPSetIRQLine(DSPIRQ_TIMER1, ASSERT_LINE);      // This does the 'IRQ enabled' checking...
-//             jerry_reset_timer_2();
-               jerry_timer_2_counter += (jerry_timer_2_prescaler + 1) * (jerry_timer_2_divider + 1);
+               DSPSetIRQLine(DSPIRQ_SSI, ASSERT_LINE);         // This does the 'IRQ enabled' checking...
+               double usecs = (float)jerryI2SCycles * RISC_CYCLE_IN_USEC;
+               SetCallbackTime(JERRYI2SCallback, usecs);
+       }
+       else                                                                                    // JERRY is slave to external word clock
+       {
+//Note that 44100 Hz requires samples every 22.675737 usec.
+//When JERRY is slave to the word clock, we need to do interrupts either at 44.1K
+//sample rate or at a 88.2K sample rate (11.332... usec).
+/*             // This is just a temporary kludge to see if the CD bus mastering works
+               // I.e., this is totally faked...!
+// The whole interrupt system is pretty much borked and is need of an overhaul.
+// What we need is a way of handling these interrupts when they happen instead of
+// scanline boundaries the way it is now.
+               jerry_i2s_interrupt_timer -= cycles;
+               if (jerry_i2s_interrupt_timer <= 0)
+               {
+//This is probably wrong as well (i.e., need to check enable lines)... !!! FIX !!! [DONE]
+                       if (ButchIsReadyToSend())//Not sure this is right spot to check...
+                       {
+//     return GetWordFromButchSSI(offset, who);
+                               SetSSIWordsXmittedFromButch();
+                               DSPSetIRQLine(DSPIRQ_SSI, ASSERT_LINE);
+                       }
+                       jerry_i2s_interrupt_timer += 602;
+               }*/
        }
 }
 
+
 void jerry_init(void)
 {
 //     clock_init();
-       anajoy_init();
+//     anajoy_init();
        joystick_init();
        DACInit();
 //This should be handled with the cart initialization...
 //     eeprom_init();
        memory_malloc_secure((void **)&jerry_ram_8, 0x10000, "JERRY RAM/ROM");
        memcpy(&jerry_ram_8[0xD000], wave_table, 0x1000);
+
+       JERRYPIT1Prescaler = 0xFFFF;
+       JERRYPIT2Prescaler = 0xFFFF;
+       JERRYPIT1Divider = 0xFFFF;
+       JERRYPIT2Divider = 0xFFFF;
 }
 
 void jerry_reset(void)
 {
 //     clock_reset();
-       anajoy_reset();
+//     anajoy_reset();
        joystick_reset();
        eeprom_reset();
-       jerry_reset_i2s_timer();
+       JERRYResetI2S();
        DACReset();
 
        memset(jerry_ram_8, 0x00, 0xD000);              // Don't clear out the Wavetable ROM...!
-       jerry_timer_1_prescaler = 0xFFFF;
-       jerry_timer_2_prescaler = 0xFFFF;
-       jerry_timer_1_divider = 0xFFFF;
-       jerry_timer_2_divider = 0xFFFF;
+       JERRYPIT1Prescaler = 0xFFFF;
+       JERRYPIT2Prescaler = 0xFFFF;
+       JERRYPIT1Divider = 0xFFFF;
+       JERRYPIT2Divider = 0xFFFF;
        jerry_timer_1_counter = 0;
        jerry_timer_2_counter = 0;
 }
@@ -322,7 +421,7 @@ void jerry_done(void)
        WriteLog("JERRY: M68K Interrupt control ($F10020) = %04X\n", GET16(jerry_ram_8, 0x20));
        memory_free(jerry_ram_8);
 //     clock_done();
-       anajoy_done();
+//     anajoy_done();
        joystick_done();
        DACDone();
        eeprom_done();
@@ -363,46 +462,52 @@ uint8 JERRYReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
 //     else if (offset >= 0xF10000 && offset <= 0xF10007)
 //This is still wrong. What needs to be returned here are the values being counted down
 //in the jerry_timer_n_counter variables... !!! FIX !!! [DONE]
+
+//This is probably the problem with the new timer code... This is invalid
+//under the new system... !!! FIX !!!
        else if ((offset >= 0xF10036) && (offset <= 0xF1003D))
        {
-//             jerry_timer_1_counter = (jerry_timer_1_prescaler + 1) * (jerry_timer_1_divider + 1);
-               uint32 counter1Hi = (jerry_timer_1_counter / (jerry_timer_1_divider + 1)) - 1;
-               uint32 counter1Lo = (jerry_timer_1_counter % (jerry_timer_1_divider + 1)) - 1;
-               uint32 counter2Hi = (jerry_timer_2_counter / (jerry_timer_2_divider + 1)) - 1;
-               uint32 counter2Lo = (jerry_timer_2_counter % (jerry_timer_2_divider + 1)) - 1;
+#ifndef NEW_TIMER_SYSTEM
+//             jerry_timer_1_counter = (JERRYPIT1Prescaler + 1) * (JERRYPIT1Divider + 1);
+               uint32 counter1Hi = (jerry_timer_1_counter / (JERRYPIT1Divider + 1)) - 1;
+               uint32 counter1Lo = (jerry_timer_1_counter % (JERRYPIT1Divider + 1)) - 1;
+               uint32 counter2Hi = (jerry_timer_2_counter / (JERRYPIT2Divider + 1)) - 1;
+               uint32 counter2Lo = (jerry_timer_2_counter % (JERRYPIT2Divider + 1)) - 1;
 
                switch(offset & 0x0F)
                {
                case 6:
-//                     return jerry_timer_1_prescaler >> 8;
+//                     return JERRYPIT1Prescaler >> 8;
                        return counter1Hi >> 8;
                case 7:
-//                     return jerry_timer_1_prescaler & 0xFF;
+//                     return JERRYPIT1Prescaler & 0xFF;
                        return counter1Hi & 0xFF;
                case 8:
-//                     return jerry_timer_1_divider >> 8;
+//                     return JERRYPIT1Divider >> 8;
                        return counter1Lo >> 8;
                case 9:
-//                     return jerry_timer_1_divider & 0xFF;
+//                     return JERRYPIT1Divider & 0xFF;
                        return counter1Lo & 0xFF;
                case 10:
-//                     return jerry_timer_2_prescaler >> 8;
+//                     return JERRYPIT2Prescaler >> 8;
                        return counter2Hi >> 8;
                case 11:
-//                     return jerry_timer_2_prescaler & 0xFF;
+//                     return JERRYPIT2Prescaler & 0xFF;
                        return counter2Hi & 0xFF;
                case 12:
-//                     return jerry_timer_2_divider >> 8;
+//                     return JERRYPIT2Divider >> 8;
                        return counter2Lo >> 8;
                case 13:
-//                     return jerry_timer_2_divider & 0xFF;
+//                     return JERRYPIT2Divider & 0xFF;
                        return counter2Lo & 0xFF;
                }
+#else
+#endif
        }
 //     else if (offset >= 0xF10010 && offset <= 0xF10015)
 //             return clock_byte_read(offset);
-       else if (offset >= 0xF17C00 && offset <= 0xF17C01)
-               return anajoy_byte_read(offset);
+//     else if (offset >= 0xF17C00 && offset <= 0xF17C01)
+//             return anajoy_byte_read(offset);
        else if (offset >= 0xF14000 && offset <= 0xF14003)
                return joystick_byte_read(offset) | eeprom_byte_read(offset);
        else if (offset >= 0xF14000 && offset <= 0xF1A0FF)
@@ -437,25 +542,25 @@ uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
 //in the jerry_timer_n_counter variables... !!! FIX !!! [DONE]
        else if ((offset >= 0xF10036) && (offset <= 0xF1003D))
        {
-//             jerry_timer_1_counter = (jerry_timer_1_prescaler + 1) * (jerry_timer_1_divider + 1);
-               uint32 counter1Hi = (jerry_timer_1_counter / (jerry_timer_1_divider + 1)) - 1;
-               uint32 counter1Lo = (jerry_timer_1_counter % (jerry_timer_1_divider + 1)) - 1;
-               uint32 counter2Hi = (jerry_timer_2_counter / (jerry_timer_2_divider + 1)) - 1;
-               uint32 counter2Lo = (jerry_timer_2_counter % (jerry_timer_2_divider + 1)) - 1;
+//             jerry_timer_1_counter = (JERRYPIT1Prescaler + 1) * (JERRYPIT1Divider + 1);
+               uint32 counter1Hi = (jerry_timer_1_counter / (JERRYPIT1Divider + 1)) - 1;
+               uint32 counter1Lo = (jerry_timer_1_counter % (JERRYPIT1Divider + 1)) - 1;
+               uint32 counter2Hi = (jerry_timer_2_counter / (JERRYPIT2Divider + 1)) - 1;
+               uint32 counter2Lo = (jerry_timer_2_counter % (JERRYPIT2Divider + 1)) - 1;
 
                switch(offset & 0x0F)
                {
                case 6:
-//                     return jerry_timer_1_prescaler;
+//                     return JERRYPIT1Prescaler;
                        return counter1Hi;
                case 8:
-//                     return jerry_timer_1_divider;
+//                     return JERRYPIT1Divider;
                        return counter1Lo;
                case 10:
-//                     return jerry_timer_2_prescaler;
+//                     return JERRYPIT2Prescaler;
                        return counter2Hi;
                case 12:
-//                     return jerry_timer_2_divider;
+//                     return JERRYPIT2Divider;
                        return counter2Lo;
                }
                // Unaligned word reads???
@@ -464,8 +569,8 @@ uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
 //             return clock_word_read(offset);
        else if (offset == 0xF10020)
                return jerryIntPending;
-       else if ((offset >= 0xF17C00) && (offset <= 0xF17C01))
-               return anajoy_word_read(offset);
+//     else if ((offset >= 0xF17C00) && (offset <= 0xF17C01))
+//             return anajoy_word_read(offset);
        else if (offset == 0xF14000)
                return (joystick_word_read(offset) & 0xFFFE) | eeprom_word_read(offset);
        else if ((offset >= 0xF14002) && (offset < 0xF14003))
@@ -499,6 +604,7 @@ void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
                return;
        }
        // SCLK ($F1A150--8 bits wide)
+//NOTE: This should be taken care of in DAC...
        else if ((offset >= 0xF1A152) && (offset <= 0xF1A153))
        {
 //             WriteLog("JERRY: Writing %02X to SCLK...\n", data);
@@ -522,36 +628,36 @@ void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
                switch (offset & 0x07)
                {
                case 0:
-                       jerry_timer_1_prescaler = (jerry_timer_1_prescaler & 0x00FF) | (data << 8);
-                       jerry_reset_timer_1();
+                       JERRYPIT1Prescaler = (JERRYPIT1Prescaler & 0x00FF) | (data << 8);
+                       JERRYResetPIT1();
                        break;
                case 1:
-                       jerry_timer_1_prescaler = (jerry_timer_1_prescaler & 0xFF00) | data;
-                       jerry_reset_timer_1();
+                       JERRYPIT1Prescaler = (JERRYPIT1Prescaler & 0xFF00) | data;
+                       JERRYResetPIT1();
                        break;
                case 2:
-                       jerry_timer_1_divider = (jerry_timer_1_divider & 0x00FF) | (data << 8);
-                       jerry_reset_timer_1();
+                       JERRYPIT1Divider = (JERRYPIT1Divider & 0x00FF) | (data << 8);
+                       JERRYResetPIT1();
                        break;
                case 3:
-                       jerry_timer_1_divider = (jerry_timer_1_divider & 0xFF00) | data;
-                       jerry_reset_timer_1();
+                       JERRYPIT1Divider = (JERRYPIT1Divider & 0xFF00) | data;
+                       JERRYResetPIT1();
                        break;
                case 4:
-                       jerry_timer_2_prescaler = (jerry_timer_2_prescaler & 0x00FF) | (data << 8);
-                       jerry_reset_timer_2();
+                       JERRYPIT2Prescaler = (JERRYPIT2Prescaler & 0x00FF) | (data << 8);
+                       JERRYResetPIT2();
                        break;
                case 5:
-                       jerry_timer_2_prescaler = (jerry_timer_2_prescaler & 0xFF00) | data;
-                       jerry_reset_timer_2();
+                       JERRYPIT2Prescaler = (JERRYPIT2Prescaler & 0xFF00) | data;
+                       JERRYResetPIT2();
                        break;
                case 6:
-                       jerry_timer_2_divider = (jerry_timer_2_divider & 0x00FF) | (data << 8);
-                       jerry_reset_timer_2();
+                       JERRYPIT2Divider = (JERRYPIT2Divider & 0x00FF) | (data << 8);
+                       JERRYResetPIT2();
                        break;
                case 7:
-                       jerry_timer_2_divider = (jerry_timer_2_divider & 0xFF00) | data;
-                       jerry_reset_timer_2();
+                       JERRYPIT2Divider = (JERRYPIT2Divider & 0xFF00) | data;
+                       JERRYResetPIT2();
                }
                return;
        }
@@ -565,11 +671,11 @@ void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
        {
 WriteLog("JERRY: (68K int en/lat - Unhandled!) Tried to write $%02X to $%08X!\n", data, offset);
        }
-       else if ((offset >= 0xF17C00) && (offset <= 0xF17C01))
+/*     else if ((offset >= 0xF17C00) && (offset <= 0xF17C01))
        {
                anajoy_byte_write(offset, data);
                return;
-       }
+       }*/
        else if ((offset >= 0xF14000) && (offset <= 0xF14003))
        {
                joystick_byte_write(offset, data);
@@ -608,6 +714,7 @@ void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
                DSPWriteWord(offset, data, who);
                return;
        }
+//NOTE: This should be taken care of in DAC...
        else if (offset == 0xF1A152)                                    // Bottom half of SCLK ($F1A150)
        {
                WriteLog("JERRY: Writing %04X to SCLK (by %s)...\n", data, whoName[who]);
@@ -630,20 +737,20 @@ void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
                switch(offset & 0x07)
                {
                case 0:
-                       jerry_timer_1_prescaler = data;
-                       jerry_reset_timer_1();
+                       JERRYPIT1Prescaler = data;
+                       JERRYResetPIT1();
                        break;
                case 2:
-                       jerry_timer_1_divider = data;
-                       jerry_reset_timer_1();
+                       JERRYPIT1Divider = data;
+                       JERRYResetPIT1();
                        break;
                case 4:
-                       jerry_timer_2_prescaler = data;
-                       jerry_reset_timer_2();
+                       JERRYPIT2Prescaler = data;
+                       JERRYResetPIT2();
                        break;
                case 6:
-                       jerry_timer_2_divider = data;
-                       jerry_reset_timer_2();
+                       JERRYPIT2Divider = data;
+                       JERRYResetPIT2();
                }
                // Need to handle (unaligned) cases???
                return;
@@ -658,12 +765,12 @@ void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
        {
 WriteLog("JERRY: (68K int en/lat - Unhandled!) Tried to write $%04X to $%08X!\n", data, offset);
        }
-       else if (offset >= 0xF17C00 && offset < 0xF17C02)
+/*     else if (offset >= 0xF17C00 && offset < 0xF17C02)
        {
 //I think this was removed from the Jaguar. If so, then we don't need this...!
                anajoy_word_write(offset, data);
                return;
-       }
+       }*/
        else if (offset >= 0xF14000 && offset < 0xF14003)
        {
                joystick_word_write(offset, data);
index 604f2cce1e5d96fa1cf85a20eab159edf2c97800..ca38c13c54fb879b52ac6d78b29f177d458c9d25 100644 (file)
 #include "objectp.h"
 #include "cry2rgb.h"
 #include "settings.h"
+#include "clock.h"
+
+#define NEW_TIMER_SYSTEM
 
 // TOM registers (offset from $F00000)
 
@@ -969,7 +972,7 @@ uint16 topVisible = (vjs.hardwareTypeNTSC ? TOP_VISIBLE_VC : TOP_VISIBLE_VC_PAL)
                }
 
 //             TOMBackbuffer += GetSDLScreenPitch() / 2;       // Returns bytes, but we need words
-               TOMBackbuffer += GetSDLScreenPitch() / 4;       // Returns bytes, but we need dwords
+               TOMBackbuffer += GetSDLScreenWidthInPixels();
        }
 }
 
@@ -1467,8 +1470,16 @@ int tom_irq_enabled(int irq)
        return (tom_ram_8[0xE0] << 8) | tom_ram_8[0xE1];
 }*/
 
+// NEW:
+// TOM Programmable Interrupt Timer handler
+// NOTE: TOM's PIT is only enabled if the prescaler is != 0
+//       The PIT only generates an interrupt when it counts down to zero, not when loaded!
+
+void TOMPITCallback(void);
+
 void TOMResetPIT(void)
 {
+#ifndef NEW_TIMER_SYSTEM
 //Probably should *add* this amount to the counter to retain cycle accuracy! !!! FIX !!! [DONE]
 //Also, why +1??? 'Cause that's what it says in the JTRM...!
 //There is a small problem with this approach: If both the prescaler and the divider are equal
@@ -1476,6 +1487,16 @@ void TOMResetPIT(void)
        if (tom_timer_prescaler)
                tom_timer_counter += (1 + tom_timer_prescaler) * (1 + tom_timer_divider);
 //     WriteLog("tom: reseting timer to 0x%.8x (%i)\n",tom_timer_counter,tom_timer_counter);
+#else
+       // Need to remove previous timer from the queue, if it exists...
+       RemoveCallback(TOMPITCallback);
+       
+       if (tom_timer_prescaler)
+       {
+               double usecs = (float)(tom_timer_prescaler + 1) * (float)(tom_timer_divider + 1) * RISC_CYCLE_IN_USEC;
+               SetCallbackTime(TOMPITCallback, usecs);
+       }
+#endif
 }
 
 //
@@ -1499,3 +1520,17 @@ void TOMExecPIT(uint32 cycles)
                }
        }
 }
+
+
+void TOMPITCallback(void)
+{
+//     INT1_RREG |= 0x08;                         // Set TOM PIT interrupt pending
+       tom_set_pending_timer_int();
+    GPUSetIRQLine(GPUIRQ_TIMER, ASSERT_LINE);  // It does the 'IRQ enabled' checking
+
+//     if (INT1_WREG & 0x08)
+       if (tom_irq_enabled(IRQ_TIMER))
+               m68k_set_irq(7);                       // Generate 68K NMI
+
+       TOMResetPIT();
+}