summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/formats/fdi_dsk.cpp
blob: c703f29cc69248e15ab708f4b1a277d59c7fed8b (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
// license:BSD-3-Clause
// copyright-holders:Curt Coder
/*********************************************************************

    formats/fdi_dsk.h

    Floppy format code for Formatted Disk Image (FDI) v2.0 images

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

/*

    TODO:

    - everything

*/

#include <assert.h>
#include "imageutl.h"
#include "flopimg.h"

/***************************************************************************
    PARAMETERS
***************************************************************************/

#define LOG 1

#define MAX_TRACKS  180

#define FLAGS_WRITE_PROTECTED   0x01
#define FLAGS_INDEX_SYNC        0x02

/* media type */
enum
{
	TYPE_8_INCH = 0,
	TYPE_5_25_INCH,
	TYPE_3_5_INCH,
	TYPE_3_INCH
};

static const char *const MEDIA_TYPE[] = { "8", "5.25", "3.5", "3" };

/* tracks per inch */
enum
{
	TPI_48 = 0,
	TPI_67,
	TPI_96,
	TPI_100,
	TPI_135,
	TPI_192
};

static const int TRACKS_PER_INCH[] = { 48, 67, 96, 100, 135, 192 };

/* high level track types */
enum
{
	TRACK_BLANK = 0,    /* not supported */
	TRACK_AMIGA_DD,     /* not supported */
	TRACK_AMIGA_HD,     /* not supported */
	TRACK_ST_DD_9,      /* not supported */
	TRACK_ST_DD_10,     /* not supported */
	TRACK_PC_DD_8,      /* not supported */
	TRACK_PC_DD_9,      /* not supported */
	TRACK_PC_HD_15,     /* not supported */
	TRACK_IBM_HD_18,    /* not supported */
	TRACK_IBM_ED_36,    /* not supported */
	TRACK_CBM_1541,     /* not supported */
	TRACK_APPLE_DOS_32, /* not supported */
	TRACK_APPLE_DOS_33, /* not supported */
	TRACK_APPLE_GCR,    /* not supported */
	TRACK_IBM_SD_10     /* not supported */
};

//static const int SECTORS_PER_TRACK[] = { 0, 11, 22, 9, 10, 8, 9, 15, 18, 36, -1, -1, -1, -1, 10 };

/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

struct fdidsk_tag
{
	int version;
	int heads;                              /* number of physical heads */
	int tracks;                             /* number of physical tracks */
	uint32_t track_offset[MAX_TRACKS];        /* offset within data for each track */
	uint8_t track_type[MAX_TRACKS];
};

struct fdi_track
{
	uint8_t type;
	uint8_t size;
};

struct fdi_header
{
	uint8_t signature[27];
	uint8_t creator[32];
	uint8_t comment[80];
	uint8_t eof;
	uint8_t version[2];
	uint8_t ltrack[2];
	uint8_t lhead;
	uint8_t type;
	uint8_t rotspeed;
	uint8_t flags;
	uint8_t tpi;
	uint8_t headwidth;
	uint8_t reserved[2];
	struct fdi_track track[MAX_TRACKS];
};

struct fdi_pulse_track_header
{
	uint8_t numpulses[4];
	uint8_t averagesz[3];
	uint8_t minsize[3];
	uint8_t maxsize[3];
	uint8_t indexsize[3];
//  uint8_t averagedt[averagesz];
//  uint8_t mindata[minsize];
//  uint8_t maxdata[maxsize];
//  uint8_t indexdata[indexsize];
};

/***************************************************************************
    INLINE FUNCTIONS
***************************************************************************/

static inline struct fdidsk_tag *get_tag(floppy_image_legacy *floppy)
{
	return (fdidsk_tag *)floppy_tag(floppy);
}

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

static int fdi_get_heads_per_disk(floppy_image_legacy *floppy)
{
	return get_tag(floppy)->heads;
}

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

static int fdi_get_sectors_per_track(floppy_image_legacy *floppy, int head, int track)
{
	return 0;
}

static floperr_t fdi_get_sector_length(floppy_image_legacy *floppy, int head, int track, int sector, uint32_t *sector_length)
{
	return FLOPPY_ERROR_SUCCESS;
}
/*
static uint32_t fdi_get_sector_offset(floppy_image_legacy* floppy, int head, int track, int sector)
{
    return 0;
}
*/
static floperr_t fdi_get_indexed_sector_info(floppy_image_legacy *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, uint32_t *sector_length, unsigned long *flags)
{
	return FLOPPY_ERROR_UNSUPPORTED;
}

static floperr_t fdi_read_track(floppy_image_legacy *floppy, int head, int track, uint64_t offset, void *buffer, size_t buflen)
{
	return FLOPPY_ERROR_UNSUPPORTED;
}

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

static floperr_t fdi_read_indexed_sector(floppy_image_legacy *floppy, int head, int track, int sector, void *buffer, size_t buffer_len)
{
	return FLOPPY_ERROR_UNSUPPORTED;
}

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

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

/*-------------------------------------------------
    FLOPPY_IDENTIFY( fdi_dsk_identify )
-------------------------------------------------*/

FLOPPY_IDENTIFY( fdi_dsk_identify )
{
	uint8_t header[25];

	floppy_image_read(floppy, header, 0, 25);

	if (!strncmp((const char *)header, "Formatted Disk Image file", 25))
	{
		*vote = 100;
	}
	else
	{
		*vote = 0;
	}

	return FLOPPY_ERROR_SUCCESS;
}

/*-------------------------------------------------
    FLOPPY_CONSTRUCT( fdi_dsk_construct )
-------------------------------------------------*/

FLOPPY_CONSTRUCT( fdi_dsk_construct )
{
	if (params)
	{
		/* create not supported */
		return FLOPPY_ERROR_UNSUPPORTED;
	}

	struct fdidsk_tag *tag = (struct fdidsk_tag *) floppy_create_tag(floppy, sizeof(struct fdidsk_tag));

	if (!tag) return FLOPPY_ERROR_OUTOFMEMORY;

	/* read header */
	struct fdi_header header;
	floppy_image_read(floppy, &header, 0, sizeof(header));

	tag->version = header.version[0];
	tag->tracks = pick_integer_be(header.ltrack, 0, 2) + 1;
	tag->heads = header.lhead + 1;

	if (LOG)
	{
		LOG_FORMATS("FDI creator: %s\n", header.creator);
		LOG_FORMATS("FDI comment: %s\n", header.comment);
		LOG_FORMATS("FDI version: %u\n", tag->version);
		LOG_FORMATS("FDI tracks: %u\n", tag->tracks);
		LOG_FORMATS("FDI heads: %u\n", tag->heads);
		LOG_FORMATS("FDI media type: %s\"\n", MEDIA_TYPE[header.type]);
		LOG_FORMATS("FDI rotation speed: %u\n", header.rotspeed + 128);
		if (header.flags & FLAGS_WRITE_PROTECTED) LOG_FORMATS("FDI is write protected\n");
		if (header.flags & FLAGS_INDEX_SYNC) LOG_FORMATS("FDI is index synchronized\n");
		LOG_FORMATS("FDI media TPI: %u\n", TRACKS_PER_INCH[header.tpi]);
		LOG_FORMATS("FDI head TPI: %u\n", TRACKS_PER_INCH[header.headwidth]);
	}

	/* find track offsets */
	int offset = sizeof(header);

	for (int track = 0; track < tag->tracks; track++)
	{
		uint8_t type = header.track[track].type;
		int size = header.track[track].size * 256;

		if (LOG) LOG_FORMATS("FDI track %d type %02x size %d offset %d\n", track, type, size, offset);

		tag->track_offset[track] = offset;
		tag->track_type[track] = type;

		offset += size;
	}

	/* set callbacks */
	struct FloppyCallbacks *callbacks = floppy_callbacks(floppy);

	callbacks->read_track = fdi_read_track;
	callbacks->get_heads_per_disk = fdi_get_heads_per_disk;
	callbacks->get_tracks_per_disk = fdi_get_tracks_per_disk;
	callbacks->get_sector_length = fdi_get_sector_length;
	callbacks->read_sector = fdi_read_sector;
	callbacks->read_indexed_sector = fdi_read_indexed_sector;
//  callbacks->write_track = fdi_write_track;
	callbacks->write_sector = fdi_write_sector;
	callbacks->write_indexed_sector = fdi_write_indexed_sector;
	callbacks->get_indexed_sector_info = fdi_get_indexed_sector_info;
	callbacks->get_sectors_per_track = fdi_get_sectors_per_track;

	return FLOPPY_ERROR_SUCCESS;
}