aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-03-04 19:01:53 +0100
committerBad Diode <bd@badd10de.dev>2022-03-04 19:01:53 +0100
commita46fb7bd7eb1cf4c3237c2f3c944dfc82bcc2ee5 (patch)
treee63d6ccd134b7476e519a2415cd845f41bf8d86e
parent6b29a0cb080aa7e8fad9de8943d2637437cad53d (diff)
downloaduxnfb-a46fb7bd7eb1cf4c3237c2f3c944dfc82bcc2ee5.tar.gz
uxnfb-a46fb7bd7eb1cf4c3237c2f3c944dfc82bcc2ee5.zip
Disable tty echo
-rw-r--r--src/main.c5
-rw-r--r--src/ppu.c116
2 files changed, 69 insertions, 52 deletions
diff --git a/src/main.c b/src/main.c
index e213bbb..de566ac 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,3 @@
1#include <fcntl.h>
2#include <stdbool.h> 1#include <stdbool.h>
3#include <stdint.h> 2#include <stdint.h>
4#include <stdio.h> 3#include <stdio.h>
@@ -6,10 +5,6 @@
6#include <time.h> 5#include <time.h>
7#include <unistd.h> 6#include <unistd.h>
8 7
9#include <linux/fb.h>
10#include <sys/ioctl.h>
11#include <sys/mman.h>
12
13#include "shorthand.h" 8#include "shorthand.h"
14#include "ppu.c" 9#include "ppu.c"
15#include "uxn-fast.c" 10#include "uxn-fast.c"
diff --git a/src/ppu.c b/src/ppu.c
index b0ddc5f..ff93ec1 100644
--- a/src/ppu.c
+++ b/src/ppu.c
@@ -1,19 +1,25 @@
1#include <fcntl.h>
1#include <string.h> 2#include <string.h>
3#include <termios.h>
4
5#include <linux/fb.h>
6#include <sys/ioctl.h>
7#include <sys/mman.h>
2 8
3#include "ppu.h" 9#include "ppu.h"
4 10
5/* 11/*
6Copyright (c) 2021 Devine Lu Linvega 12 Copyright (c) 2021 Devine Lu Linvega
7Copyright (c) 2021 Andrew Alderwick 13 Copyright (c) 2021 Andrew Alderwick
8Copyright (c) 2021 Bad Diode 14 Copyright (c) 2021 Bad Diode
9 15
10Permission to use, copy, modify, and distribute this software for any 16 Permission to use, copy, modify, and distribute this software for any
11purpose with or without fee is hereby granted, provided that the above 17 purpose with or without fee is hereby granted, provided that the above
12copyright notice and this permission notice appear in all copies. 18 copyright notice and this permission notice appear in all copies.
13 19
14THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 20 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15WITH REGARD TO THIS SOFTWARE. 21 WITH REGARD TO THIS SOFTWARE.
16*/ 22 */
17 23
18static size_t screen_width = 0; 24static size_t screen_width = 0;
19static size_t screen_height = 0; 25static size_t screen_height = 0;
@@ -37,12 +43,12 @@ static u8 blending[5][16] = {
37 43
38void 44void
39ppu_pixel(u8 *layer, u16 x, u16 y, u8 color) { 45ppu_pixel(u8 *layer, u16 x, u16 y, u8 color) {
40 if(x < screen_width && y < screen_height) { 46 if(x < screen_width && y < screen_height) {
41 Uint32 i = x + y *screen_width; 47 Uint32 i = x + y *screen_width;
42 if(color != layer[i]) { 48 if(color != layer[i]) {
43 layer[i] = color; 49 layer[i] = color;
44 } 50 }
45 } 51 }
46 dirty_lines[y] |= 1; 52 dirty_lines[y] |= 1;
47} 53}
48 54
@@ -54,9 +60,9 @@ ppu_1bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
54 u8 ch1 = (sprite[v] >> (7 - h)) & 0x1; 60 u8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
55 if(ch1 || blending[4][color]) 61 if(ch1 || blending[4][color])
56 ppu_pixel(layer, 62 ppu_pixel(layer,
57 x + (flipx ? 7 - h : h), 63 x + (flipx ? 7 - h : h),
58 y + (flipy ? 7 - v : v), 64 y + (flipy ? 7 - v : v),
59 blending[ch1][color]); 65 blending[ch1][color]);
60 } 66 }
61} 67}
62 68
@@ -70,9 +76,9 @@ ppu_2bpp(u8 *layer, u16 x, u16 y, u8 *sprite, u8 color, u8 flipx, u8 flipy) {
70 u8 ch = ch1 + ch2 * 2; 76 u8 ch = ch1 + ch2 * 2;
71 if(ch || blending[4][color]) 77 if(ch || blending[4][color])
72 ppu_pixel(layer, 78 ppu_pixel(layer,
73 x + (flipx ? 7 - h : h), 79 x + (flipx ? 7 - h : h),
74 y + (flipy ? 7 - v : v), 80 y + (flipy ? 7 - v : v),
75 blending[ch][color]); 81 blending[ch][color]);
76 } 82 }
77} 83}
78 84
@@ -85,32 +91,48 @@ redraw_screen(void) {
85 91
86int 92int
87ppu_init(void) { 93ppu_init(void) {
88 // Open frambuffer and get the size. 94 // Open frambuffer and get the size.
89 int fb = open("/dev/fb0", O_RDWR); 95 int fb = open("/dev/fb0", O_RDWR);
90 if (fb <= 0) { 96 if (fb <= 0) {
91 fprintf(stderr, "error: couldn't open the framebuffer\n"); 97 fprintf(stderr, "error: couldn't open the framebuffer\n");
92 exit(EXIT_FAILURE); 98 exit(EXIT_FAILURE);
93 } 99 }
94 struct fb_var_screeninfo info; 100 struct fb_var_screeninfo info;
95 if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) { 101 if (ioctl(fb, FBIOGET_VSCREENINFO, &info) != 0) {
96 fprintf(stderr, "error: couldn't get the framebuffer size\n"); 102 fprintf(stderr, "error: couldn't get the framebuffer size\n");
97 exit(EXIT_FAILURE); 103 exit(EXIT_FAILURE);
98 } 104 }
99 105
100 // Mmap the framebuffer to a buffer object. 106 // Mmap the framebuffer to a buffer object.
101 screen_width = info.xres; 107 screen_width = info.xres;
102 screen_height = info.yres; 108 screen_height = info.yres;
103 size_t len = 4 * screen_width * screen_height; 109 size_t len = 4 * screen_width * screen_height;
104 framebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); 110 framebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);
105 if (framebuffer == MAP_FAILED) { 111 if (framebuffer == MAP_FAILED) {
106 fprintf(stderr, "error: couldn't mmap the framebuffer\n"); 112 fprintf(stderr, "error: couldn't mmap the framebuffer\n");
107 exit(EXIT_FAILURE); 113 exit(EXIT_FAILURE);
108 } 114 }
109 115
110 // Allocate intermediate buffers. 116 // Allocate intermediate buffers.
111 pixels_fg = malloc(screen_width * screen_height); 117 pixels_fg = malloc(screen_width * screen_height);
112 pixels_bg = malloc(screen_width * screen_height); 118 pixels_bg = malloc(screen_width * screen_height);
113 dirty_lines = malloc(screen_height); 119 dirty_lines = malloc(screen_height);
120 if (pixels_fg == NULL || pixels_bg == NULL || dirty_lines == NULL) {
121 fprintf(stderr, "error: couldn't allocate memory for the ppu\n");
122 exit(EXIT_FAILURE);
123 }
124
125 // Disable echo.
126 struct termios t;
127 if (tcgetattr(STDIN_FILENO, &t)) {
128 fprintf(stderr, "error: couldn't disable terminal echo\n");
129 exit(EXIT_FAILURE);
130 }
131 t.c_lflag &= ~((tcflag_t) ECHO);
132 if (tcsetattr(STDIN_FILENO, TCSANOW, &t)) {
133 fprintf(stderr, "error: couldn't disable terminal echo\n");
134 exit(EXIT_FAILURE);
135 }
114 136
115 // Initialize default palette. 137 // Initialize default palette.
116 palette[0] = 0x444444; 138 palette[0] = 0x444444;