Previous 15994 Revisions Next

r15262 Monday 21st May, 2012 at 17:49:33 UTC by Sandro Ronco
tvc: Added .cas cassette image support. [Sandro Ronco]
[src/lib]lib.mak
[src/lib/formats]tvc_cas.c* tvc_cas.h*
[src/mess/drivers]tvc.c
[src/mess/includes]tvc.h
[src/mess/tools/castool]main.c

src/lib/formats/tvc_cas.c
r0r15262
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
20static 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
27static 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
46static 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
57static 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
81static 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
199static 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
215static const struct CassetteFormat tvc64_cassette_image_format =
216{
217   "cas",
218   tvc64_cassette_identify,
219   tvc64_cassette_load,
220   NULL
221};
222
223CASSETTE_FORMATLIST_START(tvc64_cassette_formats)
224   CASSETTE_FORMAT(tvc64_cassette_image_format)
225CASSETTE_FORMATLIST_END
src/lib/formats/tvc_cas.h
r0r15262
1/*********************************************************************
2
3    tvc_cas.h
4
5    Format code for Videoton TVC casette files
6
7*********************************************************************/
8
9#ifndef TVC_CAS_H
10#define TVC_CAS_H
11
12#include "cassimg.h"
13
14CASSETTE_FORMATLIST_EXTERN(tvc64_cassette_formats);
15
16#endif /* TVC_CAS_H */
src/lib/lib.mak
r15261r15262
155155   $(LIBOBJ)/formats/trd_dsk.o      \
156156   $(LIBOBJ)/formats/trs_cas.o      \
157157   $(LIBOBJ)/formats/trs_dsk.o      \
158   $(LIBOBJ)/formats/tvc_cas.o      \
158159   $(LIBOBJ)/formats/tzx_cas.o      \
159160   $(LIBOBJ)/formats/uef_cas.o      \
160161   $(LIBOBJ)/formats/vg5k_cas.o   \
src/mess/tools/castool/main.c
r15261r15262
4040#include "formats/sord_cas.h"
4141#include "formats/svi_cas.h"
4242#include "formats/trs_cas.h"
43#include "formats/tvc_cas.h"
4344#include "formats/tzx_cas.h"
4445#include "formats/uef_cas.h"
4546#include "formats/vg5k_cas.h"
r15261r15262
8687   {"sordm5", sordm5_cassette_formats         ,"Sord M5"},
8788   {"svi", svi_cassette_formats               ,"SVI"},
8889   {"trs80l2", trs80l2_cassette_formats       ,"TRS-80 Level 2"},
90   {"tvc64", tvc64_cassette_formats           ,"Videoton TVC 64"},
8991   {"tzx", tzx_cassette_formats               ,"ZX Spectrum"},
9092   {"cdt", cdt_cassette_formats               ,"Amstrad CPC"},
9193   {"uef", uef_cassette_formats               ,"Acorn Electron"},
src/mess/includes/tvc.h
r15261r15262
1616#include "imagedev/cartslot.h"
1717#include "imagedev/cassette.h"
1818#include "imagedev/snapquik.h"
19#include "formats/tvc_cas.h"
1920
2021#include "machine/tvcexp.h"
2122#include "machine/tvc_hbf.h"
src/mess/drivers/tvc.c
r15261r15262
55        12/05/2009 Skeleton driver.
66
77        TODO:
8        - floppy write doesn't work
8        - UPM crashes when formatting a floppy
99        - overscan and mid-frame changes
10        - load cassette in .cas format
1110
1211****************************************************************************/
1312
r15261r15262
650649
651650static const cassette_interface tvc_cassette_interface =
652651{
653   cassette_default_formats,
652   tvc64_cassette_formats,
654653   NULL,
655654   (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED),
656655   "tvc_cass",

Previous 15994 Revisions Next


© 1998-2018 The MESS Team