summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/ss50/dc5.cpp
blob: 2522733d4e4808ace0ee966c01cbb4716e10adbc (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// license:BSD-3-Clause
// copyright-holders:68bit
/**********************************************************************

    SWTPC SS30 DC5 Floppy Disk Controller

    See: http://www.swtpc.com/mholley/DC_5/DC5_Index.htm

    The DC5 features an extra control register compared with the DC4. This
    extra register can be enabled or disabled via a jumper setting. This
    register is write only, and reading back gives a version number of 0x02.

    This register can be useful for setting the clock rate, and particular
    clock rates are needed for difference floppy disk sizes and formats. The
    clock rates are control by bits bits 2 and 3 as follows: 0x00 gives 1MHz,
    0x04 gives 1.2MHz, 0x80 gives 2MHz, and 0xC0 gives 2.4MHz.

    This register also controls the single versus double density format
    selection, via bit 5, so 0x00 gives double density and 0x20 gives single
    density.

    Software is typically not written for this DC5 extended control register,
    but typically does not touch it. Thus is can be practical to work with a
    purely single or double density disk by setting that format in this
    control register. Most virtual disks appear to fit this pattern, rather
    than mixing single and double density in the one disk.

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

#include "emu.h"
#include "dc5.h"
#include "machine/wd_fdc.h"
#include "imagedev/floppy.h"
#include "formats/flex_dsk.h"

class ss50_dc5_device : public device_t, public ss50_card_interface
{
public:
	ss50_dc5_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
		: device_t(mconfig, SS50_DC5, tag, owner, clock)
		, ss50_card_interface(mconfig, *this)
		, m_fdc(*this, "fdc")
		, m_floppy0(*this, "fdc:0")
		, m_floppy1(*this, "fdc:1")
		, m_floppy2(*this, "fdc:2")
		, m_floppy3(*this, "fdc:3")
		, m_interrupt_select(*this, "interrupt_select")
		, m_address_mode(*this, "address_mode")
		, m_two_control_regs(*this, "two_control_regs")
		, m_ctrl_reg_bit7_side_select(*this, "ctrl_reg_bit7_side_select")
		, m_expected_clock(*this, "expected_clock")
		, m_expected_density(*this, "expected_density")
		, m_expected_sectors(*this, "expected_sectors")
		, m_track_zero_expected_sectors(*this, "track_zero_expected_sectors")
	{
	}

protected:
	virtual ioport_constructor device_input_ports() const override;
	virtual void device_add_mconfig(machine_config &config) override;
	virtual void device_start() override;
	virtual void device_reset() override;

	virtual uint8_t register_read(offs_t offset) override;
	virtual void register_write(offs_t offset, uint8_t data) override;

private:
	DECLARE_WRITE_LINE_MEMBER( fdc_intrq_w );
	DECLARE_WRITE_LINE_MEMBER( fdc_drq_w );
	DECLARE_WRITE_LINE_MEMBER( fdc_sso_w );

	DECLARE_FLOPPY_FORMATS(floppy_formats);
	uint8_t m_fdc_status;              // for floppy controller
	uint8_t m_control_register;
	uint8_t m_motor_timer_out;
	emu_timer *m_floppy_motor_timer;
	floppy_image_device *m_fdc_selected_floppy; // Current selected floppy.
	uint8_t m_fdc_side;                // Current floppy side.
	uint32_t m_fdc_clock;		   // Current clock frequency.

	TIMER_CALLBACK_MEMBER(floppy_motor_callback);

	void floppy_motor_trigger();
	void control_register_update();
	void validate_floppy_side(uint8_t cmd);
	uint8_t validate_fdc_dden(uint8_t dden);
	uint8_t validate_fdc_sso(uint8_t cmd);
	uint8_t validate_fdc_sector_size(uint8_t cmd);

	required_device<wd2797_device> m_fdc;
	required_device<floppy_connector> m_floppy0;
	required_device<floppy_connector> m_floppy1;
	required_device<floppy_connector> m_floppy2;
	required_device<floppy_connector> m_floppy3;
	required_ioport m_interrupt_select;
	required_ioport m_address_mode;
	required_ioport m_two_control_regs;
	required_ioport m_ctrl_reg_bit7_side_select;
	required_ioport m_expected_clock;
	required_ioport m_expected_density;
	required_ioport m_expected_sectors;
	required_ioport m_track_zero_expected_sectors;
};


static INPUT_PORTS_START( dc5 )
	PORT_START("interrupt_select")
	PORT_DIPNAME(0xf, 0, "Interrupt select")
	PORT_DIPSETTING(0, "N/C")
	PORT_DIPSETTING(1, "IRQ")
	PORT_DIPSETTING(2, "NMI/FIRQ")

	PORT_START("address_mode")
	PORT_DIPNAME(0xf, 1, "Address mode")
	PORT_DIPSETTING(0, "4 address")
	PORT_DIPSETTING(1, "16 address")

	PORT_START("two_control_regs")
	PORT_DIPNAME(0xf, 0, "Two control registers")
	PORT_DIPSETTING(0, "No, DC4 compatible")
	PORT_DIPSETTING(1, "Yes, DC5 extension")

	// This config setting allows checking of the FDC clock rate and
	// overriding it to assist driver development. The DC5 supports
	// programming of the clock rate via the second control register, and
	// that setting can be validate using this setting. This setting also
	// sets overrides the initial clock rate which is otherwise 1MHz.
	//
	// Some common rates:
	// 5.25" single or double density  -  1.0MHz
	// 3.5" 'standard'  -  1.2MHz
	// 3.5" HD  -  2.0MHz
	// 8" 360rpm  -  2.4MHz
	PORT_START("expected_clock")
	PORT_CONFNAME(0xffffff, 0, "FDC expected clock rate")
	PORT_CONFSETTING(0, "-")
	PORT_CONFSETTING(1000000, "1.0 MHz")
	PORT_CONFSETTING(1200000, "1.2 MHz")
	PORT_CONFSETTING(2000000, "2.0 MHz")
	PORT_CONFSETTING(2400000, "2.4 MHz")

	// It is not uncommon to see drivers using bit 7 of the control
	// register as a side selection. This conflicts with the DC5, and
	// earlier controllers in this series, which use bit 7 to inhibit
	// drive selection. These drivers need to be corrected, but this
	// option helps identify this issue and work around it.
	PORT_START("ctrl_reg_bit7_side_select")
	PORT_CONFNAME(0x01, 0, "Control register bit 7")
	PORT_CONFSETTING(0, "Inhibits drive selection")
	PORT_CONFSETTING(1, "Erroneous side select")

	// Debug aid to hard code the density. The FLEX format uses single
	// density for track zero. Many virtual disks 'fix' the format to be
	// purely double density and often without properly implement driver
	// support for that. This setting is an aid to report unexpected
	// usage, and it attempts to correct that.
	PORT_START("expected_density")
	PORT_CONFNAME(0xff, 0, "Expected density")
	PORT_CONFSETTING(0, "-")
	PORT_CONFSETTING(1, "single density") // Purely single density.
	PORT_CONFSETTING(2, "double density, with single density track zero") // The default FLEX double density format.
	PORT_CONFSETTING(3, "double density") // Purely double density.

	// Debug aid, to check that the sector head or side is set as expected
	// for the sector ID being read for the FLEX floppy disk format. Many
	// FLEX disk images were developed for vitural machines that have
	// little regard for the actual head and can work off the sector ID
	// and their drivers can set the head incorrectly. E.g. a disk with 18
	// sectors per side might have a drive set to switch heads for sectors
	// above 10. Another issue is that double density disk images are
	// often 'fixed' so that they are pure double density without being
	// single density onthe first track, and the drivers might still set
	// the head based track zero being single density. This aid is not
	// intended to be a substitute for fixing the drivers but it does help
	// work through the issues while patching the disks.
	PORT_START("expected_sectors")
	PORT_CONFNAME(0xff, 0, "Expected sectors per side")
	PORT_CONFSETTING(0, "-")
	PORT_CONFSETTING(10, "10") // 5 1/4" single density 256B
	PORT_CONFSETTING(15, "15") // 8" single density 256B
	PORT_CONFSETTING(18, "18") // 5 1/4" double density 256B
	PORT_CONFSETTING(26, "26") // 8" double density 256B
	PORT_CONFSETTING(36, "36") // 3.5" 1.4M QD 256B
	// The track zero expected sectors if different from the above. FLEX
	// 6800 disks did format track zero in single density and if the
	// driver sticks to that and if using a double density disk then set a
	// single density size here.
	PORT_START("track_zero_expected_sectors")
	PORT_CONFNAME(0xff, 0, "Track zero expected sectors per side")
	PORT_CONFSETTING(0, "-")
	PORT_CONFSETTING(10, "10") // 5 1/4" single density 256B
	PORT_CONFSETTING(15, "15") // 8" single density 256B
	PORT_CONFSETTING(18, "18") // 5 1/4" double density 256B
	PORT_CONFSETTING(26, "26") // 8" double density 256B
	PORT_CONFSETTING(36, "36") // 3.5" 1.4M QD 256B
INPUT_PORTS_END


//-------------------------------------------------
//  input_ports - device-specific input ports
//-------------------------------------------------

ioport_constructor ss50_dc5_device::device_input_ports() const
{
	return INPUT_PORTS_NAME(dc5);
}


//-------------------------------------------------
//  device_add_mconfig - add device-specific
//  machine configuration
//-------------------------------------------------

FLOPPY_FORMATS_MEMBER( ss50_dc5_device::floppy_formats )
	FLOPPY_FLEX_FORMAT
FLOPPY_FORMATS_END

// todo: implement floppy controller cards as slot devices and do this properly
static void flex_floppies(device_slot_interface &device)
{
	device.option_add("sssd35", FLOPPY_525_SSSD_35T); // 35 trks ss sd 5.25
	device.option_add("sssd",   FLOPPY_525_SSSD);     // 40 trks ss sd 5.25
	device.option_add("dssd35", FLOPPY_525_SD_35T);   // 35 trks ds sd 5.25
	device.option_add("dssd",   FLOPPY_525_SD);       // 40 trks ds sd 5.25
	device.option_add("ssdd",   FLOPPY_525_SSDD);     // 40 trks ss dd 5.25
	device.option_add("dd",     FLOPPY_525_DD);       // 40 trks ds dd 5.25
	device.option_add("ssqd",   FLOPPY_525_SSQD);     // 80 trks ss dd 5.25
	device.option_add("qd",     FLOPPY_525_QD);       // 80 trks ds dd 5.25
	device.option_add("8sssd",  FLOPPY_8_SSSD);       // 77 trks ss sd 8"
	device.option_add("8dssd",  FLOPPY_8_DSSD);       // 77 trks ds sd 8"
	device.option_add("8ssdd",  FLOPPY_8_SSDD);       // 77 trks ss dd 8"
	device.option_add("8dsdd",  FLOPPY_8_DSDD);       // 77 trks ds dd 8"
	device.option_add("35hd",   FLOPPY_35_HD);        // 1.44mb disk from swtpc emu (emulator only?)
}

void ss50_dc5_device::device_add_mconfig(machine_config &config)
{
	WD2797(config, m_fdc, 1_MHz_XTAL);
	FLOPPY_CONNECTOR(config, "fdc:0", flex_floppies, "sssd35", ss50_dc5_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:1", flex_floppies, "sssd35", ss50_dc5_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:2", flex_floppies, "sssd35", ss50_dc5_device::floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:3", flex_floppies, "sssd35", ss50_dc5_device::floppy_formats).enable_sound(true);

	m_fdc->intrq_wr_callback().set(FUNC(ss50_dc5_device::fdc_intrq_w));
	m_fdc->drq_wr_callback().set(FUNC(ss50_dc5_device::fdc_drq_w));
	m_fdc->sso_wr_callback().set(FUNC(ss50_dc5_device::fdc_sso_w));
}

void ss50_dc5_device::device_reset()
{
	// Initialize to the expected rate if any, otherwise to 1MHz.
	uint32_t clock = m_expected_clock->read();
	clock = clock ? clock : 1000000;
	m_fdc->set_unscaled_clock(clock);
	m_fdc_clock = clock;

	// The reset state of DDEN for the DC5 is one, so selecting single density.
	m_fdc->dden_w(1);
}

void ss50_dc5_device::device_start()
{
	m_fdc_status = 0;
	m_control_register = 0;
	m_fdc_side = 0;
	m_floppy_motor_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(ss50_dc5_device::floppy_motor_callback),this));
	m_motor_timer_out = 0;
}


/* Shared floppy support. */

void ss50_dc5_device::floppy_motor_trigger()
{
	m_floppy_motor_timer->adjust(attotime::from_msec(30000));
	if (!m_motor_timer_out)
	{
		m_motor_timer_out = 1;
		control_register_update();
	}
}

TIMER_CALLBACK_MEMBER(ss50_dc5_device::floppy_motor_callback)
{
	if (m_motor_timer_out)
	{
		m_motor_timer_out = 0;
		control_register_update();
	}
}

// On a FDC command write, check that the floppy side is as expected given
// the track and sector. This check is performed for the type II and III
// commands. The floppy side is modified if necessary.
void ss50_dc5_device::validate_floppy_side(uint8_t cmd)
{
	if ((cmd & 0xe1) == 0x80 || (cmd & 0xe0) == 0xa0 ||
	    (cmd & 0xf9) == 0xc0 || (cmd & 0xf9) == 0xe0 ||
	    (cmd & 0xf9) == 0xf0)
	{
		uint32_t expected_sectors = m_expected_sectors->read();
		uint32_t track_zero_expected_sectors = m_track_zero_expected_sectors->read();
		uint8_t sector = m_fdc->sector_r();
		uint8_t track = m_fdc->track_r();

		if (track_zero_expected_sectors && track == 0)
		{
			uint8_t expected_side = sector > track_zero_expected_sectors ? 1 : 0;

			if (m_fdc_side != expected_side)
			{
				logerror("%s Unexpected size %d for track %d sector %d expected side %d\n", machine().describe_context(), m_fdc_side, track, sector, expected_side);
				if (m_fdc_selected_floppy)
				{
					m_fdc_selected_floppy->ss_w(expected_side);
					m_fdc_side = expected_side;
				}
			}
		}

		if (expected_sectors)
		{
			uint8_t expected_side = sector > expected_sectors ? 1 : 0;

			if (m_fdc_side != expected_side)
			{
				logerror("%s Unexpected side %d for track %d sector %d expected side %d\n", machine().describe_context(), m_fdc_side, track, sector, expected_side);
				if (m_fdc_selected_floppy)
				{
					m_fdc_selected_floppy->ss_w(expected_side);
					m_fdc_side = expected_side;
				}
			}
		}
	}
}

// Note the dden line is low for double density.
uint8_t ss50_dc5_device::validate_fdc_dden(uint8_t dden)
{
	uint8_t expected_density = m_expected_density->read();
	switch (expected_density)
	{
		case 1:
			// Single density.
			if (!dden)
				logerror("%s Unexpected DDEN %d for single density\n", machine().describe_context(), dden);
			return 1;
		case 2:
		{
			// Double density with track zero single density.
			uint8_t track = m_fdc->track_r();

			if (track == 0)
			{
				// If this path is call on an update of the
				// second control register then the track need
				// not be accurate so this message might not
				// be accurate.
				if (!dden)
					logerror("%s Unexpected DDEN %d for single density trak 0\n", machine().describe_context(), dden);
				return 1;
			}
			if (dden)
				logerror("%s Unexpected DDEN %d for double density\n", machine().describe_context(), dden);
			return 0;
		}
		case 3:
			// Pure double density.
			if (dden)
				logerror("%s Unexpected DDEN %d for double density\n", machine().describe_context(), dden);
			return 0;
		default:
			return dden;
	}
}

uint8_t ss50_dc5_device::validate_fdc_sso(uint8_t cmd)
{
	// The DC4 and DC5 invert the SSO output and wire it to the DDEN input
	// to allow selection of single or double density.

	// In the DC5 two-register mode the SSO pin does not control DDEN.
	uint32_t two_regs = m_two_control_regs->read();
	if (two_regs)
	{
		// How if drivers are attempting to use SSO to control the
		// density then that might need correcting so report such
		// usage.
		logerror("%s Unexpected SSO flag set in two control registers mode\n", machine().describe_context());
		return cmd;
	}

	// The SSO is only loaded for type II and III commands.
	if ((cmd & 0xe1) == 0x80 ||
	    (cmd & 0xe0) == 0xa0 ||
	    (cmd & 0xf9) == 0xc0 ||
	    (cmd & 0xf9) == 0xe0 ||
	    (cmd & 0xf9) == 0xf0)
	{
		// The SSO output is inverted and input to DDEN.
		uint8_t dden = !BIT(cmd, 1);
		uint8_t sso = !validate_fdc_dden(dden);
		cmd = (cmd & 0xfd) | (sso << 1);
	}

	return cmd;
}

// The WD2797 supports an alternate interpretation of the sector size. Check
// that the flag is as expected for FLEX and return the corrected command if
// necessary.
uint8_t ss50_dc5_device::validate_fdc_sector_size(uint8_t cmd)
{
	if ((cmd & 0xe1) == 0x80 || (cmd & 0xe0) == 0xa0)
	{
		// Check that the sector length flag is set as expected.
		uint8_t sector_length_default = cmd & 0x08;
		if (sector_length_default != 0x08)
		{
			logerror("%s Unexpected sector length default %02x\n", machine().describe_context(), sector_length_default);
			// Patch the sector length flag.
			cmd |= 0x08;
		}
	}
	return cmd;
}

WRITE_LINE_MEMBER( ss50_dc5_device::fdc_intrq_w )
{
	if (state)
		m_fdc_status |= 0x40;
	else
		m_fdc_status &= ~0x40;

	uint8_t selection = m_interrupt_select->read();

	if (selection == 1)
	{
		write_irq(state);
	}
	else if (selection == 2)
	{
		write_firq(state);
	}
}

WRITE_LINE_MEMBER( ss50_dc5_device::fdc_drq_w )
{
	if (state)
		m_fdc_status |= 0x80;
	else
		m_fdc_status &= 0x7f;
}

WRITE_LINE_MEMBER( ss50_dc5_device::fdc_sso_w )
{
	// The DC4 and DC5 invert the SSO output and wire it to the DDEN input
	// to allow selection of single or double density.

	// A DC5 extension. In two-register mode the SSO pin does not control
	// DDEN.
	uint32_t two_regs = m_two_control_regs->read();
	if (two_regs)
		return;

	// However there are a lot of boot loaders that will
	// not work properly with this, so allow other options.
	uint8_t expected_density = m_expected_density->read();
	switch (expected_density)
	{
		case 1:
			// Single density.
			if (state)
				logerror("Unexpected SSO %d for single density\n", state);
			m_fdc->dden_w(1);
			break;
		case 2:
		{
			// Double density with track zero single density.
			uint8_t track = m_fdc->track_r();

			if (track == 0)
			{
				if (state)
					logerror("Unexpected SSO %d for single density trak 0\n", state);
				m_fdc->dden_w(1);
			}
			else
			{
				if (!state)
					logerror("Unexpected SSO %d for double density\n", state);
				m_fdc->dden_w(0);
			}
			break;
		}
		case 3:
			// Pure double density.
			if (!state)
				logerror("Unexpected SSO %d for double density\n", state);
			m_fdc->dden_w(0);
			break;
		default:
			m_fdc->dden_w(!state);
			break;
	}
}

// Called when state affected by the control register changes, when the
// control register is modified, or when the motor starts or stops.
void ss50_dc5_device::control_register_update()
{
	// The floppy drives do not gate the motor based on the select input,
	// so the motors run, or not, irrespective of the drive selection.
	uint8_t motor = m_motor_timer_out ? CLEAR_LINE : ASSERT_LINE;
	m_floppy0->get_device()->mon_w(motor);
	m_floppy1->get_device()->mon_w(motor);
	m_floppy2->get_device()->mon_w(motor);
	m_floppy3->get_device()->mon_w(motor);

	// The DC2, DC3, DC4, DC5 have a drive inhibit feature controlled by
	// bit 7, and the drives are de-selected if the motor timer has timed
	// out.
	uint8_t inhibit = BIT(m_control_register, 7);

	// However it is common to see drivers using bit 7 for side selection,
	// so offer any alternative interpretation as a work around.
	uint8_t bit7_side_select = m_ctrl_reg_bit7_side_select->read();

	floppy_image_device *floppy = nullptr;

	if ((bit7_side_select || !inhibit) && m_motor_timer_out)
	{
		uint8_t drive = m_control_register & 3;
		if (drive == 0) floppy = m_floppy0->get_device();
		if (drive == 1) floppy = m_floppy1->get_device();
		if (drive == 2) floppy = m_floppy2->get_device();
		if (drive == 3) floppy = m_floppy3->get_device();
	}

	if (floppy != m_fdc_selected_floppy)
	{
		m_fdc->set_floppy(floppy);
		m_fdc_selected_floppy = floppy;
	}

	if (floppy)
	{
		// The DC3, DC4, DC5 have a side select feature that is
		// controlled by bit 6. The DC1 and DC2 do not have a floppy
		// side output so did not support double sided disks.
		uint8_t side = BIT(m_control_register, 6);

		if (bit7_side_select)
		{
			uint8_t bit7 = BIT(m_control_register, 7);
			if (bit7)
				logerror("%s Unexpected use of DC5 control register bit 7 for side selection.\n", machine().describe_context());
			// OR together the side selection bits. This appears
			// the most useful choice because drivers in error are
			// most likely just writing to the wrong bit rather
			// than inconsistently to both bits. This allows
			// incremental correction of driver while spotting
			// remaining uses of bit 7 for side selection.
			side |= bit7;
		}
		floppy->ss_w(side);
		m_fdc_side = side;
	}
}


//-------------------------------------------------
//  register_read - read from a port register
//-------------------------------------------------

uint8_t ss50_dc5_device::register_read(offs_t offset)
{
	uint8_t address_mode = m_address_mode->read();

	// If the address mode is 16 bytes then subtract 4 and handle it as a
	// 4 byte address base.
	if (address_mode == 1) {
		if (offset < 4)
			return 0;
		offset -= 4;
	}

	if (offset > 7)
		return 0;

	if ((offset & 0x4) == 0x0)
	{
		uint32_t two_regs = m_two_control_regs->read();

		// Note: access to this control register does not trigger the
		// motor on and and does not reset the motor timer.

		if (!two_regs || (offset & 2) == 0)
		{
			// Control register 8014 / e014 read.
			return m_fdc_status;
		}
		else
		{
			// DC5 extension, reads a version number.
			return 0x02;
		}
	}
	else
	{
		// FDC 8018-801b / e018-e01b
		offset &= 3;
		// Access to the FDC triggers the motor timer.
		if (!machine().side_effects_disabled())
			floppy_motor_trigger();

		uint8_t data = m_fdc->wd2797_device::read(offset);
		return data;
	}
}

//-------------------------------------------------
//  register_write - write to a port register
//-------------------------------------------------

void ss50_dc5_device::register_write(offs_t offset, uint8_t data)
{
	uint8_t address_mode = m_address_mode->read();

	// If the address mode is 16 bytes then subtract 4 and handle it as a
	// 4 byte address base.
	if (address_mode == 1) {
		if (offset < 4)
			return;
		offset -= 4;
	}

	if (offset > 7)
		return;

	if ((offset & 0x4) == 0x0)
	{
		// Control register(s) 8014-8017 E014-E017
		uint32_t two_regs = m_two_control_regs->read();

		// Note: access to this control register does not trigger the
		// motor on and and does not reset the motor timer.

		if (!two_regs || (offset & 2) == 0)
		{
			// Control register 8014 / e014 write.
			m_control_register = data;
			control_register_update();
		}

		if (two_regs && (offset & 2) == 2)
		{
			// DC5 extended control register 8016 / E016.

			// Osc20 and osc12 control a clock divider.
			uint8_t osc12 = BIT(data, 2);
			uint8_t osc20 = BIT(data, 3);
			uint32_t clock = 1000000;

			// Note: supporting 2.4MHz is here is an extension to
			// the published DC5 design, and appears necessary to
			// support 360 rpm 8" drives. The case in which osc20
			// and osc12 are high generates 2.0MHz in the
			// published design, and that is redundant and is used
			// here to generate 2.4MHz. The DC5 is an open design
			// using a PLD and this variation can be programmed
			// into the DC5 hardware. The 2.4MHz rate appears to
			// be outside the limit for the WD2797 which appears
			// to be 2.173MHz. TODO explore this further; were the
			// 8" drives run at 300rpm; is there an error in the
			// rates elsewhere?
			if (osc20 && osc12)
				clock = 2400000;
			else if (osc20)
				clock = 2000000;
			else if (osc12)
				clock = 1200000;

			// Compare to the expected clock rate if any.
			uint32_t expected_clock = m_expected_clock->read();
			if (expected_clock && clock != expected_clock)
			{
				logerror("%s Unexpected clock rate of %dHz expected %dHz.\n", machine().describe_context(), clock, expected_clock);
				clock = expected_clock;
			}

			if (clock != m_fdc_clock)
			{
				m_fdc->set_unscaled_clock(clock);
				m_fdc_clock = clock;
			}

#if 0
			// Connects to the WD2979 5/8 pin.
			// TODO is this needed for the emulator?
			uint8_t five_or_eight = BIT(data, 4);
#endif

			// In this two-register mode the FDC SSO output pin
			// does not control DDEN. The bit 5 value is inverted
			// and input to DDEN.
			uint8_t dden = !BIT(data, 5);
			dden = validate_fdc_dden(dden);
			m_fdc->dden_w(dden);
		}
	}
	else
	{
		// FDC 8018-801b / e018-e01b
		offset &= 3;
		// Access to the FDC triggers the motor timer.
		floppy_motor_trigger();

		if (offset == 0) {
			validate_floppy_side(data);
			data = validate_fdc_sector_size(data);
			data = validate_fdc_sso(data);
		}

		m_fdc->wd2797_device::write(offset, data);
	}
}

// device type definition
DEFINE_DEVICE_TYPE_PRIVATE(SS50_DC5, ss50_card_interface, ss50_dc5_device, "ss50_dc5", "DC5 Floppy Disk Controller")