dmolony-DiskBrowser/src/com/bytezone/diskbrowser/nib/DiskReader13Sector.java

86 lines
3.0 KiB
Java
Raw Permalink Normal View History

2018-08-17 01:20:00 +00:00
package com.bytezone.diskbrowser.nib;
2018-08-11 00:07:50 +00:00
2019-08-03 07:49:10 +00:00
// -----------------------------------------------------------------------------------//
2019-09-04 20:44:43 +00:00
class DiskReader13Sector extends DiskReader
2019-08-03 07:49:10 +00:00
// -----------------------------------------------------------------------------------//
2018-08-11 00:07:50 +00:00
{
2018-08-11 04:12:21 +00:00
private static final int RAW_BUFFER_SIZE = 410;
private static final int BUFFER_WITH_CHECKSUM_SIZE = RAW_BUFFER_SIZE + 1;
2018-08-11 00:07:50 +00:00
2018-08-11 04:12:21 +00:00
private final byte[] decodeA = new byte[BUFFER_WITH_CHECKSUM_SIZE];
private final byte[] decodeB = new byte[RAW_BUFFER_SIZE];
2018-08-11 00:07:50 +00:00
2018-08-12 07:16:03 +00:00
private final ByteTranslator byteTranslator = new ByteTranslator5and3 ();
2018-08-11 00:07:50 +00:00
// ---------------------------------------------------------------------------------//
DiskReader13Sector ()
2019-08-03 07:49:10 +00:00
// ---------------------------------------------------------------------------------//
2018-08-11 00:07:50 +00:00
{
super (13);
}
// ---------------------------------------------------------------------------------//
@Override
2019-07-28 06:24:52 +00:00
byte[] decodeSector (byte[] buffer, int offset) throws DiskNibbleException
2019-08-03 07:49:10 +00:00
// ---------------------------------------------------------------------------------//
2018-08-11 00:07:50 +00:00
{
2019-09-05 12:34:34 +00:00
byte[] decodedBuffer = new byte[SECTOR_SIZE];
2018-08-11 00:07:50 +00:00
2018-08-13 09:17:05 +00:00
// convert legal disk values to actual 5 bit values
for (int i = 0; i < BUFFER_WITH_CHECKSUM_SIZE; i++) // 411 bytes
decodeA[i] = (byte) (byteTranslator.decode (buffer[offset++]) << 3);
// reconstruct 410 bytes each with 5 bits
byte chk = 0;
int ptr = 0;
for (int i = 409; i >= 256; i--) // 154 bytes
chk = decodeB[i] = (byte) (decodeA[ptr++] ^ chk);
for (int i = 0; i < 256; i++) // 256 bytes
chk = decodeB[i] = (byte) (decodeA[ptr++] ^ chk);
if ((chk ^ decodeA[ptr]) != 0)
throw new DiskNibbleException ("Checksum failed");
// rearrange 410 bytes into 256
byte[] k = new byte[8];
final int[] lines = { 0, 51, 102, 153, 204, 256, 307, 358 }; // 255 is skipped
// process 8 disk bytes at a time, giving 5 valid bytes
// do this 51 times, giving 255 bytes
ptr = 0;
for (int i = 50; i >= 0; i--)
2018-08-11 00:07:50 +00:00
{
2018-08-13 09:17:05 +00:00
for (int j = 0; j < 8; j++)
k[j] = decodeB[i + lines[j]];
k[0] |= (k[5] & 0xE0) >>> 5;
k[1] |= (k[6] & 0xE0) >>> 5;
k[2] |= (k[7] & 0xE0) >>> 5;
k[3] |= (k[5] & 0x10) >>> 2;
k[3] |= (k[6] & 0x10) >>> 3;
k[3] |= (k[7] & 0x10) >>> 4;
k[4] |= (k[5] & 0x08) >>> 1;
k[4] |= (k[6] & 0x08) >>> 2;
k[4] |= (k[7] & 0x08) >>> 3;
for (int j = 0; j < 5; j++)
decodedBuffer[ptr++] = k[j];
2018-08-11 00:07:50 +00:00
}
2018-08-13 09:17:05 +00:00
// add last byte
decodedBuffer[ptr] = (byte) (decodeB[255] | ((decodeB[409] & 0x3F) >>> 3));
2018-08-11 00:07:50 +00:00
return decodedBuffer;
}
// ---------------------------------------------------------------------------------//
@Override
byte[] encodeSector (byte[] buffer)
2019-08-03 07:49:10 +00:00
// ---------------------------------------------------------------------------------//
2018-08-11 00:07:50 +00:00
{
System.out.println ("encodeSector() not written");
return null;
}
}