View Javadoc

1   /*
2    * $Id: MemorySection.java,v 1.1 2005/11/01 00:22:28 weiju Exp $
3    * 
4    * Created on 2005/09/23
5    * Copyright 2005 by Wei-ju Wu
6    *
7    * This file is part of The Z-machine Preservation Project (ZMPP).
8    *
9    * ZMPP is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation; either version 2 of the License, or
12   * (at your option) any later version.
13   *
14   * ZMPP is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with ZMPP; if not, write to the Free Software
21   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22   */
23  package org.zmpp.base;
24  
25  /***
26   * A MemorySection object wraps a reference to a MemoryAccess object, a length
27   * and a start to support subsections within memory.
28   * All access functions will be relative to the initialized start offset
29   * within the global memory.
30   * 
31   * @author Wei-ju Wu
32   * @version 1.0
33   */
34  public class MemorySection implements MemoryAccess {
35  
36    private MemoryAccess memaccess;
37    private int start;
38    private int length;
39    
40    public MemorySection(MemoryAccess memaccess, int start, int length) {
41      
42      this.memaccess = memaccess;
43      this.start = start;
44      this.length = length;
45    }
46    
47    /***
48     * Returns the length of this object in bytes.
49     * 
50     * @return the length in bytes
51     */
52    public int getLength() {
53      
54      return length;
55    }
56    
57    /***
58     * {@inheritDoc}
59     */
60    public void writeUnsignedShort(int address, int value) {
61      
62      memaccess.writeUnsignedShort(address + start, value);
63    }
64  
65    /***
66     * {@inheritDoc}
67     */
68    public void writeShort(int address, short value) {
69      
70      memaccess.writeShort(address + start, value);
71    }
72  
73    /***
74     * {@inheritDoc}
75     */
76    public void writeUnsignedByte(int address, short value) {
77  
78      memaccess.writeUnsignedByte(address + start, value);
79    }
80  
81    /***
82     * {@inheritDoc}
83     */
84    public void writeByte(int address, byte value) {
85  
86      memaccess.writeByte(address + start, value);
87    }
88  
89    /***
90     * {@inheritDoc}
91     */
92    public void writeUnsigned32(int address, long value) {
93  
94      memaccess.writeUnsigned32(address + start, value);
95    }
96  
97    /***
98     * {@inheritDoc}
99     */
100   public long readUnsigned32(int address) {
101     
102     return memaccess.readUnsigned32(address + start);
103   }
104 
105   /***
106    * {@inheritDoc}
107    */
108   public int readUnsignedShort(int address) {
109     
110     return memaccess.readUnsignedShort(address + start);
111   }
112 
113   /***
114    * {@inheritDoc}
115    */
116   public short readShort(int address) {
117     
118     return memaccess.readShort(address + start);
119   }
120 
121   /***
122    * {@inheritDoc}
123    */
124   public short readUnsignedByte(int address) {
125     
126     return memaccess.readUnsignedByte(address + start);
127   }
128 
129   /***
130    * {@inheritDoc}
131    */
132   public byte readByte(int address) {
133     
134     return memaccess.readByte(address + start);
135   }
136 }