www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/25/15:46:27

From: Nicolas Blais <eletech AT netrover DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: HELP::Plz check my code for some errors/optimization. (VESA.CPP)
Date: Wed, 25 Mar 1998 08:01:31 -0500
Organization: Elemental Technologies
Lines: 206
Message-ID: <3519002B.CF717049@netrover.com>
NNTP-Posting-Host: 198.168.87.56
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

This is a multi-part message in MIME format.
--------------AAD9C6BEFF5C90BCE047177D
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Attached is my VESA sub routine that I use to create most of my
programs.  Are there anything I don't need or I need?  Or anything I can

do better?  For example, when I want to output text on my screen, I use
put_char, ex:
for (p = "Hello World!"; *p;p++)
put_char(*p, 7);

You see, that's 2 lines of code for just 2 words.  Can I simplify this?
Also, how do I set it to 640x480x16bit?  Thanks, Nicolas Blais
//-----------------------------------------------------------------------------------------

--------------AAD9C6BEFF5C90BCE047177D
Content-Type: text/plain; charset=us-ascii; name="Vesa.cpp"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Vesa.cpp"

#include <dpmi.h>
#include <go32.h>
#include <sys/farptr.h>
#include <iostream.h>

typedef struct VESA_INFO
{
unsigned char  VESASignature[4]     __attribute__ ((packed));
unsigned short VESAVersion          __attribute__ ((packed));
unsigned long  OEMStringPtr         __attribute__ ((packed));
unsigned char  Capabilities[4]      __attribute__ ((packed));
unsigned long  VideoModePtr         __attribute__ ((packed));
unsigned short TotalMemory          __attribute__ ((packed));
unsigned short OemSoftwareRev       __attribute__ ((packed));
unsigned long  OemVendorNamePtr     __attribute__ ((packed));
unsigned long  OemProductNamePtr    __attribute__ ((packed));
unsigned long  OemProductRevPtr     __attribute__ ((packed));
unsigned char  Reserved[222]        __attribute__ ((packed));
unsigned char  OemData[256]         __attribute__ ((packed));
} VESA_INFO;

VESA_INFO vesa_info;

int get_vesa_info()
{
__dpmi_regs r;
long dosbuf;
int c;
dosbuf = __tb & 0xFFFFF;
for (c=0; c<sizeof(VESA_INFO); c++)
_farpokeb(_dos_ds, dosbuf+c, 0);
dosmemput("VBE2", 4, dosbuf);
r.x.ax = 0x4F00;
r.x.di = dosbuf & 0xF;
r.x.es = (dosbuf>>4) & 0xFFFF;
__dpmi_int(0x10, &r);
if (r.h.ah)
return -1;
dosmemget(dosbuf, sizeof(VESA_INFO), &vesa_info);
if (strncmp(vesa_info.VESASignature, "VESA", 4) != 0)
return -1;
return 0;
}

typedef struct MODE_INFO
{
unsigned short ModeAttributes       __attribute__ ((packed));
unsigned char  WinAAttributes       __attribute__ ((packed));
unsigned char  WinBAttributes       __attribute__ ((packed));
unsigned short WinGranularity       __attribute__ ((packed));
unsigned short WinSize              __attribute__ ((packed));
unsigned short WinASegment          __attribute__ ((packed));
unsigned short WinBSegment          __attribute__ ((packed));
unsigned long  WinFuncPtr           __attribute__ ((packed));
unsigned short BytesPerScanLine     __attribute__ ((packed));
unsigned short XResolution          __attribute__ ((packed));
unsigned short YResolution          __attribute__ ((packed));
unsigned char  XCharSize            __attribute__ ((packed));
unsigned char  YCharSize            __attribute__ ((packed));
unsigned char  NumberOfPlanes       __attribute__ ((packed));
unsigned char  BitsPerPixel         __attribute__ ((packed));
unsigned char  NumberOfBanks        __attribute__ ((packed));
unsigned char  MemoryModel          __attribute__ ((packed));
unsigned char  BankSize             __attribute__ ((packed));
unsigned char  NumberOfImagePages   __attribute__ ((packed));
unsigned char  Reserved_page        __attribute__ ((packed));
unsigned char  RedMaskSize          __attribute__ ((packed));
unsigned char  RedMaskPos           __attribute__ ((packed));
unsigned char  GreenMaskSize        __attribute__ ((packed));
unsigned char  GreenMaskPos         __attribute__ ((packed));
unsigned char  BlueMaskSize         __attribute__ ((packed));
unsigned char  BlueMaskPos          __attribute__ ((packed));
unsigned char  ReservedMaskSize     __attribute__ ((packed));
unsigned char  ReservedMaskPos      __attribute__ ((packed));
unsigned char  DirectColorModeInfo  __attribute__ ((packed));
unsigned long  PhysBasePtr          __attribute__ ((packed));
unsigned long  OffScreenMemOffset   __attribute__ ((packed));
unsigned short OffScreenMemSize     __attribute__ ((packed));
unsigned char  Reserved[206]        __attribute__ ((packed));
} MODE_INFO;

MODE_INFO mode_info;


int get_mode_info(int mode)
{
__dpmi_regs r;
long dosbuf;
int c;
dosbuf = __tb & 0xFFFFF;
for (c=0; c<sizeof(MODE_INFO); c++)
_farpokeb(_dos_ds, dosbuf+c, 0);
r.x.ax = 0x4F01;
r.x.di = dosbuf & 0xF;
r.x.es = (dosbuf>>4) & 0xFFFF;
r.x.cx = mode;
__dpmi_int(0x10, &r);
if (r.h.ah)
return -1;
dosmemget(dosbuf, sizeof(MODE_INFO), &mode_info);
return 0;
}

int find_vesa_mode(int w, int h)
{
int mode_list[256];
int number_of_modes;
long mode_ptr;
int c;
if (get_vesa_info() != 0)
return 0;
mode_ptr = ((vesa_info.VideoModePtr & 0xFFFF0000) >> 12) + (vesa_info.VideoModePtr & 0xFFFF);
number_of_modes = 0;
while (_farpeekw(_dos_ds, mode_ptr) != 0xFFFF)
{
mode_list[number_of_modes] = _farpeekw(_dos_ds, mode_ptr);
number_of_modes++;
mode_ptr += 2;
}
for (c=0; c<number_of_modes; c++) {
if (get_mode_info(mode_list[c]) != 0) continue;
if ((mode_info.ModeAttributes & 0x19) != 0x19) continue;
if ((mode_info.XResolution != w) || (mode_info.YResolution != h)) continue;
if (mode_info.NumberOfPlanes != 1) continue;
if (mode_info.MemoryModel != 4) continue;
if (mode_info.BitsPerPixel != 8) continue;
return mode_list[c];
}
return 0;
}

int set_vesa_mode(int w, int h)
{
 __dpmi_regs r;
 int mode_number;
 mode_number = find_vesa_mode(w, h);
 if (!mode_number)
 return -1;
 r.x.ax = 0x4F02;
 r.x.bx = mode_number;
 __dpmi_int(0x10, &r);
 if (r.h.ah) return -1;
 return 0;
}

void set_vesa_bank(int bank_number)
{
 __dpmi_regs r;
 r.x.ax = 0x4F05;
 r.x.bx = 0;
 r.x.dx = bank_number;
 __dpmi_int(0x10, &r);
}

void putpixel_vesa_640x480(int x, int y, int color)
{
 int address = y*640+x;
 int bank_size = mode_info.WinGranularity*1024;
 int bank_number = address/bank_size;
 int bank_offset = address%bank_size;
 set_vesa_bank(bank_number);
_farpokeb(_dos_ds, 0xA0000+bank_offset, color);
}

void set_mode(int m)
{
  __dpmi_regs r;
  r.h.ah = 0;
  r.h.al = m;
  __dpmi_int(0x10, &r);
}

void put_char(int ch, int color)
{
  __dpmi_regs r;
  r.h.ah = 0x0e;
  r.h.al = ch;
  r.h.bh = 0;
  r.h.bl = color;
  __dpmi_int(0x10, &r);
}

--------------AAD9C6BEFF5C90BCE047177D--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019