-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.c
216 lines (177 loc) · 6.26 KB
/
Controller.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* ============================================================================
* Controller.c: Video controller.
*
* VIDEOSIM: VIDEO Interface SIMulator.
* Copyright (C) 2013, Tyler J. Stachecki.
* All rights reserved.
*
* This file is subject to the terms and conditions defined in
* file 'LICENSE', which is part of this source code package.
* ========================================================================= */
#include "Address.h"
#include "Common.h"
#include "Controller.h"
#include "Definitions.h"
#include "Externs.h"
#include "Render.h"
#ifdef __cplusplus
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#else
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include <GL/glfw.h>
static void InitVIF(struct VIFController *);
/* ============================================================================
* Mnemonics table.
* ========================================================================= */
#ifndef NDEBUG
const char *VIRegisterMnemonics[NUM_VI_REGISTERS] = {
#define X(reg) #reg,
#include "Registers.md"
#undef X
};
#endif
/* ============================================================================
* ConnectVIFToBus: Connects a VIF instance to a Bus instance.
* ========================================================================= */
void
ConnectVIFToBus(struct VIFController *controller, struct BusController *bus) {
controller->bus = bus;
}
/* ============================================================================
* CreateVIF: Creates and initializes an VIF instance.
* ========================================================================= */
struct VIFController *
CreateVIF(void) {
struct VIFController *controller;
if (glfwInit() != GL_TRUE) {
debug("Failed to initialize GLFW.");
return NULL;
}
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_TRUE);
if (glfwOpenWindow(640, 480, 5, 6, 5, 0, 8, 0, GLFW_WINDOW) != GL_TRUE) {
debug("Failed to open a GLFW window.");
glfwTerminate();
return NULL;
}
glfwSetWindowTitle("CEN64");
glfwPollEvents();
/* Load the viewport. */
glViewport(0, 0, 640, 480);
/* Disable everything. */
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glDisable(GL_DITHER);
/* Allocate memory for controller. */
if ((controller = (struct VIFController*) malloc(
sizeof(struct VIFController))) == NULL) {
return NULL;
}
/* Generate frame texture. */
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &controller->frameTexture);
glBindTexture(GL_TEXTURE_2D, controller->frameTexture);
/* Init texture */
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
/* Tell OpenGL that the byte order is swapped. */
#ifdef LITTLE_ENDIAN
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_TRUE);
#endif
InitVIF(controller);
return controller;
}
/* ============================================================================
* DestroyVIF: Releases any resources allocated for an VIF instance.
* ========================================================================= */
void
DestroyVIF(struct VIFController *controller) {
glDeleteTextures(1, &controller->frameTexture);
glfwCloseWindow();
glfwTerminate();
free(controller);
}
/* ============================================================================
* InitVIF: Initializes the VIF controller.
* ========================================================================= */
static void
InitVIF(struct VIFController *controller) {
debug("Initializing VIF.");
memset(controller, 0, sizeof(*controller));
controller->startTime = glfwGetTime();
}
/* ============================================================================
* CycleVIF: Lets the VI know we are cycling the machine.
* ========================================================================= */
void
CycleVIF(struct VIFController *controller) {
if (unlikely(controller->cyclesUntilIntr == 0)) {
char buffer[4096];
double vis, hz;
if (++(controller->frameCount) == 10) {
vis = (double) 10 / (glfwGetTime() - controller->startTime);
hz = ((double) 6250000 / 60) / (glfwGetTime() - controller->startTime);
sprintf(buffer, "CEN64 [%.2f VI/s] [RCP: %.2f MHz]", vis, hz / 10000);
glfwSetWindowTitle(buffer);
controller->startTime = glfwGetTime();
controller->frameCount = 0;
}
RenderFrame(controller);
/* Raise an interrupt. */
BusRaiseRCPInterrupt(controller->bus, MI_INTR_VI);
controller->cyclesUntilIntr = (62500000 / 60) + 1;
}
controller->cyclesUntilIntr--;
}
/* ============================================================================
* VIRegRead: Read from VI registers.
* ========================================================================= */
int
VIRegRead(void *_controller, uint32_t address, void *_data) {
struct VIFController *controller = (struct VIFController*) _controller;
uint32_t *data = (uint32_t*) _data;
address -= VI_REGS_BASE_ADDRESS;
enum VIRegister reg = (enum VIRegister) (address / 4);
debugarg("VIRegRead: Reading from register [%s].", VIRegisterMnemonics[reg]);
*data = controller->regs[reg];
return 0;
}
/* ============================================================================
* VIRegWrite: Write to VI registers.
* ========================================================================= */
int
VIRegWrite(void *_controller, uint32_t address, void *_data) {
struct VIFController *controller = (struct VIFController*) _controller;
uint32_t *data = (uint32_t*) _data;
address -= VI_REGS_BASE_ADDRESS;
enum VIRegister reg = (enum VIRegister) (address / 4);
debugarg("VIRegWrite: Writing to register [%s].", VIRegisterMnemonics[reg]);
switch(reg) {
case VI_H_START_REG:
case VI_V_START_REG:
case VI_X_SCALE_REG:
case VI_Y_SCALE_REG:
case VI_WIDTH_REG:
controller->regs[reg] = *data;
ResetPerspective(controller);
break;
case VI_CURRENT_REG:
BusClearRCPInterrupt(controller->bus, MI_INTR_VI);
break;
default:
controller->regs[reg] = *data;
break;
}
return 0;
}