Improved support for CP/M disk images. Can now view files. There are

a few bugs remaining, however.
This commit is contained in:
Robert Greene 2003-12-22 17:24:57 +00:00
parent 24c2eee291
commit b5147523f7
2 changed files with 454 additions and 79 deletions

View File

@ -1,9 +1,11 @@
package com.webcodepro.applecommander.storage.cpm; package com.webcodepro.applecommander.storage.cpm;
import com.webcodepro.applecommander.storage.BinaryFileFilter;
import com.webcodepro.applecommander.storage.DiskFullException; import com.webcodepro.applecommander.storage.DiskFullException;
import com.webcodepro.applecommander.storage.FileEntry; import com.webcodepro.applecommander.storage.FileEntry;
import com.webcodepro.applecommander.storage.FileFilter; import com.webcodepro.applecommander.storage.FileFilter;
import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.FormattedDisk;
import com.webcodepro.applecommander.storage.TextFileFilter;
import com.webcodepro.applecommander.util.AppleUtil; import com.webcodepro.applecommander.util.AppleUtil;
import java.text.NumberFormat; import java.text.NumberFormat;
@ -11,13 +13,79 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* @author Rob * Support the CP/M file entry. Note that this may actually contain references
* to multiple file entries via the extent counter.
* <p>
* @author Rob Greene
*/ */
public class CpmFileEntry implements FileEntry { public class CpmFileEntry implements FileEntry {
/** /**
* The standard CP/M file entry length. * The standard CP/M file entry length.
*/ */
public static final int ENTRY_LENGTH = 0x20; public static final int ENTRY_LENGTH = 0x20;
/**
* The maximum number of extents per file entry record.
*/
public static final int MAX_EXTENTS_PER_ENTRY = 0x80;
/**
* The number of bytes used if all records in an extent are filled.
* (MAX_EXTENTS_PER_ENTRY * CPM_SECTOR_SIZE)
*/
public static final int ALL_RECORDS_FILLED_SIZE = 16384;
/**
* The user number (UU) field is to distinguish multiple files with the
* same filename. This appears to be primarily with deleted files?
*/
public static final int USER_NUMBER_OFFSET = 0;
/**
* Offset to beginning of the filename.
*/
public static final int FILENAME_OFFSET = 1;
/**
* Filename length (excluding extension).
*/
public static final int FILENAME_LENGTH = 8;
/**
* Offset to beginning of the filetype.
*/
public static final int FILETYPE_OFFSET = 9;
/**
* Filetype length.
*/
public static final int FILETYPE_LENGTH = 3;
/**
* Offset to the filetype "T1" entry.
* Indicates read-only.
*/
public static final int FILETYPE_T1_OFFSET = FILETYPE_OFFSET;
/**
* Offset to the filetype "T2" entry.
* Indicates system or hidden file.
*/
public static final int FILETYPE_T2_OFFSET = FILETYPE_OFFSET+1;
/**
* Offset to the filetype "T3" entry.
* Backup bit (CP/M 3.1 and later).
*/
public static final int FILETYPE_T3_OFFSET = FILETYPE_OFFSET+2;
/**
* Offset to the extent counter (EX) field.
*/
public static final int EXTENT_COUNTER_OFFSET = 0xc;
/**
* Offset to the record count (RC) field.
*/
public static final int RECORD_COUNT_OFFSET = 0xf;
/**
* Beginning of block allocations.
*/
public static final int ALLOCATION_OFFSET = 0x10;
/**
* A short collection of known text-type files.
*/
public static final String[] TEXT_FILETYPES = {
"TXT", "ASM", "MAC", "DOC", "PRN", "PAS", "ME", "INC", "HLP"
};
/** /**
* Reference to the disk this FileEntry is attached to. * Reference to the disk this FileEntry is attached to.
*/ */
@ -46,29 +114,52 @@ public class CpmFileEntry implements FileEntry {
* Read the fileEntry bytes from the disk image. * Read the fileEntry bytes from the disk image.
*/ */
protected byte[] readFileEntry(int number) { protected byte[] readFileEntry(int number) {
byte[] data = new byte[2048]; byte[] data = new byte[2 * CpmFormatDisk.CPM_BLOCKSIZE];
System.arraycopy(disk.readCpmBlock(0), 0, data, 0, 1024); System.arraycopy(disk.readCpmBlock(0), 0, data,
System.arraycopy(disk.readCpmBlock(1), 0, data, 1024, 1024); 0, CpmFormatDisk.CPM_BLOCKSIZE);
System.arraycopy(disk.readCpmBlock(1), 0, data,
CpmFormatDisk.CPM_BLOCKSIZE, CpmFormatDisk.CPM_BLOCKSIZE);
byte[] entry = new byte[ENTRY_LENGTH]; byte[] entry = new byte[ENTRY_LENGTH];
int offset = ((Integer)offsets.get(number)).intValue(); int offset = ((Integer)offsets.get(number)).intValue();
System.arraycopy(data, offset, entry, 0, ENTRY_LENGTH); System.arraycopy(data, offset, entry, 0, ENTRY_LENGTH);
return entry; return entry;
} }
/**
* Write the fileEntry bytes back to the disk image.
*/
protected void writeFileEntry(int number, byte[] data) {
byte[] block = new byte[CpmFormatDisk.CPM_BLOCKSIZE];
System.arraycopy(data, 0, block,
0, CpmFormatDisk.CPM_BLOCKSIZE);
disk.writeCpmBlock(0, block);
System.arraycopy(data, 0, block,
CpmFormatDisk.CPM_BLOCKSIZE, CpmFormatDisk.CPM_BLOCKSIZE);
disk.writeCpmBlock(1, block);
}
/** /**
* Answer with the name of the file. * Answer with the name of the file.
* @see com.webcodepro.applecommander.storage.FileEntry#getFilename() * @see com.webcodepro.applecommander.storage.FileEntry#getFilename()
*/ */
public String getFilename() { public String getFilename() {
return AppleUtil.getString(readFileEntry(0), 1, 8).trim(); return AppleUtil.getString(readFileEntry(0),
FILENAME_OFFSET, FILENAME_LENGTH).trim();
} }
/** /**
* Set the filename. Note that this assumes the file extension
* is completely separate and does not validate characters that
* are being set!
* @see com.webcodepro.applecommander.storage.FileEntry#setFilename(java.lang.String) * @see com.webcodepro.applecommander.storage.FileEntry#setFilename(java.lang.String)
*/ */
public void setFilename(String filename) { public void setFilename(String filename) {
// TODO Auto-generated method stub for (int i=0; i<offsets.size(); i++) {
byte[] data = readFileEntry(i);
AppleUtil.setString(data, FILENAME_OFFSET, filename,
FILENAME_LENGTH, false);
writeFileEntry(i, data);
}
} }
/** /**
@ -76,15 +167,26 @@ public class CpmFileEntry implements FileEntry {
* @see com.webcodepro.applecommander.storage.FileEntry#getFiletype() * @see com.webcodepro.applecommander.storage.FileEntry#getFiletype()
*/ */
public String getFiletype() { public String getFiletype() {
return AppleUtil.getString(readFileEntry(0), 9, 3).trim(); return AppleUtil.getString(readFileEntry(0),
FILETYPE_OFFSET, FILETYPE_LENGTH).trim();
} }
/** /**
* Set the filetype. Note that the highbits need to be preserved.
* @see com.webcodepro.applecommander.storage.FileEntry#setFiletype(java.lang.String) * @see com.webcodepro.applecommander.storage.FileEntry#setFiletype(java.lang.String)
*/ */
public void setFiletype(String filetype) { public void setFiletype(String filetype) {
// TODO Auto-generated method stub for (int i=0; i<offsets.size(); i++) {
int T1 = getFileTypeT1(i);
int T2 = getFileTypeT2(i);
int T3 = getFileTypeT3(i);
byte[] data = readFileEntry(i);
AppleUtil.setString(data, FILETYPE_OFFSET, filetype,
FILETYPE_LENGTH, false);
data[FILETYPE_OFFSET] |= (T1 > 127) ? 0x80 : 0x00;
data[FILETYPE_OFFSET+1] |= (T2 > 127) ? 0x80 : 0x00;
data[FILETYPE_OFFSET+1] |= (T3 > 127) ? 0x80 : 0x00;
}
} }
/** /**
@ -100,7 +202,16 @@ public class CpmFileEntry implements FileEntry {
* file type and the high bit indicates read-only. * file type and the high bit indicates read-only.
*/ */
public byte getFileTypeT1(int entryNumber) { public byte getFileTypeT1(int entryNumber) {
return readFileEntry(entryNumber)[0x9]; return readFileEntry(entryNumber)[FILETYPE_T1_OFFSET];
}
/**
* Write the file type T1 entry.
*/
public void setFileTypeT1(int entryNumber, int t1) {
byte[] data = readFileEntry(entryNumber);
data[FILETYPE_T1_OFFSET] = (byte) t1;
writeFileEntry(entryNumber, data);
} }
/** /**
@ -108,7 +219,16 @@ public class CpmFileEntry implements FileEntry {
* file type and the high bit indicates a system or hidden file. * file type and the high bit indicates a system or hidden file.
*/ */
public byte getFileTypeT2(int entryNumber) { public byte getFileTypeT2(int entryNumber) {
return readFileEntry(entryNumber)[0xa]; return readFileEntry(entryNumber)[FILETYPE_T2_OFFSET];
}
/**
* Write the file type T2 entry.
*/
public void setFileTypeT2(int entryNumber, int t2) {
byte[] data = readFileEntry(entryNumber);
data[FILETYPE_T2_OFFSET] = (byte) t2;
writeFileEntry(entryNumber, data);
} }
/** /**
@ -116,22 +236,38 @@ public class CpmFileEntry implements FileEntry {
* file type and the high bit is the backup bit (CP/M 3.1 and later). * file type and the high bit is the backup bit (CP/M 3.1 and later).
*/ */
public byte getFileTypeT3(int entryNumber) { public byte getFileTypeT3(int entryNumber) {
return readFileEntry(entryNumber)[0xb]; return readFileEntry(entryNumber)[FILETYPE_T3_OFFSET];
} }
/** /**
* Write the file type T3 entry.
*/
public void setFileTypeT3(int entryNumber, int t3) {
byte[] data = readFileEntry(entryNumber);
data[FILETYPE_T3_OFFSET] = (byte) t3;
writeFileEntry(entryNumber, data);
}
/**
* Set the locked status. This is interpreted as read-only.
* @see com.webcodepro.applecommander.storage.FileEntry#setLocked(boolean) * @see com.webcodepro.applecommander.storage.FileEntry#setLocked(boolean)
*/ */
public void setLocked(boolean lock) { public void setLocked(boolean lock) {
// TODO Auto-generated method stub for (int i=0; i<offsets.size(); i++) {
if (lock) {
setFileTypeT1(i, getFileTypeT1(i) | 0x80);
} else {
setFileTypeT1(i, getFileTypeT1(i) & 0x7f);
}
}
} }
/** /**
* Read the extent number, low byte. * Read the extent number, low byte.
*/ */
public int getExtentCounterLow(int entryNumber) { public int getExtentCounterLow(int entryNumber) {
return AppleUtil.getUnsignedByte(readFileEntry(entryNumber)[0xc]); return AppleUtil.getUnsignedByte(
readFileEntry(entryNumber)[EXTENT_COUNTER_OFFSET]);
} }
/** /**
@ -139,6 +275,16 @@ public class CpmFileEntry implements FileEntry {
* @see com.webcodepro.applecommander.storage.FileEntry#getSize() * @see com.webcodepro.applecommander.storage.FileEntry#getSize()
*/ */
public int getSize() { public int getSize() {
int entry = findLargestExtent();
// Compute file size:
return getExtentCounterLow(entry) * ALL_RECORDS_FILLED_SIZE +
getNumberOfRecordsUsed(entry) * CpmFormatDisk.CPM_SECTORSIZE;
}
/**
* Locate the largest extent for this file.
*/
protected int findLargestExtent() {
int entry = -1; int entry = -1;
// Locate largest extent number: // Locate largest extent number:
for (int i=0; i<offsets.size(); i++) { for (int i=0; i<offsets.size(); i++) {
@ -150,9 +296,17 @@ public class CpmFileEntry implements FileEntry {
if (thisExtent > currentExtent) entry = i; if (thisExtent > currentExtent) entry = i;
} }
} }
// Compute file size: return entry;
return getExtentCounterLow(entry) * 16384 + }
getNumberOfRecordsUsed(entry) * 128;
/**
* Compute the number of blocks used.
*/
public int getBlocksUsed() {
int entry = findLargestExtent();
return getExtentCounterLow(entry) * ALL_RECORDS_FILLED_SIZE +
(getNumberOfRecordsUsed(entry) - 1) /
CpmFormatDisk.CPM_SECTORS_PER_CPM_BLOCK + 1;
} }
/** /**
@ -160,14 +314,15 @@ public class CpmFileEntry implements FileEntry {
* 1 record = 128 bytes. * 1 record = 128 bytes.
*/ */
public int getNumberOfRecordsUsed(int entryNumber) { public int getNumberOfRecordsUsed(int entryNumber) {
return AppleUtil.getUnsignedByte(readFileEntry(entryNumber)[0xf]); return AppleUtil.getUnsignedByte(
readFileEntry(entryNumber)[RECORD_COUNT_OFFSET]);
} }
/** /**
* Apple CP/M does not support directories.
* @see com.webcodepro.applecommander.storage.FileEntry#isDirectory() * @see com.webcodepro.applecommander.storage.FileEntry#isDirectory()
*/ */
public boolean isDirectory() { public boolean isDirectory() {
// TODO Auto-generated method stub
return false; return false;
} }
@ -186,15 +341,27 @@ public class CpmFileEntry implements FileEntry {
* conjunction with deleted files. * conjunction with deleted files.
*/ */
public int getUserNumber(int entryNumber) { public int getUserNumber(int entryNumber) {
return AppleUtil.getUnsignedByte(readFileEntry(entryNumber)[0x0]); return AppleUtil.getUnsignedByte(readFileEntry(entryNumber)[USER_NUMBER_OFFSET]);
} }
/** /**
* Write the user number (UU).
*/
public void setUserNumber(int entryNumber, int userNumber) {
byte[] data = readFileEntry(entryNumber);
data[USER_NUMBER_OFFSET] = (byte) userNumber;
writeFileEntry(entryNumber, data);
}
/**
* There appears to be no disk map involved, so deleting a file consists
* of writing a 0xe5 to the user number.
* @see com.webcodepro.applecommander.storage.FileEntry#delete() * @see com.webcodepro.applecommander.storage.FileEntry#delete()
*/ */
public void delete() { public void delete() {
// TODO Auto-generated method stub for (int i=0; i<offsets.size(); i++) {
setUserNumber(i, 0xe5);
}
} }
/** /**
@ -230,27 +397,35 @@ public class CpmFileEntry implements FileEntry {
} }
/** /**
* Get file data. This handles any operating-system specific issues.
* @see com.webcodepro.applecommander.storage.FileEntry#getFileData() * @see com.webcodepro.applecommander.storage.FileEntry#getFileData()
*/ */
public byte[] getFileData() { public byte[] getFileData() {
// TODO Auto-generated method stub return disk.getFileData(this);
return null;
} }
/** /**
* Set file data. This, essentially, is saving data to disk using this
* file entry.
* @see com.webcodepro.applecommander.storage.FileEntry#setFileData(byte[]) * @see com.webcodepro.applecommander.storage.FileEntry#setFileData(byte[])
*/ */
public void setFileData(byte[] data) throws DiskFullException { public void setFileData(byte[] data) throws DiskFullException {
// TODO Auto-generated method stub // TODO CP/M format disks don't save data...
} }
/** /**
* Get the suggested FileFilter. This is a guess based on what appears to
* be text-based files.
* @see com.webcodepro.applecommander.storage.FileEntry#getSuggestedFilter() * @see com.webcodepro.applecommander.storage.FileEntry#getSuggestedFilter()
*/ */
public FileFilter getSuggestedFilter() { public FileFilter getSuggestedFilter() {
// TODO Auto-generated method stub String filetype = getFiletype();
return null; for (int i=0; i<TEXT_FILETYPES.length; i++) {
if (TEXT_FILETYPES[i].equals(filetype)) {
return new TextFileFilter();
}
}
return new BinaryFileFilter();
} }
/** /**
@ -270,26 +445,27 @@ public class CpmFileEntry implements FileEntry {
} }
/** /**
* Indicates if this filetype requires an address component.
* @see com.webcodepro.applecommander.storage.FileEntry#needsAddress() * @see com.webcodepro.applecommander.storage.FileEntry#needsAddress()
*/ */
public boolean needsAddress() { public boolean needsAddress() {
// TODO Auto-generated method stub return disk.needsAddress(getFiletype());
return false;
} }
/** /**
* Set the address that this file loads at.
* @see com.webcodepro.applecommander.storage.FileEntry#setAddress(int) * @see com.webcodepro.applecommander.storage.FileEntry#setAddress(int)
*/ */
public void setAddress(int address) { public void setAddress(int address) {
// TODO Auto-generated method stub // not applicable
} }
/** /**
* Indicates that this filetype can be compiled.
* AppleCommander cannot do much with CP/M files.
* @see com.webcodepro.applecommander.storage.FileEntry#canCompile() * @see com.webcodepro.applecommander.storage.FileEntry#canCompile()
*/ */
public boolean canCompile() { public boolean canCompile() {
// TODO Auto-generated method stub
return false; return false;
} }
@ -305,4 +481,21 @@ public class CpmFileEntry implements FileEntry {
} }
return true; return true;
} }
/**
* Answer with a list of blocks allocated to this file.
*/
public int[] getAllocations() {
int blocks = getBlocksUsed();
int[] allocations = new int[blocks];
int block = 0;
for (int i=0; i<offsets.size(); i++) {
byte[] data = readFileEntry(i);
int offset = ALLOCATION_OFFSET;
while (block < blocks && offset < ENTRY_LENGTH) {
allocations[block++] = AppleUtil.getUnsignedByte(data[offset++]);
}
}
return allocations;
}
} }

View File

@ -22,12 +22,12 @@ package com.webcodepro.applecommander.storage.cpm;
import com.webcodepro.applecommander.storage.DiskFullException; import com.webcodepro.applecommander.storage.DiskFullException;
import com.webcodepro.applecommander.storage.FileEntry; import com.webcodepro.applecommander.storage.FileEntry;
import com.webcodepro.applecommander.storage.FormattedDisk; import com.webcodepro.applecommander.storage.FormattedDisk;
import com.webcodepro.applecommander.storage.FormattedDisk.DiskUsage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer;
/** /**
* Manages a disk that is in the Apple CP/M format. * Manages a disk that is in the Apple CP/M format.
@ -35,6 +35,63 @@ import java.util.Map;
* @author Rob Greene * @author Rob Greene
*/ */
public class CpmFormatDisk extends FormattedDisk { public class CpmFormatDisk extends FormattedDisk {
/**
* The size of the CP/M sector. Assumed to be 128.
*/
public static final int CPM_SECTORSIZE = 128;
/**
* The size of a CP/M block. Assumed to be 1K.
*/
public static final int CPM_BLOCKSIZE = 1024;
/**
* The number of CP/M sectors per CP/M block.
*/
public static final int CPM_SECTORS_PER_CPM_BLOCK =
CPM_BLOCKSIZE / CPM_SECTORSIZE;
/**
* The number of CP/M blocks per physical track.
*/
public static final int CPM_BLOCKS_PER_TRACK = 4;
/**
* The number of physical sectors per CP/M block.
*/
public static final int PHYSICAL_SECTORS_PER_BLOCK = 4;
/**
* The track number which CP/M block #0 resides at.
* (The other tracks are boot-related and not available.)
*/
public static final int PHYSICAL_BLOCK_TRACK_START = 3;
/**
* The sector skew of the CP/M disk image.
*/
public static final int[] sectorSkew = {
0x0, 0x6, 0xc, 0x3, 0x9, 0xf, 0xe, 0x5,
0xb, 0x2, 0x8, 0x7, 0xd, 0x4, 0xa, 0x1 };
/**
* Manage CP/M disk usage.
*/
public class CpmDiskUsage implements DiskUsage {
int block = -1;
boolean[] usage = null;
public CpmDiskUsage(boolean[] usage) {
this.usage = usage;
}
public boolean hasNext() {
return block < usage.length-1;
}
public void next() {
block++;
}
public boolean isFree() {
return !isUsed();
}
public boolean isUsed() {
return usage[block];
}
}
/** /**
* Create a CP/M formatted disk. * Create a CP/M formatted disk.
*/ */
@ -59,19 +116,34 @@ public class CpmFormatDisk extends FormattedDisk {
} }
/** /**
* Return the amount of free space in bytes.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getFreeSpace() * @see com.webcodepro.applecommander.storage.FormattedDisk#getFreeSpace()
*/ */
public int getFreeSpace() { public int getFreeSpace() {
// TODO Auto-generated method stub return getPhysicalSize() - getUsedSpace();
return 0;
} }
/** /**
* Return the amount of used space in bytes.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getUsedSpace() * @see com.webcodepro.applecommander.storage.FormattedDisk#getUsedSpace()
*/ */
public int getUsedSpace() { public int getUsedSpace() {
// TODO Auto-generated method stub int blocksUsed = getBlocksUsed();
return 0; return blocksUsed * CPM_BLOCKSIZE +
PHYSICAL_BLOCK_TRACK_START * CPM_BLOCKS_PER_TRACK * CPM_BLOCKSIZE;
}
/**
* Compute the number of CP/M blocks that are currently used.
*/
public int getBlocksUsed() {
List files = getFiles();
int blocksUsed = 0;
for (int i=0; i<files.size(); i++) {
CpmFileEntry fileEntry = (CpmFileEntry) files.get(i);
blocksUsed = fileEntry.getBlocksUsed();
}
return blocksUsed;
} }
/** /**
@ -86,27 +158,43 @@ public class CpmFormatDisk extends FormattedDisk {
/** /**
* Get the length of the bitmap. * Get the length of the bitmap.
* This is hard-coded to 128 (0x80). * This is hard-coded to 140.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getBitmapLength() * @see com.webcodepro.applecommander.storage.FormattedDisk#getBitmapLength()
*/ */
public int getBitmapLength() { public int getBitmapLength() {
return 0x80; return getPhysicalSize() / CPM_BLOCKSIZE;
} }
/** /**
* Get the disk usage iterator.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getDiskUsage() * @see com.webcodepro.applecommander.storage.FormattedDisk#getDiskUsage()
*/ */
public DiskUsage getDiskUsage() { public DiskUsage getDiskUsage() {
// TODO Auto-generated method stub boolean[] usage = new boolean[getBitmapLength()];
return null; // fill in reserved space at beginning of disk (including 2 directory blocks)
int dataBlockStart = PHYSICAL_BLOCK_TRACK_START * CPM_BLOCKS_PER_TRACK;
for (int i=0; i<dataBlockStart+2; i++) {
usage[i] = true;
}
// fill in space used by files
List files = getFiles();
for (int i=0; i<files.size(); i++) {
CpmFileEntry fileEntry = (CpmFileEntry) files.get(i);
int[] allocation = fileEntry.getAllocations();
for (int a=0; a<allocation.length; a++) {
int block = dataBlockStart + allocation[a];
usage[block] = true;
}
}
return new CpmDiskUsage(usage);
} }
/** /**
* Get the labels to use in the bitmap.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getBitmapLabels() * @see com.webcodepro.applecommander.storage.FormattedDisk#getBitmapLabels()
*/ */
public String[] getBitmapLabels() { public String[] getBitmapLabels() {
// TODO Auto-generated method stub return new String[] { "CP/M 1K BLOCK" };
return null;
} }
/** /**
@ -118,90 +206,156 @@ public class CpmFormatDisk extends FormattedDisk {
} }
/** /**
* Indicates if this disk image can read data from a file.
* @see com.webcodepro.applecommander.storage.FormattedDisk#canReadFileData() * @see com.webcodepro.applecommander.storage.FormattedDisk#canReadFileData()
*/ */
public boolean canReadFileData() { public boolean canReadFileData() {
// TODO Auto-generated method stub return true;
return false;
} }
/** /**
* Indicates if this disk image can write data to a file.
* @see com.webcodepro.applecommander.storage.FormattedDisk#canWriteFileData() * @see com.webcodepro.applecommander.storage.FormattedDisk#canWriteFileData()
*/ */
public boolean canWriteFileData() { public boolean canWriteFileData() {
// TODO Auto-generated method stub
return false; return false;
} }
/** /**
* Identify if this disk format is capable of having directories.
* @see com.webcodepro.applecommander.storage.FormattedDisk#canHaveDirectories() * @see com.webcodepro.applecommander.storage.FormattedDisk#canHaveDirectories()
*/ */
public boolean canHaveDirectories() { public boolean canHaveDirectories() {
// TODO Auto-generated method stub
return false; return false;
} }
/** /**
* Indicates if this disk image can delete a file.
* @see com.webcodepro.applecommander.storage.FormattedDisk#canDeleteFile() * @see com.webcodepro.applecommander.storage.FormattedDisk#canDeleteFile()
*/ */
public boolean canDeleteFile() { public boolean canDeleteFile() {
// TODO Auto-generated method stub return true;
return false;
} }
/** /**
* Get the data associated with the specified FileEntry.
* This is just the raw data. Use the FileEntry itself to read
* data appropriately!
* @see com.webcodepro.applecommander.storage.FormattedDisk#getFileData(com.webcodepro.applecommander.storage.FileEntry) * @see com.webcodepro.applecommander.storage.FormattedDisk#getFileData(com.webcodepro.applecommander.storage.FileEntry)
*/ */
public byte[] getFileData(FileEntry fileEntry) { public byte[] getFileData(FileEntry fileEntry) {
// TODO Auto-generated method stub CpmFileEntry cpmEntry = (CpmFileEntry) fileEntry;
return null; int[] allocations = cpmEntry.getAllocations();
byte[] data = new byte[allocations.length * CPM_BLOCKSIZE];
for (int i=0; i<allocations.length; i++) {
int blockNumber = allocations[i];
if (blockNumber > 0) {
byte[] block = readCpmBlock(blockNumber);
System.arraycopy(block, 0,
data, i * CPM_BLOCKSIZE, CPM_BLOCKSIZE);
}
}
return data;
} }
/** /**
* Format the disk. Simply wipes the disk to all 0xE5 - this seems to
* be the standard (or a requirement).
* <p>
* Note: Assumes that this is a 140K CP/M disk of 35 tracks and
* 16 physical sectors.
* @see com.webcodepro.applecommander.storage.FormattedDisk#format() * @see com.webcodepro.applecommander.storage.FormattedDisk#format()
*/ */
public void format() { public void format() {
// TODO Auto-generated method stub byte[] sectorData = new byte[SECTOR_SIZE];
for (int i=0; i<SECTOR_SIZE; i++) {
sectorData[i] = (byte) 0xe5;
}
for (int track=0; track<35; track++) {
for (int sector=0; sector<16; sector++) {
writeSector(track, sector, sectorData);
}
}
} }
/** /**
* Returns the logical disk number. This can be used to identify
* between disks when a format supports multiple logical volumes.
* If a value of 0 is returned, there is not multiple logical
* volumes to distinguish.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getLogicalDiskNumber() * @see com.webcodepro.applecommander.storage.FormattedDisk#getLogicalDiskNumber()
*/ */
public int getLogicalDiskNumber() { public int getLogicalDiskNumber() {
// TODO Auto-generated method stub
return 0; return 0;
} }
/** /**
* Returns a valid filename for the given filename. The assumed rules
* are that the name must be 8 characters long (padded on the end with
* spaces) and alphanumeric, starting with a character.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getSuggestedFilename(java.lang.String) * @see com.webcodepro.applecommander.storage.FormattedDisk#getSuggestedFilename(java.lang.String)
*/ */
public String getSuggestedFilename(String filename) { public String getSuggestedFilename(String filename) {
// TODO Auto-generated method stub StringTokenizer tokenizer = new StringTokenizer(filename, ".");
return null; filename = tokenizer.nextToken(); // grab just the first part of the name..
StringBuffer newName = new StringBuffer();
if (!Character.isLetter(filename.charAt(0))) {
newName.append('A');
}
int i=0;
while (newName.length() < 8 && i<filename.length()) {
char ch = filename.charAt(i);
if (Character.isLetterOrDigit(ch) || ch == '.') {
newName.append(ch);
}
i++;
}
while (newName.length() < 8) newName.append(' ');
return newName.toString().toUpperCase().trim();
} }
/** /**
* Returns a valid filetype for the given filename. Rules are very
* similar to the filename, but trim to 3 characters.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getSuggestedFiletype(java.lang.String) * @see com.webcodepro.applecommander.storage.FormattedDisk#getSuggestedFiletype(java.lang.String)
*/ */
public String getSuggestedFiletype(String filename) { public String getSuggestedFiletype(String filetype) {
// TODO Auto-generated method stub StringTokenizer tokenizer = new StringTokenizer(filetype, ".");
return null; tokenizer.nextToken();
filetype = "";
while (tokenizer.hasMoreTokens()) {
filetype = tokenizer.nextToken(); // grab just the last part of the name...
}
StringBuffer newType = new StringBuffer();
if (filetype.length() > 0 && !Character.isLetter(filetype.charAt(0))) {
newType.append('A');
}
int i=0;
while (newType.length() < 3 && i<filetype.length()) {
char ch = filetype.charAt(i);
if (Character.isLetterOrDigit(ch) || ch == '.') {
newType.append(ch);
}
i++;
}
while (newType.length() < 3) newType.append(' ');
return newType.toString().toUpperCase().trim();
} }
/** /**
* Returns a list of possible file types. Since the filetype is
* specific to each operating system, a simple String is used.
* @see com.webcodepro.applecommander.storage.FormattedDisk#getFiletypes() * @see com.webcodepro.applecommander.storage.FormattedDisk#getFiletypes()
*/ */
public String[] getFiletypes() { public String[] getFiletypes() {
// TODO Auto-generated method stub return null; // there are no standard filetypes...
return null;
} }
/** /**
* Indicates if this filetype requires an address component.
* @see com.webcodepro.applecommander.storage.FormattedDisk#needsAddress(java.lang.String) * @see com.webcodepro.applecommander.storage.FormattedDisk#needsAddress(java.lang.String)
*/ */
public boolean needsAddress(String filetype) { public boolean needsAddress(String filetype) {
// TODO Auto-generated method stub
return false; return false;
} }
@ -233,50 +387,72 @@ public class CpmFormatDisk extends FormattedDisk {
} }
/** /**
* Create a new FileEntry.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#createFile() * @see com.webcodepro.applecommander.storage.DirectoryEntry#createFile()
*/ */
public FileEntry createFile() throws DiskFullException { public FileEntry createFile() throws DiskFullException {
// TODO Auto-generated method stub
return null; return null;
} }
/** /**
* Identify if additional directories can be created. CP/M doesn't
* support directories.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateDirectories() * @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateDirectories()
*/ */
public boolean canCreateDirectories() { public boolean canCreateDirectories() {
// TODO Auto-generated method stub
return false; return false;
} }
/** /**
* Indicates if this disk image can create a file.
* @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateFile() * @see com.webcodepro.applecommander.storage.DirectoryEntry#canCreateFile()
*/ */
public boolean canCreateFile() { public boolean canCreateFile() {
// TODO Auto-generated method stub
return false; return false;
} }
/** /**
* Read a CP/M block (1K in size). * Read a CP/M block (1K in size).
* <p>
* Apparantly, data block #0 starts at track 3, sector 0.
* There should be 64 directory entries.
* Each block is 1024 bytes (16 128-byte sectors).
* One block should be the entire directory...
*/ */
public byte[] readCpmBlock(int block) { public byte[] readCpmBlock(int block) {
int[] sectorSkew = { 0x0, 0x6, 0xc, 0x3, 0x9, 0xf, 0xe, 0x5, byte[] data = new byte[CPM_BLOCKSIZE];
0xb, 0x2, 0x8, 0x7, 0xd, 0x4, 0xa, 0x1 }; int track = computeTrack(block);
byte[] data = new byte[1024]; int sector = computeSector(block);
int track = 3 + (block / 4); for (int i=0; i<PHYSICAL_SECTORS_PER_BLOCK; i++) {
int sector = (block % 4) * 4;
for (int i=0; i<4; i++) {
System.arraycopy(readSector(track, sectorSkew[sector+i]), System.arraycopy(readSector(track, sectorSkew[sector+i]),
0, data, i*SECTOR_SIZE, SECTOR_SIZE); 0, data, i*SECTOR_SIZE, SECTOR_SIZE);
} }
return data; return data;
} }
/**
* Compute the physical track number.
*/
protected int computeTrack(int block) {
return PHYSICAL_BLOCK_TRACK_START + (block / CPM_BLOCKS_PER_TRACK);
}
/**
* Compute the physical sector number. The rest of the block
* follows in sequential order.
*/
protected int computeSector(int block) {
return (block % CPM_BLOCKS_PER_TRACK) * PHYSICAL_SECTORS_PER_BLOCK;
}
/**
* Write a CP/M block.
*/
public void writeCpmBlock(int block, byte[] data) {
int track = computeTrack(block);
int sector = computeSector(block);
byte[] sectorData = new byte[SECTOR_SIZE];
for (int i=0; i<PHYSICAL_SECTORS_PER_BLOCK; i++) {
System.arraycopy(data, i*SECTOR_SIZE, sectorData, 0, SECTOR_SIZE);
writeSector(track, sectorSkew[sector+i], sectorData);
}
}
/** /**
* Get the standard file column header information. * Get the standard file column header information.
* This default implementation is intended only for standard mode. * This default implementation is intended only for standard mode.
@ -303,4 +479,10 @@ public class CpmFormatDisk extends FormattedDisk {
return list; return list;
} }
/**
* Indicates if this FormattedDisk supports a disk map.
*/
public boolean supportsDiskMap() {
return true;
}
} }