summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/eepromdev.c
blob: 71723a73b6ff3a685c71b9fe66d545d1c5fdee23 (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
/*
 * Serial eeproms
 *
 */

#include "driver.h"
#include "machine/eepromdev.h"

#define VERBOSE 0
#define LOG(x) do { if (VERBOSE) logerror x; } while (0)

#define SERIAL_BUFFER_LENGTH 40
#define MEMORY_SIZE 1024

typedef struct _eeprom_state eeprom_state;
struct _eeprom_state
{
	const eeprom_interface *intf;
	int serial_count;
	UINT8 serial_buffer[SERIAL_BUFFER_LENGTH];
	UINT8 eeprom_data[MEMORY_SIZE];
	int eeprom_data_bits;
	int eeprom_read_address;
	int eeprom_clock_count;
	int latch,reset_line,clock_line,sending;
	int locked;
	int reset_delay;
};

/*-------------------------------------------------
    get_safe_token - makes sure that the passed
    in device is, in fact, an I2C memory
-------------------------------------------------*/

INLINE eeprom_state *get_safe_token(const device_config *device)
{
	assert( device != NULL );
	assert( device->token != NULL );
	assert( device->type == EEPROM );

	return (eeprom_state *)device->token;
}

/*
    eeprom_command_match:

    Try to match the first (len) digits in the EEPROM serial buffer
    string (*buf) with  an EEPROM command string (*cmd).
    Return non zero if a match was found.

    The serial buffer only contains '0' or '1' (e.g. "1001").
    The command can contain: '0' or '1' or these wildcards:

    'x' :   match both '0' and '1'
    "*1":   match "1", "01", "001", "0001" etc.
    "*0":   match "0", "10", "110", "1110" etc.

    Note: (cmd) may be NULL. Return 0 (no match) in this case.
*/
static int eeprom_command_match(const char *buf, const char *cmd, int len)
{
	if ( cmd == 0 )	return 0;
	if ( len == 0 )	return 0;

	for (;len>0;)
	{
		char b = *buf;
		char c = *cmd;

		if ((b==0) || (c==0))
			return (b==c);

		switch ( c )
		{
			case '0':
			case '1':
				if (b != c)	return 0;
			case 'X':
			case 'x':
				buf++;
				len--;
				cmd++;
				break;

			case '*':
				c = cmd[1];
				switch( c )
				{
					case '0':
					case '1':
					  	if (b == c)	{	cmd++;			}
						else		{	buf++;	len--;	}
						break;
					default:	return 0;
				}
		}
	}
	return (*cmd==0);
}


const eeprom_interface eepromdev_interface_93C46 =
{
	6,				// address bits 6
	16,				// data bits    16
	"*110",			// read         1 10 aaaaaa
	"*101",			// write        1 01 aaaaaa dddddddddddddddd
	"*111",			// erase        1 11 aaaaaa
	"*10000xxxx",	// lock         1 00 00xxxx
	"*10011xxxx",	// unlock       1 00 11xxxx
	1,
//  "*10001xxxx"    // write all    1 00 01xxxx dddddddddddddddd
//  "*10010xxxx"    // erase all    1 00 10xxxx
};

const eeprom_interface eepromdev_interface_93C66B =
{
	8,				/* address bits */
	16,				/* data bits */
	"*110",			/* read command */
	"*101",			/* write command */
	"*111",			/* erase command */
	"*10000xxxxxx",	/* lock command */
	"*10011xxxxxx", /* unlock command */
	1,
//  "*10001xxxxxx", /* write all */
//  "*10010xxxxxx", /* erase all */
};

static void eeprom_write(eeprom_state *c, int bit)
{
	LOG(("EEPROM write bit %d\n",bit));

	if (c->serial_count >= SERIAL_BUFFER_LENGTH-1)
	{
		logerror("error: EEPROM serial buffer overflow\n");
		return;
	}

	c->serial_buffer[c->serial_count++] = (bit ? '1' : '0');
	c->serial_buffer[c->serial_count] = 0;	/* nul terminate so we can treat it as a string */

	if ( (c->serial_count > c->intf->address_bits) &&
	      eeprom_command_match((char*)(c->serial_buffer),c->intf->cmd_read,strlen((char*)(c->serial_buffer))-c->intf->address_bits) )
	{
		int i,address;

		address = 0;
		for (i = c->serial_count-c->intf->address_bits;i < c->serial_count;i++)
		{
			address <<= 1;
			if (c->serial_buffer[i] == '1') address |= 1;
		}
		if (c->intf->data_bits == 16)
			c->eeprom_data_bits = (c->eeprom_data[2*address+0] << 8) + c->eeprom_data[2*address+1];
		else
			c->eeprom_data_bits = c->eeprom_data[address];
		c->eeprom_read_address = address;
		c->eeprom_clock_count = 0;
		c->sending = 1;
		c->serial_count = 0;
logerror("EEPROM read %04x from address %02x\n",c->eeprom_data_bits,address);
	}
	else if ( (c->serial_count > c->intf->address_bits) &&
	           eeprom_command_match((char*)(c->serial_buffer),c->intf->cmd_erase,strlen((char*)(c->serial_buffer))-c->intf->address_bits) )
	{
		int i,address;

		address = 0;
		for (i = c->serial_count-c->intf->address_bits;i < c->serial_count;i++)
		{
			address <<= 1;
			if (c->serial_buffer[i] == '1') address |= 1;
		}
logerror("EEPROM erase address %02x\n",address);
		if (c->locked == 0)
		{
			if (c->intf->data_bits == 16)
			{
				c->eeprom_data[2*address+0] = 0x00;
				c->eeprom_data[2*address+1] = 0x00;
			}
			else
				c->eeprom_data[address] = 0x00;
		}
		else
logerror("Error: EEPROM is locked\n");
		c->serial_count = 0;
	}
	else if ( (c->serial_count > (c->intf->address_bits + c->intf->data_bits)) &&
	           eeprom_command_match((char*)(c->serial_buffer),c->intf->cmd_write,strlen((char*)(c->serial_buffer))-(c->intf->address_bits + c->intf->data_bits)) )
	{
		int i,address,data;

		address = 0;
		for (i = c->serial_count-c->intf->data_bits-c->intf->address_bits;i < (c->serial_count-c->intf->data_bits);i++)
		{
			address <<= 1;
			if (c->serial_buffer[i] == '1') address |= 1;
		}
		data = 0;
		for (i = c->serial_count-c->intf->data_bits;i < c->serial_count;i++)
		{
			data <<= 1;
			if (c->serial_buffer[i] == '1') data |= 1;
		}
logerror("EEPROM write %04x to address %02x\n",data,address);
		if (c->locked == 0)
		{
			if (c->intf->data_bits == 16)
			{
				c->eeprom_data[2*address+0] = data >> 8;
				c->eeprom_data[2*address+1] = data & 0xff;
			}
			else
				c->eeprom_data[address] = data;
		}
		else
logerror("Error: EEPROM is locked\n");
		c->serial_count = 0;
	}
	else if ( eeprom_command_match((char*)(c->serial_buffer),c->intf->cmd_lock,strlen((char*)(c->serial_buffer))) )
	{
logerror("EEPROM lock\n");
		c->locked = 1;
		c->serial_count = 0;
	}
	else if ( eeprom_command_match((char*)(c->serial_buffer),c->intf->cmd_unlock,strlen((char*)(c->serial_buffer))) )
	{
logerror("EEPROM unlock\n");
		c->locked = 0;
		c->serial_count = 0;
	}
}

static void eeprom_reset(eeprom_state *c)
{
	if (c->serial_count)
		logerror("EEPROM reset, buffer = %s\n",c->serial_buffer);

	c->serial_count = 0;
	c->sending = 0;
	c->reset_delay = c->intf->reset_delay;	/* delay a little before returning setting data to 1 (needed by wbeachvl) */
}


void eepromdev_write_bit(const device_config *device, int bit)
{
	eeprom_state *c = get_safe_token(device);

	LOG(("write bit %d\n",bit));
	c->latch = bit;
}

int eepromdev_read_bit(const device_config *device)
{
	eeprom_state *c = get_safe_token(device);
	int res;

	if (c->sending)
		res = (c->eeprom_data_bits >> c->intf->data_bits) & 1;
	else
	{
		if (c->reset_delay > 0)
		{
			/* this is needed by wbeachvl */
			c->reset_delay--;
			res = 0;
		}
		else
			res = 1;
	}

	LOG(("read bit %d\n",res));

	return res;
}

/*CUSTOM_INPUT( eeprom_bit_r )
{
	return eeprom_read_bit();
}*/

void eepromdev_set_cs_line(const device_config *device, int state)
{
	eeprom_state *c = get_safe_token(device);

	LOG(("set reset line %d\n",state));
	c->reset_line = state;

	if (c->reset_line != CLEAR_LINE)
		eeprom_reset(c);
}

void eepromdev_set_clock_line(const device_config *device, int state)
{
	eeprom_state *c = get_safe_token(device);

	LOG(("set clock line %d\n",state));
	if (state == PULSE_LINE || (c->clock_line == CLEAR_LINE && state != CLEAR_LINE))
	{
		if (c->reset_line == CLEAR_LINE)
		{
			if (c->sending)
			{
				if (c->eeprom_clock_count == c->intf->data_bits && c->intf->enable_multi_read)
				{
					c->eeprom_read_address = (c->eeprom_read_address + 1) & ((1 << c->intf->address_bits) - 1);
					if (c->intf->data_bits == 16)
						c->eeprom_data_bits = (c->eeprom_data[2*c->eeprom_read_address+0] << 8) + c->eeprom_data[2*c->eeprom_read_address+1];
					else
						c->eeprom_data_bits = c->eeprom_data[c->eeprom_read_address];
					c->eeprom_clock_count = 0;
logerror("EEPROM read %04x from address %02x\n",c->eeprom_data_bits,c->eeprom_read_address);
				}
				c->eeprom_data_bits = (c->eeprom_data_bits << 1) | 1;
				c->eeprom_clock_count++;
			}
			else
				eeprom_write(c,c->latch);
		}
	}

	c->clock_line = state;
}


void eepromdev_load(const device_config *device, mame_file *f)
{
	eeprom_state *c = get_safe_token(device);

	mame_fread(f, c->eeprom_data, (1 << c->intf->address_bits) * c->intf->data_bits / 8);
}

void eepromdev_save(const device_config *device, mame_file *f)
{
	eeprom_state *c = get_safe_token(device);

	mame_fwrite(f, c->eeprom_data, (1 << c->intf->address_bits) * c->intf->data_bits / 8);
}

void eepromdev_set_data(const device_config *device, const UINT8 *data, int length)
{
	eeprom_state *c = get_safe_token(device);

	assert(length <= ((1 << c->intf->address_bits) * c->intf->data_bits / 8));
	memcpy(c->eeprom_data, data, length);
}

void *eepromdev_get_data_pointer(const device_config *device, UINT32 *length, UINT32 *size)
{
	eeprom_state *c = get_safe_token(device);

	if (length != NULL && c->intf != NULL)
		*length = 1 << c->intf->address_bits;
	if (size != NULL && c->intf != NULL)
		*size = c->intf->data_bits / 8;

	return c->eeprom_data;
}

static DEVICE_NVRAM( eeprom )
{
	const eeprom_config *config = device->inline_config;

	if (read_or_write)
		eepromdev_save(device, file);
	else
		if (file)	
			eepromdev_load(device, file);
		else
			if ((config->default_data != NULL) && (config->default_data_size != 0))
				eepromdev_set_data(device, config->default_data, config->default_data_size);
}

static DEVICE_START(eeprom)
{
	eeprom_state *c = get_safe_token(device);
	const eeprom_config *config;

	/* validate some basic stuff */
	assert(device != NULL);
	assert(device->inline_config != NULL);
	assert(device->machine != NULL);
	assert(device->machine->config != NULL);

	config = device->inline_config;

	c->intf = config->pinterface;

	if ((1 << c->intf->address_bits) * c->intf->data_bits / 8 > MEMORY_SIZE)
	{
		fatalerror("EEPROM larger than eepromdev.c allows");
	}

	memset(c->eeprom_data, 0xff, (1 << c->intf->address_bits) * c->intf->data_bits / 8);
	if ((config->default_data != NULL) && (config->default_data_size != 0))
		eepromdev_set_data(device, config->default_data, config->default_data_size);
	c->serial_count = 0;
	c->latch = 0;
	c->reset_line = ASSERT_LINE;
	c->clock_line = ASSERT_LINE;
	c->eeprom_read_address = 0;
	c->sending = 0;
	if (c->intf->cmd_unlock) c->locked = 1;
	else c->locked = 0;

	state_save_register_device_item_pointer( device, 0, c->eeprom_data, MEMORY_SIZE);
	state_save_register_device_item_pointer( device, 0, c->serial_buffer, SERIAL_BUFFER_LENGTH);
	state_save_register_device_item( device, 0, c->clock_line);
	state_save_register_device_item( device, 0, c->reset_line);
	state_save_register_device_item( device, 0, c->locked);
	state_save_register_device_item( device, 0, c->serial_count);
	state_save_register_device_item( device, 0, c->latch);
	state_save_register_device_item( device, 0, c->reset_delay);
	state_save_register_device_item( device, 0, c->eeprom_clock_count);
	state_save_register_device_item( device, 0, c->eeprom_data_bits);
	state_save_register_device_item( device, 0, c->eeprom_read_address);
}

static DEVICE_RESET(eeprom)
{
}

static DEVICE_SET_INFO(eeprom)
{
	switch (state)
	{
		/* no parameters to set */
	}
}

DEVICE_GET_INFO(eeprom)
{
	switch (state)
	{
		/* --- the following bits of info are returned as 64-bit signed integers --- */
		case DEVINFO_INT_TOKEN_BYTES:			info->i = sizeof(eeprom_state); break;
		case DEVINFO_INT_INLINE_CONFIG_BYTES:	info->i = sizeof(eeprom_config); break;
		case DEVINFO_INT_CLASS:					info->i = DEVICE_CLASS_PERIPHERAL; break;

		/* --- the following bits of info are returned as pointers to data or functions --- */
		case DEVINFO_FCT_SET_INFO:				info->set_info = DEVICE_SET_INFO_NAME(eeprom); break;
		case DEVINFO_FCT_START:					info->start = DEVICE_START_NAME(eeprom); break;
		case DEVINFO_FCT_STOP:					/* nothing */ break;
		case DEVINFO_FCT_RESET:					info->reset = DEVICE_RESET_NAME(eeprom); break;
		case DEVINFO_FCT_NVRAM:					info->nvram = DEVICE_NVRAM_NAME(eeprom); break;

		/* --- the following bits of info are returned as NULL-terminated strings --- */
		case DEVINFO_STR_NAME:					strcpy(info->s, "EEPROM"); break;
		case DEVINFO_STR_FAMILY:				strcpy(info->s, "EEPROM"); break;
		case DEVINFO_STR_VERSION:				strcpy(info->s, "1.0"); break;
		case DEVINFO_STR_SOURCE_FILE:			strcpy(info->s, __FILE__); break;
		case DEVINFO_STR_CREDITS:				strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
	}
}