src/lib/formats/tvc_cas.c
| r0 | r15262 | |
| 1 | /******************************************************************** |
| 2 | |
| 3 | Support for Videoton TVC .cas files |
| 4 | |
| 5 | http://tvc.homeserver.hu/html/konvertformatum.html |
| 6 | |
| 7 | ********************************************************************/ |
| 8 | |
| 9 | #include "tvc_cas.h" |
| 10 | |
| 11 | #define TVC64_BIT0_FREQ 1812 |
| 12 | #define TVC64_BIT1_FREQ 2577 |
| 13 | #define TVC64_PRE_FREQ 2128 |
| 14 | #define TVC64_SYNC_FREQ 1359 |
| 15 | |
| 16 | #define WAVE_AMPLITUDE 0x3fffffff |
| 17 | #define TVC64_HEADER_BYTES 0x90 |
| 18 | #define TVC64_HEADER_BYTES 0x90 |
| 19 | |
| 20 | static void tvc64_emit_level(cassette_image *cass, double &time, int freq, int level) |
| 21 | { |
| 22 | double period = 1.0 / freq; |
| 23 | cassette_put_sample(cass, 0, time, period, level * WAVE_AMPLITUDE); |
| 24 | time += period; |
| 25 | } |
| 26 | |
| 27 | static casserr_t tvc64_output_byte(cassette_image *cass, double &time, UINT8 byte) |
| 28 | { |
| 29 | for (int i=0; i<8; i++) |
| 30 | { |
| 31 | if ((byte>>i) & 0x01) |
| 32 | { |
| 33 | tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, +1); |
| 34 | tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, -1); |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, +1); |
| 39 | tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, -1); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return CASSETTE_ERROR_SUCCESS; |
| 44 | } |
| 45 | |
| 46 | static int tvc64_output_predata(cassette_image *cass, double &time, int number) |
| 47 | { |
| 48 | for (int i=0; i<number; i++) |
| 49 | { |
| 50 | tvc64_emit_level(cass, time, TVC64_PRE_FREQ*2, +1); |
| 51 | tvc64_emit_level(cass, time, TVC64_PRE_FREQ*2, -1); |
| 52 | } |
| 53 | |
| 54 | return CASSETTE_ERROR_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | static UINT16 tvc64_calc_crc(const UINT8 *bytes, int size) |
| 58 | { |
| 59 | UINT16 crc = 0; |
| 60 | |
| 61 | for (int i=0; i<size; i++) |
| 62 | { |
| 63 | for (int b=0; b<8; b++) |
| 64 | { |
| 65 | UINT8 al = (bytes[i] & (1<<b)) ? 0x80 : 0x00; |
| 66 | |
| 67 | al ^= ((crc>>8) & 0xff); |
| 68 | |
| 69 | if (al & 0x80) |
| 70 | crc ^= 0x0810; |
| 71 | |
| 72 | crc <<= 1; |
| 73 | |
| 74 | if (al & 0x80) crc += 1; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return crc; |
| 79 | } |
| 80 | |
| 81 | static casserr_t tvc64_cassette_load(cassette_image *cassette) |
| 82 | { |
| 83 | UINT8 tmp_buff[512]; |
| 84 | int buff_idx = 0; |
| 85 | double time = 0.0; |
| 86 | |
| 87 | UINT8 header[TVC64_HEADER_BYTES]; |
| 88 | cassette_image_read(cassette, header, 0, TVC64_HEADER_BYTES); |
| 89 | UINT16 cas_size = (header[0x83]<<8) | header[0x82]; |
| 90 | |
| 91 | // tape header |
| 92 | tmp_buff[buff_idx++] = 0x00; |
| 93 | tmp_buff[buff_idx++] = 0x6a; |
| 94 | tmp_buff[buff_idx++] = 0xff; // head sector |
| 95 | tmp_buff[buff_idx++] = 0x11; // not puffered |
| 96 | tmp_buff[buff_idx++] = 0x00; // not write protected |
| 97 | tmp_buff[buff_idx++] = 0x01; // 1 sector |
| 98 | tmp_buff[buff_idx++] = 0x00; // sector number |
| 99 | tmp_buff[buff_idx++] = 0x12; // head size |
| 100 | tmp_buff[buff_idx++] = 0x01; // name size |
| 101 | tmp_buff[buff_idx++] = ' '; // name |
| 102 | tmp_buff[buff_idx++] = 0x00; |
| 103 | tmp_buff[buff_idx++] = header[0x81]; // type |
| 104 | tmp_buff[buff_idx++] = header[0x82]; // size LSB |
| 105 | tmp_buff[buff_idx++] = header[0x83]; // size MSB |
| 106 | tmp_buff[buff_idx++] = header[0x84]; // autostart |
| 107 | |
| 108 | // sector fill |
| 109 | for (int i=0; i<10 ; i++) |
| 110 | tmp_buff[buff_idx++] = 0x00; |
| 111 | |
| 112 | tmp_buff[buff_idx++] = 0x00; // file version |
| 113 | tmp_buff[buff_idx++] = 0x00; // no last sector |
| 114 | |
| 115 | // updates the header CRC |
| 116 | UINT16 crc = tvc64_calc_crc(tmp_buff, buff_idx); |
| 117 | tmp_buff[buff_idx++] = crc & 0xff; |
| 118 | tmp_buff[buff_idx++] = (crc>>8) & 0xff; |
| 119 | |
| 120 | // 2 sec silence |
| 121 | tvc64_emit_level(cassette, time, 2, 0); |
| 122 | |
| 123 | // 10240 pre data cycles |
| 124 | tvc64_output_predata(cassette, time, 10240); |
| 125 | |
| 126 | // 1 synchro cycle |
| 127 | tvc64_emit_level(cassette, time, TVC64_SYNC_FREQ*2, +1); |
| 128 | tvc64_emit_level(cassette, time, TVC64_SYNC_FREQ*2, -1); |
| 129 | |
| 130 | // header data |
| 131 | for (int i=0; i<buff_idx; i++) |
| 132 | tvc64_output_byte(cassette, time, tmp_buff[i]); |
| 133 | |
| 134 | // 5 post data cycles |
| 135 | tvc64_output_predata(cassette, time, 5); |
| 136 | |
| 137 | // 1 sec silence |
| 138 | tvc64_emit_level(cassette, time, 1, 0); |
| 139 | |
| 140 | // 5120 pre data cycles |
| 141 | tvc64_output_predata(cassette, time, 5120); |
| 142 | |
| 143 | // 1 synchro cycle |
| 144 | tvc64_emit_level(cassette, time, TVC64_SYNC_FREQ*2, +1); |
| 145 | tvc64_emit_level(cassette, time, TVC64_SYNC_FREQ*2, -1); |
| 146 | |
| 147 | // first data sector contain the data header |
| 148 | buff_idx = 0; |
| 149 | tmp_buff[buff_idx++] = 0x00; |
| 150 | tmp_buff[buff_idx++] = 0x6a; |
| 151 | tmp_buff[buff_idx++] = 0x00; // data sector |
| 152 | tmp_buff[buff_idx++] = 0x11; // not puffered |
| 153 | tmp_buff[buff_idx++] = 0x00; // not write protected |
| 154 | tmp_buff[buff_idx++] = (UINT8)((cas_size / 256) + ((cas_size % 256) > 0 ? 1 : 0)); // number of sectors |
| 155 | |
| 156 | UINT8 sect_num = 1; |
| 157 | int sector_num = cas_size / 256; |
| 158 | for (int i=0; i<=sector_num; i++) |
| 159 | { |
| 160 | tmp_buff[buff_idx++] = sect_num++; // sector number |
| 161 | |
| 162 | // sector size |
| 163 | if (i == sector_num) |
| 164 | tmp_buff[buff_idx++] = cas_size % 256; |
| 165 | else |
| 166 | tmp_buff[buff_idx++] = 0x00; |
| 167 | |
| 168 | // sector data |
| 169 | int sector_size = (i == sector_num) ? (cas_size % 256) : 256; |
| 170 | for (int z=0; z < sector_size; z++) |
| 171 | cassette_image_read(cassette, &tmp_buff[buff_idx++], TVC64_HEADER_BYTES + i*256 + z, 1); |
| 172 | |
| 173 | if (i == sector_num || ((i+1) == sector_num && (cas_size % 256 ) == 0)) |
| 174 | tmp_buff[buff_idx++] = 0xff; // last sector |
| 175 | else |
| 176 | tmp_buff[buff_idx++] = 0x00; // no last sector |
| 177 | |
| 178 | // sector crc |
| 179 | crc = tvc64_calc_crc(tmp_buff, buff_idx); |
| 180 | tmp_buff[buff_idx++] = crc & 0xff; |
| 181 | tmp_buff[buff_idx++] = (crc>>8) & 0xff; |
| 182 | |
| 183 | // output the sector |
| 184 | for (int z=0; z<buff_idx; z++) |
| 185 | tvc64_output_byte(cassette, time, tmp_buff[z]); |
| 186 | |
| 187 | buff_idx = 0; |
| 188 | } |
| 189 | |
| 190 | // 5 post data cycles |
| 191 | tvc64_output_predata(cassette, time, 5); |
| 192 | |
| 193 | // 1 sec silence |
| 194 | tvc64_emit_level(cassette, time, 1, 0); |
| 195 | |
| 196 | return CASSETTE_ERROR_SUCCESS; |
| 197 | } |
| 198 | |
| 199 | static casserr_t tvc64_cassette_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 200 | { |
| 201 | UINT8 byte; |
| 202 | cassette_image_read(cassette, &byte, 0, 1); |
| 203 | |
| 204 | if (byte == 0x11) |
| 205 | { |
| 206 | opts->bits_per_sample = 16; |
| 207 | opts->channels = 1; |
| 208 | opts->sample_frequency = 44100; |
| 209 | return CASSETTE_ERROR_SUCCESS; |
| 210 | } |
| 211 | |
| 212 | return CASSETTE_ERROR_INVALIDIMAGE; |
| 213 | } |
| 214 | |
| 215 | static const struct CassetteFormat tvc64_cassette_image_format = |
| 216 | { |
| 217 | "cas", |
| 218 | tvc64_cassette_identify, |
| 219 | tvc64_cassette_load, |
| 220 | NULL |
| 221 | }; |
| 222 | |
| 223 | CASSETTE_FORMATLIST_START(tvc64_cassette_formats) |
| 224 | CASSETTE_FORMAT(tvc64_cassette_image_format) |
| 225 | CASSETTE_FORMATLIST_END |