uk.co.javagear
Class Memory

java.lang.Object
  extended by uk.co.javagear.Memory

public final class Memory
extends java.lang.Object

Emulates SMS/GG Memory and Paging Hardware. Also Handles Reading ROMs and configuring the memory appropriately.

Version:
18th January 2003
Author:
Copyright (C) 2002-2003 Chris White, Jesús Adolfo García Pasquel
See Also:
"JavaGear Final Project Report"

Field Summary
private  int cartRamPage
          Stores which page of cartridge RAM to use.
private  byte[][] cartRamPages
          Array of cartridge RAM pages.
private  byte[][] frames
          Array of memory frames.
private  boolean frameTwoRom
          Stores whether frame 2 contains cartridge ROM or cartridge RAM.
private  int numberOfPages
          Stores number of pages.
private  byte[][] pages
          Array of memory pages.
private  Setup setup
          Pointer to general parameters.
 
Constructor Summary
Memory(Setup set)
          Memory constructor.
 
Method Summary
private static java.lang.String getExtension(java.lang.String filename)
          Strip an extension from a file.
private static java.util.zip.ZipEntry getRomEntry(java.util.zip.ZipInputStream zis)
          Returns the first entry in the ZipInputStream that contains an SMS or GG ROM or null if none is found.
private  void page(int address, int value)
          Write to a paging register.
 int read(int address)
          Read from a memory location.
private  void readCart(java.io.InputStream is, int size)
          Read a cartridge's data from an InputStream.
 void readCart(java.net.URL url)
          Read a cartridge into memory from the specified URL.
 int readSigned(int address)
          Read a signed value from a memory location.
 int readWord(int address)
          Read a word (two bytes) from a memory location.
 void reset()
          Reset memory to default values.
private  int unsigned(int b)
          Unsign an integer.
 void write(int address, int value)
          Write to a memory location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

frames

private byte[][] frames
Array of memory frames.


pages

private byte[][] pages
Array of memory pages.


cartRamPages

private byte[][] cartRamPages
Array of cartridge RAM pages.


frameTwoRom

private boolean frameTwoRom
Stores whether frame 2 contains cartridge ROM or cartridge RAM.


cartRamPage

private int cartRamPage
Stores which page of cartridge RAM to use.


numberOfPages

private int numberOfPages
Stores number of pages.


setup

private Setup setup
Pointer to general parameters.

Constructor Detail

Memory

public Memory(Setup set)
Memory constructor.

Parameters:
set - pointer to general parameters.
Method Detail

reset

public void reset()
Reset memory to default values.


write

public void write(int address,
                  int value)
Write to a memory location.

Parameters:
address - memory address
value - value to write

read

public int read(int address)
Read from a memory location.

Parameters:
address - memory location.
Returns:
value from memory location.

readSigned

public int readSigned(int address)
Read a signed value from a memory location.

Parameters:
address - Memory address
Returns:
Value from memory location

readWord

public int readWord(int address)
Read a word (two bytes) from a memory location.

Parameters:
address - memory address.
Returns:
value from memory location

unsigned

private int unsigned(int b)
Unsign an integer.

Parameters:
b - value to unsign.
Returns:
unsigned value.

page

private void page(int address,
                  int value)
Write to a paging register.

Parameters:
address - memory location.
value - value to write.

readCart

public void readCart(java.net.URL url)
              throws java.io.IOException,
                     java.lang.IllegalArgumentException
Read a cartridge into memory from the specified URL. The file it points to must be a ROM for the SMS or GG, or a zip file containing one.

Parameters:
url - URL of cartridge image.
Throws:
java.io.IOException - if an error occurs while reading the file.
java.lang.IllegalArgumentException - if the file is neither a ROM, nor a Zip file containing one.

getRomEntry

private static java.util.zip.ZipEntry getRomEntry(java.util.zip.ZipInputStream zis)
                                           throws java.io.IOException
Returns the first entry in the ZipInputStream that contains an SMS or GG ROM or null if none is found. If the stream does not include a ROM or an IOException occurs while looking for a valid entry, the stream will be closed. Otherwise it is left open, pointing at the ROM's contents.

Parameters:
zis - an instance of ZipInputStream, may be null.
Returns:
an SMS or GG ROM in the form of a ZipEntry or null if none were found.
Throws:
java.io.IOException - if an error occurs while reading from the stream.

readCart

private void readCart(java.io.InputStream is,
                      int size)
               throws java.io.IOException,
                      java.lang.IllegalArgumentException
Read a cartridge's data from an InputStream. The method does not close when it finishes reading.

Parameters:
is - the stream to read the ROM's data from.
size - the size of the stream's content.
Throws:
java.lang.IllegalArgumentException - if is is null or size.
java.io.IOException - if an error occurs while reading the file.

getExtension

private static java.lang.String getExtension(java.lang.String filename)
Strip an extension from a file.

Parameters:
filename - filename to strip.
Returns:
extension of file in lowercase characters.


Copyright © 2007 JavaGear. All Rights Reserved.