2022-03-21 01:01:35 +01:00
|
|
|
//********************************************************
|
|
|
|
// SEGA MASTER SYSTEM/Mark III/SG-1000/GAME GEAR MODULE
|
|
|
|
//********************************************************
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 17:07:14 +02:00
|
|
|
#include "options.h"
|
|
|
|
#ifdef enable_MD
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
/******************************************
|
|
|
|
Variables
|
|
|
|
*****************************************/
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
Menu
|
|
|
|
*****************************************/
|
2022-03-21 01:01:35 +01:00
|
|
|
// Adapter menu
|
|
|
|
static const char SMSAdapterItem1[] PROGMEM = "SMS/Mark 3 raphnet";
|
|
|
|
static const char SMSAdapterItem2[] PROGMEM = "SG-1000 raphnet";
|
|
|
|
static const char SMSAdapterItem3[] PROGMEM = "SMS Retrode";
|
|
|
|
static const char SMSAdapterItem4[] PROGMEM = "GG Retrode";
|
|
|
|
static const char* const menuAdapterSMS[] PROGMEM = {SMSAdapterItem1, SMSAdapterItem2, SMSAdapterItem3, SMSAdapterItem4};
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
// MD menu items
|
|
|
|
static const char SMSMenuItem1[] PROGMEM = "Read Rom";
|
2020-07-04 18:42:05 +02:00
|
|
|
static const char SMSMenuItem2[] PROGMEM = "Read from SRAM";
|
|
|
|
static const char SMSMenuItem3[] PROGMEM = "Write to SRAM";
|
2022-03-21 01:01:35 +01:00
|
|
|
static const char SMSMenuItem4[] PROGMEM = "Reset";
|
|
|
|
static const char SMSMenuItem5[] PROGMEM = "Change Retrode Mode";
|
2020-07-04 18:42:05 +02:00
|
|
|
static const char* const menuOptionsSMS[] PROGMEM = {SMSMenuItem1, SMSMenuItem2, SMSMenuItem3, SMSMenuItem4, SMSMenuItem5};
|
2020-07-04 12:16:14 +02:00
|
|
|
|
2022-03-10 15:05:20 +01:00
|
|
|
// Rom Size menu
|
|
|
|
static const char SMSRomItem1[] PROGMEM = "8KB";
|
|
|
|
static const char SMSRomItem2[] PROGMEM = "16KB";
|
|
|
|
static const char SMSRomItem3[] PROGMEM = "24KB";
|
|
|
|
static const char SMSRomItem4[] PROGMEM = "32KB";
|
|
|
|
static const char SMSRomItem5[] PROGMEM = "40KB";
|
|
|
|
static const char SMSRomItem6[] PROGMEM = "48KB";
|
|
|
|
static const char SMSRomItem7[] PROGMEM = "512KB";
|
|
|
|
static const char* const romOptionsSMS[] PROGMEM = {SMSRomItem1, SMSRomItem2, SMSRomItem3, SMSRomItem4, SMSRomItem5, SMSRomItem6, SMSRomItem7};
|
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set retrode_mode to true when using a retrode SMS/GG adapter
|
|
|
|
static bool retrode_mode = false;
|
2020-10-17 19:11:34 +02:00
|
|
|
static bool retrode_mode_sms = false; // true: SMS/Mark3 false: GG
|
2022-03-21 01:01:35 +01:00
|
|
|
static bool raphnet_mode_sg1000 = false; // true: SG-1000 false: SMS/Mark3
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 17:05:06 +02:00
|
|
|
void _smsMenu() {
|
2022-03-21 01:01:35 +01:00
|
|
|
// create menu with title and n options to choose from
|
2019-09-01 14:36:53 +02:00
|
|
|
unsigned char mainMenu;
|
2022-03-21 01:01:35 +01:00
|
|
|
|
|
|
|
if (retrode_mode) {
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
int noptions = sizeof(menuOptionsSMS) / sizeof(menuOptionsSMS[0]);
|
|
|
|
convertPgm(menuOptionsSMS, noptions);
|
|
|
|
mainMenu = question_box(retrode_mode ? (retrode_mode_sms ? F("Retrode:SMS") : F("Retrode:GG")) : F("SMS/GG Retrode:NO"), menuOptions, noptions, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
convertPgm(menuOptionsSMS, (raphnet_mode_sg1000 ? 1 : 4));
|
|
|
|
mainMenu = question_box((raphnet_mode_sg1000 ? F("SG-1000") : F("SMS/Mark III") ), menuOptions, (raphnet_mode_sg1000 ? 1 : 4), 0);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
|
|
|
switch (mainMenu)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
display_Clear();
|
2019-09-27 23:01:45 +02:00
|
|
|
mode = mode_SMS;
|
2019-09-19 13:42:46 +02:00
|
|
|
setup_SMS();
|
2019-09-01 14:36:53 +02:00
|
|
|
// Change working dir to root
|
|
|
|
sd.chdir("/");
|
|
|
|
readROM_SMS();
|
2022-06-16 17:17:16 +02:00
|
|
|
compareCRC("sms.txt");
|
2022-06-16 15:15:43 +02:00
|
|
|
#ifdef global_log
|
|
|
|
save_log();
|
|
|
|
#endif
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2020-07-04 12:16:14 +02:00
|
|
|
display_Clear();
|
|
|
|
mode = mode_SMS;
|
|
|
|
setup_SMS();
|
|
|
|
// Change working dir to root
|
|
|
|
sd.chdir("/");
|
2020-07-04 17:05:06 +02:00
|
|
|
readSRAM_SMS();
|
2020-07-04 12:16:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2020-07-04 18:42:05 +02:00
|
|
|
display_Clear();
|
|
|
|
mode = mode_SMS;
|
|
|
|
setup_SMS();
|
|
|
|
// Change working dir to root
|
|
|
|
sd.chdir("/");
|
|
|
|
writeSRAM_SMS();
|
2020-07-04 17:05:06 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2022-03-21 01:01:35 +01:00
|
|
|
// Reset
|
|
|
|
resetArduino();
|
2020-07-04 18:42:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2022-03-21 01:01:35 +01:00
|
|
|
if (retrode_mode && !retrode_mode_sms) {
|
|
|
|
// Change to SMS
|
|
|
|
retrode_mode_sms = true;
|
|
|
|
} else if (retrode_mode && retrode_mode_sms) {
|
|
|
|
// Change to GG
|
|
|
|
retrode_mode_sms = false;
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-03-21 01:01:35 +01:00
|
|
|
if (retrode_mode) {
|
|
|
|
println_Msg(retrode_mode ? (retrode_mode_sms ? F("Retrode Mode SMS") : F("Retrode Mode GG")) : F("Retrode Mode Off"));
|
|
|
|
println_Msg(F("Press Button..."));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
println_Msg(F(""));
|
|
|
|
println_Msg(F("Press Button..."));
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
display_Update();
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:05:06 +02:00
|
|
|
void smsMenu() {
|
2022-03-21 01:01:35 +01:00
|
|
|
// create main menu with title and 4 options to choose from
|
|
|
|
unsigned char SMSAdapterMenu;
|
|
|
|
// Copy menuOptions out of progmem
|
|
|
|
convertPgm(menuAdapterSMS, 4);
|
|
|
|
SMSAdapterMenu = question_box(F("Select System/Adapter"), menuOptions, 4, 0);
|
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
|
|
|
switch (SMSAdapterMenu)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// raphnet SMS/Mark3
|
|
|
|
retrode_mode = false;
|
|
|
|
retrode_mode_sms = false;
|
|
|
|
raphnet_mode_sg1000 = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// raphnet SG-1000
|
|
|
|
retrode_mode = false;
|
|
|
|
retrode_mode_sms = false;
|
|
|
|
raphnet_mode_sg1000 = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// retrode SMS/Mark3
|
|
|
|
retrode_mode = true;
|
|
|
|
retrode_mode_sms = true;
|
|
|
|
raphnet_mode_sg1000 = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// retrode GG
|
|
|
|
retrode_mode = true;
|
|
|
|
retrode_mode_sms = false;
|
|
|
|
raphnet_mode_sg1000 = false;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-04 17:05:06 +02:00
|
|
|
for (;;) _smsMenu();
|
|
|
|
}
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
/******************************************
|
|
|
|
Setup
|
|
|
|
*****************************************/
|
|
|
|
void setup_SMS() {
|
|
|
|
// Set Address Pins to Output
|
|
|
|
//A0-A7
|
|
|
|
DDRF = 0xFF;
|
2019-09-20 14:30:57 +02:00
|
|
|
//A8-A14
|
2019-09-01 14:36:53 +02:00
|
|
|
DDRK = 0xFF;
|
2019-09-20 14:30:57 +02:00
|
|
|
//A15
|
|
|
|
DDRH |= (1 << 3);
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
if (retrode_mode) {
|
2020-07-04 15:33:50 +02:00
|
|
|
// Revert changes from the other mode
|
|
|
|
PORTH &= ~((1 << 0) | (1 << 3) | (1 << 5));
|
|
|
|
PORTL &= ~(1 << 1);
|
|
|
|
DDRH &= ~((1 << 0) | (1 << 5));
|
|
|
|
DDRL &= ~((1 << 1));
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set Control Pins to Output OE(PH6)
|
|
|
|
DDRH |= (1 << 6);
|
|
|
|
// WR(PL5) and RD(PL6)
|
|
|
|
DDRL |= (1 << 5) | (1 << 6);
|
|
|
|
|
|
|
|
// Setting OE(PH6) HIGH
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
// Setting WR(PL5) and RD(PL6) HIGH
|
|
|
|
PORTL |= (1 << 5) | (1 << 6);
|
|
|
|
} else {
|
2020-07-04 15:33:50 +02:00
|
|
|
// Revert changes from the other mode
|
|
|
|
PORTL &= ~((1 << 5) | (1 << 6));
|
|
|
|
DDRL &= ~((1 << 5) | (1 << 6));
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set Control Pins to Output RST(PH0) WR(PH5) OE(PH6)
|
|
|
|
DDRH |= (1 << 0) | (1 << 5) | (1 << 6);
|
|
|
|
// CE(PL1)
|
|
|
|
DDRL |= (1 << 1);
|
|
|
|
|
|
|
|
// Setting RST(PH0) WR(PH5) OE(PH6) HIGH
|
|
|
|
PORTH |= (1 << 0) | (1 << 5) | (1 << 6);
|
|
|
|
// CE(PL1)
|
|
|
|
PORTL |= (1 << 1);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2019-09-19 13:42:46 +02:00
|
|
|
// ROM has 16KB banks which can be mapped to one of three slots via register writes
|
|
|
|
// Register Slot Address space
|
|
|
|
// $fffd 0 $0000-$3fff
|
|
|
|
// $fffe 1 $4000-$7fff
|
|
|
|
// $ffff 2 $8000-$bfff
|
2019-09-20 11:16:53 +02:00
|
|
|
// Disable sram
|
|
|
|
writeByte_SMS(0xFFFC, 0);
|
2019-09-19 13:42:46 +02:00
|
|
|
// Map first 3 banks so we can read-out the header info
|
|
|
|
writeByte_SMS(0xFFFD, 0);
|
|
|
|
writeByte_SMS(0xFFFE, 1);
|
|
|
|
writeByte_SMS(0xFFFF, 2);
|
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
delay(400);
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// Print all the info
|
|
|
|
getCartInfo_SMS();
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************
|
|
|
|
Low level functions
|
|
|
|
*****************************************/
|
2019-09-19 16:06:19 +02:00
|
|
|
void writeByte_SMS(word myAddress, byte myData) {
|
2020-10-17 19:11:34 +02:00
|
|
|
if (retrode_mode && !retrode_mode_sms) {
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set Data Pins (D8-D15) to Output
|
|
|
|
DDRA = 0xFF;
|
|
|
|
} else {
|
|
|
|
// Set Data Pins (D0-D7) to Output
|
|
|
|
DDRC = 0xFF;
|
|
|
|
}
|
2019-09-20 11:16:53 +02:00
|
|
|
|
2019-09-19 13:42:46 +02:00
|
|
|
// Set address
|
2019-09-01 14:36:53 +02:00
|
|
|
PORTF = myAddress & 0xFF;
|
|
|
|
PORTK = (myAddress >> 8) & 0xFF;
|
2020-07-04 12:16:14 +02:00
|
|
|
if (!retrode_mode) {
|
|
|
|
// CE(PH3) and OE(PH6) are connected
|
|
|
|
PORTH = (PORTH & 0b11110111) | ((myAddress >> 12) & 0b00001000);
|
|
|
|
}
|
|
|
|
|
2019-09-19 13:42:46 +02:00
|
|
|
// Output data
|
2020-10-17 19:11:34 +02:00
|
|
|
if (retrode_mode && !retrode_mode_sms) {
|
2020-07-04 12:16:14 +02:00
|
|
|
PORTA = myData;
|
|
|
|
} else {
|
|
|
|
PORTC = myData;
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// Arduino running at 16Mhz -> one nop = 62.5ns
|
|
|
|
// Wait till output is stable
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
if (retrode_mode) {
|
|
|
|
// Switch WR(PL5) and OE/CE(PH6) to LOW
|
|
|
|
PORTL &= ~(1 << 5);
|
|
|
|
PORTH &= ~(1 << 6);
|
|
|
|
} else {
|
|
|
|
// Switch CE(PL1) and WR(PH5) to LOW
|
|
|
|
PORTL &= ~(1 << 1);
|
|
|
|
PORTH &= ~(1 << 5);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
// Leave WR low for at least 60ns
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
if (retrode_mode) {
|
|
|
|
// Switch WR(PL5) and OE/CE(PH6) to HIGH
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
PORTL |= (1 << 5);
|
|
|
|
} else {
|
|
|
|
// Switch CE(PL1) and WR(PH5) to HIGH
|
|
|
|
PORTH |= (1 << 5);
|
|
|
|
PORTL |= (1 << 1);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
// Leave WR high for at least 50ns
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-19 13:42:46 +02:00
|
|
|
|
2020-10-17 19:11:34 +02:00
|
|
|
if (retrode_mode && !retrode_mode_sms) {
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set Data Pins (D8-D15) to Input
|
|
|
|
DDRA = 0x00;
|
|
|
|
} else {
|
|
|
|
// Set Data Pins (D0-D7) to Input
|
|
|
|
DDRC = 0x00;
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
byte readByte_SMS(word myAddress) {
|
2020-10-17 19:11:34 +02:00
|
|
|
if (retrode_mode && !retrode_mode_sms) {
|
2020-07-04 12:16:14 +02:00
|
|
|
// Set Data Pins (D8-D15) to Input
|
|
|
|
DDRA = 0x00;
|
|
|
|
} else {
|
|
|
|
// Set Data Pins (D0-D7) to Input
|
|
|
|
DDRC = 0x00;
|
|
|
|
}
|
2019-09-19 13:42:46 +02:00
|
|
|
|
|
|
|
// Set Address
|
2019-09-01 14:36:53 +02:00
|
|
|
PORTF = myAddress & 0xFF;
|
|
|
|
PORTK = (myAddress >> 8) & 0xFF;
|
2020-07-04 12:16:14 +02:00
|
|
|
if (!retrode_mode) {
|
|
|
|
// CE(PH3) and OE(PH6) are connected
|
|
|
|
PORTH = (PORTH & 0b11110111) | ((myAddress >> 12) & 0b00001000);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-07-04 12:16:14 +02:00
|
|
|
if (retrode_mode) {
|
|
|
|
// Switch RD(PL6) and OE(PH6) to LOW
|
|
|
|
PORTL &= ~(1 << 6);
|
|
|
|
PORTH &= ~(1 << 6);
|
|
|
|
} else {
|
|
|
|
// Switch CE(PL1) and OE(PH6) to LOW
|
|
|
|
PORTL &= ~(1 << 1);
|
|
|
|
PORTH &= ~(1 << 6);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// Read
|
2020-10-17 19:11:34 +02:00
|
|
|
byte tempByte = (retrode_mode && !retrode_mode_sms) ? PINA : PINC;
|
2020-07-04 12:16:14 +02:00
|
|
|
|
|
|
|
if (retrode_mode) {
|
|
|
|
// Switch RD(PL6) and OE(PH6) to HIGH
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
PORTL |= (1 << 6);
|
|
|
|
} else {
|
|
|
|
// Switch CE(PL1) and OE(PH6) to HIGH
|
|
|
|
PORTH |= (1 << 6);
|
|
|
|
PORTL |= (1 << 1);
|
|
|
|
}
|
2019-09-19 13:42:46 +02:00
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
__asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t");
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
return tempByte;
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
//unsigned char hex2bcd (unsigned char x) {
|
|
|
|
// unsigned char y;
|
|
|
|
// y = (x / 10) << 4;
|
|
|
|
// y = y | (x % 10);
|
|
|
|
//return (y);
|
|
|
|
//}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2019-09-19 13:42:46 +02:00
|
|
|
byte readNibble(byte data, byte number) {
|
|
|
|
return ((data >> (number * 4)) & 0xf);
|
|
|
|
}
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
/******************************************
|
|
|
|
MASTER SYSTEM functions
|
|
|
|
*****************************************/
|
|
|
|
void getCartInfo_SMS() {
|
|
|
|
// Rom size
|
2019-09-19 13:42:46 +02:00
|
|
|
switch (readNibble(readByte_SMS(0x7fff), 0)) {
|
2019-09-01 14:36:53 +02:00
|
|
|
case 0xa:
|
2019-09-10 17:38:56 +02:00
|
|
|
// Adding UL gets rid of integer overflow compiler warning
|
|
|
|
cartSize = 8 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0xb:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 16 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0xc:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 32 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0xd:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 48 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0xe:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 64 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0xf:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 128 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0x0:
|
2020-10-17 19:11:34 +02:00
|
|
|
cartSize = 256 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0x1:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 512 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
|
|
|
case 0x2:
|
2019-09-10 17:38:56 +02:00
|
|
|
cartSize = 512 * 1024UL;
|
2019-09-01 14:36:53 +02:00
|
|
|
break;
|
2021-04-10 17:21:08 +02:00
|
|
|
case 0x3:
|
|
|
|
// 0x3 is (only?) used in The Pro Yakyuu '91 (Game Gear)
|
|
|
|
cartSize = 128 * 1024UL;
|
|
|
|
break;
|
2019-09-19 16:06:19 +02:00
|
|
|
default:
|
|
|
|
cartSize = 48 * 1024UL;
|
|
|
|
// LED Error
|
2021-10-24 00:41:18 +02:00
|
|
|
setColor_RGB(0, 0, 255);
|
2019-09-19 16:06:19 +02:00
|
|
|
break;
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 19:11:34 +02:00
|
|
|
// Read TMR SEGA string
|
2019-09-19 13:42:46 +02:00
|
|
|
for (byte i = 0; i < 8; i++) {
|
|
|
|
romName[i] = char(readByte_SMS(0x7ff0 + i));
|
|
|
|
}
|
|
|
|
romName[8] = '\0';
|
2019-09-01 14:36:53 +02:00
|
|
|
|
2020-10-17 19:11:34 +02:00
|
|
|
// Attempt to detect cart size by checking if TMR SEGA is mirrored
|
|
|
|
unsigned long mirror_offset = cartSize;
|
|
|
|
char romName2[9];
|
|
|
|
while (mirror_offset < 1024 * 1024UL) {
|
|
|
|
byte bank = 1 + (mirror_offset / (16 * 1024UL));
|
|
|
|
writeByte_SMS(0xFFFE, bank);
|
|
|
|
for (byte i = 0; i < 8; i++) {
|
|
|
|
romName2[i] = char(readByte_SMS(0x7ff0 + i));
|
|
|
|
}
|
|
|
|
romName2[8] = '\0';
|
|
|
|
// print_Msg(F("Name2: "));
|
|
|
|
// println_Msg(romName2);
|
|
|
|
// print_Msg(F("from bank "));
|
|
|
|
// print_Msg(bank);
|
|
|
|
// print_Msg(F(" offset "));
|
|
|
|
// print_Msg_PaddedHex32(mirror_offset + 0x7ff0);
|
|
|
|
// println_Msg(F(""));
|
|
|
|
if (strcmp(romName2, romName) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cartSize == 48 * 1024UL) {
|
|
|
|
cartSize = 64 * 1024UL;
|
|
|
|
} else {
|
|
|
|
cartSize *= 2;
|
|
|
|
}
|
|
|
|
mirror_offset = cartSize;
|
|
|
|
}
|
|
|
|
writeByte_SMS(0xFFFE, 1);
|
|
|
|
|
2021-12-08 17:21:01 +01:00
|
|
|
// Fix for "Fantasy Zone (J) (V1.0)" that has not the normal header, but "COPYRIGHT SEGAPRG. BY T.ASAI".
|
|
|
|
char headerFZ[29];
|
|
|
|
if (strcmp(romName, "G. BY T.A") != 0) {
|
|
|
|
for (byte i = 0; i < 28; i++) {
|
|
|
|
headerFZ[i] = char(readByte_SMS(0x7fe0 + i));
|
|
|
|
}
|
|
|
|
headerFZ[28] = '\0';
|
2022-02-15 12:09:21 +01:00
|
|
|
|
2021-12-08 17:21:01 +01:00
|
|
|
if (strcmp(headerFZ, "COPYRIGHT SEGAPRG. BY T.ASAI") == 0) {
|
|
|
|
strcpy(romName, "TMR SEGA");
|
|
|
|
cartSize = 128 * 1024UL;
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:09:21 +01:00
|
|
|
|
2022-03-10 15:05:20 +01:00
|
|
|
// SMS header not found
|
2019-09-19 16:06:19 +02:00
|
|
|
if (strcmp(romName, "TMR SEGA") != 0) {
|
2022-03-10 15:05:20 +01:00
|
|
|
// Set cartsize manually
|
|
|
|
unsigned char SMSRomMenu;
|
|
|
|
// Copy menuOptions out of progmem
|
2022-03-21 01:01:35 +01:00
|
|
|
convertPgm(romOptionsSMS, (raphnet_mode_sg1000 ? 4 : 7));
|
|
|
|
SMSRomMenu = question_box(F("Select ROM size"), menuOptions, (raphnet_mode_sg1000 ? 4 : 7), 0);
|
2022-03-10 15:05:20 +01:00
|
|
|
|
|
|
|
// wait for user choice to come back from the question box menu
|
|
|
|
switch (SMSRomMenu)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// 8KB
|
|
|
|
cartSize = 8 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// 16KB
|
|
|
|
cartSize = 16 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// 24KB
|
|
|
|
cartSize = 24 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
// 32KB
|
|
|
|
cartSize = 32 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
// 40KB
|
|
|
|
cartSize = 40 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
// 48KB
|
|
|
|
cartSize = 48 * 1024UL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
// 512KB
|
|
|
|
cartSize = 512 * 1024UL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
display_Clear();
|
|
|
|
println_Msg(F("SMS Header not found"));
|
|
|
|
println_Msg(F(" "));
|
|
|
|
print_Msg(F("Name: "));
|
|
|
|
println_Msg(romName);
|
|
|
|
print_Msg(F("Selected Size: "));
|
|
|
|
print_Msg(cartSize / 1024);
|
|
|
|
println_Msg(F("KB"));
|
|
|
|
println_Msg(F(" "));
|
|
|
|
sprintf(romName, "UNKNOWN");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Header found
|
|
|
|
else {
|
|
|
|
display_Clear();
|
|
|
|
println_Msg(F("SMS Header Info"));
|
|
|
|
println_Msg(F(" "));
|
|
|
|
print_Msg(F("Name: "));
|
|
|
|
println_Msg(romName);
|
|
|
|
print_Msg(F("Size: "));
|
|
|
|
print_Msg(cartSize / 1024);
|
|
|
|
println_Msg(F("KB"));
|
|
|
|
println_Msg(F(" "));
|
2019-09-19 16:06:19 +02:00
|
|
|
}
|
2019-09-19 13:42:46 +02:00
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
// Wait for user input
|
2021-11-17 21:31:18 +01:00
|
|
|
#if (defined(enable_LCD) || defined(enable_OLED))
|
2019-09-05 00:48:39 +02:00
|
|
|
println_Msg(F("Press Button..."));
|
|
|
|
display_Update();
|
|
|
|
wait();
|
|
|
|
#endif
|
2019-09-27 23:01:45 +02:00
|
|
|
// Turn off LED
|
2021-10-24 00:41:18 +02:00
|
|
|
setColor_RGB(0, 0, 0);
|
2019-09-01 14:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read rom and save to the SD card
|
|
|
|
void readROM_SMS() {
|
|
|
|
// Get name, add extension and convert to char array for sd lib
|
|
|
|
strcpy(fileName, romName);
|
|
|
|
strcat(fileName, ".SMS");
|
|
|
|
|
|
|
|
// create a new folder
|
2019-09-27 17:38:42 +02:00
|
|
|
EEPROM_readAnything(0, foldern);
|
2019-09-01 14:36:53 +02:00
|
|
|
sprintf(folder, "SMS/ROM/%s/%d", romName, foldern);
|
|
|
|
sd.mkdir(folder, true);
|
|
|
|
sd.chdir(folder);
|
|
|
|
|
|
|
|
display_Clear();
|
|
|
|
print_Msg(F("Saving to "));
|
|
|
|
print_Msg(folder);
|
|
|
|
println_Msg(F("/..."));
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
// write new folder number back to eeprom
|
|
|
|
foldern = foldern + 1;
|
2019-09-27 17:38:42 +02:00
|
|
|
EEPROM_writeAnything(0, foldern);
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
|
|
|
|
print_Error(F("SD Error"), true);
|
|
|
|
}
|
2022-03-21 01:01:35 +01:00
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
word bankSize = 16 * 1024UL;
|
2022-03-21 01:01:35 +01:00
|
|
|
|
|
|
|
if (raphnet_mode_sg1000) {
|
|
|
|
// cart with no mapper
|
|
|
|
bankSize = cartSize;
|
|
|
|
}
|
|
|
|
|
2019-09-19 16:06:19 +02:00
|
|
|
for (byte currBank = 0x0; currBank < (cartSize / bankSize); currBank++) {
|
2019-09-19 13:42:46 +02:00
|
|
|
// Write current 16KB bank to slot 2 register 0xFFFF
|
2022-03-21 01:01:35 +01:00
|
|
|
if (!raphnet_mode_sg1000) {
|
|
|
|
writeByte_SMS(0xFFFF, currBank);
|
|
|
|
}
|
2019-09-01 14:36:53 +02:00
|
|
|
|
|
|
|
// Blink led
|
2021-10-26 17:13:42 +02:00
|
|
|
blinkLED();
|
2019-09-19 13:42:46 +02:00
|
|
|
// Read 16KB from slot 2 which starts at 0x8000
|
2019-09-19 16:06:19 +02:00
|
|
|
for (word currBuffer = 0; currBuffer < bankSize; currBuffer += 512) {
|
2019-09-19 13:42:46 +02:00
|
|
|
// Fill SD buffer
|
|
|
|
for (int currByte = 0; currByte < 512; currByte++) {
|
2022-03-21 01:01:35 +01:00
|
|
|
sdBuffer[currByte] = readByte_SMS((raphnet_mode_sg1000 ? 0 : 0x8000) + currBuffer + currByte);
|
2019-09-19 13:42:46 +02:00
|
|
|
}
|
2020-10-17 19:11:34 +02:00
|
|
|
// hexdump for debugging:
|
|
|
|
// if (currBank == 0 && currBuffer == 0) {
|
|
|
|
// for (word xi = 0; xi < 0x100; xi++) {
|
|
|
|
// if (xi%16==0) {
|
|
|
|
// print_Msg_PaddedHex16(xi);
|
|
|
|
// print_Msg(F(" "));
|
|
|
|
// }
|
|
|
|
// print_Msg_PaddedHexByte(sdBuffer[xi]);
|
|
|
|
// if (xi>0&&((xi+1)%16)==0) {
|
|
|
|
// println_Msg(F(""));
|
|
|
|
// } else {
|
|
|
|
// print_Msg(F(" "));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2019-09-19 13:42:46 +02:00
|
|
|
myFile.write(sdBuffer, 512);
|
2019-09-01 14:36:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close the file:
|
|
|
|
myFile.close();
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:05:06 +02:00
|
|
|
// Read SRAM and save to the SD card
|
|
|
|
void readSRAM_SMS() {
|
|
|
|
// Get name, add extension and convert to char array for sd lib
|
|
|
|
strcpy(fileName, romName);
|
|
|
|
strcat(fileName, ".SAV");
|
|
|
|
|
|
|
|
// create a new folder
|
|
|
|
EEPROM_readAnything(0, foldern);
|
|
|
|
sprintf(folder, "SMS/SAVE/%s/%d", romName, foldern);
|
|
|
|
sd.mkdir(folder, true);
|
|
|
|
sd.chdir(folder);
|
|
|
|
|
|
|
|
display_Clear();
|
|
|
|
print_Msg(F("Saving to "));
|
|
|
|
print_Msg(folder);
|
|
|
|
println_Msg(F("/..."));
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
// write new folder number back to eeprom
|
|
|
|
foldern = foldern + 1;
|
|
|
|
EEPROM_writeAnything(0, foldern);
|
|
|
|
|
|
|
|
// Open file on sd card
|
|
|
|
if (!myFile.open(fileName, O_RDWR | O_CREAT)) {
|
|
|
|
print_Error(F("SD Error"), true);
|
|
|
|
}
|
|
|
|
// Write the whole 32KB
|
|
|
|
// When there is only 8KB of SRAM, the contents should be duplicated
|
|
|
|
word bankSize = 16 * 1024UL;
|
|
|
|
for (byte currBank = 0x0; currBank < 2; currBank++) {
|
|
|
|
writeByte_SMS(0xFFFC, 0x08 | (currBank << 2));
|
|
|
|
|
|
|
|
// Blink led
|
2021-10-26 17:13:42 +02:00
|
|
|
blinkLED();
|
2020-07-04 17:05:06 +02:00
|
|
|
// Read 16KB from slot 2 which starts at 0x8000
|
|
|
|
for (word currBuffer = 0; currBuffer < bankSize; currBuffer += 512) {
|
|
|
|
// Fill SD buffer
|
|
|
|
for (int currByte = 0; currByte < 512; currByte++) {
|
|
|
|
sdBuffer[currByte] = readByte_SMS(0x8000 + currBuffer + currByte);
|
|
|
|
}
|
|
|
|
myFile.write(sdBuffer, 512);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Close the file:
|
|
|
|
myFile.close();
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:42:05 +02:00
|
|
|
void writeSRAM_SMS() {
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
if (false) {
|
|
|
|
print_Error(F("DISABLED"), false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fileBrowser(F("Select file"));
|
|
|
|
|
|
|
|
sd.chdir();
|
|
|
|
sprintf(filePath, "%s/%s", filePath, fileName);
|
|
|
|
|
|
|
|
display_Clear();
|
|
|
|
println_Msg(F("Restoring from "));
|
|
|
|
println_Msg(filePath);
|
|
|
|
println_Msg(fileName);
|
|
|
|
display_Update();
|
|
|
|
|
|
|
|
if (myFile.open(filePath, O_READ)) {
|
|
|
|
// Get SRAM size from file, with a maximum of 32KB
|
|
|
|
uint32_t sramSize = myFile.fileSize();
|
2022-02-25 00:59:25 +01:00
|
|
|
if (sramSize > ((uint32_t)32 * (uint32_t)1024)) {
|
|
|
|
sramSize = (uint32_t)32 * (uint32_t)1024;
|
2020-07-04 18:42:05 +02:00
|
|
|
}
|
|
|
|
print_Msg(F("sramSize: "));
|
|
|
|
print_Msg(sramSize);
|
|
|
|
println_Msg(F(""));
|
|
|
|
word bankSize = 16 * 1024;
|
|
|
|
for (word address = 0x0; address < sramSize; address += 512) {
|
|
|
|
byte currBank = address >= bankSize ? 1 : 0;
|
|
|
|
word page_address = address - (currBank * bankSize);
|
|
|
|
writeByte_SMS(0xFFFC, 0x08 | (currBank << 2));
|
|
|
|
// Blink led
|
2021-10-26 17:13:42 +02:00
|
|
|
blinkLED();
|
2020-07-04 18:42:05 +02:00
|
|
|
myFile.read(sdBuffer, 512);
|
|
|
|
for (int x = 0; x < 512; x++) {
|
|
|
|
writeByte_SMS(0x8000 + page_address + x, sdBuffer[x]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
myFile.close();
|
|
|
|
|
|
|
|
// Blink led
|
2021-10-26 17:13:42 +02:00
|
|
|
blinkLED();
|
2020-07-04 18:42:05 +02:00
|
|
|
|
|
|
|
println_Msg(F(""));
|
|
|
|
println_Msg(F("DONE"));
|
|
|
|
display_Update();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print_Error(F("SD ERROR"), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
display_Clear();
|
|
|
|
|
|
|
|
sd.chdir(); // root
|
|
|
|
filePath[0] = '\0'; // Reset filePath
|
|
|
|
}
|
2020-07-04 17:07:14 +02:00
|
|
|
#endif
|
|
|
|
|
2019-09-01 14:36:53 +02:00
|
|
|
//******************************************
|
|
|
|
// End of File
|
|
|
|
//******************************************
|