Basic Video Driver

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Basic Video Driver

Post by Pikkolamakkia »

I've just finished to write this small part of a video driver... Can it work? How can I improve it?

(Changed last time the 26/02/09 at 11.51)

Code: Select all

struct s_char{
       char s_char;    //The character
       char s_add;     //Color and blinking (I think..)
       };
       
s_char video[25][80];  //Virtual screen for debug

       
class s_screen{
       s_char temp;
       s_char *screen;
       unsigned char xPointer;
       unsigned char yPointer;
       
       long int x;
       long int y;
       s_char nullChar;

       
       public:
              s_screen(long int s_x=80, long int s_y=25){
                        //screen=(s_char*)0xb8000;
                        screen=(s_char*)video;
                        xPointer=0;
                        yPointer=0;
                        x=s_x;
                        y=s_y;
                        nullChar.s_char='\0';
                        nullChar.s_add='\0';
                        }
              
              void inline setPoiter(unsigned char s_xPointer, unsigned char s_yPointer){
                   xPointer=s_xPointer;
                   yPointer=s_yPointer;
                   }
                   
              long int lenghtOfchar(char *string);
              long int lenghtOfs_char(s_char *string);
              
              void write(s_char *string){
                   
                   }
                   
              void write(char *string, char *adds){
                   for(int i=0;*(string+i)!=0;i++){
                           if(*(string+i)==' '){
                                                if(yPointer<y){
                                                                if(xPointer<(x-1))xPointer++;
                                                                else{
                                                                     xPointer=0;
                                                                     yPointer++;
                                                                     }
                                                                }
                                                else yPointer=0;
                           }
                           
                           else if(*(string+i)=='\n'){
                                                 if(yPointer<y)yPointer++;
                                                 else yPointer=0;
                                                 xPointer=0;
                                                 }
                           
                           else{
                                //std::cout<<"i:"<<(xPointer+yPointer*x);
                                temp.s_char=*(string+i);
                                temp.s_add=*(adds+i);

                                *(screen+(xPointer+yPointer*x))=temp;                              
                               }
                           
                           
                           if(yPointer<25){
                                           if(xPointer<(x-1))xPointer++;
                                           else{
                                                xPointer=0;
                                                yPointer++;
                                                }
                                           }
                           else yPointer=0;
                           
                   }
                   }    
              void inline convertCto_C(char *string, char *adds, s_char *result){
                   
                   }
                   
              void inline clear(){for(int i=0;i!=y;i++){for(int i2=0;i2!=x;i2++)*(screen+i*x+i2)=nullChar;}}
       };
Last edited by Pikkolamakkia on Thu Feb 26, 2009 5:51 am, edited 2 times in total.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Basic Video Driver

Post by Troy Martin »

Code: Select all

struct _char
Not a typedef struct, so you can't make a variable of type _char.

I don't know much about OS dev in C++ though, sorry.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Basic Video Driver

Post by JohnnyTheDon »

Troy Martin wrote:

Code: Select all

struct _char
Not a typedef struct, so you can't make a variable of type _char.

I don't know much about OS dev in C++ though, sorry.
That issue doesn't exist in C++, but I usually use typedef struct in C++ anyway because if I don't I forget to do it when I work on something else in C.
You shouldn't have alignment issues (since char is one byte) but when making structures that directly apply to hardware or software standards its good practice (and necessary in most cases) to make them packed, either through gcc's __attribute__((packed)) or #pragma pack(push,1) ... #pragma pack(pop).
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Basic Video Driver

Post by xenos »

I wonder whether it makes sense to declare a function like write() as inline. Inserting this huge pile of code whenever you want to print something on the screen will increase the size of your executable, and I don't see any advantage in inlining it.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Basic Video Driver

Post by Creature »

I'm not really sure about this:

Code: Select all

struct _char{
       char _char;    //The character
       char _add;     //Color and blinking (I think..)
       };
As I'm not really at home in structure aligment and sizes and stuff, I'm just going to put it here anyways;

1) Shouldn't the type of these variables best be byte (unsigned char instead of letting the compiler decide)?
2) You're using _Char as video memory type (which usually is a word [unsigned short]), although I see your point of taking 2 chars/bytes which together form 16 bits = 2 bytes = 1 word, are you sure it's not possible that alignment or something will somehow occur differently messing the entire structure size of 2 bytes up?
3) The first byte of a value in video memory (a cell?) is indeed the actual character and the second byte contains the colour. If you mean the console cursor with 'blinking', that's not specified in there :P.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Basic Video Driver

Post by Pikkolamakkia »

I wonder whether it makes sense to declare a function like write() as inline. Inserting this huge pile of code whenever you want to print something on the screen will increase the size of your executable, and I don't see any advantage in inlining it.
Yes, It's right... I forgot the inline. I'm changing it right now
1) Shouldn't the type of these variables best be byte (unsigned char instead of letting the compiler decide)?
2) You're using _Char as video memory type (which usually is a word [unsigned short]), although I see your point of taking 2 chars/bytes which together form 16 bits = 2 bytes = 1 word, are you sure it's not possible that alignment or something will somehow occur differently messing the entire structure size of 2 bytes up?
3) The first byte of a value in video memory (a cell?) is indeed the actual character and the second byte contains the colour. If you mean the console cursor with 'blinking', that's not specified in there .
1) Ok :) It's right
2) I've not undestood what you mean sorry
3) No I don't mean it XD...
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: Basic Video Driver

Post by Creature »

Pikkolamakkia wrote:2) I've not undestood what you mean sorry
What I actually meant was, are you sure a structure like this:

Code: Select all

struct foo
{
    byte a;   //1 byte
    byte b;   //1 byte
};
will always report '2 bytes' if you call sizeof(foo)? Again, I'm not really into structure alignment and sizes so I'm not really sure.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Basic Video Driver

Post by Pikkolamakkia »

berkus wrote:Not to steal the topic, but... you could add more underscores.

Seriously, names starting with _ and __ are reserved for compiler implementors, so you're just risking running into trouble if some compiler has _char or _screen. Stick to a more sane naming convention established by C++ programmers (either system_style or MfcStyle) to save yourself from trouble.
Ok I'll change all the names XD
will always report '2 bytes' if you call sizeof(foo)? Again, I'm not really into structure alignment and sizes so I'm not really sure.
Why It had to be more than 2 bytes? I mean a byte plus a byte is 2 bytes... I could make some controls when you compile it

Code: Select all

if(sizeof(_char)!=2)//Make something....
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Basic Video Driver

Post by JohnnyTheDon »

Sometimes compilers will add empty space in between structure members to keep items aligned and speed up memory access. This shouldn't be a problem since char is one byte (so it can't be unaligned). To be safe you could pack the structure using

Code: Select all

__attribute__((packed))
or

Code: Select all

#pragma pack(push, 1)
....
#pragma pack(pop)
as I said before. You should almost always use this when dealing with structures that are read/written from hardware or ones that involve standards (for example, ACPI Tables). This is one place where not packing the structure shouldn't cause an issue.
Pikkolamakkia
Posts: 8
Joined: Tue Feb 24, 2009 1:51 pm

Re: Basic Video Driver

Post by Pikkolamakkia »

JohnnyTheDon wrote:Sometimes compilers will add empty space in between structure members to keep items aligned and speed up memory access. This shouldn't be a problem since char is one byte (so it can't be unaligned). To be safe you could pack the structure using

Code: Select all

__attribute__((packed))
or

Code: Select all

#pragma pack(push, 1)
....
#pragma pack(pop)
as I said before. You should almost always use this when dealing with structures that are read/written from hardware or ones that involve standards (for example, ACPI Tables). This is one place where not packing the structure shouldn't cause an issue.
Ok thank you I'll add it...
frazzledjazz
Posts: 7
Joined: Tue Mar 03, 2009 3:52 pm

Re: Basic Video Driver

Post by frazzledjazz »

not quite.

this is the pascal you need.
[sorry.dont do C, but it looks close to what you have.]

var
// Video memory array
VidMem: PChar = PChar($B8000);
CursorPosX: Word = 0;
CursorPosY: Word = 0;
// Color attribute, $07 would be better, this is BRIGHT AS HELL white....
Attrib: Word = $0F;
// Blank (space) character for current color
Blank: Word;

use 'and 128' for blink.

above would be called like so...

//pushes text up two lines
procedure Scroll;
begin
if CursorPosY>=24 then begin

Move((VidMem+2*80)^,VidMem^,23*2*80);
// Empty last line
FillWord((VidMem+23*2*80)^,80,Blank);
CursorPosX:=0;
CursorPosY:=23;
end;
end;

//clears screen
procedure Clrscr;

var
i: Byte;
begin
Blank:=$0 or (TextAttr shl 8);
for i:=0 to 23 do
FillWord((VidMem+i*2*80)^,80,Blank);
CursorPosX:=0;
CursorPosY:=0;
BlinkCursor;
end;
//thanks, you helped me find out why i'm not resetting to 1,1. code always needs a fresh pair of eyes.
Post Reply