[7c014d1] | 1 | /*
|
---|
[7dbf1f6] | 2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
[7c014d1] | 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 |
|
---|
[4122410] | 30 | /** @addtogroup output
|
---|
| 31 | * @{
|
---|
[7c014d1] | 32 | */
|
---|
| 33 |
|
---|
[9b2e20c] | 34 | #include <codepage/cp437.h>
|
---|
[7c014d1] | 35 | #include <errno.h>
|
---|
| 36 | #include <sysinfo.h>
|
---|
[6d5e378] | 37 | #include <align.h>
|
---|
| 38 | #include <as.h>
|
---|
[7c014d1] | 39 | #include <ddi.h>
|
---|
[6d5e378] | 40 | #include <io/chargrid.h>
|
---|
| 41 | #include "../output.h"
|
---|
[7c014d1] | 42 | #include "ega.h"
|
---|
| 43 |
|
---|
| 44 | #define EGA_IO_BASE ((ioport8_t *) 0x3d4)
|
---|
| 45 | #define EGA_IO_SIZE 2
|
---|
| 46 |
|
---|
[6d5e378] | 47 | #define FB_POS(x, y) (((y) * ega.cols + (x)) << 1)
|
---|
[7c014d1] | 48 |
|
---|
| 49 | typedef struct {
|
---|
[6d5e378] | 50 | sysarg_t cols;
|
---|
| 51 | sysarg_t rows;
|
---|
[a35b458] | 52 |
|
---|
[7c014d1] | 53 | uint8_t style_normal;
|
---|
| 54 | uint8_t style_inverted;
|
---|
[a35b458] | 55 |
|
---|
[6d5e378] | 56 | size_t size;
|
---|
| 57 | uint8_t *addr;
|
---|
[7c014d1] | 58 | } ega_t;
|
---|
| 59 |
|
---|
| 60 | static ega_t ega;
|
---|
| 61 |
|
---|
| 62 | static uint8_t attrs_attr(char_attrs_t attrs)
|
---|
| 63 | {
|
---|
| 64 | uint8_t attr = 0;
|
---|
[a35b458] | 65 |
|
---|
[7c014d1] | 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);
|
---|
[a35b458] | 86 |
|
---|
[7c014d1] | 87 | if (attrs.val.index.attr & CATTR_BRIGHT)
|
---|
| 88 | attr |= 0x08;
|
---|
[a35b458] | 89 |
|
---|
[7c014d1] | 90 | break;
|
---|
| 91 | case CHAR_ATTR_RGB:
|
---|
[7dbf1f6] | 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);
|
---|
[7c014d1] | 99 | break;
|
---|
| 100 | }
|
---|
[a35b458] | 101 |
|
---|
[7c014d1] | 102 | return attr;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[6d5e378] | 105 | /** Draw the character at the specified position.
|
---|
[7c014d1] | 106 | *
|
---|
[6d5e378] | 107 | * @param field Character field.
|
---|
| 108 | * @param col Horizontal screen position.
|
---|
| 109 | * @param row Vertical screen position.
|
---|
[7c014d1] | 110 | *
|
---|
| 111 | */
|
---|
[6d5e378] | 112 | static void draw_char(charfield_t *field, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 113 | {
|
---|
| 114 | uint8_t glyph;
|
---|
[9b2e20c] | 115 | errno_t rc;
|
---|
[a35b458] | 116 |
|
---|
[9b2e20c] | 117 | rc = cp437_encode(field->ch, &glyph);
|
---|
| 118 | if (rc != EOK)
|
---|
[7c014d1] | 119 | glyph = '?';
|
---|
[a35b458] | 120 |
|
---|
[7c014d1] | 121 | uint8_t attr = attrs_attr(field->attrs);
|
---|
[a35b458] | 122 |
|
---|
[6d5e378] | 123 | ega.addr[FB_POS(col, row)] = glyph;
|
---|
| 124 | ega.addr[FB_POS(col, row) + 1] = attr;
|
---|
[7c014d1] | 125 | }
|
---|
| 126 |
|
---|
[b7fd2a0] | 127 | static errno_t ega_yield(outdev_t *dev)
|
---|
[7c014d1] | 128 | {
|
---|
| 129 | return EOK;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[b7fd2a0] | 132 | static errno_t ega_claim(outdev_t *dev)
|
---|
[7c014d1] | 133 | {
|
---|
| 134 | return EOK;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[6d5e378] | 137 | static void ega_get_dimensions(outdev_t *dev, sysarg_t *cols, sysarg_t *rows)
|
---|
[7c014d1] | 138 | {
|
---|
[6d5e378] | 139 | *cols = ega.cols;
|
---|
| 140 | *rows = ega.rows;
|
---|
[7c014d1] | 141 | }
|
---|
| 142 |
|
---|
[6d5e378] | 143 | static console_caps_t ega_get_caps(outdev_t *dev)
|
---|
[7c014d1] | 144 | {
|
---|
| 145 | return (CONSOLE_CAP_STYLE | CONSOLE_CAP_INDEXED);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[6d5e378] | 148 | static void ega_cursor_update(outdev_t *dev, sysarg_t prev_col,
|
---|
[7c014d1] | 149 | sysarg_t prev_row, sysarg_t col, sysarg_t row, bool visible)
|
---|
| 150 | {
|
---|
| 151 | /* Cursor position */
|
---|
[6d5e378] | 152 | uint16_t cursor = row * ega.cols + col;
|
---|
[a35b458] | 153 |
|
---|
[7c014d1] | 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);
|
---|
[a35b458] | 158 |
|
---|
[7c014d1] | 159 | /* Cursor visibility */
|
---|
| 160 | pio_write_8(EGA_IO_BASE, 0x0a);
|
---|
| 161 | uint8_t stat = pio_read_8(EGA_IO_BASE + 1);
|
---|
[a35b458] | 162 |
|
---|
[7c014d1] | 163 | pio_write_8(EGA_IO_BASE, 0x0a);
|
---|
[a35b458] | 164 |
|
---|
[7c014d1] | 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 |
|
---|
[6d5e378] | 171 | static void ega_char_update(outdev_t *dev, sysarg_t col, sysarg_t row)
|
---|
[7c014d1] | 172 | {
|
---|
[6d5e378] | 173 | charfield_t *field =
|
---|
| 174 | chargrid_charfield_at(dev->backbuf, col, row);
|
---|
[a35b458] | 175 |
|
---|
[6d5e378] | 176 | draw_char(field, col, row);
|
---|
[7c014d1] | 177 | }
|
---|
| 178 |
|
---|
[bbc6277] | 179 | static void ega_flush(outdev_t *dev)
|
---|
| 180 | {
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[6d5e378] | 183 | static outdev_ops_t ega_ops = {
|
---|
[7c014d1] | 184 | .yield = ega_yield,
|
---|
| 185 | .claim = ega_claim,
|
---|
[6d5e378] | 186 | .get_dimensions = ega_get_dimensions,
|
---|
| 187 | .get_caps = ega_get_caps,
|
---|
| 188 | .cursor_update = ega_cursor_update,
|
---|
[bbc6277] | 189 | .char_update = ega_char_update,
|
---|
| 190 | .flush = ega_flush
|
---|
[7c014d1] | 191 | };
|
---|
| 192 |
|
---|
[b7fd2a0] | 193 | errno_t ega_init(void)
|
---|
[7c014d1] | 194 | {
|
---|
| 195 | sysarg_t present;
|
---|
[b7fd2a0] | 196 | errno_t rc = sysinfo_get_value("fb", &present);
|
---|
[7c014d1] | 197 | if (rc != EOK)
|
---|
| 198 | present = false;
|
---|
[a35b458] | 199 |
|
---|
[7c014d1] | 200 | if (!present)
|
---|
| 201 | return ENOENT;
|
---|
[a35b458] | 202 |
|
---|
[7c014d1] | 203 | sysarg_t kind;
|
---|
| 204 | rc = sysinfo_get_value("fb.kind", &kind);
|
---|
| 205 | if (rc != EOK)
|
---|
| 206 | kind = (sysarg_t) -1;
|
---|
[a35b458] | 207 |
|
---|
[7c014d1] | 208 | if (kind != 2)
|
---|
| 209 | return EINVAL;
|
---|
[a35b458] | 210 |
|
---|
[7c014d1] | 211 | sysarg_t paddr;
|
---|
| 212 | rc = sysinfo_get_value("fb.address.physical", &paddr);
|
---|
| 213 | if (rc != EOK)
|
---|
| 214 | return rc;
|
---|
[a35b458] | 215 |
|
---|
[6d5e378] | 216 | rc = sysinfo_get_value("fb.width", &ega.cols);
|
---|
[7c014d1] | 217 | if (rc != EOK)
|
---|
| 218 | return rc;
|
---|
[a35b458] | 219 |
|
---|
[6d5e378] | 220 | rc = sysinfo_get_value("fb.height", &ega.rows);
|
---|
[7c014d1] | 221 | if (rc != EOK)
|
---|
| 222 | return rc;
|
---|
[a35b458] | 223 |
|
---|
[1433ecda] | 224 | rc = pio_enable((void *)EGA_IO_BASE, EGA_IO_SIZE, NULL);
|
---|
[7c014d1] | 225 | if (rc != EOK)
|
---|
| 226 | return rc;
|
---|
[a35b458] | 227 |
|
---|
[6d5e378] | 228 | ega.size = (ega.cols * ega.rows) << 1;
|
---|
[bf9cb2f] | 229 | ega.addr = AS_AREA_ANY;
|
---|
[a35b458] | 230 |
|
---|
[8442d10] | 231 | rc = physmem_map(paddr,
|
---|
[fbcdeb8] | 232 | ALIGN_UP(ega.size, PAGE_SIZE) >> PAGE_WIDTH,
|
---|
| 233 | AS_AREA_READ | AS_AREA_WRITE, (void *) &ega.addr);
|
---|
[7c014d1] | 234 | if (rc != EOK)
|
---|
| 235 | return rc;
|
---|
[a35b458] | 236 |
|
---|
[7c014d1] | 237 | sysarg_t blinking;
|
---|
| 238 | rc = sysinfo_get_value("fb.blinking", &blinking);
|
---|
| 239 | if (rc != EOK)
|
---|
| 240 | blinking = false;
|
---|
[a35b458] | 241 |
|
---|
[7c014d1] | 242 | ega.style_normal = 0xf0;
|
---|
| 243 | ega.style_inverted = 0x0f;
|
---|
[a35b458] | 244 |
|
---|
[7c014d1] | 245 | if (blinking) {
|
---|
| 246 | ega.style_normal &= 0x77;
|
---|
| 247 | ega.style_inverted &= 0x77;
|
---|
| 248 | }
|
---|
[a35b458] | 249 |
|
---|
[6d5e378] | 250 | outdev_t *dev = outdev_register(&ega_ops, (void *) &ega);
|
---|
[7c014d1] | 251 | if (dev == NULL) {
|
---|
| 252 | as_area_destroy(ega.addr);
|
---|
| 253 | return EINVAL;
|
---|
| 254 | }
|
---|
[a35b458] | 255 |
|
---|
[7c014d1] | 256 | return EOK;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[6d5e378] | 259 | /** @}
|
---|
[7c014d1] | 260 | */
|
---|