- All Superinterfaces:
AutoCloseable,Cartridge
A Game Boy cartridge.
- See Also:
- The Cycle-Accurate Game Boy Docs, Pan Docs
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceThe header of a Game Boy cartridge.static classSpecifies the memory bank controller (if any) and other hardware present in the cartridge. -
Method Summary
Modifier and TypeMethodDescriptionshortComputes the checksum of the ROM contents.getAscii(long offset, int length)Decodeslengthbytes to characters using theStandardCharsets.US_ASCIIcharset, beginning at the given offset in the cartridge.bytegetByte(long offset)Reads the byte at the given offset.voidgetBytes(long offset, byte[] dest)Transfers bytes from the cartridge into the given array, beginning at the given offset in the cartridge.voidgetBytes(long offset, byte[] dest, int destOffset, int length)Transferslengthbytes from the cartridge into the given array, beginning at the given offset in the cartridge and at the given offset in the array.intgetInt(long offset)Reads the int at the given offset.longgetLong(long offset)Reads the long at the given offset.shortgetShort(long offset)Reads the short at the given offset.Decodeslengthbytes to characters using the given charset, beginning at the given offset in the cartridge.intgetUnsignedByte(long offset)Reads the unsigned byte at the given offset.longgetUnsignedInt(long offset)Reads the unsigned int at the given offset.intgetUnsignedShort(long offset)Reads the unsigned short at the given offset.getUtf8(long offset, int length)Decodeslengthbytes to characters using theStandardCharsets.UTF_8charset, beginning at the given offset in the cartridge.header()Returns the cartridge header.static GBCartridgeread(InputStream stream)Reads a cartridge from the given stream.static GBCartridgeReads a cartridge from the given path.readAscii(int length)Decodeslengthbytes to characters using theStandardCharsets.US_ASCIIcharset, beginning at the cartridge's current offset, which is then incremented bylength.bytereadByte()Reads the byte at the cartridge's current offset, and then increments the offset.voidreadBytes(byte[] dest)Transfers bytes from the cartridge into the given array, beginning at the cartridge's current offset, which is then incremented bydest.length.voidreadBytes(byte[] dest, int destOffset, int length)Transferslengthbytes from the cartridge into the given array, beginning at the cartridge's current offset and at the given offset in the array.intreadInt()Reads the int at the cartridge's current offset, and then increments the offset byInteger.BYTES.longreadLong()Reads the long at the cartridge's current offset, and then increments the offset byLong.BYTES.shortReads the short at the cartridge's current offset, and then increments the offset byShort.BYTES.readString(int length, Charset charset)Decodeslengthbytes to characters using the given charset, beginning at the cartridge's current offset, which is then incremented bylength.intReads the unsigned byte at the cartridge's current offset, and then increments the offset.longReads the unsigned int at the cartridge's current offset, and then increments the offset byInteger.BYTES.intReads the unsigned short at the cartridge's current offset, and then increments the offset byShort.BYTES.readUtf8(int length)Decodeslengthbytes to characters using theStandardCharsets.UTF_8charset, beginning at the cartridge's current offset, which is then incremented bylength.intsetAscii(long offset, CharSequence sequence)Writes the encoded characters using theStandardCharsets.US_ASCIIcharset, beginning at the given offset in the cartridge.voidsetByte(long offset, byte value)Writes the given byte at the given offset.voidsetBytes(long offset, byte[] source)Transfers bytes from the given array into the cartridge, beginning at the given offset in the cartridge.voidsetBytes(long offset, byte[] source, int sourceOffset, int length)Transferslengthbytes from the given array into the cartridge, beginning at the given offset in the cartridge and at the given offset in the array.voidsetInt(long offset, int value)Writes the given int at the given offset.voidsetLong(long offset, long value)Writes the given long at the given offset.voidsetShort(long offset, short value)Writes the given short at the given offset.intsetString(long offset, CharSequence sequence, Charset charset)Writes the encoded characters using the given charset, beginning at the given offset in the cartridge.voidsetUnsignedByte(long offset, int value)Writes the given unsigned byte at the given offset.voidsetUnsignedInt(long offset, long value)Writes the given unsigned int at the given offset.voidsetUnsignedShort(long offset, int value)Writes the given unsigned short at the given offset.intsetUtf8(long offset, CharSequence sequence)Writes the encoded characters using theStandardCharsets.UTF_8charset, beginning at the given offset in the cartridge.intwriteAscii(CharSequence sequence)Writes the encoded characters using theStandardCharsets.US_ASCIIcharset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.voidwriteByte(byte value)Writes the given byte at the cartridge's current offset, and then increments the offset.voidwriteBytes(byte[] source)Transfers the entire content of the given array into the cartridge, beginning at the cartridge's current offset, which is then incremented bysource.length.voidwriteBytes(byte[] source, int sourceOffset, int length)Transferslengthbytes from the given array into the cartridge, beginning at the cartridge's current offset and at the given offset in the array.voidwriteInt(int value)Writes the given int at the cartridge's current offset, and then increments the offset byInteger.BYTES.voidwriteLong(long value)Writes the given long at the cartridge's current offset, and then increments the offset byLong.BYTES.voidwriteShort(short value)Writes the given short at the cartridge's current offset, and then increments the offset byShort.BYTES.intwriteString(CharSequence sequence, Charset charset)Writes the encoded characters using the given charset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.voidwriteUnsignedByte(int value)Writes the given unsigned byte at the cartridge's current offset, and then increments the offset.voidwriteUnsignedInt(long value)Writes the given unsigned int at the cartridge's current offset, and then increments the offset byInteger.BYTES.voidwriteUnsignedShort(int value)Writes the given unsigned short at the cartridge's current offset, and then increments the offset byShort.BYTES.intwriteUtf8(CharSequence sequence)Writes the encoded characters using theStandardCharsets.UTF_8charset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.
-
Method Details
-
read
Reads a cartridge from the given path.- Parameters:
path- the path- Returns:
- the read cartridge
- Throws:
IOException- if an I/O error occurs
-
read
Reads a cartridge from the given stream. The stream is not closed.- Parameters:
stream- the input stream- Returns:
- the read cartridge
- Throws:
IOException- if an I/O error occurs
-
header
GBCartridge.Header header()Returns the cartridge header.- Returns:
- the header
-
computeChecksum
short computeChecksum()Computes the checksum of the ROM contents.- Returns:
- the computed global checksum
- See Also:
to get the global checksum,to update the global checksum
-
readByte
byte readByte()Reads the byte at the cartridge's current offset, and then increments the offset.- Returns:
- the byte value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is 0
-
getByte
byte getByte(long offset)Reads the byte at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the byte value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than or equal toCartridge.size()
-
readUnsignedByte
int readUnsignedByte()Reads the unsigned byte at the cartridge's current offset, and then increments the offset.- Returns:
- the unsigned byte value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is 0
-
getUnsignedByte
int getUnsignedByte(long offset)Reads the unsigned byte at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the unsigned byte value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than or equal toCartridge.size()
-
writeByte
void writeByte(byte value)Writes the given byte at the cartridge's current offset, and then increments the offset.- Parameters:
value- the byte value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is 0
-
setByte
void setByte(long offset, byte value)Writes the given byte at the given offset.- Parameters:
offset- the offset to write tovalue- the byte value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than or equal toCartridge.size()
-
writeUnsignedByte
void writeUnsignedByte(int value)Writes the given unsigned byte at the cartridge's current offset, and then increments the offset.- Parameters:
value- the unsigned byte value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is 0
-
setUnsignedByte
void setUnsignedByte(long offset, int value)Writes the given unsigned byte at the given offset.- Parameters:
offset- the offset to write tovalue- the byte value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than or equal toCartridge.size()
-
readShort
short readShort()Reads the short at the cartridge's current offset, and then increments the offset byShort.BYTES.- Returns:
- the short value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanShort.BYTES.
-
getShort
short getShort(long offset)Reads the short at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the short value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Short#BYTES})
-
readUnsignedShort
int readUnsignedShort()Reads the unsigned short at the cartridge's current offset, and then increments the offset byShort.BYTES.- Returns:
- the unsigned short value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanShort.BYTES.
-
getUnsignedShort
int getUnsignedShort(long offset)Reads the unsigned short at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the unsigned short value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than{@link Cartridge#size()} - {@link Short#BYTES}
-
writeShort
void writeShort(short value)Writes the given short at the cartridge's current offset, and then increments the offset byShort.BYTES.- Parameters:
value- the short value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanShort.BYTES
-
setShort
void setShort(long offset, short value)Writes the given short at the given offset.- Parameters:
offset- the offset to write tovalue- the short value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Short#BYTES})
-
writeUnsignedShort
void writeUnsignedShort(int value)Writes the given unsigned short at the cartridge's current offset, and then increments the offset byShort.BYTES.- Parameters:
value- the unsigned short value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanShort.BYTES
-
setUnsignedShort
void setUnsignedShort(long offset, int value)Writes the given unsigned short at the given offset.- Parameters:
offset- the offset to write tovalue- the unsigned short value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Short#BYTES})
-
readInt
int readInt()Reads the int at the cartridge's current offset, and then increments the offset byInteger.BYTES.- Returns:
- the int value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanInteger.BYTES.
-
getInt
int getInt(long offset)Reads the int at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the int value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Integer#BYTES})
-
readUnsignedInt
long readUnsignedInt()Reads the unsigned int at the cartridge's current offset, and then increments the offset byInteger.BYTES.- Returns:
- the unsigned int value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanInteger.BYTES.
-
getUnsignedInt
long getUnsignedInt(long offset)Reads the unsigned int at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the unsigned int value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Integer#BYTES})
-
writeInt
void writeInt(int value)Writes the given int at the cartridge's current offset, and then increments the offset byInteger.BYTES.- Parameters:
value- the int value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanInteger.BYTES
-
setInt
void setInt(long offset, int value)Writes the given int at the given offset.- Parameters:
offset- the offset to write tovalue- the int value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Integer#BYTES})
-
writeUnsignedInt
void writeUnsignedInt(long value)Writes the given unsigned int at the cartridge's current offset, and then increments the offset byInteger.BYTES.- Parameters:
value- the unsigned int value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanInteger.BYTES
-
setUnsignedInt
void setUnsignedInt(long offset, long value)Writes the given unsigned int at the given offset.- Parameters:
offset- the offset to write tovalue- the unsigned int value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Integer#BYTES})
-
readLong
long readLong()Reads the long at the cartridge's current offset, and then increments the offset byLong.BYTES.- Returns:
- the long value at the current offset
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanLong.BYTES.
-
getLong
long getLong(long offset)Reads the long at the given offset.- Parameters:
offset- the offset to read from- Returns:
- the long value at the given offset
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Long#BYTES})
-
writeLong
void writeLong(long value)Writes the given long at the cartridge's current offset, and then increments the offset byLong.BYTES.- Parameters:
value- the long value to write- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanLong.BYTES
-
setLong
void setLong(long offset, long value)Writes the given long at the given offset.- Parameters:
offset- the offset to write tovalue- the long value to write- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - {@link Long#BYTES})
-
readBytes
void readBytes(byte[] dest)Transfers bytes from the cartridge into the given array, beginning at the cartridge's current offset, which is then incremented bydest.length.- Parameters:
dest- the destination array- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thandest.length
-
readBytes
void readBytes(byte[] dest, int destOffset, int length)Transferslengthbytes from the cartridge into the given array, beginning at the cartridge's current offset and at the given offset in the array. The cartridge's offset is then incremented bylength.- Parameters:
dest- the destination arraydestOffset- the offset within the array of the first byte to be writtenlength- the number of bytes to be written- Throws:
IndexOutOfBoundsException- ifdestOffsetis out of bounds, i.e. less than 0 or greater thandest.length - destOffset; orCartridge.remaining()is less thanlength
-
getBytes
void getBytes(long offset, byte[] dest)Transfers bytes from the cartridge into the given array, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge from which the first byte will be readdest- the destination array- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - dest.length)
-
getBytes
void getBytes(long offset, byte[] dest, int destOffset, int length)Transferslengthbytes from the cartridge into the given array, beginning at the given offset in the cartridge and at the given offset in the array.- Parameters:
offset- the offset in the cartridge from which the first byte will be readdest- the destination arraydestOffset- the offset within the array of the first byte to be writtenlength- the number of bytes to be written- Throws:
IndexOutOfBoundsException- ifoffsetis out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - length); ordestOffsetis out of bounds, i.e. less than 0 or greater thandest.length - length; orCartridge.remaining()is less thanlength
-
writeBytes
void writeBytes(byte[] source)Transfers the entire content of the given array into the cartridge, beginning at the cartridge's current offset, which is then incremented bysource.length.- Parameters:
source- the source array- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thansource.length
-
writeBytes
void writeBytes(byte[] source, int sourceOffset, int length)Transferslengthbytes from the given array into the cartridge, beginning at the cartridge's current offset and at the given offset in the array. The cartridge's offset is then incremented bylength.- Parameters:
source- the source arraysourceOffset- the offset within the array of the first byte to be readlength- the number of bytes to be written- Throws:
IndexOutOfBoundsException- ifsourceOffsetis out of bounds, i.e. less than 0 or greater than(source.length - sourceOffset); orCartridge.remaining()is less thanlength
-
setBytes
void setBytes(long offset, byte[] source)Transfers bytes from the given array into the cartridge, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge at which the first byte will be writtensource- the source array- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater than{@link Cartridge#remaining()} - source.length
-
setBytes
void setBytes(long offset, byte[] source, int sourceOffset, int length)Transferslengthbytes from the given array into the cartridge, beginning at the given offset in the cartridge and at the given offset in the array.- Parameters:
offset- the offset in the cartridge at which the first byte will be writtensource- the source arraysourceOffset- the offset within the array of the first byte to be readlength- the number of bytes to be written- Throws:
IndexOutOfBoundsException- ifoffsetis out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - length); orsourceOffsetis out of bounds, i.e. less than 0 or greater than(source.length - length); orCartridge.remaining()is less thanlength
-
readString
Decodeslengthbytes to characters using the given charset, beginning at the cartridge's current offset, which is then incremented bylength.- Parameters:
length- the number of bytes to be readcharset- the charset to be used- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanlength
-
getString
Decodeslengthbytes to characters using the given charset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge from which the first byte will be readlength- the number of bytes to be readcharset- the charset to be used- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifoffsetis out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - length)
-
readAscii
Decodeslengthbytes to characters using theStandardCharsets.US_ASCIIcharset, beginning at the cartridge's current offset, which is then incremented bylength.- Parameters:
length- the number of bytes to be read- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanlength
-
getAscii
Decodeslengthbytes to characters using theStandardCharsets.US_ASCIIcharset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge from which the first byte will be readlength- the number of bytes to be read- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifoffsetis out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - length)
-
readUtf8
Decodeslengthbytes to characters using theStandardCharsets.UTF_8charset, beginning at the cartridge's current offset, which is then incremented bylength.- Parameters:
length- the number of bytes to be read- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less thanlength
-
getUtf8
Decodeslengthbytes to characters using theStandardCharsets.UTF_8charset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge from which the first byte will be readlength- the number of bytes to be read- Returns:
- a string containing the decoded characters
- Throws:
IndexOutOfBoundsException- ifoffsetis out of bounds, i.e. less than 0 or greater than({@link Cartridge#size()} - length)
-
writeString
Writes the encoded characters using the given charset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.- Parameters:
sequence- the sequence of characters to be encodedcharset- the charset to be used- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()} is less than the number of bytes to be written
-
setString
Writes the encoded characters using the given charset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge at which the first byte will be writtensequence- the sequence of characters to be encodedcharset- the charset to be used- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater thanCartridge.size()minus the number of bytes to be written
-
writeAscii
Writes the encoded characters using theStandardCharsets.US_ASCIIcharset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.- Parameters:
sequence- the sequence of characters to be encoded- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less than the number of bytes to be written
-
setAscii
Writes the encoded characters using theStandardCharsets.US_ASCIIcharset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge at which the first byte will be writtensequence- the sequence of characters to be encoded- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater thanCartridge.size()minus the number of bytes to be written
-
writeUtf8
Writes the encoded characters using theStandardCharsets.UTF_8charset, beginning at the cartridge's current offset, which is then incremented by the number of written bytes.- Parameters:
sequence- the sequence of characters to be encoded- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- ifCartridge.remaining()is less than the number of bytes to be written
-
setUtf8
Writes the encoded characters using theStandardCharsets.UTF_8charset, beginning at the given offset in the cartridge.- Parameters:
offset- the offset in the cartridge at which the first byte will be writtensequence- the sequence of characters to be encoded- Returns:
- the number of bytes written to the cartridge
- Throws:
IndexOutOfBoundsException- if the given offset is out of bounds, i.e. less than 0 or greater thanCartridge.size()minus the number of bytes to be written
-