summaryrefslogtreecommitdiff
path: root/driver
diff options
context:
space:
mode:
Diffstat (limited to 'driver')
-rw-r--r--driver/console.c195
-rw-r--r--driver/console.h40
-rw-r--r--driver/keyboard.c226
-rw-r--r--driver/keyboard.h1
4 files changed, 462 insertions, 0 deletions
diff --git a/driver/console.c b/driver/console.c
new file mode 100644
index 0000000..b6958c5
--- /dev/null
+++ b/driver/console.c
@@ -0,0 +1,195 @@
+#include "console.h"
+#include "kernel/config.h"
+
+//#define FOOLOS_CONSOLE
+
+static int posx=0;
+static int posy=0;
+
+static void scr_nextline();
+
+void update_cursor(uint32_t col,uint32_t row)
+{
+ unsigned short position=(row*80) + col;
+
+ // cursor LOW port to vga INDEX register
+ x86_outb(0x3D4, 0x0F);
+ x86_outb(0x3D5, (unsigned char)(position&0xFF));
+ // cursor HIGH port to vga INDEX register
+ x86_outb(0x3D4, 0x0E);
+ x86_outb(0x3D5, (unsigned char )((position>>8)&0xFF));
+ }
+
+
+// helper_funcs
+static void print_char_col(int x, int y, char c, char col)
+{
+
+ #ifdef FOOLOS_CONSOLE
+ char* video_mem=(char *)SCR_VIDEOMEM+(x+y*SCR_REAL_WIDTH)*2;
+ video_mem[0]=c;
+ video_mem[1]=col;
+ #endif
+
+}
+
+// glue func for vt52 terminal
+void console_put_char(uint8_t c,uint8_t color, uint32_t x, uint32_t y)
+{
+
+ print_char_col(x,y,c, color);
+}
+//
+//
+static void print_char(int x, int y, char c)
+{
+ print_char_col(x,y,c,SCR_WHITE);
+}
+
+static void print_single_num(int i)
+{
+ if(i<10)print_char_col(posx,posy,'0'+i,SCR_GREEN);
+ else if(i<16)print_char_col(posx,posy,'A'+i-10,SCR_GREEN);
+ posx++;
+ if(posx>=SCR_WIDTH)scr_nextline();
+
+}
+
+static void print_str_col(int x,int y,char *str, char col)
+{
+
+ while(*str!=0)
+ {
+ print_char_col(x++,y,*(str++),col);
+ }
+
+}
+
+static void print_str(int x,int y,char *str)
+{
+ print_str_col(x,y,str,SCR_WHITE);
+}
+
+
+//
+//
+void scr_clear()
+{
+ int x,y;
+
+ for(x=0;x<SCR_WIDTH+1;x++)
+ for(y=0;y<SCR_HEIGHT;y++)
+ {
+ if(x==SCR_WIDTH)print_char_col(x,y,'|',SCR_BLUE);
+ else print_char_col(x,y,' ',SCR_BLUE);
+
+
+ }
+
+ posx=posy=0;
+
+}
+/*
+void scr_put_string_nl(char *str)
+{
+ scr_put_string(str);
+ scr_nextline();
+}
+*/
+
+static void scr_nextline()
+{
+#ifdef FOOLOS_CONSOLE
+ int i,x;
+
+ posx=0;
+ posy++;
+
+ if(posy>SCR_HEIGHT-2)
+ {
+ for(i=1;i<SCR_HEIGHT-1;i++)
+ {
+
+ for(x=0;x<SCR_WIDTH+1;x++)
+ {
+ char* video_mem=(char *)SCR_VIDEOMEM+((x)+(i-1)*SCR_REAL_WIDTH)*2;
+ char* video_mem2=(char *)SCR_VIDEOMEM+((x)+i*SCR_REAL_WIDTH)*2;
+ *video_mem=*video_mem2;
+ *(video_mem+1)=*(video_mem2+1);
+ //clear last line
+ if(i==SCR_HEIGHT-2)
+ {
+ if(x==SCR_WIDTH)print_char_col(x,i,'|',SCR_BLUE);
+ else print_char_col(x,i,' ',SCR_BLUE);
+
+
+ }
+ }
+ }
+
+ posy--;
+ }
+#endif
+}
+
+static void scr_put_char(char ch,char col)
+{
+
+ if(ch=='\n')scr_nextline();
+ else if(posx<SCR_WIDTH)
+ {
+ print_char_col(posx,posy,ch,col);
+ posx++;
+ }
+
+#ifdef FOOLOS_CONSOLE_AUTOBREAK
+ if(posx>=SCR_WIDTH)scr_nextline();
+#endif
+
+}
+
+/*
+void scr_put_hex(uint16_t val)
+{
+
+ int i;
+
+ scr_put_string("0x");
+
+ for(i=0;i<4;i++)
+ {
+ print_single_num((val&0xf000)>>12);
+ val=val << 4;
+
+ }
+
+
+}
+*/
+
+/*
+void scr_put_hex32(uint32_t val)
+{
+ scr_put_string("[");
+ scr_put_hex(val>>16);
+ scr_put_string(",");
+ scr_put_hex(val&0xffff);
+ scr_put_string("]");
+}
+*/
+
+static void scr_put_string(char *str, char col)
+{
+ while(*str!=0)
+ {
+ scr_put_char(*(str++),col);
+ }
+}
+
+static void scr_backspace()
+{
+ if(posx==0)return;
+ print_char_col(posx-1,posy,' ',SCR_LGREEN);
+ posx--;
+
+}
diff --git a/driver/console.h b/driver/console.h
new file mode 100644
index 0000000..819bd8d
--- /dev/null
+++ b/driver/console.h
@@ -0,0 +1,40 @@
+#ifndef CONSOLEINT_H
+#define CONSOLEINT_H
+
+// 80 x 24 ?
+
+#include <stdint.h>
+
+#define SCR_VIDEOMEM 0xb8000
+
+#define SCR_REAL_WIDTH 80
+
+#define SCR_WIDTH 78
+#define SCR_HEIGHT 24
+
+#define SCR_CTRL 0x3D4
+#define SCR_DATA 0x3D5
+
+// colors
+#define SCR_BLACK 0x0
+#define SCR_BLUE 0x1
+#define SCR_GREEN 0x2
+#define SCR_CYAN 0x3
+#define SCR_RED 0x4
+#define SCR_VIOLET 0x5
+#define SCR_BROWN 0x6
+#define SCR_GRAY1 0x7
+#define SCR_GRAY2 0x8
+#define SCR_LBLUE 0x9
+#define SCR_LGREEN 0xa
+#define SCR_LCYAN 0xb
+#define SCR_LRED 0xc
+#define SCR_PINK 0xd
+#define SCR_YELLOW 0xe
+#define SCR_WHITE 0xf
+
+//autoscroll
+void update_cursor(uint32_t col,uint32_t row);
+void console_put_char(uint8_t c,uint8_t color, uint32_t x, uint32_t y);
+
+#endif
diff --git a/driver/keyboard.c b/driver/keyboard.c
new file mode 100644
index 0000000..4366127
--- /dev/null
+++ b/driver/keyboard.c
@@ -0,0 +1,226 @@
+#include <stdbool.h>
+
+#define FOOLOS_MODULE_NAME "keyboard"
+
+#include "x86.h"
+
+/// idiots keyboard driver ////
+// http://www.computer-engineering.org/ps2keyboard/scancodes1.html
+
+static bool ctrl_l=false;
+static bool shift_l=false;
+static bool shift_r=false;
+static bool capslock=false;
+
+static void (*put)(uint8_t c);
+
+void keyboard_init(void (*func)(uint8_t c))
+{
+ put=func;
+}
+
+void keyboard_handle(uint8_t in)
+{
+
+ uint8_t make_alpha[]={
+ 0x1e, // A
+ 0x30, // B
+ 0x2e, // C
+ 0x20, // D
+ 0x12, // E
+ 0x21, // F
+ 0x22, // G
+ 0x23, // H
+ 0x17, // I
+ 0x24, // J
+ 0x25, // K
+ 0x26, // L
+ 0x32, // M
+ 0x31, // N
+ 0x18, // O
+ 0x19, // P
+ 0x10, // Q
+ 0x13, // R
+ 0x1F, // S
+ 0x14, // T
+ 0x16, // U
+ 0x2F, // V
+ 0x11, // W
+ 0x2D, // X
+ 0x15, // Y
+ 0x2c, // Z
+ };
+
+ uint8_t break_alpha[]={
+ 0x9e, // A
+ 0xb0, // B
+ 0xae, // C
+ 0xa0, // D
+ 0x92, // E
+ 0xa1, // F
+ 0xa2, // G
+ 0xa3, // H
+ 0x97, // I
+ 0xa4, // J
+ 0xa5, // K
+ 0xa6, // L
+ 0xb2, // M
+ 0xb1, // N
+ 0x98, // O
+ 0x99, // P
+ 0x90, // Q
+ 0x93, // R
+ 0x9F, // S
+ 0x94, // T
+ 0x96, // U
+ 0xaF, // V
+ 0x91, // W
+ 0xaD, // X
+ 0x95, // Y
+ 0xac, // Z
+ };
+
+ uint8_t break_num[]={
+
+ 0x8b, //0
+ 0x82,
+ 0x83,
+ 0x84,
+ 0x85,
+ 0x86,
+ 0x87,
+ 0x88,
+ 0x89,
+ 0x8a, // 9
+
+ };
+
+
+
+
+
+ uint8_t break_key_enter=0x9c;
+ uint8_t break_key_space=0xb9;
+ uint8_t break_key_backspace=0x8e;
+
+ uint8_t make_key_shift_l=0x2a;
+ uint8_t break_key_shift_l=0xaa;
+
+ uint8_t make_key_shift_r=0x36;
+ uint8_t break_key_shift_r=0xb6;
+
+ uint8_t break_caps_lock=0xba;
+ uint8_t break_slash=0xb5;
+
+ uint8_t make_ctrl_l=0x1d;
+ uint8_t break_ctrl_l=0x9d;
+
+ if(make_key_shift_l==in)shift_l=true;
+ if(break_key_shift_l==in)shift_l=false;;
+
+ if(make_key_shift_r==in)shift_r=true;
+ if(break_key_shift_r==in)shift_r=false;;
+
+ if(make_ctrl_l==in)ctrl_l=true;
+ if(break_ctrl_l==in)ctrl_l=false;
+
+ if(break_caps_lock==in)capslock=!capslock;
+
+
+ char ascii;
+
+ bool match=false;
+ // optimize this!
+ if(ctrl_l)
+ {
+ if(in==0xa0)
+ {
+ ascii=4; //end of transmission ctrl-d
+ match=true;
+ }
+ }
+ if(break_slash==in)
+ {
+ ascii='/';
+ match=true;
+ }
+ else if(break_key_space==in)
+ {
+ ascii=' ';
+ match=true;
+ }
+ else if(in==0xB4)
+ {
+ ascii='.';
+ match=true;
+ }
+ else if(in==0xB3)
+ {
+ ascii=',';
+ match=true;
+ }
+ else if(in==0x8D)
+ {
+ ascii='+';
+ match=true;
+ }
+ else if(in==0x8C)
+ {
+ ascii='-';
+ match=true;
+ }
+ else if(in==0x9A)
+ {
+ ascii='[';
+ match=true;
+ }
+ else if(in==0x9B)
+ {
+ ascii=']';
+ match=true;
+ }
+ else if(break_key_backspace==in)
+ {
+ ascii=0x08;
+ match=true;
+ }
+
+ else if(break_key_enter==in)
+ {
+ ascii='\n';
+ match=true;
+ }
+
+ else for(int i=0;i<26;i++)
+ {
+ if(match)break;
+ if(break_alpha[i]==in)
+ {
+ ascii=('a'+i);
+
+ if(shift_l||shift_r||capslock) // capslock makes trouble :(
+ {
+ ascii=('A'+i);
+ }
+ match=true;
+ break;
+ }
+ }
+
+ for(int i=0;i<10;i++)
+ {
+ if(break_num[i]==in)
+ {
+ ascii=('0'+i);
+ match=true;
+ break;
+ }
+ }
+
+
+ if(match)
+ {
+ put(ascii);
+ }
+
+}
diff --git a/driver/keyboard.h b/driver/keyboard.h
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/driver/keyboard.h
@@ -0,0 +1 @@
+