infocom changes

This commit is contained in:
Denis Molony 2019-04-22 14:35:50 +10:00
parent 8764dd0db8
commit 76125f9a4e
16 changed files with 1051 additions and 870 deletions

View File

@ -20,7 +20,7 @@ class Abbreviations extends InfocomAbstractFile
dataPtr = header.getWord (header.abbreviationsTable) * 2; dataPtr = header.getWord (header.abbreviationsTable) * 2;
dataSize = header.abbreviationsTable - dataPtr; dataSize = header.abbreviationsTable - dataPtr;
tablePtr = header.abbreviationsTable; tablePtr = header.abbreviationsTable;
tableSize = header.objectTable - header.abbreviationsTable; tableSize = header.objectTableOffset - header.abbreviationsTable;
// prepare hex dump // prepare hex dump
hexBlocks.add (new HexBlock (dataPtr, dataSize, "Abbreviations data:")); hexBlocks.add (new HexBlock (dataPtr, dataSize, "Abbreviations data:"));
@ -29,21 +29,17 @@ class Abbreviations extends InfocomAbstractFile
private void populate () private void populate ()
{ {
System.out.println ("populating abbreviations");
list = new ArrayList<ZString> (); list = new ArrayList<ZString> ();
for (int i = header.abbreviationsTable; i < header.objectTable; i += 2) for (int i = header.abbreviationsTable; i < header.objectTableOffset; i += 2)
{ list.add (new ZString (header, header.getWord (i) * 2));
int j = header.getWord (i) * 2;
ZString zs = new ZString (buffer, j, header);
list.add (zs);
}
} }
public String getAbbreviation (int abbreviationNumber) public String getAbbreviation (int abbreviationNumber)
{ {
if (list == null) if (list == null)
populate (); populate ();
return list.get (abbreviationNumber).value; return list.get (abbreviationNumber).value;
} }

View File

@ -69,7 +69,7 @@ class AttributeManager extends AbstractFile
new StringBuilder ("Objects with attribute " + id + " set:\n\n"); new StringBuilder ("Objects with attribute " + id + " set:\n\n");
for (ZObject o : list) for (ZObject o : list)
{ {
text.append (String.format ("%3d %-28s%n", o.id, o.getName ())); text.append (String.format ("%3d %-28s%n", o.getId (), o.getName ()));
} }
if (text.length () > 0) if (text.length () > 0)
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);

View File

@ -114,10 +114,10 @@ class CodeManager extends AbstractFile
for (Routine r : routines.values ()) for (Routine r : routines.values ())
{ {
int gap = r.startPtr - nextAddress; int gap = r.startPtr - nextAddress;
text.append (String text.append (String.format (
.format ("%3d %05X %5d %3d %2d %3d %3d %4d %04X%n", "%3d %05X %5d %3d %2d %3d %3d %4d %04X%n", ++count,
++count, r.startPtr, r.length, r.instructions.size (), r.strings, r.startPtr, r.length, r.instructions.size (), r.strings, r.calledBy.size (),
r.calledBy.size (), r.calls.size (), gap, r.startPtr / 2)); r.calls.size (), gap, r.startPtr / 2));
nextAddress = r.startPtr + r.length; nextAddress = r.startPtr + r.length;
} }

View File

@ -15,15 +15,12 @@ class Dictionary extends AbstractFile
private final int totalSeparators; private final int totalSeparators;
private final int dictionaryPtr, dictionarySize; private final int dictionaryPtr, dictionarySize;
private final int entryLength; private final int entryLength;
// private final Header header;
// this could be a Google Multimap
Map<Integer, List<WordEntry>> synonymList = new TreeMap<Integer, List<WordEntry>> (); Map<Integer, List<WordEntry>> synonymList = new TreeMap<Integer, List<WordEntry>> ();
public Dictionary (Header header) public Dictionary (Header header)
{ {
super ("Dictionary", header.buffer); super ("Dictionary", header.buffer);
// this.header = header;
dictionaryPtr = header.dictionaryOffset; dictionaryPtr = header.dictionaryOffset;
dictionary = new TreeMap<Integer, ZString> (); dictionary = new TreeMap<Integer, ZString> ();
@ -38,7 +35,7 @@ class Dictionary extends AbstractFile
int count = 0; int count = 0;
for (int i = 0; i < totalEntries; i++) for (int i = 0; i < totalEntries; i++)
{ {
ZString string = new ZString (buffer, ptr, header); ZString string = new ZString (header, ptr);
dictionary.put (ptr, string); dictionary.put (ptr, string);
WordEntry wordEntry = new WordEntry (string, count++); WordEntry wordEntry = new WordEntry (string, count++);
@ -56,8 +53,8 @@ class Dictionary extends AbstractFile
{ {
int b1 = buffer[ptr + 5] & 0xFF; int b1 = buffer[ptr + 5] & 0xFF;
int property = (b1 >= 1 && b1 <= 31) ? b1 : buffer[ptr + 6] & 0xFF; int property = (b1 >= 1 && b1 <= 31) ? b1 : buffer[ptr + 6] & 0xFF;
if (header.propertyNames[property] == null if (header.getPropertyName (property) == null
|| header.propertyNames[property].length () > string.value.length ()) || header.getPropertyName (property).length () > string.value.length ())
header.propertyNames[property] = string.value; header.propertyNames[property] = string.value;
} }
ptr += entryLength; ptr += entryLength;
@ -68,6 +65,48 @@ class Dictionary extends AbstractFile
for (int i = 1; i < header.propertyNames.length; i++) for (int i = 1; i < header.propertyNames.length; i++)
if (header.propertyNames[i] == null) if (header.propertyNames[i] == null)
header.propertyNames[i] = i + ""; header.propertyNames[i] = i + "";
// testing (only works in Zork 1)
if (false)
{
if (header.propertyNames[4].equals ("4"))
header.propertyNames[4] = "PSEUDO";
if (header.propertyNames[5].equals ("5"))
header.propertyNames[5] = "GLOBAL";
if (header.propertyNames[6].equals ("6"))
header.propertyNames[6] = "VTYPE";
if (header.propertyNames[7].equals ("7"))
header.propertyNames[7] = "STRENGTH";
if (header.propertyNames[10].equals ("10"))
header.propertyNames[10] = "CAPACITY";
if (header.propertyNames[12].equals ("12"))
header.propertyNames[12] = "TVALU";
if (header.propertyNames[13].equals ("13"))
header.propertyNames[13] = "VALUE";
if (header.propertyNames[15].equals ("15"))
header.propertyNames[15] = "SIZE";
if (header.propertyNames[16].equals ("16"))
header.propertyNames[16] = "ADJ";
}
// 4 = PSEUDO (property 4)
// 5 = GLOBAL (property 5)
// 6 = VTYPE (property 6)
// 7 = STRENGTH (property 7)
// STR3 = TEXT (property 8)
// CODE2 = DESCFCN (property 9)
// 10 = CAPACITY (property 10)
// STR1 = LDESC (property 11)
// 12 = TVALUE (property 12) value in trophy case
// 13 = VALUE (property 13)
// STR2 = FDESC (property 14)
// 15 = SIZE (property 15)
// 16 = ADJ (property 16)
// CODE1 = ACTION (property 17)
// 18 = DICT (property 18)
// 19 = LAND (property 19)
// 20 = OUT (property 20)
// 21 = IN (property 21)
} }
public boolean containsWordAt (int address) public boolean containsWordAt (int address)

View File

@ -1,13 +1,19 @@
package com.bytezone.diskbrowser.infocom; package com.bytezone.diskbrowser.infocom;
import java.util.ArrayList;
import java.util.List;
import com.bytezone.diskbrowser.infocom.Instruction.Operand;
class Globals extends InfocomAbstractFile class Globals extends InfocomAbstractFile
{ {
static final int TOTAL_GLOBALS = 240; private static final int TOTAL_GLOBALS = 240;
Header header; private final Header header;
int globalsPtr, globalsSize; private final int globalsPtr, globalsSize;
int arrayPtr, arraySize; private final int arrayPtr, arraySize;
private final List<List<Routine>> globalRoutines;
public Globals (Header header) Globals (Header header)
{ {
super ("Globals", header.buffer); super ("Globals", header.buffer);
this.header = header; this.header = header;
@ -20,21 +26,39 @@ class Globals extends InfocomAbstractFile
// add entries for AbstractFile.getHexDump () // add entries for AbstractFile.getHexDump ()
hexBlocks.add (new HexBlock (globalsPtr, globalsSize, "Globals:")); hexBlocks.add (new HexBlock (globalsPtr, globalsSize, "Globals:"));
hexBlocks.add (new HexBlock (arrayPtr, arraySize, "Arrays:")); hexBlocks.add (new HexBlock (arrayPtr, arraySize, "Arrays:"));
globalRoutines = new ArrayList<> (250);
for (int i = 0; i < 250; i++)
globalRoutines.add (new ArrayList<> ());
}
void addRoutine (Routine routine, Operand operand)
{
int global = operand.value - 15;
List<Routine> list = globalRoutines.get (global);
if (!list.contains (routine))
list.add (routine);
} }
@Override @Override
public String getText () public String getText ()
{ {
StringBuilder text = new StringBuilder (); StringBuilder text = new StringBuilder ("GLB Value Routines\n");
for (int i = 1; i <= TOTAL_GLOBALS; i++) for (int i = 1; i <= TOTAL_GLOBALS; i++)
{ {
int value = header.getWord (globalsPtr + i * 2); int value = header.getWord (globalsPtr + i * 2);
text.append (String.format ("G%03d %04X ", i, value)); text.append (String.format ("G%03d %04X %02d ", i, value,
globalRoutines.get (i).size ()));
int address = value * 2; int address = value * 2;
if (address >= header.stringPointer && address < header.fileLength) if (address >= header.stringPointer && address < header.fileLength)
text.append (header.stringManager.stringAt (address) + "\n"); text.append (header.stringManager.stringAt (address) + "\n");
else else
text.append (String.format ("%,6d%n", value)); {
for (Routine routine : globalRoutines.get (i))
text.append (String.format ("%05X ", routine.startPtr));
text.append ("\n");
}
// text.append (String.format ("%,6d%n", value));
} }
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);
return text.toString (); return text.toString ();

View File

@ -7,23 +7,24 @@ import com.bytezone.diskbrowser.utilities.HexFormatter;
class Grammar extends InfocomAbstractFile class Grammar extends InfocomAbstractFile
{ {
private static final int SENTENCE_LENGTH = 8; private static final int SENTENCE_LENGTH = 8;
Header header; private final Header header;
int indexPtr, indexSize; private final int indexPtr, indexSize;
int tablePtr, tableSize; private final int tablePtr, tableSize;
int actionPtr, actionSize; private final int actionPtr, actionSize;
int preActionPtr, preActionSize; private final int preActionPtr, preActionSize;
int prepositionPtr, prepositionSize; private final int prepositionPtr, prepositionSize;
int indexEntries; private final int indexEntries;
int totalPrepositions; private final int totalPrepositions;
int padding; private final int padding;
List<SentenceGroup> sentenceGroups = new ArrayList<SentenceGroup> (); private final List<SentenceGroup> sentenceGroups = new ArrayList<SentenceGroup> ();
Map<Integer, List<Sentence>> actionList = new TreeMap<Integer, List<Sentence>> (); private final Map<Integer, List<Sentence>> actionList =
new TreeMap<Integer, List<Sentence>> ();
List<Integer> actionRoutines = new ArrayList<Integer> (); private final List<Integer> actionRoutines = new ArrayList<Integer> ();
List<Integer> preActionRoutines = new ArrayList<Integer> (); private final List<Integer> preActionRoutines = new ArrayList<Integer> ();
public Grammar (String name, byte[] buffer, Header header) Grammar (String name, byte[] buffer, Header header)
{ {
super (name, buffer); super (name, buffer);
this.header = header; this.header = header;
@ -173,8 +174,8 @@ class Grammar extends InfocomAbstractFile
text.append (line); text.append (line);
} }
text.append ("\n" + actionRoutines.size () text.append (
+ " Action routines\n===================\n\n"); "\n" + actionRoutines.size () + " Action routines\n===================\n\n");
// add sentences in action routine sequence // add sentences in action routine sequence
for (Integer routine : actionRoutines) for (Integer routine : actionRoutines)

View File

@ -9,12 +9,11 @@ class Header extends InfocomAbstractFile
final String[] propertyNames = new String[32]; final String[] propertyNames = new String[32];
private final File file; private final File file;
// private final Disk disk;
int version; int version;
int highMemory; int highMemory;
int programCounter; int programCounter;
int dictionaryOffset; int dictionaryOffset;
int objectTable; int objectTableOffset;
int globalsOffset; int globalsOffset;
int staticMemory; int staticMemory;
int abbreviationsTable; int abbreviationsTable;
@ -23,29 +22,37 @@ class Header extends InfocomAbstractFile
int stringPointer; int stringPointer;
final Abbreviations abbreviations; final Abbreviations abbreviations;
final Dictionary dictionary;
final ObjectManager objectManager; final ObjectManager objectManager;
final StringManager stringManager;
final CodeManager codeManager;
final Globals globals; final Globals globals;
final Grammar grammar; final Grammar grammar;
final Dictionary dictionary;
final CodeManager codeManager;
final StringManager stringManager;
public Header (String name, byte[] buffer, Disk disk) public Header (String name, byte[] buffer, Disk disk)
{ {
super (name, buffer); super (name, buffer);
// this.disk = disk;
this.file = disk.getFile (); this.file = disk.getFile ();
version = getByte (0); version = getByte (00);
highMemory = getWord (4); highMemory = getWord (0x04);
programCounter = getWord (6); programCounter = getWord (0x06);
dictionaryOffset = getWord (8);
objectTable = getWord (10); dictionaryOffset = getWord (0x08);
globalsOffset = getWord (12); objectTableOffset = getWord (0x0A);
staticMemory = getWord (14); globalsOffset = getWord (0x0C);
abbreviationsTable = getWord (24); staticMemory = getWord (0x0E);
checksum = getWord (28); abbreviationsTable = getWord (0x18);
fileLength = getWord (26) * 2;
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);
if (fileLength == 0) if (fileLength == 0)
fileLength = buffer.length; fileLength = buffer.length;
@ -75,6 +82,11 @@ class Header extends InfocomAbstractFile
hexBlocks.add (new HexBlock (0, 64, "Header data:")); hexBlocks.add (new HexBlock (0, 64, "Header data:"));
} }
String getPropertyName (int id)
{
return propertyNames[id];
}
public String getAbbreviation (int index) public String getAbbreviation (int index)
{ {
return abbreviations.getAbbreviation (index); return abbreviations.getAbbreviation (index);
@ -100,8 +112,8 @@ class Header extends InfocomAbstractFile
text.append ("\nDynamic memory:\n"); text.append ("\nDynamic memory:\n");
text.append (String.format (" Abbreviation table %04X %,6d%n", text.append (String.format (" Abbreviation table %04X %,6d%n",
abbreviationsTable, abbreviationsTable)); abbreviationsTable, abbreviationsTable));
text.append (String.format (" Objects table %04X %,6d%n", objectTable, text.append (String.format (" Objects table %04X %,6d%n",
objectTable)); objectTableOffset, objectTableOffset));
text.append (String.format (" Global variables %04X %,6d%n", globalsOffset, text.append (String.format (" Global variables %04X %,6d%n", globalsOffset,
globalsOffset)); globalsOffset));
@ -125,11 +137,16 @@ class Header extends InfocomAbstractFile
text.append (String.format ("Total strings %d%n", text.append (String.format ("Total strings %d%n",
stringManager.strings.size ())); stringManager.strings.size ()));
text.append (String.format ("Total objects %d%n", text.append (String.format ("Total objects %d%n",
objectManager.list.size ())); objectManager.getObjects ().size ()));
return text.toString (); return text.toString ();
} }
ZObject getObject (int index)
{
return objectManager.getObject (index);
}
int getByte (int offset) int getByte (int offset)
{ {
return buffer[offset] & 0xFF; return buffer[offset] & 0xFF;

View File

@ -86,16 +86,16 @@ public class InfocomDisk extends AbstractFormattedDisk
stringsNode = addToTree (root, "Strings", header.stringManager, TYPE_LEAF); stringsNode = addToTree (root, "Strings", header.stringManager, TYPE_LEAF);
PropertyManager pm = new PropertyManager ("Properties", data, header); PropertyManager pm = new PropertyManager ("Properties", data, header);
pm.addNodes (addToTree (objectNode, "Properties", pm, TYPE_NODE), this); pm.addNodes (addToTree (root, "Properties", pm, TYPE_NODE), this);
AttributeManager am = new AttributeManager ("Attributes", data, header); AttributeManager am = new AttributeManager ("Attributes", data, header);
am.addNodes (addToTree (objectNode, "Attributes", am, TYPE_NODE), this); am.addNodes (addToTree (root, "Attributes", am, TYPE_NODE), this);
sectorTypes[48] = headerSector; sectorTypes[48] = headerSector;
setSectorTypes (header.abbreviationsTable, header.objectTable, abbreviationsSector, setSectorTypes (header.abbreviationsTable, header.objectTableOffset, abbreviationsSector,
abbreviationsNode); abbreviationsNode);
setSectorTypes (header.objectTable, header.globalsOffset, objectsSector, objectNode); setSectorTypes (header.objectTableOffset, header.globalsOffset, objectsSector, objectNode);
setSectorTypes (header.globalsOffset, header.staticMemory, globalsSector, setSectorTypes (header.globalsOffset, header.staticMemory, globalsSector,
globalsNode); globalsNode);
setSectorTypes (header.staticMemory, header.dictionaryOffset, grammarSector, setSectorTypes (header.staticMemory, header.dictionaryOffset, grammarSector,

View File

@ -13,24 +13,30 @@ class Instruction
// List<ZString> abbreviations; // List<ZString> abbreviations;
Header header; Header header;
enum OperandType
{
VAR_STACK, VAR_LOCAL, VAR_GLOBAL, BYTE, WORD, ARG_BRANCH, ARG_STRING
}
static final String[] name2OP = static final String[] name2OP =
{ "*bad*", "je", "jl", "jg", "dec_chk", "inc_chk", "jin", "test", "or", "and", "test_attr", { "*bad*", "je", "jl", "jg", "dec_chk", "inc_chk", "jin", "test", "or", "and",
"set_attr", "clear_attr", "store", "insert_obj", "loadw", "loadb", "get_prop", "test_attr", "set_attr", "clear_attr", "store", "insert_obj", "loadw", "loadb",
"get_prop_addr", "get_next_prop", "add", "sub", "mul", "div", "mod", "call_2s", "get_prop", "get_prop_addr", "get_next_prop", "add", "sub", "mul", "div", "mod",
"call_2n", "set_colour", "throw", "*bad*", "*bad*", "*bad*" }; "call_2s", "call_2n", "set_colour", "throw", "*bad*", "*bad*", "*bad*" };
static final String[] name1OP = static final String[] name1OP =
{ "jz", "get_sibling", "get_child", "get_parent", "get_prop_len", "inc", "dec", { "jz", "get_sibling", "get_child", "get_parent", "get_prop_len", "inc", "dec",
"print_addr", "call_ls", "remove_obj", "print_obj", "ret", "jump", "print_paddr", "load", "print_addr", "call_ls", "remove_obj", "print_obj", "ret", "jump", "print_paddr",
"not" }; "load", "not" };
static final String[] name0OP = static final String[] name0OP =
{ "rtrue", "rfalse", "print", "print_ret", "nop", "save", "restore", "restart", { "rtrue", "rfalse", "print", "print_ret", "nop", "save", "restore", "restart",
"ret_popped", "pop", "quit", "new_line", "show_status", "verify", "", "piracy" }; "ret_popped", "pop", "quit", "new_line", "show_status", "verify", "", "piracy" };
static final String[] nameVAR = static final String[] nameVAR =
{ "call", "storew", "storeb", "put_prop", "sread", "print_char", "print_num", "random", { "call", "storew", "storeb", "put_prop", "sread", "print_char", "print_num",
"push", "pull", "split_window", "set_window", "call_vs2", "erase_window", "erase_line", "random", "push", "pull", "split_window", "set_window", "call_vs2",
"set_cursor", "get_cursor", "set_text_style", "buffer_mode", "output_stream", "erase_window", "erase_line", "set_cursor", "get_cursor", "set_text_style",
"input_stream", "sound_effect", "read_char", "scan_table", "not", "call_vn", "call_vn2", "buffer_mode", "output_stream", "input_stream", "sound_effect", "read_char",
"tokenise", "encode_text", "copy_table", "print_table", "check_arg" }; "scan_table", "not", "call_vn", "call_vn2", "tokenise", "encode_text",
"copy_table", "print_table", "check_arg" };
public Instruction (byte[] buffer, int ptr, Header header) public Instruction (byte[] buffer, int ptr, Header header)
{ {
@ -260,7 +266,8 @@ class Instruction
if (opcodeNumber == 5 || opcodeNumber == 6 || opcodeNumber == 13) if (opcodeNumber == 5 || opcodeNumber == 6 || opcodeNumber == 13)
setBranch (buffer); setBranch (buffer);
if (opcodeNumber == 0 || opcodeNumber == 1 || opcodeNumber == 3 || opcodeNumber == 8) if (opcodeNumber == 0 || opcodeNumber == 1 || opcodeNumber == 3
|| opcodeNumber == 8)
isReturn = true; isReturn = true;
if (opcodeNumber == 2 || opcodeNumber == 3) if (opcodeNumber == 2 || opcodeNumber == 3)
@ -288,8 +295,8 @@ class Instruction
boolean bit2 = ((buffer[ptr] & 0x10) == 0x10); boolean bit2 = ((buffer[ptr] & 0x10) == 0x10);
addOperand (buffer, ptr + 1, bit1, bit2); addOperand (buffer, ptr + 1, bit1, bit2);
if ((opcodeNumber >= 1 && opcodeNumber <= 4) || opcodeNumber == 8 || opcodeNumber == 14 if ((opcodeNumber >= 1 && opcodeNumber <= 4) || opcodeNumber == 8
|| opcodeNumber == 15) || opcodeNumber == 14 || opcodeNumber == 15)
setStore (buffer); setStore (buffer);
if (opcodeNumber <= 2) if (opcodeNumber <= 2)
setBranch (buffer); setBranch (buffer);
@ -317,7 +324,8 @@ class Instruction
{ {
if ((opcodeNumber >= 1 && opcodeNumber <= 7) || opcodeNumber == 10) if ((opcodeNumber >= 1 && opcodeNumber <= 7) || opcodeNumber == 10)
setBranch (buffer); setBranch (buffer);
else if ((opcodeNumber >= 15 && opcodeNumber <= 25) || opcodeNumber == 8 || opcodeNumber == 9) else if ((opcodeNumber >= 15 && opcodeNumber <= 25) || opcodeNumber == 8
|| opcodeNumber == 9)
setStore (buffer); setStore (buffer);
} }
@ -381,6 +389,7 @@ class Instruction
{ {
int length; int length;
int value; int value;
OperandType operandType;
} }
class OperandWord extends Operand class OperandWord extends Operand
@ -389,6 +398,7 @@ class Instruction
{ {
this.value = value; this.value = value;
length = 2; length = 2;
operandType = OperandType.WORD;
} }
@Override @Override
@ -404,6 +414,7 @@ class Instruction
{ {
this.value = value & 0xFF; this.value = value & 0xFF;
length = 1; length = 1;
operandType = OperandType.BYTE;
} }
@Override @Override
@ -419,14 +430,21 @@ class Instruction
{ {
this.value = value & 0xFF; this.value = value & 0xFF;
length = 1; length = 1;
if (value == 0)
operandType = OperandType.VAR_STACK;
else if (value <= 15)
operandType = OperandType.VAR_LOCAL;
else
operandType = OperandType.VAR_GLOBAL;
} }
@Override @Override
public String toString () public String toString ()
{ {
if (value == 0) if (operandType == OperandType.VAR_STACK)
return ("ToS"); return ("ToS");
if (value <= 15) if (operandType == OperandType.VAR_LOCAL)
return (String.format ("L%02d", value)); return (String.format ("L%02d", value));
return String.format ("G%03d", (value - 15)); return String.format ("G%03d", (value - 15));
} }
@ -446,6 +464,7 @@ class Instruction
else else
target = val + offset - 1; target = val + offset - 1;
length = 1; length = 1;
operandType = OperandType.ARG_BRANCH;
} }
public ArgumentBranch (int value, int offset) public ArgumentBranch (int value, int offset)
@ -456,6 +475,7 @@ class Instruction
val -= 16384; val -= 16384;
target = val + offset; target = val + offset;
length = 2; length = 2;
operandType = OperandType.ARG_BRANCH;
} }
@Override @Override
@ -480,8 +500,9 @@ class Instruction
public ArgumentString (byte[] buffer, int offset) public ArgumentString (byte[] buffer, int offset)
{ {
this.buffer = buffer; this.buffer = buffer;
text = new ZString (buffer, offset, header); text = new ZString (header, offset);
length = text.length; length = text.length;
operandType = OperandType.ARG_STRING;
} }
@Override @Override

View File

@ -33,13 +33,13 @@ class ObjectAnalyser
public void setStringPointer () public void setStringPointer ()
{ {
PropertyTester pt = new PropertyTester (parent.list); PropertyTester pt = new PropertyTester (parent.getObjects ());
pt.addTest (new LengthTwoCondition ()); pt.addTest (new LengthTwoCondition ());
HighMemoryCondition hmc = new HighMemoryCondition (); HighMemoryCondition hmc = new HighMemoryCondition ();
pt.addTest (hmc); pt.addTest (hmc);
pt.doTests (); pt.doTests ();
System.out.println ("\nSetting the string pointer\n"); // System.out.println ("\nSetting the string pointer\n");
for (Integer propertyNo : pt) for (Integer propertyNo : pt)
// list of all properties that passed all tests // list of all properties that passed all tests
@ -70,7 +70,7 @@ class ObjectAnalyser
for (Statistics s : list) for (Statistics s : list)
{ {
if (header.propertyNames[s.propertyNumber].charAt (0) >= 'a') if (header.getPropertyName (s.propertyNumber).charAt (0) >= 'a')
continue; continue;
if (s.lo >= header.stringPointer) if (s.lo >= header.stringPointer)
{ {
@ -90,11 +90,12 @@ class ObjectAnalyser
public void checkThreeByteProperties () public void checkThreeByteProperties ()
{ {
for (ZObject object : parent.list) for (ZObject object : parent.getObjects ())
{ {
for (Property property : object.properties) for (Property property : object.properties)
{ {
if (header.propertyNames[property.propertyNumber].charAt (0) < 'a' && property.length == 3) if (header.getPropertyName (property.propertyNumber).charAt (0) < 'a'
&& property.length == 3)
{ {
int address = header.getWord (property.ptr + 1) * 2; int address = header.getWord (property.ptr + 1) * 2;
System.out.println ("checking " + address); System.out.println ("checking " + address);
@ -107,14 +108,14 @@ class ObjectAnalyser
// find the property with only dictionary entries // find the property with only dictionary entries
public void setDictionary () public void setDictionary ()
{ {
PropertyTester pt = new PropertyTester (parent.list); PropertyTester pt = new PropertyTester (parent.getObjects ());
pt.addTest (new LengthEvenCondition ()); pt.addTest (new LengthEvenCondition ());
pt.addTest (new ValidDictionaryCondition ()); pt.addTest (new ValidDictionaryCondition ());
pt.doTests (); pt.doTests ();
for (Integer i : pt) for (Integer i : pt)
// should only be one // should only be one
header.propertyNames[i] = "DICT"; header.propertyNames[i] = "DICT"; // SYNONYM
} }
class Statistics implements Comparable<Statistics> class Statistics implements Comparable<Statistics>
@ -141,7 +142,8 @@ class ObjectAnalyser
@Override @Override
public String toString () public String toString ()
{ {
return String.format ("%2d %3d %,7d %,7d", propertyNumber, offsets.size (), lo, hi); return String.format ("%2d %3d %,7d %,7d", propertyNumber, offsets.size (),
lo, hi);
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package com.bytezone.diskbrowser.infocom; package com.bytezone.diskbrowser.infocom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -11,21 +12,22 @@ import com.bytezone.diskbrowser.disk.FormattedDisk;
class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject> class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
{ {
Header header; private final Header header;
List<ZObject> list; private final List<ZObject> list;
int defaultsPtr, defaultsSize; private List<ZObject> sortedList;
int tablePtr, tableSize; private final int defaultsPtr, defaultsSize;
int propertyPtr, propertySize; private final int tablePtr, tableSize;
ObjectAnalyser analyser; private final int propertyPtr, propertySize;
private final ObjectAnalyser analyser;
public ObjectManager (Header header) public ObjectManager (Header header)
{ {
super ("Objects", header.buffer); super ("Objects", header.buffer);
this.header = header; this.header = header;
defaultsPtr = header.objectTable; defaultsPtr = header.objectTableOffset;
defaultsSize = 62; defaultsSize = 62;
tablePtr = header.objectTable + 62; tablePtr = header.objectTableOffset + 62;
propertyPtr = header.getWord (tablePtr + 7); propertyPtr = header.getWord (tablePtr + 7);
propertySize = header.globalsOffset - propertyPtr; propertySize = header.globalsOffset - propertyPtr;
tableSize = (propertyPtr - tablePtr); tableSize = (propertyPtr - tablePtr);
@ -45,6 +47,16 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
hexBlocks.add (new HexBlock (propertyPtr, propertySize, "Properties:")); hexBlocks.add (new HexBlock (propertyPtr, propertySize, "Properties:"));
} }
List<ZObject> getObjects ()
{
return list;
}
ZObject getObject (int index)
{
return list.get (index);
}
public void addNodes (DefaultMutableTreeNode root, FormattedDisk disk) public void addNodes (DefaultMutableTreeNode root, FormattedDisk disk)
{ {
root.setAllowsChildren (true); root.setAllowsChildren (true);
@ -61,10 +73,9 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
new DefaultAppleFileSource (object.getName (), object, disk)); new DefaultAppleFileSource (object.getName (), object, disk));
parentNode.add (child); parentNode.add (child);
if (object.sibling > 0) if (object.sibling > 0)
buildObjectTree (header.objectManager.list.get (object.sibling - 1), parentNode, buildObjectTree (list.get (object.sibling - 1), parentNode, disk);
disk);
if (object.child > 0) if (object.child > 0)
buildObjectTree (header.objectManager.list.get (object.child - 1), child, disk); buildObjectTree (list.get (object.child - 1), child, disk);
else else
child.setAllowsChildren (false); child.setAllowsChildren (false);
} }
@ -77,8 +88,8 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
@Override @Override
public String getText () public String getText ()
{ {
String header1 = "ID Attributes Pr Sb Ch Prop Title\n-- -----------" // String header1 = "ID Attributes Pr Sb Ch Prop Title\n-- -----------"
+ " -- -- -- ----- -----------------------------\n"; // + " -- -- -- ----- -----------------------------\n";
String underline = " ----------------------------------------"; String underline = " ----------------------------------------";
String titles[] = String titles[] =
{ "ID ", "Title ", { "ID ", "Title ",
@ -89,13 +100,21 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
+ "-- " + underline + underline + underline + underline + " ----------- -----\n"; + "-- " + underline + underline + underline + underline + " ----------- -----\n";
StringBuilder text = new StringBuilder (header2); StringBuilder text = new StringBuilder (header2);
int objectNumber = 0; if (sortedList == null)
sortedList = new ArrayList<> (list);
Collections.sort (sortedList);
// int objectNumber = 0;
for (ZObject zo : list) for (ZObject zo : list)
if (false) // if (false)
text.append (String.format ("%02X %s%n", ++objectNumber, zo)); // text.append (String.format ("%02X %s%n", ++objectNumber, zo));
else // else
text.append ( text.append (String.format ("%02X %s%n", zo.getId (), zo.getDescription (list)));
String.format ("%02X %s%n", ++objectNumber, zo.getDescription (list)));
text.append ("\n\n");
text.append (header2);
for (ZObject zo : sortedList)
text.append (String.format ("%02X %s%n", zo.getId (), zo.getDescription (list)));
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);
return text.toString (); return text.toString ();

View File

@ -33,7 +33,7 @@ class PropertyManager extends AbstractFile
for (Statistic stat : list) for (Statistic stat : list)
if (stat.list.size () > 0) if (stat.list.size () > 0)
{ {
String title = "Property " + header.propertyNames[stat.id].trim (); String title = "Property " + header.getPropertyName (stat.id).trim ();
DefaultMutableTreeNode child = new DefaultMutableTreeNode ( DefaultMutableTreeNode child = new DefaultMutableTreeNode (
new DefaultAppleFileSource (title, stat.getText (), disk)); new DefaultAppleFileSource (title, stat.getText (), disk));
node.add (child); node.add (child);
@ -72,13 +72,13 @@ class PropertyManager extends AbstractFile
String getText () String getText ()
{ {
StringBuilder text = StringBuilder text = new StringBuilder (String
new StringBuilder ("Objects with property " + id + " set:\n\n"); .format ("Objects with property %d %s set:%n%n", id, header.propertyNames[id]));
for (ZObject o : list) for (ZObject o : list)
{ {
ZObject.Property p = o.getProperty (id); ZObject.Property p = o.getProperty (id);
text.append (String.format ("%02X %-29s%s%n", o.id, o.getName (), text.append (String.format ("%02X %-29s%s%n", o.getId (), o.getName (),
p.toString ().substring (7))); p.toString ().substring (11)));
} }
if (text.length () > 0) if (text.length () > 0)
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);
@ -88,7 +88,7 @@ class PropertyManager extends AbstractFile
@Override @Override
public String toString () public String toString ()
{ {
return String.format (" %2d %-6s %3d", id, header.propertyNames[id], return String.format (" %2d %-6s %3d", id, header.getPropertyName (id),
list.size ()); list.size ());
} }
} }

View File

@ -4,13 +4,15 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import com.bytezone.diskbrowser.infocom.Instruction.Operand;
import com.bytezone.diskbrowser.infocom.Instruction.OperandType;
class Routine extends InfocomAbstractFile class Routine extends InfocomAbstractFile
implements Iterable<Instruction>, Comparable<Routine> implements Iterable<Instruction>, Comparable<Routine>
{ {
private static final String padding = " "; private static final String padding = " ";
int startPtr, length, strings, locals; int startPtr, length, strings, locals;
// private final Header header;
List<Parameter> parameters = new ArrayList<Parameter> (); List<Parameter> parameters = new ArrayList<Parameter> ();
List<Instruction> instructions = new ArrayList<Instruction> (); List<Instruction> instructions = new ArrayList<Instruction> ();
@ -21,11 +23,13 @@ class Routine extends InfocomAbstractFile
public Routine (int ptr, Header header, int caller) public Routine (int ptr, Header header, int caller)
{ {
super (String.format ("Routine %05X", ptr), header.buffer); super (String.format ("Routine %05X", ptr), header.buffer);
// this.header = header;
locals = buffer[ptr] & 0xFF; locals = buffer[ptr] & 0xFF;
if (locals > 15) if (locals > 15)
{
System.out.println ("Too many locals: " + locals);
return; return;
}
startPtr = ptr++; // also used to flag a valid routine startPtr = ptr++; // also used to flag a valid routine
calledBy.add (caller); calledBy.add (caller);
@ -53,6 +57,10 @@ class Routine extends InfocomAbstractFile
if (instruction.isPrint ()) if (instruction.isPrint ())
strings++; strings++;
for (Operand operand : instruction.opcode.operands)
if (operand.operandType == OperandType.VAR_GLOBAL)
header.globals.addRoutine (this, operand);
ptr += instruction.length (); ptr += instruction.length ();
// is it a backwards jump? // is it a backwards jump?

View File

@ -20,7 +20,7 @@ class StringManager extends AbstractFile
int max = header.fileLength; int max = header.fileLength;
while (ptr < max) while (ptr < max)
{ {
ZString zs = new ZString (buffer, ptr, header); ZString zs = new ZString (header, ptr);
if (zs.value == null) if (zs.value == null)
break; // used when eof not known or correct - fix!! break; // used when eof not known or correct - fix!!
strings.put (ptr, zs); strings.put (ptr, zs);

View File

@ -7,26 +7,28 @@ import java.util.List;
import com.bytezone.diskbrowser.applefile.AbstractFile; import com.bytezone.diskbrowser.applefile.AbstractFile;
import com.bytezone.diskbrowser.utilities.HexFormatter; import com.bytezone.diskbrowser.utilities.HexFormatter;
class ZObject extends AbstractFile class ZObject extends AbstractFile implements Comparable<ZObject>
{ {
static final int HEADER_SIZE = 9; static final int HEADER_SIZE = 9;
int id; private final Header header;
int startPtr; private final int id;
int propertyTablePtr; private final int startPtr;
int propertyTableLength;
int parent, sibling, child;
List<Property> properties = new ArrayList<Property> ();
Header header;
BitSet attributes = new BitSet (32);
public ZObject (String name, byte[] buffer, int offset, int seq, Header header) private final int propertyTablePtr;
private final int propertyTableLength;
final int parent, sibling, child;
final List<Property> properties = new ArrayList<Property> ();
final BitSet attributes = new BitSet (32);
public ZObject (String name, byte[] buffer, int offset, int id, Header header)
{ {
super (name, buffer); super (name, buffer);
this.header = header; this.header = header;
this.startPtr = offset; this.startPtr = offset;
id = seq; this.id = id;
// attributes // attributes
int bitIndex = 0; int bitIndex = 0;
@ -51,19 +53,24 @@ class ZObject extends AbstractFile
propertyTablePtr = header.getWord (offset + 7); propertyTablePtr = header.getWord (offset + 7);
int ptr = propertyTablePtr; int ptr = propertyTablePtr;
int nameLength = header.getByte (ptr) * 2; int nameLength = header.getByte (ptr) * 2;
this.name = nameLength == 0 ? "<<none>>" : new ZString (buffer, ++ptr, header).value; this.name = nameLength == 0 ? "<<" + id + ">>" : new ZString (header, ++ptr).value;
ptr += nameLength; ptr += nameLength;
// read each property // read each property
while (buffer[ptr] != 0) while (buffer[ptr] != 0)
{ {
Property p = new Property (buffer, ptr); Property p = new Property (ptr);
properties.add (p); properties.add (p);
ptr += p.length + 1; ptr += p.length + 1;
} }
propertyTableLength = ptr - propertyTablePtr; propertyTableLength = ptr - propertyTablePtr;
} }
int getId ()
{
return id;
}
@Override @Override
public String getText () public String getText ()
{ {
@ -71,9 +78,9 @@ class ZObject extends AbstractFile
text.append (String.format ("ID : %02X (%<3d) %s%n%n", id, name)); text.append (String.format ("ID : %02X (%<3d) %s%n%n", id, name));
String obj1 = parent == 0 ? "" : header.objectManager.list.get (parent - 1).name; String obj1 = parent == 0 ? "" : header.getObject (parent - 1).name;
String obj2 = sibling == 0 ? "" : header.objectManager.list.get (sibling - 1).name; String obj2 = sibling == 0 ? "" : header.getObject (sibling - 1).name;
String obj3 = child == 0 ? "" : header.objectManager.list.get (child - 1).name; String obj3 = child == 0 ? "" : header.getObject (child - 1).name;
text.append (String.format ("Parent : %02X (%<3d) %s%n", parent, obj1)); text.append (String.format ("Parent : %02X (%<3d) %s%n", parent, obj1));
text.append (String.format ("Sibling : %02X (%<3d) %s%n", sibling, obj2)); text.append (String.format ("Sibling : %02X (%<3d) %s%n", sibling, obj2));
@ -140,7 +147,7 @@ class ZObject extends AbstractFile
int length; int length;
int offset; // only used if length == 2 int offset; // only used if length == 2
public Property (byte[] buffer, int ptr) public Property (int ptr)
{ {
this.ptr = ptr; this.ptr = ptr;
length = header.getByte (ptr) / 32 + 1; length = header.getByte (ptr) / 32 + 1;
@ -152,41 +159,53 @@ class ZObject extends AbstractFile
private ZObject getObject () private ZObject getObject ()
{ {
return header.objectManager.list.get ((buffer[ptr + 1] & 0xFF) - 1); return header.getObject ((buffer[ptr + 1] & 0xFF) - 1);
}
private ZObject getObject (int id)
{
return header.getObject (id - 1);
} }
@Override @Override
public String toString () public String toString ()
{ {
StringBuilder text = new StringBuilder ( StringBuilder text = new StringBuilder (
String.format ("%5s : ", header.propertyNames[propertyNumber])); String.format ("%8s : ", header.getPropertyName (propertyNumber)));
String propertyType = header.propertyNames[propertyNumber]; String propertyType = header.getPropertyName (propertyNumber);
if (!propertyType.equals ("DICT") && !propertyType.contains ("STR")) if (!(propertyType.equals ("DICT") || propertyType.startsWith ("STR")))
text.append ( text.append (
String.format ("%-20s", HexFormatter.getHexString (buffer, ptr + 1, length))); String.format ("%-20s", HexFormatter.getHexString (buffer, ptr + 1, length)));
if (propertyType.charAt (0) >= 'a') // directions are in lowercase if (propertyNumber >= 19) // directions
{ {
switch (length) switch (length)
{ {
case 1: case 1: // UEXIT - unconditional exit
text.append (getObject ().name); text.append (getObject ().name);
break; break;
case 2: case 2:
text.append ("\"" + header.stringManager.stringAt (offset) + "\""); text.append ("\"" + header.stringManager.stringAt (offset) + "\"");
break; break;
case 3: // executable routine case 3: // FEXIT - function exit
int address = header.getWord (ptr + 1) * 2; int address = header.getWord (ptr + 1) * 2;
text.append (String.format ("R:%05X", address)); text.append (String.format ("R:%05X", address));
appendRoutine (text, address); appendRoutine (text, address);
break; break;
case 4: case 4:
// text.append (getObject ().name + " : ");
text.append (String.format ("%s : IF G%02X ELSE ", getObject ().name,
header.getByte (ptr + 2)));
address = header.getWord (ptr + 3) * 2; address = header.getWord (ptr + 3) * 2;
if (address > 0) if (address > 0)
text.append ("\"" + header.stringManager.stringAt (address) + "\""); text.append ("\"" + header.stringManager.stringAt (address) + "\"");
break; break;
case 5:
text.append (String.format ("%s : IF G%02X ", getObject ().name,
header.getByte (ptr + 2)));
break;
default: default:
break; break;
} }
@ -196,7 +215,7 @@ class ZObject extends AbstractFile
for (int i = 1; i <= length; i += 2) for (int i = 1; i <= length; i += 2)
{ {
int address = header.getWord (ptr + i); int address = header.getWord (ptr + i);
text.append (header.wordAt (address) + ", "); text.append (String.format ("%02X: %s, ", address, header.wordAt (address)));
} }
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);
text.deleteCharAt (text.length () - 1); text.deleteCharAt (text.length () - 1);
@ -211,6 +230,33 @@ class ZObject extends AbstractFile
text.append (String.format ("(%4X) \"%s\"", offset, text.append (String.format ("(%4X) \"%s\"", offset,
header.stringManager.stringAt (offset))); header.stringManager.stringAt (offset)));
} }
else if (propertyType.equals ("ADJ"))
{
}
else if (propertyType.equals ("SIZE"))
{
}
else if (propertyType.equals ("VALUE"))
{
}
else if (propertyType.equals ("TVALU"))
{
}
else if (propertyType.equals ("GLBL"))
{
for (int i = 0; i < length; i++)
{
int objectId = header.getByte (ptr + i + 1);
text.append (
String.format ("%s%s", (i == 0 ? "" : ", "), getObject (objectId).name));
}
}
// else
// text.append ("Unknown property type: " + propertyType);
return text.toString (); return text.toString ();
} }
@ -224,4 +270,10 @@ class ZObject extends AbstractFile
text.append ("\n\n****** null routine\n"); text.append ("\n\n****** null routine\n");
} }
} }
@Override
public int compareTo (ZObject o)
{
return this.name.compareTo (o.name);
}
} }

View File

@ -10,7 +10,7 @@ class ZString
int startPtr; int startPtr;
int length; int length;
public ZString (byte[] buffer, int offset, Header header) public ZString (Header header, int offset)
{ {
ZStringBuilder text = new ZStringBuilder (); ZStringBuilder text = new ZStringBuilder ();
this.header = header; this.header = header;
@ -28,11 +28,11 @@ class ZString
int val = header.getWord (offset); int val = header.getWord (offset);
// process each zChar as a 5-bit value // process each zChar as a 5-bit value
text.processZChar ((byte) ((val >> 10) & 0x1F)); text.processZChar ((byte) ((val >>> 10) & 0x1F));
text.processZChar ((byte) ((val >> 5) & 0x1F)); text.processZChar ((byte) ((val >>> 5) & 0x1F));
text.processZChar ((byte) (val & 0x1F)); text.processZChar ((byte) (val & 0x1F));
if ((val & 0x8000) > 0) // bit 15 = finished flag if ((val & 0x8000) != 0) // bit 15 = finished flag
{ {
length = offset - startPtr + 2; length = offset - startPtr + 2;
value = text.toString (); value = text.toString ();
@ -75,8 +75,8 @@ class ZString
} }
// A flag to indicate that we need to insert an abbreviation. The synonym value // A flag to indicate that we need to insert an abbreviation. The synonym value
// (1-3) indicates which abbreviation block to use, and the current zchar is the offset // (1-3) indicates which abbreviation block to use, and the current zchar is the
// within that block. // offset within that block.
if (synonym > 0) if (synonym > 0)
{ {
text.append (header.getAbbreviation ((synonym - 1) * 32 + zchar)); text.append (header.getAbbreviation ((synonym - 1) * 32 + zchar));
@ -84,7 +84,6 @@ class ZString
return; return;
} }
// this should be in the switch
if ((shift && shiftAlphabet == 2) || (!shift && alphabet == 2)) if ((shift && shiftAlphabet == 2) || (!shift && alphabet == 2))
{ {
if (zchar == 6) if (zchar == 6)
@ -110,9 +109,11 @@ class ZString
text.append (" "); text.append (" ");
shift = false; shift = false;
return; return;
case 1: case 1:
synonym = zchar; synonym = zchar;
return; return;
case 2: case 2:
case 3: case 3:
if (header.version >= 3) if (header.version >= 3)
@ -124,6 +125,7 @@ class ZString
shiftAlphabet = (alphabet + zchar - 1) % 3; shiftAlphabet = (alphabet + zchar - 1) % 3;
shift = true; shift = true;
return; return;
case 4: case 4:
case 5: case 5:
if (header.version >= 3) // shift key if (header.version >= 3) // shift key
@ -131,10 +133,10 @@ class ZString
shiftAlphabet = zchar - 3; shiftAlphabet = zchar - 3;
shift = true; shift = true;
} }
else else // shift lock key
// shift lock key
alphabet = (alphabet + zchar - 3) % 3; alphabet = (alphabet + zchar - 3) % 3;
return; return;
default: default:
if (shift) if (shift)
{ {