2 JPC-RR: A x86 PC Hardware Emulator
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
.emulator
.memory
.codeblock
.optimised
;
32 import org
.jpc
.emulator
.memory
.codeblock
.*;
36 * @author Chris Dennis
38 public class OptimisedCompiler
implements CodeBlockCompiler
{
40 private int bufferOffset
;
41 private int[] bufferMicrocodes
;
42 private int[] bufferPositions
;
44 public OptimisedCompiler()
46 bufferMicrocodes
= new int[100];
47 bufferPositions
= new int[100];
52 public RealModeCodeBlock
getRealModeCodeBlock(InstructionSource source
)
54 buildCodeBlockBuffers(source
);
56 int[] newMicrocodes
= new int[bufferOffset
];
57 int[] newPositions
= new int[bufferOffset
];
58 System
.arraycopy(bufferMicrocodes
, 0, newMicrocodes
, 0, bufferOffset
);
59 System
.arraycopy(bufferPositions
, 0, newPositions
, 0, bufferOffset
);
61 return new RealModeUBlock(newMicrocodes
, newPositions
);
64 public ProtectedModeCodeBlock
getProtectedModeCodeBlock(InstructionSource source
)
66 buildCodeBlockBuffers(source
);
68 int[] newMicrocodes
= new int[bufferOffset
];
69 int[] newPositions
= new int[bufferOffset
];
70 System
.arraycopy(bufferMicrocodes
, 0, newMicrocodes
, 0, bufferOffset
);
71 System
.arraycopy(bufferPositions
, 0, newPositions
, 0, bufferOffset
);
73 return new ProtectedModeUBlock(newMicrocodes
, newPositions
);
76 public Virtual8086ModeCodeBlock
getVirtual8086ModeCodeBlock(InstructionSource source
)
78 buildCodeBlockBuffers(source
);
80 int[] newMicrocodes
= new int[bufferOffset
];
81 int[] newPositions
= new int[bufferOffset
];
82 System
.arraycopy(bufferMicrocodes
, 0, newMicrocodes
, 0, bufferOffset
);
83 System
.arraycopy(bufferPositions
, 0, newPositions
, 0, bufferOffset
);
85 return new Virtual8086ModeUBlock(newMicrocodes
, newPositions
);
88 private void buildCodeBlockBuffers(InstructionSource source
)
93 while(source
.getNext()) {
94 int uCodeLength
= source
.getLength();
95 int uCodeX86Length
= source
.getX86Length();
96 position
+= uCodeX86Length
;
98 for(int i
= 0; i
< uCodeLength
; i
++) {
99 int data
= source
.getMicrocode();
101 bufferMicrocodes
[bufferOffset
] = data
;
102 bufferPositions
[bufferOffset
] = position
;
103 } catch (ArrayIndexOutOfBoundsException e
) {
104 int[] newMicrocodes
= new int[bufferMicrocodes
.length
* 2];
105 int[] newPositions
= new int[bufferMicrocodes
.length
* 2];
106 System
.arraycopy(bufferMicrocodes
, 0, newMicrocodes
, 0, bufferMicrocodes
.length
);
107 System
.arraycopy(bufferPositions
, 0, newPositions
, 0, bufferPositions
.length
);
108 bufferMicrocodes
= newMicrocodes
;
109 bufferPositions
= newPositions
;
110 bufferMicrocodes
[bufferOffset
] = data
;
111 bufferPositions
[bufferOffset
] = position
;