1 |
/* |
2 |
* Copyright (C) 2005-2006 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 |
* $Id: dev_disk.c,v 1.12 2006/05/06 08:42:49 debug Exp $ |
29 |
* |
30 |
* Basic "disk" device. This is a simple test device which can be used to |
31 |
* read and write data from disk devices. |
32 |
*/ |
33 |
|
34 |
#include <stdio.h> |
35 |
#include <stdlib.h> |
36 |
#include <string.h> |
37 |
|
38 |
#include "cpu.h" |
39 |
#include "device.h" |
40 |
#include "devices.h" |
41 |
#include "diskimage.h" |
42 |
#include "emul.h" |
43 |
#include "machine.h" |
44 |
#include "memory.h" |
45 |
#include "misc.h" |
46 |
|
47 |
#include "testmachine/dev_disk.h" |
48 |
|
49 |
|
50 |
struct disk_data { |
51 |
int64_t offset; |
52 |
int disk_id; |
53 |
int command; |
54 |
int status; |
55 |
unsigned char *buf; |
56 |
}; |
57 |
|
58 |
|
59 |
/* |
60 |
* dev_disk_buf_access(): |
61 |
*/ |
62 |
DEVICE_ACCESS(disk_buf) |
63 |
{ |
64 |
struct disk_data *d = (struct disk_data *) extra; |
65 |
|
66 |
if (writeflag == MEM_WRITE) |
67 |
memcpy(d->buf + relative_addr, data, len); |
68 |
else |
69 |
memcpy(data, d->buf + relative_addr, len); |
70 |
return 1; |
71 |
} |
72 |
|
73 |
|
74 |
/* |
75 |
* dev_disk_access(): |
76 |
*/ |
77 |
DEVICE_ACCESS(disk) |
78 |
{ |
79 |
struct disk_data *d = (struct disk_data *) extra; |
80 |
uint64_t idata = 0, odata = 0; |
81 |
|
82 |
if (writeflag == MEM_WRITE) |
83 |
idata = memory_readmax64(cpu, data, len); |
84 |
|
85 |
switch (relative_addr) { |
86 |
|
87 |
case DEV_DISK_OFFSET: |
88 |
if (writeflag == MEM_READ) { |
89 |
odata = d->offset; |
90 |
} else { |
91 |
d->offset = idata; |
92 |
} |
93 |
break; |
94 |
|
95 |
case DEV_DISK_ID: |
96 |
if (writeflag == MEM_READ) { |
97 |
odata = d->disk_id; |
98 |
} else { |
99 |
d->disk_id = idata; |
100 |
} |
101 |
break; |
102 |
|
103 |
case DEV_DISK_START_OPERATION: |
104 |
if (writeflag == MEM_READ) { |
105 |
odata = d->command; |
106 |
} else { |
107 |
d->command = idata; |
108 |
switch (d->command) { |
109 |
case 0: d->status = diskimage_access(cpu->machine, |
110 |
d->disk_id, DISKIMAGE_IDE, 0, |
111 |
d->offset, d->buf, 512); |
112 |
break; |
113 |
case 1: d->status = diskimage_access(cpu->machine, |
114 |
d->disk_id, DISKIMAGE_IDE, 1, |
115 |
d->offset, d->buf, 512); |
116 |
break; |
117 |
} |
118 |
} |
119 |
break; |
120 |
|
121 |
case DEV_DISK_STATUS: |
122 |
if (writeflag == MEM_READ) { |
123 |
odata = d->status; |
124 |
} else { |
125 |
d->status = idata; |
126 |
} |
127 |
break; |
128 |
|
129 |
default:if (writeflag == MEM_WRITE) { |
130 |
fatal("[ disk: unimplemented write to " |
131 |
"offset 0x%x: data=0x%x ]\n", (int) |
132 |
relative_addr, (int)idata); |
133 |
} else { |
134 |
fatal("[ disk: unimplemented read from " |
135 |
"offset 0x%x ]\n", (int)relative_addr); |
136 |
} |
137 |
} |
138 |
|
139 |
if (writeflag == MEM_READ) |
140 |
memory_writemax64(cpu, data, len, odata); |
141 |
|
142 |
return 1; |
143 |
} |
144 |
|
145 |
|
146 |
DEVINIT(disk) |
147 |
{ |
148 |
struct disk_data *d = malloc(sizeof(struct disk_data)); |
149 |
size_t nlen; |
150 |
char *n1, *n2; |
151 |
|
152 |
nlen = strlen(devinit->name) + 30; |
153 |
n1 = malloc(nlen); |
154 |
n2 = malloc(nlen); |
155 |
|
156 |
if (d == NULL || n1 == NULL || n2 == NULL) { |
157 |
fprintf(stderr, "out of memory\n"); |
158 |
exit(1); |
159 |
} |
160 |
memset(d, 0, sizeof(struct disk_data)); |
161 |
|
162 |
d->buf = malloc(devinit->machine->arch_pagesize); |
163 |
if (d->buf == NULL) { |
164 |
fprintf(stderr, "out of memory\n"); |
165 |
exit(1); |
166 |
} |
167 |
memset(d->buf, 0, devinit->machine->arch_pagesize); |
168 |
|
169 |
snprintf(n1, nlen, "%s [control]", devinit->name); |
170 |
snprintf(n2, nlen, "%s [data buffer]", devinit->name); |
171 |
|
172 |
memory_device_register(devinit->machine->memory, n1, |
173 |
devinit->addr, DEV_DISK_BUFFER, dev_disk_access, (void *)d, |
174 |
DM_DEFAULT, NULL); |
175 |
|
176 |
memory_device_register(devinit->machine->memory, n2, |
177 |
devinit->addr + DEV_DISK_BUFFER, |
178 |
devinit->machine->arch_pagesize, dev_disk_buf_access, |
179 |
(void *)d, DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK | |
180 |
DM_READS_HAVE_NO_SIDE_EFFECTS, d->buf); |
181 |
|
182 |
return 1; |
183 |
} |
184 |
|