Files
Main_MiSTer/support/saturn/saturn.cpp
2022-05-22 20:09:13 +08:00

231 lines
6.6 KiB
C++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "../../file_io.h"
#include "../../user_io.h"
#include "../../spi.h"
#include "../../hardware.h"
#include "../../menu.h"
#include "../../cheats.h"
#include "saturn.h"
static int need_reset = 0;
uint32_t frame_cnt = 0;
static uint32_t CalcTimerOffset(uint8_t speed) {
static uint8_t adj = 0x1;
uint32_t offs;
if (speed == 2) {
offs = 6 + ((adj & 0x03) != 0x00 ? 1 : 0); //6.6
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
}
else if (speed == 1) {
offs = 13 + ((adj & 0x01) != 0x00 ? 1 : 0); //13.3
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
}
else {
offs = 16 + ((adj & 0x03) != 0x00 ? 1 : 0); //16.7
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
}
return offs;
}
void saturn_poll()
{
static unsigned long poll_timer = 0;
static uint8_t last_req = 255;
if (!poll_timer || CheckTimer(poll_timer))
{
poll_timer = GetTimer(0);
uint16_t data_in[6];
uint8_t req = spi_uio_cmd_cont(UIO_CD_GET);
if (req != last_req)
{
last_req = req;
for (int i = 0; i < 6; i++) data_in[i] = spi_w(0);
DisableIO();
satcdd.SetCommand((uint8_t*)data_in);
satcdd.CommandExec();
}
else
DisableIO();
uint8_t time_mode;
satcdd.Process(&time_mode);
poll_timer += CalcTimerOffset(time_mode);
uint16_t* s = (uint16_t*)satcdd.GetStatus();
spi_uio_cmd_cont(UIO_CD_SET);
for (int i = 0; i < 6; i++) spi_w(s[i]);
DisableIO();
satcdd.Update();
frame_cnt++;
}
}
static char buf[1024];
static void saturn_mount_save(const char *filename)
{
/*user_io_set_index(SAVE_IO_INDEX);
user_io_set_download(1);
if (strlen(filename))
{
FileGenerateSavePath(filename, buf);
user_io_file_mount(buf, 0, 1);
}
else
{
user_io_file_mount("");
}
user_io_set_download(0);*/
}
static int saturn_load_rom(const char *basename, const char *name, int sub_index)
{
strcpy(buf, basename);
char *p = strrchr(buf, '/');
if (p)
{
p++;
strcpy(p, name);
if (user_io_file_tx(buf, sub_index << 6)) return 1;
}
return 0;
}
void saturn_set_image(int num, const char *filename)
{
static char last_dir[1024] = {};
(void)num;
satcdd.Unload();
satcdd.state = Open;
int same_game = *filename && *last_dir && !strncmp(last_dir, filename, strlen(last_dir));
strcpy(last_dir, filename);
char *p = strrchr(last_dir, '/');
if (p) *p = 0;
int loaded = 1;
if (!same_game)
{
saturn_mount_save("");
user_io_status_set("[0]", 1);
user_io_status_set("[0]", 0);
saturn_reset();
loaded = 0;
strcpy(buf, last_dir);
char *p = strrchr(buf, '/');
if (p)
{
strcpy(p + 1, "cd_bios.rom");
loaded = user_io_file_tx(buf);
}
if (!loaded)
{
sprintf(buf, "%s/boot.rom", HomeDir());
loaded = user_io_file_tx(buf);
}
if (!loaded) Info("CD BIOS not found!", 4000);
}
if (strlen(filename))
{
if (satcdd.Load(filename) > 0)
{
satcdd.state = satcdd.loaded ? Stop : Open;
satcdd.SendData = saturn_send_data;
if (!same_game)
{
saturn_load_rom(filename, "cd_bios.rom", 0);
//saturn_load_rom(filename, "cart.rom", 1);
saturn_mount_save(filename);
//cheats_init(filename, 0);
}
}
else
{
satcdd.state = Open;
}
}
}
void saturn_reset() {
need_reset = 1;
}
int saturn_send_data(uint8_t* buf, int len, uint8_t index) {
// set index byte
user_io_set_index(index);
user_io_set_download(1);
user_io_file_tx_data(buf, len);
user_io_set_download(0);
return 1;
}
static char int_blank[] = {
0x42, 0x61, 0x63, 0x6B, 0x55, 0x70, 0x52, 0x61, 0x6D, 0x20, 0x46, 0x6F, 0x72, 0x6D, 0x61, 0x74,
0x42, 0x61, 0x63, 0x6B, 0x55, 0x70, 0x52, 0x61, 0x6D, 0x20, 0x46, 0x6F, 0x72, 0x6D, 0x61, 0x74,
0x42, 0x61, 0x63, 0x6B, 0x55, 0x70, 0x52, 0x61, 0x6D, 0x20, 0x46, 0x6F, 0x72, 0x6D, 0x61, 0x74,
0x42, 0x61, 0x63, 0x6B, 0x55, 0x70, 0x52, 0x61, 0x6D, 0x20, 0x46, 0x6F, 0x72, 0x6D, 0x61, 0x74,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void saturn_fill_blanksave(uint8_t *buffer, uint32_t lba)
{
if (lba == 0)
{
memcpy(buffer, int_blank, 512);
}
else
{
memset(buffer, 0, 512);
}
}