Introduce TinyHttp server
[lcapit-junk-code.git] / emu8086 / src / EmulatorShell.java
blobb5310b3c77aca406c293b43935979b0a34260b6b
1 import java.io.BufferedReader;
2 import java.io.FileNotFoundException;
3 import java.io.FileReader;
4 import java.io.IOException;
6 /**
7 * Implements a 8086 Emulator shell based.
8 *
9 * @author Luiz Fernando N. Capitulino
11 public class EmulatorShell {
12 private static Emulator emulator;
13 private static BufferedReader cmdfile;
14 private static boolean verbose;
15 private static boolean batchmode;
17 private static void emulatorInit()
19 verbose = false;
20 emulator = new Emulator();
23 private static void shellPrint(String msg)
25 if (!batchmode)
26 System.out.println("-> " + msg);
29 private static void shellError(String msg)
31 System.err.println("-> ERROR: " + msg);
32 if (batchmode)
33 System.exit(1);
36 private static void fileCommandOpen(String file)
38 try {
39 cmdfile = new BufferedReader(new FileReader(file));
40 } catch (FileNotFoundException e) {
41 System.err.println(e.getMessage());
42 System.exit(1);
46 private static void showHelp()
48 System.out.println("load < filepath >");
49 System.out.println("next");
50 System.out.println("print < regs | mem < addr > >");
51 System.out.println("reset");
52 System.out.println("restore < name >");
53 System.out.println("save < name >");
54 System.out.println("restore < name >");
55 System.out.println("run");
56 System.out.println("quit");
57 System.out.println("verbose");
60 private static boolean process(String cmd)
62 if (cmd == null)
63 return true;
65 if (cmd.length() == 0 || cmd.startsWith("#"))
66 return false;
68 if (verbose)
69 shellPrint("cmd: " + cmd);
71 try {
72 if (cmd.startsWith("quit")) {
73 return true;
74 } else if (cmd.startsWith("load")) {
75 if (cmd.indexOf(' ') == -1) {
76 shellError("hm? No filename to load?");
77 return false;
79 String prog = cmd.substring(cmd.indexOf(' ') + 1);
80 shellPrint("loading: " + prog);
81 emulator.loadProgram(prog);
82 } else if (cmd.startsWith("run")) {
83 emulator.run();
84 } else if (cmd.startsWith("verbose")) {
85 verbose = !verbose;
86 } else if (cmd.startsWith("print")) {
87 if (cmd.indexOf(' ') == -1) {
88 shellError("hm? Print what?");
89 return false;
91 String arg = cmd.substring(cmd.indexOf(' ') + 1);
92 if (arg.startsWith("flags")) {
93 ProcStatus status = emulator.getStatus();
94 status.dump_flags();
95 } else if (arg.startsWith("regs")) {
96 ProcStatus status = emulator.getStatus();
97 status.dump_registers();
98 } else if (arg.startsWith("mem")) {
99 if (arg.indexOf(' ') == -1) {
100 shellError("hm? Print what address?");
101 return false;
103 String saddr = arg.substring(cmd.indexOf(' ') + 1);
104 int addr = Integer.parseInt(saddr, 16);
105 RAMMemory ram = emulator.getRAM();
106 ram.show(addr);
107 } else {
108 shellError("invalid print command");
110 } else if (cmd.startsWith("reset")) {
111 emulatorInit();
112 } else if (cmd.startsWith("next")) {
113 if (emulator.ExecuteNext() == false)
114 shellPrint("processor halted");
115 } else if (cmd.startsWith("save")) {
116 if (cmd.indexOf(' ') == -1) {
117 shellError("hm? What name?");
118 return false;
120 String name = cmd.substring(cmd.indexOf(' ') + 1);
121 String statedir = "/home/lcapitulino/.emu8086-states";
122 emulator.saveState(statedir, name);
123 } else if (cmd.startsWith("restore")) {
124 if (cmd.indexOf(' ') == -1) {
125 shellError("hm? What name?");
126 return false;
128 String name = cmd.substring(cmd.indexOf(' ') + 1);
129 String statedir = "/home/lcapitulino/.emu8086-states";
130 emulator.loadState(statedir, name);
131 } else if (cmd.startsWith("help")) {
132 showHelp();
133 } else {
134 shellError("unknown command: " + cmd);
136 } catch (Exception e) {
137 shellError(e.getMessage());
140 return false;
144 * @param args
146 public static void main(String[] args)
148 String file = "/dev/stdin";
150 if (args.length > 0) {
151 if (args.length != 2 ||
152 !args[0].startsWith("-b") ||
153 args[1].length() == 0) {
154 shellError("emu8086 [ -b < file > ]");
157 // Batch mode
158 file = args[1];
159 batchmode = true;
162 fileCommandOpen(file);
163 emulatorInit();
165 shellPrint("Emulator is ready!");
166 // main shell loop
167 try {
168 for (;;) {
169 if (!batchmode)
170 System.out.print(">>> ");
171 if (process(cmdfile.readLine()) == true) {
172 shellPrint("Exiting");
173 cmdfile.close();
174 System.exit(0);
177 } catch (IOException e) {
178 System.err.println(e.getMessage());