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.cpp212
1 files changed, 118 insertions, 94 deletions
diff --git a/ue4/mycpu/mycpu.cpp b/ue4/mycpu/mycpu.cpp
index 2d73177..ac4498a 100644
--- a/ue4/mycpu/mycpu.cpp
+++ b/ue4/mycpu/mycpu.cpp
@@ -12,20 +12,100 @@
12 */ 12 */
13 13
14#include <boost/program_options.hpp> 14#include <boost/program_options.hpp>
15#include <boost/lexical_cast.hpp>
15#include <iostream> 16#include <iostream>
16#include <fstream> 17#include <fstream>
17#include <stdexcept> 18#include <stdexcept>
18#include <stdlib.h> 19#include <stdlib.h>
19#include "cdat.h" 20#include "cdat.h"
20#include "cdatn.h"
21#include "cdatset.h" 21#include "cdatset.h"
22#include "cdatn.h"
23#include "ccpu.h"
22#include "cmem.h" 24#include "cmem.h"
23#include "cprogram.h" 25#include "cprogram.h"
24#include "ccpu.h" 26
27#define REGISTERS 256
25 28
26using namespace std; 29using namespace std;
27namespace po = boost::program_options; 30namespace po = boost::program_options;
28 31
32/* TODO */
33template<class T>
34void cpu_run(string& me, po::variables_map& vm, unsigned registers, T& datatype)
35{
36 CMem<T> memory;
37 /* optionally initialize memory from file */
38 if (vm.count("memory"))
39 {
40 string memoryfile(vm["memory"].as<string>());
41 ifstream file(memoryfile.c_str(), ios::in);
42 if (!file.is_open())
43 throw runtime_error("Unable to open memoryfile '" + memoryfile + "' for reading.");
44
45 try
46 {
47 memory.initialize(file, datatype);
48 file.close();
49 }
50 catch(CMemError& ex)
51 {
52 file.close();
53 std::stringstream sstr;
54 sstr << "Error while reading from memoryfile:" << endl << " " << ex.what();
55 throw runtime_error(sstr.str());
56 }
57
58#if DEBUG
59 memory.dump(cerr);
60#endif
61 }
62
63 /* create program instance */
64 CProgram<T> program;
65 string programfile(vm["compile"].as<string>());
66 ifstream file(programfile.c_str(), ios::in);
67 if (!file.is_open())
68 throw runtime_error("Unable to open programfile '" + programfile + "' for reading.");
69
70 try
71 {
72 program.compile(file);
73 file.close();
74 }
75 catch(CProgramError& ex)
76 {
77 file.close();
78 std::stringstream sstr;
79 sstr << "Error while compiling programfile:" << endl << " " << ex.what();
80 throw runtime_error(sstr.str());
81 }
82
83#if DEBUG
84 program.dump(cerr);
85#endif
86
87 /* execute the program */
88 CCPU<T> cpu(registers, datatype);
89 try
90 {
91 cpu.setMemory(&memory);
92 cpu.setProgram(&program);
93 cpu.run();
94#if DEBUG
95 //cpu.dumpRegisters(cerr);
96#endif
97 }
98 catch(CCPUError& ex)
99 {
100 std::stringstream sstr;
101 sstr << "Error while executing program:" << endl << " " << ex.what();
102#if DEBUG
103 memory.dump(cerr);
104#endif
105 throw runtime_error(sstr.str());
106 }
107}
108
29/** 109/**
30 * @func main 110 * @func main
31 * @brief program entry point 111 * @brief program entry point
@@ -34,7 +114,8 @@ namespace po = boost::program_options;
34 * @return 0 on success, not 0 otherwise 114 * @return 0 on success, not 0 otherwise
35 * @globalvars none 115 * @globalvars none
36 * @exception none 116 * @exception none
37 * @conditions none 117 * @pre none
118 * @post none
38 * 119 *
39 * parse commandline options, create and initialize memory, 120 * parse commandline options, create and initialize memory,
40 * create cprogram instance, which parses the programfile and 121 * create cprogram instance, which parses the programfile and
@@ -50,7 +131,7 @@ int main(int argc, char* argv[])
50 po::options_description desc("Allowed options"); 131 po::options_description desc("Allowed options");
51 desc.add_options() 132 desc.add_options()
52 ("help,h", "this help message") 133 ("help,h", "this help message")
53 ("format,f", po::value<string>(), "format") 134 ("format,f", po::value<string>(), "input format")
54 ("compile,c", po::value<string>(), "input programfile") 135 ("compile,c", po::value<string>(), "input programfile")
55 ("memory,m", po::value<string>(), "input memoryfile"); 136 ("memory,m", po::value<string>(), "input memoryfile");
56 137
@@ -64,6 +145,7 @@ int main(int argc, char* argv[])
64 catch(po::error& ex) 145 catch(po::error& ex)
65 { 146 {
66 cerr << me << ": Error: " << ex.what() << endl; 147 cerr << me << ": Error: " << ex.what() << endl;
148 return 1;
67 } 149 }
68 150
69 /* print usage upon request or missing params */ 151 /* print usage upon request or missing params */
@@ -74,107 +156,49 @@ int main(int argc, char* argv[])
74 return 0; 156 return 0;
75 } 157 }
76 158
77 /* create memory and optionally initialize memory from file */ 159 /* create memory, program and cpu from templates */
78 160 try
79 CMem *memory = NULL;
80 if (vm.count("format"))
81 { 161 {
82 string format(vm["format"].as<string>()); 162 if (vm.count("format"))
83 if(format == "s")
84 memory = new CMem<CDatSet<int> >();
85 else
86 { 163 {
87 try 164 string format(vm["format"].as<string>());
165 if (format == "s")
88 { 166 {
89 int bc = boost::lexical_cast<int>(format); 167 CDatSet datatype(0);
90 if (bc > 1 && bc < 33) 168 cpu_run<CDatSet>(me, vm, REGISTERS, datatype);
91 memory = new CMem<CDatN<int> >(); 169 }
92 } 170 else
93 catch(boost::bad_lexical_cast& ex)
94 { 171 {
95 std::stringstream sstr; 172 unsigned bc;
96 sstr << "Illegal format: (" << format << "): " << ex.what(); 173 try
97 throw std::runtime_error(sstr.str()); 174 {
175 bc = boost::lexical_cast<unsigned>(format);
176 }
177 catch(boost::bad_lexical_cast& ex)
178 {
179 cerr << me << ": Paramater 'format' has invalid or unknown format." << endl;
180 return 1;
181 }
182
183 if (bc < 2 || bc > 31)
184 {
185 cerr << me << ": Paramater 'format' must be inbetween 2 and 32." << endl;
186 return 1;
187 }
188
189 CDatN datatype(0, bc);
190 cpu_run<CDatN>(me, vm, REGISTERS, datatype);
98 } 191 }
99 } 192 }
100 } 193 else
101 else
102
103
104 if (vm.count("memory"))
105 {
106 string memoryfile(vm["memory"].as<string>());
107 ifstream file(memoryfile.c_str(), ios::in);
108 if (!file.is_open())
109 {
110 cerr << me << ": Unable to open memoryfile '" << memoryfile << "' for reading." << endl;
111 return 1;
112 }
113
114 try
115 {
116 memory->initialize(file);
117 file.close();
118 }
119 catch(runtime_error& ex)
120 { 194 {
121 file.close(); 195 CDat<int> datatype(0);
122 cerr << me << ": Error while reading from memoryfile:" << endl 196 cpu_run<CDat<int> >(me, vm, REGISTERS, datatype);
123 << " " << ex.what() << endl;
124 return 1;
125 } 197 }
126
127#if DEBUG
128 memory->dump(cerr);
129#endif
130 }
131
132 /* create program instance */
133 CProgram<CDat<int> > program;
134 string programfile(vm["compile"].as<string>());
135 ifstream file(programfile.c_str(), ios::in);
136 if (!file.is_open())
137 {
138 cerr << me << ": Unable to open programfile '" << programfile << "' for reading." << endl;
139 return 1;
140 }
141
142 try
143 {
144 program.compile(file);
145 file.close();
146 }
147 catch(runtime_error& ex)
148 {
149 file.close();
150 cerr << me << ": Error while compiling programfile:" << endl
151 << " " << ex.what() << endl;
152 return 1;
153 }
154
155#if DEBUG
156 program.dump(cerr);
157#endif
158
159
160 /* create cpu and execute the program */
161 try
162 {
163 CCPU<CDat<int> > cpu(256);
164 cpu.setMemory(memory);
165 cpu.setProgram(&program);
166 cpu.run();
167#if DEBUG
168 //cpu.dumpRegisters(cerr);
169#endif
170 } 198 }
171 catch(runtime_error& ex) 199 catch(runtime_error& ex)
172 { 200 {
173 cerr << me << ": Error while executing program:" << endl 201 cerr << me << ": " << ex.what() << endl;
174 << " " << ex.what() << endl;
175#if DEBUG
176 memory->dump(cerr);
177#endif
178 return 1; 202 return 1;
179 } 203 }
180 204