vgabios: Merge support for GeodeLX vga bios.
[seabios.git] / vgasrc / vga.c
index b94f9f9306d9e852b300ead7039234f6eb6c32b5..37f227c078598fb2141fa54ba8cf1b005c6e3bc5 100644 (file)
@@ -7,43 +7,45 @@
 
 
 // TODO:
-//  * introduce "struct vregs", or add ebp to struct bregs.
-//  * define structs for save/restore state
 //  * 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
-//
-//  * separate code into separate files
-//  * extract hw code from bios interfaces
 
 #include "bregs.h" // struct bregs
 #include "biosvar.h" // GET_BDA
 #include "util.h" // memset
-#include "vgatables.h" // vga_modes
-
-// XXX
-#define CONFIG_VBE 0
-#define CONFIG_CIRRUS 0
+#include "vgatables.h" // find_vga_entry
+#include "optionroms.h" // struct pci_data
+#include "config.h" // CONFIG_*
+#include "vbe.h" // vbe_*
+#include "geodelx.h" // geodelx_init
 
 // XXX
 #define DEBUG_VGA_POST 1
 #define DEBUG_VGA_10 3
 
-#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
 
-// ===================================================================
-//
-// Video Utils
-//
-// ===================================================================
+/****************************************************************
+ * Helper functions
+ ****************************************************************/
 
-// -------------------------------------------------------------------
-inline void
+static inline void
 call16_vgaint(u32 eax, u32 ebx)
 {
     asm volatile(
@@ -55,82 +57,48 @@ call16_vgaint(u32 eax, u32 ebx)
         : "cc", "memory");
 }
 
-// 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);
-}
-
-
-// ===================================================================
-//
-// BIOS functions
-//
-// ===================================================================
-
-// -------------------------------------------------------------------
 static void
-biosfn_perform_gray_scale_summing(u16 start, u16 count)
+perform_gray_scale_summing(u16 start, u16 count)
 {
-    inb(VGAREG_ACTL_RESET);
-    outb(0x00, VGAREG_ACTL_ADDRESS);
-
+    vgahw_screen_disable();
     int i;
     for (i = start; i < start+count; i++) {
-        // set read address and switch to read mode
-        outb(i, VGAREG_DAC_READ_ADDRESS);
-        // get 6-bit wide RGB data values
-        u8 r = inb(VGAREG_DAC_DATA);
-        u8 g = inb(VGAREG_DAC_DATA);
-        u8 b = inb(VGAREG_DAC_DATA);
+        u8 rgb[3];
+        vgahw_get_dac_regs(GET_SEG(SS), rgb, i, 1);
 
         // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue )
-        u16 intensity = ((77 * r + 151 * g + 28 * b) + 0x80) >> 8;
-
+        u16 intensity = ((77 * rgb[0] + 151 * rgb[1] + 28 * rgb[2]) + 0x80) >> 8;
         if (intensity > 0x3f)
             intensity = 0x3f;
 
-        // set write address and switch to write mode
-        outb(i, VGAREG_DAC_WRITE_ADDRESS);
-        // write new intensity value
-        outb(intensity & 0xff, VGAREG_DAC_DATA);
-        outb(intensity & 0xff, VGAREG_DAC_DATA);
-        outb(intensity & 0xff, VGAREG_DAC_DATA);
+        vgahw_set_dac_regs(GET_SEG(SS), rgb, i, 1);
     }
-    inb(VGAREG_ACTL_RESET);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
+    vgahw_screen_enable();
 }
 
-// -------------------------------------------------------------------
 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;
     }
-    // CTRC regs 0x0a and 0x0b
-    u16 crtc_addr = GET_BDA(crtc_address);
-    outb(0x0a, crtc_addr);
-    outb(CH, crtc_addr + 1);
-    outb(0x0b, crtc_addr);
-    outb(CL, crtc_addr + 1);
+    vgahw_set_cursor_shape(start, end);
 }
 
 static u16
-biosfn_get_cursor_shape(u8 page)
+get_cursor_shape(u8 page)
 {
     if (page > 7)
         return 0;
@@ -138,52 +106,50 @@ biosfn_get_cursor_shape(u8 page)
     return GET_BDA(cursor_type);
 }
 
-// -------------------------------------------------------------------
 static void
-biosfn_set_cursor_pos(u8 page, u16 cursor)
+set_cursor_pos(struct cursorpos cp)
 {
     // Should not happen...
-    if (page > 7)
+    if (cp.page > 7)
         return;
 
     // Bios cursor pos
-    SET_BDA(cursor_pos[page], cursor);
+    SET_BDA(cursor_pos[cp.page], (cp.y << 8) | cp.x);
 
     // Set the hardware cursor
     u8 current = GET_BDA(video_page);
-    if (page != current)
+    if (cp.page != current)
         return;
 
     // Get the dimensions
     u16 nbcols = GET_BDA(video_cols);
     u16 nbrows = GET_BDA(video_rows) + 1;
 
-    u8 xcurs = cursor & 0x00ff;
-    u8 ycurs = (cursor & 0xff00) >> 8;
-
     // Calculate the address knowing nbcols nbrows and page num
-    u16 address = SCREEN_IO_START(nbcols, nbrows, page) + xcurs + ycurs * nbcols;
+    u16 address = (SCREEN_IO_START(nbcols, nbrows, cp.page)
+                   + cp.x + cp.y * nbcols);
 
-    // CRTC regs 0x0e and 0x0f
-    u16 crtc_addr = GET_BDA(crtc_address);
-    outb(0x0e, crtc_addr);
-    outb((address & 0xff00) >> 8, crtc_addr + 1);
-    outb(0x0f, crtc_addr);
-    outb(address & 0x00ff, crtc_addr + 1);
+    vgahw_set_cursor_pos(address);
 }
 
-static u16
-biosfn_get_cursor_pos(u8 page)
+static struct cursorpos
+get_cursor_pos(u8 page)
 {
-    if (page > 7)
-        return 0;
+    if (page == 0xff)
+        // special case - use current page
+        page = GET_BDA(video_page);
+    if (page > 7) {
+        struct cursorpos cp = { 0, 0, 0xfe };
+        return cp;
+    }
     // FIXME should handle VGA 14/16 lines
-    return GET_BDA(cursor_pos[page]);
+    u16 xy = GET_BDA(cursor_pos[page]);
+    struct cursorpos cp = {xy, xy>>8, page};
+    return cp;
 }
 
-// -------------------------------------------------------------------
 static void
-biosfn_set_active_page(u8 page)
+set_active_page(u8 page)
 {
     if (page > 7)
         return;
@@ -194,10 +160,10 @@ biosfn_set_active_page(u8 page)
         return;
 
     // Get pos curs pos for the right page
-    u16 cursor = biosfn_get_cursor_pos(page);
+    struct cursorpos cp = get_cursor_pos(page);
 
     u16 address;
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
+    if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
         // Get the dimensions
         u16 nbcols = GET_BDA(video_cols);
         u16 nbrows = GET_BDA(video_rows) + 1;
@@ -209,16 +175,10 @@ biosfn_set_active_page(u8 page)
         // Start address
         address = SCREEN_IO_START(nbcols, nbrows, page);
     } else {
-        struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-        address = page * GET_GLOBAL(vparam_g->slength);
+        address = page * GET_GLOBAL(vmode_g->slength);
     }
 
-    // CRTC regs 0x0c and 0x0d
-    u16 crtc_addr = GET_BDA(crtc_address);
-    outb(0x0c, crtc_addr);
-    outb((address & 0xff00) >> 8, crtc_addr + 1);
-    outb(0x0d, crtc_addr);
-    outb(address & 0x00ff, crtc_addr + 1);
+    vgahw_set_active_page(address);
 
     // And change the BIOS page
     SET_BDA(video_page, page);
@@ -226,1742 +186,324 @@ biosfn_set_active_page(u8 page)
     dprintf(1, "Set active page %02x address %04x\n", page, address);
 
     // Display the cursor, now the page is active
-    biosfn_set_cursor_pos(page, cursor);
-}
-
-static void
-biosfn_set_video_mode(u8 mode)
-{                               // mode: Bit 7 is 1 if no clear screen
-    if (CONFIG_CIRRUS)
-        cirrus_set_video_mode(mode);
-
-#ifdef VBE
-    if (vbe_has_vbe_display())
-        dispi_set_enable(VBE_DISPI_DISABLED);
-#endif
-
-    // The real mode
-    u8 noclearmem = mode & 0x80;
-    mode = mode & 0x7f;
-
-    // find the entry in the video modes
-    struct vgamode_s *vmode_g = find_vga_entry(mode);
-    dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
-    if (!vmode_g)
-        return;
-
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u16 twidth = GET_GLOBAL(vparam_g->twidth);
-    u16 theightm1 = GET_GLOBAL(vparam_g->theightm1);
-    u16 cheight = GET_GLOBAL(vparam_g->cheight);
-
-    // Read the bios mode set control
-    u8 modeset_ctl = GET_BDA(modeset_ctl);
-
-    // Then we know the number of lines
-// FIXME
-
-    // if palette loading (bit 3 of modeset ctl = 0)
-    if ((modeset_ctl & 0x08) == 0) {    // Set the PEL mask
-        outb(GET_GLOBAL(vmode_g->pelmask), VGAREG_PEL_MASK);
-
-        // Set the whole dac always, from 0
-        outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
-
-        // From which palette
-        u8 *palette_g = GET_GLOBAL(vmode_g->dac);
-        u16 palsize = GET_GLOBAL(vmode_g->dacsize);
-        // Always 256*3 values
-        u16 i;
-        for (i = 0; i < 0x0100; i++) {
-            if (i <= palsize) {
-                outb(GET_GLOBAL(palette_g[(i * 3) + 0]), VGAREG_DAC_DATA);
-                outb(GET_GLOBAL(palette_g[(i * 3) + 1]), VGAREG_DAC_DATA);
-                outb(GET_GLOBAL(palette_g[(i * 3) + 2]), VGAREG_DAC_DATA);
-            } else {
-                outb(0, VGAREG_DAC_DATA);
-                outb(0, VGAREG_DAC_DATA);
-                outb(0, VGAREG_DAC_DATA);
-            }
-        }
-        if ((modeset_ctl & 0x02) == 0x02)
-            biosfn_perform_gray_scale_summing(0x00, 0x100);
-    }
-    // Reset Attribute Ctl flip-flop
-    inb(VGAREG_ACTL_RESET);
-
-    // Set Attribute Ctl
-    u16 i;
-    for (i = 0; i <= 0x13; i++) {
-        outb(i, VGAREG_ACTL_ADDRESS);
-        outb(GET_GLOBAL(vparam_g->actl_regs[i]), VGAREG_ACTL_WRITE_DATA);
-    }
-    outb(0x14, VGAREG_ACTL_ADDRESS);
-    outb(0x00, VGAREG_ACTL_WRITE_DATA);
-
-    // Set Sequencer Ctl
-    outb(0, VGAREG_SEQU_ADDRESS);
-    outb(0x03, VGAREG_SEQU_DATA);
-    for (i = 1; i <= 4; i++) {
-        outb(i, VGAREG_SEQU_ADDRESS);
-        outb(GET_GLOBAL(vparam_g->sequ_regs[i - 1]), VGAREG_SEQU_DATA);
-    }
-
-    // Set Grafx Ctl
-    for (i = 0; i <= 8; i++) {
-        outb(i, VGAREG_GRDC_ADDRESS);
-        outb(GET_GLOBAL(vparam_g->grdc_regs[i]), VGAREG_GRDC_DATA);
-    }
-
-    // Set CRTC address VGA or MDA
-    u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
-    if (GET_GLOBAL(vmode_g->memmodel) == MTEXT)
-        crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
-
-    // Disable CRTC write protection
-    outw(0x0011, crtc_addr);
-    // Set CRTC regs
-    for (i = 0; i <= 0x18; i++) {
-        outb(i, crtc_addr);
-        outb(GET_GLOBAL(vparam_g->crtc_regs[i]), crtc_addr + 1);
-    }
-
-    // Set the misc register
-    outb(GET_GLOBAL(vparam_g->miscreg), VGAREG_WRITE_MISC_OUTPUT);
-
-    // Enable video
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-    inb(VGAREG_ACTL_RESET);
-
-    if (noclearmem == 0x00) {
-        if (GET_GLOBAL(vmode_g->class) == TEXT) {
-            memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0720, 32*1024);
-        } else {
-            if (mode < 0x0d) {
-                memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 32*1024);
-            } else {
-                outb(0x02, VGAREG_SEQU_ADDRESS);
-                u8 mmask = inb(VGAREG_SEQU_DATA);
-                outb(0x0f, VGAREG_SEQU_DATA);   // all planes
-                memset16_far(GET_GLOBAL(vmode_g->sstart), 0, 0x0000, 64*1024);
-                outb(mmask, VGAREG_SEQU_DATA);
-            }
-        }
-    }
-    // Set the BIOS mem
-    SET_BDA(video_mode, mode);
-    SET_BDA(video_cols, twidth);
-    SET_BDA(video_pagesize, GET_GLOBAL(vparam_g->slength));
-    SET_BDA(crtc_address, crtc_addr);
-    SET_BDA(video_rows, theightm1);
-    SET_BDA(char_height, cheight);
-    SET_BDA(video_ctl, (0x60 | noclearmem));
-    SET_BDA(video_switches, 0xF9);
-    SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
-
-    // 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());
-
-    // FIXME
-    SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
-    SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
-
-    // Set cursor shape
-    if (GET_GLOBAL(vmode_g->class) == TEXT)
-        biosfn_set_cursor_shape(0x06, 0x07);
-    // Set cursor pos for page 0..7
-    for (i = 0; i < 8; i++)
-        biosfn_set_cursor_pos(i, 0x0000);
-
-    // Set active page 0
-    biosfn_set_active_page(0x00);
-
-    // Write the fonts in memory
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
-        call16_vgaint(0x1104, 0);
-        call16_vgaint(0x1103, 0);
-    }
-    // Set the ints 0x1F and 0x43
-    SET_IVT(0x1f, get_global_seg(), (u32)&vgafont8[128 * 8]);
-
-    switch (cheight) {
-    case 8:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont8);
-        break;
-    case 14:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont14);
-        break;
-    case 16:
-        SET_IVT(0x43, get_global_seg(), (u32)vgafont16);
-        break;
-    }
+    set_cursor_pos(cp);
 }
 
-// -------------------------------------------------------------------
 static void
-vgamem_copy_pl4(u8 xstart, u8 ysrc, u8 ydest, u8 cols, u8 nbcols,
-                u8 cheight)
+set_scan_lines(u8 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);
+    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);
 }
 
-// -------------------------------------------------------------------
-static void
-vgamem_fill_pl4(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
-                u8 attr)
-{
-    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);
-}
 
-// -------------------------------------------------------------------
-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);
-}
+/****************************************************************
+ * Character writing
+ ****************************************************************/
 
-// -------------------------------------------------------------------
-static void
-vgamem_fill_cga(u8 xstart, u8 ystart, u8 cols, u8 nbcols, u8 cheight,
-                u8 attr)
+// 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)
 {
-    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);
+    struct cursorpos ul = {0, 0, page};
+    struct cursorpos lr = {nbcols-1, nbrows-1, page};
+    vgafb_scroll(1, -1, ul, lr);
 }
 
-// -------------------------------------------------------------------
+// Write a character to the screen at a given position.  Implement
+// special characters and scroll the screen if necessary.
 static void
-biosfn_scroll(u8 nblines, u8 attr, u8 rul, u8 cul, u8 rlr, u8 clr, u8 page,
-              u8 dir)
+write_teletype(struct cursorpos *pcp, struct carattr ca)
 {
-    // page == 0xFF if current
-    if (rul > rlr)
-        return;
-    if (cul > clr)
-        return;
-
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
+    struct cursorpos cp = *pcp;
 
     // Get the dimensions
     u16 nbrows = GET_BDA(video_rows) + 1;
     u16 nbcols = GET_BDA(video_cols);
 
-    // Get the current page
-    if (page == 0xFF)
-        page = GET_BDA(video_page);
-
-    if (rlr >= nbrows)
-        rlr = nbrows - 1;
-    if (clr >= nbcols)
-        clr = nbcols - 1;
-    if (nblines > nbrows)
-        nblines = 0;
-    u8 cols = clr - cul + 1;
-
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
-        // Compute the address
-        void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page));
-        dprintf(3, "Scroll, address %p (%d %d %02x)\n"
-                , address_far, nbrows, nbcols, page);
-
-        if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
-            && clr == nbcols - 1) {
-            memset16_far(GET_GLOBAL(vmode_g->sstart), address_far
-                         , (u16)attr * 0x100 + ' ', nbrows * nbcols * 2);
-        } else {                // if Scroll up
-            if (dir == SCROLL_UP) {
-                u16 i;
-                for (i = rul; i <= rlr; i++)
-                    if ((i + nblines > rlr) || (nblines == 0))
-                        memset16_far(GET_GLOBAL(vmode_g->sstart)
-                                     , address_far + (i * nbcols + cul) * 2
-                                     , (u16)attr * 0x100 + ' ', cols * 2);
-                    else
-                        memcpy16_far(GET_GLOBAL(vmode_g->sstart)
-                                     , address_far + (i * nbcols + cul) * 2
-                                     , GET_GLOBAL(vmode_g->sstart)
-                                     , (void*)(((i + nblines) * nbcols + cul) * 2)
-                                     , cols * 2);
-            } else {
-                u16 i;
-                for (i = rlr; i >= rul; i--) {
-                    if ((i < rul + nblines) || (nblines == 0))
-                        memset16_far(GET_GLOBAL(vmode_g->sstart)
-                                     , address_far + (i * nbcols + cul) * 2
-                                     , (u16)attr * 0x100 + ' ', cols * 2);
-                    else
-                        memcpy16_far(GET_GLOBAL(vmode_g->sstart)
-                                     , address_far + (i * nbcols + cul) * 2
-                                     , GET_GLOBAL(vmode_g->sstart)
-                                     , (void*)(((i - nblines) * nbcols + cul) * 2)
-                                     , cols * 2);
-                    if (i > rlr)
-                        break;
-                }
-            }
-        }
-        return;
-    }
-
-    // FIXME gfx mode not complete
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
-    switch (GET_GLOBAL(vmode_g->memmodel)) {
-    case PLANAR4:
-    case PLANAR1:
-        if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
-            && clr == nbcols - 1) {
-            outw(0x0205, VGAREG_GRDC_ADDRESS);
-            memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
-                       nbrows * nbcols * cheight);
-            outw(0x0005, VGAREG_GRDC_ADDRESS);
-        } else {            // if Scroll up
-            if (dir == SCROLL_UP) {
-                u16 i;
-                for (i = rul; i <= rlr; i++)
-                    if ((i + nblines > rlr) || (nblines == 0))
-                        vgamem_fill_pl4(cul, i, cols, nbcols, cheight,
-                                        attr);
-                    else
-                        vgamem_copy_pl4(cul, i + nblines, i, cols,
-                                        nbcols, cheight);
-            } else {
-                u16 i;
-                for (i = rlr; i >= rul; i--) {
-                    if ((i < rul + nblines) || (nblines == 0))
-                        vgamem_fill_pl4(cul, i, cols, nbcols, cheight,
-                                        attr);
-                    else
-                        vgamem_copy_pl4(cul, i, i - nblines, cols,
-                                        nbcols, cheight);
-                    if (i > rlr)
-                        break;
-                }
-            }
-        }
+    switch (ca.car) {
+    case 7:
+        //FIXME should beep
         break;
-    case CGA: {
-        u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-        if (nblines == 0 && rul == 0 && cul == 0 && rlr == nbrows - 1
-            && clr == nbcols - 1) {
-            memset_far(GET_GLOBAL(vmode_g->sstart), 0, attr,
-                       nbrows * nbcols * cheight * bpp);
-        } else {
-            if (bpp == 2) {
-                cul <<= 1;
-                cols <<= 1;
-                nbcols <<= 1;
-            }
-            // if Scroll up
-            if (dir == SCROLL_UP) {
-                u16 i;
-                for (i = rul; i <= rlr; i++)
-                    if ((i + nblines > rlr) || (nblines == 0))
-                        vgamem_fill_cga(cul, i, cols, nbcols, cheight,
-                                        attr);
-                    else
-                        vgamem_copy_cga(cul, i + nblines, i, cols,
-                                        nbcols, cheight);
-            } else {
-                u16 i;
-                for (i = rlr; i >= rul; i--) {
-                    if ((i < rul + nblines) || (nblines == 0))
-                        vgamem_fill_cga(cul, i, cols, nbcols, cheight,
-                                        attr);
-                    else
-                        vgamem_copy_cga(cul, i, i - nblines, cols,
-                                        nbcols, cheight);
-                    if (i > rlr)
-                        break;
-                }
-            }
-        }
+    case 8:
+        if (cp.x > 0)
+            cp.x--;
         break;
-    }
-    default:
-        dprintf(1, "Scroll in graphics mode\n");
-    }
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_char_attr(u8 page, u16 *car)
-{
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-
-    // Get the cursor pos for the page
-    u16 cursor = biosfn_get_cursor_pos(page);
-    u8 xcurs = cursor & 0x00ff;
-    u8 ycurs = (cursor & 0xff00) >> 8;
-
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
-        // Compute the address
-        u16 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
-                                   + (xcurs + ycurs * nbcols) * 2);
-
-        *car = GET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far);
-    } else {
-        // FIXME gfx mode
-        dprintf(1, "Read char in graphics mode\n");
-    }
-}
-
-// -------------------------------------------------------------------
-static void
-write_gfx_char_pl4(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols,
-                   u8 cheight)
-{
-    u8 *fdata_g;
-    switch (cheight) {
-    case 14:
-        fdata_g = vgafont14;
+    case '\r':
+        cp.x = 0;
         break;
-    case 16:
-        fdata_g = vgafont16;
+    case '\n':
+        cp.y++;
+        break;
+    case '\t':
+        do {
+            struct carattr dummyca = {' ', ca.attr, ca.use_attr};
+            vgafb_write_char(cp, dummyca);
+            cp.x++;
+        } while (cp.x < nbcols && cp.x % 8);
         break;
     default:
-        fdata_g = vgafont8;
-    }
-    u16 addr = xcurs + ycurs * cheight * nbcols;
-    u16 src = car * cheight;
-    outw(0x0f02, VGAREG_SEQU_ADDRESS);
-    outw(0x0205, VGAREG_GRDC_ADDRESS);
-    if (attr & 0x80)
-        outw(0x1803, VGAREG_GRDC_ADDRESS);
-    else
-        outw(0x0003, VGAREG_GRDC_ADDRESS);
-    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);
-            GET_FARVAR(SEG_GRAPH, *dest_far);
-            if (GET_GLOBAL(fdata_g[src + i]) & mask)
-                SET_FARVAR(SEG_GRAPH, *dest_far, attr & 0x0f);
-            else
-                SET_FARVAR(SEG_GRAPH, *dest_far, 0x00);
-        }
+        vgafb_write_char(cp, ca);
+        cp.x++;
     }
-    outw(0xff08, VGAREG_GRDC_ADDRESS);
-    outw(0x0005, VGAREG_GRDC_ADDRESS);
-    outw(0x0003, VGAREG_GRDC_ADDRESS);
-}
 
-// -------------------------------------------------------------------
-static void
-write_gfx_char_cga(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols, u8 bpp)
-{
-    u8 *fdata_g = vgafont8;
-    u16 addr = (xcurs * bpp) + ycurs * 320;
-    u16 src = car * 8;
-    u8 i;
-    for (i = 0; i < 8; i++) {
-        u8 *dest_far = (void*)(addr + (i >> 1) * 80);
-        if (i & 1)
-            dest_far += 0x2000;
-        u8 mask = 0x80;
-        if (bpp == 1) {
-            u8 data = 0;
-            if (attr & 0x80)
-                data = GET_FARVAR(SEG_CTEXT, *dest_far);
-            u8 j;
-            for (j = 0; j < 8; j++) {
-                if (GET_GLOBAL(fdata_g[src + i]) & mask) {
-                    if (attr & 0x80)
-                        data ^= (attr & 0x01) << (7 - j);
-                    else
-                        data |= (attr & 0x01) << (7 - j);
-                }
-                mask >>= 1;
-            }
-            SET_FARVAR(SEG_CTEXT, *dest_far, data);
-        } else {
-            while (mask > 0) {
-                u8 data = 0;
-                if (attr & 0x80)
-                    data = GET_FARVAR(SEG_CTEXT, *dest_far);
-                u8 j;
-                for (j = 0; j < 4; j++) {
-                    if (GET_GLOBAL(fdata_g[src + i]) & mask) {
-                        if (attr & 0x80)
-                            data ^= (attr & 0x03) << ((3 - j) * 2);
-                        else
-                            data |= (attr & 0x03) << ((3 - j) * 2);
-                    }
-                    mask >>= 1;
-                }
-                SET_FARVAR(SEG_CTEXT, *dest_far, data);
-                dest_far += 1;
-            }
-        }
+    // Do we need to wrap ?
+    if (cp.x == nbcols) {
+        cp.x = 0;
+        cp.y++;
     }
-}
-
-// -------------------------------------------------------------------
-static void
-write_gfx_char_lin(u8 car, u8 attr, u8 xcurs, u8 ycurs, u8 nbcols)
-{
-    u8 *fdata_g = vgafont8;
-    u16 addr = xcurs * 8 + ycurs * nbcols * 64;
-    u16 src = car * 8;
-    u8 i;
-    for (i = 0; i < 8; i++) {
-        u8 *dest_far = (void*)(addr + i * nbcols * 8);
-        u8 mask = 0x80;
-        u8 j;
-        for (j = 0; j < 8; j++) {
-            u8 data = 0x00;
-            if (GET_GLOBAL(fdata_g[src + i]) & mask)
-                data = attr;
-            SET_FARVAR(SEG_GRAPH, dest_far[j], data);
-            mask >>= 1;
-        }
+    // 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
-biosfn_write_char_attr(u8 car, u8 page, u8 attr, u16 count)
+write_attr_string(struct cursorpos *pcp, u16 count, u16 seg, u8 *offset_far)
 {
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-
-    // Get the cursor pos for the page
-    u16 cursor = biosfn_get_cursor_pos(page);
-    u8 xcurs = cursor & 0x00ff;
-    u8 ycurs = (cursor & 0xff00) >> 8;
-
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
-        // Compute the address
-        void *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
-                                    + (xcurs + ycurs * nbcols) * 2);
-
-        u16 dummy = ((u16)attr << 8) + car;
-        memset16_far(GET_GLOBAL(vmode_g->sstart), address_far, dummy, count * 2);
-        return;
-    }
+    while (count--) {
+        u8 car = GET_FARVAR(seg, *offset_far);
+        offset_far++;
+        u8 attr = GET_FARVAR(seg, *offset_far);
+        offset_far++;
 
-    // FIXME gfx mode not complete
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
-    u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-    while ((count-- > 0) && (xcurs < nbcols)) {
-        switch (GET_GLOBAL(vmode_g->memmodel)) {
-        case PLANAR4:
-        case PLANAR1:
-            write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols,
-                               cheight);
-            break;
-        case CGA:
-            write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
-            break;
-        case LINEAR8:
-            write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
-            break;
-        }
-        xcurs++;
+        struct carattr ca = {car, attr, 1};
+        write_teletype(pcp, ca);
     }
 }
 
-// -------------------------------------------------------------------
+// Write out a buffer of characters.
 static void
-biosfn_write_char_only(u8 car, u8 page, u8 attr, u16 count)
+write_string(struct cursorpos *pcp, u8 attr, u16 count, u16 seg, u8 *offset_far)
 {
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-
-    // Get the cursor pos for the page
-    u16 cursor = biosfn_get_cursor_pos(page);
-    u8 xcurs = cursor & 0x00ff;
-    u8 ycurs = (cursor & 0xff00) >> 8;
-
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
-    if (GET_GLOBAL(vmode_g->class) == TEXT) {
-        // Compute the address
-        u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
-                                  + (xcurs + ycurs * nbcols) * 2);
-        while (count-- > 0) {
-            SET_FARVAR(GET_GLOBAL(vmode_g->sstart), *address_far, car);
-            address_far += 2;
-        }
-        return;
-    }
+    while (count--) {
+        u8 car = GET_FARVAR(seg, *offset_far);
+        offset_far++;
 
-    // FIXME gfx mode not complete
-    struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-    u8 cheight = GET_GLOBAL(vparam_g->cheight);
-    u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-    while ((count-- > 0) && (xcurs < nbcols)) {
-        switch (GET_GLOBAL(vmode_g->memmodel)) {
-        case PLANAR4:
-        case PLANAR1:
-            write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols,
-                               cheight);
-            break;
-        case CGA:
-            write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
-            break;
-        case LINEAR8:
-            write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
-            break;
-        }
-        xcurs++;
+        struct carattr ca = {car, attr, 1};
+        write_teletype(pcp, ca);
     }
 }
 
-// -------------------------------------------------------------------
-static void
-biosfn_set_border_color(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x00, VGAREG_ACTL_ADDRESS);
-    u8 al = regs->bl & 0x0f;
-    if (al & 0x08)
-        al += 0x08;
-    outb(al, VGAREG_ACTL_WRITE_DATA);
-    u8 bl = regs->bl & 0x10;
-
-    int i;
-    for (i = 1; i < 4; i++) {
-        outb(i, VGAREG_ACTL_ADDRESS);
 
-        al = inb(VGAREG_ACTL_READ_DATA);
-        al &= 0xef;
-        al |= bl;
-        outb(al, VGAREG_ACTL_WRITE_DATA);
-    }
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
+/****************************************************************
+ * Save and restore bda state
+ ****************************************************************/
 
 static void
-biosfn_set_palette(struct bregs *regs)
+save_bda_state(u16 seg, struct saveBDAstate *info)
 {
-    inb(VGAREG_ACTL_RESET);
-    u8 bl = regs->bl & 0x01;
-    int i;
-    for (i = 1; i < 4; i++) {
-        outb(i, VGAREG_ACTL_ADDRESS);
-
-        u8 al = inb(VGAREG_ACTL_READ_DATA);
-        al &= 0xfe;
-        al |= bl;
-        outb(al, VGAREG_ACTL_WRITE_DATA);
-    }
-    outb(0x20, VGAREG_ACTL_ADDRESS);
+    SET_FARVAR(seg, info->video_mode, GET_BDA(video_mode));
+    SET_FARVAR(seg, info->video_cols, GET_BDA(video_cols));
+    SET_FARVAR(seg, info->video_pagesize, GET_BDA(video_pagesize));
+    SET_FARVAR(seg, info->crtc_address, GET_BDA(crtc_address));
+    SET_FARVAR(seg, info->video_rows, GET_BDA(video_rows));
+    SET_FARVAR(seg, info->char_height, GET_BDA(char_height));
+    SET_FARVAR(seg, info->video_ctl, GET_BDA(video_ctl));
+    SET_FARVAR(seg, info->video_switches, GET_BDA(video_switches));
+    SET_FARVAR(seg, info->modeset_ctl, GET_BDA(modeset_ctl));
+    SET_FARVAR(seg, info->cursor_type, GET_BDA(cursor_type));
+    u16 i;
+    for (i=0; i<8; i++)
+        SET_FARVAR(seg, info->cursor_pos[i], GET_BDA(cursor_pos[i]));
+    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, info->font0, GET_IVT(0x1f));
+    SET_FARVAR(seg, info->font1, GET_IVT(0x43));
+}
+
+static void
+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));
+    SET_BDA(video_pagesize, GET_FARVAR(seg, info->video_pagesize));
+    SET_BDA(crtc_address, GET_FARVAR(seg, info->crtc_address));
+    SET_BDA(video_rows, GET_FARVAR(seg, info->video_rows));
+    SET_BDA(char_height, GET_FARVAR(seg, info->char_height));
+    SET_BDA(video_ctl, GET_FARVAR(seg, info->video_ctl));
+    SET_BDA(video_switches, GET_FARVAR(seg, info->video_switches));
+    SET_BDA(modeset_ctl, GET_FARVAR(seg, info->modeset_ctl));
+    SET_BDA(cursor_type, GET_FARVAR(seg, info->cursor_type));
+    u16 i;
+    for (i = 0; i < 8; i++)
+        SET_BDA(cursor_pos[i], GET_FARVAR(seg, info->cursor_pos[i]));
+    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));
+    SET_IVT(0x43, GET_FARVAR(seg, info->font1));
 }
 
-// -------------------------------------------------------------------
-static void
-biosfn_write_pixel(u8 BH, u8 AL, u16 CX, u16 DX)
-{
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-    if (GET_GLOBAL(vmode_g->class) == TEXT)
-        return;
 
-    u8 *addr_far, mask, attr, data;
-    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);
-        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);
-        break;
-    case CGA:
-        if (GET_GLOBAL(vmode_g->pixbits) == 2)
-            addr_far = (void*)((CX >> 2) + (DX >> 1) * 80);
-        else
-            addr_far = (void*)((CX >> 3) + (DX >> 1) * 80);
-        if (DX & 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);
-        } else {
-            attr = (AL & 0x01) << (7 - (CX & 0x07));
-            mask = 0x01 << (7 - (CX & 0x07));
-        }
-        if (AL & 0x80) {
-            data ^= attr;
-        } else {
-            data &= ~mask;
-            data |= attr;
-        }
-        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);
-        break;
-    }
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_pixel(u8 BH, u16 CX, u16 DX, u16 *AX)
-{
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-    if (GET_GLOBAL(vmode_g->class) == TEXT)
-        return;
-
-    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);
-        attr = 0x00;
-        for (i = 0; i < 4; i++) {
-            outw((i << 8) | 0x04, VGAREG_GRDC_ADDRESS);
-            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 += 0x2000;
-        data = GET_FARVAR(SEG_CTEXT, *addr_far);
-        if (GET_GLOBAL(vmode_g->pixbits) == 2)
-            attr = (data >> ((3 - (CX & 0x03)) * 2)) & 0x03;
-        else
-            attr = (data >> (7 - (CX & 0x07))) & 0x01;
-        break;
-    case LINEAR8:
-        addr_far = (void*)(CX + DX * (GET_BDA(video_cols) * 8));
-        attr = GET_FARVAR(SEG_GRAPH, *addr_far);
-        break;
-    }
-    *AX = (*AX & 0xff00) | attr;
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_write_teletype(u8 car, u8 page, u8 attr, u8 flag)
-{                               // flag = WITH_ATTR / NO_ATTR
-    // special case if page is 0xff, use current page
-    if (page == 0xff)
-        page = GET_BDA(video_page);
-
-    // Get the mode
-    struct vgamode_s *vmode_g = find_vga_entry(GET_BDA(video_mode));
-    if (!vmode_g)
-        return;
-
-    // Get the cursor pos for the page
-    u16 cursor = biosfn_get_cursor_pos(page);
-    u8 xcurs = cursor & 0x00ff;
-    u8 ycurs = (cursor & 0xff00) >> 8;
-
-    // Get the dimensions
-    u16 nbrows = GET_BDA(video_rows) + 1;
-    u16 nbcols = GET_BDA(video_cols);
-
-    switch (car) {
-    case 7:
-        //FIXME should beep
-        break;
-
-    case 8:
-        if (xcurs > 0)
-            xcurs--;
-        break;
-
-    case '\r':
-        xcurs = 0;
-        break;
-
-    case '\n':
-        ycurs++;
-        break;
-
-    case '\t':
-        do {
-            biosfn_write_teletype(' ', page, attr, flag);
-            cursor = biosfn_get_cursor_pos(page);
-            xcurs = cursor & 0x00ff;
-            ycurs = (cursor & 0xff00) >> 8;
-        } while (xcurs % 8 == 0);
-        break;
-
-    default:
-
-        if (GET_GLOBAL(vmode_g->class) == TEXT) {
-            // Compute the address
-            u8 *address_far = (void*)(SCREEN_MEM_START(nbcols, nbrows, page)
-                                      + (xcurs + ycurs * nbcols) * 2);
-            // Write the char
-            SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[0], car);
-            if (flag == WITH_ATTR)
-                SET_FARVAR(GET_GLOBAL(vmode_g->sstart), address_far[1], attr);
-        } else {
-            // FIXME gfx mode not complete
-            struct VideoParam_s *vparam_g = GET_GLOBAL(vmode_g->vparam);
-            u8 cheight = GET_GLOBAL(vparam_g->cheight);
-            u8 bpp = GET_GLOBAL(vmode_g->pixbits);
-            switch (GET_GLOBAL(vmode_g->memmodel)) {
-            case PLANAR4:
-            case PLANAR1:
-                write_gfx_char_pl4(car, attr, xcurs, ycurs, nbcols, cheight);
-                break;
-            case CGA:
-                write_gfx_char_cga(car, attr, xcurs, ycurs, nbcols, bpp);
-                break;
-            case LINEAR8:
-                write_gfx_char_lin(car, attr, xcurs, ycurs, nbcols);
-                break;
-            }
-        }
-        xcurs++;
-    }
-
-    // Do we need to wrap ?
-    if (xcurs == nbcols) {
-        xcurs = 0;
-        ycurs++;
-    }
-    // Do we need to scroll ?
-    if (ycurs == nbrows) {
-        if (GET_GLOBAL(vmode_g->class) == TEXT)
-            biosfn_scroll(0x01, 0x07, 0, 0, nbrows - 1, nbcols - 1, page,
-                          SCROLL_UP);
-        else
-            biosfn_scroll(0x01, 0x00, 0, 0, nbrows - 1, nbcols - 1, page,
-                          SCROLL_UP);
-        ycurs -= 1;
-    }
-    // Set the cursor for the page
-    cursor = ycurs;
-    cursor <<= 8;
-    cursor += xcurs;
-    biosfn_set_cursor_pos(page, cursor);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_get_video_mode(struct bregs *regs)
-{
-    regs->bh = GET_BDA(video_page);
-    regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
-    regs->ah = GET_BDA(video_cols);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_overscan_border_color(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x11, VGAREG_ACTL_ADDRESS);
-    outb(regs->bh, VGAREG_ACTL_WRITE_DATA);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_all_palette_reg(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-
-    u8 *data_far = (u8*)(regs->dx + 0);
-    int i;
-    for (i = 0; i < 0x10; i++) {
-        outb(i, VGAREG_ACTL_ADDRESS);
-        u8 val = GET_FARVAR(regs->es, *data_far);
-        outb(val, VGAREG_ACTL_WRITE_DATA);
-        data_far++;
-    }
-    outb(0x11, VGAREG_ACTL_ADDRESS);
-    outb(GET_FARVAR(regs->es, *data_far), VGAREG_ACTL_WRITE_DATA);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_toggle_intensity(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x10, VGAREG_ACTL_ADDRESS);
-    u8 val = (inb(VGAREG_ACTL_READ_DATA) & 0x7f) | ((regs->bl & 0x01) << 3);
-    outb(val, VGAREG_ACTL_WRITE_DATA);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
+/****************************************************************
+ * VGA int 10 handler
+ ****************************************************************/
 
-// -------------------------------------------------------------------
+// set video mode
 void
-biosfn_set_single_palette_reg(u8 reg, u8 val)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(reg, VGAREG_ACTL_ADDRESS);
-    outb(val, VGAREG_ACTL_WRITE_DATA);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-u8
-biosfn_get_single_palette_reg(u8 reg)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(reg, VGAREG_ACTL_ADDRESS);
-    u8 v = inb(VGAREG_ACTL_READ_DATA);
-    inb(VGAREG_ACTL_RESET);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-    return v;
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_overscan_border_color(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x11, VGAREG_ACTL_ADDRESS);
-    regs->bh = inb(VGAREG_ACTL_READ_DATA);
-    inb(VGAREG_ACTL_RESET);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_get_all_palette_reg(struct bregs *regs)
-{
-    u8 *data_far = (u8*)(regs->dx + 0);
-    int i;
-    for (i = 0; i < 0x10; i++) {
-        inb(VGAREG_ACTL_RESET);
-        outb(i, VGAREG_ACTL_ADDRESS);
-        SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
-        data_far++;
-    }
-    inb(VGAREG_ACTL_RESET);
-    outb(0x11, VGAREG_ACTL_ADDRESS);
-    SET_FARVAR(regs->es, *data_far, inb(VGAREG_ACTL_READ_DATA));
-    inb(VGAREG_ACTL_RESET);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_single_dac_reg(struct bregs *regs)
-{
-    outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
-    outb(regs->dh, VGAREG_DAC_DATA);
-    outb(regs->ch, VGAREG_DAC_DATA);
-    outb(regs->cl, VGAREG_DAC_DATA);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_all_dac_reg(struct bregs *regs)
-{
-    outb(regs->bl, VGAREG_DAC_WRITE_ADDRESS);
-    u8 *data_far = (u8*)(regs->dx + 0);
-    int count = regs->cx;
-    while (count) {
-        outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
-        data_far++;
-        outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
-        data_far++;
-        outb(GET_FARVAR(regs->es, *data_far), VGAREG_DAC_DATA);
-        data_far++;
-        count--;
-    }
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_select_video_dac_color_page(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x10, VGAREG_ACTL_ADDRESS);
-    u8 val = inb(VGAREG_ACTL_READ_DATA);
-    if (!(regs->bl & 0x01)) {
-        val = (val & 0x7f) | (regs->bh << 7);
-        outb(val, VGAREG_ACTL_WRITE_DATA);
-        outb(0x20, VGAREG_ACTL_ADDRESS);
-        return;
-    }
-    inb(VGAREG_ACTL_RESET);
-    outb(0x14, VGAREG_ACTL_ADDRESS);
-    u8 bh = regs->bh;
-    if (!(val & 0x80))
-        bh <<= 2;
-    bh &= 0x0f;
-    outb(bh, VGAREG_ACTL_WRITE_DATA);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_single_dac_reg(struct bregs *regs)
+vga_set_mode(u8 mode, u8 noclearmem)
 {
-    outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
-    regs->dh = inb(VGAREG_DAC_DATA);
-    regs->ch = inb(VGAREG_DAC_DATA);
-    regs->cl = inb(VGAREG_DAC_DATA);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_all_dac_reg(struct bregs *regs)
-{
-    outb(regs->bl, VGAREG_DAC_READ_ADDRESS);
-    u8 *data_far = (u8*)(regs->dx + 0);
-    int count = regs->cx;
-    while (count) {
-        SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
-        data_far++;
-        SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
-        data_far++;
-        SET_FARVAR(regs->es, *data_far, inb(VGAREG_DAC_DATA));
-        data_far++;
-        count--;
-    }
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_pel_mask(struct bregs *regs)
-{
-    outb(regs->bl, VGAREG_PEL_MASK);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_pel_mask(struct bregs *regs)
-{
-    regs->bl = inb(VGAREG_PEL_MASK);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_video_dac_state(struct bregs *regs)
-{
-    inb(VGAREG_ACTL_RESET);
-    outb(0x10, VGAREG_ACTL_ADDRESS);
-    u8 val1 = inb(VGAREG_ACTL_READ_DATA) >> 7;
-
-    inb(VGAREG_ACTL_RESET);
-    outb(0x14, VGAREG_ACTL_ADDRESS);
-    u8 val2 = inb(VGAREG_ACTL_READ_DATA) & 0x0f;
-    if (!(val1 & 0x01))
-        val2 >>= 2;
-
-    inb(VGAREG_ACTL_RESET);
-    outb(0x20, VGAREG_ACTL_ADDRESS);
-
-    regs->bl = val1;
-    regs->bh = val2;
-}
-
-// -------------------------------------------------------------------
-static void
-get_font_access()
-{
-    outw(0x0100, VGAREG_SEQU_ADDRESS);
-    outw(0x0402, VGAREG_SEQU_ADDRESS);
-    outw(0x0704, VGAREG_SEQU_ADDRESS);
-    outw(0x0300, VGAREG_SEQU_ADDRESS);
-    outw(0x0204, VGAREG_GRDC_ADDRESS);
-    outw(0x0005, VGAREG_GRDC_ADDRESS);
-    outw(0x0406, VGAREG_GRDC_ADDRESS);
-}
-
-static void
-release_font_access()
-{
-    outw(0x0100, VGAREG_SEQU_ADDRESS);
-    outw(0x0302, VGAREG_SEQU_ADDRESS);
-    outw(0x0304, VGAREG_SEQU_ADDRESS);
-    outw(0x0300, VGAREG_SEQU_ADDRESS);
-    u16 v = inw(VGAREG_READ_MISC_OUTPUT);
-    v = ((v & 0x01) << 10) | 0x0a06;
-    outw(v, VGAREG_GRDC_ADDRESS);
-    outw(0x0004, VGAREG_GRDC_ADDRESS);
-    outw(0x1005, VGAREG_GRDC_ADDRESS);
-}
-
-static void
-set_scan_lines(u8 lines)
-{
-    u16 crtc_addr = GET_BDA(crtc_address);
-    outb(0x09, crtc_addr);
-    u8 crtc_r9 = inb(crtc_addr + 1);
-    crtc_r9 = (crtc_r9 & 0xe0) | (lines - 1);
-    outb(crtc_r9, crtc_addr + 1);
-    if (lines == 8)
-        biosfn_set_cursor_shape(0x06, 0x07);
-    else
-        biosfn_set_cursor_shape(lines - 4, lines - 3);
-    SET_BDA(char_height, lines);
-    outb(0x12, crtc_addr);
-    u16 vde = inb(crtc_addr + 1);
-    outb(0x07, crtc_addr);
-    u8 ovl = inb(crtc_addr + 1);
-    vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1);
-    u8 rows = vde / lines;
-    SET_BDA(video_rows, rows - 1);
-    u16 cols = GET_BDA(video_cols);
-    SET_BDA(video_pagesize, rows * cols * 2);
-}
-
-static void
-biosfn_load_text_user_pat(u8 AL, u16 ES, u16 BP, u16 CX, u16 DX, u8 BL,
-                          u8 BH)
-{
-    get_font_access();
-    u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
-    u16 i;
-    for (i = 0; i < CX; i++) {
-        void *src_far = (void*)(BP + i * BH);
-        void *dest_far = (void*)(blockaddr + (DX + i) * 32);
-        memcpy_far(SEG_GRAPH, dest_far, ES, src_far, BH);
-    }
-    release_font_access();
-    if (AL >= 0x10)
-        set_scan_lines(BH);
-}
-
-static void
-biosfn_load_text_8_14_pat(u8 AL, u8 BL)
-{
-    get_font_access();
-    u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
-    u16 i;
-    for (i = 0; i < 0x100; i++) {
-        u16 src = i * 14;
-        void *dest_far = (void*)(blockaddr + i * 32);
-        memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont14[src], 14);
-    }
-    release_font_access();
-    if (AL >= 0x10)
-        set_scan_lines(14);
-}
-
-static void
-biosfn_load_text_8_8_pat(u8 AL, u8 BL)
-{
-    get_font_access();
-    u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
-    u16 i;
-    for (i = 0; i < 0x100; i++) {
-        u16 src = i * 8;
-        void *dest_far = (void*)(blockaddr + i * 32);
-        memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont8[src], 8);
-    }
-    release_font_access();
-    if (AL >= 0x10)
-        set_scan_lines(8);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_set_text_block_specifier(struct bregs *regs)
-{
-    outw((regs->bl << 8) | 0x03, VGAREG_SEQU_ADDRESS);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_load_text_8_16_pat(u8 AL, u8 BL)
-{
-    get_font_access();
-    u16 blockaddr = ((BL & 0x03) << 14) + ((BL & 0x04) << 11);
-    u16 i;
-    for (i = 0; i < 0x100; i++) {
-        u16 src = i * 16;
-        void *dest_far = (void*)(blockaddr + i * 32);
-        memcpy_far(SEG_GRAPH, dest_far, get_global_seg(), &vgafont16[src], 16);
-    }
-    release_font_access();
-    if (AL >= 0x10)
-        set_scan_lines(16);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_get_font_info(u8 BH, u16 *ES, u16 *BP, u16 *CX, u16 *DX)
-{
-    switch (BH) {
-    case 0x00: {
-        u32 segoff = GET_IVT(0x1f).segoff;
-        *ES = segoff >> 16;
-        *BP = segoff;
-        break;
-    }
-    case 0x01: {
-        u32 segoff = GET_IVT(0x43).segoff;
-        *ES = segoff >> 16;
-        *BP = segoff;
-        break;
-    }
-    case 0x02:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont14;
-        break;
-    case 0x03:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont8;
-        break;
-    case 0x04:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont8 + 128 * 8;
-        break;
-    case 0x05:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont14alt;
-        break;
-    case 0x06:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont16;
-        break;
-    case 0x07:
-        *ES = get_global_seg();
-        *BP = (u32)vgafont16alt;
-        break;
-    default:
-        dprintf(1, "Get font info BH(%02x) was discarded\n", BH);
+    // find the entry in the video modes
+    struct vgamode_s *vmode_g = find_vga_entry(mode);
+    dprintf(1, "mode search %02x found %p\n", mode, vmode_g);
+    if (!vmode_g)
         return;
-    }
-    // Set byte/char of on screen font
-    *CX = GET_BDA(char_height) & 0xff;
-
-    // Set Highest char row
-    *DX = GET_BDA(video_rows);
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_get_ega_info(struct bregs *regs)
-{
-    regs->cx = GET_BDA(video_switches) & 0x0f;
-    regs->ax = GET_BDA(crtc_address);
-    if (regs->ax == VGAREG_MDA_CRTC_ADDRESS)
-        regs->bx = 0x0103;
-    else
-        regs->bx = 0x0003;
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_select_vert_res(struct bregs *regs)
-{
-    u8 mctl = GET_BDA(modeset_ctl);
-    u8 vswt = GET_BDA(video_switches);
-
-    switch (regs->al) {
-    case 0x00:
-        // 200 lines
-        mctl = (mctl & ~0x10) | 0x80;
-        vswt = (vswt & ~0x0f) | 0x08;
-        break;
-    case 0x01:
-        // 350 lines
-        mctl &= ~0x90;
-        vswt = (vswt & ~0x0f) | 0x09;
-        break;
-    case 0x02:
-        // 400 lines
-        mctl = (mctl & ~0x80) | 0x10;
-        vswt = (vswt & ~0x0f) | 0x09;
-        break;
-    default:
-        dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
-        break;
-    }
-    SET_BDA(modeset_ctl, mctl);
-    SET_BDA(video_switches, vswt);
-    regs->ax = 0x1212;
-}
-
-static void
-biosfn_enable_default_palette_loading(struct bregs *regs)
-{
-    u8 v = (regs->al & 0x01) << 3;
-    u8 mctl = GET_BDA(video_ctl) & ~0x08;
-    SET_BDA(video_ctl, mctl | v);
-    regs->ax = 0x1212;
-}
-
-static void
-biosfn_enable_video_addressing(struct bregs *regs)
-{
-    u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
-    u8 v2 = inb(VGAREG_READ_MISC_OUTPUT) & ~0x02;
-    outb(v | v2, VGAREG_WRITE_MISC_OUTPUT);
-    regs->ax = 0x1212;
-}
-
-
-static void
-biosfn_enable_grayscale_summing(struct bregs *regs)
-{
-    u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
-    u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
-    SET_BDA(modeset_ctl, v | v2);
-    regs->ax = 0x1212;
-}
-
-static void
-biosfn_enable_cursor_emulation(struct bregs *regs)
-{
-    u8 v = (regs->al & 0x01) ^ 0x01;
-    u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
-    SET_BDA(modeset_ctl, v | v2);
-    regs->ax = 0x1212;
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_write_string(u8 flag, u8 page, u8 attr, u16 count, u8 row, u8 col,
-                    u16 seg, u8 *offset_far)
-{
-    // Read curs info for the page
-    u16 oldcurs = biosfn_get_cursor_pos(page);
-
-    // if row=0xff special case : use current cursor position
-    if (row == 0xff) {
-        col = oldcurs & 0x00ff;
-        row = (oldcurs & 0xff00) >> 8;
-    }
-
-    u16 newcurs = row;
-    newcurs <<= 8;
-    newcurs += col;
-    biosfn_set_cursor_pos(page, newcurs);
-
-    while (count-- != 0) {
-        u8 car = GET_FARVAR(seg, *offset_far);
-        offset_far++;
-        if ((flag & 0x02) != 0) {
-            attr = GET_FARVAR(seg, *offset_far);
-            offset_far++;
-        }
-
-        biosfn_write_teletype(car, page, attr, WITH_ATTR);
-    }
-
-    // Set back curs pos
-    if ((flag & 0x01) == 0)
-        biosfn_set_cursor_pos(page, oldcurs);
-}
 
-// -------------------------------------------------------------------
-static void
-biosfn_read_display_code(struct bregs *regs)
-{
-    regs->bx = GET_BDA(dcc_index);
-    regs->al = 0x1a;
-}
-
-static void
-biosfn_set_display_code(struct bregs *regs)
-{
-    SET_BDA(dcc_index, regs->bl);
-    dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
-    regs->al = 0x1a;
-}
-
-// -------------------------------------------------------------------
-static void
-biosfn_read_state_info(u16 BX, u16 ES, u16 DI)
-{
-    // Address of static functionality table
-    SET_FARVAR(ES, *(u16*)(DI + 0x00), (u32)static_functionality);
-    SET_FARVAR(ES, *(u16*)(DI + 0x02), get_global_seg());
-
-    // Hard coded copy from BIOS area. Should it be cleaner ?
-    memcpy_far(ES, (void*)(DI + 0x04), SEG_BDA, (void*)0x49, 30);
-    memcpy_far(ES, (void*)(DI + 0x22), SEG_BDA, (void*)0x84, 3);
-
-    SET_FARVAR(ES, *(u8*)(DI + 0x25), GET_BDA(dcc_index));
-    SET_FARVAR(ES, *(u8*)(DI + 0x26), 0);
-    SET_FARVAR(ES, *(u8*)(DI + 0x27), 16);
-    SET_FARVAR(ES, *(u8*)(DI + 0x28), 0);
-    SET_FARVAR(ES, *(u8*)(DI + 0x29), 8);
-    SET_FARVAR(ES, *(u8*)(DI + 0x2a), 2);
-    SET_FARVAR(ES, *(u8*)(DI + 0x2b), 0);
-    SET_FARVAR(ES, *(u8*)(DI + 0x2c), 0);
-    SET_FARVAR(ES, *(u8*)(DI + 0x31), 3);
-    SET_FARVAR(ES, *(u8*)(DI + 0x32), 0);
+    // Read the bios mode set control
+    u8 modeset_ctl = GET_BDA(modeset_ctl);
 
-    memset_far(ES, (void*)(DI + 0x33), 0, 13);
-}
+    // Then we know the number of lines
+// FIXME
 
-// -------------------------------------------------------------------
-// -------------------------------------------------------------------
-static u16
-biosfn_read_video_state_size(u16 CX)
-{
-    u16 size = 0;
-    if (CX & 1)
-        size += 0x46;
-    if (CX & 2)
-        size += (5 + 8 + 5) * 2 + 6;
-    if (CX & 4)
-        size += 3 + 256 * 3 + 1;
-    return size;
-}
+    // if palette loading (bit 3 of modeset ctl = 0)
+    if ((modeset_ctl & 0x08) == 0) {    // Set the PEL mask
+        vgahw_set_pel_mask(GET_GLOBAL(vmode_g->pelmask));
 
-static u16
-biosfn_save_video_state(u16 CX, u16 ES, u16 BX)
-{
-    u16 crtc_addr = GET_BDA(crtc_address);
-    if (CX & 1) {
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_ADDRESS));
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr));
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_ADDRESS));
-        BX++;
-        inb(VGAREG_ACTL_RESET);
-        u16 ar_index = inb(VGAREG_ACTL_ADDRESS);
-        SET_FARVAR(ES, *(u8*)(BX+0), ar_index);
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_READ_FEATURE_CTL));
-        BX++;
+        // From which palette
+        u8 *palette_g = GET_GLOBAL(vmode_g->dac);
+        u16 palsize = GET_GLOBAL(vmode_g->dacsize) / 3;
 
+        // Always 256*3 values
+        vgahw_set_dac_regs(get_global_seg(), palette_g, 0, palsize);
         u16 i;
-        for (i = 1; i <= 4; i++) {
-            outb(i, VGAREG_SEQU_ADDRESS);
-            SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
-            BX++;
-        }
-        outb(0, VGAREG_SEQU_ADDRESS);
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_SEQU_DATA));
-        BX++;
-
-        for (i = 0; i <= 0x18; i++) {
-            outb(i, crtc_addr);
-            SET_FARVAR(ES, *(u8*)(BX+0), inb(crtc_addr + 1));
-            BX++;
+        for (i = palsize; i < 0x0100; i++) {
+            static u8 rgb[3] VAR16;
+            vgahw_set_dac_regs(get_global_seg(), rgb, i, 1);
         }
 
-        for (i = 0; i <= 0x13; i++) {
-            inb(VGAREG_ACTL_RESET);
-            outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
-            SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_ACTL_READ_DATA));
-            BX++;
-        }
-        inb(VGAREG_ACTL_RESET);
+        if ((modeset_ctl & 0x02) == 0x02)
+            perform_gray_scale_summing(0x00, 0x100);
+    }
 
-        for (i = 0; i <= 8; i++) {
-            outb(i, VGAREG_GRDC_ADDRESS);
-            SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_GRDC_DATA));
-            BX++;
-        }
+    vgahw_set_mode(vmode_g);
 
-        SET_FARVAR(ES, *(u16*)(BX+0), crtc_addr);
-        BX += 2;
-
-        /* XXX: read plane latches */
-        SET_FARVAR(ES, *(u8*)(BX+0), 0);
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), 0);
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), 0);
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), 0);
-        BX++;
-    }
-    if (CX & 2) {
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_mode));
-        BX++;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_cols));
-        BX += 2;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagesize));
-        BX += 2;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(crtc_address));
-        BX += 2;
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_rows));
-        BX++;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(char_height));
-        BX += 2;
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_ctl));
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_switches));
-        BX++;
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(modeset_ctl));
-        BX++;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_type));
-        BX += 2;
-        u16 i;
-        for (i = 0; i < 8; i++) {
-            SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(cursor_pos[i]));
-            BX += 2;
-        }
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_BDA(video_pagestart));
-        BX += 2;
-        SET_FARVAR(ES, *(u8*)(BX+0), GET_BDA(video_page));
-        BX++;
-        /* current font */
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_FARVAR(0, *(u16*)(0x1f * 4)));
-        BX += 2;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_FARVAR(0, *(u16*)(0x1f * 4 + 2)));
-        BX += 2;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_FARVAR(0, *(u16*)(0x43 * 4)));
-        BX += 2;
-        SET_FARVAR(ES, *(u16*)(BX+0), GET_FARVAR(0, *(u16*)(0x43 * 4 + 2)));
-        BX += 2;
-    }
-    if (CX & 4) {
-        /* XXX: check this */
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_STATE));
-        BX++;                   /* read/write mode dac */
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_WRITE_ADDRESS));
-        BX++;                   /* pix address */
-        SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_PEL_MASK));
-        BX++;
-        // Set the whole dac always, from 0
-        outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
-        u16 i;
-        for (i = 0; i < 256 * 3; i++) {
-            SET_FARVAR(ES, *(u8*)(BX+0), inb(VGAREG_DAC_DATA));
-            BX++;
-        }
-        SET_FARVAR(ES, *(u8*)(BX+0), 0);
-        BX++;                   /* color select register */
-    }
-    return BX;
-}
+    if (noclearmem == 0x00)
+        clear_screen(vmode_g);
 
-static u16
-biosfn_restore_video_state(u16 CX, u16 ES, u16 BX)
-{
-    if (CX & 1) {
-        // Reset Attribute Ctl flip-flop
-        inb(VGAREG_ACTL_RESET);
+    // Set CRTC address VGA or MDA
+    u16 crtc_addr = VGAREG_VGA_CRTC_ADDRESS;
+    if (GET_GLOBAL(vmode_g->memmodel) == MTEXT)
+        crtc_addr = VGAREG_MDA_CRTC_ADDRESS;
 
-        u16 crtc_addr = GET_FARVAR(ES, *(u16*)(BX + 0x40));
-        u16 addr1 = BX;
-        BX += 5;
+    // Set the BIOS mem
+    u16 cheight = GET_GLOBAL(vmode_g->cheight);
+    SET_BDA(video_mode, mode);
+    SET_BDA(video_cols, GET_GLOBAL(vmode_g->twidth));
+    SET_BDA(video_pagesize, GET_GLOBAL(vmode_g->slength));
+    SET_BDA(crtc_address, crtc_addr);
+    SET_BDA(video_rows, GET_GLOBAL(vmode_g->theight)-1);
+    SET_BDA(char_height, cheight);
+    SET_BDA(video_ctl, (0x60 | noclearmem));
+    SET_BDA(video_switches, 0xF9);
+    SET_BDA(modeset_ctl, GET_BDA(modeset_ctl) & 0x7f);
 
-        u16 i;
-        for (i = 1; i <= 4; i++) {
-            outb(i, VGAREG_SEQU_ADDRESS);
-            outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
-            BX++;
-        }
-        outb(0, VGAREG_SEQU_ADDRESS);
-        outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_SEQU_DATA);
-        BX++;
-
-        // Disable CRTC write protection
-        outw(0x0011, crtc_addr);
-        // Set CRTC regs
-        for (i = 0; i <= 0x18; i++) {
-            if (i != 0x11) {
-                outb(i, crtc_addr);
-                outb(GET_FARVAR(ES, *(u8*)(BX+0)), crtc_addr + 1);
-            }
-            BX++;
-        }
-        // select crtc base address
-        u16 v = inb(VGAREG_READ_MISC_OUTPUT) & ~0x01;
-        if (crtc_addr == VGAREG_VGA_CRTC_ADDRESS)
-            v |= 0x01;
-        outb(v, VGAREG_WRITE_MISC_OUTPUT);
-
-        // enable write protection if needed
-        outb(0x11, crtc_addr);
-        outb(GET_FARVAR(ES, *(u8*)(BX - 0x18 + 0x11)), crtc_addr + 1);
-
-        // Set Attribute Ctl
-        u16 ar_index = GET_FARVAR(ES, *(u8*)(addr1 + 0x03));
-        inb(VGAREG_ACTL_RESET);
-        for (i = 0; i <= 0x13; i++) {
-            outb(i | (ar_index & 0x20), VGAREG_ACTL_ADDRESS);
-            outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_ACTL_WRITE_DATA);
-            BX++;
-        }
-        outb(ar_index, VGAREG_ACTL_ADDRESS);
-        inb(VGAREG_ACTL_RESET);
+    // 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
+            , SEGOFF(get_global_seg(), (u32)&video_save_pointer_table));
 
-        for (i = 0; i <= 8; i++) {
-            outb(i, VGAREG_GRDC_ADDRESS);
-            outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_GRDC_DATA);
-            BX++;
-        }
-        BX += 2;                /* crtc_addr */
-        BX += 4;                /* plane latches */
-
-        outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_SEQU_ADDRESS);
-        addr1++;
-        outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr);
-        addr1++;
-        outb(GET_FARVAR(ES, *(u8*)(addr1+0)), VGAREG_GRDC_ADDRESS);
-        addr1++;
-        addr1++;
-        outb(GET_FARVAR(ES, *(u8*)(addr1+0)), crtc_addr - 0x4 + 0xa);
-        addr1++;
+    // FIXME
+    SET_BDA(video_msr, 0x00); // Unavailable on vanilla vga, but...
+    SET_BDA(video_pal, 0x00); // Unavailable on vanilla vga, but...
+
+    // Set cursor shape
+    if (GET_GLOBAL(vmode_g->memmodel) & TEXT)
+        set_cursor_shape(0x06, 0x07);
+    // Set cursor pos for page 0..7
+    int i;
+    for (i = 0; i < 8; i++) {
+        struct cursorpos cp = {0, 0, i};
+        set_cursor_pos(cp);
     }
-    if (CX & 2) {
-        SET_BDA(video_mode, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        SET_BDA(video_cols, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        SET_BDA(video_pagesize, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        SET_BDA(crtc_address, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        SET_BDA(video_rows, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        SET_BDA(char_height, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        SET_BDA(video_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        SET_BDA(video_switches, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        SET_BDA(modeset_ctl, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        SET_BDA(cursor_type, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        u16 i;
-        for (i = 0; i < 8; i++) {
-            SET_BDA(cursor_pos[i], GET_FARVAR(ES, *(u16*)(BX+0)));
-            BX += 2;
-        }
-        SET_BDA(video_pagestart, GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 2;
-        SET_BDA(video_page, GET_FARVAR(ES, *(u8*)(BX+0)));
-        BX++;
-        /* current font */
-        SET_IVT(0x1f, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 4;
-        SET_IVT(0x43, GET_FARVAR(ES, *(u16*)(BX+2)), GET_FARVAR(ES, *(u16*)(BX+0)));
-        BX += 4;
+
+    // Set active page 0
+    set_active_page(0x00);
+
+    // Write the fonts in memory
+    if (GET_GLOBAL(vmode_g->memmodel) & TEXT) {
+        call16_vgaint(0x1104, 0);
+        call16_vgaint(0x1103, 0);
     }
-    if (CX & 4) {
-        BX++;
-        u16 v = GET_FARVAR(ES, *(u8*)(BX+0));
-        BX++;
-        outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_PEL_MASK);
-        BX++;
-        // Set the whole dac always, from 0
-        outb(0x00, VGAREG_DAC_WRITE_ADDRESS);
-        u16 i;
-        for (i = 0; i < 256 * 3; i++) {
-            outb(GET_FARVAR(ES, *(u8*)(BX+0)), VGAREG_DAC_DATA);
-            BX++;
-        }
-        BX++;
-        outb(v, VGAREG_DAC_WRITE_ADDRESS);
+    // Set the ints 0x1F and 0x43
+    SET_IVT(0x1f, SEGOFF(get_global_seg(), (u32)&vgafont8[128 * 8]));
+
+    switch (cheight) {
+    case 8:
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont8));
+        break;
+    case 14:
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont14));
+        break;
+    case 16:
+        SET_IVT(0x43, SEGOFF(get_global_seg(), (u32)vgafont16));
+        break;
     }
-    return BX;
 }
 
-
-/****************************************************************
- * VGA int 10 handler
- ****************************************************************/
-
 static void
 handle_1000(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_video_mode(regs->al);
-    switch(regs->al & 0x7F) {
-    case 6:
-        regs->al = 0x3F;
-        break;
-    case 0:
-    case 1:
-    case 2:
-    case 3:
-    case 4:
-    case 5:
-    case 7:
-        regs->al = 0x30;
-        break;
-    default:
+    u8 noclearmem = regs->al & 0x80;
+    u8 mode = regs->al & 0x7f;
+
+    // Set regs->al
+    if (mode > 7)
         regs->al = 0x20;
+    else if (mode == 6)
+        regs->al = 0x3f;
+    else
+        regs->al = 0x30;
+
+    if (CONFIG_VGA_CIRRUS) {
+        int ret = cirrus_set_video_mode(mode, noclearmem);
+        if (ret)
+            return;
     }
+
+    if (vbe_enabled())
+        vbe_hires_enable(0);
+
+    vga_set_mode(mode, noclearmem);
 }
 
 static void
 handle_1001(struct bregs *regs)
 {
-    biosfn_set_cursor_shape(regs->ch, regs->cl);
+    set_cursor_shape(regs->ch, regs->cl);
 }
 
 static void
 handle_1002(struct bregs *regs)
 {
-    biosfn_set_cursor_pos(regs->bh, regs->dx);
+    struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
+    set_cursor_pos(cp);
 }
 
 static void
 handle_1003(struct bregs *regs)
 {
-    regs->cx = biosfn_get_cursor_shape(regs->bh);
-    regs->dx = biosfn_get_cursor_pos(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;
 }
 
 // Read light pen pos (unimplemented)
@@ -1975,57 +517,88 @@ 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
+verify_scroll(struct bregs *regs, int dir)
+{
+    u8 page = GET_BDA(video_page);
+    struct cursorpos ul = {regs->cl, regs->ch, page};
+    struct cursorpos lr = {regs->dl, regs->dh, page};
+
+    u16 nbrows = GET_BDA(video_rows) + 1;
+    if (lr.y >= nbrows)
+        lr.y = nbrows - 1;
+    u16 nbcols = GET_BDA(video_cols);
+    if (lr.x >= nbcols)
+        lr.x = nbcols - 1;
+
+    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
 handle_1006(struct bregs *regs)
 {
-    biosfn_scroll(regs->al, regs->bh, regs->ch, regs->cl, regs->dh, regs->dl
-                  , 0xFF, SCROLL_UP);
+    verify_scroll(regs, 1);
 }
 
 static void
 handle_1007(struct bregs *regs)
 {
-    biosfn_scroll(regs->al, regs->bh, regs->ch, regs->cl, regs->dh, regs->dl
-                  , 0xFF, SCROLL_DOWN);
+    verify_scroll(regs, -1);
 }
 
 static void
 handle_1008(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_char_attr(regs->bh, &regs->ax);
+    struct carattr ca = vgafb_read_char(get_cursor_pos(regs->bh));
+    regs->al = ca.car;
+    regs->ah = ca.attr;
+}
+
+static void noinline
+write_chars(u8 page, struct carattr ca, u16 count)
+{
+    struct cursorpos cp = get_cursor_pos(page);
+    while (count--) {
+        vgafb_write_char(cp, ca);
+        cp.x++;
+    }
 }
 
 static void
 handle_1009(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_write_char_attr(regs->al, regs->bh, regs->bl, regs->cx);
+    struct carattr ca = {regs->al, regs->bl, 1};
+    write_chars(regs->bh, ca, regs->cx);
 }
 
 static void
 handle_100a(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_write_char_only(regs->al, regs->bh, regs->bl, regs->cx);
+    struct carattr ca = {regs->al, regs->bl, 0};
+    write_chars(regs->bh, ca, regs->cx);
 }
 
 
 static void
 handle_100b00(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_border_color(regs);
+    vgahw_set_border_color(regs->bl);
 }
 
 static void
 handle_100b01(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_palette(regs);
+    vgahw_set_palette(regs->bl);
 }
 
 static void
@@ -2048,30 +621,34 @@ 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 !
-    biosfn_write_teletype(regs->al, 0xff, regs->bl, NO_ATTR);
+    struct carattr ca = {regs->al, regs->bl, 0};
+    struct cursorpos cp = get_cursor_pos(0xff);
+    write_teletype(&cp, ca);
+    set_cursor_pos(cp);
 }
 
 static void
 handle_100f(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_get_video_mode(regs);
+    regs->bh = GET_BDA(video_page);
+    regs->al = GET_BDA(video_mode) | (GET_BDA(video_ctl) & 0x80);
+    regs->ah = GET_BDA(video_cols);
 }
 
 
@@ -2080,28 +657,25 @@ handle_101000(struct bregs *regs)
 {
     if (regs->bl > 0x14)
         return;
-    biosfn_set_single_palette_reg(regs->bl, regs->bh);
+    vgahw_set_single_palette_reg(regs->bl, regs->bh);
 }
 
 static void
 handle_101001(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_overscan_border_color(regs);
+    vgahw_set_overscan_border_color(regs->bh);
 }
 
 static void
 handle_101002(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_all_palette_reg(regs);
+    vgahw_set_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
 }
 
 static void
 handle_101003(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_toggle_intensity(regs);
+    vgahw_toggle_intensity(regs->bl);
 }
 
 static void
@@ -2109,83 +683,78 @@ handle_101007(struct bregs *regs)
 {
     if (regs->bl > 0x14)
         return;
-    regs->bh = biosfn_get_single_palette_reg(regs->bl);
+    regs->bh = vgahw_get_single_palette_reg(regs->bl);
 }
 
 static void
 handle_101008(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_overscan_border_color(regs);
+    regs->bh = vgahw_get_overscan_border_color();
 }
 
 static void
 handle_101009(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_get_all_palette_reg(regs);
+    vgahw_get_all_palette_reg(regs->es, (u8*)(regs->dx + 0));
 }
 
-static void
+static void noinline
 handle_101010(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_single_dac_reg(regs);
+    u8 rgb[3] = {regs->dh, regs->ch, regs->cl};
+    vgahw_set_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
 }
 
 static void
 handle_101012(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_all_dac_reg(regs);
+    vgahw_set_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
 }
 
 static void
 handle_101013(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_select_video_dac_color_page(regs);
+    vgahw_select_video_dac_color_page(regs->bl, regs->bh);
 }
 
-static void
+static void noinline
 handle_101015(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_single_dac_reg(regs);
+    u8 rgb[3];
+    vgahw_get_dac_regs(GET_SEG(SS), rgb, regs->bx, 1);
+    regs->dh = rgb[0];
+    regs->ch = rgb[1];
+    regs->cl = rgb[2];
 }
 
 static void
 handle_101017(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_all_dac_reg(regs);
+    vgahw_get_dac_regs(regs->es, (u8*)(regs->dx + 0), regs->bx, regs->cx);
 }
 
 static void
 handle_101018(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_pel_mask(regs);
+    vgahw_set_pel_mask(regs->bl);
 }
 
 static void
 handle_101019(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_pel_mask(regs);
+    regs->bl = vgahw_get_pel_mask();
 }
 
 static void
 handle_10101a(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_video_dac_state(regs);
+    vgahw_read_video_dac_state(&regs->bl, &regs->bh);
 }
 
 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
@@ -2222,69 +791,112 @@ handle_1010(struct bregs *regs)
 static void
 handle_101100(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_load_text_user_pat(regs->al, regs->es, 0 // XXX - regs->bp
-                              , regs->cx, regs->dx, regs->bl, regs->bh);
+    vgafb_load_font(regs->es, (void*)(regs->bp+0), regs->cx
+                    , regs->dx, regs->bl, regs->bh);
 }
 
 static void
 handle_101101(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_load_text_8_14_pat(regs->al, regs->bl);
+    vgafb_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
 }
 
 static void
 handle_101102(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_load_text_8_8_pat(regs->al, regs->bl);
+    vgafb_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
 }
 
 static void
 handle_101103(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_text_block_specifier(regs);
+    vgahw_set_text_block_specifier(regs->bl);
 }
 
 static void
 handle_101104(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_load_text_8_16_pat(regs->al, regs->bl);
+    vgafb_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
 }
 
 static void
 handle_101110(struct bregs *regs)
 {
-    handle_101100(regs);
+    vgafb_load_font(regs->es, (void*)(regs->bp+0), regs->cx
+                    , regs->dx, regs->bl, regs->bh);
+    set_scan_lines(regs->bh);
 }
 
 static void
 handle_101111(struct bregs *regs)
 {
-    handle_101101(regs);
+    vgafb_load_font(get_global_seg(), vgafont14, 0x100, 0, regs->bl, 14);
+    set_scan_lines(14);
 }
 
 static void
 handle_101112(struct bregs *regs)
 {
-    handle_101102(regs);
+    vgafb_load_font(get_global_seg(), vgafont8, 0x100, 0, regs->bl, 8);
+    set_scan_lines(8);
 }
 
 static void
 handle_101114(struct bregs *regs)
 {
-    handle_101104(regs);
+    vgafb_load_font(get_global_seg(), vgafont16, 0x100, 0, regs->bl, 16);
+    set_scan_lines(16);
 }
 
 static void
 handle_101130(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_get_font_info(regs->bh, &regs->es, 0 // &regs->bp
-                         , &regs->cx, &regs->dx);
+    switch (regs->bh) {
+    case 0x00: {
+        struct segoff_s so = GET_IVT(0x1f);
+        regs->es = so.seg;
+        regs->bp = so.offset;
+        break;
+    }
+    case 0x01: {
+        struct segoff_s so = GET_IVT(0x43);
+        regs->es = so.seg;
+        regs->bp = so.offset;
+        break;
+    }
+    case 0x02:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont14;
+        break;
+    case 0x03:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont8;
+        break;
+    case 0x04:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont8 + 128 * 8;
+        break;
+    case 0x05:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont14alt;
+        break;
+    case 0x06:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont16;
+        break;
+    case 0x07:
+        regs->es = get_global_seg();
+        regs->bp = (u32)vgafont16alt;
+        break;
+    default:
+        dprintf(1, "Get font info BH(%02x) was discarded\n", regs->bh);
+        return;
+    }
+    // Set byte/char of on screen font
+    regs->cx = GET_BDA(char_height) & 0xff;
+
+    // Set Highest char row
+    regs->dx = GET_BDA(video_rows);
 }
 
 static void
@@ -2315,43 +927,76 @@ handle_1011(struct bregs *regs)
 static void
 handle_101210(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_get_ega_info(regs);
+    u16 crtc_addr = GET_BDA(crtc_address);
+    if (crtc_addr == VGAREG_MDA_CRTC_ADDRESS)
+        regs->bx = 0x0103;
+    else
+        regs->bx = 0x0003;
+    regs->cx = GET_BDA(video_switches) & 0x0f;
 }
 
 static void
 handle_101230(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_select_vert_res(regs);
+    u8 mctl = GET_BDA(modeset_ctl);
+    u8 vswt = GET_BDA(video_switches);
+    switch (regs->al) {
+    case 0x00:
+        // 200 lines
+        mctl = (mctl & ~0x10) | 0x80;
+        vswt = (vswt & ~0x0f) | 0x08;
+        break;
+    case 0x01:
+        // 350 lines
+        mctl &= ~0x90;
+        vswt = (vswt & ~0x0f) | 0x09;
+        break;
+    case 0x02:
+        // 400 lines
+        mctl = (mctl & ~0x80) | 0x10;
+        vswt = (vswt & ~0x0f) | 0x09;
+        break;
+    default:
+        dprintf(1, "Select vert res (%02x) was discarded\n", regs->al);
+        break;
+    }
+    SET_BDA(modeset_ctl, mctl);
+    SET_BDA(video_switches, vswt);
+    regs->al = 0x12;
 }
 
 static void
 handle_101231(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_enable_default_palette_loading(regs);
+    u8 v = (regs->al & 0x01) << 3;
+    u8 mctl = GET_BDA(video_ctl) & ~0x08;
+    SET_BDA(video_ctl, mctl | v);
+    regs->al = 0x12;
 }
 
 static void
 handle_101232(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_enable_video_addressing(regs);
+    vgahw_enable_video_addressing(regs->al);
+    regs->al = 0x12;
 }
 
 static void
 handle_101233(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_enable_grayscale_summing(regs);
+    u8 v = ((regs->al << 1) & 0x02) ^ 0x02;
+    u8 v2 = GET_BDA(modeset_ctl) & ~0x02;
+    SET_BDA(modeset_ctl, v | v2);
+    regs->al = 0x12;
 }
 
 static void
 handle_101234(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_enable_cursor_emulation(regs);
+    u8 v = (regs->al & 0x01) ^ 0x01;
+    u8 v2 = GET_BDA(modeset_ctl) & ~0x01;
+    SET_BDA(modeset_ctl, v | v2);
+    regs->al = 0x12;
 }
 
 static void
@@ -2393,27 +1038,38 @@ handle_1012(struct bregs *regs)
 }
 
 
-static void
+// Write string
+static void noinline
 handle_1013(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_write_string(regs->al, regs->bh, regs->bl, regs->cx
-                        , regs->dh, regs->dl, regs->es, 0); // regs->bp);
+    struct cursorpos cp = {regs->dl, regs->dh, regs->bh};
+    // 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);
 }
 
 
 static void
 handle_101a00(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_display_code(regs);
+    regs->bx = GET_BDA(dcc_index);
+    regs->al = 0x1a;
 }
 
 static void
 handle_101a01(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_set_display_code(regs);
+    SET_BDA(dcc_index, regs->bl);
+    dprintf(1, "Alternate Display code (%02x) was discarded\n", regs->bh);
+    regs->al = 0x1a;
 }
 
 static void
@@ -2433,11 +1089,47 @@ handle_101a(struct bregs *regs)
 }
 
 
+struct funcInfo {
+    struct segoff_s static_functionality;
+    u8 bda_0x49[30];
+    u8 bda_0x84[3];
+    u8 dcc_index;
+    u8 dcc_alt;
+    u16 colors;
+    u8 pages;
+    u8 scan_lines;
+    u8 primary_char;
+    u8 secondar_char;
+    u8 misc;
+    u8 non_vga_mode;
+    u8 reserved_2f[2];
+    u8 video_mem;
+    u8 save_flags;
+    u8 disp_info;
+    u8 reserved_34[12];
+};
+
 static void
 handle_101b(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_read_state_info(regs->bx, regs->es, regs->di);
+    u16 seg = regs->es;
+    struct funcInfo *info = (void*)(regs->di+0);
+    memset_far(seg, info, 0, sizeof(*info));
+    // Address of static functionality table
+    SET_FARVAR(seg, info->static_functionality
+               , SEGOFF(get_global_seg(), (u32)static_functionality));
+
+    // Hard coded copy from BIOS area. Should it be cleaner ?
+    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);
+    SET_FARVAR(seg, info->pages, 8);
+    SET_FARVAR(seg, info->scan_lines, 2);
+    SET_FARVAR(seg, info->video_mem, 3);
     regs->al = 0x1B;
 }
 
@@ -2445,22 +1137,54 @@ handle_101b(struct bregs *regs)
 static void
 handle_101c00(struct bregs *regs)
 {
-    // XXX - inline
-    regs->bx = biosfn_read_video_state_size(regs->cx);
+    u16 flags = regs->cx;
+    u16 size = 0;
+    if (flags & 1)
+        size += sizeof(struct saveVideoHardware);
+    if (flags & 2)
+        size += sizeof(struct saveBDAstate);
+    if (flags & 4)
+        size += sizeof(struct saveDACcolors);
+    regs->bx = size;
+    regs->al = 0x1c;
 }
 
 static void
 handle_101c01(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_save_video_state(regs->cx, regs->es, regs->bx);
+    u16 flags = regs->cx;
+    u16 seg = regs->es;
+    void *data = (void*)(regs->bx+0);
+    if (flags & 1) {
+        vgahw_save_state(seg, data);
+        data += sizeof(struct saveVideoHardware);
+    }
+    if (flags & 2) {
+        save_bda_state(seg, data);
+        data += sizeof(struct saveBDAstate);
+    }
+    if (flags & 4)
+        vgahw_save_dac_state(seg, data);
+    regs->al = 0x1c;
 }
 
 static void
 handle_101c02(struct bregs *regs)
 {
-    // XXX - inline
-    biosfn_restore_video_state(regs->cx, regs->es, regs->bx);
+    u16 flags = regs->cx;
+    u16 seg = regs->es;
+    void *data = (void*)(regs->bx+0);
+    if (flags & 1) {
+        vgahw_restore_state(seg, data);
+        data += sizeof(struct saveVideoHardware);
+    }
+    if (flags & 2) {
+        restore_bda_state(seg, data);
+        data += sizeof(struct saveBDAstate);
+    }
+    if (flags & 4)
+        vgahw_restore_dac_state(seg, data);
+    regs->al = 0x1c;
 }
 
 static void
@@ -2480,72 +1204,261 @@ handle_101c(struct bregs *regs)
     }
 }
 
-
 static void
 handle_104f00(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_return_controller_information(&AX,ES,DI);
-    // XXX - OR cirrus_vesa_00h
+    u16 seg = regs->es;
+    struct vbe_info *info = (void*)(regs->di+0);
+
+    if (GET_FARVAR(seg, info->signature) == VBE2_SIGNATURE) {
+        dprintf(4, "Get VBE Controller: VBE2 Signature found\n");
+    } else if (GET_FARVAR(seg, info->signature) == VESA_SIGNATURE) {
+        dprintf(4, "Get VBE Controller: VESA Signature found\n");
+    } else {
+        dprintf(4, "Get VBE Controller: Invalid Signature\n");
+    }
+
+    memset_far(seg, info, 0, sizeof(*info));
+
+    SET_FARVAR(seg, info->signature, VESA_SIGNATURE);
+
+    SET_FARVAR(seg, info->version, 0x0200);
+
+    SET_FARVAR(seg, info->oem_string,
+            SEGOFF(get_global_seg(), (u32)VBE_OEM_STRING));
+    SET_FARVAR(seg, info->capabilities[0], 0x1); /* 8BIT DAC */
+
+    /* We generate our mode list in the reserved field of the info block */
+    SET_FARVAR(seg, info->video_mode, SEGOFF(seg, regs->di + 34));
+
+    /* Total memory (in 64 blocks) */
+    SET_FARVAR(seg, info->total_memory, vbe_total_mem());
+
+    SET_FARVAR(seg, info->oem_vendor_string,
+            SEGOFF(get_global_seg(), (u32)VBE_VENDOR_STRING));
+    SET_FARVAR(seg, info->oem_product_string,
+            SEGOFF(get_global_seg(), (u32)VBE_PRODUCT_STRING));
+    SET_FARVAR(seg, info->oem_revision_string,
+            SEGOFF(get_global_seg(), (u32)VBE_REVISION_STRING));
+
+    /* Fill list of modes */
+    vbe_list_modes(seg, regs->di + 32);
+
+    regs->al = regs->ah; /* 0x4F, Function supported */
+    regs->ah = 0x0; /* 0x0, Function call successful */
 }
 
 static void
 handle_104f01(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_return_mode_information(&AX,CX,ES,DI);
-    // XXX - OR cirrus_vesa_01h
+    u16 seg = regs->es;
+    struct vbe_mode_info *info = (void*)(regs->di+0);
+    u16 mode = regs->cx;
+    struct vbe_modeinfo modeinfo;
+    int rc;
+
+    dprintf(1, "VBE mode info request: %x\n", mode);
+
+    rc = vbe_mode_info(mode, &modeinfo);
+    if (rc) {
+        dprintf(1, "VBE mode %x not found\n", mode);
+        regs->ax = 0x100;
+        return;
+    }
+
+    u16 mode_attr = VBE_MODE_ATTRIBUTE_SUPPORTED |
+                    VBE_MODE_ATTRIBUTE_EXTENDED_INFORMATION_AVAILABLE |
+                    VBE_MODE_ATTRIBUTE_COLOR_MODE |
+                    VBE_MODE_ATTRIBUTE_GRAPHICS_MODE;
+    if (modeinfo.depth == 4)
+        mode_attr |= VBE_MODE_ATTRIBUTE_TTY_BIOS_SUPPORT;
+    else
+        mode_attr |= VBE_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER_MODE;
+    SET_FARVAR(seg, info->mode_attributes, mode_attr);
+    SET_FARVAR(seg, info->winA_attributes,
+               VBE_WINDOW_ATTRIBUTE_RELOCATABLE |
+               VBE_WINDOW_ATTRIBUTE_READABLE |
+               VBE_WINDOW_ATTRIBUTE_WRITEABLE);
+    SET_FARVAR(seg, info->winB_attributes, 0);
+    SET_FARVAR(seg, info->win_granularity, 64); /* Bank size 64K */
+    SET_FARVAR(seg, info->win_size, 64); /* Bank size 64K */
+    SET_FARVAR(seg, info->winA_seg, 0xA000);
+    SET_FARVAR(seg, info->winB_seg, 0x0);
+    SET_FARVAR(seg, info->win_func_ptr, 0x0);
+    SET_FARVAR(seg, info->bytes_per_scanline, modeinfo.linesize);
+    SET_FARVAR(seg, info->xres, modeinfo.width);
+    SET_FARVAR(seg, info->yres, modeinfo.height);
+    SET_FARVAR(seg, info->xcharsize, 8);
+    SET_FARVAR(seg, info->ycharsize, 16);
+    if (modeinfo.depth == 4)
+        SET_FARVAR(seg, info->planes, 4);
+    else
+        SET_FARVAR(seg, info->planes, 1);
+    SET_FARVAR(seg, info->bits_per_pixel, modeinfo.depth);
+    SET_FARVAR(seg, info->banks,
+            (modeinfo.linesize * modeinfo.height + 65535) / 65536);
+    if (modeinfo.depth == 4)
+        SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_PLANAR);
+    else if (modeinfo.depth == 8)
+        SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_PACKED_PIXEL);
+    else
+        SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_DIRECT_COLOR);
+    SET_FARVAR(seg, info->bank_size, 0);
+    u32 pages = modeinfo.vram_size / (modeinfo.height * modeinfo.linesize);
+    if (modeinfo.depth == 4)
+        SET_FARVAR(seg, info->pages, (pages / 4) - 1);
+    else
+        SET_FARVAR(seg, info->pages, pages - 1);
+    SET_FARVAR(seg, info->reserved0, 1);
+
+    u8 r_size, r_pos, g_size, g_pos, b_size, b_pos, a_size, a_pos;
+
+    switch (modeinfo.depth) {
+    case 15: r_size = 5; r_pos = 10; g_size = 5; g_pos = 5;
+             b_size = 5; b_pos = 0; a_size = 1; a_pos = 15; break;
+    case 16: r_size = 5; r_pos = 11; g_size = 6; g_pos = 5;
+             b_size = 5; b_pos = 0; a_size = 0; a_pos = 0; break;
+    case 24: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
+             b_size = 8; b_pos = 0; a_size = 0; a_pos = 0; break;
+    case 32: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
+             b_size = 8; b_pos = 0; a_size = 8; a_pos = 24; break;
+    default: r_size = 0; r_pos = 0; g_size = 0; g_pos = 0;
+             b_size = 0; b_pos = 0; a_size = 0; a_pos = 0; break;
+    }
+
+    SET_FARVAR(seg, info->red_size, r_size);
+    SET_FARVAR(seg, info->red_pos, r_pos);
+    SET_FARVAR(seg, info->green_size, g_size);
+    SET_FARVAR(seg, info->green_pos, g_pos);
+    SET_FARVAR(seg, info->blue_size, b_size);
+    SET_FARVAR(seg, info->blue_pos, b_pos);
+    SET_FARVAR(seg, info->alpha_size, a_size);
+    SET_FARVAR(seg, info->alpha_pos, a_pos);
+
+    if (modeinfo.depth == 32)
+        SET_FARVAR(seg, info->directcolor_info,
+                   VBE_DIRECTCOLOR_RESERVED_BITS_AVAILABLE);
+    else
+        SET_FARVAR(seg, info->directcolor_info, 0);
+
+    if (modeinfo.depth > 4)
+        SET_FARVAR(seg, info->phys_base, modeinfo.phys_base);
+    else
+        SET_FARVAR(seg, info->phys_base, 0);
+
+    SET_FARVAR(seg, info->reserved1, 0);
+    SET_FARVAR(seg, info->reserved2, 0);
+    SET_FARVAR(seg, info->linear_bytes_per_scanline, modeinfo.linesize);
+    SET_FARVAR(seg, info->bank_pages, 0);
+    SET_FARVAR(seg, info->linear_pages, 0);
+    SET_FARVAR(seg, info->linear_red_size, r_size);
+    SET_FARVAR(seg, info->linear_red_pos, r_pos);
+    SET_FARVAR(seg, info->linear_green_size, g_size);
+    SET_FARVAR(seg, info->linear_green_pos, g_pos);
+    SET_FARVAR(seg, info->linear_blue_size, b_size);
+    SET_FARVAR(seg, info->linear_blue_pos, b_pos);
+    SET_FARVAR(seg, info->linear_alpha_size, a_size);
+    SET_FARVAR(seg, info->linear_alpha_pos, a_pos);
+    SET_FARVAR(seg, info->pixclock_max, 0);
+
+    regs->al = regs->ah; /* 0x4F, Function supported */
+    regs->ah = 0x0; /* 0x0, Function call successful */
 }
 
 static void
 handle_104f02(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_set_mode(&AX,BX,ES,DI);
-    // XXX - OR cirrus_vesa_02h
+    //u16 seg = regs->es;
+    //struct vbe_crtc_info *crtc_info = (void*)(regs->di+0);
+    u16 mode = regs->bx;
+    struct vbe_modeinfo modeinfo;
+    int rc;
+
+    dprintf(1, "VBE mode set: %x\n", mode);
+
+    if (mode < 0x100) { /* VGA */
+        dprintf(1, "set VGA mode %x\n", mode);
+
+        vbe_hires_enable(0);
+        vga_set_mode(mode, 0);
+    } else { /* VBE */
+        rc = vbe_mode_info(mode & 0x1ff, &modeinfo);
+        if (rc) {
+            dprintf(1, "VBE mode %x not found\n", mode & 0x1ff);
+            regs->ax = 0x100;
+            return;
+        }
+        vbe_hires_enable(1);
+        vbe_set_mode(mode & 0x1ff, &modeinfo);
+
+        if (mode & 0x4000) {
+            /* Linear frame buffer */
+            /* XXX: ??? */
+        }
+        if (!(mode & 0x8000)) {
+            vbe_clear_scr();
+        }
+    }
+
+    regs->al = regs->ah; /* 0x4F, Function supported */
+    regs->ah = 0x0; /* 0x0, Function call successful */
 }
 
 static void
 handle_104f03(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_return_current_mode
-    // XXX - OR cirrus_vesa_03h
+    if (!vbe_hires_enabled()) {
+        regs->bx = GET_BDA(video_mode);
+    } else {
+        regs->bx = vbe_curr_mode();
+    }
+
+    dprintf(1, "VBE current mode=%x\n", regs->bx);
+
+    regs->al = regs->ah; /* 0x4F, Function supported */
+    regs->ah = 0x0; /* 0x0, Function call successful */
 }
 
 static void
 handle_104f04(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_save_restore_state(&AX, CX, DX, ES, &BX);
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
 handle_104f05(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_display_window_control
-    // XXX - OR cirrus_vesa_05h
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
 handle_104f06(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_set_get_logical_scan_line_length
-    // XXX - OR cirrus_vesa_06h
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
 handle_104f07(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_set_get_display_start
-    // XXX - OR cirrus_vesa_07h
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
 handle_104f08(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_set_get_dac_palette_format
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
 handle_104f0a(struct bregs *regs)
 {
-    // XXX - vbe_biosfn_return_protected_mode_interface
+    debug_enter(regs, DEBUG_VGA_10);
+    regs->ax = 0x0100;
 }
 
 static void
@@ -2558,13 +1471,11 @@ handle_104fXX(struct bregs *regs)
 static void
 handle_104f(struct bregs *regs)
 {
-    if (! CONFIG_VBE) {
+    if (!vbe_enabled()) {
         handle_104fXX(regs);
         return;
     }
 
-    // XXX - check vbe_has_vbe_display()?
-
     switch (regs->al) {
     case 0x00: handle_104f00(regs); break;
     case 0x01: handle_104f01(regs); break;
@@ -2627,7 +1538,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)
@@ -2652,38 +1563,29 @@ init_bios_area()
     SET_BDA(video_msr, 0x09);
 }
 
-static void
-init_vga_card()
-{
-    // switch to color mode and enable CPU access 480 lines
-    outb(0xc3, VGAREG_WRITE_MISC_OUTPUT);
-    // more than 64k 3C4/04
-    outb(0x04, VGAREG_SEQU_ADDRESS);
-    outb(0x02, VGAREG_SEQU_DATA);
-}
-
 void VISIBLE16
 vga_post(struct bregs *regs)
 {
     debug_enter(regs, DEBUG_VGA_POST);
 
-    init_vga_card();
+    vgahw_init();
+
+    if (CONFIG_VGA_GEODELX)
+        geodelx_init();
 
     init_bios_area();
 
-    // vbe_init();
+    vbe_init(regs->ah, regs->al);
 
     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
 
-    // XXX: fill it
-    SET_VGA(video_save_pointer_table[0], (u32)video_param_table);
-    SET_VGA(video_save_pointer_table[1], get_global_seg());
+    build_video_param();
 
     // Fixup checksum
     extern u8 _rom_header_size, _rom_header_checksum;