]> Joshua Wise's Git repositories - netwatch.git/blame - video/text.c
more licensing
[netwatch.git] / video / text.c
CommitLineData
98e930a2
JP
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
cdde55f5
JW
11#include <io.h>
12#include <text.h>
13#include <paging.h>
14#include <minilib.h>
15#include <stdint.h>
74032dae
JW
16#include <output.h>
17#include <smram.h>
1deef7f3 18#include <crc32.h>
7e946b3c 19#include <video_defines.h>
cdde55f5
JW
20
21static unsigned char _font[256 * 32];
22
7e946b3c
JW
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
29static unsigned char vga_read(unsigned char idx)
30{
31 outb(CRTC_IDX_REG, idx);
32 return inb(CRTC_DATA_REG);
33}
34
35static 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
cdde55f5
JW
44/* Must be called from a firstrun context, where we don't care about saving
45 * 0x3CE state. */
46void text_init()
47{
48 unsigned char oldread;
7e946b3c 49
74032dae 50 smram_state_t old_state = smram_save_state();
826e2bd2
JW
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);
cdde55f5
JW
57 outb(0x3CE, 0x04 /* Read register */);
58 oldread = inb(0x3CF);
59 outb(0x3CF, 0x02 /* Font plane */);
7e946b3c 60
74032dae 61 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5 62 memcpy(_font, p2v(0xB8000), sizeof(_font));
74032dae 63 smram_restore_state(old_state);
7e946b3c 64
cdde55f5 65 outb(0x3CF, oldread);
826e2bd2
JW
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);
cdde55f5
JW
72}
73
66cd7e82 74void text_render(char *buf, int x, int y, int w, int h)
cdde55f5 75{
7e946b3c 76 unsigned char *video = (unsigned char *)vga_base();
cdde55f5 77 unsigned int textx = x / 9;
826e2bd2 78 unsigned int texty = y / 16;
cdde55f5
JW
79 unsigned int cx, cy;
80 unsigned char ch, at, font;
74032dae 81 smram_state_t old_state = smram_save_state();
0b67a02c 82/*
740eaa87 83 outputf("text_render: (%d,%d),(%d,%d)", x, y, w, h);
0b67a02c 84*/
74032dae 85 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5
JW
86 for (cy = y; cy < (y + h); cy++)
87 {
77671f96 88 cx = x;
826e2bd2 89 texty = cy / 16;
cdde55f5 90 textx = cx / 9;
7364b727
JW
91 ch = video[texty * 160 + textx * 2];
92 at = video[texty * 160 + textx * 2 + 1];
826e2bd2 93 font = _font[ch * 32 + cy % 16];
cdde55f5
JW
94 for (cx = x; cx < (x + w); cx++)
95 {
0013b64e 96 unsigned int pos = cx % 8;
cdde55f5
JW
97 if (pos == 0)
98 {
0013b64e 99 textx = cx / 8;
7364b727
JW
100 ch = video[texty * 160 + textx * 2];
101 at = video[texty * 160 + textx * 2 + 1];
826e2bd2 102 font = _font[ch * 32 + cy % 16];
cdde55f5
JW
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 }
66cd7e82 117 *(buf++) = 0;
cdde55f5
JW
118 }
119 }
74032dae 120 smram_restore_state(old_state);
cdde55f5
JW
121}
122
123uint32_t text_checksum(int x, int y, int w, int h)
124{
7e946b3c 125 unsigned char *video = (unsigned char *)vga_base();
0013b64e 126 unsigned int textx = x / 8;
826e2bd2 127 unsigned int texty = y / 16;
cdde55f5 128 int cx, cy;
cdde55f5 129 uint32_t cksm = 0;
74032dae
JW
130 smram_state_t old_state = smram_save_state();
131
74032dae 132 smram_aseg_set_state(SMRAM_ASEG_SMMCODE);
cdde55f5
JW
133
134 for (cy = y; cy < (y + h); cy++)
135 {
77671f96 136 cx = x;
826e2bd2 137 texty = cy / 16;
0013b64e
JW
138 textx = cx / 8;
139 cksm = crc32(video + texty * 160 + textx * 2, (w / 8) * 2 + 2, cksm); /* Err on the side of 'too many'. */
cdde55f5
JW
140 }
141
74032dae
JW
142 smram_restore_state(old_state);
143
cdde55f5
JW
144 return cksm;
145}
This page took 0.036925 seconds and 4 git commands to generate.