summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/imagedev/floppy.c
blob: 78a5c090a9c93e9b1fa49bd5a6853da1fefd60b2 (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
/*********************************************************************



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

#include "emu.h"
#include "floppy.h"
#include "formats/imageutl.h"

// device type definition
const device_type FLOPPY = &device_creator<floppy_image_device>;

//-------------------------------------------------
//  floppy_image_device - constructor
//-------------------------------------------------

floppy_image_device::floppy_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
    : device_t(mconfig, FLOPPY, "Floppy drive", tag, owner, clock),
	  device_image_interface(mconfig, *this),
	  m_image(NULL)
{

}

//-------------------------------------------------
//  floppy_image_device - destructor
//-------------------------------------------------

floppy_image_device::~floppy_image_device()
{
}

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

void floppy_image_device::device_config_complete()
{
	// inherit a copy of the static data
	const floppy_interface *intf = reinterpret_cast<const floppy_interface *>(static_config());
	if (intf != NULL)
		*static_cast<floppy_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{    	
		memset(&m_formats, 0, sizeof(m_formats));
		memset(&m_interface, 0, sizeof(m_interface));
		memset(&m_device_displayinfo, 0, sizeof(m_device_displayinfo));
		memset(&m_load_func, 0, sizeof(m_load_func));
		memset(&m_unload_func, 0, sizeof(m_unload_func));
	}

	image_device_format **formatptr;
    image_device_format *format;
    formatptr = &m_formatlist;
	int cnt = 0;
	m_extension_list[0] = '\0';
	while (m_formats[cnt].name)
	{
		// allocate a new format
		format = global_alloc_clear(image_device_format);		
		format->m_index 	  = cnt;
		format->m_name        = m_formats[cnt].name;
		format->m_description = m_formats[cnt].description;
		format->m_extensions  = m_formats[cnt].extensions;
		format->m_optspec     = (m_formats[cnt].param_guidelines) ? m_formats[cnt].param_guidelines : "";

		image_specify_extension( m_extension_list, 256, m_formats[cnt].extensions );
		// and append it to the list
		*formatptr = format;
		formatptr = &format->m_next;
		cnt++;
	}
	
	// set brief and instance name
	update_names();
}

static TIMER_CALLBACK(floppy_drive_index_callback)
{
	floppy_image_device *image = (floppy_image_device *) ptr;
	image->index_func();
}

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

void floppy_image_device::device_start()
{
	// resolve callbacks
	m_out_idx_func.resolve(m_out_idx_cb, *this);
	
	m_idx = 0;
	
	/* motor off */
	m_mon = 1;
	/* set write protect on */
	m_wpt = 0;

	m_rpm = 300.0f;
	
	m_cyl = 0;
	m_ss  = 1;	
	m_stp = 1;
	m_dskchg = 0;
	m_index_timer = machine().scheduler().timer_alloc(FUNC(floppy_drive_index_callback), (void *)this);
}

bool floppy_image_device::call_load()
{
	device_image_interface *image = this;
	int best;
	m_image = global_alloc(floppy_image((void *) image, &image_ioprocs, m_formats));
	const struct floppy_format_def *format = m_image->identify(&best);
	m_dskchg = 0;
	if (format) {
		m_image->load(best);
		if (m_load_func)
			return m_load_func(*this);
	} else {
		return IMAGE_INIT_FAIL;
	}
	return IMAGE_INIT_PASS;
}

void floppy_image_device::call_unload()
{
	m_dskchg = 0;
	
	if (m_image)
		global_free(m_image);
	if (m_unload_func)
		m_unload_func(*this);
}

/* motor on, active low */
void floppy_image_device::mon_w(int state)
{
	/* force off if there is no attached image */
	if (!exists())
		state = 1;

	/* off -> on */
	if (m_mon && state == 0)
	{
		m_idx = 0;
		index_func();
	}

	/* on -> off */
	else if (m_mon == 0 && state)
		m_index_timer->adjust(attotime::zero);

	m_mon = state;
}

/* index pulses at rpm/60 Hz, and stays high 1/20th of time */
void floppy_image_device::index_func()
{
	double ms = 1000.0 / (m_rpm / 60.0);

	if (m_idx)
	{
		m_idx = 0;
		m_index_timer->adjust(attotime::from_double(ms*19/20/1000.0));
	}
	else
	{
		m_idx = 1;
		m_index_timer->adjust(attotime::from_double(ms/20/1000.0));
	}

	m_out_idx_func(m_idx);
}

int floppy_image_device::ready_r()
{
	if (exists())
	{
		if (m_mon == 0)
		{
			return 0;
		}
	}
	return 1;
}

double floppy_image_device::get_pos()
{
	return m_index_timer->elapsed().as_double();
}

void floppy_image_device::stp_w(int state)
{
    if ( m_stp != state ) {
		m_stp = state;
    	if ( m_stp == 0 ) {
			if ( m_dir ) {
				if ( m_cyl ) m_cyl--;
			} else {
				if ( m_cyl < 83 ) m_cyl++;			
			}
			/* Update disk detection if applicable */
			if (exists())
			{
				if (m_dskchg==0) m_dskchg = 1;
			}			
		}
	}

}