summaryrefslogtreecommitdiffstatshomepage
path: root/src/mess/audio/arcadia.c
blob: 306932a0b446c244beaf1071cc1325143761cde7 (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
// license:GPL-2.0+
// copyright-holders:David Viens, Peter Trauner
/***************************************************************************

  PeT mess@utanet.at
  main part in video/

  Refined with recording/analysis on MPT-03 (PAL UVI chip) by plgDavid

  NTSC UVI sound clock: 15734Hz (arcadia)
  PAL  UVI sound clock: 15625Hz (Soundic MPT-03 - owned by plgDavid)
***************************************************************************/


#include "includes/arcadia.h"

//known UVI audio clocks
#define UVI_NTSC 15734
#define UVI_PAL  15625

/* we need to create pulse transitions that sound 'decent'
   with the current mess/mame interp scheme

  this is not needed anymore with the new trick in streams.c
*/

#define OSAMP  1

//lfsr is 9 bits long (and same as Atari TIA pure noise)
#define LFSR_MASK (1<<8)

//makes alien invaders samples noise sync.
#define LFSR_INIT 0x00f0

//lfsr states at resynch borders
//0x01c1
//0x01e0
//0x00f0  //good synch
//0x0178
//0x01bc


// device type definition
const device_type ARCADIA_SOUND = &device_creator<arcadia_sound_device>;

//-------------------------------------------------
//  arcadia_sound_device - constructor
//-------------------------------------------------

arcadia_sound_device::arcadia_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, ARCADIA_SOUND, "Arcadia Audio Custom", tag, owner, clock, "arcadia_sound", __FILE__),
		device_sound_interface(mconfig, *this)
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------
void arcadia_sound_device::device_start()
{
	m_channel = machine().sound().stream_alloc(*this, 0, 1, UVI_PAL*OSAMP);
	m_lfsr    = LFSR_INIT;
	m_tval    = 1;
	logerror("arcadia_sound start\n");
}

//-------------------------------------------------
//  device_start - device-specific reset
//-------------------------------------------------
void arcadia_sound_device::device_reset()
{
	memset(m_reg, 0, sizeof(m_reg));
	m_omode = 0;
	m_pos = 0;
}

//-------------------------------------------------
//  sound_stream_update - handle update requests for
//  our sound stream
//-------------------------------------------------

void arcadia_sound_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
{
	int i;
	stream_sample_t *buffer = outputs[0];

	for (i = 0; i < samples; i++, buffer++)
	{
		*buffer = 0;

		//if minimal pitch ?
		if (m_reg[1]){
			switch (m_mode){
				//dont play anything
				case 0:break;

				//tone only
				case 1:
					*buffer = m_volume * m_tval;
				break;

				//noise only
				case 2:
					*buffer = m_volume * m_nval;
				break;

				//tone AND noise (bitwise and)
				case 3:
					*buffer = m_volume * (m_tval & m_nval);
				break;
			}

			//counter
			m_pos++;

			if (m_pos >= m_size){
				//calculate new noise bit ( taps: 0000T000T)
				unsigned char newBit = m_lfsr & 1;         //first tap
				newBit = (newBit ^ ((m_lfsr & 0x10)?1:0) );//xor with second tap

				m_nval = m_lfsr & 1; //taking new output from LSB
				m_lfsr = m_lfsr >> 1;//shifting

				//insert new bit at end position (size-1) (only if non null)
				if (newBit)
					m_lfsr |= LFSR_MASK;

				//invert tone
				m_tval = !m_tval;

				m_pos = 0;
			}
		}
	}
}



//-------------------------------------------------
//  soundport_w
//-------------------------------------------------

WRITE8_MEMBER(arcadia_sound_device::write)
{
	m_channel->update();
	m_reg[offset] = data;

	//logerror("arcadia_sound write:%x=%x\n",offset,data);

	switch (offset)
	{
		case 1:
			//as per Gobbler samples:
			//the freq counter is only applied on the next change in the flip flop
			m_size = (data & 0x7f)*OSAMP;
			//logerror("arcadia_sound write: frq:%d\n",data);

			//reset LFSR
			if(!m_size)
				m_lfsr = LFSR_INIT;
		break;

		case 2:
			m_volume = (data & 0x07) * 0x800;
			m_mode   = (data & 0x18) >> 3;

			//logerror("arcadia_sound write: vol:%d mode:%d\n",m_volume,m_mode );

			if (m_mode != m_omode){
				//not 100% sure about this, maybe we should not reset anything
				//m_pos  = 0;
				m_tval = 0;
			}
			m_omode = m_mode;
		break;
	}
}