]> Joshua Wise's Git repositories - netwatch.git/blame - video/text.c
Merge nyus.joshuawise.com:/storage/git/netwatch
[netwatch.git] / video / text.c
CommitLineData
cdde55f5
JW
1#include <io.h>
2#include <text.h>
3#include <paging.h>
4#include <minilib.h>
5#include <stdint.h>
74032dae
JW
6#include <output.h>
7#include <smram.h>
1deef7f3 8#include <crc32.h>
7e946b3c 9#include <video_defines.h>
cdde55f5
JW
10
11static unsigned char _font[256 * 32];
12
7e946b3c
JW
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
19static unsigned char vga_read(unsigned char idx)
20{
21 outb(CRTC_IDX_REG, idx);
22 return inb(CRTC_DATA_REG);
23}
24
25static 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
cdde55f5
JW
34/* Must be called from a firstrun context, where we don't care about saving
35 * 0x3CE state. */
36void text_init()
37{
38 unsigned char oldread;
7e946b3c 39
74032dae 40 smram_state_t old_state = smram_save_state();
826e2bd2
JW
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);
cdde55f5
JW
47 outb(0x3CE, 0x04 /* Read register */);
48 oldread = inb(0x3CF);
49 outb(0x3CF, 0x02 /* Font plane */);
7e946b3c 50
74032dae 51 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5 52 memcpy(_font, p2v(0xB8000), sizeof(_font));
74032dae 53 smram_restore_state(old_state);
7e946b3c 54
cdde55f5 55 outb(0x3CF, oldread);
826e2bd2
JW
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);
cdde55f5
JW
62}
63
66cd7e82 64void text_render(char *buf, int x, int y, int w, int h)
cdde55f5 65{
7e946b3c 66 unsigned char *video = (unsigned char *)vga_base();
cdde55f5 67 unsigned int textx = x / 9;
826e2bd2 68 unsigned int texty = y / 16;
cdde55f5
JW
69 unsigned int cx, cy;
70 unsigned char ch, at, font;
74032dae 71 smram_state_t old_state = smram_save_state();
0b67a02c 72/*
740eaa87 73 outputf("text_render: (%d,%d),(%d,%d)", x, y, w, h);
0b67a02c 74*/
74032dae 75 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5
JW
76 for (cy = y; cy < (y + h); cy++)
77 {
77671f96 78 cx = x;
826e2bd2 79 texty = cy / 16;
cdde55f5 80 textx = cx / 9;
7364b727
JW
81 ch = video[texty * 160 + textx * 2];
82 at = video[texty * 160 + textx * 2 + 1];
826e2bd2 83 font = _font[ch * 32 + cy % 16];
cdde55f5
JW
84 for (cx = x; cx < (x + w); cx++)
85 {
0013b64e 86 unsigned int pos = cx % 8;
cdde55f5
JW
87 if (pos == 0)
88 {
0013b64e 89 textx = cx / 8;
7364b727
JW
90 ch = video[texty * 160 + textx * 2];
91 at = video[texty * 160 + textx * 2 + 1];
826e2bd2 92 font = _font[ch * 32 + cy % 16];
cdde55f5
JW
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 }
66cd7e82 107 *(buf++) = 0;
cdde55f5
JW
108 }
109 }
74032dae 110 smram_restore_state(old_state);
cdde55f5
JW
111}
112
113uint32_t text_checksum(int x, int y, int w, int h)
114{
7e946b3c 115 unsigned char *video = (unsigned char *)vga_base();
0013b64e 116 unsigned int textx = x / 8;
826e2bd2 117 unsigned int texty = y / 16;
cdde55f5 118 int cx, cy;
cdde55f5 119 uint32_t cksm = 0;
74032dae
JW
120 smram_state_t old_state = smram_save_state();
121
74032dae 122 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5
JW
123
124 for (cy = y; cy < (y + h); cy++)
125 {
77671f96 126 cx = x;
826e2bd2 127 texty = cy / 16;
0013b64e
JW
128 textx = cx / 8;
129 cksm = crc32(video + texty * 160 + textx * 2, (w / 8) * 2 + 2, cksm); /* Err on the side of 'too many'. */
cdde55f5
JW
130 }
131
74032dae
JW
132 smram_restore_state(old_state);
133
cdde55f5
JW
134 return cksm;
135}
This page took 0.032664 seconds and 4 git commands to generate.