vgabios: Use vgamode_s instead of video_param_table in code.
[seabios.git] / vgasrc / vgafb.c
index 3e36b1cdf73f14fc1a6ab89f61bc3503a15e4497..1e1943dc2b6cb126b885a29c33d0f2ff1dbd5969 100644 (file)
 #include "util.h" // memset_far
 #include "vgatables.h" // find_vga_entry
 
-// TODO
-//  * extract hw code from framebuffer code
-//  * use clear_screen() in scroll code
-//  * normalize params (don't use AX/BX/CX/etc.)
-
-// XXX
-inline void
-memcpy16_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
-{
-    memcpy_far(d_seg, d_far, s_seg, s_far, len);
-}
-
 
 /****************************************************************
  * Screen scrolling
  ****************************************************************/
 
-static void
-vgamem_copy_pl4(u8 xstart, u8 ysrc, u8 ydest, u8 cols, u8 nbcols,
-                u8 cheight)
+static inline void *
+memcpy_stride(u16 seg, void *dst, void *src, int copylen, int stride, int lines)
 {
-    u16 src = ysrc * cheight * nbcols + xstart;
-    u16 dest = ydest * cheight * nbcols + xstart;
-    outw(0x0105, VGAREG_GRDC_ADDRESS);
-    u8 i;
-    for (i = 0; i < cheight; i++)
-        memcpy_far(SEG_GRAPH, (void*)(dest + i * nbcols)
-                   , SEG_GRAPH, (void*)(src + i * nbcols), cols);
-    outw(0x0005, VGAREG_GRDC_ADDRESS);
+    for (; lines; lines--, dst+=stride, src+=stride)
+        memcpy_far(seg, dst, seg, src, copylen);
+    return dst;
 }
 
-static void
-vgamem_fill_pl4(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
-                u8 attr)
+static inline void
+memset_stride(u16 seg, void *dst, u8 val, int setlen, int stride, int lines)
 {
-    u16 dest = ystart * cheight * nbcols + xstart;
-    outw(0x0205, VGAREG_GRDC_ADDRESS);
-    u8 i;
-    for (i = 0; i < cheight; i++)
-        memset_far(SEG_GRAPH, (void*)(dest + i * nbcols), attr, cols);
-    outw(0x0005, VGAREG_GRDC_ADDRESS);
+    for (; lines; lines--, dst+=stride)
+        memset_far(seg, dst, val, setlen);
+}
+
+static inline void
+memset16_stride(u16 seg, void *dst, u16 val, int setlen, int stride, int lines)
+{
+    for (; lines; lines--, dst+=stride)
+        memset16_far(seg, dst, val, setlen);
 }
 
 static void
 scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
            , struct cursorpos ul, struct cursorpos lr)
 {
-    if (attr < 0)
-        attr = 0;
-    u8 dir = SCROLL_UP;
-    if (nblines < 0) {
+    u8 cheight = GET_GLOBAL(vmode_g->cheight);
+    int stride = GET_BDA(video_cols);
+    void *src_far, *dest_far;
+    if (nblines >= 0) {
+        dest_far = (void*)(ul.y * cheight * stride + ul.x);
+        src_far = dest_far + nblines * cheight * stride;
+    } else {
+        // Scroll down
         nblines = -nblines;
-        dir = SCROLL_DOWN;
-    }
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-    if (nblines > nbrows)
-        nblines = 0;
-    u8 cols = lr.x - ul.x + 1;
-
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
-    if (nblines == 0 && ul.y == 0 && ul.x == 0 && lr.y == nbrows - 1
-        && lr.x == nbcols - 1) {
-        outw(0x0205, VGAREG_GRDC_ADDRESS);
-        memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
-                   nbrows * nbcols * cheight);
-        outw(0x0005, VGAREG_GRDC_ADDRESS);
-        return;
-    }
-    if (dir == SCROLL_UP) {
-        u16 i;
-        for (i = ul.y; i <= lr.y; i++)
-            if ((i + nblines > lr.y) || (nblines == 0))
-                vgamem_fill_pl4(ul.x, i, cols, nbcols, cheight,
-                                attr);
-            else
-                vgamem_copy_pl4(ul.x, i + nblines, i, cols,
-                                nbcols, cheight);
-        return;
+        dest_far = (void*)(lr.y * cheight * stride + ul.x);
+        src_far = dest_far - nblines * cheight * stride;
+        stride = -stride;
     }
-    u16 i;
-    for (i = lr.y; i >= ul.y; i--) {
-        if ((i < ul.y + nblines) || (nblines == 0))
-            vgamem_fill_pl4(ul.x, i, cols, nbcols, cheight,
-                            attr);
-        else
-            vgamem_copy_pl4(ul.x, i, i - nblines, cols,
-                            nbcols, cheight);
-        if (i > lr.y)
-            break;
+    int cols = lr.x - ul.x + 1;
+    int rows = lr.y - ul.y + 1;
+    if (nblines < rows) {
+        vgahw_grdc_write(0x05, 0x01);
+        dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols, stride
+                                 , (rows - nblines) * cheight);
     }
-}
-
-static void
-vgamem_copy_cga(u8 xstart, u8 ysrc, u8 ydest, u8 cols, u8 nbcols,
-                u8 cheight)
-{
-    u16 src = ((ysrc * cheight * nbcols) >> 1) + xstart;
-    u16 dest = ((ydest * cheight * nbcols) >> 1) + xstart;
-    u8 i;
-    for (i = 0; i < cheight; i++)
-        if (i & 1)
-            memcpy_far(SEG_CTEXT, (void*)(0x2000 + dest + (i >> 1) * nbcols)
-                       , SEG_CTEXT, (void*)(0x2000 + src + (i >> 1) * nbcols)
-                       , cols);
-        else
-            memcpy_far(SEG_CTEXT, (void*)(dest + (i >> 1) * nbcols)
-                       , SEG_CTEXT, (void*)(src + (i >> 1) * nbcols), cols);
-}
-
-static void
-vgamem_fill_cga(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
-                u8 attr)
-{
-    u16 dest = ((ystart * cheight * nbcols) >> 1) + xstart;
-    u8 i;
-    for (i = 0; i < cheight; i++)
-        if (i & 1)
-            memset_far(SEG_CTEXT, (void*)(0x2000 + dest + (i >> 1) * nbcols)
-                       , attr, cols);
-        else
-            memset_far(SEG_CTEXT, (void*)(dest + (i >> 1) * nbcols), attr, cols);
+    if (attr < 0)
+        attr = 0;
+    vgahw_grdc_write(0x05, 0x02);
+    memset_stride(SEG_GRAPH, dest_far, attr, cols, stride, nblines * cheight);
+    vgahw_grdc_write(0x05, 0x00);
 }
 
 static void
 scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
             , struct cursorpos ul, struct cursorpos lr)
 {
-    if (attr < 0)
-        attr = 0;
-    u8 dir = SCROLL_UP;
-    if (nblines < 0) {
-        nblines = -nblines;
-        dir = SCROLL_DOWN;
-    }
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-    if (nblines > nbrows)
-        nblines = 0;
-    u8 cols = lr.x - ul.x + 1;
-
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
+    u8 cheight = GET_GLOBAL(vmode_g->cheight);
     u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-    if (nblines == 0 && ul.y == 0 && ul.x == 0 && lr.y == nbrows - 1
-        && lr.x == nbcols - 1) {
-        memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
-                   nbrows * nbcols * cheight * bpp);
-        return;
-    }
-    if (bpp == 2) {
-        ul.x <<= 1;
-        cols <<= 1;
-        nbcols <<= 1;
-    }
-    // if Scroll up
-    if (dir == SCROLL_UP) {
-        u16 i;
-        for (i = ul.y; i <= lr.y; i++)
-            if ((i + nblines > lr.y) || (nblines == 0))
-                vgamem_fill_cga(ul.x, i, cols, nbcols, cheight,
-                                attr);
-            else
-                vgamem_copy_cga(ul.x, i + nblines, i, cols,
-                                nbcols, cheight);
-        return;
+    int stride = GET_BDA(video_cols) * bpp;
+    void *src_far, *dest_far;
+    if (nblines >= 0) {
+        dest_far = (void*)(ul.y * cheight * stride + ul.x * bpp);
+        src_far = dest_far + nblines * cheight * stride;
+    } else {
+        // Scroll down
+        nblines = -nblines;
+        dest_far = (void*)(lr.y * cheight * stride + ul.x * bpp);
+        src_far = dest_far - nblines * cheight * stride;
+        stride = -stride;
     }
-    u16 i;
-    for (i = lr.y; i >= ul.y; i--) {
-        if ((i < ul.y + nblines) || (nblines == 0))
-            vgamem_fill_cga(ul.x, i, cols, nbcols, cheight,
-                            attr);
-        else
-            vgamem_copy_cga(ul.x, i, i - nblines, cols,
-                            nbcols, cheight);
-        if (i > lr.y)
-            break;
+    int cols = (lr.x - ul.x + 1) * bpp;
+    int rows = lr.y - ul.y + 1;
+    if (nblines < rows) {
+        memcpy_stride(SEG_CTEXT, dest_far + 0x2000, src_far + 0x2000, cols
+                      , stride, (rows - nblines) * cheight / 2);
+        dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols
+                                 , stride, (rows - nblines) * cheight / 2);
     }
+    if (attr < 0)
+        attr = 0;
+    memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols
+                  , stride, nblines * cheight / 2);
+    memset_stride(SEG_CTEXT, dest_far, attr, cols
+                  , stride, nblines * cheight / 2);
 }
 
 static void
 scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
             , struct cursorpos ul, struct cursorpos lr)
 {
-    if (attr < 0)
-        attr = 0x07;
-    u8 dir = SCROLL_UP;
-    if (nblines < 0) {
-        nblines = -nblines;
-        dir = SCROLL_DOWN;
-    }
-    // Get the dimensions
     u16 nbrows = GET_BDA(video_rows) + 1;
     u16 nbcols = GET_BDA(video_cols);
-
-    if (nblines > nbrows)
-        nblines = 0;
-    u8 cols = lr.x - ul.x + 1;
-
-    // Compute the address
-    void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, ul.page));
-    dprintf(3, "Scroll, address %p (%d %d %02x)\n"
-            , address_far, nbrows, nbcols, ul.page);
-
-    if (nblines == 0 && ul.y == 0 && ul.x == 0 && lr.y == nbrows - 1
-        && lr.x == nbcols - 1) {
-        memset16_far(GET_GLOBAL(vmode_g->sstart), address_far
-                     , (u16)attr * 0x100 + ' ', nbrows * nbcols * 2);
-        return;
-    }
-    if (dir == SCROLL_UP) {
-        u16 i;
-        for (i = ul.y; i <= lr.y; i++)
-            if ((i + nblines > lr.y) || (nblines == 0))
-                memset16_far(GET_GLOBAL(vmode_g->sstart)
-                             , address_far + (i * nbcols + ul.x) * 2
-                             , (u16)attr * 0x100 + ' ', cols * 2);
-            else
-                memcpy16_far(GET_GLOBAL(vmode_g->sstart)
-                             , address_far + (i * nbcols + ul.x) * 2
-                             , GET_GLOBAL(vmode_g->sstart)
-                             , (void*)(((i + nblines) * nbcols + ul.x) * 2)
-                             , cols * 2);
-        return;
-    }
-
-    u16 i;
-    for (i = lr.y; i >= ul.y; i--) {
-        if ((i < ul.y + nblines) || (nblines == 0))
-            memset16_far(GET_GLOBAL(vmode_g->sstart)
-                         , address_far + (i * nbcols + ul.x) * 2
-                         , (u16)attr * 0x100 + ' ', cols * 2);
-        else
-            memcpy16_far(GET_GLOBAL(vmode_g->sstart)
-                         , address_far + (i * nbcols + ul.x) * 2
-                         , GET_GLOBAL(vmode_g->sstart)
-                         , (void*)(((i - nblines) * nbcols + ul.x) * 2)
-                         , cols * 2);
-        if (i > lr.y)
-            break;
+    void *src_far, *dest_far = (void*)SCREEN_MEM_START(nbcols, nbrows, ul.page);
+    int stride = nbcols * 2;
+    if (nblines >= 0) {
+        dest_far += ul.y * stride + ul.x * 2;
+        src_far = dest_far + nblines * stride;
+    } else {
+        // Scroll down
+        nblines = -nblines;
+        dest_far += lr.y * stride + ul.x * 2;
+        src_far = dest_far - nblines * stride;
+        stride = -stride;
     }
+    int cols = (lr.x - ul.x + 1) * 2;
+    int rows = lr.y - ul.y + 1;
+    u16 seg = GET_GLOBAL(vmode_g->sstart);
+    if (nblines < rows)
+        dest_far = memcpy_stride(seg, dest_far, src_far, cols, stride
+                                 , (rows - nblines));
+    if (attr < 0)
+        attr = 0x07;
+    attr = (attr << 8) | ' ';
+    memset16_stride(seg, dest_far, attr, cols, stride, nblines);
 }
 
 void
@@ -292,13 +168,10 @@ clear_screen(struct vgamode_s *vmode_g)
     case CGA:
         memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 32*1024);
         break;
-    default: {
-        outb(0x02, VGAREG_SEQU_ADDRESS);
-        u8 mmask = inb(VGAREG_SEQU_DATA);
-        outb(0x0f, VGAREG_SEQU_DATA);   // all planes
+    default:
+        // XXX - old code gets/sets/restores sequ register 2 to 0xf -
+        // but it should always be 0xf anyway.
         memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 64*1024);
-        outb(mmask, VGAREG_SEQU_DATA);
-    }
     }
 }
 
@@ -315,8 +188,7 @@ write_gfx_char_pl4(struct vgamode_s *vmode_g
     if (cp.x >= nbcols)
         return;
 
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
+    u8 cheight = GET_GLOBAL(vmode_g->cheight);
     u8 *fdata_g;
     switch (cheight) {
     case 14:
@@ -330,19 +202,19 @@ write_gfx_char_pl4(struct vgamode_s *vmode_g
     }
     u16 addr = cp.x + cp.y * cheight * nbcols;
     u16 src = ca.car * cheight;
-    outw(0x0f02, VGAREG_SEQU_ADDRESS);
-    outw(0x0205, VGAREG_GRDC_ADDRESS);
+    vgahw_sequ_write(0x02, 0x0f);
+    vgahw_grdc_write(0x05, 0x02);
     if (ca.attr & 0x80)
-        outw(0x1803, VGAREG_GRDC_ADDRESS);
+        vgahw_grdc_write(0x03, 0x18);
     else
-        outw(0x0003, VGAREG_GRDC_ADDRESS);
+        vgahw_grdc_write(0x03, 0x00);
     u8 i;
     for (i = 0; i < cheight; i++) {
         u8 *dest_far = (void*)(addr + i * nbcols);
         u8 j;
         for (j = 0; j < 8; j++) {
             u8 mask = 0x80 >> j;
-            outw((mask << 8) | 0x08, VGAREG_GRDC_ADDRESS);
+            vgahw_grdc_write(0x08, mask);
             GET_FARVAR(SEG_GRAPH, *dest_far);
             if (GET_GLOBAL(fdata_g[src + i]) & mask)
                 SET_FARVAR(SEG_GRAPH, *dest_far, ca.attr & 0x0f);
@@ -350,9 +222,9 @@ write_gfx_char_pl4(struct vgamode_s *vmode_g
                 SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
         }
     }
-    outw(0xff08, VGAREG_GRDC_ADDRESS);
-    outw(0x0005, VGAREG_GRDC_ADDRESS);
-    outw(0x0003, VGAREG_GRDC_ADDRESS);
+    vgahw_grdc_write(0x08, 0xff);
+    vgahw_grdc_write(0x05, 0x00);
+    vgahw_grdc_write(0x03, 0x00);
 }
 
 static void
@@ -520,7 +392,7 @@ fail: ;
  ****************************************************************/
 
 void
-biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
+vgafb_write_pixel(u8 color, u16 x, u16 y)
 {
     // Get the mode
     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
@@ -533,34 +405,34 @@ biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
     switch (GET_GLOBAL(vmode_g->memmodel)) {
     case PLANAR4:
     case PLANAR1:
-        addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
-        mask = 0x80 >> (CX & 0x07);
-        outw((mask << 8) | 0x08, VGAREG_GRDC_ADDRESS);
-        outw(0x0205, VGAREG_GRDC_ADDRESS);
+        addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
+        mask = 0x80 >> (x & 0x07);
+        vgahw_grdc_write(0x08, mask);
+        vgahw_grdc_write(0x05, 0x02);
         data = GET_FARVAR(SEG_GRAPH, *addr_far);
-        if (AL & 0x80)
-            outw(0x1803, VGAREG_GRDC_ADDRESS);
-        SET_FARVAR(SEG_GRAPH, *addr_far, AL);
-        outw(0xff08, VGAREG_GRDC_ADDRESS);
-        outw(0x0005, VGAREG_GRDC_ADDRESS);
-        outw(0x0003, VGAREG_GRDC_ADDRESS);
+        if (color & 0x80)
+            vgahw_grdc_write(0x03, 0x18);
+        SET_FARVAR(SEG_GRAPH, *addr_far, color);
+        vgahw_grdc_write(0x08, 0xff);
+        vgahw_grdc_write(0x05, 0x00);
+        vgahw_grdc_write(0x03, 0x00);
         break;
     case CGA:
         if (GET_GLOBAL(vmode_g->pixbits) == 2)
-            addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
+            addr_far = (void*)((x >> 2) + (y >> 1) * 80);
         else
-            addr_far = (void*)((CX >> 3) + (DX >> 1) * 80);
-        if (DX & 1)
+            addr_far = (void*)((x >> 3) + (y >> 1) * 80);
+        if (y & 1)
             addr_far += 0x2000;
         data = GET_FARVAR(SEG_CTEXT, *addr_far);
         if (GET_GLOBAL(vmode_g->pixbits) == 2) {
-            attr = (AL & 0x03) << ((3 - (CX & 0x03)) * 2);
-            mask = 0x03 << ((3 - (CX & 0x03)) * 2);
+            attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
+            mask = 0x03 << ((3 - (x & 0x03)) * 2);
         } else {
-            attr = (AL & 0x01) << (7 - (CX & 0x07));
-            mask = 0x01 << (7 - (CX & 0x07));
+            attr = (color & 0x01) << (7 - (x & 0x07));
+            mask = 0x01 << (7 - (x & 0x07));
         }
-        if (AL & 0x80) {
+        if (color & 0x80) {
             data ^= attr;
         } else {
             data &= ~mask;
@@ -569,52 +441,52 @@ biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
         SET_FARVAR(SEG_CTEXT, *addr_far, data);
         break;
     case LINEAR8:
-        addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
-        SET_FARVAR(SEG_GRAPH, *addr_far, AL);
+        addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
+        SET_FARVAR(SEG_GRAPH, *addr_far, color);
         break;
     }
 }
 
-void
-biosfn_read_pixel(u8 BH, u16 CX, u16 DX, u16 *AX)
+u8
+vgafb_read_pixel(u16 x, u16 y)
 {
     // Get the mode
     struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
     if (!vmode_g)
-        return;
+        return 0;
     if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
-        return;
+        return 0;
 
     u8 *addr_far, mask, attr=0, data, i;
     switch (GET_GLOBAL(vmode_g->memmodel)) {
     case PLANAR4:
     case PLANAR1:
-        addr_far = (void*)(CX / 8 + DX * GET_BDA(video_cols));
-        mask = 0x80 >> (CX & 0x07);
+        addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
+        mask = 0x80 >> (x & 0x07);
         attr = 0x00;
         for (i = 0; i < 4; i++) {
-            outw((i << 8) | 0x04, VGAREG_GRDC_ADDRESS);
+            vgahw_grdc_write(0x04, i);
             data = GET_FARVAR(SEG_GRAPH, *addr_far) & mask;
             if (data > 0)
                 attr |= (0x01 << i);
         }
         break;
     case CGA:
-        addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
-        if (DX & 1)
+        addr_far = (void*)((x >> 2) + (y >> 1) * 80);
+        if (y & 1)
             addr_far += 0x2000;
         data = GET_FARVAR(SEG_CTEXT, *addr_far);
         if (GET_GLOBAL(vmode_g->pixbits) == 2)
-            attr = (data >> ((3 - (CX & 0x03)) * 2)) & 0x03;
+            attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
         else
-            attr = (data >> (7 - (CX & 0x07))) & 0x01;
+            attr = (data >> (7 - (x & 0x07))) & 0x01;
         break;
     case LINEAR8:
-        addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
+        addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
         attr = GET_FARVAR(SEG_GRAPH, *addr_far);
         break;
     }
-    *AX = (*AX & 0xff00) | attr;
+    return attr;
 }