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
|
/**
* @module mycpu
* @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
* @brief TODO
* @date 11.05.2009
* @par Exercise
* 3
*/
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <stdlib.h>
#include "ccpu.h"
#include "cmem.h"
#include "cprogram.h"
using namespace std;
namespace po = boost::program_options;
/** TODO */
void myterminate()
{
cerr << "Unexpected termination" << endl;
abort();
}
/**
* @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
*
* TODO
*/
int main(int argc, char* argv[])
{
//TODO set_terminate(myterminate);
string me(argv[0]);
/* define commandline options */
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "this help message")
("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 << " -c <programfile> [-m <memoryfile>]" << endl;
cout << desc << endl;
return 0;
}
/* create memory and optionally initialize memory from file */
CMem memory;
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(cout);
#endif
}
/* create program instance */
CProgram 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(cout);
#endif
/* create cpu and execute the program */
try
{
CCPU cpu(256);
cpu.setMemory(&memory);
cpu.setProgram(&program);
cpu.run();
#if DEBUG
cpu.dumpRegisters(cout);
#endif
}
catch(runtime_error& ex)
{
cerr << me << ": Error while executing program:" << endl
<< " " << ex.what() << endl;
return 1;
}
return 0;
}
/* vim: set et sw=2 ts=2: */
|