]>
Commit | Line | Data |
---|---|---|
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 | |
11 | static 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 | ||
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 | ||
cdde55f5 JW |
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; | |
7e946b3c | 39 | |
74032dae | 40 | smram_state_t old_state = smram_save_state(); |
cdde55f5 JW |
41 | outb(0x3CE, 0x04 /* Read register */); |
42 | oldread = inb(0x3CF); | |
43 | outb(0x3CF, 0x02 /* Font plane */); | |
7e946b3c | 44 | |
74032dae | 45 | smram_aseg_set_state(SMRAM_ASEG_SMMCODE); |
cdde55f5 | 46 | memcpy(_font, p2v(0xB8000), sizeof(_font)); |
74032dae | 47 | smram_restore_state(old_state); |
7e946b3c | 48 | |
cdde55f5 JW |
49 | outb(0x3CF, oldread); |
50 | } | |
51 | ||
66cd7e82 | 52 | void text_render(char *buf, int x, int y, int w, int h) |
cdde55f5 | 53 | { |
7e946b3c | 54 | unsigned char *video = (unsigned char *)vga_base(); |
cdde55f5 JW |
55 | unsigned int textx = x / 9; |
56 | unsigned int texty = y / 14; | |
57 | unsigned int cx, cy; | |
58 | unsigned char ch, at, font; | |
74032dae | 59 | smram_state_t old_state = smram_save_state(); |
cdde55f5 | 60 | |
53b39ad0 | 61 | outputf("text_render: (%d,%d),(%d,%d)", buf, x, y, w, h); |
74032dae JW |
62 | |
63 | smram_aseg_set_state(SMRAM_ASEG_SMMCODE); | |
cdde55f5 JW |
64 | for (cy = y; cy < (y + h); cy++) |
65 | { | |
77671f96 | 66 | cx = x; |
cdde55f5 JW |
67 | texty = cy / 14; |
68 | textx = cx / 9; | |
7364b727 JW |
69 | ch = video[texty * 160 + textx * 2]; |
70 | at = video[texty * 160 + textx * 2 + 1]; | |
cdde55f5 JW |
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; | |
7364b727 JW |
78 | ch = video[texty * 160 + textx * 2]; |
79 | at = video[texty * 160 + textx * 2 + 1]; | |
cdde55f5 JW |
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 | } | |
66cd7e82 | 95 | *(buf++) = 0; |
cdde55f5 JW |
96 | } |
97 | } | |
74032dae | 98 | smram_restore_state(old_state); |
cdde55f5 JW |
99 | } |
100 | ||
101 | uint32_t text_checksum(int x, int y, int w, int h) | |
102 | { | |
7e946b3c | 103 | unsigned char *video = (unsigned char *)vga_base(); |
cdde55f5 JW |
104 | unsigned int textx = x / 9; |
105 | unsigned int texty = y / 14; | |
106 | int cx, cy; | |
cdde55f5 | 107 | uint32_t cksm = 0; |
74032dae JW |
108 | smram_state_t old_state = smram_save_state(); |
109 | ||
74032dae | 110 | smram_aseg_set_state(SMRAM_ASEG_SMMCODE); |
cdde55f5 JW |
111 | |
112 | for (cy = y; cy < (y + h); cy++) | |
113 | { | |
77671f96 | 114 | cx = x; |
cdde55f5 JW |
115 | texty = cy / 14; |
116 | textx = cx / 9; | |
f2da68c5 | 117 | cksm = crc32(video + texty * 160 + textx * 2, (w / 9) * 2 + 2, cksm); /* Err on the side of 'too many'. */ |
cdde55f5 JW |
118 | } |
119 | ||
74032dae JW |
120 | smram_restore_state(old_state); |
121 | ||
cdde55f5 JW |
122 | return cksm; |
123 | } |