Page 1 of 2

Getting started....

Posted: Sat Sep 30, 2006 6:24 pm
by shell
Ok, well, I've started out coding and everything and so far my C++ source has passed VC compile validation. Well, anyways, how can I use VC 6 to compile this into an OS? (Please excuse the noob question). I saw someone else on this forum trying to do this too, but with no definite answer.

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);
}
textmode.h:

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;
}
bootcode.asm:

Code: Select all

[BITS 32]
[global start]
[extern _main] ; this is in main.cpp

start:
  call _main

  cli  ; stop interrupts
  hlt ; halt the CPU
Please excuse any errors. I just started out about 3 days ago on this. (The bootcode.asm is also copied).

Re:Getting started....

Posted: Sun Oct 01, 2006 9:45 pm
by shell
Wow? Still no replies? Come on people

Re:Getting started....

Posted: Sun Oct 01, 2006 9:54 pm
by elaverick
I'd recommend reading this section of the Wiki - http://www.mega-tokyo.com/osfaq/Visual%20C

Personally after looking through the instructions for using VC for OS dev I decided it was going to be a little too much work to get it going easily. I use the Visual C IDE to do my development work but have defined a couple of batch files to do the compilation using a Cygwin built version of GCC. You might find this to be an easier solution.

Re:Getting started....

Posted: Sun Oct 01, 2006 10:02 pm
by shell
What about Dev-C++

Do you think my source looks right so far?

Re:Getting started....

Posted: Sun Oct 01, 2006 10:41 pm
by elaverick
shell wrote: What about Dev-C++
It really just depends what you're comfortable with. I like the standards compliance of GCC and the fact (once I got it set as a cross compiler) that it produces nice neat ELF binarys that can easily be used with GRUB. I also like the setup of the Visual Studio IDE... I know where everything is and how most of it works :)
Do you think my source looks right so far?
Well yes for the most part, but I'm certainly no expert on any of this. I've only just got my HigherHalf Kernel to compile properly for the first time.

Personally I hate having functions inlined in header files, it's going to mean that any code you #include from is going to get the extra bloat of all those functions which seems like a really bad idea for a Kernel.

Also you should lock your Kernel in a loop towards the end of main() as it shouldn't really be exiting, a simple

Code: Select all

for(;;);
should suffice.

I'd go and have a look through some of the example barebones tutorials if I were you. I spent about a week digging through the various tutorials comparing peoples methods until I was happy with what they were doing, that's just a suggestion tho.

Re:Getting started....

Posted: Sun Oct 01, 2006 10:45 pm
by shell
No, I mean, How do you set up Dev-C++ to compile the OS???

Re:Getting started....

Posted: Sun Oct 01, 2006 10:49 pm
by elaverick
Ah, haven't a clue I'm afraid. I've never used it. Though it says it supports Cygwin GCC so I should imagine it should be a case of pointing it at an appropriate cross-compiler and letting it trot through it.

Re:Getting started....

Posted: Mon Oct 02, 2006 7:21 am
by Combuster
Two ways to do it
first one:
Tools -> Compiler options
hit the add button, give it a name, then go through the tabs and fill in the fields
Then in Project -> Project options -> Compiler, select your added compiler of choice

second one:
Project -> Project options -> Makefile
tick custom makefile and point it at some makefile which uses the tools you need.

You can use both at the same time if you want :)

Re:Getting started....

Posted: Mon Oct 02, 2006 7:31 am
by shell
But how can you build a crosscompiler on windows??

Re:Getting started....

Posted: Mon Oct 02, 2006 7:57 am
by Combuster

Re:Getting started....

Posted: Mon Oct 02, 2006 8:21 am
by shell
Sorry, but I already read TFM multiple times at multiple places. But none of the account for errors in CygWin which happened last time.

Re:Getting started....

Posted: Mon Oct 02, 2006 8:42 am
by Combuster
in that case, read this
Really, if you stick to that tutorial it will work. Every failed compile i've seen so far is caused by skipping over certain steps. (two weeks ago i compiled binutils 2.17 and gcc 3.4.4 correctly the first time so i know it works)

Quick guess, have you forgotten to tick all of gcc, binutils, make, flex and bison in cygwins's setup app?

Re:Getting started....

Posted: Mon Oct 02, 2006 9:04 am
by shell
I'm setting the target to i386-unknown-gnu. Sound good?

Re:Getting started....

Posted: Mon Oct 02, 2006 10:01 am
by elaverick
I went with i586-ELF for my target.

One thing to remeber which might confuse you if you're not used to *nix stuff... Bintools 2.9.x is an earlier release than Bintools 2.15.x (I mention this specifically because 2.9.x doesn't actually compile properly anymore...)

Re:Getting started....

Posted: Mon Oct 02, 2006 1:17 pm
by shell
Ok, binutils went fine but heres what happened with GCC:
/home/root/devenv/bin/i386-unknown-gnu-ld: crti.o: No such file: No such file or directory
collect2: ld returned 1 exit status
make[2]: *** [libgcc_s.so] Error 1
make[2]: Leaving directory '/usr/src/build-gcc/gcc'
make[1]: *** [libgcc.a] Error 2
make[1]: Leaving directory '/usr/src/build-gcc/gcc'
make: *** [all-gcc] Error 2
What to do now?