/[gxemul]/trunk/src/devices/dev_disk.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_disk.c

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

revision 20 by dpavlin, Mon Oct 8 16:19:23 2007 UTC revision 42 by dpavlin, Mon Oct 8 16:22:32 2007 UTC
# Line 1  Line 1 
1  /*  /*
2   *  Copyright (C) 2005  Anders Gavare.  All rights reserved.   *  Copyright (C) 2005-2007  Anders Gavare.  All rights reserved.
3   *   *
4   *  Redistribution and use in source and binary forms, with or without   *  Redistribution and use in source and binary forms, with or without
5   *  modification, are permitted provided that the following conditions are met:   *  modification, are permitted provided that the following conditions are met:
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *     *  
27   *   *
28   *  $Id: dev_disk.c,v 1.9 2005/11/13 00:14:08 debug Exp $   *  $Id: dev_disk.c,v 1.15 2007/06/15 18:44:19 debug Exp $
29   *   *
30   *  Basic "Disk" device. This is a simple test device which can be used to   *  COMMENT: A simple disk controller device, for the test machines
31     *
32     *  Basic "disk" device. This is a simple test device which can be used to
33   *  read and write data from disk devices.   *  read and write data from disk devices.
34   */   */
35    
# Line 37  Line 39 
39    
40  #include "cpu.h"  #include "cpu.h"
41  #include "device.h"  #include "device.h"
 #include "devices.h"  
42  #include "diskimage.h"  #include "diskimage.h"
43  #include "emul.h"  #include "emul.h"
44  #include "machine.h"  #include "machine.h"
45  #include "memory.h"  #include "memory.h"
46  #include "misc.h"  #include "misc.h"
47    
48    #include "testmachine/dev_disk.h"
49    
 #define BUF_SIZE        8192  
50    
51  struct disk_data {  struct disk_data {
52          int64_t         offset;          int64_t         offset;
# Line 56  struct disk_data { Line 57  struct disk_data {
57  };  };
58    
59    
60  /*  DEVICE_ACCESS(disk_buf)
  *  dev_disk_buf_access():  
  */  
 int dev_disk_buf_access(struct cpu *cpu, struct memory *mem,  
         uint64_t relative_addr, unsigned char *data, size_t len,  
         int writeflag, void *extra)  
61  {  {
62          struct disk_data *d = (struct disk_data *) extra;          struct disk_data *d = extra;
63    
64          if (writeflag == MEM_WRITE)          if (writeflag == MEM_WRITE)
65                  memcpy(d->buf + relative_addr, data, len);                  memcpy(d->buf + relative_addr, data, len);
66          else          else
67                  memcpy(data, d->buf + relative_addr, len);                  memcpy(data, d->buf + relative_addr, len);
68    
69          return 1;          return 1;
70  }  }
71    
72    
73  /*  DEVICE_ACCESS(disk)
  *  dev_disk_access():  
  */  
 int dev_disk_access(struct cpu *cpu, struct memory *mem,  
         uint64_t relative_addr, unsigned char *data, size_t len,  
         int writeflag, void *extra)  
74  {  {
75          struct disk_data *d = (struct disk_data *) extra;          struct disk_data *d = extra;
76          uint64_t idata = 0, odata = 0;          uint64_t idata = 0, odata = 0;
77    
78          if (writeflag == MEM_WRITE)          if (writeflag == MEM_WRITE)
79                  idata = memory_readmax64(cpu, data, len);                  idata = memory_readmax64(cpu, data, len);
80    
81          switch (relative_addr) {          switch (relative_addr) {
82          case 0x00:  
83            case DEV_DISK_OFFSET:
84                  if (writeflag == MEM_READ) {                  if (writeflag == MEM_READ) {
85                          odata = d->offset;                          odata = d->offset;
86                  } else {                  } else {
87                          d->offset = idata;                          d->offset = idata;
88                  }                  }
89                  break;                  break;
90          case 0x10:  
91            case DEV_DISK_ID:
92                  if (writeflag == MEM_READ) {                  if (writeflag == MEM_READ) {
93                          odata = d->disk_id;                          odata = d->disk_id;
94                  } else {                  } else {
95                          d->disk_id = idata;                          d->disk_id = idata;
96                  }                  }
97                  break;                  break;
98          case 0x20:  
99            case DEV_DISK_START_OPERATION:
100                  if (writeflag == MEM_READ) {                  if (writeflag == MEM_READ) {
101                          odata = d->command;                          odata = d->command;
102                  } else {                  } else {
103                          d->command = idata;                          d->command = idata;
104                          switch (d->command) {                          switch (d->command) {
105                          case 0: d->status = diskimage_access(cpu->machine,                          case 0: d->status = diskimage_access(cpu->machine,
106                                       d->disk_id, DISKIMAGE_SCSI, 0,                                       d->disk_id, DISKIMAGE_IDE, 0,
107                                       d->offset, d->buf, 512);                                       d->offset, d->buf, 512);
108                                  break;                                  break;
109                          case 1: d->status = diskimage_access(cpu->machine,                          case 1: d->status = diskimage_access(cpu->machine,
110                                       d->disk_id, DISKIMAGE_SCSI, 1,                                       d->disk_id, DISKIMAGE_IDE, 1,
111                                       d->offset, d->buf, 512);                                       d->offset, d->buf, 512);
112                                  break;                                  break;
113                          }                          }
114                  }                  }
115                  break;                  break;
116          case 0x30:  
117            case DEV_DISK_STATUS:
118                  if (writeflag == MEM_READ) {                  if (writeflag == MEM_READ) {
119                          odata = d->status;                          odata = d->status;
120                  } else {                  } else {
121                          d->status = idata;                          d->status = idata;
122                  }                  }
123                  break;                  break;
124    
125          default:if (writeflag == MEM_WRITE) {          default:if (writeflag == MEM_WRITE) {
126                          fatal("[ disk: unimplemented write to "                          fatal("[ disk: unimplemented write to "
127                              "offset 0x%x: data=0x%x ]\n", (int)                              "offset 0x%x: data=0x%x ]\n", (int)
# Line 142  int dev_disk_access(struct cpu *cpu, str Line 139  int dev_disk_access(struct cpu *cpu, str
139  }  }
140    
141    
142  /*  DEVINIT(disk)
  *  devinit_disk():  
  */  
 int devinit_disk(struct devinit *devinit)  
143  {  {
144          struct disk_data *d = malloc(sizeof(struct disk_data));          struct disk_data *d;
145          size_t nlen;          size_t nlen;
146          char *n1, *n2;          char *n1, *n2;
147                                    
148          nlen = strlen(devinit->name) + 30;          CHECK_ALLOCATION(d = malloc(sizeof(struct disk_data)));
         n1 = malloc(nlen);  
         n2 = malloc(nlen);  
   
         if (d == NULL || n1 == NULL || n2 == NULL) {  
                 fprintf(stderr, "out of memory\n");  
                 exit(1);  
         }  
149          memset(d, 0, sizeof(struct disk_data));          memset(d, 0, sizeof(struct disk_data));
150    
151          d->buf = malloc(devinit->machine->arch_pagesize);          nlen = strlen(devinit->name) + 30;
152          if (d->buf == NULL) {          CHECK_ALLOCATION(n1 = malloc(nlen));
153                  fprintf(stderr, "out of memory\n");          CHECK_ALLOCATION(n2 = malloc(nlen));
154                  exit(1);  
155          }          CHECK_ALLOCATION(d->buf = malloc(devinit->machine->arch_pagesize));
156          memset(d->buf, 0, devinit->machine->arch_pagesize);          memset(d->buf, 0, devinit->machine->arch_pagesize);
157    
158          snprintf(n1, nlen, "%s [control]", devinit->name);          snprintf(n1, nlen, "%s [control]", devinit->name);
159          snprintf(n2, nlen, "%s [data buffer]", devinit->name);          snprintf(n2, nlen, "%s [data buffer]", devinit->name);
160    
161          memory_device_register(devinit->machine->memory, n1,          memory_device_register(devinit->machine->memory, n1,
162              devinit->addr, 0x4000, dev_disk_access, (void *)d,              devinit->addr, DEV_DISK_BUFFER, dev_disk_access, (void *)d,
163              DM_DEFAULT, NULL);              DM_DEFAULT, NULL);
164    
165          memory_device_register(devinit->machine->memory, n2,          memory_device_register(devinit->machine->memory, n2,
166              devinit->addr + 0x4000, devinit->machine->arch_pagesize,              devinit->addr + DEV_DISK_BUFFER,
167              dev_disk_buf_access, (void *)d, DM_DYNTRANS_OK |              devinit->machine->arch_pagesize, dev_disk_buf_access,
168              DM_DYNTRANS_WRITE_OK | DM_READS_HAVE_NO_SIDE_EFFECTS, d->buf);              (void *)d, DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK |
169                DM_READS_HAVE_NO_SIDE_EFFECTS, d->buf);
170    
171          return 1;          return 1;
172  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26