Skip to content

Commit c1d861e

Browse files
committed
Ported CALC to protected mode along with a potential buggit add .!
1 parent 69fd003 commit c1d861e

File tree

11 files changed

+104
-26
lines changed

11 files changed

+104
-26
lines changed

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ SECTOR_COUNT_BT_STAGE2 = 19 # In Hex
4040
SECTOR_START_SHARED_LIBRARY = 1
4141
SECTOR_COUNT_SHARED_LIBRARY = 1
4242
SECTOR_START_KERNEL = 27
43-
SECTOR_COUNT_KERNEL = 33
44-
SECTOR_START_APP_TTT = 60
43+
SECTOR_COUNT_KERNEL = 41
44+
SECTOR_START_APP_TTT = 68
4545
SECTOR_COUNT_APP_TTT = 25
46-
SECTOR_START_APP_CALC = 85
46+
SECTOR_START_APP_CALC = 93
4747
SECTOR_COUNT_APP_CALC= 25
4848

4949
MEMORY_LOCATION_KERNEL = 0xC000
@@ -167,6 +167,8 @@ $(kernel_core): $(SRC_KERNEL)/core.asm $(SRC_KERNEL)/core.c $(SRC_KERNEL)/essent
167167
-D SECTOR_COUNT_APP_TTT=$(SECTOR_COUNT_APP_TTT) \
168168
-D MEMORY_LOCATION_KERNEL=$(MEMORY_LOCATION_KERNEL) \
169169
-D MEMORY_LOCATION_APP=$(MEMORY_LOCATION_APP) \
170+
-D SECTOR_START_APP_CALC=$(SECTOR_START_APP_CALC) \
171+
-D SECTOR_COUNT_APP_CALC=$(SECTOR_COUNT_APP_CALC) \
170172
-o $(BUILD_KERNEL)/core_c.o $(SRC_KERNEL)/core.c
171173
ld --oformat binary -m elf_i386 --trace -Ttext 0x0000 --strip-all -o $(kernel_core) $(BUILD_KERNEL)/core_asm.o $(BUILD_KERNEL)/core_c.o $(BUILD_KERNEL)/interrupts_asm.o $(BUILD_DRIVERS)/keyboard/libkeyboard $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga $(BUILD_LIB_DS)/libds $(BUILD_DRIVERS)/disk/libdisk
172174
truncate --size=%512 $(kernel_core)
@@ -240,8 +242,8 @@ $(BUILD_LIB_DS)/libds: $(SRC_LIB_DS)/queue.h $(SRC_LIB_DS)/queue.c
240242
# User Applications
241243
$(app_calc): $(app_entry) $(SRC_APP)/calc.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/time.h $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga # And dependecies :/
242244
mkdir -p $$(dirname $(app_calc))
243-
gcc -m16 -fno-pie -c -Isrc -o $(BUILD_APP)/calc.o $(SRC_APP)/calc.c
244-
ld --oformat binary -m elf_i386 -Ttext 0x2000 --strip-all -o $@ $(app_entry) $(BUILD_APP)/calc.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga
245+
gcc -m32 -fno-pie -c -Isrc -o $(BUILD_APP)/calc.o $(SRC_APP)/calc.c
246+
ld --oformat binary -m elf_i386 -Ttext 0x0 --strip-all -o $@ $(app_entry) $(BUILD_APP)/calc.o $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga
245247
truncate --size=%512 $@
246248

247249
$(app_tic_tac_toe): $(app_entry) $(SRC_APP)/tic_tac_toe.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/input.h $(SRC_LIB_UTILS)/time.h $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga # And dependecies :/

src/app/calc.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ void print_int(int x) {
1212
printf("%d", x);
1313
}
1414
#else
15+
#include <lib/utils/input.h>
1516
#include <lib/utils/output.h>
1617
#include <lib/utils/time.h>
1718
#include <lib/utils/string.h>
18-
void read_line(char *s) {
19-
// Not Implemented.
20-
// Migrate binary to 32-bit.
21-
}
2219
#endif
2320

2421
int err;
@@ -86,11 +83,11 @@ char expr[100];
8683
int result;
8784

8885
int handle_expression(char str[]) {
89-
if(strcmp(str, "HELP")==0) {
86+
if(strcmpi(str, "HELP")==0) {
9087
show_usage();
9188
return 1;
9289
}
93-
if(strcmp(str, "EXIT")==0) {
90+
if(strcmpi(str, "EXIT")==0) {
9491
return 0;
9592
}
9693
result = solve(str);

src/drivers/display/text_mode_vga.asm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ global _low_vga_copy_step
88
push ebp
99
mov ebp, esp
1010
push ds
11-
mov eax, 0
12-
mov ds, eax ; memory address mapping is absolute
13-
11+
mov eax, 0x20
12+
mov ds, eax ; Absolute memory address
1413

1514
mov ebx,[ebp + 0x10] ; (ROW_WIDTH*y+x)
1615
shl ebx, 1
@@ -28,8 +27,8 @@ global _low_vga_copy_step
2827
push ebp
2928
mov ebp, esp
3029
push ds
31-
mov eax, 0
32-
mov ds, eax ; memory address mapping is absolute
30+
mov eax, 0x20
31+
mov ds, eax ; Absolute memory address
3332

3433
; Copy char+colors in Row Order Format
3534
mov eax,[ebp + 0x8] ; (ROW_WIDTH*y1+x1)

src/drivers/keyboard/keyboard.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ char keyboard_get_key_pressed_blocking() {
170170
while(!keyboard_scanner_ascii_is_available()) {
171171
keyboard_scanner_step();
172172
}
173-
return keyboard_scanner_ascii_get();
173+
char c = keyboard_scanner_ascii_get();
174+
return c;
174175
}
175176

176177
void keyboard_init() {

src/drivers/keyboard/scancode_handler.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ static const unsigned char KEYBOARD_ASCII_MAPH_09[] = {
158158
KEYBOARD_SC_RPF0_9
159159
};
160160

161+
static const unsigned char KEYBOARD_ASCII_MAPH_KEYPAD_09[] = {
162+
KEYBOARD_SC_RPF0_keypad_0,
163+
KEYBOARD_SC_RPF0_keypad_1,
164+
KEYBOARD_SC_RPF0_keypad_2,
165+
KEYBOARD_SC_RPF0_keypad_3,
166+
KEYBOARD_SC_RPF0_keypad_4,
167+
KEYBOARD_SC_RPF0_keypad_5,
168+
KEYBOARD_SC_RPF0_keypad_6,
169+
KEYBOARD_SC_RPF0_keypad_7,
170+
KEYBOARD_SC_RPF0_keypad_8,
171+
KEYBOARD_SC_RPF0_keypad_9
172+
};
173+
174+
161175
// Key ScanCode and ASCII code are listed alternatively in the next list.
162176
static const unsigned char KEYBOARD_ASCII_MAPH_OTHERS[] = {
163177
KEYBOARD_SC_RPF0_space, ' ',
@@ -168,7 +182,15 @@ static const unsigned char KEYBOARD_ASCII_MAPH_OTHERS[] = {
168182
KEYBOARD_SC_RPF0_quotation, '\'',
169183
KEYBOARD_SC_RPF0_brace_sq_o, '[',
170184
KEYBOARD_SC_RPF0_brace_sq_c, ']',
171-
KEYBOARD_SC_RPF0_blackslash, '\\'
185+
KEYBOARD_SC_RPF0_blackslash, '\\',
186+
187+
KEYBOARD_SC_RPF0_enter, '\n',
188+
KEYBOARD_SC_RPF0_keypad_dot, '.',
189+
KEYBOARD_SC_RPF0_keypad_minus, '-',
190+
KEYBOARD_SC_RPF0_keypad_mul, '*',
191+
KEYBOARD_SC_RPF0_keypad_plus, '+',
192+
KEYBOARD_SC_PE0_RPF0_keypad_enter , '\n'
193+
172194
};
173195

174196
static unsigned char KEYBOARD_ASCII_MAPPING[256];
@@ -204,7 +226,11 @@ void keyboard_scanner_handler_init() {
204226
unsigned char c = '0'+i;
205227
KEYBOARD_ASCII_MAPPING[code]=c;
206228
}
207-
if(last_ascii_available) return 1;
229+
for(int i = 0;i<sizeof(KEYBOARD_ASCII_MAPH_KEYPAD_09);i++) {
230+
unsigned char code = KEYBOARD_ASCII_MAPH_KEYPAD_09[i];
231+
unsigned char c = '0'+i;
232+
KEYBOARD_ASCII_MAPPING[code]=c;
233+
}
208234
for(int i = 0;i<sizeof(KEYBOARD_ASCII_MAPH_OTHERS);i+=2) {
209235
unsigned char code = KEYBOARD_ASCII_MAPH_OTHERS[i];
210236
unsigned char c = KEYBOARD_ASCII_MAPH_OTHERS[i+1];

src/kernel/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern void kernel_enable_interrupts();
1414
extern int call_main(int argc, char *argv[]);
1515

1616
void exec(int sector_index, int sector_count){
17-
int err = load_sectors(MEMORY_LOCATION_APP, 0x80, SECTOR_START_APP_TTT, SECTOR_COUNT_APP_TTT);
17+
int err = load_sectors(MEMORY_LOCATION_APP, 0x80, SECTOR_START_APP_CALC, SECTOR_COUNT_APP_CALC);
1818
if(err) {
1919
move_xy(3,8);
2020
print_line("Failed to load app in memory, Error: ");

src/kernel/interrupts.asm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,20 @@ extern syscall_interrupt_keyboard_getch
4040
iret
4141

4242
syscall_interrupt_keyboard_getch_low:
43+
; Not saving SS
4344
push ds
45+
push es
46+
push fs
47+
push gs
4448
mov bx, 0x10
4549
mov ds, bx
50+
mov es, bx
51+
mov fs, bx
52+
mov gs, bx
4653
call syscall_interrupt_keyboard_getch
54+
pop gs
55+
pop fs
56+
pop es
4757
pop ds
4858
iret
4959

src/lib/utils/input.asm

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
[BITS 32]
22

3-
global getch
3+
global getch_low
44

55
[SECTION .text]
6-
getch:
6+
getch_low:
7+
push ebp
8+
mov ebp, esp
9+
10+
711
; We are expecting interrupt to set return value in eax
12+
mov eax, 0
813
int 0x60
14+
mov esp, ebp
15+
pop ebp
916
ret

src/lib/utils/input.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
#include <lib/utils/input.h>
2+
#include <lib/utils/output.h>
23
#include <lib/utils/string.h>
34

45
static char buffer_num[20];
56

6-
extern char getch();
7+
extern int getch_low();
8+
9+
char getch() {
10+
return getch_low();
11+
}
712

813
void read_line(char *str) {
914
int i = 0;
1015
while(1) {
11-
str[i]=getch();
12-
if(str[i]=='\r') {
16+
// Bug: Wierd hack to mitigate another hack.
17+
// Using following instead of str[i]=getch();
18+
char z = getch();
19+
str[i] = z;
20+
21+
if(str[i]=='\r' || str[i]=='\n') {
1322
str[i]='\0';
1423
print_char('\n');
1524
break;

src/lib/utils/string.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,29 @@ int strcmp(char *l, char *r) {
2424
}
2525
return -1;
2626
}
27+
28+
char tolower(char x) {
29+
if(x>='A' && x<='Z') {
30+
return x-'A'+'a';
31+
}
32+
return x;
33+
}
34+
35+
int strcmpi(char *l, char *r) {
36+
int i=0,j=0;
37+
while(l[i]!='\0' && r[j]!='\0') {
38+
char a = tolower(l[i]);
39+
char b = tolower(r[j]);
40+
if(a!=b) {
41+
return a-b;
42+
}
43+
i++;j++;
44+
}
45+
if(l[i]=='\0' && r[j]=='\0') {
46+
return 0;
47+
}
48+
if(l[i]=='\0') {
49+
return 1;
50+
}
51+
return -1;
52+
}

0 commit comments

Comments
 (0)