jfrace.device
Class RAM

java.lang.Object
  |
  +--jfrace.device.RAM
All Implemented Interfaces:
Device, Hardware

public class RAM
extends java.lang.Object
implements Device

This class emulates the random access memory (RAM) of a computer. By setting the readOnly flag it can be turned into a ROM.

Author:
Franz-Josef Elmer

Constructor Summary
RAM()
          Create an empty memory.
RAM(int numberOfBits)
          Create an empty memory with a given size of the addressable unit.
RAM(int numberOfBits, int numberOfUnits, boolean readOnlyFlag)
          Create an empty memory with a given size of the addressable unit and a given number of addressable units.
RAM(int numberOfBits, java.net.URL source, boolean readOnlyFlag)
          Create a memory with a given size of the addressable unit and filled with raw data from a given source.
 
Method Summary
 void createMemory(int numberOfUnits)
          Create memory of a given number of addressable units.
static void createRAMAndAddToAS(AddressSpace as, java.lang.String description, java.net.URL documentBase)
          Create an instance from a description string and add it to the given address space.
 byte getByte(int address)
          Get the byte at the given address.
 byte[] getMemory()
          Return the memory array.
 int getNumberOfBits()
          Return the size of an addressable unit.
 int getNumberOfUnits()
          Return the number of addressable units.
 int getValue(int address)
          Get the value of the addressable unit at the given address.
 boolean isReadOnly()
          Return true if read only.
 void reset()
          Fill the memory with zeros.
 void setByte(int address, byte value)
          Set the byte at the given address.
 void setValue(int address, int value)
          Set the value of the addressable unit at the given address.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RAM

public RAM()
Create an empty memory. The addressable unit is one byte.

RAM

public RAM(int numberOfBits)
Create an empty memory with a given size of the addressable unit.
Parameters:
numberOfBits - the size of an addressable unit in number of bits.

RAM

public RAM(int numberOfBits,
           int numberOfUnits,
           boolean readOnlyFlag)
Create an empty memory with a given size of the addressable unit and a given number of addressable units.
Parameters:
numberOfBits - The size of an addressable unit in number of bits.
numberOfUnits - Number of addressable units.
readOnlyFlag - readOnly flag.

RAM

public RAM(int numberOfBits,
           java.net.URL source,
           boolean readOnlyFlag)
    throws java.io.IOException
Create a memory with a given size of the addressable unit and filled with raw data from a given source. The raw data is a sequence of byte where chunks of numberOfBytes belong to an addressable unit. numberOfBytes is the number of bytes to cover an addressable unit with numberOfBits bits. The data in a chunk is stored in big-endian form (i.e., the highest bytes come first).
Parameters:
numberOfBits - the size of an addressable unit in number of bits.
source - Source of the raw data.
readOnlyFlag - readOnly flag.
Throws:
java.io.IOException - is thrown when an error occured during obtaining the raw data.
Method Detail

createRAMAndAddToAS

public static void createRAMAndAddToAS(AddressSpace as,
                                       java.lang.String description,
                                       java.net.URL documentBase)
                                throws AddressBankOverlapException,
                                       java.lang.IllegalArgumentException
Create an instance from a description string and add it to the given address space. The description string has the form:

<number of bits> <first address> {<size>|<raw data>} [<mode mask>] [readOnly]

where
<number of bits>
is the number of bits of an addressable unit
<first address>
is the first address in the address space
<size>
the number of addressable units
<raw data>
URL source with initial raw data
<mode mask>
the mode mask
readOnly
if the RAM is actually a ROM
The last two terms are optional. If the third parameter is not a number it will be interpreted as a file name. The URL source contains the initial content of the RAM.
Parameters:
as - Address space.
description - Description string.
documentBase - URL of the document base if <raw data> is a relative URL.
Throws:
java.lang.IllegalArgumentException - is thrown when the description string has some error.
AddressBankOverlayException - is thrown when a collision between the RAM and other devices in the address space occur.

isReadOnly

public boolean isReadOnly()
Return true if read only. That is, all invocations of setValue and setByte. will be ignored.

createMemory

public void createMemory(int numberOfUnits)
Create memory of a given number of addressable units.
Parameters:
numberOfUnits - number of addressable units.

getMemory

public byte[] getMemory()
Return the memory array.

reset

public void reset()
Fill the memory with zeros.
Specified by:
reset in interface Hardware

getNumberOfBits

public int getNumberOfBits()
Return the size of an addressable unit.
Specified by:
getNumberOfBits in interface Hardware

getNumberOfUnits

public int getNumberOfUnits()
Return the number of addressable units.
Specified by:
getNumberOfUnits in interface Device

getValue

public int getValue(int address)
Get the value of the addressable unit at the given address.
Specified by:
getValue in interface Device
Parameters:
address - memory address.
Returns:
the value.

setValue

public void setValue(int address,
                     int value)
Set the value of the addressable unit at the given address.
Specified by:
setValue in interface Device
Parameters:
address - memory address.
value - the new value.

getByte

public byte getByte(int address)
Get the byte at the given address. This method is similar to getValue but has a better performance.
WARNING: This method should be used only when the addressable unit is a byte.
Specified by:
getByte in interface Device
Parameters:
address - memory address.
Returns:
the value of the byte at address.

setByte

public void setByte(int address,
                    byte value)
Set the byte at the given address. This method is similar to setValue but has a better performance.
WARNING: This method should be used only when the addressable unit is a byte.
Specified by:
setByte in interface Device
Parameters:
address - memory address.
value - new value of the byte at address.