dmolony-DiskBrowser/src/com/bytezone/diskbrowser/infocom/Header.java

159 lines
5.0 KiB
Java
Raw Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.infocom;
import java.io.File;
2016-07-20 05:22:33 +00:00
import com.bytezone.diskbrowser.disk.Disk;
2016-02-05 07:18:23 +00:00
class Header extends InfocomAbstractFile
2015-06-01 09:35:51 +00:00
{
2016-02-05 07:18:23 +00:00
final String[] propertyNames = new String[32];
2016-07-19 07:44:42 +00:00
private final File file;
2016-02-05 07:18:23 +00:00
int version;
int highMemory;
int programCounter;
int dictionaryOffset;
2019-04-22 04:35:50 +00:00
int objectTableOffset;
2016-02-05 07:18:23 +00:00
int globalsOffset;
int staticMemory;
int abbreviationsTable;
int fileLength;
int checksum;
int stringPointer;
2016-07-20 10:35:13 +00:00
final Abbreviations abbreviations;
final ObjectManager objectManager;
final Globals globals;
final Grammar grammar;
2019-04-22 04:35:50 +00:00
final Dictionary dictionary;
final CodeManager codeManager;
final StringManager stringManager;
2016-02-05 07:18:23 +00:00
2016-07-20 05:22:33 +00:00
public Header (String name, byte[] buffer, Disk disk)
2016-02-05 07:18:23 +00:00
{
super (name, buffer);
2016-07-20 05:22:33 +00:00
this.file = disk.getFile ();
2016-02-05 07:18:23 +00:00
2019-04-22 04:35:50 +00:00
version = getByte (00);
highMemory = getWord (0x04);
programCounter = getWord (0x06);
dictionaryOffset = getWord (0x08);
objectTableOffset = getWord (0x0A);
globalsOffset = getWord (0x0C);
staticMemory = getWord (0x0E);
abbreviationsTable = getWord (0x18);
fileLength = getWord (0x1A) * 2; // 2 for versions 1-3
checksum = getWord (0x1C);
int interpreterNumber = getByte (0x1E);
int interpreterVersion = getByte (0x1F);
int revision = getWord (0x30);
System.out.printf ("Version : %d%n", version);
System.out.printf ("Interpreter: %d.%d%n", interpreterNumber, interpreterVersion);
System.out.printf ("Revision : %d%n", revision);
2016-02-05 07:18:23 +00:00
if (fileLength == 0)
fileLength = buffer.length;
// do the basic managers
abbreviations = new Abbreviations (this);
dictionary = new Dictionary (this);
2016-07-19 07:44:42 +00:00
globals = new Globals (this); // may display ZStrings
2016-02-05 07:18:23 +00:00
// set up an empty object to store Routines in
codeManager = new CodeManager (this);
grammar = new Grammar ("Grammar", buffer, this);
// add all the ZObjects, and analyse them to find stringPtr, DICT etc.
objectManager = new ObjectManager (this);
// add all the ZStrings
stringManager = new StringManager ("Strings", buffer, this);
codeManager.addRoutine (programCounter - 1, 0);
2016-07-19 07:44:42 +00:00
codeManager.addActionRoutines (); // obtained from Grammar
codeManager.addCodeRoutines (); // obtained from Object properties
codeManager.addMissingRoutines (); // requires stringPtr to be set
2016-02-05 07:18:23 +00:00
// add entries for AbstractFile.getHexDump ()
hexBlocks.add (new HexBlock (0, 64, "Header data:"));
}
2019-04-22 04:35:50 +00:00
String getPropertyName (int id)
{
return propertyNames[id];
}
2016-02-05 07:18:23 +00:00
public String getAbbreviation (int index)
{
return abbreviations.getAbbreviation (index);
}
public boolean containsWordAt (int address)
{
return dictionary.containsWordAt (address);
}
public String wordAt (int address)
{
return dictionary.wordAt (address);
}
@Override
public String getText ()
{
StringBuilder text = new StringBuilder ();
text.append (String.format ("Disk name %s%n", file.getName ()));
text.append (String.format ("Version %d%n", version));
text.append ("\nDynamic memory:\n");
text.append (String.format (" Abbreviation table %04X %,6d%n",
2016-12-04 02:33:21 +00:00
abbreviationsTable, abbreviationsTable));
2019-04-22 04:35:50 +00:00
text.append (String.format (" Objects table %04X %,6d%n",
objectTableOffset, objectTableOffset));
2016-02-05 07:18:23 +00:00
text.append (String.format (" Global variables %04X %,6d%n", globalsOffset,
2016-12-04 02:33:21 +00:00
globalsOffset));
2016-02-05 07:18:23 +00:00
text.append ("\nStatic memory:\n");
text.append (String.format (" Grammar table etc %04X %,6d%n", staticMemory,
2016-12-04 02:33:21 +00:00
staticMemory));
2016-02-05 07:18:23 +00:00
text.append (String.format (" Dictionary %04X %,6d%n", dictionaryOffset,
2016-12-04 02:33:21 +00:00
dictionaryOffset));
2016-02-05 07:18:23 +00:00
text.append ("\nHigh memory:\n");
2016-12-04 02:33:21 +00:00
text.append (
String.format (" ZCode %04X %,6d%n", highMemory, highMemory));
2016-02-05 07:18:23 +00:00
text.append (String.format (" Program counter %04X %,6d%n", programCounter,
2016-12-04 02:33:21 +00:00
programCounter));
text.append (
String.format ("\nFile length %05X %,7d%n", fileLength, fileLength));
text.append (
String.format ("Checksum %04X %,6d%n", checksum, checksum));
text.append (String.format ("%nZString offset %05X %,7d%n", stringPointer,
stringPointer));
2016-02-05 07:18:23 +00:00
text.append (String.format ("Total strings %d%n",
2016-12-04 02:33:21 +00:00
stringManager.strings.size ()));
2016-02-05 07:18:23 +00:00
text.append (String.format ("Total objects %d%n",
2019-04-22 04:35:50 +00:00
objectManager.getObjects ().size ()));
2016-02-05 07:18:23 +00:00
return text.toString ();
}
2019-04-22 04:35:50 +00:00
ZObject getObject (int index)
{
return objectManager.getObject (index);
}
2016-02-05 07:18:23 +00:00
int getByte (int offset)
{
return buffer[offset] & 0xFF;
}
int getWord (int offset)
{
return ((buffer[offset] << 8) & 0xFF00) | ((buffer[offset + 1]) & 0xFF);
}
2015-06-01 09:35:51 +00:00
}