View Javadoc

1   package com.tapina.robe.runtime;
2   
3   import java.util.LinkedList;
4   import java.util.Iterator;
5   import java.util.logging.Logger;
6   import java.io.Writer;
7   import java.io.IOException;
8   import java.io.OutputStreamWriter;
9   import java.io.FileWriter;
10  
11  /***
12   * Created by IntelliJ IDEA.
13   * User: gareth
14   * Date: Aug 21, 2003
15   * Time: 7:59:53 AM
16   */
17  public final class InterpretedCodeBlock extends CodeBlock {
18      private static final Logger log = Logger.getLogger(InterpretedCodeBlock.class.getName());
19      private int executeCount = 0;
20      private final LinkedList instructions = new LinkedList();
21  
22      public InterpretedCodeBlock(int address) {
23          super(address);
24          setExtendable(true);
25      }
26  
27      public final void appendInstruction(Instruction instruction) {
28          instructions.add(instruction);
29      }
30  
31      public final Instruction getInstruction(int index) {
32          return (Instruction) instructions.get(index);
33      }
34  
35      public final void execute(Environment environment) throws DecoderException {
36          executeCount++;
37  /*
38          if (executeCount == 2) {
39              dumpJavaSource();
40          }
41  */
42  /*
43          if (executeCount % 1000 == 0) {
44              log.info("Executed code at &" + Integer.toHexString(getAddress()) + " " + executeCount + " times");
45          }
46  */
47          // Iterate over the instructions
48          final int[] R = environment.getCpu().R;
49          for (Iterator iterator = instructions.iterator(); iterator.hasNext();) {
50              Instruction instruction = (Instruction) iterator.next();
51              if (instruction.checkAndExecute(environment)) {
52                  return;
53              }
54              R[15] += 4;
55          }
56          // Fetch more instructions since we haven't finished yet
57          do {
58              final int fetchAddress = (R[15] & CPU.ADDRESS_MASK) - 8;
59              Instruction instruction = environment.getMemoryMap().fetchAndDecode(fetchAddress);
60  //            System.out.println(Integer.toHexString(fetchAddress) + " " + instruction.toString());
61              instructions.addLast(instruction);
62              if (instruction.checkAndExecute(environment)) {
63                  return;
64              }
65              R[15] += 4;
66          } while (true);
67      }
68  
69      public void dumpJavaSource() {
70          try {
71              Writer out = new FileWriter("DumpedCodeBlock" + Integer.toHexString(getAddress()) + ".java");
72              dumpJavaSource(out);
73              out.close();
74          } catch (IOException e) {
75              throw new RuntimeException(e);
76          }
77      }
78  
79      public void dumpJavaSource(Writer out) throws IOException {
80          final String dumpedClassName = "DumpedCodeBlock" + Integer.toHexString(getAddress());
81          out.write("public final class ");
82          out.write(dumpedClassName);
83          out.write(" extends CodeBlock {\n\n");
84          out.write("public ");
85          out.write(dumpedClassName);
86          out.write("() { super(0x");
87          out.write(Integer.toHexString(getAddress()));
88          out.write("); }\n\n");
89  
90          out.write("public void execute(Environment env) {\n");
91          out.write(" final CPU cpu = env.getCpu();\n");
92          out.write(" final int[] R = cpu.R;\n");
93          out.write(" final MemoryMap memoryMap = env.getMemoryMap();\n\n");
94          for (Iterator iterator = instructions.iterator(); iterator.hasNext();) {
95              Instruction instruction = (Instruction) iterator.next();
96              instruction.dumpJavaSourceUnconditional(out);
97          }
98          out.write("}\n");
99  
100         out.write("}\n");
101     }
102 }