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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
// license:BSD-3-Clause
// copyright-holders:Robbbert,Nigel Barnes
/********************************************************************
Support for Camputers Lynx cassette images
We support TAP files used by the Pale and Jynx emulators.
Tape format:
- about 7 seconds of zeroes
- A5 byte
- 22 byte
- program name
- 22 byte
- about 7 seconds of zeroes
- A5 byte
- header
- main program
- checksum
Each byte is 8 bits (MSB first) with no start or stop bits.
********************************************************************/
#include "emu.h" // for popmessage and <string>
#include "camplynx_cas.h"
#define WAVEENTRY_LOW -32768
#define WAVEENTRY_HIGH 32767
#define LYNX48K_WAV_FREQUENCY 4000
#define LYNX128K_WAV_FREQUENCY 8000
// image size
static int camplynx_image_size;
static int camplynx_put_samples(int16_t *buffer, int sample_pos, int count, int level)
{
if (buffer)
{
for (int i=0; i<count; i++)
buffer[sample_pos + i] = level;
}
return count;
}
static int camplynx_output_bit(int16_t *buffer, int sample_pos, bool bit)
{
int samples = 0;
if (bit)
{
samples += camplynx_put_samples(buffer, sample_pos + samples, 4, WAVEENTRY_HIGH);
samples += camplynx_put_samples(buffer, sample_pos + samples, 4, WAVEENTRY_LOW);
}
else
{
samples += camplynx_put_samples(buffer, sample_pos + samples, 2, WAVEENTRY_HIGH);
samples += camplynx_put_samples(buffer, sample_pos + samples, 2, WAVEENTRY_LOW);
}
return samples;
}
static int camplynx_output_byte(int16_t *buffer, int sample_pos, uint8_t byte)
{
int samples = 0;
uint8_t i;
/* data */
for (i = 0; i<8; i++)
samples += camplynx_output_bit (buffer, sample_pos + samples, (byte >> (7-i)) & 1);
return samples;
}
static int camplynx_handle_cassette(int16_t *buffer, const uint8_t *bytes)
{
uint32_t sample_count = 0;
uint32_t byte_count = 0;
uint32_t data_size = 0;
uint32_t i;
uint8_t file_type;
std::string pgmname = "";
while (byte_count < camplynx_image_size)
{
/* initial SYNC + A5 applies to all file types */
for (i = 0; i < 555; i++)
sample_count += camplynx_output_byte(buffer, sample_count, 0);
sample_count += camplynx_output_byte(buffer, sample_count, 0xA5);
/* some TAPs have a spurious A5 at the start, ignore */
while (bytes[byte_count] == 0xA5)
byte_count++;
if (bytes[byte_count] == 0x22)
{
pgmname = " LOAD \"";
byte_count++;
sample_count += camplynx_output_byte(buffer, sample_count, 0x22);
/* output program name - include protection in case tape is corrupt */
for (i = byte_count; bytes[i] != 0x22; i++)
{
if (i < camplynx_image_size)
{
sample_count += camplynx_output_byte(buffer, sample_count, bytes[i]);
pgmname.append(1, (char)bytes[i]);
}
else
return sample_count;
byte_count++;
}
pgmname.append(1, (char)0x22);
sample_count += camplynx_output_byte(buffer, sample_count, bytes[byte_count++]); // should be 0x22
/* read file type letter, should be 'B' or 'M' */
file_type = bytes[byte_count];
/* if a machine-language program, say to use MLOAD */
if (file_type == 'M') pgmname[0] = 'M';
/* tell user how to load the tape */
if (buffer)
osd_printf_info("%s\n", pgmname);
/* second SYNC + A5 */
for (i = 0; i < 555; i++)
sample_count += camplynx_output_byte(buffer, sample_count, 0);
sample_count += camplynx_output_byte(buffer, sample_count, 0xA5);
}
/* read file type letter, should be 'A', 'B' or 'M' */
file_type = bytes[byte_count];
/* determine the data size (as recorded in the file) + extra bytes per file type */
switch (file_type)
{
case 'A':
data_size = 5 + ((bytes[byte_count + 4]) << 8 | bytes[byte_count + 3]) + 12;
break;
case 'B':
data_size = 3 + ((bytes[byte_count + 2]) << 8 | bytes[byte_count + 1]) + 3;
break;
case 'M':
data_size = 3 + ((bytes[byte_count + 2]) << 8 | bytes[byte_count + 1]) + 7;
break;
}
/* output data - include protection in case tape is corrupt */
for (i = byte_count; i < byte_count + data_size; i++)
{
if (i < camplynx_image_size)
{
sample_count += camplynx_output_byte(buffer, sample_count, bytes[i]);
}
}
byte_count += data_size;
/* some TAPs have a spurious 00 at the end, ignore */
while (bytes[byte_count] == 0x00)
byte_count++;
}
return sample_count;
}
/*******************************************************************
Generate samples for the tape image
********************************************************************/
static int camplynx_cassette_fill_wave(int16_t *buffer, int length, uint8_t *bytes)
{
return camplynx_handle_cassette(buffer, bytes);
}
/*******************************************************************
Calculate the number of samples needed for this tape image
********************************************************************/
static int camplynx_cassette_calculate_size_in_samples(const uint8_t *bytes, int length)
{
camplynx_image_size = length;
return camplynx_handle_cassette(nullptr, bytes);
}
static const struct CassetteLegacyWaveFiller lynx48k_legacy_fill_wave =
{
camplynx_cassette_fill_wave, /* fill_wave */
-1, /* chunk_size */
0, /* chunk_samples */
camplynx_cassette_calculate_size_in_samples, /* chunk_sample_calc */
LYNX48K_WAV_FREQUENCY, /* sample_frequency */
0, /* header_samples */
0 /* trailer_samples */
};
static const struct CassetteLegacyWaveFiller lynx128k_legacy_fill_wave =
{
camplynx_cassette_fill_wave, /* fill_wave */
-1, /* chunk_size */
0, /* chunk_samples */
camplynx_cassette_calculate_size_in_samples, /* chunk_sample_calc */
LYNX128K_WAV_FREQUENCY, /* sample_frequency */
0, /* header_samples */
0 /* trailer_samples */
};
static cassette_image::error lynx48k_cassette_identify(cassette_image *cassette, struct CassetteOptions *opts)
{
return cassette_legacy_identify(cassette, opts, &lynx48k_legacy_fill_wave);
}
static cassette_image::error lynx128k_cassette_identify(cassette_image *cassette, struct CassetteOptions *opts)
{
return cassette_legacy_identify(cassette, opts, &lynx128k_legacy_fill_wave);
}
static cassette_image::error lynx48k_cassette_load(cassette_image *cassette)
{
return cassette_legacy_construct(cassette, &lynx48k_legacy_fill_wave);
}
static cassette_image::error lynx128k_cassette_load(cassette_image *cassette)
{
return cassette_legacy_construct(cassette, &lynx128k_legacy_fill_wave);
}
static const struct CassetteFormat lynx48k_cassette_image_format =
{
"tap",
lynx48k_cassette_identify,
lynx48k_cassette_load,
nullptr
};
static const struct CassetteFormat lynx128k_cassette_image_format =
{
"tap",
lynx128k_cassette_identify,
lynx128k_cassette_load,
nullptr
};
CASSETTE_FORMATLIST_START(lynx48k_cassette_formats)
CASSETTE_FORMAT(lynx48k_cassette_image_format)
CASSETTE_FORMATLIST_END
CASSETTE_FORMATLIST_START(lynx128k_cassette_formats)
CASSETTE_FORMAT(lynx128k_cassette_image_format)
CASSETTE_FORMATLIST_END
|