summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/er1400.cpp
blob: 763e80af58f6176db9e3c91b4c83be1cfaef7b1b (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
// license:BSD-3-Clause
// copyright-holders:AJR
/***************************************************************************

    GI ER1400 1400 Bit Serial Electrically Alterable Read-Only Memory

    This device uses negative logic. The emulation uses logic levels for
    all data, which means that inputs should be inverted if non-inverting
    drivers are used and vice versa.

    Vgg should be 35 volts below Vss. Logic one is output as 12 volts below
    Vss. Vss represents logic zero, which is nominally GND but may be +12V.

    The frequency of the input clock should be between 10 kHz and 17 kHz,
    with a 35–65% duty cycle when the device is in operation. The clock may
    be left at logic zero when the device is in standby.

    Write and erase times are 10 ms (min), 15 ms (typical), 24 ms (max).
    The data retention capability of the actual chip can be greatly
    degraded by frequent reprogramming.

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

#include "emu.h"
#include "machine/er1400.h"

#define VERBOSE 0
#include "logmacro.h"

// device type definition
DEFINE_DEVICE_TYPE(ER1400, er1400_device, "er1400", "ER1400 Serial EAROM (100x14)")


//**************************************************************************
//  ER1400 DEVICE
//**************************************************************************

//-------------------------------------------------
//  er1400_device - constructor
//-------------------------------------------------

er1400_device::er1400_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: device_t(mconfig, ER1400, tag, owner, clock)
	, device_nvram_interface(mconfig, *this)
	, m_clock_input(0)
	, m_code_input(0)
	, m_data_input(0)
	, m_data_output(0)
	, m_write_time(attotime::never)
	, m_erase_time(attotime::never)
	, m_data_register(0)
	, m_address_register(0)
{
}


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

void er1400_device::device_start()
{
	save_item(NAME(m_clock_input));
	save_item(NAME(m_code_input));
	save_item(NAME(m_data_input));
	save_item(NAME(m_data_output));

	save_item(NAME(m_write_time));
	save_item(NAME(m_erase_time));

	save_item(NAME(m_data_register));
	save_item(NAME(m_address_register));

	m_data_array = std::make_unique<u16[]>(100);
	save_pointer(NAME(m_data_array), 100);

	m_data_propagation_timer = timer_alloc(PROPAGATION_TIMER);
}


//-------------------------------------------------
//  nvram_default - called to initialize NVRAM to
//  its default state
//-------------------------------------------------

void er1400_device::nvram_default()
{
	// all locations erased
	std::fill(&m_data_array[0], &m_data_array[100], 0x3fff);
}


//-------------------------------------------------
//  nvram_read - called to read NVRAM from the
//  .nv file
//-------------------------------------------------

void er1400_device::nvram_read(emu_file &file)
{
	file.read(&m_data_array[0], 100 * sizeof(m_data_array[0]));
}


//-------------------------------------------------
//  nvram_write - called to write NVRAM to the
//  specified file
//-------------------------------------------------

void er1400_device::nvram_write(emu_file &file)
{
	file.write(&m_data_array[0], 100 * sizeof(m_data_array[0]));
}


//-------------------------------------------------
//  address_binary_format - logging helper
//-------------------------------------------------

std::string er1400_device::address_binary_format() const
{
	std::ostringstream result;
	for (int i = 19; i >= 10; i--)
		result << (BIT(m_address_register, i) ? '1' : '0');
	result << ':';
	for (int i = 9; i >= 0; i--)
		result << (BIT(m_address_register, i) ? '1' : '0');
	return result.str();
}


//-------------------------------------------------
//  read_data - read addressed word into data
//  register
//-------------------------------------------------

void er1400_device::read_data()
{
	int selected = 0;
	m_data_register = 0;
	for (int tens = 10; tens < 20; tens++)
	{
		if (BIT(m_address_register, tens))
		{
			for (int units = 0; units < 10; units++)
			{
				if (BIT(m_address_register, units))
				{
					offs_t offset = 10 * (tens - 10) + units;
					LOG("Reading data at %d (%04X) into register\n", offset, m_data_array[offset]);
					m_data_register |= m_data_array[offset];
					selected++;
				}
			}
		}
	}

	if (selected != 1)
		logerror("%d addresses selected for read operation\n", selected);
}


//-------------------------------------------------
//  write_data - write data register to addressed
//  word (which must have been erased first)
//-------------------------------------------------

void er1400_device::write_data()
{
	int selected = 0;
	for (int tens = 10; tens < 20; tens++)
	{
		if (BIT(m_address_register, tens))
		{
			for (int units = 0; units < 10; units++)
			{
				if (BIT(m_address_register, units))
				{
					offs_t offset = 10 * (tens - 10) + units;
					if ((m_data_array[offset] & ~m_data_register) != 0)
					{
						LOG("Writing data at %d (%04X changed to %04X)\n", offset,
							m_data_array[offset], m_data_array[offset] & m_data_register);
						m_data_array[offset] &= m_data_register;
					}
					selected++;
				}
			}
		}
	}

	if (selected != 1)
		logerror("%d addresses selected for write operation\n", selected);
}


//-------------------------------------------------
//  erase_data - erase data at addressed word to
//  all ones
//-------------------------------------------------

void er1400_device::erase_data()
{
	int selected = 0;
	for (int tens = 10; tens < 20; tens++)
	{
		if (BIT(m_address_register, tens))
		{
			for (int units = 0; units < 10; units++)
			{
				if (BIT(m_address_register, units))
				{
					offs_t offset = 10 * (tens - 10) + units;
					if (m_data_array[offset] != 0x3fff)
					{
						LOG("Erasing data at %d\n", offset);
						m_data_array[offset] = 0x3fff;
					}
					selected++;
				}
			}
		}
	}

	if (selected != 1)
		logerror("%d addresses selected for erase operation\n", selected);
}


//-------------------------------------------------
//  data_w - write data input line
//-------------------------------------------------

WRITE_LINE_MEMBER(er1400_device::data_w)
{
	m_data_input = bool(state);
}


//-------------------------------------------------
//  c1_w - write to first control line
//-------------------------------------------------

WRITE_LINE_MEMBER(er1400_device::c1_w)
{
	if (bool(state) == BIT(m_code_input, 2))
		return;

	m_code_input = (m_code_input & 3) | (bool(state) << 2);
	if (!m_data_propagation_timer->enabled())
		m_data_propagation_timer->adjust(attotime::from_usec(20));
}


//-------------------------------------------------
//  c2_w - write to second control line
//-------------------------------------------------

WRITE_LINE_MEMBER(er1400_device::c2_w)
{
	if (bool(state) == BIT(m_code_input, 1))
		return;

	m_code_input = (m_code_input & 5) | (bool(state) << 1);
	if (!m_data_propagation_timer->enabled())
		m_data_propagation_timer->adjust(attotime::from_usec(20));
}


//-------------------------------------------------
//  c3_w - write to third control line
//-------------------------------------------------

WRITE_LINE_MEMBER(er1400_device::c3_w)
{
	if (bool(state) == BIT(m_code_input, 0))
		return;

	m_code_input = (m_code_input & 6) | bool(state);
	if (!m_data_propagation_timer->enabled())
		m_data_propagation_timer->adjust(attotime::from_usec(20));
}


//-------------------------------------------------
//  device_timer - called whenever a device timer
//  fires
//-------------------------------------------------

void er1400_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
	case PROPAGATION_TIMER:
		m_data_output = (m_code_input == 5) ? BIT(m_data_register, 13) : false;
		break;
	}
}


//-------------------------------------------------
//  clock_w - write to clock line
//-------------------------------------------------

WRITE_LINE_MEMBER(er1400_device::clock_w)
{
	if (m_clock_input == bool(state))
		return;
	m_clock_input = bool(state);

	// Commands are clocked by a logical 1 -> 0 transition (i.e. rising edge)
	if (!state)
	{
		if (machine().time() >= m_write_time)
			write_data();
		if (m_code_input != 6)
		{
			if (m_write_time != attotime::never && machine().time() < m_write_time)
				logerror("Write not completed in time\n");
			m_write_time = attotime::never;
		}

		if (machine().time() >= m_erase_time)
			erase_data();
		if (m_code_input != 2)
		{
			if (m_erase_time != attotime::never && machine().time() < m_erase_time)
				logerror("Erase not completed in time\n");
			m_erase_time = attotime::never;
		}

		switch (m_code_input)
		{
		case 0: // standby
			break;

		case 1: default: // not used
			break;

		case 2: // erase
			if (m_erase_time == attotime::never)
			{
				LOG("Entering erase command (address = %s)\n", address_binary_format());
				m_erase_time = machine().time() + attotime::from_msec(15);
			}
			break;

		case 3: // accept address
			m_address_register = (m_address_register << 1) | m_data_input;
			m_address_register &= 0xfffff;
			break;

		case 4: // read
			read_data();
			break;

		case 5: // shift data out
			m_data_register = (m_data_register & 0x1fff) << 1;
			m_data_propagation_timer->adjust(attotime::from_usec(20));
			break;

		case 6: // write
			if (m_write_time == attotime::never)
			{
				LOG("Entering write command (address = %s, data = %04X)\n", address_binary_format(), m_data_register);
				m_write_time = machine().time() + attotime::from_msec(15);
			}
			break;

		case 7: // accept data
			m_data_register = (m_data_register & 0x1fff) << 1;
			m_data_register |= m_data_input;
			break;
		}
	}
}


//-------------------------------------------------
//  data_r - read data line
//-------------------------------------------------

READ_LINE_MEMBER(er1400_device::data_r)
{
	return m_data_input | m_data_output;
}