From 66f96a4fed6e798b4ed09698a8679879c01ec639 Mon Sep 17 00:00:00 2001 From: Sergey Dvodnenko Date: Thu, 12 Dec 2019 23:55:12 +0200 Subject: [PATCH] megacd: fix command/status handling, some reorganisation --- support/megacd/cdd.cpp | 48 +++++++++++++++++++++++------ support/megacd/megacd.cpp | 63 +++++++-------------------------------- support/megacd/megacd.h | 10 ++++--- 3 files changed, 55 insertions(+), 66 deletions(-) diff --git a/support/megacd/cdd.cpp b/support/megacd/cdd.cpp index f26f254..61e2488 100644 --- a/support/megacd/cdd.cpp +++ b/support/megacd/cdd.cpp @@ -7,12 +7,11 @@ #include "../../file_io.h" #include "megacd.h" +#define CD_DATA_IO_INDEX 2 +#define CD_SUB_IO_INDEX 3 cdd_t cdd; - -uint32_t frame = 0; - cdd_t::cdd_t() { latency = 10; loaded = 0; @@ -23,6 +22,7 @@ cdd_t::cdd_t() { status = CD_STAT_NO_DISC; audioLength = 0; audioOffset = 0; + SendData = NULL; stat[0] = 0xB; stat[1] = 0x0; @@ -621,12 +621,12 @@ void cdd_t::CommandExec() { MSFToLBA(&lba_, comm[2] * 10 + comm[3], comm[4] * 10 + comm[5], comm[6] * 10 + comm[7]); lba_ -= 150; - if (!this->latency) + //if (!this->latency) { this->latency = 11; } - this->latency += abs(((lba_ - this->lba) * 120) / 270000); + this->latency += (abs(lba_ - this->lba) * 120) / 270000; this->lba = lba_; @@ -676,7 +676,7 @@ void cdd_t::CommandExec() { MSFToLBA(&lba_, comm[2] * 10 + comm[3], comm[4] * 10 + comm[5], comm[6] * 10 + comm[7]); lba_ -= 150; - this->latency = abs((lba_ - this->lba) * 120) / 270000; + this->latency = (abs(lba_ - this->lba) * 120) / 270000; this->lba = lba_; @@ -699,8 +699,6 @@ void cdd_t::CommandExec() { FileSeek(&this->toc.tracks[index].f, (lba_ * 2352) - this->toc.tracks[index].offset, SEEK_SET); } - //this->audioOffset = 0; - //if (this->toc.sub) fseek(this->toc.sub, lba_ * 96, SEEK_SET); this->isData = 1; @@ -736,7 +734,7 @@ void cdd_t::CommandExec() { stat[0] = this->status; this->audioOffset = 0; - printf("\x1b[32mMCD: Command RESUME, status = %X, frame = %u\n\x1b[0m", this->status, frame); + //printf("\x1b[32mMCD: Command RESUME, status = %X\n\x1b[0m", this->status); break; case CD_COMM_FW_SCAN: @@ -882,6 +880,38 @@ void cdd_t::ReadSubcode(uint16_t* buf) */ } +int cdd_t::SectorSend(uint8_t* header) +{ + uint8_t buf[2352 + 2352]; + int len = 2352; + + if (header) { + memcpy(buf + 12, header, 4); + ReadData(buf + 16); + } + else { + len = ReadCDDA(buf); + } + + if (SendData) + return SendData(buf, len, CD_DATA_IO_INDEX); + + return 0; +} + + +int cdd_t::SubcodeSend() +{ + uint16_t buf[98 / 2]; + + ReadSubcode(buf); + + if (SendData) + return SendData((uint8_t*)buf, 98, CD_SUB_IO_INDEX); + + return 0; +} + diff --git a/support/megacd/megacd.cpp b/support/megacd/megacd.cpp index 590b93c..5edd7cc 100644 --- a/support/megacd/megacd.cpp +++ b/support/megacd/megacd.cpp @@ -10,8 +10,6 @@ #include "../../hardware.h" #include "megacd.h" -#define CD_DATA_IO_INDEX 2 -#define CD_SUB_IO_INDEX 3 #define SAVE_IO_INDEX 5 // fake download to trigger save loading int loaded = 0, unloaded = 0, need_reset=0; @@ -19,13 +17,14 @@ static uint8_t has_command = 0; void mcd_poll() { - static uint32_t poll_timer = 0, stat_timer = 0; + static uint32_t poll_timer = 0; static uint8_t last_req = 255; static uint8_t adj = 0; - if (!stat_timer || CheckTimer(stat_timer)) + if (!poll_timer || CheckTimer(poll_timer)) { - stat_timer = GetTimer(15); + poll_timer = GetTimer(13 + (!adj ? 1 : 0)); + if (++adj >= 3) adj = 0; if (has_command) { spi_uio_cmd_cont(UIO_CD_SET); @@ -40,6 +39,7 @@ void mcd_poll() //printf("\x1b[32mMCD: Send status, status = %04X%04X%04X, frame = %u\n\x1b[0m", (uint16_t)((s >> 32) & 0x00FF), (uint16_t)((s >> 16) & 0xFFFF), (uint16_t)((s >> 0) & 0xFFFF), frame); } + cdd.Update(); } @@ -61,22 +61,14 @@ void mcd_poll() uint64_t c = *((uint64_t*)(data_in)); cdd.SetCommand(c); - cdd.CommandExec(); - has_command = 1; + cdd.CommandExec(); + has_command = 1; //printf("\x1b[32mMCD: Get command, command = %04X%04X%04X, has_command = %u\n\x1b[0m", data_in[2], data_in[1], data_in[0], has_command); } else DisableIO(); - - if (!poll_timer || CheckTimer(poll_timer)) - { - poll_timer = GetTimer(13 + (!adj ? 1 : 0)); - if (++adj >= 3) adj = 0; - - cdd.Update(); - } } static void mcd_mount_save(const char *filename) @@ -107,6 +99,7 @@ void mcd_set_image(int num, const char *filename) loaded = 1; cdd.status = cdd.loaded ? CD_STAT_STOP : CD_STAT_NO_DISC; cdd.latency = 10; + cdd.SendData = mcd_send_data; } else { cdd.status = CD_STAT_NO_DISC; @@ -118,22 +111,9 @@ void mcd_reset() { need_reset = 1; } - -int cdd_t::SectorSend(uint8_t* header) -{ - uint8_t buf[2352+2352]; - int len = 2352; - - if (header) { - memcpy(buf + 12, header, 4); - ReadData(buf + 16); - } - else { - len = ReadCDDA(buf); - } - +int mcd_send_data(uint8_t* buf, int len, uint8_t index) { // set index byte - user_io_set_index(CD_DATA_IO_INDEX); + user_io_set_index(index); // prepare transmission of new file user_io_set_download(1); @@ -148,29 +128,6 @@ int cdd_t::SectorSend(uint8_t* header) return 1; } - -int cdd_t::SubcodeSend() -{ - uint16_t buf[98/2]; - - ReadSubcode(buf); - - // set index byte - user_io_set_index(CD_SUB_IO_INDEX); - - // prepare transmission of new file - user_io_set_download(1); - - EnableFpga(); - spi8(UIO_FILE_TX_DAT); - spi_write((uint8_t*)buf, 98, 1); - DisableFpga(); - - // signal end of transmission - user_io_set_download(0); - return 1; -} - static char int_blank[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, diff --git a/support/megacd/megacd.h b/support/megacd/megacd.h index e474007..f41f8f2 100644 --- a/support/megacd/megacd.h +++ b/support/megacd/megacd.h @@ -59,6 +59,8 @@ typedef struct uint8_t f; } msf_t; +typedef int (*SendDataFunc) (uint8_t* buf, int len, uint8_t index); + class cdd_t { public: @@ -66,6 +68,7 @@ public: uint8_t status; uint8_t isData; int loaded; + SendDataFunc SendData; cdd_t(); int Load(const char *filename); @@ -73,9 +76,6 @@ public: void Reset(); void Update(); void CommandExec(); - int SectorSend(uint8_t* header); - int SubcodeSend(); - uint64_t GetStatus(); int SetCommand(uint64_t c); @@ -92,6 +92,8 @@ private: uint8_t comm[10]; int LoadCUE(const char* filename); + int SectorSend(uint8_t* header); + int SubcodeSend(); void ReadData(uint8_t *buf); int ReadCDDA(uint8_t *buf); void ReadSubcode(uint16_t* buf); @@ -106,12 +108,12 @@ private: //cdd.cpp extern cdd_t cdd; -extern uint32_t frame; void mcd_poll(); void mcd_set_image(int num, const char *filename); void mcd_reset(); +int mcd_send_data(uint8_t* buf, int len, uint8_t index); void mcd_fill_blanksave(uint8_t *buffer, uint32_t lba); #endif