summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/csw_cas.cpp
blob: e55346cef4e6abd8b6bc7537acc8db339ae40a32 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// license:BSD-3-Clause
// copyright-holders:Nigel Barnes
/*
  CSW format
  ----------
  Header Description

  Offset  Value   Type    Description
  0x00    (note)  ASCII   22 bytes "Compressed Square Wave" signature
  0x16    0x1A    BYTE    Terminator code
  0x17    0x02    BYTE    CSW major revision number
  0x18    0x00    BYTE    CSW minor revision number
  0x19            DWORD   Sample rate
  0x1D            DWORD   Total number of pulses (after decompression)
  0x21            BYTE    Compression type  0x01: RLE    0x02: Z-RLE
  0x22            BYTE    Flags   b0: initial polarity; if set, the signal starts at logical high
  0x23    HDR     BYTE    Header extension length in bytes (0x00)
  0x24            ASCII   Encoding application description
  0x34            BYTE    Header extension data (if present HDR>0)
  0x34+HDR                CSW data
*/

#include "csw_cas.h"
#include "uef_cas.h"

#include <zlib.h>

#include <cassert>
#include <cstring>


static const uint8_t CSW_HEADER[] = { "Compressed Square Wave" };


/*-------------------------------------------------
    csw_cassette_identify - identify cassette
-------------------------------------------------*/

static cassette_image::error csw_cassette_identify(cassette_image *cassette, cassette_image::Options *opts)
{
	uint8_t header[0x34];

	cassette->image_read(header, 0, sizeof(header));
	if (memcmp(&header[0], CSW_HEADER, sizeof(CSW_HEADER) - 1)) {
		return cassette_image::error::INVALID_IMAGE;
	}

	opts->bits_per_sample = 8;
	opts->channels = 1;
	opts->sample_frequency = little_endianize_int16(*(uint32_t*)(header + 0x19));
	return cassette_image::error::SUCCESS;
}


/*-------------------------------------------------
    csw_cassette_load - load cassette
-------------------------------------------------*/

static cassette_image::error csw_cassette_load(cassette_image *cassette)
{
	uint8_t  header[0x34];
	uint64_t image_size = cassette->image_size();
	std::vector<uint8_t> image_data(image_size);

	int8_t bit;
	uint8_t compression;
	int bsize = 0;
	size_t csw_data = 0;
	size_t sample_count = 0;
	uint32_t sample_rate;
	std::vector<int8_t> samples;

	/* csw header */
	cassette->image_read(header, 0, sizeof(header));

	if (header[0x16] != 0x1a)
	{
		LOG_FORMATS("csw_cassette_load: Terminator Code Not Found\n");
		return cassette_image::error::INVALID_IMAGE;
	}

	LOG_FORMATS("CSW Version %d.%d\n", header[0x17], header[0x18]);
	switch (header[0x17])
	{
	case 1:
		sample_rate = little_endianize_int16(*(uint32_t*)(header + 0x19));
		compression = header[0x1b];
		bit = (header[0x1c] & 1) ? 127 : -128;
		csw_data = 0x20;

		LOG_FORMATS("Sample Rate: %u\n", sample_rate);
		LOG_FORMATS("CompressionType: %u   Flags: %u\n", header[0x1b], header[0x1c]);
		break;

	case 2:
		sample_rate = little_endianize_int32(*(uint32_t*)(header + 0x19));
		compression = header[0x21];
		bit = (header[0x22] & 1) ? 127 : -128;
		csw_data = (size_t) header[0x23] + 0x34;

		LOG_FORMATS("Sample Rate: %u\n", sample_rate);
		LOG_FORMATS("Number of Pulses: %u\n", little_endianize_int32(*(uint32_t *)(header + 0x1d)));
		LOG_FORMATS("CompressionType: %u   Flags: %u\n", header[0x21], header[0x22]);
		LOG_FORMATS("Encoder: ");
		for (int i = 0; i < 16; i++)
			LOG_FORMATS("%c", header[0x24 + i]);
		LOG_FORMATS("\n");
		break;

	default:
		LOG_FORMATS("Unsupported Major Version\n");
		return cassette_image::error::INVALID_IMAGE;
	}

	/* csw data */
	switch (compression)
	{
	case 0x01:
		/* RLE (Run Length Encoding) */
		for (size_t pos = csw_data; pos < image_size; pos++)
		{
			bsize = image_data[pos];
			if (bsize == 0)
			{
				bsize = little_endianize_int32(*(uint32_t *)(&image_data[pos + 1]));
				pos += 4;
			}
			for (int i = 0; i < bsize; i++)
			{
				samples.resize(sample_count + 1);
				samples[sample_count++] = bit;
			}
			bit ^= 0xff;
		}
		break;

	case 0x02:
		/* Z-RLE (CSW v2.xx only) */
		cassette->image_read(&image_data[0], 0, image_size);

		std::vector<uint8_t> gz_ptr;
		z_stream    d_stream;
		int         err;

		gz_ptr.resize(8);

		d_stream.next_in = (unsigned char *) &image_data[csw_data];
		d_stream.avail_in = image_size - 0x34 - header[0x23];
		d_stream.total_in = 0;

		d_stream.next_out = &gz_ptr[0];
		d_stream.avail_out = 1;
		d_stream.total_out = 0;

		d_stream.zalloc = nullptr;
		d_stream.zfree = nullptr;
		d_stream.opaque = nullptr;
		d_stream.data_type = 0;

		err = inflateInit(&d_stream);
		if (err != Z_OK)
		{
			LOG_FORMATS("inflateInit error: %d\n", err);
			return cassette_image::error::INVALID_IMAGE;
		}

		do
		{
			d_stream.next_out = &gz_ptr[0];
			d_stream.avail_out = 1;
			err = inflate(&d_stream, Z_SYNC_FLUSH);
			if (err == Z_OK)
			{
				bsize = gz_ptr[0];
				if (bsize == 0)
				{
					d_stream.avail_out = 4;
					d_stream.next_out = &gz_ptr[0];
					err = inflate(&d_stream, Z_SYNC_FLUSH);
					bsize = little_endianize_int32(*(uint32_t *)(&gz_ptr[0]));
				}
				for (int i = 0; i < bsize; i++)
				{
					samples.resize(sample_count + 1);
					samples[sample_count++] = bit;
				}
				bit ^= 0xff;
			}
		}
		while (err == Z_OK);

		if (err != Z_STREAM_END)
		{
			LOG_FORMATS("inflate error: %d\n", err);
			return cassette_image::error::INVALID_IMAGE;
		}

		err = inflateEnd(&d_stream);
		if (err != Z_OK)
		{
			LOG_FORMATS("inflateEnd error: %d\n", err);
			return cassette_image::error::INVALID_IMAGE;
		}
		break;
	}

	return cassette->put_samples(0, 0.0, (double) sample_count / sample_rate, sample_count, 1, &samples[0], cassette_image::WAVEFORM_8BIT);
}


/*-------------------------------------------------
    CassetteFormat csw_cassette_format
-------------------------------------------------*/

const cassette_image::Format csw_cassette_format = {
	"csw",
	csw_cassette_identify,
	csw_cassette_load,
	nullptr
};

CASSETTE_FORMATLIST_START(csw_cassette_formats)
	CASSETTE_FORMAT(csw_cassette_format)
CASSETTE_FORMATLIST_END

CASSETTE_FORMATLIST_START(bbc_cassette_formats)
	CASSETTE_FORMAT(csw_cassette_format)
	CASSETTE_FORMAT(uef_cassette_format)
CASSETTE_FORMATLIST_END