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

View File

@ -11,7 +11,9 @@ set incpath=../include
del %objpath%\system.o
del %objpath%\string.o
del %objpath%\conio.o
del %objpath%\stdlib.o
del %objpath%\time.o
del %objpath%\ctype.o
goto build
:error
@ -30,6 +32,13 @@ goto build
@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
@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 ...
%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%\string.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

View File

@ -1,34 +1,530 @@
#include <stdarg.h>
#include <stdint.h>
#include <string.h>
#include <system.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";
const static char base_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* External routines the user must specify */
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()
{
// 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
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
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 (fore < 0x10) default_foreground = fore;
if (back < 0x10) ColorDefaultBack = back;
if (fore < 0x10) ColorDefaultFore = fore;
}
// Clear screen, and set font to default font
void clrscr()
{
byte font = default_foreground | (default_background<<4);
unsigned char font = ColorDefaultFore | (ColorDefaultBack<<4);
int i = 0;
for (i = 0; i < current_mode_width*current_mode_height; i++)
{ TextVideoRam[2*i] = 0;
@ -66,7 +562,7 @@ void scroll(int n)
TextVideoRam+(current_mode_width*n*2),
2*current_mode_width*(current_mode_height - n));
byte blank = default_foreground | (default_background<<4);
unsigned char blank = ColorDefaultFore | (ColorDefaultBack<<4);
int i;
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
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)+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;
}
// 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();
@ -126,50 +622,11 @@ void putc(char c)
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
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;
for (i = 0; str[i] != 0; i++)
@ -191,7 +648,7 @@ void puts(const char *str)
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;
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);
}
void put_bin (int x, int y, byte xz)
void put_bin (int x, int y, unsigned char xz)
{
int i;
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>
size_t strlen (const char *str)
unsigned strlen (const char *str)
{
size_t i;
unsigned i;
for (i = 0; *str!=0; str++) i++;
return i;
}

View File

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

View File

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