From 45581d3d376e8deed84952cb838ae330549e5241 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 12 May 2009 23:18:17 +0200 Subject: my cpu design --- ue3/mycpu/mycpu.cpp | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 ue3/mycpu/mycpu.cpp (limited to 'ue3/mycpu/mycpu.cpp') diff --git a/ue3/mycpu/mycpu.cpp b/ue3/mycpu/mycpu.cpp new file mode 100644 index 0000000..af91fe7 --- /dev/null +++ b/ue3/mycpu/mycpu.cpp @@ -0,0 +1,152 @@ +/** + * @module mycpu + * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) + * @brief TODO + * @date 11.05.2009 + * @par Exercise + * 3 + */ + +#include +#include +#include +#include +#include +#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(), "input programfile") + ("memory,m", po::value(), "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 [-m ]" << 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()); + 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()); + 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: */ -- cgit v1.2.3