vgabios: Unify page size calculations; remove page size from vgamode_s.

The previous code could obtain the page size in different ways - from
vmode_g->sslength, from SCREEN_MEM_START, or by manually multiplying
cols and rows.  Add a new func (calc_page_size) and use that in areas
that need to calculate the page size.

Convert readers of the page size to read it from the "video_pagesize"
entry in the BDA instead of recalculating it.

Remove the page size from struct vgamode_s (slength) as it is now
calculated dynamically.  The new calculated versions are different
from the existing values exported in video_param_table.  However, the
existing values (for CGA) did not look correct - I compared the values
to those exported by two other VGA BIOS manufacturers and used the
values that look more standard.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
diff --git a/vgasrc/stdvga.h b/vgasrc/stdvga.h
index 735022c..2aabf1b 100644
--- a/vgasrc/stdvga.h
+++ b/vgasrc/stdvga.h
@@ -53,7 +53,6 @@
     u8 cwidth;
     u8 cheight;
     u16 sstart;
-    u16 slength;
 
     u8 pelmask;
     u8 *dac;
diff --git a/vgasrc/vgabios.c b/vgasrc/vgabios.c
index 31636cc..854ed38 100644
--- a/vgasrc/vgabios.c
+++ b/vgasrc/vgabios.c
@@ -48,6 +48,19 @@
  * Helper functions
  ****************************************************************/
 
+u16
+calc_page_size(u8 memmodel, u16 width, u16 height)
+{
+    switch (memmodel) {
+    case MM_TEXT:
+        return ALIGN(width * height * 2, 2*1024);
+    case MM_CGA:
+        return 16*1024;
+    default:
+        return ALIGN(width * height / 8, 8*1024);
+    }
+}
+
 static void
 set_cursor_shape(u8 start, u8 end)
 {
@@ -93,15 +106,12 @@
     if (cp.page != current)
         return;
 
-    // Get the dimensions
+    // Calculate the memory address
     u16 nbcols = GET_BDA(video_cols);
-    u16 nbrows = GET_BDA(video_rows) + 1;
+    u16 address = (GET_BDA(video_pagesize) * cp.page
+                   + (cp.x + cp.y * nbcols) * 2);
 
-    // Calculate the address knowing nbcols nbrows and page num
-    u16 address = (SCREEN_IO_START(nbcols, nbrows, cp.page)
-                   + cp.x + cp.y * nbcols);
-
-    stdvga_set_cursor_pos(address);
+    stdvga_set_cursor_pos(address / 2);
 }
 
 static struct cursorpos
@@ -134,25 +144,13 @@
     // Get pos curs pos for the right page
     struct cursorpos cp = get_cursor_pos(page);
 
-    u16 address;
-    if (GET_GLOBAL(vmode_g->memmodel) == MM_TEXT) {
-        // Get the dimensions
-        u16 nbcols = GET_BDA(video_cols);
-        u16 nbrows = GET_BDA(video_rows) + 1;
-
-        // Calculate the address knowing nbcols nbrows and page num
-        address = SCREEN_MEM_START(nbcols, nbrows, page);
-        SET_BDA(video_pagestart, address);
-
-        // Start address
-        address = SCREEN_IO_START(nbcols, nbrows, page);
-    } else {
-        address = page * GET_GLOBAL(vmode_g->slength);
-    }
-
-    stdvga_set_active_page(address);
+    // Calculate memory address of start of page
+    u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
+    u16 address = GET_BDA(video_pagesize) * page;
+    stdvga_set_active_page(memmodel == MM_TEXT ? address / 2 : address);
 
     // And change the BIOS page
+    SET_BDA(video_pagestart, address);
     SET_BDA(video_page, page);
 
     dprintf(1, "Set active page %02x address %04x\n", page, address);
@@ -174,7 +172,7 @@
     u8 rows = vde / lines;
     SET_BDA(video_rows, rows - 1);
     u16 cols = GET_BDA(video_cols);
-    SET_BDA(video_pagesize, rows * cols * 2);
+    SET_BDA(video_pagesize, calc_page_size(MM_TEXT, cols, rows));
 }
 
 
@@ -331,9 +329,10 @@
     // Set the BIOS mem
     int width = GET_GLOBAL(vmode_g->width);
     int height = GET_GLOBAL(vmode_g->height);
+    u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
     int cheight = GET_GLOBAL(vmode_g->cheight);
     SET_BDA(video_mode, mode);
-    if (GET_GLOBAL(vmode_g->memmodel) == MM_TEXT) {
+    if (memmodel == MM_TEXT) {
         SET_BDA(video_cols, width);
         SET_BDA(video_rows, height-1);
         SET_BDA(cursor_type, 0x0607);
@@ -343,7 +342,7 @@
         SET_BDA(video_rows, (height / cheight) - 1);
         SET_BDA(cursor_type, 0x0000);
     }
-    SET_BDA(video_pagesize, GET_GLOBAL(vmode_g->slength));
+    SET_BDA(video_pagesize, calc_page_size(memmodel, width, height));
     SET_BDA(crtc_address, stdvga_get_crtc());
     SET_BDA(char_height, cheight);
     SET_BDA(video_ctl, 0x60 | (flags & MF_NOCLEARMEM ? 0x80 : 0x00));
diff --git a/vgasrc/vgabios.h b/vgasrc/vgabios.h
index 7c5e8d3..8b806ff 100644
--- a/vgasrc/vgabios.h
+++ b/vgasrc/vgabios.h
@@ -4,9 +4,6 @@
 #include "types.h" // u8
 #include "farptr.h" // struct segoff_s
 
-#define SCREEN_IO_START(x,y,p) (((((x)*(y)) | 0x00ff) + 1) * (p))
-#define SCREEN_MEM_START(x,y,p) SCREEN_IO_START(((x)*2),(y),(p))
-
 struct saveBDAstate {
     u8 video_mode;
     u16 video_cols;
@@ -66,6 +63,7 @@
 struct cursorpos {
     u8 x, y, page;
 };
+u16 calc_page_size(u8 memmodel, u16 width, u16 height);
 void modeswitch_set_bda(int mode, int flags, struct vgamode_s *vmode_g);
 
 // vgafb.c
diff --git a/vgasrc/vgafb.c b/vgasrc/vgafb.c
index eb4ced8..b933738 100644
--- a/vgasrc/vgafb.c
+++ b/vgasrc/vgafb.c
@@ -139,10 +139,8 @@
 {
     int cheight = 1;
     int cwidth = 2;
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-    int stride = nbcols * cwidth;
-    void *src_far, *dest_far = (void*)SCREEN_MEM_START(nbcols, nbrows, ul.page);
+    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 * cheight * stride + ul.x * cwidth;
         src_far = dest_far + nblines * cheight * stride;
@@ -330,12 +328,9 @@
 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) {
@@ -386,12 +381,9 @@
         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};
diff --git a/vgasrc/vgatables.c b/vgasrc/vgatables.c
index 314222e..4fd9964 100644
--- a/vgasrc/vgatables.c
+++ b/vgasrc/vgatables.c
@@ -63,8 +63,9 @@
             continue;
         int width = GET_GLOBAL(vmode_g->width);
         int height = GET_GLOBAL(vmode_g->height);
+        u8 memmodel = GET_GLOBAL(vmode_g->memmodel);
         int cheight = GET_GLOBAL(vmode_g->cheight);
-        if (GET_GLOBAL(vmode_g->memmodel) == MM_TEXT) {
+        if (memmodel == MM_TEXT) {
             SET_VGA(vparam_g->twidth, width);
             SET_VGA(vparam_g->theightm1, height-1);
         } else {
@@ -73,7 +74,7 @@
             SET_VGA(vparam_g->theightm1, (height / cheight) - 1);
         }
         SET_VGA(vparam_g->cheight, cheight);
-        SET_VGA(vparam_g->slength, GET_GLOBAL(vmode_g->slength));
+        SET_VGA(vparam_g->slength, calc_page_size(memmodel, width, height));
         memcpy_far(get_global_seg(), vparam_g->sequ_regs
                    , get_global_seg(), GET_GLOBAL(vmode_g->sequ_regs)
                    , ARRAY_SIZE(vparam_g->sequ_regs));
@@ -346,39 +347,39 @@
 #define VPARAM(x) &video_param_table[x]
 
 static struct vgamode_s vga_modes[] VAR16 = {
-    //mode model       tx   ty bpp cw ch  sstart     slength
+    //mode model       tx   ty bpp cw ch  sstart
     // pelm  dac            sequ     misc  crtc     actl     grdc
-    {0x00, MM_TEXT,    40,  25, 4, 9, 16, SEG_CTEXT, 0x0800
+    {0x00, MM_TEXT,    40,  25, 4, 9, 16, SEG_CTEXT
      , 0xFF, PAL(palette2), sequ_01, 0x67, crtc_01, actl_01, grdc_01},
-    {0x01, MM_TEXT,    40,  25, 4, 9, 16, SEG_CTEXT, 0x0800
+    {0x01, MM_TEXT,    40,  25, 4, 9, 16, SEG_CTEXT
      , 0xFF, PAL(palette2), sequ_01, 0x67, crtc_01, actl_01, grdc_01},
-    {0x02, MM_TEXT,    80,  25, 4, 9, 16, SEG_CTEXT, 0x1000
+    {0x02, MM_TEXT,    80,  25, 4, 9, 16, SEG_CTEXT
      , 0xFF, PAL(palette2), sequ_03, 0x67, crtc_03, actl_01, grdc_01},
-    {0x03, MM_TEXT,    80,  25, 4, 9, 16, SEG_CTEXT, 0x1000
+    {0x03, MM_TEXT,    80,  25, 4, 9, 16, SEG_CTEXT
      , 0xFF, PAL(palette2), sequ_03, 0x67, crtc_03, actl_01, grdc_01},
-    {0x04, MM_CGA,    320, 200, 2, 8,  8, SEG_CTEXT, 0x0800
+    {0x04, MM_CGA,    320, 200, 2, 8,  8, SEG_CTEXT
      , 0xFF, PAL(palette1), sequ_04, 0x63, crtc_04, actl_04, grdc_04},
-    {0x05, MM_CGA,    320, 200, 2, 8,  8, SEG_CTEXT, 0x0800
+    {0x05, MM_CGA,    320, 200, 2, 8,  8, SEG_CTEXT
      , 0xFF, PAL(palette1), sequ_04, 0x63, crtc_04, actl_04, grdc_04},
-    {0x06, MM_CGA,    640, 200, 1, 8,  8, SEG_CTEXT, 0x1000
+    {0x06, MM_CGA,    640, 200, 1, 8,  8, SEG_CTEXT
      , 0xFF, PAL(palette1), sequ_06, 0x63, crtc_06, actl_06, grdc_06},
-    {0x07, MM_TEXT,    80,  25, 4, 9, 16, SEG_MTEXT, 0x1000
+    {0x07, MM_TEXT,    80,  25, 4, 9, 16, SEG_MTEXT
      , 0xFF, PAL(palette0), sequ_03, 0x66, crtc_07, actl_07, grdc_07},
-    {0x0D, MM_PLANAR, 320, 200, 4, 8,  8, SEG_GRAPH, 0x2000
+    {0x0D, MM_PLANAR, 320, 200, 4, 8,  8, SEG_GRAPH
      , 0xFF, PAL(palette1), sequ_0d, 0x63, crtc_0d, actl_0d, grdc_0d},
-    {0x0E, MM_PLANAR, 640, 200, 4, 8,  8, SEG_GRAPH, 0x4000
+    {0x0E, MM_PLANAR, 640, 200, 4, 8,  8, SEG_GRAPH
      , 0xFF, PAL(palette1), sequ_0e, 0x63, crtc_0e, actl_0d, grdc_0d},
-    {0x0F, MM_PLANAR, 640, 350, 1, 8, 14, SEG_GRAPH, 0x8000
+    {0x0F, MM_PLANAR, 640, 350, 1, 8, 14, SEG_GRAPH
      , 0xFF, PAL(palette0), sequ_0e, 0xa3, crtc_0f, actl_0f, grdc_0d},
-    {0x10, MM_PLANAR, 640, 350, 4, 8, 14, SEG_GRAPH, 0x8000
+    {0x10, MM_PLANAR, 640, 350, 4, 8, 14, SEG_GRAPH
      , 0xFF, PAL(palette2), sequ_0e, 0xa3, crtc_0f, actl_10, grdc_0d},
-    {0x11, MM_PLANAR, 640, 480, 1, 8, 16, SEG_GRAPH, 0x0000
+    {0x11, MM_PLANAR, 640, 480, 1, 8, 16, SEG_GRAPH
      , 0xFF, PAL(palette2), sequ_0e, 0xe3, crtc_11, actl_11, grdc_0d},
-    {0x12, MM_PLANAR, 640, 480, 4, 8, 16, SEG_GRAPH, 0x0000
+    {0x12, MM_PLANAR, 640, 480, 4, 8, 16, SEG_GRAPH
      , 0xFF, PAL(palette2), sequ_0e, 0xe3, crtc_11, actl_10, grdc_0d},
-    {0x13, MM_PACKED, 320, 200, 8, 8,  8, SEG_GRAPH, 0x0000
+    {0x13, MM_PACKED, 320, 200, 8, 8,  8, SEG_GRAPH
      , 0xFF, PAL(palette3), sequ_13, 0x63, crtc_13, actl_13, grdc_13},
-    {0x6A, MM_PLANAR, 800, 600, 4, 8, 16, SEG_GRAPH, 0x0000
+    {0x6A, MM_PLANAR, 800, 600, 4, 8, 16, SEG_GRAPH
      , 0xFF, PAL(palette2), sequ_0e, 0xe3, crtc_6A, actl_10, grdc_0d},
 };