aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/coreview/memory.cpp
blob: d7211b76bc1aceb2b9cb2ac46ca73135b557e743 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "memory.h"
#include <cmath>

using namespace coreview;

//////////////////////
#define WIDTH 60
#define HEIGHT 80
#define CACHE_HEIGHT 47
#define PENW 1
//////////////////////

Memory::Memory(bool cache_used, const machine::Cache *cch) : QGraphicsObject(nullptr), name("Memory", this), type(this), cache_t("Cache", this), cache_hit_t("Hit: 0", this), cache_miss_t("Miss: 0", this) {
    cache = cache_used;

    QFont font;
    font.setPointSize(7);
    name.setFont(font);
    type.setFont(font);
    cache_t.setFont(font);
    cache_hit_t.setFont(font);
    cache_miss_t.setFont(font);

    name.setPos(WIDTH/2 - name.boundingRect().width()/2, HEIGHT - (HEIGHT - CACHE_HEIGHT)/2);
    if (cache) {
        const QRectF &cache_t_b = cache_t.boundingRect();
        cache_t.setPos(WIDTH/2 - cache_t_b.width()/2, 1);
        const QRectF &cache_hit_b = cache_hit_t.boundingRect();
        cache_hit_t.setPos(WIDTH/2 - cache_hit_b.width()/2, cache_t_b.height() + 2);
        cache_miss_t.setPos(WIDTH/2 - cache_miss_t.boundingRect().width()/2, cache_t_b.height() + cache_hit_b.height() + 3);
    }

    cache_t.setVisible(cache);
    cache_hit_t.setVisible(cache);
    cache_miss_t.setVisible(cache);

    connect(cch, SIGNAL(hit_update(uint)), this, SLOT(cache_hit_update(uint)));
    connect(cch, SIGNAL(miss_update(uint)), this, SLOT(cache_miss_update(uint)));

    setPos(x(), y()); // set connector's position
}

QRectF Memory::boundingRect() const {
    return QRectF(-PENW / 2, -PENW / 2, WIDTH + PENW, HEIGHT + PENW);
}

void Memory::paint(QPainter *painter, const QStyleOptionGraphicsItem *option __attribute__((unused)), QWidget *widget __attribute__((unused))) {
    painter->drawRect(0, 0, WIDTH, HEIGHT);
    if (cache)
        painter->drawLine(0, CACHE_HEIGHT, WIDTH, CACHE_HEIGHT);
}

void Memory::cache_hit_update(unsigned val) {
    cache_hit_t.setText("Hit: " + QString::number(val));
}

void Memory::cache_miss_update(unsigned val) {
    cache_miss_t.setText("Miss: " + QString::number(val));
}

void Memory::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
    QGraphicsObject::mouseDoubleClickEvent(event);

    if (cache && event->pos().y() < HEIGHT/2)
        emit open_cache();
    else
        emit open_mem();
}

void Memory::set_type(const QString &text) {
    type.setText(text);
    const QRectF &box = type.boundingRect();
    type.setPos(WIDTH/2 - box.width()/2, HEIGHT - (HEIGHT - CACHE_HEIGHT)/2 - box.height());
}

ProgramMemory::ProgramMemory(machine::QtMipsMachine *machine) : Memory(machine->config().cache_program().enabled(), machine->cache_program()) {
    set_type("Program");

    con_address = new Connector(Connector::AX_X);
    con_inst = new Connector(Connector::AX_X);
}

ProgramMemory::~ProgramMemory() {
    delete con_address;
    delete con_inst;
}

void ProgramMemory::setPos(qreal x, qreal y) {
    QGraphicsObject::setPos(x, y);

    con_address->setPos(x, y + 20);
    con_inst->setPos(x + WIDTH, y + 20);
}

const Connector *ProgramMemory::connector_address() const {
    return con_address;
}

const Connector *ProgramMemory::connector_instruction() const {
    return con_inst;
}

DataMemory::DataMemory(machine::QtMipsMachine *machine) : Memory(machine->config().cache_data().enabled(), machine->cache_data()) {
    set_type("Data");

    con_address = new Connector(Connector::AX_X);
    con_data_out = new Connector(Connector::AX_X);
    con_data_in	= new Connector(Connector::AX_X);
    con_req_write = new Connector(Connector::AX_Y);
    con_req_read = new Connector(Connector::AX_Y);
}

DataMemory::~DataMemory() {
    delete con_address;
    delete con_data_out;
    delete con_data_in;
    delete con_req_write;
    delete con_req_read;
}

void DataMemory::setPos(qreal x, qreal y) {
    QGraphicsObject::setPos(x, y);

    con_address->setPos(x, y + 20);
    con_data_out->setPos(x + WIDTH, y + 20);
    if (cache)
        con_data_in->setPos(x, y + 40);
    else
        con_data_in->setPos(x, y + 60);
    con_req_write->setPos(x + 40, y);
    con_req_read->setPos(x + 50, y);
}

const Connector *DataMemory::connector_address() const {
    return con_address;
}

const Connector *DataMemory::connector_data_out() const {
    return con_data_out;
}

const Connector *DataMemory::connector_data_in() const {
    return con_data_in;
}

const Connector *DataMemory::connector_req_write() const {
    return con_req_write;
}

const Connector *DataMemory::connector_req_read() const {
    return con_req_read;
}