1 // license:BSD-3-Clause
2 // copyright-holders:Jonathan Gevaryahu
3 /***************************************************************************
4 
5     audio/socrates.c
6     Copyright (C) 2010-2011 Jonathan Gevaryahu AKA Lord Nightmare
7 
8     This handles the two squarewaves (plus the one weird wave) channels
9     on the V-tech Socrates system 27-0769 ASIC.
10 
11 
12 ****************************************************************************/
13 
14 #include "emu.h"
15 #include "socrates.h"
16 
17 
18 // device type definition
19 DEFINE_DEVICE_TYPE(SOCRATES_SOUND, socrates_snd_device, "socrates_snd", "Socrates Sound")
20 
21 
22 //-------------------------------------------------
23 //  socrates_snd_device - constructor
24 //-------------------------------------------------
25 
socrates_snd_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)26 socrates_snd_device::socrates_snd_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
27 	: device_t(mconfig, SOCRATES_SOUND, tag, owner, clock)
28 	, device_sound_interface(mconfig, *this)
29 {
30 }
31 
32 
33 //-------------------------------------------------
34 //  device_start - device-specific startup
35 //-------------------------------------------------
36 
device_start()37 void socrates_snd_device::device_start()
38 {
39 	m_freq[0] = m_freq[1] = 0xff; /* channel 1,2 frequency */
40 	m_vol[0] = m_vol[1] = 0x07; /* channel 1,2 volume */
41 	m_enable[0] = m_enable[1] = 0x01; /* channel 1,2 enable */
42 	m_channel3 = 0x00; /* channel 3 weird register */
43 	m_DAC_output = 0x00; /* output */
44 	m_state[0] = m_state[1] = m_state[2] = 0;
45 	m_accum[0] = m_accum[1] = m_accum[2] = 0xFF;
46 	m_stream = stream_alloc(0, 1, clock() ? clock() : machine().sample_rate());
47 }
48 
49 
50 //-------------------------------------------------
51 //  sound_stream_update - handle a stream update
52 //-------------------------------------------------
53 
sound_stream_update(sound_stream & stream,std::vector<read_stream_view> const & inputs,std::vector<write_stream_view> & outputs)54 void socrates_snd_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
55 {
56 	for (int i = 0; i < outputs[0].samples(); i++)
57 	{
58 		snd_clock();
59 		outputs[0].put_int(i, (int)m_DAC_output, 32768 >> 4);
60 	}
61 }
62 
63 
64 const uint8_t socrates_snd_device::s_volumeLUT[16] =
65 {
66 0, 61, 100, 132, 158, 183, 201, 218,
67 233, 242, 253, 255, 250, 240, 224, 211
68 }; // this table is actually quite weird on the real console.
69 // 0, 0.033, 0.055, 0.07175, 0.086, 0.1, 0.11, 0.119, 0.127, 0.132, 0.138, 0.139, 0.136, 0.131, 0.122, 0.115 are the voltage amplitudes for the steps on channel 2. the last four are particularly bizarre, probably caused by some sort of internal clipping.
70 
snd_clock()71 void socrates_snd_device::snd_clock() /* called once per clock */
72 {
73 	for (int channel = 0; channel < 2; channel++)
74 	{
75 		if ((m_accum[channel] == 0) && m_enable[channel])
76 		{
77 			m_state[channel] = (m_state[channel]^0x1);
78 			m_accum[channel] = m_freq[channel];
79 		}
80 		else if (m_enable[channel])
81 		{
82 			m_accum[channel]--;
83 		}
84 		else
85 		{
86 			m_accum[channel] = 0; // channel is disabled
87 			m_state[channel] = 0;
88 		}
89 	}
90 	// handle channel 3 here
91 	m_DAC_output = (m_state[0]?(s_volumeLUT[m_vol[0]]*9.4):0); // channel 1 is ~2.4 times as loud as channel 2
92 	m_DAC_output += (m_state[1]?(s_volumeLUT[m_vol[1]]<<2):0);
93 	// add channel 3 to dac output here
94 }
95 
96 
reg0_w(int data)97 void socrates_snd_device::reg0_w(int data)
98 {
99 	m_stream->update();
100 	m_freq[0] = data;
101 }
102 
reg1_w(int data)103 void socrates_snd_device::reg1_w(int data)
104 {
105 	m_stream->update();
106 	m_freq[1] = data;
107 }
108 
reg2_w(int data)109 void socrates_snd_device::reg2_w(int data)
110 {
111 	m_stream->update();
112 	m_vol[0] = data&0xF;
113 	m_enable[0] = (data&0x10)>>4;
114 }
115 
reg3_w(int data)116 void socrates_snd_device::reg3_w(int data)
117 {
118 	m_stream->update();
119 	m_vol[1] = data&0xF;
120 	m_enable[1] = (data&0x10)>>4;
121 }
122 
reg4_w(int data)123 void socrates_snd_device::reg4_w(int data)
124 {
125 	m_stream->update();
126 	m_channel3 = data;
127 }
128