// -*- c++ -*-
//
// cpu.cc:
//
#include <iostream>
#include "cpu.h"

using namespace std;

//
// constructor
//
cpu::cpu()
{
    machdep = new mips; 

    cycle_time = 0;
}

//
// destructor
//
cpu::~cpu()
{
    delete machdep;
}

//
// cpu class member functions
//
void
cpu::connect_memory(memory *mem)
{
    this->mem = mem;
    machdep->connect_memory(mem);	// don't forget
}

void
cpu::execute(sim_addr init_pc, sim_addr init_sp)
{
    int phase = 0;

    machdep->reset();

    machdep->set_pc(init_pc);
    machdep->set_reg(29, init_sp);	// set initial value for SP

    cout << "program start" << endl;
    cout << "PC=" << hex << machdep->get_pc() << endl;
    cout << "SP=" << hex << machdep->get_reg(29) << endl;

    while (machdep->is_running()) {	// instruction cycle
	cout << "********** cycle=" << dec << cycle_time
	     << " **********" << endl;

	switch (phase) {
	case 0:
	    machdep->IF(); phase++; break;
	case 1:
	    machdep->ID(); phase++; break;
	case 2:
	    machdep->EX(); phase++; break;
	case 3:
	    machdep->MA(); phase++; break;
	case 4:
	    machdep->WB(); phase = 0; break;
	}

	cycle_time++;		// increment cycle counter
    }

    cout << "program finished" << endl;
}

// end of cpu.cc