This commit is contained in:
Tiberiu Chibici 2021-09-14 18:35:52 +03:00
parent f052f2294e
commit d605c6a016
84 changed files with 3647 additions and 1192 deletions

Binary file not shown.

View File

@ -1,10 +1,13 @@
;***** memory.inc ****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Enable A20 address line *
;* ======================= *
;* *
;************************************************************ cta os */
%ifndef __A20_INC_CTA000__
;******************************************** %define __A20_INC_CTA000__
; Enable A20 address line
;
; OS Development Series
;********************************************
bits 16 ; real mode 16 bit code bits 16 ; real mode 16 bit code
@ -53,3 +56,5 @@ a20wait2:
test al,1 test al,1
jz a20wait2 jz a20wait2
ret ret
%endif

View File

@ -1,39 +1,38 @@
;***** bootinfo.inc **************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Multiboot information structure *
;* =============================== *
;* *
;************************************************************ cta os */
;******************************************************* %ifndef __BOOTINFO_INC_CTA001__
; %define __BOOTINFO_INC_CTA001__
; bootinfo.inc
; multiboot information structure
;
; OS Development Series
;*******************************************************
%ifndef __BOOTINFO_INC_67343546FDCC56AAB872_INCLUDED__
%define __BOOTINFO_INC_67343546FDCC56AAB872_INCLUDED__
struc multiboot_info struc multiboot_info
.flags resd 1 .flags resd 1
.memoryLo resd 1 .memoryLo resd 1
.memoryHi resd 1 .memoryHi resd 1
.bootDevice resd 1 .bootDevice resd 1
.cmdLine resd 1 .cmdLine resd 1
.mods_count resd 1 .mods_count resd 1
.mods_addr resd 1 .mods_addr resd 1
.syms0 resd 1 .syms0 resd 1
.syms1 resd 1 .syms1 resd 1
.syms2 resd 1 .syms2 resd 1
.mmap_length resd 1 .mmap_length resd 1
.mmap_addr resd 1 .mmap_addr resd 1
.drives_length resd 1 .drives_length resd 1
.drives_addr resd 1 .drives_addr resd 1
.config_table resd 1 .config_table resd 1
.bootloader_name resd 1 .bootloader_name resd 1
.apm_table resd 1 .apm_table resd 1
.vbe_control_info resd 1 .vbe_control_info resd 1
.vbe_mode_info resd 1 .vbe_mode_info resd 1
.vbe_mode resw 1 .vbe_mode resw 1
.vbe_interface_seg resw 1 .vbe_interface_seg resw 1
.vbe_interface_off resw 1 .vbe_interface_off resw 1
.vbe_interface_len resw 1 .vbe_interface_len resw 1
endstruc endstruc

View File

@ -1,9 +1,16 @@
;***** common.inc ****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Common definitions, variables *
;* ============================= *
;* *
;************************************************************ cta os */
%ifndef _COMMON_INC_INCLUDED %ifndef _COMMON_INC_CTA002__
%define _COMMON_INC_INCLUDED %define _COMMON_INC_CTA002__
; where the kernel is to be loaded to in protected mode ; where the kernel is to be loaded to in protected mode
%define IMAGE_PMODE_BASE 0x100000 %define IMAGE_PMODE_BASE 0xC0000000
; where the kernel is to be loaded to in real mode ; where the kernel is to be loaded to in real mode
%define IMAGE_RMODE_BASE 0x3000 %define IMAGE_RMODE_BASE 0x3000

View File

@ -1,14 +1,13 @@
;***** FAT12.inc *****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Fat12 filesystem for 3.5" floppies *
;* ================================== *
;* *
;************************************************************ cta os */
;******************************************************* %ifndef __FAT12_INC_CTA003__
; %define __FAT12_INC_CTA003__
; Fat12.inc
; FAT12 filesystem for 3-1/2 floppies
;
; OS Development Series
;*******************************************************
%ifndef __FAT12_INC_67343546FDCC56AAB872_INCLUDED__
%define __FAT12_INC_67343546FDCC56AAB872_INCLUDED__
bits 16 bits 16

View File

@ -1,18 +1,17 @@
;***** floppy16.inc **************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Floppy drive interface routines *
;* =============================== *
;* *
;************************************************************ cta os */
;******************************************************* %ifndef __FLOPPY16_INC_CTA004__
; %define __FLOPPY16_INC_CTA004__
; Floppy16.inc
; Floppy drive interface routines
;
; OS Development Series
;*******************************************************
%ifndef __FLOPPY16_INC_67343546FDCC56AAB872_INCLUDED__
%define __FLOPPY16_INC_67343546FDCC56AAB872_INCLUDED__
bits 16 bits 16
bpbOEM db "My OS " bpbOEM db "CTA OS "
bpbBytesPerSector: DW 512 bpbBytesPerSector: DW 512
bpbSectorsPerCluster: DB 1 bpbSectorsPerCluster: DB 1
bpbReservedSectors: DW 1 bpbReservedSectors: DW 1
@ -29,7 +28,7 @@ bsDriveNumber: DB 0
bsUnused: DB 0 bsUnused: DB 0
bsExtBootSignature: DB 0x29 bsExtBootSignature: DB 0x29
bsSerialNumber: DD 0xa0a1a2a3 bsSerialNumber: DD 0xa0a1a2a3
bsVolumeLabel: DB "MOS FLOPPY " bsVolumeLabel: DB "CTA OS "
bsFileSystem: DB "FAT12 " bsFileSystem: DB "FAT12 "
datasector dw 0x0000 datasector dw 0x0000

View File

@ -1,14 +1,14 @@
;***** gdt.inc *******************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Global Descriptor Table routines *
;* ===================== *
;* *
;************************************************************ cta os */
;************************************************* %ifndef __GDT_INC_CTA005__
; Gdt.inc %define __GDT_INC_CTA005__
; -GDT Routines
;
; OS Development Series
;*************************************************
%ifndef __GDT_INC_67343546FDCC56AAB872_INCLUDED__
%define __GDT_INC_67343546FDCC56AAB872_INCLUDED__
bits 16 bits 16

View File

@ -0,0 +1,98 @@
gatherinfo:
pusha
push es
xor eax, eax
mov ebx, eax
mov ecx, eax
mov edx, eax
;Boot device
mov dword [multiboot_info_bootDevice], 0 ; drive 0
;Detect current video mode
mov ah, 0Fh
int 10h
mov byte [multiboot_info_video_mode], al
mov byte [multiboot_info_video_columns], ah
mov byte [multiboot_info_video_page], bh
;Get VESA information
xor eax, eax
xor ebx, ebx
xor edx, edx
mov dword [TemporaryStorage], vbeControllerInfo
mov ax, word [TemporaryStorage]
mov dx, word [TemporaryStorage+2]
shl ax, 1
mov es, ax
mov di, dx
mov ax, 0x4f00
int 10h
cmp ax, 0x004F
; not correct
je vesa_00_ok
mov dword [multiboot_info_vbe_control_info], 0x0 ; failure
jmp vesa_00_done
; correct
vesa_00_ok:
mov dword [multiboot_info_vbe_control_info], vbeControllerInfo ; success
vesa_00_done:
;Write VBE 2.0+ Interface pointers to 0
mov word [multiboot_info_vbe_interface_seg], 0
mov word [multiboot_info_vbe_interface_off], 0
mov word [multiboot_info_vbe_interface_len], 0
;Get VESA Current mode
mov ax, 4f03h
int 10h
cmp ax, 004fh
je vesa_01_ok
; not ok:
mov word [multiboot_info_vbe_mode], 0
jmp vesa_01_done
vesa_01_ok:
mov word [multiboot_info_vbe_mode], bx
vesa_01_done:
;Get some info about current VESA mode
xor eax, eax
mov ebx, eax
mov edx, eax
mov eax, vbeControllerInfo
push eax
and eax, 0fh
mov dx, ax
pop eax
shr eax, 4
mov es, ax
mov di, dx
mov ax, 4f01h
mov cx, [multiboot_info_vbe_mode]
int 10h
cmp ax, 004fh
je vesa_02_ok
; not ok:
mov word [multiboot_info_vbe_mode_info], 0
jmp vesa_02_done
vesa_02_ok:
mov word [multiboot_info_vbe_mode_info], vbeModeInfo
vesa_02_done:
pop es
popa
ret

View File

@ -1,13 +1,13 @@
;***** memory.inc ****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Basic memory routines *
;* ===================== *
;* *
;************************************************************ cta os */
;************************************************* %ifndef __MEMORY_INC_CTA006__
; Memory.inc %define __MEMORY_INC_CTA006__
; -Basic memory routines
;
; OS Development Series
;*************************************************
%ifndef __MEMORY_INC_67343546FDCC56AAB872_INCLUDED__
%define __MEMORY_INC_67343546FDCC56AAB872_INCLUDED__
bits 16 bits 16
@ -29,43 +29,50 @@ endstruc
;--------------------------------------------- ;---------------------------------------------
BiosGetMemoryMap: BiosGetMemoryMap:
pushad
xor ebx, ebx xor ebx, ebx
xor bp, bp ; number of entries stored here xor bp, bp
mov edx, 'PAMS' ; 'SMAP' mov edx, 0x0534D4150 ; Place "SMAP" into edx
mov eax, 0xe820 mov eax, 0xe820
mov ecx, 24 ; memory map entry struct is 24 bytes mov [es:di + 20], dword 1 ; force a valid ACPI 3.X entry
int 0x15 ; get first entry mov ecx, 24
jc .error int 0x15
cmp eax, 'PAMS' ; bios returns SMAP in eax jc short .failed ; carry set on first call means "unsupported function"
jne .error mov edx, 0x0534D4150 ; Some BIOSes apparently trash this register?
test ebx, ebx ; if ebx=0 then list is one entry long; bail out cmp eax, edx ; on success, eax must have been reset to "SMAP"
je .error jne short .failed
jmp .start test ebx, ebx ; ebx = 0 implies list is only 1 entry long (worthless)
.next_entry: je short .failed
mov edx, 'PAMS' ; some bios's trash this register jmp short .jmpin
mov ecx, 24 ; entry is 24 bytes .e820lp:
mov eax, 0xe820 mov eax, 0xe820 ; eax, ecx get trashed on every int 0x15 call
int 0x15 ; get next entry mov [es:di + 20], dword 1 ; force a valid ACPI 3.X entry
.start: mov ecx, 24 ; ask for 24 bytes again
jcxz .skip_entry ; if actual returned bytes is 0, skip entry int 0x15
jc short .e820f ; carry set means "end of list already reached"
mov edx, 0x0534D4150 ; repair potentially trashed register
.jmpin:
jcxz .skipent ; skip any 0 length entries
cmp cl, 20 ; got a 24 byte ACPI 3.X response?
jbe short .notext
test byte [es:di + 20], 1 ; if so: is the "ignore this data" bit clear?
je short .skipent
.notext: .notext:
mov ecx, [es:di + MemoryMapEntry.length] ; get length (low dword) mov ecx, [es:di + 8] ; get lower dword of memory region length
test ecx, ecx ; if length is 0 skip it test ecx, ecx ; is the qword == 0?
jne short .good_entry jne short .goodent
mov ecx, [es:di + MemoryMapEntry.length + 4]; get length (upper dword) mov ecx, [es:di + 12] ; get upper dword of memory region length
jecxz .skip_entry ; if length is 0 skip it jecxz .skipent ; if length qword is 0, skip entry
.good_entry: .goodent:
inc bp ; increment entry count inc bp ; got a good entry: ++count, move to next storage spot
add di, 24 ; point di to next entry in buffer add di, 24
.skip_entry: .skipent:
cmp ebx, 0 ; if ebx return is 0, list is done test ebx, ebx ; if ebx resets to 0, list is complete
jne .next_entry ; get next entry jne short .e820lp
jmp .done .e820f:
.error: ret ; bp=entry count
stc .failed:
.done: stc ; "function unsupported" error exit
popad
ret ret
;--------------------------------------------- ;---------------------------------------------
@ -75,21 +82,17 @@ BiosGetMemoryMap:
; ret\ bx=0 and ax= -1 on error ; ret\ bx=0 and ax= -1 on error
;--------------------------------------------- ;---------------------------------------------
BiosGetMemorySize64MB_32Bit: BiosGetMemorySize64MB_32bit:
push ecx push ecx
push edx push edx
xor ecx, ecx xor ecx, ecx
xor edx, edx xor edx, edx
mov ax, 0xe881 mov eax, 0xe881
int 0x15 int 0x15
jc .error jc .error
cmp ah, 0x86 ;unsupported function jcxz .use_ax ;bios may have stored it in ax,bx or cx,dx. test if cx is 0
je .error mov eax, ecx ;its not, so it should contain mem size; store it
cmp ah, 0x80 ;invalid command mov ebx, edx
je .error
jcxz .use_ax ;bios may have stored it in ax,bx or cx,dx. test if cx is 0
mov ax, cx ;its not, so it should contain mem size; store it
mov bx, dx
.use_ax: .use_ax:
pop edx ;mem size is in ax and bx already, return it pop edx ;mem size is in ax and bx already, return it
@ -102,7 +105,7 @@ BiosGetMemorySize64MB_32Bit:
pop edx pop edx
pop ecx pop ecx
ret ret
;--------------------------------------------- ;---------------------------------------------
; Get memory size for >64M configuations ; Get memory size for >64M configuations
; ret\ ax=KB between 1MB and 16MB ; ret\ ax=KB between 1MB and 16MB
@ -132,8 +135,6 @@ BiosGetMemorySize64MB:
ret ret
.error: .error:
mov si, msgNotSupported
call Puts16
mov ax, -1 mov ax, -1
mov bx, 0 mov bx, 0
pop edx pop edx
@ -170,6 +171,4 @@ BiosGetExtendedMemorySize:
ret ret
msgNotSupported db 0x0A, 0x0D, "BiosGetMemorySize64MB: function not supported.",0x0A, 0x0D, 0x00
%endif %endif

83
SysBoot/stage2/paging.inc Normal file
View File

@ -0,0 +1,83 @@
;***** paging.inc ****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Basic paging for bootloader *
;* =========================== *
;* *
;************************************************************ cta os */
%ifndef __PAGING_INC_CTA007__
%define __PAGING_INC_CTA007__
[bits 32]
%define PAGE_DIR 0x9C000 ; page directory table
%define PAGE_TABLE_0 0x9D000 ; 0th page table. Address must be 4KB aligned
%define PAGE_TABLE_768 0x9E000 ; 768th page table. Address must be 4KB aligned
%define PAGE_TABLE_ENTRIES 1024 ; each page table has 1024 entries
%define PRIV 3 ; attributes (page is present;page is writable; supervisor mode)
;****************************************
; Enable Paging
;****************************************
EnablePaging:
pusha ; save stack frame
;------------------------------------------
; idenitity map 1st page table (4MB)
;------------------------------------------
mov eax, PAGE_TABLE_0 ; first page table
mov ebx, 0x0 | PRIV ; starting physical address of page
mov ecx, PAGE_TABLE_ENTRIES ; for every page in table...
.loop:
mov dword [eax], ebx ; write the entry
add eax, 4 ; go to next page entry in table (Each entry is 4 bytes)
add ebx, 4096 ; go to next page address (Each page is 4Kb)
loop .loop ; go to next entry
;------------------------------------------
; set up the entries in the directory table
;------------------------------------------
mov eax, PAGE_TABLE_0 | PRIV ; 1st table is directory entry 0
mov dword [PAGE_DIR], eax
mov eax, PAGE_TABLE_768 | PRIV ; 768th entry in directory table
mov dword [PAGE_DIR+(768*4)], eax
;------------------------------------------
; install directory table
;------------------------------------------
mov eax, PAGE_DIR
mov cr3, eax
;------------------------------------------
; enable paging
;------------------------------------------
mov eax, cr0
or eax, 0x80000000
mov cr0, eax
;------------------------------------------
; map the 768th table to physical addr 1MB
; the 768th table starts the 3gb virtual address
;------------------------------------------
mov eax, PAGE_TABLE_768 ; first page table
mov ebx, 0x100000 | PRIV ; starting physical address of page
mov ecx, PAGE_TABLE_ENTRIES ; for every page in table...
.loop2:
mov dword [eax], ebx ; write the entry
add eax, 4 ; go to next page entry in table (Each entry is 4 bytes)
add ebx, 4096 ; go to next page address (Each page is 4Kb)
loop .loop2 ; go to next entry
popa
ret
%endif

View File

@ -1,12 +1,10 @@
;***** stage2.asm ****************************************************
;******************************************************* ;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
; ;* *
; Stage2.asm ;* Stage 2 Bootloader *
; Stage2 Bootloader ;* ================== *
; ;* *
; OS Development Series ;************************************************************ cta os */
;*******************************************************
bits 16 bits 16
org 0x500 org 0x500
@ -24,6 +22,7 @@ jmp main ; go to start
%include "common.inc" %include "common.inc"
;%include "bootinfo.inc" ;%include "bootinfo.inc"
%include "memory.inc" %include "memory.inc"
%include "getdata.inc"
;******************************************************* ;*******************************************************
; Data Section ; Data Section
@ -32,28 +31,51 @@ jmp main ; go to start
msgFailure db 0x0D, 0x0A, "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0x0D, 0x0A, 0x0A, 0x00 msgFailure db 0x0D, 0x0A, "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0x0D, 0x0A, 0x0A, 0x00
boot_info: boot_info:
multiboot_info_flags dd 0 ; Memory
multiboot_info_memoryLo dd 0 multiboot_info_memoryLo dd 0
multiboot_info_memoryHi dd 0 multiboot_info_memoryHi dd 0
multiboot_info_bootDevice dd 0 ; Boot device
multiboot_info_cmdLine dd 0 multiboot_info_bootDevice dd 0
multiboot_info_mods_count dd 0 ; Pointer to a char[] string. (not implemented)
multiboot_info_mods_addr dd 0 multiboot_info_cmdLine dd 0
multiboot_info_syms0 dd 0 ; Other modules loaded by bootloader. (not implemented
multiboot_info_syms1 dd 0 multiboot_info_mods_count dd 0
multiboot_info_syms2 dd 0 multiboot_info_mods_addr dd 0
multiboot_info_mmap_length dd 0 ; Pointer to memory map
multiboot_info_mmap_addr dd 0 multiboot_info_mmap_length dd 0
multiboot_info_drives_length dd 0 multiboot_info_mmap_addr dd 0
multiboot_info_drives_addr dd 0 ; TODO:
multiboot_info_config_table dd 0 multiboot_info_drives_length dd 0
multiboot_info_bootloader_name dd 0 multiboot_info_drives_addr dd 0
multiboot_info_apm_table dd 0 ; BIOS ROM config table. TODO:
multiboot_info_vbe_control_info dd 0 multiboot_info_config_table dd 0
multiboot_info_vbe_mode_info dw 0 ; CTA bootloader name
multiboot_info_vbe_interface_seg dw 0 multiboot_info_bootloader_name db "CTA", 0
multiboot_info_vbe_interface_off dw 0 ; TODO:
multiboot_info_vbe_interface_len dw 0 multiboot_info_apm_table dd 0
; Returns VGA current video mode info
multiboot_info_video_mode db 0
multiboot_info_video_columns db 0
multiboot_info_video_page db 0
; Returns VESA information
multiboot_info_vbe_control_info dd 0
multiboot_info_vbe_mode_info dd 0
multiboot_info_vbe_mode dw 0
multiboot_info_vbe_interface_seg dw 0
multiboot_info_vbe_interface_off dw 0
multiboot_info_vbe_interface_len dw 0
vbeControllerInfo:
vbeControllerInfo_signature dd 0 ; "VESA", taken as 4 bytes
vbeControllerInfo_version dw 0 ; 0x0300 for VBE 3.0
vbeControllerInfo_oemString dd 0 ; isa vbeFarPtr, taken as 2 shorts
vbeControllerInfo_capabilities dd 0 ; taken as 4 bytes
vbeControllerInfo_videomodes dd 0 ; isa vbeFarPtr, taken as 2 shorts
vbeControllerInfo_totalMemory dw 0 ; as # of 64k blocks
TemporaryStorage dd 0 ; temporary storage
main: main:
@ -70,34 +92,48 @@ main:
mov sp, 0xFFFF mov sp, 0xFFFF
sti ; enable interrupts sti ; enable interrupts
mov [multiboot_info_bootDevice], dl
call _EnableA20 call _EnableA20
call InstallGDT call InstallGDT
sti sti
;-------------------------------;
; Fill in the boot structure ;
;-------------------------------;
; Memory Size
xor eax, eax xor eax, eax
xor ebx, ebx xor ebx, ebx
call BiosGetMemorySize64MB call BiosGetMemorySize64MB
mov word [multiboot_info_memoryHi], bx push eax
mov word [multiboot_info_memoryLo], ax mov eax, 64
mul ebx
mov ecx, eax
pop eax
add eax, ecx
add eax, 1024 ; the routine doesnt add the KB between 0-1MB; add it
mov dword [multiboot_info_memoryHi], 0
mov dword [multiboot_info_memoryLo], eax
;Memory map
mov eax, 0x0 mov eax, 0x0
mov ds, ax mov ds, ax
mov di, 0x1000 mov di, 0x1000
call BiosGetMemoryMap call BiosGetMemoryMap
mov dword [multiboot_info_mmap_addr], 0x1000
mov dword [multiboot_info_mmap_addr], 0x1000 ; address
xor eax, eax xor eax, eax
mov ax, bp mov ax, bp
mov dword [multiboot_info_mmap_length], eax mov dword [multiboot_info_mmap_length], eax ; length
call gatherinfo
call LoadRoot
call LoadRoot mov ebx, 0
mov ebx, 0 mov ebp, IMAGE_RMODE_BASE
mov ebp, IMAGE_RMODE_BASE mov esi, ImageName
mov esi, ImageName call LoadFile ; load our file
call LoadFile ; load our file
mov dword [ImageSize], ecx mov dword [ImageSize], ecx
cmp ax, 0 cmp ax, 0
je EnterStage3 je EnterStage3
@ -129,6 +165,8 @@ EnterStage3:
bits 32 bits 32
%include "paging.inc"
BadImage db "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0 BadImage db "FATAL ERROR: Kernel file KERNEL.CTA missing or corrupt. Press Any Key to Reboot.", 0
Stage3: Stage3:
@ -143,6 +181,7 @@ Stage3:
mov es, ax mov es, ax
mov esp, 90000h ; stack begins from 90000h mov esp, 90000h ; stack begins from 90000h
call EnablePaging
CopyImage: CopyImage:
mov eax, dword [ImageSize] mov eax, dword [ImageSize]
@ -156,7 +195,7 @@ CopyImage:
mov ecx, eax mov ecx, eax
rep movsd ; copy image to its protected mode address rep movsd ; copy image to its protected mode address
mov eax, 0x2badb002 ; multiboot specs say eax should be this mov eax, 0xC0DEcC7A ; cta bootloader specific
mov ebx, 0 mov ebx, 0
;edx=8 ;edx=8
@ -169,4 +208,56 @@ CopyImage:
cli cli
hlt hlt
vbeModeInfo:
vbeModeInfo_attributes dw 0
vbeModeInfo_winA db 0
vbeModeInfo_winB db 0
vbeModeInfo_granularity dw 0
vbeModeInfo_winsize dw 0
vbeModeInfo_segmentA dw 0
vbeModeInfo_segmentB dw 0
vbeModeInfo_realFctPtr dd 0
vbeModeInfo_pitc dw 0 ; // bytes per scanline
vbeModeInfo_Xres dw 0
vbeModeInfo_Yres dw 0
vbeModeInfo_Wchar db 0
vbeModeInfo_Ychar db 0
vbeModeInfo_planes db 0
vbeModeInfo_bpp db 0
vbeModeInfo_banks db 0
vbeModeInfo_memory_model db 0
vbeModeInfo_bank_size db 0
vbeModeInfo_image_pages db 0
vbeModeInfo_reserved0 db 0
; VBE v1.2+
vbeModeInfo_red_mask db 0
vbeModeInfo_red_position db 0
vbeModeInfo_green_mask db 0
vbeModeInfo_green_position db 0
vbeModeInfo_blue_mask db 0
vbeModeInfo_blue_position db 0
vbeModeInfo_rsv_mask db 0
vbeModeInfo_rsv_position db 0
vbeModeInfo_directcolor_attrib db 0
; VBE v2.0+
vbeModeInfo_physbase dd 0
vbeModeInfo_start_offscreen_mem dd 0
vbeModeInfo_size_offscreen_mem dw 0
; VBE v3.0+
vbeModeInfo_bytes_per_scanline dw 0
vbeModeInfo_number_images_banked db 0
vbeModeInfo_number_images_linear db 0
vbeModeInfo_linear_red_mask db 0
vbeModeInfo_linear_red_pos db 0
vbeModeInfo_linear_green_mask db 0
vbeModeInfo_linear_green_pos db 0
vbeModeInfo_linear_blue_mask db 0
vbeModeInfo_linear_blue_pos db 0
vbeModeInfo_linear_res_mask db 0
vbeModeInfo_linear_res_pos db 0
vbeModeInfo_max_pixel_clock dd 0

View File

@ -1,13 +1,14 @@
;***** stdio.inc *****************************************************
;* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
;* *
;* Standard Input/Output routines *
;* ============================== *
;* *
;************************************************************ cta os */
;*************************************************
; stdio.inc
; -Input/Output routines
;
; OS Development Series
;*************************************************
%ifndef __STDIO_INC_67343546FDCC56AAB872_INCLUDED__ %ifndef __STDIO_INC_CTA008__
%define __STDIO_INC_67343546FDCC56AAB872_INCLUDED__ %define __STDIO_INC_CTA008__
;========================================================== ;==========================================================
@ -82,15 +83,15 @@ PutINT:
; 32 Bit Protected Mode Routines ; 32 Bit Protected Mode Routines
;========================================================== ;==========================================================
bits 32 ;bits 32
%define VIDMEM 0xB8000 ; video memory ;%define VIDMEM 0xB8000 ; video memory
%define COLS 80 ; width and height of screen ;%define COLS 80 ; width and height of screen
%define LINES 25 ;%define LINES 25
%define CHAR_ATTRIB 14 ; character attribute (White text on black background) ;%define CHAR_ATTRIB 14 ; character attribute (White text on black background)
_CurX db 0 ; current x/y location ;_CurX db 0 ; current x/y location
_CurY db 0 ;_CurY db 0
;**************************************************; ;**************************************************;
; Putch32 () ; Putch32 ()
@ -98,56 +99,56 @@ _CurY db 0
; BL => Character to print ; BL => Character to print
;**************************************************; ;**************************************************;
Putch32: ;Putch32:
;
pusha ; pusha
mov edi, VIDMEM ; mov edi, VIDMEM
;
xor eax, eax ; clear eax ; xor eax, eax ; clear eax
;
; y * screen width ; ; y * screen width
;
mov ecx, COLS*2 ; Mode 7 has 2 bytes per char, so its COLS*2 bytes per line ; mov ecx, COLS*2 ; Mode 7 has 2 bytes per char, so its COLS*2 bytes per line
mov al, byte [_CurY] ; get y pos ; mov al, byte [_CurY] ; get y pos
mul ecx ; multiply y*COLS ; mul ecx ; multiply y*COLS
push eax ; save eax--the multiplication ; push eax ; save eax--the multiplication
; now add _CurX * 2 ; now add _CurX * 2
mov al, byte [_CurX] ; multiply _CurX by 2 because it is 2 bytes per char ; mov al, byte [_CurX] ; multiply _CurX by 2 because it is 2 bytes per char
mov cl, 2 ; mov cl, 2
mul cl ; mul cl
pop ecx ; pop y*COLS result ; pop ecx ; pop y*COLS result
add eax, ecx ; add eax, ecx
; add the position to draw to the base of vid memory ; add the position to draw to the base of vid memory
xor ecx, ecx ; xor ecx, ecx
add edi, eax ; add it to the base address ; add edi, eax ; add it to the base address
;
; watch for new line ; watch for new line
cmp bl, 0x0A ; is it a newline character? ; cmp bl, 0x0A ; is it a newline character?
je .Row ; yep--go to next row ; je .Row ; yep--go to next row
;
; print the character ; print the character
mov dl, bl ; Get character ; mov dl, bl ; Get character
mov dh, CHAR_ATTRIB ; the character attribute ; mov dh, CHAR_ATTRIB ; the character attribute
mov word [edi], dx ; write to video display ; mov word [edi], dx ; write to video display
;
; ; go to next location
; go to next location ; inc byte [_CurX] ; go to next character
; jmp .done ; nope, bail out
inc byte [_CurX] ; go to next character ;.Row:
jmp .done ; nope, bail out ; mov byte [_CurX], 0 ; go back to col 0
; inc byte [_CurY] ; go to next row
.Row: ;.done:
mov byte [_CurX], 0 ; go back to col 0 ; popa
inc byte [_CurY] ; go to next row ; ret
.done:
popa
ret
;**************************************************; ;**************************************************;
; Puts32 () ; Puts32 ()
@ -155,47 +156,47 @@ Putch32:
; parm\ EBX = address of string to print ; parm\ EBX = address of string to print
;**************************************************; ;**************************************************;
Puts32: ;Puts32:
pusha ; pusha
push ebx ; copy the string address ; push ebx ; copy the string address
pop edi ; pop edi
.loop: ;.loop:
;-------------------------------; ;-------------------------------;
; Get character ; ; Get character ;
;-------------------------------; ;-------------------------------;
mov bl, byte [edi] ; get next character ; mov bl, byte [edi] ; get next character
cmp bl, 0 ; is it 0 (Null terminator)? ; cmp bl, 0 ; is it 0 (Null terminator)?
je .done ; yep-bail out ; je .done ; yep-bail out
;-------------------------------; ;-------------------------------;
; Print the character ; ; Print the character ;
;-------------------------------; ;-------------------------------;
call Putch32 ; Nope-print it out ; call Putch32 ; Nope-print it out
;-------------------------------; ;-------------------------------;
; Go to next character ; ; Go to next character ;
;-------------------------------; ;-------------------------------;
inc edi ; go to next character ; inc edi ; go to next character
jmp .loop ; jmp .loop
.done: ;.done:
;-------------------------------; ;-------------------------------;
; Update hardware cursor ; ; Update hardware cursor ;
;-------------------------------; ;-------------------------------;
mov bh, byte [_CurY] ; get current position ; mov bh, byte [_CurY] ; get current position
mov bl, byte [_CurX] ; mov bl, byte [_CurX]
call MovCur ; update cursor ; call MovCur ; update cursor
popa ; restore registers, and return ; popa ; restore registers, and return
ret ; ret
;**************************************************; ;**************************************************;
; MoveCur () ; MoveCur ()
@ -206,74 +207,74 @@ Puts32:
bits 32 ;bits 32
MovCur: ;MovCur:
pusha
; pusha
;
;-------------------------------; ;-------------------------------;
; Get current position ; ; Get current position ;
;-------------------------------; ;-------------------------------;
; location = _CurX + _CurY * COLS ; location = _CurX + _CurY * COLS
xor eax, eax ; xor eax, eax
mov ecx, COLS ; mov ecx, COLS
mov al, bh ; get y pos ; mov al, bh ; get y pos
mul ecx ; multiply y*COLS ; mul ecx ; multiply y*COLS
add al, bl ; Now add x ; add al, bl ; Now add x
mov ebx, eax ; mov ebx, eax
;
;--------------------------------------; ;--------------------------------------;
; Set low byte index to VGA register ; ; Set low byte index to VGA register ;
;--------------------------------------; ;--------------------------------------;
mov al, 0x0f ; mov al, 0x0f
mov dx, 0x03D4 ; mov dx, 0x03D4
out dx, al ; out dx, al
;
mov al, bl ; mov al, bl
mov dx, 0x03D5 ; mov dx, 0x03D5
out dx, al ; low byte ; out dx, al ; low byte
;---------------------------------------; ;---------------------------------------;
; Set high byte index to VGA register ; ; Set high byte index to VGA register ;
;---------------------------------------; ;---------------------------------------;
xor eax, eax ; xor eax, eax
mov al, 0x0e ; mov al, 0x0e
mov dx, 0x03D4 ; mov dx, 0x03D4
out dx, al ; out dx, al
mov al, bh ; mov al, bh
mov dx, 0x03D5 ; mov dx, 0x03D5
out dx, al ; high byte ; out dx, al ; high byte
popa ; popa
ret ; ret
;**************************************************; ;**************************************************;
; ClrScr32 () ; ClrScr32 ()
; - Clears screen ; - Clears screen
;**************************************************; ;**************************************************;
;
bits 32 ;bits 32
;
ClrScr32: ;ClrScr32:
;
pusha ; pusha
cld ; cld
mov edi, VIDMEM ; mov edi, VIDMEM
mov cx, 2000 ; mov cx, 2000
mov ah, CHAR_ATTRIB ; mov ah, CHAR_ATTRIB
mov al, ' ' ; mov al, ' '
rep stosw ; rep stosw
mov byte [_CurX], 0 ; mov byte [_CurX], 0
mov byte [_CurY], 0 ; mov byte [_CurY], 0
popa ; popa
ret ; ret
;**************************************************; ;**************************************************;
; GotoXY () ; GotoXY ()
@ -282,14 +283,14 @@ ClrScr32:
; parm\ AH=Y position ; parm\ AH=Y position
;**************************************************; ;**************************************************;
bits 32 ;bits 32
GotoXY: ;GotoXY:
pusha ; pusha
mov [_CurX], al ; mov [_CurX], al
mov [_CurY], ah ; mov [_CurY], ah
popa ; popa
ret ; ret
%endif ;__STDIO_INC_67343546FDCC56AAB872_INCLUDED__ %endif ;__STDIO_INC_67343546FDCC56AAB872_INCLUDED__

View File

@ -1,92 +1,81 @@
/***** cmos.c ********************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
* *
* CMOS I/O Routines *
* ================= *
* *
* ! IMPORTANT NOTE ! Close interrupts before any CMOS operation *
************************************************************ cta os */
#include <system.h> #include <system.h>
#include <time.h> #include <time.h>
#include "cmos.h" #include "cmos.h"
volatile byte i86_cmos_data[128]; /*****************************************************************
* !!!!!!!!!! IMPORTANT NOTE !!!!!!!!!! *
void i86_cmos_write () * You should close interrupts before any CMOS operation. *
*****************************************************************/
inline unsigned char i86_cmos_read (unsigned char address)
{ {
byte i; outportb(0x70, address); iowait();
for (i = 0; i < 128; i++) { return inportb(0x71);
//asm volatile ("cli");
outportb(0x70, i);
iowait();
outportb(0x71, i86_cmos_data[i]);
//asm volatile ("sti");
}
} }
void i86_cmos_read () inline void i86_cmos_write (unsigned char address, unsigned char val)
{ {
byte i; outportb(0x70, address); iowait();
for (i = 0; i < 128; i++) { outportb(0x71, val);
//asm volatile ("cli"); }
outportb(0x70, i);
iowait(); void i86_cmos_write_clock (const TIME* time)
i86_cmos_data[i] = inportb(0x71); {
//asm volatile ("sti"); unsigned char BCD = ((i86_cmos_read(0x0b)&4)==0) ? 1 : 0;
} unsigned char ampm = ((i86_cmos_read(0x0b)&2)==0) ? 1 : 0;
i86_cmos_write (0, (BCD) ? (time->second%10) | (time->second/10*16) : time->second); // Seconds
i86_cmos_write (2, (BCD) ? (time->minute%10) | (time->minute/10*16) : time->minute); // Minutes
if (ampm && time->hour > 12) // Hours
i86_cmos_write (4, (BCD) ? (((time->hour - 12) % 10) | ((time->hour - 12)/10*16) | 0x80) : (time->hour | 0x80) );
else if (ampm && time->hour == 0) // Midnight convention: 12 PM = 00:00
i86_cmos_write (4, (BCD) ? 0x92 : 0x8C);
else i86_cmos_write (4, (BCD) ? (time->hour%10) | (time->hour/10*16) : time->hour); // 24h / AM
i86_cmos_write (6, (BCD) ? (time->weekday%10) | (time->weekday/10*16) : time->weekday); // Weekday
i86_cmos_write (7, (BCD) ? (time->day%10) | (time->day/10*16) : time->day); // Day
i86_cmos_write (8, (BCD) ? (time->month%10) | (time->month/10*16) : time->month); // Month
i86_cmos_write (9, (BCD) ? (time->year%10) | (time->year/10*16) : time->year); // Year
i86_cmos_write (0x32, (BCD) ? (time->century%10) | (time->century/10*16) : time->century); // Century
} }
void i86_cmos_read_clock(TIME* tim) void i86_cmos_read_clock(TIME* tim)
{ {
i86_cmos_read(); unsigned char BCD = ((i86_cmos_read(0x0b)&4)==0) ? 1 : 0;
unsigned char am_pm = ((i86_cmos_read(0x0b)&2)==0) ? 1 : 0;
if ((i86_cmos_data[0x0b]&4)==0) // BCD = true; tim->second = (BCD) ? (i86_cmos_read(0x00)%16) + 10*(i86_cmos_read(0x00)/16): i86_cmos_read(0x00);
{ tim->minute = (BCD) ? (i86_cmos_read(0x02)%16) + 10*(i86_cmos_read(0x02)/16): i86_cmos_read(0x02);
tim->seconds = (i86_cmos_data[0x00]%16) + 10*(i86_cmos_data[0x00]/16);
tim->minutes = (i86_cmos_data[0x02]%16) + 10*(i86_cmos_data[0x02]/16);
if ((i86_cmos_data[0x0b]&2)==0) { // AM/PM
if (i86_cmos_data[0x04]&80) { // pm
tim->hours = ((i86_cmos_data[0x04]-0x80)%16) + 10*((i86_cmos_data[0x04]-0x80)/16);
tim->am_pm = 1;
}
else { // am
tim->hours = (i86_cmos_data[0x04]%16) + 10*(i86_cmos_data[0x04]/16);
tim->am_pm = 0;
}
}
else { // 24 hours
tim->hours = (i86_cmos_data[0x04]%16) + 10*(i86_cmos_data[0x04]/16);
if (tim->hours > 12) {
tim->am_pm = 1;
tim->hours -= 12;
}
else tim->am_pm = 0;
}
tim->weekday = (i86_cmos_data[0x06]%16) + 10*(i86_cmos_data[0x06]/16); // Time is PM
tim->day = (i86_cmos_data[0x07]%16) + 10*(i86_cmos_data[0x07]/16); if (am_pm && i86_cmos_read(0x04)&80) {
tim->month = (i86_cmos_data[0x08]%16) + 10*(i86_cmos_data[0x08]/16); tim->hour = (BCD) ? ((i86_cmos_read(0x04)-0x80)%16) + 10*((i86_cmos_read(0x04)-0x80)/16): i86_cmos_read(0x04)-0x80;
tim->year = (i86_cmos_data[0x09]%16) + 10*(i86_cmos_data[0x09]/16); tim->hour += 12;
tim->century = (i86_cmos_data[0x32]%16) + 10*(i86_cmos_data[0x32]/16);
}
else {//BCD = false;
tim->seconds = i86_cmos_data[0x00];
tim->minutes = i86_cmos_data[0x02];
if ((i86_cmos_data[0x0b]&2)==0) { // AM/PM
if (i86_cmos_data[0x04]&80) { // pm
tim->hours = i86_cmos_data[0x04]-0x80;
tim->am_pm = 1;
}
else { // am
tim->hours = i86_cmos_data[0x04];
tim->am_pm = 0;
}
}
else { // 24 hours
tim->hours = i86_cmos_data[0x02];
if (tim->hours > 12) {
tim->am_pm = 1;
tim->hours -= 12;
}
else tim->am_pm = 0;
}
tim->weekday = i86_cmos_data[0x06];
tim->day = i86_cmos_data[0x07];
tim->month = i86_cmos_data[0x08];
tim->year = i86_cmos_data[0x09];
tim->century = i86_cmos_data[0x32];
} }
// 24Hour format, or AM
else tim->hour = (BCD) ? (i86_cmos_read(0x04)%16) + 10*(i86_cmos_read(0x04)/16): i86_cmos_read(0x04);
tim->weekday = (BCD) ? (i86_cmos_read(0x06)%16) + 10*(i86_cmos_read(0x06)/16): i86_cmos_read(0x06);
tim->day = (BCD) ? (i86_cmos_read(0x07)%16) + 10*(i86_cmos_read(0x07)/16): i86_cmos_read(0x07);
tim->month = (BCD) ? (i86_cmos_read(0x08)%16) + 10*(i86_cmos_read(0x08)/16): i86_cmos_read(0x08);
tim->year = (BCD) ? (i86_cmos_read(0x09)%16) + 10*(i86_cmos_read(0x09)/16): i86_cmos_read(0x09);
tim->century = (BCD) ? (i86_cmos_read(0x32)%16) + 10*(i86_cmos_read(0x32)/16): i86_cmos_read(0x32);
} }
unsigned char i86_cmos_read_floppy_drives ()
{
outportb (0x70, 0x10);
return inportb(0x71);
}

View File

@ -1,10 +1,17 @@
/***** cmos.h ********************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. *
* *
* CMOS I/O Routines *
* ================= *
* *
* ! IMPORTANT NOTE ! Close interrupts before any CMOS operation *
************************************************************ cta os */
#ifndef __CMOS_H #ifndef __CMOS_H
#define __CMOS_H #define __CMOS_H
extern volatile byte i86_cmos_data[128]; extern void i86_cmos_write_clock (const TIME* time);
extern void i86_cmos_write ();
extern void i86_cmos_read ();
extern void i86_cmos_read_clock (TIME *tim); extern void i86_cmos_read_clock (TIME *tim);
extern unsigned char i86_cmos_read_floppy_drives ();
#endif #endif

View File

@ -4,8 +4,6 @@
#include "../idt/idt.h" #include "../idt/idt.h"
#define cpuid(in, a, b, c, d) __asm__("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in)); #define cpuid(in, a, b, c, d) __asm__("cpuid": "=a" (a), "=b" (b), "=c" (c), "=d" (d) : "a" (in));
// initializes cpu resources // initializes cpu resources
void i86_cpu_initialize() void i86_cpu_initialize()
{ {
@ -21,15 +19,10 @@ void i86_cpu_shutdown()
char* i86_cpu_get_vender() char* i86_cpu_get_vender()
{ {
static char vender[13]; dword unused;
dword unused, arr[3]; dword static arr[3];
int i;
cpuid(0, unused, arr[0], arr[2], arr[1]); cpuid(0, unused, arr[0], arr[2], arr[1]);
for (i=0; i<12; i++) return (char*) arr;
vender[i] = (arr[i/4]>>(i%4*8)) && 0xFF;
vender[12] = 0;
return vender;
} }

View File

@ -0,0 +1,18 @@
@echo off
rem The name of the loader assembly file (without extension, must be .asm):
set loader_name=loader
rem NASM and DJGPP executable paths:
set nasm_path=C:\nasm
set djgpp_path=C:\DJGPP\bin
set objpath=../../objects
set incpath=../../include
@echo on
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/dma.o dma.c
@echo off
@echo .
@echo Done!
@pause

137
SysCore/hal/dma/dma.c Normal file
View File

@ -0,0 +1,137 @@
/***** dma.c *********************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
* *
* Direct Memory Access (DMA) Routines *
* =================================== *
* *
************************************************************ cta os */
#include "dma.h"
#include <system.h>
enum DMA0_IO {
DMA0_STATUS_REG = 0x08,
DMA0_COMMAND_REG = 0x08,
DMA0_REQUEST_REG = 0x09,
DMA0_CHANMASK_REG = 0x0a,
DMA0_MODE_REG = 0x0b,
DMA0_CLEARBYTE_FLIPFLOP_REG = 0x0c,
DMA0_TEMP_REG = 0x0d,
DMA0_MASTER_CLEAR_REG = 0x0d,
DMA0_CLEAR_MASK_REG = 0x0e,
DMA0_MASK_REG = 0x0f
};
enum DMA1_IO {
DMA1_STATUS_REG = 0xd0,
DMA1_COMMAND_REG = 0xd0,
DMA1_REQUEST_REG = 0xd2,
DMA1_CHANMASK_REG = 0xd4,
DMA1_MODE_REG = 0xd6,
DMA1_CLEARBYTE_FLIPFLOP_REG = 0xd8,
DMA1_INTER_REG = 0xda,
DMA1_UNMASK_ALL_REG = 0xdc,
DMA1_MASK_REG = 0xde
};
void i86_dma_set_address(unsigned short channel, unsigned char low, unsigned char high)
{
if (channel > 7) return; // Ignore if channel > 7
// Calculate port
unsigned short port = (channel >= 4) ? 4*(channel - 4) + 0xc0 : 2*channel;
// Set address
outportb (port, low);
outportb (port, high);
}
void i86_dma_set_count (unsigned short channel, unsigned char low, unsigned char high)
{
if (channel > 7) return; // Ignore if channel > 7
// Calculate port
unsigned short port = (channel >= 4) ? 4*(channel - 4) + 0xc2
: (2*channel) + 1;
// Set count
outportb (port, low);
outportb (port, high);
}
void i86_dma_set_external_page_registers (unsigned char channel, unsigned char val)
{
unsigned short port = 0;
switch (channel) {
case 1: port = 0x83; break;
case 2: port = 0x81; break;
case 3: port = 0x82; break;
// <- nothing should ever write to chan 4
case 5: port = 0x89; break;
case 6: port = 0x87; break;
case 7: port = 0x88; break;
default: if (channel == 4 || channel > 14) return;
}
outportb(port, val);
}
void i86_dma_mask_channel (unsigned char channel)
{
if (channel <= 4) outportb (DMA0_CHANMASK_REG, (1<< (channel -1)));
else outportb (DMA1_CHANMASK_REG, (1<< (channel -5)));
}
void i86_dma_unmask_channel (unsigned char channel)
{
if (channel <= 4) outportb (DMA0_CHANMASK_REG, channel);
else outportb (DMA1_CHANMASK_REG, channel);
}
void i86_dma_unmask_all()
{
outportb (DMA1_UNMASK_ALL_REG, 0xff);
}
void i86_dma_reset_flipflop (unsigned char dma)
{
switch (dma) {
case 0: outportb (DMA0_CLEARBYTE_FLIPFLOP_REG, 0xff);
case 1: outportb (DMA1_CLEARBYTE_FLIPFLOP_REG, 0xff);
}
}
void i86_dma_reset ()
{
outportb (DMA0_TEMP_REG, 0xff);
}
void i86_dma_set_mode(unsigned char channel, unsigned char mode)
{
unsigned char dma = (channel < 4) ? 0:1;
unsigned char chan = (dma == 0) ? channel : channel-4;
i86_dma_mask_channel (channel);
outportb ((channel < 4) ? DMA0_MODE_REG : DMA1_MODE_REG, chan | mode);
i86_dma_unmask_all ();
}
void i86_dma_set_read (unsigned char channel)
{
i86_dma_set_mode (channel, DMA_MODE_READ_TRANSFER | DMA_MODE_TRANSFER_SINGLE);
}
void i86_dma_set_write (unsigned char channel)
{
i86_dma_set_mode (channel, DMA_MODE_WRITE_TRANSFER | DMA_MODE_TRANSFER_SINGLE);
}

42
SysCore/hal/dma/dma.h Normal file
View File

@ -0,0 +1,42 @@
/***** dma.h *********************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
* *
* Direct Memory Access (DMA) Routines *
* =================================== *
* *
************************************************************ cta os */
#ifndef __DMA__H__
#define __DMA__H__
enum DMA_MODE_REG_MASK {
DMA_MODE_MASK_SEL = 3,
DMA_MODE_MASK_TRA = 0xc,
DMA_MODE_SELF_TEST = 0,
DMA_MODE_READ_TRANSFER = 4,
DMA_MODE_WRITE_TRANSFER = 8,
DMA_MODE_MASK_AUTO = 0x10,
DMA_MODE_MASK_IDEC = 0x20,
DMA_MODE_MASK = 0xc0,
DMA_MODE_TRANSFER_ON_DEMAND = 0,
DMA_MODE_TRANSFER_SINGLE = 0x40,
DMA_MODE_TRANSFER_BLOCK = 0x80,
DMA_MODE_TRANSFER_CASCADE = 0xC0
};
extern void i86_dma_set_address(unsigned short channel, unsigned char low, unsigned char high);
extern void i86_dma_set_count (unsigned short channel, unsigned char low, unsigned char high);
extern void i86_dma_set_external_page_registers (unsigned char channel, unsigned char val);
extern void i86_dma_mask_channel (unsigned char channel);
extern void i86_dma_unmask_channel (unsigned char channel);
extern void i86_dma_unmask_all();
extern void i86_dma_reset_flipflop (unsigned char dma);
extern void i86_dma_reset ();
extern void i86_dma_set_mode(unsigned char channel, unsigned char mode);
extern void i86_dma_set_read (unsigned char channel);
extern void i86_dma_set_write (unsigned char channel);
#endif

View File

@ -0,0 +1,18 @@
@echo off
rem The name of the loader assembly file (without extension, must be .asm):
set loader_name=loader
rem NASM and DJGPP executable paths:
set nasm_path=C:\nasm
set djgpp_path=C:\DJGPP\bin
set objpath=../../objects
set incpath=../../include
@echo on
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/floppy.o floppy.c
@echo off
@echo .
@echo Done!
@pause

356
SysCore/hal/floppy/floppy.c Normal file
View File

@ -0,0 +1,356 @@
/***** floppy.c ******************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
* *
* Floppy Drive I/O Routines *
* ========================= *
* *
************************************************************ cta os */
#include "../irq/irq.h"
#include "../dma/dma.h"
#include <system.h>
#include <conio.h>
// Used ports:
// ***********
// Digital Output Register (DOR): 0x3f2
// Main Status Register (MSR): 0x3f4
// Data Register (FIFO): 0x3f5
// Configuration Control Register (CTRL): 0x3f7
unsigned char floppy_drives_installed;
volatile unsigned char i86_floppy_new_interrupt;
struct {
unsigned char type;
unsigned char data_rate;
unsigned char step_rate_time;
unsigned char head_load_time;
unsigned char head_unload_time;
unsigned char sectors_per_track;
} fd[2];
// Initialize DMA
unsigned char i86_floppy_initialize_dma(unsigned char* buffer, unsigned length)
{
union { unsigned char byt[4]; // Low[0], Mid[1], Hi[2]
unsigned long l;
} a, c;
a.l = (unsigned) buffer;
c.l = (unsigned) length-1;
// Check for buffer issues
if ((a.l >> 24) || (c.l >> 16) || (((a.l & 0xffff)+c.l) >> 16)) return 0;
i86_dma_reset();
i86_dma_mask_channel(2); // Mask channel 2
i86_dma_reset_flipflop(1); // FlipFlop reset on DMA1
i86_dma_set_address(2, a.byt[0], a.byt[1]); // Buffer address
i86_dma_reset_flipflop(1); // FlipFlop reset on DMA2
i86_dma_set_count(2, c.byt[0], c.byt[1]); // Set count
i86_dma_set_read(2);
i86_dma_unmask_all();
return 1;
}
inline void i86_floppy_disable_controller() {
outportb (0x3F2, 0);
}
inline void i86_floppy_enable_controller() {
outportb (0x3F2, 4 | 8);
}
inline unsigned char i86_floppy_send_command (unsigned char command)
{
int i;
for (i = 0; i < 750; i++)
if (inportb(0x3F4) & 128) {
outportb(0x3F5, command); return 1;
}
return 0;
}
inline unsigned char i86_floppy_read_data ()
{
int i;
for (i = 0; i < 750; i++)
if (inportb(0x3F4) & 0x80)
return inportb(0x3F5);
return 0;
}
inline void i86_floppy_check_int(unsigned* st0, unsigned* cyl)
{
i86_floppy_send_command(0x8);
*st0 = i86_floppy_read_data();
*cyl = i86_floppy_read_data();
}
extern unsigned i86_pit_get_tick_count();
extern unsigned i86_pit_get_frequency();
inline unsigned char i86_floppy_wait()
{
unsigned temp = i86_pit_get_tick_count();
unsigned freq = i86_pit_get_frequency();
while (i86_floppy_new_interrupt==0)
if (temp + (3*freq) == i86_pit_get_frequency()) return 0; // timeout
i86_floppy_new_interrupt = 0;
return 1;
}
void i86_floppy_motor (unsigned char drive, unsigned char on)
{
if (drive >= floppy_drives_installed) return;
// Read DOR register
unsigned char dor = inportb(0x3F2);
// Un/set selected drive motor
if (on) dor |= drive << 4;
else dor &= ~(drive << 4);
// Write DOR
outportb (0x3F2, dor);
// Wait a fifth of a second for motor to turn on
unsigned temp = i86_pit_get_tick_count();
unsigned freq = i86_pit_get_frequency();
while (temp + (freq/5) > i86_pit_get_tick_count());
}
void i86_floppy_handler(ISR_stack_regs *r)
{
i86_floppy_new_interrupt = 1;
}
void i86_floppy_drive_data (unsigned char drv, unsigned char dma)
{
unsigned data = 0;
if (drv >= floppy_drives_installed) return;
outportb(0x3F7, fd[drv].data_rate);
i86_floppy_send_command (0x3);
data = ((fd[drv].step_rate_time & 0xf) << 4) | (fd[drv].head_unload_time & 0xf);
i86_floppy_send_command (data);
data = (fd[drv].head_load_time <<1 ) | (dma) ? 1 : 0;
i86_floppy_send_command (data);
}
inline void i86_floppy_select(unsigned char drive)
{
if (drive >= floppy_drives_installed) return;
// Send mechanical drive data
i86_floppy_drive_data(drive, 1);
// Select drive in DOR register
outportb (0x3F2, 4 | 8 | drive);
}
unsigned char i86_floppy_calibrate(unsigned drive)
{
unsigned st0, cyl;
if (drive >= floppy_drives_installed) return 0;
i86_floppy_motor (drive, 1);
int i;
for (i = 0; i < 15; i++) {
i86_floppy_new_interrupt = 0;
i86_floppy_send_command(0x7);
i86_floppy_send_command(drive);
i86_floppy_wait();
i86_floppy_check_int(&st0, &cyl);
if (!cyl) {
i86_floppy_motor(drive, 0);
return 1;
}
}
i86_floppy_motor(drive, 0);
return 0;
}
void i86_floppy_reset()
{
unsigned st0, cyl;
i86_floppy_new_interrupt = 0;
i86_floppy_disable_controller();
i86_floppy_enable_controller();
i86_floppy_wait();
int i;
for (i = 0; i < 4; i++)
i86_floppy_check_int(&st0, &cyl);
unsigned char drive;
for (drive = 0; drive < floppy_drives_installed; drive++) {
i86_floppy_drive_data(drive, 1);
i86_floppy_calibrate(drive);
}
}
void i86_floppy_read_sector_imp (unsigned* where, unsigned char drive, unsigned char head, unsigned char track, unsigned char sector)
{
unsigned st0, cyl;
i86_floppy_select (drive);
i86_floppy_initialize_dma((unsigned char*) where, 512);
i86_dma_set_read(2);
i86_floppy_new_interrupt = 0;
i86_floppy_send_command(0x06 | 0x80 | 0x40 );
i86_floppy_send_command(head<<2 | drive);
i86_floppy_send_command(track);
i86_floppy_send_command(head);
i86_floppy_send_command(sector);
i86_floppy_send_command(0x02);
i86_floppy_send_command( ((sector+1) >= fd[drive].sectors_per_track) ? fd[drive].sectors_per_track : sector+1);
i86_floppy_send_command(0x1b);
i86_floppy_send_command(0xff);
i86_floppy_wait();
int i;
for (i = 0; i < 7; i++) i86_floppy_read_data();
i86_floppy_check_int (&st0, &cyl);
}
unsigned char i86_floppy_seek (unsigned drive, unsigned cyl, unsigned head)
{
unsigned st0, cyl0;
if (drive >= floppy_drives_installed) return 0;
i86_floppy_select (drive);
int i;
for (i = 0; i < 20; i++) {
i86_floppy_new_interrupt = 0;
i86_floppy_send_command (0xF);
i86_floppy_send_command ( (head) << 2 | drive);
i86_floppy_send_command (cyl);
i86_floppy_wait();
i86_floppy_check_int(&st0, &cyl0);
if (cyl0 == cyl) return 1;
}
return 0;
}
inline void i86_floppy_lba_to_chs (int lba, unsigned char drive, unsigned char *head, unsigned char *track, unsigned char *sectors)
{
*head = (lba % (fd[drive].sectors_per_track * 2)) / fd[drive].sectors_per_track;
*track = lba / (fd[drive].sectors_per_track * 2);
*sectors = lba % fd[drive].sectors_per_track + 1;
}
extern unsigned char i86_cmos_read_floppy_drives();
const char* types[] = {
"Nonexistant", "5.25\", unsupported.", "5.25\", unsupported.",
"3.5\", 720kb", "3.5\", 1.44mb", "3.5\", 2.88 mb"};
void i86_floppy_install()
{
unsigned char temp = i86_cmos_read_floppy_drives();
int i;
// Set fd0 and fd1 types
fd[1].type = temp & 0xf;
fd[0].type = temp >> 4;
// SRT = 16 - (ms * datarate / 500000);
// HLT = ms * datarate / 1000000
// HUT = ms * datarate / 8000000
// Set up
for (i = 0; i < 2; i++) {
if (fd[i].type >= 3) floppy_drives_installed++; // 5.25" drives unsupported
if (fd[i].type == 3) { // 720 kb, DD
fd[i].data_rate = 2; // speed = 250 kbps
fd[i].step_rate_time = 12; // 16 - (ms * 250000 / 500000), ms = 8
fd[i].head_load_time = 7;
fd[i].head_unload_time = 7;
fd[i].sectors_per_track = 9;
}
else if (fd[i].type == 4) { // 1.44 MB, HD
fd[i].data_rate = 0; // speed = 500 kbps
fd[i].step_rate_time = 8;
fd[i].head_load_time = 15;
fd[i].head_unload_time = 15;
fd[i].sectors_per_track = 18;
}
else if (fd[i].type == 5) { // 2.88 MB, ED
fd[i].data_rate = 3; // speed = 1000 kbps;
fd[i].step_rate_time = 0;
fd[i].head_load_time = 30;
fd[i].head_unload_time = 30;
fd[i].sectors_per_track = 36;
}
}
if (floppy_drives_installed == 0) return; // No drives to set
// Install handler
i86_irq_install_handler(6, i86_floppy_handler);
i86_floppy_reset();
}
unsigned char i86_floppy_driver_enabled()
{
return (floppy_drives_installed>0);
}
unsigned* i86_read_sector (unsigned* where, unsigned char drive, int sectorLBA)
{
if (drive >= floppy_drives_installed) return 0;
if ((unsigned)(where) > (0xFFFF - 513)) return 0;
// convert lba to chs
unsigned head, track, sector;
i86_floppy_lba_to_chs(sectorLBA, drive, (unsigned char*)&head, (unsigned char*)&track, (unsigned char*) &sector);
// start motor
i86_floppy_motor(drive, 1);
if (!i86_floppy_seek(drive, track, head)) return 0;
i86_floppy_read_sector_imp(where, drive, head, track, sector);
i86_floppy_motor(drive, 0);
return (unsigned*)where;
}

View File

@ -0,0 +1,19 @@
/***** floppy.h ******************************************************
* (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
* *
* Floppy Drive I/O Routines *
* ========================= *
* *
************************************************************ cta os */
extern unsigned char i86_floppy_initialize_dma(unsigned char* buffer, unsigned length);
extern void i86_floppy_motor (unsigned char drive, unsigned char on);
extern void i86_floppy_handler(ISR_stack_regs *r);
extern void i86_floppy_drive_data (unsigned char drv, unsigned char dma);
extern unsigned char i86_floppy_calibrate(unsigned drive);
extern void i86_floppy_reset();
extern void i86_floppy_read_sector_imp (unsigned* where, unsigned char drive, unsigned char head, unsigned char track, unsigned char sector);
extern unsigned char i86_floppy_seek (unsigned drive, unsigned cyl, unsigned head);
extern void i86_floppy_install();
extern unsigned char i86_floppy_driver_enabled();
extern unsigned* i86_read_sector (unsigned* where, unsigned char drive, int sectorLBA);

View File

@ -2,7 +2,7 @@
* gdt.c - GLOBAL DESCRIPTOR TABLE * * gdt.c - GLOBAL DESCRIPTOR TABLE *
* Contains function prototypes for setting up the GDT * * Contains function prototypes for setting up the GDT *
******************************************************************/ ******************************************************************/
#define MAX_DESCRIPTORS 3 #define MAX_DESCRIPTORS 5
#include "gdt.h" #include "gdt.h"
/* Our GDT, with 3 entries, and finally our special GDT pointer */ /* Our GDT, with 3 entries, and finally our special GDT pointer */
@ -59,6 +59,12 @@ void i86_gdt_install()
* this entry's access byte says it's a Data Segment */ * this entry's access byte says it's a Data Segment */
i86_gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); i86_gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
/* User mode Code segment*/
i86_gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
/* User mode data segment*/
i86_gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
/* Flush out the old GDT and install the new changes! */ /* Flush out the old GDT and install the new changes! */
i86_gdt_flush(); i86_gdt_flush();
} }

View File

@ -10,24 +10,26 @@
#include "irq/irq.h" #include "irq/irq.h"
#include "isrs/isrs.h" #include "isrs/isrs.h"
#include "keyboard/keyus.h" #include "keyboard/keyus.h"
#include "floppy/floppy.h"
// initialize hardware devices // initialize hardware devices
void i86_hal_initialize () { void i86_hal_initialize () {
// initialize motherboard controllers and system timer // initialize motherboard controllers and system timer
i86_cpu_initialize (); // (install GDT, IDT) i86_cpu_initialize (); // (install GDT, IDT)
i86_isrs_install(); // (install ISR handler) i86_isrs_install(); // (install ISR handler)
i86_irq_install(); // (install IRQ handler) i86_irq_install(); // (install IRQ handler)
// install PIT and system clock; pit at 100 Hz // install PIT and system clock; pit at 100 Hz
i86_kb_install_partone(); i86_kb_install_partone();
i86_cmos_read_clock((TIME*)&_internal_clock); i86_cmos_read_clock((TIME*)&_internal_clock);
i86_pit_install (100); i86_pit_install (100);
i86_kb_install_parttwo(); i86_kb_install_parttwo();
// enable interrupts
i86_start_interrupts();
// enable interrupts i86_floppy_install();
i86_start_interrupts();
} }
// shutdown hardware devices // shutdown hardware devices
@ -46,58 +48,14 @@ void reboot()
__asm__ __volatile__ ("hlt"); __asm__ __volatile__ ("hlt");
} }
//! notifies hal interrupt is done
/*inline void interruptdone (unsigned int intno) {
//! insure its a valid hardware irq
if (intno > 16)
return;
//! test if we need to send end-of-interrupt to second pic
if (intno >= 8)
i86_pic_send_command (0x20, 0xA1);
//! always send end-of-interrupt to primary pic
i86_pic_send_command (0x20, 0x21);
}
*/
//! output sound to speaker //! output sound to speaker
void sound (unsigned frequency) { void sound (unsigned frequency) {
//! sets frequency for speaker. frequency of 0 disables speaker //! sets frequency for speaker. frequency of 0 disables speaker
outportb (0x61, 3 | (byte)(frequency<<2) ); outportb (0x61, 3 | (unsigned char)(frequency<<2) );
} }
//! sets new interrupt vector
/*void _cdecl setvect (int intno, void (_cdecl far &vect) ( ) ) {
//! install interrupt handler! This overwrites prev interrupt descriptor
i86_install_ir (intno, I86_IDT_DESC_PRESENT | I86_IDT_DESC_BIT32,
0x8, vect);
}
//! returns current interrupt vector
void (_cdecl far * _cdecl getvect (int intno)) ( ) {
//! get the descriptor from the idt
idt_descriptor* desc = i86_get_ir (intno);
if (!desc)
return 0;
//! get address of interrupt handler
uint32_t addr = desc->baseLo | (desc->baseHi << 16);
//! return interrupt handler
I86_IRQ_HANDLER irq = (I86_IRQ_HANDLER)addr;
return irq;
}
*/
//! returns cpu vender //! returns cpu vender
const char* get_cpu_vender () { const char* get_cpu_vender () {
@ -108,19 +66,18 @@ const char* get_cpu_vender () {
/*************************************************************************************** /***************************************************************************************
* Keyboard Routines * * Keyboard Routines *
***************************************************************************************/ ***************************************************************************************/
char getch() /*char getch()
{ {
kb_key alpha = getkey(); -> moved in conio library
return alpha.character; }*/
}
char scancode_to_ascii(byte scancode, byte status) char scancode_to_ascii(unsigned char scancode, unsigned char status)
{ {
if ((status&1) || (status&2)) return kbdus_map_shift[scancode]; if ((status&1) || (status&2)) return kbdus_map_shift[scancode];
else return kbdus_map[scancode]; else return kbdus_map[scancode];
} }
byte get_key_status (byte scancode) unsigned char get_key_status (unsigned char scancode)
{ {
if (scancode&0xF0) return kb_lights_status&0x0F; if (scancode&0xF0) return kb_lights_status&0x0F;
else if (scancode&0x80) return kb_modifier_status&0x7F; else if (scancode&0x80) return kb_modifier_status&0x7F;
@ -153,7 +110,7 @@ void kb_set_repeat(float rate, int delay){
3.7, 3.3, 3.0, 2.7, 2.5, 2.3, 2.1, 2.0} ; 3.7, 3.3, 3.0, 2.7, 2.5, 2.3, 2.1, 2.0} ;
byte r,d; unsigned char r,d;
for (r = 0; rate != rates[r] && r < 32; r++) for (r = 0; rate != rates[r] && r < 32; r++)
if (rate==32) return; if (rate==32) return;
@ -179,6 +136,6 @@ void kb_set_repeat(float rate, int delay){
|(reserved) | lock | lock | lock | |(reserved) | lock | lock | lock |
+-----------+-------+-------+--------+ +-----------+-------+-------+--------+
***************************************/ ***************************************/
void kb_set_LEDs(byte status) { void kb_set_LEDs(unsigned char status) {
i86_kb_set_LEDs(status); i86_kb_set_LEDs(status);
} }

View File

@ -1,6 +1,8 @@
#ifndef __IRQ_H #ifndef __IRQ_H
#define __IRQ_H #define __IRQ_H
#include <regs.h>
/* These are own ISRs that point to our special IRQ handler /* These are own ISRs that point to our special IRQ handler
* instead of the regular 'fault_handler' function */ * instead of the regular 'fault_handler' function */

View File

@ -36,10 +36,10 @@ char *exception_messages[] = {
}; };
void _STOP_ERROR_SCREEN (ISR_stack_regs *r) //void _STOP_ERROR_SCREEN (ISR_stack_regs *r)
{ //{
set_default_colors (0x01, 0x0F); clrscr(); //set_default_colors (0x01, 0x0F); clrscr();
puts (" Blue Screen Of Death\n"); /*puts_pos (0, 0, " Blue Screen Of Death\n");
int i; for (i = 79; i>=0; i--) putc('='); int i; for (i = 79; i>=0; i--) putc('=');
puts_pos_font (15, 2, "A fatal error has occured, CTA OS has been halted.", 0x01, 0x0C); puts_pos_font (15, 2, "A fatal error has occured, CTA OS has been halted.", 0x01, 0x0C);
puts_pos_font (10, 4, "gs", 0x01, 0x0B); put_hex_pos(15, 4, r->gs); puts_pos_font (10, 4, "gs", 0x01, 0x0B); put_hex_pos(15, 4, r->gs);
@ -67,8 +67,8 @@ void _STOP_ERROR_SCREEN (ISR_stack_regs *r)
puts_pos_font (10, 20, "useresp", 0x01, 0x0B); put_hex_pos(17, 20, r->useresp); puts_pos_font (10, 20, "useresp", 0x01, 0x0B); put_hex_pos(17, 20, r->useresp);
puts_pos_font (10, 21, "ss", 0x01, 0x0B); put_hex_pos(17, 21, r->ss); puts_pos_font (10, 21, "ss", 0x01, 0x0B); put_hex_pos(17, 21, r->ss);
puts_pos_font (29, 24, "!!! System Halted !!!", 0x01, 0x0C); puts_pos_font (29, 24, "!!! System Halted !!!", 0x01, 0x0C);*/
} //}
/*void _STOP_ERROR_SCREEN (ISR_stack_regs *r) /*void _STOP_ERROR_SCREEN (ISR_stack_regs *r)
{ {
@ -76,4 +76,9 @@ void _STOP_ERROR_SCREEN (ISR_stack_regs *r)
puts_pos(50, 24, exception_messages[r->int_no]); puts_pos(50, 24, exception_messages[r->int_no]);
puts_pos_font (5, 24, "!!! System Halted !!!", 0x01, 0x0C); puts_pos_font (5, 24, "!!! System Halted !!!", 0x01, 0x0C);
}*/ }*/
void _STOP_ERROR_SCREEN (ISR_stack_regs *r)
{
cprintf ("%#0C** Fatal Error: "); cprintf("%#0E %s \n\r", exception_messages[r->int_no]);
}

View File

@ -6,128 +6,130 @@
#define KB_KEY_LCTRL 0x90 // 1001 0000 #define KB_KEY_LCTRL 0x90 // 1001 0000
#define KB_KEY_RCTRL 0xA0 // 1010 0000 #define KB_KEY_RCTRL 0xA0 // 1010 0000
#define KB_KEY_FSHIFT 0xC0 // 1100 0000 #define KB_KEY_FSHIFT 0xC0 // 1100 0000
extern volatile byte kb_modifier_status; extern volatile unsigned char kb_modifier_status;
#define KB_PREFIX_GRAY 0x01 // Gray #define KB_PREFIX_GRAY 0x01 // Gray
#define KB_PREFIX_BREAK 0x02 // Break code #define KB_PREFIX_BREAK 0x02 // Break code
#define KB_PREFIX_PAUSE 0x04 // Pause/break key #define KB_PREFIX_PAUSE 0x04 // Pause/break key
#define KB_PREFIX_PAUSE1 0x08 // Recieved first byte from pause/break #define KB_PREFIX_PAUSE1 0x08 // Recieved first unsigned char from pause/break
extern volatile byte kb_prefix; extern volatile unsigned char kb_prefix;
#define KB_KEY_SCROLL 0xF1 // 1111 0001 #define KB_KEY_SCROLL 0xF1 // 1111 0001
#define KB_KEY_NUM 0xF2 // 1111 0010 #define KB_KEY_NUM 0xF2 // 1111 0010
#define KB_KEY_CAPS 0xF4 // 1111 0100 #define KB_KEY_CAPS 0xF4 // 1111 0100
extern volatile byte kb_lights_status; extern volatile unsigned char kb_lights_status;
extern byte kb_scancode_set; extern unsigned char kb_scancode_set;
#define KB_KEY_PAUSE 0x00
#define KB_KEY_F9 0x01
#define KB_KEY_F7 0x02
#define KB_KEY_F5 0X03
#define KB_KEY_F3 0x04
#define KB_KEY_F1 0x05
#define KB_KEY_F2 0x06
#define KB_KEY_F12 0x07
#define KB_KEY_PRINTSCRN 0x08
#define KB_KEY_F10 0x09
#define KB_KEY_F8 0x0A
#define KB_KEY_F6 0x0B
#define KB_KEY_F4 0x0C
#define KB_KEY_TAB 0x0D
#define KB_KEY_TILDA 0x0E
#define KB_KEY_Q 0x15
#define KB_KEY_1 0x16
#define KB_KEY_Z 0x1A
#define KB_KEY_S 0x1B
#define KB_KEY_A 0x1C
#define KB_KEY_W 0x1D
#define KB_KEY_2 0x1E
#define KB_KEY_LWIN 0x1F
#define KB_KEY_C 0x21
#define KB_KEY_X 0x22
#define KB_KEY_D 0x23
#define KB_KEY_E 0x24
#define KB_KEY_4 0x25
#define KB_KEY_3 0x26
#define KB_KEY_RWIN 0x27
#define KB_KEY_SPACE 0x29
#define KB_KEY_V 0x2A
#define KB_KEY_F 0x2B
#define KB_KEY_T 0x2C
#define KB_KEY_R 0x2D
#define KB_KEY_5 0x2E
#define KB_KEY_MENU 0x2F
#define KB_KEY_N 0x31
#define KB_KEY_B 0x32
#define KB_KEY_H 0x33
#define KB_KEY_G 0x34
#define KB_KEY_Y 0x35
#define KB_KEY_6 0x36
#define KB_KEY_M 0x3A
#define KB_KEY_J 0x3B
#define KB_KEY_U 0x3C
#define KB_KEY_7 0x3D
#define KB_KEY_8 0x3E
#define KB_KEY_COMMA 0x41
#define KB_KEY_K 0x42
#define KB_KEY_I 0x43
#define KB_KEY_O 0x44
#define KB_KEY_0 0x45
#define KB_KEY_9 0x46
#define KB_KEY_PERIOD 0x49
#define KB_KEY_SLASH 0x4A
#define KB_KEY_L 0x4B
#define KB_KEY_SEMICOLON 0x4C
#define KB_KEY_P 0x4D
#define KB_KEY_DASH 0x4E
#define KB_KEY_APOSTROPHE 0x52
#define KB_KEY_LBRACKET 0x54
#define KB_KEY_EQUAL 0x55
#define KB_KEY_NUMPAD_ENTER 0x59
#define KB_KEY_ENTER 0x5A
#define KB_KEY_RBRACKET 0x5B
#define KB_KEY_BACKSLASH 0x5D
#define KB_KEY_END 0x5E
#define KB_KEY_LEFT 0x5F
#define KB_KEY_HOME 0x60
#define KB_KEY_INSERT 0x61
#define KB_KEY_DELETE 0x62
#define KB_KEY_DOWN 0x63
#define KB_KEY_RIGHT 0x64
#define KB_KEY_UP 0x65
#define KB_KEY_BACKSPACE 0x66
#define KB_KEY_PGDOWN 0x67
#define KB_KEY_PGUP 0x68
#define KB_KEY_NUMPAD_1 0x69
#define KB_KEY_NUMPAD_SLASH 0x6A
#define KB_KEY_NUMPAD_4 0x6B
#define KB_KEY_NUMPAD_7 0x6C
#define KB_KEY_NUMPAD_0 0x70
#define KB_KEY_NUMPAD_COLON 0x71
#define KB_KEY_NUMPAD_2 0x72
#define KB_KEY_NUMPAD_5 0x73
#define KB_KEY_NUMPAD_6 0x74
#define KB_KEY_NUMPAD_8 0x75
#define KB_KEY_ESC 0x76
#define KB_KEY_F11 0x78
#define KB_KEY_NUMPAD_PLUS 0x79
#define KB_KEY_NUMPAD_3 0x7A
#define KB_KEY_NUMPAD_MINUS 0x7B
#define KB_KEY_NUMPAD_ASTERISK 0x7C
#define KB_KEY_NUMPAD_9 0x7D
enum KB_KEYS {
KB_KEY_PAUSE = 0x00
KB_KEY_F9 = 0x01
KB_KEY_F7 = 0x02
KB_KEY_F5 = 0X03
KB_KEY_F3 = 0x04
KB_KEY_F1 = 0x05
KB_KEY_F2 = 0x06
KB_KEY_F12 = 0x07
KB_KEY_PRINTSCRN = 0x08
KB_KEY_F10 = 0x09
KB_KEY_F8 = 0x0A
KB_KEY_F6 = 0x0B
KB_KEY_F4 = 0x0C
KB_KEY_TAB = 0x0D
KB_KEY_TILDA = 0x0E
KB_KEY_Q = 0x15
KB_KEY_1 = 0x16
KB_KEY_Z = 0x1A
KB_KEY_S = 0x1B
KB_KEY_A = 0x1C
KB_KEY_W = 0x1D
KB_KEY_2 = 0x1E
KB_KEY_LWIN = 0x1F
KB_KEY_C = 0x21
KB_KEY_X = 0x22
KB_KEY_D = 0x23
KB_KEY_E = 0x24
KB_KEY_4 = 0x25
KB_KEY_3 = 0x26
KB_KEY_RWIN = 0x27
KB_KEY_SPACE = 0x29
KB_KEY_V = 0x2A
KB_KEY_F = 0x2B
KB_KEY_T = 0x2C
KB_KEY_R = 0x2D
KB_KEY_5 = 0x2E
KB_KEY_MENU = 0x2F
KB_KEY_N = 0x31
KB_KEY_B = 0x32
KB_KEY_H = 0x33
KB_KEY_G = 0x34
KB_KEY_Y = 0x35
KB_KEY_6 = 0x36
KB_KEY_M = 0x3A
KB_KEY_J = 0x3B
KB_KEY_U = 0x3C
KB_KEY_7 = 0x3D
KB_KEY_8 = 0x3E
KB_KEY_COMMA = 0x41
KB_KEY_K = 0x42
KB_KEY_I = 0x43
KB_KEY_O = 0x44
KB_KEY_0 = 0x45
KB_KEY_9 = 0x46
KB_KEY_PERIOD = 0x49
KB_KEY_SLASH = 0x4A
KB_KEY_L = 0x4B
KB_KEY_SEMICOLON = 0x4C
KB_KEY_P = 0x4D
KB_KEY_DASH = 0x4E
KB_KEY_APOSTROPHE = 0x52
KB_KEY_LBRACKET = 0x54
KB_KEY_EQUAL = 0x55
KB_KEY_NUMPAD_ENTER = 0x59
KB_KEY_ENTER = 0x5A
KB_KEY_RBRACKET = 0x5B
KB_KEY_BACKSLASH = 0x5D
KB_KEY_END = 0x5E
KB_KEY_LEFT = 0x5F
KB_KEY_HOME = 0x60
KB_KEY_INSERT = 0x61
KB_KEY_DELETE = 0x62
KB_KEY_DOWN = 0x63
KB_KEY_RIGHT = 0x64
KB_KEY_UP = 0x65
KB_KEY_BACKSPACE = 0x66
KB_KEY_PGDOWN = 0x67
KB_KEY_PGUP = 0x68
KB_KEY_NUMPAD_1 = 0x69
KB_KEY_NUMPAD_SLASH = 0x6A
KB_KEY_NUMPAD_4 = 0x6B
KB_KEY_NUMPAD_7 = 0x6C
KB_KEY_NUMPAD_0 = 0x70
KB_KEY_NUMPAD_COLON = 0x71
KB_KEY_NUMPAD_2 = 0x72
KB_KEY_NUMPAD_5 = 0x73
KB_KEY_NUMPAD_6 = 0x74
KB_KEY_NUMPAD_8 = 0x75
KB_KEY_ESC = 0x76
KB_KEY_F11 = 0x78
KB_KEY_NUMPAD_PLUS = 0x79
KB_KEY_NUMPAD_3 = 0x7A
KB_KEY_NUMPAD_MINUS = 0x7B
KB_KEY_NUMPAD_ASTERISK = 0x7C
KB_KEY_NUMPAD_9 = 0x7D
};
typedef struct { typedef struct {
byte status; unsigned char status;
byte lights; unsigned char lights;
byte scancode; unsigned char scancode;
byte character; unsigned char character;
} kb_key; } kb_key;
extern char getch(); extern char getch();
extern kb_key get_key(); extern kb_key get_key();
extern scancode_to_ascii(byte scancode); extern scancode_to_ascii(unsigned char scancode);
extern byte get_key_status(byte scancode); extern unsigned char get_key_status(unsigned char scancode);
extern void kb_set_repeat(float rate, int delay); extern void kb_set_repeat(float rate, int delay);
extern void kb_set_LEDs(byte status); extern void kb_set_LEDs(unsigned char status);

View File

@ -28,18 +28,18 @@ const char kbdus_map_shift[] = {
}; };
volatile byte kb_array[16]; volatile unsigned char kb_array[16];
volatile byte kb_newdata; volatile unsigned char kb_newdata;
volatile byte kb_modifier_status; volatile unsigned char kb_modifier_status;
volatile byte kb_prefix; volatile unsigned char kb_prefix;
volatile byte kb_lights_status; volatile unsigned char kb_lights_status;
byte kb_scancode_set; unsigned char kb_scancode_set;
void i86_kb_set_key(byte scancode, byte val) void i86_kb_set_key(unsigned char scancode, unsigned char val)
{ {
byte pos = scancode/8; unsigned char pos = scancode/8;
byte offset = scancode%8; unsigned char offset = scancode%8;
if (val) { if (val) {
kb_array[pos] |= 1<<offset; kb_array[pos] |= 1<<offset;
@ -48,16 +48,16 @@ void i86_kb_set_key(byte scancode, byte val)
else kb_array[pos] &= 0xFF - (1<<offset); else kb_array[pos] &= 0xFF - (1<<offset);
} }
byte i86_kb_get_key(byte scancode) unsigned char i86_kb_get_key(unsigned char scancode)
{ {
byte pos = scancode/8; unsigned char pos = scancode/8;
byte offset = scancode%8; unsigned char offset = scancode%8;
return (kb_array[pos]&(1<<offset)); return (kb_array[pos]&(1<<offset));
} }
void i86_kb_handler(ISR_stack_regs *r) { void i86_kb_handler(ISR_stack_regs *r) {
byte scancode = inportb(0x60); unsigned char scancode = inportb(0x60);
switch (scancode) { switch (scancode) {
case 0x00: // Error 0x00 case 0x00: // Error 0x00
@ -165,7 +165,6 @@ void i86_kb_handler(ISR_stack_regs *r) {
if (ok==2) reboot(); if (ok==2) reboot();
} }
outportb(0x20, 0x20);
} }
@ -210,11 +209,11 @@ kb_key getkey()
250 | 500 | 750 | 1000 250 | 500 | 750 | 1000
***************************************/ ***************************************/
void i86_kb_set_repeat(byte rate, byte delay) void i86_kb_set_repeat(unsigned char rate, unsigned char delay)
{ {
if (rate>3 || delay>31) return; if (rate>3 || delay>31) return;
byte out = rate<<5 | delay; unsigned char out = rate<<5 | delay;
while ((inportb(0x64)&2) != 0); while ((inportb(0x64)&2) != 0);
outportb(0x60, 0xF3); outportb(0x60, 0xF3);
while ((inportb(0x64)&2) != 0); while ((inportb(0x64)&2) != 0);
@ -231,7 +230,7 @@ void i86_kb_set_repeat(byte rate, byte delay)
|(reserved) | lock | lock | lock | |(reserved) | lock | lock | lock |
+-----------+-------+-------+--------+ +-----------+-------+-------+--------+
***************************************/ ***************************************/
void i86_kb_set_LEDs(byte status) void i86_kb_set_LEDs(unsigned char status)
{ {
while ((inportb (0x64)&2)!=0); while ((inportb (0x64)&2)!=0);
outportb (0x60, 0xED); outportb (0x60, 0xED);
@ -248,7 +247,7 @@ void i86_kb_set_LEDs(byte status)
2 Set to scancode set 2 2 Set to scancode set 2
3 Set to scancode set 3 3 Set to scancode set 3
***************************************/ ***************************************/
void i86_kb_set_scancodeset(byte set) void i86_kb_set_scancodeset(unsigned char set)
{ {
if (set>3) return; if (set>3) return;
@ -261,7 +260,7 @@ void i86_kb_set_scancodeset(byte set)
kb_scancode_set = set; kb_scancode_set = set;
} }
byte i86_kb_get_scancodeset() { unsigned char i86_kb_get_scancodeset() {
return kb_scancode_set; return kb_scancode_set;
} }
@ -298,7 +297,7 @@ int i86_kb_install_parttwo()
int ret = 0; int ret = 0;
// Wait for BAT test results // Wait for BAT test results
byte temp; unsigned char temp;
do temp = inportb(0x60); do temp = inportb(0x60);
while (temp!=0xAA && temp!=0xFC); while (temp!=0xAA && temp!=0xFC);
@ -311,13 +310,13 @@ int i86_kb_install_parttwo()
i86_kb_set_scancodeset(2); // Set new scancode set i86_kb_set_scancodeset(2); // Set new scancode set
i86_kb_waitin(); i86_kb_waitin();
outportb(0x64, 0x20); // Get "Command byte" outportb(0x64, 0x20); // Get "Command unsigned char"
do { temp = inportb(0x60); do { temp = inportb(0x60);
} while (temp==0xFA || temp==0xAA); } while (temp==0xFA || temp==0xAA);
temp &= 0xFF - (1<<6); // Set bit6 to 0: disable conversion temp &= 0xFF - (1<<6); // Set bit6 to 0: disable conversion
i86_kb_waitin(); outportb(0x64, 0x60); // Function to write cmd byte i86_kb_waitin(); outportb(0x64, 0x60); // Function to write cmd unsigned char
i86_kb_waitin(); outportb(0x60, temp); // Send it i86_kb_waitin(); outportb(0x60, temp); // Send it
memset((void*)kb_array, 0, 16); memset((void*)kb_array, 0, 16);

View File

@ -4,22 +4,22 @@
extern const char kbdus_map[0x80]; extern const char kbdus_map[0x80];
extern const char kbdus_map_shift[0x80]; extern const char kbdus_map_shift[0x80];
extern volatile byte kb_modifier_status; extern volatile unsigned char kb_modifier_status;
extern volatile byte kb_prefix; extern volatile unsigned char kb_prefix;
extern volatile byte kb_lights_status; extern volatile unsigned char kb_lights_status;
extern byte kb_scancode_set; extern unsigned char kb_scancode_set;
extern void i86_kb_set_key(byte scancode, byte val); extern void i86_kb_set_key(unsigned char scancode, unsigned char val);
extern void i86_kb_set_LEDs(byte status); extern void i86_kb_set_LEDs(unsigned char status);
extern void i86_kb_set_repeat(byte rate, byte delay); extern void i86_kb_set_repeat(unsigned char rate, unsigned char delay);
extern void i86_kb_set_scancodeset(byte set); extern void i86_kb_set_scancodeset(unsigned char set);
extern byte i86_kb_get_key(byte scancode); extern unsigned char i86_kb_get_key(unsigned char scancode);
extern void i86_kb_handler(ISR_stack_regs *r); extern void i86_kb_handler(ISR_stack_regs *r);
extern kb_key getkey(); extern kb_key getkey();
extern void i86_kb_set_repeat(byte rate, byte delay); extern void i86_kb_set_repeat(unsigned char rate, unsigned char delay);
extern void i86_kb_set_LEDs(byte status); extern void i86_kb_set_LEDs(unsigned char status);
extern void i86_kb_set_scancodeset(byte set); extern void i86_kb_set_scancodeset(unsigned char set);
extern byte i86_kb_get_scancodeset(); extern unsigned char i86_kb_get_scancodeset();
extern void i86_kb_waitin(); extern void i86_kb_waitin();
extern void i86_kb_waitout(); extern void i86_kb_waitout();
extern void i86_kb_install_partone(); extern void i86_kb_install_partone();

View File

@ -30,13 +30,31 @@ goto cmos
:cpu :cpu
cd cpu cd cpu
@echo * Compiling Central Processing Unit... @echo * Compiling Central Processing Unit (CPU)...
del %objpath%\cpu.o del %objpath%\cpu.o
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/cpu.o cpu.c %djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/cpu.o cpu.c
if not exist %objpath%\cpu.o goto error if not exist %objpath%\cpu.o goto error
cd.. cd..
:dma
cd dma
@echo * Compiling Direct Memory Access Controller (DMAC)...
del %objpath%\dma.o
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/dma.o dma.c
if not exist %objpath%\dma.o goto error
cd..
:floppy
cd floppy
@echo * Compiling Floppy Driver...
del %objpath%\floppy.o
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/floppy.o floppy.c
if not exist %objpath%\floppy.o goto error
cd..
:gdt :gdt
cd gdt cd gdt
@echo * Compiling Global Descriptor Table... @echo * Compiling Global Descriptor Table...

View File

@ -50,6 +50,11 @@ void i86_pit_install(int freq)
} }
TIME i86_pit_get_time()
{
return _internal_clock;
}
unsigned char i86_pit_is_initialized() unsigned char i86_pit_is_initialized()
{ {
return _pit_init; return _pit_init;

View File

@ -2,7 +2,7 @@
#define __PIT_H #define __PIT_H
#include<regs.h> #include<regs.h>
extern volatile unsigned int _pit_ticks; extern volatile unsigned int _pit_ticks;
extern volatile unsigned int _pit_frequency; extern volatile unsigned int _pit_frequency;
extern volatile TIME _internal_clock; extern volatile TIME _internal_clock;

View File

@ -6,32 +6,185 @@
#include <stdint.h> #include <stdint.h>
//! multiboot info structure passed from boot loader /**The structure used by the VESA Controller Info table.*/
struct _VESA_CONTROLLER_INFO {
/** Vesa signature, should be "VESA". */
unsigned char Signature[4];
/** Version number, example: 0x0300 for VBE 3.0. */
uint16_t Version;
/** FAR pointer to OEM string. (seg:offset). */
uint16_t OEMString[2];
/** Capabilities, taken as 4 bytes. */
uint32_t Capabilities;
/** Video modes. FAR pointer (seg:offset). */
uint16_t VideoModes[2];
/** Total memory as number of 64k blocks. */
uint16_t TotalMemory;
} __attribute__((packed));
enum _VESA_MODE_ATTRIBUTES {
/** The mode is supported by the present hardware configuration. */
IsSupported = 0x01,
/** Optional information is available (must be = 1 for VBE v1.2+) */
OptionalInformation = 0x02,
/** BIOS output is supported. */
BiosOutput = 0x04,
/** Set if color, clear if monochrome. */
IsColor = 0x08,
/** Set if graphic mode, clear if text mode. */
IsGraphic = 0x10,
/** (VBE v2.0+) mode is not VGA compatible. */
VGACompatible = 0x20,
/** (VBE v2.0+) Bank switched mode not supported. */
BankSwitchedNotSupported = 0x40,
/** (VBE v2.0+) Linear framebuffer mode supported. */
LinearFramebufferSupported = 0x80,
/** (VBE v3.0) Interlaced mode available. */
InterlacedAvailable = 0x100,
/** (VBE/AF v1.0P) Application must call EnableDirrectAccess before calling bank-switching functions. */
EnableDirectAccess = 0x100,
/** (VBE v3.0) Hardware supports triple buffering. */
TripleBufferingSupported = 0x200,
/** (VBE v3.0) Hardware supports stereoscopic display. */
StereoscopicSupported = 0x400,
/** (VBE v3.0) Dual display start address support. */
DualDisplayStartAddress = 0x800
} VESAModeAttributeMasks;
enum _VESA_MODE_WINDOW_ATTRIBUTES {
/** Window exists. */
Exists = 0x1,
/** Window is readable. */
Readable = 0x2,
/** Window is writable. */
Writable = 0x4
} VESAModeWindowAttributeMasks;
enum _VESA_MODE_MEMORY_MODELS {
/** Text */
Text = 0x00,
/** CGA graphics*/
CGA = 0x01,
/** HGC graphics*/
HGC = 0x02,
/** 16-color EGA grahpics*/
EGA = 0x03,
/** packed pixel graphics*/
PackedPixel = 0x04,
/** "sequ 256 (non chain 4) grahpics*/
Sequ256 = 0x05,
/** Direct color (HiColor, 24bit TrueColor)*/
DirectColor = 0x06,
/** YUV (luminance-chrominance, also called YIQ)*/
YUV = 0x07
} VESAModeMemoryModels;
struct _VESA_MODE_INFO {
/** Mode attributes, defined in VESAModeAttributeMasks. */
uint16_t Attributes;
/** Window attributes (window A or B), defined in VESAModeWindowAttributeMasks. */
uint8_t WindowAttributesA, WindowAttributesB;
/** Window granularity in KB. */
uint16_t WindowGranularity;
/** Start segment of window A (0x0000 if not supported).*/
uint16_t WindowStartSegmentA;
/** Start segment of window B (0x0000 if not supported).*/
uint16_t WindowStartSegmentB;
/** FAR window positioning function (equivalent to AX = 0x4F05. */
uint32_t WindowPositioningFunction;
/** Bytes per scanline.*/
uint16_t BytesPerScanline;
/** Width in pixels (graphics) or characters (text).*/
uint16_t Width;
/** Height in pixels (graphics) or characters (text).*/
uint16_t Height;
/** Width of character cell in pixels. */
uint8_t CharacterWidth;
/** Height of character cell in pixels. */
uint8_t CharacterHeight;
/** Number of memory planes. */
uint8_t MemoryPlanes;
/** Number of bits per pixel. */
uint8_t Bpp;
/** Number of banks.*/
uint8_t Banks;
/** Memory model type, defined in VESAModeMemoryModels.*/
uint8_t MemoryModelType;
/** Size of bank in KB. */
uint8_t BankSize;
/** Number of image pages minus one, that will fit in video RAM.*/
uint8_t ImagePages;
/** Reserved, 0x00 for VBE 1.0 to 2.0, 0x01 for VBE 3.0.*/
uint8_t Reserved_0;
/** (VESA v1.2+) Self explanatory.*/
uint8_t RedMaskSize, RedFieldPosition;
uint8_t GreenMaskSize, GreenFieldPosition;
uint8_t BlueMaskSize, BlueFieldPosition;
uint8_t ReservedMaskSize, ReservedFieldPosition;
/** (VESA v1.2+) Direct Color Mode info\n
Bit 0: color ramp is programmable\n
Bit 1: bytes in reserved field may be used by application.*/
uint8_t DirectColorModeInfo;
/** (VESA v2.0+) Physical address of linear video buffer.*/
void* LinearVideoBuffer;
/** (VESA v2.0+) Pointer to start of offscreen memory.*/
void* OffscreenMemory;
/** (VESA v2.0+) KB of offscreen memory.*/
uint16_t OffscreenMemorySize;
/** (VESA v3.0) Bytes per scanline in linear modes.*/
uint16_t BytesPerScanlineLinear;
/** (VESA v3.0) Number of images minus one for banked video modes.*/
uint8_t ImagesBankedMode;
/** (VESA v3.0) Number of images minus one for linear video modes.*/
uint8_t ImagesLinearMode;
/** (VESA v3.0) LINEAR MODES ONLY\n
* \DirectColorMaskSize: size of direct color <color> mask (in bits).\n
* \BitPosition: bit position of <color> mask LSB (e.g. shift count)*/
uint8_t RedDirectColorMaskSize, RedBitPosition;
uint8_t GreenDirectColorMaskSize, GreenBitPosition;
uint8_t BlueDirectColorMaskSize, BlueBitPosition;
uint8_t ReservedDirectColorMaskSize, ReservedBitPosition;
/** (VESA v3.0) Maximum pixel clock for graphics video mode, in Hz*/
uint32_t MaxPixelClock;
} __attribute__ ((packed));
/**The structure passed by the bootloader. */
typedef struct { typedef struct {
/** How much memory is installed on the system (in KB). */
uint64_t Memory;
/** Boot device. */
uint32_t BootDevice;
/** Pointer to a char[] string containing kernel parameters. */
unsigned char* CommandLine;
/** Number of other modules loaded by the bootloader. */
uint32_t ModulesCount;
/** Pointer to where other modules were loaded. */
void* ModulesAddress;
/** Size of memory map. */
uint32_t MemoryMapLength;
/** Pointer to memory map. */
void* MemoryMapAddress;
/** Drives info length. */
uint32_t DrivesLength;
/** Pointer to drives info. */
void* DrivesAddress;
/** BIOS ROM configuration table. */
uint32_t ConfigurationTable;
/** Name of bootloader, should be "CTA" (0 ended string). */
unsigned char BootloaderName[4];
/** APM Table. */
uint32_t APMTable;
/** VGA current video mode information. */
uint8_t VGACurrentVideoMode;
uint8_t VGACurrentVideoModeColumns;
uint8_t VGACurrentVideoPage;
/** VESA video mode(s) information. */
struct _VESA_CONTROLLER_INFO* VESAControllerInformation;
uint32_t m_flags;
uint32_t m_memoryLo;
uint32_t m_memoryHi;
uint32_t m_bootDevice;
uint32_t m_cmdLine;
uint32_t m_modsCount;
uint32_t m_modsAddr;
uint32_t m_syms0;
uint32_t m_syms1;
uint32_t m_syms2;
uint32_t m_mmap_length;
uint32_t m_mmap_addr;
uint32_t m_drives_length;
uint32_t m_drives_addr;
uint32_t m_config_table;
uint32_t m_bootloader_name;
uint32_t m_apm_table;
uint32_t m_vbe_control_info;
uint32_t m_vbe_mode_info;
uint16_t m_vbe_mode;
uint32_t m_vbe_interface_addr;
uint16_t m_vbe_interface_len;
} multiboot_info ; } multiboot_info ;

View File

@ -1,30 +1,104 @@
#ifndef __CONIO_H #ifndef __CONIO_H
#define __CONIO_H #define __CONIO_H
#define _ATTRIB 0x0F
extern unsigned char default_background, default_foreground; #include <stdlib.h>
extern char hex[16]; //#define _ATTRIB 0x0F
extern void itoa (int value, char *string, unsigned int radix); typedef struct {
extern int printf(const char* str, ...); /** Console window width. */
extern int abs(int x); unsigned width;
extern void graphics_init(); /** Console window height. */
extern void text_mode_cursor(int x, int y); unsigned height;
extern void set_default_colors(unsigned char back, unsigned char fore); /** Default colors (can be changed later with ConsoleSetDefaultColors() routine)*/
unsigned char defcolors;
/** Pointer to a routine to set the blinking cursor position.\n
Parameters are as following: (int x, int y), where x, y is a 2D position on the screen. */
void (*cursor)(int, int);
/** Pointer to a routine to put a character in a specified position.\n
Parameters are as following: (int x, int y, unsigned char c),\n
* where: x, y = 2D position on the screen\n
* c = ascii character*/
void (*putc)(int, int, unsigned char);
/** Pointer to a routine to return a character in a specified position.\n
Parameters are as following: (int x, int y), where x, y is a 2D position on the screen. */
unsigned char (*getc)(int, int);
/** Pointer to a routine to set the colors for the character in the specified position\n
Parameters are as following: (int x, int y, unsigned char color),\n
* where: x, y = 2D position on the screen\n
* color = index in 16 color palette for background (high nibble) and foreground (low nibble)*/
void (*putcolor)(int, int, unsigned char);
/** Pointer to a routine to return the colors for the character in the specified position\n
Parameters are as following: (int x, int y, unsigned char c),\n
* where: x, y = 2D position on the screen*/
unsigned char (*getcolor)(int, int);
} ConsoleScreen;
typedef struct {
/**Integer coordonates*/
int X, Y;
} Point;
typedef struct {
/**Unsigned integer coordonates.*/
unsigned X, Y;
} UPoint;
enum COLORS {
BLACK = 0x0,
BLUE = 0x1,
GREEN = 0x2,
CYAN = 0x3,
RED = 0x4,
MAGENTA = 0x5,
BROWN = 0x6,
LIGHTGRAY = 0x7,
DARKGRAY = 0x8,
LIGHTBLUE = 0x9,
LIGHTGREEN = 0xA,
LIGHTCYAN = 0xB,
LIGHTRED = 0xC,
LIGHTMAGENTA = 0xD,
YELLOW = 0xE,
WHITE = 0xF,
BLINK = 0x80
};
enum CURSORSHAPE {
_NOCURSOR = 0x0,
_SOLIDCURSOR = 0x1,
_NORMALCURSOR = 0x2
};
/***/
//extern char* cgets(char* string);
extern void ConsoleInstall(ConsoleScreen screen);
extern void ConsoleUpdateCursor(UPoint position, unsigned char type);
extern void ConsoleScroll(unsigned lines);
/** Clears to end of line in text window\n\n
Declaration: void clreol(void);\n\n
Remarks:\n
clreol clears all characters from the cursor position to the end of the line
within the current text window, without moving the cursor.\n\n
Return Value: None*/
extern void clreol();
extern void clrscr(); extern void clrscr();
extern void scroll(int n); extern int gettext (int left, int top, int right, int bottom, unsigned char* dest);
extern void prev_line(); extern void gotoxy (int x, int y);
extern void next_line(); extern int cprintf(const char* str, ...);
extern void putc_pos_font(int x, int y, char c, unsigned char back, unsigned char fore); extern int cputs(const char* str);
extern void putc_pos(int x, int y, char c); extern int cgets(char* string, int maxlen);
extern void putc_font(char c, unsigned char back, unsigned char fore); extern int getch();
extern void putc(char c); extern int getche();
extern void puts_pos_font(int x, int y, const char *str, unsigned char back, unsigned char fore); extern void movetext(int left, int top, int right, int bottom, int destleft, int desttop);
extern void puts_pos(int x, int y, const char *str); extern int putch(const char c);
extern void puts(const char *str); extern int puttext(int left, int top, int right, int bottom, unsigned char* src);
extern void puts_font(const char *str, unsigned char back, unsigned char fore); extern void _setcursortype (int cursor);
extern void put_hex(unsigned int alpha); extern int wherex();
extern void put_hex_pos(int x, int y, unsigned int alpha); extern int wherey();
extern void put_bin (int x, int y, unsigned char xz);
#endif #endif

View File

@ -1,55 +1,31 @@
#ifndef __CTYPE_H #ifndef __CTYPE_H
#define __CTYPE_H #define __CTYPE_H
/****************************** extern unsigned char _ctype[];
* ctype.h *
* - character macros *
******************************/
#ifdef _MSC_VER #define _CTYPE_ISCONTROL 0x01 // 0000 0001
// Get rid of conversion warnings #define _CTYPE_ISSPACE 0x02 // 0000 0010
#pragma warning (disable:4244) #define _CTYPE_ISBLANK 0x04 // 0000 0100 etc.
#endif #define _CTYPE_ISPUNCT 0x08
#define _CTYPE_ISDIGIT 0x10
#define _CTYPE_ISHEX 0x20
#define _CTYPE_ISUPPER 0x40
#define _CTYPE_ISLOWER 0x80
#ifdef __cplusplus #define isalnum(c) (_ctype[(int)c+1] & (_CTYPE_ISLOWER | _CTYPE_ISUPPER | _CTYPE_ISDIGIT))
extern "C" #define isalpha(c) (_ctype[(int)c+1] & (_CTYPE_ISLOWER | _CTYPE_ISUPPER))
{ #define isblank(c) (_ctype[(int)c+1] & (_CTYPE_ISBLANK))
#endif #define iscntrl(c) (_ctype[(int)c+1] & (_CTYPE_ISCONTROL))
#define isdigit(c) (_ctype[(int)c+1] & (_CTYPE_ISDIGIT))
#define isgraph(c) (_ctype[(int)c+1] & (_CTYPE_ISLOWER | _CTYPE_ISUPPER | _CTYPE_ISDIGIT | _CTYPE_ISPUNCT))
#define islower(c) (_ctype[(int)c+1] & (_CTYPE_ISLOWER))
#define isprint(c) (_ctype[(int)c+1] & (_CTYPE_ISLOWER | _CTYPE_ISUPPER | _CTYPE_ISDIGIT | _CTYPE_ISPUNCT | _CTYPE_ISBLANK))
#define ispunct(c) (_ctype[(int)c+1] & (_CTYPE_ISPUNCT))
#define isspace(c) (_ctype[(int)c+1] & (_CTYPE_ISSPACE))
#define isupper(c) (_ctype[(int)c+1] & (_CTYPE_ISUPPER))
#define isxdigit(c) (_ctype[(int)c+1] & (_CTYPE_ISHEX))
extern char _ctype[]; extern int toupper(int c);
extern int tolower(int c);
/* Constants */ #endif
#define CT_UP 0x01 /* upper case */
#define CT_LOW 0x02 /* lower case */
#define CT_DIG 0x04 /* digit */
#define CT_CTL 0x08 /* control */
#define CT_PUN 0x10 /* punctuation */
#define CT_WHT 0x20 /* white space (space/cr/lf/tab) */
#define CT_HEX 0x40 /* hex digit */
#define CT_SP 0x80 /* hard space (0x20) */
/* Basic macros */
#define isalnum(c) ((_ctype + 1)[(unsigned)(c)] & (CT_UP | CT_LOW | CT_DIG))
#define isalpha(c) ((_ctype + 1)[(unsigned)(c)] & (CT_UP | CT_LOW))
#define iscntrl(c) ((_ctype + 1)[(unsigned)(c)] & (CT_CTL))
#define isdigit(c) ((_ctype + 1)[(unsigned)(c)] & (CT_DIG))
#define isgraph(c) ((_ctype + 1)[(unsigned)(c)] & (CT_PUN | CT_UP | CT_LOW | CT_DIG))
#define islower(c) ((_ctype + 1)[(unsigned)(c)] & (CT_LOW))
#define isprint(c) ((_ctype + 1)[(unsigned)(c)] & (CT_PUN | CT_UP | CT_LOW | CT_DIG | CT_SP))
#define ispunct(c) ((_ctype + 1)[(unsigned)(c)] & (CT_PUN))
#define isspace(c) ((_ctype + 1)[(unsigned)(c)] & (CT_WHT))
#define isupper(c) ((_ctype + 1)[(unsigned)(c)] & (CT_UP))
#define isxdigit(c) ((_ctype + 1)[(unsigned)(c)] & (CT_DIG | CT_HEX))
#define isascii(c) ((unsigned)(c) <= 0x7F)
#define toascii(c) ((unsigned)(c) & 0x7F)
#define tolower(c) (isupper(c) ? c + 'a' - 'A' : c)
#define toupper(c) (islower(c) ? c + 'A' - 'a' : c)
#ifdef __cplusplus
}
#endif
#endif

View File

@ -3,14 +3,15 @@
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
//#include <..\hal\floppy\floppy.h>
#define far #define far
#define near #define near
#define i86_start_interrupts() __asm__ __volatile__ ("sti"); #define i86_start_interrupts() __asm__ __volatile__ ("sti");
#define i86_clear_interrupts() __asm__ __volatile__ ("cli"); #define i86_clear_interrupts() __asm__ __volatile__ ("cli");
extern volatile TIME _internal_clock; extern TIME i86_pit_get_time() ;
extern unsigned* i86_read_sector (unsigned* where, unsigned char drive, int sectorLBA);
// initialize hardware abstraction layer // initialize hardware abstraction layer
extern void i86_hal_initialize (); extern void i86_hal_initialize ();
@ -23,10 +24,10 @@ extern int i86_hal_shutdown ();
//! output sound to speaker //! output sound to speaker
extern void sound (unsigned frequency); extern void sound (unsigned frequency);
//! read byte from device using port mapped io //! read unsigned char from device using port mapped io
//extern unsigned char inportb (unsigned short _port); //extern unsigned char inportb (unsigned short _port);
//! write byte to device through port mapped io //! write unsigned char to device through port mapped io
//extern void outportb (unsigned short _port, unsigned char _data); //extern void outportb (unsigned short _port, unsigned char _data);
//! sets new interrupt vector //! sets new interrupt vector
@ -44,6 +45,7 @@ extern void reboot();
/********************************************************************** /**********************************************************************
* KEYBOARD STUFF * * KEYBOARD STUFF *
**********************************************************************/ **********************************************************************/
#define KB_KEY_LSHIFT 0x81 // 1000 0001 #define KB_KEY_LSHIFT 0x81 // 1000 0001
#define KB_KEY_RSHIFT 0X82 // 1000 0010 #define KB_KEY_RSHIFT 0X82 // 1000 0010
#define KB_KEY_LALT 0X84 // 1000 0100 #define KB_KEY_LALT 0X84 // 1000 0100
@ -51,130 +53,132 @@ extern void reboot();
#define KB_KEY_LCTRL 0x90 // 1001 0000 #define KB_KEY_LCTRL 0x90 // 1001 0000
#define KB_KEY_RCTRL 0xA0 // 1010 0000 #define KB_KEY_RCTRL 0xA0 // 1010 0000
#define KB_KEY_FSHIFT 0xC0 // 1100 0000 #define KB_KEY_FSHIFT 0xC0 // 1100 0000
extern volatile byte kb_modifier_status; extern volatile unsigned char kb_modifier_status;
#define KB_PREFIX_GRAY 0x01 // Gray #define KB_PREFIX_GRAY 0x01 // Gray
#define KB_PREFIX_BREAK 0x02 // Break code #define KB_PREFIX_BREAK 0x02 // Break code
#define KB_PREFIX_PAUSE 0x04 // Pause/break key #define KB_PREFIX_PAUSE 0x04 // Pause/break key
#define KB_PREFIX_PAUSE1 0x08 // Recieved first byte from pause/break #define KB_PREFIX_PAUSE1 0x08 // Recieved first unsigned char from pause/break
extern volatile byte kb_prefix; extern volatile unsigned char kb_prefix;
#define KB_KEY_SCROLL 0xF1 // 1111 0001 #define KB_KEY_SCROLL 0xF1 // 1111 0001
#define KB_KEY_NUM 0xF2 // 1111 0010 #define KB_KEY_NUM 0xF2 // 1111 0010
#define KB_KEY_CAPS 0xF4 // 1111 0100 #define KB_KEY_CAPS 0xF4 // 1111 0100
extern volatile byte kb_lights_status; extern volatile unsigned char kb_lights_status;
extern byte kb_scancode_set; extern unsigned char kb_scancode_set;
#define KB_KEY_PAUSE 0x00
#define KB_KEY_F9 0x01
#define KB_KEY_F7 0x02
#define KB_KEY_F5 0X03
#define KB_KEY_F3 0x04
#define KB_KEY_F1 0x05
#define KB_KEY_F2 0x06
#define KB_KEY_F12 0x07
#define KB_KEY_PRINTSCRN 0x08
#define KB_KEY_F10 0x09
#define KB_KEY_F8 0x0A
#define KB_KEY_F6 0x0B
#define KB_KEY_F4 0x0C
#define KB_KEY_TAB 0x0D
#define KB_KEY_TILDA 0x0E
#define KB_KEY_Q 0x15
#define KB_KEY_1 0x16
#define KB_KEY_Z 0x1A
#define KB_KEY_S 0x1B
#define KB_KEY_A 0x1C
#define KB_KEY_W 0x1D
#define KB_KEY_2 0x1E
#define KB_KEY_LWIN 0x1F
#define KB_KEY_C 0x21
#define KB_KEY_X 0x22
#define KB_KEY_D 0x23
#define KB_KEY_E 0x24
#define KB_KEY_4 0x25
#define KB_KEY_3 0x26
#define KB_KEY_RWIN 0x27
#define KB_KEY_SPACE 0x29
#define KB_KEY_V 0x2A
#define KB_KEY_F 0x2B
#define KB_KEY_T 0x2C
#define KB_KEY_R 0x2D
#define KB_KEY_5 0x2E
#define KB_KEY_MENU 0x2F
#define KB_KEY_N 0x31
#define KB_KEY_B 0x32
#define KB_KEY_H 0x33
#define KB_KEY_G 0x34
#define KB_KEY_Y 0x35
#define KB_KEY_6 0x36
#define KB_KEY_M 0x3A
#define KB_KEY_J 0x3B
#define KB_KEY_U 0x3C
#define KB_KEY_7 0x3D
#define KB_KEY_8 0x3E
#define KB_KEY_COMMA 0x41
#define KB_KEY_K 0x42
#define KB_KEY_I 0x43
#define KB_KEY_O 0x44
#define KB_KEY_0 0x45
#define KB_KEY_9 0x46
#define KB_KEY_PERIOD 0x49
#define KB_KEY_SLASH 0x4A
#define KB_KEY_L 0x4B
#define KB_KEY_SEMICOLON 0x4C
#define KB_KEY_P 0x4D
#define KB_KEY_DASH 0x4E
#define KB_KEY_APOSTROPHE 0x52
#define KB_KEY_LBRACKET 0x54
#define KB_KEY_EQUAL 0x55
#define KB_KEY_NUMPAD_ENTER 0x59
#define KB_KEY_ENTER 0x5A
#define KB_KEY_RBRACKET 0x5B
#define KB_KEY_BACKSLASH 0x5D
#define KB_KEY_END 0x5E
#define KB_KEY_LEFT 0x5F
#define KB_KEY_HOME 0x60
#define KB_KEY_INSERT 0x61
#define KB_KEY_DELETE 0x62
#define KB_KEY_DOWN 0x63
#define KB_KEY_RIGHT 0x64
#define KB_KEY_UP 0x65
#define KB_KEY_BACKSPACE 0x66
#define KB_KEY_PGDOWN 0x67
#define KB_KEY_PGUP 0x68
#define KB_KEY_NUMPAD_1 0x69
#define KB_KEY_NUMPAD_SLASH 0x6A
#define KB_KEY_NUMPAD_4 0x6B
#define KB_KEY_NUMPAD_7 0x6C
#define KB_KEY_NUMPAD_0 0x70
#define KB_KEY_NUMPAD_COLON 0x71
#define KB_KEY_NUMPAD_2 0x72
#define KB_KEY_NUMPAD_5 0x73
#define KB_KEY_NUMPAD_6 0x74
#define KB_KEY_NUMPAD_8 0x75
#define KB_KEY_ESC 0x76
#define KB_KEY_F11 0x78
#define KB_KEY_NUMPAD_PLUS 0x79
#define KB_KEY_NUMPAD_3 0x7A
#define KB_KEY_NUMPAD_MINUS 0x7B
#define KB_KEY_NUMPAD_ASTERISK 0x7C
#define KB_KEY_NUMPAD_9 0x7D
enum KB_KEYS {
KB_KEY_PAUSE = 0x00,
KB_KEY_F9 = 0x01,
KB_KEY_F7 = 0x02,
KB_KEY_F5 = 0X03,
KB_KEY_F3 = 0x04,
KB_KEY_F1 = 0x05,
KB_KEY_F2 = 0x06,
KB_KEY_F12 = 0x07,
KB_KEY_PRINTSCRN = 0x08,
KB_KEY_F10 = 0x09,
KB_KEY_F8 = 0x0A,
KB_KEY_F6 = 0x0B,
KB_KEY_F4 = 0x0C,
KB_KEY_TAB = 0x0D,
KB_KEY_TILDA = 0x0E,
KB_KEY_Q = 0x15,
KB_KEY_1 = 0x16,
KB_KEY_Z = 0x1A,
KB_KEY_S = 0x1B,
KB_KEY_A = 0x1C,
KB_KEY_W = 0x1D,
KB_KEY_2 = 0x1E,
KB_KEY_LWIN = 0x1F,
KB_KEY_C = 0x21,
KB_KEY_X = 0x22,
KB_KEY_D = 0x23,
KB_KEY_E = 0x24,
KB_KEY_4 = 0x25,
KB_KEY_3 = 0x26,
KB_KEY_RWIN = 0x27,
KB_KEY_SPACE = 0x29,
KB_KEY_V = 0x2A,
KB_KEY_F = 0x2B,
KB_KEY_T = 0x2C,
KB_KEY_R = 0x2D,
KB_KEY_5 = 0x2E,
KB_KEY_MENU = 0x2F,
KB_KEY_N = 0x31,
KB_KEY_B = 0x32,
KB_KEY_H = 0x33,
KB_KEY_G = 0x34,
KB_KEY_Y = 0x35,
KB_KEY_6 = 0x36,
KB_KEY_M = 0x3A,
KB_KEY_J = 0x3B,
KB_KEY_U = 0x3C,
KB_KEY_7 = 0x3D,
KB_KEY_8 = 0x3E,
KB_KEY_COMMA = 0x41,
KB_KEY_K = 0x42,
KB_KEY_I = 0x43,
KB_KEY_O = 0x44,
KB_KEY_0 = 0x45,
KB_KEY_9 = 0x46,
KB_KEY_PERIOD = 0x49,
KB_KEY_SLASH = 0x4A,
KB_KEY_L = 0x4B,
KB_KEY_SEMICOLON = 0x4C,
KB_KEY_P = 0x4D,
KB_KEY_DASH = 0x4E,
KB_KEY_APOSTROPHE = 0x52,
KB_KEY_LBRACKET = 0x54,
KB_KEY_EQUAL = 0x55,
KB_KEY_NUMPAD_ENTER = 0x59,
KB_KEY_ENTER = 0x5A,
KB_KEY_RBRACKET = 0x5B,
KB_KEY_BACKSLASH = 0x5D,
KB_KEY_END = 0x5E,
KB_KEY_LEFT = 0x5F,
KB_KEY_HOME = 0x60,
KB_KEY_INSERT = 0x61,
KB_KEY_DELETE = 0x62,
KB_KEY_DOWN = 0x63,
KB_KEY_RIGHT = 0x64,
KB_KEY_UP = 0x65,
KB_KEY_BACKSPACE = 0x66,
KB_KEY_PGDOWN = 0x67,
KB_KEY_PGUP = 0x68,
KB_KEY_NUMPAD_1 = 0x69,
KB_KEY_NUMPAD_SLASH = 0x6A,
KB_KEY_NUMPAD_4 = 0x6B,
KB_KEY_NUMPAD_7 = 0x6C,
KB_KEY_NUMPAD_0 = 0x70,
KB_KEY_NUMPAD_COLON = 0x71,
KB_KEY_NUMPAD_2 = 0x72,
KB_KEY_NUMPAD_5 = 0x73,
KB_KEY_NUMPAD_6 = 0x74,
KB_KEY_NUMPAD_8 = 0x75,
KB_KEY_ESC = 0x76,
KB_KEY_F11 = 0x78,
KB_KEY_NUMPAD_PLUS = 0x79,
KB_KEY_NUMPAD_3 = 0x7A,
KB_KEY_NUMPAD_MINUS = 0x7B,
KB_KEY_NUMPAD_ASTERISK = 0x7C,
KB_KEY_NUMPAD_9 = 0x7D
};
typedef struct { typedef struct {
byte status; unsigned char status;
byte lights; unsigned char lights;
byte scancode; unsigned char scancode;
byte character; unsigned char character;
} kb_key; } kb_key;
extern char getch(); //extern char getch();
extern kb_key getkey(); extern kb_key getkey();
extern char scancode_to_ascii(byte scancode, byte status); extern char scancode_to_ascii(unsigned char scancode, unsigned char status);
extern byte get_key_status(byte scancode); extern unsigned char get_key_status(unsigned char scancode);
extern void kb_set_repeat(float rate, int delay); extern void kb_set_repeat(float rate, int delay);
extern void kb_set_LEDs(byte status); extern void kb_set_LEDs(unsigned char status);
#endif #endif

93
SysCore/include/stdlib.h Normal file
View File

@ -0,0 +1,93 @@
#ifndef __STDLIB_H__
#define __STDLIB_H__
/** Returns the absolute value of an integer.*/
#define abs(x) (x>0) ? (x) : (x*-1)
/** Returns the absolute value of a long variable.*/
#define labs(x) (x>0) ? (x) : (x*-1)
/** Returns the maximum of two numbers.*/
#define max(a, b) (a > b) ? a : b
/** Returns the minimum of two numbers.*/
#define min(a, b) (a < b) ? a : b
#define NULL 0
/** div_t is a structure of integers used by div()\n
Notes:\n
- quot = quotient;\n
- rem = remainder;\n */
typedef struct {
/** Quotient */
long quot;
/** Remainder */
long rem;
} div_t;
/** ldiv_t is a structure of integers used by ldiv()\n
Notes:\n
- quot = quotient;\n
- rem = remainder;\n */
typedef struct {
/** Quotient*/
long quot;
/** Remainder*/
long rem;
} ldiv_t;
// TODO: extern long double _atold (const char* string);
// TODO: extern double atof (const char* string); // TODO: initialize FPU
/** Convert ASCII string to INT */
extern int atoi (const char* string);
/** Convert ASCII string to LONG */
extern long atol (const char* string);
/** Convert ASCII string in hexadecimal to unsigned integer.*/
extern unsigned int atox (const char* string);
/** Peform a binary search\n
Notes:\n
- const void* key = A pointer to the element to look for\n
- const void* base = A pointer to the first element of the table\n
- unsigned nelem = The number of elements in the table\n
- unsigned width = The size of one element of the table\n
- int *fcmp = A user defined comparison routine\n */
extern void* bsearch (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
/**Divides two integers and returns both the quotient and the remainder as a div_t structure.*/
extern div_t div (int numerator, int denominator);
/** Convert SIGNED INT to ASCII string */
extern void itoa (signed int value, char *string, int radix);
/**Divides two longs and returns both the quotient and the remainder as a ldiv_t structure.*/
extern ldiv_t ldiv (long numerator, long denominator);
/**Does a linear search for *key in a table\n
Notes:\n
- const void* key = A pointer to the element to look for\n
- const void* base = A pointer to the first element of the table\n
- unsigned nelem = The number of elements in the table\n
- unsigned width = The size of one element of the table\n
- int *fcmp = A user defined comparison routine\n */
void* lfind (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
/** Convert SIGNED LONG to ASCII string */
extern void ltoa (signed long value, char *string, int radix);
/** Sorts an array using an optimized quick sort algorithm.\n
Notes:\n
- void base = A pointer to the first element of the table\n
- unsigned *nelem = The number of elements in the table\n
- unsigned width = The size of one element of the table\n
- int *fcmp = A user defined comparison routine\n */
void qsort (void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*));
/** Convert UNSIGNED INT to ASCII string */
extern void uitoa (unsigned int value, char *string, int radix);
/** Convert UNSIGNED LONG to ASCII string */
extern void ultoa (unsigned long value, char *string, int radix);
#endif

View File

@ -1,21 +1,19 @@
#ifndef __TIME_C #ifndef __TIME_C
#define __TIME_C #define __TIME_C
extern const char* clock_months[13]; extern const char* clock_month[13];
extern const char* clock_weekdays[8]; extern const char* clock_weekday[8];
extern byte clock_months_len[13]; extern unsigned char clock_month_len[13];
typedef struct { typedef struct {
byte seconds; unsigned char second;
byte minutes; unsigned char minute;
byte hours; unsigned char hour;
byte weekday; unsigned char weekday;
byte day; unsigned char day;
byte month; unsigned char month;
byte year; unsigned char year;
byte century; unsigned char century;
byte am_pm;
} TIME; } TIME;
extern void _CLOCK_INC(TIME *tim); extern void _CLOCK_INC(TIME *tim);

View File

@ -11,7 +11,9 @@ set incpath=../include
del %objpath%\system.o del %objpath%\system.o
del %objpath%\string.o del %objpath%\string.o
del %objpath%\conio.o del %objpath%\conio.o
del %objpath%\stdlib.o
del %objpath%\time.o del %objpath%\time.o
del %objpath%\ctype.o
goto build goto build
:error :error
@ -30,6 +32,13 @@ goto build
@echo * Compiling CONIO.C ... @echo * Compiling CONIO.C ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/conio.o conio.c %djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/conio.o conio.c
@echo * Compiling CTYPE.C ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/ctype.o ctype.c
@echo * Compiling STDLIB.C ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/stdlib.o stdlib.c
@echo * Compiling TIME.C ... @echo * Compiling TIME.C ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/time.o time.c %djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/time.o time.c
@ -37,4 +46,6 @@ goto build
if not exist %objpath%\system.o goto error if not exist %objpath%\system.o goto error
if not exist %objpath%\string.o goto error if not exist %objpath%\string.o goto error
if not exist %objpath%\conio.o goto error if not exist %objpath%\conio.o goto error
if not exist %objpath%\ctype.o goto error
if not exist %objpath%\stdlib.o goto error
if not exist %objpath%\time.o goto error if not exist %objpath%\time.o goto error

View File

@ -1,34 +1,530 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <system.h>
#include <conio.h> #include <conio.h>
#include <hal.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../memory/mmngr_ph.h"
byte default_background, default_foreground; unsigned char ColorDefault;
unsigned char ConsoleScreenInstalled = 0;
/*char hex[] = "0123456789ABCDEF"; */
char hex[] = "0123456789ABCDEF"; /* External routines the user must specify */
const static char base_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; void (*_console_cursor)(int x, int y);
void (*_console_putc)(int x, int y, unsigned char c);
unsigned char (*_console_getc) (int x, int y);
void (*_console_putcolor)(int x, int y, unsigned char color);
unsigned char (*_console_getcolor)(int x, int y);
int abs(int x) /* Other external routines */
extern void *memset(void *dest, char val, int count);
/* Important variables */
UPoint ScreenSize, Cursor;
void ConsoleInstall(ConsoleScreen screen)
{ {
return (x>0) ? (x) : (x*-1); _console_cursor = screen.cursor;
_console_putc = screen.putc;
_console_getc = screen.getc;
_console_putcolor = screen.putcolor;
_console_getcolor = screen.getcolor;
ScreenSize.X = screen.width;
ScreenSize.Y = screen.height;
ColorDefault = screen.defcolors;
Cursor.X = 0; Cursor.Y = 0;
ConsoleUpdateCursor (Cursor, _NORMALCURSOR);
ConsoleScreenInstalled = 1;
} }
unsigned char _cuc_ex_type;
UPoint _cuc_ex_pos;
void ConsoleUpdateCursor(UPoint position, unsigned char type)
{
if (_cuc_ex_type == _SOLIDCURSOR)
(*_console_putcolor) (
_cuc_ex_pos.X,
_cuc_ex_pos.Y,
0xFF - (*_console_getcolor)(_cuc_ex_pos.X, _cuc_ex_pos.Y)
);
if (_cuc_ex_type == _NORMALCURSOR && type != _NORMALCURSOR)
(*_console_cursor)(ScreenSize.X, ScreenSize.Y);
switch (type) {
case _NORMALCURSOR:
(*_console_cursor)(position.X, position.Y);
break;
case _SOLIDCURSOR:
(*_console_putcolor) (
position.X, position.Y,
0xFF - (*_console_getcolor)(position.X, position.Y)
); break;
case _NOCURSOR:
(*_console_cursor)(ScreenSize.X, ScreenSize.Y); break;
}
_cuc_ex_type = type;
_cuc_ex_pos.X = position.X;
_cuc_ex_pos.Y = position.Y;
}
void ConsoleScroll(unsigned lines)
{
movetext(0, (int)lines, ScreenSize.X, ScreenSize.Y, 0, 0);
}
void ConsoleSetDefaultColors (unsigned char background, unsigned char foreground)
{
ColorDefault = (foreground & 0xF) | (background<<4);
}
void ConsoleSetCursorPosition (Point rel, unsigned char display)
{
Point abs = { (int)(Cursor.X) + rel.X,
(int)(Cursor.Y) + rel.Y };
while (abs.X < 0) { abs.X += (int)ScreenSize.X; abs.Y--; }
while (abs.X >= (int)ScreenSize.X) { abs.X -= (int)ScreenSize.X; abs.Y++; }
if (abs.Y < 0) abs.Y = 0;
if (abs.Y >= (int)ScreenSize.Y) {
ConsoleScroll((unsigned)abs.Y - ScreenSize.Y + 1);
abs.Y = (int)ScreenSize.Y - 1;
}
Cursor.X = (unsigned)abs.X;
Cursor.Y = (unsigned)abs.Y;
if (display) ConsoleUpdateCursor(Cursor, _cuc_ex_type);
}
/**Inline function to return the index of next word\n
Parameters:\n
- const char* string = string to check for words\n
- int len = length of string\n
- int current = current cursor position\n
- int plus = direction (0:back, 1:foward)\n*/
inline int __cgets__skip_word(const char* string, int len, int current, int plus)
{
int tmp = current;
if (!plus) {
--tmp;
while ((isspace((unsigned char)string[tmp]) || ispunct((unsigned char)string[tmp])) && tmp > 0) --tmp;
while (isalnum((unsigned char)string[tmp]) && tmp > 0) --tmp;
}
else {
++tmp;
while (isalnum((unsigned char)string[tmp]) && tmp < len) ++tmp;
while ((isspace((unsigned char)string[tmp]) || ispunct((unsigned char)string[tmp])) && tmp < len) ++tmp;
}
if (tmp != 0 && plus==0) tmp++;
return tmp;
}
/**Macro which defines the INSERT key behaviour (overwrite/insert mode)*/
#define __cgets__key_insert() OverWrite = 1 - OverWrite
/**Macro which defines the ENTER key behaviour*/
#define __cgets__key_enter() { ConsoleSetCursorPosition((Point){Len, 0}, 1); return Len; }
/**Move the cursor to the left/right\n
Parameters:\n
- const char* string = the character string to work with\n
- int direction = direction to go (0: backward, 1:foward)\n
- int len = length of string\n
- int* cursor = pointer to cursor integer\n
- int word = move one word (1) or just one character(0)\n */
inline void __cgets__move_cursor(const char* string, int direction, int len, int* cursor, int word)
{
int dtmp = (direction) ? 1 : -1;
if ((*cursor <= 0 && direction == 0) || (*cursor >= len && direction == 1)) return;
// Skip one word (e.g. CTRL is pressed)
if (word) {
int tmp = __cgets__skip_word(string, len, *cursor+dtmp, direction);
*cursor = tmp;
}
else {
*cursor += dtmp;
}
}
/**Deletes the substring between index_start and index_end-1, also updates string length\n
Parameters:\n
- char* string = the character string to work with\n
- int index_start = where to start\n
- int index_end = where to stop\n
- int* len = pointer to length of string\n*/
inline void __cgets__delete (char* string, int index_start, int index_end, int* len)
{
for (; index_end <= *len+1; index_start++, index_end++)
string[index_start] = string[index_end];
*len = strlen(string);
}
/**Inserts a char in position of index, also updates string length\n
Parameters:\n
- char* string = the character string to work with\n
- int index = where to insert\n
- char c = characer to insert\n
- int* len = pointer to length of string\n*/
inline void __cgets__insert (char* string, int index, char c, int* len)
{
*len = *len+1;
int i;
for (i = *len-1; i > index; i--)
string[i] = string[i-1];
string[index] = c;
string[*len] = 0;
}
int cgets(char* string, int maxlen)
{
unsigned char OverWrite = 0;
memset ((void*)string, 0, maxlen);
int Len = 0, CurPos = 0;
kb_key Key;
UPoint CursorSave;
while (Len < maxlen)
{
// Display string on the screen
CursorSave = Cursor;
ConsoleUpdateCursor(Cursor, _NOCURSOR);
cputs(string); clreol();
// Display cursor in right position
Cursor = CursorSave;
ConsoleSetCursorPosition((Point){CurPos, 0}, 0);
ConsoleUpdateCursor(Cursor, (OverWrite) ? _SOLIDCURSOR : _NORMALCURSOR);
// Restore cursor
Cursor = CursorSave;
// Get key and process
Key = getkey();
switch (Key.scancode) {
// Switch overwrite/insert
case KB_KEY_INSERT: __cgets__key_insert(); break;
// Finish writing (return)
case KB_KEY_ENTER: __cgets__key_enter(); break;
case KB_KEY_NUMPAD_ENTER: __cgets__key_enter(); break;
// Left
case KB_KEY_LEFT:
__cgets__move_cursor(string, 0, Len, &CurPos, ((Key.status & KB_KEY_LCTRL) || (Key.status & KB_KEY_RCTRL)));
break;
// Right
case KB_KEY_RIGHT:
__cgets__move_cursor(string, 1, Len, &CurPos, ((Key.status & KB_KEY_LCTRL) || (Key.status & KB_KEY_RCTRL)));
break;
case KB_KEY_HOME:
CurPos = 0;
break;
case KB_KEY_END:
CurPos = Len;
break;
case KB_KEY_BACKSPACE:
if (CurPos > 0) {
int tmp;
if ((Key.status & KB_KEY_LCTRL) || (Key.status & KB_KEY_RCTRL))
tmp = __cgets__skip_word(string, Len, CurPos, 0);
else tmp = CurPos-1;
__cgets__delete(string, tmp, CurPos, &Len);
CurPos = tmp;
};
break;
// Delete
case KB_KEY_DELETE:
if (CurPos < Len) {
int tmp;
// If CTRL is pressed, foward one word
if ((Key.status & KB_KEY_LCTRL) || (Key.status & KB_KEY_RCTRL))
tmp = __cgets__skip_word(string, Len, CurPos, 1);
else tmp = CurPos+1;
__cgets__delete(string, CurPos, tmp, &Len);
}
break;
// Text character
default:
if (isprint(Key.character)) {
// fix CAPS bug
if ((Key.lights & KB_KEY_CAPS) && islower((unsigned char)Key.character))
Key.character = toupper(Key.character);
// Cursor is at the end of the string
if (CurPos == Len) {
Len++; string[Len-1] = Key.character;
string[Len] = 0;
}
// Cursor is not at the end in OverWrite mode
else if (OverWrite) string[CurPos] = Key.character;
// Cursor is not at the end in Insert mode
else __cgets__insert(string, CurPos, Key.character, &Len);
// Increase cursor position
CurPos++;
}
break;
}
}
__cgets__key_enter();
}
void clreol()
{
int counter = Cursor.X;
while (counter < ScreenSize.X) {
(*_console_putc)(counter, Cursor.Y, 0);
++counter;
}
}
void clrscr()
{
for (Cursor.Y = 0; Cursor.Y < ScreenSize.Y; Cursor.Y++)
for (Cursor.X = 0; Cursor.X < ScreenSize.X; Cursor.X++) {
_console_putc(Cursor.X, Cursor.Y, 0);
_console_putcolor(Cursor.X, Cursor.Y, ColorDefault);
}
Cursor.X = 0; Cursor.Y = 0;
}
void getpass (char* string)
{}
int gettext (int left, int top, int right, int bottom, unsigned char* dest)
{
if (left < 0 || top < 0 || (unsigned)right > ScreenSize.X || (unsigned)bottom > ScreenSize.Y)
return 0;
int i, j;
for (i = top; i < bottom; i++)
for (j = left; j < right; j++) {
*dest++ = (*_console_getc)(j, i);
*dest++ = (*_console_getcolor)(j, i);
}
return 1;
}
void gotoxy (int x, int y)
{
Cursor.X = x; Cursor.Y = y;
ConsoleUpdateCursor(Cursor, _cuc_ex_type);
}
int cputs(const char* str)
{
while (*str != 0) {
putch(*str); str++;
}
return (int)*str;
}
int getch()
{
kb_key k;
k = getkey();
if ((k.lights & KB_KEY_CAPS) && k.character >= 'a' && k.character <= 'z')
return (int)(k.character - 'a' + 'A');
return k.character;
}
int getche()
{
int ret = getch();
putch((char)ret);
return ret;
}
/*TODO: kbhit()*/
void movetext(int left, int top, int right, int bottom, int destleft, int desttop)
{
int destright = destleft + right - left;
int destbottom = desttop + bottom - top;
// Sanity check
if (left < 0 || top < 0 || (unsigned)right > ScreenSize.X ||
(unsigned)bottom > ScreenSize.Y || destleft < 0 || desttop < 0)
return;
// Allocate memory
int needed_mem = ((right-left) * (bottom - top) * 2) / pmmngr_get_block_size();
unsigned char* map = (unsigned char*) pmmngr_alloc_blocks(needed_mem);
// Save box in a buffer
gettext(left, top, right, bottom, map);
// Empty box
int i, j;
for (i = top; i < bottom; i++)
for (j = left; j < right; j++) {
(*_console_putc)(j, i, 0);
(*_console_putcolor)(j, i, ColorDefault);
}
// Put new text
puttext(destleft, desttop, destright, destbottom, map);
// Free used memory
pmmngr_free_blocks((unsigned)map, needed_mem * pmmngr_get_block_size());
}
int putch(const char c)
{
switch(c)
{
case '\n': ConsoleSetCursorPosition((Point){0, 1}, 0);
break;
case '\r': Cursor.X = 0;
break;
case '\t': ConsoleSetCursorPosition((Point){6 - (Cursor.X % 6), 0}, 0);
break;
case '\b': ConsoleSetCursorPosition((Point){-1, 0}, 0);
(*_console_putc)(Cursor.X, Cursor.Y, 0);
break;
default:
(*_console_putc)(Cursor.X, Cursor.Y, c);
(*_console_putcolor)(Cursor.X, Cursor.Y, ColorDefault);
ConsoleSetCursorPosition((Point){1,0}, 0);
break;
}
return c;
}
int puttext(int left, int top, int right, int bottom, unsigned char* src)
{
if (left < 0 || top < 0 || (unsigned)right > ScreenSize.X || (unsigned)bottom > ScreenSize.Y)
return 0;
int i,j;
for (i = top; i < bottom; i++)
for (j = left; j < right; j++) {
(*_console_putc)(j, i, *src++);
(*_console_putcolor)(j, i, *src++);
}
return 1;
}
void _setcursortype (int cursor)
{
ConsoleUpdateCursor(Cursor, cursor);
}
int wherex() { return (int)Cursor.X; };
int wherey() { return (int)Cursor.Y; };
int cprintf(const char* str, ...)
{
if (!ConsoleScreenInstalled) return -1;
if (!str || !*str) return 0;
va_list args;
va_start (args, str);
unsigned i, len = strlen(str);
unsigned char temp_col = ColorDefault;
for (i = 0; i < len; i++)
switch (str[i]) {
case '%':
switch (str[i+1]) {
// Character
case 'c': {char c = va_arg (args, char);
putch(c); i++; break;}
// String
case 's': {int* c = (int*) va_arg (args, char*);
cputs((const char*)c); i++; break;}
// Integers
case 'd':
case 'i': {int c = va_arg(args, int);
char temp[32];
itoa(c, temp, 10);
cputs(temp);
i++; break;}
// Integers - hex
case 'X':
case 'x': {int c = va_arg(args, int);
char temp[32];
uitoa(c, temp, 16);
cputs(temp);
i++; break;}
// Integers - unsigned
case 'u': { int c = va_arg(args, unsigned int);
char temp[32];
uitoa(c, temp, 10);
cputs(temp);
i++; break;
}
case '#': { char temp[] = {str[i+2], str[i+3], 0};
ColorDefault = (unsigned char) atox(temp);
i+=3;
break; }
default: va_end(args); return 1;
};
break;
default: putch(str[i]); break;
}
va_end(args);
ColorDefault = temp_col;
return i;
}
/*
void graphics_init() void graphics_init()
{ {
// Detect if color/monochrome screen
char c = (*(volatile unsigned short*)0x410)&0x30;
if (c==0x30) TextVideoRam = (byte *)0xb0000;
else TextVideoRam = (byte *)0xb8000;
// Reset cursor, use 80x25 text video mode
current_mode_width = 80;
current_mode_height = 25;
cursor_x = cursor_y = 0;
} }
// Change cursor position // Change cursor position
void text_mode_cursor(int x, int y) void text_mode_cursor(int x, int y)
{ {
@ -41,17 +537,17 @@ void text_mode_cursor(int x, int y)
} }
// Set the default colors; max is 0x0F // Set the default colors; max is 0x0F
void set_default_colors(byte back, byte fore) void set_default_colors(unsigned char back, unsigned char fore)
{ {
if (back < 0x10) default_background = back; if (back < 0x10) ColorDefaultBack = back;
if (fore < 0x10) default_foreground = fore; if (fore < 0x10) ColorDefaultFore = fore;
} }
// Clear screen, and set font to default font // Clear screen, and set font to default font
void clrscr() void clrscr()
{ {
byte font = default_foreground | (default_background<<4); unsigned char font = ColorDefaultFore | (ColorDefaultBack<<4);
int i = 0; int i = 0;
for (i = 0; i < current_mode_width*current_mode_height; i++) for (i = 0; i < current_mode_width*current_mode_height; i++)
{ TextVideoRam[2*i] = 0; { TextVideoRam[2*i] = 0;
@ -66,7 +562,7 @@ void scroll(int n)
TextVideoRam+(current_mode_width*n*2), TextVideoRam+(current_mode_width*n*2),
2*current_mode_width*(current_mode_height - n)); 2*current_mode_width*(current_mode_height - n));
byte blank = default_foreground | (default_background<<4); unsigned char blank = ColorDefaultFore | (ColorDefaultBack<<4);
int i; int i;
for (i = current_mode_width*(current_mode_height-n); for (i = current_mode_width*(current_mode_height-n);
@ -95,7 +591,7 @@ void next_line()
// Put character on screen in specified position; can use different font colors // Put character on screen in specified position; can use different font colors
void putc_pos_font(int x, int y, char c, byte back, byte fore) void putc_pos_font(int x, int y, char c, unsigned char back, unsigned char fore)
{ {
TextVideoRam[2*(y*current_mode_width+x)] = c; TextVideoRam[2*(y*current_mode_width+x)] = c;
TextVideoRam[2*(y*current_mode_width+x)+1] = fore|(back<<4); TextVideoRam[2*(y*current_mode_width+x)+1] = fore|(back<<4);
@ -106,7 +602,7 @@ void putc_pos(int x, int y, char c)
TextVideoRam[2*(y*current_mode_width+x)] = c; TextVideoRam[2*(y*current_mode_width+x)] = c;
} }
// Put character on screen in the current cursor position; different font colors // Put character on screen in the current cursor position; different font colors
void putc_font(char c, byte back, byte fore) void putc_font(char c, unsigned char back, unsigned char fore)
{ {
if (cursor_x >= current_mode_width) next_line(); if (cursor_x >= current_mode_width) next_line();
@ -126,50 +622,11 @@ void putc(char c)
cursor_x++; cursor_x++;
} }
// Unsigned INT to ASCII
void uitoa (unsigned int value, char *string, int radix)
{
if (radix == 1 || radix>36) return;
// Calculate how much space needed for number
int len, temp = abs(value);
for (len = 0; temp > 0; len++) temp/=radix;
if (len == 0) len = 1;
string[len] = 0;
for (len--; len >=0; len-- ) {
string[len] = base_chars[value%radix];
value/=radix;
}
}
// Signed INT to ASCII
void itoa (int value, char *string, unsigned int radix)
{
if (radix < 2 || radix>36) return;
// Calculate how much space needed for number
int len, temp = abs(value);
for (len = 0; temp > 0; len++) temp/=radix;
if (len == 0) len = 1;
//Add a space for '-'
if (value<0) {
len++; string[0] = '-';
value = abs(value);
}
string[len] = 0;
for (len--; len >0; len-- ) {
string[len] = base_chars[value%radix];
value/=radix;
}
}
// Put string on screen in specified position; can use different font colors // Put string on screen in specified position; can use different font colors
void puts_pos_font(int x, int y, const char *str, byte back, byte fore) void puts_pos_font(int x, int y, const char *str, unsigned char back, unsigned char fore)
{ {
int i; int i;
for (i = 0; str[i] != 0; i++) for (i = 0; str[i] != 0; i++)
@ -191,7 +648,7 @@ void puts(const char *str)
putc(str[i]); putc(str[i]);
} }
void puts_font(const char *str, byte back, byte fore) void puts_font(const char *str, unsigned char back, unsigned char fore)
{ {
int i; int i;
for (i = 0; str[i] != 0; i++) for (i = 0; str[i] != 0; i++)
@ -221,7 +678,7 @@ void put_hex_pos(int x, int y, unsigned int alpha)
puts_pos(x,y,nr); puts_pos(x,y,nr);
} }
void put_bin (int x, int y, byte xz) void put_bin (int x, int y, unsigned char xz)
{ {
int i; int i;
char arr[9] = {0,0,0,0,0,0,0,0,0}; char arr[9] = {0,0,0,0,0,0,0,0,0};
@ -232,53 +689,6 @@ void put_bin (int x, int y, byte xz)
} }
int printf(const char* str, ...)
{
if (!str) return 0;
va_list args;
va_start (args, str);
size_t i, len = strlen(str);
for (i = 0; i < len; i++)
switch (str[i]) {
case '%':
switch (str[i+1]) {
// Character
case 'c': {char c = va_arg (args, char);
putc(c); i++; break;}
// String
case 's': {int* c = (int*) va_arg (args, char*);
puts((const char*)c); i++; break;}
// Integers
case 'd':
case 'i': {int c = va_arg(args, int);
char temp[32];
itoa(c, temp, 10);
puts(temp);
i++; break;}
// Integers - hex
case 'X':
case 'x': {int c = va_arg(args, int);
char temp[32];
uitoa(c, temp, 16);
puts(temp);
i++; break;}
// Integers - unsigned
case 'u': { int c = va_arg(args, unsigned int);
char temp[32];
uitoa(c, temp, 10);
puts(temp);
i++; break;
}
default: va_end(args); return 1;
};
break;
default: putc(str[i]); break;
}
va_end(args);
return i;
}
*/

146
SysCore/lib/ctype.c Normal file
View File

@ -0,0 +1,146 @@
#include<ctype.h>
unsigned char _ctype[256] =
{
0, // -1 EOF
_CTYPE_ISCONTROL, // 00 (NUL)
_CTYPE_ISCONTROL, // 01 (SOH)
_CTYPE_ISCONTROL, // 02 (STX)
_CTYPE_ISCONTROL, // 03 (ETX)
_CTYPE_ISCONTROL, // 04 (EOT)
_CTYPE_ISCONTROL, // 05 (ENQ)
_CTYPE_ISCONTROL, // 06 (ACK)
_CTYPE_ISCONTROL, // 07 (BEL)
_CTYPE_ISCONTROL, // 08 (BS)
_CTYPE_ISSPACE+_CTYPE_ISCONTROL, // 09 (HT)
_CTYPE_ISSPACE+_CTYPE_ISCONTROL, // 0A (LF)
_CTYPE_ISSPACE+_CTYPE_ISCONTROL, // 0B (VT)
_CTYPE_ISSPACE+_CTYPE_ISCONTROL, // 0C (FF)
_CTYPE_ISSPACE+_CTYPE_ISCONTROL, // 0D (CR)
_CTYPE_ISCONTROL, // 0E (SI)
_CTYPE_ISCONTROL, // 0F (SO)
_CTYPE_ISCONTROL, // 10 (DLE)
_CTYPE_ISCONTROL, // 11 (DC1)
_CTYPE_ISCONTROL, // 12 (DC2)
_CTYPE_ISCONTROL, // 13 (DC3)
_CTYPE_ISCONTROL, // 14 (DC4)
_CTYPE_ISCONTROL, // 15 (NAK)
_CTYPE_ISCONTROL, // 16 (SYN)
_CTYPE_ISCONTROL, // 17 (ETB)
_CTYPE_ISCONTROL, // 18 (CAN)
_CTYPE_ISCONTROL, // 19 (EM)
_CTYPE_ISCONTROL, // 1A (SUB)
_CTYPE_ISCONTROL, // 1B (ESC)
_CTYPE_ISCONTROL, // 1C (FS)
_CTYPE_ISCONTROL, // 1D (GS)
_CTYPE_ISCONTROL, // 1E (RS)
_CTYPE_ISCONTROL, // 1F (US)
_CTYPE_ISSPACE+_CTYPE_ISBLANK, // 20 SPACE
_CTYPE_ISPUNCT, // 21 !
_CTYPE_ISPUNCT, // 22 "
_CTYPE_ISPUNCT, // 23 #
_CTYPE_ISPUNCT, // 24 $
_CTYPE_ISPUNCT, // 25 %
_CTYPE_ISPUNCT, // 26 &
_CTYPE_ISPUNCT, // 27 '
_CTYPE_ISPUNCT, // 28 (
_CTYPE_ISPUNCT, // 29 )
_CTYPE_ISPUNCT, // 2A *
_CTYPE_ISPUNCT, // 2B +
_CTYPE_ISPUNCT, // 2C ,
_CTYPE_ISPUNCT, // 2D -
_CTYPE_ISPUNCT, // 2E .
_CTYPE_ISPUNCT, // 2F /
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 30 0
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 31 1
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 32 2
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 33 3
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 34 4
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 35 5
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 36 6
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 37 7
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 38 8
_CTYPE_ISDIGIT+_CTYPE_ISHEX, // 39 9
_CTYPE_ISPUNCT, // 3A :
_CTYPE_ISPUNCT, // 3B ;
_CTYPE_ISPUNCT, // 3C <
_CTYPE_ISPUNCT, // 3D =
_CTYPE_ISPUNCT, // 3E >
_CTYPE_ISPUNCT, // 3F ?
_CTYPE_ISPUNCT, // 40 @
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 41 A
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 42 B
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 43 C
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 44 D
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 45 E
_CTYPE_ISUPPER+_CTYPE_ISHEX, // 46 F
_CTYPE_ISUPPER, // 47 G
_CTYPE_ISUPPER, // 48 H
_CTYPE_ISUPPER, // 49 I
_CTYPE_ISUPPER, // 4A J
_CTYPE_ISUPPER, // 4B K
_CTYPE_ISUPPER, // 4C L
_CTYPE_ISUPPER, // 4D M
_CTYPE_ISUPPER, // 4E N
_CTYPE_ISUPPER, // 4F O
_CTYPE_ISUPPER, // 50 P
_CTYPE_ISUPPER, // 51 Q
_CTYPE_ISUPPER, // 52 R
_CTYPE_ISUPPER, // 53 S
_CTYPE_ISUPPER, // 54 T
_CTYPE_ISUPPER, // 55 U
_CTYPE_ISUPPER, // 56 V
_CTYPE_ISUPPER, // 57 W
_CTYPE_ISUPPER, // 58 X
_CTYPE_ISUPPER, // 59 Y
_CTYPE_ISUPPER, // 5A Z
_CTYPE_ISPUNCT, // 5B [
_CTYPE_ISPUNCT, // 5C backslash
_CTYPE_ISPUNCT, // 5D ]
_CTYPE_ISPUNCT, // 5E ^
_CTYPE_ISPUNCT, // 5F _
_CTYPE_ISPUNCT, // 60 `
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 61 a
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 62 b
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 63 c
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 64 d
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 65 e
_CTYPE_ISLOWER+_CTYPE_ISHEX, // 66 f
_CTYPE_ISLOWER, // 67 g
_CTYPE_ISLOWER, // 68 h
_CTYPE_ISLOWER, // 69 i
_CTYPE_ISLOWER, // 6A j
_CTYPE_ISLOWER, // 6B k
_CTYPE_ISLOWER, // 6C l
_CTYPE_ISLOWER, // 6D m
_CTYPE_ISLOWER, // 6E n
_CTYPE_ISLOWER, // 6F o
_CTYPE_ISLOWER, // 70 p
_CTYPE_ISLOWER, // 71 q
_CTYPE_ISLOWER, // 72 r
_CTYPE_ISLOWER, // 73 s
_CTYPE_ISLOWER, // 74 t
_CTYPE_ISLOWER, // 75 u
_CTYPE_ISLOWER, // 76 v
_CTYPE_ISLOWER, // 77 w
_CTYPE_ISLOWER, // 78 x
_CTYPE_ISLOWER, // 79 y
_CTYPE_ISLOWER, // 7A z
_CTYPE_ISPUNCT, // 7B {
_CTYPE_ISPUNCT, // 7C +
_CTYPE_ISPUNCT, // 7D }
_CTYPE_ISPUNCT, // 7E ~
_CTYPE_ISCONTROL, // 7F (DEL)
// and the rest are 0...
};
int toupper(int c)
{
return ((islower(c)) ? (c-'a'+'A') : c);
}
int tolower(int c)
{
return((isupper(c)) ? (c-'A'+'a') : c);
}

286
SysCore/lib/stdlib.c Normal file
View File

@ -0,0 +1,286 @@
#include <stdlib.h>
#include <ctype.h>
const char base_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
void uitoa (unsigned int value, char *string, int radix)
{
if (radix == 1 || radix>36) return;
// Variables
int len; *string = '0';
unsigned int temp = value;
// Calculate string length needed
for (len = 0; temp > 0; len++) temp /= radix;
if (len == 0) len = 2;
// Last character is NULL
string[len] = 0;
// Write characters
for (len--; len >=0; len-- ) {
string[len] = base_chars[value%radix];
value/=radix;
}
}
void ultoa (unsigned long value, char *string, int radix)
{
if (radix == 1 || radix>36) return;
// Variables
int len; *string = '0';
unsigned long temp = value;
// Calculate string length needed
for (len = 0; temp > 0; len++) temp /= radix;
if (len == 0) len = 2;
// Last character is NULL
string[len] = 0;
// Write characters
for (len--; len >=0; len-- ) {
string[len] = base_chars[value%radix];
value/=radix;
}
}
void itoa (signed int value, char *string, int radix)
{
if (radix == 1 || radix>36) return;
// Variables
int len = 0; *string = '0';
unsigned int copy = value;
// If number is < 0
if (value < 0) {
if (radix == 10) {
len++; copy = abs(value);
}
else copy = (unsigned) value; // If base is not 10, set high bit
}
// Calculate string length needed
unsigned int temp = copy;
for (; temp > 0; len++) temp /= radix;
if (len == 0) len = 2;
// Last character is NULL
string[len] = 0;
// Write characters
for (len--; len >= 0; len-- ) {
string[len] = base_chars[copy%radix];
copy/=radix;
}
// Add minus sign
if (value < 0 && radix == 10) string[0] = '-';
}
void ltoa (signed long value, char *string, int radix)
{
if (radix == 1 || radix>36) return;
// Variables
int len = 0; *string = '0';
unsigned long copy = value;
// If number is < 0
if (value < 0) {
if (radix == 10) {
len++; copy = abs(value);
}
else copy = (unsigned) value; // If base is not 10, set high bit
}
// Calculate string length needed
unsigned long temp = copy;
for (; temp > 0; len++) temp /= radix;
if (len == 0) len = 2;
// Last character is NULL
string[len] = 0;
// Write characters
for (len--; len >= 0; len-- ) {
string[len] = base_chars[copy%radix];
copy/=radix;
}
// Add minus sign
if (value < 0 && radix == 10) string[0] = '-';
}
int atoi (const char* string)
{
int ret = 0;
unsigned char sign = 0;
for (;!isdigit((unsigned char)*string); string++) {
if (*string == NULL) return 0;
else if (*string == '-' && isdigit(*(string+1)))
sign = 1;
}
for (;isdigit(*string); string++)
ret = ret*10 + (*string - '0');
if (sign) ret*=-1;
return ret;
}
long atol (const char* string)
{
long int ret = 0;
unsigned char sign = 0;
for (;!isdigit(*string); string++) {
if (*string == NULL) return 0;
else if (*string == '-' && *(string+1) > '0' && *(string+1) < '9')
sign = 1;
}
for (;isdigit(*string); string++)
ret = ret*10 + (*string - '0');
if (sign) ret*=-1;
return ret;
}
unsigned int atox (const char* string)
{
unsigned ret = 0;
unsigned temp;
for (;!isxdigit(*string); string++)
if (*string == 0) return 0;
for (;isxdigit(*string); string++) {
if (isdigit(*string)) temp = (unsigned)*string - '0';
else if (isupper(*string)) temp = 10 + (unsigned)(*string) - 'A';
else temp = 10 + (unsigned)(*string) - 'a';
ret = ret*0x10 + temp;
}
return ret;
}
void* bsearch (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*))
{
int beg = 0, end = nelem, mid, result;
unsigned addr;
while (beg != end && beg != end-1) {
mid = (beg + end) / 2;
addr = (unsigned)base + (mid * width);
result = (*fcmp)(key, (void*) addr);
if (result == 0) return (void*) addr;
else if (result > 0) beg = mid;
else end = mid;
}
return 0;
}
void* lfind (const void* key, const void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*))
{
int result = 1, i = 0;
while (result != 0 && i != nelem) {
base = (void*)((unsigned)base + width);
result = (*fcmp)(key, base);
i++;
}
if (result == 0) return (void*) base;
return 0;
}
div_t div (int numerator, int denominator)
{
div_t ret;
ret.quot = numerator / denominator;
ret.rem = numerator % denominator;
return ret;
}
ldiv_t ldiv (long numerator, long denominator)
{
ldiv_t ret;
ret.quot = numerator / denominator;
ret.rem = numerator % denominator;
return ret;
}
inline void __qassign (void *dest, void *source, unsigned width)
{
unsigned char* dst = (unsigned char*)dest;
unsigned char* src = (unsigned char*)source;
int i;
for (i = 0; i < width; i++, dst++, src++)
*dst = *src;
}
void __qsort(void* base, unsigned width, int (*fcmp)(const void*, const void*), int beg, int end)
{
unsigned char piv_str[width];
unsigned char tmp_str[width];
void* piv = (void*) piv_str; void* tmp = (void*) tmp_str;
int l,r,p;
while (beg<end) // This while loop will avoid the second recursive call
{
l = beg; p = (beg+end)/2; r = end;
__qassign(piv, (void*) ((unsigned)base + (width * p)), width);
//piv = (void*) ((unsigned)base + (width * p));
while (1)
{
while ( (l<=r) && ( (*fcmp)( (void*) ((unsigned)base + (width * l)) ,piv) <= 0 ) ) l++;
while ( (l<=r) && ( (*fcmp)( (void*) ((unsigned)base + (width * r)) ,piv) > 0 ) ) r--;
if (l>r) break;
__qassign (tmp, (void*) ((unsigned)base + (width * l)), width);
__qassign ((void*) ((unsigned)base + (width * l)), (void*) ((unsigned)base + (width * r)), width);
__qassign ((void*) ((unsigned)base + (width * r)), tmp, width);
//tmp=base[l]; base[l]=base[r]; base[r]=tmp;
if (p==r) p=l;
l++; r--;
}
__qassign((void*) ((unsigned)base + (width * p)), (void*) ((unsigned)base + (width * r)), width);
__qassign((void*) ((unsigned)base + (width * r)), piv, width);
//base[p]=base[r]; base[r]=piv;
r--;
// Recursion on the shorter side & loop (with new indexes) on the longer
if ((r-beg)<(end-l))
{
__qsort(base, width, *fcmp, beg, r);
beg=l;
}
else
{
__qsort(base, width, *fcmp, l, end);
end=r;
}
}
}
void qsort (void* base, unsigned nelem, unsigned width, int (*fcmp)(const void*, const void*))
{
__qsort(base, width, *fcmp, 0, nelem-1);
}

View File

@ -1,8 +1,8 @@
#include <string.h> #include <string.h>
size_t strlen (const char *str) unsigned strlen (const char *str)
{ {
size_t i; unsigned i;
for (i = 0; *str!=0; str++) i++; for (i = 0; *str!=0; str++) i++;
return i; return i;
} }

View File

@ -4,7 +4,7 @@
#include <system.h> #include <system.h>
byte *TextVideoRam; extern unsigned char *TextVideoRam;
volatile int cursor_x, cursor_y; volatile int cursor_x, cursor_y;
int current_mode_width; int current_mode_width;
int current_mode_height; int current_mode_height;
@ -32,13 +32,13 @@ unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
} }
byte inportb (word _port) { unsigned char inportb (word _port) {
byte rv; unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port)); __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv; return rv;
} }
byte inb (word _port) { unsigned char inb (word _port) {
byte rv; unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port)); __asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv; return rv;
} }

View File

@ -1,13 +1,13 @@
#include <system.h> #include <system.h>
#include <time.h> #include <time.h>
const char* clock_months[] = {0, const char* clock_month[] = {0,
"January", "February", "March", "April", "May", "June", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"}; "July", "August", "September", "October", "November", "December"};
const char* clock_weekdays[] = {0, const char* clock_weekday[] = {0,
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
byte clock_months_len[] = { unsigned char clock_months_len[] = {
0, 0,
31, // January 31, // January
28, // February 28, // February
@ -26,14 +26,14 @@ byte clock_months_len[] = {
void _CLOCK_INC(TIME *tim) void _CLOCK_INC(TIME *tim)
{ {
// New minute // New minute
if (++tim->seconds > 59) { if (++tim->second > 59) {
tim->seconds = 0; tim->second = 0;
// New hour // New hour
if (++tim->minutes > 59) { if (++tim->minute > 59) {
tim->minutes = 0; tim->minute = 0;
tim->hours++; tim->hour++;
if (tim->hours == 12 && tim->am_pm == 1) { // 11:59pm -> 0:00am if (tim->hour == 24) { // Midnight
tim->hours = 0; tim->am_pm = 0; tim->hour = 0;
// New day // New day
tim->weekday = 1+(tim->weekday%7); tim->weekday = 1+(tim->weekday%7);
// Leap years // Leap years
@ -49,8 +49,6 @@ void _CLOCK_INC(TIME *tim)
} }
} }
} }
else if (tim->hours == 12 && tim->am_pm == 0) tim->am_pm = 1; // 11:59am -> 12:00pm
else if (tim->hours == 13 && tim->am_pm == 1) tim->hours = 1; // 12:59pm -> 1:59pm
} }
} }
} }
@ -69,7 +67,7 @@ void _CLOCK_INC(TIME *tim)
{ {
char str[100]; char str[100];
int l = strlen(format), i; int l = strlen(format), i;
byte special = 0; unsigned char special = 0;
for (i=0; i<l-1 && i<100; i++) { for (i=0; i<l-1 && i<100; i++) {
if (format[i] == '%') switch (format[i+1]) { if (format[i] == '%') switch (format[i+1]) {

View File

@ -4,7 +4,7 @@ global start
start: jmp stub start: jmp stub
stub: cmp eax, 0x2badb002 stub: cmp eax, 0xC0DEcC7A
jne .bad jne .bad
mov ax, 10h mov ax, 10h
@ -19,8 +19,6 @@ stub: cmp eax, 0x2badb002
.bad: .bad:
cli cli
hlt hlt
SECTION .bss SECTION .bss
resb 8192 ; This reserves 8KBytes of memory here resb 8192 ; This reserves 8KBytes of memory here

View File

@ -2,79 +2,71 @@
#include <system.h> #include <system.h>
#include <conio.h> #include <conio.h>
#include <hal.h> #include <hal.h>
#include "hal/floppy/floppy.h"
#include <time.h> #include <time.h>
#include <bootinfo.h> #include <bootinfo.h>
#include "memory/mmngr_ph.h" #include "memory/mmngr_ph.h"
#include "memory/mmngr_vi.h" #include "memory/mmngr_vi.h"
#include "shell/shell.c" #include "video/vga03h.h"
//! format of a memory region #include <stdlib.h>
// format of a memory region
typedef struct { typedef struct {
unsigned startLo;
uint32_t startLo; unsigned startHi;
uint32_t startHi; unsigned sizeLo;
uint32_t sizeLo; unsigned sizeHi;
uint32_t sizeHi; unsigned type;
uint32_t type; unsigned acpi_3_0;
uint32_t acpi_3_0;
} memory_region ; } memory_region ;
//! different memory regions (in memory_region.type) // different memory regions (in memory_region.type)
char* strMemoryTypes[] = { /*char* strMemoryTypes[] = {
"", "",
"Available", //memory_region.type==0 "Available", //memory_region.type==0
"Reserved", //memory_region.type==1 "Reserved", //memory_region.type==1
"ACPI Reclaim", //memory_region.type==2 "ACPI Reclaim", //memory_region.type==2
"ACPI NVS Memory" //memory_region.type==3 "ACPI NVS Memory" //memory_region.type==3
}; };*/
extern dword _code; extern void _code, _data, _bss, _end;
extern dword _data;
extern dword _bss;
extern dword _end;
void k_init(multiboot_info* bootinfo) void k_init(multiboot_info* bootinfo)
{ {
// Start by zero-ing the bss memset(&_bss, 0, &_end - &_bss); // zero the bss
memset(&_bss, 0, &_end - &_bss);
// Initialize HAL
i86_hal_initialize();
// Initialize Graphics
graphics_init();
// cleanup the console screen
set_default_colors (0x07, 0x04);
clrscr();
// Start memory manager
uint32_t memSize = 1024 + bootinfo->m_memoryLo + bootinfo->m_memoryHi*64;
memory_region* memMap = (memory_region*)0x1000;
pmmngr_init (memSize, (uint32_t)&_end);
unsigned int i; unsigned int i;
for (i=0; (memMap[i].sizeHi != 0 || memMap[i].sizeLo != 0) && i<15; ++i) // Start phyiscal memory manager
if (memMap[i].type==1) pmmngr_init_region (memMap[i].startLo, memMap[i].sizeLo); unsigned memSize = bootinfo->Memory;
// Protect kernel, bios data area etc
_end += (memSize / 4) * 3;
pmmngr_deinit_region (0x100000, _end - 0x100000);
pmmngr_deinit_region (0x0, 0x500);
pmmngr_deinit_region (0x9FC00, 0x400);
pmmngr_deinit_region (0xA0000, 0x60000);
// Start Virtual Memory Manager & Enable paging memory_region* memMap = bootinfo->MemoryMapAddress;
pmmngr_init (memSize, (unsigned)&_end);
// Initialize graphics & HAL
//graphics_init();
i86_hal_initialize();
// Initialize memory
for (i=0; (memMap[i].sizeHi != 0 || memMap[i].sizeLo != 0) && i<15; ++i)
if (memMap[i].type==1)
pmmngr_init_region (memMap[i].startLo, memMap[i].sizeLo);
// Protect kernel, bios data area etc
pmmngr_deinit_region (0x100000, 4096+(unsigned)((&_end) + (memSize / 4)*3) - 0xC0000000);
pmmngr_deinit_region (0x0, 0x500); // IVT, Bios Data Area
// Initialize virtual mem manager
vmmngr_initialize(); vmmngr_initialize();
} }
void k_main(uint32_t kernel_size, multiboot_info* bootinfo)
{
k_init(bootinfo);
apps_osver();
shell();
for(;;); extern void shell();
void k_main(unsigned kernel_size, multiboot_info* bootinfo)
{
k_init(bootinfo);
vga03h_install();
shell();
for(;;);
} }

View File

@ -33,14 +33,24 @@ goto KernelEntry
:KernelSTDLIB :KernelSTDLIB
cd lib cd lib
rem call compile.bat call compile.bat
cd.. cd..
:KernelMemoryManager :KernelMemoryManager
cd memory cd memory
rem call compile.bat
cd..
:KernelVIDEO
cd video
call compile.bat call compile.bat
cd.. cd..
:KernelSHELL
cd shell
call compile.bat
cd..
rem here go other sources: rem here go other sources:
rem here go other sources ^ rem here go other sources ^

View File

@ -48,6 +48,16 @@ goto KernelEntry
call makeall.bat call makeall.bat
cd.. cd..
:KernelVIDEO
cd video
call compile.bat
cd..
:KernelSHELL
cd shell
call compile.bat
cd..
rem here go other sources: rem here go other sources:
rem here go other sources ^ rem here go other sources ^

View File

@ -22,4 +22,24 @@ void pt_entry_add_attrib (pt_entry* entry, unsigned mask) {
unsigned char pt_entry_is_writable (pt_entry entry) { unsigned char pt_entry_is_writable (pt_entry entry) {
return (entry & _I86_PTE_WRITABLE); return (entry & _I86_PTE_WRITABLE);
} }
/*#include <conio.h>
void pt_entry_print(pt_entry entry)
{
cprintf ("page <FRAME=%u", entry & _I86_PTE_FRAME);
if (entry & _I86_PTE_PRESENT) cprintf (" PRESENT");
if (entry & _I86_PTE_WRITABLE) cprintf (" WRITABLE");
if (entry & _I86_PTE_USER) cprintf (" USER");
if (entry & _I86_PTE_WRITETHROUGH) cprintf (" WRITETHROUGH");
if (entry & _I86_PTE_NOT_CACHEABLE) cprintf (" NOT-CACHEABLE");
if (entry & _I86_PTE_ACCESSED) cprintf (" ACCESSED");
if (entry & _I86_PTE_DIRTY) cprintf (" DIRTY");
if (entry & _I86_PTE_PAT) cprintf (" PAT");
if (entry & _I86_PTE_CPU_GLOBAL) cprintf (" CPU-GLOBAL");
if (entry & _I86_PTE_LV4_GLOBAL) cprintf (" LV4-GLOBAL");
cprintf(">");
}*/

View File

@ -24,4 +24,6 @@ extern unsigned pt_entry_get_frame (pt_entry entry);
extern unsigned char pt_entry_is_present (pt_entry entry); extern unsigned char pt_entry_is_present (pt_entry entry);
extern unsigned char pt_entry_is_writable (pt_entry entry); extern unsigned char pt_entry_is_writable (pt_entry entry);
extern void pt_entry_print(pt_entry entry);
#endif #endif

View File

@ -21,6 +21,7 @@ _read_cr3:
global _write_cr3 global _write_cr3
_write_cr3: _write_cr3:
;xchg bx, bx ; bochs magic breakpoint
push ebp push ebp
mov ebp, esp mov ebp, esp
mov eax, [ebp+8] mov eax, [ebp+8]
@ -34,4 +35,4 @@ _vmmngr_flush_tbl_entry:
cli cli
invlpg [eax] invlpg [eax]
sti sti
retn retn

View File

@ -1,6 +1,9 @@
/****************************************************** /*********************************************************************
* Physical Memory Manager * * (c) 2010 CTA Systems Inc. All rights reserved. Glory To God *
******************************************************/ * *
* Physical Memory Manager *
* ======================= *
************************************************************ cta os */
// +==============================================+ // +==============================================+
// | HEADERS | // | HEADERS |
@ -129,7 +132,7 @@ void mstack_qsort(int beg, int end)
/*void print_stack() /*void print_stack()
{ {
int i; int i;
for (i = 0; i < _mmngr_index; i++) printf (" %u", _mmngr_memory_stack[i].low); for (i = 0; i < _mmngr_index; i++) \n\r (" %u", _mmngr_memory_stack[i].low);
} }
extern char getch();*/ extern char getch();*/
@ -285,15 +288,15 @@ void* pmmngr_alloc_blocks (unsigned blocks)
unsigned prev = _mmngr_memory_stack[i].low | (_mmngr_memory_stack[i].high<<16); --i; unsigned prev = _mmngr_memory_stack[i].low | (_mmngr_memory_stack[i].high<<16); --i;
// Search consecutive blocks // Search consecutive blocks
for (; i >=0; i--) { for (i = 0; i < _mmngr_index; i++) {
temp = _mmngr_memory_stack[i].low | (_mmngr_memory_stack[i].high<<16); temp = _mmngr_memory_stack[i].low | (_mmngr_memory_stack[i].high<<16);
if (temp == prev-1) l++; if (temp == prev+1) l++;
else l = 1; else l = 1;
if (l == blocks) { if (l == blocks) {
pmmngr_deinit_region (temp * PMMNGR_BLOCK_SIZE, blocks * PMMNGR_BLOCK_SIZE); pmmngr_deinit_region (temp * PMMNGR_BLOCK_SIZE, blocks * PMMNGR_BLOCK_SIZE);
return (void*) (temp * PMMNGR_BLOCK_SIZE); return (void*) ((temp-l+1) * PMMNGR_BLOCK_SIZE);
} }
prev = temp; prev = temp;

View File

@ -5,16 +5,18 @@
#include "mmngr_ph.h" #include "mmngr_ph.h"
// +==============================================+ // +==============================================+
// | DEFINITIONS | // | DEFINITIONS & MACROS |
// +===================================== cta os =+ // +===================================== cta os =+
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
#define PTABLE_ADDR_SPACE_SIZE 0x400000 #define PTABLE_ADDR_SPACE_SIZE 0x400000
#define DTABLE_ADDR_SPACE_SIZE 0xffffffff #define DTABLE_ADDR_SPACE_SIZE 0xffffffff
#define PAGE_DIRECTORY_INDEX(x) (((x) >> 22) & 0x3ff)
#define PAGE_TABLE_INDEX(x) (((x) >> 12) & 0x3ff)
#define PAGE_GET_PHYSICAL_ADDRESS(x) (*x & ~0xfff)
pdirectory* _current_directory; pdirectory* _current_directory;
unsigned _current_page_directory_base_register; unsigned _current_page_directory_base_register;
extern unsigned char *memset (unsigned char *dest, unsigned char val, int count); extern unsigned char *memset (unsigned char *dest, unsigned char val, int count);
extern char getch();
// +==============================================+ // +==============================================+
// | PAGE FUNCTIONS | // | PAGE FUNCTIONS |
@ -95,39 +97,88 @@ pdirectory* vmmngr_get_directory() {
return _current_directory; return _current_directory;
} }
void vmmngr_initialize()
unsigned char vmmngr_map_page (unsigned phys, unsigned virt)
{ {
// Allocate default page table pdirectory* dir = _current_directory; // get page directory
ptable* table = (ptable*) pmmngr_alloc_block(); pd_entry* e = &dir->m_entries [PAGE_DIRECTORY_INDEX (virt)]; // get page table
if (!table) return;
// Clear page table if ((*e & _I86_PTE_PRESENT) != _I86_PTE_PRESENT) {
// Page table not present, allocate it
ptable* table = (ptable *)pmmngr_alloc_block();
if (!table) return 0;
// Clear it
vmmngr_ptable_clear(table);
// Create new entry
pd_entry* entry = &dir->m_entries [PAGE_DIRECTORY_INDEX (virt)];
// Map in the table
pd_entry_add_attrib (entry, _I86_PDE_PRESENT);
pd_entry_add_attrib (entry, _I86_PDE_WRITABLE);
pd_entry_set_frame (entry, (unsigned) table);
}
// get table
ptable* table = (ptable*) PAGE_GET_PHYSICAL_ADDRESS(e);
// get page
pt_entry* page = &table->m_entries [PAGE_TABLE_INDEX (virt)];
pt_entry_set_frame (page, phys);
pt_entry_add_attrib (page, _I86_PTE_PRESENT);
return 1;
}
unsigned char vmmngr_initialize()
{
unsigned int i, virt, frame;
// Allocate default page & directory table(s)
ptable* table = (ptable*) pmmngr_alloc_block();
ptable* table2 = (ptable*) pmmngr_alloc_block();
pdirectory* dir = (pdirectory*) pmmngr_alloc_blocks(3);
if (!table || !table2 || !dir) return 0;
// Clear page & directory tables
vmmngr_ptable_clear(table); vmmngr_ptable_clear(table);
vmmngr_ptable_clear(table2);
vmmngr_pdirectory_clear(dir);
// Identity map the first page table // Identity map the first page table
int i, frame; virt = 0; frame = 0;
for (i = 0, frame = 0; i < 1024; i++, frame += 4096) for (i = 0; i < 1024; i++, virt+=4096, frame += 4096)
{ {
// Create a new page pt_entry* page = &table2->m_entries[PAGE_TABLE_INDEX(virt)];
pt_entry page = 0; *page = 0;
pt_entry_add_attrib (&page, _I86_PTE_PRESENT); pt_entry_add_attrib (page, _I86_PTE_PRESENT);
pt_entry_set_frame (&page, frame); pt_entry_set_frame (page, frame);
table->m_entries[vmmngr_ptable_virt_to_index(frame)] = page;
} }
// Create default directory table // Map 0x100000 to 0xC0000000
pdirectory* dir = (pdirectory*) pmmngr_alloc_blocks(3); virt = 0xC0000000; frame = 0x100000;
if (!dir) return; for (i = 0; i < 1024; i++, virt+=4096, frame += 4096)
{
// Clear directory table and set it as current pt_entry* page = &table->m_entries[PAGE_TABLE_INDEX(virt)];
vmmngr_pdirectory_clear(dir); *page = 0;
pt_entry_add_attrib (page, _I86_PTE_PRESENT);
// Get first entry in dir table and set it up to point to our table pt_entry_set_frame (page, frame);
pd_entry* entry = vmmngr_pdirectory_lookup_entry(dir, 0); }
// Get tables in dir table and point them to our tables
pd_entry* entry = &dir->m_entries [PAGE_DIRECTORY_INDEX (0xC0000000)];
pd_entry_add_attrib (entry, _I86_PDE_PRESENT); pd_entry_add_attrib (entry, _I86_PDE_PRESENT);
pd_entry_add_attrib (entry, _I86_PDE_WRITABLE); pd_entry_add_attrib (entry, _I86_PDE_WRITABLE);
pd_entry_set_frame (entry, (unsigned) table); pd_entry_set_frame (entry, (unsigned) table);
entry = &dir->m_entries [PAGE_DIRECTORY_INDEX (0x00000000)];
pd_entry_add_attrib (entry, _I86_PDE_PRESENT);
pd_entry_add_attrib (entry, _I86_PDE_WRITABLE);
pd_entry_set_frame (entry, (unsigned) table2);
// Store current PDBR // Store current PDBR
_current_page_directory_base_register = (unsigned) &dir->m_entries; _current_page_directory_base_register = (unsigned) &dir->m_entries;
@ -137,4 +188,6 @@ void vmmngr_initialize()
// Enable paging // Enable paging
pmmngr_paging_enable (1); pmmngr_paging_enable (1);
return 1;
} }

View File

@ -20,6 +20,7 @@ typedef struct {
//extern pdirectory* _current_directory; //extern pdirectory* _current_directory;
extern void vmmngr_flush_tbl_entry (unsigned addr); extern void vmmngr_flush_tbl_entry (unsigned addr);
extern void vmmngr_initialize(); extern unsigned char vmmngr_initialize();
extern unsigned char vmmngr_map_page(unsigned phys, unsigned virt);
#endif #endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
SysCore/objects/CTYPE.O Normal file

Binary file not shown.

BIN
SysCore/objects/DMA.O Normal file

Binary file not shown.

BIN
SysCore/objects/FLOPPY.O Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
SysCore/objects/SHELL.O Normal file

Binary file not shown.

BIN
SysCore/objects/STDLIB.O Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -6,6 +6,9 @@ INPUT("loader.o",
"cmos.o", "cmos.o",
"conio.o", "conio.o",
"cpu.o", "cpu.o",
"ctype.o",
"dma.o",
"floppy.o",
"gdt.o", "gdt.o",
"gdt_asm.o", "gdt_asm.o",
"hal.o", "hal.o",
@ -23,12 +26,15 @@ INPUT("loader.o",
"keyus.o", "keyus.o",
"pic.o", "pic.o",
"pit.o", "pit.o",
"shell.o",
"stdlib.o",
"string.o", "string.o",
"system.o", "system.o",
"time.o" "time.o",
"video/vga03h.o"
) )
OUTPUT(kernel.bin) OUTPUT(kernel.bin)
phys = 0x00100000; phys = 0xC0000000;
SECTIONS SECTIONS
{ {
.text phys : AT(phys) { .text phys : AT(phys) {

Binary file not shown.

Binary file not shown.

View File

@ -1,69 +1,40 @@
#define hex(x) (x < 10) ? x+'0' : x-10+'a'
const char *apps_lst[] = { const char *apps_lst[] = {
"", /*0*/ "",
"reboot", /*1*/ "reboot",
"osver", /*2*/ "osver",
"time", /*3*/ "time",
"place", /*4*/ "place",
"cls", /*5*/ "cls",
"memory", /*6*/ "dump",
"help", /*7*/ "help",
"cpu_info", /*8*/ "cpuid",
"memstat" /*9*/ "memstat",
/*A*/ "read"
}; };
int apps_count = 10; int apps_count = 11;
unsigned int atox(const char* str)
{
unsigned int temp = 0;
int i; unsigned int digit = 0;
for (i = 0; str[i]!=0; i++) {
if (str[i] >= '0' && str[i] <= '9') digit = str[i] - '0';
else if (str[i] >= 'A' && str[i] <= 'F') digit = str[i] - 'A' + 10;
else if (str[i] >= 'a' && str[i] <= 'f') digit = str[i] - 'a' + 10;
else break;
temp = temp*16 + digit;
}
return temp;
}
unsigned int atoui(const char* str)
{
unsigned int temp = 0;
int i;
for (i = 0; str[i] >= '0' && str[i] <= '9' ; i++)
temp = temp*10 + (str[i] - '0');
return temp;
}
void apps_osver() void apps_osver()
{ {
printf ("CTA 32bit Operating System v0.1"); cprintf ("%#0BC%#0CT%#0AA %#0F32bit Operating System v0.1\n\r");
printf ("\n(c) CTA 2010\n"); cprintf ("(c) CTA 2010\n\r");
} }
void apps_time() void apps_time()
{ {
printf ("Today is %s, %u of %s, %u%u.\n", clock_weekdays[_internal_clock.weekday], TIME _internal_clock = i86_pit_get_time();
(unsigned int) _internal_clock.day, clock_months[_internal_clock.month], cprintf ("Today is %#0F%s%#07, %#0F%u %#07of %#0F%s%#07, %#0F%u%u%#07.\n\r", clock_weekday[_internal_clock.weekday],
(unsigned int) _internal_clock.century, (unsigned int) _internal_clock.year); (unsigned) _internal_clock.day, clock_month[_internal_clock.month],
(unsigned) _internal_clock.century, (unsigned) _internal_clock.year);
printf ("Now is %u:%u:%u.\n", (unsigned int) _internal_clock.hours, cprintf ("%#07Now is %#0F%u%#87:%#0F%u%#87:%#0F%u%#07.\n\r", (unsigned) _internal_clock.hour,
(unsigned int) _internal_clock.minutes, (unsigned int) _internal_clock.seconds); (unsigned) _internal_clock.minute, (unsigned) _internal_clock.second);
} }
void apps_place() void apps_place()
{ {
printf ("On your desk, if you didn't notice... \n"); cprintf ("%#0FOn your desk, %#07if you didn't notice... \n\r");
} }
void apps_clrscr() void apps_clrscr()
@ -71,66 +42,112 @@ void apps_clrscr()
clrscr(); clrscr();
} }
void apps_memory(const int pn, const char* param[]) void apps_dump(const int pn, const char* param[])
{ {
if (pn<3) { if (pn<3) {
printf ("Correct syntax: memory [start_address] [end_address] (in hex)\n"); cprintf ("%#0CCorrect syntax: %#07dump %#0F[start_address] %#0F[end_address] %#0C(in hex)\n\r");
return; return;
} }
byte *start, *end; unsigned char *start, *end;
start = (byte *) atox (param[1]); start = (unsigned char *) atox (param[1]);
end = (byte *) atox (param[2]); end = (unsigned char *) atox (param[2]);
byte* count; unsigned char* count;
while (start <= end) { while (start <= end) {
put_hex ((unsigned int) start); puts(": "); cprintf("%#0D%x%#07: ", (unsigned int) start);
for (count = start; count < start+16; count++) { for (count = start; count < start+16; count++) {
putc(hex[*count/16]); putc(hex[*count%16]); if (*count == 0) cprintf ("%#0800 ");
putc(' '); else cprintf ("%#0F%c%c ", hex(*count/16), hex(*count%16));
} }
puts(" ");
cprintf(" ");
for (count = start; count < start+16; count++) { for (count = start; count < start+16; count++) {
if (*count < 32) putc('.'); if (*count < 32) cprintf(".");
else putc(*count); else cprintf("%#0A%c", *count);
} }
putc('\n'); cprintf("\n\r");
start+=16; start+=16;
} }
}
int apps_help_sort(const void* a, const void* b)
{
return strcmp(apps_lst[*(short *)a], apps_lst[*(short *)b]);
} }
void apps_help(const int pn, const char* param[]) void apps_help(const int pn, const char* param[])
{ {
short arr[apps_count];
int i; int i;
puts ("\n");
for (i = 0; i < apps_count; i++)
arr[i] = i;
qsort((void*)arr, apps_count, sizeof(short), apps_help_sort);
if (pn==1) { if (pn==1) {
puts("[BeTA]\n"); cprintf("%#0BC%#0CT%#0AA %#0FShell %#07commands:\n\r");
puts("Available commands:"); for (i = 1; i < apps_count; i++)
for (i = 1; i < apps_count; i++) { cprintf("%#0F\t%c %s\n\r", 0x7 ,apps_lst[arr[i]]);
puts("\n \t "); puts((char*)apps_lst[i]);
} cprintf("\n\rUse help %#0E[command]%#07 for help on individual commands.\n\r");
puts("\n\nUse help [command] for help on individual commands.\n");
return; return;
} }
for (i = 0; strcmp(apps_lst[i], param[1])!=0 && i<apps_count; i++); for (i = 0; strcmp(apps_lst[i], param[1])!=0 && i<apps_count; i++);
puts("[BeTA]\nShowing help for command: "); puts((char*)apps_lst[i]); cprintf("\n\r[%#0Bc%#0Ct%#0Aa %#0APreALphA%#07] Showing help for command: %#0E%s", apps_lst[i]);
// DESCRIPTION:
cprintf ("\n\r%#0FDescription:\t");
switch (i) { switch (i) {
case 0: break; case 1: cprintf("Restarts the system."); break;
case 1: puts("\n\nReboots the system.\nUsage: reboot"); break; case 2: cprintf("Shows information about the operating system."); break;
case 2: puts("\n\nShows information about the current operating system.\nUsage: osver"); break; case 3: cprintf("Displays the current time and date."); break;
case 3: puts("\n\nDisplays the current date.\nUsage: date"); break; case 4: cprintf("%#08A little easter egg (shhhh... don't tell anyone."); break;
case 5: puts("\n\nClears the screen.\nUsage: cls");break; case 5: cprintf("Clears the screen.");break;
case 6: puts("\n\nShows the content of memory in the interval specified.\nUsage: memory [start_address] [end_address]");break; case 6: cprintf("Shows the virtual memory content between the specified\n\r\t\taddresses. ");break;
case 7: puts("\n\nShows a list of commands, or help for a specified command.\nUsage: help [opt:command]"); break; case 7: cprintf("Displays instructions for each command, or the list of\n\r\t\tcommands if none specified."); break;
default: puts("Invalid function: "); puts((char*)param[1]); case 8: cprintf("Shows the CPU's vendor."); break;
break; case 9: cprintf("Displays the current status of the memory, reported by\n\r\t\tthe memory manager."); break;
case 0xA: cprintf ("Reads one sector from floppy disk."); break;
default: cprintf("%#0CInvalid function, or help not implemented. "); break;
} }
puts("\n\n");
// USAGE:
cprintf ("\n\r%#0FUsage:\t\t");
switch (i) {
case 1: cprintf("reboot"); break;
case 2: cprintf("osver"); break;
case 3: cprintf("time"); break;
case 4: cprintf("%#08Nobody knows. And %#07nobody %#08should ever do."); break;
case 5: cprintf("cls"); break;
case 6: cprintf("dump %#0E[start] [end]"); break;
case 7: cprintf("help %#0E[command]"); break;
case 8: cprintf("cpuid"); break;
case 9: cprintf("memstat"); break;
case 0xA: cprintf("read %#0e[sector]"); break;
default: cprintf("%#0CInvalid function, or help not implemented. "); break;
}
// NOTES:
cprintf ("\n\r%#0FNotes:\t");
switch (i) {
case 1: cprintf("\t%c Use wisely.", 0x7); break;
case 2: cprintf("%#08\t%c (None)", 0x7); break;
case 3: cprintf("%#08\t%c (None)", 0x7); break;
case 4: cprintf("%#08\t%c (None)", 0x7); break;
case 5: cprintf("%#08\t%c (None)", 0x7); break;
case 6: cprintf("\t%c %#0E[start]%#07 and %#0E[end]%#07 are virtual addresses in %#0Ahexadecimal%#07.", 0x7); break;
case 7: cprintf("\t%c The %#0E[command]%#07 parameter is optional.", 0x7); break;
case 8: cprintf("\t%c Shows CPU vendor returned by CPUID function.", 0x7); break;
case 9: cprintf("%#08\t%c (None)", 0x7); break;
case 0xA: cprintf("\t%c Sector is in decimal, starts from 0.", 0x7); break;
default: cprintf("%#0C\t%c Invalid function, or help not implemented. ", 0x7); break;
}
cprintf("\n\r");
} }
@ -138,8 +155,20 @@ extern void detect_cpu();
void apps_memory_status() void apps_memory_status()
{ {
printf ("Memory available: %u KB \n", pmmngr_get_memory_size ()); cprintf ("Memory available: %#0F%u KB \n\r", pmmngr_get_memory_size ());
printf ("Total blocks: %u Used: %u Free: %u\n", pmmngr_get_block_count (), pmmngr_get_use_block_count (), pmmngr_get_free_block_count ()); cprintf ("Total blocks: %#0F%u %#07Used: %#0F%u %#07Free: %#0F%u\n\r", pmmngr_get_block_count (), pmmngr_get_use_block_count (), pmmngr_get_free_block_count ());
printf ("Block size: %u bytes\nPaging is ", pmmngr_get_block_size()); cprintf ("Block size: %#0F%u %#07bytes\n\rPaging is ", pmmngr_get_block_size());
printf ( pmmngr_is_paging() ? "enabled.\n" : "disabled.\n"); cprintf ( pmmngr_is_paging() ? "%#0Aenabled%#07.\n\r" : "%#0Cdisabled%#07.\n\r");
} }
void apps_read(const int pn, const char *param[])
{
if (pn <= 1) {
cprintf ("%#0CParameter missing: %#0F[sectorLBA]\n\r");
return;
}
cprintf ("Sector read, located at address %#0F0x%x.\n\r", i86_read_sector ((unsigned*) 0x7E00, 0, atoi(param[1])));
}

26
SysCore/shell/compile.bat Normal file
View File

@ -0,0 +1,26 @@
@echo off
rem NASM and DJGPP executable paths:
set nasm_path=C:\nasm
set djgpp_path=C:\DJGPP\bin
set objpath=..\objects
set incpath=../include
@echo Building CTA Shell...
del %objpath%\shell.o
goto build
:error
@echo.
@echo There have been build errors. Building halted.
@pause
exit
:build
@echo * Compiling ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/shell.o shell.c
:check
if not exist %objpath%\shell.o goto error

View File

@ -1,82 +1,52 @@
#include <conio.h> #include <conio.h>
#include <hal.h>
#include <stdlib.h>
#include <string.h>
#include "../memory/mmngr_ph.h"
#include "apps.h" #include "apps.h"
void get_str(char *str, int len)
{
kb_key alpha;
int i;
for (i = 0; i<len-1 ; i++) {
text_mode_cursor(cursor_x, cursor_y);
alpha = getkey();
switch (alpha.character) {
case 0x00: --i; break; // Ignore null characters
case 0x7F: --i; break;
case '\b': // Backspace
if (i>0) { // Only backspace our string
if (--cursor_x < 0) { // Begin of row - 1 = End of previous row
cursor_x = 79; cursor_y--;
}
putc_pos(cursor_x, cursor_y, 0);
str[--i] = 0;
}
i--; break;
case '\n': str[i]=0; putc('\n'); return;
default: putc(alpha.character);
str[i] = alpha.character;
str[i+1] = 0;
break;
}
}
}
void shell() void shell()
{ {
apps_osver();
char str[256]; char str[256];
char* param[16]; char* param[16];
int i, len, params=0; int i, len, params=0;
//clrscr();
for (;;) { for (;;) {
puts("\n] "); cprintf("%#0A\n] ");
get_str(str, 256); cgets(str, 256);
cprintf("\n\r");
len = strlen(str); len = strlen(str);
// Ignore spaces in front of command // Ignore spaces in front of command
i=0; params = 0; i=0; params = 0;
while (str[i] == ' ') i++; while (str[i] == ' ') i++;
param[params] = str+i; params++; i++; // Parameter 0 = app itself param[params] = str+i; params++; i++; // Parameter 0 = app itself
for (; i < len && params<16; i++) { for (; i < len && params<16; i++) {
if (str[i] == ' ') str[i]=0; if (str[i] == ' ') str[i]=0;
if (str[i] != 0 && str[i-1]==0) { if (str[i] != 0 && str[i-1]==0) {
param[params] = str+i; params++; param[params] = str+i; params++;
} }
} }
for (i = 0; strcmp(apps_lst[i], param[0])!=0 && i<apps_count; i++); for (i = 0; strcmp(apps_lst[i], param[0])!=0 && i<apps_count; i++);
switch (i) { switch (i) {
case 0: puts("You must enter a command!\n"); break; case 0: cprintf("%#0CYou must enter a command!\n\r"); break;
case 1: reboot(); case 1: reboot();
case 2: apps_osver(); break; case 2: apps_osver(); break;
case 3: apps_time(); break; case 3: apps_time(); break;
case 4: apps_place(); break; case 4: apps_place(); break;
case 5: apps_clrscr(); break; case 5: apps_clrscr(); break;
case 6: apps_memory(params, (const char**)param); break; case 6: apps_dump(params, (const char**)param); break;
case 7: apps_help(params, (const char**)param); break; case 7: apps_help(params, (const char**)param); break;
case 8: puts((char*)get_cpu_vender()); break; case 8: cprintf("%#0A%s\n\r", (char*)get_cpu_vender()); break;
case 9: apps_memory_status(); break; case 9: apps_memory_status(); break;
default: puts("Invalid function: "); puts(param[0]); case 10: apps_read (params, (const char**)param); break;
putc('\n'); default: cprintf("%#0CInvalid function: %s\n\r", param[0]);
break; break;
} }
} }

165
SysCore/video/color/color.c Normal file
View File

@ -0,0 +1,165 @@
#include <color.h>
#include <stdlib.h>
RGBColor RGBColors_4bpp[] = {{0, 0, 0}, // 00 Black
{0, 0, 127}, // 01 Dark Blue
{0, 127, 0}, // 02 Dark Green
{0, 127, 127}, // 03 Dark Cyan
{127, 0, 0}, // 04 Dark Red
{127, 0, 127}, // 05 Dark Magenta
{127, 127, 0}, // 06 Dark Yellow
{192, 192, 192},// 07 Light Gray
{127, 127, 127},// 08 Dark Gray
{0, 0, 255}, // 09 Blue
{0, 255, 0}, // 10 Green
{0, 255, 255}, // 11 Cyan
{255, 0, 0}, // 12 Red
{255, 0, 255}, // 13 Magenta
{255, 255, 0}, // 14 Yellow
{255, 255, 255} // 15 White
};
RGBColor Convert_1bpp_to_RGB(Color_1Bpp c)
{
unsigned char t = (c) ? 0xFF : 0x00;
RGBColor temp = {t, t, t};
return temp;
}
RGBColor Convert_4bpp_to_RGB(Color_4Bpp c)
{
return RGBColors_4bpp[c];
}
RGBColor Convert_6bpp_to_RGB(Color_6Bpp c)
{
unsigned char R, G, B;
R = (unsigned char) ((unsigned)((c & 0x30)>>4) * 255 / 3 );
G = (unsigned char) ((unsigned)((c & 0x0C)>>2) * 255 / 3 );
B = (unsigned char) ((unsigned) (c & 0x03) * 255 / 3 );
RGBColor ret = {R, G, B};
return ret;
}
RGBColor Convert_8bpp_to_RGB(Color_8Bpp c)
{
unsigned char R, G, B;
R = (unsigned char) ((unsigned)((c & 0xE0)>>5) * 255 / 7 );
G = (unsigned char) ((unsigned)((c & 0x1C)>>2) * 255 / 7 );
B = (unsigned char) ((unsigned) (c & 0x03) * 255 / 3 );
RGBColor ret = {R, G, B};
return ret;
}
RGBColor Convert_15bpp_to_RGB(Color_15Bpp c)
{
unsigned char R, G, B;
R = (unsigned char) ((unsigned)((c & 0x7C00)>>10) * 255 / 31 );
G = (unsigned char) ((unsigned)((c & 0x03E0)>>5) * 255 / 31 );
B = (unsigned char) ((unsigned) (c & 0x001F) * 255 / 31 );
RGBColor ret = {R, G, B};
return ret;
}
RGBColor Convert_16bpp_to_RGB(Color_16Bpp c)
{
unsigned char R, G, B;
R = (unsigned char) ((unsigned)((c & 0xF800)>>11) * 255 / 31 );
G = (unsigned char) ((unsigned)((c & 0x07E0)>>5) * 255 / 63 );
B = (unsigned char) ((unsigned) (c & 0x001F) * 255 / 31 );
RGBColor ret = {R, G, B};
return ret;
}
RGBColor Convert_18bpp_to_RGB(Color_18Bpp c)
{
unsigned char R, G, B;
R = (unsigned char) ((unsigned)(c.R) * 255 / 0x3F );
G = (unsigned char) ((unsigned)(c.G) * 255 / 0x3F);
B = (unsigned char) ((unsigned)(c.B) * 255 / 0x3F);
RGBColor ret = {R, G, B};
return ret;
}
RGBColor Convert_24bpp_to_RGB(Color_24Bpp c)
{
return c;
}
Color_1Bpp Convert_RGB_to_1bpp(RGBColor c)
{
if (((unsigned)c.R + (unsigned)c.G + (unsigned)c.B) / 3 >= 128) return 1;
return 0;
}
Color_4Bpp Convert_RGB_to_4bpp(RGBColor c)
{
int i; int minim = 0xFFFF; int index = 0;
int R, G, B;
for (i = 0; i < 16; i++)
{
R = (int)(c.R) - (int)(RGBColors_4bpp[i].R);
G = (int)(c.G) - (int)(RGBColors_4bpp[i].G);
B = (int)(c.B) - (int)(RGBColors_4bpp[i].B);
R = abs(R) + abs(G) + abs(B);
if (R <= minim) {
minim = R; index = i;
}
}
return index;
}
Color_6Bpp Convert_RGB_to_6bpp(RGBColor c)
{
Color_6Bpp temp = 0; unsigned tmp;
tmp = (unsigned)(c.R) * 3 / 255; temp |= (tmp & 0x03) << 4;
tmp = (unsigned)(c.G) * 3 / 255; temp |= (tmp & 0x03) << 2;
tmp = (unsigned)(c.B) * 3 / 255; temp |= (tmp & 0x03);
return temp;
}
Color_8Bpp Convert_RGB_to_8bpp(RGBColor c)
{
Color_8Bpp temp = 0; unsigned tmp;
tmp = (unsigned)(c.R) * 7 / 255; temp |= (tmp & 0x07) << 5;
tmp = (unsigned)(c.G) * 7 / 255; temp |= (tmp & 0x07) << 2;
tmp = (unsigned)(c.B) * 3 / 255; temp |= (tmp & 0x03);
return temp;
}
Color_15Bpp Convert_RGB_to_15bpp(RGBColor c)
{
Color_15Bpp temp = 0; unsigned tmp;
tmp = (unsigned)(c.R) * 0x1F / 255; temp |= (tmp & 0x1F) << 10;
tmp = (unsigned)(c.G) * 0x1F / 255; temp |= (tmp & 0x1F) << 5;
tmp = (unsigned)(c.B) * 0x1F / 255; temp |= (tmp & 0x1F);
return temp;
}
Color_16Bpp Convert_RGB_to_16bpp(RGBColor c)
{
Color_16Bpp temp = 0; unsigned tmp;
tmp = (unsigned)(c.R) * 0x1F / 255; temp |= (tmp & 0x1F) << 11;
tmp = (unsigned)(c.G) * 0x3F / 255; temp |= (tmp & 0x3F) << 5;
tmp = (unsigned)(c.B) * 0x1F / 255; temp |= (tmp & 0x1F);
return temp;
}
Color_18Bpp Convert_RGB_to_18bpp(RGBColor c)
{
Color_18Bpp temp; unsigned tmp;
tmp = (unsigned)(c.R) * 0x3F / 255; temp.R = (unsigned char)tmp;
tmp = (unsigned)(c.G) * 0x3F / 255; temp.G = (unsigned char)tmp;
tmp = (unsigned)(c.B) * 0x3F / 255; temp.B = (unsigned char)tmp;
return temp;
}
Color_24Bpp Convert_RGB_to_24bpp(RGBColor c)
{
return c;
}

View File

@ -0,0 +1,66 @@
#ifndef __COLOR__H__
#define __COLOR__H__
/**RGB color structure.*/
typedef struct {
unsigned char R,G,B;
} RGBColor;
/**\Monochrome (black and white)*/
typedef bool Color_1Bpp;
/**CGA 2 bits per pixel indexed color.\n\n\Notes: Cannot be converted to/from RGB.*/
typedef unsigned char Color_2Bpp;
/**16 color VGA.
* \n\n\Format: Uses 1-1-1 bit format, highest bit is intensity.
* \n\n\Notes: Unused bits are ignored (should be 0).*/
typedef unsigned char Color_4Bpp;
/**64 color EGA.
* \n\n\Format: Uses 2-2-2 bit format.
* \n\n\Notes: High 2 bits are ignored (should be 0).*/
typedef unsigned char Color_6Bpp;
/**256 color VGA.
* \n\n\Format: Uses 3-3-2 bit format
* \n\n\Notes: The palette must be changed before usable.*/
typedef unsigned char Color_8Bpp;
/**SVGA Highcolor palette.
* \n\n\Format: Uses 5-5-5 bit format.
* \n\n\Notes: Unused high bit is ignored (should be 0).*/
typedef unsigned short Color_15Bpp;
/**SVGA Highcolor palette.
* \n\n\Format: Uses 5-6-5 bit format.*/
typedef unsigned short Color_16Bpp;
/**VGA 18-bit RGB
* \n\n\Format: Uses the RGBColor structure, with the R, G and B components.
* \n\n\Notes: High 2 bits of each component are ignored, should be 0.*/
typedef RGBColor Color_18Bpp;
/**24-bit Truecolor
* \n\n\Format: Uses the RGBColor structure, with the R, G and B components.*/
typedef RGBColor Color_24Bpp;
/**Few functions to convert values from one format to another.*/
extern RGBColor Convert_1bpp_to_RGB(Color_1Bpp c);
extern RGBColor Convert_4bpp_to_RGB(Color_4Bpp c);
extern RGBColor Convert_6bpp_to_RGB(Color_6Bpp c);
extern RGBColor Convert_8bpp_to_RGB(Color_8Bpp c);
extern RGBColor Convert_15bpp_to_RGB(Color_15Bpp c);
extern RGBColor Convert_16bpp_to_RGB(Color_16Bpp c);
extern RGBColor Convert_18bpp_to_RGB(Color_18Bpp c);
extern RGBColor Convert_24bpp_to_RGB(Color_24Bpp c);
extern Color_1Bpp Convert_RGB_to_1bpp(RGBColor c);
extern Color_4Bpp Convert_RGB_to_4bpp(RGBColor c);
extern Color_6Bpp Convert_RGB_to_6bpp(RGBColor c);
extern Color_8Bpp Convert_RGB_to_8bpp(RGBColor c);
extern Color_15Bpp Convert_RGB_to_15bpp(RGBColor c);
extern Color_16Bpp Convert_RGB_to_16bpp(RGBColor c);
extern Color_18Bpp Convert_RGB_to_18bpp(RGBColor c);
extern Color_24Bpp Convert_RGB_to_24bpp(RGBColor c);
#endif

26
SysCore/video/compile.bat Normal file
View File

@ -0,0 +1,26 @@
@echo off
rem NASM and DJGPP executable paths:
set nasm_path=C:\nasm
set djgpp_path=C:\DJGPP\bin
set objpath=..\objects
set incpath=../include
@echo Building Video Drivers...
del %objpath%\video\vga03h.o
goto build
:error
@echo.
@echo There have been build errors. Building halted.
@pause
exit
:build
@echo * Compiling Text Mode 0x03 video driver ...
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/video/vga03h.o vga03h.c
:check
if not exist %objpath%\video\vga03h.o goto error

37
SysCore/video/vga.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef __VGA__H__
#define __VGA__H__
typedef struct {
enum MODE_TYPES {
TextMode = 0,
GraphicsMode = 1
};
/**Defines the mode returned by BIOS int 0x10, ah = 0xF*/
unsigned char Mode;
/**Text mode or Graphic mode (defined in MODE_TYPES enumeration)*/
unsigned char ModeType;
/**Screen size (characters in text modes, pixels in graphic modes)*/
unsigned Width, Height;
/**Bits per pixel*/
unsigned bpp;
/**Pointer to a function that sets the cursor position*/
void (*SetCursor) (int wherex, int wherey);
/**Pointer to a function that prints an ascii character in a specified position*/
void (*PutChar) (int wherex, int wherey, unsigned char character);
/**Pointer to a function that returns the ascii character in the specified position*/
unsigned char (*GetChar) (int wherex, int wherey);
/**Pointer to a function which plots a pixel on the screen. Should be set NULL in text modes.
\nColor is a void pointer, to ensure compatibility with different colors.*/
void (*PutPixel) (int wherex, int wherey, void* color);
/**Pointer to a function which returns the color of a pixel on the screen. Should be set NULL in text modes.
\nReturn is a void pointer, to ensure compatibility with different colors.*/
void* (*GetPixel) (int wherex, int wherey);
} VideoMode;
#endif

30
SysCore/video/vga03h.c Normal file
View File

@ -0,0 +1,30 @@
//#include "vga.h"
#include <conio.h>
#include <system.h>
unsigned char* TextVideoRam = (unsigned char*)0xB8000;
void vga03h_cursor(int x, int y)
{
unsigned temp = y*80 + x;
outportb (0x3D4, 14);
outportb (0x3D5, temp >> 8);
outportb (0x3D4, 15);
outportb (0x3D5, temp);
}
void vga03h_putc (int x, int y, unsigned char c) { TextVideoRam[2*(y*80+x)] = c; }
unsigned char vga03h_getc (int x, int y) { return TextVideoRam[2*(y*80+x)]; }
void vga03h_putcolor (int x, int y, unsigned char c) { TextVideoRam[2*(y*80+x)+1] = c; }
unsigned char vga03h_getcolor (int x, int y) { return TextVideoRam[2*(y*80+x)+1]; }
void vga03h_install()
{
ConsoleScreen screen = {
80, 25, 0x07, vga03h_cursor, vga03h_putc, vga03h_getc,
vga03h_putcolor, vga03h_getcolor};
ConsoleInstall (screen);
clrscr();
}

7
SysCore/video/vga03h.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __DEFAULT__TEXT__MODE__H__
#define __DEFAULT__TEXT__MODE__H__
void vga03h_install();
#endif