summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/adc0808.cpp
blob: 5f8aeb8f93cc590dfbc779c13f0a316c8979d306 (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
// license: BSD-3-Clause
// copyright-holders: Dirk Best
/***************************************************************************

    ADC0808/ADC0809

    A/D Converter with 8 Channel-Multiplexer

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

#include "emu.h"
#include "adc0808.h"


//**************************************************************************
//  CONSTANTS/MACROS
//**************************************************************************

#define VERBOSE 0


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

DEFINE_DEVICE_TYPE(ADC0808, adc0808_device, "adc0808", "ADC0808 A/D Converter")
DEFINE_DEVICE_TYPE(ADC0809, adc0809_device, "adc0809", "ADC0809 A/D Converter")
DEFINE_DEVICE_TYPE(M58990, m58990_device, "m58990", "M58990 A/D Converter")


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

// permit our enum to be saved
ALLOW_SAVE_TYPE(adc0808_device::state);

//-------------------------------------------------
//  adc0808_device - constructor
//-------------------------------------------------

adc0808_device::adc0808_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, type, tag, owner, clock),
	m_eoc_cb(*this), m_eoc_ff_cb(*this),
	m_in_cb{ {*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this}, {*this} },
	m_state(STATE_IDLE),
	m_cycle_timer(nullptr),
	m_start(0), m_address(0), m_sar(0xff), m_eoc(1)
{
}

adc0808_device::adc0808_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	adc0808_device(mconfig, ADC0808, tag, owner, clock)
{
}

//-------------------------------------------------
//  adc0809_device - constructor
//-------------------------------------------------

adc0809_device::adc0809_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	adc0808_device(mconfig, ADC0809, tag, owner, clock)
{
}

//-------------------------------------------------
//  m58990_device - constructor
//-------------------------------------------------

m58990_device::m58990_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	adc0808_device(mconfig, M58990, tag, owner, clock)
{
}

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

void adc0808_device::device_start()
{
	// resolve callbacks
	m_eoc_cb.resolve_safe();
	m_eoc_ff_cb.resolve_safe();

	for (int i = 0; i < 8; i++)
		m_in_cb[i].resolve_safe(0xff);

	// allocate timers
	m_cycle_timer = timer_alloc();
	m_cycle_timer->adjust(attotime::zero, 0, attotime::from_hz(clock()));

	// register for save states
	save_item(NAME(m_state));
	save_item(NAME(m_start));
	save_item(NAME(m_address));
	save_item(NAME(m_sar));
	save_item(NAME(m_eoc));
}

//-------------------------------------------------
//  device_timer - handler timer events
//-------------------------------------------------

void adc0808_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (m_state)
	{
		case STATE_IDLE:
			m_cycle_timer->adjust(attotime::never);
			return;

		// ready; beginning to run conversion cycle
		case STATE_CONVERSION_READY:
			m_state = STATE_CONVERSION_RUNNING;

			m_sar = m_in_cb[m_address](0);
			m_eoc = 0;
			m_eoc_cb(m_eoc);

			// the conversion takes 8 steps per 8 cycles
			m_cycle_timer->adjust(attotime::from_ticks(64, clock()));
			return;

		// start; mark ourselves as ready for conversion 1 cycle later
		case STATE_CONVERSION_START:
			m_state = STATE_CONVERSION_READY;
			m_cycle_timer->adjust(attotime::from_hz(clock()));
			return;

		// end of conversion cycle
		case STATE_CONVERSION_RUNNING:
		{
			m_state = STATE_IDLE;
			const uint8_t start_sar = m_sar;
			m_sar = m_in_cb[m_address](0);

			m_eoc = 1;
			m_eoc_cb(m_eoc);
			m_eoc_ff_cb(1);

			if (VERBOSE)
				logerror("Conversion finished, result %02x\n", m_sar);

			if (m_sar != start_sar)
				logerror("Conversion finished, should fail - starting value %02x, ending value %02x", start_sar, m_sar);

			// eoc is delayed by one cycle
			m_cycle_timer->adjust(attotime::never);
			break;
		}
	}
}


//**************************************************************************
//  INTERFACE
//**************************************************************************

READ8_MEMBER( adc0808_device::data_r )
{
	if (!machine().side_effects_disabled())
	{
		if (VERBOSE)
			logerror("data_r: %02x\n", m_sar);

		// oe connected to flip-flop clear
		m_eoc_ff_cb(0);
	}

	return m_sar;
}

WRITE8_MEMBER( adc0808_device::address_w )
{
	m_address = data & 7;
}

WRITE_LINE_MEMBER( adc0808_device::start_w )
{
	if (m_start == state)
		return;

	if (state && !m_start)
	{
		m_state = STATE_CONVERSION_START;
		m_cycle_timer->adjust(attotime::from_hz(clock()));
	}
	else if (!state && m_start)
	{
		m_cycle_timer->adjust(attotime::from_hz(clock()));
	}

	m_start = state;
}

READ_LINE_MEMBER( adc0808_device::eoc_r )
{
	return m_eoc;
}

WRITE8_MEMBER( adc0808_device::address_offset_start_w )
{
	if (VERBOSE)
		logerror("address_offset_start_w %02x %02x\n", offset, data);

	start_w(1);
	address_w(space, 0, offset);
	start_w(0);
}

WRITE8_MEMBER( adc0808_device::address_data_start_w )
{
	if (VERBOSE)
		logerror("address_data_start_w %02x %02x\n", offset, data);

	start_w(1);
	address_w(space, 0, data);
	start_w(0);
}