src/mess/formats/tzx_cas.c
| r5271 | r5272 | |
| 73 | 73 | static UINT8** blocks; |
| 74 | 74 | static float t_scale = 1; /* for scaling T-states to the 4MHz CPC */ |
| 75 | 75 | |
| 76 | | static void toggle_wave_data(void) { |
| 77 | | if ( wave_data == WAVE_LOW ) { |
| 76 | static void toggle_wave_data(void) |
| 77 | { |
| 78 | if (wave_data == WAVE_LOW) |
| 79 | { |
| 78 | 80 | wave_data = WAVE_HIGH; |
| 79 | | } else { |
| 81 | } |
| 82 | else |
| 83 | { |
| 80 | 84 | wave_data = WAVE_LOW; |
| 81 | 85 | } |
| 82 | 86 | } |
| 83 | 87 | |
| 84 | | static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen ) { |
| 88 | static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen ) |
| 89 | { |
| 85 | 90 | int pos = sizeof(TZX_HEADER) + 2; |
| 86 | 91 | int max_block_count = INITIAL_MAX_BLOCK_COUNT; |
| 87 | 92 | |
| 88 | 93 | blocks = alloc_array_clear_or_die(UINT8*, max_block_count); |
| 89 | 94 | block_count = 0; |
| 90 | 95 | |
| 91 | | while( pos < caslen ) { |
| 96 | while (pos < caslen) |
| 97 | { |
| 92 | 98 | UINT32 datasize; |
| 93 | 99 | UINT8 blocktype = casdata[pos]; |
| 94 | 100 | |
| 95 | | if ( block_count == max_block_count ) { |
| 101 | if (block_count == max_block_count) |
| 102 | { |
| 96 | 103 | void *old_blocks = blocks; |
| 97 | 104 | int old_max_block_count = max_block_count; |
| 98 | 105 | |
| 99 | 106 | max_block_count = max_block_count + BLOCK_COUNT_INCREMENTS; |
| 100 | 107 | blocks = auto_alloc_array(NULL /*machine*/, UINT8*, max_block_count); // SHOULD NOT BE USING auto_alloc_array() |
| 101 | | memcpy( blocks, old_blocks, old_max_block_count * sizeof(UINT8*) ); |
| 108 | memcpy(blocks, old_blocks, old_max_block_count * sizeof(UINT8*)); |
| 102 | 109 | } |
| 103 | 110 | |
| 104 | 111 | blocks[block_count] = (UINT8*)&casdata[pos]; |
| 105 | 112 | |
| 106 | 113 | pos += 1; |
| 107 | 114 | |
| 108 | | switch( blocktype ) { |
| 115 | switch (blocktype) |
| 116 | { |
| 109 | 117 | case 0x10: |
| 110 | 118 | pos += 2; |
| 111 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ); |
| 119 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 112 | 120 | pos += 2 + datasize; |
| 113 | 121 | break; |
| 114 | 122 | case 0x11: |
| 115 | 123 | pos += 0x0f; |
| 116 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ); |
| 124 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 117 | 125 | pos += 3 + datasize; |
| 118 | 126 | break; |
| 119 | 127 | case 0x12: |
| r5271 | r5272 | |
| 125 | 133 | break; |
| 126 | 134 | case 0x14: |
| 127 | 135 | pos += 7; |
| 128 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ); |
| 136 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 129 | 137 | pos += 3 + datasize; |
| 130 | 138 | break; |
| 131 | 139 | case 0x15: |
| 132 | 140 | pos += 5; |
| 133 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ); |
| 141 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 134 | 142 | pos += 3 + datasize; |
| 135 | 143 | break; |
| 136 | 144 | case 0x20: case 0x23: case 0x24: |
| r5271 | r5272 | |
| 143 | 151 | case 0x22: case 0x25: case 0x27: |
| 144 | 152 | break; |
| 145 | 153 | case 0x26: |
| 146 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ); |
| 154 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 147 | 155 | pos += 2 + 2 * datasize; |
| 148 | 156 | break; |
| 149 | 157 | case 0x28: case 0x32: |
| 150 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ); |
| 158 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 151 | 159 | pos += 2 + datasize; |
| 152 | 160 | break; |
| 153 | 161 | case 0x31: |
| r5271 | r5272 | |
| 164 | 172 | break; |
| 165 | 173 | case 0x35: |
| 166 | 174 | pos += 0x10; |
| 167 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ) + ( casdata[pos+3] << 24 ); |
| 175 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 168 | 176 | pos += 4 + datasize; |
| 169 | 177 | break; |
| 170 | 178 | case 0x40: |
| 171 | 179 | pos += 1; |
| 172 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ); |
| 180 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 173 | 181 | pos += 3 + datasize; |
| 174 | 182 | break; |
| 175 | 183 | case 0x5A: |
| 176 | 184 | pos += 9; |
| 177 | 185 | break; |
| 178 | 186 | default: |
| 179 | | datasize = casdata[pos] + ( casdata[pos+1] << 8 ) + ( casdata[pos+2] << 16 ) + ( casdata[pos+3] << 24 ); |
| 187 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 180 | 188 | pos += 4 + datasize; |
| 181 | 189 | break; |
| 182 | 190 | } |
| r5271 | r5272 | |
| 186 | 194 | free(blocks); |
| 187 | 195 | } |
| 188 | 196 | |
| 189 | | INLINE int millisec_to_samplecount( int millisec ) { |
| 190 | | return (int) ( millisec * ( (double)TZX_WAV_FREQUENCY / 1000.0 ) ); |
| 197 | INLINE int millisec_to_samplecount( int millisec ) |
| 198 | { |
| 199 | return (int) (millisec * ((double)TZX_WAV_FREQUENCY / 1000.0)); |
| 191 | 200 | } |
| 192 | 201 | |
| 193 | | INLINE int tcycles_to_samplecount( int tcycles ) { |
| 194 | | return (int) ( ( 0.5 + ( ( (double)TZX_WAV_FREQUENCY / 3500000 ) * (double)tcycles ) ) * t_scale ); |
| 202 | INLINE int tcycles_to_samplecount( int tcycles ) |
| 203 | { |
| 204 | return (int) ((0.5 + (((double)TZX_WAV_FREQUENCY / 3500000) * (double)tcycles)) * t_scale); |
| 195 | 205 | } |
| 196 | 206 | |
| 197 | | static void tzx_output_wave( INT16 **buffer, int length ) { |
| 198 | | if ( buffer == NULL ) { |
| 207 | static void tzx_output_wave( INT16 **buffer, int length ) |
| 208 | { |
| 209 | if (buffer == NULL) |
| 210 | { |
| 199 | 211 | return; |
| 200 | 212 | } |
| 201 | 213 | |
| 202 | | for( ; length > 0; length-- ) { |
| 214 | for ( ; length > 0; length--) |
| 215 | { |
| 203 | 216 | **buffer = wave_data; |
| 204 | 217 | *buffer = *buffer + 1; |
| 205 | 218 | } |
| 206 | 219 | } |
| 207 | 220 | |
| 208 | | static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte ) { |
| 209 | | int pilot_samples = tcycles_to_samplecount( pilot ); |
| 210 | | int sync1_samples = tcycles_to_samplecount( sync1 ); |
| 211 | | int sync2_samples = tcycles_to_samplecount( sync2 ); |
| 212 | | int bit0_samples = tcycles_to_samplecount( bit0 ); |
| 213 | | int bit1_samples = tcycles_to_samplecount( bit1 ); |
| 221 | static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte ) |
| 222 | { |
| 223 | int pilot_samples = tcycles_to_samplecount(pilot); |
| 224 | int sync1_samples = tcycles_to_samplecount(sync1); |
| 225 | int sync2_samples = tcycles_to_samplecount(sync2); |
| 226 | int bit0_samples = tcycles_to_samplecount(bit0); |
| 227 | int bit1_samples = tcycles_to_samplecount(bit1); |
| 214 | 228 | int data_index; |
| 215 | 229 | int size = 0; |
| 216 | 230 | |
| 217 | | logerror( "tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples ); |
| 231 | /* Uncomment this to include into error.log a fully detailed analysis of each block */ |
| 232 | // logerror("tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples); |
| 218 | 233 | |
| 219 | 234 | /* PILOT */ |
| 220 | | for( ; pilot_length > 0; pilot_length-- ) { |
| 221 | | tzx_output_wave( buffer, pilot_samples ); |
| 235 | for ( ; pilot_length > 0; pilot_length--) |
| 236 | { |
| 237 | tzx_output_wave(buffer, pilot_samples); |
| 222 | 238 | size += pilot_samples; |
| 223 | 239 | toggle_wave_data(); |
| 224 | 240 | } |
| 225 | 241 | /* SYNC1 */ |
| 226 | | if ( sync1_samples > 0 ) { |
| 227 | | tzx_output_wave( buffer, sync1_samples ); |
| 242 | if (sync1_samples > 0) |
| 243 | { |
| 244 | tzx_output_wave(buffer, sync1_samples); |
| 228 | 245 | size += sync1_samples; |
| 229 | 246 | toggle_wave_data(); |
| 230 | 247 | } |
| 231 | 248 | /* SYNC2 */ |
| 232 | | if ( sync2_samples > 0 ) { |
| 233 | | tzx_output_wave( buffer, sync2_samples ); |
| 249 | if (sync2_samples > 0) |
| 250 | { |
| 251 | tzx_output_wave(buffer, sync2_samples); |
| 234 | 252 | size += sync2_samples; |
| 235 | 253 | toggle_wave_data(); |
| 236 | 254 | } |
| 237 | 255 | /* data */ |
| 238 | | for( data_index = 0; data_index < data_size; data_index++ ) { |
| 239 | | UINT8 byte = bytes[data_index]; |
| 240 | | int bits_to_go = ( data_index == ( data_size - 1 ) ) ? bits_in_last_byte : 8; |
| 241 | | for( ; bits_to_go > 0; byte <<= 1, bits_to_go-- ) { |
| 242 | | int bit_samples = ( byte & 0x80 ) ? bit1_samples : bit0_samples; |
| 243 | | tzx_output_wave( buffer, bit_samples ); |
| 256 | for (data_index = 0; data_index < data_size; data_index++) |
| 257 | { |
| 258 | UINT8 byte = bytes[data_index]; |
| 259 | int bits_to_go = (data_index == (data_size - 1)) ? bits_in_last_byte : 8; |
| 260 | |
| 261 | for ( ; bits_to_go > 0; byte <<= 1, bits_to_go--) |
| 262 | { |
| 263 | int bit_samples = (byte & 0x80) ? bit1_samples : bit0_samples; |
| 264 | tzx_output_wave(buffer, bit_samples); |
| 244 | 265 | size += bit_samples; |
| 245 | 266 | toggle_wave_data(); |
| 246 | | tzx_output_wave( buffer, bit_samples ); |
| 267 | tzx_output_wave(buffer, bit_samples); |
| 247 | 268 | size += bit_samples; |
| 248 | 269 | toggle_wave_data(); |
| 249 | 270 | } |
| 250 | 271 | } |
| 251 | 272 | /* pause */ |
| 252 | | if ( pause > 0 ) { |
| 253 | | int start_pause_samples = millisec_to_samplecount( 1 ); |
| 254 | | int rest_pause_samples = millisec_to_samplecount( pause - 1 ); |
| 273 | if (pause > 0) |
| 274 | { |
| 275 | int start_pause_samples = millisec_to_samplecount(1); |
| 276 | int rest_pause_samples = millisec_to_samplecount(pause - 1); |
| 255 | 277 | |
| 256 | | tzx_output_wave( buffer, start_pause_samples ); |
| 278 | tzx_output_wave(buffer, start_pause_samples); |
| 257 | 279 | size += start_pause_samples; |
| 258 | 280 | wave_data = WAVE_LOW; |
| 259 | | tzx_output_wave( buffer, rest_pause_samples ); |
| 281 | tzx_output_wave(buffer, rest_pause_samples); |
| 260 | 282 | size += rest_pause_samples; |
| 261 | 283 | } |
| 262 | | return size; |
| 284 | return size; |
| 263 | 285 | } |
| 264 | 286 | |
| 265 | | /* |
| 266 | | Will go through blocks and calculate number of samples needed. |
| 267 | | If buffer is not NULL the sample data will also be written. |
| 268 | | */ |
| 269 | | static int tzx_cas_do_work( INT16 **buffer ) { |
| 287 | static void ascii_block_common_log( const char *block_type_string, UINT8 block_type ) |
| 288 | { |
| 289 | logerror("%s (type %02x) encountered.\n", block_type_string, block_type); |
| 290 | logerror("This block contains info on the .tzx file you are loading.\n"); |
| 291 | logerror("Please include the following info in your bug reports, if the image has issue in M.E.S.S.\n"); |
| 292 | } |
| 293 | |
| 294 | static const char *const archive_ident[] = |
| 295 | { |
| 296 | "Full title", |
| 297 | "Software house/publisher", |
| 298 | "Author(s)", |
| 299 | "Year of publication", |
| 300 | "Language", |
| 301 | "Game/utility type", |
| 302 | "Price", |
| 303 | "Protection scheme/loader", |
| 304 | "Origin", |
| 305 | }; |
| 306 | |
| 307 | static const char *const hw_info[] = |
| 308 | { |
| 309 | "Tape runs on this machine / this hardware", |
| 310 | "Tape needs this machine / this hardware", |
| 311 | "Tape runs on this machine / this hardware, but does not require its special features", |
| 312 | "Tape does not run on this machine / this hardware", |
| 313 | }; |
| 314 | |
| 315 | /* Will go through blocks and calculate number of samples needed. |
| 316 | If buffer is not NULL the sample data will also be written. */ |
| 317 | static int tzx_cas_do_work( INT16 **buffer ) |
| 318 | { |
| 270 | 319 | int current_block = 0; |
| 271 | 320 | int size = 0; |
| 272 | 321 | |
| 273 | 322 | wave_data = WAVE_LOW; |
| 274 | 323 | |
| 275 | | while( current_block < block_count ) { |
| 324 | while (current_block < block_count) |
| 325 | { |
| 276 | 326 | int pause_time; |
| 277 | | int data_size; |
| 327 | int data_size, text_size, total_size, i; |
| 278 | 328 | int pilot, pilot_length, sync1, sync2; |
| 279 | 329 | int bit0, bit1, bits_in_last_byte; |
| 280 | | UINT8 *cur_block = blocks[current_block]; |
| 281 | | UINT8 block_type =cur_block[0]; |
| 282 | | logerror( "tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type ); |
| 283 | | switch( block_type ) { |
| 330 | UINT8 *cur_block = blocks[current_block]; |
| 331 | UINT8 block_type = cur_block[0]; |
| 332 | |
| 333 | /* Uncomment this to include into error.log a list of the types each block */ |
| 334 | // logerror("tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type); |
| 335 | |
| 336 | switch (block_type) |
| 337 | { |
| 284 | 338 | case 0x10: /* Standard Speed Data Block (.TAP block) */ |
| 285 | | pause_time = cur_block[1] + ( cur_block[2] << 8 ); |
| 286 | | data_size = cur_block[3] + ( cur_block[4] << 8 ); |
| 287 | | pilot_length = ( cur_block[5] == 0x00 ) ? 8064 : 3220; |
| 288 | | size += tzx_cas_handle_block( buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8 ); |
| 339 | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 340 | data_size = cur_block[3] + (cur_block[4] << 8); |
| 341 | pilot_length = (cur_block[5] == 0x00) ? 8064 : 3220; |
| 342 | size += tzx_cas_handle_block(buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 289 | 343 | current_block++; |
| 290 | 344 | break; |
| 291 | 345 | case 0x11: /* Turbo Loading Data Block */ |
| 292 | | pilot = cur_block[1] + ( cur_block[2] << 8 ); |
| 293 | | sync1 = cur_block[3] + ( cur_block[4] << 8 ); |
| 294 | | sync2 = cur_block[5] + ( cur_block[6] << 8 ); |
| 295 | | bit0 = cur_block[7] + ( cur_block[8] << 8 ); |
| 296 | | bit1 = cur_block[9] + ( cur_block[10] << 8 ); |
| 297 | | pilot_length = cur_block[11] + ( cur_block[12] << 8 ); |
| 346 | pilot = cur_block[1] + (cur_block[2] << 8); |
| 347 | sync1 = cur_block[3] + (cur_block[4] << 8); |
| 348 | sync2 = cur_block[5] + (cur_block[6] << 8); |
| 349 | bit0 = cur_block[7] + (cur_block[8] << 8); |
| 350 | bit1 = cur_block[9] + (cur_block[10] << 8); |
| 351 | pilot_length = cur_block[11] + (cur_block[12] << 8); |
| 298 | 352 | bits_in_last_byte = cur_block[13]; |
| 299 | | pause_time = cur_block[14] + ( cur_block[15] << 8 ); |
| 300 | | data_size = cur_block[16] + ( cur_block[17] << 8 ) + ( cur_block[18] << 16 ); |
| 301 | | size += tzx_cas_handle_block( buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte ); |
| 353 | pause_time = cur_block[14] + (cur_block[15] << 8); |
| 354 | data_size = cur_block[16] + (cur_block[17] << 8) + (cur_block[18] << 16); |
| 355 | size += tzx_cas_handle_block(buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte); |
| 302 | 356 | current_block++; |
| 303 | 357 | break; |
| 304 | 358 | case 0x12: /* Pure Tone */ |
| 305 | | pilot = cur_block[1] + ( cur_block[2] << 8 ); |
| 306 | | pilot_length = cur_block[3] + ( cur_block[4] << 8 ); |
| 307 | | size += tzx_cas_handle_block( buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0 ); |
| 359 | pilot = cur_block[1] + (cur_block[2] << 8); |
| 360 | pilot_length = cur_block[3] + (cur_block[4] << 8); |
| 361 | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0); |
| 308 | 362 | current_block++; |
| 309 | 363 | break; |
| 310 | 364 | case 0x13: /* Sequence of Pulses of Different Lengths */ |
| 311 | | for( data_size = 0; data_size < cur_block[1]; data_size++ ) { |
| 312 | | pilot = cur_block[2 + 2*data_size] + ( cur_block[3 + 2*data_size] << 8 ); |
| 313 | | size += tzx_cas_handle_block( buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0 ); |
| 365 | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 366 | { |
| 367 | pilot = cur_block[2 + 2 * data_size] + (cur_block[3 + 2 * data_size] << 8); |
| 368 | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0); |
| 314 | 369 | } |
| 315 | 370 | current_block++; |
| 316 | 371 | break; |
| 317 | 372 | case 0x14: /* Pure Data Block */ |
| 318 | | bit0 = cur_block[1] + ( cur_block[2] << 8 ); |
| 319 | | bit1 = cur_block[3] + ( cur_block[4] << 8 ); |
| 373 | bit0 = cur_block[1] + (cur_block[2] << 8); |
| 374 | bit1 = cur_block[3] + (cur_block[4] << 8); |
| 320 | 375 | bits_in_last_byte = cur_block[5]; |
| 321 | | pause_time = cur_block[6] + ( cur_block[7] << 8 ); |
| 322 | | data_size = cur_block[8] + ( cur_block[9] << 8 ) + ( cur_block[10] << 16 ); |
| 323 | | size += tzx_cas_handle_block( buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte ); |
| 376 | pause_time = cur_block[6] + (cur_block[7] << 8); |
| 377 | data_size = cur_block[8] + (cur_block[9] << 8) + (cur_block[10] << 16); |
| 378 | size += tzx_cas_handle_block(buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte); |
| 324 | 379 | current_block++; |
| 325 | 380 | break; |
| 326 | | case 0x15: /* Direct Recording */ |
| 327 | | case 0x16: /* C64 ROM Type Data Block */ |
| 328 | | case 0x17: /* C64 Turbo Tape Data Block */ |
| 329 | | logerror( "Unsupported block type (%02x) encountered\n", block_type ); |
| 330 | | current_block++; |
| 331 | | break; |
| 332 | 381 | case 0x20: /* Pause (Silence) or 'Stop the Tape' Command */ |
| 333 | | pause_time = cur_block[1] + ( cur_block[2] << 8 ); |
| 334 | | if ( pause_time == 0 ) { |
| 335 | | /* pause = 0 is used to let an emulator automagically stop the tape |
| 336 | | in MESS we do not do that, so we insert a 5 second pause. |
| 337 | | */ |
| 382 | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 383 | if (pause_time == 0) |
| 384 | { |
| 385 | /* pause = 0 is used to let an emulator automagically stop the tape |
| 386 | in MESS we do not do that, so we insert a 5 second pause. */ |
| 338 | 387 | pause_time = 5000; |
| 339 | 388 | } |
| 340 | | size += tzx_cas_handle_block( buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0 ); |
| 389 | size += tzx_cas_handle_block(buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0); |
| 341 | 390 | current_block++; |
| 342 | 391 | break; |
| 392 | case 0x16: /* C64 ROM Type Data Block */ // Deprecated in TZX 1.20 |
| 393 | case 0x17: /* C64 Turbo Tape Data Block */ // Deprecated in TZX 1.20 |
| 394 | case 0x34: /* Emulation Info */ // Deprecated in TZX 1.20 |
| 395 | case 0x40: /* Snapshot Block */ // Deprecated in TZX 1.20 |
| 396 | logerror("Deprecated block type (%02x) encountered.\n", block_type); |
| 397 | logerror("Please look for an updated .tzx file.\n"); |
| 398 | current_block++; |
| 399 | break; |
| 400 | case 0x30: /* Text Description */ |
| 401 | ascii_block_common_log("Text Description Block", block_type); |
| 402 | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 403 | logerror("%c", cur_block[2 + data_size]); |
| 404 | logerror("\n"); |
| 405 | current_block++; |
| 406 | break; |
| 407 | case 0x31: /* Message Block */ |
| 408 | ascii_block_common_log("Message Block", block_type); |
| 409 | logerror("Expected duration of the message display: %02x\n", cur_block[1]); |
| 410 | logerror("Message: \n"); |
| 411 | for (data_size = 0; data_size < cur_block[2]; data_size++) |
| 412 | { |
| 413 | logerror("%c", cur_block[3 + data_size]); |
| 414 | if (cur_block[3 + data_size] == 0x0d) |
| 415 | logerror("\n"); |
| 416 | } |
| 417 | logerror("\n"); |
| 418 | current_block++; |
| 419 | break; |
| 420 | case 0x32: /* Archive Info */ |
| 421 | ascii_block_common_log("Archive Info Block", block_type); |
| 422 | total_size = cur_block[1] + (cur_block[2] << 8); |
| 423 | text_size = 0; |
| 424 | for (data_size = 0; data_size < cur_block[3]; data_size++) // data_size = number of text blocks, in this case |
| 425 | { |
| 426 | if (cur_block[4 + text_size] < 0x09) |
| 427 | logerror("%s: \n", archive_ident[cur_block[4 + text_size]]); |
| 428 | else |
| 429 | logerror("Comment(s): \n"); |
| 430 | |
| 431 | for (i = 0; i < cur_block[4 + text_size + 1]; i++) |
| 432 | { |
| 433 | logerror("%c", cur_block[4 + text_size + 2 + i]); |
| 434 | } |
| 435 | text_size += 2 + i; |
| 436 | } |
| 437 | logerror("\n"); |
| 438 | if (text_size != total_size) |
| 439 | logerror("Malformed Archive Info Block (Text length different from the declared one).\n Please verify your tape image.\n"); |
| 440 | current_block++; |
| 441 | break; |
| 442 | case 0x33: /* Hardware Type */ |
| 443 | ascii_block_common_log("Hardware Type Block", block_type); |
| 444 | for (data_size = 0; data_size < cur_block[1]; data_size++) // data_size = number of hardware blocks, in this case |
| 445 | { |
| 446 | logerror("Hardware Type %02x - Hardware ID %02x - ", cur_block[2 + data_size * 3], cur_block[2 + data_size * 3 + 1]); |
| 447 | logerror("%s \n ", hw_info[cur_block[2 + data_size * 3 + 2]]); |
| 448 | } |
| 449 | current_block++; |
| 450 | break; |
| 451 | case 0x35: /* Custom Info Block */ |
| 452 | ascii_block_common_log("Custom Info Block", block_type); |
| 453 | for (data_size = 0; data_size < 10; data_size++) |
| 454 | { |
| 455 | logerror("%c", cur_block[1 + data_size]); |
| 456 | } |
| 457 | logerror(":\n"); |
| 458 | text_size = cur_block[11] + (cur_block[12] << 8) + (cur_block[13] << 16) + (cur_block[14] << 24); |
| 459 | for (data_size = 0; data_size < text_size; data_size++) |
| 460 | logerror("%c", cur_block[15 + data_size]); |
| 461 | logerror("\n"); |
| 462 | current_block++; |
| 463 | break; |
| 464 | case 0x5A: /* "Glue" Block */ |
| 465 | logerror("Glue Block (type %02x) encountered.\n", block_type); |
| 466 | logerror("Please use a .tzx handling utility to split the merged tape files.\n"); |
| 467 | current_block++; |
| 468 | break; |
| 469 | case 0x15: /* Direct Recording */ |
| 470 | case 0x18: /* CSW Recording */ |
| 471 | case 0x19: /* Generalized Data Block */ |
| 343 | 472 | case 0x21: /* Group Start */ |
| 344 | 473 | case 0x22: /* Group End */ |
| 345 | 474 | case 0x23: /* Jump To Block */ |
| r5271 | r5272 | |
| 349 | 478 | case 0x27: /* Return From Sequence */ |
| 350 | 479 | case 0x28: /* Select Block */ |
| 351 | 480 | case 0x2A: /* Stop Tape if in 48K Mode */ |
| 352 | | case 0x30: /* Text Description */ |
| 353 | | case 0x31: /* Message Block */ |
| 354 | | case 0x32: /* Archive Info */ |
| 355 | | case 0x33: /* Hardware Type */ |
| 356 | | case 0x34: /* Emulation Info */ |
| 357 | | case 0x35: /* Custom Info Block */ |
| 358 | | case 0x40: /* Snapshot Block */ |
| 359 | | case 0x5A: /* Merge Block */ |
| 481 | case 0x2B: /* Set signal level */ |
| 360 | 482 | default: |
| 361 | | logerror( "Unsupported block type (%02x) encountered\n", block_type ); |
| 483 | logerror("Unsupported block type (%02x) encountered.\n", block_type); |
| 362 | 484 | current_block++; |
| 363 | 485 | break; |
| 364 | 486 | } |
| r5271 | r5272 | |
| 366 | 488 | return size; |
| 367 | 489 | } |
| 368 | 490 | |
| 369 | | static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen ) { |
| 491 | static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 492 | { |
| 370 | 493 | int size = 0; |
| 371 | 494 | |
| 372 | 495 | /* Header size plus major and minor version number */ |
| 373 | | if ( caslen < 10 ) { |
| 374 | | logerror( "tzx_cas_to_wav_size: cassette image too small\n" ); |
| 496 | if (caslen < 10) |
| 497 | { |
| 498 | logerror("tzx_cas_to_wav_size: cassette image too small\n"); |
| 375 | 499 | goto cleanup; |
| 376 | 500 | } |
| 377 | 501 | |
| 378 | 502 | /* Check for correct header */ |
| 379 | | if ( memcmp( casdata, TZX_HEADER, sizeof(TZX_HEADER) ) ) { |
| 380 | | logerror( "tzx_cas_to_wav_size: cassette image has incompatible header\n" ); |
| 503 | if (memcmp(casdata, TZX_HEADER, sizeof(TZX_HEADER))) |
| 504 | { |
| 505 | logerror("tzx_cas_to_wav_size: cassette image has incompatible header\n"); |
| 381 | 506 | goto cleanup; |
| 382 | 507 | } |
| 383 | 508 | |
| 384 | 509 | /* Check major version number in header */ |
| 385 | | if ( casdata[0x08] > SUPPORTED_VERSION_MAJOR ) { |
| 386 | | logerror( "tzx_cas_to_wav_size: unsupported version\n" ); |
| 510 | if (casdata[0x08] > SUPPORTED_VERSION_MAJOR) |
| 511 | { |
| 512 | logerror("tzx_cas_to_wav_size: unsupported version\n"); |
| 387 | 513 | goto cleanup; |
| 388 | 514 | } |
| 389 | 515 | |
| 390 | | tzx_cas_get_blocks( casdata, caslen ); |
| 516 | tzx_cas_get_blocks(casdata, caslen); |
| 391 | 517 | |
| 392 | | logerror( "tzx_cas_to_wav_size: %d blocks found\n", block_count ); |
| 518 | logerror("tzx_cas_to_wav_size: %d blocks found\n", block_count); |
| 393 | 519 | |
| 394 | | if ( block_count == 0 ) { |
| 395 | | logerror( "tzx_cas_to_wav_size: no blocks found!\n" ); |
| 520 | if (block_count == 0) |
| 521 | { |
| 522 | logerror("tzx_cas_to_wav_size: no blocks found!\n"); |
| 396 | 523 | goto cleanup; |
| 397 | 524 | } |
| 398 | 525 | |
| 399 | | size = tzx_cas_do_work( NULL ); |
| 526 | size = tzx_cas_do_work(NULL); |
| 400 | 527 | |
| 401 | 528 | return size; |
| 402 | 529 | |
| r5271 | r5272 | |
| 404 | 531 | return -1; |
| 405 | 532 | } |
| 406 | 533 | |
| 407 | | static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) { |
| 408 | | INT16 *p = buffer; |
| 534 | static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 535 | { |
| 536 | INT16 *p = buffer; |
| 409 | 537 | int size = 0; |
| 410 | 538 | t_scale = 1.0; |
| 411 | | size = tzx_cas_do_work( &p ); |
| 539 | size = tzx_cas_do_work(&p); |
| 412 | 540 | return size; |
| 413 | 541 | } |
| 414 | 542 | |
| 415 | | static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) { |
| 416 | | INT16 *p = buffer; |
| 543 | static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 544 | { |
| 545 | INT16 *p = buffer; |
| 417 | 546 | int size = 0; |
| 418 | 547 | t_scale = (40 / 35); /* scale to 4MHz */ |
| 419 | | size = tzx_cas_do_work( &p ); |
| 548 | size = tzx_cas_do_work(&p); |
| 420 | 549 | return size; |
| 421 | 550 | } |
| 422 | 551 | |
| 423 | | static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen ) { |
| 552 | static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 553 | { |
| 424 | 554 | int size = 0; |
| 425 | 555 | const UINT8 *p = casdata; |
| 426 | 556 | |
| 427 | | while( p < casdata + caslen ) { |
| 428 | | int data_size = p[0] + ( p[1] << 8 ); |
| 429 | | int pilot_length = ( p[2] == 0x00 ) ? 8064 : 3220; /* TZX specification */ |
| 430 | | // int pilot_length = ( p[2] == 0x00 ) ? 8063 : 3223; /* worldofspectrum */ |
| 431 | | logerror( "tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size ); |
| 557 | while (p < casdata + caslen) |
| 558 | { |
| 559 | int data_size = p[0] + (p[1] << 8); |
| 560 | int pilot_length = (p[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 561 | // int pilot_length = (p[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 562 | logerror("tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size); |
| 432 | 563 | p += 2; |
| 433 | | size += tzx_cas_handle_block( NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8 ); |
| 434 | | logerror( ", total size is now: %d\n", size ); |
| 564 | size += tzx_cas_handle_block(NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 565 | logerror(", total size is now: %d\n", size); |
| 435 | 566 | p += data_size; |
| 436 | 567 | } |
| 437 | 568 | return size; |
| 438 | 569 | } |
| 439 | 570 | |
| 440 | | static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) { |
| 571 | static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 572 | { |
| 441 | 573 | INT16 *p = buffer; |
| 442 | 574 | int size = 0; |
| 443 | 575 | |
| 444 | | while( size < length ) { |
| 445 | | int data_size = bytes[0] + ( bytes[1] << 8 ); |
| 446 | | int pilot_length = ( bytes[2] == 0x00 ) ? 8064 : 3220; /* TZX specification */ |
| 447 | | // int pilot_length = ( bytes[2] == 0x00 ) ? 8063 : 3223; /* worldofspectrum */ |
| 448 | | logerror( "tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size ); |
| 576 | while (size < length) |
| 577 | { |
| 578 | int data_size = bytes[0] + (bytes[1] << 8); |
| 579 | int pilot_length = (bytes[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 580 | // int pilot_length = (bytes[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 581 | logerror("tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size); |
| 449 | 582 | bytes += 2; |
| 450 | | size += tzx_cas_handle_block( &p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8 ); |
| 583 | size += tzx_cas_handle_block(&p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 451 | 584 | bytes += data_size; |
| 452 | 585 | } |
| 453 | 586 | return size; |
| 454 | 587 | } |
| 455 | 588 | |
| 456 | | static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave = { |
| 589 | static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave = |
| 590 | { |
| 457 | 591 | tzx_cas_fill_wave, /* fill_wave */ |
| 458 | 592 | -1, /* chunk_size */ |
| 459 | 593 | 0, /* chunk_samples */ |
| r5271 | r5272 | |
| 463 | 597 | 0 /* trailer_samples */ |
| 464 | 598 | }; |
| 465 | 599 | |
| 466 | | static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave = { |
| 600 | static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave = |
| 601 | { |
| 467 | 602 | tap_cas_fill_wave, /* fill_wave */ |
| 468 | 603 | -1, /* chunk_size */ |
| 469 | 604 | 0, /* chunk_samples */ |
| r5271 | r5272 | |
| 473 | 608 | 0 /* trailer_samples */ |
| 474 | 609 | }; |
| 475 | 610 | |
| 476 | | static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave = { |
| 611 | static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave = |
| 612 | { |
| 477 | 613 | cdt_cas_fill_wave, /* fill_wave */ |
| 478 | 614 | -1, /* chunk_size */ |
| 479 | 615 | 0, /* chunk_samples */ |
| r5271 | r5272 | |
| 483 | 619 | 0 /* trailer_samples */ |
| 484 | 620 | }; |
| 485 | 621 | |
| 486 | | static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) { |
| 487 | | return cassette_legacy_identify( cassette, opts, &tzx_legacy_fill_wave ); |
| 622 | static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 623 | { |
| 624 | return cassette_legacy_identify(cassette, opts, &tzx_legacy_fill_wave); |
| 488 | 625 | } |
| 489 | 626 | |
| 490 | | static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) { |
| 491 | | return cassette_legacy_identify( cassette, opts, &tap_legacy_fill_wave ); |
| 627 | static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 628 | { |
| 629 | return cassette_legacy_identify(cassette, opts, &tap_legacy_fill_wave); |
| 492 | 630 | } |
| 493 | 631 | |
| 494 | | static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) { |
| 495 | | return cassette_legacy_identify( cassette, opts, &cdt_legacy_fill_wave ); |
| 632 | static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 633 | { |
| 634 | return cassette_legacy_identify(cassette, opts, &cdt_legacy_fill_wave); |
| 496 | 635 | } |
| 497 | 636 | |
| 498 | | static casserr_t tzx_cassette_load( cassette_image *cassette ) { |
| 499 | | return cassette_legacy_construct( cassette, &tzx_legacy_fill_wave ); |
| 637 | static casserr_t tzx_cassette_load( cassette_image *cassette ) |
| 638 | { |
| 639 | return cassette_legacy_construct(cassette, &tzx_legacy_fill_wave); |
| 500 | 640 | } |
| 501 | 641 | |
| 502 | | static casserr_t tap_cassette_load( cassette_image *cassette ) { |
| 503 | | return cassette_legacy_construct( cassette, &tap_legacy_fill_wave ); |
| 642 | static casserr_t tap_cassette_load( cassette_image *cassette ) |
| 643 | { |
| 644 | return cassette_legacy_construct(cassette, &tap_legacy_fill_wave); |
| 504 | 645 | } |
| 505 | 646 | |
| 506 | | static casserr_t cdt_cassette_load( cassette_image *cassette ) { |
| 507 | | return cassette_legacy_construct( cassette, &cdt_legacy_fill_wave ); |
| 647 | static casserr_t cdt_cassette_load( cassette_image *cassette ) |
| 648 | { |
| 649 | return cassette_legacy_construct(cassette, &cdt_legacy_fill_wave); |
| 508 | 650 | } |
| 509 | 651 | |
| 510 | | static const struct CassetteFormat tzx_cassette_format = { |
| 652 | static const struct CassetteFormat tzx_cassette_format = |
| 653 | { |
| 511 | 654 | "tzx", |
| 512 | 655 | tzx_cassette_identify, |
| 513 | 656 | tzx_cassette_load, |
| 514 | 657 | NULL |
| 515 | 658 | }; |
| 516 | 659 | |
| 517 | | static const struct CassetteFormat tap_cassette_format = { |
| 660 | static const struct CassetteFormat tap_cassette_format = |
| 661 | { |
| 518 | 662 | "tap", |
| 519 | 663 | tap_cassette_identify, |
| 520 | 664 | tap_cassette_load, |
| 521 | 665 | NULL |
| 522 | 666 | }; |
| 523 | 667 | |
| 524 | | static const struct CassetteFormat cdt_cassette_format = { |
| 668 | static const struct CassetteFormat cdt_cassette_format = |
| 669 | { |
| 525 | 670 | "cdt", |
| 526 | 671 | cdt_cassette_identify, |
| 527 | 672 | cdt_cassette_load, |