summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/fmt_icmem.cpp
blob: bdb058b11dd7f0b30e7a17169635dc9e98ad4e9a (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
// license:BSD-3-Clause
// copyright-holders:Barry Rodewald
/*********************************************************************

    fmt_icmem.cpp

    FM Towns IC Memory Card
    PCMCIA SRAM Memory Cards, up to 64MB supported

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

#include "emu.h"
#include "emuopts.h"
#include "fmt_icmem.h"

// device type definition
const device_type FMT_ICMEM = device_creator<fmt_icmem_device>;

//-------------------------------------------------
//  fmt_icmem_device - constructor
//-------------------------------------------------

fmt_icmem_device::fmt_icmem_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, FMT_ICMEM, "FM Towns IC Memory Card", tag, owner, clock, "fmt_icmem", __FILE__),
		device_image_interface(mconfig, *this),
		m_writeprotect(*this,"icmem"),
		m_change(false),
		m_attr_select(false),
		m_detect(false),
		m_bank(0)
{
}


static INPUT_PORTS_START( fmt_icmem )
	PORT_START("icmem")
	PORT_CONFNAME(0x01, 0x00, "IC Memory Card Write Protect")
	PORT_CONFSETTING(0x00, DEF_STR( Off ))
	PORT_CONFSETTING(0x01, DEF_STR( On ))
INPUT_PORTS_END


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

void fmt_icmem_device::device_config_complete()
{
	// set brief and instance name
	update_names();
}


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

void fmt_icmem_device::device_start()
{
	m_memcard_ram = std::make_unique<uint8_t[]>(0x1000000);
	m_bank = 0;
	m_detect = false;
	m_change = false;
	save_item(NAME(m_change));
	save_item(NAME(m_detect));
	save_item(NAME(m_bank));
}

ioport_constructor fmt_icmem_device::device_input_ports() const
{
	return INPUT_PORTS_NAME(fmt_icmem);
}

image_init_result fmt_icmem_device::call_load()
{
	memset(m_memcard_ram.get(), 0xff, 0x1000000);
	fseek(0, SEEK_SET);
	size_t ret = fread(m_memcard_ram.get(), 0x1000000);

	if(ret != length())
		return image_init_result::FAIL;

	m_change = true;
	m_detect = true;
	return image_init_result::PASS;
}

void fmt_icmem_device::call_unload()
{
	fseek(0, SEEK_SET);
	if(!m_writeprotect->read())
		fwrite(m_memcard_ram.get(), 0x1000000);
	m_change = true;
	m_detect = false;
}

image_init_result fmt_icmem_device::call_create(int format_type, util::option_resolution *format_options)
{
	memset(m_memcard_ram.get(), 0xff, 0x1000000);

	size_t ret = fwrite(m_memcard_ram.get(), 0x1000000);
	if(ret != 0x1000000)
		return image_init_result::FAIL;

	m_change = true;
	m_detect = true;
	return image_init_result::PASS;
}


READ8_MEMBER(fmt_icmem_device::static_mem_read)
{
	return m_memcard_ram[offset];
}

WRITE8_MEMBER(fmt_icmem_device::static_mem_write)
{
	m_memcard_ram[offset] = data;
}

READ8_MEMBER(fmt_icmem_device::mem_read)
{
	return m_memcard_ram[(m_bank*0x100000) + offset];
}

WRITE8_MEMBER(fmt_icmem_device::mem_write)
{
	m_memcard_ram[(m_bank*0x100000) + offset] = data;
}

// Memory Card status:
//  bit 0 - 0 = Write Enable, 1 = Write Protect
//  bit 1,2 - Card Detect - 00 = Card Inserted, 11 = No card inserted, 10 or 01 = Card error?
//  bit 3,4,5 - not memory card related (EEPROM and backup battery level)
//  bit 6 - unknown
//  bit 7 - 1 = card changed, flips back to 0 when read
READ8_MEMBER(fmt_icmem_device::status_r)
{
	uint8_t ret = 0x00;

	ret |= (m_writeprotect->read() & 0x01);
	if(is_readonly())  // if image is read-only, then set write protect.
		ret |= 0x01;
	if(!m_detect)
		ret |= 0x06;
	if(m_change)
		ret |= 0x80;
	m_change = false;

	return ret;
}

// Memory Card bank select (0x490)
//  bit 0-5: bank select (bits 0-3 not used in non-386SX systems?)
// Attribute/Common memory select (0x491)
//  bit 0: 0 = common memory, 1 = attribute memory (TODO)
//  bit 7: 0 indicates that card is JEIDA v4 compliant
READ8_MEMBER(fmt_icmem_device::bank_r)
{
	switch(offset)
	{
	case 0:
		return m_bank & 0x0f;
	case 1:
		return m_attr_select ? 1 : 0;
	}
	return 0xff;
}

WRITE8_MEMBER(fmt_icmem_device::bank_w)
{
	switch(offset)
	{
	case 0:
		m_bank = data & 0x0f;
		break;
	case 1:
		m_attr_select = data & 0x01;
		break;
	}
}