dev_disk.cc Source File

Back to the index.

dev_disk.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2018 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  * COMMENT: A simple disk controller device, for the test machines
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 "diskimage.h"
41 #include "emul.h"
42 #include "machine.h"
43 #include "memory.h"
44 #include "misc.h"
45 
46 #include "testmachine/dev_disk.h"
47 
48 extern int verbose;
49 
50 #define SECTOR_SIZE 512
51 
52 
53 struct disk_data {
54  uint64_t offset;
55  int disk_id;
56  int command;
57  int status;
58  unsigned char *buf;
59 };
60 
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 
71  return 1;
72 }
73 
74 
76 {
77  struct disk_data *d = (struct disk_data *) extra;
78  uint64_t idata = 0, odata = 0;
79 
80  if (writeflag == MEM_WRITE)
81  idata = memory_readmax64(cpu, data, len);
82 
83  switch (relative_addr) {
84 
85  case DEV_DISK_OFFSET:
86  if (writeflag == MEM_READ) {
87  odata = d->offset;
88  } else {
89  d->offset = idata;
90  }
91 
92  if (d->offset & (SECTOR_SIZE-1))
93  fatal("[ disk: WARNING! offset (%lli) must be %i-byte aligned ]\n",
94  (long long)d->offset, SECTOR_SIZE);
95 
96  break;
97 
99  if (writeflag == MEM_READ) {
100  odata = d->offset >> 32;
101  } else {
102  d->offset = (uint32_t)d->offset | (idata << 32);
103  }
104  break;
105 
106  case DEV_DISK_ID:
107  if (writeflag == MEM_READ) {
108  odata = d->disk_id;
109  } else {
110  d->disk_id = idata;
111  }
112  break;
113 
115  if (writeflag == MEM_READ) {
116  odata = d->command;
117  } else {
118  d->command = idata;
119  switch (d->command) {
120  case 0: d->status = diskimage_access(cpu->machine,
121  d->disk_id, DISKIMAGE_IDE, 0,
122  d->offset, d->buf, SECTOR_SIZE);
123  break;
124  case 1: d->status = diskimage_access(cpu->machine,
125  d->disk_id, DISKIMAGE_IDE, 1,
126  d->offset, d->buf, SECTOR_SIZE);
127  break;
128  }
129 
130  if (verbose >= 2) {
131  debug("[ disk: %s disk %i offset %lli ]\n",
132  d->command ? "WRITE" : "READ",
133  d->disk_id, (long long)d->offset);
134  }
135 
136  d->offset += SECTOR_SIZE;
137  }
138  break;
139 
140  case DEV_DISK_STATUS:
141  if (writeflag == MEM_READ) {
142  odata = d->status;
143  } else {
144  d->status = idata;
145  }
146  break;
147 
148  default:if (writeflag == MEM_WRITE) {
149  fatal("[ disk: unimplemented write to "
150  "offset 0x%x: data=0x%x ]\n", (int)
151  relative_addr, (int)idata);
152  } else {
153  fatal("[ disk: unimplemented read from "
154  "offset 0x%x ]\n", (int)relative_addr);
155  }
156  }
157 
158  if (writeflag == MEM_READ)
159  memory_writemax64(cpu, data, len, odata);
160 
161  return 1;
162 }
163 
164 
165 DEVINIT(disk)
166 {
167  struct disk_data *d;
168  size_t nlen;
169  char *n1, *n2;
170 
171  CHECK_ALLOCATION(d = (struct disk_data *) malloc(sizeof(struct disk_data)));
172  memset(d, 0, sizeof(struct disk_data));
173 
174  nlen = strlen(devinit->name) + 30;
175  CHECK_ALLOCATION(n1 = (char *) malloc(nlen));
176  CHECK_ALLOCATION(n2 = (char *) malloc(nlen));
177 
178  CHECK_ALLOCATION(d->buf = (unsigned char *) malloc(devinit->machine->arch_pagesize));
179  memset(d->buf, 0, devinit->machine->arch_pagesize);
180 
181  snprintf(n1, nlen, "%s [control]", devinit->name);
182  snprintf(n2, nlen, "%s [data buffer]", devinit->name);
183 
185  devinit->addr, DEV_DISK_BUFFER, dev_disk_access, (void *)d,
186  DM_DEFAULT, NULL);
187 
190  devinit->machine->arch_pagesize, dev_disk_buf_access,
191  (void *)d, DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK |
193 
194  return 1;
195 }
196 
data
u_short data
Definition: siireg.h:79
verbose
int verbose
Definition: main.cc:77
machine::arch_pagesize
int arch_pagesize
Definition: machine.h:151
diskimage.h
debug
#define debug
Definition: dev_adb.cc:57
DEV_DISK_OFFSET_HIGH32
#define DEV_DISK_OFFSET_HIGH32
Definition: dev_disk.h:13
devinit::addr
uint64_t addr
Definition: device.h:46
memory_device_register
void memory_device_register(struct memory *mem, const char *, uint64_t baseaddr, uint64_t len, int(*f)(struct cpu *, struct memory *, uint64_t, unsigned char *, size_t, int, void *), void *extra, int flags, unsigned char *dyntrans_data)
Definition: memory.cc:339
MEM_READ
#define MEM_READ
Definition: memory.h:116
disk_data::status
int status
Definition: dev_disk.cc:57
DM_DEFAULT
#define DM_DEFAULT
Definition: memory.h:130
diskimage_access
int diskimage_access(struct machine *machine, int id, int type, int writeflag, off_t offset, unsigned char *buf, size_t len)
Definition: diskimage.cc:617
devinit::machine
struct machine * machine
Definition: device.h:41
device.h
MEM_WRITE
#define MEM_WRITE
Definition: memory.h:117
DEVINIT
DEVINIT(disk)
Definition: dev_disk.cc:165
DM_DYNTRANS_WRITE_OK
#define DM_DYNTRANS_WRITE_OK
Definition: memory.h:132
disk_data::disk_id
int disk_id
Definition: dev_disk.cc:55
dev_disk.h
strlen
void COMBINE() strlen(struct cpu *cpu, struct arm_instr_call *ic, int low_addr)
Definition: cpu_arm_instr.cc:2686
fatal
void fatal(const char *fmt,...)
Definition: main.cc:152
disk_data::command
int command
Definition: dev_disk.cc:56
misc.h
memory_readmax64
uint64_t memory_readmax64(struct cpu *cpu, unsigned char *buf, int len)
Definition: memory.cc:55
machine.h
devinit::name
char * name
Definition: device.h:43
emul.h
disk_data::buf
unsigned char * buf
Definition: dev_disk.cc:58
devinit
Definition: device.h:40
disk_data
Definition: dev_disk.cc:53
cpu.h
DEV_DISK_STATUS
#define DEV_DISK_STATUS
Definition: dev_disk.h:16
DEVICE_ACCESS
DEVICE_ACCESS(disk_buf)
Definition: dev_disk.cc:62
machine::memory
struct memory * memory
Definition: machine.h:126
cpu::machine
struct machine * machine
Definition: cpu.h:328
DM_READS_HAVE_NO_SIDE_EFFECTS
#define DM_READS_HAVE_NO_SIDE_EFFECTS
Definition: memory.h:133
SECTOR_SIZE
#define SECTOR_SIZE
Definition: dev_disk.cc:50
DISKIMAGE_IDE
#define DISKIMAGE_IDE
Definition: diskimage.h:41
DEV_DISK_OFFSET
#define DEV_DISK_OFFSET
Definition: dev_disk.h:12
memory_writemax64
void memory_writemax64(struct cpu *cpu, unsigned char *buf, int len, uint64_t data)
Definition: memory.cc:89
DEV_DISK_START_OPERATION
#define DEV_DISK_START_OPERATION
Definition: dev_disk.h:15
cpu
Definition: cpu.h:326
DEV_DISK_ID
#define DEV_DISK_ID
Definition: dev_disk.h:14
disk_data::offset
uint64_t offset
Definition: dev_disk.cc:54
DM_DYNTRANS_OK
#define DM_DYNTRANS_OK
Definition: memory.h:131
memory.h
DEV_DISK_BUFFER
#define DEV_DISK_BUFFER
Definition: dev_disk.h:17
CHECK_ALLOCATION
#define CHECK_ALLOCATION(ptr)
Definition: misc.h:239

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18