Some coding style fixes
[jpcrr.git] / org / jpc / emulator / memory / codeblock / CodeBlockManager.java
blobc937c5461e5b2c8ac03decbad2272f7e08199d33
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009-2010 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.emulator.memory.codeblock;
32 import java.io.*;
34 import org.jpc.emulator.SRLoader;
35 import org.jpc.emulator.SRDumper;
36 import org.jpc.emulator.SRDumpable;
37 import org.jpc.emulator.memory.Memory;
38 import org.jpc.emulator.memory.codeblock.optimised.*;
40 /**
41 * Provides the outer skin for the codeblock construction system.
42 * <p>
43 * If blocks are not found in memory, then they are requested from this class.
44 * @author Chris Dennis
46 public class CodeBlockManager implements SRDumpable
48 public static volatile int BLOCK_LIMIT = 1000; //minimum of 2 because of STI/CLI
49 private CodeBlockFactory realModeChain, protectedModeChain, virtual8086ModeChain;
50 private ByteSourceWrappedMemory byteSource;
52 /**
53 * Constructs a default manager.
54 * <p>
55 * The default manager creates interpreted mode codeblocks.
57 public CodeBlockManager()
59 byteSource = new ByteSourceWrappedMemory();
61 realModeChain = new DefaultCodeBlockFactory(new RealModeUDecoder(), new OptimisedCompiler(), BLOCK_LIMIT);
62 protectedModeChain = new DefaultCodeBlockFactory(new ProtectedModeUDecoder(), new OptimisedCompiler(), BLOCK_LIMIT);
63 virtual8086ModeChain = new DefaultCodeBlockFactory(new RealModeUDecoder(), new OptimisedCompiler(), BLOCK_LIMIT);
66 public void dumpSRPartial(SRDumper output) throws IOException
70 public CodeBlockManager(SRLoader input)
72 this();
73 input.objectCreated(this);
76 private RealModeCodeBlock tryRealModeFactory(CodeBlockFactory ff, Memory memory, int offset)
78 try {
79 byteSource.set(memory, offset);
80 return ff.getRealModeCodeBlock(byteSource);
81 } catch (ArrayIndexOutOfBoundsException e) {
82 return new SpanningRealModeCodeBlock(new CodeBlockFactory[]{realModeChain});
86 private ProtectedModeCodeBlock tryProtectedModeFactory(CodeBlockFactory ff, Memory memory, int offset, boolean operandSizeFlag)
88 try {
89 byteSource.set(memory, offset);
90 return ff.getProtectedModeCodeBlock(byteSource, operandSizeFlag);
91 } catch (ArrayIndexOutOfBoundsException e) {
92 return new SpanningProtectedModeCodeBlock(new CodeBlockFactory[]{protectedModeChain});
96 private Virtual8086ModeCodeBlock tryVirtual8086ModeFactory(CodeBlockFactory ff, Memory memory, int offset)
98 try {
99 byteSource.set(memory, offset);
100 return ff.getVirtual8086ModeCodeBlock(byteSource);
101 } catch (ArrayIndexOutOfBoundsException e) {
102 return new SpanningVirtual8086ModeCodeBlock(new CodeBlockFactory[]{virtual8086ModeChain});
107 * Get a real mode codeblock instance for the given memory area.
108 * @param memory source for the x86 bytes
109 * @param offset address in the given memory object
110 * @return real mode codeblock instance
112 public RealModeCodeBlock getRealModeCodeBlockAt(Memory memory, int offset)
114 RealModeCodeBlock block;
116 if((block = tryRealModeFactory(realModeChain, memory, offset)) == null) {
117 System.err.println("Critical error: Can't find nor make suitable real mode codeblock.");
118 throw new IllegalStateException("Couldn't find/make suitable realmode block");
120 return block;
125 * Get a protected mode codeblock instance for the given memory area.
126 * @param memory source for the x86 bytes
127 * @param offset address in the given memory object
128 * @param operandSize <code>true</code> for 32-bit, <code>false</code> for 16-bit
129 * @return protected mode codeblock instance
131 public ProtectedModeCodeBlock getProtectedModeCodeBlockAt(Memory memory, int offset, boolean operandSize)
133 ProtectedModeCodeBlock block;
135 if((block = tryProtectedModeFactory(protectedModeChain, memory, offset, operandSize)) == null) {
136 System.err.println("Critical error: Can't find nor make suitable protected mode codeblock.");
137 throw new IllegalStateException("Couldn't find/make suitable pmode block");
139 return block;
143 * Get a Virtual8086 mode codeblock instance for the given memory area.
144 * @param memory source for the x86 bytes
145 * @param offset address in the given memory object
146 * @return Virtual8086 mode codeblock instance
148 public Virtual8086ModeCodeBlock getVirtual8086ModeCodeBlockAt(Memory memory, int offset)
150 Virtual8086ModeCodeBlock block;
152 if((block = tryVirtual8086ModeFactory(virtual8086ModeChain, memory, offset)) == null) {
153 System.err.println("Critical error: Can't find nor make suitable VM8086 mode codeblock.");
154 throw new IllegalStateException("Couldn't find/make suitable VM86 block");
156 return block;