mirror of
https://github.com/dmolony/DiskBrowser.git
synced 2025-02-20 04:29:02 +00:00
infocom changes
This commit is contained in:
parent
8764dd0db8
commit
76125f9a4e
@ -20,7 +20,7 @@ class Abbreviations extends InfocomAbstractFile
|
||||
dataPtr = header.getWord (header.abbreviationsTable) * 2;
|
||||
dataSize = header.abbreviationsTable - dataPtr;
|
||||
tablePtr = header.abbreviationsTable;
|
||||
tableSize = header.objectTable - header.abbreviationsTable;
|
||||
tableSize = header.objectTableOffset - header.abbreviationsTable;
|
||||
|
||||
// prepare hex dump
|
||||
hexBlocks.add (new HexBlock (dataPtr, dataSize, "Abbreviations data:"));
|
||||
@ -29,21 +29,17 @@ class Abbreviations extends InfocomAbstractFile
|
||||
|
||||
private void populate ()
|
||||
{
|
||||
System.out.println ("populating abbreviations");
|
||||
list = new ArrayList<ZString> ();
|
||||
|
||||
for (int i = header.abbreviationsTable; i < header.objectTable; i += 2)
|
||||
{
|
||||
int j = header.getWord (i) * 2;
|
||||
ZString zs = new ZString (buffer, j, header);
|
||||
list.add (zs);
|
||||
}
|
||||
for (int i = header.abbreviationsTable; i < header.objectTableOffset; i += 2)
|
||||
list.add (new ZString (header, header.getWord (i) * 2));
|
||||
}
|
||||
|
||||
public String getAbbreviation (int abbreviationNumber)
|
||||
{
|
||||
if (list == null)
|
||||
populate ();
|
||||
|
||||
return list.get (abbreviationNumber).value;
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ class AttributeManager extends AbstractFile
|
||||
new StringBuilder ("Objects with attribute " + id + " set:\n\n");
|
||||
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)
|
||||
text.deleteCharAt (text.length () - 1);
|
||||
|
@ -36,7 +36,7 @@ class CodeManager extends AbstractFile
|
||||
for (Routine routine : routines.values ())
|
||||
{
|
||||
String name = String.format ("%3d %s (%04X)", ++count, routine.getName (),
|
||||
routine.startPtr / 2);
|
||||
routine.startPtr / 2);
|
||||
DefaultAppleFileSource dafs = new DefaultAppleFileSource (name, routine, disk);
|
||||
dafs.setSectors (getSectors (routine, disk.getDisk ()));
|
||||
|
||||
@ -114,10 +114,10 @@ class CodeManager extends AbstractFile
|
||||
for (Routine r : routines.values ())
|
||||
{
|
||||
int gap = r.startPtr - nextAddress;
|
||||
text.append (String
|
||||
.format ("%3d %05X %5d %3d %2d %3d %3d %4d %04X%n",
|
||||
++count, r.startPtr, r.length, r.instructions.size (), r.strings,
|
||||
r.calledBy.size (), r.calls.size (), gap, r.startPtr / 2));
|
||||
text.append (String.format (
|
||||
"%3d %05X %5d %3d %2d %3d %3d %4d %04X%n", ++count,
|
||||
r.startPtr, r.length, r.instructions.size (), r.strings, r.calledBy.size (),
|
||||
r.calls.size (), gap, r.startPtr / 2));
|
||||
|
||||
nextAddress = r.startPtr + r.length;
|
||||
}
|
||||
@ -148,7 +148,7 @@ class CodeManager extends AbstractFile
|
||||
|
||||
Routine addRoutine (int address, int caller)
|
||||
{
|
||||
if (address == 0) // stack-based call
|
||||
if (address == 0) // stack-based call
|
||||
return null;
|
||||
if (address > header.fileLength)
|
||||
return null;
|
||||
@ -163,7 +163,7 @@ class CodeManager extends AbstractFile
|
||||
|
||||
// try to create a new Routine
|
||||
Routine r = new Routine (address, header, caller);
|
||||
if (r.length == 0) // invalid routine
|
||||
if (r.length == 0) // invalid routine
|
||||
return null;
|
||||
|
||||
// recursively add all routines called by this one
|
||||
|
@ -15,15 +15,12 @@ class Dictionary extends AbstractFile
|
||||
private final int totalSeparators;
|
||||
private final int dictionaryPtr, dictionarySize;
|
||||
private final int entryLength;
|
||||
// private final Header header;
|
||||
|
||||
// this could be a Google Multimap
|
||||
Map<Integer, List<WordEntry>> synonymList = new TreeMap<Integer, List<WordEntry>> ();
|
||||
|
||||
public Dictionary (Header header)
|
||||
{
|
||||
super ("Dictionary", header.buffer);
|
||||
// this.header = header;
|
||||
|
||||
dictionaryPtr = header.dictionaryOffset;
|
||||
dictionary = new TreeMap<Integer, ZString> ();
|
||||
@ -38,7 +35,7 @@ class Dictionary extends AbstractFile
|
||||
int count = 0;
|
||||
for (int i = 0; i < totalEntries; i++)
|
||||
{
|
||||
ZString string = new ZString (buffer, ptr, header);
|
||||
ZString string = new ZString (header, ptr);
|
||||
dictionary.put (ptr, string);
|
||||
WordEntry wordEntry = new WordEntry (string, count++);
|
||||
|
||||
@ -56,8 +53,8 @@ class Dictionary extends AbstractFile
|
||||
{
|
||||
int b1 = buffer[ptr + 5] & 0xFF;
|
||||
int property = (b1 >= 1 && b1 <= 31) ? b1 : buffer[ptr + 6] & 0xFF;
|
||||
if (header.propertyNames[property] == null
|
||||
|| header.propertyNames[property].length () > string.value.length ())
|
||||
if (header.getPropertyName (property) == null
|
||||
|| header.getPropertyName (property).length () > string.value.length ())
|
||||
header.propertyNames[property] = string.value;
|
||||
}
|
||||
ptr += entryLength;
|
||||
@ -68,6 +65,48 @@ class Dictionary extends AbstractFile
|
||||
for (int i = 1; i < header.propertyNames.length; i++)
|
||||
if (header.propertyNames[i] == null)
|
||||
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)
|
||||
@ -168,7 +207,7 @@ class Dictionary extends AbstractFile
|
||||
text.append ("\n");
|
||||
}
|
||||
|
||||
if (wordEntry.value == 0x80) // nouns are all in one entry
|
||||
if (wordEntry.value == 0x80) // nouns are all in one entry
|
||||
{
|
||||
for (WordEntry we : list)
|
||||
text.append (we + "\n");
|
||||
|
@ -1,13 +1,19 @@
|
||||
package com.bytezone.diskbrowser.infocom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.bytezone.diskbrowser.infocom.Instruction.Operand;
|
||||
|
||||
class Globals extends InfocomAbstractFile
|
||||
{
|
||||
static final int TOTAL_GLOBALS = 240;
|
||||
Header header;
|
||||
int globalsPtr, globalsSize;
|
||||
int arrayPtr, arraySize;
|
||||
private static final int TOTAL_GLOBALS = 240;
|
||||
private final Header header;
|
||||
private final int globalsPtr, globalsSize;
|
||||
private final int arrayPtr, arraySize;
|
||||
private final List<List<Routine>> globalRoutines;
|
||||
|
||||
public Globals (Header header)
|
||||
Globals (Header header)
|
||||
{
|
||||
super ("Globals", header.buffer);
|
||||
this.header = header;
|
||||
@ -20,21 +26,39 @@ class Globals extends InfocomAbstractFile
|
||||
// add entries for AbstractFile.getHexDump ()
|
||||
hexBlocks.add (new HexBlock (globalsPtr, globalsSize, "Globals:"));
|
||||
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
|
||||
public String getText ()
|
||||
{
|
||||
StringBuilder text = new StringBuilder ();
|
||||
StringBuilder text = new StringBuilder ("GLB Value Routines\n");
|
||||
for (int i = 1; i <= TOTAL_GLOBALS; i++)
|
||||
{
|
||||
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;
|
||||
if (address >= header.stringPointer && address < header.fileLength)
|
||||
text.append (header.stringManager.stringAt (address) + "\n");
|
||||
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);
|
||||
return text.toString ();
|
||||
|
@ -7,23 +7,24 @@ import com.bytezone.diskbrowser.utilities.HexFormatter;
|
||||
class Grammar extends InfocomAbstractFile
|
||||
{
|
||||
private static final int SENTENCE_LENGTH = 8;
|
||||
Header header;
|
||||
int indexPtr, indexSize;
|
||||
int tablePtr, tableSize;
|
||||
int actionPtr, actionSize;
|
||||
int preActionPtr, preActionSize;
|
||||
int prepositionPtr, prepositionSize;
|
||||
int indexEntries;
|
||||
int totalPrepositions;
|
||||
int padding;
|
||||
private final Header header;
|
||||
private final int indexPtr, indexSize;
|
||||
private final int tablePtr, tableSize;
|
||||
private final int actionPtr, actionSize;
|
||||
private final int preActionPtr, preActionSize;
|
||||
private final int prepositionPtr, prepositionSize;
|
||||
private final int indexEntries;
|
||||
private final int totalPrepositions;
|
||||
private final int padding;
|
||||
|
||||
List<SentenceGroup> sentenceGroups = new ArrayList<SentenceGroup> ();
|
||||
Map<Integer, List<Sentence>> actionList = new TreeMap<Integer, List<Sentence>> ();
|
||||
private final List<SentenceGroup> sentenceGroups = new ArrayList<SentenceGroup> ();
|
||||
private final Map<Integer, List<Sentence>> actionList =
|
||||
new TreeMap<Integer, List<Sentence>> ();
|
||||
|
||||
List<Integer> actionRoutines = new ArrayList<Integer> ();
|
||||
List<Integer> preActionRoutines = new ArrayList<Integer> ();
|
||||
private final List<Integer> actionRoutines = 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);
|
||||
this.header = header;
|
||||
@ -173,8 +174,8 @@ class Grammar extends InfocomAbstractFile
|
||||
text.append (line);
|
||||
}
|
||||
|
||||
text.append ("\n" + actionRoutines.size ()
|
||||
+ " Action routines\n===================\n\n");
|
||||
text.append (
|
||||
"\n" + actionRoutines.size () + " Action routines\n===================\n\n");
|
||||
|
||||
// add sentences in action routine sequence
|
||||
for (Integer routine : actionRoutines)
|
||||
|
@ -9,12 +9,11 @@ class Header extends InfocomAbstractFile
|
||||
final String[] propertyNames = new String[32];
|
||||
|
||||
private final File file;
|
||||
// private final Disk disk;
|
||||
int version;
|
||||
int highMemory;
|
||||
int programCounter;
|
||||
int dictionaryOffset;
|
||||
int objectTable;
|
||||
int objectTableOffset;
|
||||
int globalsOffset;
|
||||
int staticMemory;
|
||||
int abbreviationsTable;
|
||||
@ -23,29 +22,37 @@ class Header extends InfocomAbstractFile
|
||||
int stringPointer;
|
||||
|
||||
final Abbreviations abbreviations;
|
||||
final Dictionary dictionary;
|
||||
final ObjectManager objectManager;
|
||||
final StringManager stringManager;
|
||||
final CodeManager codeManager;
|
||||
final Globals globals;
|
||||
final Grammar grammar;
|
||||
final Dictionary dictionary;
|
||||
final CodeManager codeManager;
|
||||
final StringManager stringManager;
|
||||
|
||||
public Header (String name, byte[] buffer, Disk disk)
|
||||
{
|
||||
super (name, buffer);
|
||||
// this.disk = disk;
|
||||
this.file = disk.getFile ();
|
||||
|
||||
version = getByte (0);
|
||||
highMemory = getWord (4);
|
||||
programCounter = getWord (6);
|
||||
dictionaryOffset = getWord (8);
|
||||
objectTable = getWord (10);
|
||||
globalsOffset = getWord (12);
|
||||
staticMemory = getWord (14);
|
||||
abbreviationsTable = getWord (24);
|
||||
checksum = getWord (28);
|
||||
fileLength = getWord (26) * 2;
|
||||
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);
|
||||
|
||||
if (fileLength == 0)
|
||||
fileLength = buffer.length;
|
||||
@ -75,6 +82,11 @@ class Header extends InfocomAbstractFile
|
||||
hexBlocks.add (new HexBlock (0, 64, "Header data:"));
|
||||
}
|
||||
|
||||
String getPropertyName (int id)
|
||||
{
|
||||
return propertyNames[id];
|
||||
}
|
||||
|
||||
public String getAbbreviation (int index)
|
||||
{
|
||||
return abbreviations.getAbbreviation (index);
|
||||
@ -100,8 +112,8 @@ class Header extends InfocomAbstractFile
|
||||
text.append ("\nDynamic memory:\n");
|
||||
text.append (String.format (" Abbreviation table %04X %,6d%n",
|
||||
abbreviationsTable, abbreviationsTable));
|
||||
text.append (String.format (" Objects table %04X %,6d%n", objectTable,
|
||||
objectTable));
|
||||
text.append (String.format (" Objects table %04X %,6d%n",
|
||||
objectTableOffset, objectTableOffset));
|
||||
text.append (String.format (" Global variables %04X %,6d%n", globalsOffset,
|
||||
globalsOffset));
|
||||
|
||||
@ -125,11 +137,16 @@ class Header extends InfocomAbstractFile
|
||||
text.append (String.format ("Total strings %d%n",
|
||||
stringManager.strings.size ()));
|
||||
text.append (String.format ("Total objects %d%n",
|
||||
objectManager.list.size ()));
|
||||
objectManager.getObjects ().size ()));
|
||||
|
||||
return text.toString ();
|
||||
}
|
||||
|
||||
ZObject getObject (int index)
|
||||
{
|
||||
return objectManager.getObject (index);
|
||||
}
|
||||
|
||||
int getByte (int offset)
|
||||
{
|
||||
return buffer[offset] & 0xFF;
|
||||
|
@ -86,16 +86,16 @@ public class InfocomDisk extends AbstractFormattedDisk
|
||||
stringsNode = addToTree (root, "Strings", header.stringManager, TYPE_LEAF);
|
||||
|
||||
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);
|
||||
am.addNodes (addToTree (objectNode, "Attributes", am, TYPE_NODE), this);
|
||||
am.addNodes (addToTree (root, "Attributes", am, TYPE_NODE), this);
|
||||
|
||||
sectorTypes[48] = headerSector;
|
||||
|
||||
setSectorTypes (header.abbreviationsTable, header.objectTable, abbreviationsSector,
|
||||
setSectorTypes (header.abbreviationsTable, header.objectTableOffset, abbreviationsSector,
|
||||
abbreviationsNode);
|
||||
setSectorTypes (header.objectTable, header.globalsOffset, objectsSector, objectNode);
|
||||
setSectorTypes (header.objectTableOffset, header.globalsOffset, objectsSector, objectNode);
|
||||
setSectorTypes (header.globalsOffset, header.staticMemory, globalsSector,
|
||||
globalsNode);
|
||||
setSectorTypes (header.staticMemory, header.dictionaryOffset, grammarSector,
|
||||
|
@ -7,487 +7,508 @@ import com.bytezone.diskbrowser.utilities.HexFormatter;
|
||||
|
||||
class Instruction
|
||||
{
|
||||
Opcode opcode;
|
||||
int startPtr;
|
||||
byte[] buffer;
|
||||
// List<ZString> abbreviations;
|
||||
Header header;
|
||||
Opcode opcode;
|
||||
int startPtr;
|
||||
byte[] buffer;
|
||||
// List<ZString> abbreviations;
|
||||
Header header;
|
||||
|
||||
static final String[] name2OP =
|
||||
{ "*bad*", "je", "jl", "jg", "dec_chk", "inc_chk", "jin", "test", "or", "and", "test_attr",
|
||||
"set_attr", "clear_attr", "store", "insert_obj", "loadw", "loadb", "get_prop",
|
||||
"get_prop_addr", "get_next_prop", "add", "sub", "mul", "div", "mod", "call_2s",
|
||||
"call_2n", "set_colour", "throw", "*bad*", "*bad*", "*bad*" };
|
||||
static final String[] name1OP =
|
||||
{ "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",
|
||||
"not" };
|
||||
static final String[] name0OP =
|
||||
{ "rtrue", "rfalse", "print", "print_ret", "nop", "save", "restore", "restart",
|
||||
"ret_popped", "pop", "quit", "new_line", "show_status", "verify", "", "piracy" };
|
||||
static final String[] nameVAR =
|
||||
{ "call", "storew", "storeb", "put_prop", "sread", "print_char", "print_num", "random",
|
||||
"push", "pull", "split_window", "set_window", "call_vs2", "erase_window", "erase_line",
|
||||
"set_cursor", "get_cursor", "set_text_style", "buffer_mode", "output_stream",
|
||||
"input_stream", "sound_effect", "read_char", "scan_table", "not", "call_vn", "call_vn2",
|
||||
"tokenise", "encode_text", "copy_table", "print_table", "check_arg" };
|
||||
enum OperandType
|
||||
{
|
||||
VAR_STACK, VAR_LOCAL, VAR_GLOBAL, BYTE, WORD, ARG_BRANCH, ARG_STRING
|
||||
}
|
||||
|
||||
public Instruction (byte[] buffer, int ptr, Header header)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.startPtr = ptr;
|
||||
this.header = header;
|
||||
byte b1 = buffer[ptr];
|
||||
static final String[] name2OP =
|
||||
{ "*bad*", "je", "jl", "jg", "dec_chk", "inc_chk", "jin", "test", "or", "and",
|
||||
"test_attr", "set_attr", "clear_attr", "store", "insert_obj", "loadw", "loadb",
|
||||
"get_prop", "get_prop_addr", "get_next_prop", "add", "sub", "mul", "div", "mod",
|
||||
"call_2s", "call_2n", "set_colour", "throw", "*bad*", "*bad*", "*bad*" };
|
||||
static final String[] name1OP =
|
||||
{ "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", "not" };
|
||||
static final String[] name0OP =
|
||||
{ "rtrue", "rfalse", "print", "print_ret", "nop", "save", "restore", "restart",
|
||||
"ret_popped", "pop", "quit", "new_line", "show_status", "verify", "", "piracy" };
|
||||
static final String[] nameVAR =
|
||||
{ "call", "storew", "storeb", "put_prop", "sread", "print_char", "print_num",
|
||||
"random", "push", "pull", "split_window", "set_window", "call_vs2",
|
||||
"erase_window", "erase_line", "set_cursor", "get_cursor", "set_text_style",
|
||||
"buffer_mode", "output_stream", "input_stream", "sound_effect", "read_char",
|
||||
"scan_table", "not", "call_vn", "call_vn2", "tokenise", "encode_text",
|
||||
"copy_table", "print_table", "check_arg" };
|
||||
|
||||
// long
|
||||
if ((b1 & 0x80) == 0)
|
||||
opcode = new Opcode2OPLong (buffer, ptr);
|
||||
// short
|
||||
else if ((b1 & 0x40) == 0)
|
||||
{
|
||||
if ((b1 & 0x30) == 0x30)
|
||||
opcode = new Opcode0OP (buffer, ptr);
|
||||
else
|
||||
opcode = new Opcode1OP (buffer, ptr);
|
||||
}
|
||||
// variable
|
||||
else
|
||||
{
|
||||
if ((b1 & 0x20) == 0)
|
||||
opcode = new Opcode2OPVar (buffer, ptr);
|
||||
else
|
||||
opcode = new OpcodeVar (buffer, ptr);
|
||||
}
|
||||
}
|
||||
public Instruction (byte[] buffer, int ptr, Header header)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
this.startPtr = ptr;
|
||||
this.header = header;
|
||||
byte b1 = buffer[ptr];
|
||||
|
||||
public int length ()
|
||||
{
|
||||
return opcode.length ();
|
||||
}
|
||||
// long
|
||||
if ((b1 & 0x80) == 0)
|
||||
opcode = new Opcode2OPLong (buffer, ptr);
|
||||
// short
|
||||
else if ((b1 & 0x40) == 0)
|
||||
{
|
||||
if ((b1 & 0x30) == 0x30)
|
||||
opcode = new Opcode0OP (buffer, ptr);
|
||||
else
|
||||
opcode = new Opcode1OP (buffer, ptr);
|
||||
}
|
||||
// variable
|
||||
else
|
||||
{
|
||||
if ((b1 & 0x20) == 0)
|
||||
opcode = new Opcode2OPVar (buffer, ptr);
|
||||
else
|
||||
opcode = new OpcodeVar (buffer, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isReturn ()
|
||||
{
|
||||
return opcode.isReturn;
|
||||
}
|
||||
public int length ()
|
||||
{
|
||||
return opcode.length ();
|
||||
}
|
||||
|
||||
public boolean isPrint ()
|
||||
{
|
||||
return opcode.string != null;
|
||||
}
|
||||
public boolean isReturn ()
|
||||
{
|
||||
return opcode.isReturn;
|
||||
}
|
||||
|
||||
public boolean isCall ()
|
||||
{
|
||||
return opcode.isCall;
|
||||
}
|
||||
public boolean isPrint ()
|
||||
{
|
||||
return opcode.string != null;
|
||||
}
|
||||
|
||||
public boolean isJump ()
|
||||
{
|
||||
// could use jumpTarget != 0
|
||||
return (opcode instanceof Opcode1OP && opcode.opcodeNumber == 12);
|
||||
}
|
||||
public boolean isCall ()
|
||||
{
|
||||
return opcode.isCall;
|
||||
}
|
||||
|
||||
public boolean isBranch ()
|
||||
{
|
||||
return opcode.branch != null;
|
||||
}
|
||||
public boolean isJump ()
|
||||
{
|
||||
// could use jumpTarget != 0
|
||||
return (opcode instanceof Opcode1OP && opcode.opcodeNumber == 12);
|
||||
}
|
||||
|
||||
public boolean isStore ()
|
||||
{
|
||||
return opcode.store != null;
|
||||
}
|
||||
public boolean isBranch ()
|
||||
{
|
||||
return opcode.branch != null;
|
||||
}
|
||||
|
||||
public int target ()
|
||||
{
|
||||
return isBranch () ? opcode.branch.target : 0;
|
||||
}
|
||||
public boolean isStore ()
|
||||
{
|
||||
return opcode.store != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
int max = opcode.length ();
|
||||
String extra = "";
|
||||
if (max > 9)
|
||||
{
|
||||
max = 9;
|
||||
extra = "..";
|
||||
}
|
||||
String hex = HexFormatter.getHexString (buffer, startPtr, max);
|
||||
return String.format ("%-26s%2s %s", hex, extra, opcode.toString ());
|
||||
}
|
||||
public int target ()
|
||||
{
|
||||
return isBranch () ? opcode.branch.target : 0;
|
||||
}
|
||||
|
||||
abstract class Opcode
|
||||
{
|
||||
int opcodeNumber;
|
||||
int opcodeLength;
|
||||
List<Operand> operands;
|
||||
int totalOperandLength;
|
||||
ArgumentBranch branch;
|
||||
ArgumentString string;
|
||||
OperandVariable store;
|
||||
boolean isReturn, isCall, isExit;
|
||||
int jumpTarget;
|
||||
int callTarget;
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
int max = opcode.length ();
|
||||
String extra = "";
|
||||
if (max > 9)
|
||||
{
|
||||
max = 9;
|
||||
extra = "..";
|
||||
}
|
||||
String hex = HexFormatter.getHexString (buffer, startPtr, max);
|
||||
return String.format ("%-26s%2s %s", hex, extra, opcode.toString ());
|
||||
}
|
||||
|
||||
public Opcode ()
|
||||
{
|
||||
operands = new ArrayList<Operand> ();
|
||||
}
|
||||
abstract class Opcode
|
||||
{
|
||||
int opcodeNumber;
|
||||
int opcodeLength;
|
||||
List<Operand> operands;
|
||||
int totalOperandLength;
|
||||
ArgumentBranch branch;
|
||||
ArgumentString string;
|
||||
OperandVariable store;
|
||||
boolean isReturn, isCall, isExit;
|
||||
int jumpTarget;
|
||||
int callTarget;
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
StringBuilder text = new StringBuilder ();
|
||||
if (false)
|
||||
text.append (HexFormatter.formatNoHeader (buffer, startPtr, length ()) + "\n");
|
||||
text.append (String.format ("%05X : %-12s", startPtr, opcodeName ()));
|
||||
if (jumpTarget != 0)
|
||||
text.append (String.format (" L:%05X", jumpTarget));
|
||||
else if (isCall)
|
||||
{
|
||||
text.append (String.format (" R:%05X (", callTarget));
|
||||
int count = 0;
|
||||
for (Operand op : operands)
|
||||
if (count++ > 0)
|
||||
text.append (op + ", ");
|
||||
if (operands.size () > 1)
|
||||
text.delete (text.length () - 2, text.length ());
|
||||
text.append (") --> " + store);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Operand op : operands)
|
||||
text.append (" " + op);
|
||||
if (branch != null)
|
||||
text.append (branch);
|
||||
if (store != null)
|
||||
text.append (" --> " + store);
|
||||
if (string != null)
|
||||
text.append (" \"" + string + "\"");
|
||||
}
|
||||
return text.toString ();
|
||||
}
|
||||
public Opcode ()
|
||||
{
|
||||
operands = new ArrayList<Operand> ();
|
||||
}
|
||||
|
||||
public int length ()
|
||||
{
|
||||
int length = totalOperandLength + opcodeLength;
|
||||
if (branch != null)
|
||||
length += branch.length;
|
||||
if (store != null)
|
||||
length += store.length;
|
||||
if (string != null)
|
||||
length += string.length;
|
||||
return length;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
StringBuilder text = new StringBuilder ();
|
||||
if (false)
|
||||
text.append (HexFormatter.formatNoHeader (buffer, startPtr, length ()) + "\n");
|
||||
text.append (String.format ("%05X : %-12s", startPtr, opcodeName ()));
|
||||
if (jumpTarget != 0)
|
||||
text.append (String.format (" L:%05X", jumpTarget));
|
||||
else if (isCall)
|
||||
{
|
||||
text.append (String.format (" R:%05X (", callTarget));
|
||||
int count = 0;
|
||||
for (Operand op : operands)
|
||||
if (count++ > 0)
|
||||
text.append (op + ", ");
|
||||
if (operands.size () > 1)
|
||||
text.delete (text.length () - 2, text.length ());
|
||||
text.append (") --> " + store);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (Operand op : operands)
|
||||
text.append (" " + op);
|
||||
if (branch != null)
|
||||
text.append (branch);
|
||||
if (store != null)
|
||||
text.append (" --> " + store);
|
||||
if (string != null)
|
||||
text.append (" \"" + string + "\"");
|
||||
}
|
||||
return text.toString ();
|
||||
}
|
||||
|
||||
public abstract String opcodeName ();
|
||||
public int length ()
|
||||
{
|
||||
int length = totalOperandLength + opcodeLength;
|
||||
if (branch != null)
|
||||
length += branch.length;
|
||||
if (store != null)
|
||||
length += store.length;
|
||||
if (string != null)
|
||||
length += string.length;
|
||||
return length;
|
||||
}
|
||||
|
||||
private void addOperand (Operand operand)
|
||||
{
|
||||
operands.add (operand);
|
||||
totalOperandLength += operand.length;
|
||||
}
|
||||
public abstract String opcodeName ();
|
||||
|
||||
protected void addOperand (byte[] buffer, int ptr, boolean bit1, boolean bit2)
|
||||
{
|
||||
int offset = ptr + totalOperandLength;
|
||||
if (bit1)
|
||||
{
|
||||
if (!bit2)
|
||||
addOperand (new OperandVariable (buffer[offset])); // %10
|
||||
}
|
||||
else if (bit2)
|
||||
addOperand (new OperandByte (buffer[offset])); // %01
|
||||
else
|
||||
addOperand (new OperandWord (header.getWord (offset))); // %00
|
||||
}
|
||||
private void addOperand (Operand operand)
|
||||
{
|
||||
operands.add (operand);
|
||||
totalOperandLength += operand.length;
|
||||
}
|
||||
|
||||
protected void addOperand (byte[] buffer, int ptr, boolean bit)
|
||||
{
|
||||
int address = ptr + totalOperandLength;
|
||||
if (address >= buffer.length)
|
||||
{
|
||||
System.out.println ("Illegal byte address : " + address);
|
||||
return;
|
||||
}
|
||||
if (bit)
|
||||
addOperand (new OperandVariable (buffer[address]));
|
||||
else
|
||||
addOperand (new OperandByte (buffer[address]));
|
||||
}
|
||||
protected void addOperand (byte[] buffer, int ptr, boolean bit1, boolean bit2)
|
||||
{
|
||||
int offset = ptr + totalOperandLength;
|
||||
if (bit1)
|
||||
{
|
||||
if (!bit2)
|
||||
addOperand (new OperandVariable (buffer[offset])); // %10
|
||||
}
|
||||
else if (bit2)
|
||||
addOperand (new OperandByte (buffer[offset])); // %01
|
||||
else
|
||||
addOperand (new OperandWord (header.getWord (offset))); // %00
|
||||
}
|
||||
|
||||
protected void setVariableOperands (int ptr)
|
||||
{
|
||||
int value = buffer[ptr + 1] & 0xFF;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
boolean bit1 = ((value & 0x80) == 0x80);
|
||||
boolean bit2 = ((value & 0x40) == 0x40);
|
||||
if (bit1 && bit2)
|
||||
break;
|
||||
addOperand (buffer, ptr + 2, bit1, bit2);
|
||||
value <<= 2;
|
||||
}
|
||||
}
|
||||
protected void addOperand (byte[] buffer, int ptr, boolean bit)
|
||||
{
|
||||
int address = ptr + totalOperandLength;
|
||||
if (address >= buffer.length)
|
||||
{
|
||||
System.out.println ("Illegal byte address : " + address);
|
||||
return;
|
||||
}
|
||||
if (bit)
|
||||
addOperand (new OperandVariable (buffer[address]));
|
||||
else
|
||||
addOperand (new OperandByte (buffer[address]));
|
||||
}
|
||||
|
||||
protected void setStore (byte[] buffer)
|
||||
{
|
||||
store = new OperandVariable (buffer[startPtr + totalOperandLength + opcodeLength]);
|
||||
}
|
||||
protected void setVariableOperands (int ptr)
|
||||
{
|
||||
int value = buffer[ptr + 1] & 0xFF;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
boolean bit1 = ((value & 0x80) == 0x80);
|
||||
boolean bit2 = ((value & 0x40) == 0x40);
|
||||
if (bit1 && bit2)
|
||||
break;
|
||||
addOperand (buffer, ptr + 2, bit1, bit2);
|
||||
value <<= 2;
|
||||
}
|
||||
}
|
||||
|
||||
protected void setBranch (byte[] buffer)
|
||||
{
|
||||
int offset = startPtr + totalOperandLength + (store == null ? 0 : 1) + opcodeLength;
|
||||
if ((buffer[offset] & 0x40) == 0x40)
|
||||
branch = new ArgumentBranch (buffer[offset], offset);
|
||||
else
|
||||
branch = new ArgumentBranch (header.getWord (offset), offset);
|
||||
}
|
||||
protected void setStore (byte[] buffer)
|
||||
{
|
||||
store = new OperandVariable (buffer[startPtr + totalOperandLength + opcodeLength]);
|
||||
}
|
||||
|
||||
protected void setZString (byte[] buffer)
|
||||
{
|
||||
int offset = startPtr + totalOperandLength + opcodeLength;
|
||||
string = new ArgumentString (buffer, offset);
|
||||
}
|
||||
}
|
||||
protected void setBranch (byte[] buffer)
|
||||
{
|
||||
int offset = startPtr + totalOperandLength + (store == null ? 0 : 1) + opcodeLength;
|
||||
if ((buffer[offset] & 0x40) == 0x40)
|
||||
branch = new ArgumentBranch (buffer[offset], offset);
|
||||
else
|
||||
branch = new ArgumentBranch (header.getWord (offset), offset);
|
||||
}
|
||||
|
||||
class Opcode0OP extends Opcode
|
||||
{
|
||||
public Opcode0OP (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x0F;
|
||||
opcodeLength = 1;
|
||||
protected void setZString (byte[] buffer)
|
||||
{
|
||||
int offset = startPtr + totalOperandLength + opcodeLength;
|
||||
string = new ArgumentString (buffer, offset);
|
||||
}
|
||||
}
|
||||
|
||||
if (opcodeNumber == 5 || opcodeNumber == 6 || opcodeNumber == 13)
|
||||
setBranch (buffer);
|
||||
class Opcode0OP extends Opcode
|
||||
{
|
||||
public Opcode0OP (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x0F;
|
||||
opcodeLength = 1;
|
||||
|
||||
if (opcodeNumber == 0 || opcodeNumber == 1 || opcodeNumber == 3 || opcodeNumber == 8)
|
||||
isReturn = true;
|
||||
if (opcodeNumber == 5 || opcodeNumber == 6 || opcodeNumber == 13)
|
||||
setBranch (buffer);
|
||||
|
||||
if (opcodeNumber == 2 || opcodeNumber == 3)
|
||||
setZString (buffer);
|
||||
if (opcodeNumber == 0 || opcodeNumber == 1 || opcodeNumber == 3
|
||||
|| opcodeNumber == 8)
|
||||
isReturn = true;
|
||||
|
||||
if (opcodeNumber == 7 || opcodeNumber == 10)
|
||||
isExit = true;
|
||||
}
|
||||
if (opcodeNumber == 2 || opcodeNumber == 3)
|
||||
setZString (buffer);
|
||||
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name0OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
if (opcodeNumber == 7 || opcodeNumber == 10)
|
||||
isExit = true;
|
||||
}
|
||||
|
||||
class Opcode1OP extends Opcode
|
||||
{
|
||||
public Opcode1OP (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x0F;
|
||||
opcodeLength = 1;
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name0OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
|
||||
boolean bit1 = ((buffer[ptr] & 0x20) == 0x20);
|
||||
boolean bit2 = ((buffer[ptr] & 0x10) == 0x10);
|
||||
addOperand (buffer, ptr + 1, bit1, bit2);
|
||||
class Opcode1OP extends Opcode
|
||||
{
|
||||
public Opcode1OP (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x0F;
|
||||
opcodeLength = 1;
|
||||
|
||||
if ((opcodeNumber >= 1 && opcodeNumber <= 4) || opcodeNumber == 8 || opcodeNumber == 14
|
||||
|| opcodeNumber == 15)
|
||||
setStore (buffer);
|
||||
if (opcodeNumber <= 2)
|
||||
setBranch (buffer);
|
||||
if (opcodeNumber == 12)
|
||||
jumpTarget = (short) operands.get (0).value + startPtr - 2 + length ();
|
||||
if (opcodeNumber == 11)
|
||||
isReturn = true;
|
||||
}
|
||||
boolean bit1 = ((buffer[ptr] & 0x20) == 0x20);
|
||||
boolean bit2 = ((buffer[ptr] & 0x10) == 0x10);
|
||||
addOperand (buffer, ptr + 1, bit1, bit2);
|
||||
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name1OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
if ((opcodeNumber >= 1 && opcodeNumber <= 4) || opcodeNumber == 8
|
||||
|| opcodeNumber == 14 || opcodeNumber == 15)
|
||||
setStore (buffer);
|
||||
if (opcodeNumber <= 2)
|
||||
setBranch (buffer);
|
||||
if (opcodeNumber == 12)
|
||||
jumpTarget = (short) operands.get (0).value + startPtr - 2 + length ();
|
||||
if (opcodeNumber == 11)
|
||||
isReturn = true;
|
||||
}
|
||||
|
||||
abstract class Opcode2OP extends Opcode
|
||||
{
|
||||
public Opcode2OP ()
|
||||
{
|
||||
opcodeLength = 1;
|
||||
}
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name1OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
|
||||
public void setArguments (byte[] buffer)
|
||||
{
|
||||
if ((opcodeNumber >= 1 && opcodeNumber <= 7) || opcodeNumber == 10)
|
||||
setBranch (buffer);
|
||||
else if ((opcodeNumber >= 15 && opcodeNumber <= 25) || opcodeNumber == 8 || opcodeNumber == 9)
|
||||
setStore (buffer);
|
||||
}
|
||||
abstract class Opcode2OP extends Opcode
|
||||
{
|
||||
public Opcode2OP ()
|
||||
{
|
||||
opcodeLength = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name2OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
public void setArguments (byte[] buffer)
|
||||
{
|
||||
if ((opcodeNumber >= 1 && opcodeNumber <= 7) || opcodeNumber == 10)
|
||||
setBranch (buffer);
|
||||
else if ((opcodeNumber >= 15 && opcodeNumber <= 25) || opcodeNumber == 8
|
||||
|| opcodeNumber == 9)
|
||||
setStore (buffer);
|
||||
}
|
||||
|
||||
class Opcode2OPLong extends Opcode2OP
|
||||
{
|
||||
public Opcode2OPLong (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
boolean bit1 = ((buffer[ptr] & 0x40) == 0x40);
|
||||
boolean bit2 = ((buffer[ptr] & 0x20) == 0x20);
|
||||
addOperand (buffer, ptr + 1, bit1);
|
||||
addOperand (buffer, ptr + 1, bit2);
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return name2OP[opcodeNumber];
|
||||
}
|
||||
}
|
||||
|
||||
setArguments (buffer);
|
||||
}
|
||||
}
|
||||
class Opcode2OPLong extends Opcode2OP
|
||||
{
|
||||
public Opcode2OPLong (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
boolean bit1 = ((buffer[ptr] & 0x40) == 0x40);
|
||||
boolean bit2 = ((buffer[ptr] & 0x20) == 0x20);
|
||||
addOperand (buffer, ptr + 1, bit1);
|
||||
addOperand (buffer, ptr + 1, bit2);
|
||||
|
||||
class Opcode2OPVar extends Opcode2OP
|
||||
{
|
||||
public Opcode2OPVar (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
opcodeLength = 2;
|
||||
setVariableOperands (ptr);
|
||||
setArguments (buffer);
|
||||
}
|
||||
}
|
||||
setArguments (buffer);
|
||||
}
|
||||
}
|
||||
|
||||
class OpcodeVar extends Opcode
|
||||
{
|
||||
public OpcodeVar (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
opcodeLength = 2;
|
||||
setVariableOperands (ptr);
|
||||
class Opcode2OPVar extends Opcode2OP
|
||||
{
|
||||
public Opcode2OPVar (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
opcodeLength = 2;
|
||||
setVariableOperands (ptr);
|
||||
setArguments (buffer);
|
||||
}
|
||||
}
|
||||
|
||||
if (opcodeNumber == 0 || opcodeNumber == 7)
|
||||
setStore (buffer);
|
||||
if (opcodeNumber == 0)
|
||||
{
|
||||
isCall = true;
|
||||
callTarget = operands.get (0).value * 2;
|
||||
}
|
||||
}
|
||||
class OpcodeVar extends Opcode
|
||||
{
|
||||
public OpcodeVar (byte[] buffer, int ptr)
|
||||
{
|
||||
opcodeNumber = buffer[ptr] & 0x1F;
|
||||
opcodeLength = 2;
|
||||
setVariableOperands (ptr);
|
||||
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return nameVAR[opcodeNumber];
|
||||
}
|
||||
}
|
||||
if (opcodeNumber == 0 || opcodeNumber == 7)
|
||||
setStore (buffer);
|
||||
if (opcodeNumber == 0)
|
||||
{
|
||||
isCall = true;
|
||||
callTarget = operands.get (0).value * 2;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Operand
|
||||
{
|
||||
int length;
|
||||
int value;
|
||||
}
|
||||
@Override
|
||||
public String opcodeName ()
|
||||
{
|
||||
return nameVAR[opcodeNumber];
|
||||
}
|
||||
}
|
||||
|
||||
class OperandWord extends Operand
|
||||
{
|
||||
public OperandWord (int value)
|
||||
{
|
||||
this.value = value;
|
||||
length = 2;
|
||||
}
|
||||
abstract class Operand
|
||||
{
|
||||
int length;
|
||||
int value;
|
||||
OperandType operandType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("#%05d", value);
|
||||
}
|
||||
}
|
||||
class OperandWord extends Operand
|
||||
{
|
||||
public OperandWord (int value)
|
||||
{
|
||||
this.value = value;
|
||||
length = 2;
|
||||
operandType = OperandType.WORD;
|
||||
}
|
||||
|
||||
class OperandByte extends Operand
|
||||
{
|
||||
public OperandByte (byte value)
|
||||
{
|
||||
this.value = value & 0xFF;
|
||||
length = 1;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("#%05d", value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("#%03d", value);
|
||||
}
|
||||
}
|
||||
class OperandByte extends Operand
|
||||
{
|
||||
public OperandByte (byte value)
|
||||
{
|
||||
this.value = value & 0xFF;
|
||||
length = 1;
|
||||
operandType = OperandType.BYTE;
|
||||
}
|
||||
|
||||
class OperandVariable extends Operand
|
||||
{
|
||||
public OperandVariable (byte value)
|
||||
{
|
||||
this.value = value & 0xFF;
|
||||
length = 1;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("#%03d", value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
if (value == 0)
|
||||
return ("ToS");
|
||||
if (value <= 15)
|
||||
return (String.format ("L%02d", value));
|
||||
return String.format ("G%03d", (value - 15));
|
||||
}
|
||||
}
|
||||
class OperandVariable extends Operand
|
||||
{
|
||||
public OperandVariable (byte value)
|
||||
{
|
||||
this.value = value & 0xFF;
|
||||
length = 1;
|
||||
|
||||
class ArgumentBranch extends Operand
|
||||
{
|
||||
int target;
|
||||
boolean branchOnTrue;
|
||||
if (value == 0)
|
||||
operandType = OperandType.VAR_STACK;
|
||||
else if (value <= 15)
|
||||
operandType = OperandType.VAR_LOCAL;
|
||||
else
|
||||
operandType = OperandType.VAR_GLOBAL;
|
||||
}
|
||||
|
||||
public ArgumentBranch (byte value, int offset)
|
||||
{
|
||||
branchOnTrue = (value & 0x80) == 0x80;
|
||||
int val = value & 0x3F; // unsigned
|
||||
if (val <= 1)
|
||||
target = val;
|
||||
else
|
||||
target = val + offset - 1;
|
||||
length = 1;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
if (operandType == OperandType.VAR_STACK)
|
||||
return ("ToS");
|
||||
if (operandType == OperandType.VAR_LOCAL)
|
||||
return (String.format ("L%02d", value));
|
||||
return String.format ("G%03d", (value - 15));
|
||||
}
|
||||
}
|
||||
|
||||
public ArgumentBranch (int value, int offset)
|
||||
{
|
||||
branchOnTrue = (value & 0x8000) == 0x8000;
|
||||
int val = value & 0x3FFF; // signed
|
||||
if (val > 8191)
|
||||
val -= 16384;
|
||||
target = val + offset;
|
||||
length = 2;
|
||||
}
|
||||
class ArgumentBranch extends Operand
|
||||
{
|
||||
int target;
|
||||
boolean branchOnTrue;
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
StringBuilder text = new StringBuilder ();
|
||||
text.append (" [" + (branchOnTrue ? "true" : "false") + "] ");
|
||||
if (target == 0 || target == 1)
|
||||
text.append (target == 0 ? "RFALSE" : "RTRUE");
|
||||
else
|
||||
text.append (String.format ("%05X", target));
|
||||
return text.toString ();
|
||||
}
|
||||
}
|
||||
public ArgumentBranch (byte value, int offset)
|
||||
{
|
||||
branchOnTrue = (value & 0x80) == 0x80;
|
||||
int val = value & 0x3F; // unsigned
|
||||
if (val <= 1)
|
||||
target = val;
|
||||
else
|
||||
target = val + offset - 1;
|
||||
length = 1;
|
||||
operandType = OperandType.ARG_BRANCH;
|
||||
}
|
||||
|
||||
class ArgumentString extends Operand
|
||||
{
|
||||
ZString text;
|
||||
int startPtr;
|
||||
byte[] buffer;
|
||||
public ArgumentBranch (int value, int offset)
|
||||
{
|
||||
branchOnTrue = (value & 0x8000) == 0x8000;
|
||||
int val = value & 0x3FFF; // signed
|
||||
if (val > 8191)
|
||||
val -= 16384;
|
||||
target = val + offset;
|
||||
length = 2;
|
||||
operandType = OperandType.ARG_BRANCH;
|
||||
}
|
||||
|
||||
public ArgumentString (byte[] buffer, int offset)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
text = new ZString (buffer, offset, header);
|
||||
length = text.length;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
StringBuilder text = new StringBuilder ();
|
||||
text.append (" [" + (branchOnTrue ? "true" : "false") + "] ");
|
||||
if (target == 0 || target == 1)
|
||||
text.append (target == 0 ? "RFALSE" : "RTRUE");
|
||||
else
|
||||
text.append (String.format ("%05X", target));
|
||||
return text.toString ();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return text.value;
|
||||
}
|
||||
}
|
||||
class ArgumentString extends Operand
|
||||
{
|
||||
ZString text;
|
||||
int startPtr;
|
||||
byte[] buffer;
|
||||
|
||||
public ArgumentString (byte[] buffer, int offset)
|
||||
{
|
||||
this.buffer = buffer;
|
||||
text = new ZString (header, offset);
|
||||
length = text.length;
|
||||
operandType = OperandType.ARG_STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return text.value;
|
||||
}
|
||||
}
|
||||
}
|
@ -8,210 +8,212 @@ import com.bytezone.diskbrowser.infocom.ZObject.Property;
|
||||
|
||||
class ObjectAnalyser
|
||||
{
|
||||
Header header;
|
||||
ObjectManager parent;
|
||||
List<Statistics> list = new ArrayList<Statistics> ();
|
||||
List<Integer> routines = new ArrayList<Integer> ();
|
||||
Header header;
|
||||
ObjectManager parent;
|
||||
List<Statistics> list = new ArrayList<Statistics> ();
|
||||
List<Integer> routines = new ArrayList<Integer> ();
|
||||
|
||||
public ObjectAnalyser (Header header, ObjectManager parent)
|
||||
{
|
||||
this.header = header;
|
||||
this.parent = parent;
|
||||
public ObjectAnalyser (Header header, ObjectManager parent)
|
||||
{
|
||||
this.header = header;
|
||||
this.parent = parent;
|
||||
|
||||
// assign the DICT property for each object
|
||||
setDictionary ();
|
||||
// assign the DICT property for each object
|
||||
setDictionary ();
|
||||
|
||||
// find the point where code ends and strings begin
|
||||
setStringPointer ();
|
||||
// find the point where code ends and strings begin
|
||||
setStringPointer ();
|
||||
|
||||
// add routines called from object properties (requires stringPointer)
|
||||
createPropertyLinks ();
|
||||
// add routines called from object properties (requires stringPointer)
|
||||
createPropertyLinks ();
|
||||
|
||||
// assumes that all properties with exactly three bytes are routine addresses
|
||||
// checkThreeByteProperties ();
|
||||
}
|
||||
// assumes that all properties with exactly three bytes are routine addresses
|
||||
// checkThreeByteProperties ();
|
||||
}
|
||||
|
||||
public void setStringPointer ()
|
||||
{
|
||||
PropertyTester pt = new PropertyTester (parent.list);
|
||||
pt.addTest (new LengthTwoCondition ());
|
||||
HighMemoryCondition hmc = new HighMemoryCondition ();
|
||||
pt.addTest (hmc);
|
||||
pt.doTests ();
|
||||
public void setStringPointer ()
|
||||
{
|
||||
PropertyTester pt = new PropertyTester (parent.getObjects ());
|
||||
pt.addTest (new LengthTwoCondition ());
|
||||
HighMemoryCondition hmc = new HighMemoryCondition ();
|
||||
pt.addTest (hmc);
|
||||
pt.doTests ();
|
||||
|
||||
System.out.println ("\nSetting the string pointer\n");
|
||||
// System.out.println ("\nSetting the string pointer\n");
|
||||
|
||||
for (Integer propertyNo : pt)
|
||||
// list of all properties that passed all tests
|
||||
list.add (hmc.statistics[propertyNo]);
|
||||
Collections.sort (list);
|
||||
for (Integer propertyNo : pt)
|
||||
// list of all properties that passed all tests
|
||||
list.add (hmc.statistics[propertyNo]);
|
||||
Collections.sort (list);
|
||||
|
||||
// Calculate lowest string pointer
|
||||
int lo = list.get (0).lo;
|
||||
for (Statistics s : list)
|
||||
{
|
||||
System.out.println (s);
|
||||
if (s.hi > lo && s.lo < lo)
|
||||
lo = s.lo;
|
||||
if (s.hi < lo)
|
||||
{
|
||||
header.stringPointer = lo;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Calculate lowest string pointer
|
||||
int lo = list.get (0).lo;
|
||||
for (Statistics s : list)
|
||||
{
|
||||
System.out.println (s);
|
||||
if (s.hi > lo && s.lo < lo)
|
||||
lo = s.lo;
|
||||
if (s.hi < lo)
|
||||
{
|
||||
header.stringPointer = lo;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void createPropertyLinks ()
|
||||
{
|
||||
int sCount = 0;
|
||||
int rCount = 0;
|
||||
int totStrings = 0;
|
||||
int totRoutines = 0;
|
||||
public void createPropertyLinks ()
|
||||
{
|
||||
int sCount = 0;
|
||||
int rCount = 0;
|
||||
int totStrings = 0;
|
||||
int totRoutines = 0;
|
||||
|
||||
for (Statistics s : list)
|
||||
{
|
||||
if (header.propertyNames[s.propertyNumber].charAt (0) >= 'a')
|
||||
continue;
|
||||
if (s.lo >= header.stringPointer)
|
||||
{
|
||||
header.propertyNames[s.propertyNumber] = "STR" + ++sCount;
|
||||
totStrings += s.offsets.size ();
|
||||
}
|
||||
else
|
||||
{
|
||||
header.propertyNames[s.propertyNumber] = "CODE" + ++rCount;
|
||||
routines.addAll (s.offsets);
|
||||
totRoutines += s.offsets.size ();
|
||||
}
|
||||
}
|
||||
System.out.println ("Strings found : " + totStrings);
|
||||
System.out.println ("Routines found : " + totRoutines);
|
||||
}
|
||||
for (Statistics s : list)
|
||||
{
|
||||
if (header.getPropertyName (s.propertyNumber).charAt (0) >= 'a')
|
||||
continue;
|
||||
if (s.lo >= header.stringPointer)
|
||||
{
|
||||
header.propertyNames[s.propertyNumber] = "STR" + ++sCount;
|
||||
totStrings += s.offsets.size ();
|
||||
}
|
||||
else
|
||||
{
|
||||
header.propertyNames[s.propertyNumber] = "CODE" + ++rCount;
|
||||
routines.addAll (s.offsets);
|
||||
totRoutines += s.offsets.size ();
|
||||
}
|
||||
}
|
||||
System.out.println ("Strings found : " + totStrings);
|
||||
System.out.println ("Routines found : " + totRoutines);
|
||||
}
|
||||
|
||||
public void checkThreeByteProperties ()
|
||||
{
|
||||
for (ZObject object : parent.list)
|
||||
{
|
||||
for (Property property : object.properties)
|
||||
{
|
||||
if (header.propertyNames[property.propertyNumber].charAt (0) < 'a' && property.length == 3)
|
||||
{
|
||||
int address = header.getWord (property.ptr + 1) * 2;
|
||||
System.out.println ("checking " + address);
|
||||
header.codeManager.addRoutine (address, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void checkThreeByteProperties ()
|
||||
{
|
||||
for (ZObject object : parent.getObjects ())
|
||||
{
|
||||
for (Property property : object.properties)
|
||||
{
|
||||
if (header.getPropertyName (property.propertyNumber).charAt (0) < 'a'
|
||||
&& property.length == 3)
|
||||
{
|
||||
int address = header.getWord (property.ptr + 1) * 2;
|
||||
System.out.println ("checking " + address);
|
||||
header.codeManager.addRoutine (address, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// find the property with only dictionary entries
|
||||
public void setDictionary ()
|
||||
{
|
||||
PropertyTester pt = new PropertyTester (parent.list);
|
||||
pt.addTest (new LengthEvenCondition ());
|
||||
pt.addTest (new ValidDictionaryCondition ());
|
||||
pt.doTests ();
|
||||
// find the property with only dictionary entries
|
||||
public void setDictionary ()
|
||||
{
|
||||
PropertyTester pt = new PropertyTester (parent.getObjects ());
|
||||
pt.addTest (new LengthEvenCondition ());
|
||||
pt.addTest (new ValidDictionaryCondition ());
|
||||
pt.doTests ();
|
||||
|
||||
for (Integer i : pt)
|
||||
// should only be one
|
||||
header.propertyNames[i] = "DICT";
|
||||
}
|
||||
for (Integer i : pt)
|
||||
// should only be one
|
||||
header.propertyNames[i] = "DICT"; // SYNONYM
|
||||
}
|
||||
|
||||
class Statistics implements Comparable<Statistics>
|
||||
{
|
||||
int propertyNumber;
|
||||
int lo;
|
||||
int hi;
|
||||
List<Integer> offsets = new ArrayList<Integer> ();
|
||||
class Statistics implements Comparable<Statistics>
|
||||
{
|
||||
int propertyNumber;
|
||||
int lo;
|
||||
int hi;
|
||||
List<Integer> offsets = new ArrayList<Integer> ();
|
||||
|
||||
public Statistics (int propertyNumber)
|
||||
{
|
||||
this.propertyNumber = propertyNumber;
|
||||
}
|
||||
public Statistics (int propertyNumber)
|
||||
{
|
||||
this.propertyNumber = propertyNumber;
|
||||
}
|
||||
|
||||
public void increment (Property property)
|
||||
{
|
||||
offsets.add (property.offset);
|
||||
if (property.offset > hi)
|
||||
hi = property.offset;
|
||||
if (property.offset < lo || lo == 0)
|
||||
lo = property.offset;
|
||||
}
|
||||
public void increment (Property property)
|
||||
{
|
||||
offsets.add (property.offset);
|
||||
if (property.offset > hi)
|
||||
hi = property.offset;
|
||||
if (property.offset < lo || lo == 0)
|
||||
lo = property.offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("%2d %3d %,7d %,7d", propertyNumber, offsets.size (), lo, hi);
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return String.format ("%2d %3d %,7d %,7d", propertyNumber, offsets.size (),
|
||||
lo, hi);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo (Statistics o)
|
||||
{
|
||||
return o.hi - hi;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public int compareTo (Statistics o)
|
||||
{
|
||||
return o.hi - hi;
|
||||
}
|
||||
}
|
||||
|
||||
class LengthTwoCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return property.length == 2;
|
||||
}
|
||||
}
|
||||
class LengthTwoCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return property.length == 2;
|
||||
}
|
||||
}
|
||||
|
||||
class LengthThreeCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return property.length == 3;
|
||||
}
|
||||
}
|
||||
class LengthThreeCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return property.length == 3;
|
||||
}
|
||||
}
|
||||
|
||||
class LengthEvenCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return (property.length % 2) == 0;
|
||||
}
|
||||
}
|
||||
class LengthEvenCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
return (property.length % 2) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
class HighMemoryCondition extends Condition
|
||||
{
|
||||
int lo, hi;
|
||||
Statistics[] statistics = new Statistics[32]; // note there is no property #0
|
||||
class HighMemoryCondition extends Condition
|
||||
{
|
||||
int lo, hi;
|
||||
Statistics[] statistics = new Statistics[32]; // note there is no property #0
|
||||
|
||||
public HighMemoryCondition ()
|
||||
{
|
||||
lo = header.highMemory;
|
||||
hi = header.fileLength;
|
||||
for (int i = 1; i < statistics.length; i++)
|
||||
statistics[i] = new Statistics (i);
|
||||
}
|
||||
public HighMemoryCondition ()
|
||||
{
|
||||
lo = header.highMemory;
|
||||
hi = header.fileLength;
|
||||
for (int i = 1; i < statistics.length; i++)
|
||||
statistics[i] = new Statistics (i);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
statistics[property.propertyNumber].increment (property);
|
||||
int address = header.getWord (property.ptr + 1) * 2;
|
||||
return (address >= lo && address < hi) || address == 0;
|
||||
}
|
||||
}
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
statistics[property.propertyNumber].increment (property);
|
||||
int address = header.getWord (property.ptr + 1) * 2;
|
||||
return (address >= lo && address < hi) || address == 0;
|
||||
}
|
||||
}
|
||||
|
||||
class ValidDictionaryCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
for (int i = 1; i <= property.length; i += 2)
|
||||
{
|
||||
int address = header.getWord (property.ptr + i);
|
||||
if (!header.containsWordAt (address))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
class ValidDictionaryCondition extends Condition
|
||||
{
|
||||
@Override
|
||||
boolean test (Property property)
|
||||
{
|
||||
for (int i = 1; i <= property.length; i += 2)
|
||||
{
|
||||
int address = header.getWord (property.ptr + i);
|
||||
if (!header.containsWordAt (address))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.bytezone.diskbrowser.infocom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@ -11,21 +12,22 @@ import com.bytezone.diskbrowser.disk.FormattedDisk;
|
||||
|
||||
class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
|
||||
{
|
||||
Header header;
|
||||
List<ZObject> list;
|
||||
int defaultsPtr, defaultsSize;
|
||||
int tablePtr, tableSize;
|
||||
int propertyPtr, propertySize;
|
||||
ObjectAnalyser analyser;
|
||||
private final Header header;
|
||||
private final List<ZObject> list;
|
||||
private List<ZObject> sortedList;
|
||||
private final int defaultsPtr, defaultsSize;
|
||||
private final int tablePtr, tableSize;
|
||||
private final int propertyPtr, propertySize;
|
||||
private final ObjectAnalyser analyser;
|
||||
|
||||
public ObjectManager (Header header)
|
||||
{
|
||||
super ("Objects", header.buffer);
|
||||
this.header = header;
|
||||
|
||||
defaultsPtr = header.objectTable;
|
||||
defaultsPtr = header.objectTableOffset;
|
||||
defaultsSize = 62;
|
||||
tablePtr = header.objectTable + 62;
|
||||
tablePtr = header.objectTableOffset + 62;
|
||||
propertyPtr = header.getWord (tablePtr + 7);
|
||||
propertySize = header.globalsOffset - propertyPtr;
|
||||
tableSize = (propertyPtr - tablePtr);
|
||||
@ -45,6 +47,16 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
|
||||
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)
|
||||
{
|
||||
root.setAllowsChildren (true);
|
||||
@ -61,10 +73,9 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
|
||||
new DefaultAppleFileSource (object.getName (), object, disk));
|
||||
parentNode.add (child);
|
||||
if (object.sibling > 0)
|
||||
buildObjectTree (header.objectManager.list.get (object.sibling - 1), parentNode,
|
||||
disk);
|
||||
buildObjectTree (list.get (object.sibling - 1), parentNode, disk);
|
||||
if (object.child > 0)
|
||||
buildObjectTree (header.objectManager.list.get (object.child - 1), child, disk);
|
||||
buildObjectTree (list.get (object.child - 1), child, disk);
|
||||
else
|
||||
child.setAllowsChildren (false);
|
||||
}
|
||||
@ -77,8 +88,8 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
|
||||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
String header1 = "ID Attributes Pr Sb Ch Prop Title\n-- -----------"
|
||||
+ " -- -- -- ----- -----------------------------\n";
|
||||
// String header1 = "ID Attributes Pr Sb Ch Prop Title\n-- -----------"
|
||||
// + " -- -- -- ----- -----------------------------\n";
|
||||
String underline = " ----------------------------------------";
|
||||
String titles[] =
|
||||
{ "ID ", "Title ",
|
||||
@ -89,13 +100,21 @@ class ObjectManager extends InfocomAbstractFile implements Iterable<ZObject>
|
||||
+ "-- " + underline + underline + underline + underline + " ----------- -----\n";
|
||||
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)
|
||||
if (false)
|
||||
text.append (String.format ("%02X %s%n", ++objectNumber, zo));
|
||||
else
|
||||
text.append (
|
||||
String.format ("%02X %s%n", ++objectNumber, zo.getDescription (list)));
|
||||
// if (false)
|
||||
// text.append (String.format ("%02X %s%n", ++objectNumber, zo));
|
||||
// else
|
||||
text.append (String.format ("%02X %s%n", zo.getId (), 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);
|
||||
return text.toString ();
|
||||
|
@ -33,7 +33,7 @@ class PropertyManager extends AbstractFile
|
||||
for (Statistic stat : list)
|
||||
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 (
|
||||
new DefaultAppleFileSource (title, stat.getText (), disk));
|
||||
node.add (child);
|
||||
@ -72,13 +72,13 @@ class PropertyManager extends AbstractFile
|
||||
|
||||
String getText ()
|
||||
{
|
||||
StringBuilder text =
|
||||
new StringBuilder ("Objects with property " + id + " set:\n\n");
|
||||
StringBuilder text = new StringBuilder (String
|
||||
.format ("Objects with property %d %s set:%n%n", id, header.propertyNames[id]));
|
||||
for (ZObject o : list)
|
||||
{
|
||||
ZObject.Property p = o.getProperty (id);
|
||||
text.append (String.format ("%02X %-29s%s%n", o.id, o.getName (),
|
||||
p.toString ().substring (7)));
|
||||
text.append (String.format ("%02X %-29s%s%n", o.getId (), o.getName (),
|
||||
p.toString ().substring (11)));
|
||||
}
|
||||
if (text.length () > 0)
|
||||
text.deleteCharAt (text.length () - 1);
|
||||
@ -88,7 +88,7 @@ class PropertyManager extends AbstractFile
|
||||
@Override
|
||||
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 ());
|
||||
}
|
||||
}
|
||||
|
@ -4,13 +4,15 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.bytezone.diskbrowser.infocom.Instruction.Operand;
|
||||
import com.bytezone.diskbrowser.infocom.Instruction.OperandType;
|
||||
|
||||
class Routine extends InfocomAbstractFile
|
||||
implements Iterable<Instruction>, Comparable<Routine>
|
||||
{
|
||||
private static final String padding = " ";
|
||||
|
||||
int startPtr, length, strings, locals;
|
||||
// private final Header header;
|
||||
|
||||
List<Parameter> parameters = new ArrayList<Parameter> ();
|
||||
List<Instruction> instructions = new ArrayList<Instruction> ();
|
||||
@ -21,11 +23,13 @@ class Routine extends InfocomAbstractFile
|
||||
public Routine (int ptr, Header header, int caller)
|
||||
{
|
||||
super (String.format ("Routine %05X", ptr), header.buffer);
|
||||
// this.header = header;
|
||||
|
||||
locals = buffer[ptr] & 0xFF;
|
||||
if (locals > 15)
|
||||
{
|
||||
System.out.println ("Too many locals: " + locals);
|
||||
return;
|
||||
}
|
||||
|
||||
startPtr = ptr++; // also used to flag a valid routine
|
||||
calledBy.add (caller);
|
||||
@ -53,6 +57,10 @@ class Routine extends InfocomAbstractFile
|
||||
if (instruction.isPrint ())
|
||||
strings++;
|
||||
|
||||
for (Operand operand : instruction.opcode.operands)
|
||||
if (operand.operandType == OperandType.VAR_GLOBAL)
|
||||
header.globals.addRoutine (this, operand);
|
||||
|
||||
ptr += instruction.length ();
|
||||
|
||||
// is it a backwards jump?
|
||||
|
@ -20,9 +20,9 @@ class StringManager extends AbstractFile
|
||||
int max = header.fileLength;
|
||||
while (ptr < max)
|
||||
{
|
||||
ZString zs = new ZString (buffer, ptr, header);
|
||||
ZString zs = new ZString (header, ptr);
|
||||
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);
|
||||
ptr += zs.length;
|
||||
}
|
||||
@ -47,7 +47,7 @@ class StringManager extends AbstractFile
|
||||
int count = 0;
|
||||
text.append (" # Start String\n");
|
||||
text.append ("--- ----- --------------------------------------------------------"
|
||||
+ "-------------------\n");
|
||||
+ "-------------------\n");
|
||||
|
||||
for (ZString s : strings.values ())
|
||||
{
|
||||
|
@ -7,26 +7,28 @@ import java.util.List;
|
||||
import com.bytezone.diskbrowser.applefile.AbstractFile;
|
||||
import com.bytezone.diskbrowser.utilities.HexFormatter;
|
||||
|
||||
class ZObject extends AbstractFile
|
||||
class ZObject extends AbstractFile implements Comparable<ZObject>
|
||||
{
|
||||
static final int HEADER_SIZE = 9;
|
||||
|
||||
int id;
|
||||
int startPtr;
|
||||
int propertyTablePtr;
|
||||
int propertyTableLength;
|
||||
int parent, sibling, child;
|
||||
List<Property> properties = new ArrayList<Property> ();
|
||||
Header header;
|
||||
BitSet attributes = new BitSet (32);
|
||||
private final Header header;
|
||||
private final int id;
|
||||
private final int startPtr;
|
||||
|
||||
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);
|
||||
|
||||
this.header = header;
|
||||
this.startPtr = offset;
|
||||
id = seq;
|
||||
this.id = id;
|
||||
|
||||
// attributes
|
||||
int bitIndex = 0;
|
||||
@ -51,19 +53,24 @@ class ZObject extends AbstractFile
|
||||
propertyTablePtr = header.getWord (offset + 7);
|
||||
int ptr = propertyTablePtr;
|
||||
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;
|
||||
|
||||
// read each property
|
||||
while (buffer[ptr] != 0)
|
||||
{
|
||||
Property p = new Property (buffer, ptr);
|
||||
Property p = new Property (ptr);
|
||||
properties.add (p);
|
||||
ptr += p.length + 1;
|
||||
}
|
||||
propertyTableLength = ptr - propertyTablePtr;
|
||||
}
|
||||
|
||||
int getId ()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText ()
|
||||
{
|
||||
@ -71,9 +78,9 @@ class ZObject extends AbstractFile
|
||||
|
||||
text.append (String.format ("ID : %02X (%<3d) %s%n%n", id, name));
|
||||
|
||||
String obj1 = parent == 0 ? "" : header.objectManager.list.get (parent - 1).name;
|
||||
String obj2 = sibling == 0 ? "" : header.objectManager.list.get (sibling - 1).name;
|
||||
String obj3 = child == 0 ? "" : header.objectManager.list.get (child - 1).name;
|
||||
String obj1 = parent == 0 ? "" : header.getObject (parent - 1).name;
|
||||
String obj2 = sibling == 0 ? "" : header.getObject (sibling - 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 ("Sibling : %02X (%<3d) %s%n", sibling, obj2));
|
||||
@ -140,7 +147,7 @@ class ZObject extends AbstractFile
|
||||
int length;
|
||||
int offset; // only used if length == 2
|
||||
|
||||
public Property (byte[] buffer, int ptr)
|
||||
public Property (int ptr)
|
||||
{
|
||||
this.ptr = ptr;
|
||||
length = header.getByte (ptr) / 32 + 1;
|
||||
@ -152,41 +159,53 @@ class ZObject extends AbstractFile
|
||||
|
||||
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
|
||||
public String toString ()
|
||||
{
|
||||
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 (
|
||||
String.format ("%-20s", HexFormatter.getHexString (buffer, ptr + 1, length)));
|
||||
|
||||
if (propertyType.charAt (0) >= 'a') // directions are in lowercase
|
||||
if (propertyNumber >= 19) // directions
|
||||
{
|
||||
switch (length)
|
||||
{
|
||||
case 1:
|
||||
case 1: // UEXIT - unconditional exit
|
||||
text.append (getObject ().name);
|
||||
break;
|
||||
case 2:
|
||||
text.append ("\"" + header.stringManager.stringAt (offset) + "\"");
|
||||
break;
|
||||
case 3: // executable routine
|
||||
case 3: // FEXIT - function exit
|
||||
int address = header.getWord (ptr + 1) * 2;
|
||||
text.append (String.format ("R:%05X", address));
|
||||
appendRoutine (text, address);
|
||||
break;
|
||||
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;
|
||||
if (address > 0)
|
||||
text.append ("\"" + header.stringManager.stringAt (address) + "\"");
|
||||
break;
|
||||
case 5:
|
||||
text.append (String.format ("%s : IF G%02X ", getObject ().name,
|
||||
header.getByte (ptr + 2)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -196,7 +215,7 @@ class ZObject extends AbstractFile
|
||||
for (int i = 1; i <= length; i += 2)
|
||||
{
|
||||
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);
|
||||
@ -211,6 +230,33 @@ class ZObject extends AbstractFile
|
||||
text.append (String.format ("(%4X) \"%s\"", 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 ();
|
||||
}
|
||||
@ -224,4 +270,10 @@ class ZObject extends AbstractFile
|
||||
text.append ("\n\n****** null routine\n");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo (ZObject o)
|
||||
{
|
||||
return this.name.compareTo (o.name);
|
||||
}
|
||||
}
|
@ -2,155 +2,157 @@ package com.bytezone.diskbrowser.infocom;
|
||||
|
||||
class ZString
|
||||
{
|
||||
private static String[] letters =
|
||||
{ " abcdefghijklmnopqrstuvwxyz", " ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
" 0123456789.,!?_#\'\"/\\-:()" };
|
||||
String value;
|
||||
Header header;
|
||||
int startPtr;
|
||||
int length;
|
||||
private static String[] letters =
|
||||
{ " abcdefghijklmnopqrstuvwxyz", " ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
" 0123456789.,!?_#\'\"/\\-:()" };
|
||||
String value;
|
||||
Header header;
|
||||
int startPtr;
|
||||
int length;
|
||||
|
||||
public ZString (byte[] buffer, int offset, Header header)
|
||||
{
|
||||
ZStringBuilder text = new ZStringBuilder ();
|
||||
this.header = header;
|
||||
this.startPtr = offset;
|
||||
public ZString (Header header, int offset)
|
||||
{
|
||||
ZStringBuilder text = new ZStringBuilder ();
|
||||
this.header = header;
|
||||
this.startPtr = offset;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (offset >= header.buffer.length - 1)
|
||||
{
|
||||
System.out.println ("********" + text.toString ());
|
||||
break;
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
if (offset >= header.buffer.length - 1)
|
||||
{
|
||||
System.out.println ("********" + text.toString ());
|
||||
break;
|
||||
}
|
||||
|
||||
// get the next two bytes
|
||||
int val = header.getWord (offset);
|
||||
// get the next two bytes
|
||||
int val = header.getWord (offset);
|
||||
|
||||
// process each zChar as a 5-bit value
|
||||
text.processZChar ((byte) ((val >> 10) & 0x1F));
|
||||
text.processZChar ((byte) ((val >> 5) & 0x1F));
|
||||
text.processZChar ((byte) (val & 0x1F));
|
||||
// process each zChar as a 5-bit value
|
||||
text.processZChar ((byte) ((val >>> 10) & 0x1F));
|
||||
text.processZChar ((byte) ((val >>> 5) & 0x1F));
|
||||
text.processZChar ((byte) (val & 0x1F));
|
||||
|
||||
if ((val & 0x8000) > 0) // bit 15 = finished flag
|
||||
{
|
||||
length = offset - startPtr + 2;
|
||||
value = text.toString ();
|
||||
break;
|
||||
}
|
||||
offset += 2;
|
||||
}
|
||||
}
|
||||
if ((val & 0x8000) != 0) // bit 15 = finished flag
|
||||
{
|
||||
length = offset - startPtr + 2;
|
||||
value = text.toString ();
|
||||
break;
|
||||
}
|
||||
offset += 2;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
private class ZStringBuilder
|
||||
{
|
||||
int alphabet;
|
||||
boolean shift;
|
||||
int shiftAlphabet;
|
||||
int synonym;
|
||||
int buildingLevel;
|
||||
int builtLetter;
|
||||
StringBuilder text = new StringBuilder ();
|
||||
private class ZStringBuilder
|
||||
{
|
||||
int alphabet;
|
||||
boolean shift;
|
||||
int shiftAlphabet;
|
||||
int synonym;
|
||||
int buildingLevel;
|
||||
int builtLetter;
|
||||
StringBuilder text = new StringBuilder ();
|
||||
|
||||
private void processZChar (byte zchar)
|
||||
{
|
||||
// A flag to indicate that we are building a character not in the alphabet. The
|
||||
// value indicates which half of the character the current zchar goes into. Once
|
||||
// both halves are full, we use the ascii value in builtLetter.
|
||||
if (buildingLevel > 0)
|
||||
{
|
||||
builtLetter = (short) ((builtLetter << 5) | zchar);
|
||||
if (++buildingLevel == 3)
|
||||
{
|
||||
text.append ((char) builtLetter);
|
||||
buildingLevel = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
private void processZChar (byte zchar)
|
||||
{
|
||||
// A flag to indicate that we are building a character not in the alphabet. The
|
||||
// value indicates which half of the character the current zchar goes into. Once
|
||||
// both halves are full, we use the ascii value in builtLetter.
|
||||
if (buildingLevel > 0)
|
||||
{
|
||||
builtLetter = (short) ((builtLetter << 5) | zchar);
|
||||
if (++buildingLevel == 3)
|
||||
{
|
||||
text.append ((char) builtLetter);
|
||||
buildingLevel = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
// within that block.
|
||||
if (synonym > 0)
|
||||
{
|
||||
text.append (header.getAbbreviation ((synonym - 1) * 32 + zchar));
|
||||
synonym = 0;
|
||||
return;
|
||||
}
|
||||
// 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 within that block.
|
||||
if (synonym > 0)
|
||||
{
|
||||
text.append (header.getAbbreviation ((synonym - 1) * 32 + zchar));
|
||||
synonym = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// this should be in the switch
|
||||
if ((shift && shiftAlphabet == 2) || (!shift && alphabet == 2))
|
||||
{
|
||||
if (zchar == 6)
|
||||
{
|
||||
buildingLevel = 1;
|
||||
builtLetter = 0;
|
||||
shift = false;
|
||||
return;
|
||||
}
|
||||
if (zchar == 7)
|
||||
{
|
||||
text.append ("\n");
|
||||
shift = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
if ((shift && shiftAlphabet == 2) || (!shift && alphabet == 2))
|
||||
{
|
||||
if (zchar == 6)
|
||||
{
|
||||
buildingLevel = 1;
|
||||
builtLetter = 0;
|
||||
shift = false;
|
||||
return;
|
||||
}
|
||||
if (zchar == 7)
|
||||
{
|
||||
text.append ("\n");
|
||||
shift = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// zChar values 0-5 have special meanings, and 6-7 are special only in alphabet #2.
|
||||
// Otherwise it's just a straight lookup into the current alphabet.
|
||||
switch (zchar)
|
||||
{
|
||||
case 0:
|
||||
text.append (" ");
|
||||
shift = false;
|
||||
return;
|
||||
case 1:
|
||||
synonym = zchar;
|
||||
return;
|
||||
case 2:
|
||||
case 3:
|
||||
if (header.version >= 3)
|
||||
{
|
||||
synonym = zchar;
|
||||
return;
|
||||
}
|
||||
// version 1 or 2
|
||||
shiftAlphabet = (alphabet + zchar - 1) % 3;
|
||||
shift = true;
|
||||
return;
|
||||
case 4:
|
||||
case 5:
|
||||
if (header.version >= 3) // shift key
|
||||
{
|
||||
shiftAlphabet = zchar - 3;
|
||||
shift = true;
|
||||
}
|
||||
else
|
||||
// shift lock key
|
||||
alphabet = (alphabet + zchar - 3) % 3;
|
||||
return;
|
||||
default:
|
||||
if (shift)
|
||||
{
|
||||
text.append (letters[shiftAlphabet].charAt (zchar));
|
||||
shift = false;
|
||||
}
|
||||
else
|
||||
text.append (letters[alphabet].charAt (zchar));
|
||||
return;
|
||||
}
|
||||
}
|
||||
// zChar values 0-5 have special meanings, and 6-7 are special only in alphabet #2.
|
||||
// Otherwise it's just a straight lookup into the current alphabet.
|
||||
switch (zchar)
|
||||
{
|
||||
case 0:
|
||||
text.append (" ");
|
||||
shift = false;
|
||||
return;
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return text.toString ();
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
synonym = zchar;
|
||||
return;
|
||||
|
||||
case 2:
|
||||
case 3:
|
||||
if (header.version >= 3)
|
||||
{
|
||||
synonym = zchar;
|
||||
return;
|
||||
}
|
||||
// version 1 or 2
|
||||
shiftAlphabet = (alphabet + zchar - 1) % 3;
|
||||
shift = true;
|
||||
return;
|
||||
|
||||
case 4:
|
||||
case 5:
|
||||
if (header.version >= 3) // shift key
|
||||
{
|
||||
shiftAlphabet = zchar - 3;
|
||||
shift = true;
|
||||
}
|
||||
else // shift lock key
|
||||
alphabet = (alphabet + zchar - 3) % 3;
|
||||
return;
|
||||
|
||||
default:
|
||||
if (shift)
|
||||
{
|
||||
text.append (letters[shiftAlphabet].charAt (zchar));
|
||||
shift = false;
|
||||
}
|
||||
else
|
||||
text.append (letters[alphabet].charAt (zchar));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString ()
|
||||
{
|
||||
return text.toString ();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user