Files
Aaru/Aaru.Decoders/Sega/Saturn.cs

263 lines
10 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:23 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Saturn.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Device structures decoders.
//
// --[ Description ] ----------------------------------------------------------
//
// Decodes Sega Saturn IP.BIN.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
2017-10-08 18:55:06 +01:00
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:24 +00:00
using Aaru.Console;
using Marshal = Aaru.Helpers.Marshal;
namespace Aaru.Decoders.Sega;
2022-03-06 13:29:37 +00:00
/// <summary>Represents the IP.BIN from a SEGA Saturn</summary>
2023-10-03 23:09:28 +01:00
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "MemberCanBeInternal")]
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
2022-03-06 13:29:37 +00:00
public static class Saturn
{
const string MODULE_NAME = "Saturn IP.BIN Decoder";
2022-03-06 13:29:37 +00:00
/// <summary>Decodes an IP.BIN sector in Saturn format</summary>
/// <param name="ipbin_sector">IP.BIN sector</param>
/// <returns>Decoded IP.BIN</returns>
public static IPBin? DecodeIPBin(byte[] ipbin_sector)
{
2024-05-01 04:05:22 +01:00
if(ipbin_sector == null) return null;
2022-03-06 13:29:37 +00:00
2024-05-01 04:05:22 +01:00
if(ipbin_sector.Length < 512) return null;
2022-03-06 13:29:37 +00:00
IPBin ipbin = Marshal.ByteArrayToStructureLittleEndian<IPBin>(ipbin_sector);
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.maker_id = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.maker_id));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.product_no = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.product_no));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.product_version = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.product_version));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.release_date = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.release_date));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.saturn_media = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.saturn_media));
AaruConsole.DebugWriteLine(MODULE_NAME, "saturn_ipbin.disc_no = {0}", (char)ipbin.disc_no);
2022-03-06 13:29:37 +00:00
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.disc_no_separator = \"{0}\"",
2022-03-06 13:29:37 +00:00
(char)ipbin.disc_no_separator);
AaruConsole.DebugWriteLine(MODULE_NAME, "saturn_ipbin.disc_total_nos = {0}", (char)ipbin.disc_total_nos);
2022-03-06 13:29:37 +00:00
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.release_date = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.release_date));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.spare_space1 = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.spare_space1));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.region_codes = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.region_codes));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.peripherals = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.peripherals));
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
"saturn_ipbin.product_name = \"{0}\"",
2022-03-06 13:29:37 +00:00
Encoding.ASCII.GetString(ipbin.product_name));
2022-03-07 07:36:42 +00:00
return Encoding.ASCII.GetString(ipbin.SegaHardwareID) == "SEGA SEGASATURN " ? ipbin : null;
2022-03-06 13:29:37 +00:00
}
/// <summary>Pretty prints a decoded IP.BIN in Saturn format</summary>
/// <param name="decoded">Decoded IP.BIN</param>
/// <returns>Description of the IP.BIN contents</returns>
public static string Prettify(IPBin? decoded)
{
2024-05-01 04:05:22 +01:00
if(decoded == null) return null;
2022-03-06 13:29:37 +00:00
IPBin ipbin = decoded.Value;
2022-03-06 13:29:37 +00:00
var IPBinInformation = new StringBuilder();
2022-03-06 13:29:37 +00:00
IPBinInformation.AppendLine("--------------------------------");
IPBinInformation.AppendLine(Localization.SEGA_IP_BIN_INFORMATION);
2022-03-06 13:29:37 +00:00
IPBinInformation.AppendLine("--------------------------------");
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
// Decoding all data
CultureInfo provider = CultureInfo.InvariantCulture;
var ipbindate = DateTime.ParseExact(Encoding.ASCII.GetString(ipbin.release_date), "yyyyMMdd", provider);
2019-11-25 00:54:38 +00:00
2024-05-01 04:05:22 +01:00
IPBinInformation.AppendFormat(Localization.Product_name_0, Encoding.ASCII.GetString(ipbin.product_name))
.AppendLine();
2019-11-25 00:54:38 +00:00
2023-10-04 08:16:20 +01:00
IPBinInformation.Append($"Product number: {Encoding.ASCII.GetString(ipbin.product_no)}").AppendLine();
2019-11-25 00:54:38 +00:00
2024-05-01 04:05:22 +01:00
IPBinInformation.AppendFormat(Localization.Product_version_0, Encoding.ASCII.GetString(ipbin.product_version))
.AppendLine();
2019-11-25 00:54:38 +00:00
IPBinInformation.AppendFormat(Localization.Release_date_0, ipbindate).AppendLine();
2019-11-25 00:54:38 +00:00
2024-05-01 04:05:22 +01:00
IPBinInformation.AppendFormat(Localization.Disc_number_0_of_1, (char)ipbin.disc_no, (char)ipbin.disc_total_nos)
.AppendLine();
2019-11-25 00:54:38 +00:00
IPBinInformation.AppendFormat(Localization.Peripherals).AppendLine();
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
foreach(byte peripheral in ipbin.peripherals)
2023-10-03 23:09:28 +01:00
{
2022-03-06 13:29:37 +00:00
switch((char)peripheral)
{
case 'A':
IPBinInformation.AppendLine(Localization.Game_supports_analog_controller);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'J':
IPBinInformation.AppendLine(Localization.Game_supports_JoyPad);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'K':
IPBinInformation.AppendLine(Localization.Game_supports_keyboard);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'M':
IPBinInformation.AppendLine(Localization.Game_supports_mouse);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'S':
IPBinInformation.AppendLine(Localization.Game_supports_analog_steering_controller);
2022-03-06 13:29:37 +00:00
break;
case 'T':
IPBinInformation.AppendLine(Localization.Game_supports_multitap);
2022-03-06 13:29:37 +00:00
break;
2023-10-03 23:09:28 +01:00
case ' ':
break;
2022-03-06 13:29:37 +00:00
default:
2024-05-01 04:05:22 +01:00
IPBinInformation.AppendFormat(Localization.Game_supports_unknown_peripheral_0, peripheral)
.AppendLine();
2022-03-06 13:29:37 +00:00
break;
}
2023-10-03 23:09:28 +01:00
}
IPBinInformation.AppendLine(Localization.Regions_supported);
2022-03-06 13:29:37 +00:00
foreach(byte region in ipbin.region_codes)
2023-10-03 23:09:28 +01:00
{
2022-03-06 13:29:37 +00:00
switch((char)region)
{
case 'J':
IPBinInformation.AppendLine(Localization.Japanese_NTSC);
2022-03-06 13:29:37 +00:00
break;
case 'U':
IPBinInformation.AppendLine(Localization.North_America_NTSC);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'E':
IPBinInformation.AppendLine(Localization.Europe_PAL);
2019-11-25 00:54:38 +00:00
2022-03-06 13:29:37 +00:00
break;
case 'T':
IPBinInformation.AppendLine(Localization.Asia_NTSC);
2022-03-06 13:29:37 +00:00
break;
2023-10-03 23:09:28 +01:00
case ' ':
break;
2022-03-06 13:29:37 +00:00
default:
IPBinInformation.AppendFormat(Localization.Game_supports_unknown_region_0, region).AppendLine();
2022-03-06 13:29:37 +00:00
break;
}
2023-10-03 23:09:28 +01:00
}
2022-03-06 13:29:37 +00:00
return IPBinInformation.ToString();
}
2023-10-03 23:09:28 +01:00
#region Nested type: IPBin
2022-03-06 13:29:37 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct IPBin
{
/// <summary>Must be "SEGA SEGASATURN "</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] SegaHardwareID;
/// <summary>0x010, "SEGA ENTERPRISES"</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] maker_id;
/// <summary>0x020, Product number</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public byte[] product_no;
/// <summary>0x02A, Product version</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] product_version;
/// <summary>0x030, YYYYMMDD</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] release_date;
/// <summary>0x038, "CD-"</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] saturn_media;
/// <summary>0x03B, Disc number</summary>
public byte disc_no;
/// <summary>// 0x03C, '/'</summary>
public byte disc_no_separator;
/// <summary>// 0x03D, Total number of discs</summary>
public byte disc_total_nos;
/// <summary>0x03E, " "</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public byte[] spare_space1;
/// <summary>0x040, Region codes, space-filled</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] region_codes;
/// <summary>0x050, Supported peripherals, see above</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] peripherals;
/// <summary>0x060, Game name, space-filled</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 112)]
public byte[] product_name;
}
2023-10-03 23:09:28 +01:00
#endregion
2017-12-19 20:33:03 +00:00
}