D Bare Bones: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(22 intermediate revisions by 11 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==
 
<sourcesyntaxhighlight lang="asm">
 
global start
extern mainkmain ; Allow mainkmain() to be called from the assembly code
extern start_ctors, end_ctors, start_dtors, end_dtors
 
Line 39 ⟶ 40:
dd CHECKSUM
 
STACKSIZE equ 0x4000 ; 16k16 KiB if you're wondering
 
static_ctors_loop:
Line 57 ⟶ 58:
push ebx
 
call mainkmain
 
static_dtors_loop:
Line 80 ⟶ 81:
resb STACKSIZE
 
</syntaxhighlight>
</source>
 
Assemble that with:
 
<sourcesyntaxhighlight lang="bash">nasm -f elf -o start.o start.asm</sourcesyntaxhighlight>
 
==kernel.main.d==
<sourcesyntaxhighlight lang="d">
module kernel.main;
import core.volatile;
 
extern(C) void mainkmain(uint magic, uint addr) {
int ypos = 0; //Starting points of the cursor
int xpos = 0;
ubyte* vidmem = cast(ubyte*)0xFFFF8000000B80000xFFFF_8000_000B_8000; //Video memory address
const uint COLUMNS = 80; //Screensize
const uint LINES = 24;
for (int i = 0; i < COLUMNS 80* LINES 25* 2; i++) { //Loops through the screen and clears it
 
volatile *volatileStore(vidmem + i) =, 0);
ubyte* vidmem = cast(ubyte*)0xFFFF8000000B8000; //Video memory address
 
for (int i = 0; i < COLUMNS * LINES * 2; i++) { //Loops through the screen and clears it
volatile *(vidmem + i) = 0;
}
volatile *volatileStore(vidmem + (xpos + ypos * COLUMNS) * 2) =, 'D' & 0xFF); //Prints the letter D
volatile *volatileStore(vidmem + (xpos + ypos * COLUMNS) * 2 + 1) =, 0x07); //Sets the colour for D to be light grey (0x07)
for (;;) { //Loop forever. You can add your kernel logic here
 
volatile *(vidmem + (xpos + ypos * COLUMNS) * 2) = 'D' & 0xFF; //Prints the letter D
volatile *(vidmem + (xpos + ypos * COLUMNS) * 2 + 1) = 0x07; //Sets the colour for D to be light grey (0x07)
 
for (;;) { //Loop forever. You can add your kernel logic here
}
}
</syntaxhighlight>
</source>
 
You then compile that with:
 
<sourcesyntaxhighlight lang="bash">gdc -nostdlib fno-nodefaultlibsdruntime -gm32 -c -o kernel.main.d -o kernel.main.do -g</sourcesyntaxhighlight>
 
==linker.ld==
<syntaxhighlight lang="c">
<pre>
OUTPUT_FORMAT(elf32-i386)
ENTRY (start)
Line 137 ⟶ 136:
start_ctors = .; *(.ctors) end_ctors = .;
start_dtors = .; *(.dtors) end_dtors = .;
}
}
 
Line 149 ⟶ 147:
end = .; _end = .; __end = .;
}
</syntaxhighlight>
</pre>
 
Now finally you can link all of that with:
 
<sourcesyntaxhighlight lang="bash">ld -melf_i386 -T linker.ld -o kernel.bin start.o kernel.main.o</sourcesyntaxhighlight>
 
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]]