2009-05-21 17:48:31 +02:00
|
|
|
/* gameplaySP
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Exophase <exophase@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
2014-12-09 00:17:28 +01:00
|
|
|
#include <ctype.h>
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
timer_type timer[4];
|
|
|
|
|
|
|
|
u32 global_cycles_per_instruction = 1;
|
2013-03-04 22:28:03 +01:00
|
|
|
|
2009-05-21 17:48:31 +02:00
|
|
|
u32 cpu_ticks = 0;
|
|
|
|
|
|
|
|
u32 execute_cycles = 960;
|
|
|
|
s32 video_count = 960;
|
|
|
|
u32 ticks;
|
|
|
|
|
|
|
|
u32 arm_frame = 0;
|
|
|
|
u32 thumb_frame = 0;
|
|
|
|
u32 last_frame = 0;
|
|
|
|
|
|
|
|
u32 flush_ram_count = 0;
|
|
|
|
u32 gbc_update_count = 0;
|
|
|
|
u32 oam_update_count = 0;
|
|
|
|
|
2011-09-05 18:31:58 +02:00
|
|
|
char main_path[512];
|
2014-12-10 12:00:12 +01:00
|
|
|
char save_path[512];
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2014-12-10 15:47:19 +01:00
|
|
|
void trigger_ext_event(void);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2014-12-11 02:29:51 +01:00
|
|
|
static void update_timers(irq_type *irq_raised)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if(timer[i].status == TIMER_INACTIVE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(timer[i].status != TIMER_CASCADE)
|
|
|
|
{
|
|
|
|
timer[i].count -= execute_cycles;
|
2014-12-11 02:32:14 +01:00
|
|
|
/* io_registers accessors range: REG_TM0D, REG_TM1D, REG_TM2D, REG_TM3D */
|
2021-06-27 01:16:28 +02:00
|
|
|
write_ioreg(REG_TM0D + (i * 2), -(timer[i].count > timer[i].prescale));
|
2014-12-11 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(timer[i].count > 0)
|
|
|
|
continue;
|
|
|
|
|
2014-12-11 02:32:14 +01:00
|
|
|
/* irq_raised value range: IRQ_TIMER0, IRQ_TIMER1, IRQ_TIMER2, IRQ_TIMER3 */
|
2014-12-11 02:29:51 +01:00
|
|
|
if(timer[i].irq == TIMER_TRIGGER_IRQ)
|
2014-12-11 02:32:14 +01:00
|
|
|
*irq_raised |= (8 << i);
|
2014-12-11 02:29:51 +01:00
|
|
|
|
|
|
|
if((i != 3) && (timer[i + 1].status == TIMER_CASCADE))
|
|
|
|
{
|
|
|
|
timer[i + 1].count--;
|
2021-06-27 01:16:28 +02:00
|
|
|
write_ioreg(REG_TM0D + (i + 1) * 2, -timer[i+1].count);
|
2014-12-11 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(i < 2)
|
|
|
|
{
|
|
|
|
if(timer[i].direct_sound_channels & 0x01)
|
|
|
|
sound_timer(timer[i].frequency_step, 0);
|
|
|
|
|
|
|
|
if(timer[i].direct_sound_channels & 0x02)
|
|
|
|
sound_timer(timer[i].frequency_step, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer[i].count += (timer[i].reload << timer[i].prescale);
|
|
|
|
}
|
|
|
|
}
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2014-12-10 15:47:19 +01:00
|
|
|
void init_main(void)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
for(i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
dma[i].start_type = DMA_INACTIVE;
|
|
|
|
dma[i].direct_sound_channel = DMA_NO_DIRECT_SOUND;
|
|
|
|
timer[i].status = TIMER_INACTIVE;
|
|
|
|
timer[i].reload = 0x10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer[0].direct_sound_channels = TIMER_DS_CHANNEL_BOTH;
|
|
|
|
timer[1].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
|
|
|
|
|
|
|
|
cpu_ticks = 0;
|
|
|
|
|
|
|
|
execute_cycles = 960;
|
|
|
|
video_count = 960;
|
|
|
|
|
2014-12-10 17:06:09 +01:00
|
|
|
#ifdef HAVE_DYNAREC
|
2021-03-30 21:06:52 +02:00
|
|
|
init_caches();
|
2021-03-03 01:38:09 +01:00
|
|
|
init_emitter();
|
2014-12-10 17:06:09 +01:00
|
|
|
#endif
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:47:19 +01:00
|
|
|
u32 update_gba(void)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
irq_type irq_raised = IRQ_NONE;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-12-11 02:29:51 +01:00
|
|
|
unsigned i;
|
2009-05-21 17:48:31 +02:00
|
|
|
cpu_ticks += execute_cycles;
|
|
|
|
|
|
|
|
reg[CHANGED_PC_STATUS] = 0;
|
2021-03-08 18:44:03 +01:00
|
|
|
reg[COMPLETED_FRAME] = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
if(gbc_sound_update)
|
|
|
|
{
|
|
|
|
gbc_update_count++;
|
|
|
|
update_gbc_sound(cpu_ticks);
|
|
|
|
gbc_sound_update = 0;
|
|
|
|
}
|
|
|
|
|
2014-12-11 02:29:51 +01:00
|
|
|
update_timers(&irq_raised);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
video_count -= execute_cycles;
|
|
|
|
|
|
|
|
if(video_count <= 0)
|
|
|
|
{
|
2021-06-27 01:16:28 +02:00
|
|
|
u32 vcount = read_ioreg(REG_VCOUNT);
|
|
|
|
u32 dispstat = read_ioreg(REG_DISPSTAT);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
if((dispstat & 0x02) == 0)
|
|
|
|
{
|
|
|
|
// Transition from hrefresh to hblank
|
|
|
|
video_count += (272);
|
|
|
|
dispstat |= 0x02;
|
|
|
|
|
|
|
|
if((dispstat & 0x01) == 0)
|
|
|
|
{
|
|
|
|
u32 i;
|
2021-03-25 21:02:06 +01:00
|
|
|
if(reg[OAM_UPDATED])
|
2009-05-21 17:48:31 +02:00
|
|
|
oam_update_count++;
|
|
|
|
|
|
|
|
update_scanline();
|
|
|
|
|
|
|
|
// If in visible area also fire HDMA
|
|
|
|
for(i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if(dma[i].start_type == DMA_START_HBLANK)
|
2021-08-21 18:10:12 +02:00
|
|
|
dma_transfer(&dma[i]);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dispstat & 0x10)
|
|
|
|
irq_raised |= IRQ_HBLANK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Transition from hblank to next line
|
|
|
|
video_count += 960;
|
|
|
|
dispstat &= ~0x02;
|
|
|
|
|
|
|
|
vcount++;
|
|
|
|
|
|
|
|
if(vcount == 160)
|
|
|
|
{
|
|
|
|
// Transition from vrefresh to vblank
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
dispstat |= 0x01;
|
|
|
|
if(dispstat & 0x8)
|
|
|
|
{
|
|
|
|
irq_raised |= IRQ_VBLANK;
|
|
|
|
}
|
|
|
|
|
2021-08-15 23:27:04 +02:00
|
|
|
video_reload_counters();
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
for(i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if(dma[i].start_type == DMA_START_VBLANK)
|
2021-08-21 18:10:12 +02:00
|
|
|
dma_transfer(&dma[i]);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
if(vcount == 228)
|
|
|
|
{
|
|
|
|
// Transition from vblank to next screen
|
|
|
|
dispstat &= ~0x01;
|
|
|
|
|
2011-09-07 22:45:45 +02:00
|
|
|
/* printf("frame update (%x), %d instructions total, %d RAM flushes\n",
|
2009-05-21 17:48:31 +02:00
|
|
|
reg[REG_PC], instruction_count - last_frame, flush_ram_count);
|
|
|
|
last_frame = instruction_count;
|
2011-09-07 22:45:45 +02:00
|
|
|
*/
|
2009-05-21 17:48:31 +02:00
|
|
|
/* printf("%d gbc audio updates\n", gbc_update_count);
|
|
|
|
printf("%d oam updates\n", oam_update_count); */
|
|
|
|
gbc_update_count = 0;
|
|
|
|
oam_update_count = 0;
|
|
|
|
flush_ram_count = 0;
|
|
|
|
|
2014-12-09 01:59:02 +01:00
|
|
|
update_gbc_sound(cpu_ticks);
|
|
|
|
gbc_sound_update = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2021-05-05 02:20:00 +02:00
|
|
|
/* If there's no cheat hook, run on vblank! */
|
|
|
|
if (cheat_master_hook == ~0U)
|
|
|
|
process_cheats();
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
vcount = 0;
|
2021-03-08 18:44:03 +01:00
|
|
|
// We completed a frame, tell the dynarec to exit to the main thread
|
|
|
|
reg[COMPLETED_FRAME] = 1;
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(vcount == (dispstat >> 8))
|
|
|
|
{
|
|
|
|
// vcount trigger
|
|
|
|
dispstat |= 0x04;
|
|
|
|
if(dispstat & 0x20)
|
|
|
|
{
|
|
|
|
irq_raised |= IRQ_VCOUNT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dispstat &= ~0x04;
|
|
|
|
|
2021-06-27 01:16:28 +02:00
|
|
|
write_ioreg(REG_VCOUNT, vcount);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
2021-06-27 01:16:28 +02:00
|
|
|
write_ioreg(REG_DISPSTAT, dispstat);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(irq_raised)
|
|
|
|
raise_interrupt(irq_raised);
|
|
|
|
|
|
|
|
execute_cycles = video_count;
|
|
|
|
|
2014-12-11 02:29:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if(timer[i].status != TIMER_PRESCALE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(timer[i].count < execute_cycles)
|
|
|
|
execute_cycles = timer[i].count;
|
|
|
|
}
|
2021-03-08 18:44:03 +01:00
|
|
|
} while(reg[CPU_HALT_STATE] != CPU_ACTIVE && !reg[COMPLETED_FRAME]);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
return execute_cycles;
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:47:19 +01:00
|
|
|
void reset_gba(void)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
init_main();
|
|
|
|
init_memory();
|
|
|
|
init_cpu();
|
|
|
|
reset_sound();
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:39:43 +01:00
|
|
|
u32 file_length(FILE *fp)
|
2019-09-19 18:48:21 +02:00
|
|
|
{
|
2009-05-21 17:48:31 +02:00
|
|
|
u32 length;
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
length = ftell(fp);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
return length;
|
2014-12-09 21:48:21 +01:00
|
|
|
}
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2011-09-05 18:31:58 +02:00
|
|
|
void change_ext(const char *src, char *buffer, const char *extension)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
2011-09-05 18:31:58 +02:00
|
|
|
char *dot_position;
|
2009-05-21 17:48:31 +02:00
|
|
|
strcpy(buffer, src);
|
|
|
|
dot_position = strrchr(buffer, '.');
|
|
|
|
|
|
|
|
if(dot_position)
|
|
|
|
strcpy(dot_position, extension);
|
|
|
|
}
|
|
|
|
|
2014-12-10 11:06:17 +01:00
|
|
|
#define main_savestate_builder(type) \
|
|
|
|
void main_##type##_savestate(void) \
|
|
|
|
{ \
|
|
|
|
state_mem_##type##_variable(cpu_ticks); \
|
|
|
|
state_mem_##type##_variable(execute_cycles); \
|
|
|
|
state_mem_##type##_variable(video_count); \
|
|
|
|
state_mem_##type##_array(timer); \
|
|
|
|
}
|
|
|
|
|
|
|
|
main_savestate_builder(read)
|
|
|
|
main_savestate_builder(write)
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
|