shift done, backspace done

This commit is contained in:
Андреев Григорий 2026-04-04 12:36:25 +03:00
parent 79f4d25c27
commit 2503938fc4
7 changed files with 56 additions and 33 deletions

View File

@ -27,7 +27,7 @@ bootmain(void)
void (*entry)(void); void (*entry)(void);
uchar* pa; uchar* pa;
elf = (Elf32_Ehdr*)0x10000; // scratch space elf = (Elf32_Ehdr*)0x20000; // scratch space
// Read 1st page off disk // Read 1st page off disk
readseg((uchar*)elf, 4096, 0); readseg((uchar*)elf, 4096, 0);

View File

@ -2,7 +2,7 @@
#define KERNBASE 0x80000000 #define KERNBASE 0x80000000
#define PGSIZE 0x1000 #define PGSIZE 0x1000
#define PHYSTOP 0x8000000 #define PHYSTOP 0x8000000 // 128 Mib
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1)) #define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
#define PGROUNDDOWN(a) (((a)) & ~((uintptr_t)(PGSIZE-1))) #define PGROUNDDOWN(a) (((a)) & ~((uintptr_t)(PGSIZE-1)))

View File

@ -41,4 +41,8 @@ uint8_t kbd_take_from_copy_buffer() {
bool kbd_can_take_from_copy_buffer() { bool kbd_can_take_from_copy_buffer() {
return kbd_state_shrd.copy_len > 0; return kbd_state_shrd.copy_len > 0;
}
bool kbd_is_pressed(uint8_t code) {
check(code < 128);
return kbd_state_shrd.copy_pressed[code >> 3] & (1u << (code & 0x7));
} }

View File

@ -16,6 +16,7 @@ typedef struct {
#define KEYCODE_SHIFT 42 #define KEYCODE_SHIFT 42
#define KEYCODE_ENTER 28 #define KEYCODE_ENTER 28
#define KEYCODE_BACKSPACE 14
/* decoding */ /* decoding */
static const char keysym_mapped_ascii_lower[] = { static const char keysym_mapped_ascii_lower[] = {
@ -40,4 +41,5 @@ extern keyboard_interrupt_shared_t kbd_state_shrd;
void init_keyboard(); void init_keyboard();
uint8_t kbd_take_from_copy_buffer(); uint8_t kbd_take_from_copy_buffer();
bool kbd_can_take_from_copy_buffer(); bool kbd_can_take_from_copy_buffer();
bool kbd_is_pressed(uint8_t code);

View File

@ -1,3 +1,4 @@
#include "vga.h"
#include "port.h" #include "port.h"
#include "../lib/string.h" #include "../lib/string.h"
#include "cpu/memlayout.h" #include "cpu/memlayout.h"
@ -27,22 +28,12 @@ static unsigned char get_color(unsigned char fg, unsigned char bg) {
return (bg << 4) + fg; return (bg << 4) + fg;
} }
enum { unsigned vga_get_offset(unsigned col, unsigned row) {
ROWS = 25, return row * VGA_COLS + col;
COLS = 80,
VGA_CTRL_REGISTER = 0x3d4,
VGA_DATA_REGISTER = 0x3d5,
VGA_OFFSET_LOW = 0x0f,
VGA_OFFSET_HIGH = 0x0e,
};
static unsigned get_offset(unsigned col, unsigned row) {
return row * COLS + col;
} }
static unsigned get_row_from_offset(unsigned offset) { unsigned vga_get_row_from_offset(unsigned offset) {
return offset / COLS; return offset / VGA_COLS;
} }
void vga_set_cursor(unsigned offset) { void vga_set_cursor(unsigned offset) {
@ -66,31 +57,31 @@ void vga_set_char(unsigned offset, char c) {
} }
void vga_clear_screen() { void vga_clear_screen() {
for (unsigned i = 0; i < ROWS * COLS; ++i) { for (unsigned i = 0; i < VGA_ROWS * VGA_COLS; ++i) {
vga_set_char(i, ' '); vga_set_char(i, ' ');
} }
vga_set_cursor(0); vga_set_cursor(0);
} }
static unsigned scroll() { static unsigned scroll() {
kmemmove(video_memory, video_memory + 2 * COLS, 2 * COLS * (ROWS-1)); kmemmove(video_memory, video_memory + 2 * VGA_COLS, 2 * VGA_COLS * (VGA_ROWS-1));
for (int col = 0; col < COLS; col++) { for (int col = 0; col < VGA_COLS; col++) {
vga_set_char(get_offset(col, ROWS - 1), ' '); vga_set_char(vga_get_offset(col, VGA_ROWS - 1), ' ');
} }
return get_offset(0, ROWS - 1); return vga_get_offset(0, VGA_ROWS - 1);
} }
void vga_print_string(const char* s) { void vga_print_string(const char* s) {
unsigned offset = vga_get_cursor(); unsigned offset = vga_get_cursor();
while (*s != 0) { while (*s != 0) {
if (*s == '\n') { if (*s == '\n') {
offset = get_offset(0, get_row_from_offset(offset) + 1); offset = vga_get_offset(0, vga_get_row_from_offset(offset) + 1);
} else { } else {
vga_set_char(offset, *s); vga_set_char(offset, *s);
offset++; offset++;
} }
s++; s++;
if (offset >= COLS * ROWS) { if (offset >= VGA_COLS * VGA_ROWS) {
offset = scroll(); offset = scroll();
} }
} }

View File

@ -1,7 +1,26 @@
#pragma once #pragma once
void vga_clear_screen(); #include <stdint.h>
enum {
VGA_ROWS = 25,
VGA_COLS = 80,
VGA_CTRL_REGISTER = 0x3d4,
VGA_DATA_REGISTER = 0x3d5,
VGA_OFFSET_LOW = 0x0f,
VGA_OFFSET_HIGH = 0x0e,
};
unsigned vga_get_cursor();
unsigned vga_get_row_from_offset(unsigned offset);
unsigned vga_get_offset(unsigned col, unsigned row);
void vga_set_cursor(unsigned offset);
void vga_clear_screen();
void vga_set_char(unsigned offset, char c);
void vga_print_string(const char* s); void vga_print_string(const char* s);
void vgaMode13(); void vgaMode13();

View File

@ -36,14 +36,24 @@ void read_line_input(char* buf, size_t* ret_len, size_t cap) {
while (kbd_can_take_from_copy_buffer()) { while (kbd_can_take_from_copy_buffer()) {
uint8_t keycode = kbd_take_from_copy_buffer(); uint8_t keycode = kbd_take_from_copy_buffer();
bool shift_pressed = kbd_state_shrd.copy_pressed[42 >> 3] & (42 & 0x7); bool shift_pressed = kbd_is_pressed(KEYCODE_SHIFT);
const size_t keycodes = sizeof(keysym_mapped_ascii_upper); const size_t keycodes = sizeof(keysym_mapped_ascii_upper);
check(keycodes == sizeof(keysym_mapped_ascii_lower)); check(keycodes == sizeof(keysym_mapped_ascii_lower));
if (keycode == KEYCODE_ENTER) { if (keycode == KEYCODE_ENTER) {
goto end; goto end;
} } else if (keycode == KEYCODE_BACKSPACE) {
if (keycode < keycodes && keycode) { unsigned scr_offset = vga_get_cursor();
if (len > 0 && scr_offset > 0) {
// if (scr_offset % VGA_COLS) {
// vga_set
// }
// vga_set_char()
vga_set_char(scr_offset - 1, ' ');
vga_set_cursor(scr_offset - 1);
len--;
}
} else if (keycode < keycodes) {
char ch = shift_pressed ? keysym_mapped_ascii_upper[keycode] : char ch = shift_pressed ? keysym_mapped_ascii_upper[keycode] :
keysym_mapped_ascii_lower[keycode]; keysym_mapped_ascii_lower[keycode];
if (ch != 0) { if (ch != 0) {
@ -61,8 +71,6 @@ void read_line_input(char* buf, size_t* ret_len, size_t cap) {
asm("hlt"); asm("hlt");
size_t count = kbd_state_shrd.len; size_t count = kbd_state_shrd.len;
if (count > 0) { if (count > 0) {
// char haha[2] = {count+'0', 0};
// printk(haha);
cli(); cli();
size_t rem = KEYBOARD_INTERRUPT_BUF_CAP - kbd_state_shrd.copy_len; size_t rem = KEYBOARD_INTERRUPT_BUF_CAP - kbd_state_shrd.copy_len;
size_t copying = rem < count ? rem : count; size_t copying = rem < count ? rem : count;
@ -79,9 +87,9 @@ void read_line_input(char* buf, size_t* ret_len, size_t cap) {
void kmain() { void kmain() {
freerange(P2V(1u<<20), P2V(2u<<20)); // 1MB - 2MB freerange(P2V(2u<<20), P2V(3u<<20));
kvmalloc(); // map all of physical memory at KERNBASE kvmalloc(); // map all of physical memory at KERNBASE
freerange(P2V(2u<<20), P2V(PHYSTOP)); freerange(P2V(3u<<20), P2V(PHYSTOP));
load_gdt(); load_gdt();
init_keyboard(); init_keyboard();
@ -116,7 +124,6 @@ void kmain() {
const char* cmd = input + 4; const char* cmd = input + 4;
run_elf(cmd); run_elf(cmd);
} else { } else {
printk("as\n");
printk("unknown command, try: halt | run CMD\n"); printk("unknown command, try: halt | run CMD\n");
} }
} }