| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: sramstream.cpp ( POWDER Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | * The SRAMSTREAM class provides the routines to push bits |
| 14 | * into the saved memory space. It also does implicit zero |
| 15 | * compression, so the more zeros present, the less space |
| 16 | * required. |
| 17 | */ |
| 18 | |
| 19 | #include "sramstream.h" |
| 20 | #include <stdio.h> |
| 21 | |
| 22 | #include "sramstream.h" |
| 23 | #include "msg.h" |
| 24 | #include "assert.h" |
| 25 | |
| 26 | #include "mygba.h" |
| 27 | |
| 28 | // Crappy flashers require one to specify your save type. |
| 29 | // Since we aren't using the official HDK, we have to roll our own |
| 30 | // signifier: |
| 31 | const char *glbSaveType = "SRAM_V113"; |
| 32 | |
| 33 | static void |
| 34 | delay() |
| 35 | { |
| 36 | static volatile int delay = 0; |
| 37 | |
| 38 | delay *= delay + 1; |
| 39 | } |
| 40 | |
| 41 | SRAMSTREAM::SRAMSTREAM(bool silent) |
| 42 | { |
| 43 | mySRAMPtr = 0; |
| 44 | myPos = 0; |
| 45 | myBit = 0; |
| 46 | myNumBlocks = 0; |
| 47 | mySilent = silent; |
| 48 | } |
| 49 | |
| 50 | SRAMSTREAM::~SRAMSTREAM() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | SRAMSTREAM::rewind() |
| 56 | { |
| 57 | mySRAMPtr = 0; |
| 58 | myPos = 0; |
| 59 | myBit = 0; |
| 60 | myNumBlocks = 0; |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | SRAMSTREAM::writeRaw(const char *data, int length) |
| 65 | { |
| 66 | char *startsram = hamfake_writeLockSRAM(), *sram; |
| 67 | int i; |
| 68 | |
| 69 | if (mySRAMPtr + length > 63000) |
| 70 | { |
| 71 | // Fail to write! |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | sram = startsram; |
| 76 | sram += mySRAMPtr; |
| 77 | for (i = 0; i < length; i++) |
| 78 | { |
| 79 | *sram++ = *data++; |
| 80 | delay(); |
| 81 | } |
| 82 | mySRAMPtr += length; |
| 83 | |
| 84 | hamfake_writeUnlockSRAM(startsram); |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | bool |
| 90 | SRAMSTREAM::readRaw(char *data, int len) |
| 91 | { |
| 92 | char *sram, *startsram = hamfake_readLockSRAM(); |
| 93 | int i; |
| 94 | |
| 95 | sram = startsram; |
| 96 | sram += mySRAMPtr; |
| 97 | |
| 98 | for (i = 0; i < len; i++) |
| 99 | { |
| 100 | *data++ = *sram++; |
| 101 | delay(); |
| 102 | } |
| 103 | |
| 104 | mySRAMPtr += len; |
| 105 | |
| 106 | hamfake_readUnlockSRAM(startsram); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | bool |
| 111 | SRAMSTREAM::write(int val, int bits) |
| 112 | { |
| 113 | |
| 114 | // Only support full int writes for now. |
| 115 | while (bits > 0) |
| 116 | { |
| 117 | myBuffer[myPos++] = val & 0xff; |
| 118 | val >>= 8; |
| 119 | bits -= 8; |
| 120 | |
| 121 | if (myPos >= 1024) |
| 122 | { |
| 123 | writeToSRAM(myBuffer, 1024); |
| 124 | myPos = 0; |
| 125 | } |
| 126 | } |
| 127 | // Does not yet handle fractional. |
| 128 | UT_ASSERT(!bits); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | bool |
| 133 | SRAMSTREAM::uread(int &val, int bits) |
| 134 | { |
| 135 | int offset = 0; |
| 136 | int newval; |
| 137 | |
| 138 | val = 0; |
| 139 | while (bits > 0) |
| 140 | { |
| 141 | if (!myPos && !myBit) |
| 142 | { |
| 143 | // myBuffer not yet read... |
| 144 | readFromSRAM((char *)myBuffer, 256 * sizeof(int)); |
| 145 | } |
| 146 | newval = myBuffer[myPos++]; |
| 147 | newval &= 0xff; |
| 148 | val |= newval << offset; |
| 149 | offset += 8; |
| 150 | bits -= 8; |
| 151 | |
| 152 | if (myPos >= 1024) |
| 153 | myPos = 0; |
| 154 | } |
| 155 | UT_ASSERT(!bits); // Should be even bytes. |
| 156 | return true; |
| 157 | } |
| 158 | |
| 159 | bool |
| 160 | SRAMSTREAM::read(int &val, int bits) |
| 161 | { |
| 162 | uread(val, bits); |
| 163 | if (bits < 32) |
| 164 | { |
| 165 | if (val & (1 << (bits-1))) |
| 166 | { |
| 167 | // Sign extend... |
| 168 | val |= ~(int)((1 << bits) - 1); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool |
| 176 | SRAMSTREAM::readString(char *dst, int maxlen) |
| 177 | { |
| 178 | int i; |
| 179 | int val; |
| 180 | |
| 181 | for (i = 0; i < maxlen-1; i++) |
| 182 | { |
| 183 | if (!uread(val, 8)) |
| 184 | { |
| 185 | dst[i] = 0; |
| 186 | return false; |
| 187 | } |
| 188 | dst[i] = val; |
| 189 | if (!dst[i]) |
| 190 | return true; // success. |
| 191 | } |
| 192 | // Clipped, likely corrupt now. |
| 193 | dst[i] = 0; |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool |
| 198 | SRAMSTREAM::writeString(const char *src, int maxlen) |
| 199 | { |
| 200 | int i; |
| 201 | int val; |
| 202 | |
| 203 | if (!src) |
| 204 | src = ""; |
| 205 | |
| 206 | for (i = 0; i < maxlen-1; i++) |
| 207 | { |
| 208 | val = src[i]; |
| 209 | if (!write(val, 8)) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | if (!src[i]) |
| 214 | return true; // success. |
| 215 | } |
| 216 | // Clip the string. |
| 217 | val = 0; |
| 218 | if (!write(val, 8)) |
| 219 | return false; |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool |
| 224 | SRAMSTREAM::flush() |
| 225 | { |
| 226 | BUF buf; |
| 227 | |
| 228 | if (myPos || myBit) |
| 229 | { |
| 230 | int i; |
| 231 | // We want to zero out the end parts to reduce the saved size... |
| 232 | for (i = myPos+1; i < 256; i++) |
| 233 | { |
| 234 | myBuffer[i] = 0; |
| 235 | } |
| 236 | writeToSRAM((char *)myBuffer, 256 * sizeof(int)); |
| 237 | } |
| 238 | buf.sprintf("Completed save of %d bytes, %d blocks.", |
| 239 | mySRAMPtr, |
| 240 | myNumBlocks); |
| 241 | if (!mySilent) |
| 242 | msg_report(buf); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | SRAMSTREAM::writeToSRAM(char *data, int len) |
| 248 | { |
| 249 | char *sram = hamfake_writeLockSRAM(); |
| 250 | |
| 251 | if (mySRAMPtr > 63000) |
| 252 | { |
| 253 | // Fail to write! |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | sram += mySRAMPtr; |
| 258 | UT_ASSERT(len == 1024); |
| 259 | mySRAMPtr += compress(sram, data, len); |
| 260 | myNumBlocks++; |
| 261 | |
| 262 | hamfake_writeUnlockSRAM(sram); |
| 263 | } |
| 264 | |
| 265 | void |
| 266 | SRAMSTREAM::readFromSRAM(char *data, int len) |
| 267 | { |
| 268 | char *sram = hamfake_readLockSRAM(); |
| 269 | |
| 270 | sram += mySRAMPtr; |
| 271 | UT_ASSERT(len == 1024); |
| 272 | mySRAMPtr += decompress(data, sram, len); |
| 273 | myNumBlocks++; |
| 274 | |
| 275 | hamfake_readUnlockSRAM(sram); |
| 276 | } |
| 277 | |
| 278 | // This will compress 1k block into dst. |
| 279 | int |
| 280 | SRAMSTREAM::compress(char *dst, char *src, int blocklen) |
| 281 | { |
| 282 | // First, we determine the best method... |
| 283 | int zero = 0; // Number zeros.. |
| 284 | int rlesave = 0; // Amount saved by RLE |
| 285 | int zeroprebyte = 0; // Zeros with byte preproc |
| 286 | int zeropreshort = 0; // Zeros with short preproc |
| 287 | int zeropreint = 0; // Zeros with int preproc |
| 288 | int currunlen, minzero; |
| 289 | int isconstant = 1; |
| 290 | int zerotablesize; |
| 291 | int i; |
| 292 | PREPROCESS_TYPE preprocess; |
| 293 | bool istopblock = false; |
| 294 | |
| 295 | #if 1 |
| 296 | if (blocklen == 1024) |
| 297 | istopblock = true; |
| 298 | #endif |
| 299 | |
| 300 | // Number of bytes needed for a zerotable of this blocklen. |
| 301 | zerotablesize = (blocklen + 7) >> 3; |
| 302 | |
| 303 | currunlen = 0; |
| 304 | for (i = 0; i < blocklen; i++) |
| 305 | { |
| 306 | if (src[i] == 0) |
| 307 | zero++; |
| 308 | if (i && src[i] == src[i-1]) |
| 309 | { |
| 310 | currunlen++; |
| 311 | } |
| 312 | else |
| 313 | currunlen = 1; // Difference, run of 1. |
| 314 | if (currunlen > 3) |
| 315 | rlesave++; // After three codes we save... |
| 316 | if ((!i && !src[i]) || (src[i] == src[i-1])) |
| 317 | zeroprebyte++; |
| 318 | if ((i < 2 && !src[i]) || (src[i] == src[i-2])) |
| 319 | zeropreshort++; |
| 320 | if ((i < 4 && !src[i]) || (src[i] == src[i-4])) |
| 321 | zeropreint++; |
| 322 | else |
| 323 | isconstant = 0; |
| 324 | } |
| 325 | if ((0)) |
| 326 | { |
| 327 | BUF buf; |
| 328 | buf.sprintf("Z:%d,B:%d,S:%d,I:%d", |
| 329 | zero, zeroprebyte, zeropreshort, zeropreint); |
| 330 | if (!mySilent) |
| 331 | msg_report(buf); |
| 332 | } |
| 333 | |
| 334 | // Find best preprocess... |
| 335 | if (zero >= zeroprebyte) |
| 336 | { |
| 337 | minzero = zero; |
| 338 | preprocess = PREPROCESS_NONE; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | minzero = zeroprebyte; |
| 343 | preprocess = PREPROCESS_BYTE; |
| 344 | } |
| 345 | if (minzero < zeropreshort) |
| 346 | { |
| 347 | preprocess = PREPROCESS_SHORT; |
| 348 | minzero = zeropreshort; |
| 349 | } |
| 350 | if (minzero < zeropreint) |
| 351 | { |
| 352 | preprocess = PREPROCESS_INT; |
| 353 | minzero = zeropreint; |
| 354 | } |
| 355 | |
| 356 | // Determine best method... |
| 357 | if (isconstant) |
| 358 | { |
| 359 | //msg_report("Constant..."); |
| 360 | if (!mySilent) |
| 361 | { |
| 362 | if (istopblock) |
| 363 | msg_report("_"); |
| 364 | } |
| 365 | return compressConstant(dst, src); |
| 366 | } |
| 367 | |
| 368 | // May have to punt for uncompressed... |
| 369 | if ((blocklen - minzero + zerotablesize) > blocklen) // && |
| 370 | // (blocklen + 8 - rlesave) > blocklen) |
| 371 | { |
| 372 | // msg_report("None..."); |
| 373 | if (!mySilent) |
| 374 | { |
| 375 | if (istopblock) |
| 376 | msg_report("x"); |
| 377 | } |
| 378 | return compressNone(dst, src, blocklen); |
| 379 | } |
| 380 | |
| 381 | //if ((blocklen - minzero + zerotablesize) < (blocklen - rlesave)) |
| 382 | if ((1)) |
| 383 | { |
| 384 | //msg_report("ZeroTable..."); |
| 385 | if (!mySilent) |
| 386 | { |
| 387 | if (istopblock) |
| 388 | msg_report("."); |
| 389 | } |
| 390 | return compressZeroTable(dst, src, preprocess, blocklen); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | if (!mySilent) |
| 395 | { |
| 396 | if (istopblock) |
| 397 | msg_report("r"); |
| 398 | } |
| 399 | return compressRLE(dst, src, blocklen); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // This will decompress into a 1k block. |
| 404 | int |
| 405 | SRAMSTREAM::decompress(char *dst, char *src, int blocklen) |
| 406 | { |
| 407 | PREPROCESS_TYPE preprocess; |
| 408 | COMPRESS_TYPE compress; |
| 409 | char c; |
| 410 | bool istopblock = false; |
| 411 | |
| 412 | #if 1 |
| 413 | if (blocklen == 1024) |
| 414 | istopblock = true; |
| 415 | #endif |
| 416 | |
| 417 | // Get type code... |
| 418 | c = *src++; |
| 419 | delay(); |
| 420 | |
| 421 | compress = (COMPRESS_TYPE) (c & 0xf); |
| 422 | preprocess = (PREPROCESS_TYPE) ((c >> 4) & 0xf); |
| 423 | |
| 424 | switch (compress) |
| 425 | { |
| 426 | case COMPRESS_NONE: |
| 427 | //msg_report("None..."); |
| 428 | if (istopblock && !mySilent) |
| 429 | msg_report("x"); |
| 430 | return decompressNone(dst, src, blocklen) + 1; |
| 431 | case COMPRESS_CONSTANT: |
| 432 | //msg_report("Constant..."); |
| 433 | if (istopblock && !mySilent) |
| 434 | msg_report("_"); |
| 435 | return decompressConstant(dst, src, blocklen) + 1; |
| 436 | case COMPRESS_ZEROTABLE: |
| 437 | //msg_report("ZeroTable..."); |
| 438 | if (istopblock && !mySilent) |
| 439 | msg_report("."); |
| 440 | return decompressZeroTable(dst, src, preprocess, blocklen) + 1; |
| 441 | case COMPRESS_RLE: |
| 442 | //msg_report("RLE..."); |
| 443 | if (istopblock && !mySilent) |
| 444 | msg_report("r"); |
| 445 | return decompressRLE(dst, src, blocklen); |
| 446 | default: |
| 447 | UT_ASSERT(!"Unknown compression type!"); |
| 448 | return 0; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | SRAMSTREAM::compressConstant(char *dst, char *src) |
| 454 | { |
| 455 | // Write out the constant code, than the four bytes needed... |
| 456 | *dst++ = COMPRESS_CONSTANT; |
| 457 | delay(); |
| 458 | *dst++ = *src++; |
| 459 | delay(); |
| 460 | *dst++ = *src++; |
| 461 | delay(); |
| 462 | *dst++ = *src++; |
| 463 | delay(); |
| 464 | *dst++ = *src++; |
| 465 | delay(); |
| 466 | |
| 467 | return 5; // Fixed 5 bytes.. |
| 468 | } |
| 469 | |
| 470 | int |
| 471 | SRAMSTREAM::decompressConstant(char *dst, char *src, int len) |
| 472 | { |
| 473 | int i; |
| 474 | |
| 475 | UT_ASSERT(! (len & 3)); |
| 476 | for (i = 0; i < (len >> 2); i++) |
| 477 | { |
| 478 | *dst++ = src[0]; |
| 479 | delay(); |
| 480 | *dst++ = src[1]; |
| 481 | delay(); |
| 482 | *dst++ = src[2]; |
| 483 | delay(); |
| 484 | *dst++ = src[3]; |
| 485 | delay(); |
| 486 | } |
| 487 | return 4; |
| 488 | } |
| 489 | |
| 490 | int |
| 491 | SRAMSTREAM::compressNone(char *dst, char *src, int len, bool header) |
| 492 | { |
| 493 | int i; |
| 494 | |
| 495 | if (header) |
| 496 | { |
| 497 | *dst++ = COMPRESS_NONE; |
| 498 | delay(); |
| 499 | } |
| 500 | |
| 501 | for (i = 0; i < len; i++) |
| 502 | { |
| 503 | *dst++ = *src++; |
| 504 | delay(); |
| 505 | } |
| 506 | return len + (header ? 1 : 0); // Fixed size.. |
| 507 | } |
| 508 | |
| 509 | int |
| 510 | SRAMSTREAM::decompressNone(char *dst, char *src, int len) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | for (i = 0; i < len; i++) |
| 515 | { |
| 516 | *dst++ = *src++; |
| 517 | delay(); |
| 518 | } |
| 519 | return len; |
| 520 | } |
| 521 | |
| 522 | int |
| 523 | SRAMSTREAM::compressZeroTable(char *dst, char *src, PREPROCESS_TYPE preprocess, int blocklen) |
| 524 | { |
| 525 | char zerotable[128]; |
| 526 | int zerotablelen; |
| 527 | int i, j, pos, prelen; |
| 528 | char *dststart = dst; |
| 529 | |
| 530 | zerotablelen = (blocklen + 7) >> 3; |
| 531 | |
| 532 | UT_ASSERT(! (blocklen & 7)); |
| 533 | |
| 534 | *dst++ = COMPRESS_ZEROTABLE | (preprocess << 4); |
| 535 | delay(); |
| 536 | |
| 537 | switch (preprocess) |
| 538 | { |
| 539 | case PREPROCESS_NONE: |
| 540 | //msg_report("prenone"); |
| 541 | prelen = 0; |
| 542 | break; |
| 543 | case PREPROCESS_BYTE: |
| 544 | //msg_report("prebyte"); |
| 545 | prelen = 1; |
| 546 | break; |
| 547 | case PREPROCESS_SHORT: |
| 548 | //msg_report("preshort"); |
| 549 | prelen = 2; |
| 550 | break; |
| 551 | case PREPROCESS_INT: |
| 552 | //msg_report("preint"); |
| 553 | prelen = 4; |
| 554 | break; |
| 555 | default: |
| 556 | UT_ASSERT(0); |
| 557 | prelen = 1024; |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | // First, we build the zero table... |
| 562 | pos = 0; |
| 563 | for (i = 0; i < zerotablelen; i++) |
| 564 | { |
| 565 | zerotable[i] = 0; |
| 566 | for (j = 0; j < 8; j++) |
| 567 | { |
| 568 | // If before prelen, act as if pre was zero... |
| 569 | if (pos < prelen) |
| 570 | { |
| 571 | if (src[pos]) |
| 572 | zerotable[i] |= 1 << j; |
| 573 | } |
| 574 | else |
| 575 | { |
| 576 | switch (preprocess) |
| 577 | { |
| 578 | case PREPROCESS_NONE: |
| 579 | if (src[pos]) |
| 580 | zerotable[i] |= 1 << j; |
| 581 | break; |
| 582 | case PREPROCESS_BYTE: |
| 583 | case PREPROCESS_SHORT: |
| 584 | case PREPROCESS_INT: |
| 585 | if (src[pos] != src[pos - prelen]) |
| 586 | zerotable[i] |= 1 << j; |
| 587 | break; |
| 588 | } |
| 589 | } |
| 590 | pos++; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Now save it using simple RLE.. |
| 595 | //dst += compressRLE(dst, zerotable, 128, false); |
| 596 | |
| 597 | // If zerotablelen is short enough, go to no compression. |
| 598 | if (zerotablelen < 20) |
| 599 | dst += compressNone(dst, zerotable, zerotablelen, false); |
| 600 | else |
| 601 | dst += compress(dst, zerotable, zerotablelen); |
| 602 | |
| 603 | // And now save the non-zero entries... |
| 604 | pos = 0; |
| 605 | for (i = 0; i < zerotablelen; i++) |
| 606 | { |
| 607 | for (j = 0; j < 8; j++) |
| 608 | { |
| 609 | if (pos < prelen || (preprocess == PREPROCESS_NONE)) |
| 610 | { |
| 611 | if (zerotable[i] & (1 << j)) |
| 612 | { |
| 613 | *dst++ = src[pos]; |
| 614 | delay(); |
| 615 | } |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | // Modulate our result by the prelen previous one.. |
| 620 | if (zerotable[i] & (1 << j)) |
| 621 | { |
| 622 | *dst++ = src[pos] ^ src[pos-prelen]; |
| 623 | delay(); |
| 624 | } |
| 625 | } |
| 626 | pos++; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | return (dst - dststart); |
| 631 | } |
| 632 | |
| 633 | int |
| 634 | SRAMSTREAM::decompressZeroTable(char *dst, char *src, PREPROCESS_TYPE preprocess, int blocklen) |
| 635 | { |
| 636 | char zerotable[128]; |
| 637 | int zerotablelen; |
| 638 | char *srcstart = src; |
| 639 | int prelen; |
| 640 | int i, j, pos; |
| 641 | char c; |
| 642 | |
| 643 | zerotablelen = (blocklen + 7) >> 3; |
| 644 | UT_ASSERT(! (blocklen & 7) ); |
| 645 | |
| 646 | // Fetch the zero table... |
| 647 | //src += decompressRLE(zerotable, src, 128); |
| 648 | if (zerotablelen < 20) |
| 649 | src += decompressNone(zerotable, src, zerotablelen); |
| 650 | else |
| 651 | src += decompress(zerotable, src, zerotablelen); |
| 652 | |
| 653 | switch (preprocess) |
| 654 | { |
| 655 | case PREPROCESS_NONE: |
| 656 | //msg_report("prenone"); |
| 657 | prelen = 0; |
| 658 | break; |
| 659 | case PREPROCESS_BYTE: |
| 660 | //msg_report("prebyte"); |
| 661 | prelen = 1; |
| 662 | break; |
| 663 | case PREPROCESS_SHORT: |
| 664 | //msg_report("preshort"); |
| 665 | prelen = 2; |
| 666 | break; |
| 667 | case PREPROCESS_INT: |
| 668 | //msg_report("preint"); |
| 669 | prelen = 4; |
| 670 | break; |
| 671 | default: |
| 672 | UT_ASSERT(0); |
| 673 | prelen = 1024; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | pos = 0; |
| 678 | for (i = 0; i < zerotablelen; i++) |
| 679 | { |
| 680 | for (j = 0; j < 8; j++) |
| 681 | { |
| 682 | if (pos < prelen || (preprocess == PREPROCESS_NONE)) |
| 683 | { |
| 684 | c = 0; |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | c = dst[-prelen]; |
| 689 | } |
| 690 | if (zerotable[i] & (1 << j)) |
| 691 | { |
| 692 | c ^= *src++; |
| 693 | } |
| 694 | *dst++ = c; |
| 695 | delay(); |
| 696 | pos++; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | return (src - srcstart); |
| 701 | } |
| 702 | |
| 703 | int |
| 704 | SRAMSTREAM::compressRLE(char *dst, char *src, int len, bool header) |
| 705 | { |
| 706 | char *dststart = dst; |
| 707 | int currun, i; |
| 708 | int runsame; |
| 709 | int n, j; |
| 710 | |
| 711 | UT_ASSERT(!"RLE IS BROKEN!"); |
| 712 | |
| 713 | if (header) |
| 714 | { |
| 715 | *dst++ = COMPRESS_RLE; |
| 716 | delay(); |
| 717 | } |
| 718 | |
| 719 | // The rule is if we see something 3 long, we do a run. This doesn't |
| 720 | // save anything, except it might collapse one of the 8 mandatory |
| 721 | // restarts in an 8k table. |
| 722 | |
| 723 | currun = 0; |
| 724 | runsame = 0; |
| 725 | i = 0; |
| 726 | while (i < 1024) |
| 727 | { |
| 728 | if (currun && (src[i-1] == src[i])) |
| 729 | runsame++; |
| 730 | else |
| 731 | { |
| 732 | // We have hit the end of a same run.. Properly write out |
| 733 | // if 3 or greater. |
| 734 | if (runsame > 3) |
| 735 | { |
| 736 | // TODO: Is this off by one? |
| 737 | j = i - currun; // Get back to the start... |
| 738 | n = currun - runsame; // number different prior to same. |
| 739 | if (n) |
| 740 | { |
| 741 | *dst++ = n-1; // No high bit, raw data... |
| 742 | delay(); |
| 743 | while (n--) |
| 744 | { |
| 745 | *dst++ = src[j++]; |
| 746 | delay(); |
| 747 | } |
| 748 | } |
| 749 | // Now, dump out the run proper... |
| 750 | *dst++ = (runsame-1) | 0x80; // high bit for rle |
| 751 | delay(); |
| 752 | *dst++ = src[i-1]; // Last valid... |
| 753 | delay(); |
| 754 | |
| 755 | // We now want i to be on the stack, so a currun of 1 |
| 756 | // and runsame of 1. |
| 757 | runsame = currun = 1; |
| 758 | i++; |
| 759 | continue; |
| 760 | } |
| 761 | // Otherwise, we keep trucking resetting runsame to 1. |
| 762 | runsame = 1; |
| 763 | } |
| 764 | currun++; |
| 765 | if (currun == 128) |
| 766 | { |
| 767 | // We need to dump out this sequence and reset... |
| 768 | // If runsame == currun, we had 128 in a row the same. |
| 769 | // Otherwise, it is raw data time... |
| 770 | if (runsame == currun) |
| 771 | { |
| 772 | // NOTE: This errors out on MSVC. |
| 773 | // Good thing we don't use runlength due to some |
| 774 | // other error |
| 775 | *dst++ = (char) (unsigned char)0xFF; // (128 - 1) | 0x80 |
| 776 | delay(); |
| 777 | *dst++ = src[i]; |
| 778 | delay(); |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | j = i - currun; |
| 783 | n = 128; |
| 784 | *dst++ = n-1; // No high bit, raw data... |
| 785 | delay(); |
| 786 | while (n--) |
| 787 | { |
| 788 | *dst++ = src[j++]; |
| 789 | delay(); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | // Now we have dumped out a full block, reset everything.. |
| 794 | runsame = currun = 0; |
| 795 | i++; |
| 796 | continue; |
| 797 | } |
| 798 | |
| 799 | i++; |
| 800 | } |
| 801 | |
| 802 | // i hit 1024. Need to dump what is left... |
| 803 | if (currun) |
| 804 | { |
| 805 | if (runsame == currun) |
| 806 | { |
| 807 | *dst++ = 0x80 | (currun - 1); |
| 808 | delay(); |
| 809 | *dst++ = src[i]; |
| 810 | delay(); |
| 811 | } |
| 812 | else |
| 813 | { |
| 814 | j = i - currun; |
| 815 | n = currun; |
| 816 | *dst++ = n-1; // No high bit, raw data... |
| 817 | delay(); |
| 818 | while (n--) |
| 819 | { |
| 820 | *dst++ = src[j++]; |
| 821 | delay(); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | return (dst - dststart); |
| 827 | } |
| 828 | |
| 829 | int |
| 830 | SRAMSTREAM::decompressRLE(char *dst, char *src, int len) |
| 831 | { |
| 832 | int n; |
| 833 | char c; |
| 834 | char *srcstart = src; |
| 835 | |
| 836 | while (len > 0) |
| 837 | { |
| 838 | c = *src++; |
| 839 | delay(); |
| 840 | if (c & 0x80) |
| 841 | { |
| 842 | // RLE... |
| 843 | n = c & 0x7f; |
| 844 | n++; |
| 845 | c = *src++; |
| 846 | delay(); |
| 847 | while (n--) |
| 848 | { |
| 849 | *dst++ = c; |
| 850 | len--; |
| 851 | } |
| 852 | } |
| 853 | else |
| 854 | { |
| 855 | // Raw data... |
| 856 | n = c & 0x7f; |
| 857 | n++; |
| 858 | while (n--) |
| 859 | { |
| 860 | *dst++ = *src++; |
| 861 | len--; |
| 862 | delay(); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | return (src - srcstart); |
| 868 | } |
| 869 | |
| 870 | // |
| 871 | // Hamfake methods defined in the non-SDL world. |
| 872 | // |
| 873 | #if !defined(USING_SDL) && !defined(USING_DS) |
| 874 | |
| 875 | char * |
| 876 | hamfake_readLockSRAM() |
| 877 | { |
| 878 | return (char *) 0xE000000; |
| 879 | } |
| 880 | |
| 881 | void |
| 882 | hamfake_readUnlockSRAM(char *) |
| 883 | { |
| 884 | } |
| 885 | |
| 886 | char * |
| 887 | hamfake_writeLockSRAM() |
| 888 | { |
| 889 | return (char *) 0xE000000; |
| 890 | } |
| 891 | |
| 892 | void |
| 893 | hamfake_writeUnlockSRAM(char *) |
| 894 | { |
| 895 | } |
| 896 | |
| 897 | // This is being written whilst travelling at a great land speed. |
| 898 | // I'm on the Nozomi Super Express Shinkasen, speeding past Mt. Fuji. |
| 899 | void |
| 900 | hamfake_endWritingSession() |
| 901 | { |
| 902 | } |
| 903 | |
| 904 | #endif |