↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

keybrd.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:        keybrd.c
DESCRIPTION: Basic text input system  (for use in kernel debugger)
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "..\init\init.h"
#include "keybrd.h"

// Globals
//
static char g_char;
static volatile char g_avail = 0;

static char g_arKeyMap[80] = {
    '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
    '-', '+', ' ', ' ', 'q', 'w', 'e', 'r', 't', 'y',
    'u', 'i', 'o', 'p', '[', ']',
    '\n', //Enter
    ' ',  //Ctl
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
    '\'', ' ', ' ', '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
    ',', '.', '/',
    ' ', //Shift
    ' ',
    ' ',
    ' ',
    ' ', //Space
    ' ',
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
    ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
    ' ', ' ', ' ', ' ',
};

// Initialize keyboard
//
void kbdinit() {
    setvector(32+1, kbdirq);
}


// Handler of the keyboard hardware IRQ
//
void kbdirq() {
    char c, dc;
    c = access_in(0x60);

    dc = kbddecode(c);
    if(dc!=0)
    {
        g_char=dc;
        g_avail=1;
    }
}

// Character input routines
//
char getc() {
    while(!g_avail);
    g_avail = 0;
   
    return g_char;
}

void gets(char*pBuf, unsigned long dwBufSize) {
    char c;
    unsigned long i=0;
   
    do{
        c = getc();
        putc(c);
        pBuf[i++] = c;
    }
    while(c!='\n' && i<dwBufSize-1);
   
    pBuf[i-1] ='\0';
}

// Convert keycodes to ASCII
//
char kbddecode(signed char scancode)
{
    if(scancode<0)    return 0;
    if(scancode>=80 ) return 0;

    scancode = g_arKeyMap[(unsigned long)scancode];

    return scancode;
}

// Wait for input
//
void kbdwait() {
    signed char state;
   
    while(1) {
        state = access_in(0x64);
        if((state & 0x02)==0) break;
    }
}

// Reboot PC
//
void reboot()
{
    unsigned char state;

    puts("\nRebooting Machine...");
   
    while(1){ state = access_in(0x64); if((state & 0x01)==0) break;}
    access_out(0x64, 0xD1);
    while(1){ state = access_in(0x64); if((state & 0x01)==0) break;}
    access_out(0x60, 2);
}