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
|
/**********************************************************************
National Semiconductor NMC9306 256-Bit Serial EEPROM emulation
Copyright MESS Team.
Visit http://mamedev.org for licensing and usage restrictions.
**********************************************************************/
#include "emu.h"
#include "nmc9306.h"
#include "machine/devhelpr.h"
//**************************************************************************
// MACROS / CONSTANTS
//**************************************************************************
#define LOG 1
#define RAM_SIZE 32
// instructions
enum
{
OTHER = 0,
WRITE, // write register A3A2A1A0
READ, // read register A3A2A1A0
ERASE // erase register A3A2A1A0
};
// other instructions
enum
{
EWDS = 0, // erase/write disable
WRAL, // write all registers
ERAL, // erase all registers
EWEN, // erase/write enable
};
// states
enum
{
STATE_IDLE = 0,
STATE_COMMAND,
STATE_ADDRESS,
STATE_DATA_IN,
STATE_DATA_OUT,
STATE_ERASE
};
//**************************************************************************
// INLINE HELPERS
//**************************************************************************
// device type definition
const device_type NMC9306 = &device_creator<nmc9306_device>;
//-------------------------------------------------
// nmc9306_device - constructor
//-------------------------------------------------
inline UINT16 nmc9306_device::read(offs_t offset)
{
return m_register[offset];
}
//-------------------------------------------------
// nmc9306_device - constructor
//-------------------------------------------------
inline void nmc9306_device::write(offs_t offset, UINT16 data)
{
if (m_ewen)
{
m_register[offset] &= data;
}
}
//-------------------------------------------------
// nmc9306_device - constructor
//-------------------------------------------------
inline void nmc9306_device::erase(offs_t offset)
{
if (m_ewen)
{
m_register[offset] = 0xffff;
}
}
//**************************************************************************
// LIVE DEVICE
//**************************************************************************
//-------------------------------------------------
// nmc9306_device - constructor
//-------------------------------------------------
nmc9306_device::nmc9306_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
: device_t(mconfig, NMC9306, "NMC9306", tag, owner, clock),
device_nvram_interface(mconfig, *this),
m_state(STATE_IDLE),
m_ewen(false)
{
}
//-------------------------------------------------
// device_start - device-specific startup
//-------------------------------------------------
void nmc9306_device::device_start()
{
// state saving
save_item(NAME(m_bits));
save_item(NAME(m_state));
save_item(NAME(m_command));
save_item(NAME(m_address));
save_item(NAME(m_data));
save_item(NAME(m_ewen));
save_item(NAME(m_cs));
save_item(NAME(m_sk));
save_item(NAME(m_do));
save_item(NAME(m_di));
}
//-------------------------------------------------
// nvram_default - called to initialize NVRAM to
// its default state
//-------------------------------------------------
void nmc9306_device::nvram_default()
{
}
//-------------------------------------------------
// nvram_read - called to read NVRAM from the
// .nv file
//-------------------------------------------------
void nmc9306_device::nvram_read(emu_file &file)
{
file.read(m_register, RAM_SIZE);
}
//-------------------------------------------------
// nvram_write - called to write NVRAM to the
// .nv file
//-------------------------------------------------
void nmc9306_device::nvram_write(emu_file &file)
{
file.write(m_register, RAM_SIZE);
}
//-------------------------------------------------
// cs_w - chip select input
//-------------------------------------------------
WRITE_LINE_MEMBER( nmc9306_device::cs_w )
{
m_cs = state;
}
//-------------------------------------------------
// ck_w - serial clock input
//-------------------------------------------------
WRITE_LINE_MEMBER( nmc9306_device::sk_w )
{
m_sk = state;
if (!m_cs || !m_sk) return;
switch (m_state)
{
case STATE_IDLE:
if (LOG) logerror("NMC9306 '%s' Idle %u\n", tag(), m_di);
if (m_di)
{
// start bit received
m_state = STATE_COMMAND;
m_bits = 0;
}
break;
case STATE_COMMAND:
if (LOG) logerror("NMC9306 '%s' Command Bit %u\n", tag(), m_di);
m_command <<= 1;
m_command |= m_di;
m_bits++;
if (m_bits == 4)
{
m_state = STATE_ADDRESS;
m_bits = 0;
}
break;
case STATE_ADDRESS:
if (LOG) logerror("NMC9306 '%s' Address Bit %u\n", tag(), m_di);
m_address <<= 1;
m_address |= m_di;
m_bits++;
if (m_bits == 4)
{
switch ((m_command >> 2) & 0x03)
{
case OTHER:
switch (m_command & 0x03)
{
case EWDS:
if (LOG) logerror("NMC9306 '%s' EWDS\n", tag());
m_ewen = false;
m_state = STATE_IDLE;
break;
case WRAL:
if (LOG) logerror("NMC9306 '%s' WRAL\n", tag());
break;
case ERAL:
if (LOG) logerror("NMC9306 '%s' ERAL\n", tag());
break;
case EWEN:
if (LOG) logerror("NMC9306 '%s' EWEN\n", tag());
m_ewen = true;
m_state = STATE_IDLE;
break;
}
break;
case WRITE:
if (LOG) logerror("NMC9306 '%s' WRITE %u\n", tag(), m_address & 0x0f);
m_state = STATE_DATA_IN;
break;
case READ:
if (LOG) logerror("NMC9306 '%s' READ %u\n", tag(), m_address & 0x0f);
m_data = read(m_address & 0x0f);
m_state = STATE_DATA_OUT;
break;
case ERASE:
if (LOG) logerror("NMC9306 '%s' ERASE %u\n", tag(), m_address & 0x0f);
erase(m_address & 0x0f);
m_state = STATE_ERASE;
break;
}
m_bits = 0;
}
break;
case STATE_DATA_IN:
if (LOG) logerror("NMC9306 '%s' Data Bit IN %u\n", tag(), m_di);
m_data <<= 1;
m_data |= m_di;
m_bits++;
if (m_bits == 16)
{
write(m_address & 0x0f, m_data);
m_state = STATE_IDLE;
}
break;
case STATE_DATA_OUT:
if (LOG) logerror("NMC9306 '%s' Data Bit OUT %u\n", tag(), m_di);
m_do = BIT(m_data, 15);
m_data <<= 1;
m_bits++;
if (m_bits == 16)
{
m_state = STATE_IDLE;
}
break;
}
}
//-------------------------------------------------
// di_w - serial data input
//-------------------------------------------------
WRITE_LINE_MEMBER( nmc9306_device::di_w )
{
m_di = state;
}
//-------------------------------------------------
// do_r - serial data output
//-------------------------------------------------
READ_LINE_MEMBER( nmc9306_device::do_r )
{
return m_do;
}
|