diff --git a/Makefile b/Makefile index 2e10099..f0a50aa 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,23 @@ CC = sdcc ASM = sdasz80 PLATFORM = -mz80 -EMULATOR = /Applications/openMSX.app/Contents/MacOS/openmsx -machine Philips_NMS_8255 -ext msxdos2 -diska emulation/msx-dos2/ -script emulation/boot.tcl -diskb bin/ +EMULATOR = openmsx -machine Philips_NMS_8255 -ext msxdos2 -diska emulation/msx-dos2/ -diskb bin/ -script emulation/boot.tcl #EMULATOR = /Applications/openMSX.app/Contents/MacOS/openmsx -machine C-BIOS_MSX2 -carta HEXBIN = hex2bin -STARTUPDIR = startups -INCLUDEDIR = includes -LIBDIR = libs +STARTUPDIR = crt0msx +INCLUDEDIR = include +LIBDIR = lib SRCDIR = src # See startup files for the correct ADDR_CODE and ADDR_DATA CRT0 = crt0msx_msxdos_advanced.s -ADDR_CODE = 0x0107 +ADDR_CODE = 0x0192 ADDR_DATA = 0 VERBOSE = -V CCFLAGS = $(VERBOSE) $(PLATFORM) --code-loc $(ADDR_CODE) --data-loc $(ADDR_DATA) \ - --no-std-crt0 --opt-code-size --out-fmt-ihx + --no-std-crt0 --opt-code-size --out-fmt-ihx --std-c99 OBJECTS = $(CRT0) putchar.s getchar.s dos.s conio.c SOURCES = main.c OUTFILE = main.com @@ -48,6 +48,7 @@ $(SOURCES): build: main.ihx @echo "Building $(OUTFILE)..." @$(HEXBIN) main.ihx + mkdir -p bin @mv main.bin bin/${OUTFILE} @echo "Done." @@ -56,7 +57,7 @@ clean: rm -f *.asm *.bin *.cdb *.ihx *.lk *.lst *.map *.mem *.omf *.rst *.rel *.sym *.noi *.hex *.lnk *.dep rm -f bin/$(OUTFILE) -emulator: $(OUTFILE) +emulator: $(EMULATOR) & #For ROM use: #$(EMULATOR) $(OUTFILE) & diff --git a/README.md b/README.md index c173294..29b2324 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# Skeleton C project for MSX -Will create a "hello world" program running on MSX Z80 platform and emulated hardware. Following packages are required: +# Skeleton C project for MSX (SDCC) +Will create a simple program running on MSX Z80 platform and emulated hardware. Following packages are required: * make * [hex2bin](http://sourceforge.net/projects/hex2bin/) (1) @@ -25,6 +25,8 @@ Check the makefile to select the correct startup file (ROM/COM). Current support - MSX-DOS COM file (simple main) - MSX-DOS COM file (main with arguments) +__The "main with arguments" runtime header has been fixed to support stardard C argument ordering. The application name is still not passed in to the application in the first argument. This will have to be fixed. Stay tuned.__ + ## Thanks - [Avelino Herrera Morales](http://msx.atlantes.org/index_en.html) for providing the basic files needed to create a MSX-DOS executable. - [Laurens Holst](http://map.grauw.nl/) for helping with hex2bin. diff --git a/startups/crt0msx.16k.4000.s b/crt0msx/crt0msx.16k.4000.s similarity index 100% rename from startups/crt0msx.16k.4000.s rename to crt0msx/crt0msx.16k.4000.s diff --git a/startups/crt0msx.16k.8000.s b/crt0msx/crt0msx.16k.8000.s similarity index 100% rename from startups/crt0msx.16k.8000.s rename to crt0msx/crt0msx.16k.8000.s diff --git a/startups/crt0msx.32k.4000.s b/crt0msx/crt0msx.32k.4000.s similarity index 100% rename from startups/crt0msx.32k.4000.s rename to crt0msx/crt0msx.32k.4000.s diff --git a/startups/crt0msx_msxdos.s b/crt0msx/crt0msx_msxdos.s similarity index 100% rename from startups/crt0msx_msxdos.s rename to crt0msx/crt0msx_msxdos.s diff --git a/startups/crt0msx_msxdos_advanced.s b/crt0msx/crt0msx_msxdos_advanced.s similarity index 71% rename from startups/crt0msx_msxdos_advanced.s rename to crt0msx/crt0msx_msxdos_advanced.s index 4e856cf..d964364 100644 --- a/startups/crt0msx_msxdos_advanced.s +++ b/crt0msx/crt0msx_msxdos_advanced.s @@ -1,17 +1,18 @@ - ;--- crt0.s for MSX-DOS - by Konami Man, 11/2004 - ; Advanced version: allows "int main(char** argv, int argc)", - ; the returned value will be passed to _TERM on DOS 2, - ; argv is always 0x100 (the startup code memory is recycled). - ; Overhead: 112 bytes. - ; - ; Compile programs with --code-loc 0x170 --data-loc X + ;--- crt0.s for MSX-DOS - by Konami Man, 11/2004 + ; Modified by algodesigner, 09/2021 + ; + ; Advanced version: allows "int main(int argc, char** argv)", + ; the returned value will be passed to _TERM on DOS 2, + ; argv is always 0x100 (the startup code memory is recycled). + ; + ; Compile programs with --code-loc 0x192 --data-loc X ; X=0 -> global vars will be placed immediately after code ; X!=0 -> global vars will be placed at address X ; (make sure that X>0x100+code size) - .globl _main + .globl _main - .area _HEADER (ABS) + .area _HEADER (ABS) .org 0x0100 ;MSX-DOS .COM programs start address @@ -23,12 +24,23 @@ init: call gsinit ; and terminate each parameter with 0. ; MSX-DOS places the command line length at 0x80 (one byte), ; and the command line itself at 0x81 (up to 127 characters). + + ; Retrieve the full program path, our first parameter + + ld hl,#0x100 + ld de,#_HEAP_start + ld (hl),e + inc hl + ld (hl),d + ld hl,#envvar + ld bc,#0xff6b + call #5 - ;* Check if there are any parameters at all + ;* Check if there are any command line parameters ld a,(#0x80) or a - ld c,#0 + ld c,#1 ; c <- number of parameters jr z,cont ;* Terminate command line with 0 @@ -51,22 +63,25 @@ init: call gsinit ld de,#0xC000 ld bc,#parloopend-#parloop ldir - + ;* Initialize registers and jump to the loop routine ld hl,#0x81 ;Command line pointer - ld c,#0 ;Number of params found - ld ix,#0x100 ;Params table pointer - + ld c,#1 ;Number of params found + ld ix,#0x102 ;Params table pointer +2 as the first entry + ;is taken by the program name ld de,#cont ;To continue execution at "cont" push de ;when the routine RETs jp 0xC000 +envvar: .asciz "PROGRAM" + ;>>> Command line processing routine begin ;* Loop over the command line: skip spaces -parloop: ld a,(hl) +parloop: + ld a,(hl) or a ;Command line end found? ret z @@ -111,16 +126,16 @@ parloopend: cont: ld hl,#0x100 ld b,#0 - push bc ;Pass info as parameters to "main" push hl + push bc ;Pass info as parameters to "main" ;--- Step 3: Call the "main" function - push de - ld de,#_HEAP_start - ld (_heap_top),de - pop de + push de + ld de,#_HEAP_start + 256 ; 256 bytes reserved for the program name + ld (_heap_top),de + pop de - call _main + call _main ;--- Step 4: Program termination. ; Termination code for DOS 2 was returned on L. @@ -134,23 +149,23 @@ cont: ld hl,#0x100 ;--- Program code and data (global vars) start here - ;* Place data after program code, and data init code after data + ;* Place data after program code, and data init code after data - .area _CODE - .area _DATA + .area _CODE + .area _DATA _heap_top:: - .dw 0 + .dw 0 gsinit: .area _GSINIT .area _GSFINAL ret - ;* These doesn't seem to be necessary... (?) + ;* These doesn't seem to be necessary... (?) ;.area _OVERLAY - ;.area _HOME + ;.area _HOME ;.area _BSS - .area _HEAP + .area _HEAP _HEAP_start:: diff --git a/includes/conio.h b/include/conio.h similarity index 100% rename from includes/conio.h rename to include/conio.h diff --git a/includes/dos.h b/include/dos.h similarity index 100% rename from includes/dos.h rename to include/dos.h diff --git a/includes/dos2.h b/include/dos2.h similarity index 100% rename from includes/dos2.h rename to include/dos2.h diff --git a/includes/heap.h b/include/heap.h similarity index 100% rename from includes/heap.h rename to include/heap.h diff --git a/includes/interrupt.h b/include/interrupt.h similarity index 100% rename from includes/interrupt.h rename to include/interrupt.h diff --git a/includes/interrupt_ROM.h b/include/interrupt_ROM.h similarity index 100% rename from includes/interrupt_ROM.h rename to include/interrupt_ROM.h diff --git a/includes/ioport.h b/include/ioport.h similarity index 100% rename from includes/ioport.h rename to include/ioport.h diff --git a/includes/keyboard.h b/include/keyboard.h similarity index 100% rename from includes/keyboard.h rename to include/keyboard.h diff --git a/includes/mem.h b/include/mem.h similarity index 100% rename from includes/mem.h rename to include/mem.h diff --git a/includes/types.h b/include/types.h similarity index 100% rename from includes/types.h rename to include/types.h diff --git a/includes/vdp.h b/include/vdp.h similarity index 100% rename from includes/vdp.h rename to include/vdp.h diff --git a/libs/conio.c b/lib/conio.c similarity index 91% rename from libs/conio.c rename to lib/conio.c index 3119717..f90c1d6 100644 --- a/libs/conio.c +++ b/lib/conio.c @@ -1,5 +1,5 @@ -#include "../includes/types.h" -#include "../includes/conio.h" +#include "../include/types.h" +#include "../include/conio.h" void puts(char *s) { diff --git a/libs/dos.s b/lib/dos.s similarity index 100% rename from libs/dos.s rename to lib/dos.s diff --git a/libs/dos2.s b/lib/dos2.s similarity index 100% rename from libs/dos2.s rename to lib/dos2.s diff --git a/libs/getchar.s b/lib/getchar.s similarity index 100% rename from libs/getchar.s rename to lib/getchar.s diff --git a/libs/heap.c b/lib/heap.c similarity index 62% rename from libs/heap.c rename to lib/heap.c index 38421c2..b412326 100644 --- a/libs/heap.c +++ b/lib/heap.c @@ -1,7 +1,7 @@ -#include "../includes/types.h" -#include "../includes/heap.h" +#include "../include/types.h" +#include "../include/heap.h" void *malloc(uint16_t size) { diff --git a/libs/interrupt.s b/lib/interrupt.s similarity index 100% rename from libs/interrupt.s rename to lib/interrupt.s diff --git a/libs/interrupt_ROM.s b/lib/interrupt_ROM.s similarity index 100% rename from libs/interrupt_ROM.s rename to lib/interrupt_ROM.s diff --git a/libs/ioport.s b/lib/ioport.s similarity index 100% rename from libs/ioport.s rename to lib/ioport.s diff --git a/libs/keyboard.s b/lib/keyboard.s similarity index 100% rename from libs/keyboard.s rename to lib/keyboard.s diff --git a/libs/mem.c b/lib/mem.c similarity index 78% rename from libs/mem.c rename to lib/mem.c index 5a9143b..df6de95 100644 --- a/libs/mem.c +++ b/lib/mem.c @@ -1,7 +1,7 @@ -#include "../includes/types.h" -#include "../includes/mem.h" +#include "../include/types.h" +#include "../include/mem.h" void memcpy(uint8_t *dest, uint8_t *src, uint16_t n) { diff --git a/libs/putchar.s b/lib/putchar.s similarity index 100% rename from libs/putchar.s rename to lib/putchar.s diff --git a/libs/vdp.s b/lib/vdp.s similarity index 100% rename from libs/vdp.s rename to lib/vdp.s diff --git a/src/main.c b/src/main.c index 1578b13..f18a13f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,18 @@ #include "conio.h" #include "dos.h" -void main(void) { - puts("Hello, world :-)\r\n"); +void main(int argc, char *args[]) +{ + puts("argc="); + putdec8(argc); + puts("\r\n"); + for (int i = 0; i < argc; i++) { + puts("argument["); + putdec8(i); + puts("]="); + puts(args[i]); + puts("\r\n"); + } + exit(0); }