summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/isa/ibm_mfc.cpp
blob: b7f5d8e70b56721a8678dd086f23e49858dfc32d (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
// license:BSD-3-Clause
// copyright-holders:Philip Bennett
/***************************************************************************

  ISA 8 bit IBM PC Music Feature Card

  TODO:
   - YM-2164
   - MIDI
   - IRQ/base address selection

  Notes:
   - Some software does not function correctly at higher CPU speeds
  (e.g. the Sierra games and Yamaha Compose/PlayRec)

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


#include "emu.h"
#include "ibm_mfc.h"

#include "cpu/z80/z80.h"
#include "machine/clock.h"
#include "machine/pit8253.h"
#include "speaker.h"


//-------------------------------------------------
//  Constants
//-------------------------------------------------

#define TCR_TAC                 0x01
#define TCR_TBC                 0x02
#define TCR_TAE                 0x04
#define TCR_TBE                 0x08
#define TCR_EXT8                0x10
#define TCR_TMSK                0x40
#define TCR_IBE                 0x80

#define TSR_TAS                 0x01
#define TSR_TBS                 0x02
#define TSR_TCS                 0x80

enum
{
	PC_IRQ_TIMERA,
	PC_IRQ_TIMERB,
	PC_IRQ_RXRDY,
	PC_IRQ_TXRDY
};

enum
{
	Z80_IRQ_YM,
	Z80_IRQ_RXRDY,
	Z80_IRQ_TXRDY,
	Z80_IRQ_MIDI_RXRDY,
	Z80_IRQ_MIDI_TXRDY
};


//-------------------------------------------------
//  Globals
//-------------------------------------------------

DEFINE_DEVICE_TYPE(ISA8_IBM_MFC, isa8_ibm_mfc_device, "ibm_mfc", "IBM PC Music Feature Card")


//-------------------------------------------------
//  Interrupt handling
//-------------------------------------------------

void isa8_ibm_mfc_device::set_pc_interrupt(int src, int state)
{
	if (state)
		m_pc_irq_state |= 1 << src;
	else
		m_pc_irq_state &= ~(1 << src);

	update_pc_interrupts();
}

void isa8_ibm_mfc_device::update_pc_interrupts(void)
{
	// IRQs enabled?
	if (m_tcr & TCR_IBE)
	{
		// IRQs unmasked?
		if (m_tcr & TCR_TMSK)
		{
			m_isa->irq3_w(m_pc_irq_state ? ASSERT_LINE : CLEAR_LINE);
		}
	}
}

void isa8_ibm_mfc_device::set_z80_interrupt(int src, int state)
{
	if (state)
		m_z80_irq_state |= 1 << src;
	else
		m_z80_irq_state &= ~(1 << src);

	update_z80_interrupts();
}

void isa8_ibm_mfc_device::update_z80_interrupts(void)
{
	m_cpu->set_input_line(0, m_z80_irq_state ? ASSERT_LINE : CLEAR_LINE);
}


//-------------------------------------------------
//  Z80 memory map
//-------------------------------------------------

void isa8_ibm_mfc_device::prg_map(address_map &map)
{
	map(0x0000, 0x7fff).rom();
	map(0x8000, 0x8000).ram(); // Unknown - tested on startup
	map(0xbfff, 0xbfff).ram(); // Unknown - tested on startup
	map(0xc000, 0xdfff).ram();
	map(0xe000, 0xffff).ram();
}

void isa8_ibm_mfc_device::io_map(address_map &map)
{
	map.unmap_value_high();
	map.global_mask(0xff);
	map(0x00, 0x01).rw(m_ym2151, FUNC(ym2151_device::read), FUNC(ym2151_device::write));
	map(0x10, 0x10).rw("d71051", FUNC(i8251_device::data_r), FUNC(i8251_device::data_w));
	map(0x11, 0x11).rw("d71051", FUNC(i8251_device::status_r), FUNC(i8251_device::control_w));
	map(0x20, 0x23).rw("d71055c_1", FUNC(i8255_device::read), FUNC(i8255_device::write));
}


//-------------------------------------------------
//  Jumpers and DIP switches
//-------------------------------------------------

static INPUT_PORTS_START( ibm_mfc )
	PORT_START("J1")
	PORT_DIPNAME( 0x07, 0x03, "IBM MFC J1: IRQ" )
	PORT_DIPSETTING(    0x02, "2" )
	PORT_DIPSETTING(    0x03, "3" )
	PORT_DIPSETTING(    0x04, "4" )
	PORT_DIPSETTING(    0x05, "5" )
	PORT_DIPSETTING(    0x06, "6" )
	PORT_DIPSETTING(    0x07, "7" )

	PORT_START("SW1")
	PORT_DIPNAME( 0x03, 0x00, "IBM MFC SW1: Base Address" )
	PORT_DIPSETTING(    0x00, "2A00" )
	PORT_DIPSETTING(    0x01, "2A10" )
	PORT_DIPSETTING(    0x02, "2A20" )
	PORT_DIPSETTING(    0x03, "2A30" )
INPUT_PORTS_END

//-------------------------------------------------
//  D71055C PPI (PC)
//-------------------------------------------------

READ8_MEMBER( isa8_ibm_mfc_device::ppi0_i_a )
{
	// Read data from the Z80 PIU
	return m_d71055c_1->pa_r();
}

WRITE8_MEMBER( isa8_ibm_mfc_device::ppi0_o_b )
{
	// Write data to the Z80 PIU - no action required
}

WRITE8_MEMBER( isa8_ibm_mfc_device::ppi0_o_c )
{
	// PC Port B /OBF (C1) -> Z80 Port B /STB (C2)
	m_d71055c_1->pc2_w(BIT(data, 1));

	// PC Port A IBF (C5) -> Z80 Port A /ACK (C6)
#if 0 // TODO
	m_d71055c_1->pc6_w(!BIT(data, 5));
#else
	if (!BIT(data, 5) && BIT(m_pc_ppi_c, 5))
		m_d71055c_1->pc6_w(0);
#endif

	// Bit 0 (INTRB) is TxRDY
	set_pc_interrupt(PC_IRQ_TXRDY, BIT(data, 0));

	// Bit 3 (INTRA) is RxRDY
	set_pc_interrupt(PC_IRQ_RXRDY, BIT(data, 3));

	m_pc_ppi_c = data;
}

READ8_MEMBER( isa8_ibm_mfc_device::ppi0_i_c )
{
	// Receive data bit 8
	return BIT(m_z80_ppi_c, 5) << 7;
}

//-------------------------------------------------
//  D71055C PPI (Z80)
//-------------------------------------------------

WRITE8_MEMBER( isa8_ibm_mfc_device::ppi1_o_a )
{
	// Write data to the PC PIU - no action required
}

READ8_MEMBER( isa8_ibm_mfc_device::ppi1_i_b )
{
	// Read data from the PC PIU
	return m_d71055c_0->pb_r();
}

WRITE8_MEMBER( isa8_ibm_mfc_device::ppi1_o_c )
{
	// PortA /OBF (C7) -> PortA /STB (C2)
	m_d71055c_0->pc4_w(BIT(data, 7));

	// PortB IBF (C1) -> PortB /ACK (C2)
#if 0 // TODO
	m_d71055c_0->pc2_w(!BIT(data, 1));
#else
	if (!BIT(data, 1) && BIT(m_z80_ppi_c, 1))
		m_d71055c_0->pc2_w(0);
#endif

	set_z80_interrupt(Z80_IRQ_TXRDY, BIT(data, 3));
	set_z80_interrupt(Z80_IRQ_RXRDY, BIT(data, 0));

	m_z80_ppi_c = data;
}

//-------------------------------------------------
//  D8253 PIT
//-------------------------------------------------

WRITE_LINE_MEMBER( isa8_ibm_mfc_device::d8253_out0 )
{
	if (m_tcr & TCR_TAE)
		set_pc_interrupt(PC_IRQ_TIMERA, 1);
}

WRITE_LINE_MEMBER( isa8_ibm_mfc_device::d8253_out1 )
{
	if (m_tcr & TCR_TBE)
		set_pc_interrupt(PC_IRQ_TIMERB, 1);
}


//-------------------------------------------------
//  uPD71051 USART
//-------------------------------------------------

WRITE_LINE_MEMBER( isa8_ibm_mfc_device::write_usart_clock )
{
	m_d71051->write_txc(state);
	m_d71051->write_rxc(state);
}

//-------------------------------------------------
//  YM-2164
//-------------------------------------------------


WRITE_LINE_MEMBER(isa8_ibm_mfc_device::ibm_mfc_ym_irq)
{
	set_z80_interrupt(Z80_IRQ_YM, state);
}


//-------------------------------------------------
//  ISA interface
//-------------------------------------------------

READ8_MEMBER( isa8_ibm_mfc_device::ibm_mfc_r )
{
	uint8_t val;

	switch (offset)
	{
		case 0x0:
		case 0x1:
		case 0x2:
		case 0x3:
		{
			val = m_d71055c_0->read(offset);
			break;
		}

		case 0xc:
		case 0xd:
		case 0xe:
		case 0xf:
		{
			val = (m_pc_irq_state ? 0x80 : 0) | (m_pc_irq_state & 3);
			break;
		}

		default:
		{
			fatalerror("Unhandled IBM MFC read from %d\n", offset);
		}
	}

	return val;
}

WRITE8_MEMBER( isa8_ibm_mfc_device::ibm_mfc_w )
{
	switch (offset)
	{
		case 0x0:
		case 0x1:
		case 0x2:
		case 0x3:
		{
			machine().scheduler().boost_interleave(attotime::zero, attotime::from_usec(1000));
			m_d71055c_0->write(offset, data);
			break;
		}

		case 0x4:
		case 0x5:
		case 0x6:
		case 0x7:
		{
			m_d8253->write(offset & 3, data);
			break;
		}

		case 0x8:
		case 0x9:
		case 0xa:
		case 0xb:
		{
			m_tcr = data;

			if (~m_tcr & TCR_TAC)
				set_pc_interrupt(PC_IRQ_TIMERA, 0);

			if (~m_tcr & TCR_TBC)
				set_pc_interrupt(PC_IRQ_TIMERB, 0);

			m_d71051->write_dsr((m_tcr & TCR_EXT8) ? 1 : 0);

			break;
		}

		case 0xc:
		case 0xd:
		case 0xe:
		case 0xf:
		{
			// TSR is read-only but Yamaha software attempts to write to it
			break;
		}
	}
}


//-------------------------------------------------
//  ROM definition
//-------------------------------------------------

ROM_START( ibm_mfc )
	ROM_REGION( 0x8000, "ibm_mfc", 0 )
	ROM_LOAD( "xc215 c 0.bin", 0x0000, 0x8000, CRC(28c58a4f) SHA1(e7edf28d20e6c146e3144526c89cd6beea64663b) )
ROM_END


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

void isa8_ibm_mfc_device::device_add_mconfig(machine_config &config)
{
	Z80(config, m_cpu, XTAL(11'800'000) / 2);
	m_cpu->set_addrmap(AS_PROGRAM, &isa8_ibm_mfc_device::prg_map);
	m_cpu->set_addrmap(AS_IO, &isa8_ibm_mfc_device::io_map);

	I8255(config, m_d71055c_0);
	m_d71055c_0->in_pa_callback().set(FUNC(isa8_ibm_mfc_device::ppi0_i_a));
	m_d71055c_0->out_pb_callback().set(FUNC(isa8_ibm_mfc_device::ppi0_o_b));
	m_d71055c_0->in_pc_callback().set(FUNC(isa8_ibm_mfc_device::ppi0_i_c));
	m_d71055c_0->out_pc_callback().set(FUNC(isa8_ibm_mfc_device::ppi0_o_c));

	I8255(config, m_d71055c_1);
	m_d71055c_1->out_pa_callback().set(FUNC(isa8_ibm_mfc_device::ppi1_o_a));
	m_d71055c_1->in_pb_callback().set(FUNC(isa8_ibm_mfc_device::ppi1_i_b));
	m_d71055c_1->out_pc_callback().set(FUNC(isa8_ibm_mfc_device::ppi1_o_c));

	I8251(config, m_d71051, 0);

	clock_device &usart_clock(CLOCK(config, "usart_clock", XTAL(4'000'000) / 8)); // 500KHz
	usart_clock.signal_handler().set(FUNC(isa8_ibm_mfc_device::write_usart_clock));

	PIT8253(config, m_d8253, 0);
	m_d8253->set_clk<0>(XTAL(4'000'000) / 8);
	m_d8253->out_handler<0>().set(FUNC(isa8_ibm_mfc_device::d8253_out0));
	m_d8253->set_clk<1>(0);
	m_d8253->out_handler<1>().set(FUNC(isa8_ibm_mfc_device::d8253_out1));
	m_d8253->set_clk<2>(XTAL(4'000'000) / 2);
	m_d8253->out_handler<2>().set(m_d8253, FUNC(pit8253_device::write_clk1));

	SPEAKER(config, "ymleft").front_left();
	SPEAKER(config, "ymright").front_right();
	YM2151(config, m_ym2151, XTAL(4'000'000));
	m_ym2151->irq_handler().set(FUNC(isa8_ibm_mfc_device::ibm_mfc_ym_irq));
	m_ym2151->add_route(0, "ymleft", 1.00);
	m_ym2151->add_route(1, "ymright", 1.00);
}


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

ioport_constructor isa8_ibm_mfc_device::device_input_ports() const
{
	return INPUT_PORTS_NAME( ibm_mfc );
}


//-------------------------------------------------
//  rom_region - return a pointer to the device's
//  internal ROM region
//-------------------------------------------------

const tiny_rom_entry *isa8_ibm_mfc_device::device_rom_region() const
{
	return ROM_NAME( ibm_mfc );
}


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

//-------------------------------------------------
//  isa8_ibm_mfc_device - constructor
//-------------------------------------------------

isa8_ibm_mfc_device::isa8_ibm_mfc_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, ISA8_IBM_MFC, tag, owner, clock),
	device_isa8_card_interface(mconfig, *this),
	m_tcr(0), m_pc_ppi_c(0), m_z80_ppi_c(0), m_pc_irq_state(0), m_z80_irq_state(0),
	m_cpu(*this, "ibm_mfc"),
	m_ym2151(*this, "ym2151"),
	m_d8253(*this, "d8253"),
	m_d71051(*this, "d71051"),
	m_d71055c_0(*this, "d71055c_0"),
	m_d71055c_1(*this, "d71055c_1")
{
}


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

void isa8_ibm_mfc_device::device_start()
{
	set_isa_device();
	m_isa->install_device(0x2a20, 0x2a20 + 15, read8_delegate(*this, FUNC(isa8_ibm_mfc_device::ibm_mfc_r)), write8_delegate(*this, FUNC(isa8_ibm_mfc_device::ibm_mfc_w)));
}


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

void isa8_ibm_mfc_device::device_reset()
{
	m_tcr = 0;
	m_d71051->write_dsr(0);
	m_pc_irq_state = 0;
	m_z80_irq_state = 0;
}