git-svn-id: https://spexeah.com:8443/svn/Asuro@472 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

This commit is contained in:
kieron
2018-04-10 15:53:08 +00:00
parent c56319923a
commit c477deefb2
6 changed files with 3 additions and 13 deletions

@ -0,0 +1,67 @@
{ ************************************************
* Asuro
* Unit: bios_data_area
* Description: Data Structures controlled by
* the BIOS.
************************************************
* Author: K Morris
* Contributors:
************************************************ }
unit bios_data_area;
interface
type
TBDA = bitpacked record
COM1 : uint16;
COM2 : uint16;
COM3 : uint16;
COM4 : uint16;
LPT1 : uint16;
LPT2 : uint16;
LPT3 : uint16;
EBDA : uint16;
Hardware_Flags : uint16;
Keyboard_Flags : uint16;
Keyboard_Buffer : ARRAY[0..31] OF uint8;
Display_Mode : uint8;
BaseIO : uint16;
Ticks : uint16;
HDD_Count : uint8;
Keyboard_Start : uint16;
Keyboard_End : uint16;
Keyboard_State : uint8;
end;
PBDA = ^TBDA;
TMCFG = bitpacked record
Signature : Array[0..3] of Char;
Table_Length : uint32;
Revision : Byte;
Checksum : Byte;
OEM_ID : Array[0..5] of Byte;
OEM_Table_ID : uint64;
OEM_Revision : uint32;
Creator_ID : uint32;
Creator_Revision : uint32;
Reserved : uint64;
end;
PMCFG = ^TMCFG;
const
BDA : PBDA = PBDA($C0000400);
procedure tick_update(data : void);
implementation
uses
console, vmemorymanager;
procedure tick_update(data : void);
begin
BDA^.Ticks:= BDA^.Ticks + 1;
end;
end.

230
src/include/lists.pas Normal file

@ -0,0 +1,230 @@
unit lists;
interface
uses
console,
lmemorymanager,
util;
type
{ Managed Linked List }
PLinkedList = ^TLinkedList;
TLinkedList = record
Previous : PLinkedList;
Data : void;
Next : PLinkedList;
end;
PLinkedListBase = ^TLinkedListBase;
TLinkedListBase = record
Count : uint32;
Head : PLinkedList;
ElementSize : uint32;
end;
{ Managed Linked List }
function LL_New(ElementSize : uint32) : PLinkedListBase;
function LL_Add(LinkedList : PLinkedListBase) : Void;
function LL_Delete(LinkedList : PLinkedListBase; idx : uint32) : boolean;
function LL_Size(LinkedList : PLinkedListBase) : uint32;
function LL_Insert(LinkedList : PLinkedListBase; idx : uint32) : Void;
function LL_Get(LinkedList : PLinkedListBase; idx : uint32) : Void;
procedure LL_Free(LinkedList : PLinkedListBase);
procedure LL_TEST();
implementation
{ Managed Linked List }
function LL_New(ElementSize : uint32) : PLinkedListBase;
begin
LL_New:= PLinkedListBase(kalloc(sizeof(TLinkedListBase)));
LL_New^.ElementSize:= ElementSize;
LL_New^.Count:= 0;
LL_New^.Head:= nil;
end;
function LL_Add(LinkedList : PLinkedListBase) : Void;
var
Element : PLinkedList;
Base : PLinkedList;
Count : uint32;
begin
if LinkedList^.Head = nil then begin
Element:= PLinkedList(kalloc(sizeof(TLinkedList)));
Element^.Previous:= nil;
Element^.Next:= nil;
Element^.Data:= kalloc(LinkedList^.ElementSize);
memset(uint32(Element^.Data), 0, LinkedList^.ElementSize);
LinkedList^.Head:= Element;
LinkedList^.Count:= LinkedList^.Count + 1;
LL_Add:= Element^.Data;
end else begin
Base:= LinkedList^.Head;
Count:= 1;
While Base^.Next <> nil do begin
Base:= Base^.Next;
Count:= Count + 1;
end;
Element:= PLinkedList(kalloc(sizeof(TLinkedList)));
Base^.Next:= Element;
Element^.Previous:= Base;
Element^.Next:= nil;
Element^.Data:= kalloc(LinkedList^.ElementSize);
memset(uint32(Element^.Data), 0, LinkedList^.ElementSize);
LinkedList^.Count:= LinkedList^.Count + 1;
LL_Add:= Element^.Data;
end;
end;
function LL_Delete(LinkedList : PLinkedListBase; idx : uint32) : boolean;
var
Prev, Next : PLinkedList;
Base : PLinkedList;
i : uint32;
begin
Base:= LinkedList^.Head;
i:= 0;
while (i < idx) and (Base <> nil) do begin
i:= i + 1;
Base:= Base^.Next;
end;
if Base = nil then begin
LL_Delete:= false;
exit;
end;
Prev:= Base^.Previous;
Next:= Base^.Next;
if Prev = nil then begin
LinkedList^.Head:= Next;
end else begin
Prev^.Next:= Next;
end;
if Next <> nil then begin
Next^.Previous:= Prev;
end;
LinkedList^.Count:= LinkedList^.Count - 1;
kfree(void(Base^.Data));
kfree(void(Base));
LL_Delete:= True;
end;
function LL_Size(LinkedList : PLinkedListBase) : uint32;
begin
LL_Size:= LinkedList^.Count;
end;
function LL_Insert(LinkedList : PLinkedListBase; idx : uint32) : void;
var
i : uint32;
Base : PLinkedList;
Prev, Next : PLinkedList;
Element : PLinkedList;
begin
LL_Insert:= nil;
if idx >= LinkedList^.Count then exit;
Base:= LinkedList^.Head;
i:=0;
while (i < idx) and (Base <> nil) do begin
i:= i + 1;
Base:= Base^.Next;
end;
if Base = nil then exit;
Prev:= Base^.Previous;
Next:= Base;
Element:= PLinkedList(kalloc(sizeof(TLinkedList)));
Element^.Data:= kalloc(LinkedList^.ElementSize);
memset(uint32(Element^.Data), 0, LinkedList^.ElementSize);
Element^.Previous:= Prev;
Element^.Next:= Next;
if Prev = nil then begin
LinkedList^.Head:= Element;
end else begin
Prev^.Next:= Element;
end;
if Next <> nil then begin
Next^.Previous:= Element;
end;
LinkedList^.Count:= LinkedList^.Count + 1;
LL_Insert:= Element^.Data;
end;
function LL_Get(LinkedList : PLinkedListBase; idx : uint32) : void;
var
i : uint32;
Base : PLinkedList;
begin
LL_Get:= nil;
if idx >= LinkedList^.Count then exit;
Base:= LinkedList^.Head;
i:=0;
while (i < idx) and (Base <> nil) do begin
i:= i + 1;
Base:= Base^.Next;
end;
if Base = nil then exit;
LL_Get:= Base^.Data;
end;
procedure LL_Free(LinkedList : PLinkedListBase);
begin
while LL_Size(LinkedList) > 0 do begin
LL_Delete(LinkedList, 0);
end;
kfree(void(LinkedList));
end;
procedure LL_Test();
var
i : uint32;
LList : PLinkedListBase;
Elem : Void;
Str : PChar;
begin
console.writestringln('Add 3 Elements: ');
LList:= LL_New(10);
Elem:= LL_Add(LList);
Str:= PChar(Elem);
Str[0]:= 'H';
Str[1]:= 'E';
Str[2]:= 'L';
Str[3]:= 'L';
Str[4]:= 'O';
Elem:= LL_Add(LList);
Str:= PChar(Elem);
Str[0]:= 'W';
Str[1]:= 'O';
Str[2]:= 'R';
Str[3]:= 'L';
Str[4]:= 'D';
Elem:= LL_Add(LList);
Str:= PChar(Elem);
Str[0]:= 'T';
Str[1]:= 'E';
Str[2]:= 'S';
Str[3]:= 'T';
for i:=0 to LL_Size(LList)-1 do begin
console.writestringln(PChar(LL_Get(LList, i)));
end;
console.writestringln(' ');
console.writestringln('Remove Element 1:');
LL_Delete(LList, 1);
for i:=0 to LL_Size(LList)-1 do begin
console.writestringln(PChar(LL_Get(LList, i)));
end;
LL_Free(LList);
end;
end.

63
src/include/multiboot.pas Normal file

@ -0,0 +1,63 @@
{ ************************************************
* Asuro
* Unit: Multiboot
* Description: Mutliboot (GRUB) Structures.
************************************************
* Author: K Morris
* Contributors:
************************************************ }
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: uint32;
size: uint32;
addr: uint32;
shndx: uint32;
end;
Pmultiboot_info_t = ^multiboot_info_t;
multiboot_info_t = packed record
flags: uint32;
mem_lower: uint32;
mem_upper: uint32;
boot_device: uint32;
cmdline: uint32;
mods_count: uint32;
mods_addr: uint32;
elf_sec: elf_section_header_table_t;
mmap_length: uint32;
mmap_addr: uint32;
end;
Pmodule_t = ^module_t;
module_t = packed record
mod_start: uint32;
mod_end: uint32;
name: uint32;
reserved: uint32;
end;
Pmemory_map_t = ^memory_map_t;
memory_map_t = packed record
size: uint32;
base_addr : uint64;
length : uint64;
mtype: uint32;
end;
var
multibootinfo : Pmultiboot_info_t = nil;
multibootmagic : uint32;
implementation
end.

176
src/include/strings.pas Normal file

@ -0,0 +1,176 @@
{ ************************************************
* Asuro
* Unit: Strings
* Description: Collection of function for string manipulation.
************************************************
* Author: K Morris
* Contributors:
************************************************ }
unit strings;
interface
uses
util,
lmemorymanager;
function stringToUpper(str : pchar) : pchar;
function stringToLower(str : pchar) : pchar;
function stringEquals(str1, str2 : pchar) : boolean;
function stringCopy(str : pchar) : pchar;
function stringNew(size : uint32) : pchar;
function stringSize(str : pchar) : uint32;
function stringConcat(str1, str2 : pchar) : pchar;
function stringContains(str : pchar; sub : pchar) : boolean;
function stringToInt(str : pchar) : uint32;
function intToString(i : uint32) : pchar;
function boolToString(b : boolean; ext : boolean) : pchar;
implementation
function stringToUpper(str : pchar) : pchar;
var
result : pchar;
i : uint32;
begin
result:= stringCopy(str);
for i:=0 to stringSize(result) do begin
if (byte(result[i]) >= 97) and (byte(result[i]) <= 122) then result[i]:= char(byte(result[i]) - 32);
end;
stringToUpper:= result;
end;
function stringToLower(str : pchar) : pchar;
var
result : pchar;
i : uint32;
begin
result:= stringCopy(str);
for i:=0 to stringSize(result) do begin
if (byte(result[i]) >= 65) and (byte(result[i]) <= 90) then result[i]:= char(byte(result[i]) + 32);
end;
stringToLower:= result;
end;
function stringEquals(str1, str2 : pchar) : boolean;
var
i : uint32;
begin
stringEquals:= true;
if stringSize(str1) <> stringSize(str2) then begin
stringEquals:= false;
exit;
end else begin
for i:=0 to stringSize(str1)-1 do begin
if str1[i] <> str2[i] then begin
stringEquals:= false;
exit;
end;
end;
end;
end;
function stringCopy(str : pchar) : pchar;
var
result : pchar;
size : uint32;
begin
size:= stringSize(str);
result:= stringNew(size);
memcpy(uint32(str), uint32(result), size);
stringCopy:= result;
end;
function stringNew(size : uint32) : pchar;
var
result : pchar;
begin
result:= pchar(kalloc(size + 1));
memset(uint32(result), 0, size + 1);
stringNew:= result;
end;
function stringSize(str : pchar) : uint32;
var
i : uint32;
begin
i:=0;
while byte(str[i]) <> 0 do begin
inc(i);
end;
stringSize:=i;
end;
function stringConcat(str1, str2 : pchar) : pchar;
var
size : uint32;
result : pchar;
begin
result:= stringNew(stringSize(str1)+stringSize(str2));
memcpy(uint32(str1), uint32(result), stringSize(str1));
memcpy(uint32(str2), uint32(result+stringSize(str1)), stringSize(str2));
stringConcat:= result;
end;
function stringContains(str : pchar; sub : pchar) : boolean;
begin
stringContains:= false;
end;
function stringToInt(str : pchar) : uint32;
var
i : uint32;
x : uint32;
v : uint32;
r : uint32;
begin
stringToInt:= 0;
x:= 1;
r:= 0;
for i:=stringSize(str)-1 downto 0 do begin
v:= byte(str[i]) - 48;
if (v >= 0) and (v <= 9) then begin
r:= r + (v * x);
end;
x:= x * 10;
end;
stringToInt:= r;
end;
function intToString(i : uint32) : pchar;
begin
intToString:= ' ';
end;
function boolToString(b : boolean; ext : boolean) : pchar;
var
t : pchar;
f : pchar;
begin
if ext then begin
t:= stringCopy('true');
f:= stringCopy('false');
end else begin
t:= stringCopy('1');
f:= stringCopy('0');
end;
if b then begin
kfree(void(f));
boolToString:= t;
end else begin
kfree(void(t));
boolToString:= f;
end;
end;
end.

304
src/include/util.pas Normal file

@ -0,0 +1,304 @@
{ ************************************************
* Asuro
* Unit: util
* Description: Utilities for data manipulation
************************************************
* Author: K Morris
* Contributors:
************************************************ }
unit util;
{$ASMMODE intel}
interface
uses
bios_data_area;
procedure CLI();
procedure STI();
procedure GPF();
function hi(b : uint8) : uint8;
function lo(b : uint8) : uint8;
function switchendian(b : uint8) : uint8;
function getWord(i : uint32; hi : boolean) : uint16;
function getByte(i : uint32; index : uint8) : uint8;
procedure outb(port : uint16; val : uint8);
procedure outw(port : uint16; val : uint16);
procedure outl(port : uint16; val : uint32);
function inb(port : uint16) : uint8;
function inw(port : uint16) : uint16;
function inl(port : uint16) : uint32;
procedure memset(location : uint32; value : uint8; size : uint32);
procedure memcpy(source : uint32; dest : uint32; size : uint32);
procedure printmemory(source : uint32; length : uint32; col : uint32; delim : PChar; offset_row : boolean);
procedure halt_and_catch_fire();
procedure halt_and_dont_catch_fire();
procedure BSOD(fault : pchar; info : pchar);
procedure psleep(t : uint16);
var
endptr : uint32; external name '__end';
stack : uint32; external name 'KERNEL_STACK';
implementation
uses
console;
procedure printmemory(source : uint32; length : uint32; col : uint32; delim : PChar; offset_row : boolean);
var
buf : puint8;
i : uint32;
begin
buf:= puint8(source);
for i:=0 to length do begin
if offset_row and (i = 0) then begin
console.writehex(source + (i * col));
console.writestring(': ');
end;
console.writehexpair(buf[i]);
if ((i+1) MOD col) = 0 then begin
console.writestringln(' ');
if offset_row then begin
console.writehex(source + (i * col));
console.writestring(': ');
end;
end else begin
console.writestring(delim);
end;
end;
console.writestringln(' ');
end;
function hi(b : uint8) : uint8; [public, alias: 'util_hi'];
begin
hi:= (b AND $F0) SHR 4;
end;
function lo(b : uint8) : uint8; [public, alias: 'util_lo'];
begin
lo:= b AND $0F;
end;
procedure CLI(); assembler; nostackframe;
asm
CLI
end;
procedure STI(); assembler; nostackframe;
asm
STI
end;
procedure GPF(); assembler;
asm
INT 13
end;
function switchendian(b : uint8) : uint8; [public, alias: 'util_switchendian'];
begin
switchendian:= (lo(b) SHL 4) OR hi(b);
end;
procedure psleep(t : uint16);
var
t1, t2 : uint16;
begin
t1:= BDA^.Ticks;
t2:= BDA^.Ticks;
while t2-t1 < t do begin
t2:= BDA^.Ticks;
if t2 < t1 then break;
end;
end;
procedure outl(port : uint16; val : uint32); [public, alias: 'util_outl'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
MOV EAX, val
OUT DX, EAX
POP EDX
POP EAX
end;
end;
procedure outw(port : uint16; val : uint16); [public, alias: 'util_outw'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
MOV AX, val
OUT DX, AX
POP EDX
POP EAX
end;
end;
procedure outb(port : uint16; val : uint8); [public, alias: 'util_outb'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
MOV AL, val
OUT DX, AL
POP EDX
POP EAX
end;
end;
procedure halt_and_catch_fire(); [public, alias: 'util_halt_and_catch_fire'];
begin
asm
cli
hlt
end;
end;
procedure halt_and_dont_catch_fire(); [public, alias: 'util_halt_and_dont_catch_fire'];
begin
while true do begin
end;
end;
function inl(port : uint16) : uint32; [public, alias: 'util_inl'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
IN EAX, DX
MOV inl, EAX
POP EDX
POP EAX
end;
end;
function inw(port : uint16) : uint16; [public, alias: 'util_inw'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
IN AX, DX
MOV inw, AX
POP EDX
POP EAX
end;
end;
function inb(port : uint16) : uint8; [public, alias: 'util_inb'];
begin
asm
PUSH EAX
PUSH EDX
MOV DX, port
IN AL, DX
MOV inb, AL
POP EDX
POP EAX
end;
end;
procedure memset(location : uint32; value : uint8; size : uint32);
var
loc : puint8;
i : uint32;
begin
for i:=0 to size-1 do begin
loc:= puint8(location + i);
loc^:= value;
end;
end;
procedure memcpy(source : uint32; dest : uint32; size : uint32);
var
src, dst : puint8;
i : uint32;
begin
for i:=0 to size-1 do begin
src:= puint8(source + i);
dst:= puint8(dest + i);
dst^:= src^;
end;
end;
function getWord(i : uint32; hi : boolean) : uint16;
begin
if hi then begin
getWord:= (i AND $FFFF0000) SHR 16;
end else begin
getWord:= (i AND $0000FFFF);
end;
end;
function getByte(i : uint32; index : uint8) : uint8;
var
mask : uint32;
begin
mask:= ($FF SHL (8*index));
getByte:= (i AND mask) SHR (8*index);
end;
procedure BSOD(fault : pchar; info : pchar);
begin
console.setdefaultattribute(console.combinecolors(white, Red));
console.clear;
console.writestringln(' ');
console.writestring(' ');
console.setdefaultattribute(console.combinecolors(black, white));
console.writestring(' ');
console.setdefaultattribute(console.combinecolors(white, Red));
console.writestringln(' ');
console.writestring(' ');
console.setdefaultattribute(console.combinecolors(black, white));
console.writestring(' SOMETHING HAS GONE WRONG AND ASURO HAD TO STOP! ');
console.setdefaultattribute(console.combinecolors(lwhite, Red));
console.writestringln(' ');
console.writestring(' ');
console.setdefaultattribute(console.combinecolors(black, white));
console.writestring(' ');
console.setdefaultattribute(console.combinecolors(white, Red));
console.writestringln(' ');
console.writestringln(' ');
console.writestringln(' ');
console.writestringln(' Asuro encountered an error and could not recover.');
console.writestringln(' ');
console.writestringln(' ');
console.writestringln(' The fault could have been caused by one or more of the following: ');
console.writestringln(' - A misconfigured device.');
console.writestringln(' - A malfunctioning driver.');
console.writestringln(' - A malfunctioning device.');
console.writestringln(' - A devlopers inability to handle faults correctly.');
console.writestringln(' - A Monkey inside the PC Case.');
console.writestringln(' - Spilt Coffeee.');
console.writestringln(' ');
console.writestringln(' ');
console.writestringln(' Details of the fault (for those boring enough to read) are as follows: ');
console.writestringln(' ');
console.writestring(' Fault ID: ');
console.writestringln(fault);
console.writestring(' Fault Info: ');
console.writestringln(info);
console.writestringln(' ');
console.writestringln(' ');
halt_and_catch_fire();
end;
end.