diff options
| author | manuel <manuel@nc8430.lan> | 2009-05-12 23:18:17 +0200 |
|---|---|---|
| committer | manuel <manuel@nc8430.lan> | 2009-05-12 23:18:17 +0200 |
| commit | 45581d3d376e8deed84952cb838ae330549e5241 (patch) | |
| tree | ab23bd5cc1f2e32e95faa372b3b46e69ccb6a3c3 /ue3/mycpu/mycpu.cpp | |
| parent | fe1ef6b47f59899e8687bb1dcc92eba1d103a08f (diff) | |
| download | ooprog-45581d3d376e8deed84952cb838ae330549e5241.tar.gz ooprog-45581d3d376e8deed84952cb838ae330549e5241.tar.bz2 ooprog-45581d3d376e8deed84952cb838ae330549e5241.zip | |
my cpu design
Diffstat (limited to 'ue3/mycpu/mycpu.cpp')
| -rw-r--r-- | ue3/mycpu/mycpu.cpp | 152 |
1 files changed, 152 insertions, 0 deletions
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 @@ | |||
| 1 | /** | ||
| 2 | * @module mycpu | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief TODO | ||
| 5 | * @date 11.05.2009 | ||
| 6 | * @par Exercise | ||
| 7 | * 3 | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <boost/program_options.hpp> | ||
| 11 | #include <iostream> | ||
| 12 | #include <fstream> | ||
| 13 | #include <stdexcept> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include "ccpu.h" | ||
| 16 | #include "cmem.h" | ||
| 17 | #include "cprogram.h" | ||
| 18 | |||
| 19 | using namespace std; | ||
| 20 | namespace po = boost::program_options; | ||
| 21 | |||
| 22 | /** TODO */ | ||
| 23 | void myterminate() | ||
| 24 | { | ||
| 25 | cerr << "Unexpected termination" << endl; | ||
| 26 | abort(); | ||
| 27 | } | ||
| 28 | |||
| 29 | /** | ||
| 30 | * @func main | ||
| 31 | * @brief program entry point | ||
| 32 | * @param argc standard parameter of main | ||
| 33 | * @param argv standard parameter of main | ||
| 34 | * @return 0 on success, not 0 otherwise | ||
| 35 | * @globalvars none | ||
| 36 | * @exception none | ||
| 37 | * @conditions none | ||
| 38 | * | ||
| 39 | * TODO | ||
| 40 | */ | ||
| 41 | int main(int argc, char* argv[]) | ||
| 42 | { | ||
| 43 | //TODO set_terminate(myterminate); | ||
| 44 | string me(argv[0]); | ||
| 45 | |||
| 46 | /* define commandline options */ | ||
| 47 | po::options_description desc("Allowed options"); | ||
| 48 | desc.add_options() | ||
| 49 | ("help,h", "this help message") | ||
| 50 | ("compile,c", po::value<string>(), "input programfile") | ||
| 51 | ("memory,m", po::value<string>(), "input memoryfile"); | ||
| 52 | |||
| 53 | /* parse commandline options */ | ||
| 54 | po::variables_map vm; | ||
| 55 | try | ||
| 56 | { | ||
| 57 | po::store(po::parse_command_line(argc, argv, desc), vm); | ||
| 58 | po::notify(vm); | ||
| 59 | } | ||
| 60 | catch(po::error& ex) | ||
| 61 | { | ||
| 62 | cerr << me << ": Error: " << ex.what() << endl; | ||
| 63 | } | ||
| 64 | |||
| 65 | /* print usage upon request or missing params */ | ||
| 66 | if (vm.count("help") || !vm.count("compile")) | ||
| 67 | { | ||
| 68 | cout << "Usage: " << me << " -c <programfile> [-m <memoryfile>]" << endl; | ||
| 69 | cout << desc << endl; | ||
| 70 | 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(cout); | ||
| 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; | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | |||
| 113 | try | ||
| 114 | { | ||
| 115 | program.compile(file); | ||
| 116 | file.close(); | ||
| 117 | } | ||
| 118 | catch(runtime_error& ex) | ||
| 119 | { | ||
| 120 | file.close(); | ||
| 121 | cerr << me << ": Error while compiling programfile:" << endl | ||
| 122 | << " " << ex.what() << endl; | ||
| 123 | return 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | #if DEBUG | ||
| 127 | program.dump(cout); | ||
| 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(cout); | ||
| 140 | #endif | ||
| 141 | } | ||
| 142 | catch(runtime_error& ex) | ||
| 143 | { | ||
| 144 | cerr << me << ": Error while executing program:" << endl | ||
| 145 | << " " << ex.what() << endl; | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | |||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | /* vim: set et sw=2 ts=2: */ | ||
