From 9e256a7e3caef349c7150d9afc2ebc4246211d69 Mon Sep 17 00:00:00 2001 From: Ry Date: Sat, 21 Jan 2023 00:17:10 -0800 Subject: [PATCH] sh: Move to an application outside of the kernel Fixes issue #3 --- Makefile | 4 + .../sh}/commands/clear.asm | 0 .../sh}/commands/commands.asm | 14 +- .../sh}/commands/dir.asm | 0 .../sh}/commands/disk.asm | 0 .../sh}/commands/diskrm.asm | 0 .../sh}/commands/exit.asm | 0 .../sh}/commands/help.asm | 0 .../sh}/commands/type.asm | 0 {kernel/shell => applications/sh}/launch.asm | 2 +- .../shell.asm => applications/sh/main.asm | 131 ++++++------------ applications/terminal/main.asm | 3 + applications/terminal/task.asm | 69 +++++++++ applications/terminal/text.asm | 21 +++ fox32os.def | 7 +- kernel/main.asm | 7 +- 16 files changed, 154 insertions(+), 104 deletions(-) rename {kernel/shell => applications/sh}/commands/clear.asm (100%) rename {kernel/shell => applications/sh}/commands/commands.asm (79%) rename {kernel/shell => applications/sh}/commands/dir.asm (100%) rename {kernel/shell => applications/sh}/commands/disk.asm (100%) rename {kernel/shell => applications/sh}/commands/diskrm.asm (100%) rename {kernel/shell => applications/sh}/commands/exit.asm (100%) rename {kernel/shell => applications/sh}/commands/help.asm (100%) rename {kernel/shell => applications/sh}/commands/type.asm (100%) rename {kernel/shell => applications/sh}/launch.asm (99%) rename kernel/shell/shell.asm => applications/sh/main.asm (86%) create mode 100644 applications/terminal/task.asm diff --git a/Makefile b/Makefile index 05cdcb6..c0b7efc 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ base_image: base_image/kernel.fxf: kernel/main.asm $(FOX32ASM) $< $@ +base_image/sh.fxf: applications/sh/main.asm + $(FOX32ASM) $< $@ + base_image/barclock.fxf: applications/barclock/main.asm $(FOX32ASM) $< $@ @@ -36,6 +39,7 @@ bootloader/bootloader.bin: bootloader/main.asm FILES = \ base_image/kernel.fxf \ + base_image/sh.fxf \ base_image/barclock.fxf \ base_image/terminal.fxf \ base_image/foxpaint.fxf \ diff --git a/kernel/shell/commands/clear.asm b/applications/sh/commands/clear.asm similarity index 100% rename from kernel/shell/commands/clear.asm rename to applications/sh/commands/clear.asm diff --git a/kernel/shell/commands/commands.asm b/applications/sh/commands/commands.asm similarity index 79% rename from kernel/shell/commands/commands.asm rename to applications/sh/commands/commands.asm index af40a13..8f6031a 100644 --- a/kernel/shell/commands/commands.asm +++ b/applications/sh/commands/commands.asm @@ -50,10 +50,10 @@ shell_parse_command: shell_invalid_command_string: data.str "invalid command or FXF binary" data.8 10 data.8 0 ; all commands - #include "shell/commands/clear.asm" - #include "shell/commands/dir.asm" - #include "shell/commands/disk.asm" - #include "shell/commands/diskrm.asm" - #include "shell/commands/exit.asm" - #include "shell/commands/help.asm" - #include "shell/commands/type.asm" + #include "commands/clear.asm" + #include "commands/dir.asm" + #include "commands/disk.asm" + #include "commands/diskrm.asm" + #include "commands/exit.asm" + #include "commands/help.asm" + #include "commands/type.asm" diff --git a/kernel/shell/commands/dir.asm b/applications/sh/commands/dir.asm similarity index 100% rename from kernel/shell/commands/dir.asm rename to applications/sh/commands/dir.asm diff --git a/kernel/shell/commands/disk.asm b/applications/sh/commands/disk.asm similarity index 100% rename from kernel/shell/commands/disk.asm rename to applications/sh/commands/disk.asm diff --git a/kernel/shell/commands/diskrm.asm b/applications/sh/commands/diskrm.asm similarity index 100% rename from kernel/shell/commands/diskrm.asm rename to applications/sh/commands/diskrm.asm diff --git a/kernel/shell/commands/exit.asm b/applications/sh/commands/exit.asm similarity index 100% rename from kernel/shell/commands/exit.asm rename to applications/sh/commands/exit.asm diff --git a/kernel/shell/commands/help.asm b/applications/sh/commands/help.asm similarity index 100% rename from kernel/shell/commands/help.asm rename to applications/sh/commands/help.asm diff --git a/kernel/shell/commands/type.asm b/applications/sh/commands/type.asm similarity index 100% rename from kernel/shell/commands/type.asm rename to applications/sh/commands/type.asm diff --git a/kernel/shell/launch.asm b/applications/sh/launch.asm similarity index 99% rename from kernel/shell/launch.asm rename to applications/sh/launch.asm index 225118a..e583571 100644 --- a/kernel/shell/launch.asm +++ b/applications/sh/launch.asm @@ -38,7 +38,7 @@ launch_fxf_name_loop_done: mov r0, launch_fxf_name movz.8 r1, [shell_current_disk] mov r2, launch_fxf_struct - call ryfs_open + call open cmp r0, 0 ifz ret diff --git a/kernel/shell/shell.asm b/applications/sh/main.asm similarity index 86% rename from kernel/shell/shell.asm rename to applications/sh/main.asm index e2f2a53..5965148 100644 --- a/kernel/shell/shell.asm +++ b/applications/sh/main.asm @@ -1,90 +1,8 @@ -; shell routines +; fox32os shell const CURSOR: 0x8A const REDRAW_LINE: 0xFE -; create a new shell task -; inputs: -; r0: task ID -; r1: pointer to stream struct -; outputs: -; none -new_shell_task: - push r0 - push r1 - push r2 - push r3 - push r4 - push r10 - - ; allocate a 64KiB stack and push the pointer to the stream struct to it - push r0 - push r1 - mov r0, 65536 - call allocate_memory - add r0, 65532 - pop r1 - mov [r0], r1 - mov r10, r0 - pop r0 - - ; then start the task - mov r1, shell_task ; initial instruction pointer - mov r2, r10 ; initial stack pointer - mov r3, 0 ; pointer to task code block to free when task ends - ; (zero since we don't want to free any code blocks when the task ends) - mov r4, r10 ; pointer to task stack block to free when task ends - sub r4, 65536 ; point to the start of the stack block that we allocated above - call new_task - - pop r10 - pop r4 - pop r3 - pop r2 - pop r1 - pop r0 - ret - -; print a character to the terminal -; inputs: -; r0: ASCII character -; outputs: -; none -print_character_to_terminal: - push r1 - push r2 - - mov.8 [shell_char_buffer], r0 - mov r1, [shell_terminal_stream_struct_ptr] - mov r2, shell_char_buffer - call write - - pop r2 - pop r1 - ret - -; print a string to the terminal -; inputs: -; r0: pointer to null-terminated string -; outputs: -; none -print_str_to_terminal: - push r0 - push r2 - - mov r1, [shell_terminal_stream_struct_ptr] - mov r2, r0 -print_str_to_terminal_loop: - call write - inc r2 - cmp.8 [r2], 0x00 - ifnz jmp print_str_to_terminal_loop - - pop r2 - pop r0 - ret - -shell_task: pop [shell_terminal_stream_struct_ptr] shell_task_return: call shell_clear_buffer @@ -309,6 +227,45 @@ shell_clear_buffer: pop r0 ret +; print a character to the terminal +; inputs: +; r0: ASCII character +; outputs: +; none +print_character_to_terminal: + push r1 + push r2 + + mov.8 [shell_char_buffer], r0 + mov r1, [shell_terminal_stream_struct_ptr] + mov r2, shell_char_buffer + call write + + pop r2 + pop r1 + ret + +; print a string to the terminal +; inputs: +; r0: pointer to null-terminated string +; outputs: +; none +print_str_to_terminal: + push r0 + push r2 + + mov r1, [shell_terminal_stream_struct_ptr] + mov r2, r0 +print_str_to_terminal_loop: + call write + inc r2 + cmp.8 [r2], 0x00 + ifnz jmp print_str_to_terminal_loop + + pop r2 + pop r0 + ret + shell_text_buf_bottom: data.fill 0, 512 shell_text_buf_top: shell_text_buf_ptr: data.32 0 ; pointer to the current input character @@ -321,5 +278,9 @@ shell_prompt: data.str "> " data.8 CURSOR data.8 0 shell_terminal_stream_struct_ptr: data.32 0 shell_char_buffer: data.32 0 - #include "shell/commands/commands.asm" - #include "shell/launch.asm" + #include "commands/commands.asm" + #include "launch.asm" + + ; include system defs + #include "../../../fox32rom/fox32rom.def" + #include "../../fox32os.def" diff --git a/applications/terminal/main.asm b/applications/terminal/main.asm index 98fcb30..91a269c 100644 --- a/applications/terminal/main.asm +++ b/applications/terminal/main.asm @@ -1,5 +1,6 @@ ; terminal + ; create the window mov r0, window_struct mov r1, window_title mov r2, 320 @@ -10,6 +11,7 @@ mov r7, 0 call new_window + ; start an instance of sh.fxf call get_unused_task_id mov.8 [shell_task_id], r0 mov r1, stream_struct @@ -103,6 +105,7 @@ stream_struct: data.32 stream_write_to_terminal #include "stream.asm" + #include "task.asm" #include "text.asm" ; include system defs diff --git a/applications/terminal/task.asm b/applications/terminal/task.asm new file mode 100644 index 0000000..a5ba108 --- /dev/null +++ b/applications/terminal/task.asm @@ -0,0 +1,69 @@ +; sh.fxf launching routines + +; start an instance of sh.fxf +; inputs: +; r0: task ID +; r1: pointer to stream struct +; outputs: +; none +new_shell_task: + push r0 + push r1 + + ; open the file + mov r0, sh_fxf_name + mov r1, 0 + mov r2, sh_fxf_struct + call open + cmp r0, 0 + ifz jmp sh_fxf_missing + + ; allocate memory for the binary + mov r0, sh_fxf_struct + call ryfs_get_size + call allocate_memory + cmp r0, 0 + ifz jmp sh_fxf_missing + mov [sh_fxf_binary_ptr], r0 + + ; read the file into memory + mov r0, sh_fxf_struct + mov r1, [sh_fxf_binary_ptr] + call ryfs_read_whole_file + + ; allocate a 64KiB stack + mov r0, 65536 + call allocate_memory + cmp r0, 0 + ifz jmp sh_fxf_missing + mov [sh_fxf_stack_ptr], r0 + + ; push the stream struct pointer to the shell's stack + add r0, 65532 + pop r1 + mov [r0], r1 + mov r10, r0 + + ; relocate the binary + mov r0, [sh_fxf_binary_ptr] + call parse_fxf_binary + + ; then start the task + mov r1, r0 ; initial instruction pointer + pop r0 ; task ID + mov r2, r10 ; initial stack pointer + mov r3, [sh_fxf_binary_ptr] ; pointer to task code block to free when task ends + mov r4, [sh_fxf_stack_ptr] ; pointer to task stack block to free when task ends + call new_task + ret + +sh_fxf_missing: + mov r0, sh_fxf_missing_str + call print_str_to_terminal + rjmp 0 + +sh_fxf_name: data.str "sh fxf" +sh_fxf_struct: data.fill 0, 8 +sh_fxf_missing_str: data.str "sh could not be launched!" data.8 0 +sh_fxf_binary_ptr: data.32 0 +sh_fxf_stack_ptr: data.32 0 diff --git a/applications/terminal/text.asm b/applications/terminal/text.asm index d26e403..700bcc0 100644 --- a/applications/terminal/text.asm +++ b/applications/terminal/text.asm @@ -11,6 +11,27 @@ const MOVE_CURSOR: 0xF1 const REDRAW_LINE: 0xFE const REDRAW: 0xFF +; print a string to the terminal +; inputs: +; r0: pointer to null-terminated string +; outputs: +; none +print_str_to_terminal: + push r0 + push r1 + + mov r1, r0 +print_str_to_terminal_loop: + movz.8 r0, [r1] + call print_character_to_terminal + inc r1 + cmp.8 [r1], 0x00 + ifnz jmp print_str_to_terminal_loop + + pop r1 + pop r0 + ret + ; print a single character to the terminal ; inputs: ; r0: ASCII character or control character diff --git a/fox32os.def b/fox32os.def index 44d3366..1627c3e 100644 --- a/fox32os.def +++ b/fox32os.def @@ -36,12 +36,9 @@ tell: jmp [0x00000D18] read: jmp [0x00000D1C] write: jmp [0x00000D20] -; shell jump table -new_shell_task: jmp [0x00000E10] - ; widget jump table -draw_widgets_to_window: jmp [0x00000F10] -handle_widget_click: jmp [0x00000F14] +draw_widgets_to_window: jmp [0x00000E10] +handle_widget_click: jmp [0x00000E14] ; event types const EVENT_TYPE_BUTTON_CLICK: 0x80000000 diff --git a/kernel/main.asm b/kernel/main.asm index 10a97fb..c07b64a 100644 --- a/kernel/main.asm +++ b/kernel/main.asm @@ -53,12 +53,8 @@ jump_table: data.32 read data.32 write - ; shell jump table - org.pad 0x00000610 - data.32 new_shell_task - ; widget jump table - org.pad 0x00000710 + org.pad 0x00000610 data.32 draw_widgets_to_window data.32 handle_widget_click jump_table_end: @@ -302,7 +298,6 @@ get_os_version: #include "allocator.asm" #include "fxf/fxf.asm" - #include "shell/shell.asm" #include "task.asm" #include "widget/widget.asm" #include "window/window.asm"