:: commit 5082a450a085bca81a1920febcf63e01bb5e594d

mintsuki <mintsuki@protonmail.com> — 2020-01-22 04:02

parents: fdfc83abfb

Clean up more and add getchar and gets

diff --git a/src/bootsect/bootsect.asm b/src/bootsect/bootsect.asm
index a9d58a5d..69802afb 100644
--- a/src/bootsect/bootsect.asm
+++ b/src/bootsect/bootsect.asm
@@ -3,6 +3,7 @@ bits 16
 
 start:
     cli
+    cld
     jmp 0x0000:.initialise_cs
   .initialise_cs:
     xor ax, ax
diff --git a/src/bootsect/disk.inc b/src/bootsect/disk.inc
index a8f429e3..c278648f 100644
--- a/src/bootsect/disk.inc
+++ b/src/bootsect/disk.inc
@@ -28,9 +28,8 @@ read_sector:
     mov si, .da_struct
     mov ah, 0x42
 
-    clc										; Clear carry for int 0x13 because some BIOSes may not clear it on success
-
-    int 0x13								; Call int 0x13
+    clc
+    int 0x13
 
   .done:
     pop edi
@@ -39,7 +38,7 @@ read_sector:
     pop ecx
     pop ebx
     pop eax
-    ret										; Exit routine
+    ret
 
 align 4
 .da_struct:
@@ -65,6 +64,9 @@ align 4
 ; OUT:
 ; Carry if error
 
+%define TEMP_BUFFER_SEG 0x7000
+%define BYTES_PER_SECT  512
+
 read_sectors:
     push eax									; Save GPRs
     push ebx
@@ -77,41 +79,41 @@ read_sectors:
     push es
     push ebx
 
-    mov bx, 0x7000							; Load in a temp buffer
+    mov bx, TEMP_BUFFER_SEG
     mov es, bx
     xor bx, bx
 
-    call read_sector						; Read sector
+    call read_sector
 
     pop ebx
     pop es
 
-    jc .done								; If carry exit with flag
+    jc .done
 
     push ds
 
-    mov si, 0x7000
+    mov si, TEMP_BUFFER_SEG
     mov ds, si
     mov edi, ebx
     xor esi, esi
 
     push ecx
-    mov ecx, 512
+    mov ecx, BYTES_PER_SECT
     a32 o32 rep movsb
     pop ecx
 
     pop ds
 
-    inc eax									; Increment sector
-    add ebx, 512							; Add 512 to the buffer
+    inc eax
+    add ebx, BYTES_PER_SECT
 
-    loop .loop								; Loop!
+    loop .loop
 
   .done:
     pop edi
     pop esi
     pop edx
-    pop ecx									; Restore GPRs
+    pop ecx
     pop ebx
     pop eax
-    ret										; Exit routine
+    ret
diff --git a/src/bootsect/simple_print.inc b/src/bootsect/simple_print.inc
index fc40edf3..ee68dcc9 100644
--- a/src/bootsect/simple_print.inc
+++ b/src/bootsect/simple_print.inc
@@ -6,16 +6,17 @@
 ; SI = points to a 0x00 terminated string
 
 simple_print:
-    push ax						; Save registers
+    push ax
     push si
-    mov ah, 0x0E				; int 0x10, function 0x0E (print character)
+    ; int 0x10, function 0x0e (print character)
+    mov ah, 0x0e
   .loop:
-	lodsb					; Load character from string
-	test al, al				; Is is the 0x00 terminator?
-	jz .done				; If it is, exit routine
-	int 0x10				; Call BIOS
-	jmp .loop				; Repeat!
+    lodsb
+    test al, al
+    jz .done
+    int 0x10
+    jmp .loop
   .done:
-	pop si					; Restore registers
-	pop ax
-	ret						; Exit routine
+    pop si
+    pop ax
+    ret
diff --git a/src/drivers/disk.c b/src/drivers/disk.c
index bc53728d..df13b5a2 100644
--- a/src/drivers/disk.c
+++ b/src/drivers/disk.c
@@ -3,7 +3,7 @@
 #include <lib/libc.h>
 #include <drivers/disk.h>
 #include <lib/real.h>
-#include <lib/print.h>
+#include <lib/blib.h>
 #include <lib/mbr.h>
 
 #define SECTOR_SIZE 512
diff --git a/src/fs/echfs.c b/src/fs/echfs.c
index 42611eb0..1bbf85f7 100644
--- a/src/fs/echfs.c
+++ b/src/fs/echfs.c
@@ -1,9 +1,8 @@
 #include <fs/echfs.h>
 #include <stdint.h>
 #include <lib/libc.h>
-#include <lib/print.h>
+#include <lib/blib.h>
 #include <drivers/disk.h>
-#include <lib/types.h>
 
 struct echfs_identity_table {
     uint8_t jmp[4];
diff --git a/src/lib/print.c b/src/lib/blib.c
similarity index 86%
rename from src/lib/print.c
rename to src/lib/blib.c
index 0c48f439..2d5d1ff8 100644
--- a/src/lib/print.c
+++ b/src/lib/blib.c
@@ -1,8 +1,37 @@
 #include <stdint.h>
 #include <stddef.h>
 #include <stdarg.h>
-#include <lib/print.h>
+#include <lib/blib.h>
 #include <drivers/vga_textmode.h>
+#include <lib/real.h>
+
+char getchar(void) {
+    struct rm_regs r = {0};
+    rm_int(0x16, &r, &r);
+    return (char)(r.eax & 0xff);
+}
+
+void gets(char *buf, size_t limit) {
+    for (size_t i = 0; ; ) {
+        char c = getchar();
+        switch (c) {
+            case '\b':
+                if (i) {
+                    i--;
+                    text_write(&c, 1);
+                }
+                continue;
+            case '\n':
+                buf[i] = 0;
+                text_write(&c, 1);
+                return;
+        }
+        if (i < limit-1) {
+            buf[i++] = c;
+            text_write(&c, 1);
+        }
+    }
+}
 
 static const char *base_digits = "0123456789abcdef";
 
diff --git a/src/lib/blib.h b/src/lib/blib.h
new file mode 100644
index 00000000..0cba0693
--- /dev/null
+++ b/src/lib/blib.h
@@ -0,0 +1,14 @@
+#ifndef __LIB__BLIB_H__
+#define __LIB__BLIB_H__
+
+#include <stddef.h>
+
+void print(const char *fmt, ...);
+char getchar(void);
+void gets(char *buf, size_t limit);
+
+#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
+
+typedef void *symbol[];
+
+#endif
diff --git a/src/lib/print.h b/src/lib/print.h
deleted file mode 100644
index fcefd890..00000000
--- a/src/lib/print.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef __PRINT_H__
-#define __PRINT_H__
-
-void print(const char *, ...);
-
-#endif
diff --git a/src/lib/types.h b/src/lib/types.h
deleted file mode 100644
index a7a6b784..00000000
--- a/src/lib/types.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef __LIB_TYPES_H__
-#define __LIB_TYPES_H__
-
-typedef void *symbol[];
-
-#define DIV_ROUNDUP(a, b) (((a) + ((b) - 1)) / (b))
-
-#endif
diff --git a/src/main.c b/src/main.c
index 54351234..449d38c1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,8 +7,7 @@ asm (
 
 #include <drivers/vga_textmode.h>
 #include <lib/real.h>
-#include <lib/print.h>
-#include <lib/types.h>
+#include <lib/blib.h>
 #include <lib/mbr.h>
 #include <fs/echfs.h>
 
@@ -51,11 +50,4 @@ void main(int boot_drive) {
         : "b" ("")
         : "memory"
     );
-
-    /*for (;;) {
-        struct rm_regs r = {0};
-        rm_int(0x16, &r, &r);    // Real mode interrupt 16h
-        char c = (char)(r.eax & 0xff);
-        text_write(&c, 1);
-    }*/
 }
tab: 248 wrap: offon