1 // SPDX-License-Identifier: MIT
14 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
16 uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
17 extern uint8_t *rtg_mem;
18 extern uint32_t framebuffer_addr;
19 extern uint32_t framebuffer_addr_adj;
21 extern uint16_t rtg_display_width, rtg_display_height;
22 extern uint16_t rtg_display_format;
23 extern uint16_t rtg_pitch, rtg_total_rows;
24 extern uint16_t rtg_offset_x, rtg_offset_y;
26 static pthread_t thread_id;
28 struct rtg_shared_data {
29 uint16_t *width, *height;
30 uint16_t *format, *pitch;
31 uint16_t *offset_x, *offset_y;
37 SDL_Window *win = NULL;
38 SDL_Renderer *renderer = NULL;
39 SDL_Texture *img = NULL;
41 struct rtg_shared_data rtg_share_data;
42 static uint32_t palette[256];
44 void rtg_update_screen() {}
46 uint32_t rtg_to_sdl2[RTGFMT_NUM] = {
47 SDL_PIXELFORMAT_ARGB8888,
48 SDL_PIXELFORMAT_RGB565,
49 SDL_PIXELFORMAT_ARGB8888,
50 SDL_PIXELFORMAT_RGB555,
53 void *rtgThread(void *args) {
55 printf("RTG thread running\n");
61 uint32_t *indexed_buf = NULL;
63 rtg_share_data.format = &rtg_display_format;
64 rtg_share_data.width = &rtg_display_width;
65 rtg_share_data.height = &rtg_display_height;
66 rtg_share_data.pitch = &rtg_pitch;
67 rtg_share_data.offset_x = &rtg_offset_x;
68 rtg_share_data.offset_y = &rtg_offset_y;
69 rtg_share_data.memory = rtg_mem;
70 rtg_share_data.running = &rtg_on;
71 rtg_share_data.addr = &framebuffer_addr_adj;
72 struct rtg_shared_data *data = &rtg_share_data;
74 uint16_t width = rtg_display_width;
75 uint16_t height = rtg_display_height;
76 uint16_t format = rtg_display_format;
77 uint16_t pitch = rtg_pitch;
79 printf("Initializing SDL2...\n");
80 if (SDL_Init(0) < 0) {
81 printf("Failed to initialize SDL2.\n");
84 printf("Initializing SDL2 Video...\n");
85 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
86 printf("Failed to initialize SDL2 Video..\n");
91 printf("Reinitializing SDL2...\n");
92 width = rtg_display_width;
93 height = rtg_display_height;
94 format = rtg_display_format;
103 printf("Creating %dx%d SDL2 window...\n", width, height);
104 win = SDL_CreateWindow("Pistorm RTG", 0, 0, width, height, 0);
105 SDL_ShowCursor(SDL_DISABLE);
107 RTG_INIT_ERR("Failed create SDL2 window.\n");
110 printf("Created %dx%d window.\n", width, height);
113 printf("Creating SDL2 renderer...\n");
114 renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
116 RTG_INIT_ERR("Failed create SDL2 renderer.\n");
119 printf("Created SDL2 renderer.\n");
122 printf("Creating SDL2 texture...\n");
123 img = SDL_CreateTexture(renderer, rtg_to_sdl2[format], SDL_TEXTUREACCESS_TARGET, width, height);
125 RTG_INIT_ERR("Failed create SDL2 texture.\n");
128 printf("Created %dx%d texture.\n", width, height);
133 indexed_buf = calloc(1, width * height * 4);
136 indexed_buf = calloc(1, width * height * 2);
142 uint64_t frame_start = 0, frame_end = 0;
143 float elapsed = 0.0f;
146 if (renderer && win && img) {
147 frame_start = SDL_GetPerformanceCounter();
148 SDL_RenderClear(renderer);
149 if (*data->running) {
152 SDL_UpdateTexture(img, NULL, &data->memory[*data->addr], pitch);
155 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 2);
158 SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 4);
161 SDL_RenderCopy(renderer, img, NULL, NULL);
163 SDL_RenderPresent(renderer);
164 //usleep(16667); //ghetto 60hz
165 if (height != *data->height || width != *data->width || format != *data->format) {
166 printf("Reinitializing due to something change.\n");
172 for (int y = 0; y < height; y++) {
173 for (int x = 0; x < width; x++) {
174 indexed_buf[x + (y * width)] = palette[data->memory[*data->addr + x + (y * pitch)]];
179 for (int y = 0; y < height; y++) {
180 for (int x = 0; x < width; x++) {
181 ((uint16_t *)indexed_buf)[x + (y * width)] = be16toh(((uint16_t *)data->memory)[(*data->addr / 2) + x + (y * (pitch / 2))]);
186 frame_end = SDL_GetPerformanceCounter();
187 elapsed = (frame_end - frame_start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
189 SDL_Delay(floor(16.66666f - elapsed));
196 printf("RTG thread shut down.\n");
199 if (img) SDL_DestroyTexture(img);
200 if (renderer) SDL_DestroyRenderer(renderer);
201 if (win) SDL_DestroyWindow(win);
213 SDL_QuitSubSystem(SDL_INIT_VIDEO);
219 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
220 palette[index] = xrgb;
223 void rtg_init_display() {
227 if (!rtg_initialized) {
228 err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
231 printf("can't create RTG thread :[%s]", strerror(err));
235 pthread_setname_np(thread_id, "pistorm: rtg");
236 printf("RTG Thread created successfully\n");
239 printf("RTG display enabled.\n");
242 void rtg_shutdown_display() {
243 printf("RTG display disabled.\n");