common, start printf

This commit is contained in:
sfja 2026-11-14 05:21:53 +01:00
parent 23c9cad52c
commit 725c555623
10 changed files with 334 additions and 125 deletions

View File

@ -31,6 +31,9 @@ block()
OUTPUT ${fs_file} OUTPUT ${fs_file}
COMMAND fstool ${fs_file} mkfile /kernel.bin COMMAND fstool ${fs_file} mkfile /kernel.bin
COMMAND fstool ${fs_file} import ${kernel_file} /kernel.bin COMMAND fstool ${fs_file} import ${kernel_file} /kernel.bin
COMMAND fstool ${fs_file} mkdir /docs
COMMAND fstool ${fs_file} mkfile /docs/readme.txt
COMMAND fstool ${fs_file} importb ${CMAKE_CURRENT_SOURCE_DIR}/readme.txt /docs/readme.txt
DEPENDS fstool ${kernel_file} DEPENDS fstool ${kernel_file}
VERBATIM VERBATIM
USES_TERMINAL USES_TERMINAL

View File

@ -1,5 +1,5 @@
@include "arch.vc6asm" @include "../common/arch.vc6asm"
@include "macros.vc6asm" @include "../common/macros.vc6asm"
@const stack_base 0x1fff @const stack_base 0x1fff
@const heap_base 0x2000 @const heap_base 0x2000
@ -372,6 +372,13 @@ fs_search_dir:
load r1, [DirEntry.name_len] load r1, [DirEntry.name_len]
cmp r0, r1 cmp r0, r1
jne .not_a_match jne .not_a_match
mov r3, r1
ldo r1, sp, 2
load r2, [DirEntry.name]
cmp r0, 0
je .not_a_match
mov r0, 0 mov r0, 0
jmp .leave jmp .leave
.not_a_match: .not_a_match:
@ -450,79 +457,7 @@ DirEntry:
.name_len: dw 0 .name_len: dw 0
.name: resw 61 .name: resw 61
; r1 destination @include "../common/memstr.vc6asm"
; r2 source
; r3 size
memcpy:
jmp .l0_cond
.l0_body:
load r0, [r2]
store [r1], r0
add r1, 1
add r2, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
ret
; r1 destination
; r2 value
; r3 size
memset:
jmp .l0_cond
.l0_body:
store [r1], r2
add r1, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
ret
; r1 a
; r2 b
; r3 len
; ---
; r0 if equal
strneq:
jmp .l0_cond
.l0_body:
load r0, [r1]
load fl, [r2]
cmp r0, fl
je .equal
mov r0, 0
jmp .leave
.equal:
add r1, 1
add r2, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
mov r0, 1
.leave:
ret
; r1 str
; ---
; r0 len
strlen:
mov r0, 0
jmp .l0_cond
.l0_body:
add r1, 1
add r0, 1
.l0_cond:
load r2, [r1]
cmp r2, '\0'
jne .l0_body
ret
@const BOOT_SIZE $ @const BOOT_SIZE $
@static_assert 0x800 - BOOT_SIZE & 0x8000 ^ 0x8000 ; BOOT_SIZE <= 0x800 @static_assert 0x800 - BOOT_SIZE & 0x8000 ^ 0x8000 ; BOOT_SIZE <= 0x800

105
common/memstr.vc6asm Normal file
View File

@ -0,0 +1,105 @@
@include "macros.vc6asm"
; r1 destination
; r2 source
; r3 size
memcpy:
cmp r1, r2
je .leave
jmp .l0_cond
.l0_body:
load r0, [r2]
store [r1], r0
add r1, 1
add r2, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
.leave:
ret
; r1 destination
; r2 source
; r3 size
memmove:
cmp r1, r2
jle memcpy
add r1, r3
sub r1, 1
add r2, r3
sub r2, 1
jmp .l0_cond
.l0_body:
load r0, [r2]
store [r1], r0
sub r1, 1
sub r2, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
.leave:
ret
; r1 destination
; r2 value
; r3 size
memset:
jmp .l0_cond
.l0_body:
store [r1], r2
add r1, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
ret
; r1 a
; r2 b
; r3 len
; ---
; r0 if equal
memeq:
jmp .l0_cond
.l0_body:
load r0, [r1]
load fl, [r2]
cmp r0, fl
je .equal
mov r0, 0
jmp .leave
.equal:
add r1, 1
add r2, 1
sub r3, 1
.l0_cond:
cmp r3, 0
jg .l0_body
mov r0, 1
.leave:
ret
; r1 str
; ---
; r0 len
strlen:
mov r0, 0
jmp .l0_cond
.l0_body:
add r1, 1
add r0, 1
.l0_cond:
load r2, [r1]
cmp r2, '\0'
jne .l0_body
ret

View File

@ -1,24 +1,23 @@
@include "../boot/arch.vc6asm" @include "../common/arch.vc6asm"
@include "../boot/macros.vc6asm" @include "../common/macros.vc6asm"
@const k_stack_base 0x1fff
@const k_heap_base 0x2000
@org 0x0800 @org 0x0800
main: main:
mov bp, 0x1000 mov bp, k_stack_base
mov sp, bp mov sp, bp
; mov r1, text mov r0, 40 * 5
; mov r2, VCD_BEGIN + 80 store [Vcd.row], r0
; call println
mov r1, .splash_str mov r1, .splash_str
mov r2, VCD_BEGIN + 40 * 5
call println call println
mov r1, .prompt mov r1, .prompt
mov r2, VCD_BEGIN + 40 * 7 call puts
mov r0, ip
call println
mov r0, keydown_handler mov r0, keydown_handler
store [KBD_KEYDOWN_HANDLER], r0 store [KBD_KEYDOWN_HANDLER], r0
@ -34,48 +33,95 @@ main:
keydown_handler: keydown_handler:
load r0, [KBD_SCANCODE] load r1, [KBD_SCANCODE]
add r0, 93 add r1, 93
mov r2, VCD_BEGIN + 40 * 7 + 2 call putc
add r2, r1
store [r2], r0
add r1, 1
sti sti
ret ret
println: @const vcd_width 40
push bp @const vcd_height 25
mov bp, sp
sub sp, 2
store [sp], r1 Vcd:
sto r0, r2, sp, 1 .col: dw 0
.row: dw 0
mov r1, 0 ; r1 character
jr .l0_cond putc:
.l0_body: cmp r1, '\n'
ldo r3, sp, 1 je .newline
add r3, r1
load r2, [sp]
add r2, r1
load r2, [r2]
store [r3], r2
add r1, 1 load r2, [Vcd.col]
.l0_cond: load r3, [Vcd.row]
load r2, [sp]
add r2, r1
load r2, [r2]
clf
lr r3, .l0_body
cmp r2, '\0'
not fl
and fl, FL_EQ
cmov ip, r3
.leave: add r3, r2
mov sp, bp add r3, VCD_BEGIN
pop bp store [r3], r1
add r2, 1
cmp r2, vcd_width
jge .newline
store [Vcd.col], r2
ret ret
.newline:
mov r2, 0
store [Vcd.col], r2
load r3, [Vcd.row]
add r3, 40
cmp r3, vcd_width * vcd_height
jge .scroll
store [Vcd.row], r3
ret
.scroll:
mov r1, VCD_BEGIN
mov r2, VCD_BEGIN + vcd_width
mov r3, vcd_width * (vcd_height - 1)
call memmove
mov r1, VCD_BEGIN + vcd_width * (vcd_height - 1)
mov r2, 0
mov r3, vcd_width
call memset
mov r3, vcd_width * (vcd_height - 1)
store [Vcd.row], r3
ret
; r1 string
puts:
push r1
jmp .l0_cond
.l0_body:
store [sp], r1
load r1, [r1]
call putc
load r1, [sp]
add r1, 1
.l0_cond:
load r2, [r1]
cmp r2, '\0'
jne .l0_body
pop r1
ret
; r1 string
println:
call puts
mov r1, '\n'
jmp putc
; r1 format string
; r2 args
; ---
; r0 words printed
printf:
@include "../common/memstr.vc6asm"

93
kernel/printf_reference.c Normal file
View File

@ -0,0 +1,93 @@
int my_putc(int ch);
int my_puts(const int *str);
int my_strlen(const int *str);
int put_word_unsigned(int val)
{
int vs[] = { 10000, 1000, 100, 10, 1 };
int written = 0;
int digit = 0;
for (int i = 0; i < 5; ++i) {
while (val > vs[i]) {
val -= vs[i];
digit += 1;
}
my_putc('0' + digit);
written += 1;
}
return written;
}
int put_word_signed(int val)
{
if (val >= 0) {
return put_word_unsigned(val);
}
my_putc('-');
return put_word_unsigned(-val) + 1;
}
int put_word_hex(int val)
{
int vs[] = { 0x1000, 0x100, 0x10, 0x1 };
int written = 0;
int digit = 0;
for (int i = 0; i < 4; ++i) {
while (val > vs[i]) {
val -= vs[i];
digit += 1;
}
my_putc(digit > 9 ? digit + '0' : digit + 'a');
written += 1;
}
return written;
}
int kprintf(const int *fmt, const int args[])
{
int written = 0;
for (; *fmt; ++fmt) {
if (*fmt != '%') {
my_putc(*fmt);
written += 1;
continue;
}
++fmt;
if (!*fmt)
return -1;
if (*fmt == '%') {
my_putc('%');
written += 1;
continue;
}
switch (*fmt) {
case 'c':
my_putc(*args);
written += 1;
case 's':
my_puts((int *)(long)*args);
written += my_strlen((int *)(long)*args);
case 'u':
written += put_word_unsigned(*args);
break;
case 'i':
written += put_word_signed(*args);
break;
case 'x':
written += put_word_hex(*args);
break;
default:
return -1;
}
++args;
}
return written;
}

View File

@ -1213,7 +1213,7 @@ static void a_parse_directive_include(Assembler *a)
static void a_skip_until_directive_end(Assembler *a) static void a_skip_until_directive_end(Assembler *a)
{ {
Tok last = a->tok; Tok last = a->tok;
a_step(a); a_step_without_expand(a);
int start_depth = a->directive_depth; int start_depth = a->directive_depth;
while (a->tok.ty != Tok_Eof) { while (a->tok.ty != Tok_Eof) {
if (last.ty == '@' && (a->tok.ty == Tok_End)) if (last.ty == '@' && (a->tok.ty == Tok_End))
@ -1225,7 +1225,7 @@ static void a_skip_until_directive_end(Assembler *a)
if (a->directive_depth < start_depth) if (a->directive_depth < start_depth)
break; break;
last = a->tok; last = a->tok;
a_step(a); a_step_without_expand(a);
} }
if (a->tok.ty == Tok_Eof) if (a->tok.ty == Tok_Eof)
print_error_exit(a->tok.loc, "expected '@end'"); print_error_exit(a->tok.loc, "expected '@end'");

View File

@ -339,10 +339,17 @@ static void vcd_render_char(
constexpr SDL_Color fg = { 0xff, 0xff, 0xff, 0xff }; constexpr SDL_Color fg = { 0xff, 0xff, 0xff, 0xff };
constexpr SDL_Color bg = { 0x00, 0x00, 0x00, 0xff }; constexpr SDL_Color bg = { 0x00, 0x00, 0x00, 0xff };
uint64_t font_ch = font_char((uint8_t)ch & 0xff);
size_t row_size = pitch / 4; size_t row_size = pitch / 4;
if (!ch) {
for (int ch_y = 0; ch_y < 8; ++ch_y)
for (int ch_x = 0; ch_x < 8; ++ch_x)
tex_buffer[(y * 8 + ch_y) * row_size + x * 8 + ch_x] = bg;
return;
}
uint64_t font_ch = font_char((uint8_t)ch & 0xff);
for (int ch_y = 0; ch_y < 8; ++ch_y) { for (int ch_y = 0; ch_y < 8; ++ch_y) {
uint8_t row = font_ch >> (7 - ch_y) * 8 & 0xff; uint8_t row = font_ch >> (7 - ch_y) * 8 & 0xff;

View File

@ -118,7 +118,27 @@ int main(int argc, char *argv[])
name += 1; name += 1;
INode parent; INode parent;
resolve_path(fs, &parent, path_copy); if (!path_copy[0]) {
fs_root(fs, &parent);
} else {
resolve_path(fs, &parent, path_copy);
}
constexpr size_t entries_cap = 16;
FsListEntry entries[entries_cap];
size_t entries_size = fs_list(fs, entries, entries_cap, &parent);
assert(entries_size < entries_cap);
uint16_t inode_id = search_entries(entries, entries_size, name);
if (inode_id) {
printf("folder %s already exists in %s\n",
name,
*path_copy ? path_copy : "/");
free(path_copy);
close_without_saving(fs, &disk);
return 0;
}
printf("creating directory '%s' in %d\n", name, parent.id); printf("creating directory '%s' in %d\n", name, parent.id);
INode inode; INode inode;