/[gxemul]/trunk/src/devices/dev_wdc.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/devices/dev_wdc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 6 by dpavlin, Mon Oct 8 16:18:11 2007 UTC revision 20 by dpavlin, Mon Oct 8 16:19:23 2007 UTC
# Line 23  Line 23 
23   *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY   *  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   *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
  *    
26   *   *
27   *  $Id: dev_wdc.c,v 1.37 2005/05/27 07:29:25 debug Exp $   *
28   *     *  $Id: dev_wdc.c,v 1.56 2005/11/23 23:31:36 debug Exp $
29   *  Standard IDE controller.   *
30     *  Standard "wdc" IDE controller.
31   */   */
32    
33  #include <stdio.h>  #include <stdio.h>
34  #include <stdlib.h>  #include <stdlib.h>
35  #include <string.h>  #include <string.h>
36    
 #include "console.h"  
 #include "cop0.h"  
37  #include "cpu.h"  #include "cpu.h"
38  #include "devices.h"  #include "device.h"
39  #include "diskimage.h"  #include "diskimage.h"
40  #include "machine.h"  #include "machine.h"
41  #include "memory.h"  #include "memory.h"
# Line 45  Line 43 
43    
44  #include "wdcreg.h"  #include "wdcreg.h"
45    
46    #define DEV_WDC_LENGTH          8
47  #define WDC_TICK_SHIFT          14  #define WDC_TICK_SHIFT          14
48  #define WDC_INBUF_SIZE          (512*257)  #define WDC_MAX_SECTORS         512
49    #define WDC_INBUF_SIZE          (512*(WDC_MAX_SECTORS+1))
50    
51  /*  INT_DELAY=2 to be safe, 1 is faster but maybe buggy.  */  /*
52     *  INT_DELAY: This is an old hack which only exists because (some versions of)
53     *  NetBSD for hpcmips have interrupt problems. These problems are probably not
54     *  specific to GXemul, but are also triggered on real hardware.
55     *
56     *  See the following URL for more info:
57     *  http://mail-index.netbsd.org/port-hpcmips/2004/12/30/0003.html
58     *
59     *  NetBSD/malta also bugs out if wdc interrupts come too quickly. Hm.
60     */
61  #define INT_DELAY               1  #define INT_DELAY               1
62    
63  extern int quiet_mode;  extern int quiet_mode;
64    
65  /*  #define debug fatal  */  /*  #define debug fatal  */
 /*  #define DATA_DEBUG  */  
66    
67  struct wdc_data {  struct wdc_data {
68          int             irq_nr;          int             irq_nr;
69          int             base_drive;          int             base_drive;
70            int             data_debug;
71    
72          int             delayed_interrupt;          /*  Cached values:  */
73            int             cyls[2];
74          unsigned char   identify_struct[512];          int             heads[2];
75            int             sectors_per_track[2];
76    
77          unsigned char   inbuf[WDC_INBUF_SIZE];          unsigned char   *inbuf;
78          int             inbuf_head;          int             inbuf_head;
79          int             inbuf_tail;          int             inbuf_tail;
80    
81            int             delayed_interrupt;
82            int             int_asserted;
83    
84          int             write_in_progress;          int             write_in_progress;
85          int             write_count;          int             write_count;
86          int64_t         write_offset;          int64_t         write_offset;
# Line 84  struct wdc_data { Line 96  struct wdc_data {
96          int             drive;          int             drive;
97          int             head;          int             head;
98          int             cur_command;          int             cur_command;
99    
100            int             atapi_cmd_in_progress;
101            int             atapi_phase;
102            struct scsi_transfer *atapi_st;
103            int             atapi_len;
104            int             atapi_received;
105    
106            unsigned char   identify_struct[512];
107  };  };
108    
109    
110    #define COMMAND_RESET   0x100
111    
112    
113  /*  /*
114   *  dev_wdc_tick():   *  dev_wdc_tick():
115   */   */
116  void dev_wdc_tick(struct cpu *cpu, void *extra)  void dev_wdc_tick(struct cpu *cpu, void *extra)
117  {  {
118          struct wdc_data *d = extra;          struct wdc_data *d = extra;
119            int old_di = d->delayed_interrupt;
120    
121          if (d->delayed_interrupt) {          if (d->delayed_interrupt)
122                  d->delayed_interrupt --;                  d->delayed_interrupt --;
123    
124                  if (d->delayed_interrupt == 0)          if (old_di == 1 || d->int_asserted) {
125                          cpu_interrupt(cpu, d->irq_nr);                  cpu_interrupt(cpu, d->irq_nr);
126                    d->int_asserted = 1;
127          }          }
128  }  }
129    
# Line 114  static void wdc_addtoinbuf(struct wdc_da Line 139  static void wdc_addtoinbuf(struct wdc_da
139    
140          d->inbuf_head = (d->inbuf_head + 1) % WDC_INBUF_SIZE;          d->inbuf_head = (d->inbuf_head + 1) % WDC_INBUF_SIZE;
141          if (d->inbuf_head == d->inbuf_tail)          if (d->inbuf_head == d->inbuf_tail)
142                  fatal("[ wdc_addtoinbuf(): WARNING! wdc inbuf overrun ]\n");                  fatal("[ wdc_addtoinbuf(): WARNING! wdc inbuf overrun!"
143                        " Increase WDC_MAX_SECTORS. ]\n");
144  }  }
145    
146    
# Line 139  static uint64_t wdc_get_inbuf(struct wdc Line 165  static uint64_t wdc_get_inbuf(struct wdc
165    
166    
167  /*  /*
168   *  wdc_initialize_identify_struct(d):   *  wdc_initialize_identify_struct():
169   */   */
170  static void wdc_initialize_identify_struct(struct cpu *cpu, struct wdc_data *d)  static void wdc_initialize_identify_struct(struct cpu *cpu, struct wdc_data *d)
171  {  {
172          uint64_t total_size;          uint64_t total_size;
173          int cyls, heads, sectors_per_track;          int flags, cdrom = 0;
174            char namebuf[40];
175    
176          total_size = diskimage_getsize(cpu->machine, d->drive + d->base_drive,          total_size = diskimage_getsize(cpu->machine, d->drive + d->base_drive,
177              DISKIMAGE_IDE);              DISKIMAGE_IDE);
178            if (diskimage_is_a_cdrom(cpu->machine, d->drive + d->base_drive,
179          diskimage_getchs(cpu->machine, d->drive + d->base_drive,              DISKIMAGE_IDE))
180              DISKIMAGE_IDE, &cyls, &heads, &sectors_per_track);                  cdrom = 1;
181    
182          memset(d->identify_struct, 0, sizeof(d->identify_struct));          memset(d->identify_struct, 0, sizeof(d->identify_struct));
183    
184          /*  Offsets are in 16-bit WORDS!  High byte, then low.  */          /*  Offsets are in 16-bit WORDS!  High byte, then low.  */
185    
186          /*  0: general flags  */          /*  0: general flags  */
187          d->identify_struct[2 * 0 + 0] = 0;          flags = 1 << 6; /*  Fixed  */
188          d->identify_struct[2 * 0 + 1] = 1 << 6;          if (cdrom)
189                    flags = 0x8580;         /*  ATAPI, CDROM, removable  */
190            d->identify_struct[2 * 0 + 0] = flags >> 8;
191            d->identify_struct[2 * 0 + 1] = flags;
192    
193          /*  1: nr of cylinders  */          /*  1: nr of cylinders  */
194          d->identify_struct[2 * 1 + 0] = (cyls >> 8);          d->identify_struct[2 * 1 + 0] = d->cyls[d->drive] >> 8;
195          d->identify_struct[2 * 1 + 1] = cyls & 255;          d->identify_struct[2 * 1 + 1] = d->cyls[d->drive];
196    
197          /*  3: nr of heads  */          /*  3: nr of heads  */
198          d->identify_struct[2 * 3 + 0] = heads >> 8;          d->identify_struct[2 * 3 + 0] = d->heads[d->drive] >> 8;
199          d->identify_struct[2 * 3 + 1] = heads;          d->identify_struct[2 * 3 + 1] = d->heads[d->drive];
200    
201          /*  6: sectors per track  */          /*  6: sectors per track  */
202          d->identify_struct[2 * 6 + 0] = sectors_per_track >> 8;          d->identify_struct[2 * 6 + 0] = d->sectors_per_track[d->drive] >> 8;
203          d->identify_struct[2 * 6 + 1] = sectors_per_track;          d->identify_struct[2 * 6 + 1] = d->sectors_per_track[d->drive];
204    
205          /*  10-19: Serial number  */          /*  10-19: Serial number  */
206          memcpy(&d->identify_struct[2 * 10], "S/N 1234-5678       ", 20);          memcpy(&d->identify_struct[2 * 10], "#0                  ", 20);
207    
208          /*  23-26: Firmware version  */          /*  23-26: Firmware version  */
209          memcpy(&d->identify_struct[2 * 23], "VER 1.0 ", 8);          memcpy(&d->identify_struct[2 * 23], "1.0     ", 8);
210    
211          /*  27-46: Model number  */          /*  27-46: Model number  */
212          memcpy(&d->identify_struct[2 * 27],          if (diskimage_getname(cpu->machine, d->drive + d->base_drive,
213              "Fake GXemul IDE disk                    ", 40);              DISKIMAGE_IDE, namebuf, sizeof(namebuf))) {
214          /*  TODO:  Use the diskimage's filename instead?  */                  int i;
215                    for (i=0; i<sizeof(namebuf); i++)
216                            if (namebuf[i] == 0) {
217                                    for (; i<sizeof(namebuf); i++)
218                                            namebuf[i] = ' ';
219                                    break;
220                            }
221                    memcpy(&d->identify_struct[2 * 27], namebuf, 40);
222            } else
223                    memcpy(&d->identify_struct[2 * 27],
224                        "Fake GXemul IDE disk                    ", 40);
225    
226          /*  47: max sectors per multitransfer  */          /*  47: max sectors per multitransfer  */
227          d->identify_struct[2 * 47 + 0] = 0x80;          d->identify_struct[2 * 47 + 0] = 0x80;
228          d->identify_struct[2 * 47 + 1] = 1;     /*  1 or 16?  */          d->identify_struct[2 * 47 + 1] = 128;
229    
230            /*  49: capabilities:  */
231            /*  (0x200 = LBA, 0x100 = DMA support.)  */
232            d->identify_struct[2 * 49 + 0] = 0;
233            d->identify_struct[2 * 49 + 1] = 0;
234    
235            /*  51: PIO timing mode.  */
236            d->identify_struct[2 * 51 + 0] = 0x00;  /*  ?  */
237            d->identify_struct[2 * 51 + 1] = 0x00;
238    
239            /*  53: 0x02 = fields 64-70 valid, 0x01 = fields 54-58 valid  */
240            d->identify_struct[2 * 53 + 0] = 0x00;
241            d->identify_struct[2 * 53 + 1] = 0x02;
242    
243          /*  57-58: current capacity in sectors  */          /*  57-58: current capacity in sectors  */
244          d->identify_struct[2 * 57 + 0] = ((total_size / 512) >> 24) % 255;          d->identify_struct[2 * 57 + 0] = ((total_size / 512) >> 24) % 255;
# Line 193  static void wdc_initialize_identify_stru Line 246  static void wdc_initialize_identify_stru
246          d->identify_struct[2 * 58 + 0] = ((total_size / 512) >> 8) % 255;          d->identify_struct[2 * 58 + 0] = ((total_size / 512) >> 8) % 255;
247          d->identify_struct[2 * 58 + 1] = (total_size / 512) & 255;          d->identify_struct[2 * 58 + 1] = (total_size / 512) & 255;
248    
249          /*  60-61: total nr of addresable sectors  */          /*  60-61: total nr of addressable sectors  */
250          d->identify_struct[2 * 60 + 0] = ((total_size / 512) >> 24) % 255;          d->identify_struct[2 * 60 + 0] = ((total_size / 512) >> 24) % 255;
251          d->identify_struct[2 * 60 + 1] = ((total_size / 512) >> 16) % 255;          d->identify_struct[2 * 60 + 1] = ((total_size / 512) >> 16) % 255;
252          d->identify_struct[2 * 61 + 0] = ((total_size / 512) >> 8) % 255;          d->identify_struct[2 * 61 + 0] = ((total_size / 512) >> 8) % 255;
253          d->identify_struct[2 * 61 + 1] = (total_size / 512) & 255;          d->identify_struct[2 * 61 + 1] = (total_size / 512) & 255;
254    
255            /*  64: Advanced PIO mode support. 0x02 = mode4, 0x01 = mode3  */
256            d->identify_struct[2 * 64 + 0] = 0x00;
257            d->identify_struct[2 * 64 + 1] = 0x03;
258    
259            /*  67, 68: PIO timing  */
260            d->identify_struct[2 * 67 + 0] = 0;
261            d->identify_struct[2 * 67 + 1] = 120;
262            d->identify_struct[2 * 68 + 0] = 0;
263            d->identify_struct[2 * 68 + 1] = 120;
264    }
265    
266    
267    /*
268     *  wdc__read():
269     */
270    void wdc__read(struct cpu *cpu, struct wdc_data *d)
271    {
272    #define MAX_SECTORS_PER_CHUNK   64
273            const int max_sectors_per_chunk = MAX_SECTORS_PER_CHUNK;
274            unsigned char buf[512 * MAX_SECTORS_PER_CHUNK];
275            int i, cyl = d->cyl_hi * 256+ d->cyl_lo;
276            int count = d->seccnt? d->seccnt : 256;
277            uint64_t offset = 512 * (d->sector - 1
278                + d->head * d->sectors_per_track[d->drive] +
279                d->heads[d->drive] * d->sectors_per_track[d->drive] * cyl);
280    
281    #if 0
282            /*  LBA:  */
283            if (d->lba)
284                    offset = 512 * (((d->head & 0xf) << 24) + (cyl << 8)
285                        + d->sector);
286            printf("WDC read from offset %lli\n", (long long)offset);
287    #endif
288    
289            while (count > 0) {
290                    int to_read = count > max_sectors_per_chunk?
291                        max_sectors_per_chunk : count;
292    
293                    /*  TODO: result code from the read?  */
294    
295                    if (d->inbuf_head + 512 * to_read <= WDC_INBUF_SIZE) {
296                            diskimage_access(cpu->machine, d->drive + d->base_drive,
297                                DISKIMAGE_IDE, 0, offset,
298                                d->inbuf + d->inbuf_head, 512 * to_read);
299                            d->inbuf_head += 512 * to_read;
300                            if (d->inbuf_head == WDC_INBUF_SIZE)
301                                    d->inbuf_head = 0;
302                    } else {
303                            diskimage_access(cpu->machine, d->drive + d->base_drive,
304                                DISKIMAGE_IDE, 0, offset, buf, 512 * to_read);
305                            for (i=0; i<512 * to_read; i++)
306                                    wdc_addtoinbuf(d, buf[i]);
307                    }
308    
309                    offset += 512 * to_read;
310                    count -= to_read;
311            }
312    
313            d->delayed_interrupt = INT_DELAY;
314    }
315    
316    
317    /*
318     *  wdc__write():
319     */
320    void wdc__write(struct cpu *cpu, struct wdc_data *d)
321    {
322            int cyl = d->cyl_hi * 256+ d->cyl_lo;
323            int count = d->seccnt? d->seccnt : 256;
324            uint64_t offset = 512 * (d->sector - 1
325                + d->head * d->sectors_per_track[d->drive] +
326                d->heads[d->drive] * d->sectors_per_track[d->drive] * cyl);
327    #if 0
328            /*  LBA:  */
329            if (d->lba)
330                    offset = 512 * (((d->head & 0xf) << 24) +
331                        (cyl << 8) + d->sector);
332            printf("WDC write to offset %lli\n", (long long)offset);
333    #endif
334    
335            d->write_in_progress = d->cur_command;
336            d->write_count = count;
337            d->write_offset = offset;
338    
339            /*  TODO: result code?  */
340  }  }
341    
342    
343  /*  /*
344   *  status_byte():   *  status_byte():
345     *
346     *  Return a reasonable status byte corresponding to the controller's current
347     *  state.
348   */   */
349  static int status_byte(struct wdc_data *d, struct cpu *cpu)  static int status_byte(struct wdc_data *d, struct cpu *cpu)
350  {  {
351          int odata = 0;          int odata = 0;
   
352          if (diskimage_exist(cpu->machine, d->drive + d->base_drive,          if (diskimage_exist(cpu->machine, d->drive + d->base_drive,
353              DISKIMAGE_IDE))              DISKIMAGE_IDE))
354                  odata |= WDCS_DRDY;                  odata |= WDCS_DRDY | WDCS_DSC;
355          if (d->inbuf_head != d->inbuf_tail)          if (d->inbuf_head != d->inbuf_tail)
356                  odata |= WDCS_DRQ;                  odata |= WDCS_DRQ;
357          if (d->write_in_progress)          if (d->write_in_progress)
358                  odata |= WDCS_DRQ;                  odata |= WDCS_DRQ;
359          if (d->error)          if (d->error)
360                  odata |= WDCS_ERR;                  odata |= WDCS_ERR;
361            if (d->atapi_cmd_in_progress && (d->atapi_phase & WDCS_DRQ)) {
362  #if 0                  odata |= WDCS_DRQ;
363          /*          }
          *  TODO:  Is this correct behaviour?  
          *  
          *  NetBSD/cobalt seems to want it, but Linux on MobilePro does not.  
          */  
         if (!diskimage_exist(cpu->machine, d->drive + d->base_drive,  
             DISKIMAGE_IDE))  
                 odata = 0xff;  
 #endif  
   
364          return odata;          return odata;
365  }  }
366    
# Line 244  int dev_wdc_altstatus_access(struct cpu Line 375  int dev_wdc_altstatus_access(struct cpu
375          struct wdc_data *d = extra;          struct wdc_data *d = extra;
376          uint64_t idata = 0, odata = 0;          uint64_t idata = 0, odata = 0;
377    
378          idata = memory_readmax64(cpu, data, len);          idata = data[0];
379    
380          /*  Same as the normal status byte?  */          /*  Same as the normal status byte:  */
381          odata = status_byte(d, cpu);          odata = status_byte(d, cpu);
382    
383          if (writeflag==MEM_READ)          if (writeflag==MEM_READ)
384                  debug("[ wdc: read from ALTSTATUS: 0x%02x ]\n",                  debug("[ wdc: read from ALTSTATUS: 0x%02x ]\n",
385                      (int)odata);                      (int)odata);
386          else          else {
387                  debug("[ wdc: write to ALT. CTRL: 0x%02x ]\n",                  debug("[ wdc: write to ALT. CTRL: 0x%02x ]\n",
388                      (int)idata);                      (int)idata);
389                    if (idata & WDCTL_4BIT)
390                            d->cur_command = COMMAND_RESET;
391            }
392    
393          if (writeflag == MEM_READ)          if (writeflag == MEM_READ)
394                  memory_writemax64(cpu, data, len, odata);                  data[0] = odata;
395    
396          return 1;          return 1;
397  }  }
398    
399    
400  /*  /*
401     *  wdc_command():
402     */
403    void wdc_command(struct cpu *cpu, struct wdc_data *d, int idata)
404    {
405            int i;
406    
407            d->cur_command = idata;
408            d->atapi_cmd_in_progress = 0;
409            d->error = 0;
410    
411            /*
412             *  Disk images that do not exist return an ABORT error.  This also
413             *  happens with CDROM images with the WDCC_IDENTIFY command; CDROM
414             *  images must be detected with ATAPI_IDENTIFY_DEVICE instead.
415             *
416             *  TODO:  Is this correct/good behaviour?
417             */
418            if (!diskimage_exist(cpu->machine, d->drive + d->base_drive,
419                DISKIMAGE_IDE)) {
420                    debug("[ wdc: command 0x%02x drive %i, but no disk image ]\n",
421                        d->cur_command, d->drive + d->base_drive);
422                    d->error |= WDCE_ABRT;
423                    d->delayed_interrupt = INT_DELAY;
424                    return;
425            }
426            if (diskimage_is_a_cdrom(cpu->machine, d->drive + d->base_drive,
427                DISKIMAGE_IDE) && d->cur_command == WDCC_IDENTIFY) {
428                    debug("[ wdc: IDENTIFY drive %i, but it is an ATAPI "
429                        "drive ]\n", d->drive + d->base_drive);
430                    d->error |= WDCE_ABRT;
431                    d->delayed_interrupt = INT_DELAY;
432                    return;
433            }
434    
435            /*  Handle the command:  */
436            switch (d->cur_command) {
437    
438            case WDCC_READ:
439            case WDCC_READMULTI:
440                    if (!quiet_mode)
441                            debug("[ wdc: READ from drive %i, head %i, cyl %i, "
442                                "sector %i, nsecs %i ]\n", d->drive, d->head,
443                                d->cyl_hi*256+d->cyl_lo, d->sector, d->seccnt);
444                    wdc__read(cpu, d);
445                    break;
446    
447            case WDCC_WRITE:
448            case WDCC_WRITEMULTI:
449                    if (!quiet_mode)
450                            debug("[ wdc: WRITE to drive %i, head %i, cyl %i, "
451                                "sector %i, nsecs %i ]\n", d->drive, d->head,
452                                d->cyl_hi*256+d->cyl_lo, d->sector, d->seccnt);
453                    wdc__write(cpu, d);
454                    break;
455    
456            case WDCC_IDP:  /*  Initialize drive parameters  */
457                    debug("[ wdc: IDP drive %i (TODO) ]\n", d->drive);
458                    /*  TODO  */
459                    d->delayed_interrupt = INT_DELAY;
460                    break;
461    
462            case SET_FEATURES:
463                    debug("[ wdc: SET_FEATURES drive %i (TODO), feature 0x%02x ]\n",
464                        d->drive, d->precomp);
465                    /*  TODO  */
466                    switch (d->precomp) {
467                    case WDSF_SET_MODE:
468                            debug("[ wdc: WDSF_SET_MODE drive %i, pio/dma flags "
469                                "0x%02x ]\n", d->drive, d->seccnt);
470                            break;
471                    default:d->error |= WDCE_ABRT;
472                    }
473                    /*  TODO: always interrupt?  */
474                    d->delayed_interrupt = INT_DELAY;
475                    break;
476    
477            case WDCC_RECAL:
478                    debug("[ wdc: RECAL drive %i ]\n", d->drive);
479                    d->delayed_interrupt = INT_DELAY;
480                    break;
481    
482            case WDCC_IDENTIFY:
483            case ATAPI_IDENTIFY_DEVICE:
484                    debug("[ wdc: %sIDENTIFY drive %i ]\n", d->cur_command ==
485                        ATAPI_IDENTIFY_DEVICE? "ATAPI " : "", d->drive);
486                    wdc_initialize_identify_struct(cpu, d);
487                    /*  The IDENTIFY data is sent out in low/high byte order:  */
488                    for (i=0; i<sizeof(d->identify_struct); i+=2) {
489                            wdc_addtoinbuf(d, d->identify_struct[i+1]);
490                            wdc_addtoinbuf(d, d->identify_struct[i+0]);
491                    }
492                    d->delayed_interrupt = INT_DELAY;
493                    break;
494    
495            case WDCC_IDLE_IMMED:
496                    debug("[ wdc: IDLE_IMMED drive %i ]\n", d->drive);
497                    /*  TODO: interrupt here?  */
498                    d->delayed_interrupt = INT_DELAY;
499                    break;
500    
501            case WDCC_SETMULTI:
502                    debug("[ wdc: SETMULTI drive %i ]\n", d->drive);
503                    /*  TODO: interrupt here?  */
504                    d->delayed_interrupt = INT_DELAY;
505                    break;
506    
507            case ATAPI_SOFT_RESET:
508                    debug("[ wdc: ATAPI_SOFT_RESET drive %i ]\n", d->drive);
509                    /*  TODO: interrupt here?  */
510                    d->delayed_interrupt = INT_DELAY;
511                    break;
512    
513            case ATAPI_PKT_CMD:
514                    debug("[ wdc: ATAPI_PKT_CMD drive %i ]\n", d->drive);
515                    /*  TODO: interrupt here?  */
516                    /*  d->delayed_interrupt = INT_DELAY;  */
517                    d->atapi_cmd_in_progress = 1;
518                    d->atapi_phase = PHASE_CMDOUT;
519                    break;
520    
521            /*  Unsupported commands, without warning:  */
522            case WDCC_SEC_SET_PASSWORD:
523            case WDCC_SEC_UNLOCK:
524            case WDCC_SEC_ERASE_PREPARE:
525            case WDCC_SEC_ERASE_UNIT:
526            case WDCC_SEC_FREEZE_LOCK:
527            case WDCC_SEC_DISABLE_PASSWORD:
528                    d->error |= WDCE_ABRT;
529                    break;
530    
531            default:/*  TODO  */
532                    d->error |= WDCE_ABRT;
533                    fatal("[ wdc: WARNING! Unimplemented command 0x%02x (drive %i,"
534                        " head %i, cyl %i, sector %i, nsecs %i) ]\n",
535                        d->cur_command, d->drive, d->head, d->cyl_hi*256+d->cyl_lo,
536                        d->sector, d->seccnt);
537            }
538    }
539    
540    
541    /*
542   *  dev_wdc_access():   *  dev_wdc_access():
543   */   */
544  int dev_wdc_access(struct cpu *cpu, struct memory *mem,  int dev_wdc_access(struct cpu *cpu, struct memory *mem,
# Line 272  int dev_wdc_access(struct cpu *cpu, stru Line 547  int dev_wdc_access(struct cpu *cpu, stru
547  {  {
548          struct wdc_data *d = extra;          struct wdc_data *d = extra;
549          uint64_t idata = 0, odata = 0;          uint64_t idata = 0, odata = 0;
550          int i, cyls, heads, sectors_per_track;          int i;
551    
552          idata = memory_readmax64(cpu, data, len);          if (writeflag == MEM_WRITE) {
553                    if (relative_addr == wd_data)
554                            idata = memory_readmax64(cpu, data, len);
555                    else {
556                            if (len != 1)
557                                    fatal("[ wdc: WARNING! non-8-bit access! ]\n");
558                            idata = data[0];
559                    }
560            }
561    
562          switch (relative_addr) {          switch (relative_addr) {
563    
564          case wd_data:   /*  0: data  */          case wd_data:   /*  0: data  */
565                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
566                          odata = 0;                          odata = 0;
567    
                         /*  TODO: This is hardcoded for little-endian?  */  
   
568                          odata += wdc_get_inbuf(d);                          odata += wdc_get_inbuf(d);
                         if (len >= 2)  
                                 odata += (wdc_get_inbuf(d) << 8);  
                         if (len == 4) {  
                                 odata += (wdc_get_inbuf(d) << 16);  
                                 odata += (wdc_get_inbuf(d) << 24);  
                         }  
569    
570  #ifdef DATA_DEBUG                          if (cpu->byte_order == EMUL_LITTLE_ENDIAN) {
571                          debug("[ wdc: read from DATA: 0x%04x ]\n", odata);                                  if (len >= 2)
572  #endif                                          odata += (wdc_get_inbuf(d) << 8);
573                                    if (len == 4) {
574                                            odata += (wdc_get_inbuf(d) << 16);
575                                            odata += (wdc_get_inbuf(d) << 24);
576                                    }
577                            } else {
578                                    if (len >= 2)
579                                            odata = (odata << 8) + wdc_get_inbuf(d);
580                                    if (len == 4) {
581                                            odata = (odata << 8) + wdc_get_inbuf(d);
582                                            odata = (odata << 8) + wdc_get_inbuf(d);
583                                    }
584                            }
585    
586                          if (d->inbuf_tail != d->inbuf_head)                          if (d->data_debug) {
587                                  d->delayed_interrupt = INT_DELAY;                                  char *s = "0x%04llx ]\n";
588                                    if (len == 1)
589                                            s = "0x%02llx ]\n";
590                                    if (len == 4)
591                                            s = "0x%08llx ]\n";
592                                    if (len == 8)
593                                            s = "0x%016llx ]\n";
594                                    debug("[ wdc: read from DATA: ");
595                                    debug(s, (long long)odata);
596                            }
597    
598                            if (d->atapi_cmd_in_progress) {
599                                    d->atapi_len -= len;
600                                    d->atapi_received += len;
601                                    if (d->atapi_len == 0) {
602                                            if (d->atapi_received < d->atapi_st->
603                                                data_in_len) {
604                                                    d->atapi_phase = PHASE_DATAIN;
605                                                    d->atapi_len = d->atapi_st->
606                                                        data_in_len -
607                                                        d->atapi_received;
608                                                    if (d->atapi_len > 32768)
609                                                            d->atapi_len = 0;
610                                            } else
611                                                    d->atapi_phase =
612                                                        PHASE_COMPLETED;
613                                            d->delayed_interrupt = INT_DELAY;
614                                    }
615                            } else {
616    #if 0
617                                    if (d->inbuf_tail != d->inbuf_head)
618    #else
619                                    if (d->inbuf_tail != d->inbuf_head &&
620                                        ((d->inbuf_tail - d->inbuf_head) % 512)
621                                        == 0)
622    #endif
623                                            d->delayed_interrupt = INT_DELAY;
624                            }
625                  } else {                  } else {
626                          int inbuf_len;                          int inbuf_len;
627  #ifdef DATA_DEBUG                          if (d->data_debug) {
628                          debug("[ wdc: write to DATA (len=%i): 0x%08lx ]\n",                                  char *s = "0x%04llx ]\n";
629                              (int)len, (long)idata);                                  if (len == 1)
630  #endif                                          s = "0x%02llx ]\n";
631                          if (!d->write_in_progress) {                                  if (len == 4)
632                                            s = "0x%08llx ]\n";
633                                    if (len == 8)
634                                            s = "0x%016llx ]\n";
635                                    debug("[ wdc: write to DATA: ");
636                                    debug(s, (long long)idata);
637                            }
638                            if (!d->write_in_progress &&
639                                !d->atapi_cmd_in_progress) {
640                                  fatal("[ wdc: write to DATA, but not "                                  fatal("[ wdc: write to DATA, but not "
641                                      "expecting any? (len=%i): 0x%08lx ]\n",                                      "expecting any? (len=%i): 0x%08lx ]\n",
642                                      (int)len, (long)idata);                                      (int)len, (long)idata);
643                          }                          }
644    
645                          switch (len) {                          if (cpu->byte_order == EMUL_LITTLE_ENDIAN) {
646                          case 4: wdc_addtoinbuf(d, idata & 0xff);                                  switch (len) {
647                                  wdc_addtoinbuf(d, (idata >> 8) & 0xff);                                  case 4: wdc_addtoinbuf(d, idata & 0xff);
648                                  wdc_addtoinbuf(d, (idata >> 16) & 0xff);                                          wdc_addtoinbuf(d, (idata >> 8) & 0xff);
649                                  wdc_addtoinbuf(d, (idata >> 24) & 0xff);                                          wdc_addtoinbuf(d, (idata >> 16) & 0xff);
650                                  break;                                          wdc_addtoinbuf(d, (idata >> 24) & 0xff);
651                          case 2: wdc_addtoinbuf(d, idata & 0xff);                                          break;
652                                  wdc_addtoinbuf(d, (idata >> 8) & 0xff);                                  case 2: wdc_addtoinbuf(d, idata & 0xff);
653                                  break;                                          wdc_addtoinbuf(d, (idata >> 8) & 0xff);
654                          case 1: wdc_addtoinbuf(d, idata); break;                                          break;
655                          default:fatal("wdc: unimplemented write len %i\n", len);                                  case 1: wdc_addtoinbuf(d, idata); break;
656                                  exit(1);                                  default:fatal("wdc: unimplemented write "
657                                                "len %i\n", len);
658                                            exit(1);
659                                    }
660                            } else {
661                                    switch (len) {
662                                    case 4: wdc_addtoinbuf(d, (idata >> 24) & 0xff);
663                                            wdc_addtoinbuf(d, (idata >> 16) & 0xff);
664                                            wdc_addtoinbuf(d, (idata >> 8) & 0xff);
665                                            wdc_addtoinbuf(d, idata & 0xff);
666                                            break;
667                                    case 2: wdc_addtoinbuf(d, (idata >> 8) & 0xff);
668                                            wdc_addtoinbuf(d, idata & 0xff);
669                                            break;
670                                    case 1: wdc_addtoinbuf(d, idata); break;
671                                    default:fatal("wdc: unimplemented write "
672                                                "len %i\n", len);
673                                            exit(1);
674                                    }
675                          }                          }
676    
677                          inbuf_len = d->inbuf_head - d->inbuf_tail;                          inbuf_len = d->inbuf_head - d->inbuf_tail;
678                          while (inbuf_len < 0)                          while (inbuf_len < 0)
679                                  inbuf_len += WDC_INBUF_SIZE;                                  inbuf_len += WDC_INBUF_SIZE;
680    
681  #if 0                          if (d->atapi_cmd_in_progress && inbuf_len == 12) {
682                          if ((inbuf_len % (512 * d->write_count)) == 0) {                                  unsigned char *scsi_cmd = malloc(12);
683  #endif                                  int x = 0, res;
684                          if ((inbuf_len % 512) == 0) {  
685                                  int count = 1;  /*  d->write_count;  */                                  if (d->atapi_st != NULL)
686                                  unsigned char *buf = malloc(count * 512);                                          scsi_transfer_free(d->atapi_st);
687                                  if (buf == NULL) {                                  d->atapi_st = scsi_transfer_alloc();
688                                          fprintf(stderr, "out of memory\n");  
689                                    debug("[ wdc: ATAPI command ]\n");
690    
691                                    while (inbuf_len > 0) {
692                                            scsi_cmd[x++] = wdc_get_inbuf(d);
693                                            inbuf_len --;
694                                    }
695    
696                                    d->atapi_st->cmd = scsi_cmd;
697                                    d->atapi_st->cmd_len = 12;
698    
699                                    if (scsi_cmd[0] == SCSIBLOCKCMD_READ_CAPACITY
700                                        || scsi_cmd[0] == SCSICMD_READ_10
701                                        || scsi_cmd[0] == SCSICMD_MODE_SENSE10)
702                                            d->atapi_st->cmd_len = 10;
703    
704                                    res = diskimage_scsicommand(cpu,
705                                        d->drive + d->base_drive, DISKIMAGE_IDE,
706                                        d->atapi_st);
707    
708                                    if (res == 0) {
709                                            fatal("WDC: ATAPI scsi error?\n");
710                                          exit(1);                                          exit(1);
711                                  }                                  }
712    
713                                  for (i=0; i<512 * count; i++)                                  d->atapi_len = 0;
714                                          buf[i] = wdc_get_inbuf(d);                                  d->atapi_received = 0;
715    
716                                    if (res == 1) {
717                                            if (d->atapi_st->data_in != NULL) {
718                                                    int i;
719                                                    d->atapi_phase = PHASE_DATAIN;
720                                                    d->atapi_len = d->atapi_st->
721                                                        data_in_len;
722                                                    for (i=0; i<d->atapi_len; i++)
723                                                            wdc_addtoinbuf(d,
724                                                                d->atapi_st->
725                                                                data_in[i]);
726                                                    if (d->atapi_len > 32768)
727                                                            d->atapi_len = 32768;
728                                            } else {
729                                                    d->atapi_phase =
730                                                        PHASE_COMPLETED;
731                                            }
732                                    } else {
733                                            fatal("wdc atapi Dataout? TODO\n");
734                                            d->atapi_phase = PHASE_DATAOUT;
735                                            exit(1);
736                                    }
737    
738                                    d->delayed_interrupt = INT_DELAY;
739                            }
740    
741                            if (( d->write_in_progress == WDCC_WRITEMULTI &&
742                                inbuf_len % (512 * d->write_count) == 0)
743                                ||
744                                ( d->write_in_progress == WDCC_WRITE &&
745                                inbuf_len % 512 == 0) ) {
746                                    int count = (d->write_in_progress ==
747                                        WDCC_WRITEMULTI)? d->write_count : 1;
748                                    unsigned char buf[512 * count];
749                                    unsigned char *b = buf;
750    
751                                    if (d->inbuf_tail+512*count <= WDC_INBUF_SIZE) {
752                                            b = d->inbuf + d->inbuf_tail;
753                                            d->inbuf_tail = (d->inbuf_tail + 512
754                                                * count) % WDC_INBUF_SIZE;
755                                    } else {
756                                            for (i=0; i<512 * count; i++)
757                                                    buf[i] = wdc_get_inbuf(d);
758                                    }
759    
760                                  diskimage_access(cpu->machine,                                  diskimage_access(cpu->machine,
761                                      d->drive + d->base_drive, DISKIMAGE_IDE, 1,                                      d->drive + d->base_drive, DISKIMAGE_IDE, 1,
762                                      d->write_offset, buf, 512 * count);                                      d->write_offset, b, 512 * count);
                                 free(buf);  
763    
764                                  d->write_count --;                                  d->write_count -= count;
765                                  d->write_offset += 512;                                  d->write_offset += 512 * count;
766    
767                                  d->delayed_interrupt = INT_DELAY;                                  d->delayed_interrupt = INT_DELAY;
768    
# Line 360  int dev_wdc_access(struct cpu *cpu, stru Line 773  int dev_wdc_access(struct cpu *cpu, stru
773                  break;                  break;
774    
775          case wd_error:  /*  1: error (r), precomp (w)  */          case wd_error:  /*  1: error (r), precomp (w)  */
776                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
777                          odata = d->error;                          odata = d->error;
778                          debug("[ wdc: read from ERROR: 0x%02x ]\n", odata);                          debug("[ wdc: read from ERROR: 0x%02x ]\n",
779                                (int)odata);
780                          /*  TODO:  is the error value cleared on read?  */                          /*  TODO:  is the error value cleared on read?  */
781                          d->error = 0;                          d->error = 0;
782                  } else {                  } else {
783                          d->precomp = idata;                          d->precomp = idata;
784                          debug("[ wdc: write to PRECOMP: 0x%02x ]\n", idata);                          debug("[ wdc: write to PRECOMP: 0x%02x ]\n",(int)idata);
785                  }                  }
786                  break;                  break;
787    
788          case wd_seccnt: /*  2: sector count  */          case wd_seccnt: /*  2: sector count (or "ireason" for ATAPI)  */
789                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
790                          odata = d->seccnt;                          odata = d->seccnt;
791                          debug("[ wdc: read from SECCNT: 0x%02x ]\n", odata);                          if (d->atapi_cmd_in_progress) {
792                                    odata = d->atapi_phase & (WDCI_CMD | WDCI_IN);
793                            }
794                            debug("[ wdc: read from SECCNT: 0x%02x ]\n",(int)odata);
795                  } else {                  } else {
796                          d->seccnt = idata;                          d->seccnt = idata;
797                          debug("[ wdc: write to SECCNT: 0x%02x ]\n", idata);                          debug("[ wdc: write to SECCNT: 0x%02x ]\n", (int)idata);
798                  }                  }
799                  break;                  break;
800    
801          case wd_sector: /*  3: first sector  */          case wd_sector: /*  3: first sector  */
802                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
803                          odata = d->sector;                          odata = d->sector;
804                          debug("[ wdc: read from SECTOR: 0x%02x ]\n", odata);                          debug("[ wdc: read from SECTOR: 0x%02x ]\n",(int)odata);
805                  } else {                  } else {
806                          d->sector = idata;                          d->sector = idata;
807                          debug("[ wdc: write to SECTOR: 0x%02x ]\n", idata);                          debug("[ wdc: write to SECTOR: 0x%02x ]\n", (int)idata);
808                  }                  }
809                  break;                  break;
810    
811          case wd_cyl_lo: /*  4: cylinder low  */          case wd_cyl_lo: /*  4: cylinder low  */
812                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
813                          odata = d->cyl_lo;                          odata = d->cyl_lo;
814                          debug("[ wdc: read from CYL_LO: 0x%02x ]\n", odata);                          if (d->cur_command == COMMAND_RESET &&
815                                diskimage_is_a_cdrom(cpu->machine,
816                                d->drive + d->base_drive, DISKIMAGE_IDE))
817                                    odata = 0x14;
818                            if (d->atapi_cmd_in_progress) {
819                                    int x = d->atapi_len;
820                                    if (x > 32768)
821                                            x = 32768;
822                                    odata = x & 255;
823                            }
824                            debug("[ wdc: read from CYL_LO: 0x%02x ]\n",(int)odata);
825                  } else {                  } else {
826                          d->cyl_lo = idata;                          d->cyl_lo = idata;
827                          debug("[ wdc: write to CYL_LO: 0x%02x ]\n", idata);                          debug("[ wdc: write to CYL_LO: 0x%02x ]\n", (int)idata);
828                  }                  }
829                  break;                  break;
830    
831          case wd_cyl_hi: /*  5: cylinder low  */          case wd_cyl_hi: /*  5: cylinder high  */
832                  if (writeflag==MEM_READ) {                  if (writeflag == MEM_READ) {
833                          odata = d->cyl_hi;                          odata = d->cyl_hi;
834                          debug("[ wdc: read from CYL_HI: 0x%02x ]\n", odata);                          if (d->cur_command == COMMAND_RESET &&
835                                diskimage_is_a_cdrom(cpu->machine,
836                                d->drive + d->base_drive, DISKIMAGE_IDE))
837                                    odata = 0xeb;
838                            if (d->atapi_cmd_in_progress) {
839                                    int x = d->atapi_len;
840                                    if (x > 32768)
841                                            x = 32768;
842                                    odata = (x >> 8) & 255;
843                            }
844                            debug("[ wdc: read from CYL_HI: 0x%02x ]\n",(int)odata);
845                  } else {                  } else {
846                          d->cyl_hi = idata;                          d->cyl_hi = idata;
847                          debug("[ wdc: write to CYL_HI: 0x%02x ]\n", idata);                          debug("[ wdc: write to CYL_HI: 0x%02x ]\n", (int)idata);
848                  }                  }
849                  break;                  break;
850    
# Line 432  int dev_wdc_access(struct cpu *cpu, stru Line 869  int dev_wdc_access(struct cpu *cpu, stru
869          case wd_command:        /*  7: command or status  */          case wd_command:        /*  7: command or status  */
870                  if (writeflag==MEM_READ) {                  if (writeflag==MEM_READ) {
871                          odata = status_byte(d, cpu);                          odata = status_byte(d, cpu);
 #if 1  
872                          if (!quiet_mode)                          if (!quiet_mode)
873                                  debug("[ wdc: read from STATUS: 0x%02x ]\n",                                  debug("[ wdc: read from STATUS: 0x%02x ]\n",
874                                      odata);                                      (int)odata);
 #endif  
   
875                          cpu_interrupt_ack(cpu, d->irq_nr);                          cpu_interrupt_ack(cpu, d->irq_nr);
876                            d->int_asserted = 0;
877                          d->delayed_interrupt = 0;                          d->delayed_interrupt = 0;
878                  } else {                  } else {
879                          debug("[ wdc: write to COMMAND: 0x%02x ]\n", idata);                          debug("[ wdc: write to COMMAND: 0x%02x ]\n",(int)idata);
880                          d->cur_command = idata;                          wdc_command(cpu, d, idata);
   
                         /*  TODO:  Is this correct behaviour?  */  
                         if (!diskimage_exist(cpu->machine,  
                             d->drive + d->base_drive, DISKIMAGE_IDE)) {  
                                 d->error |= WDCE_ABRT;  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         }  
   
                         /*  Handle the command:  */  
                         switch (d->cur_command) {  
                         case WDCC_READ:  
                                 debug("[ wdc: READ from drive %i, head %i, "  
                                     "cylinder %i, sector %i, nsecs %i ]\n",  
                                     d->drive, d->head, d->cyl_hi*256+d->cyl_lo,  
                                     d->sector, d->seccnt);  
                                 /*  TODO:  HAHA! This should be removed  
                                     quickly  */  
                                 diskimage_getchs(cpu->machine, d->drive +  
                                     d->base_drive, DISKIMAGE_IDE, &cyls,  
                                     &heads, &sectors_per_track);  
   
                                 {  
                                         unsigned char buf[512*256];  
                                         int cyl = d->cyl_hi * 256+ d->cyl_lo;  
                                         int count = d->seccnt? d->seccnt : 256;  
                                         uint64_t offset = 512 * (d->sector - 1  
                                             + d->head * sectors_per_track +  
                                             heads*sectors_per_track*cyl);  
   
 #if 0  
 /*  LBA:  */  
 if (d->lba)  
         offset = 512 * (((d->head & 0xf) << 24) + (cyl << 8) + d->sector);  
 printf("WDC read from offset %lli\n", (long long)offset);  
 #endif  
                                         diskimage_access(cpu->machine,  
                                             d->drive + d->base_drive,  
                                             DISKIMAGE_IDE, 0,  
                                             offset, buf, 512 * count);  
                                         /*  TODO: result code  */  
                                         for (i=0; i<512 * count; i++)  
                                                 wdc_addtoinbuf(d, buf[i]);  
                                 }  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         case WDCC_WRITE:  
                                 debug("[ wdc: WRITE to drive %i, head %i, "  
                                     "cylinder %i, sector %i, nsecs %i ]\n",  
                                     d->drive, d->head, d->cyl_hi*256+d->cyl_lo,  
                                     d->sector, d->seccnt);  
                                 /*  TODO:  HAHA! This should be removed  
                                     quickly  */  
                                 diskimage_getchs(cpu->machine, d->drive +  
                                     d->base_drive, DISKIMAGE_IDE, &cyls,  
                                     &heads, &sectors_per_track);  
                                 {  
                                         int cyl = d->cyl_hi * 256+ d->cyl_lo;  
                                         int count = d->seccnt? d->seccnt : 256;  
                                         uint64_t offset = 512 * (d->sector - 1  
                                             + d->head * sectors_per_track +  
                                             heads*sectors_per_track*cyl);  
   
 #if 0  
 /*  LBA:  */  
 if (d->lba)  
         offset = 512 * (((d->head & 0xf) << 24) + (cyl << 8) + d->sector);  
 printf("WDC write to offset %lli\n", (long long)offset);  
 #endif  
   
                                         d->write_in_progress = 1;  
                                         d->write_count = count;  
                                         d->write_offset = offset;  
   
                                         /*  TODO: result code  */  
                                 }  
 /*  TODO: Really interrupt here?  */  
 #if 0  
                                 d->delayed_interrupt = INT_DELAY;  
 #endif  
                                 break;  
   
                         case WDCC_IDP:  /*  Initialize drive parameters  */  
                                 debug("[ wdc: IDP drive %i (TODO) ]\n",  
                                     d->drive);  
                                 /*  TODO  */  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         case SET_FEATURES:  
                                 fatal("[ wdc: SET_FEATURES drive %i (TODO), "  
                                     "feature 0x%02x ]\n", d->drive, d->precomp);  
                                 /*  TODO  */  
                                 switch (d->precomp) {  
                                 case WDSF_SET_MODE:  
                                         fatal("[ wdc: WDSF_SET_MODE drive %i, "  
                                             "pio/dma flags 0x%02x ]\n",  
                                             d->drive, d->seccnt);  
                                         break;  
                                 default:  
                                         d->error |= WDCE_ABRT;  
                                 }  
                                 /*  TODO: always interrupt?  */  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         case WDCC_RECAL:  
                                 debug("[ wdc: RECAL drive %i ]\n", d->drive);  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         case WDCC_IDENTIFY:  
                                 debug("[ wdc: IDENTIFY drive %i ]\n", d->drive);  
                                 wdc_initialize_identify_struct(cpu, d);  
                                 /*  The IDENTIFY data block is sent out  
                                     in low/high byte order:  */  
                                 for (i=0; i<sizeof(d->identify_struct); i+=2) {  
                                         wdc_addtoinbuf(d, d->identify_struct  
                                             [i+1]);  
                                         wdc_addtoinbuf(d, d->identify_struct  
                                             [i+0]);  
                                 }  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         case WDCC_IDLE_IMMED:  
                                 debug("[ wdc: IDLE_IMMED drive %i ]\n",  
                                     d->drive);  
                                 /*  TODO: interrupt here?  */  
                                 d->delayed_interrupt = INT_DELAY;  
                                 break;  
                         default:  
                                 /*  TODO  */  
                                 d->error |= WDCE_ABRT;  
   
                                 fatal("[ wdc: unknown command 0x%02x ("  
                                     "drive %i, head %i, cylinder %i, sector %i,"  
                                     " nsecs %i) ]\n", d->cur_command, d->drive,  
                                     d->head, d->cyl_hi*256+d->cyl_lo,  
                                     d->sector, d->seccnt);  
                                 exit(1);  
                         }  
881                  }                  }
882                  break;                  break;
883    
# Line 593  printf("WDC write to offset %lli\n", (lo Line 890  printf("WDC write to offset %lli\n", (lo
890                              (int)relative_addr, (int)idata);                              (int)relative_addr, (int)idata);
891          }          }
892    
893          if (writeflag == MEM_READ)          if (cpu->machine->machine_type != MACHINE_HPCMIPS &&
894                  memory_writemax64(cpu, data, len, odata);              cpu->machine->machine_type != MACHINE_EVBMIPS &&
895                cpu->machine->machine_type != MACHINE_BEBOX)
896                    dev_wdc_tick(cpu, extra);
897    
898            if (writeflag == MEM_READ) {
899                    if (relative_addr == wd_data)
900                            memory_writemax64(cpu, data, len, odata);
901                    else
902                            data[0] = odata;
903            }
904    
905          return 1;          return 1;
906  }  }
907    
908    
909  /*  /*
910   *  dev_wdc_init():   *  devinit_wdc():
  *  
  *  base_drive should be 0 for the primary device, and 2 for the secondary.  
911   */   */
912  void dev_wdc_init(struct machine *machine, struct memory *mem,  int devinit_wdc(struct devinit *devinit)
         uint64_t baseaddr, int irq_nr, int base_drive)  
913  {  {
914          struct wdc_data *d;          struct wdc_data *d;
915          uint64_t alt_status_addr;          uint64_t alt_status_addr;
916            int i, tick_shift = WDC_TICK_SHIFT;
917    
918          d = malloc(sizeof(struct wdc_data));          d = malloc(sizeof(struct wdc_data));
919          if (d == NULL) {          if (d == NULL) {
# Line 617  void dev_wdc_init(struct machine *machin Line 921  void dev_wdc_init(struct machine *machin
921                  exit(1);                  exit(1);
922          }          }
923          memset(d, 0, sizeof(struct wdc_data));          memset(d, 0, sizeof(struct wdc_data));
924          d->irq_nr     = irq_nr;          d->irq_nr = devinit->irq_nr;
925          d->base_drive = base_drive;  
926            d->data_debug = 1;
927    
928            d->inbuf = zeroed_alloc(WDC_INBUF_SIZE);
929    
930          alt_status_addr = baseaddr + 0x206;          /*  base_drive = 0 for the primary controller, 2 for the secondary.  */
931            d->base_drive = 0;
932            if ((devinit->addr & 0xfff) == 0x170)
933                    d->base_drive = 2;
934    
935            alt_status_addr = devinit->addr + 0x206;
936    
937          /*  Special hack for pcic/hpcmips:  TODO: Fix  */          /*  Special hack for pcic/hpcmips:  TODO: Fix  */
938          if (baseaddr == 0x14000180)          if (devinit->addr == 0x14000180)
939                  alt_status_addr = 0x14000386;                  alt_status_addr = 0x14000386;
940    
941          memory_device_register(mem, "wdc_altstatus", alt_status_addr, 2,          /*  Get disk geometries:  */
942              dev_wdc_altstatus_access, d, MEM_DEFAULT, NULL);          for (i=0; i<2; i++)
943          memory_device_register(mem, "wdc", baseaddr, DEV_WDC_LENGTH,                  if (diskimage_exist(devinit->machine, d->base_drive +i,
944              dev_wdc_access, d, MEM_DEFAULT, NULL);                      DISKIMAGE_IDE))
945                            diskimage_getchs(devinit->machine, d->base_drive + i,
946                                DISKIMAGE_IDE, &d->cyls[i], &d->heads[i],
947                                &d->sectors_per_track[i]);
948    
949            memory_device_register(devinit->machine->memory, "wdc_altstatus",
950                alt_status_addr, 2, dev_wdc_altstatus_access, d, DM_DEFAULT, NULL);
951            memory_device_register(devinit->machine->memory, devinit->name,
952                devinit->addr, DEV_WDC_LENGTH, dev_wdc_access, d, DM_DEFAULT,
953                NULL);
954    
955            if (devinit->machine->machine_type != MACHINE_HPCMIPS &&
956                devinit->machine->machine_type != MACHINE_EVBMIPS)
957                    tick_shift += 1;
958    
959            machine_add_tickfunction(devinit->machine, dev_wdc_tick,
960                d, tick_shift);
961    
962          machine_add_tickfunction(machine, dev_wdc_tick,          return 1;
             d, WDC_TICK_SHIFT);  
963  }  }
964    

Legend:
Removed from v.6  
changed lines
  Added in v.20

  ViewVC Help
Powered by ViewVC 1.1.26