-
Notifications
You must be signed in to change notification settings - Fork 1
/
chip8ui.cpp
110 lines (96 loc) · 2.6 KB
/
chip8ui.cpp
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
#include "chip8ui.h"
#include "ui_chip8ui.h"
#include "keymap.h"
#include <QMessageBox>
#include <QDialog>
#include <QString>
Chip8UI::Chip8UI(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Chip8UI)
{
ui->setupUi(this);
cpu = makeCPU();
interface = makeInterface();
ui->display->setInterface(interface);
}
Chip8UI::~Chip8UI()
{
delete ui;
}
void Chip8UI::readChip8ROM(const char* fileName)
{
readROM(cpu, fileName);
updateTimer = startTimer(UPDATE_MILLISEC);
chip8Timer = 0;
}
void Chip8UI::keyPressEvent(QKeyEvent *event)
{
const uint8_t keyIndex = mapToIndex(event->key());
if ( keyIndex == KEYMAP_SIZE ) {
event->ignore();
return;
}
setKey(interface, keyIndex);
}
void Chip8UI::keyReleaseEvent(QKeyEvent *event)
{
const uint8_t keyIndex = mapToIndex(event->key());
if ( keyIndex == KEYMAP_SIZE ) {
event->ignore();
return;
}
resetKey(interface, keyIndex);
}
void Chip8UI::timerEvent(QTimerEvent *event)
{
if ( event->timerId() == updateTimer ) {
chip8Timer++;
if (chip8Timer % 5 == 0) {
tick(cpu, interface);
chip8Timer = 0;
}
CChip8Errors error = step(cpu, interface);
switch(error) {
case ROMFileNotFound:
showErrorAndExit(tr("ROM file missing"));
break;
case ROMFileTooLarge:
showErrorAndExit(tr("ROM file too large"));
break;
case StackUnderflow:
showErrorAndExit(tr("Stack underflow"));
break;
case StackOverFlow:
showErrorAndExit(tr("Stack overflow"));
break;
case MemoryOutOfBounds:
showErrorAndExit(tr("Memory out of bounds"));
break;
case InvalidDigit:
showErrorAndExit(tr("Invalid digit"));
break;
case UnknownInstruction:
showErrorAndExit(tr("Unknown instruction"));
break;
case UpdateScreen:
update();
break;
case OK:
break;
}
} else {
event->ignore();
}
}
void Chip8UI::showErrorAndExit(QString message)
{
QString t(message + "\nDT: %1, I: %2, PC: %3, SP: %4, ST: %5");
QMessageBox::critical(this, "Error", t.arg(
QString::number(cpu->DT),
QString::number(cpu->I),
QString::number(cpu->PC),
QString::number(cpu->SP),
QString::number(cpu->ST)));
killTimer(this->updateTimer);
this->close();
}