13 #include "../include/string.h"
14 #include "../include/stdlib.h"
18 #ifndef KERNEL_ADDRESS
19 #define KERNEL_ADDRESS 0
25 extern int load(Byte beginSector, Byte track,
int parameter,
int size,
int segment);
26 int gets(
char *str,
int size);
32 #define PROGRAMS_MAX 7
36 #pragma GCC diagnostic push
37 #pragma GCC diagnostic ignored "-Wunused-parameter"
53 "iretw\n"::
"a"(KERNEL_ADDRESS),
"d"(0x200),
"c"(
IF));
63 asm(
"pushw ds\nmov ds, ax"::
"a"(KERNEL_ADDRESS));
64 static int counter = 0;
65 static int lastReset = 100;
71 asm(
"int 0x16":
"=a"(special):
"a"(0x1200));
72 if(special == 0x30C && lastReset >= 100) {
74 for(
int i = 0; i < 0x1f; i += 2) {
78 "mov ax, WORD ptr [0x41E+si]\n"
79 "popw ds":
"=a"(key):
"S"(i));
80 if(key.scancode == 38 && key.character == 0) {
82 for(
int i = 0; i < 0x1f; i += 2)
86 "mov WORD ptr [0x41E+si],bx\n"
95 asm(
"mov %0, ss":
"=r"(seg));
96 if(seg == KERNEL_ADDRESS)
124 asm(
"pushw ds\n mov ds, bx\nmov [0x471],ax\n popw ds"::
"a"(0),
"b"(0));
129 #pragma GCC diagnostic pop
133 setColorPalette(DarkGrey);
141 asm(
"int 0x21":
"=a"(bufferSize):
"a"(0));
142 printf(
"Kernel loaded.\nVersion: "__DATE__
" "__TIME__
"\nMemory size: %ikB\nLoaded %i files\n>",
getMemorySize(), bufferSize);
149 bufferSize = gets(command, 98);
154 *(
char *)
strchr(command,
' ') = 0;
157 if(bufferSize == 0) {
160 else if(
strcmp(command,
"cls")) {
163 else if(
strcmp(command,
"pos")) {
165 printf(
"%i:%i", cursor.x, cursor.y);
167 else if(
strcmp(command,
"key")) {
170 asm(
"int 0x16":
"=a"(special):
"a"(0x1200));
171 printf(
"character=%i,scancode=%i,special=%b,color=", key.character, key.scancode, special);
174 asm(
"int 0x10":
"=a"(a):
"a"(0x0800),
"b"(0x0000));
177 else if(
strcmp(command,
"mode")) {
180 asm(
"pushw ds\n xor bx,bx\nmov ds, bx\nmov ax, [0x44a]\n popw ds":
"=a"(col));
181 asm(
"pushw ds\n xor bx,bx\nmov ds, bx\nmov ax, [0x44c]\n popw ds":
"=a"(size));
182 printf(
"Mode: %i, columns/row: %i, size %i",
stoi(parameter), col, size);
184 else if(
strcmp(command,
"mem")) {
186 for(
int i = 0; i < 1024; i++) {
188 "mov al, byte ptr ds:[bx]\n"
189 "mov ds, di":
"=a"(val):
"b"(i),
"S"(
stoi(parameter)),
"D"(KERNEL_ADDRESS));
193 else if(
strcmp(command,
"ls")) {
196 cputs(
"NAME TRACK:SECTOR SIZE", Cyan);
198 for(; i < numberOfFiles; i++) {
201 printf(
"%s%i:%s%i %s%i\n", (files[i].track / 10 >= 1 ?
"" :
"0"), files[i].track, (files[i].beginSector / 10 >= 1 ?
"" :
"0"), files[i].beginSector, ((files[i].size / 10) >= 1 ?
"" :
"0"), files[i].size);
202 sum += files[i].size;
204 printf(
"\n%i file(s) %s%i sector(s)\n", i, (sum / 10 >= 1 ?
"" :
"0"), sum);
206 else if(
strcmp(command,
"map")) {
216 else if(
strcmp(command,
"rm")) {
217 if(parameter[0] == 0) {
221 asm(
"int 0x21":
"=a"(retVal):
"a"(0x0500),
"b"(parameter));
227 for(; i < 96 && command[i] != 0 && command[i] ==
' '; i++) {
230 strcpy(command, command + i);
240 char freeSegment = -ENOMEM;
242 if(!programsMap[j]) {
243 programsMap[j] =
true;
248 if(freeSegment == -ENOMEM) {
253 retVal =
load(files[
id].beginSector, files[
id].track, parameter, files[
id].size, KERNEL_ADDRESS + 0x1000 * freeSegment);
256 cputs(
"Error:", Red);
257 printf(
" \"%s\" returned %i\n", command, retVal);
261 cputs(
"Error:", Red);
263 printf(
" \"%s\" is unknown command!\n", command);
272 int gets(
char *str,
int size) {
278 if(key.character == 13) {
282 if(key.character == 8 && ptr >= 0) {
292 else if(key.character == 0 && key.scancode == 72) {
293 asm(
"int 0x10"::
"a"(0x0601),
"b"(0x0000),
"c"(0x0000),
"d"(0xffff));
297 else if(key.character == 0 && key.scancode == 80) {
298 asm(
"int 0x10"::
"a"(0x0701),
"b"(0x0000),
"c"(0x0000),
"d"(0xffff));
302 else if(key.character == 0 && key.scancode == 75) {
309 ::
"a" (0x0300),
"b" (0x0));
316 else if(key.character == 0 && key.scancode == 77) {
323 ::
"a" (0x0300),
"b" (0x0));
333 str[ptr++] = key.character;
335 str[oldPtr < ptr ? ptr : oldPtr] = 0;
347 char* ptr = *(
int *)a;
352 char specifier = *(ptr + 1);
353 if(specifier ==
'i' || specifier ==
'd')
354 puti(*(
int *)(a + 4 * iterator++));
355 else if(specifier ==
'u')
356 asm(
"int 0x20"::
"a"(0x200),
"b"(*(
int *)(a + 4 * iterator++)));
357 else if(specifier ==
'b') {
359 int par = *(
int *)(a + 4 * iterator++);
360 for(
int i = 0; i < 16; i++) {
361 num[15 - i] = par % 2 +
'0';
365 asm(
"int 0x20"::
"a"(0x100),
"b"(num));
367 else if(specifier ==
'c')
369 "int 0x20"::
"a"(*(
char *)(a + 4 * iterator++)));
370 else if(specifier ==
's')
371 asm(
"int 0x20"::
"a"(0x100),
"b"(*(
int *)(a + 4 * iterator++)));
374 "int 0x20"::
"a"(*ptr));
Console input output control.
void cputs(int str, Color color)
Color put string.
void setVideoMode(Mode mode)
Set the video mode of screen.
Cursor getCursorPosition(void)
Get position of Cursor.
void cls(void)
Clear screen.
File system, adds file system interruption in int0x21()
__int void int0x21(interruptFrame *frame)
interruption for file system
bool map[TRACKS_MAX][SECTORS_PER_TRACK]
Map of used sectors in tracks.
int sys_open(const int filename)
Open file.
#define SECTORS_PER_TRACK
Number of sectors per track.
#define FILENAME_MAX
Maximal size of file name.
#define TRACKS_MAX
Maximal number of tracks in OS.
void setInterrupts(void)
Set system interruptions.
void addInterrupt(number, function)
add interruption
Setting system interruptions, see interrupts.asm.
#define IF
Interruption flag.
#define EOI()
End of interruption.
Standard input output library.
void puti(int num)
Put int.
Key getc(void)
Wait for key in buffer, get it and clear buffer.
void printf(const int str,...)
C-like printf function.
void puts(const int string)
Put string.
void putc(Byte character)
Put character.
#define PROGRAMS_MAX
Maximal number of programs running simultaneously.
void __printf(int a)
Function for int 0x20 AH=3 see io.h printf()
__int void timer(struct interruptFrame *frame)
Interruption is called 18 times per second.
void reset()
Reset OS to kernel.
__int void ctrlBreak(struct interruptFrame *frame)
Ctrl + Break handler.
__int void shiftPrtSc(struct interruptFrame *frame)
Shift + PrintScreen handler.
int load(beginSector, track, parameter, size, segment)
load program to segment and executes it
Word getMemorySize(void)
Get RAM size in first 1MB.
size_t strlen(const char *str)
Returns length of string.
int strncpy(int destination, int source, size_t num)
Copy num Bytes from source to destination until \0 appears.
bool strcmp(int str1, int str2)
Compares two string until \0 appears.
int stoi(const char *str)
Convert string to int.
char * strcpy(char *destination, const char *source)
Copy from source to destination until \0 appears.
const char * strchr(const char *str, int character)
Find first appearance of character.
frame for interruption, required by gcc for __int
#define NULL
pointer to NULL
#define __start
Must be before main function of program to put this function as first in binary file.
Key information, got from buffer.