Skip to content

Commit

Permalink
Added Unirom via TCP
Browse files Browse the repository at this point in the history
  • Loading branch information
ShendoXT committed Jan 15, 2023
1 parent 1f7008e commit e3d223c
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 105 deletions.
111 changes: 68 additions & 43 deletions MemcardRex/GUI/cardReaderWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ enum DeviceId:int {
DexDrive,
MemCARDuino,
PS1CardLink,
PS3MemCardAdaptor,
Unirom
PS1CardLinkTCP,
Unirom,
UniromTCP,
PS3MemCardAdaptor
};

//Active device ID
int currentDeviceIdentifier;

public cardReaderWindow()
Expand Down Expand Up @@ -158,7 +161,7 @@ public byte[] readMemoryCardPS1CLnk(Form hostWindow, string applicationName, str
mainProgressBar.Maximum = 1024;

//Set current device to PS1CardLink
currentDeviceIdentifier = (int) DeviceId.MemCARDuino;
currentDeviceIdentifier = (int) DeviceId.PS1CardLink;

//Set window title and information
this.Text = "PS1CardLink communication";
Expand Down Expand Up @@ -215,21 +218,9 @@ public byte[] readMemoryCardPS3MCA(Form hostWindow, string applicationName, stri
else return null;
}

//Read a Memory Card from Unirom
public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, string remoteAddress, int remotePort, int cardSlot)
//Set everything for Unirom read be it via serial or TCP
public byte[] setupUniromRead(Form hostWindow, string applicationName, string errorString)
{
string errorString;

//Initialize Unirom
if (remoteAddress.Length > 0)
{
errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort);
}
else
{
errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Read, 1024);
}

//Check if there were any errors
if (errorString != null)
{
Expand All @@ -245,9 +236,6 @@ public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, stri
//Set scale for progress bar
mainProgressBar.Maximum = 1024;

//Set current device to Unirom
currentDeviceIdentifier = (int) DeviceId.Unirom;

//Set window title and information
this.Text = "Unirom communication";
infoLabel.Text = "Waiting for Unirom to store contents in RAM.\nTransfer will start after all the sectors have been read.";
Expand All @@ -274,8 +262,32 @@ public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, stri
else return null;
}

//Read a Memory Card from Unirom via TCP
public byte[] readMemoryCardUniromTCP(Form hostWindow, string applicationName, string remoteAddress, int remotePort, int cardSlot)
{
//Initialize Unirom via TCP
string errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort, cardSlot, (int)Unirom.Mode.Read, 1024);

//Set current device to Unirom via TCP
currentDeviceIdentifier = (int)DeviceId.UniromTCP;

return setupUniromRead(hostWindow, applicationName, errorString);
}

//Read a Memory Card from Unirom via Serial
public byte[] readMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot)
{
//Initialize Unirom
string errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Read, 1024);

//Set current device to Unirom
currentDeviceIdentifier = (int)DeviceId.Unirom;

return setupUniromRead(hostWindow, applicationName, errorString);
}

//Write a Memory Card to DexDrive
public void writeMemoryCardDexDrive(Form hostWindow, string applicationName, string comPort, byte[] memoryCardData, int frameNumber)
public void writeMemoryCardDexDrive(Form hostWindow, string applicationName, string comPort, byte[] memoryCardData, int frameNumber)
{
//Initialize DexDrive
string errorString = dexDevice.StartDexDrive(comPort);
Expand Down Expand Up @@ -444,24 +456,9 @@ public void writeMemoryCardPS3MCA(Form hostWindow, string applicationName, strin
PS3MCA.StopPS3MemCardAdaptor();
}

//Write a Memory Card to Unirom
public void writeMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot, string remoteAddress, int remotePort, byte[] memoryCardData, int frameNumber)
//Set up everything for Unirom write be it serial or TCP
private void setupUniromWrite(Form hostWindow, string applicationName, byte[] memoryCardData, int frameNumber, string errorString)
{
string errorString;

//Store checksum before opening port
uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData);

//Initialize Unirom
if (remoteAddress.Length > 0)
{
errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort);
}
else
{
errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Write, frameNumber);
}

//Check if there were any errors
if (errorString != null)
{
Expand All @@ -477,9 +474,6 @@ public void writeMemoryCardUnirom(Form hostWindow, string applicationName, strin
//Set scale for progress bar
mainProgressBar.Maximum = frameNumber / 16;

//Set current device to Unirom
currentDeviceIdentifier = (int)DeviceId.Unirom;

//Set window title and information
this.Text = "Unirom communication";
infoLabel.Text = "Writing data to Unirom...";
Expand All @@ -496,6 +490,33 @@ public void writeMemoryCardUnirom(Form hostWindow, string applicationName, strin
uniromDevice.StopUnirom();
}

//Write a Memory Card to Unirom via TCP
public void writeMemoryCardUniromTCP(Form hostWindow, string applicationName, string remoteAddress, int remotePort, int cardSlot, byte[] memoryCardData, int frameNumber)
{
//Store checksum before opening port
uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData);

//Set current device to Unirom
currentDeviceIdentifier = (int)DeviceId.UniromTCP;

string errorString = uniromDevice.StartUniromTCP(remoteAddress, remotePort, cardSlot, (int)Unirom.Mode.Write, frameNumber);

setupUniromWrite(hostWindow, applicationName, memoryCardData, frameNumber, errorString);
}

//Write a Memory Card to Unirom via serial
public void writeMemoryCardUnirom(Form hostWindow, string applicationName, string comPort, int cardSlot, byte[] memoryCardData, int frameNumber)
{
//Store checksum before opening port
uniromDevice.LastChecksum = uniromDevice.CalculateChecksum(memoryCardData);

//Set current device to Unirom
currentDeviceIdentifier = (int)DeviceId.Unirom;

string errorString = uniromDevice.StartUnirom(comPort, cardSlot, (int) Unirom.Mode.Write, frameNumber);

setupUniromWrite(hostWindow, applicationName, memoryCardData, frameNumber, errorString);
}

private void OKbutton_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -529,6 +550,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e)
break;

case (int) DeviceId.PS1CardLink:
case (int) DeviceId.PS1CardLinkTCP:
tempDataBuffer = PS1CLnk.ReadMemoryCardFrame(i);
break;

Expand All @@ -537,6 +559,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e)
break;

case (int)DeviceId.Unirom:
case (int)DeviceId.UniromTCP:
tempDataBuffer = uniromDevice.ReadMemoryCardFrame(i);
break;
}
Expand All @@ -556,7 +579,7 @@ private void backgroundReader_DoWork(object sender, DoWorkEventArgs e)

private void backgroundReader_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if(currentDeviceIdentifier == (int)DeviceId.Unirom)
if(currentDeviceIdentifier == (int)DeviceId.Unirom || currentDeviceIdentifier == (int)DeviceId.UniromTCP)
{
if(!mainProgressBar.Visible && uniromDevice.StoredInRam)
{
Expand All @@ -581,7 +604,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e)
int frameSize = 128;

//Unirom works with 2048 byte chunks
if (currentDeviceIdentifier == (int)DeviceId.Unirom) frameSize = 2048;
if (currentDeviceIdentifier == (int)DeviceId.Unirom || currentDeviceIdentifier == (int)DeviceId.UniromTCP) frameSize = 2048;

byte[] tempDataBuffer = new byte[frameSize];
ushort i = 0;
Expand Down Expand Up @@ -611,6 +634,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e)
break;

case (int) DeviceId.PS1CardLink:
case (int) DeviceId.PS1CardLinkTCP:
lastStatus = PS1CLnk.WriteMemoryCardFrame(i, tempDataBuffer);
break;

Expand All @@ -619,6 +643,7 @@ private void backgroundWriter_DoWork(object sender, DoWorkEventArgs e)
break;

case (int)DeviceId.Unirom:
case (int)DeviceId.UniromTCP:
lastStatus = uniromDevice.WriteMemoryCardChunk(i, tempDataBuffer);
break;
}
Expand Down
3 changes: 3 additions & 0 deletions MemcardRex/GUI/mainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 33 additions & 3 deletions MemcardRex/GUI/mainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,11 @@ private void formatHardwareCard(int hardDevice)
break;

case 5: //Unirom
new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, "", 0, blankCard.saveMemoryCardStream(true), frameNumber);
new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, blankCard.saveMemoryCardStream(true), frameNumber);
break;

case 6: //Unirom via TCP
new cardReaderWindow().writeMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress,mainSettings.remoteCommunicationPort, mainSettings.cardSlot, blankCard.saveMemoryCardStream(true), frameNumber);
break;
}
}
Expand Down Expand Up @@ -2329,7 +2333,7 @@ private void exportRAWSaveToolStripMenuItem1_Click(object sender, EventArgs e)
private void uniromToolStripMenuItem_Click(object sender, EventArgs e)
{
//Read a Memory Card from Unirom
byte[] tempByteArray = new cardReaderWindow().readMemoryCardUnirom(this, appName, mainSettings.communicationPort, "", 0, mainSettings.cardSlot);
byte[] tempByteArray = new cardReaderWindow().readMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot);

cardReaderRead(tempByteArray);
}
Expand All @@ -2342,7 +2346,7 @@ private void uniromToolStripMenuItem1_Click(object sender, EventArgs e)
if (PScard.Count > 0)
{
//Open Unirom communication window
new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, "", 0, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
new cardReaderWindow().writeMemoryCardUnirom(this, appName, mainSettings.communicationPort, mainSettings.cardSlot, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
}
}

Expand All @@ -2351,5 +2355,31 @@ private void uniromToolStripMenuItem2_Click(object sender, EventArgs e)
//Format a Memory Card on Unirom
formatHardwareCard(5);
}

private void uniromOverTCPToolStripMenuItem_Click(object sender, EventArgs e)
{
//Read a Memory Card from Unirom via TCP
byte[] tempByteArray = new cardReaderWindow().readMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress, mainSettings.remoteCommunicationPort, mainSettings.cardSlot);

cardReaderRead(tempByteArray);
}

private void uniromOverTCPToolStripMenuItem1_Click(object sender, EventArgs e)
{
int listIndex = mainTabControl.SelectedIndex;

//Check if there are any cards to write
if (PScard.Count > 0)
{
//Open Unirom communication window
new cardReaderWindow().writeMemoryCardUniromTCP(this, appName, mainSettings.remoteCommunicationAddress, mainSettings.remoteCommunicationPort, mainSettings.cardSlot, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
}
}

private void uniromOverTCPToolStripMenuItem2_Click(object sender, EventArgs e)
{
//Format a Memory Card on Unirom via TCP
formatHardwareCard(6);
}
}
}
4 changes: 2 additions & 2 deletions MemcardRex/GUI/preferencesWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e3d223c

Please sign in to comment.