forked from domspad/yehos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscalls.c
123 lines (109 loc) · 3.35 KB
/
syscalls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "kernel.h"
#include "asmhelpers.h"
#include "virtualmem.h"
#include "globals.h"
#include "memlib.h"
#include "task.h"
// BEWARE
#define do_mmap mmap
#define SCREEN_WIDTH 80
#define SCREEN_LINE_SIZE (SCREEN_WIDTH * 2)
char KEYBOARD_BUFFER[MAX_KEYBOARD_BUFF];
int read_keyboard_index = 0;
int write_keyboard_index = 0;
int keyboard_buffer_full = 0;
int video_mem_index = 0;
int
syscall_handler(int syscall_num, const void *parms)
{
switch (syscall_num) {
case 0: // _exit
{
const int32_t *pRetcode = (const int32_t *) parms;
int retcode = *pRetcode;
kprintf("application exited with return code %d\n", retcode);
while (1) halt();
}
break;
case 1: // mmap
{
const int32_t *pParms = (const int32_t *) parms;
int r = do_mmap(pParms[0], pParms[1]);
if (r < 0) {
return -1;
} else {
return pParms[1];
}
}
case 2: // read
{
// check if buffer empty
if (!keyboard_buffer_full && read_keyboard_index == write_keyboard_index){
return -1;
} else {
keyboard_buffer_full = 0;
char ret = KEYBOARD_BUFFER[read_keyboard_index++];
read_keyboard_index = read_keyboard_index % MAX_KEYBOARD_BUFF;
return ret;
}
}
case 3: // write
{
char *videomem = (char *) VIDEO_MEM;
const int32_t *pParms = (const int32_t *) parms;
videomem[video_mem_index] = (char) pParms[0];
videomem[video_mem_index+1] = 0x0F;
video_mem_index = video_mem_index + 2;
return;
}
case 4: // setcursor
{
const int32_t *pParms = (const int32_t *) parms;
int y = (int) pParms[0];
int x = (int) pParms[1];
u16 position = x + y * SCREEN_WIDTH;
// cursor LOW port to vga INDEX register
out8(0x3D4, 0x0F);
out8(0x3D5, (unsigned char)(position&0xFF));
// cursor HIGH port to vga INDEX register
out8(0x3D4, 0x0E);
out8(0x3D5, (unsigned char )((position>>8)&0xFF));
return;
}
case 5: // writechar
{
char *videomem = (char *) VIDEO_MEM;
const int32_t *pParms = (const int32_t *) parms;
char color = (char) pParms[0];
char c = (char) pParms[1];
int y = (int) pParms[2];
int x = (int) pParms[3];
int index = x * 2 + y * SCREEN_LINE_SIZE;
videomem[index] = c;
videomem[index+1] = color;
return;
}
case 6: // scroll
{
char *videomem = (char *) VIDEO_MEM;
memmove(videomem, videomem + SCREEN_LINE_SIZE, 24 * SCREEN_LINE_SIZE);
for (int i = 0; i < SCREEN_WIDTH; i++) {
videomem[i * 2 + 24 * SCREEN_LINE_SIZE] = ' ';
videomem[i * 2 + 24 * SCREEN_LINE_SIZE + 1] = 0xf;
}
return;
}
case 7: // yield
{
yield();
return;
}
case 8: // get_timer_index
{
return timer_index;
}
default:
kprintf("system call %d not supported\n", syscall_num);
break;
};
}