summaryrefslogtreecommitdiffstats
path: root/ue4/mycpu/mycpu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue4/mycpu/mycpu.cpp')
-rw-r--r--ue4/mycpu/mycpu.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/ue4/mycpu/mycpu.cpp b/ue4/mycpu/mycpu.cpp
new file mode 100644
index 0000000..b25e721
--- /dev/null
+++ b/ue4/mycpu/mycpu.cpp
@@ -0,0 +1,155 @@
1/**
2 * @module mycpu
3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
4 * @brief mycpu executes a programfile (in simple assembler) by parsing the
5 * programfile first. This creates a vector of instructions, which will
6 * be executed in linear order (except jumps) afterwards. In order to
7 * initialize the memory of the cpu before execution an optional
8 * memoryfile can be passed as commandline option.
9 * @date 13.05.2009
10 * @par Exercise
11 * 4
12 */
13
14#include <boost/program_options.hpp>
15#include <iostream>
16#include <fstream>
17#include <stdexcept>
18#include <stdlib.h>
19#include "ccpu.h"
20#include "cmem.h"
21#include "cprogram.h"
22
23using namespace std;
24namespace po = boost::program_options;
25
26/**
27 * @func main
28 * @brief program entry point
29 * @param argc standard parameter of main
30 * @param argv standard parameter of main
31 * @return 0 on success, not 0 otherwise
32 * @globalvars none
33 * @exception none
34 * @conditions none
35 *
36 * parse commandline options, create and initialize memory,
37 * create cprogram instance, which parses the programfile and
38 * execute CCPU::run()
39 * On error print error message to stderr.
40 * Unknown commandline options will print a usage message.
41 */
42int main(int argc, char* argv[])
43{
44 string me(argv[0]);
45
46 /* define commandline options */
47 po::options_description desc("Allowed options");
48 desc.add_options()
49 ("help,h", "this help message")
50 ("compile,c", po::value<string>(), "input programfile")
51 ("memory,m", po::value<string>(), "input memoryfile");
52
53 /* parse commandline options */
54 po::variables_map vm;
55 try
56 {
57 po::store(po::parse_command_line(argc, argv, desc), vm);
58 po::notify(vm);
59 }
60 catch(po::error& ex)
61 {
62 cerr << me << ": Error: " << ex.what() << endl;
63 }
64
65 /* print usage upon request or missing params */
66 if (vm.count("help") || !vm.count("compile"))
67 {
68 cout << "Usage: " << me << " -c <programfile> [-m <memoryfile>]" << endl;
69 cout << desc << endl;
70 return 0;
71 }
72
73 /* create memory and optionally initialize memory from file */
74 CMem memory;
75 if (vm.count("memory"))
76 {
77 string memoryfile(vm["memory"].as<string>());
78 ifstream file(memoryfile.c_str(), ios::in);
79 if (!file.is_open())
80 {
81 cerr << me << ": Unable to open memoryfile '" << memoryfile << "' for reading." << endl;
82 return 1;
83 }
84
85 try
86 {
87 memory.initialize(file);
88 file.close();
89 }
90 catch(runtime_error& ex)
91 {
92 file.close();
93 cerr << me << ": Error while reading from memoryfile:" << endl
94 << " " << ex.what() << endl;
95 return 1;
96 }
97
98#if DEBUG
99 memory.dump(cerr);
100#endif
101 }
102
103 /* create program instance */
104 CProgram program;
105 string programfile(vm["compile"].as<string>());
106 ifstream file(programfile.c_str(), ios::in);
107 if (!file.is_open())
108 {
109 cerr << me << ": Unable to open programfile '" << programfile << "' for reading." << endl;
110 return 1;
111 }
112
113 try
114 {
115 program.compile(file);
116 file.close();
117 }
118 catch(runtime_error& ex)
119 {
120 file.close();
121 cerr << me << ": Error while compiling programfile:" << endl
122 << " " << ex.what() << endl;
123 return 1;
124 }
125
126#if DEBUG
127 program.dump(cerr);
128#endif
129
130
131 /* create cpu and execute the program */
132 try
133 {
134 CCPU cpu(256);
135 cpu.setMemory(&memory);
136 cpu.setProgram(&program);
137 cpu.run();
138#if DEBUG
139 //cpu.dumpRegisters(cerr);
140#endif
141 }
142 catch(runtime_error& ex)
143 {
144 cerr << me << ": Error while executing program:" << endl
145 << " " << ex.what() << endl;
146#if DEBUG
147 memory.dump(cerr);
148#endif
149 return 1;
150 }
151
152 return 0;
153}
154
155/* vim: set et sw=2 ts=2: */