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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
|
/* tape.c: Routines for handling tape files
Copyright (c) 2001-2018 Philip Kendall, Darren Salt, Fredrick Meunier
This program 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 of the License, or
(at your option) any later version.
This program 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.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "internals.h"
#include "tape_block.h"
/* The tape type itself */
struct libspectrum_tape {
/* All the blocks */
GSList* blocks;
/* The last block */
GSList* last_block;
/* The state of the current block */
libspectrum_tape_block_state state;
};
/*** Constants ***/
/* The timings for the standard ROM loader */
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_PILOT = 2168; /*Pilot*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_SYNC1 = 667; /*Sync 1*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_SYNC2 = 735; /*Sync 2*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_DATA0 = 855; /*Reset*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_DATA1 = 1710; /*Set*/
const libspectrum_dword LIBSPECTRUM_TAPE_TIMING_TAIL = 945; /*Tail*/
/*** Local function prototypes ***/
/* Free the memory used by a block */
static void
block_free( gpointer data, gpointer user_data );
/* Functions to get the next edge */
static libspectrum_error
rom_edge( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
static libspectrum_error
rom_next_bit( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state );
static libspectrum_error
turbo_edge( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state,
libspectrum_dword *tstates,
int *end_of_block, int *flags );
static libspectrum_error
turbo_next_bit( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state );
static libspectrum_error
tone_edge( libspectrum_tape_pure_tone_block *block,
libspectrum_tape_pure_tone_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pulses_edge( libspectrum_tape_pulses_block *block,
libspectrum_tape_pulses_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pure_data_edge( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
static libspectrum_error
raw_data_edge( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags );
static libspectrum_error
jump_blocks( libspectrum_tape *tape, int offset );
static libspectrum_error
rle_pulse_edge( libspectrum_tape_rle_pulse_block *block,
libspectrum_tape_rle_pulse_block_state *state,
libspectrum_dword *tstates, int *end_of_block );
static libspectrum_error
pulse_sequence_edge( libspectrum_tape_pulse_sequence_block *block,
libspectrum_tape_pulse_sequence_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags );
libspectrum_error
libspectrum_tape_data_block_next_bit( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state );
static libspectrum_error
data_block_edge( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags );
/*** Function definitions ****/
/* Allocate a list of blocks */
libspectrum_tape*
libspectrum_tape_alloc( void )
{
libspectrum_tape *tape = libspectrum_new( libspectrum_tape, 1 );
tape->blocks = NULL;
tape->last_block = NULL;
libspectrum_tape_iterator_init( &(tape->state.current_block), tape );
tape->state.loop_block = NULL;
return tape;
}
/* Free the memory used by a list of blocks, but not the object itself */
libspectrum_error
libspectrum_tape_clear( libspectrum_tape *tape )
{
g_slist_foreach( tape->blocks, block_free, NULL );
g_slist_free( tape->blocks );
tape->blocks = NULL;
libspectrum_tape_iterator_init( &(tape->state.current_block), tape );
return LIBSPECTRUM_ERROR_NONE;
}
/* Free up a list of blocks */
libspectrum_error
libspectrum_tape_free( libspectrum_tape *tape )
{
libspectrum_error error;
error = libspectrum_tape_clear( tape );
if( error ) return error;
libspectrum_free( tape );
return LIBSPECTRUM_ERROR_NONE;
}
/* Free the memory used by one block */
static void
block_free( gpointer data, gpointer user_data GCC_UNUSED )
{
libspectrum_tape_block_free( data );
}
/* Read in a tape file, optionally guessing what sort of file it is */
libspectrum_error
libspectrum_tape_read( libspectrum_tape *tape, const libspectrum_byte *buffer,
size_t length, libspectrum_id_t type,
const char *filename )
{
libspectrum_id_t raw_type;
libspectrum_class_t class;
libspectrum_byte *new_buffer;
libspectrum_error error;
/* If we don't know what sort of file this is, make a best guess */
if( type == LIBSPECTRUM_ID_UNKNOWN ) {
error = libspectrum_identify_file( &type, filename, buffer, length );
if( error ) return error;
/* If we still can't identify it, give up */
if( type == LIBSPECTRUM_ID_UNKNOWN ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_tape_read: couldn't identify file"
);
return LIBSPECTRUM_ERROR_UNKNOWN;
}
}
/* Find out if this file needs decompression */
new_buffer = NULL;
error = libspectrum_identify_file_raw( &raw_type, filename, buffer, length );
if( error ) return error;
error = libspectrum_identify_class( &class, raw_type );
if( error ) return error;
if( class == LIBSPECTRUM_CLASS_COMPRESSED ) {
size_t new_length;
error = libspectrum_uncompress_file( &new_buffer, &new_length, NULL,
raw_type, buffer, length, NULL );
if( error ) return error;
buffer = new_buffer; length = new_length;
}
switch( type ) {
case LIBSPECTRUM_ID_TAPE_TAP:
case LIBSPECTRUM_ID_TAPE_SPC:
case LIBSPECTRUM_ID_TAPE_STA:
case LIBSPECTRUM_ID_TAPE_LTP:
error = internal_tap_read( tape, buffer, length, type ); break;
case LIBSPECTRUM_ID_TAPE_TZX:
error = internal_tzx_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_WARAJEVO:
error = internal_warajevo_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_Z80EM:
error = libspectrum_z80em_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_CSW:
error = libspectrum_csw_read( tape, buffer, length ); break;
case LIBSPECTRUM_ID_TAPE_WAV:
#ifdef HAVE_LIB_AUDIOFILE
error = libspectrum_wav_read( tape, filename ); break;
#else /* #ifdef HAVE_LIB_AUDIOFILE */
error = LIBSPECTRUM_ERROR_LOGIC;
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_read: format not supported without libaudiofile"
);
break;
#endif /* #ifdef HAVE_LIB_AUDIOFILE */
case LIBSPECTRUM_ID_TAPE_PZX:
error = internal_pzx_read( tape, buffer, length ); break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_tape_read: not a tape file" );
libspectrum_free( new_buffer );
return LIBSPECTRUM_ERROR_CORRUPT;
}
libspectrum_free( new_buffer );
return error;
}
libspectrum_error
libspectrum_tape_write( libspectrum_byte **buffer, size_t *length,
libspectrum_tape *tape, libspectrum_id_t type )
{
libspectrum_byte *ptr = *buffer;
libspectrum_buffer *new_buffer;
libspectrum_class_t class;
libspectrum_error error;
error = libspectrum_identify_class( &class, type );
if( error ) return error;
if( class != LIBSPECTRUM_CLASS_TAPE ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_INVALID,
"libspectrum_tape_write: not a tape format" );
return LIBSPECTRUM_ERROR_INVALID;
}
/* Allow for uninitialised buffer on entry */
if( !*length ) *buffer = NULL;
new_buffer = libspectrum_buffer_alloc();
switch( type ) {
case LIBSPECTRUM_ID_TAPE_TAP:
case LIBSPECTRUM_ID_TAPE_SPC:
case LIBSPECTRUM_ID_TAPE_STA:
case LIBSPECTRUM_ID_TAPE_LTP:
error = internal_tap_write( new_buffer, tape, type );
break;
case LIBSPECTRUM_ID_TAPE_TZX:
error = internal_tzx_write( new_buffer, tape );
break;
case LIBSPECTRUM_ID_TAPE_CSW:
error = libspectrum_csw_write( new_buffer, tape );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_tape_write: format not supported" );
error = LIBSPECTRUM_ERROR_UNKNOWN;
break;
}
libspectrum_buffer_append( buffer, length, &ptr, new_buffer );
libspectrum_buffer_free( new_buffer );
return error;
}
/* Does this tape structure actually contain a tape? */
int
libspectrum_tape_present( const libspectrum_tape *tape )
{
return tape->blocks != NULL;
}
/* Some flags which may be set after calling libspectrum_tape_get_next_edge */
const int LIBSPECTRUM_TAPE_FLAGS_BLOCK = 1 << 0; /* End of block */
const int LIBSPECTRUM_TAPE_FLAGS_STOP = 1 << 1; /* Stop tape */
const int LIBSPECTRUM_TAPE_FLAGS_STOP48 = 1 << 2; /* Stop tape if in
48K mode */
const int LIBSPECTRUM_TAPE_FLAGS_NO_EDGE = 1 << 3; /* Not an edge really */
const int LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW = 1 << 4; /* Set level low */
const int LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH = 1 << 5; /* Set level high */
const int LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT = 1 << 6;/* Short edge; used for
loader acceleration */
const int LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG = 1 << 7; /* Long edge; used for
loader acceleration */
const int LIBSPECTRUM_TAPE_FLAGS_TAPE = 1 << 8; /* End of tape */
libspectrum_error
libspectrum_tape_get_next_edge_internal( libspectrum_dword *tstates,
int *flags,
libspectrum_tape *tape,
libspectrum_tape_block_state *it )
{
int error;
libspectrum_tape_block *block =
libspectrum_tape_iterator_current( it->current_block );
/* Has this edge ended the block? */
int end_of_block = 0;
/* After getting a new block, do we want to advance to the next one? */
int no_advance = 0;
/* Assume no special flags by default */
*flags = 0;
if( block ) {
switch( block->type ) {
case LIBSPECTRUM_TAPE_BLOCK_ROM:
error = rom_edge( &(block->types.rom), &(it->block_state.rom), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_TURBO:
error = turbo_edge( &(block->types.turbo), &(it->block_state.turbo), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_TONE:
error = tone_edge( &(block->types.pure_tone), &(it->block_state.pure_tone),
tstates, &end_of_block );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSES:
error = pulses_edge( &(block->types.pulses), &(it->block_state.pulses),
tstates, &end_of_block );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA:
error = pure_data_edge( &(block->types.pure_data),
&(it->block_state.pure_data), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA:
error = raw_data_edge( &(block->types.raw_data), &(it->block_state.raw_data),
tstates, &end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA:
error = generalised_data_edge( &(block->types.generalised_data),
&(it->block_state.generalised_data),
tstates, &end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PAUSE:
*tstates = block->types.pause.length_tstates; end_of_block = 1;
/* If the pause isn't a "don't care" level then set the appropriate pulse
level */
if( block->types.pause.level != -1 &&
block->types.pause.length_tstates ) {
*flags |= block->types.pause.level ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
}
/* 0 ms pause => stop tape */
if( *tstates == 0 ) { *flags |= LIBSPECTRUM_TAPE_FLAGS_STOP; }
break;
case LIBSPECTRUM_TAPE_BLOCK_JUMP:
error = jump_blocks( tape, block->types.jump.offset );
if( error ) return error;
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
no_advance = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_START:
if( it->current_block->next && block->types.loop_start.count ) {
it->loop_block = it->current_block->next;
it->loop_count = block->types.loop_start.count;
}
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_END:
if( it->loop_block ) {
if( --(it->loop_count) ) {
it->current_block = it->loop_block;
no_advance = 1;
} else {
it->loop_block = NULL;
}
}
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_STOP48:
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_STOP48; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_SET_SIGNAL_LEVEL:
*tstates = 0; end_of_block = 1;
/* Inverted as the following block will flip the level before recording
the edge */
*flags |= block->types.set_signal_level.level ?
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW : LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
/* For blocks which contain no Spectrum-readable data, return zero
tstates and set end of block set so we instantly get the next block */
case LIBSPECTRUM_TAPE_BLOCK_GROUP_START:
case LIBSPECTRUM_TAPE_BLOCK_GROUP_END:
case LIBSPECTRUM_TAPE_BLOCK_SELECT:
case LIBSPECTRUM_TAPE_BLOCK_COMMENT:
case LIBSPECTRUM_TAPE_BLOCK_MESSAGE:
case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO:
case LIBSPECTRUM_TAPE_BLOCK_HARDWARE:
case LIBSPECTRUM_TAPE_BLOCK_CUSTOM:
*tstates = 0; *flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE; end_of_block = 1;
break;
case LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE:
error = rle_pulse_edge( &(block->types.rle_pulse),
&(it->block_state.rle_pulse), tstates, &end_of_block);
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSE_SEQUENCE:
error = pulse_sequence_edge( &(block->types.pulse_sequence),
&(it->block_state.pulse_sequence), tstates,
&end_of_block, flags );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_BLOCK_DATA_BLOCK:
error = data_block_edge( &(block->types.data_block),
&(it->block_state.data_block), tstates,
&end_of_block, flags );
if( error ) return error;
break;
default:
*tstates = 0;
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_get_next_edge: unknown block type 0x%02x",
block->type
);
return LIBSPECTRUM_ERROR_LOGIC;
}
} else {
*tstates = 0;
end_of_block = 1;
}
/* If that ended the block, move onto the next block */
if( end_of_block ) {
*flags |= LIBSPECTRUM_TAPE_FLAGS_BLOCK;
/* Advance to the next block, unless we've been told not to */
if( !no_advance ) {
libspectrum_tape_iterator_next( &(it->current_block) );
/* If we've just hit the end of the tape, stop the tape (and
then `rewind' to the start) */
if( libspectrum_tape_iterator_current( it->current_block ) == NULL ) {
*flags |= LIBSPECTRUM_TAPE_FLAGS_STOP;
*flags |= LIBSPECTRUM_TAPE_FLAGS_TAPE;
/* Need to have an edge at the end of the tape to terminate the last
pulse so clear the NO_EDGE flag if it has been set */
*flags &= ~LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
libspectrum_tape_iterator_init( &(it->current_block), tape );
}
}
/* Initialise the new block */
error = libspectrum_tape_block_init(
libspectrum_tape_iterator_current( it->current_block ),
it );
if( error ) return error;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* The main function: called with a tape object and returns the number of
t-states until the next edge, and a marker if this was the last edge
on the tape */
libspectrum_error
libspectrum_tape_get_next_edge( libspectrum_dword *tstates, int *flags,
libspectrum_tape *tape )
{
return libspectrum_tape_get_next_edge_internal( tstates, flags, tape,
&(tape->state) );
}
/* TZX pauses should have no edge if there is no duration, from the spec:
A 'Pause' block of zero duration is completely ignored, so the 'current pulse
level' will NOT change in this case. This also applies to 'Data' blocks that
have some pause duration included in them. */
static void
do_tail_pause( libspectrum_dword *tstates,
int *end_of_block, int *flags )
{
*end_of_block = 1;
if( *tstates == 0 ) {
/* The tail pause is optional - if there is no tail, there is no edge */
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
}
}
static libspectrum_error
rom_edge( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state,
libspectrum_dword *tstates,
int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT:
/* The next edge occurs in one pilot edge timing */
*tstates = LIBSPECTRUM_TAPE_TIMING_PILOT;
/* If that was the last pilot edge, change state */
if( --(state->edge_count) == 0 )
state->state = LIBSPECTRUM_TAPE_STATE_SYNC1;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC1:
/* The first short sync pulse */
*tstates = LIBSPECTRUM_TAPE_TIMING_SYNC1;
/* Followed by the second sync pulse */
state->state = LIBSPECTRUM_TAPE_STATE_SYNC2;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC2:
/* The second short sync pulse */
*tstates = LIBSPECTRUM_TAPE_TIMING_SYNC2;
/* Followed by the first bit of data */
error = rom_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == LIBSPECTRUM_TAPE_TIMING_DATA0 ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == LIBSPECTRUM_TAPE_TIMING_DATA0 ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = rom_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"rom_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rom_next_bit( libspectrum_tape_rom_block *block,
libspectrum_tape_rom_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
state->bits_through_byte = 0;
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? LIBSPECTRUM_TAPE_TIMING_DATA1
: LIBSPECTRUM_TAPE_TIMING_DATA0 );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
turbo_edge( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT:
/* Check we actually have some edges */
if( state->edge_count-- != 0 ) {
*tstates = block->pilot_length;
break;
}
/* Fall through */
case LIBSPECTRUM_TAPE_STATE_SYNC1:
/* The first short sync pulse */
*tstates = block->sync1_length;
/* Followed by the second sync pulse */
state->state = LIBSPECTRUM_TAPE_STATE_SYNC2;
break;
case LIBSPECTRUM_TAPE_STATE_SYNC2:
/* The second short sync pulse */
*tstates = block->sync2_length;
/* Followed by the first bit of data */
error = turbo_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = turbo_next_bit( block, state ); if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"turbo_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
turbo_next_bit( libspectrum_tape_turbo_block *block,
libspectrum_tape_turbo_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
/* If we're looking at the last byte, take account of the fact it
may have less than 8 bits in it */
if( state->bytes_through_block == block->length-1 ) {
state->bits_through_byte = 8 - block->bits_in_last_byte;
} else {
state->bits_through_byte = 0;
}
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? block->bit1_length : block->bit0_length );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
tone_edge( libspectrum_tape_pure_tone_block *block,
libspectrum_tape_pure_tone_block_state *state,
libspectrum_dword *tstates, int *end_of_block )
{
/* The next edge occurs in one pilot edge timing */
*tstates = block->length;
/* If that was the last edge, the block is finished */
if( --(state->edge_count) == 0 ) (*end_of_block) = 1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
pulses_edge( libspectrum_tape_pulses_block *block,
libspectrum_tape_pulses_block_state *state,
libspectrum_dword *tstates, int *end_of_block )
{
/* Get the length of this edge */
*tstates = block->lengths[ state->edge_count ];
/* Was that the last edge? */
if( ++(state->edge_count) == block->count ) (*end_of_block) = 1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
pure_data_edge( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The first edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the second edge */
state->state = LIBSPECTRUM_TAPE_STATE_DATA2;
break;
case LIBSPECTRUM_TAPE_STATE_DATA2:
/* The second edge for a bit of data */
*tstates = state->bit_tstates;
*flags |= ( state->bit_tstates == block->bit0_length ) ? LIBSPECTRUM_TAPE_FLAGS_LENGTH_SHORT : LIBSPECTRUM_TAPE_FLAGS_LENGTH_LONG;
/* Followed by the next bit of data (or the end of data) */
error = libspectrum_tape_pure_data_next_bit( block, state );
if( error ) return error;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"pure_data_edge: unknown state %d",
state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_tape_pure_data_next_bit( libspectrum_tape_pure_data_block *block,
libspectrum_tape_pure_data_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the pause at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
/* If we're looking at the last byte, take account of the fact it
may have less than 8 bits in it */
if( state->bytes_through_block == block->length-1 ) {
state->bits_through_byte = 8 - block->bits_in_last_byte;
} else {
state->bits_through_byte = 0;
}
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set the timing and state for another data bit */
state->bit_tstates = ( next_bit ? block->bit1_length : block->bit0_length );
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
raw_data_edge( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags )
{
switch (state->state) {
case LIBSPECTRUM_TAPE_STATE_DATA1:
*tstates = state->bit_tstates;
libspectrum_tape_raw_data_next_bit( block, state );
/* Backwards as last bit is the state of the next bit as it has already been
updated*/
*flags |= state->last_bit ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"raw_edge: unknown state %d", state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
void
libspectrum_tape_raw_data_next_bit( libspectrum_tape_raw_data_block *block,
libspectrum_tape_raw_data_block_state *state )
{
int length = 0;
if( state->bytes_through_block == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
state->last_bit ^= 0x80;
return;
}
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
/* Step through the data until we find an edge */
do {
size_t bits_in_byte = (state->bytes_through_block == block->length - 1) ?
block->bits_in_last_byte : 8;
length++;
if( ++(state->bits_through_byte) == bits_in_byte ) {
state->bits_through_byte = 0;
if( ++(state->bytes_through_block) == block->length )
break;
}
} while( ( block->data[state->bytes_through_block] <<
state->bits_through_byte & 0x80 ) != state->last_bit) ;
state->bit_tstates = length * block->bit_length;
state->last_bit ^= 0x80;
}
static libspectrum_byte
get_generalised_data_bit( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state )
{
libspectrum_byte r = state->current_byte & 0x80 ? 1 : 0;
state->current_byte <<= 1;
if( ++state->bits_through_byte == 8 ) {
state->bits_through_byte = 0;
state->bytes_through_stream++;
state->current_byte = block->data[ state->bytes_through_stream ];
}
return r;
}
libspectrum_byte
get_generalised_data_symbol( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state )
{
libspectrum_byte symbol;
size_t i;
for( i = 0, symbol = 0;
i < block->bits_per_data_symbol;
i++ ) {
symbol <<= 1;
symbol |= get_generalised_data_bit( block, state );
}
return symbol;
}
static void
set_tstates_and_flags( libspectrum_tape_generalised_data_symbol *symbol,
libspectrum_byte edge, libspectrum_dword *tstates,
int *flags )
{
*tstates = symbol->lengths[ edge ];
if( !edge ) {
switch( symbol->edge_type ) {
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_EDGE:
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_NO_EDGE:
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_LOW:
*flags |= LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
break;
case LIBSPECTRUM_TAPE_GENERALISED_DATA_SYMBOL_HIGH:
*flags |= LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH;
break;
}
}
}
libspectrum_error
generalised_data_edge( libspectrum_tape_generalised_data_block *block,
libspectrum_tape_generalised_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block,
int *flags )
{
libspectrum_tape_generalised_data_symbol_table *table;
libspectrum_tape_generalised_data_symbol *symbol;
size_t current_symbol;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_PILOT:
table = &( block->pilot_table );
current_symbol = block->pilot_symbols[ state->run ];
symbol = &( table->symbols[ current_symbol ] );
set_tstates_and_flags( symbol, state->edges_through_symbol, tstates,
flags );
state->edges_through_symbol++;
if( state->edges_through_symbol == table->max_pulses ||
symbol->lengths[ state->edges_through_symbol ] == 0 ) {
state->edges_through_symbol = 0;
if( ++state->symbols_through_run == block->pilot_repeats[ state->run ] ) {
state->symbols_through_run = 0;
if( ++state->run == table->symbols_in_block ) {
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
state->bits_through_byte = 0;
state->bytes_through_stream = 0;
state->symbols_through_stream = 0;
state->current_byte = block->data[ 0 ];
state->current_symbol = get_generalised_data_symbol( block, state );
}
}
}
break;
case LIBSPECTRUM_TAPE_STATE_DATA1:
table = &( block->data_table );
symbol = &( table->symbols[ state->current_symbol ] );
set_tstates_and_flags( symbol, state->edges_through_symbol, tstates,
flags );
state->edges_through_symbol++;
if( state->edges_through_symbol == table->max_pulses ||
symbol->lengths[ state->edges_through_symbol ] == 0 ) {
if( ++state->symbols_through_stream == table->symbols_in_block ) {
state->state = LIBSPECTRUM_TAPE_STATE_PAUSE;
} else {
state->edges_through_symbol = 0;
state->current_symbol = get_generalised_data_symbol( block, state );
}
}
break;
case LIBSPECTRUM_TAPE_STATE_PAUSE:
/* The pause at the end of the block */
*tstates = block->pause_tstates;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC, "%s: unknown state %d",
__func__, state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
jump_blocks( libspectrum_tape *tape, int offset )
{
gint current_position; GSList *new_block;
current_position =
g_slist_position( tape->blocks, tape->state.current_block );
if( current_position == -1 ) return LIBSPECTRUM_ERROR_LOGIC;
new_block = g_slist_nth( tape->blocks, current_position + offset );
if( new_block == NULL ) return LIBSPECTRUM_ERROR_CORRUPT;
tape->state.current_block = new_block;
return LIBSPECTRUM_ERROR_NONE;
}
/* Extra, non-TZX, blocks which can be handled as if TZX */
static libspectrum_error
rle_pulse_edge( libspectrum_tape_rle_pulse_block *block,
libspectrum_tape_rle_pulse_block_state *state,
libspectrum_dword *tstates, int *end_of_block )
{
if( block->data[state->index] ) {
*tstates = block->scale * block->data[ state->index++ ];
} else {
if( state->index + 5 > block->length ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"rle_pulse_edge: file is truncated\n" );
return LIBSPECTRUM_ERROR_LOGIC;
}
*tstates = block->scale * ( block->data[ state->index + 1 ] |
block->data[ state->index + 2 ] << 8 |
block->data[ state->index + 3 ] << 16 |
block->data[ state->index + 4 ] << 24 );
state->index += 5;
}
if( state->index == block->length ) *end_of_block = 1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
pulse_sequence_edge( libspectrum_tape_pulse_sequence_block *block,
libspectrum_tape_pulse_sequence_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int new_level = state->level;
/* Get the length of this edge */
*tstates = 0;
/* Skip past any 0 blocks until we find a non 0 block or reach the end of the
block, keeping track of the current mic level */
while( !( *tstates || *end_of_block ) ) {
*tstates = block->lengths[ state->index ];
new_level = !new_level;
/* Was that the last repeat of this pulse block? */
if( ++(state->pulse_count) == block->pulse_repeats[ state->index ] ) {
state->index++;
/* Was that the last block available? */
if( state->index >= block->count ) {
/* Next block */
(*end_of_block) = 1;
} else {
/* Next pulse block */
state->pulse_count = 0;
}
}
}
if( new_level != state->level ) {
*flags |= new_level ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
state->level = new_level;
} else if( !(*tstates) ) {
/* If the net effect of this edge was 0 tstates and no level change, it was
much ado about nothing */
*flags |= LIBSPECTRUM_TAPE_FLAGS_NO_EDGE;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_tape_data_block_next_bit( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state )
{
int next_bit;
/* Have we finished the current byte? */
if( ++(state->bits_through_byte) == 8 ) {
/* If so, have we finished the entire block? If so, all we've got
left after this is the tail at the end */
if( ++(state->bytes_through_block) == block->length ) {
state->state = LIBSPECTRUM_TAPE_STATE_TAIL;
return LIBSPECTRUM_ERROR_NONE;
}
/* If we've finished the current byte, but not the entire block,
get the next byte */
state->current_byte = block->data[ state->bytes_through_block ];
/* If we're looking at the last byte, take account of the fact it
may have less than 8 bits in it */
if( state->bytes_through_block == block->length-1 ) {
state->bits_through_byte = 8 - block->bits_in_last_byte;
} else {
state->bits_through_byte = 0;
}
}
/* Get the high bit, and shift the byte out leftwards */
next_bit = state->current_byte & 0x80;
state->current_byte <<= 1;
/* And set state for another data bit */
state->bit_pulses = ( next_bit ? block->bit1_pulses : block->bit0_pulses );
state->pulse_count = ( next_bit ? block->bit1_pulse_count :
block->bit0_pulse_count );
state->bit_flags = ( next_bit ? state->bit1_flags : state->bit0_flags );
state->index = 0;
state->state = LIBSPECTRUM_TAPE_STATE_DATA1;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
data_block_edge( libspectrum_tape_data_block *block,
libspectrum_tape_data_block_state *state,
libspectrum_dword *tstates, int *end_of_block, int *flags )
{
int error;
switch( state->state ) {
case LIBSPECTRUM_TAPE_STATE_DATA1:
/* The next pulse for a bit of data */
*tstates = state->bit_pulses[ state->index ];
*flags |= state->bit_flags;
if( ++(state->index) == state->pulse_count ) {
/* Followed by the next bit of data (or the end of data) */
error = libspectrum_tape_data_block_next_bit( block, state );
if( error ) return error;
}
break;
case LIBSPECTRUM_TAPE_STATE_TAIL:
/* The pulse at the end of the block */
*tstates = block->tail_length;
do_tail_pause( tstates, end_of_block, flags );
break;
default:
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"data_block_edge: unknown state %d",
state->state );
return LIBSPECTRUM_ERROR_LOGIC;
}
if( !(*flags & LIBSPECTRUM_TAPE_FLAGS_NO_EDGE )) {
*flags |= state->level ? LIBSPECTRUM_TAPE_FLAGS_LEVEL_HIGH :
LIBSPECTRUM_TAPE_FLAGS_LEVEL_LOW;
state->level = !state->level;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* Get the current block */
libspectrum_tape_block*
libspectrum_tape_current_block( libspectrum_tape *tape )
{
return libspectrum_tape_iterator_current( tape->state.current_block );
}
/* Peek at the next block on the tape */
libspectrum_tape_block*
libspectrum_tape_peek_next_block( libspectrum_tape *tape )
{
libspectrum_tape_block *block;
if( !tape->state.current_block ) return NULL;
block = libspectrum_tape_iterator_current( tape->state.current_block->next );
return block ? block : tape->blocks->data;
}
/* Peek at the last block on the tape */
libspectrum_tape_block WIN32_DLL *
libspectrum_tape_peek_last_block( libspectrum_tape *tape )
{
return tape->last_block ? tape->last_block->data : NULL;
}
/* Cause the next block on the tape to be active, initialise it
and return it */
libspectrum_tape_block*
libspectrum_tape_select_next_block( libspectrum_tape *tape )
{
libspectrum_tape_block *block;
if( !tape->state.current_block ) return NULL;
block = libspectrum_tape_iterator_next( &(tape->state.current_block) );
if( !block )
block = libspectrum_tape_iterator_init( &(tape->state.current_block), tape );
if( libspectrum_tape_block_init( block, &(tape->state) ) )
return NULL;
return block;
}
/* Get the position on the tape of the current block */
libspectrum_error
libspectrum_tape_position( int *n, libspectrum_tape *tape )
{
*n = g_slist_position( tape->blocks, tape->state.current_block );
if( *n == -1 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_position: current block is not in tape!"
);
return LIBSPECTRUM_ERROR_LOGIC;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* Select the nth block on the tape */
libspectrum_error
libspectrum_tape_nth_block( libspectrum_tape *tape, int n )
{
GSList *new_block;
libspectrum_error error;
new_block = g_slist_nth( tape->blocks, n );
if( !new_block ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_tape_nth_block: tape does not have block %d", n
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
tape->state.current_block = new_block;
error = libspectrum_tape_block_init( tape->state.current_block->data,
&(tape->state) );
if( error ) return error;
return LIBSPECTRUM_ERROR_NONE;
}
void
libspectrum_tape_append_block( libspectrum_tape *tape,
libspectrum_tape_block *block )
{
if( tape->blocks == NULL ) {
tape->blocks = g_slist_append( tape->blocks, (gpointer)block );
tape->last_block = tape->blocks;
} else {
tape->last_block =
g_slist_append( tape->last_block, (gpointer)block )->next;
}
/* If we previously didn't have a tape loaded ( implied by
tape->current_block == NULL ), set up so that we point to the
start of the tape */
if( !tape->state.current_block ) {
tape->state.current_block = tape->blocks;
libspectrum_tape_block_init( tape->blocks->data, &(tape->state) );
}
}
void
libspectrum_tape_remove_block( libspectrum_tape *tape,
libspectrum_tape_iterator it )
{
if( it->data ) libspectrum_tape_block_free( it->data );
tape->blocks = g_slist_delete_link( tape->blocks, it );
tape->last_block = g_slist_last( tape->blocks );
}
libspectrum_error
libspectrum_tape_insert_block( libspectrum_tape *tape,
libspectrum_tape_block *block,
size_t position )
{
tape->blocks = g_slist_insert( tape->blocks, block, position );
tape->last_block = g_slist_last( tape->blocks );
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_tape_block_description( char *buffer, size_t length,
libspectrum_tape_block *block )
{
switch( block->type ) {
case LIBSPECTRUM_TAPE_BLOCK_ROM:
strncpy( buffer, "Standard Speed Data", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_TURBO:
strncpy( buffer, "Turbo Speed Data", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_TONE:
strncpy( buffer, "Pure Tone", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSES:
strncpy( buffer, "List of Pulses", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA:
strncpy( buffer, "Pure Data", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA:
strncpy( buffer, "Raw Data", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_GENERALISED_DATA:
strncpy( buffer, "Generalised Data", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_PAUSE:
strncpy( buffer, "Pause", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_GROUP_START:
strncpy( buffer, "Group Start", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_GROUP_END:
strncpy( buffer, "Group End", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_JUMP:
strncpy( buffer, "Jump", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_START:
strncpy( buffer, "Loop Start Block", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_LOOP_END:
strncpy( buffer, "Loop End", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_SELECT:
strncpy( buffer, "Select", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_STOP48:
strncpy( buffer, "Stop Tape If In 48K Mode", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_SET_SIGNAL_LEVEL:
strncpy( buffer, "Set Signal Level", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_COMMENT:
strncpy( buffer, "Comment", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_MESSAGE:
strncpy( buffer, "Message", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_ARCHIVE_INFO:
strncpy( buffer, "Archive Info", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_HARDWARE:
strncpy( buffer, "Hardware Information", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_CUSTOM:
strncpy( buffer, "Custom Info", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_RLE_PULSE:
strncpy( buffer, "RLE Pulse", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_PULSE_SEQUENCE:
strncpy( buffer, "Pulse Sequence", length );
break;
case LIBSPECTRUM_TAPE_BLOCK_DATA_BLOCK:
strncpy( buffer, "Data Block", length );
break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_LOGIC,
"libspectrum_tape_block_description: unknown block type 0x%02x",
block->type
);
return LIBSPECTRUM_ERROR_LOGIC;
}
buffer[ length-1 ] = '\0';
return LIBSPECTRUM_ERROR_NONE;
}
/* Given a tape file, attempt to guess which sort of hardware it should run
on by looking for a hardware block (0x33).
Deliberately not mentioned in libspectrum.h(.in) as I'm really not
sure this function is actually useful.
*/
libspectrum_error
libspectrum_tape_guess_hardware( libspectrum_machine *machine,
const libspectrum_tape *tape )
{
GSList *ptr; int score, current_score; size_t i;
*machine = LIBSPECTRUM_MACHINE_UNKNOWN; current_score = 0;
if( !libspectrum_tape_present( tape ) ) return LIBSPECTRUM_ERROR_NONE;
for( ptr = tape->blocks; ptr; ptr = ptr->next ) {
libspectrum_tape_block *block = (libspectrum_tape_block*)ptr->data;
libspectrum_tape_hardware_block *hardware;
if( block->type != LIBSPECTRUM_TAPE_BLOCK_HARDWARE ) continue;
hardware = &( block->types.hardware );
for( i=0; i<hardware->count; i++ ) {
/* Only interested in which computer types this tape runs on */
if( hardware->types[i] != 0 ) continue;
/* Skip if the tape doesn't run on this hardware */
if( hardware->values[i] == 3 ) continue;
/* If the tape uses the special hardware, choose that preferentially.
If it doesn't (or is unknown), it's a possibility */
if( hardware->values[i] == 1 ) { score = 2; } else { score = 1; }
if( score <= current_score ) continue;
switch( hardware->ids[i] ) {
case 0: /* 16K Spectrum */
*machine = LIBSPECTRUM_MACHINE_16; current_score = score;
break;
case 1: /* 48K Spectrum */
case 2: /* 48K Issue 1 Spectrum */
*machine = LIBSPECTRUM_MACHINE_48; current_score = score;
break;
case 3: /* 128K Spectrum */
*machine = LIBSPECTRUM_MACHINE_128; current_score = score;
break;
case 4: /* +2 */
*machine = LIBSPECTRUM_MACHINE_PLUS2; current_score = score;
break;
case 5: /* +2A and +3. Gee, thanks to whoever designed the TZX format
for distinguishing those :-( */
*machine = LIBSPECTRUM_MACHINE_PLUS3; current_score = score;
break;
case 6: /* TC2048 */
*machine = LIBSPECTRUM_MACHINE_TC2048; current_score = score;
break;
}
}
}
return LIBSPECTRUM_ERROR_NONE;
}
/*
* Tape iterator functions
*/
libspectrum_tape_block*
libspectrum_tape_iterator_init( libspectrum_tape_iterator *iterator,
libspectrum_tape *tape )
{
*iterator = tape->blocks;
return libspectrum_tape_iterator_current( *iterator );
}
libspectrum_tape_block*
libspectrum_tape_block_internal_init(
libspectrum_tape_block_state *it,
libspectrum_tape *tape )
{
if( !tape || !tape->blocks )
return NULL;
it->current_block = tape->blocks;
if( libspectrum_tape_block_init( it->current_block->data,
it ) )
return NULL;
return libspectrum_tape_iterator_current( it->current_block );
}
libspectrum_tape_block*
libspectrum_tape_iterator_current( libspectrum_tape_iterator iterator )
{
return iterator ? iterator->data : NULL;
}
libspectrum_tape_block*
libspectrum_tape_iterator_next( libspectrum_tape_iterator *iterator )
{
if( iterator && *iterator ) {
*iterator = (*iterator)->next;
return libspectrum_tape_iterator_current( *iterator );
}
return NULL;
}
libspectrum_tape_block*
libspectrum_tape_iterator_peek_next( libspectrum_tape_iterator iterator )
{
if( iterator ) {
return libspectrum_tape_iterator_current( iterator->next );
}
return NULL;
}
libspectrum_tape_state_type
libspectrum_tape_state( libspectrum_tape *tape )
{
libspectrum_tape_block *block =
libspectrum_tape_iterator_current( tape->state.current_block );
switch( block->type ) {
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA: return tape->state.block_state.pure_data.state;
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA: return tape->state.block_state.raw_data.state;
case LIBSPECTRUM_TAPE_BLOCK_ROM: return tape->state.block_state.rom.state;
case LIBSPECTRUM_TAPE_BLOCK_TURBO: return tape->state.block_state.turbo.state;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_INVALID,
"invalid current block type 0x%02x in tape given to %s", block->type, __func__
);
return LIBSPECTRUM_TAPE_STATE_INVALID;
}
}
libspectrum_error
libspectrum_tape_set_state( libspectrum_tape *tape, libspectrum_tape_state_type state )
{
libspectrum_tape_block *block =
libspectrum_tape_iterator_current( tape->state.current_block );
switch( block->type ) {
case LIBSPECTRUM_TAPE_BLOCK_PURE_DATA: tape->state.block_state.pure_data.state = state; break;
case LIBSPECTRUM_TAPE_BLOCK_RAW_DATA: tape->state.block_state.raw_data.state = state; break;
case LIBSPECTRUM_TAPE_BLOCK_ROM: tape->state.block_state.rom.state = state; break;
case LIBSPECTRUM_TAPE_BLOCK_TURBO: tape->state.block_state.turbo.state = state; break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_INVALID,
"invalid current block type 0x%2x in tape given to %s", block->type, __func__
);
return LIBSPECTRUM_ERROR_INVALID;
}
return LIBSPECTRUM_ERROR_NONE;
}
|