summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/epson_qx/semidisk.cpp
blob: 475f1356eab9e383e834ac1269faea4f1056fb67 (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
// license:BSD-3-Clause
// copyright-holders:Brian Johnson
/*******************************************************************
 *
 * Semidisk Card
 *
 *******************************************************************/

#include "emu.h"
#include "semidisk.h"

//**************************************************************************
//  EPSON SEMIDISK DEVICE
//**************************************************************************

DEFINE_DEVICE_TYPE(EPSON_QX_OPTION_SEMIDISK, bus::epson_qx::semidisk_device, "epson_qx_option_semidisk", "Semidisk RAM Disk")

namespace bus::epson_qx {

static constexpr uint32_t BANK_SIZE = 512 * 1024;
static constexpr uint8_t  NUM_BANKS = 4;
static constexpr uint32_t RAM_SIZE = BANK_SIZE * NUM_BANKS;

INPUT_PORTS_START( semidisk )
	PORT_START("CONFIG")
	PORT_CONFNAME(0x01, 0x01, "Battery Backup")
	PORT_CONFSETTING(0x01, "Enabled")
	PORT_CONFSETTING(0x00, "Disabled")
INPUT_PORTS_END

//-------------------------------------------------
//  semidisk_device - constructor
//-------------------------------------------------
semidisk_device::semidisk_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
	device_t(mconfig, EPSON_QX_OPTION_SEMIDISK, tag, owner, clock),
	device_nvram_interface(mconfig, *this),
	device_option_expansion_interface(mconfig, *this),
	m_config(*this, "CONFIG")
{
}

//-------------------------------------------------
//  device_input_ports - device-specific ports
//-------------------------------------------------
ioport_constructor semidisk_device::device_input_ports() const
{
	return INPUT_PORTS_NAME( semidisk );
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------
void semidisk_device::device_start()
{
	m_ram = std::make_unique<uint8_t[]>(RAM_SIZE);

	address_space &space = m_bus->iospace();
	space.install_device(0xe0, 0xe3, *this, &semidisk_device::map);

	save_pointer(NAME(m_ram), RAM_SIZE);
	save_item(NAME(m_track));
	save_item(NAME(m_sector));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------
void semidisk_device::device_reset()
{
	m_sector = 0;
	m_track = 0;
}

//-------------------------------------------------
//  nvram implementation
//-------------------------------------------------
void semidisk_device::nvram_default()
{
	memset(m_ram.get(), 0, RAM_SIZE);
}

bool semidisk_device::nvram_read(util::read_stream &file)
{
	if (!(m_config->read() & 0x01)) {
		return false;
	}

	auto const [err, actual] = util::read(file, m_ram.get(), RAM_SIZE);
	return !err && (actual == RAM_SIZE);
}

bool semidisk_device::nvram_write(util::write_stream &file)
{
	if (!(m_config->read() & 0x01)) {
		memset(m_ram.get(), 0, RAM_SIZE);
	}

	auto const [err, actual] = util::write(file, m_ram.get(), RAM_SIZE);
	return !err;
}

//-------------------------------------------------
//  I/O implementation
//-------------------------------------------------
void semidisk_device::map(address_map &map)
{
	map(0x00, 0x00).select(0xff00).rw(FUNC(semidisk_device::data_r), FUNC(semidisk_device::data_w));
	map(0x02, 0x03).mirror(0xff00).w(FUNC(semidisk_device::reg_w));
}

uint8_t semidisk_device::data_r(offs_t offset)
{
	uint8_t bank = (m_sector >> 4) & 0x0f;
	uint8_t sector = m_sector & 0x0f;
	uint8_t byte_addr = (offset >> 8) & 0x7f;
	uint32_t ram_offset = (bank * BANK_SIZE) + (m_track * 16 * 128) + (sector * 128) + byte_addr;

	if (bank < NUM_BANKS) {
		return m_ram[ram_offset & (RAM_SIZE - 1)];
	}
	return 0xff;
}

void semidisk_device::data_w(offs_t offset, uint8_t data)
{
	uint8_t bank = (m_sector >> 4) & 0x0f;
	uint8_t sector = m_sector & 0x0f;
	uint8_t byte_addr = (offset >> 8) & 0x7f;
	uint32_t ram_offset = (bank * BANK_SIZE) + (m_track * 16 * 128) + (sector * 128) + byte_addr;

	if (bank < NUM_BANKS) {
		m_ram[ram_offset & (RAM_SIZE - 1)] = data;
	}
}

void semidisk_device::reg_w(offs_t offset, uint8_t data)
{
	switch(offset & 0xff) {
	case 0:
		m_track = data;
		break;
	case 1:
		m_sector = data;
		break;
	}
}

} // namespace bus::epson_qx