cos2000v2/lib/vga.c

528 lines
11 KiB
C
Raw Normal View History

#include "vga.h"
#include "memory.h"
#include "asm.h"
#include "port.h"
#include "types.h"
#include "modes.c"
#include "8x8fnt.c"
#include "8x16fnt.c"
/* Registres VGAs */
#define sequencer 0x3c4
#define misc 0x3c2
#define ccrt 0x3D4
#define attribs 0x3c0
#define graphics 0x3ce
#define state 0x3da
/* Taille d'un plan de bit */
#define planesize 0x10000
u16 resX,resY; /* resolution x,y en caract<63>res*/
static u8 pages,activepage; /* nombre de pages disponibles N<> de la page active*/
static u32 linesize,pagesize;/* Taille d'une ligne et d'une page */
static u8 vmode=0xFF,color; /* mode en cours d'utilisation et profondeur */
static u32 basemem; /* Adresse de la m<>moire vid<69>o */
static bool scrolling=0x1,graphic;/* Activation du d<>filement, Flag du mode graphique */
static u8 font; /* n<> font active */
/*******************************************************************************/
/* Attend la retrace verticale */
void waitvretrace (void)
{
while ((inb(state)&8)==0);
}
/*******************************************************************************/
/* Attend la retrace horizontale */
void waithretrace (void)
{
while ((inb(state)&1)==0);
}
/*******************************************************************************/
/* Active l'affichage du curseur de texte */
void enablecursor (void)
{
u8 curs;
/* active le curseur hardware */
outb(ccrt, 10);
curs = inb(ccrt+1)&~32;
outb(ccrt+1, curs);
}
/*******************************************************************************/
/* Desactive l'affichage du curseur de texte */
void disablecursor (void)
{
u8 curs;
/* Desactive le curseur hardware */
outb(ccrt, 10);
curs = inb(ccrt+1)|32;
outb(ccrt+1, curs);
}
/*******************************************************************************/
/* Active le scrolling en cas de d<>bordement d'<27>cran */
void enablescroll (void)
{
scrolling=true;
}
/*******************************************************************************/
/* Desactive le scrolling en cas de d<>bordement d'<27>cran */
void disablescroll (void)
{
scrolling=false;
}
/*******************************************************************************/
/* Utilise le plan de bit sp<73>cifi<66> */
void useplane(u8 plan)
{
u8 mask;
plan &= 3;
mask = 1 << plan;
/* choisi le plan de lecture */
outb(graphics, 4);
outb(graphics+1, plan);
/* choisi le plan d'ecriture */
outb(sequencer, 2);
outb(sequencer+1, mask);
}
/*******************************************************************************/
/* Renvoie l'adresse du segment video */
u32 getbase(void)
{
u32 base;
outb(graphics, 6);
base = inb(graphics+1);
base >>= 2;
base &= 3;
switch(base)
{
case 0:
case 1:
base = 0xA0000;
break;
case 2:
base = 0xB0000;
break;
case 3:
base = 0xB8000;
break;
}
return base;
}
/*******************************************************************************/
/* efface l'<27>cran */
void (*fill)(u8 attrib);
void fill_text (u8 attrib)
{
gotoscr(0,0);
memset((u8 *)(basemem+activepage*pagesize),' ',pagesize/2,2);
memset((u8 *)(basemem+activepage*pagesize+1),attrib,pagesize/2,2);
}
void fill_chain (u8 attrib)
{
gotoscr(0,0);
memset((u8 *)(basemem+activepage*pagesize),attrib&0x0F,pagesize,1);
}
void fill_unchain (u8 attrib)
{
gotoscr(0,0);
int i;
for(i=0;i<4;i++)
{
useplane(i);
memset((u8 *)(basemem+activepage*pagesize),attrib&0x0F,pagesize,1);
}
}
/*******************************************************************************/
/* fixe la position du curseur texte */
void gotoscr(u16 x,u16 y)
{
u16 pos;
pos=(x+y*resX);
outb(ccrt,0x0F);
outb(ccrt+1,(u8)(pos&0x00FF));
outb(ccrt,0x0E);
outb(ccrt+1,(u8)((pos&0xFF00)>>8));
}
/*******************************************************************************/
/* Fait defiler l'ecran de n lignes vers le haut */
void (*scroll)(u8 lines,u8 attrib);
void scroll_unchain(u8 lines,u8 attrib)
{
if (scrolling)
{
u8 i;
for(i=0;i<4;i++)
{
useplane(i);
memcpy((u8*)(basemem+linesize*8*lines),(u8*)basemem,pagesize-linesize*8*lines,1);
memset((u8*)(basemem+pagesize-linesize*8*lines),attrib&0x0F,linesize*8*lines,1);
}
}
}
void scroll_chain(u8 lines,u8 attrib)
{
if (scrolling)
{
memcpy((u8*)basemem+linesize*8*lines,(u8*)basemem,pagesize-linesize*8*lines,1);
memset((u8*)(basemem+pagesize-linesize*8*lines),attrib&0x0F,linesize*8*lines,1);
}
}
void scroll_text(u8 lines,u8 attrib)
{
if (scrolling)
{
memcpy((u8*)basemem+linesize*lines,(u8*)basemem,pagesize-linesize*lines,1);
memset((u8*)(basemem+pagesize-linesize*lines-2),' ',(linesize*lines)/2,2);
memset((u8*)(basemem+pagesize-linesize*lines-1),attrib,(linesize*lines)/2,2);
}
}
/*******************************************************************************/
/* Affiche le caract<63>re a l'<27>cran */
void (*showchar)(u16 coordx,u16 coordy,u8 thechar,u8 attrib);
void showchar_graphic(u16 coordx,u16 coordy,u8 thechar,u8 attrib)
{
u8 x,y,pattern,set;
for(y=0;y<8;y++)
{
pattern=font8x8[thechar*8+y];
for(x=0;x<8;x++)
{
set=((pattern>>(7-x))&0x1); /* mettre un ROL import<72> depuis asm */
if (set==0)
writepxl(coordx*8+x,coordy*8+y,((attrib&0xF0)>>8)*set);
else
writepxl(coordx*8+x,coordy*8+y,(attrib&0x0F)*set);
}
}
}
void showchar_text(u16 coordx,u16 coordy,u8 thechar,u8 attrib)
{
u8 *screen;
screen = (u8 *)basemem+activepage*pagesize+2*(coordx+coordy*resX);
*screen = thechar;
*(++screen) =attrib;
}
/*******************************************************************************/
/* Ecrit un pixel a l'<27>cran */
void (*writepxl)(u16 x, u16 y, u32 c);
void writepxl_1bit(u16 x, u16 y, u32 c)
{
u8* off;
u8 mask;
c = (c & 1) * 0xFF;
off = (u8*)(basemem+activepage*pagesize+linesize * y + x / 8);
x = (x & 7) * 1;
mask = 0x80 >> x;
*off= ((*off) & ~mask) | (c & mask);
}
void writepxl_2bits(u16 x, u16 y, u32 c)
{
u8 *off;
u8 mask;
c = (c & 3) * 0x55;
off = (u8*)(basemem+activepage*pagesize+linesize * y + x / 4);
x = (x & 3) * 2;
mask = 0xC0 >> x;
*off= ((*off) & ~mask) | (c & mask);
}
void writepxl_4bits(u16 x, u16 y, u32 c)
{
u8* off;
u8 mask, p, pmask;
off = (u8*)(basemem+activepage*pagesize+linesize * y + x / 8);
x = (x & 7) * 1;
mask = 0x80 >> x;
pmask = 1;
for(p = 0; p < 4; p++)
{
useplane(p);
if(pmask & c)
*off= ((*off) | mask);
else
*off= ((*off) & ~mask);
pmask <<= 1;
}
}
void writepxl_8bits(u16 x, u16 y, u32 c)
{
u8* off;
off = (u8*)(basemem+activepage*pagesize+linesize * y + x);
*off=c;
}
void writepxl_8bitsunchain(u16 x, u16 y, u32 c)
{
u8* off;
off = (u8*)(basemem+activepage*pagesize+linesize * y + x / 4);
useplane(x & 3);
*off=c;
}
/*******************************************************************************/
/* Change le mode video courant */
u32 setvmode(u8 mode)
{
u8 *def,i,gmode;
/* R<>cupere la definition des registres VGA en fonction du mode
graphique : >0x80
text : 0x00 - 0x7F
*/
if (mode>=0x80)
{
gmode=mode-0x80;
if (gmode>maxgraphmode) return 1; /* mode inexistant */
def=graphmodes[gmode];
graphic=true;
}
else
{
if (mode>maxtextmode) return 1; /* mode inexistant */
def=textmodes[mode];
graphic=false;
loadfont(font8x8,8,1);
loadfont(font8x16,16,0);
}
/* Initialise les registre "divers" */
outb(misc,def[0]);
/* Initialise les registre d'etat */
outb(state,0x00);
/* Initialise le s<>quenceur */
outreg(sequencer,&def[1],5);
/* Debloque le verouillage des registres controleur CRT */
outb(ccrt,0x11);
outb(ccrt+1,0x0E);
/* Initialise le controleur CRT */
outreg(ccrt,&def[6],25);
/* Initialise le controleur graphique */
outreg(graphics,&def[31],9);
inb(state);
/* Initialise le controleur d'attributs */
outregsame(attribs,&def[40],21);
inb(state);
outb(attribs,0x20);
/* R<>cupere depuis la table de d<>finition des mode la r<>solution et la
profondeur (en bits) */
resX=def[61];
resY=def[62];
color=def[63];
/* Initialise l'adresse des procedures de gestion graphique et les differentes
variables en fonction de la profondeur et du mode*/
if (!graphic)
{
/* mode texte */
linesize=resX*2;
writepxl=NULL; /* pas d'affichage de pixels */
showchar=showchar_text;
scroll=scroll_text;
fill=fill_text;
pagesize=resY*linesize;
}
else
{
switch(color)
{
case 1:
/* mode N&B */
linesize=resX;
writepxl=writepxl_1bit;
fill=fill_chain;
scroll=scroll_chain;
break;
case 2:
/* mode 4 couleurs */
linesize=(resX<<1);
writepxl=writepxl_2bits;
fill=fill_chain;
scroll=scroll_chain;
break;
case 4:
/* mode 16 couleurs */
linesize=resX;
writepxl=writepxl_4bits;
fill=fill_unchain;
scroll=scroll_unchain;
break;
case 8:
/* mode 256 couleurs */
if (def[5]==0x0E)
{
/* mode chain<69> (plus rapide mais limit<69> en m<>moire) */
linesize=(resX<<3);
writepxl=writepxl_8bits;
scroll=scroll_chain;
fill=fill_chain;
}
else
{
/* mode non chain<69> */
linesize=(resX<<1);
writepxl=writepxl_8bitsunchain;
scroll=scroll_unchain;
fill=fill_unchain;
}
break;
default:
break;
}
showchar=showchar_graphic;
pagesize=((resY*linesize)<<3);
}
/* calcul des variables d'<27>tat video */
activepage=0;
vmode=mode;
pages=(planesize/pagesize);
basemem=(def[20]<<8)+def[21]+getbase();
return 0;
}
/*******************************************************************************/
/* R<>cup<75>re le mode vid<69>o en cours */
u8 getvmode(void)
{
return vmode;
}
/*******************************************************************************/
/* Charge une nouvelle police de caract<63>re */
u32 loadfont(u8* def,u8 size,u8 font)
{
if (graphics==1) return 1;
u8 oldregs[5]={0,0,0,0,0};
u8* base;
u16 i;
if (font>7) return 1;
if (font<4)
base = (u8 *)(getbase()+(font<<14));
else
base = (u8 *)(getbase()+((((font-4)<<1)+1)<<13));
/* sauve les anciens registres */
outb(sequencer,2);
oldregs[0]=inb(sequencer+1);
outb(sequencer,4);
oldregs[1]=inb(sequencer+1);
/* Adressage paire/impair desactiv<69> (lineaire) */
outb(sequencer+1, oldregs[1]|0x04);
outb(graphics,4);
oldregs[2]=inb(graphics+1);
outb(graphics,5);
oldregs[3]=inb(graphics+1);
/* Adressage paire/impair desactiv<69> (lineaire) */
outb(graphics+1, oldregs[3]&~0x10);
outb(graphics,6);
oldregs[4]=inb(graphics+1);
/* Adressage paire/impair desactiv<69> (lineaire) */
outb(graphics+1, oldregs[4] & ~0x02);
/* utilisation du plan N<>2 */
useplane(2);
for(i=0; i < 256; i++)
{
memcpy(def,base+i*32,size,1);
def += size;
}
outb(sequencer,2);
outb(sequencer+1,oldregs[0]);
outb(sequencer,4);
outb(sequencer+1,oldregs[1]);
outb(graphics,4);
outb(graphics+1,oldregs[2]);
outb(graphics,5);
outb(graphics+1,oldregs[3]);
outb(graphics,6);
outb(graphics+1,oldregs[4]);
return 0;
}
/*******************************************************************************/
/* R<>cupere le N<> de la police de caract<63>re en cours d'utilisation */
u8 getfont(u8 num)
{
return font;
}
/*******************************************************************************/
/* Fixe le N<> de la police de caract<63>re a utiliser */
void setfont(u8 num)
{
font=num&0x07;
outb(sequencer,3);
outb(sequencer+1,(num&0x03)+((num&0x04)<<2));
}
/*******************************************************************************/