Skip to content

Decouple Text VGA out of user app #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ls.out
# tictactoe.out
# calc.out
INIT_APPNAME ?= sh.out
INIT_APPNAME ?= sh

ROOT_DIR = .
BUILD_DIR = build
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The screenshots can be located as `Artifacts` under completed run on [Actions/CI

Simple Shell | TicTacToe Game
--------------| ------------------
![image](https://user-images.githubusercontent.com/9819066/129453427-41e9322f-5e87-4d46-a4dc-0fd70b68e4a7.png) | ![image](https://user-images.githubusercontent.com/9819066/119272299-2bbf2800-bbfd-11eb-9e8a-350946e1218b.png)
![image](https://user-images.githubusercontent.com/9819066/129463788-94686a08-236e-49a9-b81b-6325e4b2ab58.png) | ![image](https://user-images.githubusercontent.com/9819066/129463802-d9a0bc77-74eb-4438-b553-e1439ada95a1.png)

### Boot OS

Expand Down
2 changes: 1 addition & 1 deletion external/src/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ $(BUILD_DIR)/external/bin/mbr_builder: external/src/mbr_builder.c
mkdir -p $(dir $@)
$(HOST_CC) -o $@ $<

$(BUILD_DIR)/external/out/fuzzy_mount: $(patsubst $(SRC_APP)/%.c,$(BUILD_APP)/%.out,$(shell find $(SRC_APP)/ -name '*.c')) \
$(BUILD_DIR)/external/out/fuzzy_mount: $(patsubst $(SRC_APP)/%.c,$(BUILD_APP)/%,$(shell find $(SRC_APP)/ -name '*.c')) \
README.md \
.gdbinit
# not a mount
Expand Down
3 changes: 3 additions & 0 deletions include/fuzzy/kernel/syscall/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

int syscall_3_console(int operation, int a1, int a2, int a3, int user_ds);
6 changes: 5 additions & 1 deletion src/kernel/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>
#include <process.h>
#include <sys/syscall.h>
#include <conio.h>

#include <drivers/disk/disk.h>
#include <drivers/display/text_mode.h>
Expand Down Expand Up @@ -45,8 +46,11 @@ void kernel_core_entry() {

process_scheduler_init();

clrscr();

int init_pid = spawn(INIT_APPNAME);
print_log("init process created: %d", init_pid);
interrupt_pit_enable();

// interrupt_pit_enable();
while (1);
}
3 changes: 0 additions & 3 deletions src/kernel/process/allocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ int process_create() {
process->ss = get_gdt_number_from_entry_id(idt_ds_entry);
// should be compatible with create_infant_process_irq0_stack
process->sp = 0xFFF0-56; // keep offset in sync with _int_irq0_start
// TODO:
// return -1;



// Potential improvement:
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/process/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int process_spawn(int lba_index, int sector_count) {
print_log("Failed to load app in memory, Error: ", err);
return -2;
}
print_log("[process_spawn] ready, pid: %d", pid);
print_info("[process_spawn] ready, pid: %d", pid);
struct Process *process = get_process(pid);
process->state = STATE_READY;
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/process/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void process_scheduler(int *_e_ip, int *_e_cs, int *_e_sp, int *_e_ss) {
PANIC(0, "[process_scheduler] no STATE_READY process alive");
}

print_log("[process_scheduler] pid: %d -> %d", pid, npid);
print_info("[process_scheduler] pid: %d -> %d", pid, npid);
struct Process *process = get_process(pid);
if(process->state != STATE_COLD) {
// if last process is still alive
Expand Down
26 changes: 26 additions & 0 deletions src/kernel/syscall/console.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <fuzzy/kernel/syscall/console.h>

#include <stdio.h>

#include <lib/utils/output.h>

int _console_putchar(char c) {
print_char(c);
return 0;
}

int _console_clrscr() {
print_rectangle(0, 0, TEXT_WINDOW_WIDTH-1, TEXT_WINDOW_HEIGHT-1);
move_xy(0,0);
}

int syscall_3_console(int operation, int a1, int a2, int a3, int user_ds) {
switch (operation) {
case SYSCALL_CONSOLE_SUB_CLRSCR:
return _console_clrscr();
case SYSCALL_CONSOLE_SUB_PUTCHAR:
return _console_putchar((char)a1);
}
return -1;
}

10 changes: 7 additions & 3 deletions src/kernel/syscall/syscall.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <fuzzy/kernel/interrupts/interrupts.h>
#include <fuzzy/kernel/process/process.h>
#include <fuzzy/kernel/syscall/file_handler.h>
#include <fuzzy/kernel/syscall/console.h>
#include <fuzzy/kernel/syscall/syscall.h>

#include <sys/syscall.h>

#include <drivers/keyboard/keyboard.h>
#include <lib/utils/logging.h>

Expand All @@ -19,9 +22,10 @@ extern int interrupt_handler_0x32_syscall_handler();
void interrupt_register_0x32_syscall() {
print_log("Registering syscalls.");
populate_idt_entry_32bit(IDT_SYSCALL, (unsigned int)interrupt_handler_0x32_syscall_handler, 0, 1);
SYSCALL_TABLE[0]=syscall_0_keyboard_getch;
SYSCALL_TABLE[1]=syscall_1_process;
SYSCALL_TABLE[2]=syscall_2_file_handler;
SYSCALL_TABLE[SYSCALL_KEYBOARD]=syscall_0_keyboard_getch;
SYSCALL_TABLE[SYSCALL_PROCESS]=syscall_1_process;
SYSCALL_TABLE[SYSCALL_FILE_OP]=syscall_2_file_handler;
SYSCALL_TABLE[SYSCALL_CONSOLE]=syscall_3_console;
}

int syscall_selector(int id, int arg0, int arg1, int arg2, int arg3, int user_ds) {
Expand Down
6 changes: 1 addition & 5 deletions src/lib/app/Makefile.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
BUILD_LIB_APP_ENTRY = $(addprefix $(BUILD_LIB)/app/,entry_asm.o entry.o)
BUILD_LIB_APP_ENTRY = $(addprefix $(BUILD_LIB)/app/,entry_asm.o)

$(BUILD_LIB)/app/%_asm.o: $(SRC_LIB)/app/%.asm
mkdir -p $(BUILD_LIB)/app
nasm -o $@ -f elf32 $<

$(BUILD_LIB)/app/%.o: $(SRC_LIB)/app/%.c $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/color.h
mkdir -p $(BUILD_LIB)
$(KERNEL_CC) -c -o $@ $<
2 changes: 0 additions & 2 deletions src/lib/app/entry.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
[BITS 32]

extern main
extern entry_console_init
extern exit

[SECTION .text]
_start:
call entry_console_init
call main ; return value should be in eax
push eax
call exit ; process should be unallocated before returning from exit
9 changes: 0 additions & 9 deletions src/lib/app/entry.c

This file was deleted.

3 changes: 0 additions & 3 deletions src/lib/app/entry.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/usr/include/conio.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

char getch();

// Temporarily added and can be deprecated anytime.
void gotoxy(unsigned char x, unsigned char y);
void clrscr();
3 changes: 2 additions & 1 deletion src/usr/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

#include <stddef.h>

// TODO: define stdio, stdout, stderr
#define SYSCALL_CONSOLE_SUB_CLRSCR 0
#define SYSCALL_CONSOLE_SUB_PUTCHAR 1

int putchar(int c);
int puts(const char *s);
Expand Down
1 change: 1 addition & 0 deletions src/usr/include/sys/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define SYSCALL_KEYBOARD 0
#define SYSCALL_PROCESS 1
#define SYSCALL_FILE_OP 2
#define SYSCALL_CONSOLE 3

#define SYSCALL_A0(id) \
syscall((id), 0, 0, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/usr/lib/Makefile.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SRC_USR_LIB=$(SRC_DIR)/usr/lib
BUILD_USR_LIB=$(BUILD_DIR)/usr/lib

$(BUILD_USR_LIB)/%.o: $(SRC_USR_LIB)/%.c $(BUILD_USR_INCLUDE_ALL) $(SRC_LIB_UTILS)/output.h $(SRC_LIB_UTILS)/input.h $(SRC_LIB)/app/entry.h
$(BUILD_USR_LIB)/%.o: $(SRC_USR_LIB)/%.c $(BUILD_USR_INCLUDE_ALL)
mkdir -p $(dir $@)
# TODO: Clean up sector location
$(KERNEL_CC) -c -o $@ \
Expand Down
15 changes: 4 additions & 11 deletions src/usr/lib/conio.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
// #include <lib/syscall/syscall.h>
#include <lib/utils/output.h>
#include <lib/utils/input.h>
#include <lib/app/entry.h>
#include <sys/syscall.h>
#include <stdio.h>

char getch() {
// return '0';
return getch_in();
}

void gotoxy(unsigned char x, unsigned char y) {
move_xy(x, y);
return SYSCALL_A0(SYSCALL_KEYBOARD);
}

void clrscr() {
entry_console_init();
SYSCALL_A1(SYSCALL_CONSOLE, SYSCALL_CONSOLE_SUB_CLRSCR);
}
7 changes: 2 additions & 5 deletions src/usr/lib/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@
#include <stdlib.h>
#include <stdarg.h>
#include <conio.h>
#include <lib/utils/output.h>
#include <sys/syscall.h>

int putchar(int c) {
print_char(c);
// TODO: Fix putchar return value.
return 0;
return SYSCALL_A2(SYSCALL_CONSOLE, SYSCALL_CONSOLE_SUB_PUTCHAR, c);
}

int puts(const char *s) {
if(!s) return 0;
int c = 0;
while ((*s)!='\0') {
print_char(*(s++));
putchar(*(s++));
c++;
}
return c;
Expand Down
5 changes: 2 additions & 3 deletions src/usr/local/src/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ $(BUILD_APP)/%.o: $(SRC_APP)/%.c $(BUILD_USR_INCLUDE_ALL) $(SRC_LIB_UTILS)/outpu
mkdir -p $(BUILD_APP)
$(USER_CC) -c -o $@ $<

# TODO: libfuzzyc and libutils should not be cyclic dependent.
$(BUILD_APP)/%.elf: $(BUILD_LIB_APP_ENTRY) $(BUILD_APP)/%.o $(BUILD_USR_LIB)/libfuzzyc $(BUILD_LIB_UTILS)/libutils $(BUILD_DRIVERS)/display/libtm_vga
$(BUILD_APP)/%.elf: $(BUILD_LIB_APP_ENTRY) $(BUILD_APP)/%.o $(BUILD_USR_LIB)/libfuzzyc
$(USER_LD) -o $@ $^ $(BUILD_USR_LIB)/libfuzzyc

$(BUILD_APP)/%.out: $(BUILD_APP)/%.elf
$(BUILD_APP)/%: $(BUILD_APP)/%.elf
$(FLAT_FROM_ELF) $< $@
truncate --size=%512 $@
2 changes: 0 additions & 2 deletions src/usr/local/src/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ void process(char filename[]) {
int main(int argc,char *argv[]) {
char filename[] = "README.md";
process(filename);
puts("Press return key to exit...\n");
getch();
return 0;
}
5 changes: 1 addition & 4 deletions src/usr/local/src/ls.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
#include <dirent.h>

void process() {
puts("list files:\n");
struct DIR dir;
opendir(&dir);
struct dirent *dp;

while ((dp = readdir(&dir)) !=NULL) {
printf("- %s \n", dp->d_name);
printf("%s\n", dp->d_name);
}
}

int main(int argc,char *argv[]) {
process();
puts("Press return key to exit...\n");
getch();
return 0;
}
35 changes: 3 additions & 32 deletions src/usr/local/src/sh.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// simple shell
#include <stdio.h>
#include <process.h>
#include <dirent.h>
#include <string.h>

const int COMMAND_SIZE = 200;
Expand All @@ -15,25 +14,14 @@ void banner() {
int cmd_help() {
printf("commands\n");
printf(" > help\n");
printf(" > dir\n");
printf(" > run ls\n");
printf(" > run <filename>\n");
printf(" > echo <text>\n");
printf(" > status\n");
printf(" > exit\n");
return 0;
}

int cmd_dir() {
struct DIR dir;
opendir(&dir);
struct dirent *dp;

while ((dp = readdir(&dir)) !=NULL) {
printf("%s \n", dp->d_name);
}
return 0;
}

int cmd_run(char *fname) {
if(fname == NULL) {
printf("invalid syntax\n");
Expand All @@ -52,33 +40,20 @@ int cmd_echo(char *text) {
return printf("%s\n", text);
}

int cmd_status_code() {
printf("last cmd status code: %d\n", last_status_code);
return 0;
}

int cmd_exit() {
exit(0);
return 0;
}

void handle_command(char *full_cmd) {
// BUG
// program executes even if arg0 is not provided
// if followed by invalid instruction containing the
// arg0.
// steps to reproduce:
// - execute ls.out
// - run

// syntax: <cmd> [arg0]
char cmd[12]; // for now: excepts no cmd to use all bytes.
memcpy(cmd, full_cmd, sizeof(cmd));
char *arg0;
{
// <cmd>
int i = 0;
while(i+1<sizeof(cmd) && cmd[i]!=' ') i++;
while(i+1<sizeof(cmd) && cmd[i]!=' ' && cmd[i]!='\0') i++;
cmd[i++]='\0';

// [arg0]
Expand All @@ -91,14 +66,10 @@ void handle_command(char *full_cmd) {

if (strcmp(cmd, "help")==0) {
last_status_code = cmd_help();
} else if (strcmp(cmd, "dir")==0) {
last_status_code = cmd_dir();
} else if (strcmp(cmd, "run")==0) {
last_status_code = cmd_run(arg0);
} else if (strcmp(cmd, "echo")==0) {
last_status_code = cmd_echo(arg0);
} else if (strcmp(cmd, "status")==0) {
last_status_code = cmd_status_code();
} else if (strcmp(cmd, "exit")==0) {
last_status_code = cmd_exit();
} else {
Expand All @@ -113,7 +84,7 @@ int main(int argc,char *argv[]) {
cmd_help();
int c = 0;
while (1) {
puts("$ ");
printf("[%d] $ ", last_status_code);
gets(command);
handle_command(command);
}
Expand Down
Loading