From 65f191920cb505d2e0a8ab25f917ec3651149391 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Fri, 4 Jun 2010 11:51:33 +0300 Subject: [PATCH] Some coding style fixes --- org/jpc/ArgProcessor.java | 13 +- org/jpc/diskimages/DiskImageSet.java | 5 +- org/jpc/emulator/Clock.java | 6 +- org/jpc/emulator/DriveSet.java | 5 +- org/jpc/emulator/PC.java | 120 ++++----- org/jpc/emulator/Timer.java | 8 +- org/jpc/emulator/TraceTrap.java | 8 +- org/jpc/emulator/memory/AbstractMemory.java | 18 +- org/jpc/emulator/memory/AddressSpace.java | 11 +- .../memory/AlignmentCheckedAddressSpace.java | 16 +- org/jpc/emulator/memory/LazyCodeBlockMemory.java | 283 ++++++++++----------- org/jpc/emulator/memory/PhysicalAddressSpace.java | 205 ++++++++------- .../codeblock/ArrayBackedInstructionSource.java | 28 +- .../memory/codeblock/ByteSourceWrappedMemory.java | 2 +- .../memory/codeblock/CodeBlockManager.java | 30 ++- .../memory/codeblock/SpanningCodeBlock.java | 2 +- .../codeblock/SpanningProtectedModeCodeBlock.java | 6 +- .../codeblock/SpanningRealModeCodeBlock.java | 3 +- .../SpanningVirtual8086ModeCodeBlock.java | 6 +- .../memory/codeblock/optimised/OpcodeLogger.java | 37 ++- .../codeblock/optimised/OptimisedCompiler.java | 19 +- .../codeblock/optimised/ProtectedModeUBlock.java | 79 +++--- .../codeblock/optimised/ProtectedModeUDecoder.java | 49 ++-- 23 files changed, 458 insertions(+), 501 deletions(-) diff --git a/org/jpc/ArgProcessor.java b/org/jpc/ArgProcessor.java index 1f0e994..d050275 100644 --- a/org/jpc/ArgProcessor.java +++ b/org/jpc/ArgProcessor.java @@ -47,10 +47,10 @@ public class ArgProcessor public static String findVariable(String[] args, String key, String defaultValue) { int keyIndex = findKey(args, key); - if (keyIndex < 0) + if(keyIndex < 0) return defaultValue; - if ((keyIndex + 1) < args.length) + if((keyIndex + 1) < args.length) return args[keyIndex + 1]; else return defaultValue; @@ -70,15 +70,14 @@ public class ArgProcessor private static int findKey(String[] args, String key) { - if (key.startsWith("-")) + if(key.startsWith("-")) key = key.substring(1); - for (int i=0; i"); if(disks[i] != null) disks[i].dumpStatus(output); + for(int i=0; i < disks.length; i++) { + output.println("\tdisks[" + i + "] "); + if(disks[i] != null) disks[i].dumpStatus(output); } } diff --git a/org/jpc/emulator/Clock.java b/org/jpc/emulator/Clock.java index 9dda8de..3869dc2 100644 --- a/org/jpc/emulator/Clock.java +++ b/org/jpc/emulator/Clock.java @@ -93,10 +93,8 @@ public class Clock extends AbstractHardwareComponent public synchronized void update(Timer object) { timers.remove(object); - if (object.enabled()) - { + if(object.enabled()) timers.offer(object); - } } public long getTime() @@ -148,7 +146,7 @@ public class Clock extends AbstractHardwareComponent while(true) { Timer tempTimer; tempTimer = c.timers.peek(); - if ((tempTimer == null) || !tempTimer.check(c.getTime())) { + if((tempTimer == null) || !tempTimer.check(c.getTime())) { if(tempTimer == null || !tempTimer.enabled()) return -1; return tempTimer.getExpiry(); diff --git a/org/jpc/emulator/DriveSet.java b/org/jpc/emulator/DriveSet.java index 16272e9..dd941f7 100644 --- a/org/jpc/emulator/DriveSet.java +++ b/org/jpc/emulator/DriveSet.java @@ -83,8 +83,9 @@ public class DriveSet extends AbstractHardwareComponent super.dumpStatusPartial(output); output.println("\tbootType " + bootType); - for (int i=0; i < ides.length; i++) { - output.println("\tides[" + i + "] "); if(ides[i] != null) ides[i].dumpStatus(output); + for(int i=0; i < ides.length; i++) { + output.println("\tides[" + i + "] "); + if(ides[i] != null) ides[i].dumpStatus(output); } } diff --git a/org/jpc/emulator/PC.java b/org/jpc/emulator/PC.java index 04a1bf6..ac30b6d 100644 --- a/org/jpc/emulator/PC.java +++ b/org/jpc/emulator/PC.java @@ -743,9 +743,8 @@ public class PC implements SRDumpable } System.err.println("Informational: Configuring components..."); - if (!configure()) { + if(!configure()) throw new IllegalStateException("Can't initialize components (cyclic dependency?)"); - } System.err.println("Informational: PC initialization done."); } @@ -784,13 +783,14 @@ public class PC implements SRDumpable output.println("\tpoller "); if(poller != null) poller.dumpStatus(output); int i = 0; - for (HardwareComponent part : parts) { - output.println("\tparts[" + i + "] "); if(part != null) part.dumpStatus(output); - i++; + for(HardwareComponent part : parts) { + output.println("\tparts[" + i++ + "] "); + if(part != null) part.dumpStatus(output); } - for (Map.Entry c : soundOutputs.entrySet()) { - output.println("\tsoundOutputs[" + c.getKey() + "] "); if(c.getValue() != null) c.getValue().dumpStatus(output); + for(Map.Entry c : soundOutputs.entrySet()) { + output.println("\tsoundOutputs[" + c.getKey() + "] "); + if(c.getValue() != null) c.getValue().dumpStatus(output); } } @@ -871,7 +871,7 @@ public class PC implements SRDumpable output.dumpObject(videoOut); output.dumpBoolean(hitTraceTrap); output.dumpBoolean(tripleFaulted); - for (HardwareComponent part : parts) { + for(HardwareComponent part : parts) { output.dumpBoolean(true); output.dumpObject(part); } @@ -880,7 +880,7 @@ public class PC implements SRDumpable output.dumpObject(brb); output.dumpObject(diskChanger); - for (Map.Entry c : soundOutputs.entrySet()) { + for(Map.Entry c : soundOutputs.entrySet()) { output.dumpBoolean(true); output.dumpString(c.getKey()); output.dumpObject(c.getValue()); @@ -1254,35 +1254,29 @@ public class PC implements SRDumpable int count = 0; do { fullyInitialised = true; - for (HardwareComponent outer : parts) { - if (outer.initialised()) { + for(HardwareComponent outer : parts) { + if(outer.initialised()) continue; - } - for (HardwareComponent inner : parts) { + for(HardwareComponent inner : parts) outer.acceptComponent(inner); - } fullyInitialised &= outer.initialised(); } count++; - } while ((fullyInitialised == false) && (count < 100)); + } while((fullyInitialised == false) && (count < 100)); - if (!fullyInitialised) { - for(HardwareComponent hwc : parts) { - if(!hwc.initialised()) { + if(!fullyInitialised) { + for(HardwareComponent hwc : parts) + if(!hwc.initialised()) System.err.println("Error: Component of type " + hwc.getClass() + " failed to initialize."); - } - } System.err.println("Critical error: PC component initialization failed."); return false; } - for (HardwareComponent hwc : parts) { - if (hwc instanceof PCIBus) { - ((PCIBus) hwc).biosInit(); - } - } + for(HardwareComponent hwc : parts) + if(hwc instanceof PCIBus) + ((PCIBus)hwc).biosInit(); return true; } @@ -1304,10 +1298,10 @@ public class PC implements SRDumpable *

* This is roughly equivalent to a hard-reset (power down-up cycle). */ - protected void reset() { - for (HardwareComponent hwc : parts) { + protected void reset() + { + for(HardwareComponent hwc : parts) hwc.reset(); - } configure(); } @@ -1397,16 +1391,15 @@ public class PC implements SRDumpable * @param cls component type required. * @return an instance of class cls, or null on failure */ - public HardwareComponent getComponent(Class cls) { - if (!HardwareComponent.class.isAssignableFrom(cls)) { + public HardwareComponent getComponent(Class cls) + { + if(!HardwareComponent.class.isAssignableFrom(cls)) return null; - } - for (HardwareComponent hwc : parts) { - if (cls.isInstance(hwc)) { + for(HardwareComponent hwc : parts) + if(cls.isInstance(hwc)) return hwc; - } - } + return null; } @@ -1419,7 +1412,8 @@ public class PC implements SRDumpable * Gets the processor instance associated with this PC. * @return associated processor instance. */ - public Processor getProcessor() { + public Processor getProcessor() + { return processor; } @@ -1431,17 +1425,15 @@ public class PC implements SRDumpable * never run indefinitely. * @return total number of x86 instructions executed. */ - public final int execute() { - - if (processor.isProtectedMode()) { - if (processor.isVirtual8086Mode()) { + public final int execute() + { + if(processor.isProtectedMode()) + if(processor.isVirtual8086Mode()) return executeVirtual8086(); - } else { + else return executeProtected(); - } - } else { + else return executeReal(); - } } public final int executeReal() @@ -1453,10 +1445,8 @@ public class PC implements SRDumpable rebootRequest = false; } - try - { - for (int i = 0; i < 100; i++) - { + try { + for(int i = 0; i < 100; i++) { int block; try { block = physicalAddr.executeReal(processor, processor.getInstructionPointer()); @@ -1483,9 +1473,7 @@ public class PC implements SRDumpable } } catch (ProcessorException p) { processor.handleRealModeException(p); - } - catch (ModeSwitchException e) - { + } catch (ModeSwitchException e) { //System.err.println("Informational: CPU switching modes: " + e.toString()); } return x86Count; @@ -1503,7 +1491,8 @@ public class PC implements SRDumpable return tmp; } - public final int executeProtected() { + public final int executeProtected() + { int x86Count = 0; if(rebootRequest) { @@ -1511,10 +1500,8 @@ public class PC implements SRDumpable rebootRequest = false; } - try - { - for (int i = 0; i < 100; i++) - { + try { + for(int i = 0; i < 100; i++) { int block; try { block= linearAddr.executeProtected(processor, processor.getInstructionPointer()); @@ -1541,15 +1528,14 @@ public class PC implements SRDumpable } } catch (ProcessorException p) { processor.handleProtectedModeException(p); - } - catch (ModeSwitchException e) - { + } catch (ModeSwitchException e) { //System.err.println("Informational: CPU switching modes: " + e.toString()); } return x86Count; } - public final int executeVirtual8086() { + public final int executeVirtual8086() + { int x86Count = 0; if(rebootRequest) { @@ -1557,10 +1543,8 @@ public class PC implements SRDumpable rebootRequest = false; } - try - { - for (int i = 0; i < 100; i++) - { + try { + for(int i = 0; i < 100; i++) { int block; try { block = linearAddr.executeVirtual8086(processor, processor.getInstructionPointer()); @@ -1585,13 +1569,9 @@ public class PC implements SRDumpable break; } } - } - catch (ProcessorException p) - { + } catch (ProcessorException p) { processor.handleVirtual8086ModeException(p); - } - catch (ModeSwitchException e) - { + } catch (ModeSwitchException e) { //System.err.println("Informational: CPU switching modes: " + e.toString()); } return x86Count; diff --git a/org/jpc/emulator/Timer.java b/org/jpc/emulator/Timer.java index f0a0ae2..1389ab3 100644 --- a/org/jpc/emulator/Timer.java +++ b/org/jpc/emulator/Timer.java @@ -141,7 +141,7 @@ public class Timer implements Comparable, SRDumpable */ public synchronized boolean check(long time) { - if (this.enabled && (time >= expireTime)) { + if(this.enabled && (time >= expireTime)) { disable(); callback.callback(); return true; @@ -170,9 +170,9 @@ public class Timer implements Comparable, SRDumpable public int compareTo(Timer o) { - if (getExpiry() - o.getExpiry() < 0) + if(getExpiry() - o.getExpiry() < 0) return -1; - else if (getExpiry() == o.getExpiry()) + else if(getExpiry() == o.getExpiry()) return 0; else return 1; @@ -188,7 +188,7 @@ public class Timer implements Comparable, SRDumpable public boolean equals(Object o) { - if (!(o instanceof Timer)) + if(!(o instanceof Timer)) return false; Timer t = (Timer)o; diff --git a/org/jpc/emulator/TraceTrap.java b/org/jpc/emulator/TraceTrap.java index 7448f1d..8ce3156 100644 --- a/org/jpc/emulator/TraceTrap.java +++ b/org/jpc/emulator/TraceTrap.java @@ -115,14 +115,10 @@ public class TraceTrap extends AbstractHardwareComponent implements TimerRespons public void acceptComponent(HardwareComponent component) { - if ((component instanceof Clock) && component.initialised()) - { + if((component instanceof Clock) && component.initialised()) trapTimer = ((Clock)component).newTimer(this); - } - if ((component instanceof Processor) && component.initialised()) - { + if((component instanceof Processor) && component.initialised()) processor = (Processor)component; - } } public void dumpStatusPartial(StatusDumper output) diff --git a/org/jpc/emulator/memory/AbstractMemory.java b/org/jpc/emulator/memory/AbstractMemory.java index c726da6..a8e2aeb 100644 --- a/org/jpc/emulator/memory/AbstractMemory.java +++ b/org/jpc/emulator/memory/AbstractMemory.java @@ -75,27 +75,27 @@ public abstract class AbstractMemory implements Memory public void clear() { - for (int i=0; i getSize()) throw new ArrayIndexOutOfBoundsException("Attempt to clear outside of memory bounds"); - for (int i = start; i < limit; i++) + if(limit > getSize()) throw new ArrayIndexOutOfBoundsException("Attempt to clear outside of memory bounds"); + for(int i = start; i < limit; i++) setByte(i, (byte)0); } public void copyContentsIntoArray(int address, byte[] buffer, int off, int len) { - for (int i=off; i 0); + } while(len > 0); } public void copyContentsIntoArray(int address, byte[] buffer, int off, int len) @@ -263,7 +262,7 @@ public abstract class AddressSpace extends AbstractMemory address += partialLength; off += partialLength; len -= partialLength; - } while (len > 0); + } while(len > 0); } /** diff --git a/org/jpc/emulator/memory/AlignmentCheckedAddressSpace.java b/org/jpc/emulator/memory/AlignmentCheckedAddressSpace.java index 750e2c7..01c9525 100644 --- a/org/jpc/emulator/memory/AlignmentCheckedAddressSpace.java +++ b/org/jpc/emulator/memory/AlignmentCheckedAddressSpace.java @@ -138,7 +138,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public short getWord(int offset) { - if ((offset & 0x1) != 0) + if((offset & 0x1) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; return addressSpace.getWord(offset); @@ -152,7 +152,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public int getDoubleWord(int offset) { - if ((offset & 0x3) != 0) + if((offset & 0x3) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; return addressSpace.getDoubleWord(offset); @@ -166,7 +166,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public long getQuadWord(int offset) { - if ((offset & 0x7) != 0) + if((offset & 0x7) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; return addressSpace.getQuadWord(offset); @@ -180,7 +180,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public long getLowerDoubleQuadWord(int offset) { - if ((offset & 0xF) != 0) + if((offset & 0xF) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; return addressSpace.getLowerDoubleQuadWord(offset); @@ -199,7 +199,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public void setWord(int offset, short data) { - if ((offset & 0x1) != 0) + if((offset & 0x1) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; addressSpace.setWord(offset, data); @@ -213,7 +213,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public void setDoubleWord(int offset, int data) { - if ((offset & 0x3) != 0) + if((offset & 0x3) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; addressSpace.setDoubleWord(offset, data); @@ -227,7 +227,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public void setQuadWord(int offset, long data) { - if ((offset & 0x7) != 0) + if((offset & 0x7) != 0) throw ProcessorException.ALIGNMENT_CHECK_0; addressSpace.setQuadWord(offset, data); @@ -241,7 +241,7 @@ public class AlignmentCheckedAddressSpace extends AddressSpace */ public void setLowerDoubleQuadWord(int offset, long data) { - if ((offset & 0xF) != 0) + if((offset & 0xF) != 0) throw ProcessorException.GENERAL_PROTECTION_0; addressSpace.setLowerDoubleQuadWord(offset, data); diff --git a/org/jpc/emulator/memory/LazyCodeBlockMemory.java b/org/jpc/emulator/memory/LazyCodeBlockMemory.java index 4300b9e..a5ae654 100644 --- a/org/jpc/emulator/memory/LazyCodeBlockMemory.java +++ b/org/jpc/emulator/memory/LazyCodeBlockMemory.java @@ -256,307 +256,283 @@ public class LazyCodeBlockMemory extends AbstractMemory { } } - private void removeVirtual8086CodeBlockAt(int offset) { + private void removeVirtual8086CodeBlockAt(int offset) + { Virtual8086ModeCodeBlock b = virtual8086CodeBuffer[offset]; - if ((b == null) || (b == PLACEHOLDER)) { + if((b == null) || (b == PLACEHOLDER)) return; - } virtual8086CodeBuffer[offset] = null; int len = b.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < virtual8086CodeBuffer.length); i++) { - if (virtual8086CodeBuffer[i] == PLACEHOLDER) { + for(int i = offset + 1; (i < offset + len) && (i < virtual8086CodeBuffer.length); i++) + if(virtual8086CodeBuffer[i] == PLACEHOLDER) virtual8086CodeBuffer[i] = null; - } - } - for (int i = Math.min(offset + len, virtual8086CodeBuffer.length) - 1; i >= 0; i--) { - if (virtual8086CodeBuffer[i] == null) { - if (i < offset) { + for(int i = Math.min(offset + len, virtual8086CodeBuffer.length) - 1; i >= 0; i--) { + if(virtual8086CodeBuffer[i] == null) { + if(i < offset) break; - } else { + else continue; - } } - if (virtual8086CodeBuffer[i] == PLACEHOLDER) { + if(virtual8086CodeBuffer[i] == PLACEHOLDER) continue; - } Virtual8086ModeCodeBlock bb = virtual8086CodeBuffer[i]; len = bb.getX86Length(); - for (int j = i + 1; (j < i + len) && (j < virtual8086CodeBuffer.length); j++) { - if (virtual8086CodeBuffer[j] == null) { + for(int j = i + 1; (j < i + len) && (j < virtual8086CodeBuffer.length); j++) + if(virtual8086CodeBuffer[j] == null) virtual8086CodeBuffer[j] = PLACEHOLDER; - } - } } } - private void removeProtectedCodeBlockAt(int offset) { + private void removeProtectedCodeBlockAt(int offset) + { ProtectedModeCodeBlock b = protectedCodeBuffer[offset]; - if ((b == null) || (b == PLACEHOLDER)) { + if((b == null) || (b == PLACEHOLDER)) return; - } protectedCodeBuffer[offset] = null; int len = b.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < protectedCodeBuffer.length); i++) { - if (protectedCodeBuffer[i] == PLACEHOLDER) { + for(int i = offset + 1; (i < offset + len) && (i < protectedCodeBuffer.length); i++) + if(protectedCodeBuffer[i] == PLACEHOLDER) protectedCodeBuffer[i] = null; - } - } - for (int i = Math.min(offset + len, protectedCodeBuffer.length) - 1; i >= 0; i--) { - if (protectedCodeBuffer[i] == null) { - if (i < offset) { + for(int i = Math.min(offset + len, protectedCodeBuffer.length) - 1; i >= 0; i--) { + if(protectedCodeBuffer[i] == null) { + if(i < offset) break; - } else { + else continue; - } } - if (protectedCodeBuffer[i] == PLACEHOLDER) { + if(protectedCodeBuffer[i] == PLACEHOLDER) continue; - } ProtectedModeCodeBlock bb = protectedCodeBuffer[i]; len = bb.getX86Length(); - for (int j = i + 1; (j < i + len) && (j < protectedCodeBuffer.length); j++) { - if (protectedCodeBuffer[j] == null) { + for(int j = i + 1; (j < i + len) && (j < protectedCodeBuffer.length); j++) + if(protectedCodeBuffer[j] == null) protectedCodeBuffer[j] = PLACEHOLDER; - } - } } } - private void removeRealCodeBlockAt(int offset) { + private void removeRealCodeBlockAt(int offset) + { RealModeCodeBlock b = realCodeBuffer[offset]; - if ((b == null) || (b == PLACEHOLDER)) { + if((b == null) || (b == PLACEHOLDER)) return; - } + realCodeBuffer[offset] = null; int len = b.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < realCodeBuffer.length); i++) { - if (realCodeBuffer[i] == PLACEHOLDER) { + for(int i = offset + 1; (i < offset + len) && (i < realCodeBuffer.length); i++) + if(realCodeBuffer[i] == PLACEHOLDER) realCodeBuffer[i] = null; - } - } - for (int i = Math.min(offset + len, realCodeBuffer.length) - 1; i >= 0; i--) { - if (realCodeBuffer[i] == null) { - if (i < offset) { + for(int i = Math.min(offset + len, realCodeBuffer.length) - 1; i >= 0; i--) { + if(realCodeBuffer[i] == null) { + if(i < offset) break; - } else { + else continue; - } } - if (realCodeBuffer[i] == PLACEHOLDER) { + if(realCodeBuffer[i] == PLACEHOLDER) continue; - } RealModeCodeBlock bb = realCodeBuffer[i]; len = bb.getX86Length(); - for (int j = i + 1; (j < i + len) && (j < realCodeBuffer.length); j++) { - if (realCodeBuffer[j] == null) { + for(int j = i + 1; (j < i + len) && (j < realCodeBuffer.length); j++) + if(realCodeBuffer[j] == null) realCodeBuffer[j] = PLACEHOLDER; - } - } } } - private void setVirtual8086CodeBlockAt(int offset, Virtual8086ModeCodeBlock block) { + private void setVirtual8086CodeBlockAt(int offset, Virtual8086ModeCodeBlock block) + { removeVirtual8086CodeBlockAt(offset); - if (block == null) { + if(block == null) return; - } virtual8086CodeBuffer[offset] = block; int len = block.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < virtual8086CodeBuffer.length); i++) { - if (virtual8086CodeBuffer[i] == null) { + for(int i = offset + 1; (i < offset + len) && (i < virtual8086CodeBuffer.length); i++) + if(virtual8086CodeBuffer[i] == null) virtual8086CodeBuffer[i] = PLACEHOLDER; - } - } } - private void setProtectedCodeBlockAt(int offset, ProtectedModeCodeBlock block) { + private void setProtectedCodeBlockAt(int offset, ProtectedModeCodeBlock block) + { removeProtectedCodeBlockAt(offset); - if (block == null) { + if(block == null) return; - } protectedCodeBuffer[offset] = block; int len = block.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < protectedCodeBuffer.length); i++) { - if (protectedCodeBuffer[i] == null) { + for(int i = offset + 1; (i < offset + len) && (i < protectedCodeBuffer.length); i++) + if(protectedCodeBuffer[i] == null) protectedCodeBuffer[i] = PLACEHOLDER; - } - } } - private void setRealCodeBlockAt(int offset, RealModeCodeBlock block) { + private void setRealCodeBlockAt(int offset, RealModeCodeBlock block) + { removeRealCodeBlockAt(offset); - if (block == null) { + if(block == null) return; - } realCodeBuffer[offset] = block; int len = block.getX86Length(); - for (int i = offset + 1; (i < offset + len) && (i < realCodeBuffer.length); i++) { - if (realCodeBuffer[i] == null) { + for(int i = offset + 1; (i < offset + len) && (i < realCodeBuffer.length); i++) + if(realCodeBuffer[i] == null) realCodeBuffer[i] = PLACEHOLDER; - } - } } private void regionAltered(int start, int end) { - if (realCodeBuffer != null) { - for (int i = end; i >= 0; i--) { + if(realCodeBuffer != null) { + for(int i = end; i >= 0; i--) { RealModeCodeBlock b = realCodeBuffer[i]; - if (b == null) { - if (i < start) { + if(b == null) { + if(i < start) break; - } else { + else continue; - } } - if (b == PLACEHOLDER) { + if(b == PLACEHOLDER) continue; - } - if (!b.handleMemoryRegionChange(start, end)) { + if(!b.handleMemoryRegionChange(start, end)) removeRealCodeBlockAt(i); - } } } - if (protectedCodeBuffer != null) { - for (int i = end; i >= 0; i--) { + if(protectedCodeBuffer != null) { + for(int i = end; i >= 0; i--) { ProtectedModeCodeBlock b = protectedCodeBuffer[i]; - if (b == null) { - if (i < start) { + if(b == null) { + if(i < start) break; - } else { + else continue; - } } - if (b == PLACEHOLDER) { + if(b == PLACEHOLDER) continue; - } - if (!b.handleMemoryRegionChange(start, end)) { + if(!b.handleMemoryRegionChange(start, end)) removeProtectedCodeBlockAt(i); - } } } - if (virtual8086CodeBuffer != null) { - for (int i = end; i >= 0; i--) { + if(virtual8086CodeBuffer != null) { + for(int i = end; i >= 0; i--) { Virtual8086ModeCodeBlock b = virtual8086CodeBuffer[i]; - if (b == null) { - if (i < start) { + if(b == null) { + if(i < start) break; - } else { + else continue; - } } - if (b == PLACEHOLDER) { + if(b == PLACEHOLDER) continue; - } - if (!b.handleMemoryRegionChange(start, end)) { + if(!b.handleMemoryRegionChange(start, end)) removeVirtual8086CodeBlockAt(i); - } } } } - public void clear() { + public void clear() + { realCodeBuffer = null; protectedCodeBuffer = null; virtual8086CodeBuffer = null; buffer = null; } - public String toString() { + public String toString() + { return "LazyCodeBlockMemory[" + getSize() + "]"; } //This class does not need to be dumpable because codeblocks can't be saved. - private static class BlankCodeBlock implements RealModeCodeBlock, ProtectedModeCodeBlock, Virtual8086ModeCodeBlock { - + private static class BlankCodeBlock implements RealModeCodeBlock, ProtectedModeCodeBlock, Virtual8086ModeCodeBlock + { private static final RuntimeException executeException = new NullPointerException(); - public int getX86Length() { + public int getX86Length() + { return 0; } - public int getX86Count() { + public int getX86Count() + { return 0; } - public int execute(Processor cpu) { + public int execute(Processor cpu) + { throw executeException; } - public boolean handleMemoryRegionChange(int startAddress, int endAddress) { + public boolean handleMemoryRegionChange(int startAddress, int endAddress) + { return false; } - public String getDisplayString() { + public String getDisplayString() + { return "\n\n<>\n\n"; } - public String toString() { + public String toString() + { return " -- Blank --\n"; } } - public ProtectedModeCodeBlock getProtectedBlock(int offset, boolean size) { - if (protectedCodeBuffer == null) { + public ProtectedModeCodeBlock getProtectedBlock(int offset, boolean size) + { + if(protectedCodeBuffer == null) { allocateBuffer(); protectedCodeBuffer = new ProtectedModeCodeBlock[(int) getSize()]; } ProtectedModeCodeBlock block = protectedCodeBuffer[offset]; - if ((block != null) && (block != PLACEHOLDER)) { + if((block != null) && (block != PLACEHOLDER)) return block; - } block = codeBlockManager.getProtectedModeCodeBlockAt(this, offset, size); setProtectedCodeBlockAt(offset, block); return block; } - public Virtual8086ModeCodeBlock getVirtual8086Block(int offset) { - if (virtual8086CodeBuffer == null) { + public Virtual8086ModeCodeBlock getVirtual8086Block(int offset) + { + if(virtual8086CodeBuffer == null) { allocateBuffer(); virtual8086CodeBuffer = new Virtual8086ModeCodeBlock[(int) getSize()]; } Virtual8086ModeCodeBlock block = virtual8086CodeBuffer[offset]; - if ((block != null) && (block != PLACEHOLDER)) { + if((block != null) && (block != PLACEHOLDER)) return block; - } block = codeBlockManager.getVirtual8086ModeCodeBlockAt(this, offset); setVirtual8086CodeBlockAt(offset, block); return block; } - public RealModeCodeBlock getRealBlock(int offset) { - if (realCodeBuffer == null) { + public RealModeCodeBlock getRealBlock(int offset) + { + if(realCodeBuffer == null) { allocateBuffer(); realCodeBuffer = new RealModeCodeBlock[(int) getSize()]; } RealModeCodeBlock block = realCodeBuffer[offset]; - if ((block != null) && (block != PLACEHOLDER)) { + if((block != null) && (block != PLACEHOLDER)) return block; - } block = codeBlockManager.getRealModeCodeBlockAt(this, offset); setRealCodeBlockAt(offset, block); @@ -564,26 +540,27 @@ public class LazyCodeBlockMemory extends AbstractMemory { } //begin lazy memory methods - private final void allocateBuffer() { - if (buffer == null) { + private final void allocateBuffer() + { + if(buffer == null) buffer = new byte[size]; - } } - public void copyContentsIntoArray(int address, byte[] buf, int off, int len) { + public void copyContentsIntoArray(int address, byte[] buf, int off, int len) + { try { System.arraycopy(buffer, address, buf, off, len); } catch (NullPointerException e) { - if (++nullReadCount == ALLOCATION_THRESHOLD) { + if(++nullReadCount == ALLOCATION_THRESHOLD) { allocateBuffer(); System.arraycopy(buffer, address, buf, off, len); - } else { + } else Arrays.fill(buf, off, off + len, (byte) 0); - } } } - public void loadInitialContents(int address, byte[] buf, int off, int len) { + public void loadInitialContents(int address, byte[] buf, int off, int len) + { try { System.arraycopy(buf, off, buffer, address, len); } catch (NullPointerException e) { @@ -592,7 +569,8 @@ public class LazyCodeBlockMemory extends AbstractMemory { } } - public void copyArrayIntoContents(int address, byte[] buf, int off, int len) { + public void copyArrayIntoContents(int address, byte[] buf, int off, int len) + { try { System.arraycopy(buf, off, buffer, address, len); } catch (NullPointerException e) { @@ -602,33 +580,34 @@ public class LazyCodeBlockMemory extends AbstractMemory { regionAltered(address, address + len - 1); } - public long getSize() { + public long getSize() + { return size; } - public boolean isAllocated() { + public boolean isAllocated() + { return (buffer != null); } - public byte getByte(int offset) { + public byte getByte(int offset) + { try { if(fpuHackFlag && offset == 0x410) return (byte)(buffer[offset] & 0xFD); return buffer[offset]; } catch (NullPointerException e) { - if (++nullReadCount == ALLOCATION_THRESHOLD) { + if(++nullReadCount == ALLOCATION_THRESHOLD) { allocateBuffer(); return buffer[offset]; - } else { + } else return 0; - } } } public void setByte(int offset, byte data) { - if (getByte(offset) == data) { + if(getByte(offset) == data) return; - } try { buffer[offset] = data; } catch (NullPointerException e) { @@ -649,15 +628,14 @@ public class LazyCodeBlockMemory extends AbstractMemory { return (short) result; } catch (NullPointerException e) { - if (++nullReadCount == ALLOCATION_THRESHOLD) { + if(++nullReadCount == ALLOCATION_THRESHOLD) { allocateBuffer(); int result = 0xFF & buffer[offset]; offset++; result |= buffer[offset] << 8; return (short) result; - } else { + } else return 0; - } } } @@ -678,7 +656,7 @@ public class LazyCodeBlockMemory extends AbstractMemory { return result; } catch (NullPointerException e) { - if (++nullReadCount == ALLOCATION_THRESHOLD) { + if(++nullReadCount == ALLOCATION_THRESHOLD) { allocateBuffer(); int result = 0xFF & buffer[offset]; offset++; @@ -688,16 +666,14 @@ public class LazyCodeBlockMemory extends AbstractMemory { offset++; result |= (buffer[offset]) << 24; return result; - } else { + } else return 0; - } } } public void setWord(int offset, short data) { - if (getWord(offset) == data) { + if(getWord(offset) == data) return; - } try { buffer[offset] = (byte) data; offset++; @@ -712,9 +688,8 @@ public class LazyCodeBlockMemory extends AbstractMemory { } public void setDoubleWord(int offset, int data) { - if (getDoubleWord(offset) == data) { + if(getDoubleWord(offset) == data) return; - } try { buffer[offset] = (byte) data; offset++; diff --git a/org/jpc/emulator/memory/PhysicalAddressSpace.java b/org/jpc/emulator/memory/PhysicalAddressSpace.java index cb6b53e..bab5852 100644 --- a/org/jpc/emulator/memory/PhysicalAddressSpace.java +++ b/org/jpc/emulator/memory/PhysicalAddressSpace.java @@ -231,7 +231,7 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware quickNonA20MaskedIndex = loadMemoryTableSR(input); nonA20MaskedIndex = loadMemoryDTableSR(input); reconstructA20MaskedTables(); - if (gateA20MaskState) { + if(gateA20MaskState) { quickIndex = quickNonA20MaskedIndex; index = nonA20MaskedIndex; } else { @@ -295,13 +295,10 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware private void initialiseMemory() { - for (int i = 0; i < sysRAMSize; i += AddressSpace.BLOCK_SIZE) { + for(int i = 0; i < sysRAMSize; i += AddressSpace.BLOCK_SIZE) mapMemory(i, new LazyCodeBlockMemory(AddressSpace.BLOCK_SIZE, manager)); - } - for (int i = 0; i < 32; i++) - { + for(int i = 0; i < 32; i++) mapMemory(0xd0000 + i * AddressSpace.BLOCK_SIZE, new PhysicalAddressSpace.UnconnectedMemoryBlock()); - } } public CodeBlockManager getCodeBlockManager() @@ -320,7 +317,7 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware */ public void setGateA20State(boolean value) { gateA20MaskState = value; - if (value) { + if(value) { quickIndex = quickNonA20MaskedIndex; index = nonA20MaskedIndex; } else { @@ -328,9 +325,8 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware index = a20MaskedIndex; } - if ((linearAddr != null) && linearAddr.isPagingEnabled()) { + if((linearAddr != null) && linearAddr.isPagingEnabled()) linearAddr.flush(); - } } /** @@ -341,68 +337,64 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware * to emulate an 8086 memory system. * @return status of the A20 line. */ - public boolean getGateA20State() { + public boolean getGateA20State() + { return gateA20MaskState; } - protected Memory getReadMemoryBlockAt(int offset) { + protected Memory getReadMemoryBlockAt(int offset) + { return getMemoryBlockAt(offset); } - protected Memory getWriteMemoryBlockAt(int offset) { + protected Memory getWriteMemoryBlockAt(int offset) + { return getMemoryBlockAt(offset); } - public int executeReal(Processor cpu, int offset) { + public int executeReal(Processor cpu, int offset) + { return getReadMemoryBlockAt(offset).executeReal(cpu, offset & AddressSpace.BLOCK_MASK); } - public int executeProtected(Processor cpu, int offset) { + public int executeProtected(Processor cpu, int offset) + { System.err.println("Critical error: Trying to run Protected mode block in physical memory."); throw new IllegalStateException("Cannot execute protected mode block in physical memory"); } - public int executeVirtual8086(Processor cpu, int offset) { + public int executeVirtual8086(Processor cpu, int offset) + { System.err.println("Critical error: Trying to run Protected mode block in physical memory."); throw new IllegalStateException("Cannot execute protected mode block in physical memory"); } protected void replaceBlocks(Memory oldBlock, Memory newBlock) { - for (int i = 0; i < quickA20MaskedIndex.length; i++) { - if (quickA20MaskedIndex[i] == oldBlock) { + for(int i = 0; i < quickA20MaskedIndex.length; i++) + if(quickA20MaskedIndex[i] == oldBlock) quickA20MaskedIndex[i] = newBlock; - } - } - for (int i = 0; i < quickNonA20MaskedIndex.length; i++) { - if (quickNonA20MaskedIndex[i] == oldBlock) { + for(int i = 0; i < quickNonA20MaskedIndex.length; i++) + if(quickNonA20MaskedIndex[i] == oldBlock) quickNonA20MaskedIndex[i] = newBlock; - } - } - for (Memory[] subArray : a20MaskedIndex) { - if (subArray == null) { + for(Memory[] subArray : a20MaskedIndex) { + if(subArray == null) continue; - } - for (int j = 0; j < subArray.length; j++) { - if (subArray[j] == oldBlock) { + for(int j = 0; j < subArray.length; j++) + if(subArray[j] == oldBlock) subArray[j] = newBlock; - } - } } - for (Memory[] subArray : nonA20MaskedIndex) { - if (subArray == null) { + for(Memory[] subArray : nonA20MaskedIndex) { + if(subArray == null) continue; - } - for (int j = 0; j < subArray.length; j++) { - if (subArray[j] == oldBlock) { + for(int j = 0; j < subArray.length; j++) + if(subArray[j] == oldBlock) subArray[j] = newBlock; - } - } } } - public static class MapWrapper implements Memory { - + public static class MapWrapper implements Memory + { private Memory memory; private int baseAddress; @@ -436,88 +428,104 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware baseAddress = input.loadInt(); } - MapWrapper(Memory mem, int base) { + MapWrapper(Memory mem, int base) + { baseAddress = base; memory = mem; } - public long getSize() { + public long getSize() + { return BLOCK_SIZE; } - public boolean isAllocated() { + public boolean isAllocated() + { return memory.isAllocated(); } - public void clear() { + public void clear() + { memory.clear(baseAddress, (int) getSize()); } - public void clear(int start, int length) { - if (start + length > getSize()) { + public void clear(int start, int length) + { + if(start + length > getSize()) throw new ArrayIndexOutOfBoundsException("Attempt to clear outside of memory bounds"); - } start = baseAddress | start; memory.clear(start, length); } - public void copyContentsIntoArray(int offset, byte[] buffer, int off, int len) { + public void copyContentsIntoArray(int offset, byte[] buffer, int off, int len) + { offset = baseAddress | offset; memory.copyContentsIntoArray(offset, buffer, off, len); } - public void copyArrayIntoContents(int offset, byte[] buffer, int off, int len) { + public void copyArrayIntoContents(int offset, byte[] buffer, int off, int len) + { offset = baseAddress | offset; memory.copyArrayIntoContents(offset, buffer, off, len); } - public byte getByte(int offset) { + public byte getByte(int offset) + { offset = baseAddress | offset; return memory.getByte(offset); } - public short getWord(int offset) { + public short getWord(int offset) + { offset = baseAddress | offset; return memory.getWord(offset); } - public int getDoubleWord(int offset) { + public int getDoubleWord(int offset) + { offset = baseAddress | offset; return memory.getDoubleWord(offset); } - public long getQuadWord(int offset) { + public long getQuadWord(int offset) + { offset = baseAddress | offset; return memory.getQuadWord(offset); } - public long getLowerDoubleQuadWord(int offset) { + public long getLowerDoubleQuadWord(int offset) + { offset = baseAddress | offset; return memory.getQuadWord(offset); } - public long getUpperDoubleQuadWord(int offset) { + public long getUpperDoubleQuadWord(int offset) + { offset += 8; offset = baseAddress | offset; return memory.getQuadWord(offset); } - public void setByte(int offset, byte data) { + public void setByte(int offset, byte data) + { offset = baseAddress | offset; memory.setByte(offset, data); } - public void setWord(int offset, short data) { + public void setWord(int offset, short data) + { offset = baseAddress | offset; memory.setWord(offset, data); } - public void setDoubleWord(int offset, int data) { + public void setDoubleWord(int offset, int data) + { offset = baseAddress | offset; memory.setDoubleWord(offset, data); } - public void setQuadWord(int offset, long data) { + public void setQuadWord(int offset, long data) + { offset = baseAddress | offset; memory.setQuadWord(offset, data); } @@ -552,25 +560,24 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware return "Mapped Memory"; } - public void loadInitialContents(int address, byte[] buf, int off, int len) { + public void loadInitialContents(int address, byte[] buf, int off, int len) + { throw new UnsupportedOperationException("Not supported yet."); } } - public void clear() { - for (Memory block : quickNonA20MaskedIndex) { + public void clear() + { + for(Memory block : quickNonA20MaskedIndex) block.clear(); - } - for (Memory[] subArray : nonA20MaskedIndex) { - if (subArray == null) { + for(Memory[] subArray : nonA20MaskedIndex) { + if(subArray == null) continue; - } - for (Memory block : subArray) { + for(Memory block : subArray) try { block.clear(); } catch (NullPointerException e) { } - } } } @@ -584,22 +591,22 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware * @param length number of addresses to clear * @throws java.lang.IllegalStateException if range is not of BLOCK_SIZE granularity */ - public void unmap(int start, int length) { - if ((start % BLOCK_SIZE) != 0) { + public void unmap(int start, int length) + { + if((start % BLOCK_SIZE) != 0) { System.err.println("Critical error: Illegal unmap request: start=" + Integer.toHexString(start) + ", length=" + Integer.toHexString(length) + "."); throw new IllegalStateException("Cannot deallocate memory starting at " + Integer.toHexString(start) + "; this is not block aligned at " + BLOCK_SIZE + " boundaries"); } - if ((length % BLOCK_SIZE) != 0) { + if((length % BLOCK_SIZE) != 0) { System.err.println("Critical error: Illegal unmap request: start=" + Integer.toHexString(start) + ", length=" + Integer.toHexString(length) + "."); throw new IllegalStateException("Cannot deallocate memory in partial blocks. " + length + " is not a multiple of " + BLOCK_SIZE); } - for (int i = start; i < start + length; i += BLOCK_SIZE) { + for(int i = start; i < start + length; i += BLOCK_SIZE) setMemoryBlockAt(i, UNCONNECTED); - } } /** @@ -613,18 +620,19 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware * @param length size of mapped region. * @throws java.lang.IllegalStateException if there is an error in the mapping. */ - public void mapMemoryRegion(Memory underlying, int start, int length) { - if (underlying.getSize() < length) { + public void mapMemoryRegion(Memory underlying, int start, int length) + { + if(underlying.getSize() < length) { System.err.println("Critical error: Map request size exceeds mapped memory size."); throw new IllegalStateException("Underlying memory (length=" + underlying.getSize() + ") is too short for mapping into region " + length + " bytes long"); } - if ((start % BLOCK_SIZE) != 0) { + if((start % BLOCK_SIZE) != 0) { System.err.println("Critical error: Illegal map request: start=" + Integer.toHexString(start) + ", length=" + Integer.toHexString(length) + "."); throw new IllegalStateException("Cannot map memory starting at " + Integer.toHexString(start) + "; this is not aligned to " + BLOCK_SIZE + " blocks"); } - if ((length % BLOCK_SIZE) != 0) { + if((length % BLOCK_SIZE) != 0) { System.err.println("Critical error: Illegal map request: start=" + Integer.toHexString(start) + ", length=" + Integer.toHexString(length) + "."); throw new IllegalStateException("Cannot map memory in partial blocks: " + length + @@ -633,7 +641,7 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware unmap(start, length); long s = 0xFFFFFFFFl & start; - for (long i = s; i < s + length; i += BLOCK_SIZE) { + for(long i = s; i < s + length; i += BLOCK_SIZE) { Memory w = new MapWrapper(underlying, (int) (i - s)); setMemoryBlockAt((int) i, w); } @@ -650,11 +658,11 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware * @throws java.lang.IllegalStateException if there is an error in the mapping. */ public void mapMemory(int start, Memory block) { - if ((start % BLOCK_SIZE) != 0) { + if((start % BLOCK_SIZE) != 0) { System.err.println("Critical error: Illegal map request: start=" + Integer.toHexString(start) + "."); throw new IllegalStateException("Cannot allocate memory starting at " + Integer.toHexString(start) + "; this is not aligned to " + BLOCK_SIZE + " bytes"); } - if (block.getSize() != BLOCK_SIZE) { + if(block.getSize() != BLOCK_SIZE) { System.err.println("Critical error: Illegal map request: Impossible underlying memory size."); throw new IllegalStateException("Can only allocate memory in blocks of " + BLOCK_SIZE); } @@ -758,50 +766,59 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware public void setUpperDoubleQuadWord(int offset, long data) { } - public int executeReal(Processor cpu, int offset) { + public int executeReal(Processor cpu, int offset) + { System.err.println("Critical error: Can not execute in unconnected memory."); throw new IllegalStateException("Trying to execute in Unconnected Block @ 0x" + Integer.toHexString(offset)); } - public int executeProtected(Processor cpu, int offset) { + public int executeProtected(Processor cpu, int offset) + { System.err.println("Critical error: Can not execute in unconnected memory."); throw new IllegalStateException("Trying to execute in Unconnected Block @ 0x" + Integer.toHexString(offset)); } - public int executeVirtual8086(Processor cpu, int offset) { + public int executeVirtual8086(Processor cpu, int offset) + { System.err.println("Critical error: Can not execute in unconnected memory."); throw new IllegalStateException("Trying to execute in Unconnected Block @ 0x" + Integer.toHexString(offset)); } - public String toString() { + public String toString() + { return "Unconnected Memory"; } - public void loadInitialContents(int address, byte[] buf, int off, int len) { + public void loadInitialContents(int address, byte[] buf, int off, int len) + { } } - public void reset() { + public void reset() + { clear(); setGateA20State(false); linearAddr = null; } - public boolean initialised() { + public boolean initialised() + { return (linearAddr != null); } - public void acceptComponent(HardwareComponent component) { - if (component instanceof LinearAddressSpace) { - linearAddr = (LinearAddressSpace) component; - } + public void acceptComponent(HardwareComponent component) + { + if(component instanceof LinearAddressSpace) + linearAddr = (LinearAddressSpace)component; } - public String toString() { + public String toString() + { return "Physical Address Bus"; } - private Memory getMemoryBlockAt(int i) { + private Memory getMemoryBlockAt(int i) + { try { return quickIndex[i >>> INDEX_SHIFT]; } catch (ArrayIndexOutOfBoundsException e) { @@ -817,7 +834,7 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware try { int idx = i >>> INDEX_SHIFT; quickNonA20MaskedIndex[idx] = b; - if ((idx & (GATEA20_MASK >>> INDEX_SHIFT)) == idx) { + if((idx & (GATEA20_MASK >>> INDEX_SHIFT)) == idx) { quickA20MaskedIndex[idx] = b; quickA20MaskedIndex[idx | ((~GATEA20_MASK) >>> INDEX_SHIFT)] = b; } @@ -829,7 +846,7 @@ public final class PhysicalAddressSpace extends AddressSpace implements Hardware nonA20MaskedIndex[i >>> TOP_INDEX_SHIFT][(i >>> BOTTOM_INDEX_SHIFT) & BOTTOM_INDEX_MASK] = b; } - if ((i & GATEA20_MASK) == i) { + if((i & GATEA20_MASK) == i) { try { a20MaskedIndex[i >>> TOP_INDEX_SHIFT][(i >>> BOTTOM_INDEX_SHIFT) & BOTTOM_INDEX_MASK] = b; } catch (NullPointerException n) { diff --git a/org/jpc/emulator/memory/codeblock/ArrayBackedInstructionSource.java b/org/jpc/emulator/memory/codeblock/ArrayBackedInstructionSource.java index 559ac0e..85be5b1 100644 --- a/org/jpc/emulator/memory/codeblock/ArrayBackedInstructionSource.java +++ b/org/jpc/emulator/memory/codeblock/ArrayBackedInstructionSource.java @@ -55,7 +55,8 @@ public class ArrayBackedInstructionSource implements InstructionSource { * @param microcodes array of microcode values * @param positions array of x86 offsets */ - public ArrayBackedInstructionSource(int[] microcodes, int[] positions) { + public ArrayBackedInstructionSource(int[] microcodes, int[] positions) + { this.microcodes = microcodes; this.positions = positions; @@ -63,7 +64,8 @@ public class ArrayBackedInstructionSource implements InstructionSource { x86End = 0; } - public void reset() { + public void reset() + { x86Start = 0; x86End = 0; readOffset=0; @@ -71,37 +73,39 @@ public class ArrayBackedInstructionSource implements InstructionSource { operationStart=0; } - public boolean getNext() { - if (operationEnd >= microcodes.length) { + public boolean getNext() + { + if(operationEnd >= microcodes.length) return false; - } operationStart = readOffset = operationEnd++; x86Start = x86End; - while ((operationEnd < microcodes.length) && (positions[operationEnd] == positions[operationEnd - 1])) { + while((operationEnd < microcodes.length) && (positions[operationEnd] == positions[operationEnd - 1])) operationEnd++; - } x86End = positions[operationEnd - 1]; return true; } - public int getMicrocode() { - if (readOffset < operationEnd) { + public int getMicrocode() + { + if(readOffset < operationEnd) return microcodes[readOffset++]; - } else { + else { System.err.println("Critical error: Attempting to read past end of microcode array."); throw new IllegalStateException("Read past end of microcode array"); } } - public int getLength() { + public int getLength() + { return operationEnd - operationStart; } - public int getX86Length() { + public int getX86Length() + { return x86End - x86Start; } } diff --git a/org/jpc/emulator/memory/codeblock/ByteSourceWrappedMemory.java b/org/jpc/emulator/memory/codeblock/ByteSourceWrappedMemory.java index c69b58b..8d49de1 100644 --- a/org/jpc/emulator/memory/codeblock/ByteSourceWrappedMemory.java +++ b/org/jpc/emulator/memory/codeblock/ByteSourceWrappedMemory.java @@ -59,7 +59,7 @@ class ByteSourceWrappedMemory implements ByteSource public void skip(int count) { - if (offset + count >= source.getSize()) + if(offset + count >= source.getSize()) throw new IndexOutOfBoundsException(); offset += count; } diff --git a/org/jpc/emulator/memory/codeblock/CodeBlockManager.java b/org/jpc/emulator/memory/codeblock/CodeBlockManager.java index 222528f..c937c54 100644 --- a/org/jpc/emulator/memory/codeblock/CodeBlockManager.java +++ b/org/jpc/emulator/memory/codeblock/CodeBlockManager.java @@ -54,7 +54,8 @@ public class CodeBlockManager implements SRDumpable *

* The default manager creates interpreted mode codeblocks. */ - public CodeBlockManager() { + public CodeBlockManager() + { byteSource = new ByteSourceWrappedMemory(); realModeChain = new DefaultCodeBlockFactory(new RealModeUDecoder(), new OptimisedCompiler(), BLOCK_LIMIT); @@ -66,12 +67,14 @@ public class CodeBlockManager implements SRDumpable { } - public CodeBlockManager(SRLoader input) { + public CodeBlockManager(SRLoader input) + { this(); input.objectCreated(this); } - private RealModeCodeBlock tryRealModeFactory(CodeBlockFactory ff, Memory memory, int offset) { + private RealModeCodeBlock tryRealModeFactory(CodeBlockFactory ff, Memory memory, int offset) + { try { byteSource.set(memory, offset); return ff.getRealModeCodeBlock(byteSource); @@ -80,7 +83,8 @@ public class CodeBlockManager implements SRDumpable } } - private ProtectedModeCodeBlock tryProtectedModeFactory(CodeBlockFactory ff, Memory memory, int offset, boolean operandSizeFlag) { + private ProtectedModeCodeBlock tryProtectedModeFactory(CodeBlockFactory ff, Memory memory, int offset, boolean operandSizeFlag) + { try { byteSource.set(memory, offset); return ff.getProtectedModeCodeBlock(byteSource, operandSizeFlag); @@ -89,7 +93,8 @@ public class CodeBlockManager implements SRDumpable } } - private Virtual8086ModeCodeBlock tryVirtual8086ModeFactory(CodeBlockFactory ff, Memory memory, int offset) { + private Virtual8086ModeCodeBlock tryVirtual8086ModeFactory(CodeBlockFactory ff, Memory memory, int offset) + { try { byteSource.set(memory, offset); return ff.getVirtual8086ModeCodeBlock(byteSource); @@ -104,10 +109,11 @@ public class CodeBlockManager implements SRDumpable * @param offset address in the given memory object * @return real mode codeblock instance */ - public RealModeCodeBlock getRealModeCodeBlockAt(Memory memory, int offset) { + public RealModeCodeBlock getRealModeCodeBlockAt(Memory memory, int offset) + { RealModeCodeBlock block; - if ((block = tryRealModeFactory(realModeChain, memory, offset)) == null) { + if((block = tryRealModeFactory(realModeChain, memory, offset)) == null) { System.err.println("Critical error: Can't find nor make suitable real mode codeblock."); throw new IllegalStateException("Couldn't find/make suitable realmode block"); } @@ -122,10 +128,11 @@ public class CodeBlockManager implements SRDumpable * @param operandSize true for 32-bit, false for 16-bit * @return protected mode codeblock instance */ - public ProtectedModeCodeBlock getProtectedModeCodeBlockAt(Memory memory, int offset, boolean operandSize) { + public ProtectedModeCodeBlock getProtectedModeCodeBlockAt(Memory memory, int offset, boolean operandSize) + { ProtectedModeCodeBlock block; - if ((block = tryProtectedModeFactory(protectedModeChain, memory, offset, operandSize)) == null) { + if((block = tryProtectedModeFactory(protectedModeChain, memory, offset, operandSize)) == null) { System.err.println("Critical error: Can't find nor make suitable protected mode codeblock."); throw new IllegalStateException("Couldn't find/make suitable pmode block"); } @@ -138,10 +145,11 @@ public class CodeBlockManager implements SRDumpable * @param offset address in the given memory object * @return Virtual8086 mode codeblock instance */ - public Virtual8086ModeCodeBlock getVirtual8086ModeCodeBlockAt(Memory memory, int offset) { + public Virtual8086ModeCodeBlock getVirtual8086ModeCodeBlockAt(Memory memory, int offset) + { Virtual8086ModeCodeBlock block; - if ((block = tryVirtual8086ModeFactory(virtual8086ModeChain, memory, offset)) == null) { + if((block = tryVirtual8086ModeFactory(virtual8086ModeChain, memory, offset)) == null) { System.err.println("Critical error: Can't find nor make suitable VM8086 mode codeblock."); throw new IllegalStateException("Couldn't find/make suitable VM86 block"); } diff --git a/org/jpc/emulator/memory/codeblock/SpanningCodeBlock.java b/org/jpc/emulator/memory/codeblock/SpanningCodeBlock.java index 7a8f04f..80ff4e8 100644 --- a/org/jpc/emulator/memory/codeblock/SpanningCodeBlock.java +++ b/org/jpc/emulator/memory/codeblock/SpanningCodeBlock.java @@ -82,7 +82,7 @@ public abstract class SpanningCodeBlock implements CodeBlock public String getDisplayString() { - if (lastBlock != null) + if(lastBlock != null) return lastBlock.getDisplayString(); else return "Undecoded Spanning Block"; diff --git a/org/jpc/emulator/memory/codeblock/SpanningProtectedModeCodeBlock.java b/org/jpc/emulator/memory/codeblock/SpanningProtectedModeCodeBlock.java index 142ccd2..e99b2f1 100644 --- a/org/jpc/emulator/memory/codeblock/SpanningProtectedModeCodeBlock.java +++ b/org/jpc/emulator/memory/codeblock/SpanningProtectedModeCodeBlock.java @@ -59,12 +59,12 @@ class SpanningProtectedModeCodeBlock extends SpanningCodeBlock implements Protec AddressSpace memory = cpu.linearMemory; int address = cpu.getInstructionPointer(); boolean opSize = cpu.cs.getDefaultSizeFlag(); - for (int i = 0; (i < factories.length) && (block == null); i++) { + for(int i = 0; (i < factories.length) && (block == null); i++) try { byteSource.set(memory, address); block = factories[i].getProtectedModeCodeBlock(byteSource, opSize); - } catch (IllegalStateException e) {} - } + } catch (IllegalStateException e) { + } length = block.getX86Length(); byteSource.set(null, 0); return block; diff --git a/org/jpc/emulator/memory/codeblock/SpanningRealModeCodeBlock.java b/org/jpc/emulator/memory/codeblock/SpanningRealModeCodeBlock.java index aa61517..c69f1b7 100644 --- a/org/jpc/emulator/memory/codeblock/SpanningRealModeCodeBlock.java +++ b/org/jpc/emulator/memory/codeblock/SpanningRealModeCodeBlock.java @@ -53,12 +53,11 @@ class SpanningRealModeCodeBlock extends SpanningCodeBlock implements RealModeCod AddressSpace memory = cpu.physicalMemory; int address = cpu.getInstructionPointer(); - for (int i = 0; (i < factories.length) && (block == null); i++) { + for(int i = 0; (i < factories.length) && (block == null); i++) try { byteSource.set(memory, address); block = factories[i].getRealModeCodeBlock(byteSource); } catch (IllegalStateException e) {} - } byteSource.set(null, 0); return block; diff --git a/org/jpc/emulator/memory/codeblock/SpanningVirtual8086ModeCodeBlock.java b/org/jpc/emulator/memory/codeblock/SpanningVirtual8086ModeCodeBlock.java index bcafcfa..f076983 100644 --- a/org/jpc/emulator/memory/codeblock/SpanningVirtual8086ModeCodeBlock.java +++ b/org/jpc/emulator/memory/codeblock/SpanningVirtual8086ModeCodeBlock.java @@ -52,12 +52,12 @@ class SpanningVirtual8086ModeCodeBlock extends SpanningCodeBlock implements Virt Virtual8086ModeCodeBlock block = null; AddressSpace memory = cpu.linearMemory; int address = cpu.getInstructionPointer(); - for (int i = 0; (i < factories.length) && (block == null); i++) { + for(int i = 0; (i < factories.length) && (block == null); i++) try { byteSource.set(memory, address); block = factories[i].getVirtual8086ModeCodeBlock(byteSource); - } catch (IllegalStateException e) {} - } + } catch (IllegalStateException e) { + } byteSource.set(null, 0); return block; diff --git a/org/jpc/emulator/memory/codeblock/optimised/OpcodeLogger.java b/org/jpc/emulator/memory/codeblock/optimised/OpcodeLogger.java index 2a99518..5484260 100644 --- a/org/jpc/emulator/memory/codeblock/optimised/OpcodeLogger.java +++ b/org/jpc/emulator/memory/codeblock/optimised/OpcodeLogger.java @@ -50,7 +50,7 @@ public class OpcodeLogger { public boolean hasImmediate(int opcode) { - if ((opcode == 3) | (opcode == 8) | (opcode == 13) + if((opcode == 3) | (opcode == 8) | (opcode == 13) | (opcode == 26) | (opcode == 27) | (opcode == 49) | (opcode == 168) | (opcode == 229) | (opcode == 255)) return true; @@ -61,23 +61,21 @@ public class OpcodeLogger { public void addBlock(int[] microcodes) { boolean IM = false; - for (int j=0; j < microcodes.length; j++) - { - if (!IM) - { + for(int j=0; j < microcodes.length; j++) { + if(!IM) { addOpcode(microcodes[j]); - if (hasImmediate(microcodes[j])) + if(hasImmediate(microcodes[j])) IM = true; } else IM = false; } } - public void addOpcode(int opcode) { + public void addOpcode(int opcode) + { opcodeCounts[opcode]++; count++; - if (count >= MAX) - { + if(count >= MAX) { printStats(); count = 0; } @@ -87,37 +85,32 @@ public class OpcodeLogger { { System.out.println("*******************************"); System.out.println(name); - for (int i=0; i < opcodeCounts.length; i++) - { - if (opcodeCounts[i] > 0) - { + for(int i=0; i < opcodeCounts.length; i++) + if(opcodeCounts[i] > 0) System.out.println(reflectedNameCache.get(String.valueOf(i)) + ": " + opcodeCounts[i]); - } - } } private static Hashtable reflectedNameCache = new Hashtable(); + static { - try - { + try { Class cls = MicrocodeSet.class; Field[] flds = cls.getDeclaredFields(); int count = 0; - for (int i=0; i cumulativeX86Length[i-1]) count++; - } + for(int i = 1; i < cumulativeX86Length.length; i++) + if(cumulativeX86Length[i] > cumulativeX86Length[i-1]) count++; x86Count = count; } } public int getX86Length() { - if (microcodes.length == 0) + if(microcodes.length == 0) return 0; return cumulativeX86Length[microcodes.length-1]; } @@ -101,7 +100,7 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock { StringBuilder buf = new StringBuilder(); buf.append(this.toString()).append('\n'); - for (int i=0; i>> (16 - reg2)); reg1 = reg2; reg2 = i; - } else - { + } else { i = (reg1 & 0xFFFF) | (reg0 << 16); reg0 = (reg1 << (reg2 - 16)) | ((reg0 & 0xFFFF) >>> (32 - reg2)); reg1 = reg2 - 15; @@ -531,19 +528,18 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock } break; case SHLD_O32: { int i = reg0; reg2 &= 0x1f; - if (reg2 != 0) + if(reg2 != 0) reg0 = (reg0 << reg2) | (reg1 >>> (32 - reg2)); reg1 = reg2; reg2 = i; } break; case SHRD_O16: { int i = reg0; reg2 &= 0x1f; - if (reg2 < 16) { + if(reg2 < 16) { reg0 = (reg0 >>> reg2) | (reg1 << (16 - reg2)); reg1 = reg2; reg2 = i; - } else - { + } else { i = (reg0 & 0xFFFF) | (reg1 << 16); reg0 = (reg1 >>> (reg2 -16)) | (reg0 << (32 - reg2)); reg1 = reg2; @@ -552,13 +548,13 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock } break; case SHRD_O32: { int i = reg0; reg2 &= 0x1f; - if (reg2 != 0) + if(reg2 != 0) reg0 = (reg0 >>> reg2) | (reg1 << (32 - reg2)); reg1 = reg2; reg2 = i; } break; - case CWD: if ((cpu.eax & 0x8000) == 0) cpu.edx &= 0xffff0000; else cpu.edx |= 0x0000ffff; break; - case CDQ: if ((cpu.eax & 0x80000000) == 0) cpu.edx = 0; else cpu.edx = -1; break; + case CWD: if((cpu.eax & 0x8000) == 0) cpu.edx &= 0xffff0000; else cpu.edx |= 0x0000ffff; break; + case CDQ: if((cpu.eax & 0x80000000) == 0) cpu.edx = 0; else cpu.edx = -1; break; case AAA: aaa(); break; case AAD: aad(reg0); break; @@ -574,29 +570,26 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock case CLC: cpu.setCarryFlag(false); break; case STC: cpu.setCarryFlag(true); break; case CLI: - if (cpu.getIOPrivilegeLevel() >= cpu.getCPL()) { + if(cpu.getIOPrivilegeLevel() >= cpu.getCPL()) { cpu.eflagsInterruptEnable = false; cpu.eflagsInterruptEnableSoon = false; - } else { - if ((cpu.getIOPrivilegeLevel() < cpu.getCPL()) && (cpu.getCPL() == 3) && ((cpu.getCR4() & 1) != 0)) { + } else + if((cpu.getIOPrivilegeLevel() < cpu.getCPL()) && (cpu.getCPL() == 3) && ((cpu.getCR4() & 1) != 0)) { cpu.eflagsInterruptEnableSoon = false; - } else - { + } else { System.err.println("Emulated: IOPL=" + cpu.getIOPrivilegeLevel() + ", CPL=" + cpu.getCPL()); throw new ProcessorException(ProcessorException.Type.GENERAL_PROTECTION, 0, true); } - } break; case STI: - if (cpu.getIOPrivilegeLevel() >= cpu.getCPL()) { + if(cpu.getIOPrivilegeLevel() >= cpu.getCPL()) { cpu.eflagsInterruptEnable = true; cpu.eflagsInterruptEnableSoon = true; - } else { - if ((cpu.getIOPrivilegeLevel() < cpu.getCPL()) && (cpu.getCPL() == 3) && ((cpu.getEFlags() & (1 << 20)) == 0)) { + } else + if((cpu.getIOPrivilegeLevel() < cpu.getCPL()) && (cpu.getCPL() == 3) && ((cpu.getEFlags() & (1 << 20)) == 0)) cpu.eflagsInterruptEnableSoon = true; - } else + else throw new ProcessorException(ProcessorException.Type.GENERAL_PROTECTION, 0, true); - } break; case CLD: cpu.eflagsDirection = false; break; case STD: cpu.eflagsDirection = true; break; @@ -610,7 +603,7 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock case DEC: reg0--; break; case HALT: - if (cpu.getCPL() != 0) + if(cpu.getCPL() != 0) throw new ProcessorException(ProcessorException.Type.GENERAL_PROTECTION, 0, true); else cpu.waitForInterrupt(); break; @@ -688,49 +681,49 @@ public class ProtectedModeUBlock implements ProtectedModeCodeBlock case CALL_O16: - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_o16_a32(reg0); else call_o16_a16(reg0); break; case CALL_O32: - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_o32_a32(reg0); else call_o32_a16(reg0); break; case CALL_ABS_O16: { - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_abs_o16_a32(reg0); else call_abs_o16_a16(reg0); } break; case CALL_ABS_O32: { - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_abs_o32_a32(reg0); else call_abs_o32_a16(reg0); } break; case CALL_FAR_O16: { - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_far_o16_a32(reg0, reg1); else call_far_o16_a16(reg0, reg1); } break; case CALL_FAR_O32: { - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) call_far_o32_a32(reg0, reg1); else call_far_o32_a16(reg0, reg1); } break; case RET_O16: { - if (cpu.ss.getDefaultSizeFlag()) + if(cpu.ss.getDefaultSizeFlag()) ret_o16_a32(); else ret_o16_a16(); diff --git a/org/jpc/emulator/memory/codeblock/optimised/ProtectedModeUDecoder.java b/org/jpc/emulator/memory/codeblock/optimised/ProtectedModeUDecoder.java index 1450fee..1d9cc2c 100644 --- a/org/jpc/emulator/memory/codeblock/optimised/ProtectedModeUDecoder.java +++ b/org/jpc/emulator/memory/codeblock/optimised/ProtectedModeUDecoder.java @@ -1797,7 +1797,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BTR_O32); else working.write(BTR_O16); @@ -1816,7 +1816,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BTC_O32); else working.write(BTC_O16); @@ -1830,7 +1830,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource default: working.write(BT_MEM); break; case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4: case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BT_O32); else working.write(BT_O16); @@ -1841,7 +1841,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource default: working.write(BTS_MEM); break; case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4: case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BTS_O32); else working.write(BTS_O16); @@ -1852,7 +1852,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource default: working.write(BTR_MEM); break; case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4: case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BTR_O32); else working.write(BTR_O16); @@ -1863,7 +1863,7 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource default: working.write(BTC_MEM); break; case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4: case 0xc5: case 0xc6: case 0xc7: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(BTC_O32); else working.write(BTC_O16); @@ -1876,13 +1876,13 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource case 0xfbd: working.write(BSR); break; //BSR Gv, Ev case 0xfbe: //MOVSX Gv, Eb - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(SIGN_EXTEND_8_32); else working.write(SIGN_EXTEND_8_16); break; case 0xfbf: //MOVSX Gv, Ew - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(SIGN_EXTEND_16_32); break; @@ -1930,38 +1930,32 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource break; case 0xd900: - if ((modrm & 0xc0) != 0xc0) - { - switch (modrm & 0x38) - { + if((modrm & 0xc0) != 0xc0) { + switch (modrm & 0x38) { case 0x00: working.write(FPUSH); break; case 0x10: case 0x18: case 0x28: case 0x38: break; case 0x20: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(FLDENV_28); else working.write(FLDENV_14); break; case 0x30: - if ((prefices & PREFICES_OPERAND) != 0) + if((prefices & PREFICES_OPERAND) != 0) working.write(FSTENV_28); else working.write(FSTENV_14); break; } - } - else - { - switch (modrm & 0xf8) - { + } else { + switch (modrm & 0xf8) { case 0xc0: working.write(FPUSH); break; case 0xc8: break; } - switch (modrm) - { + switch (modrm) { case 0xd0: break; case 0xe0: working.write(FCHS); break; case 0xe1: working.write(FABS); break; @@ -1995,10 +1989,8 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource break; case 0xda00: - if ((modrm & 0xc0) != 0xc0) - { - switch (modrm & 0x38) - { + if((modrm & 0xc0) != 0xc0) { + switch (modrm & 0x38) { case 0x00: working.write(FADD); break; case 0x08: working.write(FMUL); break; case 0x10: @@ -2008,11 +2000,8 @@ public final class ProtectedModeUDecoder implements Decoder, InstructionSource case 0x30: case 0x38: working.write(FDIV); break; } - } - else - { - switch (modrm & 0xf8) - { + } else { + switch (modrm & 0xf8) { case 0xc0: working.write(FCMOVB); break; case 0xc8: working.write(FCMOVE); break; case 0xd0: working.write(FCMOVBE); break; -- 2.11.4.GIT