|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/******************************************************************************* |
| 8 | +* Includes |
| 9 | +*******************************************************************************/ |
| 10 | +#include <freertos/FreeRTOS.h> |
| 11 | +#include <freertos/timers.h> |
| 12 | +#include <freertos/task.h> |
| 13 | +#include <freertos/queue.h> |
| 14 | +#include <string.h> |
| 15 | + |
| 16 | +#define LOBYTE(u16) ((uint8_t)(((uint16_t)(u16)) & 0xff)) |
| 17 | +#define HIBYTE(u16) ((uint8_t)((((uint16_t)(u16))>>8) & 0xff)) |
| 18 | + |
| 19 | +//Nes stuff wants to define this as well... |
| 20 | +#undef false |
| 21 | +#undef true |
| 22 | +#undef bool |
| 23 | + |
| 24 | +#include "noftypes.h" |
| 25 | +#include "nofconfig.h" |
| 26 | +#include "nofrendo.h" |
| 27 | +#include "bitmap.h" |
| 28 | +#include "osd.h" |
| 29 | +#include "bsp/esp-bsp.h" |
| 30 | + |
| 31 | + |
| 32 | +/******************************************************************************* |
| 33 | +* Function definitions |
| 34 | +*******************************************************************************/ |
| 35 | +/* Video functions */ |
| 36 | +static int app_osd_video_init(int width, int height); |
| 37 | +static void app_osd_video_shutdown(void); |
| 38 | +static int app_osd_video_set_mode(int width, int height); |
| 39 | +static void app_osd_video_set_palette(rgb_t *palette); |
| 40 | +static void app_osd_video_clear(uint8 color); |
| 41 | +static bitmap_t *app_osd_video_lock_write(void); |
| 42 | +static void app_osd_video_free_write(int num_dirties, rect_t *dirty_rects); |
| 43 | +static void app_osd_video_custom_blit(bitmap_t *primary, int num_dirties, rect_t *dirty_rects); |
| 44 | + |
| 45 | +/* Audio functions */ |
| 46 | +/******************************************************************************* |
| 47 | +* Local variables |
| 48 | +*******************************************************************************/ |
| 49 | + |
| 50 | +static viddriver_t vid_driver = { |
| 51 | + .name = "Vid Driver", |
| 52 | + .init = app_osd_video_init, |
| 53 | + .shutdown = app_osd_video_shutdown, |
| 54 | + .set_mode = app_osd_video_set_mode, |
| 55 | + .set_palette = app_osd_video_set_palette, |
| 56 | + .clear = app_osd_video_clear, |
| 57 | + .lock_write = app_osd_video_lock_write, |
| 58 | + .free_write = app_osd_video_free_write, |
| 59 | + .custom_blit = app_osd_video_custom_blit, |
| 60 | + .invalidate = false, |
| 61 | +}; |
| 62 | + |
| 63 | +static TimerHandle_t timer; |
| 64 | +static uint16 colorPalette[256]; |
| 65 | +static bitmap_t *vidBitmap = NULL; |
| 66 | + |
| 67 | +static lv_obj_t * lvgl_video_canvas = NULL; |
| 68 | +uint8_t * vid_buffer = NULL; |
| 69 | + |
| 70 | +/******************************************************************************* |
| 71 | +* Public API functions |
| 72 | +*******************************************************************************/ |
| 73 | + |
| 74 | +void osd_getvideoinfo(vidinfo_t *info) |
| 75 | +{ |
| 76 | + info->default_width = 256; |
| 77 | + info->default_height = BSP_LCD_V_RES; |
| 78 | + info->driver = &vid_driver; |
| 79 | +} |
| 80 | + |
| 81 | +void osd_getsoundinfo(sndinfo_t *info) |
| 82 | +{ |
| 83 | +} |
| 84 | + |
| 85 | +void osd_setsound(void (*playfunc)(void *buffer, int size)) |
| 86 | +{ |
| 87 | +} |
| 88 | + |
| 89 | +int osd_init(void) |
| 90 | +{ |
| 91 | + uint32_t buf_size = 256 * BSP_LCD_V_RES * sizeof(uint16_t); |
| 92 | + vid_buffer = heap_caps_malloc(buf_size, MALLOC_CAP_DEFAULT); |
| 93 | + assert(vid_buffer); |
| 94 | + memset(vid_buffer, 0, buf_size); |
| 95 | + |
| 96 | + bsp_display_lock(0); |
| 97 | + lv_obj_set_style_bg_color(lv_scr_act(), lv_color_black(), 0); |
| 98 | + lvgl_video_canvas = lv_canvas_create(lv_scr_act()); |
| 99 | + lv_canvas_set_buffer(lvgl_video_canvas, vid_buffer, 256, BSP_LCD_V_RES, LV_IMG_CF_TRUE_COLOR); |
| 100 | + lv_obj_center(lvgl_video_canvas); |
| 101 | + bsp_display_unlock(); |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +void osd_shutdown(void) |
| 107 | +{ |
| 108 | +} |
| 109 | + |
| 110 | +/* This is os-specific part of main() */ |
| 111 | +int osd_main(int argc, char *argv[]) |
| 112 | +{ |
| 113 | + config.filename = "config.cfg"; |
| 114 | + return main_loop("rom", system_autodetect); |
| 115 | +} |
| 116 | + |
| 117 | +int osd_installtimer(int frequency, void *func, int funcsize, void *counter, int countersize) |
| 118 | +{ |
| 119 | + printf("Timer install, freq=%d\n", frequency); |
| 120 | + timer=xTimerCreate("nes",configTICK_RATE_HZ/frequency, pdTRUE, NULL, func); |
| 121 | + xTimerStart(timer, 0); |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +/* File system interface */ |
| 126 | +void osd_fullname(char *fullname, const char *shortname) |
| 127 | +{ |
| 128 | + strncpy(fullname, shortname, PATH_MAX); |
| 129 | +} |
| 130 | + |
| 131 | +/* This gives filenames for storage of saves */ |
| 132 | +char *osd_newextension(char *string, char *ext) |
| 133 | +{ |
| 134 | + return string; |
| 135 | +} |
| 136 | + |
| 137 | +void osd_getinput(void) |
| 138 | +{ |
| 139 | +} |
| 140 | + |
| 141 | +void osd_getmouse(int *x, int *y, int *button) |
| 142 | +{ |
| 143 | +} |
| 144 | + |
| 145 | +/* This gives filenames for storage of PCX snapshots */ |
| 146 | +int osd_makesnapname(char *filename, int len) |
| 147 | +{ |
| 148 | + return -1; |
| 149 | +} |
| 150 | + |
| 151 | +/******************************************************************************* |
| 152 | +* Private API functions |
| 153 | +*******************************************************************************/ |
| 154 | + |
| 155 | +static int app_osd_video_init(int width, int height) |
| 156 | +{ |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +static void app_osd_video_shutdown(void) |
| 161 | +{ |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | +/* set a video mode */ |
| 166 | +static int app_osd_video_set_mode(int width, int height) |
| 167 | +{ |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +/* copy nes palette over to hardware */ |
| 172 | +static void app_osd_video_set_palette(rgb_t *palette) |
| 173 | +{ |
| 174 | + uint16 c; |
| 175 | + |
| 176 | + for (int i = 0; i < 256; i++) |
| 177 | + { |
| 178 | + c=(palette[i].b>>3)+((palette[i].g>>2)<<5)+((palette[i].r>>3)<<11); |
| 179 | + colorPalette[i]=c; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/* clear all frames to a particular color */ |
| 184 | +static void app_osd_video_clear(uint8 color) |
| 185 | +{ |
| 186 | + bsp_display_lock(0); |
| 187 | + |
| 188 | + for(int x=0; x<256; x++) |
| 189 | + { |
| 190 | + for(int y=0; y<BSP_LCD_V_RES; y++) |
| 191 | + { |
| 192 | + vid_buffer[(x*BSP_LCD_V_RES*2) + (y*2)] = HIBYTE(colorPalette[color]); |
| 193 | + vid_buffer[(x*BSP_LCD_V_RES*2) + (y*2)+1] = LOBYTE(colorPalette[color]); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + lv_obj_invalidate(lvgl_video_canvas); |
| 198 | + bsp_display_unlock(); |
| 199 | +} |
| 200 | + |
| 201 | +/* acquire the directbuffer for writing */ |
| 202 | +static bitmap_t *app_osd_video_lock_write(void) |
| 203 | +{ |
| 204 | + vidBitmap = bmp_create(256, BSP_LCD_V_RES, 256*sizeof(uint16)); |
| 205 | + return vidBitmap; |
| 206 | +} |
| 207 | + |
| 208 | +static void app_osd_video_free_write(int num_dirties, rect_t *dirty_rects) |
| 209 | +{ |
| 210 | + bmp_destroy(&vidBitmap); |
| 211 | + vidBitmap = NULL; |
| 212 | +} |
| 213 | + |
| 214 | +static void app_osd_video_custom_blit(bitmap_t *primary, int num_dirties, rect_t *dirty_rects) |
| 215 | +{ |
| 216 | + const uint8_t * data = primary->data; |
| 217 | + bsp_display_lock(0); |
| 218 | + |
| 219 | + for(int x=0; x<256; x++) |
| 220 | + { |
| 221 | + for(int y=0; y<BSP_LCD_V_RES; y++) |
| 222 | + { |
| 223 | + vid_buffer[(x*BSP_LCD_V_RES*2) + (y*2)] = HIBYTE(colorPalette[data[0]]); |
| 224 | + vid_buffer[(x*BSP_LCD_V_RES*2) + (y*2)+1] = LOBYTE(colorPalette[data[0]]); |
| 225 | + data++; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + lv_obj_invalidate(lvgl_video_canvas); |
| 230 | + bsp_display_unlock(); |
| 231 | +} |
0 commit comments