Add configuration menu for the VGA ROM
[seabios.git] / vgasrc / vga.c
index 180fc991c004ba04b377df357825d265f0a90a87..3988da9c9a58b451ace2829cd433a76e9d90b1f4 100644 (file)
@@ -8,10 +8,6 @@
 
 // TODO:
 //  * review correctness of converted asm by comparing with RBIL
-//  * refactor redundant code into sub-functions
-//  * See if there is a method to the in/out stuff that can be encapsulated.
-//  * remove "biosfn" prefixes
-//  * verify all funcs static
 //
 //  * convert vbe/clext code
 
 #include "biosvar.h" // GET_BDA
 #include "util.h" // memset
 #include "vgatables.h" // find_vga_entry
-
-// XXX
-#define CONFIG_VBE 0
-#define CONFIG_CIRRUS 0
+#include "optionroms.h" // struct pci_data
+#include "config.h" // CONFIG_*
 
 // XXX
 #define DEBUG_VGA_POST 1
 
 #define SET_VGA(var, val) SET_FARVAR(get_global_seg(), (var), (val))
 
+/****************************************************************
+ * PCI Data
+ ****************************************************************/
+#if CONFIG_VGA_PCI == 1
+struct pci_data rom_pci_data VAR16VISIBLE = {
+    .signature = PCI_ROM_SIGNATURE,
+    .vendor = CONFIG_VGA_VID,
+    .device = CONFIG_VGA_DID,
+    .dlen = 0x18,
+    .class_hi = 0x300,
+    .irevision = 1,
+    .type = PCIROM_CODETYPE_X86,
+    .indicator = 0x80,
+};
+#endif
+
+/****************************************************************
+ * Helper functions
+ ****************************************************************/
+
 static inline void
 call16_vgaint(u32 eax, u32 ebx)
 {
@@ -43,7 +57,7 @@ call16_vgaint(u32 eax, u32 ebx)
 }
 
 static void
-biosfn_perform_gray_scale_summing(u16 start, u16 count)
+perform_gray_scale_summing(u16 start, u16 count)
 {
     vgahw_screen_disable();
     int i;
@@ -62,28 +76,28 @@ biosfn_perform_gray_scale_summing(u16 start, u16 count)
 }
 
 static void
-biosfn_set_cursor_shape(u8 CH, u8 CL)
+set_cursor_shape(u8 start, u8 end)
 {
-    CH &= 0x3f;
-    CL &= 0x1f;
+    start &= 0x3f;
+    end &= 0x1f;
 
-    u16 curs = (CH << 8) + CL;
+    u16 curs = (start << 8) + end;
     SET_BDA(cursor_type, curs);
 
     u8 modeset_ctl = GET_BDA(modeset_ctl);
     u16 cheight = GET_BDA(char_height);
-    if ((modeset_ctl & 0x01) && (cheight > 8) && (CL < 8) && (CH < 0x20)) {
-        if (CL != (CH + 1))
-            CH = ((CH + 1) * cheight / 8) - 1;
+    if ((modeset_ctl & 0x01) && (cheight > 8) && (end < 8) && (start < 0x20)) {
+        if (end != (start + 1))
+            start = ((start + 1) * cheight / 8) - 1;
         else
-            CH = ((CL + 1) * cheight / 8) - 2;
-        CL = ((CL + 1) * cheight / 8) - 1;
+            start = ((end + 1) * cheight / 8) - 2;
+        end = ((end + 1) * cheight / 8) - 1;
     }
-    vgahw_set_cursor_shape(CH, CL);
+    vgahw_set_cursor_shape(start, end);
 }
 
 static u16
-biosfn_get_cursor_shape(u8 page)
+get_cursor_shape(u8 page)
 {
     if (page > 7)
         return 0;
@@ -134,7 +148,7 @@ get_cursor_pos(u8 page)
 }
 
 static void
-biosfn_set_active_page(u8 page)
+set_active_page(u8 page)
 {
     if (page > 7)
         return;
@@ -175,109 +189,125 @@ biosfn_set_active_page(u8 page)
     set_cursor_pos(cp);
 }
 
-static struct cursorpos
-check_scroll(struct cursorpos cp)
+static void
+set_scan_lines(u8 lines)
 {
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
+    vgahw_set_scan_lines(lines);
+    if (lines == 8)
+        set_cursor_shape(0x06, 0x07);
+    else
+        set_cursor_shape(lines - 4, lines - 3);
+    SET_BDA(char_height, lines);
+    u16 vde = vgahw_get_vde();
+    u8 rows = vde / lines;
+    SET_BDA(video_rows, rows - 1);
+    u16 cols = GET_BDA(video_cols);
+    SET_BDA(video_pagesize, rows * cols * 2);
+}
 
-    // Do we need to wrap ?
-    if (cp.x == nbcols) {
-        cp.x = 0;
-        cp.y++;
-    }
-    // Do we need to scroll ?
-    if (cp.y == nbrows) {
-        struct cursorpos ul = {0, 0, cp.page};
-        struct cursorpos lr = {nbcols-1, nbrows-1, cp.page};
-        vgafb_scroll(1, -1, ul, lr);
-        cp.y--;
-    }
 
-    return cp;
+/****************************************************************
+ * Character writing
+ ****************************************************************/
+
+// Scroll the screen one line.  This function is designed to be called
+// tail-recursive to reduce stack usage.
+static void noinline
+scroll_one(u16 nbrows, u16 nbcols, u8 page)
+{
+    struct cursorpos ul = {0, 0, page};
+    struct cursorpos lr = {nbcols-1, nbrows-1, page};
+    vgafb_scroll(1, -1, ul, lr);
 }
 
-static struct cursorpos
-write_teletype(struct cursorpos cp, struct carattr ca)
+// Write a character to the screen at a given position.  Implement
+// special characters and scroll the screen if necessary.
+static void
+write_teletype(struct cursorpos *pcp, struct carattr ca)
 {
+    struct cursorpos cp = *pcp;
+
+    // Get the dimensions
+    u16 nbrows = GET_BDA(video_rows) + 1;
+    u16 nbcols = GET_BDA(video_cols);
+
     switch (ca.car) {
     case 7:
         //FIXME should beep
         break;
-
     case 8:
         if (cp.x > 0)
             cp.x--;
         break;
-
     case '\r':
         cp.x = 0;
         break;
-
     case '\n':
         cp.y++;
         break;
-
     case '\t':
         do {
             struct carattr dummyca = {' ', ca.attr, ca.use_attr};
             vgafb_write_char(cp, dummyca);
             cp.x++;
-            cp = check_scroll(cp);
-        } while (cp.x % 8);
+        } while (cp.x < nbcols && cp.x % 8);
         break;
-
     default:
         vgafb_write_char(cp, ca);
         cp.x++;
     }
 
-    return check_scroll(cp);
+    // Do we need to wrap ?
+    if (cp.x == nbcols) {
+        cp.x = 0;
+        cp.y++;
+    }
+    // Do we need to scroll ?
+    if (cp.y < nbrows) {
+        *pcp = cp;
+        return;
+    }
+    // Scroll screen
+    cp.y--;
+    *pcp = cp;
+    scroll_one(nbrows, nbcols, cp.page);
 }
 
+// Write out a buffer of alternating characters and attributes.
 static void
-write_string(struct cursorpos cp, u8 flag, u8 attr, u16 count,
-             u16 seg, u8 *offset_far)
+write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
 {
-    // if row=0xff special case : use current cursor position
-    if (cp.y == 0xff)
-        cp = get_cursor_pos(cp.page);
-
-    while (count-- != 0) {
+    while (count--) {
         u8 car = GET_FARVAR(seg, *offset_far);
         offset_far++;
-        if ((flag & 0x02) != 0) {
-            attr = GET_FARVAR(seg, *offset_far);
-            offset_far++;
-        }
+        u8 attr = GET_FARVAR(seg, *offset_far);
+        offset_far++;
 
         struct carattr ca = {car, attr, 1};
-        cp = write_teletype(cp, ca);
+        write_teletype(pcp, ca);
     }
-
-    if (flag & 0x01)
-        set_cursor_pos(cp);
 }
 
+// Write out a buffer of characters.
 static void
-set_scan_lines(u8 lines)
+write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
 {
-    vgahw_set_scan_lines(lines);
-    if (lines == 8)
-        biosfn_set_cursor_shape(0x06, 0x07);
-    else
-        biosfn_set_cursor_shape(lines - 4, lines - 3);
-    SET_BDA(char_height, lines);
-    u16 vde = vgahw_get_vde();
-    u8 rows = vde / lines;
-    SET_BDA(video_rows, rows - 1);
-    u16 cols = GET_BDA(video_cols);
-    SET_BDA(video_pagesize, rows * cols * 2);
+    while (count--) {
+        u8 car = GET_FARVAR(seg, *offset_far);
+        offset_far++;
+
+        struct carattr ca = {car, attr, 1};
+        write_teletype(pcp, ca);
+    }
 }
 
+
+/****************************************************************
+ * Save and restore bda state
+ ****************************************************************/
+
 static void
-biosfn_save_bda_state(u16 seg, struct saveBDAstate *info)
+save_bda_state(u16 seg, struct saveBDAstate *info)
 {
     SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
     SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
@@ -295,12 +325,12 @@ biosfn_save_bda_state(u16 seg, struct saveBDAstate *info)
     SET_FARVAR(seg, info->video_pagestart, GET_BDA(video_pagestart));
     SET_FARVAR(seg, info->video_page, GET_BDA(video_page));
     /* current font */
-    SET_FARVAR(seg, *(u32*)&info->font0_off, GET_IVT(0x1f).segoff);
-    SET_FARVAR(seg, *(u32*)&info->font1_off, GET_IVT(0x43).segoff);
+    SET_FARVAR(seg, info->font0, GET_IVT(0x1f));
+    SET_FARVAR(seg, info->font1, GET_IVT(0x43));
 }
 
 static void
-biosfn_restore_bda_state(u16 seg, struct saveBDAstate *info)
+restore_bda_state(u16 seg, struct saveBDAstate *info)
 {
     SET_BDA(video_mode, GET_FARVAR(seg, info->video_mode));
     SET_BDA(video_cols, GET_FARVAR(seg, info->video_cols));
@@ -318,10 +348,8 @@ biosfn_restore_bda_state(u16 seg, struct saveBDAstate *info)
     SET_BDA(video_pagestart, GET_FARVAR(seg, info->video_pagestart));
     SET_BDA(video_page, GET_FARVAR(seg, info->video_page));
     /* current font */
-    SET_IVT(0x1f, GET_FARVAR(seg, info->font0_seg)
-            , GET_FARVAR(seg, info->font0_off));
-    SET_IVT(0x43, GET_FARVAR(seg, info->font1_seg)
-            , GET_FARVAR(seg, info->font1_off));
+    SET_IVT(0x1f, GET_FARVAR(seg, info->font0));
+    SET_IVT(0x43, GET_FARVAR(seg, info->font1));
 }
 
 
@@ -344,11 +372,11 @@ handle_1000(struct bregs *regs)
     else
         regs->al = 0x30;
 
-    if (CONFIG_CIRRUS)
+    if (CONFIG_VGA_CIRRUS)
         cirrus_set_video_mode(mode);
 
-    if (CONFIG_VBE)
-        if (vbe_has_vbe_display())
+    if (CONFIG_VGA_BOCHS)
+        if (bochs_has_vbe_display())
             dispi_set_enable(VBE_DISPI_DISABLED);
 
     // find the entry in the video modes
@@ -380,7 +408,7 @@ handle_1000(struct bregs *regs)
         }
 
         if ((modeset_ctl & 0x02) == 0x02)
-            biosfn_perform_gray_scale_summing(0x00, 0x100);
+            perform_gray_scale_summing(0x00, 0x100);
     }
 
     struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
@@ -408,8 +436,8 @@ handle_1000(struct bregs *regs)
 
     // FIXME We nearly have the good tables. to be reworked
     SET_BDA(dcc_index, 0x08);   // 8 is VGA should be ok for now
-    SET_BDA(video_savetable_ptr, (u32)video_save_pointer_table);
-    SET_BDA(video_savetable_seg, get_global_seg());
+    SET_BDA(video_savetable
+            , SEGOFF(get_global_seg(), (u32)video_save_pointer_table));
 
     // FIXME
     SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
@@ -417,7 +445,7 @@ handle_1000(struct bregs *regs)
 
     // Set cursor shape
     if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
-        biosfn_set_cursor_shape(0x06, 0x07);
+        set_cursor_shape(0x06, 0x07);
     // Set cursor pos for page 0..7
     int i;
     for (i = 0; i < 8; i++) {
@@ -426,7 +454,7 @@ handle_1000(struct bregs *regs)
     }
 
     // Set active page 0
-    biosfn_set_active_page(0x00);
+    set_active_page(0x00);
 
     // Write the fonts in memory
     if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
@@ -434,17 +462,17 @@ handle_1000(struct bregs *regs)
         call16_vgaint(0x1103, 0);
     }
     // Set the ints 0x1F and 0x43
-    SET_IVT(0x1f, get_global_seg(), (u32)&vgafont8[128 * 8]);
+    SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
 
     switch (cheight) {
     case 8:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont8);
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
         break;
     case 14:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont14);
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
         break;
     case 16:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont16);
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
         break;
     }
 }
@@ -452,7 +480,7 @@ handle_1000(struct bregs *regs)
 static void
 handle_1001(struct bregs *regs)
 {
-    biosfn_set_cursor_shape(regs->ch, regs->cl);
+    set_cursor_shape(regs->ch, regs->cl);
 }
 
 static void
@@ -465,7 +493,7 @@ handle_1002(struct bregs *regs)
 static void
 handle_1003(struct bregs *regs)
 {
-    regs->cx = biosfn_get_cursor_shape(regs->bh);
+    regs->cx = get_cursor_shape(regs->bh);
     struct cursorpos cp = get_cursor_pos(regs->bh);
     regs->dl = cp.x;
     regs->dh = cp.y;
@@ -482,7 +510,7 @@ handle_1004(struct bregs *regs)
 static void
 handle_1005(struct bregs *regs)
 {
-    biosfn_set_active_page(regs->al);
+    set_active_page(regs->al);
 }
 
 static void
@@ -492,18 +520,21 @@ verify_scroll(struct bregs *regs, int dir)
     struct cursorpos ul = {regs->cl, regs->ch, page};
     struct cursorpos lr = {regs->dl, regs->dh, page};
 
-    if (ul.x > lr.x || ul.y > lr.y)
-        return;
-
     u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
     if (lr.y >= nbrows)
         lr.y = nbrows - 1;
+    u16 nbcols = GET_BDA(video_cols);
     if (lr.x >= nbcols)
         lr.x = nbcols - 1;
 
-    vgafb_scroll(dir * regs->al, regs->bh, ul, lr);
+    if (ul.x > lr.x || ul.y > lr.y)
+        return;
+
+    u16 nblines = regs->al;
+    if (!nblines || nblines > lr.y - ul.y + 1)
+        nblines = lr.y - ul.y + 1;
+
+    vgafb_scroll(dir * nblines, regs->bh, ul, lr);
 }
 
 static void
@@ -526,7 +557,7 @@ handle_1008(struct bregs *regs)
     regs->ah = ca.attr;
 }
 
-static void
+static void noinline
 write_chars(u8 page, struct carattr ca, u16 count)
 {
     struct cursorpos cp = get_cursor_pos(page);
@@ -583,25 +614,25 @@ handle_100b(struct bregs *regs)
 static void
 handle_100c(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_write_pixel(regs->bh, regs->al, regs->cx, regs->dx);
+    // XXX - page (regs->bh) is unused
+    vgafb_write_pixel(regs->al, regs->cx, regs->dx);
 }
 
 static void
 handle_100d(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_pixel(regs->bh, regs->cx, regs->dx, &regs->ax);
+    // XXX - page (regs->bh) is unused
+    regs->al = vgafb_read_pixel(regs->cx, regs->dx);
 }
 
-static void
+static void noinline
 handle_100e(struct bregs *regs)
 {
     // Ralf Brown Interrupt list is WRONG on bh(page)
     // We do output only on the current page !
     struct carattr ca = {regs->al, regs->bl, 0};
     struct cursorpos cp = get_cursor_pos(0xff);
-    cp = write_teletype(cp, ca);
+    write_teletype(&cp, ca);
     set_cursor_pos(cp);
 }
 
@@ -651,7 +682,7 @@ handle_101007(struct bregs *regs)
 static void
 handle_101008(struct bregs *regs)
 {
-    regs->bh = vgahw_get_overscan_border_color(regs);
+    regs->bh = vgahw_get_overscan_border_color();
 }
 
 static void
@@ -660,7 +691,7 @@ handle_101009(struct bregs *regs)
     vgahw_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
 }
 
-static void
+static void noinline
 handle_101010(struct bregs *regs)
 {
     u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
@@ -679,7 +710,7 @@ handle_101013(struct bregs *regs)
     vgahw_select_video_dac_color_page(regs->bl, regs->bh);
 }
 
-static void
+static void noinline
 handle_101015(struct bregs *regs)
 {
     u8 rgb[3];
@@ -716,7 +747,7 @@ handle_10101a(struct bregs *regs)
 static void
 handle_10101b(struct bregs *regs)
 {
-    biosfn_perform_gray_scale_summing(regs->bx, regs->cx);
+    perform_gray_scale_summing(regs->bx, regs->cx);
 }
 
 static void
@@ -815,15 +846,15 @@ handle_101130(struct bregs *regs)
 {
     switch (regs->bh) {
     case 0x00: {
-        u32 segoff = GET_IVT(0x1f).segoff;
-        regs->es = segoff >> 16;
-        regs->bp = segoff;
+        struct segoff_s so = GET_IVT(0x1f);
+        regs->es = so.seg;
+        regs->bp = so.offset;
         break;
     }
     case 0x01: {
-        u32 segoff = GET_IVT(0x43).segoff;
-        regs->es = segoff >> 16;
-        regs->bp = segoff;
+        struct segoff_s so = GET_IVT(0x43);
+        regs->es = so.seg;
+        regs->bp = so.offset;
         break;
     }
     case 0x02:
@@ -1000,13 +1031,22 @@ handle_1012(struct bregs *regs)
 }
 
 
-static void
+// Write string
+static void noinline
 handle_1013(struct bregs *regs)
 {
-    // XXX - inline
     struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
-    write_string(cp, regs->al, regs->bl, regs->cx
-                 , regs->es, (void*)(regs->bp + 0));
+    // if row=0xff special case : use current cursor position
+    if (cp.y == 0xff)
+        cp = get_cursor_pos(cp.page);
+    u8 flag = regs->al;
+    if (flag & 2)
+        write_attr_string(&cp, regs->cx, regs->es, (void*)(regs->bp + 0));
+    else
+        write_string(&cp, regs->bl, regs->cx, regs->es, (void*)(regs->bp + 0));
+
+    if (flag & 1)
+        set_cursor_pos(cp);
 }
 
 
@@ -1074,8 +1114,10 @@ handle_101b(struct bregs *regs)
     SET_FARVAR(seg, info->static_functionality_seg, get_global_seg());
 
     // Hard coded copy from BIOS area. Should it be cleaner ?
-    memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49, 30);
-    memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84, 3);
+    memcpy_far(seg, info->bda_0x49, SEG_BDA, (void*)0x49
+               , sizeof(info->bda_0x49));
+    memcpy_far(seg, info->bda_0x84, SEG_BDA, (void*)0x84
+               , sizeof(info->bda_0x84));
 
     SET_FARVAR(seg, info->dcc_index, GET_BDA(dcc_index));
     SET_FARVAR(seg, info->colors, 16);
@@ -1112,7 +1154,7 @@ handle_101c01(struct bregs *regs)
         data += sizeof(struct saveVideoHardware);
     }
     if (flags & 2) {
-        biosfn_save_bda_state(seg, data);
+        save_bda_state(seg, data);
         data += sizeof(struct saveBDAstate);
     }
     if (flags & 4)
@@ -1131,7 +1173,7 @@ handle_101c02(struct bregs *regs)
         data += sizeof(struct saveVideoHardware);
     }
     if (flags & 2) {
-        biosfn_restore_bda_state(seg, data);
+        restore_bda_state(seg, data);
         data += sizeof(struct saveBDAstate);
     }
     if (flags & 4)
@@ -1234,7 +1276,7 @@ handle_104fXX(struct bregs *regs)
 static void
 handle_104f(struct bregs *regs)
 {
-    if (! CONFIG_VBE || !vbe_has_vbe_display()) {
+    if (! CONFIG_VGA_BOCHS || !bochs_has_vbe_display()) {
         handle_104fXX(regs);
         return;
     }
@@ -1301,7 +1343,7 @@ handle_10(struct bregs *regs)
  ****************************************************************/
 
 static void
-init_bios_area()
+init_bios_area(void)
 {
     // init detected hardware BIOS Area
     // set 80x25 color (not clear from RBIL but usual)
@@ -1335,13 +1377,13 @@ vga_post(struct bregs *regs)
 
     init_bios_area();
 
-    if (CONFIG_VBE)
-        vbe_init();
+    if (CONFIG_VGA_BOCHS)
+        bochs_init();
 
     extern void entry_10(void);
-    SET_IVT(0x10, get_global_seg(), (u32)entry_10);
+    SET_IVT(0x10, SEGOFF(get_global_seg(), (u32)entry_10));
 
-    if (CONFIG_CIRRUS)
+    if (CONFIG_VGA_CIRRUS)
         cirrus_init();
 
     // XXX - clear screen and display info