-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd.c
289 lines (244 loc) · 6.47 KB
/
osd.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
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
281
282
283
284
285
286
287
288
289
/* start rewrite from: https://github.com/espressif/esp32-nesemu.git */
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_heap_caps.h>
#include <esp_task_wdt.h>
#include "src/util/nofrendo/noftypes.h"
#include "src/util/nofrendo/event.h"
#include "src/util/nofrendo/gui.h"
#include "src/util/nofrendo/log.h"
#include "src/util/nofrendo/nes/nes.h"
#include "src/util/nofrendo/nes/nes_pal.h"
#include "src/util/nofrendo/nes/nesinput.h"
#include "src/util/nofrendo/nofconfig.h"
#include "src/util/nofrendo/osd.h"
#include "hw_config.h"
TimerHandle_t timer;
/* memory allocation */
extern void *mem_alloc(int size, bool prefer_fast_memory)
{
nofrendo_log_printf("Try allocate %d B memory. FAST: %d\n", size, prefer_fast_memory);
nofrendo_log_printf("Free default memory: %d\n", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
nofrendo_log_printf("Free SPIRAM memory: %d\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
nofrendo_log_printf("Free 8BIT memory: %d\n", heap_caps_get_free_size(MALLOC_CAP_8BIT));
nofrendo_log_printf("Larged free 8BIT memory block size: %d\n", heap_caps_get_largest_free_block(MALLOC_CAP_8BIT));
if (prefer_fast_memory)
{
return heap_caps_malloc(size, MALLOC_CAP_8BIT);
}
else
{
return heap_caps_malloc_prefer(size, MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT);
}
}
/* sound */
extern int osd_init_sound();
extern void osd_stopsound();
extern void do_audio_frame();
extern void osd_getsoundinfo(sndinfo_t *info);
/* display */
extern void display_init();
extern void display_write_frame(const uint8_t *data[]);
extern void display_clear();
//This runs on core 0.
QueueHandle_t vidQueue;
static void displayTask(void *arg)
{
nofrendo_log_printf("Drawing task started!\n");
esp_task_wdt_config_t config = {
.timeout_ms = 60000,
.trigger_panic = true,
.idle_core_mask = 0, // i.e. do not watch any idle task
};
esp_err_t err = esp_task_wdt_reconfigure(&config);
bitmap_t *bmp = NULL;
while (1)
{
// xQueueReceive(vidQueue, &bmp, portMAX_DELAY); //skip one frame to drop to 30
xQueueReceive(vidQueue, &bmp, portMAX_DELAY);
display_write_frame((const uint8_t **)bmp->line);
// TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE;
// TIMERG0.wdt_feed = 1;
// TIMERG0.wdt_wprotect = 0;
}
}
/* get info */
static char fb[1]; //dummy
bitmap_t *myBitmap;
/* initialise video */
static int init(int width, int height)
{
return 0;
}
static void shutdown(void)
{
}
/* set a video mode */
static int set_mode(int width, int height)
{
return 0;
}
/* copy nes palette over to hardware */
uint16 myPalette[256];
static void set_palette(rgb_t *pal)
{
uint16 c;
int i;
for (i = 0; i < 256; i++)
{
c = (pal[i].b >> 3) + ((pal[i].g >> 2) << 5) + ((pal[i].r >> 3) << 11);
//myPalette[i]=(c>>8)|((c&0xff)<<8);
myPalette[i] = c;
}
}
/* clear all frames to a particular color */
static void clear(uint8 color)
{
// SDL_FillRect(mySurface, 0, color);
display_clear();
}
/* acquire the directbuffer for writing */
static bitmap_t *lock_write(void)
{
// SDL_LockSurface(mySurface);
myBitmap = bmp_createhw((uint8 *)fb, NES_SCREEN_WIDTH, NES_SCREEN_HEIGHT, NES_SCREEN_WIDTH * 2);
return myBitmap;
}
/* release the resource */
static void free_write(int num_dirties, rect_t *dirty_rects)
{
bmp_destroy(&myBitmap);
}
static void custom_blit(bitmap_t *bmp, int num_dirties, rect_t *dirty_rects)
{
xQueueSend(vidQueue, &bmp, 0);
do_audio_frame();
}
viddriver_t sdlDriver =
{
"Simple DirectMedia Layer", /* name */
init, /* init */
shutdown, /* shutdown */
set_mode, /* set_mode */
set_palette, /* set_palette */
clear, /* clear */
lock_write, /* lock_write */
free_write, /* free_write */
custom_blit, /* custom_blit */
false /* invalidate flag */
};
void osd_getvideoinfo(vidinfo_t *info)
{
info->default_width = NES_SCREEN_WIDTH;
info->default_height = NES_SCREEN_HEIGHT;
info->driver = &sdlDriver;
}
/* input */
extern void controller_init();
extern uint32_t controller_read_input();
static void osd_initinput()
{
controller_init();
}
static void osd_freeinput(void)
{
}
void osd_getinput(void)
{
const int ev[32] = {
event_joypad1_up, event_joypad1_down, event_joypad1_left, event_joypad1_right,
event_joypad1_select, event_joypad1_start, event_joypad1_a, event_joypad1_b,
event_togglepause, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0};
static int oldb = 0xffff;
uint32_t b = controller_read_input();
uint32_t chg = b ^ oldb;
int x;
oldb = b;
event_t evh;
// nofrendo_log_printf("Input: %x\n", b);
for (x = 0; x < 16; x++)
{
if (chg & 1)
{
evh = event_get(ev[x]);
if (evh)
evh((b & 1) ? INP_STATE_BREAK : INP_STATE_MAKE);
}
chg >>= 1;
b >>= 1;
}
}
void osd_getmouse(int *x, int *y, int *button)
{
}
/* init / shutdown */
static int logprint(const char *string)
{
return printf("%s", string);
}
int osd_init()
{
nofrendo_log_chain_logfunc(logprint);
if (osd_init_sound())
return -1;
display_init();
vidQueue = xQueueCreate(1, sizeof(bitmap_t *));
// xTaskCreatePinnedToCore(&displayTask, "displayTask", 2048, NULL, 5, NULL, 1);
xTaskCreatePinnedToCore(&displayTask, "displayTask", 4096, NULL, 5, NULL, 0);
osd_initinput();
return 0;
}
void osd_shutdown()
{
osd_stopsound();
osd_freeinput();
}
char configfilename[] = "na";
int osd_main()
{
config.filename = configfilename;
return main_loop();
}
//Seemingly, this will be called only once. Should call func with a freq of frequency,
int osd_installtimer(int frequency, void *func, int funcsize, void *counter, int countersize)
{
if (timer == NULL || xTimerIsTimerActive(timer) == pdFALSE)
{
nofrendo_log_printf("Timer install, configTICK_RATE_HZ=%d, freq=%d\n", configTICK_RATE_HZ, frequency);
timer = xTimerCreate("nes", configTICK_RATE_HZ / frequency, pdTRUE, NULL, func);
xTimerStart(timer, 0);
}
else
{
nofrendo_log_printf("Timer allready exits for %d HZ, skipping creation...", frequency);
}
return 0;
}
/* filename manipulation */
void osd_fullname(char *fullname, const char *shortname)
{
strncpy(fullname, shortname, PATH_MAX);
}
/* This gives filenames for storage of saves */
char *osd_newextension(char *string, char *ext)
{
// dirty: assume both extensions is 3 characters
size_t l = strlen(string);
string[l - 3] = ext[1];
string[l - 2] = ext[2];
string[l - 1] = ext[3];
return string;
}
/* This gives filenames for storage of PCX snapshots */
int osd_makesnapname(char *filename, int len)
{
return -1;
}