View Javadoc

1   /*
2    * $Id: DefaultStoryFileHeader.java,v 1.1 2005/11/03 19:04:34 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.vm;
24  
25  import org.zmpp.base.MemoryReadAccess;
26  
27  /***
28   * This is the default implementation of the StoryFileHeader interface.
29   * 
30   * @author Wei-ju Wu
31   * @version 1.0
32   */
33  public class DefaultStoryFileHeader implements StoryFileHeader {
34  
35    /***
36     * The memory map.
37     */
38    private MemoryReadAccess memaccess;
39    
40    /***
41     * Constructor.
42     * 
43     * @param memaccess a MemoryReadAccess object
44     */
45    public DefaultStoryFileHeader(MemoryReadAccess memaccess) {
46      
47      this.memaccess = memaccess;
48    }
49  
50    /***
51     * {@inheritDoc}
52     */
53    public int getVersion() { return memaccess.readUnsignedByte(0x00); }
54    
55    /***
56     * {@inheritDoc}
57     */
58    public int getFlags1() { return memaccess.readUnsignedByte(0x01); }
59    
60    /***
61     * {@inheritDoc}
62     */
63    public int getRelease() { return memaccess.readUnsignedShort(0x02); }
64    
65    /***
66     * {@inheritDoc}
67     */
68    public int getHighMemAddress() { return memaccess.readUnsignedShort(0x04); }
69    
70    /***
71     * {@inheritDoc}
72     */
73    public int getProgramStart() { return memaccess.readUnsignedShort(0x06); }
74    
75    /***
76     * {@inheritDoc}
77     */
78    public int getDictionaryAddress() {
79      
80      return memaccess.readUnsignedShort(0x08);
81    }
82    
83    /***
84     * {@inheritDoc}
85     */
86    public int getObjectTableAddress() {
87      
88      return memaccess.readUnsignedShort(0x0a);
89    }
90    
91    /***
92     * {@inheritDoc}
93     */
94    public int getGlobalsAddress() { return memaccess.readUnsignedShort(0x0c); }
95    
96    /***
97     * {@inheritDoc}
98     */
99    public int getStaticsAddress() { return memaccess.readUnsignedShort(0x0e); }
100   
101   /***
102    * {@inheritDoc}
103    */
104   public int getFlags2() { return memaccess.readUnsignedByte(0x10); }
105   
106   /***
107    * {@inheritDoc}
108    */
109   public String getSerialNumber() { return extractAscii(0x12, 6); }
110   
111   /***
112    * {@inheritDoc}
113    */
114   public int getAbbreviationsAddress() {
115     
116     return memaccess.readUnsignedShort(0x18);
117   }
118   
119   /***
120    * {@inheritDoc}
121    */
122   public int getFileLength() {
123     int fileLength = memaccess.readUnsignedShort(0x1a);
124     if (getVersion() <= 3) fileLength *= 2;      
125     return fileLength;
126   }
127   
128   /***
129    * {@inheritDoc}
130    */
131   public int getChecksum() { return memaccess.readUnsignedShort(0x1c); }
132   
133   /***
134    * {@inheritDoc}
135    */
136   public int getInterpreter() { return memaccess.readUnsignedByte(0x1e); }
137   
138   /***
139    * {@inheritDoc}
140    */
141   public int getInterpreterVersion() {
142     
143     return memaccess.readUnsignedByte(0x1f);
144   }
145   
146   /***
147    * {@inheritDoc}
148    */
149   public int getRevision() { return memaccess.readUnsignedShort(0x32); }  
150 
151   /***
152    * Extract an ASCII string of the specified length starting at the specified
153    * address.
154    * 
155    * @param address the start address
156    * @param length the length of the ASCII string
157    * @return the ASCII string at the specified position
158    */
159   private String extractAscii(int address, int length) {
160     
161     StringBuilder builder = new StringBuilder();
162     for (int i = address; i < address + length; i++) {
163       
164       builder.append((char) memaccess.readUnsignedByte(i));
165     }
166     return builder.toString();
167   }
168 }