aboutsummaryrefslogtreecommitdiff
path: root/qtmips_gui/coreview/memory.cpp
blob: f4ba058753c67e9ec7276ec9b60fb0b3c6ffd0ba (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
#include "memory.h"
#include <cmath>

using namespace coreview;

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

Memory::Memory(machine::QtMipsMachine *machine) : QGraphicsObject(nullptr), name("Memory", this), type(this) {
    cache = false;

    QFont font;
    font.setPointSize(7);
    name.setFont(font);
    type.setFont(font);

    const QRectF &name_box = name.boundingRect();
    name.setPos(WIDTH/2 - name_box.width()/2, HEIGHT - (HEIGHT - CACHE_HEIGHT)/2);

    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::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) {
    cache = machine->config().cache_program().enabled();
    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) {
    cache = machine->config().cache_data().enabled();
    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;
}