From 5c3580e9b58f28a4ede1b26093daeb1d97b1ce2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sun, 19 Mar 2023 19:28:30 +0200 Subject: [PATCH] Get rid of syscall thunks --- .gitignore | 1 + Makefile | 9 ++++++--- add_syscalls.py | 47 +++++++++++++++++++++++++++++++++++++++++++ ponydos.asm | 29 +++++++++++--------------- ponydos.inc | 9 --------- ponydos_nosyscall.inc | 5 +++++ process_wallpaper.py | 6 +++--- 7 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 add_syscalls.py delete mode 100644 ponydos.inc create mode 100644 ponydos_nosyscall.inc diff --git a/.gitignore b/.gitignore index 7cf5001..1881db6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.bin *.img +ponydos.inc diff --git a/Makefile b/Makefile index d431d3b..245bed2 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,10 @@ FS_FILES = wallpaper.bin shell.bin ponydos.img: ponydos.bin $(FS_FILES) $(PYTHON) assemble_floppy.py $@ ponydos.bin $(FS_FILES) -ponydos.bin: ponydos.inc +ponydos.inc: ponydos.asm ponydos_nosyscall.inc + $(NASM) -fbin -d SYMBOLS -o /dev/null ponydos.asm | $(PYTHON) add_syscalls.py $@ ponydos_nosyscall.inc + +ponydos.bin: ponydos_nosyscall.inc shell.bin: ponydos.inc @@ -19,13 +22,13 @@ shell.bin: ponydos.inc $(NASM) -fbin -o $@ $< .ansi.bin: - $(PYTHON) process_wallpaper.py $< $@ 7 0 0 0 + $(PYTHON) process_wallpaper.py $@ $< 7 0 0 0 run: ponydos.img qemu-system-i386 -fda $< clean: - rm -f *.bin *.img + rm -f *.bin *.img ponydos.inc distclean: clean diff --git a/add_syscalls.py b/add_syscalls.py new file mode 100644 index 0000000..3ae4028 --- /dev/null +++ b/add_syscalls.py @@ -0,0 +1,47 @@ +import sys + +syscalls = { + 'open_file': None, + 'read_sectors': None, + 'draw_rect': None, +} + +if len(sys.argv) != 3: + print(f'Usage: {sys.argv[0]} outfile infile', file=sys.stderr) + sys.exit(1) + +outfile = sys.argv[1] +infile = sys.argv[2] + +with open(infile, 'r') as f: + header = f.read() +header = f'; This is from {infile}\n' + header + +mapfile = [] +while True: + try: + line = input() + except EOFError: + break + mapfile.append(line) + +section = None +for line in mapfile: + line = line.split() + if len(line) == 4 and all(c == '-' for c in line[0]) and line[1] == 'Section' and all(c == '-' for c in line[3]): + section = line[2] + if section == '.text' and len(line) == 3: + address, _, name = line + if name in syscalls: + syscalls[name] = int(address, 16) + +header += f'\n;This was automatically generated\n' + +for syscall, address in syscalls.items(): + if address is None: + print(f'{sys.argv[0]}: Error: syscall {syscall} not found', file=sys.stderr) + sys.exit(1) + header += f'SYS_{syscall.upper()} equ 0x{address:x}\n' + +with open(outfile, 'w') as f: + f.write(header) diff --git a/ponydos.asm b/ponydos.asm index 5e418e3..efd1a9f 100644 --- a/ponydos.asm +++ b/ponydos.asm @@ -1,4 +1,7 @@ -%include "ponydos.inc" +%ifdef SYMBOLS +[map symbols] +%endif +%include "ponydos_nosyscall.inc" cpu 286 bits 16 @@ -14,17 +17,6 @@ FILE_MAX_SIZE equ 128 jmp 0:start -; Thunks for application programs to use -; 0x7c05 SYS_OPEN_FILE -call open_file -retf -; 0x7c09 SYS_READ_SECTORS -call read_sectors -retf -; 0x7c0d SYS_DRAW_RECT -call draw_rect -retf - start: cld @@ -81,9 +73,9 @@ load_shell: push word 0x1000 pop es mov si, shell_name - call open_file + call 0:open_file xor bx, bx - call read_sectors + call 0:read_sectors ; TODO: error management? Surely this works... xor ax, ax ; WM_INITIALIZE @@ -134,6 +126,7 @@ mainloop: ; ds:si = beginning of source data ; di = X ; bp = Y +; [Far calls only] draw_rect: pusha push es @@ -173,7 +166,7 @@ draw_rect: pop es popa - ret + retf ; requires: ; ds = 0 @@ -224,6 +217,7 @@ flip_mouse_cursor: ;; bl = drive number ; cx = number of sectors to read (must be at least 1) ; es:bx = output buffer +; [Far calls only] read_sectors: pusha @@ -235,7 +229,7 @@ read_sectors: loop .loop popa - ret + retf ; in: ; ax = LBA of first sector @@ -291,6 +285,7 @@ shell_name db 'shell.bin', 0 ; out: ; ax = LBA of first sector, 0 if no space left ; cx = length in sectors +; [Far calls only] open_file: push si push di @@ -343,7 +338,7 @@ open_file: pop bx pop di pop si - ret + retf .create_file: ; TODO: zero out the sector for this file? diff --git a/ponydos.inc b/ponydos.inc deleted file mode 100644 index 86b71fd..0000000 --- a/ponydos.inc +++ /dev/null @@ -1,9 +0,0 @@ -PONYDOS_SEG equ 0 - -SYS_OPEN_FILE equ 0x7c05 -SYS_READ_SECTORS equ 0x7c09 -SYS_DRAW_RECT equ 0x7c0d - -GLOBAL_WALLPAPER equ 0x500 - -WM_INITIALIZE equ 0 diff --git a/ponydos_nosyscall.inc b/ponydos_nosyscall.inc new file mode 100644 index 0000000..487b2e0 --- /dev/null +++ b/ponydos_nosyscall.inc @@ -0,0 +1,5 @@ +PONYDOS_SEG equ 0 + +GLOBAL_WALLPAPER equ 0x500 + +WM_INITIALIZE equ 0 diff --git a/process_wallpaper.py b/process_wallpaper.py index 45f11cf..78a9ed3 100644 --- a/process_wallpaper.py +++ b/process_wallpaper.py @@ -7,11 +7,11 @@ HEIGHT = 25 color_map = [0, 4, 2, 6, 1, 5, 3, 7] if len(sys.argv) != 7: - print("Usage: {sys.argv[0]} infile outfile default_fgcolor default_bgcolor origin_x origin_y", file=sys.stderr) + print("Usage: {sys.argv[0]} outfile infile default_fgcolor default_bgcolor origin_x origin_y", file=sys.stderr) sys.exit(1) -infile = sys.argv[1] -outfile = sys.argv[2] +outfile = sys.argv[1] +infile = sys.argv[2] default_fgcolor = int(sys.argv[3]) assert 0 <= default_fgcolor <= 15