Skip to content

Commit 3f389c6

Browse files
committed
Upgrade to Qt6, error handling
1 parent 5a560d2 commit 3f389c6

File tree

13 files changed

+167
-96
lines changed

13 files changed

+167
-96
lines changed

CMakeLists.txt

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ set(CMAKE_AUTORCC ON)
1313
set(CMAKE_CXX_STANDARD 17)
1414
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1515

16-
find_package(Qt5 COMPONENTS Widgets REQUIRED)
16+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
17+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
1718

1819
add_subdirectory(cchip8)
1920

20-
add_executable(QChip8
21+
set(PROJECT_SOURCES
2122
main.cpp
2223

2324
onboardwindow.h
@@ -35,20 +36,54 @@ add_executable(QChip8
3536
keymap.cpp
3637
)
3738

38-
target_include_directories(QChip8 PUBLIC ${CCHIP8_INCLUDE})
39+
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
40+
qt_add_executable(QChip8
41+
MANUAL_FINALIZATION
42+
${PROJECT_SOURCES}
43+
)
44+
# Define target properties for Android with Qt 6 as:
45+
# set_property(TARGET QChip8 APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
46+
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
47+
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
48+
else()
49+
if(ANDROID)
50+
add_library(QChip8 SHARED
51+
${PROJECT_SOURCES}
52+
)
53+
# Define properties for Android with Qt 5 after find_package() calls as:
54+
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
55+
else()
56+
add_executable(QChip8
57+
${PROJECT_SOURCES}
58+
)
59+
endif()
60+
endif()
61+
62+
target_link_libraries(QChip8 PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
3963

64+
target_include_directories(QChip8 PUBLIC ${CCHIP8_INCLUDE})
4065
target_link_libraries(QChip8 PUBLIC CChip8)
41-
target_link_libraries(QChip8 PRIVATE Qt5::Widgets)
66+
set_target_properties(QChip8 PROPERTIES
67+
MACOSX_BUNDLE_GUI_IDENTIFIER top.ycao.qchip8
68+
MACOSX_BUNDLE_BUNDLE_VERSION "${PROJECT_VERSION}"
69+
MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
70+
MACOSX_BUNDLE TRUE
71+
WIN32_EXECUTABLE TRUE
72+
)
73+
74+
install(TARGETS QChip8
75+
BUNDLE DESTINATION .
76+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
4277

78+
if(QT_VERSION_MAJOR EQUAL 6)
79+
qt_finalize_executable(QChip8)
80+
endif()
4381

4482
# Testing
4583
enable_testing()
46-
47-
find_package(Qt5Test REQUIRED)
48-
add_executable(CPUTests
49-
cputests.cpp
50-
)
84+
find_package(Qt6Test REQUIRED)
85+
add_executable(CPUTests cputests.cpp)
5186
target_include_directories(CPUTests PUBLIC ${CCHIP8_INCLUDE})
52-
target_link_libraries(CPUTests CChip8 Qt5::Test)
53-
87+
target_link_libraries(CPUTests CChip8 Qt6::Test)
5488
add_test(CPUTests CPUTests)
89+

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
# Yet Another Chip8 Emulator: QChip8
1+
# YACE: Yet Another Chip8 Emulator
22

3-
***Another Chip8 emulator with a better performance.***
3+
## Components
44

5-
## What's included
65
- `QChip8`: Executable for the emulator using Qt.
76
- `CChip8`: Core chip8 emulator for calculation only. Provided as a dynamic library.
87
- `CPUTests`: Tests for `CChip8` using QtTest.
98

10-
## How to build
9+
## Building & Installing
1110

12-
### Install Qt
13-
14-
Both `Qt 5.12` and `Qt 5.15` are tested. See https://wiki.qt.io/Main 's **Install / Build** part, and make sure CMake can detect it. You may want to modify `CMAKE_PREFIX_PATH`.
15-
16-
17-
### Compile
11+
Works under Qt6.
1812

1913
```bash
2014
$ git clone https://github.com/xiaoyu2006/YACE.git

cchip8/cpu.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ uint8_t fontSet[FONT_SET_SIZE] = {
2323

2424
/*
2525
* Function: makeCPU
26-
* --------------------
26+
* -----------------
2727
* Create a Chip-8 CPU.
2828
*
2929
* returns: pointer of the CPU
@@ -58,7 +58,7 @@ void skipInstruction(CPU* cpu)
5858

5959
/*
6060
* Function: readRom
61-
* --------------------
61+
* -----------------
6262
* Read ROM into the memory.
6363
*
6464
* cpu: current cpu.
@@ -99,7 +99,7 @@ void tick(CPU* cpu, Interface* interface)
9999

100100
/*
101101
* Function: step
102-
* --------------------
102+
* --------------
103103
* Next step
104104
*
105105
* cpu: current cpu
@@ -114,7 +114,7 @@ CChip8Errors step(CPU* cpu, Interface* interface)
114114

115115
/*
116116
* Function: fetch
117-
* --------------------
117+
* ---------------
118118
* Fetch an operation code from memory.
119119
*
120120
* cpu: current cpu
@@ -129,7 +129,7 @@ uint16_t fetch(CPU* cpu)
129129

130130
/*
131131
* Function: decode
132-
* --------------------
132+
* ----------------
133133
* Disassemble the current operation code
134134
*
135135
* opcode: operation code.
@@ -157,7 +157,7 @@ DecodedInst decode(uint16_t opcode)
157157

158158
/*
159159
* Function: execute
160-
* --------------------
160+
* -----------------
161161
* Execute an instruction.
162162
*
163163
* cpu: current cpu
@@ -338,7 +338,7 @@ CChip8Errors execute(CPU* cpu, Interface* interface, DecodedInst inst)
338338

339339
case 0xc000: // RND(VX, NN)
340340
{
341-
cpu->registers[inst.args[0]] = ((rand() % 0xff) & inst.args[1]);
341+
cpu->registers[inst.args[0]] = ((arc4random() % 0xff) & inst.args[1]);
342342
nextInstruction(cpu);
343343
break;
344344
}

cchip8/instructionset.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <stdlib.h>
1212
#include <stdbool.h>
1313

14-
#include <cpu.h>
1514
#include <interface.h>
1615

1716
// Arguments for a instruction

cchip8/interface.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Copyright (c) 2020 Yi
22

3+
#include <stdlib.h>
4+
#include <string.h>
35
#include "interface.h"
46

57
/*
68
* Function: makeInterface
79
* --------------------
8-
* Create a Interface interface.
10+
* Create a new Interface.
911
*
10-
* returns: pointer of the interface
12+
* returns: pointer to the interface
1113
*/
1214
Interface* makeInterface()
1315
{

cchip8/interface.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
#ifndef _INTERFACE_H_
66
#define _INTERFACE_H_
77

8-
#include <stdio.h>
98
#include <stdbool.h>
109
#include <inttypes.h>
11-
#include <stdlib.h>
1210

1311
#define KEYMAP_LEN 16
1412

@@ -24,7 +22,6 @@ typedef struct Interface {
2422
int16_t keyPressed;
2523
} Interface;
2624

27-
#include "cpu.h"
2825

2926
#ifdef __cplusplus
3027
extern "C" {

chip8screen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CHIP8SCREEN_H
22
#define CHIP8SCREEN_H
33

4-
#include <QWidget>
4+
#include <QtWidgets>
55
#include <QPaintDevice>
66
#include <QPainter>
77
#include <QBrush>

chip8ui.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
#include "chip8ui.h"
1+
#include "chip8ui.h"
22
#include "ui_chip8ui.h"
33

4+
#include "keymap.h"
5+
#include <QMessageBox>
6+
#include <QDialog>
7+
#include <QString>
8+
49
Chip8UI::Chip8UI(QWidget *parent) :
510
QMainWindow(parent),
611
ui(new Ui::Chip8UI)
@@ -56,9 +61,50 @@ void Chip8UI::timerEvent(QTimerEvent *event)
5661
chip8Timer = 0;
5762
}
5863
CChip8Errors error = step(cpu, interface);
59-
if (error == UpdateScreen) update();
60-
// TODO: Error handling.
64+
switch(error) {
65+
case ROMFileNotFound:
66+
showErrorAndExit(tr("ROM file missing"));
67+
break;
68+
case ROMFileTooLarge:
69+
showErrorAndExit(tr("ROM file too large"));
70+
break;
71+
case StackUnderflow:
72+
showErrorAndExit(tr("Stack underflow"));
73+
break;
74+
case StackOverFlow:
75+
showErrorAndExit(tr("Stack overflow"));
76+
break;
77+
case MemoryOutOfBounds:
78+
showErrorAndExit(tr("Memory out of bounds"));
79+
break;
80+
case InvalidDigit:
81+
showErrorAndExit(tr("Invalid digit"));
82+
break;
83+
case UnknownInstruction:
84+
showErrorAndExit(tr("Unknown instruction"));
85+
break;
86+
87+
case UpdateScreen:
88+
update();
89+
break;
90+
91+
case OK:
92+
break;
93+
}
6194
} else {
6295
event->ignore();
6396
}
6497
}
98+
99+
void Chip8UI::showErrorAndExit(QString message)
100+
{
101+
QString t(message + "\nDT: %1, I: %2, PC: %3, SP: %4, ST: %5");
102+
QMessageBox::critical(this, "Error", t.arg(
103+
QString::number(cpu->DT),
104+
QString::number(cpu->I),
105+
QString::number(cpu->PC),
106+
QString::number(cpu->SP),
107+
QString::number(cpu->ST)));
108+
killTimer(this->updateTimer);
109+
this->close();
110+
}

chip8ui.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include <QTimer>
88
#include <QTimerEvent>
99

10-
#include "keymap.h"
11-
1210
#include <cpu.h>
1311
#include <interface.h>
1412

@@ -35,12 +33,12 @@ class Chip8UI : public QMainWindow
3533

3634
private:
3735
Ui::Chip8UI *ui;
38-
3936
CPU* cpu;
4037
Interface* interface;
4138
int chip8Timer;
42-
4339
uint8_t updateTimer;
40+
41+
void showErrorAndExit(QString message);
4442
};
4543

4644
#endif // CHIP8UI_H

0 commit comments

Comments
 (0)