summaryrefslogtreecommitdiffstats
path: root/ue4/mycpu/ccpu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ue4/mycpu/ccpu.cpp')
-rw-r--r--ue4/mycpu/ccpu.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/ue4/mycpu/ccpu.cpp b/ue4/mycpu/ccpu.cpp
new file mode 100644
index 0000000..af86200
--- /dev/null
+++ b/ue4/mycpu/ccpu.cpp
@@ -0,0 +1,89 @@
1/**
2 * @module ccpu
3 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
4 * @brief CPU implementation. Used as a container for memory and instructions.
5 * Implements an run method to execute the program (= the instructions).
6 * @date 10.05.2009
7 */
8
9#ifdef DEBUG
10# include <iostream>
11# include <iomanip>
12#endif
13#include "ccpu.h"
14#include "displays.h"
15
16using namespace std;
17
18CCPU::CCPU(const unsigned cnt)
19 : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false)
20{
21 /* create registers */
22 m_registers = new CDat[cnt];
23 for(unsigned i = 0; i < cnt; ++i)
24 m_registers[i] = 0;
25
26 /* create displays */
27 m_displays.insert(new CDisplayWDEZ);
28 m_displays.insert(new CDisplayWHEX);
29}
30
31/*----------------------------------------------------------------------------*/
32
33CCPU::~CCPU()
34{
35 /* delete registers */
36 delete[] m_registers;
37 m_registers = NULL;
38
39 /* delete displays */
40 std::set<CDisplay *>::iterator it;
41 for (it = m_displays.begin() ; it != m_displays.end(); ++it)
42 delete *it;
43}
44
45/*----------------------------------------------------------------------------*/
46
47void CCPU::run()
48{
49 if (m_memory == NULL)
50 throw runtime_error("CPU has no memory");
51 if (m_program == NULL)
52 throw runtime_error("CPU has no program to execute");
53 if (m_regcnt == 0)
54 throw runtime_error("CPU has no registers");
55
56 bool run = true;
57 while(run)
58 {
59 unsigned pc = static_cast<unsigned>(m_registers[0]);
60
61 /* end of the program reached */
62 if (pc == m_program->size())
63 break;
64
65 /* pc is out of bound */
66 if (pc > m_program->size())
67 throw runtime_error("Programcounter is out of bound");
68
69 /* execute instruction */
70 (*m_program->at(pc))(this);
71 ++m_registers[0];
72 }
73}
74
75/*----------------------------------------------------------------------------*/
76
77#if DEBUG
78void CCPU::dumpRegisters(std::ostream& out)
79{
80 out << "[REGISTER DUMP]" << endl;
81 for(unsigned i = 0; i < getRegisterCount(); ++i)
82 {
83 out << "[" << std::setw(4) << std::setfill('0') << i << "] "
84 << m_registers[i] << endl;
85 }
86}
87#endif
88
89/* vim: set et sw=2 ts=2: */