Skip to content

Commit

Permalink
saturn: rework CD drive (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
srg320 committed Sep 27, 2022
1 parent 801a5ea commit 3c0c664
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 264 deletions.
15 changes: 8 additions & 7 deletions cd.h
Expand Up @@ -14,6 +14,7 @@ typedef struct
{
fileTYPE f;
int offset;
int pregap;
int start;
int end;
int type;
Expand All @@ -30,6 +31,13 @@ typedef struct
chd_file *chd_f;
cd_track_t tracks[100];
fileTYPE sub;

int GetTrackByLBA(int lba)
{
int i = 0;
while ((this->tracks[i].end <= lba) && (i < this->last)) i++;
return i;
}
} toc_t;

typedef struct
Expand All @@ -43,11 +51,4 @@ typedef struct

typedef int (*SendDataFunc) (uint8_t* buf, int len, uint8_t index);

inline int FindIndexInTOC(toc_t* toc, int lba)
{
int index = 0;
while ((toc->tracks[index].end <= lba) && (index < toc->last)) index++;
return index;
}

#endif
59 changes: 31 additions & 28 deletions support/saturn/saturn.cpp
Expand Up @@ -13,25 +13,29 @@

static int need_reset = 0;
uint32_t frame_cnt = 0;
uint8_t time_mode;

static uint32_t CalcTimerOffset(uint8_t speed) {
static uint8_t adj = 0x1;
static uint8_t adj0 = 0x1, adj1 = 0x1, adj2 = 0x1;

uint32_t offs;
if (speed == 2) {
offs = 6 + ((adj & 0x03) != 0x00 ? 1 : 0); //6.6
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
offs = 6 + ((adj2 & 0x03) != 0x00 ? 1 : 0); //6.6
adj2 <<= 1;
if (adj2 >= 0x08) adj2 = 0x01;
adj0 = adj1 = 0x1;
}
else if (speed == 1) {
offs = 13 + ((adj & 0x01) != 0x00 ? 1 : 0); //13.3
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
offs = 13 + ((adj1 & 0x01) != 0x00 ? 1 : 0); //13.3
adj1 <<= 1;
if (adj1 >= 0x08) adj1 = 0x01;
adj0 = adj2 = 0x1;
}
else {
offs = 16 + ((adj & 0x03) != 0x00 ? 1 : 0); //16.7
adj <<= 1;
if (adj >= 0x08) adj = 0x01;
offs = 16 + ((adj0 & 0x03) != 0x00 ? 1 : 0); //16.7
adj0 <<= 1;
if (adj0 >= 0x08) adj0 = 0x01;
adj1 = adj2 = 0x1;
}
return offs;
}
Expand All @@ -50,7 +54,7 @@ void saturn_poll()
if (req != last_req)
{
last_req = req;

for (int i = 0; i < 6; i++) data_in[i] = spi_w(0);
DisableIO();

Expand All @@ -60,9 +64,7 @@ void saturn_poll()
else
DisableIO();

uint8_t time_mode;
satcdd.Process(&time_mode);

poll_timer += CalcTimerOffset(time_mode);

uint16_t* s = (uint16_t*)satcdd.GetStatus();
Expand All @@ -71,16 +73,23 @@ void saturn_poll()
DisableIO();

satcdd.Update();

frame_cnt++;

#ifdef SATURN_DEBUG
unsigned long curr_timer = GetTimer(0);
if (curr_timer >= poll_timer) {
printf("\x1b[32mSaturn: ");
printf("Time over: next = %u, curr = %u", poll_timer, curr_timer);
printf("\n\x1b[0m");
}
#endif // SATURN_DEBUG
}
}

static char buf[1024];
/*
static void saturn_mount_save(const char *filename)
{
user_io_set_index(SAVE_IO_INDEX);
/*user_io_set_index(SAVE_IO_INDEX);
user_io_set_download(1);
if (strlen(filename))
{
Expand All @@ -91,9 +100,8 @@ static void saturn_mount_save(const char *filename)
{
user_io_file_mount("");
}
user_io_set_download(0);
user_io_set_download(0);*/
}
*/

static int saturn_load_rom(const char *basename, const char *name, int sub_index)
{
Expand All @@ -110,12 +118,12 @@ static int saturn_load_rom(const char *basename, const char *name, int sub_index
}
void saturn_set_image(int num, const char *filename)
{
static char last_dir[1024] = {};

static char last_dir[1024] = {};
(void)num;

satcdd.Unload();
satcdd.state = Open;
satcdd.Reset();

int same_game = *filename && *last_dir && !strncmp(last_dir, filename, strlen(last_dir));
strcpy(last_dir, filename);
Expand All @@ -125,7 +133,7 @@ void saturn_set_image(int num, const char *filename)
int loaded = 1;
if (!same_game)
{
//saturn_mount_save("");
saturn_mount_save("");

user_io_status_set("[0]", 1);
user_io_status_set("[0]", 0);
Expand Down Expand Up @@ -153,21 +161,16 @@ void saturn_set_image(int num, const char *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);
saturn_mount_save(filename);
//cheats_init(filename, 0);
}
}
else
{
satcdd.state = Open;
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions support/saturn/saturn.h
Expand Up @@ -3,6 +3,8 @@

#include "../../cd.h"

//#define SATURN_DEBUG 1

// CDD command
#define SATURN_COMM_NOP 0x00
#define SATURN_COMM_SEEK_RING 0x02
Expand All @@ -20,19 +22,17 @@
#define SATURN_STAT_STOP 0x12
#define SATURN_STAT_SEEK 0x22
#define SATURN_STAT_AUDIO 0x34
#define SATURN_STAT_DATA 0x36
#define SATURN_STAT_IDLE 0x46
#define SATURN_STAT_DATA 0x32
#define SATURN_STAT_IDLE 0x42
#define SATURN_STAT_OPEN 0x80
#define SATURN_STAT_NODISK 0x83
#define SATURN_STAT_SEEK_RING 0xB2
#define SATURN_STAT_SEEK_RING2 0xB6

typedef enum {
Idle,
Open,
ReadTOC,
Read,
Play,
Pause,
Stop,
Seek,
Expand Down Expand Up @@ -61,11 +61,19 @@ class satcdd_t
private:
toc_t toc;
int lba;
int index;
int track;
int seek_lba;
uint16_t sectorSize;
int toc_pos;
satstate_t next_state;
bool lid_open;
bool stop_pend;
bool seek_pend;
bool read_pend;
bool seek_ring;
bool seek_ring2;
bool pause_pend;
bool read_toc;
uint8_t stat[12];
uint8_t comm[12];
uint8_t cd_buf[4096 + 2];
Expand Down

0 comments on commit 3c0c664

Please sign in to comment.