1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
/**
* @file decode.c
* C2N pulse stream decoder
* @author Marko Mkel (msmakela@nic.funet.fi)
*/
/* Copyright 2001 Marko Mkel.
This file is part of C2N, a program for processing data tapes in
Commodore C2N format.
C2N is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
C2N is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
The GNU General Public License is often shipped with GNU software, and
is generally kept in a file called COPYING or LICENSE. If you do not
have a copy of the license, write to the Free Software Foundation,
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include <stdio.h>
#include <stdlib.h>
#include "c2n.h"
#include "decode.h"
/** the pulse stream reader */
static pulse_r_t dec_rd;
/** the decoding error reporter */
static pulse_error_t dec_err;
/** Skip a sync mark (sequence of short pulses and pauses) */
static enum pulse
skip_sync (void)
{
enum pulse p;
unsigned count;
if (verbose)
fputs ("skipping a sync mark", stderr), fflush (stderr);
for (count = 0;; count++) {
p = (*dec_rd) ();
eval:
switch (p) {
case Pause:
p = (*dec_rd) ();
if (p == Pause)
goto done;
goto eval;
case Short:
continue;
default:
done:
if (verbose)
fprintf (stderr, " (%u pulses)\n", count);
return p;
}
}
}
/** number of the block being decoded */
static unsigned block = 0;
/** length of the decoding buffer */
static unsigned declen = 0;
/** decoding buffer */
static char* decbuf = 0;
/** flag: checking a second copy? */
static unsigned chk = 0;
/** Output a decoded byte
* @param c the decoded byte
*/
static void
output (char c)
{
if (!(declen & (declen + 1)))
decbuf = realloc (decbuf, (declen + 1) << 1);
decbuf[declen++] = c;
}
/** Decode a block in Commodore PET/VIC-20/C64/C128 format
* @return the countdown character (0x81 for 1st copy, 1 for 2nd copy)
*/
static unsigned
decBlock (void)
{
/** byte count */
unsigned cnt = 0;
/** data character being read */
unsigned c = 0;
/** last countdown character read */
unsigned countdown = 0;
/** data block checksum */
char chk = 0;
/** number of subsequent pauses */
unsigned pauses = 0;
for (;;) {
/** a pulse read from the tape */
enum pulse p = (*dec_rd) ();
resync:
if (p != Pause) pauses = 0;
switch (p) {
case Pause:
pauses++;
p = (*dec_rd) ();
if (p != Pause)
goto resync;
if (!cnt && ++pauses > 5)
return countdown;
/* fall through */
case Short:
if (chk && dec_err)
(*dec_err) (Checksum, block, cnt);
if (cnt)
return countdown;
else if ((p = skip_sync ()) != Long)
goto resync;
break;
case Medium:
if (!dec_err || (*dec_err) (p, block, cnt))
return countdown;
continue;
case Long:
break;
}
p = (*dec_rd) ();
if (p == Short)
goto resync;
else if (p == Medium) {
/** parity bit */
register unsigned parity;
/** bit count */
register unsigned bitcnt;
if (cnt)
output (c);
for (c = parity = 0, bitcnt = 9; bitcnt--; ) {
enum pulse p1 = (*dec_rd) ();
if (p1 != Short && p1 != Medium) {
if (!dec_err || (*dec_err) (p1, block, cnt))
return countdown;
p = p1;
goto resync;
}
resync2:
p = (*dec_rd) ();
if ((p != Short && p != Medium) || p == p1) {
if (!dec_err || (*dec_err) (p, block, cnt))
return countdown;
if (p == p1) goto resync2;
goto resync;
}
c >>= 1;
if (p == Short) {
c |= 0x100;
parity = !parity;
}
}
if (!parity) {
if (!dec_err || (*dec_err) (Parity, block, cnt))
return countdown;
}
c &= 0xff;
if (!countdown) {
countdown = c;
if ((c & 0x7f) != 9)
if (!dec_err || (*dec_err) (Countdown, block, c))
return countdown;
}
else if (countdown != 1 && countdown != 0x81) {
if (c != countdown - 1)
if (!dec_err || (*dec_err) (Countdown, block, c))
return countdown;
countdown = c;
}
else {
cnt++;
chk ^= c;
}
}
else if (!dec_err || (*dec_err) (p, block, cnt))
return countdown;
}
}
/** Decode a block in Commodore 264 series format
* @return the countdown character (0x81 for 1st copy, 1 for 2nd copy)
*/
static unsigned
dec264Block (void)
{
/** byte count */
unsigned cnt = 0;
/** data character being read */
unsigned c = 0;
/** last countdown character read */
unsigned countdown = 0;
/** data block checksum */
char chk = 0;
/** number of subsequent pauses */
unsigned pauses = 0;
for (;;) {
/** a pulse read from the tape */
enum pulse p = (*dec_rd) ();
resync:
if (p != Pause) pauses = 0;
switch (p) {
case Pause:
pauses++;
p = (*dec_rd) ();
if (p != Pause)
goto resync;
if (!cnt && ++pauses > 5)
return countdown;
/* fall through */
case Short:
if (chk && dec_err)
(*dec_err) (Checksum, block, cnt);
if (cnt)
return countdown;
else if ((p = skip_sync ()) != Long)
goto resync;
break;
case Medium:
p = (*dec_rd) ();
if (p == Short)
goto resync;
if (!dec_err ||
(*dec_err) (Medium, block, cnt) ||
(*dec_err) (p, block, cnt))
return countdown;
continue;
case Long:
break;
}
p = (*dec_rd) ();
if (p == Medium) {
/** parity bit */
register unsigned parity;
/** bit count */
register unsigned bitcnt;
if (cnt)
output (c);
for (c = parity = 0, bitcnt = 9; bitcnt--; ) {
enum pulse p1 = (*dec_rd) ();
if (p1 != Short && p1 != Medium) {
if (!dec_err || (*dec_err) (p1, block, cnt))
return countdown;
p = p1;
goto resync;
}
resync2:
p = (*dec_rd) ();
if ((p != Short && p != Medium) || p == p1) {
if (!dec_err || (*dec_err) (p, block, cnt))
return countdown;
if (p == p1) goto resync2;
goto resync;
}
c >>= 1;
if (p == Short) {
c |= 0x100;
parity = !parity;
}
}
if (!parity) {
if (!dec_err || (*dec_err) (Parity, block, cnt))
return countdown;
}
c &= 0xff;
if (!countdown) {
countdown = c;
if ((c & 0x7f) != 9)
if (!dec_err || (*dec_err) (Countdown, block, c))
return countdown;
}
else if (countdown != 1 && countdown != 0x81) {
if (c != countdown - 1)
if (!dec_err || (*dec_err) (Countdown, block, c))
return countdown;
countdown = c;
}
else {
cnt++;
chk ^= c;
}
}
else if (!dec_err || (*dec_err) (p, block, cnt))
return countdown;
}
}
/** Flag: use Commodore 264 series format */
static unsigned plus4;
/** Decode a block
* @return the countdown character (0x81 for 1st copy, 1 for 2nd copy)
*/
static unsigned
decodeBlock (void)
{
return plus4 ? dec264Block () : decBlock ();
}
/** Decode a block or compare the second copy to the first copy
* @param len expected length of the block
* @return the length of the decoded block
*/
static unsigned
decodeBytes (unsigned len)
{
/* first copy of the block */
char* firstbuf;
/* length of the first copy of the block */
unsigned firstlen;
if (chk) {
firstbuf = decbuf;
firstlen = declen;
}
else {
firstbuf = 0;
firstlen = 0;
free (decbuf);
}
decbuf = 0; declen = 0;
for (;; block++) {
/* decode a header block */
switch (decodeBlock ()) {
default:
continue;
case 0:
free (firstbuf);
return 0;
case 0x81: /* first copy */
if (verbose)
fprintf (stderr, "decoded %u bytes (1st copy)\n", declen);
if (chk) {
if (!dec_err || (*dec_err) (NoSecond, block, 0))
return 0;
}
free (firstbuf); firstbuf = 0; firstlen = 0;
chk = 1;
break;
case 1: /* second copy */
if (verbose)
fprintf (stderr, "decoded %u bytes (2nd copy)\n", declen);
if (!chk) {
if (!dec_err || (*dec_err) (NoFirst, block, 0))
return 0;
}
else {
chk = 0;
if (declen > firstlen) {
if (!dec_err || (*dec_err) (LongBlock, block, declen - firstlen)) {
free (firstbuf);
return 0;
}
}
else if (declen < firstlen) {
if (!dec_err || (*dec_err) (ShortBlock, block, firstlen - declen)) {
free (firstbuf);
return 0;
}
}
else {
for (declen = 0; declen < firstlen; declen++) {
if (decbuf[declen] != firstbuf[declen]) {
if (!dec_err || (*dec_err) (Mismatch, block, declen)) {
free (firstbuf);
return 0;
}
}
}
}
free (firstbuf); firstbuf = 0; firstlen = 0;
free (decbuf); decbuf = 0; declen = 0;
continue;
}
}
if (declen > len) {
if (!dec_err || (*dec_err) (LongBlock, block, declen - len))
return 0;
continue;
}
else if (declen < len) {
if (!dec_err || (*dec_err) (ShortBlock, block, len - declen))
return 0;
continue;
}
return declen;
}
}
/** Pulse stream decoder
* @param rd the pulse stream reader
* @param err the error reporter
* @param out the data output stream
* @return number of bytes converted
*/
static unsigned
dec (pulse_r_t rd, pulse_error_t err, FILE* out)
{
/** number of output bytes */
unsigned osize = 0;
/** expected length of next block */
unsigned length = 192;
/** flag: expecting a program block? */
unsigned prog = 0;
block = 0;
chk = 0;
dec_rd = rd;
dec_err = err;
free (decbuf); decbuf = 0; declen = 0;
for (;;) {
/** size of the decoded block */
unsigned bsize = decodeBytes (length);
if (bsize != length)
break;
if (prog) {
prog = 0;
length = 192;
}
else {
/* interpret the tape header */
switch (*decbuf) {
case tBasic:
case tML:
prog = 1;
length = ((unsigned) (unsigned char) decbuf[3]) |
((unsigned) (unsigned char) decbuf[4]) << 8;
length -= ((unsigned) (unsigned char) decbuf[1]) |
((unsigned) (unsigned char) decbuf[2]) << 8;
length &= 0xffff;
/* fall through */
case tDataBlock:
case tDataHeader:
case tEnd:
goto expected;
}
if (!dec_err || (*dec_err) (Unexpected, block,
(unsigned char) *decbuf))
goto done;
}
expected:
fwrite (decbuf, 1, declen, out);
osize += declen;
}
done:
free (decbuf); decbuf = 0; declen = 0;
return osize;
}
/** C2N pulse stream decoder
* @param rd the pulse stream reader
* @param err the error reporter
* @param out the data output stream
* @return number of bytes converted
*/
unsigned
decode (pulse_r_t rd, pulse_error_t err, FILE* out)
{
plus4 = 0;
return dec (rd, err, out);
}
/** Commodore 1531 (Commodore 264 series) pulse stream decoder
* @param rd the pulse stream reader
* @param err the error reporter
* @param out the data output stream
* @return number of bytes converted
*/
unsigned
decode264 (pulse_r_t rd, pulse_error_t err, FILE* out)
{
plus4 = 1;
return dec (rd, err, out);
}
|