Skip to content

Commit 2e72666

Browse files
committed
Reorganized snippets and macros into src/lib
1 parent be85f74 commit 2e72666

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

src/lib/syscalls/basic.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef __LIB_SYSCALLS_BASIC
2+
#define __LIB_SYSCALLS_BASIC
3+
4+
#define WINDOW_HEIGHT 0x18
5+
#define WINDOW_WIDTH 0x4F
6+
#define CLOCKS_PER_MS 56230 // 5% of Host CPU
7+
8+
#define INT_MAX 0xFFFFFFFF
9+
10+
#endif

src/lib/syscalls/io.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef __LIB_SYSCALLS_IO
2+
#define __LIB_SYSCALLS_IO
3+
4+
extern void _low_print(char str[], unsigned short n,
5+
unsigned char x,unsigned char y,
6+
unsigned char color);
7+
8+
extern void _low_put_chars(char c,unsigned short count, unsigned char color);
9+
extern void _low_move_xy(unsigned char x, unsigned char y, unsigned char page);
10+
extern void _low_clear_screen(unsigned char color,
11+
unsigned char x1,unsigned char y1,
12+
unsigned char x2, unsigned char y2);
13+
14+
unsigned char IO_CURRENT_X = 0;
15+
unsigned char IO_CURRENT_Y = 0;
16+
17+
void move_xy(unsigned char x, unsigned char y) {
18+
IO_CURRENT_X=x;
19+
IO_CURRENT_Y=y;
20+
_low_move_xy(x,y,0);
21+
}
22+
23+
void move_xy_diff(unsigned char dx, unsigned char dy) {
24+
IO_CURRENT_X+=dx;
25+
IO_CURRENT_Y+=dy;
26+
_low_move_xy(IO_CURRENT_X,IO_CURRENT_Y,0);
27+
}
28+
29+
void put_char(char c, unsigned char color) {
30+
_low_put_chars(c,1, color);
31+
move_xy_diff(1, 0);
32+
}
33+
34+
void put_chars(char c,unsigned short count, unsigned char color) {
35+
_low_put_chars(c,count, color);
36+
move_xy_diff(count, 0);
37+
}
38+
39+
void put_string(char *str, unsigned char color) {
40+
while((*str)!='\0') {
41+
put_char(*str, color);
42+
str++;
43+
}
44+
}
45+
46+
#endif

src/lib/syscalls/io_syscall.asm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
global _low_put_chars
2+
global _low_move_xy
3+
global _low_clear_screen
4+
5+
%macro PLUGIN_SYSCALLS_IO 0
6+
7+
_low_put_chars:
8+
push ebp
9+
mov ebp, esp
10+
11+
mov ah, 0x09 ; (write)
12+
mov al, [ebp + 0x8] ; (char)
13+
mov cx, [ebp + 0xc] ; (count)
14+
mov bh, 0x00 ; (pagenumber)
15+
mov bl, [ebp + 0x10] ; (attribute)
16+
int 0x10
17+
18+
mov esp, ebp
19+
pop ebp
20+
ret
21+
22+
_low_move_xy:
23+
push ebp
24+
mov ebp, esp
25+
26+
mov ah, 0x02 ; (set custor position)
27+
mov dl, [ebp + 0x8] ; (pos_x)
28+
mov dh, [ebp + 0xc] ; (pos_y)
29+
mov bh, [ebp + 0x10] ; (page number)
30+
int 0x10
31+
32+
mov esp, ebp
33+
pop ebp
34+
ret
35+
36+
_low_clear_screen:
37+
push ebp
38+
mov ebp, esp
39+
40+
mov ah, 0x06 ; (scroll)
41+
mov al, 0x00 ; (line count)
42+
mov bh, [ebp + 0x08] ; (attribute)
43+
mov cl, [ebp + 0x0c] ; (window top-left x)
44+
mov ch, [ebp + 0x10] ; (window top-left y)
45+
mov dl, [ebp + 0x14] ; (window bottom-right x)
46+
mov dh, [ebp + 0x18] ; (window bottom-right y)
47+
int 0x10
48+
49+
mov esp, ebp
50+
pop ebp
51+
ret
52+
53+
%endmacro

src/lib/syscalls/time.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __LIB_SYSCALLS_TIME
2+
#define __LIB_SYSCALLS_TIME
3+
4+
#include "lib/syscalls/basic.h"
5+
#define SLEEP_FACTOR_NUM 3 // Possiblity of improvement.
6+
#define SLEEP_FACTOR_DENO 2
7+
#define SLEEP_BURST_MS INT_MAX/CLOCKS_PER_MS/SLEEP_FACTOR_NUM*SLEEP_FACTOR_DENO
8+
9+
extern void _low_sleep(unsigned int half_instructions_count);
10+
11+
void sleep(unsigned int ms) {
12+
while(ms>0) {
13+
unsigned int fms = ms>SLEEP_BURST_MS?SLEEP_BURST_MS:ms;
14+
unsigned int cycles = CLOCKS_PER_MS*fms/SLEEP_FACTOR_DENO*SLEEP_FACTOR_NUM;
15+
_low_sleep(cycles);
16+
ms-=fms;
17+
}
18+
}
19+
20+
#endif

src/lib/syscalls/time_syscall.asm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
global _low_sleep
2+
3+
%macro PLUGIN_SYSCALLS_TIME 0
4+
5+
_low_sleep:
6+
push ebp
7+
mov ebp, esp
8+
mov ebx, [ebp + 0x08] ; (cycles)
9+
_low_sleep_internal:
10+
sub ebx, 1
11+
jnc _low_sleep_internal
12+
13+
mov esp, ebp
14+
pop ebp
15+
ret
16+
17+
%endmacro

src/lib/util/color.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define C_BLACK 0x0
2+
#define C_BLUE 0x1
3+
#define C_GREEN 0x2
4+
#define C_CYAN 0x3
5+
#define C_RED 0x4
6+
#define C_MAGENTA 0x5
7+
#define C_BROWN 0x6
8+
#define C_LIGHT_GRAY 0x7
9+
#define C_DARK_GRAY 0x8
10+
#define C_LIGHT_BLUE 0x9
11+
#define C_LIGHT_GREEN 0xA
12+
#define C_LIGHT_CYAN 0xB
13+
#define C_LIGHT_RED 0xC
14+
#define C_LIGHT_MAGENTA 0xD
15+
#define C_YELLOW 0xE
16+
#define C_WHITE 0xF
17+
18+
#define make_color(fg, bg) ((unsigned char)((fg) + ((bg)<<4)))

0 commit comments

Comments
 (0)