From fadf5a9782adc8d4d25b040e6feba06f16792386 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Thu, 1 Feb 2018 18:41:01 -0600 Subject: [PATCH] Add decode...code --- include/apple2.dec.h | 8 +++ sources.cmake | 1 + src/apple2.dec.c | 142 +++++++++++++++++++++++++++++++++++++++++++ tests/apple2.dec.c | 73 ++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 include/apple2.dec.h create mode 100644 src/apple2.dec.c create mode 100644 tests/apple2.dec.c diff --git a/include/apple2.dec.h b/include/apple2.dec.h new file mode 100644 index 0000000..d9a4e72 --- /dev/null +++ b/include/apple2.dec.h @@ -0,0 +1,8 @@ +#ifndef _APPLE2_DEC_H_ +#define _APPLE2_DEC_H_ + +#include "vm_segment.h" + +extern int apple2_dec_sector(vm_segment *, vm_segment *, int, int); + +#endif diff --git a/sources.cmake b/sources.cmake index 29fb6a4..1f2b6a5 100644 --- a/sources.cmake +++ b/sources.cmake @@ -3,6 +3,7 @@ set(erc_sources apple2.bank.c apple2.dbuf.c apple2.dd.c + apple2.dec.c apple2.draw.c apple2.enc.c apple2.kb.c diff --git a/src/apple2.dec.c b/src/apple2.dec.c new file mode 100644 index 0000000..4530242 --- /dev/null +++ b/src/apple2.dec.c @@ -0,0 +1,142 @@ +/* + * apple2.dec.c + */ + +#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[] = { +// 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 +}; +#endif + +#if 0 +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 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; + int epilogue = soff + 9 + 0x157; + + // Let's validate that there's really a sector where we think + // there's one. + if (vm_segment_get(src, prologue) != 0xd5 || + vm_segment_get(src, prologue + 1) != 0xaa || + vm_segment_get(src, prologue + 2) != 0xad || + vm_segment_get(src, epilogue) != 0xde || + vm_segment_get(src, epilogue + 1) != 0xaa || + vm_segment_get(src, epilogue + 2) != 0xeb + ) { + 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); + } + + return 256; +} diff --git a/tests/apple2.dec.c b/tests/apple2.dec.c new file mode 100644 index 0000000..6110ce7 --- /dev/null +++ b/tests/apple2.dec.c @@ -0,0 +1,73 @@ +#include + +#include "apple2.dec.h" + +#if 0 +/* + * Ripped from apple2.enc.c + */ +static vm_8bit f_sector[] = { + 0xdd, 0xab, 0xce, 0xd9, 0xc3, 0xf6, 0xdd, 0x77, 0x0e, 0x60, 0x6d, 0x95, 0xbd, 0xe3, 0x74, 0xc0, + 0xd4, 0xa7, 0x3f, 0xc2, 0x39, 0x37, 0xfa, 0xa7, 0xd1, 0xa9, 0xc9, 0x5e, 0xd3, 0x28, 0x47, 0x04, + 0x4f, 0xec, 0xfc, 0xc1, 0x5e, 0x90, 0x9a, 0x7d, 0x72, 0x36, 0xd0, 0x23, 0x90, 0xe5, 0x18, 0xeb, + 0x8a, 0x80, 0xad, 0xda, 0x85, 0xe4, 0xe3, 0x34, 0x72, 0x5c, 0x51, 0x18, 0xf0, 0xec, 0x3f, 0xcf, + 0xf7, 0xc1, 0x61, 0xe7, 0xad, 0x78, 0xea, 0x8a, 0xa1, 0x53, 0xbc, 0x05, 0x06, 0x77, 0x40, 0xb1, + 0xfb, 0x21, 0xf4, 0xec, 0x7a, 0xd3, 0xee, 0xcd, 0x99, 0xfa, 0x95, 0x39, 0xf8, 0xe8, 0x2d, 0xf5, + 0x40, 0x1a, 0x68, 0x0b, 0x7e, 0x96, 0x4c, 0x05, 0x03, 0x74, 0xa9, 0xca, 0x1d, 0x30, 0xf0, 0x96, + 0x7d, 0xe9, 0x24, 0xca, 0x6b, 0x09, 0x50, 0x69, 0x35, 0xe0, 0x1b, 0xd8, 0xfb, 0x62, 0x47, 0xe6, + 0x6a, 0xad, 0x3a, 0xd2, 0x5c, 0xe6, 0xda, 0x5f, 0x62, 0xfb, 0x02, 0x61, 0x2b, 0x42, 0x7a, 0x44, + 0xa2, 0x2f, 0x97, 0xe5, 0x08, 0xc7, 0xf6, 0xe1, 0x7a, 0xd6, 0x6a, 0xc0, 0x11, 0xd6, 0xa4, 0x26, + 0xac, 0xe4, 0xfb, 0xf3, 0x28, 0x8a, 0x3b, 0x1b, 0x4c, 0x9c, 0x84, 0x55, 0x73, 0x4b, 0x4f, 0xc1, + 0x0a, 0xd8, 0x9e, 0xdc, 0x1b, 0x01, 0xec, 0x12, 0x41, 0x84, 0x15, 0x8c, 0xb9, 0x29, 0x5f, 0x34, + 0x99, 0x3e, 0x3b, 0xa8, 0xcf, 0x7c, 0x65, 0x6f, 0x60, 0x45, 0x73, 0xe4, 0xea, 0x84, 0x84, 0x8b, + 0xaa, 0x46, 0x72, 0xbc, 0xd2, 0x65, 0xbe, 0x5b, 0x2d, 0x24, 0xd6, 0x41, 0x88, 0xd0, 0x6a, 0x80, + 0x1e, 0x22, 0x7a, 0x44, 0x70, 0x17, 0x44, 0x18, 0x1f, 0x31, 0x41, 0xf5, 0xa0, 0xae, 0x1b, 0xe1, + 0x4f, 0x9f, 0xdb, 0x5a, 0x74, 0x90, 0x55, 0xe1, 0x49, 0xbf, 0x2d, 0x20, 0xd6, 0x03, 0x03, 0xa2, +}; +#endif + +/* + * Same as above -- ripped from apple2.enc.c + */ +static vm_8bit f_enc_sector[] = { + 0xd5, 0xaa, 0xad, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf5, 0xaf, 0x9a, 0xd3, 0xfd, 0xb6, 0xcb, + 0xb5, 0xfa, 0xb5, 0xe6, 0xb9, 0xf3, 0xea, 0xe7, 0xd6, 0xdb, 0xad, 0xb9, 0xfe, 0xe7, 0xfd, 0xeb, + 0xfa, 0xee, 0xf3, 0xe9, 0xb7, 0xfa, 0xde, 0xcb, 0xf6, 0xcb, 0xcb, 0x96, 0xfa, 0xec, 0xaf, 0xaf, + 0xcb, 0xcb, 0xf7, 0x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0xda, 0xeb, 0xb3, 0xd7, 0xb3, 0xdd, 0xfe, 0xbd, 0xb6, 0xbf, 0xb2, 0xb3, 0xf9, + 0xda, 0xeb, 0xcd, 0xfb, 0xf2, 0xae, 0x9e, 0xdd, 0xe7, 0xec, 0xe6, 0xf3, 0xee, 0xcd, 0xd6, 0xd9, + 0xb6, 0xa6, 0x9a, 0xf5, 0xdf, 0xe9, 0xa7, 0xba, 0xf3, 0x9f, 0xf3, 0xab, 0xb2, 0x97, 0xda, 0xef, + 0xf3, 0xb5, 0xb4, 0xee, 0x9f, 0xfb, 0xff, 0xb9, 0xee, 0xdb, 0xdd, 0xaf, 0xdb, 0xce, 0xbf, 0xe7, + 0xe5, 0x97, 0xdb, 0xbe, 0xe9, 0x9f, 0x9a, 0x9b, 0xab, 0xaf, 0xdc, 0x9b, 0xcb, 0x96, 0x9b, 0xa7, + 0xfb, 0xaf, 0xf2, 0xcb, 0xea, 0xf5, 0xf9, 0xce, 0x9a, 0xfc, 0xdc, 0xef, 0xbb, 0xeb, 0xfa, 0xde, + 0xb3, 0xbb, 0xb3, 0xaf, 0xbe, 0xb9, 0xbc, 0x97, 0xad, 0xcd, 0xea, 0xba, 0x9b, 0xea, 0xfe, 0xe7, + 0xf3, 0xb5, 0xd6, 0xad, 0xf9, 0xee, 0xea, 0xe6, 0xfd, 0xdb, 0x9b, 0xfd, 0xf4, 0x96, 0xdf, 0xdf, + 0xde, 0xaa, 0xeb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +}; + +Test(apple2_dec, sector) +{ + vm_segment *src, *dest; + + src = vm_segment_create(1000); + dest = vm_segment_create(1000); + + vm_segment_copy_buf(src, f_enc_sector, 0, 0, 382); + + printf("it's %d\n", apple2_dec_sector(dest, src, 0, 9)); + + for (int i = 0; i < 256; i++) { + printf("%02x\n", vm_segment_get(dest, i)); + } +}