vgabios: Extract out common parts of struct vgamode_s.
[seabios.git] / vgasrc / vgafb.c
index e2110ccfc1a2847f3ecfc43a211d1ac01394ca43..b6daba651ca26544caacae489610ab38571f1fcc 100644 (file)
@@ -7,7 +7,7 @@
 
 #include "biosvar.h" // GET_BDA
 #include "util.h" // memset_far
-#include "vgabios.h" // find_vga_entry
+#include "vgahw.h" // vgahw_find_mode
 #include "stdvga.h" // stdvga_grdc_write
 
 
@@ -41,16 +41,17 @@ static void
 scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
            , struct cursorpos ul, struct cursorpos lr)
 {
-    u8 cheight = GET_GLOBAL(vmode_g->cheight);
-    int stride = GET_BDA(video_cols);
+    int cheight = GET_GLOBAL(vmode_g->cheight);
+    int cwidth = 1;
+    int stride = GET_BDA(video_cols) * cwidth;
     void *src_far, *dest_far;
     if (nblines >= 0) {
-        dest_far = (void*)(ul.y * cheight * stride + ul.x);
+        dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
         src_far = dest_far + nblines * cheight * stride;
     } else {
         // Scroll down
         nblines = -nblines;
-        dest_far = (void*)(lr.y * cheight * stride + ul.x);
+        dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
         src_far = dest_far - nblines * cheight * stride;
         stride = -stride;
     }
@@ -58,13 +59,14 @@ scroll_pl4(struct vgamode_s *vmode_g, int nblines, int attr
     int rows = lr.y - ul.y + 1;
     if (nblines < rows) {
         stdvga_grdc_write(0x05, 0x01);
-        dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols, stride
-                                 , (rows - nblines) * cheight);
+        dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
+                                 , stride, (rows - nblines) * cheight);
     }
     if (attr < 0)
         attr = 0;
     stdvga_grdc_write(0x05, 0x02);
-    memset_stride(SEG_GRAPH, dest_far, attr, cols, stride, nblines * cheight);
+    memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
+                  , stride, nblines * cheight);
     stdvga_grdc_write(0x05, 0x00);
 }
 
@@ -72,33 +74,62 @@ static void
 scroll_cga(struct vgamode_s *vmode_g, int nblines, int attr
             , struct cursorpos ul, struct cursorpos lr)
 {
-    u8 cheight = GET_GLOBAL(vmode_g->cheight) / 2;
-    u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-    int stride = GET_BDA(video_cols) * bpp;
+    int cheight = GET_GLOBAL(vmode_g->cheight) / 2;
+    int cwidth = GET_GLOBAL(vmode_g->depth);
+    int stride = GET_BDA(video_cols) * cwidth;
     void *src_far, *dest_far;
     if (nblines >= 0) {
-        dest_far = (void*)(ul.y * cheight * stride + ul.x * bpp);
+        dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
         src_far = dest_far + nblines * cheight * stride;
     } else {
         // Scroll down
         nblines = -nblines;
-        dest_far = (void*)(lr.y * cheight * stride + ul.x * bpp);
+        dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
         src_far = dest_far - nblines * cheight * stride;
         stride = -stride;
     }
-    int cols = (lr.x - ul.x + 1) * bpp;
+    int cols = lr.x - ul.x + 1;
     int rows = lr.y - ul.y + 1;
     if (nblines < rows) {
-        memcpy_stride(SEG_CTEXT, dest_far + 0x2000, src_far + 0x2000, cols
+        memcpy_stride(SEG_CTEXT, dest_far+0x2000, src_far+0x2000, cols * cwidth
                       , stride, (rows - nblines) * cheight);
-        dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols
+        dest_far = memcpy_stride(SEG_CTEXT, dest_far, src_far, cols * cwidth
                                  , stride, (rows - nblines) * cheight);
     }
     if (attr < 0)
         attr = 0;
-    memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols
+    memset_stride(SEG_CTEXT, dest_far + 0x2000, attr, cols * cwidth
+                  , stride, nblines * cheight);
+    memset_stride(SEG_CTEXT, dest_far, attr, cols * cwidth
                   , stride, nblines * cheight);
-    memset_stride(SEG_CTEXT, dest_far, attr, cols
+}
+
+static void
+scroll_lin(struct vgamode_s *vmode_g, int nblines, int attr
+           , struct cursorpos ul, struct cursorpos lr)
+{
+    int cheight = 8;
+    int cwidth = 8;
+    int stride = GET_BDA(video_cols) * cwidth;
+    void *src_far, *dest_far;
+    if (nblines >= 0) {
+        dest_far = (void*)(ul.y * cheight * stride + ul.x * cwidth);
+        src_far = dest_far + nblines * cheight * stride;
+    } else {
+        // Scroll down
+        nblines = -nblines;
+        dest_far = (void*)(lr.y * cheight * stride + ul.x * cwidth);
+        src_far = dest_far - nblines * cheight * stride;
+        stride = -stride;
+    }
+    int cols = lr.x - ul.x + 1;
+    int rows = lr.y - ul.y + 1;
+    if (nblines < rows)
+        dest_far = memcpy_stride(SEG_GRAPH, dest_far, src_far, cols * cwidth
+                                 , stride, (rows - nblines) * cheight);
+    if (attr < 0)
+        attr = 0;
+    memset_stride(SEG_GRAPH, dest_far, attr, cols * cwidth
                   , stride, nblines * cheight);
 }
 
@@ -106,55 +137,56 @@ static void
 scroll_text(struct vgamode_s *vmode_g, int nblines, int attr
             , struct cursorpos ul, struct cursorpos lr)
 {
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-    void *src_far, *dest_far = (void*)SCREEN_MEM_START(nbcols, nbrows, ul.page);
-    int stride = nbcols * 2;
+    int cheight = 1;
+    int cwidth = 2;
+    int stride = GET_BDA(video_cols) * cwidth;
+    void *src_far, *dest_far = (void*)(GET_BDA(video_pagesize) * ul.page);
     if (nblines >= 0) {
-        dest_far += ul.y * stride + ul.x * 2;
-        src_far = dest_far + nblines * stride;
+        dest_far += ul.y * cheight * stride + ul.x * cwidth;
+        src_far = dest_far + nblines * cheight * stride;
     } else {
         // Scroll down
         nblines = -nblines;
-        dest_far += lr.y * stride + ul.x * 2;
-        src_far = dest_far - nblines * stride;
+        dest_far += lr.y * cheight * stride + ul.x * cwidth;
+        src_far = dest_far - nblines * cheight * stride;
         stride = -stride;
     }
-    int cols = (lr.x - ul.x + 1) * 2;
+    int cols = lr.x - ul.x + 1;
     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));
+        dest_far = memcpy_stride(seg, dest_far, src_far, cols * cwidth
+                                 , stride, (rows - nblines) * cheight);
     if (attr < 0)
         attr = 0x07;
     attr = (attr << 8) | ' ';
-    memset16_stride(seg, dest_far, attr, cols, stride, nblines);
+    memset16_stride(seg, dest_far, attr, cols * cwidth
+                    , stride, nblines * cheight);
 }
 
 void
 vgafb_scroll(int nblines, int attr, struct cursorpos ul, struct cursorpos lr)
 {
     // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
+    struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
     if (!vmode_g)
         return;
 
     // FIXME gfx mode not complete
     switch (GET_GLOBAL(vmode_g->memmodel)) {
-    case CTEXT:
-    case MTEXT:
+    case MM_TEXT:
         scroll_text(vmode_g, nblines, attr, ul, lr);
         break;
-    case PLANAR4:
-    case PLANAR1:
+    case MM_PLANAR:
         scroll_pl4(vmode_g, nblines, attr, ul, lr);
         break;
-    case CGA:
+    case MM_CGA:
         scroll_cga(vmode_g, nblines, attr, ul, lr);
         break;
-    default:
-        dprintf(1, "Scroll in graphics mode\n");
+    case MM_DIRECT:
+    case MM_PACKED:
+        scroll_lin(vmode_g, nblines, attr, ul, lr);
+        break;
     }
 }
 
@@ -219,7 +251,7 @@ write_gfx_char_cga(struct vgamode_s *vmode_g
         return;
 
     u8 *fdata_g = vgafont8;
-    u8 bpp = GET_GLOBAL(vmode_g->pixbits);
+    u8 bpp = GET_GLOBAL(vmode_g->depth);
     u16 addr = (cp.x * bpp) + cp.y * 320;
     u16 src = ca.car * 8;
     u8 i;
@@ -296,12 +328,9 @@ static void
 write_text_char(struct vgamode_s *vmode_g
                 , struct cursorpos cp, struct carattr ca)
 {
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
     // Compute the address
-    void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
+    u16 nbcols = GET_BDA(video_cols);
+    void *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
                                 + (cp.x + cp.y * nbcols) * 2);
 
     if (ca.use_attr) {
@@ -316,24 +345,23 @@ void
 vgafb_write_char(struct cursorpos cp, struct carattr ca)
 {
     // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
+    struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
     if (!vmode_g)
         return;
 
     // FIXME gfx mode not complete
     switch (GET_GLOBAL(vmode_g->memmodel)) {
-    case CTEXT:
-    case MTEXT:
+    case MM_TEXT:
         write_text_char(vmode_g, cp, ca);
         break;
-    case PLANAR4:
-    case PLANAR1:
+    case MM_PLANAR:
         write_gfx_char_pl4(vmode_g, cp, ca);
         break;
-    case CGA:
+    case MM_CGA:
         write_gfx_char_cga(vmode_g, cp, ca);
         break;
-    case LINEAR8:
+    case MM_DIRECT:
+    case MM_PACKED:
         write_gfx_char_lin(vmode_g, cp, ca);
         break;
     }
@@ -343,22 +371,19 @@ struct carattr
 vgafb_read_char(struct cursorpos cp)
 {
     // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
+    struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
     if (!vmode_g)
         goto fail;
 
-    if (!(GET_GLOBAL(vmode_g->memmodel) & TEXT)) {
+    if (GET_GLOBAL(vmode_g->memmodel) != MM_TEXT) {
         // FIXME gfx mode
         dprintf(1, "Read char in graphics mode\n");
         goto fail;
     }
 
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
     // Compute the address
-    u16 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, cp.page)
+    u16 nbcols = GET_BDA(video_cols);
+    u16 *address_far = (void*)(GET_BDA(video_pagesize) * cp.page
                                + (cp.x + cp.y * nbcols) * 2);
     u16 v = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
     struct carattr ca = {v, v>>8, 0};
@@ -378,16 +403,13 @@ void
 vgafb_write_pixel(u8 color, u16 x, u16 y)
 {
     // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
+    struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
     if (!vmode_g)
         return;
-    if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
-        return;
 
     u8 *addr_far, mask, attr, data;
     switch (GET_GLOBAL(vmode_g->memmodel)) {
-    case PLANAR4:
-    case PLANAR1:
+    case MM_PLANAR:
         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
         mask = 0x80 >> (x & 0x07);
         stdvga_grdc_write(0x08, mask);
@@ -400,15 +422,15 @@ vgafb_write_pixel(u8 color, u16 x, u16 y)
         stdvga_grdc_write(0x05, 0x00);
         stdvga_grdc_write(0x03, 0x00);
         break;
-    case CGA:
-        if (GET_GLOBAL(vmode_g->pixbits) == 2)
+    case MM_CGA:
+        if (GET_GLOBAL(vmode_g->depth) == 2)
             addr_far = (void*)((x >> 2) + (y >> 1) * 80);
         else
             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) {
+        if (GET_GLOBAL(vmode_g->depth) == 2) {
             attr = (color & 0x03) << ((3 - (x & 0x03)) * 2);
             mask = 0x03 << ((3 - (x & 0x03)) * 2);
         } else {
@@ -423,10 +445,13 @@ vgafb_write_pixel(u8 color, u16 x, u16 y)
         }
         SET_FARVAR(SEG_CTEXT, *addr_far, data);
         break;
-    case LINEAR8:
+    case MM_DIRECT:
+    case MM_PACKED:
         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
         SET_FARVAR(SEG_GRAPH, *addr_far, color);
         break;
+    case MM_TEXT:
+        return;
     }
 }
 
@@ -434,16 +459,13 @@ u8
 vgafb_read_pixel(u16 x, u16 y)
 {
     // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
+    struct vgamode_s *vmode_g = vgahw_find_mode(GET_BDA(video_mode));
     if (!vmode_g)
         return 0;
-    if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
-        return 0;
 
     u8 *addr_far, mask, attr=0, data, i;
     switch (GET_GLOBAL(vmode_g->memmodel)) {
-    case PLANAR4:
-    case PLANAR1:
+    case MM_PLANAR:
         addr_far = (void*)(x / 8 + y * GET_BDA(video_cols));
         mask = 0x80 >> (x & 0x07);
         attr = 0x00;
@@ -454,20 +476,23 @@ vgafb_read_pixel(u16 x, u16 y)
                 attr |= (0x01 << i);
         }
         break;
-    case CGA:
+    case MM_CGA:
         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)
+        if (GET_GLOBAL(vmode_g->depth) == 2)
             attr = (data >> ((3 - (x & 0x03)) * 2)) & 0x03;
         else
             attr = (data >> (7 - (x & 0x07))) & 0x01;
         break;
-    case LINEAR8:
+    case MM_DIRECT:
+    case MM_PACKED:
         addr_far = (void*)(x + y * (GET_BDA(video_cols) * 8));
         attr = GET_FARVAR(SEG_GRAPH, *addr_far);
         break;
+    case MM_TEXT:
+        return 0;
     }
     return attr;
 }