aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <frankhwei536@gmail.com>2015-02-18 12:49:58 -0500
committerFranklin Wei <frankhwei536@gmail.com>2015-02-18 12:49:58 -0500
commit9defae4d6f7b30d844447549fadffea4eab5a0dd (patch)
tree2c44f6fb193d9b3f7487714e7dfa3903bedb932d
parent1d3537f33d793e2cabe53e72f0e0ead911fcc870 (diff)
downloadkappa-9defae4d6f7b30d844447549fadffea4eab5a0dd.zip
kappa-9defae4d6f7b30d844447549fadffea4eab5a0dd.tar.gz
kappa-9defae4d6f7b30d844447549fadffea4eab5a0dd.tar.bz2
kappa-9defae4d6f7b30d844447549fadffea4eab5a0dd.tar.xz
support keyboard io
-rw-r--r--OBJ2
-rw-r--r--apps/plugin.c9
-rw-r--r--apps/plugin.h3
-rw-r--r--apps/xracer/graphics.c19
-rw-r--r--apps/xracer/main.c20
-rw-r--r--apps/xracer/xracer.h4
-rw-r--r--drivers/include/ps2.h11
-rw-r--r--drivers/include/ps2kbd.h19
-rw-r--r--drivers/ps2.c71
-rw-r--r--drivers/ps2kbd.c104
-rw-r--r--kernel/main.c12
11 files changed, 172 insertions, 102 deletions
diff --git a/OBJ b/OBJ
index 056e923..010a3f9 100644
--- a/OBJ
+++ b/OBJ
@@ -12,7 +12,7 @@ drivers/gfx.o
drivers/gfx-as.o
drivers/gfx_font.o
drivers/pcspkr.o
-drivers/ps2.o
+drivers/ps2kbd.o
drivers/tty.o
kernel/fpu.o
kernel/gdt-as.o
diff --git a/apps/plugin.c b/apps/plugin.c
index 440841a..fcb659d 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include "plugin.h"
#include "gfx.h"
+#include "ps2kbd.h"
static void plugin_clear(void)
{
@@ -22,6 +23,11 @@ static void plugin_hline(int a, int b, int c)
gfx_hline(a, b, c);
}
+static int button_get(void)
+{
+ return ps2kbd_button_get();
+}
+
static const struct plugin_api kappa_api = {
&plugin_clear,
&plugin_hline,
@@ -40,7 +46,8 @@ static const struct plugin_api kappa_api = {
&gfx_drawcircle,
&gfx_fillcircle,
&gfx_update,
- &gfx_putsxy
+ &gfx_putsxy,
+ &button_get
};
void plugin_load(enum plugin_status (*plugin)(const struct plugin_api*))
diff --git a/apps/plugin.h b/apps/plugin.h
index 5d62b93..849dda9 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -5,6 +5,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "gfx.h"
+#include "ps2kbd.h"
#include "timer.h"
#define LCD_WIDTH (*gfx_width)
@@ -48,6 +49,8 @@ struct plugin_api {
void (*lcd_fillcircle)(int x, int y, int r);
void (*lcd_update)(void);
void (*lcd_putsxy)(int x, int y, const char*);
+
+ int (*button_get)(void);
};
/* defined by the plugin */
diff --git a/apps/xracer/graphics.c b/apps/xracer/graphics.c
index eb31424..d523343 100644
--- a/apps/xracer/graphics.c
+++ b/apps/xracer/graphics.c
@@ -56,13 +56,18 @@ static inline int project(int cam_x, int cam_y, int cam_z, int camera_depth, str
rel_x = pt_3d->x - cam_x;
rel_y = pt_3d->y - cam_y;
rel_z = pt_3d->z - cam_z;
- float scale = (float)camera_depth / rel_z;
- pt_2d->x = (LCD_WIDTH/2) + (scale * rel_x * (LCD_WIDTH/2));
- pt_2d->y = (LCD_WIDTH/2) - (scale * rel_y * (LCD_HEIGHT/2));
- pt_2d->w = scale * ROAD_WIDTH * (LCD_WIDTH/2);
- if(scale_return)
- *scale_return=scale;
- return rel_z;
+ if(rel_z != 0)
+ {
+ float scale = (float)camera_depth / rel_z;
+ pt_2d->x = (LCD_WIDTH/2) + (scale * rel_x * (LCD_WIDTH/2));
+ pt_2d->y = (LCD_WIDTH/2) - (scale * rel_y * (LCD_HEIGHT/2));
+ pt_2d->w = scale * ROAD_WIDTH * (LCD_WIDTH/2);
+ if(scale_return)
+ *scale_return=scale;
+
+ return rel_z;
+ }
+ return -1;
}
static inline int border_width(int projected_road_width, int lanes)
diff --git a/apps/xracer/main.c b/apps/xracer/main.c
index 9a51a31..5c88c4f 100644
--- a/apps/xracer/main.c
+++ b/apps/xracer/main.c
@@ -93,9 +93,6 @@ enum plugin_status do_flythrough(void)
//road_length = load_external_map(road, MAX_ROAD_LENGTH, "/output.xrm");
- road_length = -1;
-
-
road_length = MAX_ROAD_LENGTH;
generate_random_road(road, road_length, HILLS, CURVES);
@@ -106,6 +103,23 @@ enum plugin_status do_flythrough(void)
while(1)
{
+ int button = rb->button_get();
+ switch(button)
+ {
+ case BUTTON_UP:
+ camera_height += MANUAL_SPEED;
+ break;
+ case BUTTON_DOWN:
+ camera_height -= MANUAL_SPEED;
+ break;
+ case BUTTON_LEFT:
+ camera.pos.x -= MANUAL_SPEED;
+ break;
+ case BUTTON_RIGHT:
+ camera.pos.x += MANUAL_SPEED;
+ break;
+ }
+
camera.pos.z += 512;
/* loop the track right before going off the "end" */
camera.pos.z %= (road_length - DRAW_DIST) * SEGMENT_LENGTH;
diff --git a/apps/xracer/xracer.h b/apps/xracer/xracer.h
index 21375e3..804bf39 100644
--- a/apps/xracer/xracer.h
+++ b/apps/xracer/xracer.h
@@ -51,7 +51,7 @@
#define LANES 3
/* camera parameters */
-#define DRAW_DIST 256
+#define DRAW_DIST 1024
/* NOTE: FOV is not used, the tangent value (see below) is */
#define CAMERA_FOV 120 /* in degrees */
@@ -60,7 +60,7 @@
/* game parameters */
/* currently this is how far the camera moves per keypress */
-#define MANUAL_SPEED 50
+#define MANUAL_SPEED 10
/* number of bits to use for the fractional part of fixed-point numbers */
/* note that FOV calculation always uses 14 by default */
diff --git a/drivers/include/ps2.h b/drivers/include/ps2.h
deleted file mode 100644
index 6f2c572..0000000
--- a/drivers/include/ps2.h
+++ /dev/null
@@ -1,11 +0,0 @@
-/* this is both a PS/2 keyboard AND a PS/2 MOUSE driver */
-
-#include <stdint.h>
-
-#define PS2_SCROLL_LOCK (1 << 0)
-#define PS2_NUM_LOCK (1 << 1)
-#define PS2_CAPS_LOCK (1 << 2)
-
-void ps2_set_leds(uint8_t status);
-
-void ps2_init(void);
diff --git a/drivers/include/ps2kbd.h b/drivers/include/ps2kbd.h
new file mode 100644
index 0000000..477882f
--- /dev/null
+++ b/drivers/include/ps2kbd.h
@@ -0,0 +1,19 @@
+/* this is both a PS/2 keyboard AND a PS/2 MOUSE driver */
+
+#include <stdint.h>
+
+#define PS2_SCROLL_LOCK (1 << 0)
+#define PS2_NUM_LOCK (1 << 1)
+#define PS2_CAPS_LOCK (1 << 2)
+
+#define BUTTON_UP (1 << 0)
+#define BUTTON_LEFT (1 << 1)
+#define BUTTON_DOWN (1 << 2)
+#define BUTTON_RIGHT (1 << 3)
+
+/* returns which arrow keys are down */
+uint8_t ps2kbd_button_get(void);
+
+void ps2kbd_set_leds(uint8_t status);
+
+void ps2kbd_init(void);
diff --git a/drivers/ps2.c b/drivers/ps2.c
deleted file mode 100644
index 9da026d..0000000
--- a/drivers/ps2.c
+++ /dev/null
@@ -1,71 +0,0 @@
-/* this is both a PS/2 keyboard AND a PS/2 MOUSE driver */
-#include <stdint.h>
-#include <stdio.h>
-#include "io.h"
-#include "isr.h"
-#include "ps2.h"
-#include "ps2_keymaps.h"
-
-static void ps2_wait(void)
-{
- /* wait for the keyboard */
- while(1)
- if ((inb(0x64) & 2) == 0) break;
-}
-
-void ps2_set_leds(uint8_t status)
-{
- ps2_wait();
- outb(0x60, 0xED);
- outb(0x60, status);
-}
-
-static uint8_t keyboard_state[16];
-
-static void key_handler(struct regs_t *regs)
-{
- (void) regs;
- uint8_t scancode = inb(0x60);
- (void) scancode;
- /* TODO: handle scancode */
- /*printf("key %x\n", scancode);*/
-}
-
-static void ps2_set_scancode_set(uint8_t set)
-{
- ps2_wait();
- outb(0x60, 0xF0);
- outb(0x60, set);
-}
-
-static void keyboard_init(void)
-{
- set_interrupt_handler(IRQ(1), key_handler);
- ps2_set_scancode_set(1);
-}
-
-static void mouse_handler(struct regs_t *regs)
-{
- (void) regs;
- printf("mouse action!\n");
-}
-
-static void mouse_init(void)
-{
- /* enable IRQ12 */
- set_interrupt_handler(IRQ(12), mouse_handler);
- /* make the ps/2 controller generate IRQ12 */
- outb(0x64, 0x20);
- uint8_t status = inb(0x64);
- /* set bit 1 and unset bit 5 */
- status |= (1 << 1);
- status &= ~(1 << 5);
- outb(0x64, 0x60);
- outb(0x60, status);
-}
-
-void ps2_init(void)
-{
- keyboard_init();
- mouse_init();
-}
diff --git a/drivers/ps2kbd.c b/drivers/ps2kbd.c
new file mode 100644
index 0000000..5aa9d2a
--- /dev/null
+++ b/drivers/ps2kbd.c
@@ -0,0 +1,104 @@
+/* this is both a PS/2 keyboard driver */
+#include <stdint.h>
+#include <stdio.h>
+#include "io.h"
+#include "isr.h"
+#include "ps2kbd.h"
+#include "ps2_keymaps.h"
+
+static void ps2_wait(void)
+{
+ /* wait for the keyboard */
+ while(1)
+ if ((inb(0x64) & 2) == 0) break;
+}
+
+void ps2kbd_set_leds(uint8_t status)
+{
+ ps2_wait();
+ outb(0x60, 0xED);
+ outb(0x60, status);
+}
+
+#define IDX_UP 0
+#define IDX_LEFT 1
+#define IDX_DOWN 2
+#define IDX_RIGHT 3
+
+static uint8_t ps2_arrowkeys[4];
+
+uint8_t ps2kbd_button_get(void)
+{
+ uint8_t ret = 0;
+ if(ps2_arrowkeys[IDX_UP])
+ ret |= BUTTON_UP;
+ if(ps2_arrowkeys[IDX_LEFT])
+ ret |= BUTTON_LEFT;
+ if(ps2_arrowkeys[IDX_DOWN])
+ ret |= BUTTON_DOWN;
+ if(ps2_arrowkeys[IDX_RIGHT])
+ ret |= BUTTON_RIGHT;
+ return ret;
+}
+
+static void key_handler(struct regs_t *regs)
+{
+ (void) regs;
+ uint8_t scancode = inb(0x60);
+ switch(scancode)
+ {
+ /* ... the only one we care about! */
+ case 0xE0:
+ {
+ uint8_t spec = inb(0x60);
+ switch(spec)
+ {
+ case 0x48:
+ ps2_arrowkeys[IDX_UP] = 1;
+ break;
+ case 0x4B:
+ ps2_arrowkeys[IDX_LEFT] = 1;
+ break;
+ case 0x50:
+ ps2_arrowkeys[IDX_DOWN] = 1;
+ break;
+ case 0x4D:
+ ps2_arrowkeys[IDX_RIGHT] = 1;
+ break;
+ case 0xC8:
+ ps2_arrowkeys[IDX_UP] = 0;
+ break;
+ case 0xCB:
+ ps2_arrowkeys[IDX_LEFT] = 0;
+ break;
+ case 0xD0:
+ ps2_arrowkeys[IDX_DOWN] = 0;
+ break;
+ case 0xCD:
+ ps2_arrowkeys[IDX_RIGHT] = 0;
+ break;
+ }
+ break;
+ }
+ default:
+ break;
+ }
+}
+
+static void ps2_set_scancode_set(uint8_t set)
+{
+ ps2_wait();
+ outb(0x60, 0xF0);
+ outb(0x60, set);
+}
+
+static void keyboard_init(void)
+{
+ set_interrupt_handler(IRQ(1), key_handler);
+ ps2_set_scancode_set(1);
+}
+
+void ps2kbd_init(void)
+{
+ keyboard_init();
+}
diff --git a/kernel/main.c b/kernel/main.c
index 0ddf4e7..4c768b7 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -12,7 +12,7 @@
#include "multiboot.h"
#include "panic.h"
#include "pcspkr.h"
-#include "ps2.h"
+#include "ps2kbd.h"
#include "fpu.h"
#include "timer.h"
#include "tty.h"
@@ -78,7 +78,7 @@ void main(struct multiboot_info_t *hdr, uint32_t magic)
/* initialize other drivers */
timer_init(HZ);
- ps2_init();
+ ps2kbd_init();
set_interrupt_handler(0, div0);
set_interrupt_handler(0xd, gpf);
@@ -225,7 +225,7 @@ void main(struct multiboot_info_t *hdr, uint32_t magic)
while(1)
{
- ps2_set_leds(PS2_NUM_LOCK);
+ ps2kbd_set_leds(PS2_NUM_LOCK);
timer_delay(HZ/4);
if(s < 0)
putchar('\b');
@@ -234,7 +234,7 @@ void main(struct multiboot_info_t *hdr, uint32_t magic)
n+=s;
if(n<=0 || n>=3)
s=-s;
- ps2_set_leds(PS2_CAPS_LOCK);
+ ps2kbd_set_leds(PS2_CAPS_LOCK);
timer_delay(HZ/4);
if(s < 0)
putchar('\b');
@@ -243,7 +243,7 @@ void main(struct multiboot_info_t *hdr, uint32_t magic)
n+=s;
if(n<=0 || n>=3)
s=-s;
- ps2_set_leds(PS2_SCROLL_LOCK);
+ ps2kbd_set_leds(PS2_SCROLL_LOCK);
timer_delay(HZ/4);
if(s < 0)
putchar('\b');
@@ -252,7 +252,7 @@ void main(struct multiboot_info_t *hdr, uint32_t magic)
n+=s;
if(n<=0 || n>=3)
s=-s;
- ps2_set_leds(PS2_CAPS_LOCK);
+ ps2kbd_set_leds(PS2_CAPS_LOCK);
timer_delay(HZ/4);
if(s < 0)
putchar('\b');