Skip to content

Commit 7f180b6

Browse files
committed
Added calculator and dashboard
1 parent b8aa602 commit 7f180b6

File tree

10 files changed

+393
-193
lines changed

10 files changed

+393
-193
lines changed

Makefile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
BL_SRC_DIR = src/bootloader
2+
SYSCALLS_SRC_DIR = src/lib/syscalls
3+
UTIL_SRC_DIR = src/lib/util
24
BUILD_DIR = build
5+
APP_DIR = src/app
36

47
.PHONY: all clean
58

@@ -10,8 +13,12 @@ bt_stage2_asm_o = $(BUILD_DIR)/bt_stage2_asm.o
1013
bt_stage2 = $(BUILD_DIR)/bt_stage2
1114
image_vmdk = $(BUILD_DIR)/image.vmdk
1215

16+
# Apps
17+
app_calc = $(BUILD_DIR)/calc
18+
app_dashboard = $(BUILD_DIR)/dashboard
19+
1320
# Parameters
14-
BT_STAGE2_SECTOR_COUNT = 11 # In Hex
21+
BT_STAGE2_SECTOR_COUNT = 19 # In Hex
1522

1623
rebuild: clean all
1724

@@ -39,11 +46,11 @@ $(bt_stage1): $(BL_SRC_DIR)/stage1.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)
3946
$(bt_stage2): $(bt_stage2_asm_o) $(bt_stage2_c_o)
4047
ld --oformat binary -m elf_i386 -Ttext 0x8000 --strip-all -o $@ $^
4148

42-
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c
43-
gcc -m16 -fno-pie -c -o $@ $<
49+
$(bt_stage2_c_o): $(BL_SRC_DIR)/stage2.c $(SYSCALLS_SRC_DIR)/basic.h $(SYSCALLS_SRC_DIR)/io.h $(SYSCALLS_SRC_DIR)/time.h $(SYSCALLS_SRC_DIR)/color.h $(UTIL_SRC_DIR)/string.h $(APP_DIR)/dashboard.c $(APP_DIR)/calc.c
50+
gcc -m16 -fno-pie -c -Isrc -o $@ $<
4451

45-
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(bt_stage2_o)
46-
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ $<
52+
$(bt_stage2_asm_o): $(BL_SRC_DIR)/stage2.asm $(BL_SRC_DIR)/constants.asm $(BL_SRC_DIR)/io.asm $(SYSCALLS_SRC_DIR)/io_syscall.asm $(SYSCALLS_SRC_DIR)/time_syscall.asm $(bt_stage2_o)
53+
nasm -o $@ -f elf32 -i $(BL_SRC_DIR)/ -i$(SYSCALLS_SRC_DIR)/ $<
4754

4855
debug_stage1: $(bt_stage1)
4956
objdump -b binary -mi386 -Maddr16,data16 -D $<
@@ -61,5 +68,8 @@ debug_stage2_asm: $(bt_stage2_asm_o)
6168
objdump -d -Maddr16,data16 $<
6269
xxd $<
6370

71+
qemu: $(image_vmdk) images
72+
cpulimit -f -l 10 -- qemu-system-x86_64 -smp 1 -m 1M -hda $<
73+
6474
clean:
6575
rm -f $(image_vmdk) $(bt_stage1) $(bt_stage2) $(bt_stage2_c_o) $(bt_stage2_asm_o)

src/app/calc.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Simple Calculator
2+
#define BUILD_FOR_FUZZY
3+
#ifndef BUILD_FOR_FUZZY
4+
#include<stdio.h>
5+
void read_line(char *s) {
6+
scanf("%[^\n]",s);;
7+
}
8+
void print_line(char *s) {
9+
printf("%s", s);
10+
}
11+
void print_int(int x) {
12+
printf("%d", x);
13+
}
14+
#else
15+
#include <lib/syscalls/io.h>
16+
#include <lib/syscalls/time.h>
17+
#include <lib/util/string.h>
18+
#endif
19+
20+
int err;
21+
char tokens[2][20];
22+
int a,b;
23+
char op;
24+
int t;
25+
int tl;
26+
int solve(char s[]) {
27+
t=0;
28+
tl=0;
29+
op = 0;
30+
for(int i=0;s[i]!='\0';i++) {
31+
if(s[i]>='0' && s[i]<='9') {
32+
if(t>=2) {
33+
err = 1;
34+
return 0;
35+
}
36+
tokens[t][tl]=s[i];
37+
tl++;
38+
} else {
39+
if(op!=0) {
40+
err = 2;
41+
return 0;
42+
}
43+
op=s[i];
44+
tokens[t][tl]='\0';
45+
tl=0;
46+
t++;
47+
}
48+
}
49+
tokens[t][tl]='\0';
50+
tl=0;
51+
t++;
52+
if(t!=2) {
53+
err = 3;
54+
return 0;
55+
}
56+
a = parse_int(tokens[0]);
57+
b = parse_int(tokens[1]);
58+
switch(op) {
59+
case '+':
60+
return a+b;
61+
case '-':
62+
return a-b;
63+
case '*':
64+
return a*b;
65+
case '/':
66+
return a/b;
67+
default:
68+
err = 4;
69+
}
70+
return 0;
71+
}
72+
73+
void show_usage() {
74+
print_line(" Usage: \n");
75+
print_line(" HELP - Show Usage\n");
76+
print_line(" EXIT - To quit Program\n");
77+
print_line("\n");
78+
print_line(" <num><op><num> to calculate expression.\n");
79+
print_line(" Where <op> can be one of +-*/.\n");
80+
}
81+
char expr[100];
82+
int result;
83+
84+
int handle_expression(char str[]) {
85+
if(strcmp(str, "HELP")==0) {
86+
show_usage();
87+
return 1;
88+
}
89+
if(strcmp(str, "EXIT")==0) {
90+
return 0;
91+
}
92+
result = solve(str);
93+
if(err) {
94+
print_line(" Error[");
95+
print_int(err);
96+
print_line("]: Only '<num> [+-*/] <num>' syntax supported!\n");
97+
print_line(" Type 'HELP' for instructions!\n\n");
98+
} else {
99+
print_line(" Result: ");
100+
print_int(result);
101+
print_line("\n\n");
102+
}
103+
return 1;
104+
}
105+
106+
int calc_main(int argc,char **argv) {
107+
print_line("Simple Calculator\n");
108+
print_line("-----------------\n");
109+
print_line("\n");
110+
show_usage();
111+
while(1) {
112+
err = 0;
113+
print_line("Expression: ");
114+
read_line(expr);
115+
if(!handle_expression(expr)) {
116+
break;
117+
}
118+
}
119+
return 0;
120+
}

src/app/dashboard.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <app/calc.c>
2+
#include <lib/syscalls/io.h>
3+
#include <lib/syscalls/time.h>
4+
#include <lib/syscalls/color.h>
5+
#include <lib/util/string.h>
6+
7+
char query_app_number[] = "Enter Application Number: ";
8+
char application_list[5][15] = {"Calculator", "Sample 2", "Sample 3", "Sample 4", "Sample 5"};
9+
void print_applications(unsigned char x, unsigned char y, char *list, unsigned char count, unsigned char max_strlen) {
10+
for(int i=1;i<=count;i++) {
11+
move_xy(x,y+i);
12+
set_color_fg(C_BLUE);
13+
print_int(i);
14+
print_line(" ");
15+
set_color_fg(C_BLACK);
16+
print_line(list);
17+
list+=max_strlen;
18+
}
19+
}
20+
21+
void console_init() {
22+
set_color_bg(C_BLACK);
23+
set_color_fg(C_WHITE);
24+
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
25+
move_xy(0,0);
26+
}
27+
28+
int run_dashboard(int argc,char **argv) {
29+
while(1) {
30+
set_color_bg(C_DARK_GRAY);
31+
set_color_fg(C_WHITE);
32+
print_rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
33+
move_xy(1,0);
34+
print_line("Fuzzy OS");
35+
36+
set_color_bg(C_LIGHT_GRAY);
37+
set_color_fg(C_BLACK);
38+
print_rectangle(1, 1, WINDOW_WIDTH-1, WINDOW_HEIGHT-1);
39+
move_xy(3,3);
40+
41+
print_line(query_app_number);
42+
print_applications(4,4, (char*)application_list, 5, 15);
43+
move_xy(3+sizeof(query_app_number)-1,3);
44+
int num = read_int();
45+
console_init();
46+
calc_main(0,0);
47+
}
48+
return 0;
49+
}

src/bootloader/stage2.asm

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
; Fuzzy Bootloader Stage 2
22
%include "constants.asm"
33
%include "io.asm"
4+
%include "io_syscall.asm"
5+
%include "time_syscall.asm"
46

57
[BITS 16]
68

79
extern entry_stage
810

9-
global _low_put_chars
10-
global _low_move_xy
11-
global _low_clear_screen
12-
global _low_sleep
13-
1411
[SECTION .text]
1512
CLI
1613
MOV ax, 0x0000
@@ -21,68 +18,13 @@ global _low_sleep
2118
call entry_stage
2219
JMP label_exit
2320

24-
_low_put_chars:
25-
push ebp
26-
mov ebp, esp
27-
28-
mov ah, 0x09 ; (write)
29-
mov al, [ebp + 0x8] ; (char)
30-
mov cx, [ebp + 0xc] ; (count)
31-
mov bh, 0x00 ; (pagenumber)
32-
mov bl, [ebp + 0x10] ; (attribute)
33-
int 0x10
34-
35-
mov esp, ebp
36-
pop ebp
37-
ret
38-
39-
_low_move_xy:
40-
push ebp
41-
mov ebp, esp
42-
43-
mov ah, 0x02 ; (set custor position)
44-
mov dl, [ebp + 0x8] ; (pos_x)
45-
mov dh, [ebp + 0xc] ; (pos_y)
46-
mov bh, [ebp + 0x10] ; (page number)
47-
int 0x10
48-
49-
mov esp, ebp
50-
pop ebp
51-
ret
52-
53-
_low_clear_screen:
54-
push ebp
55-
mov ebp, esp
56-
57-
mov ah, 0x06 ; (scroll)
58-
mov al, 0x00 ; (line count)
59-
mov bh, [ebp + 0x08] ; (attribute)
60-
mov cl, [ebp + 0x0c] ; (window top-left x)
61-
mov ch, [ebp + 0x10] ; (window top-left y)
62-
mov dl, [ebp + 0x14] ; (window bottom-right x)
63-
mov dh, [ebp + 0x18] ; (window bottom-right y)
64-
int 0x10
65-
66-
mov esp, ebp
67-
pop ebp
68-
ret
69-
70-
_low_sleep:
71-
push ebp
72-
mov ebp, esp
73-
mov ebx, [ebp + 0x08] ; (cycles)
74-
_low_sleep_internal:
75-
sub ebx, 1
76-
jnc _low_sleep_internal
77-
78-
mov esp, ebp
79-
pop ebp
80-
ret
81-
8221
label_exit:
8322
HLT
8423
JMP label_exit
8524

25+
PLUGIN_SYSCALLS_IO
26+
PLUGIN_SYSCALLS_TIME
27+
8628
[SECTION .data]
8729
bl_stage_2 db "Bootloader: Stage 2"
8830
bl_stage_2_len equ ($-bl_stage_2)

0 commit comments

Comments
 (0)