1 |
dpavlin |
4 |
/* |
2 |
|
|
* Copyright (C) 2004-2005 Anders Gavare. All rights reserved. |
3 |
|
|
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions are met: |
6 |
|
|
* |
7 |
|
|
* 1. Redistributions of source code must retain the above copyright |
8 |
|
|
* notice, this list of conditions and the following disclaimer. |
9 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer in the |
11 |
|
|
* documentation and/or other materials provided with the distribution. |
12 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
13 |
|
|
* derived from this software without specific prior written permission. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
* SUCH DAMAGE. |
26 |
|
|
* |
27 |
|
|
* |
28 |
dpavlin |
6 |
* $Id: dev_wdc.c,v 1.37 2005/05/27 07:29:25 debug Exp $ |
29 |
dpavlin |
4 |
* |
30 |
|
|
* Standard IDE controller. |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
#include <stdio.h> |
34 |
|
|
#include <stdlib.h> |
35 |
|
|
#include <string.h> |
36 |
|
|
|
37 |
|
|
#include "console.h" |
38 |
|
|
#include "cop0.h" |
39 |
|
|
#include "cpu.h" |
40 |
|
|
#include "devices.h" |
41 |
|
|
#include "diskimage.h" |
42 |
|
|
#include "machine.h" |
43 |
|
|
#include "memory.h" |
44 |
|
|
#include "misc.h" |
45 |
|
|
|
46 |
|
|
#include "wdcreg.h" |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
#define WDC_TICK_SHIFT 14 |
50 |
|
|
#define WDC_INBUF_SIZE (512*257) |
51 |
|
|
|
52 |
|
|
/* INT_DELAY=2 to be safe, 1 is faster but maybe buggy. */ |
53 |
|
|
#define INT_DELAY 1 |
54 |
|
|
|
55 |
dpavlin |
6 |
extern int quiet_mode; |
56 |
|
|
|
57 |
dpavlin |
4 |
/* #define debug fatal */ |
58 |
|
|
/* #define DATA_DEBUG */ |
59 |
|
|
|
60 |
|
|
struct wdc_data { |
61 |
|
|
int irq_nr; |
62 |
|
|
int base_drive; |
63 |
|
|
|
64 |
|
|
int delayed_interrupt; |
65 |
|
|
|
66 |
|
|
unsigned char identify_struct[512]; |
67 |
|
|
|
68 |
|
|
unsigned char inbuf[WDC_INBUF_SIZE]; |
69 |
|
|
int inbuf_head; |
70 |
|
|
int inbuf_tail; |
71 |
|
|
|
72 |
|
|
int write_in_progress; |
73 |
|
|
int write_count; |
74 |
|
|
int64_t write_offset; |
75 |
|
|
|
76 |
|
|
int error; |
77 |
|
|
int precomp; |
78 |
|
|
int seccnt; |
79 |
|
|
int sector; |
80 |
|
|
int cyl_lo; |
81 |
|
|
int cyl_hi; |
82 |
|
|
int sectorsize; |
83 |
|
|
int lba; |
84 |
|
|
int drive; |
85 |
|
|
int head; |
86 |
|
|
int cur_command; |
87 |
|
|
}; |
88 |
|
|
|
89 |
|
|
|
90 |
|
|
/* |
91 |
|
|
* dev_wdc_tick(): |
92 |
|
|
*/ |
93 |
|
|
void dev_wdc_tick(struct cpu *cpu, void *extra) |
94 |
|
|
{ |
95 |
|
|
struct wdc_data *d = extra; |
96 |
|
|
|
97 |
|
|
if (d->delayed_interrupt) { |
98 |
|
|
d->delayed_interrupt --; |
99 |
|
|
|
100 |
|
|
if (d->delayed_interrupt == 0) |
101 |
|
|
cpu_interrupt(cpu, d->irq_nr); |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
/* |
107 |
|
|
* wdc_addtoinbuf(): |
108 |
|
|
* |
109 |
|
|
* Write to the inbuf at its head, read at its tail. |
110 |
|
|
*/ |
111 |
|
|
static void wdc_addtoinbuf(struct wdc_data *d, int c) |
112 |
|
|
{ |
113 |
|
|
d->inbuf[d->inbuf_head] = c; |
114 |
|
|
|
115 |
|
|
d->inbuf_head = (d->inbuf_head + 1) % WDC_INBUF_SIZE; |
116 |
|
|
if (d->inbuf_head == d->inbuf_tail) |
117 |
dpavlin |
6 |
fatal("[ wdc_addtoinbuf(): WARNING! wdc inbuf overrun ]\n"); |
118 |
dpavlin |
4 |
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
/* |
122 |
|
|
* wdc_get_inbuf(): |
123 |
|
|
* |
124 |
|
|
* Read from the tail of inbuf. |
125 |
|
|
*/ |
126 |
|
|
static uint64_t wdc_get_inbuf(struct wdc_data *d) |
127 |
|
|
{ |
128 |
|
|
int c = d->inbuf[d->inbuf_tail]; |
129 |
|
|
|
130 |
|
|
if (d->inbuf_head == d->inbuf_tail) { |
131 |
dpavlin |
6 |
fatal("[ wdc: WARNING! someone is reading too much from the " |
132 |
|
|
"wdc inbuf! ]\n"); |
133 |
dpavlin |
4 |
return -1; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
d->inbuf_tail = (d->inbuf_tail + 1) % WDC_INBUF_SIZE; |
137 |
|
|
return c; |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
|
141 |
|
|
/* |
142 |
|
|
* wdc_initialize_identify_struct(d): |
143 |
|
|
*/ |
144 |
|
|
static void wdc_initialize_identify_struct(struct cpu *cpu, struct wdc_data *d) |
145 |
|
|
{ |
146 |
dpavlin |
6 |
uint64_t total_size; |
147 |
|
|
int cyls, heads, sectors_per_track; |
148 |
dpavlin |
4 |
|
149 |
dpavlin |
6 |
total_size = diskimage_getsize(cpu->machine, d->drive + d->base_drive, |
150 |
|
|
DISKIMAGE_IDE); |
151 |
dpavlin |
4 |
|
152 |
dpavlin |
6 |
diskimage_getchs(cpu->machine, d->drive + d->base_drive, |
153 |
|
|
DISKIMAGE_IDE, &cyls, &heads, §ors_per_track); |
154 |
|
|
|
155 |
dpavlin |
4 |
memset(d->identify_struct, 0, sizeof(d->identify_struct)); |
156 |
|
|
|
157 |
|
|
/* Offsets are in 16-bit WORDS! High byte, then low. */ |
158 |
|
|
|
159 |
|
|
/* 0: general flags */ |
160 |
|
|
d->identify_struct[2 * 0 + 0] = 0; |
161 |
|
|
d->identify_struct[2 * 0 + 1] = 1 << 6; |
162 |
|
|
|
163 |
|
|
/* 1: nr of cylinders */ |
164 |
|
|
d->identify_struct[2 * 1 + 0] = (cyls >> 8); |
165 |
|
|
d->identify_struct[2 * 1 + 1] = cyls & 255; |
166 |
|
|
|
167 |
|
|
/* 3: nr of heads */ |
168 |
dpavlin |
6 |
d->identify_struct[2 * 3 + 0] = heads >> 8; |
169 |
|
|
d->identify_struct[2 * 3 + 1] = heads; |
170 |
dpavlin |
4 |
|
171 |
|
|
/* 6: sectors per track */ |
172 |
dpavlin |
6 |
d->identify_struct[2 * 6 + 0] = sectors_per_track >> 8; |
173 |
|
|
d->identify_struct[2 * 6 + 1] = sectors_per_track; |
174 |
dpavlin |
4 |
|
175 |
|
|
/* 10-19: Serial number */ |
176 |
|
|
memcpy(&d->identify_struct[2 * 10], "S/N 1234-5678 ", 20); |
177 |
|
|
|
178 |
|
|
/* 23-26: Firmware version */ |
179 |
|
|
memcpy(&d->identify_struct[2 * 23], "VER 1.0 ", 8); |
180 |
|
|
|
181 |
|
|
/* 27-46: Model number */ |
182 |
|
|
memcpy(&d->identify_struct[2 * 27], |
183 |
|
|
"Fake GXemul IDE disk ", 40); |
184 |
|
|
/* TODO: Use the diskimage's filename instead? */ |
185 |
|
|
|
186 |
|
|
/* 47: max sectors per multitransfer */ |
187 |
|
|
d->identify_struct[2 * 47 + 0] = 0x80; |
188 |
|
|
d->identify_struct[2 * 47 + 1] = 1; /* 1 or 16? */ |
189 |
|
|
|
190 |
|
|
/* 57-58: current capacity in sectors */ |
191 |
|
|
d->identify_struct[2 * 57 + 0] = ((total_size / 512) >> 24) % 255; |
192 |
|
|
d->identify_struct[2 * 57 + 1] = ((total_size / 512) >> 16) % 255; |
193 |
|
|
d->identify_struct[2 * 58 + 0] = ((total_size / 512) >> 8) % 255; |
194 |
|
|
d->identify_struct[2 * 58 + 1] = (total_size / 512) & 255; |
195 |
|
|
|
196 |
|
|
/* 60-61: total nr of addresable sectors */ |
197 |
|
|
d->identify_struct[2 * 60 + 0] = ((total_size / 512) >> 24) % 255; |
198 |
|
|
d->identify_struct[2 * 60 + 1] = ((total_size / 512) >> 16) % 255; |
199 |
|
|
d->identify_struct[2 * 61 + 0] = ((total_size / 512) >> 8) % 255; |
200 |
|
|
d->identify_struct[2 * 61 + 1] = (total_size / 512) & 255; |
201 |
|
|
|
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
|
205 |
|
|
/* |
206 |
|
|
* status_byte(): |
207 |
|
|
*/ |
208 |
|
|
static int status_byte(struct wdc_data *d, struct cpu *cpu) |
209 |
|
|
{ |
210 |
|
|
int odata = 0; |
211 |
|
|
|
212 |
dpavlin |
6 |
if (diskimage_exist(cpu->machine, d->drive + d->base_drive, |
213 |
|
|
DISKIMAGE_IDE)) |
214 |
dpavlin |
4 |
odata |= WDCS_DRDY; |
215 |
|
|
if (d->inbuf_head != d->inbuf_tail) |
216 |
|
|
odata |= WDCS_DRQ; |
217 |
|
|
if (d->write_in_progress) |
218 |
|
|
odata |= WDCS_DRQ; |
219 |
|
|
if (d->error) |
220 |
|
|
odata |= WDCS_ERR; |
221 |
|
|
|
222 |
|
|
#if 0 |
223 |
|
|
/* |
224 |
|
|
* TODO: Is this correct behaviour? |
225 |
|
|
* |
226 |
|
|
* NetBSD/cobalt seems to want it, but Linux on MobilePro does not. |
227 |
|
|
*/ |
228 |
dpavlin |
6 |
if (!diskimage_exist(cpu->machine, d->drive + d->base_drive, |
229 |
|
|
DISKIMAGE_IDE)) |
230 |
dpavlin |
4 |
odata = 0xff; |
231 |
|
|
#endif |
232 |
|
|
|
233 |
|
|
return odata; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
|
237 |
|
|
/* |
238 |
|
|
* dev_wdc_altstatus_access(): |
239 |
|
|
*/ |
240 |
|
|
int dev_wdc_altstatus_access(struct cpu *cpu, struct memory *mem, |
241 |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
242 |
|
|
int writeflag, void *extra) |
243 |
|
|
{ |
244 |
|
|
struct wdc_data *d = extra; |
245 |
|
|
uint64_t idata = 0, odata = 0; |
246 |
|
|
|
247 |
|
|
idata = memory_readmax64(cpu, data, len); |
248 |
|
|
|
249 |
|
|
/* Same as the normal status byte? */ |
250 |
|
|
odata = status_byte(d, cpu); |
251 |
|
|
|
252 |
|
|
if (writeflag==MEM_READ) |
253 |
|
|
debug("[ wdc: read from ALTSTATUS: 0x%02x ]\n", |
254 |
|
|
(int)odata); |
255 |
|
|
else |
256 |
|
|
debug("[ wdc: write to ALT. CTRL: 0x%02x ]\n", |
257 |
|
|
(int)idata); |
258 |
|
|
|
259 |
|
|
if (writeflag == MEM_READ) |
260 |
|
|
memory_writemax64(cpu, data, len, odata); |
261 |
|
|
|
262 |
|
|
return 1; |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
|
266 |
|
|
/* |
267 |
|
|
* dev_wdc_access(): |
268 |
|
|
*/ |
269 |
|
|
int dev_wdc_access(struct cpu *cpu, struct memory *mem, |
270 |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
271 |
|
|
int writeflag, void *extra) |
272 |
|
|
{ |
273 |
|
|
struct wdc_data *d = extra; |
274 |
|
|
uint64_t idata = 0, odata = 0; |
275 |
dpavlin |
6 |
int i, cyls, heads, sectors_per_track; |
276 |
dpavlin |
4 |
|
277 |
|
|
idata = memory_readmax64(cpu, data, len); |
278 |
|
|
|
279 |
|
|
switch (relative_addr) { |
280 |
|
|
|
281 |
|
|
case wd_data: /* 0: data */ |
282 |
|
|
if (writeflag==MEM_READ) { |
283 |
|
|
odata = 0; |
284 |
|
|
|
285 |
|
|
/* TODO: This is hardcoded for little-endian? */ |
286 |
|
|
|
287 |
|
|
odata += wdc_get_inbuf(d); |
288 |
|
|
if (len >= 2) |
289 |
|
|
odata += (wdc_get_inbuf(d) << 8); |
290 |
|
|
if (len == 4) { |
291 |
|
|
odata += (wdc_get_inbuf(d) << 16); |
292 |
|
|
odata += (wdc_get_inbuf(d) << 24); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
#ifdef DATA_DEBUG |
296 |
|
|
debug("[ wdc: read from DATA: 0x%04x ]\n", odata); |
297 |
|
|
#endif |
298 |
|
|
|
299 |
|
|
if (d->inbuf_tail != d->inbuf_head) |
300 |
|
|
d->delayed_interrupt = INT_DELAY; |
301 |
|
|
|
302 |
|
|
} else { |
303 |
|
|
int inbuf_len; |
304 |
|
|
#ifdef DATA_DEBUG |
305 |
|
|
debug("[ wdc: write to DATA (len=%i): 0x%08lx ]\n", |
306 |
|
|
(int)len, (long)idata); |
307 |
|
|
#endif |
308 |
|
|
if (!d->write_in_progress) { |
309 |
|
|
fatal("[ wdc: write to DATA, but not " |
310 |
|
|
"expecting any? (len=%i): 0x%08lx ]\n", |
311 |
|
|
(int)len, (long)idata); |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
switch (len) { |
315 |
|
|
case 4: wdc_addtoinbuf(d, idata & 0xff); |
316 |
|
|
wdc_addtoinbuf(d, (idata >> 8) & 0xff); |
317 |
|
|
wdc_addtoinbuf(d, (idata >> 16) & 0xff); |
318 |
|
|
wdc_addtoinbuf(d, (idata >> 24) & 0xff); |
319 |
|
|
break; |
320 |
|
|
case 2: wdc_addtoinbuf(d, idata & 0xff); |
321 |
|
|
wdc_addtoinbuf(d, (idata >> 8) & 0xff); |
322 |
|
|
break; |
323 |
|
|
case 1: wdc_addtoinbuf(d, idata); break; |
324 |
|
|
default:fatal("wdc: unimplemented write len %i\n", len); |
325 |
|
|
exit(1); |
326 |
|
|
} |
327 |
|
|
|
328 |
|
|
inbuf_len = d->inbuf_head - d->inbuf_tail; |
329 |
|
|
while (inbuf_len < 0) |
330 |
|
|
inbuf_len += WDC_INBUF_SIZE; |
331 |
|
|
|
332 |
|
|
#if 0 |
333 |
|
|
if ((inbuf_len % (512 * d->write_count)) == 0) { |
334 |
|
|
#endif |
335 |
|
|
if ((inbuf_len % 512) == 0) { |
336 |
|
|
int count = 1; /* d->write_count; */ |
337 |
|
|
unsigned char *buf = malloc(count * 512); |
338 |
|
|
if (buf == NULL) { |
339 |
|
|
fprintf(stderr, "out of memory\n"); |
340 |
|
|
exit(1); |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
for (i=0; i<512 * count; i++) |
344 |
|
|
buf[i] = wdc_get_inbuf(d); |
345 |
|
|
|
346 |
|
|
diskimage_access(cpu->machine, |
347 |
dpavlin |
6 |
d->drive + d->base_drive, DISKIMAGE_IDE, 1, |
348 |
dpavlin |
4 |
d->write_offset, buf, 512 * count); |
349 |
|
|
free(buf); |
350 |
|
|
|
351 |
|
|
d->write_count --; |
352 |
|
|
d->write_offset += 512; |
353 |
|
|
|
354 |
|
|
d->delayed_interrupt = INT_DELAY; |
355 |
|
|
|
356 |
|
|
if (d->write_count == 0) |
357 |
|
|
d->write_in_progress = 0; |
358 |
|
|
} |
359 |
|
|
} |
360 |
|
|
break; |
361 |
|
|
|
362 |
|
|
case wd_error: /* 1: error (r), precomp (w) */ |
363 |
|
|
if (writeflag==MEM_READ) { |
364 |
|
|
odata = d->error; |
365 |
|
|
debug("[ wdc: read from ERROR: 0x%02x ]\n", odata); |
366 |
|
|
/* TODO: is the error value cleared on read? */ |
367 |
|
|
d->error = 0; |
368 |
|
|
} else { |
369 |
|
|
d->precomp = idata; |
370 |
|
|
debug("[ wdc: write to PRECOMP: 0x%02x ]\n", idata); |
371 |
|
|
} |
372 |
|
|
break; |
373 |
|
|
|
374 |
|
|
case wd_seccnt: /* 2: sector count */ |
375 |
|
|
if (writeflag==MEM_READ) { |
376 |
|
|
odata = d->seccnt; |
377 |
|
|
debug("[ wdc: read from SECCNT: 0x%02x ]\n", odata); |
378 |
|
|
} else { |
379 |
|
|
d->seccnt = idata; |
380 |
|
|
debug("[ wdc: write to SECCNT: 0x%02x ]\n", idata); |
381 |
|
|
} |
382 |
|
|
break; |
383 |
|
|
|
384 |
|
|
case wd_sector: /* 3: first sector */ |
385 |
|
|
if (writeflag==MEM_READ) { |
386 |
|
|
odata = d->sector; |
387 |
|
|
debug("[ wdc: read from SECTOR: 0x%02x ]\n", odata); |
388 |
|
|
} else { |
389 |
|
|
d->sector = idata; |
390 |
|
|
debug("[ wdc: write to SECTOR: 0x%02x ]\n", idata); |
391 |
|
|
} |
392 |
|
|
break; |
393 |
|
|
|
394 |
|
|
case wd_cyl_lo: /* 4: cylinder low */ |
395 |
|
|
if (writeflag==MEM_READ) { |
396 |
|
|
odata = d->cyl_lo; |
397 |
|
|
debug("[ wdc: read from CYL_LO: 0x%02x ]\n", odata); |
398 |
|
|
} else { |
399 |
|
|
d->cyl_lo = idata; |
400 |
|
|
debug("[ wdc: write to CYL_LO: 0x%02x ]\n", idata); |
401 |
|
|
} |
402 |
|
|
break; |
403 |
|
|
|
404 |
|
|
case wd_cyl_hi: /* 5: cylinder low */ |
405 |
|
|
if (writeflag==MEM_READ) { |
406 |
|
|
odata = d->cyl_hi; |
407 |
|
|
debug("[ wdc: read from CYL_HI: 0x%02x ]\n", odata); |
408 |
|
|
} else { |
409 |
|
|
d->cyl_hi = idata; |
410 |
|
|
debug("[ wdc: write to CYL_HI: 0x%02x ]\n", idata); |
411 |
|
|
} |
412 |
|
|
break; |
413 |
|
|
|
414 |
|
|
case wd_sdh: /* 6: sectorsize/drive/head */ |
415 |
|
|
if (writeflag==MEM_READ) { |
416 |
|
|
odata = (d->sectorsize << 6) + (d->lba << 5) + |
417 |
|
|
(d->drive << 4) + (d->head); |
418 |
|
|
debug("[ wdc: read from SDH: 0x%02x (sectorsize %i," |
419 |
|
|
" lba=%i, drive %i, head %i) ]\n", (int)odata, |
420 |
|
|
d->sectorsize, d->lba, d->drive, d->head); |
421 |
|
|
} else { |
422 |
|
|
d->sectorsize = (idata >> 6) & 3; |
423 |
|
|
d->lba = (idata >> 5) & 1; |
424 |
|
|
d->drive = (idata >> 4) & 1; |
425 |
|
|
d->head = idata & 0xf; |
426 |
|
|
debug("[ wdc: write to SDH: 0x%02x (sectorsize %i," |
427 |
|
|
" lba=%i, drive %i, head %i) ]\n", (int)idata, |
428 |
|
|
d->sectorsize, d->lba, d->drive, d->head); |
429 |
|
|
} |
430 |
|
|
break; |
431 |
|
|
|
432 |
|
|
case wd_command: /* 7: command or status */ |
433 |
|
|
if (writeflag==MEM_READ) { |
434 |
|
|
odata = status_byte(d, cpu); |
435 |
|
|
#if 1 |
436 |
dpavlin |
6 |
if (!quiet_mode) |
437 |
|
|
debug("[ wdc: read from STATUS: 0x%02x ]\n", |
438 |
|
|
odata); |
439 |
dpavlin |
4 |
#endif |
440 |
|
|
|
441 |
|
|
cpu_interrupt_ack(cpu, d->irq_nr); |
442 |
|
|
d->delayed_interrupt = 0; |
443 |
|
|
} else { |
444 |
|
|
debug("[ wdc: write to COMMAND: 0x%02x ]\n", idata); |
445 |
|
|
d->cur_command = idata; |
446 |
|
|
|
447 |
|
|
/* TODO: Is this correct behaviour? */ |
448 |
|
|
if (!diskimage_exist(cpu->machine, |
449 |
dpavlin |
6 |
d->drive + d->base_drive, DISKIMAGE_IDE)) { |
450 |
dpavlin |
4 |
d->error |= WDCE_ABRT; |
451 |
|
|
d->delayed_interrupt = INT_DELAY; |
452 |
|
|
break; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
|
/* Handle the command: */ |
456 |
|
|
switch (d->cur_command) { |
457 |
|
|
case WDCC_READ: |
458 |
|
|
debug("[ wdc: READ from drive %i, head %i, " |
459 |
|
|
"cylinder %i, sector %i, nsecs %i ]\n", |
460 |
|
|
d->drive, d->head, d->cyl_hi*256+d->cyl_lo, |
461 |
|
|
d->sector, d->seccnt); |
462 |
|
|
/* TODO: HAHA! This should be removed |
463 |
|
|
quickly */ |
464 |
dpavlin |
6 |
diskimage_getchs(cpu->machine, d->drive + |
465 |
|
|
d->base_drive, DISKIMAGE_IDE, &cyls, |
466 |
|
|
&heads, §ors_per_track); |
467 |
|
|
|
468 |
dpavlin |
4 |
{ |
469 |
|
|
unsigned char buf[512*256]; |
470 |
|
|
int cyl = d->cyl_hi * 256+ d->cyl_lo; |
471 |
|
|
int count = d->seccnt? d->seccnt : 256; |
472 |
|
|
uint64_t offset = 512 * (d->sector - 1 |
473 |
dpavlin |
6 |
+ d->head * sectors_per_track + |
474 |
|
|
heads*sectors_per_track*cyl); |
475 |
dpavlin |
4 |
|
476 |
|
|
#if 0 |
477 |
|
|
/* LBA: */ |
478 |
|
|
if (d->lba) |
479 |
|
|
offset = 512 * (((d->head & 0xf) << 24) + (cyl << 8) + d->sector); |
480 |
|
|
printf("WDC read from offset %lli\n", (long long)offset); |
481 |
|
|
#endif |
482 |
|
|
diskimage_access(cpu->machine, |
483 |
dpavlin |
6 |
d->drive + d->base_drive, |
484 |
|
|
DISKIMAGE_IDE, 0, |
485 |
dpavlin |
4 |
offset, buf, 512 * count); |
486 |
|
|
/* TODO: result code */ |
487 |
|
|
for (i=0; i<512 * count; i++) |
488 |
|
|
wdc_addtoinbuf(d, buf[i]); |
489 |
|
|
} |
490 |
|
|
d->delayed_interrupt = INT_DELAY; |
491 |
|
|
break; |
492 |
|
|
case WDCC_WRITE: |
493 |
|
|
debug("[ wdc: WRITE to drive %i, head %i, " |
494 |
|
|
"cylinder %i, sector %i, nsecs %i ]\n", |
495 |
|
|
d->drive, d->head, d->cyl_hi*256+d->cyl_lo, |
496 |
|
|
d->sector, d->seccnt); |
497 |
|
|
/* TODO: HAHA! This should be removed |
498 |
|
|
quickly */ |
499 |
dpavlin |
6 |
diskimage_getchs(cpu->machine, d->drive + |
500 |
|
|
d->base_drive, DISKIMAGE_IDE, &cyls, |
501 |
|
|
&heads, §ors_per_track); |
502 |
dpavlin |
4 |
{ |
503 |
|
|
int cyl = d->cyl_hi * 256+ d->cyl_lo; |
504 |
|
|
int count = d->seccnt? d->seccnt : 256; |
505 |
|
|
uint64_t offset = 512 * (d->sector - 1 |
506 |
dpavlin |
6 |
+ d->head * sectors_per_track + |
507 |
|
|
heads*sectors_per_track*cyl); |
508 |
dpavlin |
4 |
|
509 |
|
|
#if 0 |
510 |
|
|
/* LBA: */ |
511 |
|
|
if (d->lba) |
512 |
|
|
offset = 512 * (((d->head & 0xf) << 24) + (cyl << 8) + d->sector); |
513 |
|
|
printf("WDC write to offset %lli\n", (long long)offset); |
514 |
|
|
#endif |
515 |
|
|
|
516 |
|
|
d->write_in_progress = 1; |
517 |
|
|
d->write_count = count; |
518 |
|
|
d->write_offset = offset; |
519 |
|
|
|
520 |
|
|
/* TODO: result code */ |
521 |
|
|
} |
522 |
|
|
/* TODO: Really interrupt here? */ |
523 |
|
|
#if 0 |
524 |
|
|
d->delayed_interrupt = INT_DELAY; |
525 |
|
|
#endif |
526 |
|
|
break; |
527 |
|
|
|
528 |
|
|
case WDCC_IDP: /* Initialize drive parameters */ |
529 |
|
|
debug("[ wdc: IDP drive %i (TODO) ]\n", |
530 |
|
|
d->drive); |
531 |
|
|
/* TODO */ |
532 |
|
|
d->delayed_interrupt = INT_DELAY; |
533 |
|
|
break; |
534 |
|
|
case SET_FEATURES: |
535 |
|
|
fatal("[ wdc: SET_FEATURES drive %i (TODO), " |
536 |
|
|
"feature 0x%02x ]\n", d->drive, d->precomp); |
537 |
|
|
/* TODO */ |
538 |
|
|
switch (d->precomp) { |
539 |
|
|
case WDSF_SET_MODE: |
540 |
|
|
fatal("[ wdc: WDSF_SET_MODE drive %i, " |
541 |
|
|
"pio/dma flags 0x%02x ]\n", |
542 |
|
|
d->drive, d->seccnt); |
543 |
|
|
break; |
544 |
|
|
default: |
545 |
|
|
d->error |= WDCE_ABRT; |
546 |
|
|
} |
547 |
|
|
/* TODO: always interrupt? */ |
548 |
|
|
d->delayed_interrupt = INT_DELAY; |
549 |
|
|
break; |
550 |
|
|
case WDCC_RECAL: |
551 |
|
|
debug("[ wdc: RECAL drive %i ]\n", d->drive); |
552 |
|
|
d->delayed_interrupt = INT_DELAY; |
553 |
|
|
break; |
554 |
|
|
case WDCC_IDENTIFY: |
555 |
|
|
debug("[ wdc: IDENTIFY drive %i ]\n", d->drive); |
556 |
|
|
wdc_initialize_identify_struct(cpu, d); |
557 |
|
|
/* The IDENTIFY data block is sent out |
558 |
|
|
in low/high byte order: */ |
559 |
|
|
for (i=0; i<sizeof(d->identify_struct); i+=2) { |
560 |
|
|
wdc_addtoinbuf(d, d->identify_struct |
561 |
|
|
[i+1]); |
562 |
|
|
wdc_addtoinbuf(d, d->identify_struct |
563 |
|
|
[i+0]); |
564 |
|
|
} |
565 |
|
|
d->delayed_interrupt = INT_DELAY; |
566 |
|
|
break; |
567 |
|
|
case WDCC_IDLE_IMMED: |
568 |
|
|
debug("[ wdc: IDLE_IMMED drive %i ]\n", |
569 |
|
|
d->drive); |
570 |
|
|
/* TODO: interrupt here? */ |
571 |
|
|
d->delayed_interrupt = INT_DELAY; |
572 |
|
|
break; |
573 |
|
|
default: |
574 |
|
|
/* TODO */ |
575 |
|
|
d->error |= WDCE_ABRT; |
576 |
|
|
|
577 |
|
|
fatal("[ wdc: unknown command 0x%02x (" |
578 |
|
|
"drive %i, head %i, cylinder %i, sector %i," |
579 |
|
|
" nsecs %i) ]\n", d->cur_command, d->drive, |
580 |
|
|
d->head, d->cyl_hi*256+d->cyl_lo, |
581 |
|
|
d->sector, d->seccnt); |
582 |
|
|
exit(1); |
583 |
|
|
} |
584 |
|
|
} |
585 |
|
|
break; |
586 |
|
|
|
587 |
|
|
default: |
588 |
|
|
if (writeflag==MEM_READ) |
589 |
|
|
debug("[ wdc: read from 0x%02x ]\n", |
590 |
|
|
(int)relative_addr); |
591 |
|
|
else |
592 |
|
|
debug("[ wdc: write to 0x%02x: 0x%02x ]\n", |
593 |
|
|
(int)relative_addr, (int)idata); |
594 |
|
|
} |
595 |
|
|
|
596 |
|
|
if (writeflag == MEM_READ) |
597 |
|
|
memory_writemax64(cpu, data, len, odata); |
598 |
|
|
|
599 |
|
|
return 1; |
600 |
|
|
} |
601 |
|
|
|
602 |
|
|
|
603 |
|
|
/* |
604 |
|
|
* dev_wdc_init(): |
605 |
|
|
* |
606 |
|
|
* base_drive should be 0 for the primary device, and 2 for the secondary. |
607 |
|
|
*/ |
608 |
|
|
void dev_wdc_init(struct machine *machine, struct memory *mem, |
609 |
|
|
uint64_t baseaddr, int irq_nr, int base_drive) |
610 |
|
|
{ |
611 |
|
|
struct wdc_data *d; |
612 |
|
|
uint64_t alt_status_addr; |
613 |
|
|
|
614 |
|
|
d = malloc(sizeof(struct wdc_data)); |
615 |
|
|
if (d == NULL) { |
616 |
|
|
fprintf(stderr, "out of memory\n"); |
617 |
|
|
exit(1); |
618 |
|
|
} |
619 |
|
|
memset(d, 0, sizeof(struct wdc_data)); |
620 |
|
|
d->irq_nr = irq_nr; |
621 |
|
|
d->base_drive = base_drive; |
622 |
|
|
|
623 |
|
|
alt_status_addr = baseaddr + 0x206; |
624 |
|
|
|
625 |
|
|
/* Special hack for pcic/hpcmips: TODO: Fix */ |
626 |
|
|
if (baseaddr == 0x14000180) |
627 |
|
|
alt_status_addr = 0x14000386; |
628 |
|
|
|
629 |
|
|
memory_device_register(mem, "wdc_altstatus", alt_status_addr, 2, |
630 |
|
|
dev_wdc_altstatus_access, d, MEM_DEFAULT, NULL); |
631 |
|
|
memory_device_register(mem, "wdc", baseaddr, DEV_WDC_LENGTH, |
632 |
|
|
dev_wdc_access, d, MEM_DEFAULT, NULL); |
633 |
|
|
|
634 |
|
|
machine_add_tickfunction(machine, dev_wdc_tick, |
635 |
|
|
d, WDC_TICK_SHIFT); |
636 |
|
|
} |
637 |
|
|
|