summaryrefslogtreecommitdiffstats
path: root/ue3/mycpu/ccpu.cpp
blob: af862007d39bcec1c435b6ea6bf6a7f84d30a49e (plain)
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
/**
 * @module ccpu
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  CPU implementation. Used as a container for memory and instructions.
 *         Implements an run method to execute the program (= the instructions).
 * @date   10.05.2009
 */

#ifdef DEBUG
# include <iostream>
# include <iomanip>
#endif
#include "ccpu.h"
#include "displays.h"

using namespace std;

CCPU::CCPU(const unsigned cnt)
  : m_regcnt(cnt), m_memory(NULL), m_program(NULL), m_flagzero(false), m_flagsign(false)
{
  /* create registers */
  m_registers = new CDat[cnt];
  for(unsigned i = 0; i < cnt; ++i)
    m_registers[i] = 0;

  /* create displays */
  m_displays.insert(new CDisplayWDEZ);
  m_displays.insert(new CDisplayWHEX);
}

/*----------------------------------------------------------------------------*/

CCPU::~CCPU()
{
  /* delete registers */
  delete[] m_registers;
  m_registers = NULL;

  /* delete displays */
  std::set<CDisplay *>::iterator it;
  for (it = m_displays.begin() ; it != m_displays.end(); ++it)
    delete *it;
}

/*----------------------------------------------------------------------------*/

void CCPU::run()
{
  if (m_memory == NULL)
    throw runtime_error("CPU has no memory");
  if (m_program == NULL)
    throw runtime_error("CPU has no program to execute");
  if (m_regcnt == 0)
    throw runtime_error("CPU has no registers");

  bool run = true;
  while(run)
  {
    unsigned pc = static_cast<unsigned>(m_registers[0]);

    /* end of the program reached */
    if (pc == m_program->size())
      break;

    /* pc is out of bound */
    if (pc > m_program->size())
      throw runtime_error("Programcounter is out of bound");

    /* execute instruction */
    (*m_program->at(pc))(this);
    ++m_registers[0];
  }
}

/*----------------------------------------------------------------------------*/

#if DEBUG
void CCPU::dumpRegisters(std::ostream& out)
{
  out << "[REGISTER DUMP]" << endl;
  for(unsigned i = 0; i < getRegisterCount(); ++i)
  {
    out << "[" << std::setw(4) << std::setfill('0') << i << "]  "
        << m_registers[i] << endl;
  }
}
#endif

/* vim: set et sw=2 ts=2: */