Skip to content

Commit e607540

Browse files
author
Carlos-Ariel Vanegas
committed
added serial input
1 parent daaa697 commit e607540

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/host/c/grub-kernel/kernel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void handle_irq(const int number,const int has_code,const int code)
106106
if (has_code)
107107
printf("irq %d with code %d\n",number,code);
108108
else
109-
printf("irq %d with no code\n",number,code);
109+
printf("irq %d with no code\n",number);
110110
if (number == 13) show_stack_trace();
111111
irqs.data[irqs.i] = number;
112112
irqs.i++;
@@ -194,7 +194,7 @@ int putchar(char c) {
194194

195195
size_t read(char *txt, size_t size) {
196196

197-
int shift = 0;
197+
static int shift = 0;
198198
for (int i = 0; i < size; i++) {
199199
short c = receive_scancode();
200200
if (c == 0x2a) {
@@ -214,7 +214,7 @@ size_t read(char *txt, size_t size) {
214214
} else {
215215
txt[i] = scancode_to_ascii[c];
216216
}
217-
putchar(txt[i]);
217+
// putchar(txt[i]);
218218
}
219219

220220
return size;

src/host/c/grub-kernel/kernel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ typedef unsigned int uint32_t;
33
typedef unsigned char uint8_t;
44
typedef unsigned size_t;
55
/* From https://wiki.osdev.org/8259_PIC#Programming_the_PIC_chips */
6+
#define EOF -1
67
#define PIC1 0x20 /* IO base address for master PIC */
78
#define PIC2 0xA0 /* IO base address for slave PIC */
89
#define PIC1_COMMAND PIC1

src/host/c/lib/prim-kernel.scm

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(define-primitive (inb port)
2222
"{
2323
PRIM1();
24-
push2(TAG_NUM((int)inb((unsigned short)NUM(x))),PAIR_TAG);
24+
push2(TAG_NUM(inb((unsigned short)NUM(x))),PAIR_TAG);
2525
break;
2626
}")
2727

@@ -52,6 +52,11 @@
5252
push2(TAG_NUM(NUM(x) << NUM(y)),PAIR_TAG);
5353
break;
5454
}")
55+
(define-primitive (set-interrupts)
56+
"{
57+
__asm__(\"sti\");
58+
push2(FALSE,PAIR_TAG);
59+
}")
5560

5661

5762
(define com (lambda (k) (+ 1016 k)))
@@ -62,7 +67,7 @@
6267
(outb (com 3) (lshift 1 7))
6368
(outb (com 0) 3)
6469
(outb (com 1) 0)
65-
(outb (com 3) 3)
70+
(outb (com 3) 7)
6671
(outb (com 2) 199)
6772
(outb (com 4) 11)
6873
(outb (com 4) 30)
@@ -75,10 +80,16 @@
7580

7681

7782
(define is-clear (lambda () (not (= (land (inb (com 5)) 32) 0))))
83+
(define is-recv-clear (lambda () (not (= (land (inb (com 5)) 1) 0))))
84+
7885
(define send-serial
7986
(lambda (x)
8087
(if (is-clear) (outb (com 0) x) (send-serial x))))
8188

89+
(define recv-serial
90+
(lambda ()
91+
(if (is-recv-clear) (inb (com 0)) (recv-serial))))
92+
8293
(define send-str-serial-len
8394
(lambda (str len)
8495
(if (= len 0)
@@ -91,18 +102,37 @@
91102
(lambda (str) (send-str-serial-len (car str) (cdr str))))
92103

93104
(define %%%write-char-fd %%write-char-fd)
94-
(define %%%read-char-fd %%read-char-fd)
105+
(define keyboard-read %%read-char-fd)
106+
107+
(define get-input recv-serial)
95108

96109
(define is-serial-ready #f)
97110

98-
(define
99-
(%%write-char-fd ch fd)
111+
(define set-keyboard-input
112+
(lambda () (set! get-input (lambda () (keyboard-read #f)))))
113+
114+
(define set-serial-input
115+
(lambda () (set! get-input recv-serial)))
116+
117+
(define enable-echo
118+
(lambda () (set! %%read-char-fd echo-read)))
119+
120+
(define disable-echo
121+
(lambda () (set! %%read-char-fd noecho-read)))
122+
123+
(define (%%write-char-fd ch fd)
100124
(begin
101125
(if (not is-serial-ready)
102126
(begin
103127
(setup-serial com)
104128
(set! is-serial-ready #t))
105129
#f)
106130
(%%%write-char-fd ch fd)
107-
(send-serial ch)))
131+
(send-serial ch)
132+
ch))
133+
134+
(define (noecho-read fd) (get-input))
135+
(define (echo-read fd)
136+
(%%write-char-fd (get-input) fd))
137+
(define %%read-char-fd echo-read)
108138

0 commit comments

Comments
 (0)