]> Joshua Wise's Git repositories - netwatch.git/blob - video/text.c
172b84e002f536d02df4430794190e97ea76f4b5
[netwatch.git] / video / text.c
1 #include <io.h>
2 #include <text.h>
3 #include <paging.h>
4 #include <minilib.h>
5 #include <stdint.h>
6 #include <output.h>
7 #include <smram.h>
8 #include <crc32.h>
9 #include <video_defines.h>
10
11 static unsigned char _font[256 * 32];
12
13 /* XXX reunify this with vga-overlay? */
14 #define VRAM_BASE               0xA0000UL
15 #define TEXT_CONSOLE_OFFSET     0x18000UL 
16
17 #define TEXT_CONSOLE_BASE       (VRAM_BASE + TEXT_CONSOLE_OFFSET)
18
19 static unsigned char vga_read(unsigned char idx)
20 {
21         outb(CRTC_IDX_REG, idx);
22         return inb(CRTC_DATA_REG);
23 }
24
25 static char * vga_base()
26 {
27         return (char *) (
28                 TEXT_CONSOLE_BASE
29                 + (((unsigned int) vga_read(CRTC_START_ADDR_MSB_IDX)) << 9)
30                 + (((unsigned int) vga_read(CRTC_START_ADDR_LSB_IDX)) << 1)
31         );
32 }
33
34 /* Must be called from a firstrun context, where we don't care about saving
35  * 0x3CE state. */
36 void text_init()
37 {
38         unsigned char oldread;
39
40         smram_state_t old_state = smram_save_state();
41         outb(0x3CE, 0x04 /* Read register */);
42         oldread = inb(0x3CF);
43         outb(0x3CF, 0x02 /* Font plane */);
44
45         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
46         memcpy(_font, p2v(0xB8000), sizeof(_font));
47         smram_restore_state(old_state);
48
49         outb(0x3CF, oldread);
50 }
51
52 void text_render(char *buf, int x, int y, int w, int h)
53 {
54         unsigned char *video = (unsigned char *)vga_base();
55         unsigned int textx = x / 9;
56         unsigned int texty = y / 14;
57         unsigned int cx, cy;
58         unsigned char ch, at, font;
59         smram_state_t old_state = smram_save_state();
60         
61         outputf("text_render: (%d,%d),(%d,%d)", x, y, w, h);
62         
63         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
64         for (cy = y; cy < (y + h); cy++)
65         {
66                 cx = x;
67                 texty = cy / 14;
68                 textx = cx / 9;
69                 ch = video[texty * 160 + textx * 2];
70                 at = video[texty * 160 + textx * 2 + 1];
71                 font = _font[ch * 32 + cy % 14];
72                 for (cx = x; cx < (x + w); cx++)
73                 {
74                         unsigned int pos = cx % 9;
75                         if (pos == 0)
76                         {
77                                 textx = cx / 9;
78                                 ch = video[texty * 160 + textx * 2];
79                                 at = video[texty * 160 + textx * 2 + 1];
80                                 font = _font[ch * 32 + cy % 14];
81                         }
82                         /* XXX always BGR888 */
83                         if (pos == 8)   /* 9th pixel is cloned */
84                                 pos = 7;
85                         if ((font >> (7 - pos)) & 1)
86                         {
87                                 *(buf++) = (at & 0x01) ? 0xFF : 0x00;
88                                 *(buf++) = (at & 0x02) ? 0xFF : 0x00;
89                                 *(buf++) = (at & 0x04) ? 0xFF : 0x00;
90                         } else {
91                                 *(buf++) = (at & 0x10) ? 0xFF : 0x00;
92                                 *(buf++) = (at & 0x20) ? 0xFF : 0x00;
93                                 *(buf++) = (at & 0x40) ? 0xFF : 0x00;
94                         }
95                         *(buf++) = 0;
96                 }
97         }
98         smram_restore_state(old_state);
99 }
100
101 uint32_t text_checksum(int x, int y, int w, int h)
102 {
103         unsigned char *video = (unsigned char *)vga_base();
104         unsigned int textx = x / 9;
105         unsigned int texty = y / 14;
106         int cx, cy;
107         uint32_t cksm = 0;
108         smram_state_t old_state = smram_save_state();
109         
110         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
111         
112         for (cy = y; cy < (y + h); cy++)
113         {
114                 cx = x;
115                 texty = cy / 14;
116                 textx = cx / 9;
117                 cksm = crc32(video + texty * 160 + textx * 2, (w / 9) * 2 + 2, cksm);   /* Err on the side of 'too many'. */
118         }
119         
120         smram_restore_state(old_state);
121         
122         return cksm;
123 }
This page took 0.021294 seconds and 2 git commands to generate.