From fdfeb94ae34cc4ccdf9fb98e31dec8de7975cc0f Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 9 Sep 2003 22:00:00 -0400 Subject: [PATCH] Import backup from 2003-09-09 --- idt.inc | 3 + io.asm | 9 ++ kernel.asm | 2 +- kernel.c | 44 +++------- keyboard.c | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++ keyboard.h | 47 ++++++++++ 6 files changed, 318 insertions(+), 35 deletions(-) create mode 100644 keyboard.c create mode 100644 keyboard.h diff --git a/idt.inc b/idt.inc index 511211c..8339a1b 100644 --- a/idt.inc +++ b/idt.inc @@ -78,6 +78,9 @@ isr_main: iret + + + diff --git a/io.asm b/io.asm index c229997..ce95fe7 100644 --- a/io.asm +++ b/io.asm @@ -205,6 +205,8 @@ printf_special: jz printf_ppercent cmp al, 's' jz printf_string + cmp al, 'c' + jz printf_char jmp printf_special_done printf_decimal: @@ -240,6 +242,13 @@ printf_string: call _puts add esp, 4 jmp printf_special_done + +printf_char: + mov eax, [esi] + push eax + call _putc + add esp, 4 + jmp printf_special_done printf_special_done add esi, 4 ;point to next extra argument diff --git a/kernel.asm b/kernel.asm index 14475bd..1454d26 100644 --- a/kernel.asm +++ b/kernel.asm @@ -54,7 +54,7 @@ newgdtcontinue: call _k_init hlt ;halt processor when k_init is done jmp $ ;shouldn't get here... - + %include "gdt.inc" %include "idt.inc" diff --git a/kernel.c b/kernel.c index 1929430..d69ae98 100644 --- a/kernel.c +++ b/kernel.c @@ -13,19 +13,17 @@ #include "functions.h" #include "video.h" #include "mm.h" +#include "keyboard.h" void isr(dword num); void k_init(); +#include "keyboard.c" #include "mm.c" #include "functions.c" #include "video.c" dword timer = 0; -word index = 0; -dword ptrs[512]; -dword goon = 1; -dword req = 0; void k_init() { @@ -36,12 +34,13 @@ void k_init() outportb(0x40, 0x2e); //msb video_init((ModeInfoBlock *) 0x90306); mm_init(); - int a; - for (a=0; a<512; a++) - ptrs[a] = 0; enable_ints(); console_cls(); printf("Memory available to OS: %d MB", mm_totalmem/0x100000); + for (;;) + { + printf("%d ", kbdWaitKey()); + } /* pageblock *pb = first_pageblock; for (;;) { @@ -58,31 +57,6 @@ void isr(dword num) { timer++; (*(byte *)0xb8000)++; - if ((timer%50)==0) - { - if (goon) - { - if(ptrs[index] = (dword)mm_palloc(1024)) - printf("\nReq:%d\tAddr: %x\tEntries: %x\tMem: %x\tIndex: %x", req++, ptrs[index++], mm_freeentries(), mm_freemem(), index); - else - { - goon = 0; - index = 0; - } - } - else - { - dword reslt = mm_pfree((void *)ptrs[index]); //zero returned means chunk freed successfully - printf("\nReq:%d\tFree(): %x\tEntries: %x\tMem: %x\tIndex: %x", req++, reslt, mm_freeentries(), mm_freemem(), index++); - if (reslt) - { - for (index=0; index<512; index++) - ptrs[index] = 0; - index = 0; - goon = 1; - } - } - } switch(timer) { case 150: @@ -109,8 +83,10 @@ void isr(dword num) } eoi(); } - //if (num == 0x21) - // restart(); + if (num == 0x21) + { + isr_keyboard(); + } } diff --git a/keyboard.c b/keyboard.c new file mode 100644 index 0000000..3eeade8 --- /dev/null +++ b/keyboard.c @@ -0,0 +1,248 @@ +//Keyboard.c +// Created: 04/17/03 Josh Holtrop +// Modified: 09/09/03 +//for HOS + + +byte kbdFlags = 0; +byte kbdAscii = 0; +byte kbdScan = 0; + +dword kbdBuffer[32]; +byte kbdBufferStart = 0; +byte kbdBufferEnd = 0; +byte kbdExt = 0; +byte kbdExt2 = 0; +byte ackReason = 0; //used to record the reason why we would get an acknowledge byte (0xFA) + + + //nul esc bksp tab lctl lsft rsft lalt caps F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 numScrlNumPad------- unknown---- F11 F12 unknown.... +const byte SCAN2ASCII[128] = "\000\0331234567890-=\010\011qwertyuiop[]\n\001asdfghjkl;'`\001\\zxcvbnm,./\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001-\001\001\001+\001\001\001\001\001\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002"; +const byte SCAN2ASCIISHIFT[128] = "\000\033!@#$%^&*()_+\010\011QWERTYUIOP{}\n\001ASDFGHJKL:\"~\001|ZXCVBNM<>?\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001789-456+1230.\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002"; + +//====FUNCTIONS: + +void isr_keyboard() +{ + kbdScan = inportb(0x60); + byte inState = inportb(0x61); + outportb(0x61, inState|0x80); + outportb(0x61, inState); + + if (kbdScan == 0xFA) //250 //ACKnowledge + { + if (ackReason == 0xED) //reset LEDs + { + outportb(0x64, (kbdFlags & 0x07)); + printf("KBD_ACK!"); + } + ackReason = 0; + } + if (kbdScan == 224) //extended key + { + kbdExt = 1; + eoi(); + return; + } + if (kbdScan == 225) + { + kbdExt2 = 2; + eoi(); + return; + } + +//====handle control keys:: + kbdAscii = 2; + switch (kbdScan) //control keys + { + case KBD_SCAN_LSHIFT: + kbdFlags |= KBD_SHIFT; + kbdAscii = 1; + break; + case KBD_SCAN_RSHIFT: + kbdFlags |= KBD_SHIFT; + kbdAscii = 1; + break; + case KBD_SCAN_LCTRL: + kbdFlags |= KBD_CTRL; + kbdAscii = 1; + break; + case KBD_SCAN_LALT: + kbdFlags |= KBD_ALT; + kbdAscii = 1; + break; + + case KBD_SCAN_LSHIFT + KBD_SCAN_RELEASED: + kbdFlags &= (KBD_SHIFT ^ 0xFF); + kbdAscii = 1; + break; + case KBD_SCAN_RSHIFT + KBD_SCAN_RELEASED: + kbdFlags &= (KBD_SHIFT ^ 0xFF); + kbdAscii = 1; + break; + case KBD_SCAN_LCTRL + KBD_SCAN_RELEASED: + kbdFlags &= (KBD_CTRL ^ 0xFF); + kbdAscii = 1; + break; + case KBD_SCAN_LALT + KBD_SCAN_RELEASED: + kbdFlags &= (KBD_ALT ^ 0xFF); + kbdAscii = 1; + break; + + case KBD_SCAN_CAPS+KBD_SCAN_RELEASED: + kbdFlags ^= KBD_CAPS; + kbdAscii = 1; + kbd_resetLEDs(); //update LEDs + break; + case KBD_SCAN_SCROLL+KBD_SCAN_RELEASED: + kbdFlags ^= KBD_SCROLL; + kbdAscii = 1; + kbd_resetLEDs(); //update LEDs + break; + case KBD_SCAN_NUM+KBD_SCAN_RELEASED: + kbdFlags ^= KBD_NUM; + kbdAscii = 1; + kbd_resetLEDs(); //update LEDs + break; + } + if (kbdAscii == 1) + { + if (kbdExt > 0) + kbdExt--; + eoi(); + return; + } +//====determine ASCII value of key:: + if (kbdExt > 0) //extended key, kbdScan holds extended key + { + kbdExt--; + kbdAscii = 1; + switch (kbdScan) + { + case KBD_SCANE_ENTER: + kbdAscii = '\n'; break; + case 53: // '/' character (divide on numpad) + kbdAscii = '/'; break; + } + } + else if (kbdExt2 > 0) //extended key 2 + { + kbdExt2--; +// if (kbdScan == 69) // (pause|break) +// kbdAscii = 2; //flag ascii value of 1 means control character (pausebreak) +// else + kbdAscii = 2; //flag ascii value of 2 means ignore key (or unknown value) + } + else //not an extended key + { + // if letter key + if (((kbdScan >= 16) && (kbdScan <= 25)) || ((kbdScan >= 30) && (kbdScan <= 38)) || ((kbdScan >= 44) && (kbdScan <= 50))) + { + // if caps and shift are different (either one pressed, not both) + if (((kbdFlags & KBD_SHIFT) != 0) & ((kbdFlags & KBD_CAPS) != 0)) + kbdAscii = SCAN2ASCII[kbdScan & 0x7F]; + else if (((kbdFlags & KBD_SHIFT) == 0) & ((kbdFlags & KBD_CAPS) == 0)) + kbdAscii = SCAN2ASCII[kbdScan & 0x7F]; + else + kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F]; + } + // if numpad key + else if ((kbdScan >= 71) && (kbdScan <= 83)) + { + // if numlock on + if (kbdFlags & KBD_NUM) + kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F]; + else + kbdAscii = SCAN2ASCII[kbdScan & 0x7F]; + } + // other key + else + { + if ((kbdFlags & KBD_SHIFT) != 0) + kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F]; + else + kbdAscii = SCAN2ASCII[kbdScan & 0x7F]; + } + } + +//====do something with key:: + if ((kbdScan == 83) & ((kbdFlags & KBD_CTRL) != 0) & ((kbdFlags & KBD_ALT) != 0)) + { + printf("Initiating reboot."); + restart(); + } + if (kbdAscii == 2) //unknown key / ignore key + { + eoi(); + return; + } + if (kbdScan < KBD_SCAN_RELEASED) //a key was pressed, save it + { + if ((kbdBufferStart-kbdBufferEnd) == 1) //no key slots available + { + eoi(); + return; + } + else if ((kbdBufferStart == 0) & (kbdBufferEnd == 31)) //no key slots available + { + eoi(); + return; + } + else + { + kbdBuffer[kbdBufferEnd] = (dword) ((kbdFlags << 16) | (kbdScan << 8) | kbdAscii); + kbdBufferEnd++; + if (kbdBufferEnd == 32) + kbdBufferEnd = 0; + } + } + + eoi(); + +// if (key == 0xFA) //ACKnowledge +// outportb(0x60, 0x3); +}; + +inline byte switchCase(byte asciiCode) +{ + if ((asciiCode >= 'A') & (asciiCode <= 'Z')) + return (asciiCode + ('a' - 'A')); + if ((asciiCode >= 'a') & (asciiCode <= 'z')) + return (asciiCode - ('a' - 'A')); + return asciiCode; +} + +dword kbdGetKey() +{ + if (kbdBufferEnd == kbdBufferStart) //buffer empty + return 0; + dword retVal = kbdBuffer[kbdBufferStart]; + kbdBufferStart++; + if (kbdBufferStart == 32) + kbdBufferStart = 0; + return retVal; +} + +dword kbdWaitKey() +{ + dword retVal = 0; + for (;;) + { + retVal = kbdGetKey(); + if (retVal != 0) + return retVal; + } +} + +inline void kbd_resetLEDs() +{ + outportb(0x64, 0xED); + //outportb(0x64, (kbdFlags & 0x07)); + ackReason = 0xED; +} + + + + + + diff --git a/keyboard.h b/keyboard.h new file mode 100644 index 0000000..3011fb3 --- /dev/null +++ b/keyboard.h @@ -0,0 +1,47 @@ +//Keyboard.h +// Created: 04/17/03 Josh Holtrop +// Modified: 05/07/03 +//for HOS + +#define KBD_SCROLL 0x01 +#define KBD_NUM 0x02 +#define KBD_CAPS 0x04 +#define KBD_SHIFT 0x10 +#define KBD_CTRL 0x20 +#define KBD_ALT 0x40 + +#define KBD_SCAN_RELEASED 128 + +#define KBD_SCAN_LCTRL 29 +#define KBD_SCAN_LSHIFT 42 +#define KBD_SCAN_RSHIFT 54 +#define KBD_SCAN_LALT 56 +#define KBD_SCAN_SCROLL 70 +#define KBD_SCAN_CAPS 58 +#define KBD_SCAN_NUM 69 + +#define KBD_SCANE_PRINTSCREEN 55 +#define KBD_SCANE_INS 82 +#define KBD_SCANE_HOME 71 +#define KBD_SCANE_PGUP 73 +#define KBD_SCANE_DEL 83 +#define KBD_SCANE_END 79 +#define KBD_SCANE_PGDN 81 +#define KBD_SCANE_ENTER 28 + +#define KBD_SCANE_NULL 42 + + + +//====PROTOTYPES: + +void isr_keyboard(); +inline void kbd_resetLEDs(); +inline byte kbd_ascii(dword keycode); +inline byte kbd_scancode(dword keycode); +inline byte kbd_flags(dword keycode); +inline byte switchCase(byte asciiCode); +dword kbdGetKey(); +dword kbdWaitKey(); + +