]> git.sesse.net Git - pistorm/blob - platforms/amiga/rtg/rtg-output.c
Add license information to source
[pistorm] / platforms / amiga / rtg / rtg-output.c
1 // SPDX-License-Identifier: MIT
2
3 #include "emulator.h"
4 #include "rtg.h"
5
6 #include <pthread.h>
7 #include <SDL2/SDL.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
15
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;
20
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;
25
26 static pthread_t thread_id;
27
28 struct rtg_shared_data {
29     uint16_t *width, *height;
30     uint16_t *format, *pitch;
31     uint16_t *offset_x, *offset_y;
32     uint8_t *memory;
33     uint32_t *addr;
34     uint8_t *running;
35 };
36
37 SDL_Window *win = NULL;
38 SDL_Renderer *renderer = NULL;
39 SDL_Texture *img = NULL;
40
41 struct rtg_shared_data rtg_share_data;
42 static uint32_t palette[256];
43
44 void rtg_update_screen() {}
45
46 uint32_t rtg_to_sdl2[RTGFMT_NUM] = {
47     SDL_PIXELFORMAT_ARGB8888,
48     SDL_PIXELFORMAT_RGB565,
49     SDL_PIXELFORMAT_ARGB8888,
50     SDL_PIXELFORMAT_RGB555,
51 };
52
53 void *rtgThread(void *args) {
54
55     printf("RTG thread running\n");
56     fflush(stdout);
57
58     int reinit = 0;
59     rtg_on = 1;
60
61     uint32_t *indexed_buf = NULL;
62
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;
73
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;
78
79     printf("Initializing SDL2...\n");
80     if (SDL_Init(0) < 0) {
81         printf("Failed to initialize SDL2.\n");
82     }
83
84     printf("Initializing SDL2 Video...\n");
85     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
86         printf("Failed to initialize SDL2 Video..\n");
87     }
88
89 reinit_sdl:;
90     if (reinit) {
91         printf("Reinitializing SDL2...\n");
92         width = rtg_display_width;
93         height = rtg_display_height;
94         format = rtg_display_format;
95         pitch = rtg_pitch;
96         if (indexed_buf) {
97             free(indexed_buf);
98             indexed_buf = NULL;
99         }
100         reinit = 0;
101     }
102
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);
106     if (!win) {
107         RTG_INIT_ERR("Failed create SDL2 window.\n");
108     }
109     else {
110         printf("Created %dx%d window.\n", width, height);
111     }
112
113     printf("Creating SDL2 renderer...\n");
114     renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
115     if (!renderer) {
116         RTG_INIT_ERR("Failed create SDL2 renderer.\n");
117     }
118     else {
119         printf("Created SDL2 renderer.\n");
120     }
121
122     printf("Creating SDL2 texture...\n");
123     img = SDL_CreateTexture(renderer, rtg_to_sdl2[format], SDL_TEXTUREACCESS_TARGET, width, height);
124     if (!img) {
125         RTG_INIT_ERR("Failed create SDL2 texture.\n");
126     }
127     else {
128         printf("Created %dx%d texture.\n", width, height);
129     }
130
131     switch (format) {
132         case RTGFMT_8BIT:
133             indexed_buf = calloc(1, width * height * 4);
134             break;
135         case RTGFMT_RBG565:
136             indexed_buf = calloc(1, width * height * 2);
137             break;
138         default:
139             break;
140     }
141
142     uint64_t frame_start = 0, frame_end = 0;
143     float elapsed = 0.0f;
144
145     while (1) {
146         if (renderer && win && img) {
147             frame_start = SDL_GetPerformanceCounter();
148             SDL_RenderClear(renderer);
149             if (*data->running) {
150                 switch (format) {
151                     case RTGFMT_RGB32:
152                         SDL_UpdateTexture(img, NULL, &data->memory[*data->addr], pitch);
153                         break;
154                     case RTGFMT_RBG565:
155                         SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 2);
156                         break;
157                     case RTGFMT_8BIT:
158                         SDL_UpdateTexture(img, NULL, (uint8_t *)indexed_buf, width * 4);
159                         break;
160                 }
161                 SDL_RenderCopy(renderer, img, NULL, NULL);
162             }
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");
167                 reinit = 1;
168                 goto shutdown_sdl;
169             }
170             switch (format) {
171                 case RTGFMT_8BIT:
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)]];
175                         }
176                     }
177                     break;
178                 case RTGFMT_RBG565:
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))]);
182                         }
183                     }
184                     break;
185             }
186             frame_end = SDL_GetPerformanceCounter();
187             elapsed = (frame_end - frame_start) / (float)SDL_GetPerformanceFrequency() * 1000.0f;
188             pitch = rtg_pitch;
189             SDL_Delay(floor(16.66666f - elapsed));
190         }
191         else
192             break;
193     }
194
195     rtg_initialized = 0;
196     printf("RTG thread shut down.\n");
197
198 shutdown_sdl:;
199     if (img) SDL_DestroyTexture(img);
200     if (renderer) SDL_DestroyRenderer(renderer);
201     if (win) SDL_DestroyWindow(win);
202
203     win = NULL;
204     img = NULL;
205     renderer = NULL;
206
207     if (reinit)
208         goto reinit_sdl;
209
210     if (indexed_buf)
211         free(indexed_buf);
212
213     SDL_QuitSubSystem(SDL_INIT_VIDEO);
214     SDL_Quit();
215
216     return args;
217 }
218
219 void rtg_set_clut_entry(uint8_t index, uint32_t xrgb) {
220     palette[index] = xrgb;
221 }
222
223 void rtg_init_display() {
224     int err;
225     rtg_on = 1;
226
227     if (!rtg_initialized) {
228         err = pthread_create(&thread_id, NULL, &rtgThread, (void *)&rtg_share_data);
229         if (err != 0) {
230             rtg_on = 0;
231             printf("can't create RTG thread :[%s]", strerror(err));
232         }
233         else {
234             rtg_initialized = 1;
235             pthread_setname_np(thread_id, "pistorm: rtg");
236             printf("RTG Thread created successfully\n");
237         }
238     }
239     printf("RTG display enabled.\n");
240 }
241
242 void rtg_shutdown_display() {
243     printf("RTG display disabled.\n");
244     rtg_on = 0;
245 }