summaryrefslogtreecommitdiff
path: root/ctest.c
blob: 9d356064a5faa83d635597627f5fd066e8a3145e (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#define VRAM 0x00b8000

extern void kmain(void);
extern void khalt(void);
extern unsigned short inb(unsigned short port);
extern unsigned short outb(unsigned short port, unsigned char data);
extern unsigned short kend;

unsigned short *scr = (unsigned short*) VRAM;
unsigned int cursor = 0;
unsigned char cursor_x;
unsigned char cursor_y;
unsigned char cursor_attrib;


unsigned char interrupt_handler(unsigned char intr)
{
    return intr;
}

void update_hw_cursor()
{
    unsigned char position = (cursor_y * 80) + cursor_x;
    cursor = position + VRAM;
    unsigned char x, y;
    x = (position >> 8);
    y = (position) & 0xff;
    outb(0x3D4, 14);
    outb(0x3D5, x);
    outb(0x3D4, 15);
    outb(0x3D5, y);
}

void cls(char attr)
{
    unsigned short i;
    unsigned short *sptr = (unsigned short*)scr;
    for(i = 0; i < 0x0780; ++i)
    {
        (*(sptr)) = ( 0x20 | attr << 8);
        sptr++;
    }
}

int putc(unsigned char ch)
{
    if (ch == 0x8)
    {
        if(cursor_x != 0)
            cursor_x--;
    }
    else if (ch == 0x9)
    {
        cursor_x = (cursor_x + 8) & ~(8 - 1);
    }
    else if (ch == '\r')
    {
        cursor_x = 0;
    }
    else if (ch == '\n')
    {
        cursor_x = 0;
        cursor_y++;
    }
    else if(ch >= 0x20)
    {
        (*(scr + cursor_y * 80 + cursor_x)) = (ch | cursor_attrib << 8);
        cursor_x++;
    }

    if(cursor_x > 79)
    {
        cursor_x = 0;
        cursor_y++;
    }

    scroll();
    update_hw_cursor();
    return ch;
}

void putnl()
{
    putc('\n');
}

unsigned int puts(const char *s)
{
    unsigned int i = 0;
    while(*s != 0)
    {
        putc(*s);
        i++;
        s++;
    }
    putnl();

    return i;
}

void *memset(void *s, int c, unsigned int n)
{
    unsigned int i;
    unsigned char *tmp = s;
    for(i = 0; i < n; ++i)
    {
        *tmp = c;
        tmp++;
    }
    return s;
}

void *memcpy(void *dest, void *src, unsigned int n)
{
    unsigned int i;
    unsigned short *dtmp = dest;
    unsigned short *stmp = src;

    for(i = 0; i < n; ++i)
    {
        (*(dtmp + i)) = (*(stmp + i));
    }
    return dest;
}

int strlen(const char *s)
{
    unsigned int i = 0;
    while(*s != 0)
    {
        i++;
        s++;
    }

    return i;
}

char *strchr(char *s, int c)
{
    while(*s != 0)
    {
        if(*s == c)
            break;
        s++;
    }

    return s;
}

char *strncat(char* dest, const char* src, int num)
{
    dest += strlen(dest);
    while(src != 0 && num != 0)
    {
        *dest = *src;
        src++;
        dest++;
        num--;
    }
    return dest;
}

char *strncpy(char* dest, const char* src, int num)
{
    while(src != 0 && num != 0)
    {
        *dest = *src;
        src++;
        dest++;
        num--;
    }
    return dest;
}

int strncmp(const char* str1, const char* str2, int num)
{
    while((*str1 == *str2) && (*str1 != 0) && num != 0)
    {
        str1++;
        str2++;
        num--;
    }
    return (*str1 - *str2);
}


void strrev(char *str)
{
    char temp;
    char *endp;

    if(str == 0 || !(*str))
    {
        return;
    }

    endp = str + strlen(str) - 1;
    while(endp > str)
    {
        temp = *str;
        *str = *endp;
        *endp = temp;
        str++;
        endp--;
    }

}

void putn(const char *type, long long int v)
{
    char buffer[128];
    memset(buffer, 0, 128);

    int bsize;
    int _is_signed = 0;
    int _is_hex = 0;
    int _is_int = 0;

    char r = 0;
    long int n = v;
    int i;

    for(i = 0; i <= strlen((char*)type); i++)
    {
        switch(type[i])
        {
            case 'u':
                _is_signed = 0;
                break;

            case 'x':
                _is_hex = 1;
                break;

            case 'i':
                _is_int = 1;
                break;
        }
    }

    if (n < 0)
    {
        n = ~n + 1;
        _is_signed = 1;
    }

    if(n == 0)
    {
        buffer[0] = '0';
    }
    else if(_is_int)
    {

        for(i = 0; n > 0; i++)
        {
            r = (n % 10);
            r += '0';
            buffer[i] = r;
            n /= 10;
        }
    }
    else if(_is_hex)
    {
        for(i = 0; v != 0; i++)
        {
            n = (v & 0x0f) + '0';
            if (n > '9')
            {
                n += 7;
            }
            buffer[i] = n;
            v >>= 4;
        }

    }
    else
    {
        puts("putn: invalid format specifier\n");
        return;
    }

    if(_is_hex)
    {
        buffer[strlen(buffer) + 1] = '0';
        buffer[strlen(buffer)] = 'x';
    }

    bsize = strlen(buffer);

    if(_is_signed)
        buffer[bsize] = '-';

    strrev(buffer);
    buffer[bsize+1] = 0;

    char *s = buffer;
    while(*s != 0)
    {
        putc(*s);
        i++;
        s++;
    }
}

unsigned char kbd_status(void)
{
    return inb(0x64);
}

unsigned char kbd_scancode(void)
{
    return inb(0x60);
}

void scroll(void)
{
    unsigned blank, temp;
    blank = 0x25 | (cursor_attrib << 8);
    if(cursor_y >= 25)
    {
        temp = cursor_y - 25 + 1;
        memcpy(scr, scr + temp * 80, (25 - temp) * 80 * 2);
        memcpy(scr + (25 - temp) * 80, (short*)blank, 80);
        cursor_y = 25 - 1;
    }
}

int ckmain()
{
    const char *banner =
"             _       __ _       ______   ___   ___ __ _ \n"
"    /|    //| |     // | |     //   ) )    / /    // | |\n"
"   //|   // | |    //__| |    //___/ /    / /    //__| |\n"
"  // |  //  | |   / ___  |   / ___ (     / /    / ___  |\n"
" //  | //   | |  //    | |  //   | |    / /    //    | |\n"
"//   |//    | | //     | | //    | | __/ /___ //     | |\n\n";

    const char *startup = "Kernel @ ";
    const char *startup2 = " ~ ";
    const char *msg_vram = "Video memory @ ";
    const char *shutdown = "Kernel shutting down...";

    char messages[256][10];
    memset(messages, 0, 256 * 10);
    strncpy(messages[0], "message1", 9);
    strncpy(messages[1], "message2", 9);
    strncpy(messages[2], "message3", 9);
    strncpy(messages[3], "message4", 9);

    cursor_x = 0;
    cursor_y = 1;
    cursor_attrib = 0x07;

    cls(cursor_attrib);

    /*
    for(int i = 0; i <= 79; ++i)
    {
        memset(scr-1, 0x07, 1);
        memset(scr+i, 0xB0, 1);
    }
    memcpy(scr + 1920, scr, 79);
    */

    puts(banner);
    puts(startup);
    putn("ux", (int)kmain);
    puts(startup2);

    putn("ux", (int)kend);
    putnl();

    puts(msg_vram);
    putn("ux", (int)scr);
    putnl();

    puts(shutdown);
    putn("ux", 0xDEADBEEF);
    putn("ui", 10);
    putn("x", 10);
    putn("si", -32768);
    putn("si", 32768);
    putn("x", (int)strchr((char*)shutdown, 'K'));
    putn("x", (int)strchr((char*)shutdown, '.'));

    puts((char*)messages[0]);
    puts((char*)messages[1]);
    puts((char*)messages[2]);
    puts((char*)messages[3]);

    char catme[20];
    strncpy(catme, "i like ", 8);
    strncat(catme, "cats\n", 5);
    puts((char*)catme);

    putn("i", strncmp(messages[1], messages[3], 9));

    unsigned char key, status;
    outb(0x20, 0x20);
    while(1)
    {
        status = kbd_status();
        if(status & 0x1)
        {
            key = kbd_scancode();

            putn("x", key);
            putc('\n');
        }

    }

    return 0xffffffff;
}