Johannes Zellner 8 сар өмнө
parent
commit
1dabfd0009
7 өөрчлөгдсөн 297 нэмэгдсэн , 7 устгасан
  1. 13 3
      Makefile
  2. 26 4
      io.c
  3. 2 0
      io.h
  4. 16 0
      prog_api.h
  5. 8 0
      run.c
  6. 211 0
      space.c
  7. 21 0
      terminal.c

+ 13 - 3
Makefile

@@ -53,7 +53,7 @@ ls.o: ls.c fat.h io.h
 cat.o: cat.c fat.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-run.o: run.c fat.h io.h fbtext.h prog_api.h
+run.o: run.c fat.h io.h fbtext.h prog_api.h timer.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 rtc.o: rtc.c rtc.h
@@ -117,16 +117,26 @@ prog.elf: prog.o
 prog.bin: prog.elf
 	$(OBJCOPY) -O binary $< $@
 
-root.img: prog.bin
+space.o: space.c prog_api.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+space.elf: space.o
+	$(LD) -T user.ld -nostdlib -m elf_i386 -o $@ $<
+
+space.bin: space.elf
+	$(OBJCOPY) -O binary $< $@
+
+root.img: prog.bin space.bin
 	dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
 	mkfs.fat -F 12 -n "ROOTDISK" $@
 	echo "Hello from the second floppy!" | mcopy -i $@ - ::HELLO.TXT
 	echo "This is a test file on drive 1." | mcopy -i $@ - ::TEST.TXT
 	mcopy -i $@ prog.bin ::PROG.BIN
+	mcopy -i $@ space.bin ::SPACE.BIN
 
 run: image.bin root.img
 	qemu-system-i386 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
 	qemu-system-x86_64 -M pc -drive file=image.bin,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img prog.o prog.elf prog.bin
+	rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o vga.o ati.o font_8x8.o fbtext.o clock.o colors.o display.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 26 - 4
io.c

@@ -215,11 +215,9 @@ void io_drain_keyboard(void)
 	kbd_extended = 0;
 }
 
-int io_poll_key(void)
+/* Process one scancode byte; shared by io_poll_key and io_poll_key_with_hw. */
+static int kbd_process_byte(unsigned char sc)
 {
-	unsigned char sc;
-	if (!kbd_buf_get(&sc))
-		return 0;
 	if (sc == 0xE0) {
 		kbd_extended = 1;
 		return 0;
@@ -250,6 +248,30 @@ int io_poll_key(void)
 	return 0;
 }
 
+int io_poll_key(void)
+{
+	unsigned char sc;
+	if (!kbd_buf_get(&sc))
+		return 0;
+	return kbd_process_byte(sc);
+}
+
+/* Like io_poll_key but also reads from port 0x60 when buffer is empty.
+   For use by full-screen programs (e.g. space.bin) where IRQ may be missed on real HW. */
+int io_poll_key_with_hw(void)
+{
+	unsigned char sc;
+	if (kbd_tail == kbd_head) {
+		if (!(inb(KB_STATUS) & 1))
+			return 0;
+		sc = inb(KB_DATA);
+	} else {
+		if (!kbd_buf_get(&sc))
+			return 0;
+	}
+	return kbd_process_byte(sc);
+}
+
 unsigned char io_last_scancode(void)
 {
 	return last_scancode;

+ 2 - 0
io.h

@@ -31,6 +31,8 @@ void io_drain_keyboard(void);
 #define IO_KEY_LEFT  0x100
 #define IO_KEY_RIGHT 0x101
 int io_poll_key(void);
+/* Same as io_poll_key but also reads port 0x60 when buffer empty (for full-screen programs). */
+int io_poll_key_with_hw(void);
 unsigned char io_last_scancode(void);
 
 #endif

+ 16 - 0
prog_api.h

@@ -5,6 +5,9 @@
 
 #define PROGRAM_API_ADDR 0x20F00
 #define IO_KEY_ESC       0x1B
+#define IO_KEY_SPACE     0x20
+#define IO_KEY_LEFT      0x100
+#define IO_KEY_RIGHT     0x101
 
 /* Function pointer types for the program API. */
 typedef void (*prog_draw_string_xy_t)(unsigned int x0, unsigned int y0,
@@ -13,12 +16,25 @@ typedef void (*prog_draw_string_xy_t)(unsigned int x0, unsigned int y0,
 typedef unsigned int (*prog_get_cols_t)(void);
 typedef unsigned int (*prog_get_rows_t)(void);
 typedef int (*prog_poll_key_t)(void);
+typedef int (*prog_poll_key_with_hw_t)(void);
+typedef unsigned int (*prog_get_fb_width_t)(void);
+typedef unsigned int (*prog_get_fb_height_t)(void);
+typedef void (*prog_clear_rgb_t)(unsigned int r, unsigned int g, unsigned int b);
+typedef void (*prog_put_pixel_rgb_t)(unsigned int x, unsigned int y,
+	unsigned int r, unsigned int g, unsigned int b);
+typedef unsigned int (*prog_get_ticks_ms_t)(void);
 
 struct prog_api {
 	prog_draw_string_xy_t draw_string_xy;
 	prog_get_cols_t       get_cols;
 	prog_get_rows_t       get_rows;
 	prog_poll_key_t       poll_key;
+	prog_poll_key_with_hw_t poll_key_with_hw;
+	prog_get_fb_width_t   get_fb_width;
+	prog_get_fb_height_t  get_fb_height;
+	prog_clear_rgb_t      clear_rgb;
+	prog_put_pixel_rgb_t  put_pixel_rgb;
+	prog_get_ticks_ms_t   get_ticks_ms;
 };
 
 #endif

+ 8 - 0
run.c

@@ -4,6 +4,8 @@
 #include "fbtext.h"
 #include "io.h"
 #include "prog_api.h"
+#include "timer.h"
+#include "vga.h"
 
 #define PROGRAM_LOAD_ADDR 0x21000
 #define PROGRAM_LOAD_MAX  0x10000
@@ -69,6 +71,12 @@ void program_run_main(const char *filename)
 			.get_cols       = fbtext_get_cols,
 			.get_rows       = fbtext_get_rows,
 			.poll_key       = io_poll_key,
+			.poll_key_with_hw = io_poll_key_with_hw,
+			.get_fb_width   = vga_fb_width,
+			.get_fb_height  = vga_fb_height,
+			.clear_rgb      = vga_clear_rgb,
+			.put_pixel_rgb  = vga_put_pixel_rgb,
+			.get_ticks_ms   = get_ticks_ms,
 		};
 		*(struct prog_api *)PROGRAM_API_ADDR = api;
 	}

+ 211 - 0
space.c

@@ -0,0 +1,211 @@
+/* Simple Space Invaders: pixel graphics, keyboard input. Uses kernel API at PROGRAM_API_ADDR. */
+
+#include "prog_api.h"
+
+#define INV_ROWS      5
+#define INV_COLS      10
+#define INV_W         10
+#define INV_H         8
+#define SHIP_W        16
+#define SHIP_H        8
+#define BULLET_W      2
+#define BULLET_H      6
+#define MAX_BULLETS   4
+#define FRAME_MS      33
+#define INV_STEP      4
+#define BULLET_SPEED  6
+#define SHIP_SPEED    6
+
+static const struct prog_api *const api = (const struct prog_api *)PROGRAM_API_ADDR;
+
+/* Row colors: red, green, yellow, cyan, magenta */
+static const unsigned int row_r[INV_ROWS] = { 255, 0, 255, 0, 255 };
+static const unsigned int row_g[INV_ROWS] = { 0, 255, 255, 255, 0 };
+static const unsigned int row_b[INV_ROWS] = { 0, 0, 0, 255, 255 };
+
+static unsigned int fb_w, fb_h;
+static int ship_x;
+static int inv_x, inv_y;
+static int inv_dir;
+static unsigned char alive[INV_ROWS][INV_COLS];
+static int num_alive;
+
+static int bullet_x[MAX_BULLETS];
+static int bullet_y[MAX_BULLETS];
+static int bullet_active[MAX_BULLETS];
+static int num_bullets;
+
+static void put_rect(int x0, int y0, int w, int h, unsigned int r, unsigned int g, unsigned int b)
+{
+	int x, y;
+	for (y = 0; y < h; y++)
+		for (x = 0; x < w; x++)
+			api->put_pixel_rgb((unsigned int)(x0 + x), (unsigned int)(y0 + y), r, g, b);
+}
+
+static void draw_ship(void)
+{
+	int x = ship_x;
+	int y = (int)fb_h - SHIP_H - 4;
+	put_rect(x, y, SHIP_W, SHIP_H, 0, 255, 128);
+}
+
+static void draw_invader(int row, int col)
+{
+	int x = inv_x + col * (INV_W + 4);
+	int y = inv_y + row * (INV_H + 2);
+	if (!alive[row][col])
+		return;
+	put_rect(x, y, INV_W, INV_H, row_r[row], row_g[row], row_b[row]);
+}
+
+static void draw_bullets(void)
+{
+	int i;
+	for (i = 0; i < MAX_BULLETS; i++)
+		if (bullet_active[i])
+			put_rect(bullet_x[i], bullet_y[i], BULLET_W, BULLET_H, 255, 255, 255);
+}
+
+static void spawn_bullet(void)
+{
+	int i;
+	for (i = 0; i < MAX_BULLETS; i++) {
+		if (!bullet_active[i]) {
+			bullet_x[i] = ship_x + SHIP_W / 2 - BULLET_W / 2;
+			bullet_y[i] = (int)fb_h - SHIP_H - 4 - BULLET_H;
+			bullet_active[i] = 1;
+			num_bullets++;
+			break;
+		}
+	}
+}
+
+static void update_bullets(void)
+{
+	int i, r, c;
+	int bx, by, ix, iy;
+
+	for (i = 0; i < MAX_BULLETS; i++) {
+		if (!bullet_active[i])
+			continue;
+		bullet_y[i] -= BULLET_SPEED;
+		if (bullet_y[i] < 0) {
+			bullet_active[i] = 0;
+			num_bullets--;
+			continue;
+		}
+		bx = bullet_x[i];
+		by = bullet_y[i];
+		for (r = 0; r < INV_ROWS; r++) {
+			for (c = 0; c < INV_COLS; c++) {
+				if (!alive[r][c])
+					continue;
+				ix = inv_x + c * (INV_W + 4);
+				iy = inv_y + r * (INV_H + 2);
+				if (bx + BULLET_W > ix && bx < ix + INV_W &&
+				    by + BULLET_H > iy && by < iy + INV_H) {
+					alive[r][c] = 0;
+					num_alive--;
+					bullet_active[i] = 0;
+					num_bullets--;
+					goto next_bullet;
+				}
+			}
+		}
+next_bullet:;
+	}
+}
+
+static void update_invaders(void)
+{
+	int edge;
+
+	inv_x += inv_dir * INV_STEP;
+	edge = inv_x + INV_COLS * (INV_W + 4) - 4;
+	if (inv_dir > 0 && edge >= (int)fb_w) {
+		inv_dir = -1;
+		inv_y += INV_H + 4;
+		inv_x = (int)fb_w - (INV_COLS * (INV_W + 4) - 4);
+		if (inv_x < 0)
+			inv_x = 0;
+	} else if (inv_dir < 0 && inv_x <= 0) {
+		inv_dir = 1;
+		inv_y += INV_H + 4;
+		inv_x = 0;
+	}
+}
+
+static void game_init(void)
+{
+	int r, c;
+	fb_w = api->get_fb_width();
+	fb_h = api->get_fb_height();
+	if (fb_w < 100)
+		fb_w = 320;
+	if (fb_h < 100)
+		fb_h = 200;
+
+	ship_x = (int)fb_w / 2 - SHIP_W / 2;
+	if (ship_x < 0)
+		ship_x = 0;
+
+	inv_x = 20;
+	inv_y = 20;
+	inv_dir = 1;
+	num_alive = INV_ROWS * INV_COLS;
+	for (r = 0; r < INV_ROWS; r++)
+		for (c = 0; c < INV_COLS; c++)
+			alive[r][c] = 1;
+
+	num_bullets = 0;
+	for (c = 0; c < MAX_BULLETS; c++)
+		bullet_active[c] = 0;
+}
+
+void _start(void)
+{
+	unsigned int last_tick;
+	int k;
+	int r, c;
+
+	game_init();
+	last_tick = api->get_ticks_ms();
+
+	for (;;) {
+		int want_left = 0, want_right = 0, want_fire = 0;
+		while (api->get_ticks_ms() - last_tick < FRAME_MS) {
+			k = api->poll_key_with_hw();
+			if (k == IO_KEY_ESC)
+				return;
+			if (k == IO_KEY_LEFT)
+				want_left = 1;
+			if (k == IO_KEY_RIGHT)
+				want_right = 1;
+			if (k == IO_KEY_SPACE)
+				want_fire = 1;
+		}
+		last_tick = api->get_ticks_ms();
+		if (want_left && ship_x > 0)
+			ship_x -= SHIP_SPEED;
+		if (want_right && ship_x + SHIP_W < (int)fb_w)
+			ship_x += SHIP_SPEED;
+		if (want_fire)
+			spawn_bullet();
+
+		update_bullets();
+		update_invaders();
+
+		api->clear_rgb(8, 8, 32);
+		draw_ship();
+		for (r = 0; r < INV_ROWS; r++)
+			for (c = 0; c < INV_COLS; c++)
+				draw_invader(r, c);
+		draw_bullets();
+
+		if (num_alive <= 0)
+			return;
+		if (inv_y + INV_ROWS * (INV_H + 2) >= (int)fb_h - SHIP_H - 8)
+			return;
+	}
+}

+ 21 - 0
terminal.c

@@ -168,6 +168,22 @@ static int is_cmd_display(const char *line, unsigned int line_len)
 	return (i == line_len);
 }
 
+static int is_cmd_space(const char *line, unsigned int line_len)
+{
+	unsigned int i = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 's' || line[i+1] != 'p' || line[i+2] != 'a' ||
+	    line[i+3] != 'c' || line[i+4] != 'e')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 #define CAT_FNAME_MAX 64
 static int is_cmd_cat(const char *line, unsigned int line_len, char *filename_buf, unsigned int buf_size)
 {
@@ -353,6 +369,10 @@ void terminal_run(void)
 						program_run_main(cat_fname);
 						io_drain_keyboard();
 						draw_header_row();
+					} else if (is_cmd_space(line, line_len)) {
+						program_run_main("space.bin");
+						io_drain_keyboard();
+						draw_header_row();
 					} else if (is_cmd_display(line, line_len)) {
 						program_display_main();
 					} else if (is_cmd_drive(line, line_len, &drive_sel)) {
@@ -368,6 +388,7 @@ void terminal_run(void)
 						io_putstr("colors\n");
 						io_putstr("cat <file>\n");
 						io_putstr("run <file>\n");
+						io_putstr("space\n");
 						io_putstr("display\n");
 						io_putstr("drive <0|1>\n");
 						io_putstr("help\n");