Class Memory.DataRegion

java.lang.Object
mars.mips.hardware.Memory.DataRegion
Enclosing class:
Memory

public static class Memory.DataRegion extends Object
Class representing an arbitrary contiguous region of memory which contains data.

Obviously, allocating one array for the entire region of memory would be extremely wasteful (and perhaps not even possible), so the region is divided into many fixed-size tables, each of which is further subdivided into many blocks, a block being an array of memory words. Currently, each block contains 1024 words, and each table contains 1024 blocks. Thus, each block holds 4 KiB of data, and each table can accommodate up to 4 MiB of data in total.

Although this structure uses a three-dimensional array, this makes it relatively space-efficient since only the outermost array of tables is allocated initially. Tables and blocks are only allocated when a write occurs somewhere in their range; otherwise, they are simply left as null references. In addition, the array of tables only has as many elements as is necessary to fully cover the region. A base address is used as the memory offset of the first table in the array.

Sean Clarke (05/2024): The note below is interesting, but not really relevant. I'll keep it here for now.

The SPIM simulator stores statically allocated data (i.e. data following a .data directive) starting at address 0x10010000. This is the first word beyond the reach of $gp used in conjunction with a signed 16-bit immediate offset. $gp has value 0x10008000, and with the signed 16-bit offset, it can reach from 0x10008000 - 0x8000 = 0x10000000 (base of the data segment) to 0x10008000 + 0x7FFF = 0x1000FFFF (the byte preceding 0x10010000).

  • Constructor Summary

    Constructors
    Constructor
    Description
    DataRegion(int firstAddress, int lastAddress)
    Allocate a new region of memory containing data.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    fetchWord(int address)
    Fetch a word from the region at a given address.
    fetchWordOrNull(int address)
    Fetch a word from the region at a given address, or null if the word lies in a block which has not been allocated before.
    int
    storeWord(int address, int value)
    Store a word in the region at a given address.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DataRegion

      public DataRegion(int firstAddress, int lastAddress)
      Allocate a new region of memory containing data.
      Parameters:
      firstAddress - The lowest address this region is required to contain.
      lastAddress - The highest address this region is required to contain.
  • Method Details

    • storeWord

      public int storeWord(int address, int value)
      Store a word in the region at a given address. The caller is responsible for ensuring that the address is word-aligned and falls within this region, as no checking will be done.
      Parameters:
      address - The address to store the word at.
      value - The value to store at the given address.
      Returns:
      The previous value which was overwritten (defaults to 0).
    • fetchWord

      public int fetchWord(int address)
      Fetch a word from the region at a given address. The caller is responsible for ensuring that the address is word-aligned and falls within this region, as no checking will be done.
      Parameters:
      address - The address of the word to fetch.
      Returns:
      The value stored at the given address (defaults to 0).
    • fetchWordOrNull

      public Integer fetchWordOrNull(int address)
      Fetch a word from the region at a given address, or null if the word lies in a block which has not been allocated before. The caller is responsible for ensuring that the address is word-aligned and falls within this region, as no checking will be done.

      Originally developed by Greg Gibeling of UC Berkeley, fall 2007.

      Parameters:
      address - The address of the word to fetch.
      Returns:
      The value stored at the given address (defaults to 0 within allocated blocks).