NHMLFixup v10
[jpcrr.git] / org / jpc / diskimages / DiskImageSet.java
blob28ba15cffad45deb42fa6d4cb8a00529ac7794e9
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;
38 public class DiskImageSet implements SRDumpable
40 private DiskImage[] disks;
41 private int diskCount;
42 private int lowestGap;
44 public DiskImageSet()
46 disks = new DiskImage[5];
47 diskCount = 0;
48 lowestGap = 0;
51 public int addDisk(DiskImage image)
53 if(image == null)
54 return -1;
55 int base = lowestGap;
56 if(diskCount == disks.length) {
57 DiskImage[] newDisks = new DiskImage[2 * disks.length];
58 System.arraycopy(disks, 0, newDisks, 0, disks.length);
59 disks = newDisks;
60 base = diskCount;
62 for(int i = base; i < disks.length; i++) {
63 if(disks[i] == null) {
64 disks[i] = image;
65 diskCount++;
66 lowestGap = i + 1;
67 return i;
68 } else
69 lowestGap = i;
71 return -1; //Can't come here.
74 public void addDisk(int id, DiskImage image)
76 while(id >= disks.length) {
77 DiskImage[] newDisks = new DiskImage[2 * disks.length];
78 System.arraycopy(disks, 0, newDisks, 0, disks.length);
79 disks = newDisks;
81 if(disks[id] == null && image != null)
82 diskCount++;
83 if(lowestGap == id && image != null)
84 lowestGap++;
85 else if(lowestGap > id && image == null)
86 lowestGap = id;
87 disks[id] = image;
91 public DiskImage lookupDisk(int index)
93 if(index == -1)
94 return null;
95 try {
96 return disks[index];
97 } catch(Exception e) {
98 return null;
102 public int[] diskIndicesByType(BlockDevice.Type type)
104 int images = 0, j = 0;
105 for(int i = 0; i < disks.length; i++)
106 if(disks[i] != null && disks[i].getType() == type)
107 images++;
108 int[] diskIDs = new int[images];
109 for(int i = 0; i < disks.length; i++)
110 if(disks[i] != null && disks[i].getType() == type)
111 diskIDs[j++] = i;
112 return diskIDs;
115 public int getDiskCount()
117 return diskCount;
120 public int highestDiskIndex()
122 return disks.length - 1;
125 public void dumpStatusPartial(StatusDumper output)
127 output.println("\tdiskCount " + diskCount + " lowestGap " + lowestGap);
128 for(int i=0; i < disks.length; i++) {
129 output.println("\tdisks[" + i + "] <object #" + output.objectNumber(disks[i]) + ">");
130 if(disks[i] != null) disks[i].dumpStatus(output);
134 public void dumpStatus(StatusDumper output)
136 if(output.dumped(this))
137 return;
139 output.println("#" + output.objectNumber(this) + ": DiskImageSet:");
140 dumpStatusPartial(output);
141 output.endObject();
144 public void dumpSRPartial(SRDumper output) throws IOException
146 output.dumpInt(diskCount);
147 output.dumpInt(lowestGap);
148 output.dumpInt(disks.length);
149 for(int i = 0; i < disks.length; i++)
150 output.dumpObject(disks[i]);
153 public DiskImageSet(SRLoader input) throws IOException
155 input.objectCreated(this);
156 diskCount = input.loadInt();
157 lowestGap = input.loadInt();
158 disks = new DiskImage[input.loadInt()];
159 for(int i = 0; i < disks.length; i++)
160 disks[i] = (DiskImage)(input.loadObject());