summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/x76f041.cpp
blob: 340cf224800c28cd54487f8ca806c4692c71da81 (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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// license:BSD-3-Clause
// copyright-holders:smf
/*
 * x76f041.c
 *
 * Secure SerialFlash
 *
 * The X76F041 is a Password Access Security Supervisor, containing four 128 x 8 bit SecureFlash arrays.
 * Access can be controlled by three 64-bit programmable passwords, one for read operations, one for write
 * operations and one for device configuration.
 *
 * The data sheet has an incorrect diagrams for sequential read with password, there shouldn't be an extra address after the 0xc0 command.
 *
 */

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

#include <cstdarg>

#define VERBOSE_LEVEL ( 0 )

inline void ATTR_PRINTF( 3, 4 ) x76f041_device::verboselog( int n_level, const char *s_fmt, ... )
{
	if( VERBOSE_LEVEL >= n_level )
	{
		va_list v;
		char buf[ 32768 ];
		va_start( v, s_fmt );
		vsprintf( buf, s_fmt, v );
		va_end( v );
		logerror( "%s: x76f041(%s) %s", machine().describe_context(), tag(), buf );
	}
}

// device type definition
DEFINE_DEVICE_TYPE(X76F041, x76f041_device, "x76f041", "X76F041 Secure SerialFlash")

x76f041_device::x76f041_device( const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock )
	: device_t( mconfig, X76F041, tag, owner, clock ),
	device_nvram_interface(mconfig, *this),
	m_region(*this, DEVICE_SELF),
	m_cs( 0 ),
	m_rst( 0 ),
	m_scl( 0 ),
	m_sdaw( 0 ),
	m_sdar( 0 ),
	m_state( STATE_STOP ),
	m_shift( 0 ),
	m_bit( 0 ),
	m_byte( 0 ),
	m_command( 0 ),
	m_address( 0 ),
	m_is_password_accepted ( false )
{
}

void x76f041_device::device_start()
{
	std::fill( std::begin( m_write_buffer ), std::end( m_write_buffer ), 0 );
	std::fill( std::begin( m_password_temp ), std::end( m_password_temp ), 0 );

	save_item( NAME( m_cs ) );
	save_item( NAME( m_rst ) );
	save_item( NAME( m_scl ) );
	save_item( NAME( m_sdaw ) );
	save_item( NAME( m_sdar ) );
	save_item( NAME( m_state ) );
	save_item( NAME( m_shift ) );
	save_item( NAME( m_bit ) );
	save_item( NAME( m_byte ) );
	save_item( NAME( m_command ) );
	save_item( NAME( m_address ) );
	save_item( NAME( m_is_password_accepted ) );
	save_item( NAME( m_write_buffer ) );
	save_item( NAME( m_response_to_reset ) );
	save_item( NAME( m_write_password ) );
	save_item( NAME( m_read_password ) );
	save_item( NAME( m_configuration_password ) );
	save_item( NAME( m_configuration_registers ) );
	save_item( NAME( m_data ) );
	save_item( NAME( m_password_temp ) );
}

void x76f041_device::device_reset()
{
	std::fill( std::begin( m_write_buffer ), std::end( m_write_buffer ), 0 );
	std::fill( std::begin( m_password_temp ), std::end( m_password_temp ), 0 );

	m_cs = 0;
	m_rst = 0;
	m_scl = 0;
	m_sdaw = 0;
	m_sdar = 0;
	m_state = STATE_STOP;
	m_shift = 0;
	m_bit = 0;
	m_byte = 0;
	m_command = 0;
	m_address = 0;
	m_is_password_accepted = false;
}

WRITE_LINE_MEMBER( x76f041_device::write_cs )
{
	if( m_cs != state )
	{
		verboselog( 2, "cs=%d\n", state );
	}

	if( m_cs != 0 && state == 0 )
	{
		/* enable chip */
		m_state = STATE_STOP;
	}

	if( m_cs == 0 && state != 0 )
	{
		/* disable chip */
		m_state = STATE_STOP;
		/* high impendence? */
		m_sdar = 0;
	}

	m_cs = state;
}

WRITE_LINE_MEMBER( x76f041_device::write_rst )
{
	if( m_rst != state )
	{
		verboselog( 2, "rst=%d\n", state );
	}

	if( m_rst == 0 && state != 0 && m_cs == 0 )
	{
		verboselog( 1, "goto response to reset\n" );
		m_state = STATE_RESPONSE_TO_RESET;
		m_bit = 0;
		m_byte = 0;
	}

	m_rst = state;
}

uint8_t *x76f041_device::password()
{
	switch( m_command & 0xe0 )
	{
	case COMMAND_WRITE:
		return m_write_password;

	case COMMAND_READ:
		return m_read_password;

	case COMMAND_CONFIGURATION:
		if( m_address == CONFIGURATION_PROGRAM_WRITE_PASSWORD )
			return m_write_password;

		if( m_address == CONFIGURATION_PROGRAM_READ_PASSWORD )
			return m_read_password;

		return m_configuration_password;

	default:
		return m_configuration_password;
	}
}

void x76f041_device::password_ok()
{
	if( m_configuration_registers[ CONFIG_CR ] & CR_RETRY_COUNTER_RESET_BIT )
		m_configuration_registers[ CONFIG_RC ] = 0;

	switch( m_command & 0xe0 )
	{
	case COMMAND_WRITE:
		m_state = STATE_WRITE_DATA;
		break;
	case COMMAND_READ:
		m_state = STATE_READ_DATA;
		break;
	case COMMAND_WRITE_USE_CONFIGURATION_PASSWORD:
		m_state = STATE_CONFIGURATION_WRITE_DATA;
		break;
	case COMMAND_READ_USE_CONFIGURATION_PASSWORD:
		m_state = STATE_READ_DATA;
		break;
	case COMMAND_CONFIGURATION:
		switch( m_address )
		{
		case CONFIGURATION_PROGRAM_WRITE_PASSWORD:
			m_state = STATE_PROGRAM_WRITE_PASSWORD;
			m_byte = 0;
			break;
		case CONFIGURATION_PROGRAM_READ_PASSWORD:
			m_state = STATE_PROGRAM_READ_PASSWORD;
			m_byte = 0;
			break;
		case CONFIGURATION_PROGRAM_CONFIGURATION_PASSWORD:
			m_state = STATE_PROGRAM_CONFIGURATION_PASSWORD;
			m_byte = 0;
			break;
		case CONFIGURATION_RESET_WRITE_PASSWORD:
			m_state = STATE_RESET_WRITE_PASSWORD;
			break;
		case CONFIGURATION_RESET_READ_PASSWORD:
			m_state = STATE_RESET_READ_PASSWORD;
			break;
		case CONFIGURATION_PROGRAM_CONFIGURATION_REGISTERS:
			m_state = STATE_WRITE_CONFIGURATION_REGISTERS;
			m_byte = 0;
			break;
		case CONFIGURATION_READ_CONFIGURATION_REGISTERS:
			m_state = STATE_READ_CONFIGURATION_REGISTERS;
			m_byte = 0;
			break;
		case CONFIGURATION_MASS_PROGRAM:
			m_state = STATE_MASS_PROGRAM;
			break;
		case CONFIGURATION_MASS_ERASE:
			m_state = STATE_MASS_ERASE;
			break;
		default:
			break;
		}
	}
}

void x76f041_device::load_address()
{
	m_address = m_shift;

	verboselog( 1, "-> address: %02x\n", m_address );

	if( ( m_configuration_registers[ CONFIG_CR ] & CR_RETRY_COUNTER_ENABLE_BIT ) != 0 &&
		m_configuration_registers[ CONFIG_RR ] == m_configuration_registers[ CONFIG_RC ] &&
		( m_configuration_registers[ CONFIG_CR ] & CR_UNAUTHORIZED_ACCESS_BITS ) == 0x80 )
	{
		// No commands are allowed
		verboselog( 1, "unauthorized access rejected\n" );
		m_state = STATE_STOP;
		m_sdar = 1;
		m_byte = 0;
		return;
	}

	if( ( m_command & 0xe0 ) == COMMAND_CONFIGURATION )
	{
		// Configuration commands can be used regardless of array control register bits
		if( m_address == CONFIGURATION_RESET_WRITE_PASSWORD ||
			m_address == CONFIGURATION_RESET_READ_PASSWORD ||
			m_address == CONFIGURATION_MASS_PROGRAM ||
			m_address == CONFIGURATION_MASS_ERASE )
		{
			verboselog( 1, "password not required\n" );
			password_ok();
		}
		else
		{
			verboselog( 1, "send password\n" );
			m_state = STATE_LOAD_PASSWORD;
			m_byte = 0;
		}

		return;
	}

	if( ( m_configuration_registers[ CONFIG_CR ] & CR_RETRY_COUNTER_ENABLE_BIT ) != 0 &&
		m_configuration_registers[ CONFIG_RR ] == m_configuration_registers[ CONFIG_RC ] &&
		( m_configuration_registers[ CONFIG_CR ] & CR_UNAUTHORIZED_ACCESS_BITS ) != 0x80 )
	{
		// Only configuration commands are allowed
		verboselog( 1, "unauthorized access rejected\n" );
		m_state = STATE_STOP;
		m_sdar = 1;
		m_byte = 0;
		return;
	}

	int bcr = m_configuration_registers[ ( m_command & 1 ) ? CONFIG_BCR2 : CONFIG_BCR1 ];
	if( ( m_address & 0x80 ) != 0 )
	{
		bcr >>= 4;
	}

	if( ( ( m_command & 0xe0 ) == COMMAND_READ && ( bcr & BCR_Z ) != 0 && ( bcr & BCR_T ) != 0 ) ||
		( ( m_command & 0xe0 ) == COMMAND_WRITE && ( bcr & BCR_Z ) != 0 ) )
	{
		/* todo: find out when this is really checked. */
		verboselog( 1, "command not allowed\n" );
		m_state = STATE_STOP;
		m_sdar = 1;
		m_byte = 0;
	}
	else if( ( ( m_command & 0xe0 ) == COMMAND_WRITE && ( bcr & BCR_X ) == 0 ) ||
		( ( m_command & 0xe0 ) == COMMAND_READ && ( bcr & BCR_Y ) == 0 ) )
	{
		verboselog( 1, "password not required\n" );
		password_ok();
	}
	else
	{
		verboselog( 1, "send password\n" );
		m_state = STATE_LOAD_PASSWORD;
		m_byte = 0;
	}
}

int x76f041_device::data_offset()
{
	int block_offset = ( ( m_command & 1 ) << 8 ) + m_address;

	// TODO: confirm block_start doesn't wrap.

	return ( block_offset & 0x180 ) | ( ( block_offset + m_byte ) & 0x7f );
}

WRITE_LINE_MEMBER( x76f041_device::write_scl )
{
	if( m_scl != state )
	{
		verboselog( 2, "scl=%d\n", state );
	}

	if( m_cs == 0 )
	{
		switch( m_state )
		{
		case STATE_STOP:
			break;

		case STATE_RESPONSE_TO_RESET:
			if( m_scl != 0 && state == 0 )
			{
				m_sdar = ( m_response_to_reset[ m_byte ] >> m_bit ) & 1;
				verboselog( 2, "in response to reset %d (%d/%d)\n", m_sdar, m_byte, m_bit );
				m_bit++;

				if( m_bit == 8 )
				{
					m_bit = 0;
					m_byte++;

					if( m_byte == sizeof( m_response_to_reset ) )
					{
						m_byte = 0;
					}
				}
			}
			break;

		case STATE_LOAD_COMMAND:
		case STATE_LOAD_ADDRESS:
		case STATE_LOAD_PASSWORD:
		case STATE_VERIFY_PASSWORD:
		case STATE_WRITE_DATA:
		case STATE_CONFIGURATION_WRITE_DATA:
		case STATE_WRITE_CONFIGURATION_REGISTERS:
		case STATE_PROGRAM_WRITE_PASSWORD:
		case STATE_PROGRAM_READ_PASSWORD:
		case STATE_PROGRAM_CONFIGURATION_PASSWORD:
		case STATE_RESET_WRITE_PASSWORD:
		case STATE_RESET_READ_PASSWORD:
		case STATE_MASS_PROGRAM:
		case STATE_MASS_ERASE:
			// FIXME: Processing on the rising edge of the clock causes sda to change state while clock is high
			// which is not allowed. Also need to ensure that only valid device-id's and commands
			// are acknowledged.
			if( m_scl == 0 && state != 0 )
			{
				if( m_bit < 8 )
				{
					verboselog( 2, "clock\n" );
					m_shift <<= 1;

					if( m_sdaw != 0 )
					{
						m_shift |= 1;
					}

					m_bit++;
				}
				else
				{
					m_sdar = 0;

					switch( m_state )
					{
					case STATE_LOAD_COMMAND:
						m_command = m_shift;
						verboselog( 1, "-> command: %02x\n", m_command );
						/* todo: verify command is valid? */
						m_state = STATE_LOAD_ADDRESS;
						break;

					case STATE_LOAD_ADDRESS:
						load_address();
						break;

					case STATE_LOAD_PASSWORD:
						verboselog( 1, "-> password: %02x\n", m_shift );
						m_write_buffer[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_write_buffer ) )
						{
							m_state = STATE_VERIFY_PASSWORD;

							// Perform the password acceptance check before verify password because
							// password verify ack is spammed and will quickly overflow the
							// retry counter.
							m_is_password_accepted = memcmp( password(), m_write_buffer, sizeof( m_write_buffer ) ) == 0;
							if( !m_is_password_accepted )
							{
								if( m_configuration_registers[ CONFIG_CR ] & CR_RETRY_COUNTER_ENABLE_BIT )
									m_configuration_registers[ CONFIG_RC ]++;
							}
						}
						break;

					case STATE_VERIFY_PASSWORD:
						verboselog( 1, "-> verify password: %02x\n", m_shift );

						/* todo: this should probably be handled as a command */
						if( m_shift == 0xc0 )
						{
							/* todo: this should take 10ms before it returns ok. */
							if( m_is_password_accepted )
							{
								password_ok();
							}
							else
							{
								m_sdar = 1;
							}
						}
						break;

					case STATE_WRITE_DATA:
						verboselog( 2, "-> data: %02x\n", m_shift );
						m_write_buffer[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_write_buffer ) )
						{
							int bcr = m_configuration_registers[ ( m_command & 1 ) ? CONFIG_BCR2 : CONFIG_BCR1 ];
							if( ( m_address & 0x80 ) != 0 )
							{
								bcr >>= 4;
							}

							if( ( bcr & ( BCR_Z | BCR_T ) ) == BCR_T )
							{
								// Bits in the data can only be set, not cleared, when in program only mode
								bool is_unauthorized_write = false;

								for( m_byte = 0; m_byte < sizeof( m_write_buffer ); m_byte++ )
								{
									int offset = data_offset();
									if( m_write_buffer[ m_byte ] < m_data[ offset ] )
									{
										verboselog( 1, "tried to unset bits while in program only mode\n" );
										is_unauthorized_write = true;
										break;
									}
								}

								if( is_unauthorized_write )
								{
									m_sdar = 1;
									m_byte = 0;
									break;
								}
							}

							for( m_byte = 0; m_byte < sizeof( m_write_buffer ); m_byte++ )
							{
								int offset = data_offset();
								verboselog( 1, "-> data[ %03x ]: %02x\n", offset, m_write_buffer[ m_byte ] );
								m_data[ offset ] = m_write_buffer[ m_byte ];
							}
							m_byte = 0;

							verboselog( 1, "data flushed\n" );
						}
						break;


					case STATE_CONFIGURATION_WRITE_DATA:
						// Unlike normal writes, configuration writes aren't required to be exactly 8 bytes
						// TODO: Store data in a temporary buffer until the proper end of the command before writing
						verboselog( 2, "-> data: %02x\n", m_shift );
						m_data[ data_offset() ] = m_shift;
						m_byte++;
						break;

					case STATE_WRITE_CONFIGURATION_REGISTERS:
						verboselog( 1, "-> configuration register[ %d ]: %02x\n", m_byte, m_shift );
						/* todo: write after all bytes received? */
						m_configuration_registers[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_configuration_registers ) )
						{
							m_byte = 0;
						}
						break;

					case STATE_PROGRAM_WRITE_PASSWORD:
						verboselog( 1, "-> program write password[ %d ]: %02x\n", m_byte, m_shift );
						m_password_temp[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_password_temp ) )
						{
							// Read in the password twice and if the two copies match then write it to the password field
							if( memcmp( &m_password_temp[ 0 ], &m_password_temp[ 8 ], sizeof( m_write_password ) ) == 0 )
							{
								std::copy_n( std::begin( m_password_temp ), sizeof( m_write_password ), std::begin ( m_write_password ) );
							}
							else {
								m_sdar = 1;
							}

							std::fill( std::begin( m_password_temp ), std::end( m_password_temp ), 0 );

							m_byte = 0;
						}
						break;

					case STATE_PROGRAM_READ_PASSWORD:
						verboselog( 1, "-> program read password[ %d ]: %02x\n", m_byte, m_shift );
						m_password_temp[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_password_temp ) )
						{
							if( memcmp( &m_password_temp[ 0 ], &m_password_temp[ 8 ], sizeof( m_read_password ) ) == 0 )
							{
								std::copy_n( std::begin( m_password_temp ), sizeof( m_read_password ), std::begin ( m_read_password ) );
							}
							else {
								m_sdar = 1;
							}

							std::fill( std::begin( m_password_temp ), std::end( m_password_temp ), 0 );

							m_byte = 0;
						}
						break;

					case STATE_PROGRAM_CONFIGURATION_PASSWORD:
						verboselog( 1, "-> program configuration password[ %d ]: %02x\n", m_byte, m_shift );
						m_password_temp[ m_byte++ ] = m_shift;

						if( m_byte == sizeof( m_password_temp ) )
						{
							if( memcmp( &m_password_temp[ 0 ], &m_password_temp[ 8 ], sizeof( m_configuration_password ) ) == 0 )
							{
								std::copy_n( std::begin( m_password_temp ), sizeof( m_configuration_password ), std::begin ( m_configuration_password ) );
							}
							else {
								m_sdar = 1;
							}

							std::fill( std::begin( m_password_temp ), std::end( m_password_temp ), 0 );

							m_byte = 0;
						}
						break;

					case STATE_RESET_WRITE_PASSWORD:
						verboselog( 1, "-> reset write password\n" );
						std::fill( std::begin( m_write_password ), std::end( m_write_password ), 0 );
						break;

					case STATE_RESET_READ_PASSWORD:
						verboselog( 1, "-> reset read password\n" );
						std::fill( std::begin( m_read_password ), std::end( m_read_password ), 0 );
						break;

					case STATE_MASS_PROGRAM:
					case STATE_MASS_ERASE:
					{
						const uint8_t fill = m_state == STATE_MASS_ERASE ? 0xff : 0;
						verboselog( 1, "-> mass erase %02x\n", fill );
						std::fill( std::begin( m_data ), std::end( m_data ), fill );
						std::fill( std::begin( m_configuration_password ), std::end( m_configuration_password ), fill );
						std::fill( std::begin( m_configuration_registers ), std::end( m_configuration_registers ), fill );
						std::fill( std::begin( m_write_password ), std::end( m_write_password ), fill );
						std::fill( std::begin( m_read_password ), std::end( m_read_password ), fill );
						break;
					}
					}

					m_bit = 0;
					m_shift = 0;
				}
			}
			break;

		case STATE_READ_DATA:
		case STATE_READ_CONFIGURATION_REGISTERS:
			// FIXME: Processing on the rising edge of the clock causes sda to change state while clock is high
			// which is not allowed.
			if( m_scl == 0 && state != 0 )
			{
				if( m_bit < 8 )
				{
					if( m_bit == 0 )
					{
						int offset;

						switch( m_state )
						{
						case STATE_READ_DATA:
							offset = data_offset();
							m_shift = m_data[ offset ];
							verboselog( 1, "<- data[ %03x ]: %02x\n", offset, m_shift );
							break;

						case STATE_READ_CONFIGURATION_REGISTERS:
							offset = m_byte & 7;
							m_shift = m_configuration_registers[ offset ];
							verboselog( 1, "<- configuration register[ %d ]: %02x\n", offset, m_shift );
							break;
						}
					}

					m_sdar = ( m_shift >> 7 ) & 1;
					m_shift <<= 1;
					m_bit++;
				}
				else
				{
					m_bit = 0;
					m_sdar = 0;

					if( m_sdaw == 0 )
					{
						verboselog( 2, "ack <-\n" );
						m_byte++;
					}
					else
					{
						verboselog( 2, "nak <-\n" );
					}
				}
			}
			break;
		}
	}

	m_scl = state;
}

WRITE_LINE_MEMBER( x76f041_device::write_sda )
{
	if( m_sdaw != state )
	{
		verboselog( 2, "sdaw=%d\n", state );
	}

	if( m_cs == 0 && m_scl != 0 )
	{
		if( m_sdaw == 0 && state != 0 )
		{
			verboselog( 1, "goto stop\n" );
			m_state = STATE_STOP;
			m_sdar = 0;
		}

		if( m_sdaw != 0 && state == 0 )
		{
			switch( m_state )
			{
			case STATE_STOP:
				verboselog( 1, "goto start\n" );
				m_state = STATE_LOAD_COMMAND;
				break;

			case STATE_LOAD_PASSWORD:
				/* todo: this will be the 0xc0 command, but it's not handled as a command yet. */
				verboselog( 1, "goto start\n" );
				break;

			case STATE_READ_DATA:
				verboselog( 1, "goto load address\n" );
				m_state = STATE_LOAD_ADDRESS;
				break;

			default:
				verboselog( 1, "skipped start (default)\n" );
				break;
			}

			m_bit = 0;
			m_byte = 0;
			m_shift = 0;
			m_sdar = 0;
		}
	}

	m_sdaw = state;
}

READ_LINE_MEMBER( x76f041_device::read_sda )
{
	if( m_cs != 0 )
	{
		verboselog( 2, "not selected\n" );
		return 1;
	}

	verboselog( 2, "sdar=%d\n", m_sdar );
	return m_sdar;
}

void x76f041_device::nvram_default()
{
	m_response_to_reset[0] = 0x19;
	m_response_to_reset[1] = 0x55;
	m_response_to_reset[2] = 0xaa;
	m_response_to_reset[3] = 0x55,

	memset( m_write_password, 0, sizeof( m_write_password ) );
	memset( m_read_password, 0, sizeof( m_read_password ) );
	memset( m_configuration_password, 0, sizeof( m_configuration_password ) );
	memset( m_configuration_registers, 0, sizeof( m_configuration_registers ) );
	memset( m_data, 0, sizeof( m_data ) );

	int expected_bytes = sizeof( m_response_to_reset ) + sizeof( m_write_password ) + sizeof( m_read_password ) +
		sizeof( m_configuration_password ) + sizeof( m_configuration_registers ) + sizeof( m_data );

	if (!m_region.found())
	{
		logerror( "x76f041(%s) region not found\n", tag() );
	}
	else if( m_region->bytes() != expected_bytes )
	{
		logerror( "x76f041(%s) region length 0x%x expected 0x%x\n", tag(), m_region->bytes(), expected_bytes );
	}
	else
	{
		uint8_t *region = m_region->base();

		memcpy( m_response_to_reset, region, sizeof( m_response_to_reset ) ); region += sizeof( m_response_to_reset );
		memcpy( m_write_password, region, sizeof( m_write_password ) ); region += sizeof( m_write_password );
		memcpy( m_read_password, region, sizeof( m_read_password ) ); region += sizeof( m_read_password );
		memcpy( m_configuration_password, region, sizeof( m_configuration_password ) ); region += sizeof( m_configuration_password );
		memcpy( m_configuration_registers, region, sizeof( m_configuration_registers ) ); region += sizeof( m_configuration_registers );
		memcpy( m_data, region, sizeof( m_data ) ); region += sizeof( m_data );
	}
}

bool x76f041_device::nvram_read( util::read_stream &file )
{
	size_t actual;
	bool result = !file.read( m_response_to_reset, sizeof( m_response_to_reset ), actual ) && actual == sizeof( m_response_to_reset );
	result = result && !file.read( m_write_password, sizeof( m_write_password ), actual ) && actual == sizeof( m_write_password );
	result = result && !file.read( m_read_password, sizeof( m_read_password ), actual ) && actual == sizeof( m_read_password );
	result = result && !file.read( m_configuration_password, sizeof( m_configuration_password ), actual ) && actual == sizeof( m_configuration_password );
	result = result && !file.read( m_configuration_registers, sizeof( m_configuration_registers ), actual ) && actual == sizeof( m_configuration_registers );
	result = result && !file.read( m_data, sizeof( m_data ), actual ) && actual == sizeof( m_data );
	return result;
}

bool x76f041_device::nvram_write( util::write_stream &file )
{
	size_t actual;
	bool result = !file.write( m_response_to_reset, sizeof( m_response_to_reset ), actual ) && actual == sizeof( m_response_to_reset );
	result = result && !file.write( m_write_password, sizeof( m_write_password ), actual ) && actual == sizeof( m_write_password );
	result = result && !file.write( m_read_password, sizeof( m_read_password ), actual ) && actual == sizeof( m_read_password );
	result = result && !file.write( m_configuration_password, sizeof( m_configuration_password ), actual ) && actual == sizeof( m_configuration_password );
	result = result && !file.write( m_configuration_registers, sizeof( m_configuration_registers ), actual ) && actual == sizeof( m_configuration_registers );
	result = result && !file.write( m_data, sizeof( m_data ), actual ) && actual == sizeof( m_data );
	return result;
}