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

    formats/d64_dsk.c

    Commodore 4040/1541/1551 sector disk image format

    http://unusedino.de/ec64/technical/formats/d64.html

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

#include "emu.h" // emu_fatalerror, fatalerror
#include "formats/d64_dsk.h"

d64_format::d64_format()
{
	formats = file_formats;
}

d64_format::d64_format(const format *_formats)
{
	formats = _formats;
}

const char *d64_format::name() const
{
	return "d64";
}

const char *d64_format::description() const
{
	return "Commodore 4040/1541/1551 disk image";
}

const char *d64_format::extensions() const
{
	return "d64";
}

const d64_format::format d64_format::file_formats[] = {
	{ // d64, dos 2, 35 tracks, head 48 tpi, stepper 96 tpi
		floppy_image::FF_525, floppy_image::SSSD, 683, 35, 1, 256, 9, 8
	},
	{ // d64, dos 2, 40 tracks, head 48 tpi, stepper 96 tpi
		floppy_image::FF_525, floppy_image::SSSD, 768, 40, 1, 256, 9, 8
	},
	{ // d64, dos 2, 42 tracks, head 48 tpi, stepper 96 tpi
		floppy_image::FF_525, floppy_image::SSSD, 802, 42, 1, 256, 9, 8
	},
	{}
};

const UINT32 d64_format::cell_size[] =
{
	4000, // 16MHz/16/4
	3750, // 16MHz/15/4
	3500, // 16MHz/14/4
	3250  // 16MHz/13/4
};

const int d64_format::sectors_per_track[] =
{
	21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, //  1-17
	19, 19, 19, 19, 19, 19, 19,                                         // 18-24
	18, 18, 18, 18, 18, 18,                                             // 25-30
	17, 17, 17, 17, 17,                                                 // 31-35
	17, 17, 17, 17, 17,                                                 // 36-40
	17, 17                                                              // 41-42
};

const int d64_format::speed_zone[] =
{
	3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, //  1-17
	2, 2, 2, 2, 2, 2, 2,                               // 18-24
	1, 1, 1, 1, 1, 1,                                  // 25-30
	0, 0, 0, 0, 0,                                     // 31-35
	0, 0, 0, 0, 0,                                     // 36-40
	0, 0                                               // 41-42
};

int d64_format::find_size(io_generic *io, UINT32 form_factor)
{
	UINT64 size = io_generic_size(io);
	for(int i=0; formats[i].sector_count; i++) {
		const format &f = formats[i];
		if(size == (UINT32) f.sector_count*f.sector_base_size*f.head_count)
			return i;
		if(size == (UINT32) (f.sector_count*f.sector_base_size*f.head_count) + f.sector_count)
			return i;
	}
	return -1;
}

int d64_format::identify(io_generic *io, UINT32 form_factor)
{
	int type = find_size(io, form_factor);

	if (type != -1)
		return 50;

	return 0;
}

int d64_format::get_physical_track(const format &f, int head, int track)
{
	// skip halftracks
	return track * 2;
}

int d64_format::get_disk_id_offset(const format &f)
{
	// t18s0 +0xa2
	return 0x165a2;
}

void d64_format::get_disk_id(const format &f, io_generic *io, UINT8 &id1, UINT8 &id2)
{
	UINT8 id[2];
	io_generic_read(io, id, get_disk_id_offset(f), 2);
	id1 = id[0];
	id2 = id[1];
}

UINT32 d64_format::get_cell_size(const format &f, int track)
{
	return cell_size[speed_zone[track]];
}

int d64_format::get_sectors_per_track(const format &f, int track)
{
	return sectors_per_track[track];
}

floppy_image_format_t::desc_e* d64_format::get_sector_desc(const format &f, int &current_size, int sector_count, UINT8 id1, UINT8 id2, int gap_2)
{
	static floppy_image_format_t::desc_e desc[] = {
		/* 00 */ { SECTOR_LOOP_START, 0, -1 },
		/* 01 */ {   RAWBYTE, 0xff, 5 },
		/* 02 */ {   GCR5, 0x08, 1 },
		/* 03 */ {   CRC, 1 },
		/* 04 */ {   CRC_CBM_START, 1 },
		/* 05 */ {     SECTOR_ID_GCR5 },
		/* 06 */ {     TRACK_ID_DOS2_GCR5 },
		/* 07 */ {     GCR5, id2, 1 },
		/* 08 */ {     GCR5, id1, 1 },
		/* 09 */ {   CRC_END, 1 },
		/* 10 */ {   GCR5, 0x0f, 2 },
		/* 11 */ {   RAWBYTE, 0x55, f.gap_1 },
		/* 12 */ {   RAWBYTE, 0xff, 5 },
		/* 13 */ {   GCR5, 0x07, 1 },
		/* 14 */ {   CRC_CBM_START, 2 },
		/* 15 */ {     SECTOR_DATA_GCR5, -1 },
		/* 16 */ {   CRC_END, 2 },
		/* 17 */ {   CRC, 2 },
		/* 18 */ {   GCR5, 0x00, 2 },
		/* 19 */ {   RAWBYTE, 0x55, gap_2 },
		/* 20 */ { SECTOR_LOOP_END },
		/* 21 */ { RAWBYTE, 0x55, 0 },
		/* 22 */ { RAWBITS, 0x5555, 0 },
		/* 23 */ { END }
	};

	current_size = 40 + (1+1+4+2)*10 + (f.gap_1)*8 + 40 + (1+f.sector_base_size+1+2)*10 + gap_2*8;

	current_size *= sector_count;
	return desc;
}

void d64_format::build_sector_description(const format &f, UINT8 *sectdata, UINT32 sect_offs, UINT32 error_offs, desc_s *sectors, int sector_count) const
{
	for (int i = 0; i < sector_count; i++) {
		sectors[i].data = sectdata + sect_offs;
		sectors[i].size = f.sector_base_size;
		sectors[i].sector_id = i;
		sectors[i].sector_info = sectdata[error_offs];

		sect_offs += sectors[i].size;
		error_offs++;
	}
}

void d64_format::fix_end_gap(floppy_image_format_t::desc_e* desc, int remaining_size)
{
	desc[21].p2 = remaining_size / 8;
	desc[22].p2 = remaining_size & 7;
	desc[22].p1 >>= remaining_size & 0x01;
}

bool d64_format::load(io_generic *io, UINT32 form_factor, floppy_image *image)
{
	int type = find_size(io, form_factor);
	if(type == -1)
		return false;

	const format &f = formats[type];

	UINT64 size = io_generic_size(io);
	dynamic_buffer img;

	if(size == (UINT32)f.sector_count*f.sector_base_size) {
		img.resize(size + f.sector_count);
		memset(&img[0], ERROR_00, size + f.sector_count);
	}
	else {
		img.resize(size);
	}

	io_generic_read(io, &img[0], 0, size);

	int track_offset = 0, error_offset = f.sector_count*f.sector_base_size;

	UINT8 id1 = 0, id2 = 0;
	get_disk_id(f, io, id1, id2);

	for (int head = 0; head < f.head_count; head++) {
		for (int track = 0; track < f.track_count; track++) {
			int current_size = 0;
			int total_size = 200000000./this->get_cell_size(f, track);
			int physical_track = this->get_physical_track(f, head, track);
			int sector_count = this->get_sectors_per_track(f, track);
			int track_size = sector_count*f.sector_base_size;

			floppy_image_format_t::desc_e *desc = this->get_sector_desc(f, current_size, sector_count, id1, id2, f.gap_2);

			int remaining_size = total_size - current_size;
			if(remaining_size < 0)
				throw emu_fatalerror("d64_format: Incorrect track layout, max_size=%d, current_size=%d", total_size, current_size);

			this->fix_end_gap(desc, remaining_size);

			desc_s sectors[40];

			build_sector_description(f, &img[0], track_offset, error_offset, sectors, sector_count);
			generate_track(desc, physical_track, head, sectors, sector_count, total_size, image);

			track_offset += track_size;
			error_offset += sector_count;
		}
	}

	image->set_variant(f.variant);

	return true;
}

bool d64_format::supports_save() const
{
	return false;
}

const floppy_format_type FLOPPY_D64_FORMAT = &floppy_image_format_creator<d64_format>;