summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/x07_cas.cpp
blob: 131693f2da159115971e5be9e54ff5028ed4bc05 (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
// license:BSD-3-Clause
// copyright-holders:Sandro Ronco
/********************************************************************

    Support for Canon X-07 cassette images

********************************************************************/

#include <cassert>

#include "x07_cas.h"

#define WAVEENTRY_LOW  -32768
#define WAVEENTRY_HIGH  32767

#define X07_WAV_FREQUENCY   4800
#define X07_TIMER_FREQUENCY 1200
#define X07_BIT_LENGTH      (X07_WAV_FREQUENCY/X07_TIMER_FREQUENCY)
#define X07_HEADER_BYTES    16

// image size
static int x07_image_size;

static int x07_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 x07_output_bit(int16_t *buffer, int sample_pos, uint8_t bit)
{
	int samples = 0;

	if (bit)
	{
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/4, WAVEENTRY_HIGH);
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/4, WAVEENTRY_LOW);
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/4, WAVEENTRY_HIGH);
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/4, WAVEENTRY_LOW);
	}
	else
	{
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/2, WAVEENTRY_HIGH);
		samples += x07_put_samples(buffer, sample_pos + samples, X07_BIT_LENGTH/2, WAVEENTRY_LOW);
	}

	return samples;
}

static int x07_output_byte(int16_t *buffer, int sample_pos, uint8_t byte)
{
	int samples = 0;

	/* start */
	samples += x07_output_bit (buffer, sample_pos + samples, 0);

	/* data */
	for (int i=0; i<8; i++)
		samples += x07_output_bit(buffer,sample_pos + samples, (byte>>i) & 0x01);

	/* stop */
	samples += x07_output_bit (buffer, sample_pos + samples, 1);
	samples += x07_output_bit (buffer, sample_pos + samples, 1);
	samples += x07_output_bit (buffer, sample_pos + samples, 1);

	return samples;
}

static int x07_handle_cassette(int16_t *buffer, const uint8_t *bytes)
{
	int sample_count = 0;
	int img_start = 0;

	/* start */
	for (int i=0; i<X07_WAV_FREQUENCY; i++)
		sample_count += x07_output_bit(buffer, sample_count, 1);

	/* header */
	if (bytes[0] == 0xd3 && bytes[1] == 0xd3 && bytes[2] == 0xd3 && bytes[3] == 0xd3)
	{
		// valid header
		for (int i=0; i<X07_HEADER_BYTES; i++)
			sample_count += x07_output_byte(buffer, sample_count, bytes[i]);

		img_start = X07_HEADER_BYTES;
	}
	else
	{
		// remove the nullptr chars at start
		while (!bytes[img_start])
			img_start++;

		// insert 0xd3 bytes
		for (int i=0; i<10; i++)
			sample_count += x07_output_byte(buffer, sample_count, 0xd3);

		// fake filename
		for (int i=0; i<6; i++)
			sample_count += x07_output_byte(buffer, sample_count, 'A');
	}

	/* pause */
	for (int i=0; i<X07_WAV_FREQUENCY/16; i++)
		sample_count += x07_output_bit(buffer, sample_count, 1);

	/* data */
	for (int i=img_start; i<x07_image_size; i++)
		sample_count += x07_output_byte(buffer, sample_count, bytes[i]);

	/* end */
	for (int i=0; i<X07_WAV_FREQUENCY/8; i++)
		sample_count += x07_output_bit(buffer, sample_count, 1);

	return sample_count;
}


/*******************************************************************
   Generate samples for the tape image
********************************************************************/

static int x07_cassette_fill_wave(int16_t *buffer, int length, uint8_t *bytes)
{
	return x07_handle_cassette(buffer, bytes);
}

/*******************************************************************
   Calculate the number of samples needed for this tape image
********************************************************************/

static int x07_cassette_calculate_size_in_samples(const uint8_t *bytes, int length)
{
	x07_image_size = length;

	return x07_handle_cassette(nullptr, bytes);
}

static const struct CassetteLegacyWaveFiller x07_legacy_fill_wave =
{
	x07_cassette_fill_wave,                 /* fill_wave */
	-1,                                     /* chunk_size */
	0,                                      /* chunk_samples */
	x07_cassette_calculate_size_in_samples, /* chunk_sample_calc */
	X07_WAV_FREQUENCY,                      /* sample_frequency */
	0,                                      /* header_samples */
	0                                       /* trailer_samples */
};

static cassette_image::error x07_cassette_identify(cassette_image *cassette, struct CassetteOptions *opts)
{
	return cassette_legacy_identify(cassette, opts, &x07_legacy_fill_wave);
}

static cassette_image::error x07_cassette_load(cassette_image *cassette)
{
	return cassette_legacy_construct(cassette, &x07_legacy_fill_wave);
}

static const struct CassetteFormat x07_cassette_image_format =
{
	"k7,lst,cas",
	x07_cassette_identify,
	x07_cassette_load,
	nullptr
};

CASSETTE_FORMATLIST_START(x07_cassette_formats)
	CASSETTE_FORMAT(x07_cassette_image_format)
CASSETTE_FORMATLIST_END
mame0242commit e8166b5274... Vas Crabb3 years mame0241commit 31f001e501... Vas Crabb3 years mame0240commit f0ab44fe1c... Vas Crabb3 years mame0239commit 80bcaea1ed... Vas Crabb3 years mame0238commit fb21b78904... Vas Crabb3 years mame0237commit 34d8357465... Vas Crabb4 years mame0236commit 5e865af540... Vas Crabb4 years mame0235commit ec9ba6fa76... Vas Crabb4 years mame0234commit 2633c19a68... Vas Crabb4 years mame0233commit 05d0cf61e7... Vas Crabb4 years mame0232commit 2b0f01bc3a... Vas Crabb4 years mame0231commit 1f22113661... Vas Crabb4 years mame0230commit 943c06cba0... Vas Crabb4 years mame0229commit 4322eaae9d... Vas Crabb4 years mame0228commit 140f446933... Vas Crabb4 years mame0227commit d85735634c... Vas Crabb4 years mame0226commit 3c56452b07... Vas Crabb5 years mame0225commit 5a1fd0cc17... Vas Crabb5 years mame0224commit 5892c78a15... Vas Crabb5 years mame0223commit c55a261d26... Vas Crabb5 years mame0222commit 6d50d60a43... Vas Crabb5 years mame0221commit e8a0e0469b... Vas Crabb5 years mame0220commit c5c5723b9d... Vas Crabb5 years mame0219commit 221f006442... Vas Crabb5 years mame0218commit 0e2a252d30... Vas Crabb5 years mame0217commit 13997a8f31... Vas Crabb5 years mame0216commit b8b7c7e232... Vas Crabb5 years mame0215commit e9ef4808dd... Vas Crabb6 years mame0214commit 24d07a12d7... Vas Crabb6 years mame0213commit f7172322a2... Vas Crabb6 years mame0212commit 1182bd9325... Vas Crabb6 years mame0211commit 1b969a8acb... Vas Crabb6 years mame0210commit ad45c9c609... Vas Crabb6 years mame0209commit 2b317bf296... Vas Crabb6 years mame0208commit 9483624864...