8 #include "../../../config_file/config_file.h"
10 static uint16_t palette[256];
12 static uint8_t rtg_u8[4];
13 static uint16_t rtg_x[3], rtg_y[3];
14 static uint16_t rtg_format;
15 static uint32_t rtg_address[2];
16 static uint32_t rtg_rgb[2];
18 static uint8_t display_enabled;
20 uint16_t rtg_display_width, rtg_display_height;
21 uint16_t rtg_display_format;
22 uint16_t rtg_pitch, rtg_total_rows;
23 uint16_t rtg_offset_x, rtg_offset_y;
25 uint8_t *rtg_mem; // FIXME
27 uint32_t framebuffer_addr;
29 static void handle_rtg_command(uint32_t cmd);
30 static struct timespec f1, f2;
32 static const char *op_type_names[OP_TYPE_NUM] = {
39 static const char *rtg_format_names[RTGFMT_NUM] = {
47 rtg_mem = calloc(1, 32 * SIZE_MEGA);
49 printf("Failed to allocate RTG video memory.\n");
56 extern uint8_t busy, rtg_on;
57 void rtg_update_screen();
59 unsigned int rtg_read(uint32_t address, uint8_t mode) {
60 //printf("%s read from RTG: %.8X\n", op_type_names[mode], address);
61 if (address >= PIGFX_REG_SIZE) {
65 return (rtg_mem[address - PIGFX_REG_SIZE]);
68 return *(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE]));
70 case OP_TYPE_LONGWORD:
71 return *(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE]));
82 struct timespec diff(struct timespec start, struct timespec end)
85 if ((end.tv_nsec-start.tv_nsec)<0) {
86 temp.tv_sec = end.tv_sec-start.tv_sec-1;
87 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
89 temp.tv_sec = end.tv_sec-start.tv_sec;
90 temp.tv_nsec = end.tv_nsec-start.tv_nsec;
95 void rtg_write(uint32_t address, uint32_t value, uint8_t mode) {
96 //printf("%s write to RTG: %.8X (%.8X)\n", op_type_names[mode], address, value);
97 if (address >= PIGFX_REG_SIZE) {
101 rtg_mem[address - PIGFX_REG_SIZE] = value;
104 *(( uint16_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = value;
106 case OP_TYPE_LONGWORD:
107 *(( uint32_t *) (&rtg_mem[address - PIGFX_REG_SIZE])) = value;
122 rtg_u8[address - RTG_U81] = value;
149 handle_rtg_command(value);
153 case OP_TYPE_LONGWORD:
156 rtg_address[0] = value;
159 rtg_address[1] = value;
173 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &f2);
174 if (diff(f1,f2).tv_nsec / 1000000.0 > 100.00) {
183 static void handle_rtg_command(uint32_t cmd) {
184 //printf("Handling RTG command %d (%.8X)\n", cmd, cmd);
187 rtg_display_format = rtg_format;
188 rtg_display_width = rtg_x[0];
189 rtg_display_height = rtg_y[0];
191 rtg_pitch = rtg_x[1];
192 rtg_total_rows = rtg_y[1];
195 rtg_pitch = rtg_x[1];
196 rtg_total_rows = rtg_y[1];
198 //printf("Set RTG mode:\n");
199 //printf("%dx%d pixels\n", rtg_display_width, rtg_display_height);
200 //printf("Pixel format: %s\n", rtg_format_names[rtg_display_format]);
203 //printf("Command: SetPan.\n");
204 framebuffer_addr = rtg_address[0] - (PIGFX_RTG_BASE + PIGFX_REG_SIZE);
205 rtg_offset_x = rtg_x[1];
206 rtg_offset_y = rtg_y[1];
207 rtg_pitch = (rtg_x[0] << rtg_display_format);
208 //printf("Set panning to $%.8X\n", framebuffer_addr);
209 //printf("Offset X/Y: %d/%d\n", rtg_offset_x, rtg_offset_y);
210 //printf("Pitch: %d (%d bytes)\n", rtg_x[0], rtg_pitch);
212 case RTGCMD_SETCLUT: {
213 //printf("Command: SetCLUT.\n");
214 //printf("Set palette entry %d to %d, %d, %d\n", rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_u8[3]);
215 /*int r = (int)((float)rtg_u8[1] / 255.0f * 31.0f);
216 int g = (int)((float)rtg_u8[2] / 255.0f * 63.0f);
217 int b = (int)((float)rtg_u8[3] / 255.0f * 31.0f);
218 palette[rtg_u8[0]] = ((r & 0x1F) << 11) | ((g & 0x3F) << 6) | (b & 0x1F);*/
219 rtg_set_clut_entry(rtg_u8[0], rtg_u8[1], rtg_u8[2], rtg_u8[3]);
222 case RTGCMD_SETDISPLAY:
224 //printf("Command: SetDisplay.\n");
227 case RTGCMD_SETSWITCH:
228 if (display_enabled != rtg_u8[1]) {
229 //printf("RTG Display %s\n", (rtg_u8[1]) ? "enabled" : "disabled");
230 display_enabled = rtg_u8[1];
231 if (display_enabled) {
233 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &f1);
236 rtg_shutdown_display();
239 case RTGCMD_FILLRECT:
240 rtg_fillrect(rtg_x[0], rtg_y[0], rtg_x[1], rtg_y[1], rtg_rgb[0], rtg_x[2], rtg_format, 0xFF);
245 void rtg_fillrect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t color, uint16_t pitch, uint16_t format, uint8_t mask) {
250 case RTGFMT_RBG565: {
251 uint16_t *ptr = (uint16_t *)&rtg_mem[framebuffer_addr + (x << format) + (y * pitch)];
252 for (int xs = 0; xs < w; xs++) {
253 ptr[xs] = (color >> 16);
255 for (int ys = 1; ys < h; ys++) {
256 ptr += (rtg_pitch >> format);
257 memcpy(ptr, (void *)(size_t)(ptr - (rtg_pitch >> format)), rtg_pitch);