summaryrefslogtreecommitdiffstats
path: root/ue3/mycpu/mycpu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue3/mycpu/mycpu.cpp')
-rw-r--r--ue3/mycpu/mycpu.cpp127
1 files changed, 94 insertions, 33 deletions
diff --git a/ue3/mycpu/mycpu.cpp b/ue3/mycpu/mycpu.cpp
index af6fe08..b25e721 100644
--- a/ue3/mycpu/mycpu.cpp
+++ b/ue3/mycpu/mycpu.cpp
@@ -1,17 +1,24 @@
1/** 1/**
2 * @module mycpu 2 * @module mycpu
3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) 3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
4 * @brief mycpu reads a program file and optional a memory file given as 4 * @brief mycpu executes a programfile (in simple assembler) by parsing the
5 * commandline option. 5 * programfile first. This creates a vector of instructions, which will
6 * On error (e.g. unknown function) the program will terminate 6 * be executed in linear order (except jumps) afterwards. In order to
7 * @date 11.05.2009 7 * initialize the memory of the cpu before execution an optional
8 * memoryfile can be passed as commandline option.
9 * @date 13.05.2009
8 * @par Exercise 10 * @par Exercise
9 * 3 11 * 4
10 */ 12 */
11 13
12#include <iostream>
13#include <boost/program_options.hpp> 14#include <boost/program_options.hpp>
15#include <iostream>
16#include <fstream>
17#include <stdexcept>
18#include <stdlib.h>
14#include "ccpu.h" 19#include "ccpu.h"
20#include "cmem.h"
21#include "cprogram.h"
15 22
16using namespace std; 23using namespace std;
17namespace po = boost::program_options; 24namespace po = boost::program_options;
@@ -26,8 +33,10 @@ namespace po = boost::program_options;
26 * @exception none 33 * @exception none
27 * @conditions none 34 * @conditions none
28 * 35 *
29 * setup commandline options, parse them and pass scriptfile to scriptparser 36 * parse commandline options, create and initialize memory,
30 * instance. On error print error message to stderr. 37 * create cprogram instance, which parses the programfile and
38 * execute CCPU::run()
39 * On error print error message to stderr.
31 * Unknown commandline options will print a usage message. 40 * Unknown commandline options will print a usage message.
32 */ 41 */
33int main(int argc, char* argv[]) 42int main(int argc, char* argv[])
@@ -37,9 +46,9 @@ int main(int argc, char* argv[])
37 /* define commandline options */ 46 /* define commandline options */
38 po::options_description desc("Allowed options"); 47 po::options_description desc("Allowed options");
39 desc.add_options() 48 desc.add_options()
40 ("help,h", "this help message") 49 ("help,h", "this help message")
41 ("progfile,c", po::value<string>(), "program file") 50 ("compile,c", po::value<string>(), "input programfile")
42 ("memfile,m", po::value<string>(), "memory file"); 51 ("memory,m", po::value<string>(), "input memoryfile");
43 52
44 /* parse commandline options */ 53 /* parse commandline options */
45 po::variables_map vm; 54 po::variables_map vm;
@@ -50,43 +59,95 @@ int main(int argc, char* argv[])
50 } 59 }
51 catch(po::error& ex) 60 catch(po::error& ex)
52 { 61 {
53 cerr << "Error: " << ex.what() << endl; 62 cerr << me << ": Error: " << ex.what() << endl;
54 } 63 }
55 64
56 /* print usage upon request or missing params */ 65 /* print usage upon request or missing params */
57 if (vm.count("help") || !vm.count("progfile")) 66 if (vm.count("help") || !vm.count("compile"))
58 { 67 {
59 cout << "Usage: " << me << " -c <programfile> [ -m <memoryfile>]" << endl; 68 cout << "Usage: " << me << " -c <programfile> [-m <memoryfile>]" << endl;
60 cout << desc << endl; 69 cout << desc << endl;
61 if ( vm.count("help")) 70 return 0;
62 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;
63 return 1; 110 return 1;
64 } 111 }
65 112
66 string memfile("");
67 if (vm.count("memfile"))
68 memfile = vm["memfile"].as<string>();
69
70 CCPU cpu(vm["progfile"].as<string>(), memfile);
71 cpu.proceed();
72
73/* CScriptparser parser(vm["c"].as<string>());
74 try 113 try
75 { 114 {
76 parser.parse(); 115 program.compile(file);
116 file.close();
77 } 117 }
78 catch(CScriptparser::ParserError& ex) 118 catch(runtime_error& ex)
79 { 119 {
80 cerr << me << ": Error while processing scriptfile: " << ex.what() << endl; 120 file.close();
81 if (!ex.getLine().empty()) 121 cerr << me << ": Error while compiling programfile:" << endl
82 cerr << "Scriptline: '" << ex.getLine() << "'" << endl; 122 << " " << ex.what() << endl;
83 return 1; 123 return 1;
84 } 124 }
85 catch(exception& ex) 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)
86 { 143 {
87 cerr << me << ": Unexpected exception: " << ex.what() << endl; 144 cerr << me << ": Error while executing program:" << endl
145 << " " << ex.what() << endl;
146#if DEBUG
147 memory.dump(cerr);
148#endif
88 return 1; 149 return 1;
89 }*/ 150 }
90 151
91 return 0; 152 return 0;
92} 153}