aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/hw_disk.c
blob: eb4d236e56a966e642d42b814df6e566f8b1d5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*  This file is part of the program psim.
    
    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    
    */


#ifndef _HW_DISK_C_
#define _HW_DISK_C_

#include "device_table.h"

#include "pk.h"

#include <stdio.h>


/* DEVICE
   
   cdrom - readable block device

   disk - readable block device that should be writeable
   
   floppy - readable block device that should be writeable

   DESCRIPTION
   
   Block I/O devices that model the behavour of a fixed or removable
   disk device.

   Creating an instance of this device with no arguments will provide
   access to primative read/write operators.  If arguments are
   specified then the disk-label package is used to perform abstract
   disk I/O. The disk-label package will then use this devices
   primatives.

   For hardware I/O, this device would normally be attached to a
   parent `bus' and that bus would use the I/O methods to read and
   write raw data.  The bus might actually be a SCSI or IDE device.
   It is assumed that the parent bus can take care of DMA.

   PROPERTIES
   
   reg = <address> (required)

   <address> is parent bus dependant.

   device_type = "block"

   name = "disk" | "cdrom" | "fd"

   file = <file-name> (required)

   The name of the file that contains the disk image.

   */

typedef struct _hw_disk_device {
  const char *name;
  int read_only;
  unsigned_word size;
  FILE *image;
} hw_disk_device;

typedef struct _hw_disk_instance {
  long pos;
  hw_disk_device *disk;
} hw_disk_instance;


static void
hw_disk_init_address(device *me)
{
  hw_disk_device *disk = device_data(me);
  generic_device_init_address(me);
  if (disk->image != NULL)
    fclose(disk->image);
  disk->name = device_find_string_property(me, "file");
  if (strcmp(device_name(me), "disk") == 0) {
    disk->read_only = 0;
    disk->image = fopen(disk->name, "r+");
  }
  else {
    disk->read_only = 1;
    disk->image = fopen(disk->name, "r");
  }
  if (disk->image == NULL) {
    perror(device_name(me));
    device_error(me, "open %s failed\n", disk->name);
  }
}

static unsigned
hw_disk_io_read_buffer(device *me,
		       void *dest,
		       int space,
		       unsigned_word addr,
		       unsigned nr_bytes,
		       cpu *processor,
		       unsigned_word cia)
{
  hw_disk_device *disk = device_data(me);
  if (nr_bytes == 0)
    return 0;
  if (addr + nr_bytes > disk->size)
    return 0;
  if (fseek(disk->image, addr, SEEK_SET) < 0)
    return 0;
  if (fread(dest, nr_bytes, 1, disk->image) != 1)
    return 0;
  return nr_bytes;
}


static unsigned
hw_disk_io_write_buffer(device *me,
			const void *source,
			int space,
			unsigned_word addr,
			unsigned nr_bytes,
			cpu *processor,
			unsigned_word cia)
{
  hw_disk_device *disk = device_data(me);
  if (disk->read_only)
    return 0;
  if (nr_bytes == 0)
    return 0;
  if (addr + nr_bytes > disk->size)
    return 0;
  if (fseek(disk->image, addr, SEEK_SET) < 0)
    return 0;
  if (fwrite(source, nr_bytes, 1, disk->image) != 1)
    return 0;
  return nr_bytes;
}


/* instances of the hw_disk device */

static void
hw_disk_instance_delete(device_instance *instance)
{
  hw_disk_instance *data = device_instance_data(instance);
  zfree(data);
}

static int
hw_disk_instance_read(device_instance *instance,
		      void *buf,
		      unsigned_word len)
{
  hw_disk_instance *data = device_instance_data(instance);
  if (fseek(data->disk->image, data->pos, SEEK_SET) < 0)
    return -1;
  if (fread(buf, len, 1, data->disk->image) != 1)
    return -1;
  data->pos = ftell(data->disk->image);
  return len;
}

static int
hw_disk_instance_write(device_instance *instance,
		       const void *buf,
		       unsigned_word len)
{
  hw_disk_instance *data = device_instance_data(instance);
  if (data->disk->read_only)
    return -1;
  if (fseek(data->disk->image, data->pos, SEEK_SET) < 0)
    return -1;
  if (fwrite(buf, len, 1, data->disk->image) != 1)
    return -1;
  data->pos = ftell(data->disk->image);
  return len;
}

static int
hw_disk_instance_seek(device_instance *instance,
		      unsigned_word pos_hi,
		      unsigned_word pos_lo)
{
  hw_disk_instance *data = device_instance_data(instance);
  data->pos = pos_lo;
  return 0;
}

static const device_instance_callbacks hw_disk_instance_callbacks = {
  hw_disk_instance_delete,
  hw_disk_instance_read,
  hw_disk_instance_write,
  hw_disk_instance_seek,
};

static device_instance *
hw_disk_create_instance(device *me,
			const char *path,
			const char *args)
{
  device_instance *disk_instance;
  hw_disk_device *disk = device_data(me);
  hw_disk_instance *data = ZALLOC(hw_disk_instance);
  data->disk = disk;
  data->pos = 0;
  disk_instance = device_create_instance_from(me, NULL,
					      data,
					      path, args,
					      &hw_disk_instance_callbacks);
  return pk_disklabel_create_instance(disk_instance, args);
}

static device_callbacks const hw_disk_callbacks = {
  { hw_disk_init_address, NULL },
  { NULL, }, /* address */
  { hw_disk_io_read_buffer,
      hw_disk_io_write_buffer, },
  { NULL, }, /* DMA */
  { NULL, }, /* interrupt */
  { NULL, }, /* unit */
  hw_disk_create_instance,
};


static void *
hw_disk_create(const char *name,
	       const device_unit *unit_address,
	       const char *args,
	       device *parent)
{
  /* create the descriptor */
  hw_disk_device *hw_disk = ZALLOC(hw_disk_device);
  return hw_disk;
}


const device_descriptor hw_disk_device_descriptor[] = {
  { "disk", hw_disk_create, &hw_disk_callbacks },
  { NULL },
};

#endif /* _HW_DISK_C_ */