summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/psion_pack.cpp
blob: e29d3fe71120d5cb4864f0b788b1faed46514bce (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
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
323
// license:BSD-3-Clause
// copyright-holders:Sandro Ronco
/****************************************************************************

    psion_pack.c

    Psion Organiser II Datapack emulation


    Datapack pinout from Psion's documentation
              __  __
    SD1    1 |  \/  | 2  SD0
    SD3    3 |      | 4  SD2
    SD5    5 |      | 6  SD4
    SD7    7 |      | 8  SD6
    SMR    9 |      | 10 SCLK
    SOE_B 11 |      | 12 SS_B
    GND   13 |      | 14 SPGM_B
    SVCC  15 |______| 16 SVPP

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

#include "emu.h"
#include "psion_pack.h"

// Datapack control lines
#define DP_LINE_CLOCK           0x01
#define DP_LINE_RESET           0x02
#define DP_LINE_PROGRAM         0x04
#define DP_LINE_OUTPUT_ENABLE   0x08
#define DP_LINE_SLOT_SELECT     0x10

// Datapack ID
#define DP_ID_EPROM             0x02
#define DP_ID_PAGED             0x04
#define DP_ID_WRITE             0x08
#define DP_ID_BOOT              0x10
#define DP_ID_COPY              0x20

#define OPK_HEAD_SIZE           6


OPTION_GUIDE_START( datapack_option_guide )
	OPTION_INT('S', "size", "Datapack size" )
	OPTION_INT('R', "ram", "RAM/EPROM" )
	OPTION_INT('P', "paged", "Paged" )
	OPTION_INT('W', "protect", "Write-protected" )
	OPTION_INT('B', "boot", "Bootable" )
	OPTION_INT('C', "copy", "Copyable" )
OPTION_GUIDE_END

static const char *datapack_option_spec =
	"S1/2/4/[8]/16;R0/[1];P[0]/1;W[0]/1;B[0]/1;C0/[1]";


// device type definition
const device_type PSION_DATAPACK = &device_creator<datapack_device>;

//-------------------------------------------------
//  datapack_device - constructor
//-------------------------------------------------

datapack_device::datapack_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, PSION_DATAPACK, "Psion Datapack", tag, owner, clock, "datapack", __FILE__),
		device_image_interface(mconfig, *this)
{
}


//-------------------------------------------------
//  datapack_device - destructor
//-------------------------------------------------

datapack_device::~datapack_device()
{
}


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

void datapack_device::device_start()
{
	call_unload();
}


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

void datapack_device::device_config_complete()
{
	add_format("opk", "Psion Datapack image", "opk", datapack_option_spec);

	// set brief and instance name
	update_names();
}


//-------------------------------------------------
//  option_guide for create new image
//-------------------------------------------------

const util::option_guide &datapack_device::create_option_guide() const
{
	return datapack_option_guide;
}


/*-------------------------------------------------
    update the internal state
-------------------------------------------------*/

void datapack_device::update()
{
	uint32_t pack_addr = m_counter + ((m_id & DP_ID_PAGED) ? (m_page << 8) : 0);

	// if the datapack is 128k or more is treated as segmented
	if (m_size >= 0x10)
		pack_addr += (m_segment << 14);

	if (pack_addr < (m_size << 13))
	{
		if ((m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
		{
			// write data
			if (!loaded_through_softlist() && (m_id & DP_ID_WRITE))
			{
				fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
				fwrite(&m_data, 1);
			}
		}
		else if ((m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
		{
			// write datapack segment
			if (m_size <= 0x10)
				m_segment = m_data & 0x07;
			else if (m_size <= 0x20)
				m_segment = m_data & 0x0f;
			else if (m_size <= 0x40)
				m_segment = m_data & 0x1f;
			else if (m_size <= 0x80)
				m_segment = m_data & 0x3f;
			else
				m_segment = m_data;
		}
		else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
		{
			// read data
			if ((pack_addr + OPK_HEAD_SIZE) < length())
			{
				fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
				fread(&m_data, 1);
			}
			else
			{
				m_data = 0xff;
			}
		}
		else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
		{
			// read datapack ID
			if ((m_id & DP_ID_EPROM) || loaded_through_softlist())
				m_data = m_id;
			else
				m_data = 0x01;      // for identify RAM pack
		}
	}
}


/*-------------------------------------------------
    read datapack data lines
-------------------------------------------------*/

uint8_t datapack_device::data_r()
{
	return (!(m_control & DP_LINE_SLOT_SELECT)) ? m_data : 0;
}


/*-------------------------------------------------
    write datapack data lines
-------------------------------------------------*/

void  datapack_device::data_w(uint8_t data)
{
	if (is_loaded())
	{
		m_data = data;

		// updates the internal state
		if (!(data & DP_LINE_SLOT_SELECT))
			update();
	}
}


/*-------------------------------------------------
    read datapack control lines
-------------------------------------------------*/

uint8_t datapack_device::control_r()
{
	return m_control;
}


/*-------------------------------------------------
    write datapack control lines
-------------------------------------------------*/

void datapack_device::control_w(uint8_t data)
{
	if (is_loaded())
	{
		if ((m_control & DP_LINE_CLOCK) != (data & DP_LINE_CLOCK))
		{
			// increments the counter
			m_counter++;

			if (m_counter >= ((m_id & DP_ID_PAGED) ? 0x100 : (m_size << 13)))
				m_counter = 0;
		}

		if ((m_control & DP_LINE_PROGRAM) && !(data & DP_LINE_PROGRAM))
		{
			// increments the page
			m_page++;

			if (m_page >= (m_size << 5))
				m_page = 0;
		}

		if (data & DP_LINE_RESET)
		{
			// reset counter and page
			m_counter = 0;
			m_page = 0;
		}

		m_control = data;

		// updates the internal state
		if (!(data & DP_LINE_SLOT_SELECT))
			update();
	}
}


/*-------------------------------------------------
    DEVICE_IMAGE_LOAD( datapack )
-------------------------------------------------*/

image_init_result datapack_device::call_load()
{
	uint8_t data[0x10];

	fread(data, 0x10);

	// check the OPK head
	if(strncmp((const char*)data, "OPK", 3))
		return image_init_result::FAIL;

	// get datapack ID and size
	m_id   = data[OPK_HEAD_SIZE + 0];
	m_size = data[OPK_HEAD_SIZE + 1];

	return image_init_result::PASS;
}


/*-------------------------------------------------
    DEVICE_IMAGE_CREATE( datapack )
-------------------------------------------------*/

image_init_result datapack_device::call_create(int format_type, util::option_resolution *create_args)
{
	static const uint8_t opk_head[6] = {'O', 'P', 'K', 0x00, 0x00, 0x00};

	if (create_args != nullptr)
	{
		m_id = 0x40;
		m_id |= (create_args->lookup_int('R')) ? 0x00 : 0x02;
		m_id |= (create_args->lookup_int('P')) ? 0x04 : 0x00;
		m_id |= (create_args->lookup_int('W')) ? 0x00 : 0x08;
		m_id |= (create_args->lookup_int('B')) ? 0x00 : 0x10;
		m_id |= (create_args->lookup_int('C')) ? 0x20 : 0x00;
		m_size = create_args->lookup_int('S');
	}
	else
	{
		// 64k RAM datapack by default
		m_id = 0x7c;
		m_size = 0x08;
	}

	fwrite(opk_head, 6);
	fwrite(&m_id, 1);
	fwrite(&m_size, 1);

	return image_init_result::PASS;
}


/*-------------------------------------------------
    DEVICE_IMAGE_UNLOAD( datapack )
-------------------------------------------------*/

void datapack_device::call_unload()
{
	m_id      = 0;
	m_size    = 0;
	m_counter = 0;
	m_page    = 0;
	m_segment = 0;
	m_control = 0;
	m_data    = 0;
}