From cdd0b64570a2f125e76d7ea11929e74b9b8368c2 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 15 Nov 2019 10:33:13 -0500 Subject: [PATCH] Modify userspace .org and RAMSTART expectations Instead of expecting a `USER_CODE` symbol to be set, we expect `.org` to be set in all userspace glue code. This gives us more flexibility with regards to how we manage that. Moreover, instead of making `USER_RAMSTART` mandatory, we make it default to the end of the binary, which is adequate in a majority of cases. Will be useful for my upcoming mega-commit... :) --- apps/README.md | 17 +++++++++++++++-- apps/at28w/glue.asm | 2 +- apps/basic/glue.asm | 2 +- apps/ed/glue.asm | 3 +-- apps/memt/glue.asm | 3 +-- apps/sdct/glue.asm | 2 +- apps/zasm/glue.asm | 3 +-- recipes/rc2014/zasm/user.h | 5 ++--- recipes/sms/romasm/Makefile | 8 ++++---- recipes/sms/romasm/{user-tmpl.h => user.h} | 5 +---- tools/emul/cfsin/hello.asm | 2 -- tools/emul/shell/user.h | 3 +-- tools/emul/zasm/user.h | 2 +- 13 files changed, 30 insertions(+), 27 deletions(-) rename recipes/sms/romasm/{user-tmpl.h => user.h} (82%) diff --git a/apps/README.md b/apps/README.md index f40fe27..5f62c10 100644 --- a/apps/README.md +++ b/apps/README.md @@ -10,8 +10,16 @@ you will typically not want to do that. ## Userspace convention -We execute a userspace application by calling the address it's loaded into. This -means: a userspace application is expected to return. +We execute a userspace application by calling the address it's loaded into. + +This means that userspace applications must be assembled with a proper `.org`, +otherwise labels in its code will be wrong. + +The `.org`, it is not specified by glue code of the apps themselves. It is +expected to be set either in the `user.h` file to through `zasm` 3rd argument. + +That a userspace is called also means that an application, when finished +running, is expected to return with a regular `ret` and a clean stack. Whatever calls the userspace app (usually, it will be the shell), should set HL to a pointer to unparsed arguments in string form, null terminated. @@ -25,3 +33,8 @@ because otherwise, it will break the kernel. Apps in Collapse OS are design to be ROM-compatible, that is, they don't write to addresses that are part of the code's address space. + +By default, apps set their RAM to begin at the end of the binary because in +most cases, these apps will be ran from RAM. If they're ran from ROM, make sure +to set `USER_RAMSTART` properly in your `user.h` to ensure that the RAM is +placed properly. diff --git a/apps/at28w/glue.asm b/apps/at28w/glue.asm index 33ebf04..c9f3f93 100644 --- a/apps/at28w/glue.asm +++ b/apps/at28w/glue.asm @@ -16,9 +16,9 @@ .inc "user.h" .inc "err.h" -.org USER_CODE .equ AT28W_RAMSTART USER_RAMSTART jp at28wMain .inc "at28w/main.asm" +USER_RAMSTART: diff --git a/apps/basic/glue.asm b/apps/basic/glue.asm index 219353e..c418393 100644 --- a/apps/basic/glue.asm +++ b/apps/basic/glue.asm @@ -7,7 +7,6 @@ .inc "user.h" .inc "err.h" -.org USER_CODE jp basStart @@ -16,3 +15,4 @@ .inc "lib/parse.asm" .equ BAS_RAMSTART USER_RAMSTART .inc "basic/main.asm" +USER_RAMSTART: diff --git a/apps/ed/glue.asm b/apps/ed/glue.asm index af0b9b8..847802d 100644 --- a/apps/ed/glue.asm +++ b/apps/ed/glue.asm @@ -26,8 +26,6 @@ ; ****** .inc "err.h" -.org USER_CODE - jp edMain .inc "core.asm" @@ -41,3 +39,4 @@ .inc "ed/cmd.asm" .equ ED_RAMSTART CMD_RAMEND .inc "ed/main.asm" +USER_RAMSTART: diff --git a/apps/memt/glue.asm b/apps/memt/glue.asm index 38a5495..2a3ef0d 100644 --- a/apps/memt/glue.asm +++ b/apps/memt/glue.asm @@ -13,8 +13,7 @@ ; *** Includes *** .inc "user.h" -.org USER_CODE - jp memtMain .inc "memt/main.asm" +USER_RAMSTART: diff --git a/apps/sdct/glue.asm b/apps/sdct/glue.asm index b82c9f9..0a28419 100644 --- a/apps/sdct/glue.asm +++ b/apps/sdct/glue.asm @@ -19,9 +19,9 @@ ; *** Includes *** .inc "user.h" -.org USER_CODE .equ SDCT_RAMSTART USER_RAMSTART jp sdctMain .inc "sdct/main.asm" +USER_RAMSTART: diff --git a/apps/zasm/glue.asm b/apps/zasm/glue.asm index addda95..e067ea8 100644 --- a/apps/zasm/glue.asm +++ b/apps/zasm/glue.asm @@ -64,8 +64,6 @@ .inc "err.h" .inc "ascii.h" -.org USER_CODE - jp zasmMain .inc "core.asm" @@ -87,3 +85,4 @@ jp zasmMain .inc "zasm/symbol.asm" .equ ZASM_RAMSTART SYM_RAMEND .inc "zasm/main.asm" +USER_RAMSTART: diff --git a/recipes/rc2014/zasm/user.h b/recipes/rc2014/zasm/user.h index 4b9256a..da80eed 100644 --- a/recipes/rc2014/zasm/user.h +++ b/recipes/rc2014/zasm/user.h @@ -1,6 +1,5 @@ -.equ USER_CODE 0x8700 -.equ USER_RAMSTART USER_CODE+0x1900 -.equ FS_HANDLE_SIZE 6 +.org 0x8700 +.equ FS_HANDLE_SIZE 8 .equ BLOCKDEV_SIZE 8 ; *** JUMP TABLE *** diff --git a/recipes/sms/romasm/Makefile b/recipes/sms/romasm/Makefile index 131d8f7..b1e46df 100644 --- a/recipes/sms/romasm/Makefile +++ b/recipes/sms/romasm/Makefile @@ -5,13 +5,13 @@ APPS = ../../../apps .PHONY: all clean all: os.sms +# -o value synced with offset in glue.asm ed.bin: $(APPS)/ed/glue.asm - echo ".equ USER_CODE ED_CODE" | cat user-tmpl.h - > user.h - $(ZASM) $(KERNEL) $(APPS) user.h < $< > $@ + $(ZASM) -o 19 $(KERNEL) $(APPS) user.h < $< > $@ +# -o value synced with offset in glue.asm zasm.bin: $(APPS)/zasm/glue.asm - echo ".equ USER_CODE ZASM_CODE" | cat user-tmpl.h - > user.h - $(ZASM) $(KERNEL) $(APPS) user.h < $< > $@ + $(ZASM) -o 1d $(KERNEL) $(APPS) user.h < $< > $@ os.sms: glue.asm ed.bin zasm.bin $(ZASM) $(KERNEL) ed.bin zasm.bin < $< > $@ diff --git a/recipes/sms/romasm/user-tmpl.h b/recipes/sms/romasm/user.h similarity index 82% rename from recipes/sms/romasm/user-tmpl.h rename to recipes/sms/romasm/user.h index 87bf15a..83d4a46 100644 --- a/recipes/sms/romasm/user-tmpl.h +++ b/recipes/sms/romasm/user.h @@ -1,8 +1,5 @@ -; USER_CODE is filled in on-the-fly with either ED_CODE or ZASM_CODE -.equ ED_CODE 0x1900 -.equ ZASM_CODE 0x1d00 .equ USER_RAMSTART 0xc200 -.equ FS_HANDLE_SIZE 6 +.equ FS_HANDLE_SIZE 8 .equ BLOCKDEV_SIZE 8 ; Make ed fit in SMS's memory .equ ED_BUF_MAXLINES 0x100 diff --git a/tools/emul/cfsin/hello.asm b/tools/emul/cfsin/hello.asm index 4c13840..e9a99d0 100644 --- a/tools/emul/cfsin/hello.asm +++ b/tools/emul/cfsin/hello.asm @@ -1,6 +1,4 @@ .inc "user.h" -.org USER_CODE - ld hl, sAwesome call printstr xor a ; success diff --git a/tools/emul/shell/user.h b/tools/emul/shell/user.h index 4a6296e..4bba572 100644 --- a/tools/emul/shell/user.h +++ b/tools/emul/shell/user.h @@ -1,5 +1,4 @@ -.equ USER_CODE 0x4200 -.equ USER_RAMSTART USER_CODE+0x1800 +.org 0x4200 ; in sync with USERCODE in shell/shell_.asm .equ FS_HANDLE_SIZE 8 .equ BLOCKDEV_SIZE 8 diff --git a/tools/emul/zasm/user.h b/tools/emul/zasm/user.h index 7a881be..318ba72 100644 --- a/tools/emul/zasm/user.h +++ b/tools/emul/zasm/user.h @@ -1,4 +1,4 @@ -.equ USER_CODE 0x4800 +.org 0x4800 ; in sync with USER_CODE in glue.asm .equ USER_RAMSTART 0x6000 .equ FS_HANDLE_SIZE 8 .equ BLOCKDEV_SIZE 8