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