summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/imagedev/floppy.c
blob: 258a6e9506f2ff54024ec6138c62f2d1d6d55797 (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
/*********************************************************************



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

#include "emu.h"
#include "floppy.h"
#include "formats/imageutl.h"

// device type definition
const device_type FLOPPY = &device_creator<floppy_image_device>;

//-------------------------------------------------
//  floppy_image_device - constructor
//-------------------------------------------------

floppy_image_device::floppy_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
    : device_t(mconfig, FLOPPY, "Floppy drive", tag, owner, clock),
	  device_image_interface(mconfig, *this),
	  image(NULL)
{
}

//-------------------------------------------------
//  floppy_image_device - destructor
//-------------------------------------------------

floppy_image_device::~floppy_image_device()
{
}

void floppy_image_device::setup_load_cb(load_cb cb)
{
	cur_load_cb = cb;
}

void floppy_image_device::setup_unload_cb(unload_cb cb)
{
	cur_unload_cb = cb;
}

void floppy_image_device::setup_index_pulse_cb(index_pulse_cb cb)
{
	cur_index_pulse_cb = cb;
}

void floppy_image_device::set_info(int _type, int _tracks, int _sides, const floppy_format_type *formats)
{
	type = _type;
	tracks = _tracks;
	sides = _sides;

	image_device_format **formatptr;
    image_device_format *format;
    formatptr = &m_formatlist;
	extension_list[0] = '\0';
	fif_list = 0;
	for(int cnt=0; formats[cnt]; cnt++)
	{
		// allocate a new format
		floppy_image_format_t *fif = formats[cnt]();
		if(!fif_list)
			fif_list = fif;
		else
			fif_list->append(fif);

		format = global_alloc_clear(image_device_format);
		format->m_index       = cnt;
		format->m_name        = fif->name();
		format->m_description = fif->description();
		format->m_extensions  = fif->extensions();
		format->m_optspec     = "";

		image_specify_extension( extension_list, 256, fif->extensions() );
		// and append it to the list
		*formatptr = format;
		formatptr = &format->m_next;
	}

	// set brief and instance name
	update_names();
}

void floppy_image_device::device_config_complete()
{
	update_names();
}

void floppy_image_device::set_rpm(float _rpm)
{
	if(rpm == _rpm)
		return;

	rpm = _rpm;
	rev_time = attotime::from_double(60/rpm);
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void floppy_image_device::device_start()
{
	idx = 0;

	/* motor off */
	mon = 1;
	/* set write protect on */
	wpt = 0;

	rpm = 0;
	set_rpm(300);

	cyl = 0;
	ss  = 1;
	stp = 1;
	dskchg = 0;
	index_timer = timer_alloc(0);
}

void floppy_image_device::device_reset()
{
	revolution_start_time = attotime::never;
	revolution_count = 0;
	mon = 1;
}

void floppy_image_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	index_resync();
}

bool floppy_image_device::call_load()
{
	io_generic io;
	// Do _not_ remove this cast otherwise the pointer will be incorrect when used by the ioprocs.
	io.file = (device_image_interface *)this;
	io.procs = &image_ioprocs;
	io.filler = 0xff;

	int best = 0;
	floppy_image_format_t *best_format = 0;
	for(floppy_image_format_t *format = fif_list; format; format = format->next) {
		int score = format->identify(&io);
		if(score > best) {
			best = score;
			best_format = format;
		}
	}

	if(!best_format)
		return IMAGE_INIT_FAIL;

	image = global_alloc(floppy_image(tracks, sides));
	best_format->load(&io, image);

	revolution_start_time = attotime::never;
	revolution_count = 0;

	index_resync();

	if (!cur_load_cb.isnull())
		return cur_load_cb(this);
	return IMAGE_INIT_PASS;
}

void floppy_image_device::call_unload()
{
	dskchg = 0;

	if (image) {
		global_free(image);
		image = 0;
	}
	if (!cur_unload_cb.isnull())
		cur_unload_cb(this);
}

/* motor on, active low */
void floppy_image_device::mon_w(int state)
{
	if(mon == state)
		return;

	mon = state;

	/* off -> on */
	if (!mon && image)
	{
		revolution_start_time = machine().time();
		index_resync();
	}

	/* on -> off */
	else {
		revolution_start_time = attotime::never;
		index_timer->adjust(attotime::zero);
	}
}

attotime floppy_image_device::time_next_index()
{
	if(revolution_start_time.is_never())
		return attotime::never;
	return revolution_start_time + attotime::from_double(60/rpm);
}

/* index pulses at rpm/60 Hz, and stays high for ~2ms at 300rpm */
void floppy_image_device::index_resync()
{
	if(revolution_start_time.is_never()) {
		if(idx) {
			idx = 0;
			if (!cur_index_pulse_cb.isnull())
				cur_index_pulse_cb(this, idx);
		}
		return;
	}

	attotime delta = machine().time() - revolution_start_time;
	while(delta >= rev_time) {
		delta -= rev_time;
		revolution_start_time += rev_time;
		revolution_count++;
	}
	int position = (delta*(rpm/300)).as_ticks(1000000000);

	int new_idx = position <= 20000;

	if(new_idx) {
		attotime index_up_time = attotime::from_nsec(2000000*300.0/rpm+0.5);
		index_timer->adjust(index_up_time - delta);
	} else
		index_timer->adjust(rev_time - delta);

	if(new_idx != idx) {
		idx = new_idx;
		if (!cur_index_pulse_cb.isnull())
			cur_index_pulse_cb(this, idx);
	}
}

int floppy_image_device::ready_r()
{
	if (exists())
	{
		if (mon == 0)
		{
			return 0;
		}
	}
	return 1;
}

double floppy_image_device::get_pos()
{
	return index_timer->elapsed().as_double();
}

void floppy_image_device::stp_w(int state)
{
    if ( stp != state ) {
		stp = state;
    	if ( stp == 0 ) {
			if ( dir ) {
				if ( cyl ) cyl--;
			} else {
				if ( cyl < tracks-1 ) cyl++;
			}
			/* Update disk detection if applicable */
			if (exists())
			{
				if (dskchg==0) dskchg = 1;
			}
		}
	}
}

int floppy_image_device::find_position(int position, const UINT32 *buf, int buf_size)
{
	int spos = (buf_size >> 1)-1;
	int step;
	for(step=1; step<buf_size+1; step<<=1);
	step >>= 1;

	for(;;) {
#if 0
		fprintf(stderr, "%09d - %09d / %09d  -- %6d %6d %6d\n",
			   position,
			   spos < 0 ? 0 : spos >= buf_size ? 200000000 :buf[spos] & floppy_image::TIME_MASK,
				spos < 0 ? 0 : spos >= buf_size-1 ? 200000000 : buf[spos+1] & floppy_image::TIME_MASK,
				spos, step, buf_size);
#endif

		if(spos >= buf_size || (spos > 0 && (buf[spos] & floppy_image::TIME_MASK) > position)) {
			spos -= step;
			step >>= 1;
		} else if(spos < 0 || (spos < buf_size-1 && (buf[spos+1] & floppy_image::TIME_MASK) <= position)) {
			spos += step;
			step >>= 1;
		} else
			return spos;
	}
}

attotime floppy_image_device::get_next_transition(attotime from_when)
{
	if(!image || mon)
		return attotime::never;

	int cells = image->get_track_size(cyl, ss);
	if(cells <= 1)
		return attotime::never;

	attotime base = revolution_start_time;
	UINT32 revc = revolution_count;
	attotime delta = from_when - base;

	while(delta >= rev_time) {
		delta -= rev_time;
		base += rev_time;
		revc++;
	}
	int position = (delta*(rpm/300)).as_ticks(1000000000);

	const UINT32 *buf = image->get_buffer(cyl, ss);
	int index = find_position(position, buf, cells);

	//	fprintf(stderr, "position=%9d, index=%d\n", position, index);
	if(index == -1)
		return attotime::never;

	int next_position;
	if(index < cells-1)
		next_position = buf[index+1] & floppy_image::TIME_MASK;
	else if((buf[index]^buf[0]) & floppy_image::MG_MASK)
		next_position = 200000000;
	else
		next_position = 200000000 + (buf[1] & floppy_image::TIME_MASK);

	//	printf("next_pos=%d, delta=%d\n", next_position, next_position-position);
	return base + attotime::from_nsec(next_position*(300/rpm));
}