]> Joshua Wise's Git repositories - netwatch.git/blob - video/text.c
4f35f97b34822f857a0e478d06d0bf9fdef11a3a
[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, 0x06 /* Miscellaneous */);
42         outb(0x3CF, 0x0C);
43         outb(0x3C4, 0x04 /* Seq memory mode */);
44         outb(0x3C5, inb(0x3C5) | 0x04);
45         outb(0x3CE, 0x05 /* Mode */);
46         outb(0x3CF, inb(0x3CF) & ~0x10);
47         outb(0x3CE, 0x04 /* Read register */);
48         oldread = inb(0x3CF);
49         outb(0x3CF, 0x02 /* Font plane */);
50
51         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
52         memcpy(_font, p2v(0xB8000), sizeof(_font));
53         smram_restore_state(old_state);
54
55         outb(0x3CF, oldread);
56         outb(0x3CE, 0x06 /* Miscellaneous */);
57         outb(0x3CF, 0x0E);
58         outb(0x3CE, 0x05 /* Mode */);
59         outb(0x3CF, inb(0x3CF) | 0x10);
60         outb(0x3C4, 0x04 /* Seq memory mode */);
61         outb(0x3C5, inb(0x3C5) & ~0x04);
62 }
63
64 void text_render(char *buf, int x, int y, int w, int h)
65 {
66         unsigned char *video = (unsigned char *)vga_base();
67         unsigned int textx = x / 9;
68         unsigned int texty = y / 16;
69         unsigned int cx, cy;
70         unsigned char ch, at, font;
71         smram_state_t old_state = smram_save_state();
72 /*
73         outputf("text_render: (%d,%d),(%d,%d)", x, y, w, h);
74 */      
75         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
76         for (cy = y; cy < (y + h); cy++)
77         {
78                 cx = x;
79                 texty = cy / 16;
80                 textx = cx / 9;
81                 ch = video[texty * 160 + textx * 2];
82                 at = video[texty * 160 + textx * 2 + 1];
83                 font = _font[ch * 32 + cy % 16];
84                 for (cx = x; cx < (x + w); cx++)
85                 {
86                         unsigned int pos = cx % 8;
87                         if (pos == 0)
88                         {
89                                 textx = cx / 8;
90                                 ch = video[texty * 160 + textx * 2];
91                                 at = video[texty * 160 + textx * 2 + 1];
92                                 font = _font[ch * 32 + cy % 16];
93                         }
94                         /* XXX always BGR888 */
95                         if (pos == 8)   /* 9th pixel is cloned */
96                                 pos = 7;
97                         if ((font >> (7 - pos)) & 1)
98                         {
99                                 *(buf++) = (at & 0x01) ? 0xFF : 0x00;
100                                 *(buf++) = (at & 0x02) ? 0xFF : 0x00;
101                                 *(buf++) = (at & 0x04) ? 0xFF : 0x00;
102                         } else {
103                                 *(buf++) = (at & 0x10) ? 0xFF : 0x00;
104                                 *(buf++) = (at & 0x20) ? 0xFF : 0x00;
105                                 *(buf++) = (at & 0x40) ? 0xFF : 0x00;
106                         }
107                         *(buf++) = 0;
108                 }
109         }
110         smram_restore_state(old_state);
111 }
112
113 uint32_t text_checksum(int x, int y, int w, int h)
114 {
115         unsigned char *video = (unsigned char *)vga_base();
116         unsigned int textx = x / 8;
117         unsigned int texty = y / 16;
118         int cx, cy;
119         uint32_t cksm = 0;
120         smram_state_t old_state = smram_save_state();
121         
122         smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
123         
124         for (cy = y; cy < (y + h); cy++)
125         {
126                 cx = x;
127                 texty = cy / 16;
128                 textx = cx / 8;
129                 cksm = crc32(video + texty * 160 + textx * 2, (w / 8) * 2 + 2, cksm);   /* Err on the side of 'too many'. */
130         }
131         
132         smram_restore_state(old_state);
133         
134         return cksm;
135 }
This page took 0.021445 seconds and 2 git commands to generate.