]> Joshua Wise's Git repositories - netwatch.git/blob - video/text.c
more licensing
[netwatch.git] / video / text.c
1 /* text.c
2  * Text console checksum and rendering functions
3  * NetWatch system management mode administration console
4  *
5  * Copyright (c) 2008 Jacob Potter and Joshua Wise.  All rights reserved.
6  * This program is free software; you can redistribute and/or modify it under
7  * the terms found in the file LICENSE in the root of this source tree.
8  *
9  */
10
11 #include <io.h>
12 #include <text.h>
13 #include <paging.h>
14 #include <minilib.h>
15 #include <stdint.h>
16 #include <output.h>
17 #include <smram.h>
18 #include <crc32.h>
19 #include <video_defines.h>
20
21 static unsigned char _font[256 * 32];
22
23 /* XXX reunify this with vga-overlay? */
24 #define VRAM_BASE               0xA0000UL
25 #define TEXT_CONSOLE_OFFSET     0x18000UL 
26
27 #define TEXT_CONSOLE_BASE       (VRAM_BASE + TEXT_CONSOLE_OFFSET)
28
29 static unsigned char vga_read(unsigned char idx)
30 {
31         outb(CRTC_IDX_REG, idx);
32         return inb(CRTC_DATA_REG);
33 }
34
35 static char * vga_base()
36 {
37         return (char *) (
38                 TEXT_CONSOLE_BASE
39                 + (((unsigned int) vga_read(CRTC_START_ADDR_MSB_IDX)) << 9)
40                 + (((unsigned int) vga_read(CRTC_START_ADDR_LSB_IDX)) << 1)
41         );
42 }
43
44 /* Must be called from a firstrun context, where we don't care about saving
45  * 0x3CE state. */
46 void text_init()
47 {
48         unsigned char oldread;
49
50         smram_state_t old_state = smram_save_state();
51         outb(0x3CE, 0x06 /* Miscellaneous */);
52         outb(0x3CF, 0x0C);
53         outb(0x3C4, 0x04 /* Seq memory mode */);
54         outb(0x3C5, inb(0x3C5) | 0x04);
55         outb(0x3CE, 0x05 /* Mode */);
56         outb(0x3CF, inb(0x3CF) & ~0x10);
57         outb(0x3CE, 0x04 /* Read register */);
58         oldread = inb(0x3CF);
59         outb(0x3CF, 0x02 /* Font plane */);
60
61         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
62         memcpy(_font, p2v(0xB8000), sizeof(_font));
63         smram_restore_state(old_state);
64
65         outb(0x3CF, oldread);
66         outb(0x3CE, 0x06 /* Miscellaneous */);
67         outb(0x3CF, 0x0E);
68         outb(0x3CE, 0x05 /* Mode */);
69         outb(0x3CF, inb(0x3CF) | 0x10);
70         outb(0x3C4, 0x04 /* Seq memory mode */);
71         outb(0x3C5, inb(0x3C5) & ~0x04);
72 }
73
74 void text_render(char *buf, int x, int y, int w, int h)
75 {
76         unsigned char *video = (unsigned char *)vga_base();
77         unsigned int textx = x / 9;
78         unsigned int texty = y / 16;
79         unsigned int cx, cy;
80         unsigned char ch, at, font;
81         smram_state_t old_state = smram_save_state();
82 /*
83         outputf("text_render: (%d,%d),(%d,%d)", x, y, w, h);
84 */      
85         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
86         for (cy = y; cy < (y + h); cy++)
87         {
88                 cx = x;
89                 texty = cy / 16;
90                 textx = cx / 9;
91                 ch = video[texty * 160 + textx * 2];
92                 at = video[texty * 160 + textx * 2 + 1];
93                 font = _font[ch * 32 + cy % 16];
94                 for (cx = x; cx < (x + w); cx++)
95                 {
96                         unsigned int pos = cx % 8;
97                         if (pos == 0)
98                         {
99                                 textx = cx / 8;
100                                 ch = video[texty * 160 + textx * 2];
101                                 at = video[texty * 160 + textx * 2 + 1];
102                                 font = _font[ch * 32 + cy % 16];
103                         }
104                         /* XXX always BGR888 */
105                         if (pos == 8)   /* 9th pixel is cloned */
106                                 pos = 7;
107                         if ((font >> (7 - pos)) & 1)
108                         {
109                                 *(buf++) = (at & 0x01) ? 0xFF : 0x00;
110                                 *(buf++) = (at & 0x02) ? 0xFF : 0x00;
111                                 *(buf++) = (at & 0x04) ? 0xFF : 0x00;
112                         } else {
113                                 *(buf++) = (at & 0x10) ? 0xFF : 0x00;
114                                 *(buf++) = (at & 0x20) ? 0xFF : 0x00;
115                                 *(buf++) = (at & 0x40) ? 0xFF : 0x00;
116                         }
117                         *(buf++) = 0;
118                 }
119         }
120         smram_restore_state(old_state);
121 }
122
123 uint32_t text_checksum(int x, int y, int w, int h)
124 {
125         unsigned char *video = (unsigned char *)vga_base();
126         unsigned int textx = x / 8;
127         unsigned int texty = y / 16;
128         int cx, cy;
129         uint32_t cksm = 0;
130         smram_state_t old_state = smram_save_state();
131         
132         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
133         
134         for (cy = y; cy < (y + h); cy++)
135         {
136                 cx = x;
137                 texty = cy / 16;
138                 textx = cx / 8;
139                 cksm = crc32(video + texty * 160 + textx * 2, (w / 8) * 2 + 2, cksm);   /* Err on the side of 'too many'. */
140         }
141         
142         smram_restore_state(old_state);
143         
144         return cksm;
145 }
This page took 0.031985 seconds and 4 git commands to generate.