aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/qtmipsmachine.cpp
blob: 191a7c793f0ad4196e43a3508b39f67af7225313 (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
#include "qtmipsmachine.h"
#include "programloader.h"

QtMipsMachine::QtMipsMachine(const MachineConfig &cc) {
    ProgramLoader program(cc.elf());

    regs = new Registers();
    mem = new Memory();

    program.to_memory(mem);
    program_end = program.end();
    program_ended = false;

    MemoryAccess *coremem;
    switch (cc.cache()) {
    case MachineConfig::CCT_NONE:
        cch = nullptr;
        coremem = mem;
        break;
    case MachineConfig::CCT_ASSOCIATIVE:
        // TODO
        coremem = mem;
        //coremem = cch = new CacheAssociative();
        break;
    default:
        throw QTMIPS_EXCEPTION(Sanity, "Trying to configure unknown cache type", "");
    }

    // TODO pipelined
    cr = new CoreSingle(regs, coremem);

    run_speed = 1;
    run_t = new QTimer(this);
    connect(run_t, SIGNAL(timeout()), this, SLOT(step()));
}

void QtMipsMachine::set_speed(unsigned val) {
    run_speed = val;
    if (run_t->isActive())
        play();
}

const Registers *QtMipsMachine::registers() {
    return regs;
}

const Memory *QtMipsMachine::memory() {
    return mem;
}

const Cache *QtMipsMachine::cache() {
    return cch;
}

const Core *QtMipsMachine::core() {
    return cr;
}

void QtMipsMachine::play() {
    if (program_ended)
        return;
    run_t->start(run_speed);
}

void QtMipsMachine::pause() {
    if (program_ended)
        return;
    run_t->stop();
}

void QtMipsMachine::step() {
    if (program_ended) // Ignore if program ended
        return;
    emit tick();
    cr->step();
    if (regs->read_pc() >= program_end) {
        program_ended = true;
        run_t->stop();
        emit program_exit();
    }
}

void QtMipsMachine::restart() {
    if (!program_ended)
        run_t->stop(); // Stop timer if program is still running
    // TODO
}