Page 1 of 3

pascal os development

Posted: Sat May 28, 2005 4:38 pm
by kerim
I just want to inform you that I have started a Google Group on operating system development in pascal. I plan to write a small series of tutorials on making a basic Hello world operating system in pascal, and maybe even more. Who knows ?

If you're interested, please visit us @ http://groups-beta.google.com/group/pascalosdev

Re:pascal os development

Posted: Sun May 29, 2005 2:40 am
by Kim
Are you going to use freepascal?
GRUB as bootloader?

Re:pascal os development

Posted: Sun May 29, 2005 7:45 am
by Dex4u
I made a turbo pascal OS, when i first started OS Dev, for the basic its quite easy, here's a funny example.
This code will restart your pc.

Code: Select all

        begin
                  writeln('Hello. from my OS ')
        end.
But this code works fine

Code: Select all

 uses
    Crt;
        begin
                  writeln('Hello. from my OS')
        end.
If you boot it using a program like "bootprog" available here: http://alexfru.chat.ru/epm.html#bootprog.

Re:pascal os development

Posted: Sun May 29, 2005 3:51 pm
by kerim
Not the freepascal, but Turbo Pascal version 7, as I plan to make a simple real mode OS just to demonstrate to people that it is possible and as easy as in any other language to make an operating system in Pascal.

Re:pascal os development

Posted: Mon May 30, 2005 9:06 am
by Dex4u
I remember now, most of my old pascal OS ended up in PowerDos :P

Re:pascal os development

Posted: Mon May 30, 2005 3:20 pm
by Crazed123
I'm writing in FreePascal 2.0 series and can contribute an RTL.

Re:pascal os development

Posted: Mon May 30, 2005 3:56 pm
by kerim
If you've done anything, a basic Hello World in Free Pascal, and could spare some time writing a tutorial, and making a sample Hello World OS, it would be great, as I plan to start a small web site on Pascal OS dev, and I would appreciate any kind of help.

Best regards

P.S. well not really, most of your code did not end up in PowerDOS, as I wrote all the code by my self, but I did learn a lot from your code. Thanks again ;)

Re:pascal os development

Posted: Mon May 30, 2005 11:43 pm
by kerim
A simple Hello world OS is done, and I hope that I'll find some time these days to write a tutorial on how you can do it your self.
Anyone interested ?

Re:pascal os development

Posted: Tue May 31, 2005 1:07 pm
by Kim
Why not create a 32BIT hello world os in pascal using freepascal...
a 16BIT OS is absolute... and turbo pascal's compiler is sooo old...
You will be pointing people to the rong direction.

Get a GRUB boot disk with elf kernel support.
Get new freepascal compiler.
Get NASM or not if you use AS.
Crosscompile gnu bin utils (i could upload them) in cygwin.

And your set for developing an 32BIT OS using freepascal.

First write a stub in asm to jump to the kernel.

[stub.asm]

Code: Select all

;
; Kernel stub
;

;
; We are in 32bits protected mode
;
[bits 32]

;
; Export entrypoint
;
[global kstart]

;
; Import kernel entrypoint
;
[extern kmain]

;
; Posible multiboot header flags
;
MULTIBOOT_MODULE_ALIGN      equ   1<<0
MULTIBOOT_MEMORY_MAP      equ   1<<1
MULTIBOOT_GRAPHICS_FIELDS   equ   1<<2
MULTIBOOT_ADDRESS_FIELDS   equ   1<<16

;
; Multiboot header defines
;
MULTIBOOT_HEADER_MAGIC      equ   0x1BADB002
MULTIBOOT_HEADER_FLAGS      equ   MULTIBOOT_MODULE_ALIGN | MULTIBOOT_MEMORY_MAP
MULTIBOOT_HEADER_CHECKSUM   equ   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)

;
; Kernel stack size
;
KERNEL_STACKSIZE      equ   0x4000

section .text

;
; Multiboot header
;
align 4
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_HEADER_CHECKSUM

;
; Entrypoint
;
kstart:
   mov esp, KERNEL_STACK+KERNEL_STACKSIZE   ;Create kernel stack
   push eax            ;Multiboot magic number
   push ebx            ;Multiboot info
   call kmain            ;Call kernel entrypoint
        cli               ;Clear interrupts
   hlt               ;Halt machine

section .bss

;
; Kernel stack location
;
align 32
KERNEL_STACK:
   resb KERNEL_STACKSIZE
[Small system unit (needed by compiler)]
[..\support\system.pas]

Code: Select all

unit system;

interface

{$define FPC_IS_SYSTEM}

type
   DWORD      = LongWord;
   Cardinal   = LongWord;
   Integer      = Longint;
   SizeInt      = Longint;
   SizeUInt   = DWord;
   PtrInt      = Longint;
   PtrUInt      = DWord;
   ValSIn      = Longint;
   ValUInt      = Cardinal;
   PChar      = ^Char;
   PPChar      = ^PChar;
   TAnsiChar   = Char;
   AnsiChar   = Char;
   PAnsiChar   = PChar;
   PPAnsiChar   = PPChar;
   UCS4Char    = type LongWord;
   PUCS4Char   = ^UCS4Char;
   TUCS4CharArray   = array[0..$effffff] of UCS4Char;
   PUCS4CharArray   = ^TUCS4CharArray;
   UCS4String   = array of UCS4Char;
   UTF8String   = type ansistring;
   PUTF8String   = ^UTF8String;
   HRESULT      = type Longint;
   TDateTime   = type Double;
   Error      = type Longint;
   PSingle      = ^Single;
   PDouble      = ^Double;
   PCurrency   = ^Currency;
   PComp      = ^Comp;
   PExtended   = ^Extended;
   PSmallInt   = ^Smallint;
   PShortInt   = ^Shortint;
   PInteger   = ^Longint;
   PByte      = ^Byte;
   PWord      = ^word;
   PDWord      = ^DWord;
   PLongWord   = ^LongWord;
   PLongint   = ^Longint;
   PCardinal   = ^Cardinal;
   PQWord      = ^QWord;
   PInt64      = ^Int64;
   PPtrInt      = ^PtrInt;
   PSizeInt   = ^SizeInt;
   PPointer   = ^Pointer;
   PPPointer   = ^PPointer;
   PBoolean   = ^Boolean;
   PWordBool   = ^WordBool;
   PLongBool   = ^LongBool;
   PShortString   = ^ShortString;
   PAnsiString   = ^AnsiString;
   PDate      = ^TDateTime;
   PError      = ^Error;
   PWideChar   = ^WideChar;
   PPWideChar   = ^PWideChar;
   WChar      = Widechar;
   UCS2Char   = WideChar;
   PUCS2Char   = PWideChar;
   PWideString   = ^WideString;

implementation

begin
end.
[Multiboot header]
[..\include\multiboot.pas]

Code: Select all

unit multiboot;

interface

const
   KERNEL_STACKSIZE = $4000;

   MULTIBOOT_BOOTLOADER_MAGIC = $2BADB002;

type
   Pelf_section_header_table_t = ^elf_section_header_table_t;
   elf_section_header_table_t = packed record
     num: DWORD;
     size: DWORD;
     addr: DWORD;
     shndx: DWORD;
   end;

   Pmultiboot_info_t = ^multiboot_info_t;
   multiboot_info_t = packed record
     flags: DWORD;
     mem_lower: DWORD;
     mem_upper: DWORD;
     boot_device: DWORD;
     cmdline: DWORD;
     mods_count: DWORD;
     mods_addr: DWORD;
     elf_sec: elf_section_header_table_t;
     mmap_length: DWORD;
     mmap_addr: DWORD;
   end;

   Pmodule_t = ^module_t;
   module_t = packed record
     mod_start: DWORD;
     mod_end: DWORD;
     name: DWORD;
     reserved: DWORD;
   end;

   Pmemory_map_t = ^memory_map_t;
   memory_map_t = packed record
     size: DWORD;
     base_addr_low: DWORD;
     base_addr_high: DWORD;
     length_low: DWORD;
     length_high: DWORD;
     mtype: DWORD;
   end;

implementation

end.

Re:pascal os development

Posted: Tue May 31, 2005 1:08 pm
by Kim
Now write a 32bit unit in pascal, with hello world code.

[kernel.pas]

Code: Select all

unit kernel;

interface

uses
      multiboot;

implementation

var
   vidmem: PChar = PChar($b8000);
   xpos: Integer = 0;
   ypos: Integer = 0;

procedure kclearscreen(); [public, alias: 'kclearscreen'];
var
   i: Integer;
begin
   for i := 0 to 3999 do
      vidmem[i] := #0;
end;

procedure kwritestr(s: PChar); [public, alias: 'kwritestr'];
var
   offset, i: Integer;
begin
   if (ypos > 24) then
      ypos := 0;

   if (xpos > 79) then
      xpos := 0;

   offset := (xpos shl 1) + (ypos * 160);
   i := 0;

   while (s[i] <> Char($0)) do
   begin
      vidmem[offset] := s[i];
      offset += 1;
      vidmem[offset] := #7;
      offset += 1;
      i += 1;
   end;

   xpos := (offset mod 160);
   ypos := (offset - xpos) div 160;
   xpos := xpos shr 1;
end;

procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: DWORD); stdcall; [public, alias: 'kmain'];
begin
       kclearscreen();
       kwritestr('HELLO WORLD FROM FREEPASCAL');

       xpos := 0;
       ypos += 1;

       if (mbmagic <> MULTIBOOT_BOOTLOADER_MAGIC) then
    kwritestr('Not booted by a multiboot-compliant bootloader!');
       else
         kwritestr('Booted by a multiboot-compliant bootloader!');
end;

end.


Now to build it:

[linker script]

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(kstart)
SECTIONS
{
  .text  0x100000 :
  {
    text = .; _text = .; __text = .;
    *(.text)
    . = ALIGN(4096);
  }
  .data  :
  {
    data = .; _data = .; __data = .;
    *(.data)
    kimage_text = .;
    LONG(text);
    kimage_data = .;
    LONG(data);
    kimage_bss = .;
    LONG(bss);
    kimage_end = .;
    LONG(end);
    . = ALIGN(4096);
  }
  .bss  :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .; _end = .; __end = .;
}

[build kernel.bat]

Code: Select all

@echo off

if %1'==build' goto build
if %1'==' goto force
if not %1'==' goto end

:force
echo Enabeling extentions...
cmd /V:ON /C "build kernel.bat" build
goto end

:build
cd .\kernel
echo Executing nasm...
..\bin\nasm -f elf stub.asm -o ..\output\obj\stub.o
echo Executing freepascal...
..\bin\ppc386 -Aelf -FE..\output\obj -Fi.\include -Fu.\support -n -OG3p3 -Si -Sc -Sg -Xd -Rintel -Tlinux kernel.pas
echo Executing linker...
for %%c in (..\output\obj\*.o) do set objects=!objects! %%c
..\bin\ld -T kernel.ld -o ..\output\kernel\kernel.elf%objects%
cd ..
echo Executing strip...
.\bin\strip .\output\kernel\kernel.elf
pause
goto end

:end
@echo on

Re:pascal os development

Posted: Tue May 31, 2005 1:12 pm
by Kim
I wrote this a bit fast so i hope its correct... got exams.
Meaby after my exams are done, ill upload a beginner package with compiler, build tools, and basic hello world kernel.

Greets Kim.

Re:pascal os development

Posted: Tue May 31, 2005 3:33 pm
by kerim
Well, this is just a tutorial for absolute beginners in OS development, and its goal is to show people that it is possible to develope an os in pascal just as it is in C or some other language.
Hopefully, I'll set up a web site for Pascal OS Dev these days, so if you're willing to contribute with your tutorials etc. you're WELCOME.

Thanks anyway.

Best regards ;)

Re:pascal os development

Posted: Wed Jun 01, 2005 8:05 am
by Dex4u
And people moan about the size of asm code ;D.

Re:pascal os development

Posted: Wed Jun 01, 2005 2:48 pm
by mystran
If that was the smallest possible Hello World kernel in Pascal then I don't want anything to do with that language. :)

Re:pascal os development

Posted: Wed Jun 01, 2005 3:32 pm
by kerim
I'm currently working on seting up a web site for Pascal OS Dev, and there you'll be able to download all the tutorials, and some code snipets.

Anyway, if someone is interested in Hello world kernel in pascal, feel free to email me, and I'll send it to you.