D Bare Bones: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
No edit summary
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(32 intermediate revisions by 15 users not shown)
Line 1:
{{Disputed|Talk:D Bare Bones}}
{{TutorialExplain}}
{{BeginnersWarning}}
{{Rating|1}}{{Template:Kernel designs}}
 
''In this Tutorial we will write a kernel in the [[D]] language and boot it.''
 
<big><b>WAIT! Have you read [[Getting Started]], [[Beginner Mistakes]], and some of the related [[:Category:OS theory|OS theory]]?</b></big>
 
==Preface==
Line 19 ⟶ 20:
==start.asm==
 
<syntaxhighlight lang="asm">
<pre>
 
global start
extern mainkmain ; Allow mainkmain() to be called from the assembly code
extern start_ctors, end_ctors, start_dtors, end_dtors
 
MODULEALIGN equ 1<<0
Line 38 ⟶ 40:
dd CHECKSUM
 
STACKSIZE equ 0x4000 ; 16k16 KiB if you're wondering
 
static_ctors_loop:
Line 56 ⟶ 58:
push ebx
 
call mainkmain
 
static_dtors_loop:
Line 79 ⟶ 81:
resb STACKSIZE
 
</syntaxhighlight>
</pre>
 
Assemble that with:
 
<syntaxhighlight lang="bash">nasm -f elf -o start.o start.asm</syntaxhighlight>
<pre>
nasm -f elf -o start.o start.asm
</pre>
 
==kernel.main.d==
<syntaxhighlight lang="d">
<pre>
module kernel.main;
import core.volatile;
extern(C) void mainkmain(uint magic, uint addr) {
ubyte* vidmem = cast(ubyte*)0xFFFF_8000_000B_8000; //Video memory address
for (int i = 0; i < 80*25*2; i++) { //Loops through the screen and clears it
volatileStore(vidmem + i, 0);
}
volatileStore(vidmem, 'D' & 0xFF); //Prints the letter D
volatileStore(vidmem + 1, 0x07); //Sets the colour for D to be light grey (0x07)
for (;;) { //Loop forever. You can add your kernel logic here
 
}
extern(C) void main(uint magic, uint addr)
{
byte *videoram = cast( byte * ) 0xb8000; // Set up video memory
videoram[0] = 'D';
videoram[1] = 0x07;
}
</syntaxhighlight>
</pre>
 
You then compile that with:
 
<syntaxhighlight lang="bash">gdc -fno-druntime -m32 -c kernel.main.d -o kernel.main.o -g</syntaxhighlight>
<pre>
gdc -nostdlib -nodefaultlibs -g -c -o kernel.main.o kernel.main.d
</pre>
 
==linker.ld==
<syntaxhighlight lang="c">
<pre>
OUTPUT_FORMAT("elf32-i386")
ENTRY (start)
 
Line 126 ⟶ 134:
data = .; _data = .; __data = .;
*(.data)
__CTOR_LIST__start_ctors = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) *(.ctors) LONG(0) __CTOR_END__end_ctors = .;
__DTOR_LIST__start_dtors = .; LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) *(.dtors) LONG(0) __DTOR_END__ end_dtors = .;
}
}
 
Line 140 ⟶ 147:
end = .; _end = .; __end = .;
}
</syntaxhighlight>
</pre>
 
Now finally you can link all of that with:
 
<syntaxhighlight lang="bash">ld -melf_i386 -T linker.ld -o kernel.bin start.o kernel.main.o</syntaxhighlight>
<pre>
ld -T linker.ld -o kernel.bin start.o kernel.main.o
</pre>
 
Your kernel is now kernel.bin, and can now be booted by grub., or run in qemu:
 
<syntaxhighlight lang="bash">qemu-system-i386 -kernel kernel.bin</syntaxhighlight>
 
Note the "-fno-druntime" argument above. This is how gdc spells the -betterC flag from other D compilers, and the "Better C" page of the D language reference explains how this limits the language. If you want to drop that you'll have to add the D runtime to your kernel.
 
Hopefully this has gotten you started on writing your operating system in the D programming language.
 
== Further reading ==
 
[[D_Bare_Bones_II|Creating a minimal output to the console]]
 
[[D_barebone_with_ldc2|D BareBone with 64 bit and ldc2]]
 
[[Category:Bare bones tutorials|D bare bones]]
[[Category:D]]