summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/x2212.cpp
blob: c9738d69aa1102b7f9d01bc0f39e57461dd0fc70 (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
// license:BSD-3-Clause
// copyright-holders:smf,Barry Rodewald
/***************************************************************************

    x2212.c

    Xicor X2212 256 x 4 bit Nonvolatile Static RAM.

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

#include "emu.h"
#include "machine/x2212.h"
#include <algorithm>


//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(X2212, x2212_device, "x2212", "Xicor X2212 256x4 NOVRAM")
DEFINE_DEVICE_TYPE(X2210, x2210_device, "x2210", "Xicor X2210 64x4 NOVRAM")

//-------------------------------------------------
//  x2212_device - constructor
//-------------------------------------------------

x2212_device::x2212_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: x2212_device(mconfig, X2212, tag, owner, clock, 0x100)
{
}

x2212_device::x2212_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock, int size_data)
	: device_t(mconfig, type, tag, owner, clock)
	, device_nvram_interface(mconfig, *this)
	, m_auto_save(false)
	, m_store(false)
	, m_array_recall(false)
	, m_size_data(size_data)
	, m_default_data(*this, DEVICE_SELF, size_data)
{
}

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

void x2212_device::device_start()
{
	m_sram = std::make_unique<u8[]>(m_size_data);
	m_e2prom = std::make_unique<u8[]>(m_size_data);
	std::fill_n(&m_sram[0], m_size_data, 0xff);

	save_item(NAME(m_store));
	save_item(NAME(m_array_recall));
	save_pointer(NAME(m_sram.get()), m_size_data);
	save_pointer(NAME(m_e2prom.get()), m_size_data);
}


//-------------------------------------------------
//  nvram_default - called to initialize NVRAM to
//  its default state
//-------------------------------------------------

void x2212_device::nvram_default()
{
	// default to all-0xff
	std::fill_n(&m_e2prom[0], m_size_data, 0xff);

	// populate from a memory region if present
	if (m_default_data.found())
		std::copy_n(&m_default_data[0], m_size_data, &m_e2prom[0]);
}


//-------------------------------------------------
//  nvram_read - called to read NVRAM from the
//  .nv file
//-------------------------------------------------

void x2212_device::nvram_read(emu_file &file)
{
	file.read(&m_e2prom[0], m_size_data);
}


//-------------------------------------------------
//  nvram_write - called to write NVRAM to the
//  .nv file
//-------------------------------------------------

void x2212_device::nvram_write(emu_file &file)
{
	// auto-save causes an implicit store prior to exiting (writing)
	if (m_auto_save)
		do_store();

	file.write(&m_e2prom[0], m_size_data);
}



//**************************************************************************
//  INTERNAL HELPERS
//**************************************************************************

//-------------------------------------------------
//  do_store - store data from live RAM into the
//  EEPROM
//-------------------------------------------------

void x2212_device::do_store()
{
	std::copy_n(&m_sram[0], m_size_data, &m_e2prom[0]);
}


//-------------------------------------------------
//  do_recall - fetch data from the EEPROM into live
//  RAM
//-------------------------------------------------

void x2212_device::do_recall()
{
	std::copy_n(&m_e2prom[0], m_size_data, &m_sram[0]);
}



//**************************************************************************
//  READ/WRITE HANDLERS
//**************************************************************************

//-------------------------------------------------
//  write - store to the live RAM
//-------------------------------------------------

WRITE8_MEMBER( x2212_device::write )
{
	assert(offset < m_size_data);
	m_sram[offset] = data & 0x0f;
}


//-------------------------------------------------
//  read - read from the live RAM
//-------------------------------------------------

READ8_MEMBER( x2212_device::read )
{
	assert(offset < m_size_data);
	return (m_sram[offset] & 0x0f) | (space.unmap() & 0xf0);
}


//-------------------------------------------------
//  store - set the state of the store line
//  (FIXME: actually active low, not active high)
//-------------------------------------------------

WRITE_LINE_MEMBER( x2212_device::store )
{
	if (state != 0 && !m_store)
		do_store();
	m_store = (state != 0);
}


//-------------------------------------------------
//  recall - set the state of the recall line
//  (FIXME: actually active low, not active high)
//-------------------------------------------------

WRITE_LINE_MEMBER( x2212_device::recall )
{
	if (state != 0 && !m_array_recall)
		do_recall();
	m_array_recall = (state != 0);
}


x2210_device::x2210_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: x2212_device(mconfig, X2210, tag, owner, clock, 0x40)
{
}