From 28514b4efaa973a6dfdcb74512acdad8a13418bf Mon Sep 17 00:00:00 2001 From: sfja Date: Thu, 12 Nov 2026 17:46:17 +0100 Subject: [PATCH] make disk with kernel --- CMakeLists.txt | 40 +++++++++++++++++++++++----- boot/CMakeLists.txt | 2 +- boot/boot.vc6asm | 20 +++++++++++--- kernel/CMakeLists.txt | 2 +- kernel/kernel.vc6asm | 4 ++- src/CMakeLists.txt | 17 +++++++----- src/asm.c | 2 ++ src/emu.c | 2 +- src/fstool.c | 20 +++++++------- src/mkdisk.c | 62 +++++++++++++++++++++++++++++++++++++++++++ vim/syntax/vc6asm.vim | 1 + 11 files changed, 141 insertions(+), 31 deletions(-) create mode 100644 src/mkdisk.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b382a8..09dc845 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,11 +21,37 @@ add_subdirectory(src) add_subdirectory(boot) add_subdirectory(kernel) -add_custom_target( - run - DEPENDS emu build-boot.bin - COMMAND emu boot/boot.bin - VERBATIM - USES_TERMINAL -) +block() + set(boot_file ${CMAKE_CURRENT_BINARY_DIR}/boot/boot.bin) + set(kernel_file ${CMAKE_CURRENT_BINARY_DIR}/kernel/kernel.bin) + set(fs_file ${CMAKE_CURRENT_BINARY_DIR}/fs.bin) + set(disk_file ${CMAKE_CURRENT_BINARY_DIR}/disk.bin) + + add_custom_command( + OUTPUT ${fs_file} + COMMAND fstool ${fs_file} mkfile /kernel.bin + COMMAND fstool ${fs_file} import ${kernel_file} /kernel.bin + DEPENDS fstool ${kernel_file} + VERBATIM + USES_TERMINAL + ) + + add_custom_command( + OUTPUT ${disk_file} + COMMAND mkdisk ${disk_file} ${boot_file} ${fs_file}:0800 + DEPENDS mkdisk ${boot_file} ${fs_file} + VERBATIM + USES_TERMINAL + ) + + add_custom_target(disk.bin.target ALL DEPENDS ${disk_file}) + + add_custom_target( + run + COMMAND emu ${disk_file} + DEPENDS emu ${disk_file} + VERBATIM + USES_TERMINAL + ) +endblock() diff --git a/boot/CMakeLists.txt b/boot/CMakeLists.txt index 4efd6c1..99d417b 100644 --- a/boot/CMakeLists.txt +++ b/boot/CMakeLists.txt @@ -1,3 +1,3 @@ -add_vc6asm_bin_target(boot.bin boot.vc6asm) +vc6_asm_target(boot.bin boot.vc6asm) diff --git a/boot/boot.vc6asm b/boot/boot.vc6asm index 826369a..de079af 100644 --- a/boot/boot.vc6asm +++ b/boot/boot.vc6asm @@ -13,11 +13,11 @@ start: mov sp, bp mov r1, text - mov r2, VCD_BEGIN + 80 + mov r2, VCD_BEGIN + (1 * 40 + 0) call println mov r1, prompt - mov r2, VCD_BEGIN + 120 + mov r2, VCD_BEGIN + (3 * 40 + 0) mov r0, ip call println @@ -38,6 +38,20 @@ start: and fl, FL_LT cmov ip, r2 + mov r0, 8 + store [DISK_BLOCK_ID], r0 + mov r0, 0x0400 + store [DISK_ADDR], r0 + mov r0, DISK_CTL_START + store [DISK_CTL], r0 + + mov r1, 0x0400 + mov r2, VCD_BEGIN + (2 * 40 + 0) + mov r0, ip + call println + + + mov r0, keydown_handler store [KBD_KEYDOWN_HANDLER], r0 mov r1, 0 @@ -92,7 +106,7 @@ println: ret text: - dw "hello world\0" + dw "boot loader\0" prompt: dw "> \0" diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index a96a409..285ab8d 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -1,3 +1,3 @@ -add_vc6asm_bin_target(kernel.bin kernel.vc6asm) +vc6_asm_target(kernel.bin kernel.vc6asm) diff --git a/kernel/kernel.vc6asm b/kernel/kernel.vc6asm index bf47511..bca8875 100644 --- a/kernel/kernel.vc6asm +++ b/kernel/kernel.vc6asm @@ -1,7 +1,9 @@ @include "../boot/arch.vc6asm" @include "../boot/macros.vc6asm" -@org 0x0100 +@org 0x0800 + + dw "haiiiiiiiiiiiiiiiiiii\0" main: mov r1, 'V' diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 233b25f..750c13c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -48,19 +48,22 @@ target_link_libraries(fscli PRIVATE opts fs) add_executable(fstool fstool.c) target_link_libraries(fstool PRIVATE opts fs) -function(add_vc6asm_bin_target TARGET_NAME SOURCE_FILE) +add_executable(mkdisk mkdisk.c) +target_link_libraries(mkdisk PRIVATE opts) + +function(vc6_asm_target target_name source_file) add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} - COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE} -o ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} -M - DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE_FILE} - DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.d + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target_name} + COMMAND asm ${CMAKE_CURRENT_SOURCE_DIR}/${source_file} -o ${CMAKE_CURRENT_BINARY_DIR}/${target_name} -M + DEPENDS asm ${CMAKE_CURRENT_SOURCE_DIR}/${source_file} + DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${target_name}.d VERBATIM ) add_custom_target( - ${TARGET_NAME}.target + ${target_name}.target ALL - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${target_name} ) endfunction() diff --git a/src/asm.c b/src/asm.c index c414377..5caf275 100644 --- a/src/asm.c +++ b/src/asm.c @@ -672,6 +672,7 @@ void assembler_init(Assembler *a, Lexer *lexer) .dep_str_map = { }, .dep_file = nullptr, .directive_depth = 0, + .org_offset = 0, .second_pass = false, .print = false, }; @@ -695,6 +696,7 @@ void assembler_rewind(Assembler *a) a->labels_counter = 0; a->current_label = 0; a->expan_ids = 0; + a->org_offset = 0; a->second_pass = true; map_clear(&a->guard_set); diff --git a/src/emu.c b/src/emu.c index 1183162..5652bfa 100644 --- a/src/emu.c +++ b/src/emu.c @@ -406,7 +406,7 @@ void filedisk_read_block(Disk *disk, uint16_t *data, uint16_t block_id) { FileDisk *d = (FileDisk *)disk; fseek(d->file, sizeof(uint16_t) * disk->block_size * block_id, SEEK_SET); - fread(data, d->disk.block_size, sizeof(uint16_t), d->file); + fread(data, sizeof(uint16_t), d->disk.block_size, d->file); } void filedisk_write_block(Disk *disk, const uint16_t *data, uint16_t block_id) diff --git a/src/fstool.c b/src/fstool.c index 95d299c..bfaa6ca 100644 --- a/src/fstool.c +++ b/src/fstool.c @@ -40,7 +40,7 @@ static void save(Fs *fs, Disk *disk, const char *filename) size_t words_written = fwrite( disk->blocks, sizeof(uint16_t), block_size * amount_of_blocks, file); fclose(file); - printf("wrote %zu words to %s\n", words_written, filename); + printf("%zu words written\n", words_written); fs_destroy(fs); free(disk->blocks); @@ -120,7 +120,7 @@ int main(int argc, char *argv[]) INode parent; resolve_path(fs, &parent, path_copy); - printf("creating directory '%s' in %d.\n", name, parent.id); + printf("creating directory '%s' in %d\n", name, parent.id); INode inode; fs_make_dir(fs, &inode, &parent, name); @@ -139,9 +139,13 @@ int main(int argc, char *argv[]) name += 1; INode parent; - resolve_path(fs, &parent, path_copy); + if (!path_copy[0]) { + fs_root(fs, &parent); + } else { + resolve_path(fs, &parent, path_copy); + } - printf("creating file %s in %s.\n", name, path_copy); + printf("creating file %s in %s\n", name, *path_copy ? path_copy : "/"); INode inode; fs_make_file(fs, &inode, &parent, name); @@ -190,16 +194,12 @@ int main(int argc, char *argv[]) size_t buffer_size = (file_size + 1) / 2; uint16_t *buffer = calloc(buffer_size, sizeof(uint16_t)); size_t words_read = fread(buffer, sizeof(uint16_t), buffer_size, file); - printf("file_size = %zu, words_read = %zu, buffer_size = %zu\n", - file_size, - words_read, - buffer_size); assert(words_read == buffer_size); fclose(file); fs_write(fs, &inode, buffer, buffer_size); - printf("wrote %zu words to file %s.\n", words_read, vpath); + printf("imported %zu words to file %s\n", words_read, vpath); free(buffer); save(fs, &disk, disk_filename); } else if (strcmp(cmd, "importb") == 0) { @@ -235,7 +235,7 @@ int main(int argc, char *argv[]) fs_write(fs, &inode, buffer, buffer_size); - printf("wrote %zu words to file %s.\n", words_read, vpath); + printf("imported %zu words to file %s\n", words_read, vpath); free(buffer); save(fs, &disk, disk_filename); } else { diff --git a/src/mkdisk.c b/src/mkdisk.c new file mode 100644 index 0000000..5d3a20d --- /dev/null +++ b/src/mkdisk.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + assert(argc >= 2); + + const char *path = argv[1]; + + FILE *file = fopen(path, "w"); + assert(file); + + size_t words = 0; + + for (int i = 2; i < argc; ++i) { + char *input_filename = strdup(argv[i]); + + char *sep = strchr(input_filename, ':'); + + if (sep) + *sep = '\0'; + + FILE *input_file = fopen(input_filename, "r"); + assert(input_file); + + if (sep) { + size_t offset = strtoull(sep + 1, nullptr, 16); + + assert(words <= offset); + printf("padding %zu words\n", offset - words); + + while (words < offset) { + fwrite((uint16_t[]) { 0 }, sizeof(uint16_t), 1, file); + words += 1; + } + } + + printf("appending %s at 0x%04lx\n", input_filename, words); + size_t bytes_written = 0; + int ch; + while ((ch = fgetc(input_file)) != EOF) { + fputc(ch, file); + bytes_written += 1; + } + + if (bytes_written % 1) { + fputc('\0', file); + bytes_written += 1; + } + + words += bytes_written / 2; + + free(input_filename); + fclose(input_file); + }; + + fclose(file); +} diff --git a/vim/syntax/vc6asm.vim b/vim/syntax/vc6asm.vim index 6e62104..6b54dae 100644 --- a/vim/syntax/vc6asm.vim +++ b/vim/syntax/vc6asm.vim @@ -9,6 +9,7 @@ endif syn keyword vcKeyword nop hlt mov load store cmp cmov syn keyword vcKeyword or xor and not shr shl rshr rshl add sub +syn keyword vcKeyword dw syn keyword vcOperator r0 r1 r2 r3 sp bp fl ip syn match vcOperator '|'