View Javadoc

1   /*
2    * $Id: DefaultMemoryAccess.java,v 1.1 2005/10/25 16:27:59 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   * This class is the default implementation for MemoryAccess.
27   * 
28   * @author Wei-ju Wu
29   * @version 1.0
30   */
31  public class DefaultMemoryAccess implements MemoryAccess {
32  
33    /***
34     * The data array containing the story file.
35     */
36    private byte[] data;  
37    
38    /***
39     * Constructor.
40     * 
41     * @param data the story file data
42     */
43    public DefaultMemoryAccess(byte[] data) {
44      
45      this.data = data;    
46    }
47    
48    /***
49     * {@inheritDoc}
50     */
51    public long readUnsigned32(int address) {
52    
53      return (data[address] & 0xff) << 24 | (data[address + 1] & 0xff) << 16
54             | (data[address + 2] & 0xff) << 8 | (data[address + 3] & 0xff);
55    }
56    
57    /***
58     * {@inheritDoc}
59     */
60    public int readUnsignedShort(int address) {
61      
62      return (data[address] & 0xff) << 8 | (data[address + 1] & 0xff);
63    }
64    
65    /***
66     * {@inheritDoc}
67     */
68    public short readShort(int address) {
69      
70      return (short) (data[address] << 8 | (data[address + 1] & 0xff));
71    }
72    
73    /***
74     * {@inheritDoc}
75     */
76    public short readUnsignedByte(int address) {
77      
78      return (short) (data[address] & 0xff);
79    }
80    
81    /***
82     * {@inheritDoc}
83     */
84    public byte readByte(int address) {
85      
86      return data[address];
87    }
88    
89    /***
90     * Writes an unsigned 16 bit value to the specified address.
91     * 
92     * @param address the address to write to
93     * @param value the value to write
94     */
95    public void writeUnsignedShort(int address, int value) {
96      
97      data[address] = (byte) ((value & 0xff00) >> 8);
98      data[address + 1] = (byte) (value & 0xff);
99    }
100   
101   /***
102    * Writes a short value to the memory.
103    * 
104    * @param address the address
105    * @param value the value
106    */
107   public void writeShort(int address, short value) {
108     
109     data[address] = (byte) ((value & 0xff00) >>> 8);
110     data[address + 1] = (byte) (value & 0xff);
111   }
112   
113   /***
114    * Writes an unsigned byte value to the specified address.
115    * 
116    * @param address the address to write to
117    * @param value the value to write
118    */
119   public void writeUnsignedByte(int address, short value) {
120     
121     data[address] = (byte) (value & 0xff);
122   }
123   
124   /***
125    * Writes a byte value to the specified address.
126    * 
127    * @param address the address
128    * @param value the value
129    */
130   public void writeByte(int address, byte value) {
131     
132     data[address] = value;
133   }
134   
135   /***
136    * Writes an unsigned 32 bit value to the specified address.
137    * 
138    * @param address the address to write to
139    * @param value the value to write
140    */
141   public void writeUnsigned32(int address, long value) {
142     
143     data[address] = (byte) ((value & 0xff000000) >> 24);
144     data[address + 1] = (byte) ((value & 0x00ff0000) >> 16);
145     data[address + 2] = (byte) ((value & 0x0000ff00) >> 8);
146     data[address + 3] = (byte) (value & 0x000000ff);
147   }
148   
149 }