Add disk image dumping support
[jpcrr.git] / org / jpc / diskimages / GenericBlockDevice.java
blob99848f2c928029eaf8b4a86594e9d97de86f0c52
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.diskimages;
32 import java.io.*;
33 import org.jpc.emulator.SRLoader;
34 import org.jpc.emulator.SRDumper;
35 import org.jpc.emulator.StatusDumper;
36 import org.jpc.emulator.SRDumpable;
39 public class GenericBlockDevice implements BlockDevice, SRDumpable
41 private DiskImage image;
42 private boolean isLocked;
43 private BlockDevice.Type diskType;
45 public void dumpStatusPartial(StatusDumper output)
47 output.println("\tdiskType " + diskType + " isLocked " + isLocked);
48 output.println("\timage <object #" + output.objectNumber(image) + ">"); if(image != null) image.dumpStatus(output);
51 public void dumpStatus(StatusDumper output)
53 if(output.dumped(this))
54 return;
56 output.println("#" + output.objectNumber(this) + ": GenericBlockDevice:");
57 dumpStatusPartial(output);
58 output.endObject();
61 public void dumpSRPartial(SRDumper output) throws IOException
63 output.dumpObject(image);
64 output.dumpBoolean(isLocked);
65 switch(diskType) {
66 case FLOPPY:
67 output.dumpByte((byte)0);
68 break;
69 case HARDDRIVE:
70 output.dumpByte((byte)1);
71 break;
72 case CDROM:
73 output.dumpByte((byte)2);
74 break;
78 public GenericBlockDevice(SRLoader input) throws IOException
80 input.objectCreated(this);
81 image = (DiskImage)(input.loadObject());
82 isLocked = input.loadBoolean();
83 byte tmpDiskType = input.loadByte();
84 switch(tmpDiskType) {
85 case 0:
86 diskType = BlockDevice.Type.FLOPPY;
87 break;
88 case 1:
89 diskType = BlockDevice.Type.HARDDRIVE;
90 break;
91 case 2:
92 diskType = BlockDevice.Type.CDROM;
93 break;
94 case 3:
95 throw new IOException("Invalid disk type in GenericBlockDevice.");
99 public GenericBlockDevice(BlockDevice.Type driveType)
101 diskType = driveType;
102 isLocked = false;
103 image = null;
106 public GenericBlockDevice(DiskImage _image) throws IOException
108 diskType = _image.getType();
109 isLocked = false;
110 image = _image;
111 image.use();
114 public GenericBlockDevice(DiskImage _image, BlockDevice.Type expectedType) throws IOException
116 if(_image != null && _image.getType() != expectedType)
117 throw new IOException("Disk is of wrong type.");
118 diskType = expectedType;
119 isLocked = false;
120 image = _image;
121 if(image != null)
122 image.use();
125 public byte[] getImageID()
127 if(image != null)
128 return image.getImageID();
129 else
130 return null;
133 public void close()
135 image.unuse();
136 image = null;
139 public int read(long sectorNumber, byte[] buffer, int size)
141 if(image != null)
142 return image.read(sectorNumber, buffer, size);
143 else
144 return -1;
147 public int write(long sectorNumber, byte[] buffer, int size)
149 if(image != null)
150 return image.write(sectorNumber, buffer, size);
151 else
152 return -1;
155 public boolean isInserted()
157 return (image != null);
160 public boolean isLocked()
162 return isLocked;
165 public boolean isReadOnly()
167 if(image != null)
168 return image.isReadOnly();
169 else
170 return false;
173 public void setLock(boolean locked)
175 isLocked = locked;
178 public long getTotalSectors()
180 if(image != null)
181 return image.getTotalSectors();
182 else
183 return 0;
186 public int getCylinders()
188 if(image != null)
189 return image.getCylinders();
190 else
191 return 0;
194 public int getHeads()
196 if(image != null)
197 return image.getHeads();
198 else
199 return 0;
202 public int getSectors()
204 if(image != null)
205 return image.getSectors();
206 else
207 return 0;
210 public BlockDevice.Type getType()
212 return diskType;
215 public String getImageFileName()
217 if(image != null)
218 return image.getImageFileName();
219 else
220 return null;
223 public void configure(String spec) throws IOException
225 //Just implement this, as its required by interface.
226 DiskImage dImage = new DiskImage(spec, false);
227 configure(dImage);
230 public void configure(DiskImage spec) throws IOException
232 if(spec != null && spec.getType() != diskType)
233 throw new IOException("Trying to put disk of wrong type to drive.");
234 if(isLocked)
235 throw new IOException("Can not change disk in locked drive.");
236 if(image != null)
237 image.unuse();
238 image = spec;
239 if(image != null)
240 image.use();
243 public DiskImage getImage()
245 return image;