Skip to content

Commit b6a485a

Browse files
committed
Merge remote-tracking branch 'upstream/main' into paging
2 parents 548150d + 68bd57d commit b6a485a

34 files changed

+1344
-100
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ IndentWidth: '4'
66
Language: Cpp
77
SortIncludes: 'true'
88
UseTab: Never
9+
AllowShortFunctionsOnASingleLine: Empty
910

1011
...

Makefile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ ifeq ($(PLATFORM), Darwin)
55
CC := x86_64-elf-gcc
66
LD := x86_64-elf-ld
77
OBJCOPY := x86_64-elf-objcopy
8+
GDB := x86_64-elf-gdb
89
else
910
CC := gcc
1011
LD := ld
1112
OBJCOPY := objcopy
13+
GDB := gdb
1214
endif
1315

1416
ifeq ($(DEBUG), true)
1517
DEBUG_CFLAGS := -g3 -O0
1618
DEBUG_NASM_FLAGS := -O0
1719
DEBUG_QEMU_FLAGS := -monitor stdio
20+
DEBUG_LFLAGS := -g
1821
else
1922
DEBUG_CFLAGS := -Os
2023
DEBUG_NASM_FLAGS := -Ox
2124
DEBUG_QEMU_FLAGS :=
25+
DEBUG_LFLAGS :=
2226
endif
2327

2428
QEMU_GDB_TIMEOUT ?= 10 # num. seconds to wait for qemu to start OS
@@ -33,7 +37,7 @@ export DEBUG_QEMU_FLAGS
3337
export CFLAGS := -Wall -Werror $(DEBUG_CFLAGS) -Wl,--oformat=binary -no-pie -m32 -mno-mmx -mno-sse -mno-sse2 -mno-sse3 \
3438
-s -falign-functions=4 -ffreestanding -fno-asynchronous-unwind-tables
3539

36-
export LFLAGS := -melf_i386 --build-id=none
40+
export LFLAGS := -melf_i386 --build-id=none $(DEBUG_LFLAGS)
3741

3842
ASM_BOOT_SECT_SOURCE := ./src/boot/boot_sect.asm
3943
ASM_OS_ENTRY_SOURCE := ./src/boot/os_entry.asm
@@ -48,11 +52,13 @@ OBJ_NAMES := src/os/main.o src/os/test.o os_entry.o src/os/paging.o \
4852
src/lib/device/serial.o src/lib/device/ps2.o src/lib/device/keyboard.o \
4953
src/lib/container/ring_buffer.o \
5054
src/lib/stdlib/stdio.o src/lib/stdlib/stdlib.o src/lib/stdlib/string.o \
51-
src/lib/pit/pit.o src/lib/video/VGA_text.o
52-
55+
src/lib/pit/pit.o src/lib/device/key_handlers.o src/lib/video/VGA_text.o
5356

5457
.PHONY: clean qemu test
5558

59+
all: $(OS_BIN)
60+
echo DEBUG is set to $(DEBUG)
61+
5662
$(OS_BIN): $(OBJ_NAMES) $(BOOT_OBJ)
5763
$(LD) $(LFLAGS) -T link.ld $(OBJ_NAMES) -o mOS.elf
5864
$(OBJCOPY) -O binary mOS.elf intermediate.bin
@@ -65,7 +71,7 @@ os_entry.o: $(ASM_OS_ENTRY_SOURCE)
6571
nasm $^ -f elf32 -o $@ $(DEBUG_NASM_FLAGS)
6672

6773
%.o: %.c
68-
$(CC) -c $^ -o $@ $(CFLAGS) -I./src/lib/
74+
$(CC) -c $^ -o $@ $(CFLAGS) -I./src/lib/ -I./src/lib/stdlib/
6975

7076
%.o: %.asm
7177
nasm $^ -f elf32 -o $@ $(DEBUG_NASM_FLAGS)
@@ -76,7 +82,7 @@ qemu: $(OS_BIN)
7682
qemu-gdb: $(OS_BIN)
7783
qemu-system-i386 $(DEBUG_QEMU_FLAGS) -s -S -boot c -drive format=raw,file=$^ \
7884
-no-reboot -no-shutdown &
79-
gdb mOS.elf \
85+
$(GDB) mOS.elf \
8086
-q \
8187
-ex 'set remotetimeout $(QEMU_GDB_TIMEOUT)' \
8288
-ex 'target remote localhost:1234' \
@@ -86,7 +92,7 @@ qemu-gdb: $(OS_BIN)
8692
qemu-gdb-boot: $(OS_BIN)
8793
qemu-system-i386 $(DEBUG_QEMU_FLAGS) -s -S -boot c -drive format=raw,file=$^ \
8894
-no-reboot -no-shutdown &
89-
gdb mOS.elf \
95+
$(GDB) mOS.elf \
9096
-q \
9197
-ix gdb_init_real_mode.txt \
9298
-ex 'set remotetimeout $(QEMU_GDB_TIMEOUT)' \

src/boot/boot_sect.asm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ OS_OFFSET equ 0x1000
33

44
[bits 16]
55
begin:
6-
mov [BOOT_DRIVE], dl
6+
mov [BOOT_DRIVE], dl
77
mov bp, 0x9000
88
mov sp, bp
99
jmp load_kernel
@@ -26,7 +26,7 @@ load_kernel:
2626
begin_pm:
2727
call OS_OFFSET
2828
hlt
29-
29+
3030

3131
times 509 - ($ - $$) db 0 ;padding
3232

src/lib/device/key_handlers.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#include "key_handlers.h"
2+
3+
#include "video/VGA_text.h"
4+
5+
void specialHandler(KeyPress out) {
6+
if (!(out.modifiers & KEY_MOD_SHIFT)) {
7+
switch (out.code) {
8+
case Key_backspace:
9+
if (getCursor()->highlight_offset) {
10+
highlightDeletePrev(getCursor()->highlight_offset);
11+
getCursor()->highlight_offset = 0;
12+
} else
13+
deletePrevChar();
14+
break;
15+
case Key_delete:
16+
if (getCursor()->highlight_offset) {
17+
highlightDeleteCurrent(getCursor()->highlight_offset);
18+
getCursor()->highlight_offset = 0;
19+
} else
20+
deleteCurrentChar();
21+
break;
22+
case Key_left:
23+
if (getCursor()->highlight_offset) {
24+
cursorHighlightLeft(getCursor()->highlight_offset);
25+
getCursor()->highlight_offset = 0;
26+
} else
27+
cursorLeft();
28+
break;
29+
case Key_down:
30+
if (getCursor()->highlight_offset) {
31+
cursorHighlightDown(getCursor()->highlight_offset);
32+
getCursor()->highlight_offset = 0;
33+
} else
34+
cursorDown();
35+
break;
36+
case Key_up:
37+
if (getCursor()->highlight_offset) {
38+
cursorHighlightUp(getCursor()->highlight_offset);
39+
getCursor()->highlight_offset = 0;
40+
} else
41+
cursorUp();
42+
break;
43+
case Key_right:
44+
if (getCursor()->highlight_offset) {
45+
cursorHighlightRight(getCursor()->highlight_offset);
46+
getCursor()->highlight_offset = 0;
47+
} else
48+
cursorRight();
49+
break;
50+
default:
51+
break;
52+
}
53+
} else {
54+
if (((out.code == Key_up || out.code == Key_left) &&
55+
cursorIsAtStart()) ||
56+
((out.code == Key_down || out.code == Key_right) &&
57+
cursorIsAtEnd()))
58+
return;
59+
if ((out.code == Key_up || out.code == Key_left ||
60+
out.code == Key_down || out.code == Key_right) &&
61+
!getCursor()->highlight_offset)
62+
highlightCurrentChar();
63+
switch (out.code) {
64+
case Key_up:
65+
for (int i = 0; i < VGA_WIDTH && !cursorIsAtStart(); i++) {
66+
if (getCursor()->highlight_offset > 0) {
67+
highlightCurrentChar();
68+
cursorLeft();
69+
} else {
70+
cursorLeft();
71+
highlightCurrentChar();
72+
}
73+
getCursor()->highlight_offset--;
74+
}
75+
break;
76+
case Key_down:
77+
for (int i = 0; i < VGA_WIDTH && !cursorIsAtEnd(); i++) {
78+
if (getCursor()->highlight_offset < 0) {
79+
highlightCurrentChar();
80+
cursorRight();
81+
} else {
82+
cursorRight();
83+
highlightCurrentChar();
84+
}
85+
getCursor()->highlight_offset++;
86+
}
87+
break;
88+
case Key_left:
89+
if (getCursor()->highlight_offset > 0) {
90+
highlightCurrentChar();
91+
cursorLeft();
92+
} else {
93+
cursorLeft();
94+
highlightCurrentChar();
95+
}
96+
getCursor()->highlight_offset--;
97+
break;
98+
case Key_right:
99+
if (getCursor()->highlight_offset < 0) {
100+
highlightCurrentChar();
101+
cursorRight();
102+
} else {
103+
cursorRight();
104+
highlightCurrentChar();
105+
}
106+
getCursor()->highlight_offset++;
107+
break;
108+
default:
109+
break;
110+
}
111+
if ((out.code == Key_up || out.code == Key_left ||
112+
out.code == Key_down || out.code == Key_right) &&
113+
!getCursor()->highlight_offset)
114+
highlightCurrentChar();
115+
}
116+
}

src/lib/device/key_handlers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef KEY_HANDLERS_H
2+
#define KEY_HANDLERS_H
3+
4+
#include "keyboard.h"
5+
6+
void specialHandler(KeyPress out);
7+
8+
#endif

src/lib/device/keyboard.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ char keyPressToASCII(KeyPress press) {
182182

183183
static const KeyPress nonePress = {0, Key_none, KeyReleased, 0};
184184

185-
KeyPress codePointDiscard(struct KeyboardState *, uint8_t) { return nonePress; }
185+
KeyPress codePointDiscard(struct KeyboardState *, uint8_t) {
186+
return nonePress;
187+
}
186188

187189
static const enum KeyCode sc1LUT[] = {
188190
[0x1] = Key_esc, Key_1, Key_2, Key_3,

src/lib/device/ps2.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../../os/hard/idt.h"
44
#include "../../os/hard/port_io.h"
55
#include "container/ring_buffer.h"
6+
#include "key_handlers.h"
67

78
#define PS2_BUF_SIZE 64
89
#define PS2_TIMEOUT 100000
@@ -72,6 +73,29 @@ const struct PS2Device *getPortType(int portnum) {
7273
// temporary include for #7
7374
#include "video/VGA_text.h"
7475

76+
void vgaEditor(struct PS2Buf_t out) {
77+
switch (out.keyEvent.code) {
78+
case Key_backspace:
79+
case Key_delete:
80+
case Key_left:
81+
case Key_down:
82+
case Key_up:
83+
case Key_right:
84+
specialHandler(out.keyEvent);
85+
break;
86+
default:
87+
char buf[2] = " ";
88+
buf[0] = keyPressToASCII(out.keyEvent);
89+
if (buf[0] != 0) {
90+
if (getCursor()->highlight_offset) {
91+
highlightDeletePrev(getCursor()->highlight_offset);
92+
getCursor()->highlight_offset = 0;
93+
}
94+
print(buf, white);
95+
}
96+
}
97+
}
98+
7599
void ps2HandlerPort1(isr_registers_t *regs) {
76100
uint8_t b = inb(PS2_DATA);
77101

@@ -86,9 +110,7 @@ void ps2HandlerPort1(isr_registers_t *regs) {
86110

87111
// temporary to satisfy exactly what issue #7 says
88112
if (out.keyEvent.code != Key_none && out.keyEvent.event == KeyPressed) {
89-
char buf[2] = " ";
90-
buf[0] = keyPressToASCII(out.keyEvent);
91-
print(buf, white);
113+
vgaEditor(out);
92114
}
93115
}
94116
}
@@ -126,7 +148,9 @@ enum DeviceType translateDeviceType(uint8_t b) {
126148
}
127149
}
128150

129-
uint8_t readStat(void) { return inb(PS2_STAT_CMD); }
151+
uint8_t readStat(void) {
152+
return inb(PS2_STAT_CMD);
153+
}
130154

131155
bool sendCMD(uint8_t b) {
132156
// await ready
@@ -191,7 +215,9 @@ void setConf(uint8_t newconf) {
191215
}
192216

193217
// public alias of sendData
194-
bool sendPort1(uint8_t b) { return sendData(b); }
218+
bool sendPort1(uint8_t b) {
219+
return sendData(b);
220+
}
195221

196222
bool sendPort2(uint8_t b) {
197223
// note that short circuit eval will happen
@@ -267,9 +293,15 @@ bool ps2works = false;
267293
bool port1works = false;
268294
bool port2works = false;
269295

270-
bool ps2Present(void) { return ps2works; }
271-
bool ps2Port1Present(void) { return port1works; }
272-
bool ps2Port2Present(void) { return port2works; }
296+
bool ps2Present(void) {
297+
return ps2works;
298+
}
299+
bool ps2Port1Present(void) {
300+
return port1works;
301+
}
302+
bool ps2Port2Present(void) {
303+
return port2works;
304+
}
273305

274306
int ps2Init() {
275307
ring_buffer_init(&PS2Port1, initPS2BufVal);

src/lib/device/ps2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ struct PS2Buf_t {
6969
int ps2Init();
7070
const struct PS2Device *getPortType(int portnum);
7171

72+
void vgaEditor(struct PS2Buf_t out);
73+
7274
bool ps2Present(void);
7375
bool ps2Port1Present(void);
7476
bool ps2Port2Present(void);
@@ -84,4 +86,4 @@ struct PS2Buf_t popDev1(void);
8486
struct PS2Buf_t peekDev2(void);
8587
struct PS2Buf_t popDev2(void);
8688

87-
#endif
89+
#endif

src/lib/pit/pit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
uint32_t timer_ticks = 0;
99

10-
uint32_t get_ticks() { return timer_ticks; }
10+
uint32_t get_ticks() {
11+
return timer_ticks;
12+
}
1113

1214
static void timer_handler(isr_registers_t *regs) {
1315
/* Increment 'Tick count' */

0 commit comments

Comments
 (0)