src/mess/formats/hect_tap.c
| r0 | r6736 | |
| 1 | /******************************************************************** |
| 2 | |
| 3 | Support for Micronique machine .K7 and *.FOR cassette images |
| 4 | |
| 5 | Note that the usual type for hector cassette is *.K7, |
| 6 | the *.FOR type is only for programming screen in forth format. |
| 7 | |
| 8 | You can find some *.K7 file on serveral server in France |
| 9 | (Micronique is a French factory) Enjoy ! |
| 10 | |
| 11 | jj.stacino@aliceadsl.fr |
| 12 | |
| 13 | ********************************************************************/ |
| 14 | #include "hect_tap.h" |
| 15 | |
| 16 | |
| 17 | #define SMPLO -32768 |
| 18 | #define SILENCE 0 |
| 19 | #define SMPHI 32767 |
| 20 | |
| 21 | |
| 22 | static int cas_size; |
| 23 | |
| 24 | |
| 25 | int Header_cycles = 66; //Valeur Théorique = 44100 * 1.5 / 1000 // mesuré sur jeu Formule1 = 1,75ms |
| 26 | int Zero_cycles = 17; //Valeur Théorique = 44100 * 0.4 / 1000 // mesuré sur jeu Formule1 = 0,61ms |
| 27 | int Un_cycles = 40; //Valeur Théorique = 44100 * 0.9 / 1000 // mesuré sur jeu Formule1 = 1,13ms |
| 28 | |
| 29 | /******************************************************************* |
| 30 | Generate one high-low cycle of sample data |
| 31 | ********************************************************************/ |
| 32 | INLINE int hector_tap_cycle(INT16 *buffer, int sample_pos, int high, int low) |
| 33 | { |
| 34 | int i = 0; |
| 35 | |
| 36 | if ( buffer ) |
| 37 | { |
| 38 | while( i < high) |
| 39 | { |
| 40 | buffer[ sample_pos + i ] = SMPHI; |
| 41 | i++; |
| 42 | } |
| 43 | |
| 44 | while( i < high + low ) |
| 45 | { |
| 46 | buffer[ sample_pos + i ] = SMPLO; |
| 47 | i++; |
| 48 | } |
| 49 | } |
| 50 | return high + low; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | INLINE int hector_tap_byte(INT16 *buffer, int sample_pos, UINT8 data) |
| 55 | { |
| 56 | // Writing an entire byte |
| 57 | int i, samples; |
| 58 | |
| 59 | samples = 0; |
| 60 | for ( i = 0; i < 8; i++ ) |
| 61 | { |
| 62 | if ( data & 0x01 ) |
| 63 | samples += hector_tap_cycle( buffer, sample_pos + samples, Un_cycles/2, Un_cycles/2 ); |
| 64 | else |
| 65 | samples += hector_tap_cycle( buffer, sample_pos + samples, Zero_cycles/2, Zero_cycles/2 ); |
| 66 | |
| 67 | data >>= 1; |
| 68 | } |
| 69 | return samples; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | INLINE int hector_tap_synchro(INT16 *buffer, int sample_pos, int nb_synchro) |
| 74 | { |
| 75 | // Writing an entire byte |
| 76 | int i, samples; |
| 77 | |
| 78 | samples = 0; |
| 79 | for ( i = 0; i < nb_synchro ; i++ ) |
| 80 | samples += hector_tap_cycle( buffer, sample_pos + samples, Header_cycles/2, Header_cycles/2 ); |
| 81 | |
| 82 | return samples; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static int hector_handle_tap(INT16 *buffer, const UINT8 *casdata) |
| 87 | { |
| 88 | int data_pos, sample_count, block_count; |
| 89 | int previous_block=0; |
| 90 | |
| 91 | data_pos = 0; |
| 92 | sample_count = 0; |
| 93 | block_count = 0; |
| 94 | previous_block = 0; |
| 95 | |
| 96 | /* First 768 cycle of synchro */ |
| 97 | sample_count += hector_tap_synchro( buffer, sample_count, 768-4 ); |
| 98 | |
| 99 | // Sur la longeure du fichier |
| 100 | while( data_pos < cas_size ) |
| 101 | { |
| 102 | UINT16 block_size; |
| 103 | |
| 104 | if (previous_block == 0xFE) |
| 105 | /* Starting a block with 150 cycle of synchro to let time to Hector to do the job ! */ |
| 106 | sample_count += hector_tap_synchro( buffer, sample_count, 150 ); |
| 107 | else |
| 108 | /* Starting a block with 4 cycle of synchro */ |
| 109 | sample_count += hector_tap_synchro( buffer, sample_count, 4 ); |
| 110 | |
| 111 | if (data_pos>1) |
| 112 | previous_block = casdata[data_pos-1]; |
| 113 | |
| 114 | /* Handle block lenght on tape data */ |
| 115 | block_size = casdata[data_pos] ; |
| 116 | if (block_size==0) |
| 117 | block_size=256; |
| 118 | |
| 119 | block_count++; |
| 120 | sample_count += hector_tap_byte(buffer, sample_count, casdata[data_pos] ); |
| 121 | data_pos++; |
| 122 | |
| 123 | /* Data samples */ |
| 124 | for ( ; block_size ; data_pos++, block_size-- ) |
| 125 | { |
| 126 | /* Make sure there are enough bytes left */ |
| 127 | if ( data_pos > cas_size ) |
| 128 | return -1; |
| 129 | |
| 130 | sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] ); |
| 131 | |
| 132 | } |
| 133 | } |
| 134 | //Finish by a zero |
| 135 | sample_count += hector_tap_byte( buffer, sample_count, 0 ); |
| 136 | |
| 137 | return sample_count; |
| 138 | } |
| 139 | ///////////////////////////////////////////////////////////////////// |
| 140 | //// FORTH DATA CASSETTE |
| 141 | ///////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | |
| 144 | static int hector_handle_forth_tap(INT16 *buffer, const UINT8 *casdata) |
| 145 | { |
| 146 | int data_pos, sample_count, block_count; |
| 147 | int previous_block=0; |
| 148 | |
| 149 | data_pos = 0; |
| 150 | sample_count = 0; |
| 151 | block_count = 0; |
| 152 | previous_block = 0; |
| 153 | |
| 154 | // Out if len of file not modulo 822 octets |
| 155 | if ( (cas_size % 822) != 0 ) |
| 156 | return -1; |
| 157 | |
| 158 | // on the entire file |
| 159 | while( data_pos < cas_size ) |
| 160 | { |
| 161 | UINT16 block_size; |
| 162 | |
| 163 | // Starting a block with 768 cycle of synchro |
| 164 | sample_count += hector_tap_synchro( buffer, sample_count, 768 ); |
| 165 | |
| 166 | // Handle block lenght on tape data |
| 167 | block_size = 822 ; // Fixed size for the forth |
| 168 | |
| 169 | block_count=0; |
| 170 | |
| 171 | // Data samples |
| 172 | for ( ; block_size ; data_pos++, block_size-- ) |
| 173 | { |
| 174 | // Make sure there are enough bytes left |
| 175 | if ( data_pos > cas_size ) |
| 176 | return -1; |
| 177 | |
| 178 | sample_count += hector_tap_byte( buffer, sample_count, casdata[data_pos] ); |
| 179 | |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | //Finish by a zero |
| 184 | sample_count += hector_tap_byte( buffer, sample_count, 0 ); |
| 185 | |
| 186 | return sample_count; |
| 187 | } |
| 188 | /////////////////////////////////////////////////////////////////////// |
| 189 | ///// END FORTH DATA CASSETTE |
| 190 | /////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | |
| 193 | /******************************************************************* |
| 194 | Generate samples for the tape image |
| 195 | ********************************************************************/ |
| 196 | static int hector_tap_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes) |
| 197 | { |
| 198 | return hector_handle_tap( buffer, bytes ); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /******************************************************************* |
| 203 | Calculate the number of samples needed for this tape image FORTH |
| 204 | ********************************************************************/ |
| 205 | static int hector_tap_forth_to_wav_size(const UINT8 *casdata, int caslen) |
| 206 | { |
| 207 | cas_size = caslen ; |
| 208 | |
| 209 | return hector_handle_forth_tap( NULL, casdata ); |
| 210 | } |
| 211 | |
| 212 | /******************************************************************* |
| 213 | Generate samples for the tape image FORTH |
| 214 | ********************************************************************/ |
| 215 | static int hector_tap_forth_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes) |
| 216 | { |
| 217 | return hector_handle_forth_tap( buffer, bytes ); //forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /******************************************************************* |
| 222 | Calculate the number of samples needed for this tape image classical |
| 223 | ********************************************************************/ |
| 224 | static int hector_tap_to_wav_size(const UINT8 *casdata, int caslen) |
| 225 | { |
| 226 | cas_size = caslen ; |
| 227 | |
| 228 | return hector_handle_tap( NULL, casdata );//forth removed here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 229 | } |
| 230 | |
| 231 | |
| 232 | static const struct CassetteLegacyWaveFiller hector_legacy_fill_wave = |
| 233 | { |
| 234 | hector_tap_fill_wave, /* fill_wave */ |
| 235 | -1, /* chunk_size */ |
| 236 | 0, /* chunk_samples */ |
| 237 | hector_tap_to_wav_size, /* chunk_sample_calc */ |
| 238 | 44100, /* sample_frequency */ |
| 239 | 0, /* header_samples */ |
| 240 | 0 /* trailer_samples */ |
| 241 | }; |
| 242 | |
| 243 | static const struct CassetteLegacyWaveFiller hector_forth_legacy_fill_wave = |
| 244 | { |
| 245 | hector_tap_forth_fill_wave, /* fill_wave */ |
| 246 | -1, /* chunk_size */ |
| 247 | 0, /* chunk_samples */ |
| 248 | hector_tap_forth_to_wav_size, /* chunk_sample_calc */ |
| 249 | 44100, /* sample_frequency */ |
| 250 | 0, /* header_samples */ |
| 251 | 0 /* trailer_samples */ |
| 252 | }; |
| 253 | |
| 254 | |
| 255 | static casserr_t hector_k7_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 256 | { |
| 257 | return cassette_legacy_identify(cassette, opts, &hector_legacy_fill_wave); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | static casserr_t hector_k7_load(cassette_image *cassette) |
| 262 | { |
| 263 | return cassette_legacy_construct(cassette, &hector_legacy_fill_wave); |
| 264 | } |
| 265 | |
| 266 | static casserr_t hector_k7forth_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 267 | { |
| 268 | return cassette_legacy_identify(cassette, opts, &hector_forth_legacy_fill_wave); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | static casserr_t hector_k7forth_load(cassette_image *cassette) |
| 273 | { |
| 274 | return cassette_legacy_construct(cassette, &hector_forth_legacy_fill_wave); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | static const struct CassetteFormat hector_k7_format = |
| 279 | { |
| 280 | "k7", |
| 281 | hector_k7_identify, |
| 282 | hector_k7_load, |
| 283 | NULL |
| 284 | }; |
| 285 | |
| 286 | static const struct CassetteFormat hector_k7Forth_format = |
| 287 | { |
| 288 | "for", |
| 289 | hector_k7forth_identify, |
| 290 | hector_k7forth_load, |
| 291 | NULL |
| 292 | }; |
| 293 | |
| 294 | |
| 295 | CASSETTE_FORMATLIST_START(hector_cassette_formats) |
| 296 | CASSETTE_FORMAT(hector_k7_format) |
| 297 | CASSETTE_FORMAT(hector_k7Forth_format) |
| 298 | CASSETTE_FORMATLIST_END |