org.razvan.jzx
Class BaseMemory

java.lang.Object
  extended by org.razvan.jzx.BaseComponent
      extended by org.razvan.jzx.BaseMemory
Direct Known Subclasses:
Memory, Memory

public abstract class BaseMemory
extends BaseComponent

Base class extended by all memory components that comprise the emulator.

This class provides basic, common memory functionality for the memory subsystem of the emulator. Specific methods are overriden or implemented so as to comply with the Spectrum 48k and the Spectrum 128k models.

Author:
Razvan Surdulescu (c) 2001 - 2006, Erik Kunze (c) 1995, 1996, 1997, Des Herriott (c) 1993, 1994
You may use and distribute this software for free provided you include this copyright notice. You may not sell this software, use my name for publicity reasons or modify the code without permission from me.
See Also:
Memory, Memory

Field Summary
protected  byte[][] m_frame
          Maps the frame number to the physical page data.
protected  int[] m_frameToPage
          Maps a frame number to a physical page number.
protected  byte[][] m_page
          The full memory array.
protected  BaseScreen m_screen
          Cached reference to the screen component.
static int PAGE_SIZE
          The size, in bytes, for a memory page.
static int RAM0
          The first RAM page.
static int RAM1
          The second RAM page.
static int RAM2
          The third RAM page.
static int RAM3
          The fourth RAM page.
static int RAM4
          The fifth RAM page.
static int RAM5
          The sixth RAM page.
static int RAM6
          The seventh RAM page.
static int RAM7
          The eighth RAM page.
static int ROM0
          The first ROM page.
static int ROM1
          The second ROM page.
static int ROM2
          The third ROM page.
static int ROM3
          The fourth ROM page.
 
Fields inherited from class org.razvan.jzx.BaseComponent
m_logger, m_spectrum
 
Constructor Summary
BaseMemory()
           
 
Method Summary
 byte[] getBytes(int page)
          Returns the byte array that comprises a particular page.
 void init(BaseSpectrum spectrum, ILogger logger)
          Allocate the memory arrays, cache the screen component and pages in the appropriate memory pages.
 void load(BaseLoader loader)
          Load the memory contents from the given BaseLoader object.
 void pageIn(int frame, int page)
          Maps a particular physical page into a given virtual frame number.
 int read16(int addr16)
          Reads a word starting at a particular 16-bit address.
 int read8(int addr16)
          Reads a byte from a particular 16-bit address.
protected  void readROM(java.lang.String name, byte[] page)
          Load the contents of a ROM file into the given memory page.
 void terminate()
          Sets the memory arrays to null and releases the cached screen reference.
 java.lang.String toString()
          Return a string representation of the memory state, which is useful for debugging.
 void write16(int addr16, int val16)
          Writes a word starting at a particular 16-bit address.
 void write8(int addr16, int val8)
          Writes a byte to a particular 16-bit address.
 
Methods inherited from class org.razvan.jzx.BaseComponent
getSpectrum, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PAGE_SIZE

public static final int PAGE_SIZE
The size, in bytes, for a memory page.

See Also:
Constant Field Values

ROM0

public static final int ROM0
The first ROM page.

See Also:
Constant Field Values

ROM1

public static final int ROM1
The second ROM page.

See Also:
Constant Field Values

ROM2

public static final int ROM2
The third ROM page.

See Also:
Constant Field Values

ROM3

public static final int ROM3
The fourth ROM page.

See Also:
Constant Field Values

RAM0

public static final int RAM0
The first RAM page.

See Also:
Constant Field Values

RAM1

public static final int RAM1
The second RAM page.

See Also:
Constant Field Values

RAM2

public static final int RAM2
The third RAM page.

See Also:
Constant Field Values

RAM3

public static final int RAM3
The fourth RAM page.

See Also:
Constant Field Values

RAM4

public static final int RAM4
The fifth RAM page.

See Also:
Constant Field Values

RAM5

public static final int RAM5
The sixth RAM page.

See Also:
Constant Field Values

RAM6

public static final int RAM6
The seventh RAM page.

See Also:
Constant Field Values

RAM7

public static final int RAM7
The eighth RAM page.

See Also:
Constant Field Values

m_frameToPage

protected int[] m_frameToPage
Maps a frame number to a physical page number.

There are four possible frame numbers (0 to 3.)


m_frame

protected byte[][] m_frame
Maps the frame number to the physical page data.

There are four possible frame numbers (0 to 3.)


m_page

protected byte[][] m_page
The full memory array.

The sixteen possible phisical pages (4 ROM pages and 12 RAM pages.)


m_screen

protected BaseScreen m_screen
Cached reference to the screen component.

This is used to update the screen whenever the appropriate memory area is modified.

Constructor Detail

BaseMemory

public BaseMemory()
Method Detail

init

public void init(BaseSpectrum spectrum,
                 ILogger logger)
Allocate the memory arrays, cache the screen component and pages in the appropriate memory pages.

Note that the cached screen reference can be null after this call, in the event that the BaseSpectrum parameter is itself null. This is allowed for "headless" memory objects that are not tied to a particular BaseSpectrum instance. This is used for loading the snapshot data into a memory instance, that is then copied directly into the physical BaseSpectrum memory instance.

Overrides:
init in class BaseComponent
See Also:
pageIn(int, int)

terminate

public void terminate()
Sets the memory arrays to null and releases the cached screen reference.

Overrides:
terminate in class BaseComponent

readROM

protected void readROM(java.lang.String name,
                       byte[] page)
                throws java.io.IOException
Load the contents of a ROM file into the given memory page.

The ROM file is specified as a Java Resource and loaded by means of Class.getResource(String).

Parameters:
name - The name of the resource where the ROM file resides.
page - The memory page into which to load the ROM file.
Throws:
java.io.IOException - Thrown if the resource is not found or an error occurs during reading.

pageIn

public void pageIn(int frame,
                   int page)
Maps a particular physical page into a given virtual frame number.

The method sets:

Parameters:
frame - The virtual frame number. You cannot map a RAM page into frame 0.
page - The physical page number.

getBytes

public byte[] getBytes(int page)
Returns the byte array that comprises a particular page.


read8

public int read8(int addr16)
Reads a byte from a particular 16-bit address.

The address is mapped into a frame number by taking the first two bits, and then frame number is then mapped into a physical page. The remaining address bits are used to index into the physical page.

Parameters:
addr16 - The 16-bit address to read from.
Returns:
Returns the 8 bits at the specified address.

write8

public void write8(int addr16,
                   int val8)
Writes a byte to a particular 16-bit address.

The address is mapped into a frame number by taking the first two bits, and then frame number is then mapped into a physical page. The remaining address bits are used to index into the physical page.

Nothing is written to a ROM page.

If the address falls into the memory mapped area of the screen memory, the screen object is updated appropriately.

Parameters:
addr16 - The 16-bit address to write to.
val8 - The 8 bits to write at the specified address.

read16

public int read16(int addr16)
Reads a word starting at a particular 16-bit address.

The method calls read8() for two consecutive addresses and collates the results.

Parameters:
addr16 - The 16-bit address to start reading from.
Returns:
Returns the 16 bits from the two consecutive addresses.
See Also:
read8(int)

write16

public void write16(int addr16,
                    int val16)
Writes a word starting at a particular 16-bit address.

The method calls write8() for two consecutive addresses, with the two halves of the specified argument.

Parameters:
addr16 - The 16-bit address to start writing to.
val16 - The 16 bits to start writing at the specified address.
See Also:
write16(int, int)

load

public void load(BaseLoader loader)
Load the memory contents from the given BaseLoader object.

The BaseLoader contains a "headless" memory object inside it, whose page contents are copied into the page contents of "this" memory object.

Specified by:
load in class BaseComponent
See Also:
BaseLoader

toString

public java.lang.String toString()
Return a string representation of the memory state, which is useful for debugging.

Overrides:
toString in class java.lang.Object