This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
139 lines (117 loc) · 3.63 KB
/
main.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
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
#include <Bus.hpp>
#include <getopt.h>
#include <unistd.h>
#include <EE.hpp>
#include <gs/gsrenderer.hpp>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <gs/gs.hpp>
#include <dmac.hpp>
#include <iop/iop.hpp>
#include <iop/iop_intc.hpp>
void error_callback( int error, const char *msg ) {
std::string s;
s = " [" + std::to_string(error) + "] " + msg + '\n';
std::cerr << s << std::endl;
}
IOP* iop;
int main(int argc, char** argv)
{
if(!glfwInit())
{
printf("[Main]: Error initing GLFW\n");
return -1;
}
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "PS2 Emulator", NULL, NULL);
if (window == NULL)
{
printf("[MAIN] Error creating main window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
});
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return -1;
if (argc < 2)
{
printf("Usage: %s [BIOS] {ELF/CDROM}\n", argv[0]);
return 1;
}
gs::GraphicsSynthesizer GS;
Bus* bus = new Bus(argv[1], &GS);
EmotionEngine* cpu = new EmotionEngine(bus);
GIF* gif = new GIF(&GS);
DMAController* dmac = new DMAController(bus, cpu);
bus->vu[0] = new VectorUnit(cpu);
bus->vu[1] = new VectorUnit(cpu);
INTC* intc = cpu->getIntc();
Timers* timers = cpu->getTimers();
SIO2* sio2 = new SIO2(bus);
iop = new IOP(bus);
IOP_INTC* iop_intc = new IOP_INTC(iop);
bus->vif[0] = new VIF(bus, 0);
bus->vif[1] = new VIF(bus, 1);
bus->attachIntc(intc);
bus->attachTimers(timers);
bus->attachGIF(gif);
bus->dmac = dmac;
bus->sio2 = sio2;
iop->reset();
iop->set_disassembly(true);
iop_intc->reset();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearDepth(0.0);
glClear(GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
while (!glfwWindowShouldClose(window))
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClearDepth(0.0);
glClear(GL_DEPTH_BUFFER_BIT);
uint32_t total_cycles = 0;
bool vblank_started = false;
while (total_cycles < 4919808)
{
uint32_t cycles = 32;
cpu->Clock(cycles);
cycles /= 2;
dmac->tick(cycles);
bus->vif[0]->tick(cycles);
bus->vif[1]->tick(cycles);
gif->tick(cycles);
cycles /= 4;
iop->run(cycles);
total_cycles += 32;
if (!vblank_started && total_cycles >= 4498432)
{
vblank_started = true;
GS.priv_regs.csr.vsint = true;
GS.renderer.render();
GS.priv_regs.csr.field = !GS.priv_regs.csr.field;
if (!(GS.priv_regs.imr & 0x800))
intc->trigger(Interrupt::INT_GS);
intc->trigger(Interrupt::INT_VB_ON);
iop_intc->assert_irq(0);
}
}
intc->trigger(Interrupt::INT_VB_OFF);
iop_intc->assert_irq(11);
vblank_started = false;
GS.priv_regs.csr.vsint = false;
glfwSwapBuffers(window);
glfwPollEvents();
}
return 0;
}