Page 1 of 2

Switch to Graphics mode in Protected mode

Posted: Thu May 26, 2005 1:44 am
by srg_13
Hi,
I found this cool peice of code that can change the video mode in protected mode:

Code: Select all

typedef char byte;
typedef unsigned short word;
typedef unsigned long dword;

#define SZ(x) (sizeof(x)/sizeof(x[0]))


// misc out (3c2h) value for various modes

#define R_COM  0x63 // "common" bits

#define R_W256 0x00
#define R_W320 0x00
#define R_W360 0x04
#define R_W376 0x04
#define R_W400 0x04

#define R_H200 0x00
#define R_H224 0x80
#define R_H240 0x80
#define R_H256 0x80
#define R_H270 0x80
#define R_H300 0x80
#define R_H360 0x00
#define R_H400 0x00
#define R_H480 0x80
#define R_H564 0x80
#define R_H600 0x80


static const byte hor_regs [] = { 0x0,  0x1,  0x2,  0x3,  0x4, 
0x5,  0x13 };

static const byte width_256[] = { 0x5f, 0x3f, 0x40, 0x82, 0x4a,
0x9a, 0x20 };
static const byte width_320[] = { 0x5f, 0x4f, 0x50, 0x82, 0x54,
0x80, 0x28 };
static const byte width_360[] = { 0x6b, 0x59, 0x5a, 0x8e, 0x5e,
0x8a, 0x2d };
static const byte width_376[] = { 0x6e, 0x5d, 0x5e, 0x91, 0x62,
0x8f, 0x2f };
static const byte width_400[] = { 0x70, 0x63, 0x64, 0x92, 0x65,
0x82, 0x32 };

static const byte ver_regs  [] = { 0x6,  0x7,  0x9,  0x10, 0x11,
0x12, 0x15, 0x16 };

static const byte height_200[] = { 0xbf, 0x1f, 0x41, 0x9c, 0x8e,
0x8f, 0x96, 0xb9 };
static const byte height_224[] = { 0x0b, 0x3e, 0x41, 0xda, 0x9c,
0xbf, 0xc7, 0x04 };
static const byte height_240[] = { 0x0d, 0x3e, 0x41, 0xea, 0xac,
0xdf, 0xe7, 0x06 };
static const byte height_256[] = { 0x23, 0xb2, 0x61, 0x0a, 0xac,
0xff, 0x07, 0x1a };
static const byte height_270[] = { 0x30, 0xf0, 0x61, 0x20, 0xa9,
0x1b, 0x1f, 0x2f };
static const byte height_300[] = { 0x70, 0xf0, 0x61, 0x5b, 0x8c,
0x57, 0x58, 0x70 };
static const byte height_360[] = { 0xbf, 0x1f, 0x40, 0x88, 0x85,
0x67, 0x6d, 0xba };
static const byte height_400[] = { 0xbf, 0x1f, 0x40, 0x9c, 0x8e,
0x8f, 0x96, 0xb9 };
static const byte height_480[] = { 0x0d, 0x3e, 0x40, 0xea, 0xac,
0xdf, 0xe7, 0x06 };
static const byte height_564[] = { 0x62, 0xf0, 0x60, 0x37, 0x89,
0x33, 0x3c, 0x5c };
static const byte height_600[] = { 0x70, 0xf0, 0x60, 0x5b, 0x8c,
0x57, 0x58, 0x70 };

// the chain4 parameter should be 1 for normal 13h-type mode, but //
only allows 320x200 256x200, 256x240 and 256x256 because you
// can only access the first 64kb

// if chain4 is 0, then plane mode is used (tweaked modes), and
// you'll need to switch planes to access the whole screen but
// that allows you using any resolution, up to 400x600

int init_graph_vga(int width, int height,int chain4) 
  // returns 1=ok, 0=fail
{
   const byte *w,*h;
   byte val;
   int a;

   switch(width) {
      case 256: w=width_256; val=R_COM+R_W256; break;
      case 320: w=width_320; val=R_COM+R_W320; break;
      case 360: w=width_360; val=R_COM+R_W360; break;
      case 376: w=width_376; val=R_COM+R_W376; break;
      case 400: w=width_400; val=R_COM+R_W400; break;
      default: return 0; // fail
   }
   switch(height) {
      case 200: h=height_200; val|=R_H200; break;
      case 224: h=height_224; val|=R_H224; break;
      case 240: h=height_240; val|=R_H240; break;
      case 256: h=height_256; val|=R_H256; break;
      case 270: h=height_270; val|=R_H270; break;
      case 300: h=height_300; val|=R_H300; break;
      case 360: h=height_360; val|=R_H360; break;
      case 400: h=height_400; val|=R_H400; break;
      case 480: h=height_480; val|=R_H480; break;
      case 564: h=height_564; val|=R_H564; break;
      case 600: h=height_600; val|=R_H600; break;
      default: return 0; // fail
   }

   // chain4 not available if mode takes over 64k

   if(chain4 && (long)width*(long)height>65536L) return 0; 

   // here goes the actual modeswitch

   outp(0x3c2,val);
   outw(0x3d4,0x0e11); // enable regs 0-7

   for(a=0;a<SZ(hor_regs);++a) 
      outw(0x3d4,(word)((w[a]<<8)+hor_regs[a]));
   for(a=0;a<SZ(ver_regs);++a)
      outw(0x3d4,(word)((h[a]<<8)+ver_regs[a]));

   outw(0x3d4,0x0008); // vert.panning = 0

   if(chain4) {
      outw(0x3d4,0x4014);
      outw(0x3d4,0xa317);
      outw(0x3c4,0x0e04);
   } else {
      outw(0x3d4,0x0014);
      outw(0x3d4,0xe317);
      outw(0x3c4,0x0604);
   }

   outw(0x3c4,0x0101);
   outw(0x3c4,0x0f02); // enable writing to all planes
   outw(0x3ce,0x4005); // 256color mode
   outw(0x3ce,0x0506); // graph mode & A000-AFFF

   inp(0x3da);
   outb(0x3c0,0x30); outb(0x3c0,0x41);
   outb(0x3c0,0x33); outb(0x3c0,0x00);

   for(a=0;a<16;a++) {    // ega pal
      outb(0x3c0,(byte)a); 
      outb(0x3c0,(byte)a); 
   } 
   
   outb(0x3c0, 0x20); // enable video

   return 1;

}
-Stephen

Re:Switch to Graphics mode in Protected mode

Posted: Thu May 26, 2005 1:45 am
by srg_13
You might also need:

Code: Select all

 void outw(unsigned short port, unsigned short value)
{
asm volatile ("outw %%ax,%%dx": :"dN"(port), "a"(value));
} 

 void outb(unsigned short port, unsigned char value)
{
asm volatile ("outb %%al,%%dx": :"dN"(port), "a"(value));
} 
-Stephen

Re:Switch to Graphics mode in Protected mode

Posted: Thu May 26, 2005 3:29 am
by Candy
I think we are more in need of copyright information, plus perhaps an indication of where you found it.

Re:Switch to Graphics mode in Protected mode

Posted: Thu May 26, 2005 9:52 am
by bubach
It's public domain, and problably from (or a variation of):

http://my.execpc.com/~geezer/osd/graphics/modes.c

which is well-known code... :P
PS: This code goes in many diffrent flavours, but all that I've seen is PD. Chris code seems to be the most complete, with drawing functions etc.

More snippets (asm): http://bos.asmhackers.net/docs/vga_without_bios/

Re:Switch to Graphics mode in Protected mode

Posted: Thu May 26, 2005 7:54 pm
by proxy
will this work on all VGA cards?

proxy

Re:Switch to Graphics mode in Protected mode

Posted: Fri May 27, 2005 1:34 am
by Brendan
Hi,
proxy wrote:will this work on all VGA cards?
From what I can tell it will work with all VGA cards, but not all video cards are VGA compatible. A fair number of video cards now have a VGA mode which gets disabled as soon anything above VGA is used - for example, if you used VBE to set 1024*768 and then used this code it may just create a mess. Some video cards have no VGA compatibility at all, and some monitors can't handle strange resolutions.

The code itself also doesn't initialize the start address register (in the CTR controller registers), so if it was changed you'll be left with side effects.This is easy to fix - just add these lines after the "outw(0x3ce,0x0506); // graph mode & A000-AFFF" line:

Code: Select all

      outw(0x3d4,0x000C);
      outw(0x3d4,0x000D);
It shouldn't normally be needed, but you never know - the start address register is good for scrolling the screen vertically. For e.g. in 320 *200 * 256 colour mode, setting the start address register to 320 then 640 then 960 would smoothly scroll the screen up (in 80*50 text mode you can use it to manage a much larger scroll back buffer, much like linux and "shift+cursor up/down"). It can also be used in conjunction with the line compare register to do split screen (where the bottom of the screen does scrolling as before, while the top stays stationary).


Cheers,

Brendan

Re:Switch to Graphics mode in Protected mode

Posted: Sat Jun 11, 2005 3:38 am
by dc0d32
and will it also work on standard VESA cards and monitors?

Re:Switch to Graphics mode in Protected mode

Posted: Sat Jun 11, 2005 4:53 am
by AR
If you want a resolution higher than 640x480x4 then you'll need a driver for every card, the VESA BIOS is supposedly on the ROM on the video card (meaning every card has different ports that only the BIOS or a full driver will know about).

Re:Switch to Graphics mode in Protected mode

Posted: Fri Jun 17, 2005 12:14 pm
by Crazed123
There is a protected mode interface to VESA 2.0-3.0 that can be accessed via GRUB (I think 2.0 only). It can't set the graphics mode for you, but the Multiboot standard says it should be able to return the location of the VESA PM interface if you ask for it.

From there it should be possible to change the mode and do as you please, whatnot, but the documentation is a bit dense and meant for real mode. Could someone look at the VESA 2 and 3 spec and figure out how to use the Protected Mode interface?

Re:Switch to Graphics mode in Protected mode

Posted: Sat Jun 18, 2005 11:06 pm
by SANiK
I hope you guys aren't forgetting about my project which uses the BIOS in 32 bit mode to enter VESA:
Image

I restarted the project now though and am using ASM.
Also, it's TRULY a translator core now.
So instead of having to code an instruction FULLY, all that's needed to be done is to copy-paste the opcode converting the addressing mode from 16 to 32 bit, and appending 0x66 where it's needed.

IF YOU'RE INTERESTED IN HELPING:
I need extra help with this.
The license if you care will be the standard "fair use" license which allows anything to be done with the code as long as you don't attempt to prevent others from using it.

Here is how the assembly base source looks:
(See the attachment.)
Don't just look at the source and give up!
Imagine what possibilities this will unlock, and how much of a hero you will be by allowing people to use the BIOS in 32 bit mode through translation ('emulation' done by executing the code directly).

Re:Switch to Graphics mode in Protected mode

Posted: Sun Jun 19, 2005 6:54 am
by gaf
Hi Sanik,
Your project sounds interesting but I'm wondering where you get the VESA code from that you want to translate. If it was possible to simply read the video BIOS out, a simpler solution might be to reverse-engineer the code by hand (It shouldn't be that much..). You could then write a script to boot a specific card using memory and port I/O only and, since the graphic-card manufactures must keep to *some* standards for their cards, it should also be possible to deduce a driver for all nvidia/ati cards from from the scripts.

regards,
gaf

Re:Switch to Graphics mode in Protected mode

Posted: Sun Jun 19, 2005 6:59 am
by Kim
Iam thinking of using a recompiler / opcode emulator too.
This gives you 100% controll over the codeblock you want to execute. And there allot of example 16bit intel cpu emulators out.

Re:Switch to Graphics mode in Protected mode

Posted: Sun Jun 19, 2005 9:09 am
by SANiK
Well, this is a translator.
So what it's supposed to do is:
1) Read an opcode
2) Convert it from 16 to 32 bit while taking into account prefixes
3) Backup the current registers
4) Load the emulated registers (switches the stack here)
5) Run the converted opcode here
6) Save the emulated registers
7) Load the backed up registers (switches the stack back here)
8) Do the loop

So it may be slow, but not that slow.
But the translator design makes the emulator small cause all it does is copy paste opcode operands.

The point of this is to emulate the whole BIOS, which I already did. The previous emulator was TOO BIG, and had a lot of bugs due to incorrect information in the x86 asm manual which I used, but why waste time converting EVERY Video BIOS by hand?

Plus, with this, you can do other things as well.
Need a floppy driver? Just emulate the interrupt!
Or would you rather convert the BIOS to 32 bit by hand?

And let me restate, HELP WOULD BE GREATLY APPRECIATED

Re:Switch to Graphics mode in Protected mode

Posted: Sun Jun 19, 2005 10:00 am
by proxy
SANiK,

I actually have a whole lot of experience writing interpreters and dynamic recompilers for emulators. (your idea is pretty much same as a dynamic recompiler or if you are more of a java person, a JIT compiler :P).

anyway, I might be interested in helping ya out, maybe you shoudl setup a code repository online so people could puruse it and see if they can contribute.

Though i gotta be honest, i just finished my V8086 mode and it was not very hard at all. Even the "v8086 mode is more difficult to support if you use software task switching" i find to be a bit of an exageration, it was trivial once i had user tasks working..

proxy

Re:Switch to Graphics mode in Protected mode

Posted: Sun Jun 19, 2005 10:18 am
by SANiK
Well, how about 64 bit CPUs?
V86 mode goes out the windows then.

I never setup a code repository before. Plus this is basically one asm file and one C file compiled to run on Windows for now.

Best I could do is start outlining the whole concept:

1) The translator gets the base address and code address as arguments. The base address is where the COPIED BIOS is, and the code address is the relative address to where the program that is being emulated is. If you want to execute a BIOS interrupt, then you set the code address to point to the interrupt.

2) Opcode is read. If it's a prefix, then the prefix is copied also.

3) Segment prefixes are ignored, somewhat. This is done by using the mod R/M byte and generating a linear address everytime incorporating the segment prefixes. Then, the mod R/M byte is written back using the 32 bit form (which allows linear addresses).

4) The switch statement is composed of aligned labels which are 32 bytes apart. So, to jump to one of them, it's basicly a simple:
xor eax, eax
mov al, bl ;BL contains the opcode which was read from before
shl ax, 5 ;Same as multiplying by 32
add eax, vm_base
jmp eax

vm_base: ;Opcode 0
align 32
vm_op01: ;Opcode 1
align 32
vm_op02: ;Opcode 2
align 32
...