-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathAnemoia-ESP32.ino
More file actions
280 lines (244 loc) · 6.95 KB
/
Anemoia-ESP32.ino
File metadata and controls
280 lines (244 loc) · 6.95 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <string>
#include <TFT_eSPI.h>
#include <WiFi.h>
#include <vector>
#include "src/core/bus.h"
#include "src/controller.h"
#include "src/debug.h"
#include "src/ui.h"
#include "config.h"
#include "hwconfig.h"
#include "driver/i2s.h"
#include "esp_wifi.h"
#include "esp_bt.h"
#include "esp_bt_main.h"
#ifndef OPTIMIZATION_FLAGS
#error The optimization flags were not applied! Please refer to *Step 4* of the README how to build and upload section.
#endif
HWConfig hw_config;
TFT_eSPI screen = TFT_eSPI();
SPIClass SD_SPI(HSPI);
UI ui(&screen);
Cartridge* cart;
void setup()
{
// Turn off Wifi and Bluetooth to reduce CPU overhead
#ifdef DEBUG
Serial.begin(115200);
#endif
WiFi.mode(WIFI_OFF);
esp_wifi_stop();
esp_wifi_deinit();
btStop();
esp_bt_controller_disable();
esp_bt_mem_release(ESP_BT_MODE_BTDM);
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
hw_config = loadConfig();
if (hw_config.backlight)
{
pinMode(TFT_BACKLIGHT_PIN, OUTPUT);
ledcAttach(TFT_BACKLIGHT_PIN, BL_FREQ, BL_RESOLUTION);
ledcWrite(TFT_BACKLIGHT_PIN, 255);
}
setupI2SDAC();
// Initialize TFT screen
screen.begin();
screen.setRotation(hw_config.rotation);
#ifndef TFT_PARALLEL
screen.initDMA();
#endif
screen.fillScreen(BG_COLOR);
screen.startWrite();
// Initialize microsd card
if(!initSD()) while (true);
ui.initializeSettings();
// Setup buttons
initController();
}
void loop()
{
cart = ui.selectGame();
emulate();
}
#ifdef DEBUG
unsigned long last_frame_time = 0;
unsigned long current_frame_time = 0;
unsigned long total_frame_time = 0;
unsigned long frame_count = 0;
#endif
IRAM_ATTR void emulate()
{
Bus nes;
nes.insertCartridge(cart);
nes.connectScreen(&screen);
nes.reset();
ui.loadEmulatorSettings(&nes);
TaskHandle_t apu_task_handle;
xTaskCreatePinnedToCore(
apuTask,
"APU Task",
1024,
&nes.cpu.apu,
1,
&apu_task_handle,
0
);
TaskHandle_t polling_task_handle;
xTaskCreatePinnedToCore(
pollingTask,
"Polling Task",
768,
&nes,
1,
&polling_task_handle,
0
);
screen.setAddrWindow(32, 0, 256, 240);
#ifdef DEBUG
last_frame_time = esp_timer_get_time();
#endif
// Target frame time: 16639µs (60.098 FPS)
#define FRAME_TIME 16639
uint64_t next_frame = esp_timer_get_time();
// Emulation Loop
while (true)
{
// Start + Select opens the pause menu
if ((nes.controller & CONTROLLER::Start) && (nes.controller & CONTROLLER::Select))
{
if (!ui.paused)
{
vTaskSuspend(apu_task_handle);
ui.pauseMenu(&nes);
vTaskResume(apu_task_handle);
next_frame = esp_timer_get_time() + FRAME_TIME;
nes.controller = 0;
screen.setAddrWindow(32, 0, 256, 240);
}
}
// Generate one frame
nes.clock();
#ifdef DEBUG
current_frame_time = esp_timer_get_time();
total_frame_time += (current_frame_time - last_frame_time);
frame_count++;
if ((frame_count & 63) == 0)
{
float avg_fps = (1000000.0 * frame_count) / total_frame_time;
LOGF("FPS: %.2f\n", avg_fps);
total_frame_time = 0;
frame_count = 0;
}
last_frame_time = current_frame_time;
#endif
// Frame limiting
uint64_t now = esp_timer_get_time();
// if (now < next_frame) ets_delay_us(next_frame - now);
next_frame += FRAME_TIME;
}
#undef FRAME_TIME
}
bool initSD()
{
LOG("Initializing SD...");
SD_SPI.begin(SD_SCLK_PIN, SD_MISO_PIN, SD_MOSI_PIN, SD_CS_PIN);
if (!SD.begin(SD_CS_PIN, SD_SPI, hw_config.sd_freq * 1000000))
{
LOG("SD Card Mount Failed");
screen.setTextSize(2);
const char* txt1 = "SD Init failed!";
const char* txt2 = "Insert SD card or";
const char* txt3 = "lower SD frequency";
const char* txt4 = "in config.h";
int w1 = screen.textWidth(txt1, 2);
int w2 = screen.textWidth(txt2, 2);
int w3 = screen.textWidth(txt3, 2);
int w4 = screen.textWidth(txt4, 2);
int x1 = (320 - w1) / 2;
int x2 = (320 - w2) / 2;
int x3 = (320 - w3) / 2;
int x4 = (320 - w4) / 2;
screen.setTextColor(TFT_BLACK);
screen.drawString(txt1, x1, 56, 2);
screen.drawString(txt2, x2, 88, 2);
screen.drawString(txt3, x3, 120, 2);
screen.drawString(txt4, x4, 152, 2);
return false;
}
return true;
}
void setupI2SDAC()
{
#if defined(CONFIG_IDF_TARGET_ESP32)
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0,
.dma_buf_count = 2,
.dma_buf_len = 128,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
#if DAC_PIN == 0
i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
#elif DAC_PIN == 1
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
i2s_set_dac_mode(I2S_DAC_CHANNEL_LEFT_EN);
#else
#error "Invalid DAC PIN is used."
#endif
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = 0,
.dma_buf_count = 2,
.dma_buf_len = 128,
.use_apll = false,
.tx_desc_auto_clear = true
};
esp_err_t err = i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
if (err != ESP_OK) {
LOGF("I2S install failed: %d\n", err);
return;
}
i2s_pin_config_t pin_config = {
.bck_io_num = I2S_BCLK_PIN,
.ws_io_num = I2S_LRC_PIN,
.data_out_num = I2S_DOUT_PIN,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_set_pin(I2S_NUM_0, &pin_config);
#endif
}
void apuTask(void* param)
{
Apu2A03* apu = (Apu2A03*)param;
while (true)
{
apu->clock();
}
}
void pollingTask(void* param)
{
Bus* nes = (Bus*)param;
const TickType_t frameTicks = pdMS_TO_TICKS(1000 / 60);
TickType_t lastWakeTime = xTaskGetTickCount();
while (true)
{
// Read button input
nes->controller = controllerRead();
vTaskDelayUntil(&lastWakeTime, frameTicks);
}
}