From 9104389d2287a61a4c2d8f1f9f4e210f1ab81874 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Tue, 9 Aug 2016 14:15:44 +1000 Subject: [PATCH] Wiz4 first stage complete --- .../diskbrowser/applefile/PascalCode.java | 8 +- .../applefile/PascalProcedure.java | 2 +- .../diskbrowser/applefile/PascalSegment.java | 81 +++++++++---------- .../diskbrowser/applefile/Relocator.java | 46 ++++++++--- .../bytezone/diskbrowser/disk/AppleDisk.java | 16 ++-- src/com/bytezone/diskbrowser/disk/Disk.java | 2 +- .../diskbrowser/disk/DiskFactory.java | 16 ++-- .../diskbrowser/pascal/FileEntry.java | 64 +++++++-------- .../diskbrowser/pascal/PascalDisk.java | 57 ++++--------- .../wizardry/Wizardry4BootDisk.java | 24 +++++- 10 files changed, 163 insertions(+), 153 deletions(-) diff --git a/src/com/bytezone/diskbrowser/applefile/PascalCode.java b/src/com/bytezone/diskbrowser/applefile/PascalCode.java index 3885840..d5b1481 100755 --- a/src/com/bytezone/diskbrowser/applefile/PascalCode.java +++ b/src/com/bytezone/diskbrowser/applefile/PascalCode.java @@ -22,7 +22,7 @@ public class PascalCode extends AbstractFile PascalConstants.mnemonics[i], PascalConstants.descriptions[i]); } - public PascalCode (String name, byte[] buffer, int blockOffset, Relocator relocator) + public PascalCode (String name, byte[] buffer, int blockOffset) { super (name, buffer); @@ -31,8 +31,8 @@ public class PascalCode extends AbstractFile throw new FileFormatException ("Error in PascalSegment"); // this.blockOffset = blockOffset; // this.relocator = relocator; - if (relocator != null) - relocator.getMultiDiskAddress ("SEG-DIC", blockOffset, 1); + // if (relocator != null) + // relocator.getMultiDiskAddress ("SEG-DIC", blockOffset, 1); int nonameCounter = 0; @@ -47,7 +47,7 @@ public class PascalCode extends AbstractFile { // this could throw an exception PascalSegment pascalSegment = - new PascalSegment (codeName, buffer, i, blockOffset, relocator); + new PascalSegment (codeName, buffer, i, blockOffset); segments.add (pascalSegment); } } diff --git a/src/com/bytezone/diskbrowser/applefile/PascalProcedure.java b/src/com/bytezone/diskbrowser/applefile/PascalProcedure.java index 5d9ace3..46672e7 100755 --- a/src/com/bytezone/diskbrowser/applefile/PascalProcedure.java +++ b/src/com/bytezone/diskbrowser/applefile/PascalProcedure.java @@ -70,7 +70,7 @@ public class PascalProcedure // System.out.printf ("ptr:%d, max:%d, buf:%d %n", ptr, max, buffer.length); if (ptr >= buffer.length || ptr < 0) { - System.out.println ("Ptr outside buffer"); + System.out.printf ("Ptr outside buffer: %d %d%n", ptr, buffer.length); break; } PascalCodeStatement cs = new PascalCodeStatement (buffer, ptr, procOffset); diff --git a/src/com/bytezone/diskbrowser/applefile/PascalSegment.java b/src/com/bytezone/diskbrowser/applefile/PascalSegment.java index 1948f67..e3d0de5 100755 --- a/src/com/bytezone/diskbrowser/applefile/PascalSegment.java +++ b/src/com/bytezone/diskbrowser/applefile/PascalSegment.java @@ -13,7 +13,7 @@ public class PascalSegment extends AbstractFile implements PascalConstants final int segmentNoHeader; private int segmentNoBody; private final int blockOffset; - private final Relocator relocator; + // private final Relocator relocator; public int blockNo; // public int newBlockNo; @@ -30,14 +30,13 @@ public class PascalSegment extends AbstractFile implements PascalConstants private List procedures; private List addresses; - public PascalSegment (String name, byte[] fullBuffer, int seq, int blockOffset, - Relocator relocator) + public PascalSegment (String name, byte[] fullBuffer, int seq, int blockOffset) { super (name, fullBuffer); // sets this.buffer to the full buffer temporarily this.slot = seq; this.blockOffset = blockOffset; - this.relocator = relocator; + // this.relocator = relocator; this.blockNo = HexFormatter.intValue (fullBuffer[seq * 4], fullBuffer[seq * 4 + 1]); this.size = HexFormatter.intValue (fullBuffer[seq * 4 + 2], fullBuffer[seq * 4 + 3]); @@ -68,23 +67,23 @@ public class PascalSegment extends AbstractFile implements PascalConstants int offset = blockNo * 512; - if (relocator != null) - { - // if (segmentNoHeader > 1) - // { - int sizeInBlocks = (size - 1) / BLOCK_SIZE + 1; - int targetBlock = blockNo + blockOffset; - addresses = relocator.getMultiDiskAddress (name, targetBlock, sizeInBlocks); - if (addresses.size () > 0) - { - MultiDiskAddress multiDiskAddress = addresses.get (0); - if (multiDiskAddress.diskNumber == 1) - offset = (multiDiskAddress.physicalBlockNumber - blockOffset) * BLOCK_SIZE; - else - offset = -1; - } - // } - } + // if (relocator != null) + // { + // // if (segmentNoHeader > 1) + // // { + // int sizeInBlocks = (size - 1) / BLOCK_SIZE + 1; + // int targetBlock = blockNo + blockOffset; + // addresses = relocator.getMultiDiskAddress (name, targetBlock, sizeInBlocks); + // if (addresses.size () > 0) + // { + // MultiDiskAddress multiDiskAddress = addresses.get (0); + // if (multiDiskAddress.diskNumber == 1) + // offset = (multiDiskAddress.physicalBlockNumber - blockOffset) * BLOCK_SIZE; + // else + // offset = -1; + // } + // // } + // } if (offset < 0) { @@ -204,26 +203,26 @@ public class PascalSegment extends AbstractFile implements PascalConstants // multiDiskAddressText = String.format ("1:%03X", (blockNo + blockOffset)); // } // else - if (relocator != null) - { - int targetBlock = blockNo + blockOffset; - List addresses = - relocator.getMultiDiskAddress (name, targetBlock, sizeInBlocks); - if (addresses.isEmpty ()) - multiDiskAddressText = "."; - else - { - StringBuilder locations = new StringBuilder (); - for (MultiDiskAddress multiDiskAddress : addresses) - locations.append (multiDiskAddress.toString () + ", "); - if (locations.length () > 2) - { - locations.deleteCharAt (locations.length () - 1); - locations.deleteCharAt (locations.length () - 1); - } - multiDiskAddressText = locations.toString (); - } - } + // if (relocator != null) + // { + // int targetBlock = blockNo + blockOffset; + // List addresses = + // relocator.getMultiDiskAddress (name, targetBlock, sizeInBlocks); + // if (addresses.isEmpty ()) + // multiDiskAddressText = "."; + // else + // { + // StringBuilder locations = new StringBuilder (); + // for (MultiDiskAddress multiDiskAddress : addresses) + // locations.append (multiDiskAddress.toString () + ", "); + // if (locations.length () > 2) + // { + // locations.deleteCharAt (locations.length () - 1); + // locations.deleteCharAt (locations.length () - 1); + // } + // multiDiskAddressText = locations.toString (); + // } + // } return multiDiskAddressText; } } \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/applefile/Relocator.java b/src/com/bytezone/diskbrowser/applefile/Relocator.java index 0b3bc32..71ad206 100644 --- a/src/com/bytezone/diskbrowser/applefile/Relocator.java +++ b/src/com/bytezone/diskbrowser/applefile/Relocator.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import com.bytezone.diskbrowser.disk.AppleDisk; import com.bytezone.diskbrowser.disk.Disk; import com.bytezone.diskbrowser.disk.DiskAddress; import com.bytezone.diskbrowser.utilities.HexFormatter; @@ -151,6 +152,24 @@ public class Relocator extends AbstractFile return true; } + public void createNewBuffer (Disk[] dataDisks) + { + AppleDisk master = (AppleDisk) dataDisks[0]; + + for (int logicalBlock = 0; logicalBlock < diskBlocks.length; logicalBlock++) + { + int physicalBlock = diskOffsets2[logicalBlock]; + int diskNo = diskBlocks[logicalBlock]; + if (diskNo > 0) + { + Disk disk = dataDisks[diskNo]; + byte[] temp = disk.readSector (physicalBlock); + DiskAddress da = master.getDiskAddress (logicalBlock); + master.writeSector (da, temp); + } + } + } + @Override public String getText () { @@ -165,23 +184,26 @@ public class Relocator extends AbstractFile text.append ("\n"); } - int previousDiskNumber = 0; - for (MultiDiskAddress multiDiskAddress : addresses) + if (false) { - if (multiDiskAddress.diskNumber != previousDiskNumber) + int previousDiskNumber = 0; + for (MultiDiskAddress multiDiskAddress : addresses) { - previousDiskNumber = multiDiskAddress.diskNumber; - text.append ("\n"); - text.append ("Disk Logical Physical Size Name\n"); - text.append ("---- ------- -------- ---- -------------\n"); + if (multiDiskAddress.diskNumber != previousDiskNumber) + { + previousDiskNumber = multiDiskAddress.diskNumber; + text.append ("\n"); + text.append ("Disk Logical Physical Size Name\n"); + text.append ("---- ------- -------- ---- -------------\n"); + } + text.append (String.format (" %d %03X %03X %03X %s%n", + multiDiskAddress.diskNumber, multiDiskAddress.logicalBlockNumber, + multiDiskAddress.physicalBlockNumber, multiDiskAddress.totalBlocks, + multiDiskAddress.name)); } - text.append (String.format (" %d %03X %03X %03X %s%n", - multiDiskAddress.diskNumber, multiDiskAddress.logicalBlockNumber, - multiDiskAddress.physicalBlockNumber, multiDiskAddress.totalBlocks, - multiDiskAddress.name)); } - text.append ("\n\n Logical Size Disk Physical"); + text.append ("\n Logical Size Disk Physical"); text.append ("\n--------- ---- ---- ---------\n"); int first = 0; diff --git a/src/com/bytezone/diskbrowser/disk/AppleDisk.java b/src/com/bytezone/diskbrowser/disk/AppleDisk.java index 67b497e..2d9b6ca 100755 --- a/src/com/bytezone/diskbrowser/disk/AppleDisk.java +++ b/src/com/bytezone/diskbrowser/disk/AppleDisk.java @@ -381,10 +381,11 @@ public class AppleDisk implements Disk } @Override - public int writeSector (DiskAddress da, byte[] buffer) + public void writeSector (DiskAddress da, byte[] buffer) { - System.out.println ("Not yet implemented"); - return -1; + // System.out.println ("Not yet implemented"); + // return -1; + writeBuffer (da, buffer); } @Override @@ -507,7 +508,7 @@ public class AppleDisk implements Disk } } - private void writeBuffer (DiskAddress da, byte[] buffer, int bufferOffset) + private void writeBuffer (DiskAddress da, byte[] buffer) { assert da.getDisk () == this : "Disk address not applicable to this disk"; assert sectorSize == SECTOR_SIZE @@ -518,16 +519,15 @@ public class AppleDisk implements Disk if (sectorSize == SECTOR_SIZE) { int diskOffset = getBufferOffset (da); - System.arraycopy (buffer, bufferOffset, diskBuffer, diskOffset, SECTOR_SIZE); + System.arraycopy (buffer, 0, diskBuffer, diskOffset, SECTOR_SIZE); } else { int diskOffset = getBufferOffset (da, 0); - System.arraycopy (buffer, bufferOffset, diskBuffer, diskOffset, SECTOR_SIZE); + System.arraycopy (buffer, 0, diskBuffer, diskOffset, SECTOR_SIZE); diskOffset = getBufferOffset (da, 1); - System.arraycopy (buffer, bufferOffset + SECTOR_SIZE, diskBuffer, diskOffset, - SECTOR_SIZE); + System.arraycopy (buffer, SECTOR_SIZE, diskBuffer, diskOffset, SECTOR_SIZE); } } diff --git a/src/com/bytezone/diskbrowser/disk/Disk.java b/src/com/bytezone/diskbrowser/disk/Disk.java index a2e2638..1a8bce2 100755 --- a/src/com/bytezone/diskbrowser/disk/Disk.java +++ b/src/com/bytezone/diskbrowser/disk/Disk.java @@ -40,7 +40,7 @@ public interface Disk extends Iterable public byte[] readSectors (List daList); - public int writeSector (DiskAddress da, byte[] buffer); + public void writeSector (DiskAddress da, byte[] buffer); public boolean isSectorEmpty (DiskAddress da); diff --git a/src/com/bytezone/diskbrowser/disk/DiskFactory.java b/src/com/bytezone/diskbrowser/disk/DiskFactory.java index 79828df..598f2f5 100755 --- a/src/com/bytezone/diskbrowser/disk/DiskFactory.java +++ b/src/com/bytezone/diskbrowser/disk/DiskFactory.java @@ -390,13 +390,15 @@ public class DiskFactory if (Wizardry4BootDisk.isWizardryIV (disk, debug)) { - // collect 4 data disks - AppleDisk[] disks = new AppleDisk[5]; - AppleDisk d = new AppleDisk (file, 256, 8); - d.setInterleave (1); - disks[0] = d; + // collect 4 extra data disks + AppleDisk[] disks = new AppleDisk[6]; - for (int i = 2; i <= 5; i++) + disks[0] = new AppleDisk (file, 256, 8); // will become a PascalDisk + disks[1] = new AppleDisk (file, 256, 8); // will remain a DataDisk + disks[0].setInterleave (1); + disks[1].setInterleave (1); + + for (int i = 2; i < disks.length; i++) { String filename = file.getAbsolutePath ().replace ("1.dsk", i + ".dsk"); File f = new File (filename); @@ -404,7 +406,7 @@ public class DiskFactory { AppleDisk dataDisk = new AppleDisk (f, 35, 8); dataDisk.setInterleave (1); - disks[i - 1] = dataDisk; + disks[i] = dataDisk; } else { diff --git a/src/com/bytezone/diskbrowser/pascal/FileEntry.java b/src/com/bytezone/diskbrowser/pascal/FileEntry.java index 9ad80ba..fb901a3 100644 --- a/src/com/bytezone/diskbrowser/pascal/FileEntry.java +++ b/src/com/bytezone/diskbrowser/pascal/FileEntry.java @@ -1,35 +1,26 @@ package com.bytezone.diskbrowser.pascal; +import javax.swing.tree.DefaultMutableTreeNode; + import com.bytezone.diskbrowser.applefile.*; -import com.bytezone.diskbrowser.disk.DiskAddress; import com.bytezone.diskbrowser.utilities.FileFormatException; import com.bytezone.diskbrowser.utilities.HexFormatter; public class FileEntry extends CatalogEntry { int bytesUsedInLastBlock; - private final Relocator relocator; + private DefaultMutableTreeNode node; + AbstractFile file; - public FileEntry (PascalDisk parent, byte[] buffer, Relocator relocator) + public FileEntry (PascalDisk parent, byte[] buffer) { super (parent, buffer); - this.relocator = relocator; bytesUsedInLastBlock = HexFormatter.intValue (buffer[22], buffer[23]); date = HexFormatter.getPascalDate (buffer, 24); - if (relocator != null) - { - int size = lastBlock - firstBlock; - // System.out.printf ("%04X %04X %s%n", firstBlock, size, name); - relocator.getMultiDiskAddress (name, firstBlock, size); - } - for (int i = firstBlock; i < lastBlock; i++) { - if (i >= 280) - break; - switch (fileType) { case 2: @@ -64,14 +55,22 @@ public class FileEntry extends CatalogEntry } } + void setNode (DefaultMutableTreeNode node) + { + this.node = node; + } + + public void setFile (AbstractFile file) + { + this.file = file; + } + @Override public AbstractFile getDataSource () { if (file != null) return file; - // this needs to use the Relocator to obtain the currect blocks - assembleBuffer (); byte[] buffer = getExactBuffer (); switch (fileType) @@ -79,7 +78,16 @@ public class FileEntry extends CatalogEntry case 2: // code (6502 or Pascal) try { - file = new PascalCode (name, buffer, firstBlock, relocator); + file = new PascalCode (name, buffer, firstBlock); + node.removeAllChildren (); + + for (PascalSegment pascalSegment : (PascalCode) file) + { + DefaultMutableTreeNode segmentNode = new DefaultMutableTreeNode ( + new PascalCodeObject (parent, pascalSegment, firstBlock)); + node.add (segmentNode); + segmentNode.setAllowsChildren (false); + } } catch (FileFormatException e) { @@ -103,8 +111,8 @@ public class FileEntry extends CatalogEntry file = new Charset (name, buffer); // else if (name.equals ("WT")) // only testing // file = new WizardryTitle (name, buffer); - else if (name.equals ("SYSTEM.RELOC")) - file = new Relocator (name, buffer); + // else if (name.equals ("SYSTEM.RELOC")) + // file = new Relocator (name, buffer); else file = new DefaultAppleFile (name, buffer); break; @@ -119,27 +127,11 @@ public class FileEntry extends CatalogEntry return file; } - private byte[] assembleBuffer () - { - if (relocator != null) - for (DiskAddress da : blocks) - { - System.out.println (da); - if (da.getBlock () < 20) - { - byte[] buffer = relocator.getLogicalBuffer (da); - System.out.println (HexFormatter.format (buffer)); - } - } - - return null; - } - - // use Relocator to obtain correct blocks private byte[] getExactBuffer () { byte[] buffer = parent.getDisk ().readSectors (blocks); byte[] exactBuffer; + // System.out.println (HexFormatter.format (buffer)); if (buffer.length > 0 && bytesUsedInLastBlock < 512) { diff --git a/src/com/bytezone/diskbrowser/pascal/PascalDisk.java b/src/com/bytezone/diskbrowser/pascal/PascalDisk.java index f34dff6..d7fb3b9 100755 --- a/src/com/bytezone/diskbrowser/pascal/PascalDisk.java +++ b/src/com/bytezone/diskbrowser/pascal/PascalDisk.java @@ -10,8 +10,6 @@ import javax.swing.tree.DefaultMutableTreeNode; import com.bytezone.diskbrowser.applefile.AppleFileSource; import com.bytezone.diskbrowser.applefile.BootSector; -import com.bytezone.diskbrowser.applefile.PascalCode; -import com.bytezone.diskbrowser.applefile.PascalSegment; import com.bytezone.diskbrowser.applefile.Relocator; import com.bytezone.diskbrowser.disk.*; import com.bytezone.diskbrowser.gui.DataSource; @@ -21,11 +19,10 @@ public class PascalDisk extends AbstractFormattedDisk { static final int CATALOG_ENTRY_SIZE = 26; private final DateFormat df = DateFormat.getDateInstance (DateFormat.SHORT); - private VolumeEntry volumeEntry; - private PascalCatalogSector diskCatalogSector; + private final VolumeEntry volumeEntry; + private final PascalCatalogSector diskCatalogSector; protected Relocator relocator; - protected Disk[] dataDisks; final String[] fileTypes = { "Volume", "Xdsk", "Code", "Text", "Info", "Data", "Graf", "Foto", "SecureDir" }; @@ -39,26 +36,10 @@ public class PascalDisk extends AbstractFormattedDisk SectorType grafSector = new SectorType ("Graf", Color.cyan); SectorType fotoSector = new SectorType ("Foto", Color.gray); - public PascalDisk (Disk[] disks) - { - super (disks[0]); - init (); - - dataDisks = disks; // these need to be in place before ... - - // 1. create new disk buffer - // 2. copy sectors from 5 data disks to new buffer - // 3. replace old disk buffer with new disk buffer - } - public PascalDisk (Disk disk) { super (disk); - init (); - } - private void init () - { sectorTypesList.add (diskBootSector); sectorTypesList.add (catalogSector); sectorTypesList.add (dataSector); @@ -117,32 +98,24 @@ public class PascalDisk extends AbstractFormattedDisk data = new byte[CATALOG_ENTRY_SIZE]; System.arraycopy (buffer, ptr, data, 0, CATALOG_ENTRY_SIZE); - FileEntry fileEntry = new FileEntry (this, data, relocator); + FileEntry fileEntry = new FileEntry (this, data); fileEntries.add (fileEntry); DefaultMutableTreeNode node = new DefaultMutableTreeNode (fileEntry); + fileEntry.setNode (node); - // why is this being called? it creates every file on the disk - DataSource dataSource = fileEntry.getDataSource (); // reads all buffers - - if (fileEntry.fileType == 5 && dataSource instanceof Relocator) - { - relocator = (Relocator) dataSource; - relocator.setDisks (dataDisks); - int size = fileEntry.lastBlock - fileEntry.firstBlock; - relocator.getMultiDiskAddress (fileEntry.name, fileEntry.firstBlock, size); - } - - if (fileEntry.fileType == 2 && dataSource instanceof PascalCode) + if (fileEntry.fileType == 2)// && dataSource instanceof PascalCode) { node.setAllowsChildren (true); - PascalCode pascalCode = (PascalCode) dataSource; - for (PascalSegment pascalSegment : pascalCode) - { - DefaultMutableTreeNode segmentNode = new DefaultMutableTreeNode ( - new PascalCodeObject (this, pascalSegment, fileEntry.firstBlock)); - node.add (segmentNode); - segmentNode.setAllowsChildren (false); - } + // this does not trigger correctly when opening the arrow + + // PascalCode pascalCode = (PascalCode) dataSource; + // for (PascalSegment pascalSegment : pascalCode) + // { + // DefaultMutableTreeNode segmentNode = new DefaultMutableTreeNode ( + // new PascalCodeObject (this, pascalSegment, fileEntry.firstBlock)); + // node.add (segmentNode); + // segmentNode.setAllowsChildren (false); + // } } else node.setAllowsChildren (false); diff --git a/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java b/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java index e29a3cf..6fb14e2 100644 --- a/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java +++ b/src/com/bytezone/diskbrowser/wizardry/Wizardry4BootDisk.java @@ -3,18 +3,40 @@ package com.bytezone.diskbrowser.wizardry; import java.util.ArrayList; import java.util.List; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeModel; + +import com.bytezone.diskbrowser.applefile.Relocator; import com.bytezone.diskbrowser.disk.AppleDisk; import com.bytezone.diskbrowser.disk.Disk; +import com.bytezone.diskbrowser.pascal.FileEntry; import com.bytezone.diskbrowser.pascal.PascalDisk; import com.bytezone.diskbrowser.utilities.Utility; public class Wizardry4BootDisk extends PascalDisk { List disks = new ArrayList (); + protected Disk[] dataDisks; + private Relocator relocator; public Wizardry4BootDisk (AppleDisk[] dataDisks) { - super (dataDisks); + super (dataDisks[0]); + + this.dataDisks = dataDisks; + + DefaultTreeModel model = (DefaultTreeModel) catalogTree.getModel (); + DefaultMutableTreeNode currentRoot = (DefaultMutableTreeNode) model.getRoot (); + DefaultMutableTreeNode relocNode = findNode (currentRoot, "SYSTEM.RELOC"); + FileEntry fileEntry = (FileEntry) relocNode.getUserObject (); + + if (fileEntry != null) + { + relocator = + new Relocator (fileEntry.getUniqueName (), fileEntry.getDataSource ().buffer); + relocator.createNewBuffer (dataDisks); + fileEntry.setFile (relocator); + } } public static boolean isWizardryIV (Disk disk, boolean debug)