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