added part 4
This commit is contained in:
51
Part4/10_scheduler/drivers/keyboard.c
Normal file
51
Part4/10_scheduler/drivers/keyboard.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "keyboard.h"
|
||||
#include "../cpu/ports.h"
|
||||
#include "../cpu/isr.h"
|
||||
#include "screen.h"
|
||||
#include "../libc/string.h"
|
||||
#include "../libc/function.h"
|
||||
#include "../kernel/kernel.h"
|
||||
|
||||
#define BACKSPACE 0x0E
|
||||
#define ENTER 0x1C
|
||||
|
||||
static char key_buffer[256];
|
||||
|
||||
#define SC_MAX 57
|
||||
const char *sc_name[] = { "ERROR", "Esc", "1", "2", "3", "4", "5", "6",
|
||||
"7", "8", "9", "0", "-", "=", "Backspace", "Tab", "Q", "W", "E",
|
||||
"R", "T", "Y", "U", "I", "O", "P", "[", "]", "Enter", "Lctrl",
|
||||
"A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "`",
|
||||
"LShift", "\\", "Z", "X", "C", "V", "B", "N", "M", ",", ".",
|
||||
"/", "RShift", "Keypad *", "LAlt", "Spacebar"};
|
||||
const char sc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
|
||||
'7', '8', '9', '0', '-', '=', '?', '?', 'Q', 'W', 'E', 'R', 'T', 'Y',
|
||||
'U', 'I', 'O', 'P', '[', ']', '?', '?', 'A', 'S', 'D', 'F', 'G',
|
||||
'H', 'J', 'K', 'L', ';', '\'', '`', '?', '\\', 'Z', 'X', 'C', 'V',
|
||||
'B', 'N', 'M', ',', '.', '/', '?', '?', '?', ' '};
|
||||
|
||||
static void keyboard_callback(registers_t regs) {
|
||||
/* The PIC leaves us the scancode in port 0x60 */
|
||||
u8 scancode = port_byte_in(0x60);
|
||||
|
||||
if (scancode > SC_MAX) return;
|
||||
if (scancode == BACKSPACE) {
|
||||
backspace(key_buffer);
|
||||
kprint_backspace();
|
||||
} else if (scancode == ENTER) {
|
||||
kprint("\n");
|
||||
user_input(key_buffer); /* kernel-controlled function */
|
||||
key_buffer[0] = '\0';
|
||||
} else {
|
||||
char letter = sc_ascii[(int)scancode];
|
||||
/* Remember that kprint only accepts char[] */
|
||||
char str[2] = {letter, '\0'};
|
||||
append(key_buffer, letter);
|
||||
kprint(str);
|
||||
}
|
||||
UNUSED(regs);
|
||||
}
|
||||
|
||||
void init_keyboard() {
|
||||
register_interrupt_handler(IRQ1, keyboard_callback);
|
||||
}
|
7
Part4/10_scheduler/drivers/keyboard.h
Normal file
7
Part4/10_scheduler/drivers/keyboard.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef KEYBOARD_H
|
||||
#define KEYBOARD_H
|
||||
#include "../cpu/types.h"
|
||||
|
||||
void init_keyboard();
|
||||
|
||||
#endif
|
149
Part4/10_scheduler/drivers/screen.c
Normal file
149
Part4/10_scheduler/drivers/screen.c
Normal file
@@ -0,0 +1,149 @@
|
||||
#include "screen.h"
|
||||
#include "../cpu/ports.h"
|
||||
#include "../libc/mem.h"
|
||||
|
||||
/* Declaration of private functions */
|
||||
int get_cursor_offset();
|
||||
void set_cursor_offset(int offset);
|
||||
int print_char(char c, int col, int row, char attr);
|
||||
int get_offset(int col, int row);
|
||||
int get_offset_row(int offset);
|
||||
int get_offset_col(int offset);
|
||||
|
||||
/**********************************************************
|
||||
* Public Kernel API functions *
|
||||
**********************************************************/
|
||||
|
||||
/**
|
||||
* Print a message on the specified location
|
||||
* If col, row, are negative, we will use the current offset
|
||||
*/
|
||||
void kprint_at(char *message, int col, int row) {
|
||||
/* Set cursor if col/row are negative */
|
||||
int offset;
|
||||
if (col >= 0 && row >= 0)
|
||||
offset = get_offset(col, row);
|
||||
else {
|
||||
offset = get_cursor_offset();
|
||||
row = get_offset_row(offset);
|
||||
col = get_offset_col(offset);
|
||||
}
|
||||
|
||||
/* Loop through message and print it */
|
||||
int i = 0;
|
||||
while (message[i] != 0) {
|
||||
offset = print_char(message[i++], col, row, WHITE_ON_BLACK);
|
||||
/* Compute row/col for next iteration */
|
||||
row = get_offset_row(offset);
|
||||
col = get_offset_col(offset);
|
||||
}
|
||||
}
|
||||
|
||||
void kprint(char *message) {
|
||||
kprint_at(message, -1, -1);
|
||||
}
|
||||
|
||||
void kprint_backspace() {
|
||||
int offset = get_cursor_offset()-2;
|
||||
int row = get_offset_row(offset);
|
||||
int col = get_offset_col(offset);
|
||||
print_char(0x08, col, row, WHITE_ON_BLACK);
|
||||
}
|
||||
|
||||
|
||||
/**********************************************************
|
||||
* Private kernel functions *
|
||||
**********************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* Innermost print function for our kernel, directly accesses the video memory
|
||||
*
|
||||
* If 'col' and 'row' are negative, we will print at current cursor location
|
||||
* If 'attr' is zero it will use 'white on black' as default
|
||||
* Returns the offset of the next character
|
||||
* Sets the video cursor to the returned offset
|
||||
*/
|
||||
int print_char(char c, int col, int row, char attr) {
|
||||
u8 *vidmem = (u8*) VIDEO_ADDRESS;
|
||||
if (!attr) attr = WHITE_ON_BLACK;
|
||||
|
||||
/* Error control: print a red 'E' if the coords aren't right */
|
||||
if (col >= MAX_COLS || row >= MAX_ROWS) {
|
||||
vidmem[2*(MAX_COLS)*(MAX_ROWS)-2] = 'E';
|
||||
vidmem[2*(MAX_COLS)*(MAX_ROWS)-1] = RED_ON_WHITE;
|
||||
return get_offset(col, row);
|
||||
}
|
||||
|
||||
int offset;
|
||||
if (col >= 0 && row >= 0) offset = get_offset(col, row);
|
||||
else offset = get_cursor_offset();
|
||||
|
||||
if (c == '\n') {
|
||||
row = get_offset_row(offset);
|
||||
offset = get_offset(0, row+1);
|
||||
} else if (c == 0x08) { /* Backspace */
|
||||
vidmem[offset] = ' ';
|
||||
vidmem[offset+1] = attr;
|
||||
} else {
|
||||
vidmem[offset] = c;
|
||||
vidmem[offset+1] = attr;
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
/* Check if the offset is over screen size and scroll */
|
||||
if (offset >= MAX_ROWS * MAX_COLS * 2) {
|
||||
int i;
|
||||
for (i = 1; i < MAX_ROWS; i++)
|
||||
memory_copy((u8*)(get_offset(0, i) + VIDEO_ADDRESS),
|
||||
(u8*)(get_offset(0, i-1) + VIDEO_ADDRESS),
|
||||
MAX_COLS * 2);
|
||||
|
||||
/* Blank last line */
|
||||
char *last_line = (char*) (get_offset(0, MAX_ROWS-1) + (u8*) VIDEO_ADDRESS);
|
||||
for (i = 0; i < MAX_COLS * 2; i++) last_line[i] = 0;
|
||||
|
||||
offset -= 2 * MAX_COLS;
|
||||
}
|
||||
|
||||
set_cursor_offset(offset);
|
||||
return offset;
|
||||
}
|
||||
|
||||
int get_cursor_offset() {
|
||||
/* Use the VGA ports to get the current cursor position
|
||||
* 1. Ask for high byte of the cursor offset (data 14)
|
||||
* 2. Ask for low byte (data 15)
|
||||
*/
|
||||
port_byte_out(REG_SCREEN_CTRL, 14);
|
||||
int offset = port_byte_in(REG_SCREEN_DATA) << 8; /* High byte: << 8 */
|
||||
port_byte_out(REG_SCREEN_CTRL, 15);
|
||||
offset += port_byte_in(REG_SCREEN_DATA);
|
||||
return offset * 2; /* Position * size of character cell */
|
||||
}
|
||||
|
||||
void set_cursor_offset(int offset) {
|
||||
/* Similar to get_cursor_offset, but instead of reading we write data */
|
||||
offset /= 2;
|
||||
port_byte_out(REG_SCREEN_CTRL, 14);
|
||||
port_byte_out(REG_SCREEN_DATA, (u8)(offset >> 8));
|
||||
port_byte_out(REG_SCREEN_CTRL, 15);
|
||||
port_byte_out(REG_SCREEN_DATA, (u8)(offset & 0xff));
|
||||
}
|
||||
|
||||
void clear_screen() {
|
||||
int screen_size = MAX_COLS * MAX_ROWS;
|
||||
int i;
|
||||
u8 *screen = (u8*) VIDEO_ADDRESS;
|
||||
|
||||
for (i = 0; i < screen_size; i++) {
|
||||
screen[i*2] = ' ';
|
||||
screen[i*2+1] = WHITE_ON_BLACK;
|
||||
}
|
||||
set_cursor_offset(get_offset(0, 0));
|
||||
}
|
||||
|
||||
|
||||
int get_offset(int col, int row) { return 2 * (row * MAX_COLS + col); }
|
||||
int get_offset_row(int offset) { return offset / (2 * MAX_COLS); }
|
||||
int get_offset_col(int offset) { return (offset - (get_offset_row(offset)*2*MAX_COLS))/2; }
|
22
Part4/10_scheduler/drivers/screen.h
Normal file
22
Part4/10_scheduler/drivers/screen.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef SCREEN_H
|
||||
#define SCREEN_H
|
||||
|
||||
#include "../cpu/types.h"
|
||||
|
||||
#define VIDEO_ADDRESS 0xb8000
|
||||
#define MAX_ROWS 25
|
||||
#define MAX_COLS 80
|
||||
#define WHITE_ON_BLACK 0x0f
|
||||
#define RED_ON_WHITE 0xf4
|
||||
|
||||
/* Screen i/o ports */
|
||||
#define REG_SCREEN_CTRL 0x3d4
|
||||
#define REG_SCREEN_DATA 0x3d5
|
||||
|
||||
/* Public kernel API */
|
||||
void clear_screen();
|
||||
void kprint_at(char *message, int col, int row);
|
||||
void kprint(char *message);
|
||||
void kprint_backspace();
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user