summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/er59256.c
blob: 018fe9d08829f85587839a07f888f3e1df1bdc3b (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
/*********************************************************************

    er59256.c

    Microchip ER59256 serial eeprom.


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

#include "emu.h"
#include "er59256.h"

/* LOGLEVEL 0=no logging, 1=just commands and data, 2=everything ! */

#define LOGLEVEL            0

#define LOG(level,...)      if(LOGLEVEL>=level) logerror(__VA_ARGS__)
#define LOG_BITS(bits)      logerror("CS=%d CK=%d DI=%d DO=%d", (bits&CS_MASK) ? 1 : 0, (bits&CK_MASK) ? 1 : 0, (bits&DI_MASK) ? 1 : 0, (bits&DO_MASK) ? 1 : 0)

const device_type ER59256 = &device_creator<er59256_device>;

er59256_device::er59256_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, ER59256, "Microchip ER59256 serial eeprom.", tag, owner, clock, "er59256", __FILE__),
	m_io_bits(0),
	m_old_io_bits(0),
	m_in_shifter(0),
	m_out_shifter(0),
	m_bitcount(0),
	m_command(0),
	m_flags(0)
{
}

//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void er59256_device::device_config_complete()
{
}

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

void er59256_device::device_start()
{
	// Start with rom defaulted to erased
	memset(&m_eerom, 0xFF, EEROM_WORDS*2);

	m_command=CMD_INVALID;

	m_flags&= ~FLAG_DATA_LOADED;

	save_item(NAME(m_eerom));
	save_item(NAME(m_io_bits));
	save_item(NAME(m_old_io_bits));
	save_item(NAME(m_in_shifter));
	save_item(NAME(m_out_shifter));
	save_item(NAME(m_bitcount));
	save_item(NAME(m_command));
	save_item(NAME(m_flags));
}

//-------------------------------------------------
//  device_stop - device-specific stop
//-------------------------------------------------

void er59256_device::device_stop()
{
	/* Save contents of eerom */
}

/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

void er59256_device::preload_rom(const UINT16 *rom_data, int count)
{
	int WordNo;

	logerror("Preloading %d words of data\n",count);

	if(count>EEROM_WORDS)
		memcpy(&m_eerom,rom_data,count*2);
	else
		memcpy(&m_eerom,rom_data,EEROM_WORDS*2);

	for(WordNo=0;WordNo<EEROM_WORDS;WordNo++)
		logerror("%04X ",m_eerom[WordNo]);

	logerror("\n");
}

UINT8 er59256_device::data_loaded()
{
	return (m_flags & FLAG_DATA_LOADED) ? 1 : 0;
}

void er59256_device::set_iobits(UINT8 newbits)
{
	//UINT32  bit;

	// Make sure we only apply valid bits
	newbits&=ALL_MASK;

	if(LOGLEVEL>1)
	{
		logerror("er59256:newbits=%02X : ",newbits);
		LOG_BITS(newbits);
		logerror(" io_bits=%02X : ",m_io_bits);
		LOG_BITS(m_io_bits);
		logerror(" old_io_bits=%02X : ",m_old_io_bits);
		LOG_BITS(m_old_io_bits);
		logerror(" bitcount=%d, in_shifter=%04X, out_shifter=%05X, flags=%02X\n",m_bitcount,m_in_shifter,m_out_shifter,m_flags);
	}
	// Only do anything if the inputs have changed
	if((newbits&IN_MASK)!=(m_io_bits&IN_MASK))
	{
		// save the current state, then set the new one, remembering to preserve data out
		m_old_io_bits=m_io_bits;
		m_io_bits=(newbits & ~DO_MASK) | (m_old_io_bits&DO_MASK);

		if(CS_RISE())
		{
			m_flags&=~FLAG_START_BIT;
			m_command=CMD_INVALID;
		}

		if(LOGLEVEL>1)
		{
			if(CK_RISE()) logerror("er59256:CK rise\n");
			if(CS_RISE()) logerror("er59256:CS rise\n");
			if(CK_FALL()) logerror("er59256:CK fall\n");
			if(CS_FALL()) logerror("er59256:CS fall\n");
		}

		if(CK_RISE() && CS_VALID())
		{
			if((STARTED()==0) && (GET_DI()==1))
			{
				m_bitcount=0;
				m_flags|=FLAG_START_BIT;
			}
			else
			{
				SHIFT_IN();
				m_bitcount++;

				if(m_bitcount==CMD_BITLEN)
					decode_command();

				if((m_bitcount==WRITE_BITLEN) && ((m_command & CMD_MASK)==CMD_WRITE))
				{
					m_eerom[m_command & ADDR_MASK]=m_in_shifter;
					LOG(1,"er59256:write[%02X]=%04X\n",(m_command & ADDR_MASK),m_in_shifter);
					m_command=CMD_INVALID;
				}
				LOG(1,"out_shifter=%05X, io_bits=%02X\n",m_out_shifter,m_io_bits);
				SHIFT_OUT();
			}

			LOG(2,"io_bits:out=%02X\n",m_io_bits);
		}
	}
}

UINT8 er59256_device::get_iobits()
{
	return m_io_bits;
}


void er59256_device::decode_command()
{
	m_out_shifter=0x0000;
	m_command=(m_in_shifter & (CMD_MASK | ADDR_MASK));

	switch(m_command & CMD_MASK)
	{
		case CMD_READ   : m_out_shifter=m_eerom[m_command & ADDR_MASK];
							LOG(1,"er59256:read[%02X]=%04X\n",(m_command&ADDR_MASK),m_eerom[m_command & ADDR_MASK]);
							break;
		case CMD_WRITE  : break;
		case CMD_ERASE  : if (WRITE_ENABLED()) m_eerom[m_command & ADDR_MASK]=0xFF;
							LOG(1,"er59256:erase[%02X]\n",(m_command&ADDR_MASK));
							break;
		case CMD_EWEN   : m_flags|=FLAG_WRITE_EN;
							LOG(1,"er59256:erase/write enabled\n");
							break;
		case CMD_EWDS   : m_flags&=~FLAG_WRITE_EN;
							LOG(1,"er59256:erase/write disabled\n");
							break;
		case CMD_ERAL   : if (WRITE_ENABLED()) memset(&m_eerom, 0xFF, EEROM_WORDS*2);
							LOG(1,"er59256:erase all\n");
							break;
	}

	if ((m_command & CMD_MASK)!=CMD_WRITE)
		m_command=CMD_INVALID;
}