]> 4ch.mooo.com Git - plz.git/blob - PLZPART/vga.c
28b91b664c041db7f8bbebc5e0e472d12d4c1476
[plz.git] / PLZPART / vga.c
1 /*
2  * Written by Nick Kovac with minor changes by Claudio Matsuoka
3  */
4
5 #include <string.h>
6 #include "common.h"
7
8 static int plane_select[4] = { 0, 0, 0, 0 };
9 static int line_compare = 0;
10 static int hscroll_offset = 0;
11 static uint8_t palette[256 * 3];
12
13 #define SCREEN_WIDTH 320
14 #define SCREEN_HEIGHT 400
15 #define FRAME_BUFFER_WIDTH 384
16 #define FRAME_BUFFER_HEIGHT 400
17
18 uint8_t fb[FRAME_BUFFER_WIDTH * FRAME_BUFFER_HEIGHT];
19
20
21 void vga_select_bitplanes_02()
22 {
23         plane_select[0] = 1;
24         plane_select[1] = 0;
25         plane_select[2] = 1;
26         plane_select[3] = 0;
27 }
28
29 void vga_select_bitplanes_13()
30 {
31         plane_select[0] = 0;
32         plane_select[1] = 1;
33         plane_select[2] = 0;
34         plane_select[3] = 1;
35 }
36
37 void vga_select_bitplanes_0123()
38 {
39         plane_select[0] = 1;
40         plane_select[1] = 1;
41         plane_select[2] = 1;
42         plane_select[3] = 1;
43 }
44
45 void vga_write32(int offset, int val)
46 {
47         int i, j;
48
49         // For each selected plane, calculate 4 pixel offsets and write 4 bytes.
50         // offset = 0, 4, 8, etc
51
52         for (i = 0; i < 4; i++) {
53                 if (plane_select[i]) {
54                         for (j = 0; j < 4; j++) {
55                                 // Get byte to write.
56                                 uint8_t b = (val >> (j * 8));
57
58                                 // Calculate offset.
59                                 int fb_offs = i + (offset + j) * 4;
60
61                                 // Write pixel.
62                                 fb[fb_offs] = b;
63                         }
64                 }
65         }
66 }
67
68 void vga_set_line_compare(int y)
69 {
70         line_compare = y;
71 }
72
73 void vga_set_palette_entry(int index, int r, int g, int b)
74 {
75         index *= 3;
76         palette[index++] = r;
77         palette[index++] = g;
78         palette[index] = b;
79 }
80
81 extern unsigned char *image;    // The OpenGL background image texture
82
83 void vga_show_framebuffer()
84 {
85         int x, y;
86
87         // Clear the screen.
88         memset(image, 0, FRAME_BUFFER_WIDTH * FRAME_BUFFER_HEIGHT * 3);
89
90         copper1();
91         copper2();
92
93         int nFirstLineIndex = (line_compare + 1);
94
95         // Plot the palettised frame buffer.
96         uint8_t *ptr = fb;
97
98         for (y = nFirstLineIndex; y < SCREEN_HEIGHT; y++) {
99                 for (x = 0; x < SCREEN_WIDTH; x++) {
100                         //ASSERT(x + hscroll_offset < FRAME_BUFFER_WIDTH);
101                         uint8_t r, g, b;
102                         int idx = ptr[x + hscroll_offset] * 3;
103
104                         r = palette[idx++];
105                         g = palette[idx++];
106                         b = palette[idx];
107
108                         int ofs = (y * 320 + x) * 3;
109                         image[ofs++] = r << 2;
110                         image[ofs++] = g << 2;
111                         image[ofs]   = b << 2;
112                 }
113
114                 ptr += FRAME_BUFFER_WIDTH;
115         }
116 }
117
118 void vga_set_hscroll_offset(int offset)
119 {
120         hscroll_offset = offset;
121 }
122
123 void vga_upload_palette(uint8_t *pal)
124 {
125         memcpy(palette, pal, 256 * 3);
126 }