src/mess/formats/vg5k_cas.c
| r0 | r8269 | |
| 1 | /******************************************************************** |
| 2 | |
| 3 | Support for VG-5000 .k7 cassette images |
| 4 | |
| 5 | ********************************************************************/ |
| 6 | #include "vg5k_cas.h" |
| 7 | |
| 8 | |
| 9 | #define SMPLO -32768 |
| 10 | #define SILENCE 0 |
| 11 | #define SMPHI 32767 |
| 12 | |
| 13 | |
| 14 | static int k7_size; |
| 15 | |
| 16 | /******************************************************************* |
| 17 | Generate one high-low cycle of sample data |
| 18 | ********************************************************************/ |
| 19 | INLINE int vg5k_cas_cycle(INT16 *buffer, int sample_pos, int len) |
| 20 | { |
| 21 | int i = 0; |
| 22 | |
| 23 | if (buffer) |
| 24 | { |
| 25 | while( i < len) |
| 26 | { |
| 27 | buffer[ sample_pos + i ] = SMPHI; |
| 28 | i++; |
| 29 | } |
| 30 | |
| 31 | while( i < len * 2 ) |
| 32 | { |
| 33 | buffer[ sample_pos + i ] = SMPLO; |
| 34 | i++; |
| 35 | } |
| 36 | } |
| 37 | return len * 2; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /******************************************************************* |
| 42 | Generate n samples of silence |
| 43 | ********************************************************************/ |
| 44 | INLINE int vg5k_cas_silence(INT16 *buffer, int sample_pos, int len) |
| 45 | { |
| 46 | int i = 0; |
| 47 | |
| 48 | if ( buffer ) |
| 49 | for( i = 0; i < len; i++) |
| 50 | buffer[ sample_pos + i ] = SILENCE; |
| 51 | |
| 52 | return len; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /******************************************************************* |
| 57 | Generate the end-byte samples |
| 58 | ********************************************************************/ |
| 59 | INLINE int vg5k_cas_eob(INT16 *buffer, int sample_pos) |
| 60 | { |
| 61 | int i, samples = 0; |
| 62 | |
| 63 | for (i = 0; i < 4; i++) |
| 64 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5); |
| 65 | |
| 66 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10); |
| 67 | |
| 68 | return samples; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | INLINE int vg5k_cas_byte(INT16 *buffer, int sample_pos, UINT8 data) |
| 73 | { |
| 74 | /* Writing an entire byte */ |
| 75 | int i, samples; |
| 76 | |
| 77 | samples = 0; |
| 78 | for ( i = 0; i < 8; i++ ) |
| 79 | { |
| 80 | if ( data & 0x01 ) |
| 81 | { |
| 82 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 ); |
| 83 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5 ); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 10 ); |
| 88 | } |
| 89 | |
| 90 | data >>= 1; |
| 91 | } |
| 92 | return samples; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /******************************************************************* |
| 97 | Generate n sample of synchro |
| 98 | ********************************************************************/ |
| 99 | INLINE int vg5k_k7_synchro(INT16 *buffer, int sample_pos, int len) |
| 100 | { |
| 101 | |
| 102 | int i, samples = 0; |
| 103 | |
| 104 | for ( i = 0; i < len ; i++ ) |
| 105 | samples += vg5k_cas_cycle( buffer, sample_pos + samples, 5); |
| 106 | |
| 107 | samples += vg5k_cas_eob( buffer, sample_pos + samples); |
| 108 | |
| 109 | return samples; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | static int vg5k_handle_tap(INT16 *buffer, const UINT8 *casdata) |
| 114 | { |
| 115 | int data_pos, sample_count; |
| 116 | |
| 117 | data_pos = 0; |
| 118 | sample_count = 0; |
| 119 | |
| 120 | /* on the entire file*/ |
| 121 | while( data_pos < k7_size ) |
| 122 | { |
| 123 | UINT16 block_size; |
| 124 | |
| 125 | /* Identify type of block */ |
| 126 | if (casdata[data_pos] == 0xd3) |
| 127 | { |
| 128 | /* head block have fixed size of 20 byte */ |
| 129 | block_size = 0x20; |
| 130 | |
| 131 | /* 1 sec of silence before the head block */ |
| 132 | sample_count += vg5k_cas_silence(buffer, sample_count, 44100); |
| 133 | |
| 134 | /* head block starts with 15000 samples of synchro */ |
| 135 | sample_count += vg5k_k7_synchro( buffer, sample_count, 15000 ); |
| 136 | } |
| 137 | else if (casdata[data_pos] == 0xd6) |
| 138 | { |
| 139 | /* data block size is defined in head block */ |
| 140 | block_size = (casdata[data_pos - 4] | casdata[data_pos - 3]<<8) + 20; |
| 141 | |
| 142 | /* 10000 samples of silence before the data block */ |
| 143 | sample_count += vg5k_cas_silence(buffer, sample_count, 10000); |
| 144 | |
| 145 | /* data block starts with 3600 samples of synchro */ |
| 146 | sample_count += vg5k_k7_synchro( buffer, sample_count, 3600); |
| 147 | } |
| 148 | else |
| 149 | return -1; |
| 150 | |
| 151 | /* Data samples */ |
| 152 | for ( ; block_size ; data_pos++, block_size-- ) |
| 153 | { |
| 154 | /* Make sure there are enough bytes left */ |
| 155 | if ( data_pos > k7_size ) |
| 156 | return -1; |
| 157 | |
| 158 | sample_count += vg5k_cas_byte( buffer, sample_count, casdata[data_pos] ); |
| 159 | |
| 160 | /* generate the end-byte samples */ |
| 161 | sample_count += vg5k_cas_eob( buffer, sample_count); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* Finish with 10000 samples of silence */ |
| 166 | sample_count += vg5k_cas_silence(buffer, sample_count, 10000); |
| 167 | |
| 168 | return sample_count; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /******************************************************************* |
| 173 | Generate samples for the tape image |
| 174 | ********************************************************************/ |
| 175 | static int vg5k_k7_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes) |
| 176 | { |
| 177 | return vg5k_handle_tap( buffer, bytes ); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /******************************************************************* |
| 182 | Calculate the number of samples needed for this tape image classical |
| 183 | ********************************************************************/ |
| 184 | static int vg5k_k7_to_wav_size(const UINT8 *casdata, int caslen) |
| 185 | { |
| 186 | k7_size = caslen ; |
| 187 | |
| 188 | return vg5k_handle_tap( NULL, casdata ); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | static const struct CassetteLegacyWaveFiller vg5k_legacy_fill_wave = |
| 193 | { |
| 194 | vg5k_k7_fill_wave, /* fill_wave */ |
| 195 | -1, /* chunk_size */ |
| 196 | 0, /* chunk_samples */ |
| 197 | vg5k_k7_to_wav_size, /* chunk_sample_calc */ |
| 198 | 44100, /* sample_frequency */ |
| 199 | 0, /* header_samples */ |
| 200 | 0 /* trailer_samples */ |
| 201 | }; |
| 202 | |
| 203 | static casserr_t vg5k_k7_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 204 | { |
| 205 | return cassette_legacy_identify(cassette, opts, &vg5k_legacy_fill_wave); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static casserr_t vg5k_k7_load(cassette_image *cassette) |
| 210 | { |
| 211 | return cassette_legacy_construct(cassette, &vg5k_legacy_fill_wave); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static const struct CassetteFormat vg5k_k7_format = |
| 216 | { |
| 217 | "k7", |
| 218 | vg5k_k7_identify, |
| 219 | vg5k_k7_load, |
| 220 | NULL |
| 221 | }; |
| 222 | |
| 223 | |
| 224 | CASSETTE_FORMATLIST_START(vg5k_cassette_formats) |
| 225 | CASSETTE_FORMAT(vg5k_k7_format) |
| 226 | CASSETTE_FORMATLIST_END |