Previous 15994 Revisions Next

r3093 Saturday 23rd August, 2008 at 17:06:28 UTC by Wilbert Pol
Fixed SVI cassette format handling
[src/mess/formats]svi_cas.c

src/mess/formats/svi_cas.c
r3092r3093
1#include <string.h>
21#include "svi_cas.h"
32
4#define CAS_PERIOD_0      (40)
3#define CAS_PERIOD_0      (37)
54#define CAS_PERIOD_1      (18)
65#define CAS_HEADER_PERIODS (1600)
7#define CAS_EMPTY_SAMPLES (11025)
8#define ALLOCATE_BLOCK     (1024*8)
6#define CAS_EMPTY_SAMPLES (24220)
7#define CAS_INIT_SAMPLES   (200)
98
109static const UINT8 CasHeader[17] =
1110{
r3092r3093
1312   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x7f
1413};
1514
16#define SMPLO   (INT16)0x8001
17#define SMPHI   (INT16)0x7fff
15#define SMPLO   -32768
16#define SMPHI   32767
1817
19static int svi_cas_to_wav (const UINT8 *casdata, int caslen, INT16 **wavdata, int *wavlen)
18static int cas_size;
19
20/*******************************************************************
21   Generate samples for the tape image
22********************************************************************/
23static int svi_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
2024{
21   int cas_pos, samples_size, bit, samples_pos, size, n, i;
22   INT16 *samples, *nsamples;
25   int cas_pos, samples_pos, n, i;
2326
24   if (caslen < 17) return 1;
25   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return 1;
26
2727   cas_pos = 17;
28   samples_size = ALLOCATE_BLOCK * 2;
29   samples = (INT16*) malloc (samples_size);
30   if (!samples)
31      return 2;
32
3328   samples_pos = 0;
3429
35   while (cas_pos < caslen)
30   /* write CAS_INIT_SAMPLES of silence */
31   n = CAS_INIT_SAMPLES; while (n--) buffer[samples_pos++] = 0;
32
33   while (samples_pos < sample_count && cas_pos < cas_size)
3634   {
37      /* check memory for entire header (silence + header itself) */
38      size = CAS_PERIOD_0 * (CAS_HEADER_PERIODS + 1) +
39            CAS_PERIOD_1 * (CAS_HEADER_PERIODS + 7) +
40            CAS_EMPTY_SAMPLES;
41
42      if ( (samples_pos + size) >= samples_size)
43      {
44         samples_size += size;
45         nsamples = (INT16*) realloc (samples, samples_size * 2);
46         if (!nsamples)
47            {
48            free (samples);
49            return 2;
50            }
51         else samples = nsamples;
52      }
53
54      /* write CAS_EMPTY_PERIODS of silence */
55      n = CAS_EMPTY_SAMPLES; while (n--) samples[samples_pos++] = SMPHI;
56
5735      /* write CAS_HEADER_PERIODS of header */
5836      for (i=0;i<CAS_HEADER_PERIODS;i++)
5937      {
6038         /* write a "0" */
6139         n = !(i % 4) ? 21 : 18;
62         while (n--) samples[samples_pos++] = SMPHI;
63         n = 19; while (n--) samples[samples_pos++] = SMPLO;
40         while (n--) buffer[samples_pos++] = SMPLO;
41         n = 19; while (n--) buffer[samples_pos++] = SMPHI;
6442         /* write a "1" */
65         n = 9; while (n--) samples[samples_pos++] = SMPHI;
66         n = 9; while (n--) samples[samples_pos++] = SMPLO;
43         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
44         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
6745      }
6846
69      /* write 0x7f */
47      /* write "0x7f" */
7048      /* write a "0" */
71      n = 21; while (n--) samples[samples_pos++] = SMPHI;
72      n = 19; while (n--) samples[samples_pos++] = SMPLO;
49      n = 21; while (n--) buffer[samples_pos++] = SMPLO;
50      n = 19; while (n--) buffer[samples_pos++] = SMPHI;
7351
7452      for (i=0;i<7;i++)
7553      {
7654         /* write a "1" */
77         n = 9; while (n--) samples[samples_pos++] = SMPHI;
78         n = 9; while (n--) samples[samples_pos++] = SMPLO;
55         n = 9; while (n--) buffer[samples_pos++] = SMPLO;
56         n = 9; while (n--) buffer[samples_pos++] = SMPHI;
7957      }
80      while (cas_pos < caslen)
58
59      while (samples_pos < sample_count && cas_pos < cas_size)
8160      {
82         /* check if we've hit a new header (or end of block) */
83         if ( (cas_pos + 17) < caslen)
84         {
85            if (!memcmp (casdata + cas_pos, CasHeader, 17) )
86            {
87               cas_pos += 17;
88               break; /* falls back to loop above; plays header again */
89            }
90         }
61         n = 21; while (n--) buffer[samples_pos++] = SMPLO;
62         n = 19; while (n--) buffer[samples_pos++] = SMPHI;
9163
92         /* check if we've got enough memory for the next byte */
93         size = CAS_PERIOD_0 * 9;
94         if ( (samples_pos + size) >= samples_size)
64         for (i=0;i<8;i++)
9565         {
96            samples_size += ALLOCATE_BLOCK;
97            nsamples = (INT16*) realloc (samples, samples_size * 2);
98            if (!nsamples)
99            {
100               free (samples);
101               return 2;
102            }
103            else samples = nsamples;
104         }
66            int bit = (bytes[cas_pos] & (0x80 >> i) );
10567
106         for (i=-1;i<8;i++)
107         {
108            if (i < 0) bit = 0;
109            else bit = (casdata[cas_pos] & (0x80 >> i) );
110
11168            /* write this one bit */
11269            if (bit)
11370            {
11471               /* write a "1" */
115               n = 9; while (n--) samples[samples_pos++] = SMPHI;
116               n = 9; while (n--) samples[samples_pos++] = SMPLO;
72               n = 9; while (n--) buffer[samples_pos++] = SMPLO;
73               n = 9; while (n--) buffer[samples_pos++] = SMPHI;
11774            }
11875            else
11976            {
12077               /* write a "0" */
121               n = (i < 0) ? 21 : 18;
122               while (n--) samples[samples_pos++] = SMPHI;
123               n = 19; while (n--) samples[samples_pos++] = SMPLO;
78               n = 18; while (n--) buffer[samples_pos++] = SMPLO;
79               n = 19; while (n--) buffer[samples_pos++] = SMPHI;
12480            }
12581         }
12682
12783         cas_pos++;
84
85         /* check if we've hit a new header (or end of block) */
86         if ( (cas_pos + 17) < cas_size)
87         {
88            if (!memcmp (bytes + cas_pos, CasHeader, 17) )
89            {
90               cas_pos += 17;
91
92               /* end of block marker */
93               n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
94
95               break; /* falls back to loop above; plays header again */
96            }
97         }
12898      }
12999   }
130100
131   *wavdata = samples;
132   *wavlen = samples_pos;
101   /* end of block marker */
102   n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;
133103
134   return 0;
104   return samples_pos;
135105}
136106
137107
138
139static int svi_cas_fill_wave(INT16 *buffer, int length, UINT8 *bytes)
108/*******************************************************************
109   Calculate the number of samples needed for this tape image
110********************************************************************/
111static int svi_cas_to_wav_size(const UINT8 *casdata, int caslen)
140112{
141   INT16 *wavdata;
142   int wavlen;
113   int cas_pos, samples_pos, size, i;
143114
144   if (svi_cas_to_wav(bytes, length, &wavdata, &wavlen))
145      return -1;
115   if (caslen < 17) return -1;
116   if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;
146117
147   memcpy(buffer, wavdata, wavlen * 2);
148   free(wavdata);
149   return 0;
150}
118   cas_size = caslen;
151119
120   cas_pos = 17;
152121
153static int svi_cas_chunk_sample_calc(const UINT8 *bytes, int length)
154{
155   INT16 *wavdata;
156   int wavlen;
122   samples_pos = CAS_INIT_SAMPLES;
157123
158   if (svi_cas_to_wav(bytes, length, &wavdata, &wavlen))
159      return -1;
124   while (cas_pos < caslen)
125   {
126      size = CAS_HEADER_PERIODS * ( CAS_PERIOD_0 + CAS_PERIOD_1 ) +
127            ( CAS_HEADER_PERIODS / 4 ) * 3;
160128
161   free(wavdata);
162   return wavlen;
129      samples_pos += size;
130
131      samples_pos += 21 + 19 + 7 * CAS_PERIOD_1;
132
133      while (cas_pos < caslen)
134      {
135         samples_pos += 21;
136         samples_pos += 19;
137
138         for (i=0;i<8;i++)
139         {
140            int bit = (casdata[cas_pos] & (0x80 >> i) );
141
142            samples_pos += bit ? CAS_PERIOD_1 : CAS_PERIOD_0;
143         }
144
145         cas_pos++;
146
147         /* check if we've hit a new header (or end of block) */
148         if ( (cas_pos + 17) < caslen)
149         {
150            if (!memcmp (casdata + cas_pos, CasHeader, 17) )
151            {
152               cas_pos += 17;
153
154               /* end of block marker */
155               samples_pos += CAS_EMPTY_SAMPLES;
156
157               break; /* falls back to loop above; plays header again */
158            }
159         }
160      }
161   }
162
163   /* end of block marker */
164   samples_pos += CAS_EMPTY_SAMPLES;
165
166   return samples_pos;
163167}
164168
165169
166
167170static const struct CassetteLegacyWaveFiller svi_legacy_fill_wave =
168171{
169172   svi_cas_fill_wave,                  /* fill_wave */
170173   -1,                              /* chunk_size */
171174   0,                              /* chunk_samples */
172   svi_cas_chunk_sample_calc,            /* chunk_sample_calc */
173   22050,                           /* sample_frequency */
174   -1,                              /* header_samples */
175   svi_cas_to_wav_size,               /* chunk_sample_calc */
176   44100,                           /* sample_frequency */
177   0,                              /* header_samples */
175178   0                              /* trailer_samples */
176179};
177180

Previous 15994 Revisions Next


© 1998-2018 The MESS Team