summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/coco/coco_ssc.cpp
blob: 2f7a1793071b181647129a030c5f8470adf1787c (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
// license:BSD-3-Clause
// copyright-holders:tim lindner
/***************************************************************************

    coco_ssc.cpp

    Code for emulating the CoCo Speech / Sound Cartridge

    The SSC was a complex sound cartridge. A TMS-7040 microcontroller,
    SPO256-AL2 speech processor, AY-3-8913 programable sound generator, and
    2K of RAM.

    All four ports of the microcontroller are under software control.

    Port A is input from the host CPU.
    Port B is A0-A7 for the 2k of RAM.
    Port C is the internal bus controller:
        bit 7 6 5 4 3 2 1 0
            | | | | | | | |
            | | | | | | | + A8 for RAM and BC1 of AY3-8913
            | | | | | | +-- A9 for RAM
            | | | | | +---- A10 for RAM
            | | | | +------ R/W* for RAM and BDIR for AY3-8913
            | | | +-------- CS* for RAM
            | | +---------- ALD* for SP0256
            | +------------ CS* for AY3-8913
            +-------------- BUSY* for host CPU (connects to a latch)
        * – Active low
    Port D is the 8-bit data bus.

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

#include "emu.h"
#include "coco_ssc.h"

#include "cpu/tms7000/tms7000.h"
#include "machine/netlist.h"
#include "machine/ram.h"
#include "netlist/devices/net_lib.h"
#include "sound/ay8910.h"
#include "sound/sp0256.h"

#include "speaker.h"

#define LOG_SSC 0
#define PIC_TAG "pic7040"
#define AY_TAG "cocossc_ay"
#define SP0256_TAG "sp0256"

#define SP0256_GAIN 1.75
#define AY8913_GAIN 2.0

#define C_A8   0x01
#define C_BC1  0x01
#define C_A9   0x02
#define C_A10  0x04
#define C_RRW  0x08
#define C_BDR  0x08
#define C_RCS  0x10
#define C_ALD  0x20
#define C_ACS  0x40
#define C_BSY  0x80

//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************

namespace
{
	class cocossc_sac_device;

	// ======================> coco_ssc_device

	class coco_ssc_device :
			public device_t,
			public device_cococart_interface
	{
	public:
		// construction/destruction
		coco_ssc_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);

		// optional information overrides
		virtual const tiny_rom_entry *device_rom_region() const override;
		virtual void device_add_mconfig(machine_config &config) override;
		virtual void device_reset() override;

		u8 ssc_port_a_r();
		void ssc_port_b_w(u8 data);
		u8 ssc_port_c_r();
		void ssc_port_c_w(u8 data);
		u8 ssc_port_d_r();
		void ssc_port_d_w(u8 data);

	protected:
		// device-level overrides
		virtual void device_start() override;
		virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override;
		u8 ff7d_read(offs_t offset);
		void ff7d_write(offs_t offset, u8 data);
		virtual void set_sound_enable(bool sound_enable) override;
		static constexpr device_timer_id BUSY_TIMER_ID  = 0;

	private:
		u8                                      m_reset_line;
		bool                                    m_tms7000_busy;
		u8                                      m_tms7000_porta;
		u8                                      m_tms7000_portb;
		u8                                      m_tms7000_portc;
		u8                                      m_tms7000_portd;
		emu_timer                               *m_tms7000_busy_timer;
		required_device<tms7040_device>         m_tms7040;
		required_device<ram_device>             m_staticram;
		required_device<ay8910_device>          m_ay;
		required_device<sp0256_device>          m_spo;
		required_device<cocossc_sac_device>     m_sac;
	};

	// ======================> Color Computer Sound Activity Circuit filter

	class cocossc_sac_device : public device_t,
		public device_sound_interface
	{
	public:
		cocossc_sac_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);
		~cocossc_sac_device() { }
		bool sound_activity_circuit_output();

	protected:
		// device-level overrides
		virtual void device_start() override;

		// sound stream update overrides
		virtual void sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples) override;

	private:
		sound_stream*  m_stream;
		double m_rms[16];
		int m_index;
	};
};


//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

DEFINE_DEVICE_TYPE_PRIVATE(COCO_SSC, device_cococart_interface, coco_ssc_device, "coco_ssc", "CoCo S/SC PAK");
DEFINE_DEVICE_TYPE(COCOSSC_SAC, cocossc_sac_device, "cocossc_sac", "CoCo SSC Sound Activity Circuit");


//**************************************************************************
//  MACHINE FRAGMENTS AND ADDRESS MAPS
//**************************************************************************

void coco_ssc_device::device_add_mconfig(machine_config &config)
{
	TMS7040(config, m_tms7040, DERIVED_CLOCK(2, 1));
	m_tms7040->in_porta().set(FUNC(coco_ssc_device::ssc_port_a_r));
	m_tms7040->out_portb().set(FUNC(coco_ssc_device::ssc_port_b_w));
	m_tms7040->in_portc().set(FUNC(coco_ssc_device::ssc_port_c_r));
	m_tms7040->out_portc().set(FUNC(coco_ssc_device::ssc_port_c_w));
	m_tms7040->in_portd().set(FUNC(coco_ssc_device::ssc_port_d_r));
	m_tms7040->out_portd().set(FUNC(coco_ssc_device::ssc_port_d_w));

	RAM(config, "staticram").set_default_size("2K").set_default_value(0);

	SPEAKER(config, "ssc_audio").front_center();

	SP0256(config, m_spo, XTAL(3'120'000));
	m_spo->add_route(ALL_OUTPUTS, "ssc_audio", SP0256_GAIN);
	m_spo->data_request_callback().set_inputline(m_tms7040, TMS7000_INT1_LINE);

	AY8913(config, m_ay, DERIVED_CLOCK(2, 1));
	m_ay->set_flags(AY8910_SINGLE_OUTPUT);
	m_ay->add_route(ALL_OUTPUTS, "coco_sac_tag", AY8913_GAIN);

	COCOSSC_SAC(config, m_sac, DERIVED_CLOCK(2, 1));
	m_sac->add_route(ALL_OUTPUTS, "ssc_audio", 1.0);
}

ROM_START(coco_ssc)
	ROM_REGION(0x1000, PIC_TAG, 0)
	ROM_LOAD("pic-7040-510.bin", 0x0000, 0x1000, CRC(a8e2eb98) SHA1(7c17dcbc21757535ce0b3a9e1ce5ca61319d3606)) // pic7040 cpu rom
	ROM_REGION(0x10000, SP0256_TAG, 0)
	ROM_LOAD("sp0256-al2.bin", 0x1000, 0x0800, CRC(b504ac15) SHA1(e60fcb5fa16ff3f3b69d36c7a6e955744d3feafc))
ROM_END

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

//-------------------------------------------------
//  coco_ssc_device - constructor
//-------------------------------------------------

coco_ssc_device::coco_ssc_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
		: device_t(mconfig, COCO_SSC, tag, owner, clock),
		device_cococart_interface(mconfig, *this ),
		m_tms7040(*this, PIC_TAG),
		m_staticram(*this, "staticram"),
		m_ay(*this, AY_TAG),
		m_spo(*this, SP0256_TAG),
		m_sac(*this, "coco_sac_tag")
{
}

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

void coco_ssc_device::device_start()
{
	// install $FF7D-E handler
	install_readwrite_handler(0xFF7D, 0xFF7E,
			read8sm_delegate(*this, FUNC(coco_ssc_device::ff7d_read)),
			write8sm_delegate(*this, FUNC(coco_ssc_device::ff7d_write)));

	save_item(NAME(m_reset_line));
	save_item(NAME(m_tms7000_busy));
	save_item(NAME(m_tms7000_porta));
	save_item(NAME(m_tms7000_portb));
	save_item(NAME(m_tms7000_portc));
	save_item(NAME(m_tms7000_portd));

	m_tms7000_busy_timer = timer_alloc(BUSY_TIMER_ID);

}


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

void coco_ssc_device::device_reset()
{
	m_reset_line = 0;
	m_tms7000_busy = false;
}


//-------------------------------------------------
//  device_timer - handle timer callbacks
//-------------------------------------------------

void coco_ssc_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch(id)
	{
		case BUSY_TIMER_ID:
			m_tms7000_busy = false;
			m_tms7000_busy_timer->adjust(attotime::never);
			break;

		default:
			break;

	}
}


//-------------------------------------------------
//  rom_region - device-specific ROM region
//-------------------------------------------------

const tiny_rom_entry *coco_ssc_device::device_rom_region() const
{
	return ROM_NAME( coco_ssc );
}


//-------------------------------------------------
//  set_sound_enable
//-------------------------------------------------

void coco_ssc_device::set_sound_enable(bool sound_enable)
{
	if( sound_enable )
	{
		m_sac->set_output_gain(0, 1.0);
		m_spo->set_output_gain(0, 1.0);
	}
	else
	{
		m_sac->set_output_gain(0, 0.0);
		m_spo->set_output_gain(0, 0.0);
	}
}

//-------------------------------------------------
//  ff7d_read
//-------------------------------------------------

u8 coco_ssc_device::ff7d_read(offs_t offset)
{
	u8 data = 0xff;

	switch(offset)
	{
		case 0x00:
			data = 0xff;

			if (LOG_SSC)
			{
				logerror( "[%s] ff7d read: %02x\n", machine().describe_context(), data );
			}
			break;

		case 0x01:
			data = 0x1f;

			if( m_tms7000_busy == false )
			{
				data |= 0x80;
			}

			if( m_spo->sby_r() )
			{
				data |= 0x40;
			}

			if( ! m_sac->sound_activity_circuit_output() )
			{
				data |= 0x20;
			}

			if (LOG_SSC)
			{
				logerror( "[%s] ff7e read: %c%c%c%c %c%c%c%c (%02x)\n",
					machine().describe_context(),
					data & 0x80 ? '.' : 'B',
					data & 0x40 ? '.' : 'S',
					data & 0x20 ? '.' : 'P',
					data & 0x10 ? '.' : '1',
					data & 0x08 ? '.' : '1',
					data & 0x04 ? '.' : '1',
					data & 0x02 ? '.' : '1',
					data & 0x01 ? '.' : '1',
					data );
			}
			break;
	}

	return data;
}


//-------------------------------------------------
//  ff7d_write
//-------------------------------------------------

void coco_ssc_device::ff7d_write(offs_t offset, u8 data)
{
	switch(offset)
	{
		case 0x00:
			if (LOG_SSC)
			{
				logerror( "[%s] ff7d write: %02x\n", machine().describe_context(), data );
			}

			if( (m_reset_line & 1) == 1 )
			{
				if( (data & 1) == 0 )
				{
					m_tms7040->reset();
					m_ay->reset();
					m_spo->reset();
					m_tms7000_busy = false;
				}
			}

			m_reset_line = data;
			break;

		case 0x01:

			if (LOG_SSC)
			{
				logerror( "[%s] ff7e write: %02x\n", machine().describe_context(), data );
			}

			m_tms7000_porta = data;
			m_tms7000_busy = true;
			m_tms7040->set_input_line(TMS7000_INT3_LINE, ASSERT_LINE);
			break;
	}
}


//-------------------------------------------------
//  Handlers for secondary CPU ports
//-------------------------------------------------

u8 coco_ssc_device::ssc_port_a_r()
{
	if (LOG_SSC)
	{
		logerror( "[%s] port a read: %02x\n", machine().describe_context(), m_tms7000_porta );
	}

	m_tms7040->set_input_line(TMS7000_INT3_LINE, CLEAR_LINE);

	return m_tms7000_porta;
}

void coco_ssc_device::ssc_port_b_w(u8 data)
{
	if (LOG_SSC)
	{
		logerror( "[%s] port b write: %02x\n", machine().describe_context(), data );
	}

	m_tms7000_portb = data;
}

u8 coco_ssc_device::ssc_port_c_r()
{
	if (LOG_SSC)
	{
		logerror( "[%s] port c read: %02x\n", machine().describe_context(), m_tms7000_portc );
	}

	return m_tms7000_portc;
}

void coco_ssc_device::ssc_port_c_w(u8 data)
{
	if( (data & C_RCS) == 0 && (data & C_RRW) == 0) /* static RAM write */
	{
		u16 address = u16(data) << 8;
		address += m_tms7000_portb;
		address &= 0x7ff;

		m_staticram->write(address, m_tms7000_portd);
	}

	if( (data & C_ACS) == 0 ) /* chip select for AY-3-8913 */
	{
		if( (data & (C_BDR|C_BC1)) == (C_BDR|C_BC1) ) /* BDIR = 1, BC1 = 1: latch address */
		{
			m_ay->address_w(m_tms7000_portd);
		}

		if( ((data & C_BDR) == C_BDR) && ((data & C_BC1) == 0) ) /* BDIR = 1, BC1 = 0: write data */
		{
			m_ay->data_w(m_tms7000_portd);
		}
	}

	if( (data & C_ALD) == 0 )
	{
		m_spo->ald_w(m_tms7000_portd);
	}

	if( ((m_tms7000_portc & C_BSY) == 0) && ((data & C_BSY) == C_BSY) )
	{
		m_tms7000_busy_timer->adjust(attotime::from_usec(1800));
	}

	if (LOG_SSC)
	{
		logerror( "[%s] port c write: %c%c%c%c %c%c%c%c (%02x)\n",
			machine().describe_context(),
			data & 0x80 ? '.' : 'B',
			data & 0x40 ? '.' : 'P',
			data & 0x20 ? '.' : 'V',
			data & 0x10 ? '.' : 'R',
			data & 0x40 ? (data & 0x08 ? 'R' : 'W') : (data & 0x08 ? 'D' : '.'),
			data & 0x04 ? '1' : '0',
			data & 0x02 ? '1' : '0',
			data & 0x40 ? (data & 0x01 ? '1' : '0') : (data & 0x01 ? 'C' : '.'),
			data );
	}

	m_tms7000_portc = data;
}

u8 coco_ssc_device::ssc_port_d_r()
{
	if( ((m_tms7000_portc & C_RCS) == 0) && ((m_tms7000_portc & C_ACS) == 0))
		logerror( "[%s] Warning: Reading RAM and PSG at the same time!\n", machine().describe_context() );

	if( ((m_tms7000_portc & C_RCS) == 0)  && ((m_tms7000_portc & C_RRW) == C_RRW)) /* static ram chip select (low) and static ram chip read (high) */
	{
		u16 address = u16(m_tms7000_portc) << 8;
		address += m_tms7000_portb;
		address &= 0x7ff;

		m_tms7000_portd = m_staticram->read(address);
	}

	if( (m_tms7000_portc & C_ACS) == 0 ) /* chip select for AY-3-8913 */
	{
		if( ((m_tms7000_portc & C_BDR) == 0) && ((m_tms7000_portc & C_BC1) == C_BC1) ) /* psg read data */
		{
			m_tms7000_portd = m_ay->data_r();
		}
	}

	if (LOG_SSC)
	{
		logerror( "[%s] port d read: %02x\n", machine().describe_context(), m_tms7000_portd );
	}

	return m_tms7000_portd;
}

void coco_ssc_device::ssc_port_d_w(u8 data)
{
	if (LOG_SSC)
	{
		logerror( "[%s] port d write: %02x\n", machine().describe_context(), data );
	}

	m_tms7000_portd = data;
}


//-------------------------------------------------
//  cocossc_sac_device - constructor
//-------------------------------------------------

cocossc_sac_device::cocossc_sac_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, COCOSSC_SAC, tag, owner, clock),
		device_sound_interface(mconfig, *this),
		m_stream(nullptr),
		m_index(0)
{
}


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

void cocossc_sac_device::device_start()
{
	m_stream = stream_alloc(1, 1, machine().sample_rate());
}


//-------------------------------------------------
//  sound_stream_update - handle a stream update
//-------------------------------------------------

void cocossc_sac_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	stream_sample_t *src = inputs[0];
	stream_sample_t *dst = outputs[0];

	double n = samples;

	while (samples--)
	{
		m_rms[m_index] += ( (double)*src * (double)*src );
		*dst++ = (*src++);
	}

	m_rms[m_index] = m_rms[m_index] / n;
	m_rms[m_index] = sqrt(m_rms[m_index]);

	m_index++;
	m_index &= 0x0f;
}


//-------------------------------------------------
//  sound_activity_circuit_output - making sound
//-------------------------------------------------

bool cocossc_sac_device::sound_activity_circuit_output()
{
  double average = m_rms[0] + m_rms[1] + m_rms[2] + m_rms[3] + m_rms[4] +
	m_rms[5] + m_rms[6] + m_rms[7] + m_rms[8] + m_rms[9] + m_rms[10] +
	m_rms[11] + m_rms[12] + m_rms[13] + m_rms[14] + m_rms[15];

	average /= 16.0;

	if( average > 10400.0 )
		return true;

	return false;
}