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