diff options
| author | Aleksa Vučković <aleksav013@gmail.com> | 2021-10-24 18:47:28 +0200 |
|---|---|---|
| committer | Aleksa Vučković <aleksav013@gmail.com> | 2021-10-24 18:47:28 +0200 |
| commit | 0bca634f7e70b05239f46f3bd40bb37468d67957 (patch) | |
| tree | c4c121dea59ff4183ffb78da4eba75b61f8c8891 | |
| parent | 1395dc42159d52ba36524fabe805897cd20bd01e (diff) | |
Fixing keyboard, changes to Makefile and string.h, trying to add heap...
| -rw-r--r-- | .gdbinit | 2 | ||||
| -rw-r--r-- | Makefile | 33 | ||||
| -rw-r--r-- | src/asm.h | 9 | ||||
| -rw-r--r-- | src/gdt.c | 4 | ||||
| -rw-r--r-- | src/heap.c | 167 | ||||
| -rw-r--r-- | src/heap.h | 23 | ||||
| -rw-r--r-- | src/idt.c | 4 | ||||
| -rw-r--r-- | src/kernel.c | 7 | ||||
| -rw-r--r-- | src/keyboard.c | 65 | ||||
| -rw-r--r-- | src/keyboard.h | 74 | ||||
| -rw-r--r-- | src/keymap.c | 173 | ||||
| -rw-r--r-- | src/stdio.c | 14 | ||||
| -rw-r--r-- | src/string.c | 83 | ||||
| -rw-r--r-- | src/string.h | 10 | ||||
| -rw-r--r-- | src/tty.c | 20 | ||||
| -rw-r--r-- | src/types.h | 8 | ||||
| -rw-r--r-- | src/vga.c | 24 |
17 files changed, 562 insertions, 158 deletions
diff --git a/.gdbinit b/.gdbinit deleted file mode 100644 index 5bf175c..0000000 --- a/.gdbinit +++ /dev/null @@ -1,2 +0,0 @@ -target remote localhost:1234 -file myiso.iso @@ -14,27 +14,29 @@ BUILD_DIR=build ISO_DIR=isodir TARGET=myos +BINARY=$(BUILD_DIR)/$(TARGET).bin +ISO=$(TARGET).iso -OBJ_FILES=boot.o kernel.o gdt.o idt.o keyboard.o vga.o string.o tty.o stdio.o +OBJ_FILES=boot.o kernel.o gdt.o idt.o keyboard.o keymap.o vga.o string.o tty.o stdio.o heap.o CRTBEGIN_OBJ=$(shell $(CC) -print-file-name=crtbegin.o) CRTEND_OBJ=$(shell $(CC) -print-file-name=crtend.o) OBJ=$(BUILD_DIR)/crti.o $(CRTBEGIN_OBJ) $(patsubst %,$(BUILD_DIR)/%,$(OBJ_FILES)) $(CRTEND_OBJ) $(BUILD_DIR)/crtn.o # Default action is set to making kernel binary -.PHONY: all run run-iso debug clean -all: $(BUILD_DIR)/$(TARGET).bin +.PHONY: all run run-iso clean +all: $(BINARY) # Creating iso file -$(TARGET).iso: $(BUILD_DIR)/$(TARGET).bin - grub-file --is-x86-multiboot $(BUILD_DIR)/myos.bin +$(ISO): $(BINARY) + grub-file --is-x86-multiboot $(BINARY) mkdir -p $(ISO_DIR)/boot/grub - $(CP) $(BUILD_DIR)/myos.bin $(ISO_DIR)/boot/myos.bin + $(CP) $(BINARY) $(ISO_DIR)/boot/$(TARGET).bin $(CP) $(SOURCE_DIR)/grub.cfg $(ISO_DIR)/boot/grub/grub.cfg - grub-mkrescue -o $(TARGET).iso $(ISO_DIR) + grub-mkrescue -o $(ISO) $(ISO_DIR) # Linking object files into kernel binary -$(BUILD_DIR)/$(TARGET).bin: $(OBJ) +$(BINARY): $(OBJ) $(CC) -T $(SOURCE_DIR)/linker.ld -o $@ $(CFLAGS) -nostdlib $^ -lgcc # Compiling as sources @@ -48,18 +50,13 @@ $(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c $(CC) -c $< -o $@ -std=gnu99 $(CFLAGS) # Boot kernel binary in qemu -run: $(BUILD_DIR)/$(TARGET).bin - $(QEMU) -kernel $^ +run: $(BINARY) + $(QEMU) -kernel $< # Boot iso in qemu -run-iso: $(TARGET).iso - $(QEMU) -cdrom $^ - -# Debug kernel binary in gdb -debug: $(TARGET).bin - $(QEMU) -kernel $^ -s -S & - gdb -x .gdbinit +run-iso: $(ISO) + $(QEMU) -cdrom $< # Clean build files clean: - $(RM) $(BUILD_DIR) $(ISO_DIR) $(TARGET).iso + $(RM) $(BUILD_DIR) $(ISO_DIR) $(ISO) diff --git a/src/asm.h b/src/asm.h new file mode 100644 index 0000000..1d61b88 --- /dev/null +++ b/src/asm.h @@ -0,0 +1,9 @@ +#ifndef ASM_H +#define ASM_H + +#include"types.h" + +extern uint8_t ioport_in(uint8_t port); +extern void ioport_out(uint8_t port, char data); + +#endif @@ -1,6 +1,4 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> +#include"types.h" struct gdt_entry { diff --git a/src/heap.c b/src/heap.c new file mode 100644 index 0000000..9b86ad6 --- /dev/null +++ b/src/heap.c @@ -0,0 +1,167 @@ +#include"types.h" + +/* + 2014 Leonard Kevin McGuire Jr (www.kmcg3413.net) (kmcg3413@gmail.com) + 2016 Clément Gallet (provided bug fixes) +*/ +typedef struct _KHEAPBLOCKBM { + struct _KHEAPBLOCKBM *next; + uint32_t size; + uint32_t used; + uint32_t bsize; + uint32_t lfb; +} KHEAPBLOCKBM; + +typedef struct _KHEAPBM { + KHEAPBLOCKBM *fblock; +} KHEAPBM; + +void k_heapBMInit(KHEAPBM *heap) { + heap->fblock = 0; +} + +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { + KHEAPBLOCKBM *b; + uint32_t bcnt; + uint32_t x; + uint8_t *bm; + + b = (KHEAPBLOCKBM*)addr; + b->size = size - sizeof(KHEAPBLOCKBM); + b->bsize = bsize; + + b->next = heap->fblock; + heap->fblock = b; + + bcnt = b->size / b->bsize; + bm = (uint8_t*)&b[1]; + + /* clear bitmap */ + for (x = 0; x < bcnt; ++x) { + bm[x] = 0; + } + + /* reserve room for bitmap */ + bcnt = (bcnt / bsize) * bsize < bcnt ? bcnt / bsize + 1 : bcnt / bsize; + for (x = 0; x < bcnt; ++x) { + bm[x] = 5; + } + + b->lfb = bcnt - 1; + + b->used = bcnt; + + return 1; +} + +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { + uint8_t c; + for (c = a + 1; c == b || c == 0; ++c); + return c; +} + +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { + KHEAPBLOCKBM *b; + uint8_t *bm; + uint32_t bcnt; + uint32_t x, y, z; + uint32_t bneed; + uint8_t nid; + + /* iterate blocks */ + for (b = heap->fblock; b; b = b->next) { + /* check if block has enough room */ + if (b->size - (b->used * b->bsize) >= size) { + + bcnt = b->size / b->bsize; + bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; + bm = (uint8_t*)&b[1]; + + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x < b->lfb; ++x) { + /* just wrap around */ + if (x >= bcnt) { + x = 0; + } + + if (bm[x] == 0) { + /* count free blocks */ + for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); + + /* we have enough, now allocate them */ + if (y == bneed) { + /* find ID that does not match left or right */ + nid = k_heapBMGetNID(bm[x - 1], bm[x + y]); + + /* allocate by setting id */ + for (z = 0; z < y; ++z) { + bm[x + z] = nid; + } + + /* optimization */ + b->lfb = (x + bneed) - 2; + + /* count used blocks NOT bytes */ + b->used += y; + + return (void*)(x * b->bsize + (uintptr_t)&b[1]); + } + + /* x will be incremented by one ONCE more in our FOR loop */ + x += (y - 1); + continue; + } + } + } + } + + return 0; +} + +void k_heapBMFree(KHEAPBM *heap, void *ptr) { + KHEAPBLOCKBM *b; + uintptr_t ptroff; + uint32_t bi, x; + uint8_t *bm; + uint8_t id; + uint32_t max; + + for (b = heap->fblock; b; b = b->next) { + if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { + /* found block */ + ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ + /* block offset in BM */ + bi = ptroff / b->bsize; + /* .. */ + bm = (uint8_t*)&b[1]; + /* clear allocation */ + id = bm[bi]; + /* oddly.. GCC did not optimize this */ + max = b->size / b->bsize; + for (x = bi; bm[x] == id && x < max; ++x) { + bm[x] = 0; + } + /* update free block count */ + b->used -= x - bi; + return; + } + } + + /* this error needs to be raised or reported somehow */ + return; +} + +KHEAPBM kheap; + +void heap() +{ + KHEAPBM kheap; + char *ptr; + + k_heapBMInit(&kheap); /* initialize the heap */ + k_heapBMAddBlock(&kheap, 0x100000, 0x100000, 16); /* add block to heap + (starting 1MB mark and length of 1MB) + with default block size of 16 bytes + */ + ptr = (char*)k_heapBMAlloc(&kheap, 256); /* allocate 256 bytes (malloc) */ + k_heapBMFree(&kheap, ptr); /* free the pointer (free) */ +} diff --git a/src/heap.h b/src/heap.h new file mode 100644 index 0000000..2f66501 --- /dev/null +++ b/src/heap.h @@ -0,0 +1,23 @@ +#include"types.h" + +typedef struct _KHEAPBLOCKBM { + struct _KHEAPBLOCKBM *next; + uint32_t size; + uint32_t used; + uint32_t bsize; + uint32_t lfb; +} KHEAPBLOCKBM; + +typedef struct _KHEAPBM { + KHEAPBLOCKBM *fblock; +} KHEAPBM; + +void k_heapBMInit(KHEAPBM *heap); +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize); +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size); +void k_heapBMFree(KHEAPBM *heap, void *ptr); + +extern KHEAPBM kheap; + +#define kmalloc k_heapBMAlloc +#define kfree k_heapBMFree @@ -1,6 +1,4 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> +#include"types.h" #define INTERRUPT_GATE_32 0x8e diff --git a/src/kernel.c b/src/kernel.c index 0ad3b66..b77bf2c 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1,13 +1,20 @@ +#include"heap.h" +#include"stdio.h" + void terminal_initialize(void); void init_idt_table(void); void init_keyboard(void); void prompt(void); +void *f(); + void kernel_main(void) { terminal_initialize(); init_idt_table(); init_keyboard(); + k_heapBMInit(&kheap); + prompt(); while(1) __asm__("hlt\n\t"); } diff --git a/src/keyboard.c b/src/keyboard.c index 896b61f..c945acd 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -1,7 +1,5 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> -#include"keyboard.h" +#include"types.h" +#include"asm.h" #include"stdio.h" #define BUFFER_SIZE 200 @@ -16,19 +14,22 @@ size_t buffer_index=0; #define KEYBOARD_DATA_PORT 0x60 #define KEYBOARD_STATUS_PORT 0x64 -extern char ioport_in(uint8_t port); -extern void ioport_out(uint8_t port, char data); - void previous_field(void); void terminal_putchar(char c); void tty(char *buffer); void prompt(void); +void clear(); +void us_en(char keymap[]); + +char charcode[256]; +bool ispressed[128]; void init_keyboard() { // 0xFD = 1111 1101 in binary. enables only IRQ1 // Why IRQ1? Remember, IRQ0 exists, it's 0-based ioport_out(PIC1_DATA_PORT, 0xFD); + us_en(charcode); } void backspace() @@ -55,24 +56,54 @@ void enter() return; } +void space() +{ + buffer[buffer_index++]=' '; + printf(" "); +} +#define lshift ispressed[0x2A] +#define lctrl ispressed[0x1D] + void handle_keyboard_interrupt() { - // Write end of interrupt (EOI) ioport_out(PIC1_COMMAND_PORT, 0x20); - unsigned char status = ioport_in(KEYBOARD_STATUS_PORT); + uint8_t status = ioport_in(KEYBOARD_STATUS_PORT); - // Lowest bit of status will be set if buffer not empty if (status & 0x1) { - int16_t keycode = ioport_in(KEYBOARD_DATA_PORT); - if (keycode < 0 || keycode >= 128) return; - - if(keycode==14) backspace(); - else if(keycode==28) enter(); + uint8_t keycode = ioport_in(KEYBOARD_DATA_PORT); + if(keycode<0x80) + { + ispressed[keycode]=1; + if(keycode==0x0E) backspace(); + else if(keycode==0x1C) enter(); + else if(keycode==0x39) space(); + else + { + char c=charcode[keycode]; + if(c!=' ') + { + if(lshift) + { + if(c>='a'&&c<='z') c-=32; + } + if(lctrl) + { + if(c=='l') + { + clear(); + prompt(); + return; + } + } + buffer[buffer_index++]=c; + printf("%c",c); + } + } + } else { - buffer[buffer_index++]=keyboard[keycode]; - printf("%c",keyboard[keycode]); + ispressed[keycode-0x80]=0; } } diff --git a/src/keyboard.h b/src/keyboard.h deleted file mode 100644 index 5de7e10..0000000 --- a/src/keyboard.h +++ /dev/null @@ -1,74 +0,0 @@ -unsigned char keyboard[128] = { - // -------- 0 to 9 -------- - ' ', - ' ', // escape key - '1','2','3','4','5','6','7','8', - // -------- 10 to 19 -------- - '9','0','-','=', - ' ', // Backspace - ' ', // Tab - 'q','w','e','r', - // -------- 20 to 29 -------- - 't','y','u','i','o','p','[',']', - ' ', // Enter - ' ', // left Ctrl - // -------- 30 to 39 -------- - 'a','s','d','f','g','h','j','k','l',';', - // -------- 40 to 49 -------- - ' ','`', - ' ', // left Shift - ' ','z','x','c','v','b','n', - // -------- 50 to 59 -------- - 'm',',','.', - '/', // slash, or numpad slash if preceded by keycode 224 - ' ', // right Shift - '*', // numpad asterisk - ' ', // left Alt - ' ', // Spacebar - ' ', - ' ', // F1 - // -------- 60 to 69 -------- - ' ', // F2 - ' ', // F3 - ' ', // F4 - ' ', // F5 - ' ', // F6 - ' ', // F7 - ' ', // F8 - ' ', // F9 - ' ', // F10 - ' ', - // -------- 70 to 79 -------- - ' ', // scroll lock - '7', // numpad 7, HOME key if preceded by keycode 224 - '8', // numpad 8, up arrow if preceded by keycode 224 - '9', // numpad 9, PAGE UP key if preceded by keycode 224 - '-', // numpad hyphen - '4', // numpad 4, left arrow if preceded by keycode 224 - '5', // numpad 5 - '6', // numpad 6, right arrow if preceded by keycode 224 - ' ', - '1', // numpad 1, END key if preceded by keycode 224 - // -------- 80 to 89 -------- - '2', // numpad 2, down arrow if preceded by keycode 224 - '3', // numpad 3, PAGE DOWN key if preceded by keycode 224 - '0', // numpad 0, INSERT key if preceded by keycode 224 - '.', // numpad dot, DELETE key if preceded by keycode 224 - ' ',' ',' ',' ',' ',' ', - // -------- 90 to 99 -------- - ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', - // -------- 100 to 109 -------- - ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', - // -------- 110 to 119 -------- - ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', - // -------- 120-127 -------- - ' ',' ',' ',' ',' ',' ',' ',' ', -}; -// Right control, right alt seem to send -// keycode 224, then the left control/alt keycode -// Arrow keys also send two interrupts, one 224 and then their actual code -// Same for numpad enter -// 197: Num Lock -// 157: Pause|Break (followed by 197?) -// Clicking on screen appears to send keycodes 70, 198 - // Is this the MARK command or something like that? diff --git a/src/keymap.c b/src/keymap.c new file mode 100644 index 0000000..04c79b2 --- /dev/null +++ b/src/keymap.c @@ -0,0 +1,173 @@ +void us_en(char keymap[]) +{ + keymap[0x01]=' '; //escape pressed + keymap[0x02]='1'; //1 pressed + keymap[0x03]='2'; //2 pressed + keymap[0x04]='3'; //3 pressed + keymap[0x05]='4'; //4 pressed + keymap[0x06]='4'; //5 pressed + keymap[0x07]='5'; //6 pressed + keymap[0x08]='7'; //7 pressed + keymap[0x09]='8'; //8 pressed + keymap[0x0A]='9'; //9 pressed + keymap[0x0B]='0'; //0 (zero) pressed + keymap[0x0C]='-'; //- pressed + keymap[0x0D]='='; //= pressed + keymap[0x0E]=' '; //backspace pressed + keymap[0x0F]=' '; //tab pressed + keymap[0x10]='q'; //Q pressed + keymap[0x11]='w'; //W pressed + keymap[0x12]='e'; //E pressed + keymap[0x13]='r'; //R pressed + keymap[0x14]='t'; //T pressed + keymap[0x15]='y'; //Y pressed + keymap[0x16]='u'; //U pressed + keymap[0x17]='i'; //I pressed + keymap[0x18]='o'; //O pressed + keymap[0x19]='p'; //P pressed + keymap[0x1A]='['; //[ pressed + keymap[0x1B]=']'; //] pressed + keymap[0x1C]='\n'; //enter pressed + keymap[0x1D]=' '; //left control pressed + keymap[0x1E]='a'; //A pressed + keymap[0x1F]='s'; //S pressed + keymap[0x20]='d'; //D pressed + keymap[0x21]='f'; //F pressed + keymap[0x22]='g'; //G pressed + keymap[0x23]='h'; //H pressed + keymap[0x24]='j'; //J pressed + keymap[0x25]='k'; //K pressed + keymap[0x26]='l'; //L pressed + keymap[0x27]=';'; //; pressed + keymap[0x28]='\''; //' (single quote) pressed + keymap[0x29]='`'; //` (back tick) pressed + keymap[0x2A]=' '; //left shift pressed + keymap[0x2B]='\\'; //\ pressed + keymap[0x2C]='z'; //Z pressed + keymap[0x2D]='x'; //X pressed + keymap[0x2E]='c'; //C pressed + keymap[0x2F]='v'; //V pressed + keymap[0x30]='b'; //B pressed + keymap[0x31]='n'; //N pressed + keymap[0x32]='m'; //M pressed + keymap[0x33]=','; //, pressed + keymap[0x34]='.'; //. pressed + keymap[0x35]='/'; /// pressed + keymap[0x36]=' '; //right shift pressed + keymap[0x37]=' '; //(keypad) * pressed + keymap[0x38]=' '; //left alt pressed + keymap[0x39]=' '; //space pressed + keymap[0x3A]=' '; //CapsLock pressed + keymap[0x3B]=' '; //F1 pressed + keymap[0x3C]=' '; //F2 pressed + keymap[0x3D]=' '; //F3 pressed + keymap[0x3E]=' '; //F4 pressed + keymap[0x3F]=' '; //F5 pressed + keymap[0x40]=' '; //F6 pressed + keymap[0x41]=' '; //F7 pressed + keymap[0x42]=' '; //F8 pressed + keymap[0x43]=' '; //F9 pressed + keymap[0x44]=' '; //F10 pressed + keymap[0x45]=' '; //NumberLock pressed + keymap[0x46]=' '; //ScrollLock pressed + keymap[0x47]='7'; //(keypad) 7 pressed + keymap[0x48]='8'; //(keypad) 8 pressed + keymap[0x49]='9'; //(keypad) 9 pressed + keymap[0x4A]='-'; //(keypad) - pressed + keymap[0x4B]='4'; //(keypad) 4 pressed + keymap[0x4C]='5'; //(keypad) 5 pressed + keymap[0x4D]='6'; //(keypad) 6 pressed + keymap[0x4E]='+'; //(keypad) + pressed + keymap[0x4F]='1'; //(keypad) 1 pressed + keymap[0x50]='2'; //(keypad) 2 pressed + keymap[0x51]='3'; //(keypad) 3 pressed + keymap[0x52]='0'; //(keypad) 0 pressed + keymap[0x53]='.'; //(keypad) . pressed + keymap[0x57]=' '; //F11 pressed + keymap[0x58]=' '; //F12 pressed + keymap[0x81]=' '; //escape released + keymap[0x82]=' '; //1 released + keymap[0x83]=' '; //2 released + keymap[0x84]=' '; //3 released + keymap[0x85]=' '; //4 released + keymap[0x86]=' '; //5 released + keymap[0x87]=' '; //6 released + keymap[0x88]=' '; //7 released + keymap[0x89]=' '; //8 released + keymap[0x8A]=' '; //9 released + keymap[0x8B]=' '; //0 (zero) released + keymap[0x8C]=' '; //- released + keymap[0x8D]=' '; //= released + keymap[0x8E]=' '; //backspace released + keymap[0x8F]=' '; //tab released + keymap[0x90]=' '; //Q released + keymap[0x91]=' '; //W released + keymap[0x92]=' '; //E released + keymap[0x93]=' '; //R released + keymap[0x94]=' '; //T released + keymap[0x95]=' '; //Y released + keymap[0x96]=' '; //U released + keymap[0x97]=' '; //I released + keymap[0x98]=' '; //O released + keymap[0x99]=' '; //P released + keymap[0x9A]=' '; //[ released + keymap[0x9B]=' '; //] released + keymap[0x9C]=' '; //enter released + keymap[0x9D]=' '; //left control released + keymap[0x9E]=' '; //A released + keymap[0x9F]=' '; //S released + keymap[0xA0]=' '; //D released + keymap[0xA1]=' '; //F released + keymap[0xA2]=' '; //G released + keymap[0xA3]=' '; //H released + keymap[0xA4]=' '; //J released + keymap[0xA5]=' '; //K released + keymap[0xA6]=' '; //L released + keymap[0xA7]=' '; //; released + keymap[0xA8]=' '; //' (single quote) released + keymap[0xA9]=' '; //` (back tick) released + keymap[0xAA]=' '; //left shift released + keymap[0xAB]=' '; //\ released + keymap[0xAC]=' '; //Z released + keymap[0xAD]=' '; //X released + keymap[0xAE]=' '; //C released + keymap[0xAF]=' '; //V released + keymap[0xB0]=' '; //B released + keymap[0xB1]=' '; //N released + keymap[0xB2]=' '; //M released + keymap[0xB3]=' '; //, released + keymap[0xB4]=' '; //. released + keymap[0xB5]=' '; /// released + keymap[0xB6]=' '; //right shift released + keymap[0xB7]=' '; //(keypad) * released + keymap[0xB8]=' '; //left alt released + keymap[0xB9]=' '; //space released + keymap[0xBA]=' '; //CapsLock released + keymap[0xBB]=' '; //F1 released + keymap[0xBC]=' '; //F2 released + keymap[0xBD]=' '; //F3 released + keymap[0xBE]=' '; //F4 released + keymap[0xBF]=' '; //F5 released + keymap[0xC0]=' '; //F6 released + keymap[0xC1]=' '; //F7 released + keymap[0xC2]=' '; //F8 released + keymap[0xC3]=' '; //F9 released + keymap[0xC4]=' '; //F10 released + keymap[0xC5]=' '; //NumberLock released + keymap[0xC6]=' '; //ScrollLock released + keymap[0xC7]=' '; //(keypad) 7 released + keymap[0xC8]=' '; //(keypad) 8 released + keymap[0xC9]=' '; //(keypad) 9 released + keymap[0xCA]=' '; //(keypad) - released + keymap[0xCB]=' '; //(keypad) 4 released + keymap[0xCC]=' '; //(keypad) 5 released + keymap[0xCD]=' '; //(keypad) 6 released + keymap[0xCE]=' '; //(keypad) + released + keymap[0xCF]=' '; //(keypad) 1 released + keymap[0xD0]=' '; //(keypad) 2 released + keymap[0xD1]=' '; //(keypad) 3 released + keymap[0xD2]=' '; //(keypad) 0 released + keymap[0xD3]=' '; //(keypad) . released + keymap[0xD7]=' '; //F11 released + keymap[0xD8]=' '; //F12 released +} diff --git a/src/stdio.c b/src/stdio.c index 31f7dbc..e93dea1 100644 --- a/src/stdio.c +++ b/src/stdio.c @@ -1,12 +1,11 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> -#include<stdarg.h> +#include"types.h" #include"string.h" +#include<stdarg.h> void terminal_putchar(char c); -void terminal_writestring(const char* data); -void terminal_writeint(const uint32_t data); +void terminal_writestring(char* data); +void terminal_writeint(uint32_t data); +void terminal_writefloat(double num); void printf(char *str, ...) { @@ -21,9 +20,10 @@ void printf(char *str, ...) if(str[i]=='%') { i++; - if(str[i]=='c') terminal_putchar(va_arg(list,int)); + if(str[i]=='c') terminal_putchar((char)va_arg(list,int)); else if(str[i]=='s') terminal_writestring(va_arg(list,char*)); else if(str[i]=='d') terminal_writeint(va_arg(list,int)); + else if(str[i]=='f') terminal_writefloat(va_arg(list,double)); else { terminal_writestring("wrong format using print function\n"); diff --git a/src/string.c b/src/string.c index f48614f..b0c42f2 100644 --- a/src/string.c +++ b/src/string.c @@ -1,12 +1,10 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> +#include"types.h" size_t stringlen(char *str) { size_t i; + for(i=0;str[i]!='\0';i++) ; - i--; return i; } @@ -24,11 +22,17 @@ void stringcat(char *str1,char *str2) char *tmp=str1; while(*tmp) tmp++; while(*str2) *tmp++=*str2++; + *tmp=*str2; +} + +void stringcpy(char *str1,char *str2) +{ + for(size_t i=0;str2[i]!='\0';i++) str1[i]=str2[i]; } void stringrev(char *str) { - size_t i=stringlen(str); + size_t i=stringlen(str)-1; for(size_t j=0;j<i-j;j++) { @@ -38,21 +42,72 @@ void stringrev(char *str) } } -void itos(char *str,uint32_t num) -{ - size_t i; - for(i=0;num>0;num/=10,i++) str[i]='0'+num%10; - str[i]='\0'; - stringrev(str); -} - -uint32_t stoi(char *str) +uint32_t stoi(const char *str) { uint32_t num=0; + for(size_t i=0;str[i]!='\0';i++) { + if(str[i]<'0'||str[i]>'9') return num; num*=10; num+=str[i]-'0'; } + return num; } + +void itos(uint32_t num,char *str) +{ + if(num==0) stringcpy(str,"0"); + else + { + size_t i=0; + for(;num>0;num/=10,i++) str[i]='0'+num%10; + str[i]='\0'; + + stringrev(str); + } +} + +double stof(const char *str) +{ + double num=0; + + bool point=0; + uint32_t pow=1; + size_t i=0; + + for(;str[i]!='\0';i++) + { + if(str[i]=='.') + { + if(point) return num/pow; + point=1; + continue; + } + if(str[i]<'0'||str[i]>'9') return num/pow; + num*=10; + num+=str[i]-'0'; + if(point) pow*=10; + } + + return num/pow; +} + +const int decimals=7; +void ftos(double num, char *str) +{ + itos((uint32_t)num,str); + + stringcat(str,"."); + char c[2]; + c[1]='\0'; + + for(size_t i=0;i<decimals;i++) + { + num-=(uint32_t)num; + num*=10; + c[0]=(uint32_t)num+'0'; + stringcat(str,c); + } +} diff --git a/src/string.h b/src/string.h index 8aabeff..500f545 100644 --- a/src/string.h +++ b/src/string.h @@ -1,15 +1,15 @@ #ifndef STRING_H #define STRING_H -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> +#include"types.h" size_t stringlen(char *str); bool stringcmp(char *str1,char *str2); void stringcat(char *str1,char *str2); void stringrev(char *str); -void itos(char *str,uint32_t num); -uint32_t stoi(char *str); +void itos(uint32_t num,char *str); +uint32_t stoi(const char *str); +double stof(const char *str); +void ftos(double num, char *str); #endif @@ -1,7 +1,4 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> - +#include"types.h" #include"string.h" #include"stdio.h" @@ -51,19 +48,28 @@ void merge(char parts[][CMD_LENGTH]) void ls(size_t numberof,char parts[][CMD_LENGTH]) { - size_t i=numberof; - char *part=parts[0]; printf("filesystem not implemented yet\n"); } +void number(size_t numberof,char parts[][CMD_LENGTH]) +{ + if(numberof==1) printf("Please enter a number\n"); + else + { + printf("number times two is %d\n",2*stoi(parts[1])); + printf("number times two is %f\n",2*stof(parts[1])); + } +} + void tty(char *buffer) { char parts[CMD_LENGTH][CMD_LENGTH]; size_t numberof=pieces(parts,buffer); + if(stringcmp(parts[0],"clear")) clear(); else if(stringcmp(parts[0],"echo")) echo(numberof,parts); else if(stringcmp(parts[0],"merge")) merge(parts); else if(stringcmp(parts[0],"ls")) ls(numberof,parts); - else if(stringcmp(parts[0],"number")) printf("number times two is %d\n",stoi(parts[1])*2); + else if(stringcmp(parts[0],"number")) number(numberof,parts); else printf("command not found: %s\n",parts[0]); } diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..a6d6530 --- /dev/null +++ b/src/types.h @@ -0,0 +1,8 @@ +#ifndef TYPES_H +#define TYPES_H + +#include<stdbool.h> +#include<stddef.h> +#include<stdint.h> + +#endif @@ -1,12 +1,10 @@ -#include<stdbool.h> -#include<stddef.h> -#include<stdint.h> +#include"types.h" #include"string.h" +#include"asm.h" + static const size_t VGA_WIDTH = 80; static const size_t VGA_HEIGHT = 25; -extern char ioport_in(uint8_t port); -extern void ioport_out(uint8_t port, char data); enum vga_color { VGA_COLOR_BLACK = 0, @@ -92,16 +90,26 @@ void terminal_putchar(char c) if (terminal_row==VGA_HEIGHT) movescreen(); } -void terminal_writestring(const char* data) +void terminal_writestring(char* data) { for(int i=0;data[i]!='\0';i++) terminal_putchar(data[i]); } -void terminal_writeint(const uint32_t num) +void terminal_writeint(uint32_t num) +{ + char string[100]; + for(int i=0;i<100;i++) string[i]='\0'; + char *str=string; + itos(num,str); + terminal_writestring(str); +} + +void terminal_writefloat(double num) { char string[100]; + for(int i=0;i<100;i++) string[i]='\0'; char *str=string; - itos(str,num); + ftos(num,str); terminal_writestring(str); } |
