1
0
mirror of https://github.com/pevans/erc-c.git synced 2025-02-17 07:32:05 +00:00

Implement the sector decode function

This was a bear to do, and the product being committed is probably my
third (or fourth?) try.
This commit is contained in:
Peter Evans 2018-02-02 17:19:38 -06:00
parent 55c85b0ec6
commit d2e21b55c4

View File

@ -2,118 +2,28 @@
* apple2.dec.c
*/
#include <stdbool.h>
#include "vm_segment.h"
static vm_8bit table[] = {
0x68, 0xa2, 0x5e, 0x5f, 0xff, 0x7f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0xa2, 0x5e, 0x5f, 0xff, 0x7f, 0x00, 0x00, 0x49, 0x62, 0x4f, 0x10, 0x01, 0x00, 0x00, 0x00,
0x40, 0xa2, 0x5e, 0x5f, 0xff, 0x7f, 0x00, 0x00, 0x40, 0xa2, 0x5e, 0x5f, 0xff, 0x7f, 0x00, 0x00,
0x58, 0xa2, 0x5e, 0x5f, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x50, 0x61, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, 0x55, 0x4f, 0x10, 0x01, 0x00, 0x00, 0x00,
0xc0, 0x54, 0x4f, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
#if 0
/*
* This is the "reverse" gcr table from enc.c; given one of the disk
* bytes, this table can turn them back into a 6-bit byte.
*/
static vm_8bit rev62[] = {
static vm_8bit conv6bit[] = {
// 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 00
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 10
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 20
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 30
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 40
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 50
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 60
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 70
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 80
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x03, 0x00, 0x04, 0x05, 0x06, // 90
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // a0
0x00, 0x00, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x00, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, // b0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x1c, 0x1e, 0x1e, // c0
0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x20, 0x21, 0x00, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, // d0
0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x2a, 0x2b, 0x00, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, // e0
0x00, 0x00, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x00, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, // f0
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 00
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x04, 0xff, 0xff, 0x08, 0x0c, 0xff, 0x10, 0x14, 0x18, // 10
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1c, 0x20, 0xff, 0xff, 0xff, 0x24, 0x28, 0x2c, 0x30, 0x34, // 20
0xff, 0xff, 0x38, 0x3c, 0x40, 0x44, 0x48, 0x4c, 0xff, 0x50, 0x54, 0x58, 0x5c, 0x60, 0x64, 0x68, // 30
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x6c, 0xff, 0x70, 0x74, 0x78, // 40
0xff, 0xff, 0xff, 0x7c, 0xff, 0xff, 0x80, 0x84, 0xff, 0x88, 0x8c, 0x90, 0x94, 0x98, 0x9c, 0xa0, // 50
0xff, 0xff, 0xff, 0xff, 0xff, 0xa4, 0xa8, 0xac, 0xff, 0xb0, 0xb4, 0xb8, 0xbc, 0xc0, 0xc4, 0xc8, // 60
0xff, 0xff, 0xcc, 0xd0, 0xd4, 0xd8, 0xdc, 0xe0, 0xff, 0xe4, 0xe8, 0xec, 0xf0, 0xf4, 0xf8, 0xfc, // 70
};
#endif
#if 0
int
int
apple2_dec_sector(vm_segment *dest, vm_segment *src, int doff, int soff)
{
int i, orig;
vm_8bit xoff;
vm_8bit conv[0x157], xor[0x156];
vm_8bit conv[0x157];
vm_8bit xor[0x156];
vm_8bit lval;
orig = doff;
for (i = 0; i < 0x157; i++) {
conv[i] = table[vm_segment_get(src, soff + i) & 0x7f];
}
for (i = 0, lval = 0; i < 0x156; i++) {
xor[i] = lval ^ conv[i];
lval = xor[i];
}
xoff = 0xac;
i = 0;
while (xoff != 0x02) {
if (xoff >= 0xac) {
vm_segment_set(dest, doff + xoff,
(xor[xoff + 0x56] & 0xfc) | ((xor[i] & 0x80) >> 7) | ((xor[i] & 0x40) >> 5));
}
xoff -= 0x56;
vm_segment_set(dest, doff + xoff,
(xor[xoff + 0x56] & 0xfc) | ((xor[i] & 0x20) >> 5) | ((xor[i] & 0x10) >> 3));
xoff -= 0x56;
vm_segment_set(dest, doff + xoff,
(xor[xoff + 0x56] & 0xfc) | ((xor[i] & 0x08) >> 3) | ((xor[i] & 0x04) >> 1));
xoff -= 0x53;
i++;
}
#if 0
for (i = 0; i < 0x56; i++) {
vm_8bit vac, v56, v00,
offac, off56;
offac = i + 0xAC;
off56 = i + 0x56;
vac = (xor[offac + 0x56] & 0xfc) | ((xor[i] & 0x80) >> 7) | ((xor[i] & 0x40) >> 5);
v56 = (xor[off56 + 0x56] & 0xfc) | ((xor[i] & 0x20) >> 5) | ((xor[i] & 0x10) >> 3);
v00 = (xor[i + 0x56] & 0xfc) | ((xor[i] & 0x08) >> 3) | ((xor[i] & 0x04) >> 1);
if (i == 0) {
printf("vac = %02x, v56 = %02x, v00 = %02x\n", vac, v56, v00);
}
if (offac > 0xAC) {
vm_segment_set(dest, doff + offac, vac);
}
vm_segment_set(dest, doff + off56, v56);
vm_segment_set(dest, doff + i, v00);
}
#endif
return 256;
}
#endif
int
apple2_dec_sector(vm_segment *dest, vm_segment *src, int doff, int soff)
{
vm_8bit data[500];
int i;
int prologue = soff;
@ -131,12 +41,63 @@ apple2_dec_sector(vm_segment *dest, vm_segment *src, int doff, int soff)
return 0;
}
// We want to start with just the data field from the encoded
// sector; this means stripping out any metadata and self-sync
// bytes.
for (i = soff + 9; i < epilogue; i++) {
data[i - soff - 9] = vm_segment_get(src, i);
// Here we mean to convert the 6-and-2 encoded bytes back into its
// first intermediate form
for (i = 0; i < 0x157; i++) {
conv[i] = conv6bit[vm_segment_get(src, soff + i + 9) & 0x7f];
}
// Originally, we XOR'd each byte when encoding; so we need to do
// another XOR, in pretty much the same manner.
for (i = 0, lval = 0; i < 0x156; i++) {
xor[i] = lval ^ conv[i];
lval = xor[i];
}
// Now we need to copy every byte back into its form that would be
// found on the original disk image. We're using the same sort of
// loop that jumps around three different sections of the array per
// iteration.
for (i = 0; i < 0x56; i++) {
vm_8bit offac, off56;
vm_8bit vac, v56, v00;
offac = i + 0xac;
off56 = i + 0x56;
// Recall that the least significant bits are packed into the
// first 86 (0x56) bytes of the 6-and-2 scheme block. So what
// we're doing here is grabbing the 6 _most_ significant bits
// (which is offac + 0x56), then using an OR to pack on the
// least significant bits from the those first 86 bytes.
vac =
(xor[offac + 0x56] & 0xfc) |
((xor[i] & 0x80) >> 7) |
((xor[i] & 0x40) >> 5);
v56 =
(xor[off56 + 0x56] & 0xfc) |
((xor[i] & 0x20) >> 5) |
((xor[i] & 0x10) >> 3);
v00 =
(xor[i + 0x56] & 0xfc) |
((xor[i] & 0x08) >> 3) |
((xor[i] & 0x04) >> 1);
// If we wrap around to 00 or 01, as will likely do with offac,
// don't do the set (it gets set with doff+i and v00).
if (offac >= 0xac) {
vm_segment_set(dest, doff + offac, vac);
}
// Set the rest!
vm_segment_set(dest, doff + off56, v56);
vm_segment_set(dest, doff + i, v00);
}
// Finally, we always return 256 since that's all we will be able to
// write from the given block (the validation of which is done by
// checking prologue/epilogue bytes first in this function).
return 256;
}