1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }