Skip to content

Commit

Permalink
More error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ClusterM committed Nov 28, 2022
1 parent 50912d0 commit 704c3bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions NesContainers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
<PackageProjectUrl>https://github.com/ClusterM/nes-containers</PackageProjectUrl>
<Copyright>Alexey 'Cluster' Avdyukhin, 2022</Copyright>
<Description>A simple .NET Standard 2.0 library for reading and modifying NES/Famicom ROM containers: .nes (iNES, NES 2.0), .unf (UNIF), and .fds (Famicom Disk System images).</Description>
<Version>1.1.2</Version>
<Version>1.1.3</Version>
<RepositoryUrl>https://github.com/ClusterM/nes-containers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>nes,famicom,famicom disk system</PackageTags>
<AssemblyVersion>1.1.2</AssemblyVersion>
<FileVersion>1.1.2</FileVersion>
<AssemblyVersion>1.1.3</AssemblyVersion>
<FileVersion>1.1.3</FileVersion>
<Authors>Alexey 'Cluster' Avdyukhin</Authors>
</PropertyGroup>

Expand Down
10 changes: 9 additions & 1 deletion NesFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ public byte[] ToBytes()
throw new InvalidDataException("Mapper number > 255 is supported by NES 2.0 only");
if (Submapper != 0)
throw new InvalidDataException("Submapper number is supported by NES 2.0 only");
if (prgRamSize > 0)
throw new InvalidDataException("PRG RAM size is supported by NES 2.0 only");
if (prgNvRamSize > 0)
throw new InvalidDataException("PRG NVRAM size is supported by NES 2.0 only");
if (chrRamSize > 0)
throw new InvalidDataException("CHR RAM size is supported by NES 2.0 only");
if (chrNvRamSize > 0)
throw new InvalidDataException("CHR NVRAM size is supported by NES 2.0 only");
var length16k = prg.Length / 0x4000;
if (length16k > 0xFF) throw new ArgumentOutOfRangeException("PRG size is too big for iNES, use NES 2.0 instead");
header[4] = (byte)Math.Ceiling((double)prg.Length / 0x4000);
Expand Down Expand Up @@ -833,7 +841,7 @@ public byte[] ToBytes()
header[8] |= (byte)(Submapper << 4);
// Check battery value
if ((prgNvRamSize > 0 || chrNvRamSize > 0) && !Battery)
throw new InvalidDataException("Battery flag must be set when PrgNvRamSize or ChrNvRamSize is non-zero");
throw new InvalidDataException("Battery flag must be set when PRG NVRAM size or CHR NVRAM size is non-zero");
// PRG RAM (volatile) shift count
header[10] |= (byte)(prgRamSize & 0x0F);
// PRG-NVRAM/EEPROM (non-volatile) shift count
Expand Down

0 comments on commit 704c3bf

Please sign in to comment.