source: mainline/uspace/srv/hid/output/port/ega.c@ 6a753a9c

Last change on this file since 6a753a9c was bdf06ad, checked in by Jiri Svoboda <jiri@…>, 18 months ago

Fix ccheck.

  • Property mode set to 100644
File size: 6.0 KB
Line 
1/*
2 * Copyright (c) 2024 Jiri Svoboda
3 * Copyright (c) 2011 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup output
31 * @{
32 */
33
34#include <codepage/cp437.h>
35#include <errno.h>
36#include <sysinfo.h>
37#include <align.h>
38#include <as.h>
39#include <ddi.h>
40#include <io/chargrid.h>
41#include "../output.h"
42#include "ega.h"
43
44#define EGA_IO_BASE ((ioport8_t *) 0x3d4)
45#define EGA_IO_SIZE 2
46
47#define FB_POS(x, y) (((y) * ega.cols + (x)) << 1)
48
49typedef struct {
50 sysarg_t cols;
51 sysarg_t rows;
52
53 uint8_t style_normal;
54 uint8_t style_inverted;
55
56 size_t size;
57 uint8_t *addr;
58} ega_t;
59
60static ega_t ega;
61
62static uint8_t attrs_attr(char_attrs_t attrs)
63{
64 uint8_t attr = 0;
65
66 switch (attrs.type) {
67 case CHAR_ATTR_STYLE:
68 switch (attrs.val.style) {
69 case STYLE_NORMAL:
70 attr = ega.style_normal;
71 break;
72 case STYLE_EMPHASIS:
73 attr = (ega.style_normal | 0x04);
74 break;
75 case STYLE_INVERTED:
76 attr = ega.style_inverted;
77 break;
78 case STYLE_SELECTED:
79 attr = (ega.style_inverted | 0x40);
80 break;
81 }
82 break;
83 case CHAR_ATTR_INDEX:
84 attr = ((attrs.val.index.bgcolor & 7) << 4) |
85 (attrs.val.index.fgcolor & 7);
86
87 if (attrs.val.index.attr & CATTR_BRIGHT)
88 attr |= 0x08;
89
90 break;
91 case CHAR_ATTR_RGB:
92 attr =
93 ((RED(attrs.val.rgb.fgcolor) >= 0x80) ? 0x40 : 0) |
94 ((GREEN(attrs.val.rgb.fgcolor) >= 0x80) ? 0x20 : 0) |
95 ((BLUE(attrs.val.rgb.fgcolor) >= 0x80) ? 0x10 : 0) |
96 ((RED(attrs.val.rgb.bgcolor) >= 0x80) ? 0x04 : 0) |
97 ((GREEN(attrs.val.rgb.bgcolor) >= 0x80) ? 0x02 : 0) |
98 ((BLUE(attrs.val.rgb.bgcolor) >= 0x80) ? 0x01 : 0);
99 break;
100 }
101
102 return attr;
103}
104
105/** Draw the character at the specified position.
106 *
107 * @param field Character field.
108 * @param col Horizontal screen position.
109 * @param row Vertical screen position.
110 *
111 */
112static void draw_char(charfield_t *field, sysarg_t col, sysarg_t row)
113{
114 uint8_t glyph;
115 errno_t rc;
116
117 rc = cp437_encode(field->ch, &glyph);
118 if (rc != EOK)
119 glyph = '?';
120
121 uint8_t attr = attrs_attr(field->attrs);
122
123 ega.addr[FB_POS(col, row)] = glyph;
124 ega.addr[FB_POS(col, row) + 1] = attr;
125}
126
127static errno_t ega_yield(outdev_t *dev)
128{
129 return EOK;
130}
131
132static errno_t ega_claim(outdev_t *dev)
133{
134 return EOK;
135}
136
137static void ega_get_dimensions(outdev_t *dev, sysarg_t *cols, sysarg_t *rows)
138{
139 *cols = ega.cols;
140 *rows = ega.rows;
141}
142
143static console_caps_t ega_get_caps(outdev_t *dev)
144{
145 return (CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED);
146}
147
148static void ega_cursor_update(outdev_t *dev, sysarg_t prev_col,
149 sysarg_t prev_row, sysarg_t col, sysarg_t row, bool visible)
150{
151 /* Cursor position */
152 uint16_t cursor = row * ega.cols + col;
153
154 pio_write_8(EGA_IO_BASE, 0x0e);
155 pio_write_8(EGA_IO_BASE + 1, (cursor >> 8) & 0xff);
156 pio_write_8(EGA_IO_BASE, 0x0f);
157 pio_write_8(EGA_IO_BASE + 1, cursor & 0xff);
158
159 /* Cursor visibility */
160 pio_write_8(EGA_IO_BASE, 0x0a);
161 uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
162
163 pio_write_8(EGA_IO_BASE, 0x0a);
164
165 if (visible)
166 pio_write_8(EGA_IO_BASE + 1, stat & (~(1 << 5)));
167 else
168 pio_write_8(EGA_IO_BASE + 1, stat | (1 << 5));
169}
170
171static void ega_char_update(outdev_t *dev, sysarg_t col, sysarg_t row)
172{
173 charfield_t *field =
174 chargrid_charfield_at(dev->backbuf, col, row);
175
176 draw_char(field, col, row);
177}
178
179static void ega_flush(outdev_t *dev)
180{
181}
182
183static outdev_ops_t ega_ops = {
184 .yield = ega_yield,
185 .claim = ega_claim,
186 .get_dimensions = ega_get_dimensions,
187 .get_caps = ega_get_caps,
188 .cursor_update = ega_cursor_update,
189 .char_update = ega_char_update,
190 .flush = ega_flush
191};
192
193errno_t ega_init(void)
194{
195 sysarg_t present;
196 errno_t rc = sysinfo_get_value("fb", &present);
197 if (rc != EOK)
198 present = false;
199
200 if (!present)
201 return ENOENT;
202
203 sysarg_t kind;
204 rc = sysinfo_get_value("fb.kind", &kind);
205 if (rc != EOK)
206 kind = (sysarg_t) -1;
207
208 if (kind != 2)
209 return EINVAL;
210
211 sysarg_t paddr;
212 rc = sysinfo_get_value("fb.address.physical", &paddr);
213 if (rc != EOK)
214 return rc;
215
216 rc = sysinfo_get_value("fb.width", &ega.cols);
217 if (rc != EOK)
218 return rc;
219
220 rc = sysinfo_get_value("fb.height", &ega.rows);
221 if (rc != EOK)
222 return rc;
223
224 rc = pio_enable((void *)EGA_IO_BASE, EGA_IO_SIZE, NULL);
225 if (rc != EOK)
226 return rc;
227
228 ega.size = (ega.cols * ega.rows) << 1;
229 ega.addr = AS_AREA_ANY;
230
231 rc = physmem_map(paddr,
232 ALIGN_UP(ega.size, PAGE_SIZE) >> PAGE_WIDTH,
233 AS_AREA_READ | AS_AREA_WRITE, (void *) &ega.addr);
234 if (rc != EOK)
235 return rc;
236
237 sysarg_t blinking;
238 rc = sysinfo_get_value("fb.blinking", &blinking);
239 if (rc != EOK)
240 blinking = false;
241
242 ega.style_normal = 0xf0;
243 ega.style_inverted = 0x0f;
244
245 if (blinking) {
246 ega.style_normal &= 0x77;
247 ega.style_inverted &= 0x77;
248 }
249
250 outdev_t *dev = outdev_register(&ega_ops, (void *) &ega);
251 if (dev == NULL) {
252 as_area_destroy(ega.addr);
253 return EINVAL;
254 }
255
256 return EOK;
257}
258
259/** @}
260 */
Note: See TracBrowser for help on using the repository browser.