Btw, anyone who wants to take a peek at the source, here it is:
main.cpp:
Code: Select all
#include "textmode.h"
void main()
{
// BEGIN GRAPHICS INIT
color(BLACK, WHITE);
clrscr();
// END
print("Hello World!\n");
colorch('N', WHITE, BLACK);
colorch('P', BLUE , BTWHITE);
colorch('/', BLACK, BTWHITE);
colorch('O', RED , GRAY);
colorch('S', GREEN, BTWHITE);
}
Code: Select all
// START : Text color definitions
#define BLACK 0x00
#define BLUE 0x01
#define GREEN 0x02
#define AQUA 0x03
#define RED 0x04
#define PURPLE 0x05
#define YELLOW 0x06
#define WHITE 0x07 // The white is really a light gray. use 0x0F for pure white
#define GRAY 0x08
#define LTBLUE 0x09 // LT stands for LIGHT
#define LTGREEN 0x0A
#define LTAQUA 0x0B
#define LTRED 0x0C
#define LTPURPLE 0x0D
#define LTYELLOW 0x0E
#define BTWHITE 0x0F // BR stands for BRIGHT
// END
char *vmem = (char *)0xb8000;
int colormode = 0x07;
int pos = 0;
int line = 0;
void color(int bgcolor, int fgcolor)
{
/* well, think of it this way. the color mode
* is formatted as (BGBIT)(FGBIT). 0x00 is black
* and 0x07 is white. thus, black background with
* white text is still 0x07. However, blue (0x01)
* background with white text would be 0x17
* (BSOD anyone? hehe, just a joke.)
* But anyways. in hex, instead of * by 10 to move
* up a digit like we would in our system of
* numbers, we would * by 16.
*
* now, why is the or there?
* in hex, if you have 0xF0 and 0x07, 0xF0 |
* 0x07 = 0xF7. see what im getting at?
*/
colormode = (bgcolor * 16) | fgcolor;
}
void clrscr()
{
/* the text mode screen memory works like this:
* there are 25 rows of possible 80 characters to
* display, right? well, each character also has a
* one-byte attribute (color) so, to account for the
* full size we do 25 * 80 * 2
*/
for(int i = 0; i < (25 * 80 * 2); ++i)
{
vmem[i] = ' ';
++i;
vmem[i] = colormode;
pos = pos + 2;
}
pos = 0;
}
void print(char string[])
{
int len = (sizeof(string) / sizeof(char)) * 2;
int spos = 0;
int lim;
if((len + (line * 80 * 2) + pos) >= (25 * 80 * 2))
{
lim = ((0 - 1) * ((25 * 80 * 2) - (len + (line * 80 * 2) + pos)));
}
else
{
lim = len;
}
for(int i = (line * 80 * 2) + pos; i < lim; ++i)
{
if(string[spos] == '\n')
{
if(line < 25)
{
i = i + ((line + 1) * 80 * 2) - (line * 80 * 2) + pos;
++line;
pos = 0;
++spos;
}
else
{
vmem[i] = ' ';
++i;
vmem[i] = colormode;
pos = pos + 2;
++spos;
}
}
else
{
vmem[i] = string[spos];
++i;
vmem[i] = colormode;
pos = pos + 2;
++spos;
}
}
}
void colorch(char ch, int bgcolor, int fgcolor)
{
if(pos == (24 * 2)) // if we are on our last character.....
{
++line;
}
int npos = (line * 80) + pos;
vmem[npos] = ch;
++npos;
vmem[npos] = (bgcolor * 16) | fgcolor;
pos = pos + 2;
}
Code: Select all
[BITS 32]
[global start]
[extern _main] ; this is in main.cpp
start:
call _main
cli ; stop interrupts
hlt ; halt the CPU