summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/thom_dsk.cpp
blob: da7187d65f8b8c5bb53d1fb080574f4214d54fc4 (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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// license:BSD-3-Clause
// copyright-holders:Antoine Mine
/*********************************************************************

    formats/thom_dsk.c

    Thomson disk images

    Based on work of  Antoine Mine'

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

#include <string.h>
#include <assert.h>
#include "thom_dsk.h"
#include "basicdsk.h"

static const int sap_magic_num = 0xB3; /* simple XOR crypt */


static const char sap_header[] =
	"\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. "
	"(c) Alexandre PUKALL Avril 1998";


static const UINT16 sap_crc[] =
{
	0x0000, 0x1081, 0x2102, 0x3183,   0x4204, 0x5285, 0x6306, 0x7387,
	0x8408, 0x9489, 0xa50a, 0xb58b,   0xc60c, 0xd68d, 0xe70e, 0xf78f,
};

struct sap_dsk_tag
{
	int tracks;
	int sector_size;
		int sector_pos[80][16]; /* remember sector position in file */
};

static UINT16 thom_sap_crc( UINT8* data, int size )
{
		int i;
		UINT16 crc = 0xffff, crc2;
		for ( i = 0; i < size; i++ )
		{
				crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ];
				crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ];
		}
		return crc;
}

static struct sap_dsk_tag *get_tag(floppy_image_legacy *floppy)
{
	struct sap_dsk_tag *tag;
	tag = (sap_dsk_tag *)floppy_tag(floppy);
	return tag;
}


static FLOPPY_IDENTIFY(sap_dsk_identify)
{
	char header[0x100];
	floppy_image_read(floppy, header, 0, sizeof(sap_header));
	if (!memcmp( header, sap_header, sizeof(sap_header) ) )
	{
		*vote= 100;
	} else {
		*vote = 0;
	}
	return FLOPPY_ERROR_SUCCESS;
}
static int sap_get_heads_per_disk(floppy_image_legacy *floppy)
{
	return 1;
}

static int sap_get_tracks_per_disk(floppy_image_legacy *floppy)
{
	return get_tag(floppy)->tracks;
}


static floperr_t get_offset(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset)
{
	UINT64 offs;
		struct sap_dsk_tag *tag = get_tag(floppy);
	/* translate the sector to a raw sector */
	if (!sector_is_index)
	{
		sector -= 1;
	}
	/* check to see if we are out of range */
	if ((head < 0) || (head >= 1) || (track < 0) || (track >=tag->tracks)
			|| (sector < 0) || (sector >= 16))
		return FLOPPY_ERROR_SEEKERROR;

		offs = tag->sector_pos[track][sector];
		if (offs <= 0 )
				return FLOPPY_ERROR_SEEKERROR;

	if (offset)
		*offset = offs;
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t internal_sap_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen)
{
	UINT64 offset;
	floperr_t err;
	int i;
	UINT8 *buf;
	err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
	if (err)
		return err;

	floppy_image_read(floppy, buffer, offset+4, buflen);
	buf = (UINT8*)buffer;
	for (i=0;i<buflen;i++) {
		buf[i] ^= sap_magic_num;
	}
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t internal_sap_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam)
{
	UINT64 offset;
	floperr_t err;
	UINT8 buf[256+6];
		UINT16 crc;
	int i;
	err = get_offset(floppy, head, track, sector, sector_is_index, &offset);
	if (err)
		return err;

		/* buf = 4-byte header + sector + 2-byte CRC */
	floppy_image_read(floppy, buf, offset, 4);
	for (i=0;i<buflen;i++) {
		buf[i+4] = ((UINT8*)buffer)[i];
	}
		crc = thom_sap_crc( buf, buflen+4 );
		buf[buflen+4] = crc >> 8;
		buf[buflen+5] = crc & 0xff;
	for (i=0;i<buflen;i++) {
		buf[i+4] ^= sap_magic_num;
	}
	floppy_image_write(floppy, buf, offset, buflen+6);

	return FLOPPY_ERROR_SUCCESS;
}


static floperr_t sap_read_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
	return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen);
}

static floperr_t sap_write_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
	return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam);
}

static floperr_t sap_read_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buflen)
{
	return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen);
}

static floperr_t sap_write_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam)
{
	return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam);
}

static floperr_t sap_get_sector_length(floppy_image_legacy *floppy, int head, int track, int sector, UINT32 *sector_length)
{
	floperr_t err;
	err = get_offset(floppy, head, track, sector, FALSE, nullptr);
	if (err)
		return err;

	if (sector_length) {
		*sector_length = get_tag(floppy)->sector_size;
	}
	return FLOPPY_ERROR_SUCCESS;
}



static floperr_t sap_get_indexed_sector_info(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags)
{
	floperr_t err;
	UINT8 header[4];
	UINT64 offset = 0;
	sector_index += 1;
	err = get_offset(floppy, head, track, sector_index, FALSE, &offset);

	floppy_image_read(floppy, header, offset, 4);
	if (cylinder)
		*cylinder = header[2];
	if (side)
		*side = head;
	if (sector)
		*sector = header[3];
	if (sector_length)
		*sector_length = get_tag(floppy)->sector_size;
	if (flags)
		/* TODO: read DAM or DDAM and determine flags */
		*flags = 0;
	return err;
}

static floperr_t sap_post_format(floppy_image_legacy *floppy, option_resolution *params)
{
		int track,sector;
		int pos;
		UINT8 buf[256], header[4];
	struct sap_dsk_tag *tag;
		tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));

		/* default options */
		if ( !tag->tracks )
		{
				tag->tracks = 80;
				tag->sector_size = 256;
		}

		/* create SAP file header */
		floppy_image_write( floppy, sap_header, 0, 66 );

		for ( track = 0; track < 80; track++ )
				for ( sector = 0; sector < 16; sector++ )
						tag->sector_pos[track][sector] = 0;

		/* create all sectors with valid header and CRC */
	memset(buf, 0xe5, 256);
		pos = 0x42;
		header[0] = (tag->sector_size==128) ? 1 : 0;
		header[1] = 0;
		for ( track = 0, pos = 0x42; track < tag->tracks; track++ )
				for ( sector = 0; sector < 16; sector++, pos += tag->sector_size + 6 ) {
						tag->sector_pos[track][sector] = pos;
						header[2] = track;
						header[3] = sector + 1;
						floppy_image_write(floppy, header, pos, 4);
						sap_write_indexed_sector( floppy, 0, track, sector, buf, tag->sector_size, 0 );
				}

		return FLOPPY_ERROR_SUCCESS;
}


static FLOPPY_CONSTRUCT(sap_dsk_construct)
{
	struct FloppyCallbacks *callbacks;
	struct sap_dsk_tag *tag;
	int j;
	UINT8 fmt;
	tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag));
	if (!tag)
		return FLOPPY_ERROR_OUTOFMEMORY;

		/* guess format */
	floppy_image_read(floppy, &fmt, 0x42, 1);
	if ( fmt==1 ) tag->sector_size = 128; else tag->sector_size = 256;

		/* start with an empty offset table */
		tag->tracks = 0;
		for ( int i = 0; i < 80; i++ )
				for ( j = 0; j < 16; j++ )
						tag->sector_pos[i][j] = 0;

		/* count tracks & fill sector offset table */
	for ( UINT64 i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header
	{
				UINT8 sector, track;
		floppy_image_read(floppy, &track, i+2, 1);
		floppy_image_read(floppy, &sector, i+3, 1);
				if ( track >= 80 || sector < 1 || sector > 16 ) continue;
				if ( track > tag->tracks ) tag->tracks = track+1;
				tag->sector_pos[track][sector-1] = i;
	}
	callbacks = floppy_callbacks(floppy);
	callbacks->read_sector = sap_read_sector;
	callbacks->write_sector = sap_write_sector;
	callbacks->read_indexed_sector = sap_read_indexed_sector;
	callbacks->write_indexed_sector = sap_write_indexed_sector;
	callbacks->get_sector_length = sap_get_sector_length;
	callbacks->get_heads_per_disk = sap_get_heads_per_disk;
	callbacks->get_tracks_per_disk = sap_get_tracks_per_disk;
	callbacks->get_indexed_sector_info = sap_get_indexed_sector_info;
	callbacks->post_format = sap_post_format;

	return FLOPPY_ERROR_SUCCESS;
}

static FLOPPY_IDENTIFY(qdd_dsk_identify)
{
	*vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0;
	return FLOPPY_ERROR_SUCCESS;
}

/* fixed interlacing map for QDDs */
static int thom_qdd_map[400];

static int qdd_translate_sector(floppy_image_legacy *floppy, int sector)
{
	return thom_qdd_map[sector-1];
}

static void thom_qdd_compute_map ( void )
{
	/* this map is hardcoded in the QDD BIOS */
	static const int p[6][4] =
	{
		{ 20,  2, 14,  8 }, { 21, 19, 13,  7 },
		{ 22, 18, 12,  6 }, { 23, 17, 11,  5 },
		{ 24, 16, 10,  4 }, {  1, 15,  9,  3 }
	};
	static const int q[4] = { 0, 8, 4, 12 };
	int t, s;
	for ( t = 0; t < 24; t++ )
	{
		for ( s = 0; s < 16; s++ )
		{
			thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4);
		}
	}
	for ( s = 0; s < 16; s++ )
	{
		thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4);
	}
}

static FLOPPY_CONSTRUCT(qdd_dsk_construct)
{
	struct basicdsk_geometry geometry;

	thom_qdd_compute_map();

	memset(&geometry, 0, sizeof(geometry));
	geometry.heads = 1;
	geometry.first_sector_id = 1;
	geometry.sector_length = 128;
	geometry.tracks = 1;
	geometry.sectors = 400;
	geometry.translate_sector =  qdd_translate_sector;
	return basicdsk_construct(floppy, &geometry);
}


/* ----------------------------------------------------------------------- */

LEGACY_FLOPPY_OPTIONS_START(thomson)

	LEGACY_FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image (3\"1/2 DD)", basicdsk_identify_default, basicdsk_construct_default, nullptr,
		HEADS([1])
		TRACKS([80])
		SECTORS([16])
		SECTOR_LENGTH([256])
		FIRST_SECTOR_ID([1]))

	LEGACY_FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image (5\"1/4 DD)", basicdsk_identify_default, basicdsk_construct_default, nullptr,
		HEADS([1])
		TRACKS([40])
		SECTORS([16])
		SECTOR_LENGTH([256])
		FIRST_SECTOR_ID([1]))

	LEGACY_FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image (3\"1/2 SD)", basicdsk_identify_default, basicdsk_construct_default, nullptr,
		HEADS([1])
		TRACKS([80])
		SECTORS([16])
		SECTOR_LENGTH([128])
		FIRST_SECTOR_ID([1]))

	LEGACY_FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image (5\"1/4 SD)", basicdsk_identify_default, basicdsk_construct_default, nullptr,
		HEADS([1])
		TRACKS([40])
		SECTORS([16])
		SECTOR_LENGTH([128])
		FIRST_SECTOR_ID([1]))

		LEGACY_FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image",    sap_dsk_identify, sap_dsk_construct, nullptr, nullptr)

	LEGACY_FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image (2\"8 SD)",   qdd_dsk_identify, qdd_dsk_construct, nullptr,
		HEADS([1])
		TRACKS([1])
		SECTORS([400])
		SECTOR_LENGTH([128])
		FIRST_SECTOR_ID([0]))

LEGACY_FLOPPY_OPTIONS_END