summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/spectrum/d40.cpp
blob: e88f4607c54a24466bd71539efde84f4a43434a5 (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
// license:BSD-3-Clause
// copyright-holders:MetalliC
/**********************************************************************

    Didaktik D40/D80 disk interface
    (C) 1991 Didaktik Skalica

    useful commands:
     RUN - boot disc
     CAT - files list
     LIST * - system information
     LOAD *"filename" - load program

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


#include "emu.h"
#include "d40.h"


//**************************************************************************
//  DEVICE DEFINITIONS
//**************************************************************************

DEFINE_DEVICE_TYPE(SPECTRUM_D40, spectrum_d40_device, "spectrum_d40", "Didaktik D40")
DEFINE_DEVICE_TYPE(SPECTRUM_D80, spectrum_d80_device, "spectrum_d80", "Didaktik D80 (MDOS 1, 2793 FDC)")
DEFINE_DEVICE_TYPE(SPECTRUM_D80V2, spectrum_d80v2_device, "spectrum_d80v2", "Didaktik D80 (MDOS 2, 8272 FDC)")

//-------------------------------------------------
//  INPUT_PORTS
//-------------------------------------------------

INPUT_PORTS_START(d40)
	PORT_START("BUTTON")
	PORT_BIT(0x01, IP_ACTIVE_HIGH, IPT_BUTTON1) PORT_NAME("Snapshot Button") PORT_CODE(KEYCODE_MINUS_PAD) PORT_CHANGED_MEMBER(DEVICE_SELF, spectrum_d40base_device, snapshot_button, 0)

	PORT_START("JOY")
	PORT_BIT(0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT) PORT_8WAY
	PORT_BIT(0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT)  PORT_8WAY
	PORT_BIT(0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN)  PORT_8WAY
	PORT_BIT(0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP)    PORT_8WAY
	PORT_BIT(0x10, IP_ACTIVE_HIGH, IPT_BUTTON1)
	PORT_BIT(0xe0, IP_ACTIVE_HIGH, IPT_UNUSED)
INPUT_PORTS_END

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

ioport_constructor spectrum_d40base_device::device_input_ports() const
{
	return INPUT_PORTS_NAME(d40);
}

//-------------------------------------------------
//  SLOT_INTERFACE( floppies )
//-------------------------------------------------

static void didaktik_floppies(device_slot_interface &device)
{
	device.option_add("35dd", FLOPPY_35_DD);
	device.option_add("525dd", FLOPPY_525_DD);
}

//-------------------------------------------------
//  ROM
//-------------------------------------------------

ROM_START(d40)
	ROM_REGION(0x4000, "rom", 0)
	ROM_DEFAULT_BIOS("mdos11")
	ROM_SYSTEM_BIOS(0, "mdos11", "MDOS 1.0 (1991)")
	ROMX_LOAD("mdos10.bin", 0x0000, 0x4000, CRC(e6b70939) SHA1(308c4b5daf6bb1f05c68a447129d723da423326e), ROM_BIOS(0))
	ROM_SYSTEM_BIOS(1, "mdos12", "MDOS 1.0+ (1992)") // possible unofficial modification
	ROMX_LOAD("mdos10p.bin", 0x0000, 0x4000, CRC(92c45741) SHA1(b8473235feecff4eccbace56a90cf1d8c79506eb), ROM_BIOS(1))
ROM_END

ROM_START(d80v2)
	ROM_REGION(0x4000, "rom", 0)
	ROM_DEFAULT_BIOS("mdos2")
	ROM_SYSTEM_BIOS(0, "mdos2", "MDOS 2.0 (1993)")
	ROMX_LOAD("mdos20.bin", 0x0000, 0x4000, CRC(9e79d022) SHA1(e8d3355051fb287dd0dda34ba8824442130c8254), ROM_BIOS(0))
ROM_END

//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

void spectrum_d40_device::device_add_mconfig(machine_config &config)
{
	WD2797(config, m_fdc, 4_MHz_XTAL / 4);
	m_fdc->intrq_wr_callback().set(FUNC(spectrum_d40_device::fdc_intrq_w));
	m_fdc->drq_wr_callback().set(FUNC(spectrum_d40_device::fdc_drq_w));
	FLOPPY_CONNECTOR(config, "fdc:0", didaktik_floppies, "525dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:1", didaktik_floppies, "525dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);

	I8255(config, m_ppi);
	m_ppi->in_pa_callback().set_ioport("JOY");

	/* software list */
	//SOFTWARE_LIST(config, "flop_list").set_original("spectrum_d40_flop");
}

void spectrum_d80_device::device_add_mconfig(machine_config &config)
{
	WD2797(config, m_fdc, 4_MHz_XTAL / 4);
	m_fdc->intrq_wr_callback().set(FUNC(spectrum_d80_device::fdc_intrq_w));
	m_fdc->drq_wr_callback().set(FUNC(spectrum_d80_device::fdc_drq_w));
	FLOPPY_CONNECTOR(config, "fdc:0", didaktik_floppies, "35dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:1", didaktik_floppies, "35dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);

	I8255(config, m_ppi);

	/* software list */
	//SOFTWARE_LIST(config, "flop_list").set_original("spectrum_d40_flop");
}

void spectrum_d80v2_device::device_add_mconfig(machine_config &config)
{
	WD37C65C(config, m_fdc, 16_MHz_XTAL); // actually GM82C765B
	FLOPPY_CONNECTOR(config, "fdc:0", didaktik_floppies, "35dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);
	FLOPPY_CONNECTOR(config, "fdc:1", didaktik_floppies, "35dd", floppy_image_device::default_pc_floppy_formats).enable_sound(true);

	I8255(config, m_ppi);

	/* software list */
	//SOFTWARE_LIST(config, "flop_list").set_original("spectrum_d40_flop");
}

const tiny_rom_entry *spectrum_d40_device::device_rom_region() const
{
	return ROM_NAME(d40);
}

const tiny_rom_entry *spectrum_d80v2_device::device_rom_region() const
{
	return ROM_NAME(d80v2);
}

//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  spectrum_d40base_device - constructor
//-------------------------------------------------

spectrum_d40base_device::spectrum_d40base_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, type, tag, owner, clock)
	, device_spectrum_expansion_interface(mconfig, *this)
	, m_rom(*this, "rom")
	, m_ppi(*this, "ppi")
	, m_snap(*this, "BUTTON")
{
}

spectrum_d40_device::spectrum_d40_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: spectrum_d40base_device(mconfig, SPECTRUM_D40, tag, owner, clock)
	, m_fdc(*this, "fdc")
	, m_floppy(*this, "fdc:%u", 0)
{
}

spectrum_d40_device::spectrum_d40_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	:spectrum_d40base_device(mconfig, type, tag, owner, clock)
	, m_fdc(*this, "fdc")
	, m_floppy(*this, "fdc:%u", 0)
{
}

spectrum_d80_device::spectrum_d80_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: spectrum_d40_device(mconfig, SPECTRUM_D80, tag, owner, clock)
{
}

spectrum_d80v2_device::spectrum_d80v2_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: spectrum_d40base_device(mconfig, SPECTRUM_D80V2, tag, owner, clock)
	, m_fdc(*this, "fdc")
	, m_floppy(*this, "fdc:%u", 0)
{
}

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

void spectrum_d40base_device::device_start()
{
	std::fill(std::begin(m_ram), std::end(m_ram), 0);
	save_item(NAME(m_romcs));
	save_item(NAME(m_ram));
	save_item(NAME(m_snap_flag));
	save_item(NAME(m_8255_reset));
	save_item(NAME(m_8255_enable));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void spectrum_d40base_device::device_reset()
{
	m_romcs = 0;
	m_snap_flag = 0;
	m_8255_reset = 0;
	m_8255_enable = 0;
}


//**************************************************************************
//  IMPLEMENTATION  spectrum_d40base_device
//**************************************************************************

READ_LINE_MEMBER(spectrum_d40base_device::romcs)
{
	return m_romcs;
}

void spectrum_d40base_device::pre_opcode_fetch(offs_t offset)
{
	if (!machine().side_effects_disabled())
	{
		switch (offset)
		{
		case 0x0000:
		case 0x0008:
			m_romcs = 1;
			nmi_check();
			break;
		case 0x0066:
			if (!m_romcs)
			{
				m_romcs = 1;
				m_snap_flag = 1;
				nmi_check();
			}
			break;
		case 0x1700:
			if (!m_snap_flag)
				m_romcs = 0;
			nmi_check();
			break;
		}
	}
}

uint8_t spectrum_d40base_device::iorq_r(offs_t offset)
{
	uint8_t data = 0xff;

	switch (offset & 0xf9)
	{
	case 0x81: // FDC
		data = fdc0_r(offset);
		break;
	case 0x89: // FDC
		data = fdc1_r(offset);
		break;
	}

	if (!BIT(offset, 7) && m_8255_enable) // 1F/3F/5F/7F
		data = m_ppi->read((offset >> 5) & 3);

	return data;
}

void spectrum_d40base_device::iorq_w(offs_t offset, uint8_t data)
{
	switch (offset & 0xf9)
	{
	case 0x81: // FDC
		fdc0_w(offset, data);
		break;
	case 0x89: // control port
		fdc1_w(offset, data);
		break;
	case 0x91: // PPI reset
		m_8255_reset = BIT(data, 5);
		if (!m_8255_reset)
		{
			m_ppi->reset();
			m_8255_enable = 0;
		}
		break;
	case 0x99: // PPI enable
		if (m_8255_reset)
			m_8255_enable = BIT(data, 4);
		break;
	}

	if (!BIT(offset, 7) && m_8255_enable) // 1F/3F/5F/7F
		m_ppi->write((offset >> 5) & 3, data);
}

uint8_t spectrum_d40base_device::mreq_r(offs_t offset)
{
	if (m_snap_flag && offset == 0x66)
	{
		m_snap_flag = 0; // hacky, real hardware reset this latch by Z80 /WR line
		return 0xc7; // RST 0 inject
	}

	uint8_t data = 0xff;

	if (m_romcs)
	{
		if (offset < 0x3800)
			data = m_rom->base()[offset];
		else
			data = m_ram[offset & 0x7ff];
	}

	return data;
}

void spectrum_d40base_device::mreq_w(offs_t offset, uint8_t data)
{
	if (m_romcs)
	{
		if ((offset & 0xf800) == 0x3800)
			m_ram[offset & 0x7ff] = data;
	}
}

INPUT_CHANGED_MEMBER(spectrum_d40base_device::snapshot_button)
{
	nmi_check();
}

void spectrum_d40base_device::nmi_check()
{
	int state = CLEAR_LINE;

	if (!m_romcs && m_snap->read())
		state = ASSERT_LINE;

	m_slot->nmi_w(state);
}

//**************************************************************************
//  IMPLEMENTATION  spectrum_d40_device
//**************************************************************************

void spectrum_d40_device::device_start()
{
	spectrum_d40base_device::device_start();
	save_item(NAME(m_control));
	save_item(NAME(m_intrq));
	save_item(NAME(m_drq));
}

void spectrum_d40_device::device_reset()
{
	spectrum_d40base_device::device_reset();
	m_control = 0;
	m_intrq = 0;
	m_drq = 0;
}

uint8_t spectrum_d40_device::fdc0_r(offs_t offset)
{
	return m_fdc->read((offset >> 1) & 0x03);
}

void spectrum_d40_device::fdc0_w(offs_t offset, uint8_t data)
{
	m_fdc->write((offset >> 1) & 0x03, data);
}

void spectrum_d40_device::fdc1_w(offs_t offset, uint8_t data)
{
	m_control = data;

	floppy_image_device* floppy = nullptr;
	if (BIT(data, 0))
		floppy = m_floppy[0]->get_device();
	else if (BIT(data, 1))
		floppy = m_floppy[1]->get_device();
	m_fdc->set_floppy(floppy);

	m_floppy[0]->get_device()->mon_w(BIT(data, 2) ? 0 : 1);
	m_floppy[1]->get_device()->mon_w(BIT(data, 3) ? 0 : 1);

	nmi_check();
}

void spectrum_d40_device::fdc_intrq_w(int state)
{
	m_intrq = state;
	nmi_check();
}

void spectrum_d40_device::fdc_drq_w(int state)
{
	m_drq = state;
	nmi_check();
}

void spectrum_d40_device::nmi_check()
{
	int state = CLEAR_LINE;

	if (!m_romcs && m_snap->read())
		state = ASSERT_LINE;

	if (m_intrq && BIT(m_control, 7))
		state = ASSERT_LINE;

	if (m_drq && BIT(m_control, 6))
		state = ASSERT_LINE;

	m_slot->nmi_w(state);
}

//**************************************************************************
//  IMPLEMENTATION  spectrum_d80v2_device
//**************************************************************************

void spectrum_d80v2_device::pre_opcode_fetch(offs_t offset)
{
	if (!machine().side_effects_disabled())
	{
		switch (offset)
		{
		case 0x0000:
		case 0x0008:
		case 0x0066:
			m_romcs = 1;
			nmi_check();
			break;
		case 0x1700:
			m_romcs = 0;
			nmi_check();
			break;
		}
	}
}

uint8_t spectrum_d80v2_device::fdc0_r(offs_t offset)
{
	return BIT(offset, 2) ? m_fdc->fifo_r() : m_fdc->msr_r();
}

uint8_t spectrum_d80v2_device::fdc1_r(offs_t offset)
{
	return m_fdc->dor_r();
}

void spectrum_d80v2_device::fdc0_w(offs_t offset, uint8_t data)
{
	if (BIT(offset, 2))
		m_fdc->fifo_w(data);
}

void spectrum_d80v2_device::fdc1_w(offs_t offset, uint8_t data)
{
	m_fdc->dor_w(data);
}

uint8_t spectrum_d80v2_device::mreq_r(offs_t offset)
{
	uint8_t data = 0xff;

	if (m_romcs)
	{
		if (offset < 0x3800)
			data = m_rom->base()[offset];
		else
			data = m_ram[offset & 0x7ff];
	}

	return data;
}