summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/svi_cas.cpp
blob: dd2bde8d5a1f238645a606711b61206b69e10557 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// license:BSD-3-Clause
// copyright-holders:Sean Young
#include <assert.h>

#include "svi_cas.h"

#define CAS_PERIOD_0        (37)
#define CAS_PERIOD_1        (18)
#define CAS_HEADER_PERIODS (1600)
#define CAS_EMPTY_SAMPLES (24220)
#define CAS_INIT_SAMPLES    (200)

static const UINT8 CasHeader[17] =
{
	0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
	0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x7f
};

#define SMPLO   -32768
#define SMPHI   32767

static int cas_size;

/*******************************************************************
   Generate samples for the tape image
********************************************************************/
static int svi_cas_fill_wave(INT16 *buffer, int sample_count, UINT8 *bytes)
{
	int cas_pos, samples_pos, n, i;

	cas_pos = 17;
	samples_pos = 0;

	/* write CAS_INIT_SAMPLES of silence */
	n = CAS_INIT_SAMPLES; while (n--) buffer[samples_pos++] = 0;

	while (samples_pos < sample_count && cas_pos < cas_size)
	{
		/* write CAS_HEADER_PERIODS of header */
		for (i=0;i<CAS_HEADER_PERIODS;i++)
		{
			/* write a "0" */
			n = !(i % 4) ? 21 : 18;
			while (n--) buffer[samples_pos++] = SMPHI;
			n = 19; while (n--) buffer[samples_pos++] = SMPLO;
			/* write a "1" */
			n = 9; while (n--) buffer[samples_pos++] = SMPHI;
			n = 9; while (n--) buffer[samples_pos++] = SMPLO;
		}

		/* write "0x7f" */
		/* write a "0" */
		n = 21; while (n--) buffer[samples_pos++] = SMPHI;
		n = 19; while (n--) buffer[samples_pos++] = SMPLO;

		for (i=0;i<7;i++)
		{
			/* write a "1" */
			n = 9; while (n--) buffer[samples_pos++] = SMPHI;
			n = 9; while (n--) buffer[samples_pos++] = SMPLO;
		}

		while (samples_pos < sample_count && cas_pos < cas_size)
		{
			n = 21; while (n--) buffer[samples_pos++] = SMPHI;
			n = 19; while (n--) buffer[samples_pos++] = SMPLO;

			for (i=0;i<8;i++)
			{
				int bit = (bytes[cas_pos] & (0x80 >> i) );

				/* write this one bit */
				if (bit)
				{
					/* write a "1" */
					n = 9; while (n--) buffer[samples_pos++] = SMPHI;
					n = 9; while (n--) buffer[samples_pos++] = SMPLO;
				}
				else
				{
					/* write a "0" */
					n = 18; while (n--) buffer[samples_pos++] = SMPHI;
					n = 19; while (n--) buffer[samples_pos++] = SMPLO;
				}
			}

			cas_pos++;

			/* check if we've hit a new header (or end of block) */
			if ( (cas_pos + 17) < cas_size)
			{
				if (!memcmp (bytes + cas_pos, CasHeader, 17) )
				{
					cas_pos += 17;

					/* end of block marker */
					n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;

					break; /* falls back to loop above; plays header again */
				}
			}
		}
	}

	/* end of block marker */
	n = CAS_EMPTY_SAMPLES; while (n--) buffer[samples_pos++] = SMPHI;

	return samples_pos;
}


/*******************************************************************
   Calculate the number of samples needed for this tape image
********************************************************************/
static int svi_cas_to_wav_size(const UINT8 *casdata, int caslen)
{
	int cas_pos, samples_pos, size, i;

	if (caslen < 17) return -1;
	if (memcmp (casdata, CasHeader, sizeof (CasHeader) ) ) return -1;

	cas_size = caslen;

	cas_pos = 17;

	samples_pos = CAS_INIT_SAMPLES;

	while (cas_pos < caslen)
	{
		size = CAS_HEADER_PERIODS * ( CAS_PERIOD_0 + CAS_PERIOD_1 ) +
				( CAS_HEADER_PERIODS / 4 ) * 3;

		samples_pos += size;

		samples_pos += 21 + 19 + 7 * CAS_PERIOD_1;

		while (cas_pos < caslen)
		{
			samples_pos += 21;
			samples_pos += 19;

			for (i=0;i<8;i++)
			{
				int bit = (casdata[cas_pos] & (0x80 >> i) );

				samples_pos += bit ? CAS_PERIOD_1 : CAS_PERIOD_0;
			}

			cas_pos++;

			/* check if we've hit a new header (or end of block) */
			if ( (cas_pos + 17) < caslen)
			{
				if (!memcmp (casdata + cas_pos, CasHeader, 17) )
				{
					cas_pos += 17;

					/* end of block marker */
					samples_pos += CAS_EMPTY_SAMPLES;

					break; /* falls back to loop above; plays header again */
				}
			}
		}
	}

	/* end of block marker */
	samples_pos += CAS_EMPTY_SAMPLES;

	return samples_pos;
}


static const struct CassetteLegacyWaveFiller svi_legacy_fill_wave =
{
	svi_cas_fill_wave,                      /* fill_wave */
	-1,                                     /* chunk_size */
	0,                                      /* chunk_samples */
	svi_cas_to_wav_size,                    /* chunk_sample_calc */
	44100,                                  /* sample_frequency */
	0,                                      /* header_samples */
	0                                       /* trailer_samples */
};



static casserr_t svi_cas_identify(cassette_image *cassette, struct CassetteOptions *opts)
{
	return cassette_legacy_identify(cassette, opts, &svi_legacy_fill_wave);
}



static casserr_t svi_cas_load(cassette_image *cassette)
{
	return cassette_legacy_construct(cassette, &svi_legacy_fill_wave);
}



static const struct CassetteFormat svi_cas_format =
{
	"cas",
	svi_cas_identify,
	svi_cas_load,
	NULL
};



CASSETTE_FORMATLIST_START(svi_cassette_formats)
	CASSETTE_FORMAT(svi_cas_format)
CASSETTE_FORMATLIST_END