]> Shamusworld >> Repos - virtualjaguar/blobdiff - src/jerry.cpp
And so with this revision, we commit massive breakage. We Apologize For The Inconveni...
[virtualjaguar] / src / jerry.cpp
index d569dad7b5a95faafe2f475386c8fb37d0a42dcf..c564d4306147a0aa836ac4b64e42843d04f9fbda 100644 (file)
 //     F1DE00          R     xxxxxxxx xxxxxxxx   ROM_NOISE - white noise
 //     ------------------------------------------------------------
 
+#include "jerry.h"
+
+#include <string.h>                                                            // For memcpy
 //#include <math.h>
+#include "cdrom.h"
+#include "dac.h"
+#include "dsp.h"
+#include "eeprom.h"
+#include "event.h"
 #include "jaguar.h"
+#include "joystick.h"
+#include "log.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;
+/*static*/ uint8 jerry_ram_8[0x10000];
 
 //#define JERRY_CONFIG 0x4002                                          // ??? What's this ???
 
@@ -167,8 +175,8 @@ static uint32 JERRYPIT2Divider;
 static int32 jerry_timer_1_counter;
 static int32 jerry_timer_2_counter;
 
-static uint32 jerry_i2s_interrupt_divide = 8;
-static int32 jerry_i2s_interrupt_timer = -1;
+uint32 JERRYI2SInterruptDivide = 8;
+int32 JERRYI2SInterruptTimer = -1;
 uint32 jerryI2SCycles;
 uint32 jerryIntPending;
 
@@ -185,7 +193,10 @@ 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)
+
+//This is only used by the old system, so once the new timer system is working this
+//should be safe to nuke.
+void JERRYI2SExec(uint32 cycles)
 {
 #ifndef NEW_TIMER_SYSTEM
        extern uint16 serialMode;                                               // From DAC.CPP
@@ -245,6 +256,7 @@ void jerry_i2s_exec(uint32 cycles)
 #endif
 }
 
+//NOTE: This is only used by the old execution core. Safe to nuke once it's stable.
 void JERRYExecPIT(uint32 cycles)
 {
 //This is wrong too: Counters are *always* spinning! !!! FIX !!! [DONE]
@@ -294,8 +306,12 @@ void JERRYResetPIT1(void)
 
 #else
        RemoveCallback(JERRYPIT1Callback);
-       double usecs = (float)(JERRYPIT1Prescaler + 1) * (float)(JERRYPIT1Divider + 1) * RISC_CYCLE_IN_USEC;
-       SetCallbackTime(JERRYPIT1Callback, usecs);
+
+       if (JERRYPIT1Prescaler | JERRYPIT1Divider)
+       {
+               double usecs = (float)(JERRYPIT1Prescaler + 1) * (float)(JERRYPIT1Divider + 1) * RISC_CYCLE_IN_USEC;
+               SetCallbackTime(JERRYPIT1Callback, usecs);
+       }
 #endif
 }
 
@@ -315,8 +331,12 @@ void JERRYResetPIT2(void)
 
 #else
        RemoveCallback(JERRYPIT2Callback);
-       double usecs = (float)(JERRYPIT2Prescaler + 1) * (float)(JERRYPIT2Divider + 1) * RISC_CYCLE_IN_USEC;
-       SetCallbackTime(JERRYPIT2Callback, usecs);
+
+       if (JERRYPIT1Prescaler | JERRYPIT1Divider)
+       {
+               double usecs = (float)(JERRYPIT2Prescaler + 1) * (float)(JERRYPIT2Divider + 1) * RISC_CYCLE_IN_USEC;
+               SetCallbackTime(JERRYPIT2Callback, usecs);
+       }
 #endif
 }
 
@@ -336,6 +356,7 @@ void JERRYI2SCallback(void)
 {
        // Why is it called this? Instead of SCLK? Shouldn't this be read from DAC.CPP???
 //Yes, it should. !!! FIX !!!
+#warning 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...
@@ -377,19 +398,28 @@ void JERRYI2SCallback(void)
                        }
                        jerry_i2s_interrupt_timer += 602;
                }*/
+
+               if (ButchIsReadyToSend())//Not sure this is right spot to check...
+               {
+//     return GetWordFromButchSSI(offset, who);
+                       SetSSIWordsXmittedFromButch();
+                       DSPSetIRQLine(DSPIRQ_SSI, ASSERT_LINE);
+               }
+
+               SetCallbackTime(JERRYI2SCallback, 22.675737);
        }
 }
 
 
-void jerry_init(void)
+void JERRYInit(void)
 {
 //     clock_init();
 //     anajoy_init();
-       joystick_init();
+       JoystickInit();
        DACInit();
 //This should be handled with the cart initialization...
 //     eeprom_init();
-       memory_malloc_secure((void **)&jerry_ram_8, 0x10000, "JERRY RAM/ROM");
+//     memory_malloc_secure((void **)&jerry_ram_8, 0x10000, "JERRY RAM/ROM");
        memcpy(&jerry_ram_8[0xD000], wave_table, 0x1000);
 
        JERRYPIT1Prescaler = 0xFFFF;
@@ -398,12 +428,12 @@ void jerry_init(void)
        JERRYPIT2Divider = 0xFFFF;
 }
 
-void jerry_reset(void)
+void JERRYReset(void)
 {
 //     clock_reset();
 //     anajoy_reset();
-       joystick_reset();
-       eeprom_reset();
+       JoystickReset();
+       EepromReset();
        JERRYResetI2S();
        DACReset();
 
@@ -416,20 +446,20 @@ void jerry_reset(void)
        jerry_timer_2_counter = 0;
 }
 
-void jerry_done(void)
+void JERRYDone(void)
 {
        WriteLog("JERRY: M68K Interrupt control ($F10020) = %04X\n", GET16(jerry_ram_8, 0x20));
-       memory_free(jerry_ram_8);
+//     memory_free(jerry_ram_8);
 //     clock_done();
 //     anajoy_done();
-       joystick_done();
+       JoystickDone();
        DACDone();
-       eeprom_done();
+       EepromDone();
 }
 
 bool JERRYIRQEnabled(int irq)
 {
-       // Read the word @ $F10020 
+       // Read the word @ $F10020
        return jerry_ram_8[0x21] & (1 << irq);
 }
 
@@ -502,6 +532,7 @@ uint8 JERRYReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
                        return counter2Lo & 0xFF;
                }
 #else
+WriteLog("JERRY: Unhandled timer read (BYTE) at %08X...\n", offset);
 #endif
        }
 //     else if (offset >= 0xF10010 && offset <= 0xF10015)
@@ -509,10 +540,10 @@ uint8 JERRYReadByte(uint32 offset, uint32 who/*=UNKNOWN*/)
 //     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);
+               return JoystickReadByte(offset) | EepromReadByte(offset);
        else if (offset >= 0xF14000 && offset <= 0xF1A0FF)
-               return eeprom_byte_read(offset);
-       
+               return EepromReadByte(offset);
+
        return jerry_ram_8[offset & 0xFFFF];
 }
 
@@ -542,6 +573,7 @@ uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
 //in the jerry_timer_n_counter variables... !!! FIX !!! [DONE]
        else if ((offset >= 0xF10036) && (offset <= 0xF1003D))
        {
+#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;
@@ -564,6 +596,9 @@ uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
                        return counter2Lo;
                }
                // Unaligned word reads???
+#else
+WriteLog("JERRY: Unhandled timer read (WORD) at %08X...\n", offset);
+#endif
        }
 //     else if ((offset >= 0xF10010) && (offset <= 0xF10015))
 //             return clock_word_read(offset);
@@ -572,11 +607,11 @@ uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/)
 //     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);
+               return (JoystickReadWord(offset) & 0xFFFE) | EepromReadWord(offset);
        else if ((offset >= 0xF14002) && (offset < 0xF14003))
-               return joystick_word_read(offset);
+               return JoystickReadWord(offset);
        else if ((offset >= 0xF14000) && (offset <= 0xF1A0FF))
-               return eeprom_word_read(offset);
+               return EepromReadWord(offset);
 
 /*if (offset >= 0xF1D000)
        WriteLog("JERRY: Reading word at %08X [%04X]...\n", offset, ((uint16)jerry_ram_8[(offset+0)&0xFFFF] << 8) | jerry_ram_8[(offset+1)&0xFFFF]);//*/
@@ -614,17 +649,23 @@ void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
                        jerry_i2s_interrupt_divide = (jerry_i2s_interrupt_divide & 0xFF00) | (uint32)data;
 
                jerry_i2s_interrupt_timer = -1;
+#ifndef NEW_TIMER_SYSTEM
                jerry_i2s_exec(0);
+#else
+               RemoveCallback(JERRYI2SCallback);
+               JERRYI2SCallback();
+#endif
 //             return;
        }
        // LTXD/RTXD/SCLK/SMODE $F1A148/4C/50/54 (really 16-bit registers...)
        else if (offset >= 0xF1A148 && offset <= 0xF1A157)
-       { 
+       {
                DACWriteByte(offset, data, who);
-               return; 
+               return;
        }
        else if (offset >= 0xF10000 && offset <= 0xF10007)
        {
+#ifndef NEW_TIMER_SYSTEM
                switch (offset & 0x07)
                {
                case 0:
@@ -659,6 +700,9 @@ void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/)
                        JERRYPIT2Divider = (JERRYPIT2Divider & 0xFF00) | data;
                        JERRYResetPIT2();
                }
+#else
+WriteLog("JERRY: Unhandled timer write (BYTE) at %08X...\n", offset);
+#endif
                return;
        }
 /*     else if ((offset >= 0xF10010) && (offset <= 0xF10015))
@@ -678,13 +722,13 @@ WriteLog("JERRY: (68K int en/lat - Unhandled!) Tried to write $%02X to $%08X!\n"
        }*/
        else if ((offset >= 0xF14000) && (offset <= 0xF14003))
        {
-               joystick_byte_write(offset, data);
-               eeprom_byte_write(offset, data);
+               JoystickWriteByte(offset, data);
+               EepromWriteByte(offset, data);
                return;
        }
        else if ((offset >= 0xF14000) && (offset <= 0xF1A0FF))
        {
-               eeprom_byte_write(offset, data);
+               EepromWriteByte(offset, data);
                return;
        }
 
@@ -721,19 +765,26 @@ void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
 //This should *only* be enabled when SMODE has its INTERNAL bit set! !!! FIX !!!
                jerry_i2s_interrupt_divide = (uint8)data;
                jerry_i2s_interrupt_timer = -1;
+#ifndef NEW_TIMER_SYSTEM
                jerry_i2s_exec(0);
+#else
+               RemoveCallback(JERRYI2SCallback);
+               JERRYI2SCallback();
+#endif
 
                DACWriteWord(offset, data, who);
-               return; 
+               return;
        }
        // LTXD/RTXD/SCLK/SMODE $F1A148/4C/50/54 (really 16-bit registers...)
        else if (offset >= 0xF1A148 && offset <= 0xF1A156)
-       { 
+       {
                DACWriteWord(offset, data, who);
-               return; 
+               return;
        }
        else if (offset >= 0xF10000 && offset <= 0xF10007)
        {
+//#ifndef NEW_TIMER_SYSTEM
+#if 1
                switch(offset & 0x07)
                {
                case 0:
@@ -753,6 +804,9 @@ void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/)
                        JERRYResetPIT2();
                }
                // Need to handle (unaligned) cases???
+#else
+WriteLog("JERRY: Unhandled timer write %04X (WORD) at %08X by %s...\n", data, offset, whoName[who]);
+#endif
                return;
        }
 /*     else if (offset >= 0xF10010 && offset < 0xF10016)
@@ -773,13 +827,13 @@ WriteLog("JERRY: (68K int en/lat - Unhandled!) Tried to write $%04X to $%08X!\n"
        }*/
        else if (offset >= 0xF14000 && offset < 0xF14003)
        {
-               joystick_word_write(offset, data);
-               eeprom_word_write(offset, data);
+               JoystickWriteWord(offset, data);
+               EepromWriteWord(offset, data);
                return;
        }
        else if (offset >= 0xF14000 && offset <= 0xF1A0FF)
        {
-               eeprom_word_write(offset, data);
+               EepromWriteWord(offset, data);
                return;
        }