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 cpu_ticks = 0;
|
2023-06-15 20:12:39 +02:00
|
|
|
u32 execute_cycles = 0;
|
|
|
|
s32 video_count = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-21 20:05:44 +01:00
|
|
|
static unsigned update_timers(irq_type *irq_raised, unsigned completed_cycles)
|
2014-12-11 02:29:51 +01:00
|
|
|
{
|
2021-12-16 20:10:00 +01:00
|
|
|
unsigned i, ret = 0;
|
2014-12-11 02:29:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if(timer[i].status == TIMER_INACTIVE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(timer[i].status != TIMER_CASCADE)
|
|
|
|
{
|
2021-12-21 19:59:33 +01:00
|
|
|
timer[i].count -= completed_cycles;
|
2014-12-11 02:32:14 +01:00
|
|
|
/* io_registers accessors range: REG_TM0D, REG_TM1D, REG_TM2D, REG_TM3D */
|
2023-04-19 20:00:28 +02:00
|
|
|
write_ioreg(REG_TMXD(i), -(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 */
|
2021-10-15 21:28:40 +02:00
|
|
|
if(timer[i].irq)
|
2023-04-19 20:00:28 +02:00
|
|
|
*irq_raised |= (IRQ_TIMER0 << i);
|
2014-12-11 02:29:51 +01:00
|
|
|
|
|
|
|
if((i != 3) && (timer[i + 1].status == TIMER_CASCADE))
|
|
|
|
{
|
|
|
|
timer[i + 1].count--;
|
2023-04-19 20:00:28 +02:00
|
|
|
write_ioreg(REG_TMXD(i + 1), -timer[i+1].count);
|
2014-12-11 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(i < 2)
|
|
|
|
{
|
|
|
|
if(timer[i].direct_sound_channels & 0x01)
|
2021-12-16 20:10:00 +01:00
|
|
|
ret += sound_timer(timer[i].frequency_step, 0);
|
2014-12-11 02:29:51 +01:00
|
|
|
|
|
|
|
if(timer[i].direct_sound_channels & 0x02)
|
2021-12-16 20:10:00 +01:00
|
|
|
ret += sound_timer(timer[i].frequency_step, 1);
|
2014-12-11 02:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
timer[i].count += (timer[i].reload << timer[i].prescale);
|
|
|
|
}
|
2021-12-16 20:10:00 +01:00
|
|
|
return ret;
|
2014-12-11 02:29:51 +01:00
|
|
|
}
|
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++)
|
|
|
|
{
|
|
|
|
timer[i].status = TIMER_INACTIVE;
|
2023-06-12 20:24:18 +02:00
|
|
|
timer[i].prescale = 0;
|
|
|
|
timer[i].irq = 0;
|
|
|
|
timer[i].reload = timer[i].count = 0x10000;
|
|
|
|
timer[i].direct_sound_channels = TIMER_DS_CHANNEL_NONE;
|
|
|
|
timer[i].frequency_step = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-06-09 20:21:35 +02:00
|
|
|
init_dynarec_caches();
|
2023-03-03 20:59:08 +01:00
|
|
|
init_emitter(gamepak_must_swap());
|
2014-12-10 17:06:09 +01:00
|
|
|
#endif
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 00:16:39 +01:00
|
|
|
u32 function_cc update_gba(int remaining_cycles)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
2023-04-24 20:24:03 +02:00
|
|
|
u32 changed_pc = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
irq_type irq_raised = IRQ_NONE;
|
2021-12-16 20:10:00 +01:00
|
|
|
int dma_cycles;
|
2023-04-15 02:02:17 +02:00
|
|
|
trace_update_gba(remaining_cycles);
|
|
|
|
|
2021-12-21 19:59:33 +01:00
|
|
|
remaining_cycles = MAX(remaining_cycles, -64);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-12-11 02:29:51 +01:00
|
|
|
unsigned i;
|
2021-12-21 19:59:33 +01:00
|
|
|
// Number of cycles we ask to run - cycles that we did not execute
|
|
|
|
// (remaining_cycles can be negative and should be close to zero)
|
|
|
|
unsigned completed_cycles = execute_cycles - remaining_cycles;
|
|
|
|
cpu_ticks += completed_cycles;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2021-12-21 19:59:33 +01:00
|
|
|
remaining_cycles = 0;
|
2021-03-08 18:44:03 +01:00
|
|
|
reg[COMPLETED_FRAME] = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2021-12-16 20:10:00 +01:00
|
|
|
// Timers can trigger DMA (usually sound) and consume cycles
|
2021-12-21 20:05:44 +01:00
|
|
|
dma_cycles = update_timers(&irq_raised, completed_cycles);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Video count tracks the video cycles remaining until the next event
|
2021-12-21 19:59:33 +01:00
|
|
|
video_count -= completed_cycles;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Ran out of cycles, move to the next video area
|
2009-05-21 17:48:31 +02:00
|
|
|
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
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Check if we are in hrefresh (0) or hblank (1)
|
|
|
|
if ((dispstat & 0x02) == 0)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
// Transition from hrefresh to hblank
|
|
|
|
dispstat |= 0x02;
|
2023-06-15 20:12:39 +02:00
|
|
|
video_count += (272); // hblank duration, 272 cycles
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Check if we are drawing (0) or we are in vblank (1)
|
|
|
|
if ((dispstat & 0x01) == 0)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
u32 i;
|
2023-06-15 20:12:39 +02:00
|
|
|
|
|
|
|
// Render the scan line
|
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();
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Trigger the HBlank DMAs if enabled
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
if(dma[i].start_type == DMA_START_HBLANK)
|
2021-12-16 20:10:00 +01:00
|
|
|
dma_transfer(i, &dma_cycles);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Trigger the hblank interrupt, if enabled in DISPSTAT
|
|
|
|
if (dispstat & 0x10)
|
2009-05-21 17:48:31 +02:00
|
|
|
irq_raised |= IRQ_HBLANK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-15 20:12:39 +02:00
|
|
|
// Transition from hblank to the next scan line (vdraw or vblank)
|
2009-05-21 17:48:31 +02:00
|
|
|
video_count += 960;
|
|
|
|
dispstat &= ~0x02;
|
|
|
|
vcount++;
|
|
|
|
|
|
|
|
if(vcount == 160)
|
|
|
|
{
|
|
|
|
// Transition from vrefresh to vblank
|
|
|
|
u32 i;
|
|
|
|
dispstat |= 0x01;
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Reinit affine transformation counters for the next frame
|
2021-08-15 23:27:04 +02:00
|
|
|
video_reload_counters();
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Trigger VBlank interrupt if enabled
|
|
|
|
if (dispstat & 0x8)
|
|
|
|
irq_raised |= IRQ_VBLANK;
|
|
|
|
|
|
|
|
// Trigger the VBlank DMAs if enabled
|
|
|
|
for (i = 0; i < 4; i++)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
if(dma[i].start_type == DMA_START_VBLANK)
|
2021-12-16 20:10:00 +01:00
|
|
|
dma_transfer(i, &dma_cycles);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-15 20:12:39 +02:00
|
|
|
else if (vcount == 228)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
// Transition from vblank to next screen
|
2023-06-15 20:12:39 +02:00
|
|
|
vcount = 0;
|
2009-05-21 17:48:31 +02:00
|
|
|
dispstat &= ~0x01;
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
/* If there's no cheat hook, run on vblank! */
|
|
|
|
if (cheat_master_hook == ~0U)
|
|
|
|
process_cheats();
|
|
|
|
|
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;
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Force audio generation. Need to flush samples for this frame.
|
|
|
|
render_gbc_sound();
|
2009-05-21 17:48:31 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Vcount trigger (flag) and IRQ if enabled
|
2009-05-21 17:48:31 +02:00
|
|
|
if(vcount == (dispstat >> 8))
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Flag any V/H blank interrupts, DMA IRQs, Vcount, etc.
|
2023-04-14 01:41:55 +02:00
|
|
|
if (irq_raised)
|
|
|
|
flag_interrupt(irq_raised);
|
|
|
|
|
|
|
|
// Raise any pending interrupts. This changes the CPU mode.
|
2023-04-24 20:24:03 +02:00
|
|
|
if (check_and_raise_interrupts())
|
|
|
|
changed_pc = 0x80000000;
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2023-06-15 20:12:39 +02:00
|
|
|
// Figure out when we need to stop CPU execution. The next event is
|
|
|
|
// a video event or a timer event, whatever happens first.
|
2021-12-21 19:59:33 +01:00
|
|
|
execute_cycles = MAX(video_count, 0);
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2014-12-11 02:29:51 +01:00
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2023-04-19 20:00:28 +02:00
|
|
|
if (timer[i].status == TIMER_PRESCALE &&
|
|
|
|
timer[i].count < execute_cycles)
|
2014-12-11 02:29:51 +01:00
|
|
|
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
|
|
|
|
2021-12-16 20:10:00 +01:00
|
|
|
// We voluntarily limit this. It is not accurate but it would be much harder.
|
|
|
|
dma_cycles = MIN(64, dma_cycles);
|
|
|
|
dma_cycles = MIN(execute_cycles, dma_cycles);
|
|
|
|
|
2023-04-24 20:24:03 +02:00
|
|
|
return (execute_cycles - dma_cycles) | changed_pc;
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-10 15:47:19 +01:00
|
|
|
void reset_gba(void)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
|
|
|
init_memory();
|
2021-09-10 00:30:55 +02:00
|
|
|
init_main();
|
2009-05-21 17:48:31 +02:00
|
|
|
init_cpu();
|
|
|
|
reset_sound();
|
|
|
|
}
|
|
|
|
|
2022-04-09 08:50:38 +02:00
|
|
|
#ifdef TRACE_REGISTERS
|
2021-12-11 11:27:59 +01:00
|
|
|
void print_regs(void)
|
2009-05-21 17:48:31 +02:00
|
|
|
{
|
2021-12-11 11:27:59 +01:00
|
|
|
printf("R0=%08x R1=%08x R2=%08x R3=%08x "
|
|
|
|
"R4=%08x R5=%08x R6=%08x R7=%08x "
|
|
|
|
"R8=%08x R9=%08x R10=%08x R11=%08x "
|
|
|
|
"R12=%08x R13=%08x R14=%08x\n",
|
|
|
|
reg[0], reg[1], reg[2], reg[3],
|
|
|
|
reg[4], reg[5], reg[6], reg[7],
|
|
|
|
reg[8], reg[9], reg[10], reg[11],
|
|
|
|
reg[12], reg[13], reg[14]);
|
2009-05-21 17:48:31 +02:00
|
|
|
}
|
2022-04-09 08:50:38 +02:00
|
|
|
#endif
|
2009-05-21 17:48:31 +02:00
|
|
|
|
2021-08-24 17:15:27 +02:00
|
|
|
bool main_read_savestate(const u8 *src)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const u8 *p1 = bson_find_key(src, "emu");
|
|
|
|
const u8 *p2 = bson_find_key(src, "timers");
|
|
|
|
if (!p1 || !p2)
|
|
|
|
return false;
|
2021-12-16 20:10:00 +01:00
|
|
|
|
2021-08-24 17:15:27 +02:00
|
|
|
if (!(bson_read_int32(p1, "cpu-ticks", &cpu_ticks) &&
|
|
|
|
bson_read_int32(p1, "exec-cycles", &execute_cycles) &&
|
|
|
|
bson_read_int32(p1, "video-count", (u32*)&video_count)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
char tname[2] = {'0' + i, 0};
|
|
|
|
const u8 *p = bson_find_key(p2, tname);
|
|
|
|
|
|
|
|
if (!(
|
|
|
|
bson_read_int32(p, "count", (u32*)&timer[i].count) &&
|
|
|
|
bson_read_int32(p, "reload", &timer[i].reload) &&
|
|
|
|
bson_read_int32(p, "prescale", &timer[i].prescale) &&
|
|
|
|
bson_read_int32(p, "freq-step", &timer[i].frequency_step) &&
|
|
|
|
bson_read_int32(p, "dsc", &timer[i].direct_sound_channels) &&
|
|
|
|
bson_read_int32(p, "irq", &timer[i].irq) &&
|
|
|
|
bson_read_int32(p, "status", &timer[i].status)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-12-10 11:06:17 +01:00
|
|
|
}
|
|
|
|
|
2021-08-24 17:15:27 +02:00
|
|
|
unsigned main_write_savestate(u8* dst)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u8 *wbptr, *wbptr2, *startp = dst;
|
|
|
|
bson_start_document(dst, "emu", wbptr);
|
|
|
|
bson_write_int32(dst, "cpu-ticks", cpu_ticks);
|
|
|
|
bson_write_int32(dst, "exec-cycles", execute_cycles);
|
|
|
|
bson_write_int32(dst, "video-count", video_count);
|
|
|
|
bson_finish_document(dst, wbptr);
|
|
|
|
|
|
|
|
bson_start_document(dst, "timers", wbptr);
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
char tname[2] = {'0' + i, 0};
|
|
|
|
bson_start_document(dst, tname, wbptr2);
|
|
|
|
bson_write_int32(dst, "count", timer[i].count);
|
|
|
|
bson_write_int32(dst, "reload", timer[i].reload);
|
|
|
|
bson_write_int32(dst, "prescale", timer[i].prescale);
|
|
|
|
bson_write_int32(dst, "freq-step", timer[i].frequency_step);
|
|
|
|
bson_write_int32(dst, "dsc", timer[i].direct_sound_channels);
|
|
|
|
bson_write_int32(dst, "irq", timer[i].irq);
|
|
|
|
bson_write_int32(dst, "status", timer[i].status);
|
|
|
|
bson_finish_document(dst, wbptr2);
|
|
|
|
}
|
|
|
|
bson_finish_document(dst, wbptr);
|
|
|
|
|
|
|
|
return (unsigned int)(dst - startp);
|
|
|
|
}
|
2009-05-21 17:48:31 +02:00
|
|
|
|
|
|
|
|