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
260
261
262
263
264
265
266
267
268
269
270
|
// license:BSD-3-Clause
// copyright-holders:Wilbert Pol
/********************************************************************
Support for TRS80 .cas cassette images
Types handled:
- Model 1 Level I: 250 baud
- Model 1 Level II: 500 baud
- Model 3/4: 1500 baud.
Level I and II tape format is completely identical, apart from the
baud rate. The contents are specific to each system though.
The Model 3 and 4 can load either Level II tapes (by answering L
to the Cass? prompt), or the fast format by hitting enter at Cass?
********************************************************************/
#include "formats/trs_cas.h"
#define SILENCE 0
#define SMPLO -32768
#define SMPHI 32767
static int cas_size = 0; // FIXME: global variable prevents multiple instances
/*******************************************************************
Generate one high-low cycle of sample data
********************************************************************/
static inline int trs80m1_cas_cycle(int16_t *buffer, int sample_pos, bool bit)
{
uint8_t i;
if ( buffer )
{
for (i = 0; i < 32; i++)
buffer[ sample_pos++ ] = SILENCE;
for (i = 0; i < 6; i++)
buffer[ sample_pos++ ] = bit ? SMPHI : SILENCE;
for (i = 0; i < 6; i++)
buffer[ sample_pos++ ] = bit ? SMPLO : SILENCE;
}
return 44;
}
static int trs80m1_handle_cas(int16_t *buffer, const uint8_t *casdata)
{
int data_pos = 0, sample_count = 0;
bool sw = false;
// Make sure this is a trs80 tape
// Should have some zero bytes then one 0xA5
while ((cas_size > data_pos) && (casdata[data_pos] == 0x00))
data_pos++;
if (casdata[data_pos] != 0xA5)
return 0;
data_pos = 0;
while( data_pos < cas_size )
{
uint8_t data = casdata[data_pos];
for (uint8_t i = 0; i < 8; i++ )
{
/* Signal code */
sample_count += trs80m1_cas_cycle( buffer, sample_count, true );
/* Bit code */
sample_count += trs80m1_cas_cycle( buffer, sample_count, data >> 7 );
data <<= 1;
}
if (!sw && (casdata[data_pos] == 0xA5))
{
sw = true;
// Need 1ms silence here while rom is busy
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
}
data_pos++;
}
// Specification requires a short silence to indicate EOF
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
return sample_count;
}
static inline int trs80m3_cas_cycle(int16_t *buffer, int sample_pos, bool bit)
{
uint8_t i, counts = bit ? 8 : 16;
if ( buffer )
{
for (i = 0; i < counts; i++)
buffer[ sample_pos++ ] = SMPHI;
for (i = 0; i < counts; i++)
buffer[ sample_pos++ ] = SMPLO;
}
return counts*2;
}
static int trs80m3_handle_cas(int16_t *buffer, const uint8_t *casdata)
{
int data_pos = 0, sample_count = 0;
uint8_t sw = 0, bitout = 0, byteout = 0;
// Make sure this is a trs80m3 tape
// Should have ~256 0x55 then one 0x7f
// It's possible that 0x57 could be encountered instead,
// but no working tapes with it have been found.
// Other bit-shifted variants might exist too.
while ((cas_size > data_pos) && (casdata[data_pos] == 0x55))
data_pos++;
if (casdata[data_pos] != 0x7f)
return 0;
data_pos = 0;
while( data_pos < cas_size )
{
uint8_t data = casdata[data_pos];
for (uint8_t i = 0; i < 8; i++ )
{
sample_count += trs80m3_cas_cycle( buffer, sample_count, data >> 7 );
// This paragraph unscrambles and prints the SYSTEM name.
// If the first character is U, type SYSTEM, then the next 6 characters; otherwise use CLOAD.
if ((sw == 1) && buffer)
{
if (bitout)
{
byteout = (byteout << 1) | (data >> 7);
if (bitout == 8)
{
if (byteout == 0)
sw = 2;
printf("%c",byteout);
byteout = 0;
bitout = 0;
}
else
bitout++;
}
else bitout++;
}
data <<= 1;
}
if (!sw && (casdata[data_pos] != 0x55))
{
sw = 1;
// This 1ms of silence isn't absolutely necessary, but the system
// writes it, so we may as well emulate it.
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
}
data_pos++;
}
// Specification requires a short silence to indicate EOF
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
sample_count += trs80m1_cas_cycle( buffer, sample_count, false );
return sample_count;
}
/*******************************************************************
Generate samples for the tape image
********************************************************************/
static int trs80_cas_fill_wave(int16_t *buffer, int sample_count, const uint8_t *bytes)
{
if (cas_size && (bytes[0] == 0x55))
return trs80m3_handle_cas( buffer, bytes );
else
return trs80m1_handle_cas( buffer, bytes );
}
/*******************************************************************
Calculate the number of samples needed for this tape image
********************************************************************/
static int trs80_cas_to_wav_size(const uint8_t *casdata, int caslen)
{
cas_size = caslen;
if (cas_size && (casdata[0] == 0x55))
return trs80m3_handle_cas( nullptr, casdata );
else
return trs80m1_handle_cas( nullptr, casdata );
}
static const cassette_image::LegacyWaveFiller trs80l1_cas_legacy_fill_wave =
{
trs80_cas_fill_wave, /* fill_wave */
-1, /* chunk_size */
0, /* chunk_samples */
trs80_cas_to_wav_size, /* chunk_sample_calc */
22050, /* sample_frequency */
0, /* header_samples */
0 /* trailer_samples */
};
static cassette_image::error trs80l1_cas_identify(cassette_image *cassette, cassette_image::Options *opts)
{
return cassette->legacy_identify(opts, &trs80l1_cas_legacy_fill_wave);
}
static cassette_image::error trs80l1_cas_load(cassette_image *cassette)
{
return cassette->legacy_construct(&trs80l1_cas_legacy_fill_wave);
}
static const cassette_image::Format trs80l1_cas_format =
{
"cas",
trs80l1_cas_identify,
trs80l1_cas_load,
nullptr
};
static const cassette_image::LegacyWaveFiller trs80l2_cas_legacy_fill_wave =
{
trs80_cas_fill_wave, /* fill_wave */
-1, /* chunk_size */
0, /* chunk_samples */
trs80_cas_to_wav_size, /* chunk_sample_calc */
44100, /* sample_frequency */
0, /* header_samples */
0 /* trailer_samples */
};
static cassette_image::error trs80l2_cas_identify(cassette_image *cassette, cassette_image::Options *opts)
{
return cassette->legacy_identify(opts, &trs80l2_cas_legacy_fill_wave);
}
static cassette_image::error trs80l2_cas_load(cassette_image *cassette)
{
return cassette->legacy_construct(&trs80l2_cas_legacy_fill_wave);
}
static const cassette_image::Format trs80l2_cas_format =
{
"cas",
trs80l2_cas_identify,
trs80l2_cas_load,
nullptr
};
CASSETTE_FORMATLIST_START(trs80l1_cassette_formats)
CASSETTE_FORMAT(trs80l1_cas_format)
CASSETTE_FORMATLIST_END
CASSETTE_FORMATLIST_START(trs80l2_cassette_formats)
CASSETTE_FORMAT(trs80l2_cas_format)
CASSETTE_FORMATLIST_END
|