summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/fm7_cas.cpp
blob: 32cd08e3b27676e3fb81b2d6dc36987c3d84f1e9 (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
// license:BSD-3-Clause
// copyright-holders:Barry Rodewald
/*
 * Fujitsu FM-7 series cassette handling
 */

#include <assert.h>

#include "fm7_cas.h"

#define WAVE_HIGH        0x5a9e
#define WAVE_LOW        -0x5a9e

static int cas_size;

static int fm7_fill_wave(int16_t* buffer, uint8_t high, uint8_t low, int sample_pos)
{
	uint16_t data = (high << 8) + low;
	int sample_count = 0;
	int x = 0;
	int count = (data & 0x7fff);

	if(data & 0x8000)
	{
		for(x=0;x<count;x++)
		{
			if(buffer)
				buffer[sample_pos+x] = WAVE_HIGH;
		}
	}
	else
	{
		for(x=0;x<count;x++)
		{
			if(buffer)
				buffer[sample_pos+x] = WAVE_LOW;
		}
	}

	sample_count += count;
	return sample_count;
}

static int fm7_handle_t77(int16_t* buffer, const uint8_t* casdata)
{
	int sample_count = 0;
	int data_pos = 16;

	if(memcmp(casdata, "XM7 TAPE IMAGE 0",16))  // header check
		return -1;

	while(data_pos < cas_size)
	{
		sample_count += fm7_fill_wave(buffer,casdata[data_pos],casdata[data_pos+1],sample_count);
		data_pos+=2;
	}

	return sample_count;
}

/*******************************************************************
   Calculate the number of samples needed for this tape image
********************************************************************/
static int fm7_cas_to_wav_size (const uint8_t *casdata, int caslen)
{
	cas_size = caslen;

	return fm7_handle_t77(nullptr,casdata);
}

/*******************************************************************
   Generate samples for the tape image
********************************************************************/
static int fm7_cas_fill_wave(int16_t *buffer, int sample_count, uint8_t *bytes)
{
	return fm7_handle_t77(buffer,bytes);
}

static const struct CassetteLegacyWaveFiller fm7_legacy_fill_wave =
{
	fm7_cas_fill_wave,                      /* fill_wave */
	-1,                                     /* chunk_size */
	0,                                      /* chunk_samples */
	fm7_cas_to_wav_size,                    /* chunk_sample_calc */
	110250,                                 /* sample_frequency */
	0,                                      /* header_samples */
	0                                       /* trailer_samples */
};

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



static cassette_image::error fm7_cas_load(cassette_image *cassette)
{
	return cassette_legacy_construct(cassette, &fm7_legacy_fill_wave);
}


static const struct CassetteFormat fm7_cassette_format = {
	"t77",
	fm7_cas_identify,
	fm7_cas_load,
	nullptr
};

CASSETTE_FORMATLIST_START(fm7_cassette_formats)
	CASSETTE_FORMAT(fm7_cassette_format)
CASSETTE_FORMATLIST_END