X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fjerry.cpp;h=d569dad7b5a95faafe2f475386c8fb37d0a42dcf;hb=3580a9ef98432ed54f065f2d537d588fdc083592;hp=8b97e86280c7db2a533afa472ccf6dd9d6e59a02;hpb=0031c06df2f7f099ca5ecf1632f46b92f6b0dd79;p=virtualjaguar diff --git a/src/jerry.cpp b/src/jerry.cpp index 8b97e86..d569dad 100644 --- a/src/jerry.cpp +++ b/src/jerry.cpp @@ -1,18 +1,18 @@ // // JERRY Core // -// by cal2 -// GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS) -// Cleanups by James L. Hammons +// Originally by David Raingeard +// GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Carwin Jones (BeOS) +// Cleanups/rewrites/fixes by James L. Hammons // // ------------------------------------------------------------ // JERRY REGISTERS (Mapped by Aaron Giles) // ------------------------------------------------------------ // F10000-F13FFF R/W xxxxxxxx xxxxxxxx Jerry // F10000 W xxxxxxxx xxxxxxxx JPIT1 - timer 1 pre-scaler -// F10004 W xxxxxxxx xxxxxxxx JPIT2 - timer 1 divider -// F10008 W xxxxxxxx xxxxxxxx JPIT3 - timer 2 pre-scaler -// F1000C W xxxxxxxx xxxxxxxx JPIT4 - timer 2 divider +// F10002 W xxxxxxxx xxxxxxxx JPIT2 - timer 1 divider +// F10004 W xxxxxxxx xxxxxxxx JPIT3 - timer 2 pre-scaler +// F10008 W xxxxxxxx xxxxxxxx JPIT4 - timer 2 divider // F10010 W ------xx xxxxxxxx CLK1 - processor clock divider // F10012 W ------xx xxxxxxxx CLK2 - video clock divider // F10014 W -------- --xxxxxx CLK3 - chroma clock divider @@ -52,6 +52,10 @@ // R -------- ------x- (PAREN - parity enable) // R -------- -------x (ODD - odd parity) // F10034 R/W xxxxxxxx xxxxxxxx ASICLK - asynchronous serial interface clock +// F10036 R xxxxxxxx xxxxxxxx JPIT1 - timer 1 pre-scaler +// F10038 R xxxxxxxx xxxxxxxx JPIT2 - timer 1 divider +// F1003A R xxxxxxxx xxxxxxxx JPIT3 - timer 2 pre-scaler +// F1003C R xxxxxxxx xxxxxxxx JPIT4 - timer 2 divider // ------------------------------------------------------------ // F14000-F17FFF R/W xxxxxxxx xxxxxxxx Joysticks and GPIO0-5 // F14000 R xxxxxxxx xxxxxxxx JOYSTICK - read joystick state @@ -59,7 +63,7 @@ // W x------- -------- (enable joystick outputs) // W -------- xxxxxxxx (joystick output data) // F14002 R xxxxxxxx xxxxxxxx JOYBUTS - button register -// F14800-F14FFF R/W xxxxxxxx xxxxxxxx GPI00 - reserved +// F14800-F14FFF R/W xxxxxxxx xxxxxxxx GPI00 - reserved (CD-ROM?) // F15000-F15FFF R/W xxxxxxxx xxxxxxxx GPI01 - reserved // F16000-F16FFF R/W xxxxxxxx xxxxxxxx GPI02 - reserved // F17000-F177FF R/W xxxxxxxx xxxxxxxx GPI03 - reserved @@ -140,221 +144,373 @@ // F1DE00 R xxxxxxxx xxxxxxxx ROM_NOISE - white noise // ------------------------------------------------------------ -#include "jerry.h" +//#include +#include "jaguar.h" #include "wavetable.h" -#include +#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 uint16 *jerry_ram_16; -//static uint8 * jerry_wave_rom; +/*static*/ uint8 * jerry_ram_8; - -//#define JERRY_CONFIG jerry_ram_16[0x4002>>1] -#define JERRY_CONFIG 0x4002 +//#define JERRY_CONFIG 0x4002 // ??? What's this ??? 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; static uint32 jerry_i2s_interrupt_divide = 8; -static int32 jerry_i2s_interrupt_timer = -1; -static int32 jerry_i2s_interrupt_cycles_per_scanline = 0; +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) -{ - jerry_i2s_interrupt_divide &= 0xFF; +{ +#ifndef NEW_TIMER_SYSTEM + extern uint16 serialMode; // From DAC.CPP + if (serialMode & 0x01) // INTERNAL flag (JERRY is master) + { + + // 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; + + if (jerry_i2s_interrupt_timer == -1) + { + // 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)); + } + + 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 !!! + DSPSetIRQLine(DSPIRQ_SSI, ASSERT_LINE); + jerry_i2s_interrupt_timer += jerryI2SCycles; +#ifdef JERRY_DEBUG + if (jerry_i2s_interrupt_timer < 0) + WriteLog("JERRY: Missed generating an interrupt (missed %u)!\n", (-jerry_i2s_interrupt_timer / jerryI2SCycles) + 1); +#endif + } + } + else // JERRY is slave to external word clock + { + // 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; + } + } +#else + RemoveCallback(JERRYI2SCallback); + JERRYI2SCallback(); +#endif +} - if (jerry_i2s_interrupt_timer == -1) +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) { - uint32 jerry_i2s_int_freq = (26591000 / 64) / (jerry_i2s_interrupt_divide + 1); - jerry_i2s_interrupt_cycles_per_scanline = 13300000 / jerry_i2s_int_freq; - jerry_i2s_interrupt_timer = jerry_i2s_interrupt_cycles_per_scanline; - //fprintf(log_get(),"jerry: i2s interrupt rate set to %i hz (every %i cpu clock cycles) jerry_i2s_interrupt_divide=%i\n",jerry_i2s_int_freq,jerry_i2s_interrupt_cycles_per_scanline,jerry_i2s_interrupt_divide); - pcm_set_sample_rate(jerry_i2s_int_freq); +//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); } - jerry_i2s_interrupt_timer -= cycles; - // note : commented since the sound doesn't work properly else - if (1)//jerry_i2s_interrupt_timer<=0) + +//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) { - // i2s interrupt - dsp_check_if_i2s_interrupt_needed(); - //fprintf(log_get(),"jerry_i2s_interrupt_timer=%i, generating an i2s interrupt\n",jerry_i2s_interrupt_timer); - jerry_i2s_interrupt_timer += jerry_i2s_interrupt_cycles_per_scanline; +//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) { - //fprintf(log_get(),"i2s: reseting\n"); + //WriteLog("i2s: reseting\n"); +//This is really SCLK... !!! FIX !!! jerry_i2s_interrupt_divide = 8; 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 - jerry_timer_1_counter = (1 + jerry_timer_1_prescaler) * (1 + jerry_timer_1_divider); + else//*/ +//Small problem with this approach: Overflow if both are = $FFFF. !!! FIX !!! + jerry_timer_1_counter = (JERRYPIT1Prescaler + 1) * (JERRYPIT1Divider + 1); // if (jerry_timer_1_counter) -// fprintf(log_get(),"jerry: reseting timer 1 to 0x%.8x (%i)\n",jerry_timer_1_counter,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 = ((1 + jerry_timer_2_prescaler) * (1 + jerry_timer_2_divider)); + else//*/ + jerry_timer_2_counter = (JERRYPIT2Prescaler + 1) * (JERRYPIT2Divider + 1); // if (jerry_timer_2_counter) -// fprintf(log_get(),"jerry: reseting timer 2 to 0x%.8x (%i)\n",jerry_timer_2_counter,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 jerry_pit_exec(uint32 cycles) +void JERRYPIT1Callback(void) { - 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) +void JERRYPIT2Callback(void) +{ + DSPSetIRQLine(DSPIRQ_TIMER1, ASSERT_LINE); // This does the 'IRQ enabled' checking... + JERRYResetPIT2(); +} + +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)); + +//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) { - dsp_set_irq_line(2, 1); - jerry_reset_timer_1(); + DSPSetIRQLine(DSPIRQ_SSI, ASSERT_LINE); // This does the 'IRQ enabled' checking... + double usecs = (float)jerryI2SCycles * RISC_CYCLE_IN_USEC; + SetCallbackTime(JERRYI2SCallback, usecs); } - - if (jerry_timer_2_counter) - jerry_timer_2_counter -= cycles; - - if (jerry_timer_2_counter <= 0) + else // JERRY is slave to external word clock { - dsp_set_irq_line(3, 1); - jerry_reset_timer_2(); +//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_wave_rom_init(void) -{ -// memory_malloc_secure((void **)&jerry_wave_rom, 0x1000, "jerry wave rom"); -// uint32 * jaguar_wave_rom_32 = (uint32 *)jerry_wave_rom; - - // use real wave table dump -// JLH: Looks like this WT dump is in the wrong endian (For the Jaguar, that is)... -// memcpy(jerry_wave_rom, wave_table, 0x1000); - - // reverse byte ordering -// JLH: Actually, this does nothing... -/* for(int i=0; i<0x400; i++) - { - uint32 data = jaguar_wave_rom_32[i]; - data = ((data & 0xFF000000) >> 24) | ((data & 0x0000FF00) << 8) - | ((data & 0x00FF0000) >> 8) | ((data & 0x000000FF) << 24); - }*/ -// Why the need for an extra buffer to hold it, when it already exists in the form of wave_table??? -// Also, there was a memory leak, since it was never deallocated... (jerry_wave_rom) - - // Copy it to DSP RAM -//WAS: memcpy(&jerry_ram_8[0xD000], jerry_wave_rom, 0x1000); - memcpy(&jerry_ram_8[0xD000], wave_table, 0x1000); -} void jerry_init(void) { - //fprintf(log_get(),"jerry_init()\n"); - clock_init(); - anajoy_init(); +// clock_init(); +// anajoy_init(); joystick_init(); - eeprom_init(); - memory_malloc_secure((void **)&jerry_ram_8, 0x10000, "jerry ram"); -// jerry_ram_16 = (uint16 *)jerry_ram_8; - jerry_wave_rom_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) { - //fprintf(log_get(),"jerry_reset()\n"); - clock_reset(); - anajoy_reset(); +// clock_reset(); +// anajoy_reset(); joystick_reset(); eeprom_reset(); - jerry_reset_i2s_timer(); - - memset(jerry_ram_8, 0x00, 0x10000); - jerry_ram_8[JERRY_CONFIG+1] |= 0x10; // NTSC (bit 4) - jerry_timer_1_prescaler = 0xFFFF; - jerry_timer_2_prescaler = 0xFFFF; - jerry_timer_1_divider = 0xFFFF; - jerry_timer_2_divider = 0xFFFF; + JERRYResetI2S(); + DACReset(); + + memset(jerry_ram_8, 0x00, 0xD000); // Don't clear out the Wavetable ROM...! + JERRYPIT1Prescaler = 0xFFFF; + JERRYPIT2Prescaler = 0xFFFF; + JERRYPIT1Divider = 0xFFFF; + JERRYPIT2Divider = 0xFFFF; jerry_timer_1_counter = 0; jerry_timer_2_counter = 0; - } void jerry_done(void) { - //fprintf(log_get(),"jerry_done()\n"); + WriteLog("JERRY: M68K Interrupt control ($F10020) = %04X\n", GET16(jerry_ram_8, 0x20)); memory_free(jerry_ram_8); - clock_done(); - anajoy_done(); +// clock_done(); +// anajoy_done(); joystick_done(); + DACDone(); eeprom_done(); } +bool JERRYIRQEnabled(int irq) +{ + // Read the word @ $F10020 + return jerry_ram_8[0x21] & (1 << irq); +} + +void JERRYSetPendingIRQ(int irq) +{ + // This is the shadow of INT (it's a split RO/WO register) + jerryIntPending |= (1 << irq); +} + // // JERRY byte access (read) // - -unsigned jerry_byte_read(unsigned int offset) +uint8 JERRYReadByte(uint32 offset, uint32 who/*=UNKNOWN*/) { #ifdef JERRY_DEBUG - fprintf(log_get(),"jerry: reading byte at 0x%.6x\n",offset); + WriteLog("JERRY: Reading byte at %06X\n", offset); #endif - if ((offset >= dsp_control_ram_base) && (offset < dsp_control_ram_base+0x20)) - return dsp_byte_read(offset); - else if ((offset >= dsp_work_ram_base) && (offset < dsp_work_ram_base+0x2000)) - return dsp_byte_read(offset); - else if ((offset >= 0xF10000) && (offset <= 0xF10007)) + if ((offset >= DSP_CONTROL_RAM_BASE) && (offset < DSP_CONTROL_RAM_BASE+0x20)) + return DSPReadByte(offset, who); + else if ((offset >= DSP_WORK_RAM_BASE) && (offset < DSP_WORK_RAM_BASE+0x2000)) + return DSPReadByte(offset, who); + // LRXD/RRXD/SSTAT $F1A148/4C/50 (really 16-bit registers...) + else if (offset >= 0xF1A148 && offset <= 0xF1A153) + return DACReadByte(offset, who); +// F10036 R xxxxxxxx xxxxxxxx JPIT1 - timer 1 pre-scaler +// F10038 R xxxxxxxx xxxxxxxx JPIT2 - timer 1 divider +// F1003A R xxxxxxxx xxxxxxxx JPIT3 - timer 2 pre-scaler +// F1003C R xxxxxxxx xxxxxxxx JPIT4 - timer 2 divider +//This is WRONG! +// 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)) { - switch(offset & 0x07) +#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 0: - return jerry_timer_1_prescaler >> 8; - case 1: - return jerry_timer_1_prescaler & 0xFF; - case 2: - return jerry_timer_1_divider >> 8; - case 3: - return jerry_timer_1_divider & 0xFF; - case 4: - return jerry_timer_2_prescaler >> 8; - case 5: - return jerry_timer_2_prescaler & 0xFF; case 6: - return jerry_timer_2_divider >> 8; +// return JERRYPIT1Prescaler >> 8; + return counter1Hi >> 8; case 7: - return jerry_timer_2_divider & 0xFF; +// return JERRYPIT1Prescaler & 0xFF; + return counter1Hi & 0xFF; + case 8: +// return JERRYPIT1Divider >> 8; + return counter1Lo >> 8; + case 9: +// return JERRYPIT1Divider & 0xFF; + return counter1Lo & 0xFF; + case 10: +// return JERRYPIT2Prescaler >> 8; + return counter2Hi >> 8; + case 11: +// return JERRYPIT2Prescaler & 0xFF; + return counter2Hi & 0xFF; + case 12: +// return JERRYPIT2Divider >> 8; + return counter2Lo >> 8; + case 13: +// 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 >= 0xF14000) && (offset <= 0xF14003)) - { +// 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 >= 0xF14000 && offset <= 0xF14003) return joystick_byte_read(offset) | eeprom_byte_read(offset); - } - else if ((offset >= 0xF14000) && (offset <= 0xF1A0FF)) + else if (offset >= 0xF14000 && offset <= 0xF1A0FF) return eeprom_byte_read(offset); return jerry_ram_8[offset & 0xFFFF]; @@ -363,82 +519,95 @@ unsigned jerry_byte_read(unsigned int offset) // // JERRY word access (read) // - -unsigned jerry_word_read(unsigned int offset) +uint16 JERRYReadWord(uint32 offset, uint32 who/*=UNKNOWN*/) { #ifdef JERRY_DEBUG - fprintf(log_get(),"jerry: reading word at 0x%.6x\n",offset); + WriteLog("JERRY: Reading word at %06X\n", offset); #endif - if ((offset >= dsp_control_ram_base) && (offset < dsp_control_ram_base+0x20)) - return dsp_word_read(offset); - else if ((offset >= dsp_work_ram_base) && (offset < dsp_work_ram_base+0x2000)) - return dsp_word_read(offset); - else if ((offset >= 0xF10000) && (offset <= 0xF10007)) + if ((offset >= DSP_CONTROL_RAM_BASE) && (offset < DSP_CONTROL_RAM_BASE+0x20)) + return DSPReadWord(offset, who); + else if (offset >= DSP_WORK_RAM_BASE && offset <= DSP_WORK_RAM_BASE + 0x1FFF) + return DSPReadWord(offset, who); + // LRXD/RRXD/SSTAT $F1A148/4C/50 (really 16-bit registers...) + else if (offset >= 0xF1A148 && offset <= 0xF1A153) + return DACReadWord(offset, who); +// F10036 R xxxxxxxx xxxxxxxx JPIT1 - timer 1 pre-scaler +// F10038 R xxxxxxxx xxxxxxxx JPIT2 - timer 1 divider +// F1003A R xxxxxxxx xxxxxxxx JPIT3 - timer 2 pre-scaler +// F1003C R xxxxxxxx xxxxxxxx JPIT4 - timer 2 divider +//This is WRONG! +// 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] + else if ((offset >= 0xF10036) && (offset <= 0xF1003D)) { - switch(offset & 0x07) +// 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 0: - return jerry_timer_1_prescaler; - case 2: - return jerry_timer_1_divider; - case 4: - return jerry_timer_2_prescaler; case 6: - return jerry_timer_2_divider; +// return JERRYPIT1Prescaler; + return counter1Hi; + case 8: +// return JERRYPIT1Divider; + return counter1Lo; + case 10: +// return JERRYPIT2Prescaler; + return counter2Hi; + case 12: +// return JERRYPIT2Divider; + return counter2Lo; } // Unaligned word reads??? } - else if ((offset >= 0xF10010) && (offset <= 0xF10015)) - return clock_word_read(offset); +// else if ((offset >= 0xF10010) && (offset <= 0xF10015)) +// return clock_word_read(offset); else if (offset == 0xF10020) - return 0x00; - else if ((offset >= 0xF17C00) && (offset <= 0xF17C01)) - return anajoy_word_read(offset); + return jerryIntPending; +// else if ((offset >= 0xF17C00) && (offset <= 0xF17C01)) +// return anajoy_word_read(offset); else if (offset == 0xF14000) - { - //fprintf(log_get(),"reading 0x%.4x from 0xf14000\n"); return (joystick_word_read(offset) & 0xFFFE) | eeprom_word_read(offset); - } else if ((offset >= 0xF14002) && (offset < 0xF14003)) return joystick_word_read(offset); else if ((offset >= 0xF14000) && (offset <= 0xF1A0FF)) return eeprom_word_read(offset); -// This is never executed! -/* offset &= 0xFFFF; - if (offset==0x4002) - return(0xffff);*/ +/*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]);//*/ -/* uint16 data = jerry_ram_8[offset+0]; - data <<= 8; - data |= jerry_ram_8[offset+1]; - return data;*/ + offset &= 0xFFFF; // Prevent crashing...! return ((uint16)jerry_ram_8[offset+0] << 8) | jerry_ram_8[offset+1]; } // // JERRY byte access (write) // - -void jerry_byte_write(unsigned offset, unsigned data) +void JERRYWriteByte(uint32 offset, uint8 data, uint32 who/*=UNKNOWN*/) { #ifdef JERRY_DEBUG - fprintf(log_get(),"jerry: writing byte %.2x at 0x%.6x\n",data,offset); + WriteLog("jerry: writing byte %.2x at 0x%.6x\n",data,offset); #endif - if ((offset >= dsp_control_ram_base) && (offset < dsp_control_ram_base+0x20)) + if ((offset >= DSP_CONTROL_RAM_BASE) && (offset < DSP_CONTROL_RAM_BASE+0x20)) { - dsp_byte_write(offset, data); + DSPWriteByte(offset, data, who); return; } - else if ((offset >= dsp_work_ram_base) && (offset < dsp_work_ram_base+0x2000)) + else if ((offset >= DSP_WORK_RAM_BASE) && (offset < DSP_WORK_RAM_BASE+0x2000)) { - dsp_byte_write(offset, data); + DSPWriteByte(offset, data, who); return; } + // SCLK ($F1A150--8 bits wide) +//NOTE: This should be taken care of in DAC... else if ((offset >= 0xF1A152) && (offset <= 0xF1A153)) { -// fprintf(log_get(),"i2s: writing 0x%.2x to SCLK\n",data); +// WriteLog("JERRY: Writing %02X to SCLK...\n", data); if ((offset & 0x03) == 2) jerry_i2s_interrupt_divide = (jerry_i2s_interrupt_divide & 0x00FF) | ((uint32)data << 8); else @@ -446,36 +615,67 @@ void jerry_byte_write(unsigned offset, unsigned data) jerry_i2s_interrupt_timer = -1; jerry_i2s_exec(0); - return; +// return; + } + // LTXD/RTXD/SCLK/SMODE $F1A148/4C/50/54 (really 16-bit registers...) + else if (offset >= 0xF1A148 && offset <= 0xF1A157) + { + DACWriteByte(offset, data, who); + return; } - else if ((offset >= 0xF10000) && (offset <= 0xF10007)) + else if (offset >= 0xF10000 && offset <= 0xF10007) { - switch(offset & 0x07) + 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: + JERRYPIT1Prescaler = (JERRYPIT1Prescaler & 0xFF00) | data; + JERRYResetPIT1(); + break; + case 2: + JERRYPIT1Divider = (JERRYPIT1Divider & 0x00FF) | (data << 8); + JERRYResetPIT1(); + break; + case 3: + JERRYPIT1Divider = (JERRYPIT1Divider & 0xFF00) | data; + JERRYResetPIT1(); + break; + case 4: + JERRYPIT2Prescaler = (JERRYPIT2Prescaler & 0x00FF) | (data << 8); + JERRYResetPIT2(); + break; + case 5: + JERRYPIT2Prescaler = (JERRYPIT2Prescaler & 0xFF00) | data; + JERRYResetPIT2(); + break; + case 6: + JERRYPIT2Divider = (JERRYPIT2Divider & 0x00FF) | (data << 8); + JERRYResetPIT2(); break; - case 1: { jerry_timer_1_prescaler=(jerry_timer_1_prescaler&0xff00)|(data); jerry_reset_timer_1(); return; } - case 2: { jerry_timer_1_divider=(jerry_timer_1_divider&0x00ff)|(data<<8); jerry_reset_timer_1(); return; } - case 3: { jerry_timer_1_divider=(jerry_timer_1_divider&0xff00)|(data); jerry_reset_timer_1(); return; } - case 4: { jerry_timer_2_prescaler=(jerry_timer_2_prescaler&0x00ff)|(data<<8); jerry_reset_timer_2(); return; } - case 5: { jerry_timer_2_prescaler=(jerry_timer_2_prescaler&0xff00)|(data); jerry_reset_timer_2(); return; } - case 6: { jerry_timer_2_divider=(jerry_timer_2_divider&0x00ff)|(data<<8); jerry_reset_timer_2(); return; } - case 7: { jerry_timer_2_divider=(jerry_timer_2_divider&0xff00)|(data); jerry_reset_timer_2(); return; } + case 7: + JERRYPIT2Divider = (JERRYPIT2Divider & 0xFF00) | data; + JERRYResetPIT2(); } return; } - else if ((offset >= 0xF10010) && (offset <= 0xF10015)) +/* else if ((offset >= 0xF10010) && (offset <= 0xF10015)) { clock_byte_write(offset, data); return; + }//*/ + // JERRY -> 68K interrupt enables/latches (need to be handled!) + else if (offset >= 0xF10020 && offset <= 0xF10023) + { +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); @@ -488,86 +688,105 @@ void jerry_byte_write(unsigned offset, unsigned data) return; } +//Need to protect write attempts to Wavetable ROM (F1D000-FFF) + if (offset >= 0xF1D000 && offset <= 0xF1DFFF) + return; + jerry_ram_8[offset & 0xFFFF] = data; } // // JERRY word access (write) // - -void jerry_word_write(unsigned offset, unsigned data) +void JERRYWriteWord(uint32 offset, uint16 data, uint32 who/*=UNKNOWN*/) { #ifdef JERRY_DEBUG - fprintf(log_get(), "JERRY: Writing word %04X at %06X\n", data, offset); + WriteLog( "JERRY: Writing word %04X at %06X\n", data, offset); #endif - if ((offset >= dsp_control_ram_base) && (offset < dsp_control_ram_base+0x20)) + if ((offset >= DSP_CONTROL_RAM_BASE) && (offset < DSP_CONTROL_RAM_BASE+0x20)) { - dsp_word_write(offset, data); + DSPWriteWord(offset, data, who); return; } - else if ((offset >= dsp_work_ram_base) && (offset < dsp_work_ram_base+0x2000)) + else if ((offset >= DSP_WORK_RAM_BASE) && (offset < DSP_WORK_RAM_BASE+0x2000)) { - dsp_word_write(offset, data); + DSPWriteWord(offset, data, who); return; } - else if (offset == 0xF1A152) +//NOTE: This should be taken care of in DAC... + else if (offset == 0xF1A152) // Bottom half of SCLK ($F1A150) { -// fprintf(log_get(),"i2s: writing 0x%.4x to SCLK\n",data); - jerry_i2s_interrupt_divide = data & 0xFF; + WriteLog("JERRY: Writing %04X to SCLK (by %s)...\n", data, whoName[who]); +//This should *only* be enabled when SMODE has its INTERNAL bit set! !!! FIX !!! + jerry_i2s_interrupt_divide = (uint8)data; jerry_i2s_interrupt_timer = -1; jerry_i2s_exec(0); + + DACWriteWord(offset, data, who); + return; } - else if ((offset >= 0xF10000) && (offset <= 0xF10007)) + // LTXD/RTXD/SCLK/SMODE $F1A148/4C/50/54 (really 16-bit registers...) + else if (offset >= 0xF1A148 && offset <= 0xF1A156) + { + DACWriteWord(offset, data, who); + return; + } + else if (offset >= 0xF10000 && offset <= 0xF10007) { 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; } - else if ((offset >= 0xF1A148) && (offset < 0xF1A150)) - { - pcm_word_write(offset - 0xF1A148, data); - return; - } - else if ((offset >= 0xF10010) && (offset < 0xF10016)) +/* else if (offset >= 0xF10010 && offset < 0xF10016) { clock_word_write(offset, data); return; + }//*/ + // JERRY -> 68K interrupt enables/latches (need to be handled!) + else if (offset >= 0xF10020 && offset <= 0xF10022) + { +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)) + }*/ + else if (offset >= 0xF14000 && offset < 0xF14003) { joystick_word_write(offset, data); eeprom_word_write(offset, data); return; } - else if ((offset >= 0xF14000) && (offset <= 0xF1A0FF)) + else if (offset >= 0xF14000 && offset <= 0xF1A0FF) { eeprom_word_write(offset, data); return; } +//Need to protect write attempts to Wavetable ROM (F1D000-FFF) + if (offset >= 0xF1D000 && offset <= 0xF1DFFF) + return; + jerry_ram_8[(offset+0) & 0xFFFF] = (data >> 8) & 0xFF; jerry_ram_8[(offset+1) & 0xFFFF] = data & 0xFF; }