Advertisement
Chiddix

ROM

Apr 6th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.nio.charset.Charset;
  6.  
  7. /**
  8.  * Represents a single Gameboy or Gameboy Color ROM.
  9.  *
  10.  * @author Ryan Greene
  11.  *
  12.  */
  13. public final class ROM {
  14.  
  15.     /**
  16.      * The file where the ROM is located.
  17.      */
  18.     private final File file;
  19.  
  20.     /**
  21.      * The input stream used to read the ROM.
  22.      */
  23.     private final InputStream input;
  24.  
  25.     /**
  26.      * The bytes which make up the ROM.
  27.      */
  28.     private final byte[] data;
  29.  
  30.     /**
  31.      * Title of the game in UPPER CASE ASCII. If it is less than 16 characters
  32.      * then the remaining bytes are filled with 00's. When inventing the CGB,
  33.      * Nintendo has reduced the length of this area to 15 characters, and some
  34.      * months later they had the fantastic idea to reduce it to 11 characters
  35.      * only.
  36.      */
  37.     private final String title;
  38.  
  39.     /**
  40.      * In older cartridges this area has been part of the Title, in newer
  41.      * cartridges this area contains an 4 character uppercase manufacturer code.
  42.      * Purpose and Deeper Meaning unknown.
  43.      */
  44.     private final String manufacturerCode;
  45.  
  46.     /**
  47.      * In older cartridges this byte has been part of the Title. In CGB
  48.      * cartridges the upper bit is used to enable CGB functions. This is
  49.      * required, otherwise the CGB switches itself into Non-CGB-Mode.
  50.      */
  51.     private final boolean cgb;
  52.  
  53.     /**
  54.      * Specifies a two character ASCII licensee code, indicating the company or
  55.      * publisher of the game. These two bytes are used in newer games only
  56.      * (games that have been released after the SGB has been invented). Older
  57.      * games are using the header entry at 014B instead.
  58.      */
  59.     private final String newLicenseeCode;
  60.  
  61.     /**
  62.      * Specifies whether the game supports SGB functions.
  63.      */
  64.     private final boolean sgb;
  65.  
  66.     /**
  67.      * Constructs a new ROM from the specified directory.
  68.      *
  69.      * @param directory
  70.      *            The directory the ROM is located.
  71.      * @throws IOException
  72.      *             If an exception occurs while constructing the ROM.
  73.      */
  74.     public ROM(final String directory) throws IOException {
  75.         file = new File(directory);
  76.         input = new FileInputStream(file);
  77.         data = new byte[(int) file.length()];
  78.         input.read(data);
  79.  
  80.         title = new String(data, 0x134, 11, Charset.forName("US-ASCII"));
  81.         manufacturerCode = new String(data, 0x13F, 4);
  82.         cgb = data[0x143] == 0xC0 ? true : false;
  83.         newLicenseeCode = new String(data, 0x144, 2, Charset.forName("US-ASCII"));
  84.         sgb = data[0x164] == 0x3 ? true : false;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement