summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/megadrive/eeprom.cpp
blob: 533d5d540fa63c7ae0f92ae6d1a9cf93e3feb0d2 (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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// license:BSD-3-Clause
// copyright-holders:Fabio Priuli
/***********************************************************************************************************


 MegaDrive / Genesis cart + I2C EEPROM emulation


 TODO: EEPROM hookup verification in most games.


 I2C games mapping table:

 Game Name                         |   SDA_IN   |  SDA_OUT   |     SCL    |  SIZE_MASK     | PAGE_MASK | WORKS |
 ----------------------------------|------------|------------|------------|----------------|-----------|-------|
 NBA Jam                           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x00ff (24C02) |   0x03    |       |
 NBA Jam TE                        | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    |       |
 NBA Jam TE (32x)                  | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    |       |
 NFL Quarterback Club              | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x00ff (24C02) |   0x03    |       |
 NFL Quarterback Club 96           | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x07ff (24C16) |   0x07    |       |
 College Slam                      | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    |       |
 Frank Thomas Big Hurt Baseball    | 0x200001-0 | 0x200001-0 | 0x200000-0 | 0x1fff (24C64) |   0x07    |       |
 NHLPA Hockey 93                   | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    |       |
 Rings of Power                    | 0x200001-7 | 0x200001-7 | 0x200001-6 | 0x007f (24C01) |   0x03    |       |
 Honoo no Toukyuuji - Dodge Danpei | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Unk. |
 Evander Holyfield's Boxing        | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Yes  |
 Greatest Heavyweights of the Ring | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Yes  |
 Wonder Boy V                      | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Yes  |
 Sports Talk Baseball              | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Yes  |
 Megaman - The Wily Wars           | 0x200001-0 | 0x200001-0 | 0x200001-1 | 0x007f (24C01) |   0x03    |  Yes  | **
 Micro Machines 2                  | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |  Yes  |
 Micro Machines Military           | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x03ff (24C08) |   0x0f    |  Yes  |
 Micro Machines 96                 | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x07ff (24C16) |   0x0f    |  Yes  |
 Brian Lara Cricket 96             | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x1fff (24C64) |   0x??*   |       |
 Shame Warne Cricket               | 0x380001-7 | 0x300000-0*| 0x300000-1*| 0x1fff (24C64) |   0x??*   |       |
 ----------------------------------|------------|------------|------------|----------------|-----------|-------|

 * Not specified in Eke-Eke's document
 ** Original Rockman Mega World (J) set uses normal backup RAM

 Micro Machines 2, Micro Machines Military, Micro Machines 96 are emulated in md_jcart

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



#include "emu.h"
#include "eeprom.h"


//-------------------------------------------------
//  md_rom_device - constructor
//-------------------------------------------------

DEFINE_DEVICE_TYPE(MD_STD_EEPROM,      md_std_eeprom_device,      "md_std_eeprom",      "MD Standard cart + EEPROM")
DEFINE_DEVICE_TYPE(MD_EEPROM_NBAJAM,   md_eeprom_nbajam_device,   "md_eeprom_nbajam",   "MD NBA Jam")
DEFINE_DEVICE_TYPE(MD_EEPROM_NBAJAMTE, md_eeprom_nbajamte_device, "md_eeprom_nbajamte", "MD NBA Jam TE") // and a few more
DEFINE_DEVICE_TYPE(MD_EEPROM_NFLQB,    md_eeprom_nflqb_device,    "md_eeprom_nflqb",    "MD NFL Quarterback 96")
DEFINE_DEVICE_TYPE(MD_EEPROM_CSLAM,    md_eeprom_cslam_device,    "md_eeprom_cslam",    "MD College Slam")
DEFINE_DEVICE_TYPE(MD_EEPROM_NHLPA,    md_eeprom_nhlpa_device,    "md_eeprom_nhlpa",    "MD NHLPA 93")
DEFINE_DEVICE_TYPE(MD_EEPROM_BLARA,    md_eeprom_blara_device,    "md_eeprom_blara",    "MD Brian Lara")
DEFINE_DEVICE_TYPE(MD_EEPROM_MODE1,    md_eeprom_mode1_device,    "md_eeprom_mode1",    "MD Serial EEPROM Mode 1")


md_std_eeprom_device::md_std_eeprom_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, type, tag, owner, clock)
	, device_md_cart_interface(mconfig, *this)
	, m_i2cmem(*this, "i2cmem"), m_i2c_mem(0), m_i2c_clk(0)
{
}

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

md_eeprom_nbajam_device::md_eeprom_nbajam_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_NBAJAM, tag, owner, clock)
{
}

md_eeprom_nbajamte_device::md_eeprom_nbajamte_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_NBAJAMTE, tag, owner, clock)
{
}

md_eeprom_nflqb_device::md_eeprom_nflqb_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_NFLQB, tag, owner, clock)
{
}

md_eeprom_cslam_device::md_eeprom_cslam_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_CSLAM, tag, owner, clock)
{
}

md_eeprom_nhlpa_device::md_eeprom_nhlpa_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_NHLPA, tag, owner, clock)
{
}

md_eeprom_blara_device::md_eeprom_blara_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_BLARA, tag, owner, clock)
{
}

md_eeprom_mode1_device::md_eeprom_mode1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_MODE1, tag, owner, clock)
{
}


//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

void md_std_eeprom_device::device_add_mconfig(machine_config &config)
{
	I2C_24C01(config, m_i2cmem);
}

void md_eeprom_nbajam_device::device_add_mconfig(machine_config &config)
{
	I2C_24C02(config, m_i2cmem);
}

void md_eeprom_nbajamte_device::device_add_mconfig(machine_config &config)
{
	I2C_24C01(config, m_i2cmem);
}

void md_eeprom_cslam_device::device_add_mconfig(machine_config &config)
{
	I2C_24C64(config, m_i2cmem);
}

void md_eeprom_nflqb_device::device_add_mconfig(machine_config &config)
{
	I2C_24C16(config, m_i2cmem);
}

void md_eeprom_nhlpa_device::device_add_mconfig(machine_config &config)
{
	I2C_24C01(config, m_i2cmem);
}

void md_eeprom_blara_device::device_add_mconfig(machine_config &config)
{
	I2C_24C64(config, m_i2cmem);
}

void md_eeprom_mode1_device::device_add_mconfig(machine_config &config)
{
	I2C_X24C01(config, m_i2cmem);
}

void md_std_eeprom_device::device_start()
{
	save_item(NAME(m_i2c_mem));
	save_item(NAME(m_i2c_clk));
}

void md_std_eeprom_device::device_reset()
{
	m_i2c_mem = 0;
	m_i2c_clk = 0;
}

/*-------------------------------------------------
 mapper specific handlers
 -------------------------------------------------*/

/*-------------------------------------------------
 CART + EEPROM
 -------------------------------------------------*/

uint16_t md_std_eeprom_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
		const uint8_t data = m_i2cmem->read_sda();
		return ((uint16_t)data << 8) | data;
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_std_eeprom_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		m_i2c_clk = BIT(data, 1);
		m_i2c_mem = BIT(data, 0);
		m_i2cmem->write_scl(m_i2c_clk);
		m_i2cmem->write_sda(m_i2c_mem);
	}
}

uint16_t md_eeprom_nbajam_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
//      m_i2c_mem = m_i2cmem->read_sda();
		return m_i2cmem->read_sda();
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_nbajam_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		m_i2c_clk = BIT(data, 1);
		m_i2c_mem = BIT(data, 0);
		m_i2cmem->write_scl(m_i2c_clk);
		m_i2cmem->write_sda(m_i2c_mem);
	}
}

uint16_t md_eeprom_nbajamte_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
//      m_i2c_mem = m_i2cmem->read_sda();
		return m_i2cmem->read_sda();
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_nbajamte_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		if(ACCESSING_BITS_8_15)
		{
			m_i2c_clk = BIT(data, 8);
			m_i2cmem->write_scl(m_i2c_clk);
		}

		if(ACCESSING_BITS_0_7)
		{
			m_i2c_mem = BIT(data, 0);
			m_i2cmem->write_sda(m_i2c_mem);
		}
	}
}

// same as NBAJAMTE above... derived class?
uint16_t md_eeprom_cslam_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
//      m_i2c_mem = m_i2cmem->read_sda();
		return m_i2cmem->read_sda();
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_cslam_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		if(ACCESSING_BITS_8_15)
		{
			m_i2c_clk = BIT(data, 8);
			m_i2cmem->write_scl(m_i2c_clk);
		}

		if(ACCESSING_BITS_0_7)
		{
			m_i2c_mem = BIT(data, 0);
			m_i2cmem->write_sda(m_i2c_mem);
		}
	}
}

// same as NBAJAMTE above... derived class?
uint16_t md_eeprom_nflqb_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
//      m_i2c_mem = i2cmem_sda_read(m_i2cmem);
		return m_i2cmem->read_sda();
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_nflqb_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		m_i2c_clk = BIT(data, 8);
		m_i2c_mem = BIT(data, 0);
		m_i2cmem->write_scl(m_i2c_clk);
		m_i2cmem->write_sda(m_i2c_mem);
	}
}

uint16_t md_eeprom_nhlpa_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
//      m_i2c_mem = m_i2cmem->read_sda();
		return (m_i2cmem->read_sda() & 1) << 7;
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_nhlpa_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		m_i2c_clk = BIT(data, 6);
		m_i2c_mem = BIT(data, 7);
		m_i2cmem->write_scl(m_i2c_clk);
		m_i2cmem->write_sda(m_i2c_mem);
	}
}

uint16_t md_eeprom_blara_device::read(offs_t offset)
{
	if (offset == 0x380000/2)
	{
//      m_i2c_mem = m_i2cmem->read_sda();
		return (m_i2cmem->read_sda() & 1) << 7;
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_blara_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x380000/2)
	{
		m_i2c_clk = BIT(data, 9);
		m_i2c_mem = BIT(data, 8);
		m_i2cmem->write_scl(m_i2c_clk);
		m_i2cmem->write_sda(m_i2c_mem);
	}
}

// TEMPORARY ADDITION UNTIL WE FIND OUT WHAT IS MISSING IN THE CORE X24C02 CODE
// THIS IS A CUSTOM I2C EEPROM EMULATION THAT ALLOWS NBA JAM TO WORK

DEFINE_DEVICE_TYPE(MD_EEPROM_NBAJAM_ALT, md_eeprom_nbajam_alt_device, "md_eeprom_nbajama", "MD NBA Jam (Alt)")

md_eeprom_nbajam_alt_device::md_eeprom_nbajam_alt_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: md_std_eeprom_device(mconfig, MD_EEPROM_NBAJAM_ALT, tag, owner, clock)
{
}

void md_eeprom_nbajam_alt_device::device_start()
{
	eeprom_i2c_init();
}


void md_eeprom_nbajam_alt_device::eeprom_i2c_init()
{
	m_eeprom_cnt = 0;
	m_eeprom_readwrite = 0;
	m_eeprom_slave_mask = 0;
	m_eeprom_word_address = 0;
	m_eeprom_devsel = 0;
	m_eeprom_byte = 0;

	m_eeprom_sda = m_eeprom_prev_sda = 1;
	m_eeprom_scl = m_eeprom_prev_scl = 1;
	m_eeprom_cur_state = STATE_I2C_IDLE;

	m_eeprom_mask = 0xff;
	m_eeprom_pagewrite_mask = 0x03;
}


// this is analogous to i2cmem.cpp implementation of write_sda
void md_eeprom_nbajam_alt_device::idle_devsel_check()
{
	if (m_eeprom_scl)
	{
		// All commands are preceded by the start condition, which is a HIGH to LOW
		// transition of SDA when SCL is HIGH
		if (m_eeprom_prev_sda && !m_eeprom_sda)
		{
			m_eeprom_cnt = 0;
			m_eeprom_slave_mask = 0;
			m_eeprom_cur_state = STATE_I2C_DEVSEL;
		}
		// All communications must be terminated by a stop condition, which is a LOW to HIGH
		// transition of SDA when SCL is HIGH
		else if (!m_eeprom_prev_sda && m_eeprom_sda)
		{
			m_eeprom_cur_state = STATE_I2C_IDLE;
		}
	}
}

void md_eeprom_nbajam_alt_device::eeprom_i2c_update(void)
{
	switch (m_eeprom_cur_state)
	{
		case STATE_I2C_IDLE:
			idle_devsel_check();
			break;

		case STATE_I2C_WAIT_STOP:
			if (!m_eeprom_prev_sda && m_eeprom_sda && m_eeprom_scl)
				m_eeprom_cur_state = STATE_I2C_IDLE;
			break;

		// device select: there can be up to 8 EEPROM in series, so that we start with writing 3 bits
		// to identify which device has to be accessed, followed by a Read/Write bit to specify the action
		case STATE_I2C_DEVSEL:
			idle_devsel_check();

			// LOW to HIGH transition of SCL = prepare to transmit, by moving to cnt = 1
			if (!m_eeprom_prev_scl && m_eeprom_scl)
			{
				if (m_eeprom_cnt <= 4)
				{
					// here we would transmit the Device Type Identifier which is 0101 for X24C02
					// but apparently the game does not check it, so let's skip it
				}
				else if ((m_eeprom_cnt > 4) && (m_eeprom_cnt < 8))
				{
					// here store the 3 bits of DEVICE ADDRESS
					m_eeprom_devsel = ((m_eeprom_devsel << 1) | m_eeprom_sda) & 0xff;
				}
				else if (m_eeprom_cnt == 8)
					m_eeprom_readwrite = m_eeprom_sda;
			}

			// HIGH to LOW transition of SCL = a new bit has been put on SDA line and we can read it
			// provided we already got the LOW to HIGH transition above
			if (m_eeprom_prev_scl && !m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_cnt++;
				else
				{
					// ACK
					m_eeprom_cnt = 1;
					m_eeprom_cur_state = m_eeprom_readwrite ? STATE_I2C_READ_DATA : STATE_I2C_GET_WORD_ADDR;
				}
			}
			break;

		// read operation = count 8 operations and at 9th check whether ACK has been issued
		case STATE_I2C_READ_DATA:
			idle_devsel_check();

			// HIGH to LOW transition of SCL
			if (m_eeprom_prev_scl && !m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_cnt++;
				else
					m_eeprom_cnt = 1;
			}

			// LOW to HIGH transition of SCL
			if (!m_eeprom_prev_scl && m_eeprom_scl)
			{
				if (m_eeprom_cnt == 9)
				{
					// no ACK
					if (m_eeprom_sda)
						m_eeprom_cur_state = STATE_I2C_WAIT_STOP;
				}
			}

			break;

		// For a write operation, the x24c02 requires a second address field. This address field is the
		// word address, comprised of eight bits, providing access to any one of the 256 words of memory.
		case STATE_I2C_GET_WORD_ADDR:
			idle_devsel_check();

			// HIGH to LOW transition of SCL
			if (m_eeprom_prev_scl && !m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_cnt++;
				else
				{
					// ACK
					m_eeprom_cnt = 1;
					m_eeprom_cur_state = STATE_I2C_WRITE_DATA;
					m_eeprom_byte = 0;
					m_eeprom_word_address &= m_eeprom_mask;
				}
			}

			// LOW to HIGH transition of SCL
			if (!m_eeprom_prev_scl && m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_word_address = ((m_eeprom_word_address << 1) | m_eeprom_sda) & 0xff;
			}
			break;

		// write operation
		case STATE_I2C_WRITE_DATA:
			idle_devsel_check();

			// HIGH to LOW transition of SCL
			if (m_eeprom_prev_scl && !m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_cnt++;
				else
					m_eeprom_cnt = 1;
			}

			// LOW to HIGH transition of SCL
			if (!m_eeprom_prev_scl && m_eeprom_scl)
			{
				if (m_eeprom_cnt < 9)
					m_eeprom_byte = ((m_eeprom_byte << 1) | m_eeprom_sda) & 0xff;
				else
				{
					uint8_t *nvram = (uint8_t *)&m_nvram[0];
					uint16_t sram_address = m_eeprom_slave_mask | (m_eeprom_devsel * 0x100) | m_eeprom_word_address;
					nvram[sram_address & 0xffff] = m_eeprom_byte;
					m_eeprom_byte = 0;

					//printf("Write EEPROM : status %d addr %x data %x (count 8)\n", m_eeprom_cur_state, sram_address, nvram[sram_address]);
					// WORD ADDRESS++
					m_eeprom_word_address = (m_eeprom_word_address & ~m_eeprom_pagewrite_mask) |
											((m_eeprom_word_address + 1) & m_eeprom_pagewrite_mask);
				}
			}
			break;
	}

	m_eeprom_prev_scl = m_eeprom_scl;
	m_eeprom_prev_sda = m_eeprom_sda;
	//printf("Write line : status %d SDA %x SCL %x (count %d)\n", m_eeprom_cur_state, m_eeprom_sda, m_eeprom_scl, m_eeprom_cnt);
}

uint8_t md_eeprom_nbajam_alt_device::eeprom_i2c_out()
{
	uint8_t res = m_eeprom_sda;

	switch (m_eeprom_cur_state)
	{
		case STATE_I2C_READ_DATA:
			if (m_eeprom_cnt < 9)
			{
				uint8_t *nvram = (uint8_t *)&m_nvram[0];
				uint16_t sram_address = m_eeprom_slave_mask | (m_eeprom_devsel * 0x100) | m_eeprom_word_address;
				sram_address &= 0xffff;

				res = (nvram[sram_address] >> (8 - m_eeprom_cnt)) & 1;

				if (m_eeprom_cnt == 8)
				{
					//printf("Read EEPROM : status %d addr %x data %x (count 8)\n", m_eeprom_cur_state, sram_address, nvram[sram_address]);
					// WORD ADDRESS++
					m_eeprom_word_address++;
					m_eeprom_word_address &= m_eeprom_mask;
				}
			}
			break;
		case STATE_I2C_DEVSEL:
		case STATE_I2C_GET_WORD_ADDR:
		case STATE_I2C_WRITE_DATA:
			if (m_eeprom_cnt == 9)
				res = 0;
			break;

		default:
			break;
	}

	//printf("Read line : status %d data %x (count %d)\n", m_eeprom_cur_state, res, m_eeprom_cnt);
	return res;
}


uint16_t md_eeprom_nbajam_alt_device::read(offs_t offset)
{
	if (offset == 0x200000/2)
	{
		return eeprom_i2c_out() << 1;
	}
	if (offset < 0x400000/2)
		return m_rom[MD_ADDR(offset)];
	else
		return 0xffff;
}

void md_eeprom_nbajam_alt_device::write(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	if (offset == 0x200000/2)
	{
		m_eeprom_sda = BIT(data, 0);
		m_eeprom_scl = BIT(data, 1);
		eeprom_i2c_update();
	}
}