summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/micropolis.cpp
blob: a747e7d2a366b2e2113290e31b5b011f6cd3c810 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// license:BSD-3-Clause
// copyright-holders:Robbbert
/***************************************************************************

    micropolis.c

    by Robbbert, August 2011.

This is a rough implementation of the Micropolis floppy-disk controller
as used for the Exidy Sorcerer. Since there is no documentation, coding
was done by looking at the Z80 code, and supplying the expected values.

Currently, only reading of disks is supported.

ToDo:
- Rewrite to be a standard device able to be used in a general way
- Fix bug where if you run a program on drive B,C,D then exit, you
  get a disk error.
- Enable the ability to write to disk when above bug gets fixed.
- When the controller is reset via command 5, what exactly gets reset?


Ports:
BE00 and BE01 can be used as command registers (they are identical),
              and they are also used as status registers (different).

BE02 and BE03 - read data, write data

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


#include "emu.h"
#include "machine/micropolis.h"


/***************************************************************************
    CONSTANTS
***************************************************************************/


#define STAT_RFC   0x20
#define STAT_TRACK0     0x08
#define STAT_READY      0x80

#define VERBOSE         0   /* General logging */
#define VERBOSE_DATA    0   /* Logging of each byte during read and write */

/* structure describing a single density track */
#define TRKSIZE_SD      16*270

#if 0
static const uint8_t track_SD[][2] = {
	{ 1, 0xff},     /*  1 * FF (marker)                      */
	{ 1, 0x00},     /*  1 byte, track number (00-4C)         */
	{ 1, 0x01},     /*  1 byte, sector number (00-0F)        */
	{10, 0x00},     /*  10 bytes of zeroes                   */
	{256, 0xe5},    /*  256 bytes of sector data             */
	{ 1, 0xb7},     /*  1 byte, CRC                          */
};
#endif

/***************************************************************************
    MAME DEVICE INTERFACE
***************************************************************************/

const device_type MICROPOLIS = device_creator<micropolis_device>;

micropolis_device::micropolis_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, MICROPOLIS, "MICROPOLIS", tag, owner, clock, "micropolis", __FILE__),
	m_read_dden(*this),
	m_write_intrq(*this),
	m_write_drq(*this),
	m_data(0),
	m_drive_num(0),
	m_track(0),
	m_sector(0),
	m_command(0),
	m_status(0),
	m_write_cmd(0),
	m_data_offset(0),
	m_data_count(0),
	m_sector_length(0),
	m_drive(nullptr)
{
	for (auto & elem : m_buffer)
		elem = 0;

	for (auto & elem : m_floppy_drive_tags)
		elem = nullptr;
}

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

void micropolis_device::device_start()
{
	m_read_dden.resolve_safe(1);
	m_write_intrq.resolve_safe();
	m_write_drq.resolve_safe();

	save_item(NAME(m_data));
	save_item(NAME(m_drive_num));
	save_item(NAME(m_track));
	save_item(NAME(m_sector));
	save_item(NAME(m_command));
	save_item(NAME(m_status));
	save_item(NAME(m_write_cmd));
	save_item(NAME(m_buffer));
	save_item(NAME(m_data_offset));
	save_item(NAME(m_data_count));
	save_item(NAME(m_sector_length));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void micropolis_device::device_reset()
{
	for (auto & elem : m_floppy_drive_tags)
	{
		if (elem)
		{
			legacy_floppy_image_device *img = siblingdevice<legacy_floppy_image_device>(elem);

			if (img)
			{
				img->floppy_drive_set_controller(this);
				//img->floppy_drive_set_index_pulse_callback(wd17xx_index_pulse_callback);
				img->floppy_drive_set_rpm(300.);
			}
		}
	}

	set_drive(0);

	m_drive_num = 0;
	m_sector = 0;
	m_track = 0;
	m_sector_length = 270;
	m_status = STAT_TRACK0;
}


/***************************************************************************
    IMPLEMENTATION
***************************************************************************/


/* read a sector */
void micropolis_device::read_sector()
{
	m_data_offset = 0;
	m_data_count = m_sector_length;

	/* read data */
	m_drive->floppy_drive_read_sector_data(0, m_sector, (char *)m_buffer, m_sector_length);
}


void micropolis_device::write_sector()
{
#if 0
	/* at this point, the disc is write enabled, and data
	 * has been transferred into our buffer - now write it to
	 * the disc image or to the real disc
	 */

	/* find sector */
	m_data_count = m_sector_length;

	/* write data */
	m_drive->floppy_drive_write_sector_data(0, m_sector, (char *)m_buffer, m_sector_length, m_write_cmd & 0x01);
#endif
}




/***************************************************************************
    INTERFACE
***************************************************************************/

/* select a drive */
void micropolis_device::set_drive(uint8_t drive)
{
	if (VERBOSE)
		logerror("micropolis_set_drive: $%02x\n", drive);

	if (m_floppy_drive_tags[drive])
		m_drive = siblingdevice<legacy_floppy_image_device>(m_floppy_drive_tags[drive]);
}


/***************************************************************************
    DEVICE HANDLERS
***************************************************************************/


/* read the FDC status register. */
READ8_MEMBER( micropolis_device::status_r )
{
	static int inv = 0;

	if (offset)
		return m_status | m_drive_num;
	else
	{
		// FIXME - find out what controls current sector
		m_sector = (m_sector + 3 + inv) & 15;
		read_sector();
		inv ^= 1;
		return (m_status & STAT_READY) | m_sector;
	}
}


/* read the FDC data register */
READ8_MEMBER( micropolis_device::data_r )
{
	if (m_data_offset >= m_sector_length)
		return 0;

	return m_buffer[m_data_offset++];
}

/* write the FDC command register */
WRITE8_MEMBER( micropolis_device::command_w )
{
/* List of commands:
Command (bits 5,6,7)      Options (bits 0,1,2,3,4)
0    Not used
1    Drive/head select    bits 0,1 select drive 0-3; bit 4 chooses a side
2    INT sector control   bit 0 LO = disable; HI = enable
3    Step                 bit 0 LO step out; HI = step in (increment track number)
4    Set Write
5    Reset controller
6    Not used
7    Not used */

	int direction = 0;

	switch (data >> 5)
	{
	case 1:
		m_drive_num = data & 3;
		m_drive->floppy_mon_w(1); // turn off the old drive
		set_drive(m_drive_num); // select new drive
		m_drive->floppy_mon_w(0); // turn it on
		break;
	case 2:  // not emulated, not used in sorcerer
		break;
	case 3:
		if (BIT(data, 0))
		{
			if (m_track < 76)
			{
				m_track++;
				direction = 1;
			}
		}
		else
		{
			if (m_track)
			{
				m_track--;
				direction = -1;
			}
		}
		break;
	case 4: // not emulated, to be done
		break;
	case 5: // not emulated, to be done
		break;
	}


	m_status = STAT_RFC | STAT_READY;
	if (m_track == 0)
		m_status |= STAT_TRACK0;

	m_drive->floppy_drive_set_ready_state(1,0);
	m_drive->floppy_drive_seek(direction);
}


/* write the FDC data register */
WRITE8_MEMBER( micropolis_device::data_w )
{
	if (m_data_count > 0)
	{
		/* put byte into buffer */
		if (VERBOSE_DATA)
			logerror("micropolis_info buffered data: $%02X at offset %d.\n", data, m_data_offset);

		m_buffer[m_data_offset++] = data;

		if (--m_data_count < 1)
		{
			write_sector();

			m_data_offset = 0;
		}
	}
	else
	{
		if (VERBOSE)
			logerror("%s: micropolis_data_w $%02X\n", space.machine().describe_context(), data);
	}
	m_data = data;
}

READ8_MEMBER( micropolis_device::read )
{
	uint8_t data = 0;

	switch (offset & 0x03)
	{
	case 0: data = status_r(space, 0); break;
	case 1: data = status_r(space, 1); break;
	case 2:
	case 3: data = data_r(space, 0); break;
	}

	return data;
}

WRITE8_MEMBER( micropolis_device::write )
{
	switch (offset & 0x03)
	{
	case 0:
	case 1: command_w(space, 0, data); break;
	case 2:
	case 3: data_w(space, 0, data);    break;
	}
}