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 java
.lang
.reflect
.Field
;
33 import java
.lang
.reflect
.Modifier
;
34 import java
.util
.Hashtable
;
36 /**Logs frequencies of opcodes and periodically prints the results
40 public class OpcodeLogger
{
42 int[] opcodeCounts
= new int[MicrocodeSet
.MICROCODE_LIMIT
];
47 OpcodeLogger(String name
) {
51 public boolean hasImmediate(int opcode
)
53 if((opcode
== 3) | (opcode
== 8) | (opcode
== 13)
54 | (opcode
== 26) | (opcode
== 27) | (opcode
== 49)
55 | (opcode
== 168) | (opcode
== 229) | (opcode
== 255))
61 public void addBlock(int[] microcodes
)
64 for(int j
=0; j
< microcodes
.length
; j
++) {
66 addOpcode(microcodes
[j
]);
67 if(hasImmediate(microcodes
[j
]))
74 public void addOpcode(int opcode
)
76 opcodeCounts
[opcode
]++;
84 public void printStats()
86 System
.out
.println("*******************************");
87 System
.out
.println(name
);
88 for(int i
=0; i
< opcodeCounts
.length
; i
++)
89 if(opcodeCounts
[i
] > 0)
90 System
.out
.println(reflectedNameCache
.get(String
.valueOf(i
)) + ": " + opcodeCounts
[i
]);
93 private static Hashtable
<String
, String
> reflectedNameCache
= new Hashtable
<String
, String
>();
98 Class
<MicrocodeSet
> cls
= MicrocodeSet
.class;
99 Field
[] flds
= cls
.getDeclaredFields();
101 for(int i
=0; i
<flds
.length
; i
++) {
103 int mods
= f
.getModifiers();
104 if(!Modifier
.isPublic(mods
) || !Modifier
.isStatic(mods
) || !Modifier
.isFinal(mods
))
106 if(f
.getType() != int.class)
109 int value
= f
.getInt(null);
111 reflectedNameCache
.put(String
.valueOf(value
), f
.getName());
113 } catch (Throwable t
) {