git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit ab9779f3f5f934f53b340689c447711a99a0a16b
parent f16b08fd4956503df6198aa1ab1d3f77340e0ce0
Author: Toxa <untoxa@mail.ru>
Date:   Thu, 19 Aug 2021 22:12:43 +0300

SMS/GG: basic stdio.h support

Diffstat:
Mgbdk-lib/examples/ap/bcd/bcd.c5++---
Mgbdk-lib/examples/ap/fonts/fonts.c9++++++---
Mgbdk-lib/examples/ap/irq/irq.c2+-
Mgbdk-lib/examples/ap/sgb_pong/sgb_pong.c4++--
Mgbdk-lib/examples/ap/sound/sound.c2+-
Dgbdk-lib/examples/broken/fprpn/Makefile20--------------------
Dgbdk-lib/examples/broken/fprpn/fprpn.c106-------------------------------------------------------------------------------
Mgbdk-lib/examples/gb/bcd/bcd.c5++---
Mgbdk-lib/examples/gb/fonts/fonts.c7+++++--
Mgbdk-lib/examples/gb/irq/irq.c3++-
Mgbdk-lib/examples/gb/sgb_pong/sgb_pong.c4++--
Mgbdk-lib/examples/gb/simple_physics/phys.c2+-
Mgbdk-lib/examples/gb/sound/sound.c2+-
Agbdk-lib/examples/sms/fonts/Makefile30++++++++++++++++++++++++++++++
Agbdk-lib/examples/sms/fonts/fonts.c63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dgbdk-lib/include/gb/console.h44--------------------------------------------
Dgbdk-lib/include/gb/font.h75---------------------------------------------------------------------------
Agbdk-lib/include/gbdk/console.h45+++++++++++++++++++++++++++++++++++++++++++++
Agbdk-lib/include/gbdk/font.h77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgbdk-lib/include/sms/sms.h32++++++++++++++++++++++++++++++++
Mgbdk-lib/include/stdio.h10+++++-----
Mgbdk-lib/include/stdlib.h8++++----
Mgbdk-lib/libc/asm/gbz80/itoa.s2+-
Mgbdk-lib/libc/asm/z80/__itoa.s8++++----
Mgbdk-lib/libc/asm/z80/__ltoa.s8++++----
Mgbdk-lib/libc/sprintf.c8++++----
Mgbdk-lib/libc/targets/gbz80/ap/Makefile2+-
Mgbdk-lib/libc/targets/gbz80/font.s2+-
Agbdk-lib/libc/targets/gbz80/font_color.s12++++++++++++
Mgbdk-lib/libc/targets/gbz80/gb/Makefile2+-
Mgbdk-lib/libc/targets/z80/crt0.s7+++++--
Agbdk-lib/libc/targets/z80/font.s325+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgbdk-lib/libc/targets/z80/gg/Makefile3+++
Mgbdk-lib/libc/targets/z80/gg/global.s13++++++++++++-
Agbdk-lib/libc/targets/z80/memset_small.s14++++++++++++++
Mgbdk-lib/libc/targets/z80/metasprites_hide.s2+-
Agbdk-lib/libc/targets/z80/mode.s23+++++++++++++++++++++++
Mgbdk-lib/libc/targets/z80/sms/Makefile3+++
Mgbdk-lib/libc/targets/z80/sms/global.s13++++++++++++-
39 files changed, 707 insertions(+), 295 deletions(-)

diff --git a/gbdk-lib/examples/ap/bcd/bcd.c b/gbdk-lib/examples/ap/bcd/bcd.c @@ -1,9 +1,8 @@ -#include <gb/gb.h> #include <stdint.h> - #include <stdio.h> -#include <gb/font.h> +#include <gbdk/platform.h> +#include <gbdk/font.h> #include <gbdk/bcd.h> BCD bcd = MAKE_BCD(10203040); diff --git a/gbdk-lib/examples/ap/fonts/fonts.c b/gbdk-lib/examples/ap/fonts/fonts.c @@ -5,8 +5,11 @@ */ #include <stdio.h> -#include <gb/font.h> -#include <gb/console.h> + +#include <gbdk/platform.h> +#include <gbdk/font.h> +#include <gbdk/console.h> + #include <gb/drawing.h> void main(void) @@ -22,7 +25,7 @@ void main(void) italic_font = font_load(font_italic); /* 93 tiles */ /* Load this one with dk grey background and white foreground */ - color(WHITE, DKGREY, SOLID); + font_color(WHITE, DKGREY, SOLID); min_font = font_load(font_min); diff --git a/gbdk-lib/examples/ap/irq/irq.c b/gbdk-lib/examples/ap/irq/irq.c @@ -1,6 +1,6 @@ #include <gb/gb.h> +#include <gbdk/console.h> #include <stdint.h> -#include <gb/console.h> #include <stdio.h> #include <string.h> diff --git a/gbdk-lib/examples/ap/sgb_pong/sgb_pong.c b/gbdk-lib/examples/ap/sgb_pong/sgb_pong.c @@ -1,9 +1,9 @@ #include <gb/gb.h> -#include <stdint.h> #include <gb/sgb.h> +#include <gbdk/console.h> +#include <stdint.h> #include <stdio.h> -#include <gb/console.h> uint8_t sprite_data[] = { 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, diff --git a/gbdk-lib/examples/ap/sound/sound.c b/gbdk-lib/examples/ap/sound/sound.c @@ -1,7 +1,7 @@ #include <gb/gb.h> +#include <gbdk/console.h> #include <stdint.h> #include <stdio.h> -#include <gb/console.h> #define ARROW_CHAR '>' #define SPACE_CHAR ' ' diff --git a/gbdk-lib/examples/broken/fprpn/Makefile b/gbdk-lib/examples/broken/fprpn/Makefile @@ -1,20 +0,0 @@ -CC = ../../../bin/lcc -Wa-l -Wl-m -Wl-j - -# Disabled building this for now since it's broken -BINS = -# BINS = fprpn.gb - -all: $(BINS) - -compile.bat: Makefile - @echo "REM Automatically generated from Makefile" > compile.bat -# Disabled building this for now since it's broken -# @make -sn | sed y/\\//\\\\/ | grep -v make >> compile.bat - -# Compile and link single file in one pass -%.gb: %.c - $(CC) -o $@ $< - -clean: - rm -f *.o *.lst *.map *.gb *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi - diff --git a/gbdk-lib/examples/broken/fprpn/fprpn.c b/gbdk-lib/examples/broken/fprpn/fprpn.c @@ -1,106 +0,0 @@ -#include <stdint.h> -#include <stdio.h> -#include <ctype.h> - -#define MAXOP 40 -#define NUMBER '0' -#define STACKSIZE 40 - -uint8_t sp; -float stack[STACKSIZE]; - -char s[MAXOP]; -uint8_t pos; -float f; - -void push(float f) -{ - if(sp < STACKSIZE) - stack[sp++] = f; - else - puts("Stack full"); -} - -float pop() -{ - if(sp > 0) - return stack[--sp]; - else - puts("Stack empty"); - return 0.0; -} - -float top() -{ - if(sp > 0) - return stack[sp-1]; - else - puts("Stack empty"); - return 0.0; -} - -int8_t read_op() -{ - if(pos == 0) { - gets(s); - } - - while(s[pos] == ' ' || s[pos] == '\t') - pos++; - - if(s[pos] == '\0') { - pos = 0; - return('\n'); - } - - if(!isdigit(s[pos])) - return(s[pos++]); - - f = s[pos] - '0'; - while(isdigit(s[++pos])) { - int8_t i = s[pos] - '0'; - f = 10.0 * f; - f += (float)i; - } -/* f = 10.0 * f + (float)(int8_t)(s[pos] - '0'); */ - - return NUMBER; -} - -void main() -{ - int8_t type; - float op2; - - puts("FP RPN Calculator"); - sp = 0; - pos = 0; - - while((type = read_op(s)) != 0) { - switch(type) { - case NUMBER: - push(f); - break; - case '+': - push(pop() + pop()); - break; - case '*': - push(pop() * pop()); - break; - case '-': - op2 = pop(); - push(pop() - op2); - break; - case '/': - op2 = pop(); - if(op2 != 0) - push(pop() / op2); - else - puts("Divide by 0"); - break; - case '\n': - printf("==> %f\n", top()); - break; - } - } -} diff --git a/gbdk-lib/examples/gb/bcd/bcd.c b/gbdk-lib/examples/gb/bcd/bcd.c @@ -1,9 +1,8 @@ -#include <gb/gb.h> #include <stdint.h> - #include <stdio.h> -#include <gb/font.h> +#include <gbdk/platform.h> +#include <gbdk/font.h> #include <gbdk/bcd.h> BCD bcd = MAKE_BCD(10203040); diff --git a/gbdk-lib/examples/gb/fonts/fonts.c b/gbdk-lib/examples/gb/fonts/fonts.c @@ -5,8 +5,11 @@ */ #include <stdio.h> -#include <gb/font.h> -#include <gb/console.h> + +#include <gbdk/platform.h> +#include <gbdk/font.h> +#include <gbdk/console.h> + #include <gb/drawing.h> void main(void) diff --git a/gbdk-lib/examples/gb/irq/irq.c b/gbdk-lib/examples/gb/irq/irq.c @@ -1,6 +1,7 @@ #include <gb/gb.h> +#include <gbdk/console.h> + #include <stdint.h> -#include <gb/console.h> #include <stdio.h> #include <string.h> diff --git a/gbdk-lib/examples/gb/sgb_pong/sgb_pong.c b/gbdk-lib/examples/gb/sgb_pong/sgb_pong.c @@ -1,9 +1,9 @@ #include <gb/gb.h> -#include <stdint.h> #include <gb/sgb.h> +#include <gbdk/console.h> +#include <stdint.h> #include <stdio.h> -#include <gb/console.h> uint8_t sprite_data[] = { 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, diff --git a/gbdk-lib/examples/gb/simple_physics/phys.c b/gbdk-lib/examples/gb/simple_physics/phys.c @@ -2,7 +2,7 @@ #include <gb/sgb.h> #include <stdio.h> -#include <gb/console.h> +#include <gbdk/console.h> UINT8 sprite_data[] = { 0x3C,0x3C,0x42,0x7E,0x99,0xFF,0xA9,0xFF,0x89,0xFF,0x89,0xFF,0x42,0x7E,0x3C,0x3C, diff --git a/gbdk-lib/examples/gb/sound/sound.c b/gbdk-lib/examples/gb/sound/sound.c @@ -1,7 +1,7 @@ #include <gb/gb.h> +#include <gbdk/console.h> #include <stdint.h> #include <stdio.h> -#include <gb/console.h> #define ARROW_CHAR '>' #define SPACE_CHAR ' ' diff --git a/gbdk-lib/examples/sms/fonts/Makefile b/gbdk-lib/examples/sms/fonts/Makefile @@ -0,0 +1,30 @@ +CC = ../../../bin/lcc -mz80:sms -Wm-yoA -Wa-l -Wl-j + +BINS = fonts.sms + +all: $(BINS) + +ASRC = $(wildcard *.s) +CSRC = $(wildcard *.c) + +OBJS = $(CSRC:%.c=%.o) $(ASRC:%.s=%.o) + +compile.bat: Makefile + @echo "REM Automatically generated from Makefile" > compile.bat + @make -sn | sed y/\\//\\\\/ | grep -v make >> compile.bat + +# Compile and link single file in one pass + +%.o: %.c + $(CC) -c -o $@ $< + +%.o: %.s + $(CC) -c -o $@ $< + +$(BINS): $(OBJS) + $(CC) -Wm-yS -o $@ $^ + rm -f *.map *.noi *.ihx *.lst + +clean: + rm -f *.o *.lst *.map *.sms *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi + diff --git a/gbdk-lib/examples/sms/fonts/fonts.c b/gbdk-lib/examples/sms/fonts/fonts.c @@ -0,0 +1,63 @@ +/* + fonts.c + Simple example of how to use multiple fonts on the GB + Michael Hope, 1999. +*/ + +#include <stdio.h> + +#include <gbdk/platform.h> +#include <gbdk/font.h> + +#include <gbdk/console.h> + +#define WHITE 3 +#define DKGREY 1 + +void main(void) +{ + font_t ibm_font, italic_font, min_font; + int i; + + DISPLAY_ON; + + /* First, init the font system */ + font_init(); + + /* Load all the fonts that we can */ + ibm_font = font_load(font_ibm); /* 96 tiles */ + italic_font = font_load(font_italic); /* 93 tiles */ + + /* Load this one with dk grey background and white foreground */ + font_color(WHITE, DKGREY); + + min_font = font_load(font_min); + + /* Turn scrolling off (why not?) */ + mode(get_mode() | M_NO_SCROLL); + + /* Print some text! */ + + /* IBM font */ + font_set(ibm_font); + printf("Font demo.\n\n"); + + printf("IBM Font #!?123\n"); + + /* In italic */ + font_set(italic_font); + for (i=1; i!=5; i++) { + printf("In italics, line %u\n", i); + } + + /* With a minimal, colour changed font */ + font_set(min_font); + printf("Minimal 36 tile font\n"); + + /* Done */ + font_set(ibm_font); + printf("\nDone!"); +} + + + diff --git a/gbdk-lib/include/gb/console.h b/gbdk-lib/include/gb/console.h @@ -1,44 +0,0 @@ -/** @file gb/console.h - Console functions that work like Turbo C's. - - The font is 8x8, making the screen 20x18 characters. -*/ -#ifndef _CONSOLE_H -#define _CONSOLE_H - -#include <types.h> -#include <stdint.h> - -/** Move the cursor to an absolute position at __x, y__. - - __x__ and __y__ have units of tiles (8 pixels per unit) - @see setchar() - */ -void gotoxy(uint8_t x, uint8_t y); - -/** Returns the current X position of the cursor. - - @see gotoxy() - */ -uint8_t posx(void); - -/** Returns the current Y position of the cursor. - - @see gotoxy() - */ -uint8_t posy(void); - -/** Writes out a single character at the current cursor - position. - - Does not update the cursor or interpret the character. - - @see gotoxy() -*/ -void setchar(char c); - -/** Clears the screen -*/ -void cls(); - -#endif /* _CONSOLE_H */ diff --git a/gbdk-lib/include/gb/font.h b/gbdk-lib/include/gb/font.h @@ -1,75 +0,0 @@ -/** @file gb/font.h - Multiple font support for the GameBoy - Michael Hope, 1999 - michaelh@earthling.net -*/ -#ifndef __FONT_H -#define __FONT_H - -#include <gb/gb.h> -#include <stdint.h> - -/** Various flags in the font header. - */ -#define FONT_256ENCODING 0 -#define FONT_128ENCODING 1 -#define FONT_NOENCODING 2 - -#define FONT_COMPRESSED 4 - -/* See gb.h/M_NO_SCROLL and gb.h/M_NO_INTERP */ - -/** font_t is a handle to a font loaded by font_load(). - It can be used with @ref font_set() */ -typedef uint16_t font_t; - - -/*! \defgroup gbdk_fonts List of gbdk fonts - @{ -*/ - -/** The default fonts */ -extern uint8_t font_spect[], font_italic[], font_ibm[], font_min[]; - -/** Backwards compatible font */ -extern uint8_t font_ibm_fixed[]; - - /*! @} End of gbdk_fonts */ - - -/** Initializes the font system. - Should be called before other font functions. - */ -void font_init(void) NONBANKED; - -/** Load a font and set it as the current font. - @param font Pointer to a font to load (usually a gbdk font) - - @return Handle to the loaded font, which can be used with @ref font_set() - @see font_init(), font_set(), gbdk_fonts - */ -font_t font_load( void *font ) NONBANKED; - -/** Set the current font. - @param font_handle handle of a font returned by @ref font_load() - - @return The previously used font handle. - @see font_init(), font_load() -*/ -font_t font_set( font_t font_handle ) NONBANKED; - -/* Use mode() and color() to set the font modes and colours */ - -/** Internal representation of a font. - What a font_t really is */ -typedef struct sfont_handle mfont_handle; -typedef struct sfont_handle *pmfont_handle; - -/** Font handle structure -*/ -struct sfont_handle { - uint8_t first_tile; /**< First tile used for font */ - void *font; /**< Pointer to the base of the font */ -}; - -#endif /* __FONT_H */ diff --git a/gbdk-lib/include/gbdk/console.h b/gbdk-lib/include/gbdk/console.h @@ -0,0 +1,45 @@ +/** @file gb/console.h + Console functions that work like Turbo C's. + + The font is 8x8, making the screen 20x18 characters. +*/ +#ifndef _CONSOLE_H +#define _CONSOLE_H + +#include <types.h> +#include <stdint.h> + +/** Move the cursor to an absolute position at __x, y__. + + __x__ and __y__ have units of tiles (8 pixels per unit) + @see setchar() + */ +void gotoxy(uint8_t x, uint8_t y); + +/** Returns the current X position of the cursor. + + @see gotoxy() + */ +uint8_t posx(void); + +/** Returns the current Y position of the cursor. + + @see gotoxy() + */ +uint8_t posy(void); + +/** Writes out a single character at the current cursor + position. + + Does not update the cursor or interpret the character. + + @see gotoxy() +*/ +void setchar(char c); + +/** Clears the screen +*/ +void cls(); + + +#endif /* _CONSOLE_H */ diff --git a/gbdk-lib/include/gbdk/font.h b/gbdk-lib/include/gbdk/font.h @@ -0,0 +1,77 @@ +/** @file gb/font.h + Multiple font support for the GameBoy + Michael Hope, 1999 + michaelh@earthling.net +*/ +#ifndef __FONT_H +#define __FONT_H + +#include <stdint.h> + +/** Various flags in the font header. + */ +#define FONT_256ENCODING 0 +#define FONT_128ENCODING 1 +#define FONT_NOENCODING 2 + +#define FONT_COMPRESSED 4 + +/* See gb.h/M_NO_SCROLL and gb.h/M_NO_INTERP */ + +/** font_t is a handle to a font loaded by font_load(). + It can be used with @ref font_set() */ +typedef uint16_t font_t; + + +/*! \defgroup gbdk_fonts List of gbdk fonts + @{ +*/ + +/** The default fonts */ +extern uint8_t font_spect[], font_italic[], font_ibm[], font_min[]; + +/** Backwards compatible font */ +extern uint8_t font_ibm_fixed[]; + + /*! @} End of gbdk_fonts */ + + +/** Initializes the font system. + Should be called before other font functions. + */ +void font_init(void) NONBANKED; + +/** Load a font and set it as the current font. + @param font Pointer to a font to load (usually a gbdk font) + + @return Handle to the loaded font, which can be used with @ref font_set() + @see font_init(), font_set(), gbdk_fonts + */ +font_t font_load( void *font ) NONBANKED; + +/** Set the current font. + @param font_handle handle of a font returned by @ref font_load() + + @return The previously used font handle. + @see font_init(), font_load() +*/ +font_t font_set( font_t font_handle ) NONBANKED; + +/* Use mode() and color() to set the font modes and colours */ + +/** Internal representation of a font. + What a font_t really is */ +typedef struct sfont_handle mfont_handle; +typedef struct sfont_handle *pmfont_handle; + +/** Font handle structure +*/ +struct sfont_handle { + uint8_t first_tile; /**< First tile used for font */ + void *font; /**< Pointer to the base of the font */ +}; + +/** Set the current __foreground__ colour (for pixels), __background__ colour */ +void font_color(uint8_t forecolor, uint8_t backcolor); + +#endif /* __FONT_H */ diff --git a/gbdk-lib/include/sms/sms.h b/gbdk-lib/include/sms/sms.h @@ -34,12 +34,44 @@ #define J_LEFT 0b00000100 #define J_RIGHT 0b00001000 +/** Screen modes. + Normally used by internal functions only. + @see mode() + */ +#define M_TEXT_OUT 0x02U +#define M_TEXT_INOUT 0x03U +/** Set this in addition to the others to disable scrolling + + If scrolling is disabled, the cursor returns to (0,0) + @see mode() +*/ +#define M_NO_SCROLL 0x04U +/** Set this to disable interpretation + @see mode() +*/ +#define M_NO_INTERP 0x08U + +// VDP helper macros #define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);__critical{VDP_CMD=(shadow_##REG),VDP_CMD=REG;} #define __READ_VDP_REG(REG) shadow_##REG void WRITE_VDP_CMD(uint16_t cmd) __z88dk_fastcall __preserves_regs(b, c, d, e, iyh, iyl); void WRITE_VDP_DATA(uint16_t data) __z88dk_fastcall __preserves_regs(b, c, d, e, iyh, iyl); +/** Set the current screen mode - one of M_* modes + + Normally used by internal functions only. + + @see M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP +*/ +void mode(uint8_t m) NONBANKED; + +/** Returns the current mode + + @see M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP +*/ +uint8_t get_mode(void) NONBANKED; + /** Interrupt handlers */ typedef void (*int_handler)(void) NONBANKED; diff --git a/gbdk-lib/include/stdio.h b/gbdk-lib/include/stdio.h @@ -11,12 +11,12 @@ #include <types.h> -#if STRICT_ANSI -void putchar(int c); -#else -/** Write the character __c__ to stdout. -*/ +#if defined(__PORT_gbz80) void putchar(char c); +#elif defined(__PORT_z80) +void putchar(char c) __z88dk_fastcall; +#else + #error Unrecognised port #endif /** Print the string and arguments given by format to stdout. diff --git a/gbdk-lib/include/stdlib.h b/gbdk-lib/include/stdlib.h @@ -69,7 +69,7 @@ long atol(const char *s); Returns: Pointer to converted string */ -char *itoa(int n, char *s); +char *itoa(int n, char *s, unsigned char radix); /** Converts an unsigned int into a base 10 ASCII string. @param n Unsigned Int to convert to a string @@ -77,7 +77,7 @@ char *itoa(int n, char *s); Returns: Pointer to converted string */ -char *utoa(unsigned int n, char *s); +char *uitoa(unsigned int n, char *s, unsigned char radix); /** Converts a long into a base 10 ASCII string. @param n Long int to convert to a string @@ -85,7 +85,7 @@ char *utoa(unsigned int n, char *s); Returns: Pointer to converted string */ -char *ltoa(long n, char *s); +char *ltoa(long n, char *s, unsigned char radix); /** Converts an unsigned long into a base 10 ASCII string. @param n Unsigned Long Int to convert to a string @@ -93,7 +93,7 @@ char *ltoa(long n, char *s); Returns: Pointer to converted string */ -char *ultoa(unsigned long n, char *s); +char *ultoa(unsigned long n, char *s, unsigned char radix); /** Memory allocation functions diff --git a/gbdk-lib/libc/asm/gbz80/itoa.s b/gbdk-lib/libc/asm/gbz80/itoa.s @@ -30,7 +30,7 @@ .area _BASE -_utoa:: +_uitoa:: push BC lda HL, 4(SP) ld A, (HL+) diff --git a/gbdk-lib/libc/asm/z80/__itoa.s b/gbdk-lib/libc/asm/z80/__itoa.s @@ -28,12 +28,12 @@ .area _CODE - .globl ___itoa - .globl ___uitoa + .globl _itoa + .globl _uitoa ; ;void __itoa(int value, char *string, unsigned char radix); ; -___itoa:: +_itoa:: push ix ld ix, #0 add ix, sp @@ -65,7 +65,7 @@ ___itoa:: ; ;void __uitoa(unsigned int value, char *string, unsigned char radix); ; -___uitoa:: +_uitoa:: push ix ld ix, #0 add ix, sp diff --git a/gbdk-lib/libc/asm/z80/__ltoa.s b/gbdk-lib/libc/asm/z80/__ltoa.s @@ -28,12 +28,12 @@ .area _CODE - .globl ___ltoa - .globl ___ultoa + .globl _ltoa + .globl _ultoa ; ;void __itoa(long value, char *string, unsigned char radix); ; -___ltoa:: +_ltoa:: push ix ld ix, #0 add ix, sp @@ -71,7 +71,7 @@ ___ltoa:: ; ;void __uitoa(unsigned int value, char *string, unsigned char radix); ; -___ultoa:: +_ultoa:: push ix ld ix, #0 add ix, sp diff --git a/gbdk-lib/libc/sprintf.c b/gbdk-lib/libc/sprintf.c @@ -48,13 +48,13 @@ void __printf(const char *format, emitter_t emitter, char **pData, va_list va) } case 'u': { - utoa((unsigned char)va_arg(va, char), buf); + uitoa((unsigned char)va_arg(va, char), buf, 10); _printbuf(buf, emitter, pData); break; } case 'd': { - itoa((signed char)va_arg(va, char), buf); + itoa((signed char)va_arg(va, char), buf, 10); _printbuf(buf, emitter, pData); break; } @@ -68,13 +68,13 @@ void __printf(const char *format, emitter_t emitter, char **pData, va_list va) } case 'u': { - utoa(va_arg(va, int), buf); + uitoa(va_arg(va, int), buf, 10); _printbuf(buf, emitter, pData); break; } case 'd': { - itoa(va_arg(va, int), buf); + itoa(va_arg(va, int), buf, 10); _printbuf(buf, emitter, pData); break; } diff --git a/gbdk-lib/libc/targets/gbz80/ap/Makefile b/gbdk-lib/libc/targets/gbz80/ap/Makefile @@ -19,7 +19,7 @@ ASSRC = cgb.s cgb_palettes.s cgb_compat.s \ serial.s set_bk_t.s set_tile.s \ set_data.s set_prop.s set_spr.s set_wi_t.s set_xy_t.s \ set_1bit_data.s \ - sgb.s font.s delay.s \ + sgb.s font.s font_color.s delay.s \ bgb_emu.s bgb_emu_printf.s \ nowait.s far_ptr.s \ lcd.s joy.s tim.s \ diff --git a/gbdk-lib/libc/targets/gbz80/font.s b/gbdk-lib/libc/targets/gbz80/font.s @@ -478,7 +478,7 @@ _font_init:: jr nz,1$ ld a,#3 ld (.fg_colour),a - ld a,#0 + xor a ld (.bg_colour),a call .cls diff --git a/gbdk-lib/libc/targets/gbz80/font_color.s b/gbdk-lib/libc/targets/gbz80/font_color.s @@ -0,0 +1,12 @@ + .include "global.s" + + .globl .fg_colour, .bg_colour:: + + .area _BASE +_font_color:: + LDA HL,2(SP) ; Skip return address and registers + LD A,(HL+) ; A = Foreground + LD (.fg_colour),a + LD A,(HL) + LD (.bg_colour),a + RET diff --git a/gbdk-lib/libc/targets/gbz80/gb/Makefile b/gbdk-lib/libc/targets/gbz80/gb/Makefile @@ -19,7 +19,7 @@ ASSRC = cgb.s cgb_palettes.s cgb_compat.s \ serial.s set_bk_t.s set_tile.s \ set_data.s set_prop.s set_spr.s set_wi_t.s set_xy_t.s \ set_1bit_data.s \ - sgb.s font.s delay.s \ + sgb.s font.s font_color.s delay.s \ bgb_emu.s bgb_emu_printf.s \ nowait.s far_ptr.s \ lcd.s joy.s tim.s \ diff --git a/gbdk-lib/libc/targets/z80/crt0.s b/gbdk-lib/libc/targets/z80/crt0.s @@ -213,6 +213,8 @@ __shadow_OAM_base:: .ds 0x01 __shadow_OAM_OFF:: .ds 0x01 +.mode:: + .ds 0x01 ; Current mode .area _INITIALIZER @@ -231,4 +233,5 @@ __shadow_OAM_OFF:: .db 0 ; .vbl_done .db 0 ; _VDP_ATTR_SHIFT .db #>_shadow_OAM ; __shadow_OAM_base - .db 0 ; __shadow_OAM_OFF - + .db 0 ; __shadow_OAM_OFF + .db .T_MODE_INOUT ; .mode + diff --git a/gbdk-lib/libc/targets/z80/font.s b/gbdk-lib/libc/targets/z80/font.s @@ -0,0 +1,325 @@ + .include "global.s" + + .title "Font utilities" + .module FontUtils + + .globl .memset_small + + ; Structure offsets + sfont_handle_sizeof = 3 + sfont_handle_font = 1 + sfont_handle_first_tile = 0 + + ; Encoding types - lower 2 bits of font + FONT_256ENCODING = 0 + FONT_128ENCODING = 1 + FONT_NOENCODING = 2 + + ; Other bits + FONT_BCOMPRESSED = 2 + + .CR = 0x0A ; Unix + .SPACE = 0x00 + + ; Maximum number of fonts + .MAX_FONTS = 6 + + .area _BSS + ; The current font +.start_font_vars: +.curx:: + .ds 1 +.cury:: + .ds 1 +.fg_colour:: + .ds 1 +.bg_colour:: + .ds 1 + +font_current: + .ds sfont_handle_sizeof + ; Cached copy of the first free tile +font_first_free_tile: + .ds 1 + ; Table containing descriptors for all of the fonts +font_table: + .ds (sfont_handle_sizeof*.MAX_FONTS) +.end_font_vars: + + .area _GSINIT + + xor a + ld hl,#.start_font_vars + ld c,#(.end_font_vars-.start_font_vars) + call .memset_small + ld a, #3 + ld (.fg_colour), a + + .area _HOME + +; void vmemcpy (unsigned int dst, const void *src, unsigned int size) __z88dk_callee __preserves_regs(iyh,iyl); +_font_init:: + xor a + ld (font_first_free_tile), a + + ld hl, #font_table + ld c, #(sfont_handle_sizeof*.MAX_FONTS) + call .memset_small + + ld a, #3 + ld (.fg_colour), a + xor a + ld (.bg_colour), a + + ret + +_font_set:: + pop de + pop hl + push hl + push de + +font_set:: + ld de, #font_current + ld bc, #sfont_handle_sizeof + ldir + ret + +_font_color:: + pop hl + pop de + push de + push hl + ld hl, #.fg_colour + ld (hl), e + inc hl + ld (hl), d + ret + +_font_load:: + pop de + pop hl + push hl + push de + + ;; load font in HL to free slor +font_load:: + push hl + ; find free slot + ld b, #.MAX_FONTS + ld hl, #(font_table+sfont_handle_font) +1$: + ld a, (hl) + inc hl + or (hl) + jr z, 2$ ; found + inc hl + inc hl + dec b + jr nz, 1$ + + ld hl, #0 ; no free slot + ret +2$: + ; fill free slot with passed pointer + pop de + ld (hl), d + dec hl + ld (hl), e + ld a, (font_first_free_tile) + dec hl + ld (hl),a + + push hl + call font_set + + call load_font_tiles + + ld hl, (font_current+sfont_handle_font) + + inc hl ; Number of tiles used + ld a, (font_first_free_tile) + add (hl) + ld (font_first_free_tile), a + + pop hl ; return handle in hl + ret + +load_font_tiles: + ld hl, (font_current+sfont_handle_font) + + inc hl + ld e, (hl) + ld d, #0 + + dec hl + ld a, (hl) ; a = flags + push af + and #3 + + ld bc, #128 + cp #FONT_128ENCODING ; 0 for 256 char encoding table, 1 for 128 char + jr z, 1$ + + ld bc, #0 + cp #FONT_NOENCODING + jr z, 1$ + + ld bc, #256 ; Must be 256 element encoding +1$: + inc hl + inc hl ; Points to the start of the encoding table + add hl, bc + ld c, l + ld b, h ; BC points to the start of the tile data + + ; Find the offset in VRAM for this font + ld a,(font_current+sfont_handle_first_tile) ; First tile used for this font + ld l, a + ld h, #0 + add hl, hl + add hl, hl + add hl, hl + add hl, hl + add hl, hl + ld a, #>.VDP_VRAM + add h + ld h, a ; HL = destination in VRAM + + DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT + SMS_WRITE_VDP_CMD h, l + ld h, b + ld l, c + + pop af ; Recover flags + bit FONT_BCOMPRESSED, a ; Is this font compressed? + jp z, 2$ + ; font_copy_compressed + + ld c, #.VDP_DATA + inc d + inc e + xor a + jr 7$ + +8$: + ld b, #8 +9$: + outi + jr 10$ +10$: + nop + out (c), a + jr 11$ +11$: + nop + out (c), a + jr 12$ +12$: + nop + nop + out (c), a + nop + jr nz, 9$ +7$: + dec e + jr nz, 8$ + + dec d + jr nz, 8$ + + jp 6$ +2$: + ; font_copy_uncompressed + ld c, #.VDP_DATA + inc d + inc e + jr 5$ +3$: + ld b, #32 +4$: + outi + jr nz, 4$ +5$: + dec e + jp nz, 3$ + + dec d + jp nz, 3$ + +6$: + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT + + ret + +_putchar:: + ld e, a + + cp #.CR + jr nz, 0$ + + xor a + ld (.curx), a + jp 2$ +0$: + ld hl, (font_current+sfont_handle_font) + ld a, (hl) + and #3 + cp #FONT_NOENCODING + jr z, 4$ + inc hl + inc hl + ld d, #0 + add hl, de + ld e, (hl) +4$: + ld a, (font_current) + add a, e + ld e, a + + ld a, (.cury) + ld l, a + ld h, #0 + add hl, hl + add hl, hl + add hl, hl + add hl, hl + add hl, hl + add hl, hl + ld a, (.curx) + add a + add l + ld l, a + adc h + sub l + ld h, a + + ld bc, #.VDP_TILEMAP + add hl, bc + + DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT + SMS_WRITE_VDP_CMD h, l + + ld a, e + out (.VDP_DATA), a + jr 1$ +1$: + xor a + out (.VDP_DATA), a + + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT + + ld a, (.curx) + inc a + and #0x1f + ld (.curx), a + ret nz +2$: + ld a, (.cury) + inc a + cp #28 + jr c, 3$ + xor a +3$: + ld (.cury), a + ret diff --git a/gbdk-lib/libc/targets/z80/gg/Makefile b/gbdk-lib/libc/targets/z80/gg/Makefile @@ -12,9 +12,12 @@ ASSRC = outi.s vmemcpy.s \ set_tile_map.s set_tile_map_xy.s set_tile_map_compat.s set_tile_map_xy_compat.s \ metasprites.s metasprites_hide.s metasprites_hide_spr.s \ f_ibm_full.s f_ibm_sh.s f_italic.s f_min.s f_spect.s \ + font.s \ palette.s \ pad.s pad_ex.s \ int.s nmi.s \ + mode.s \ + memset_small.s \ __sdcc_bcall.s \ crt0.s diff --git a/gbdk-lib/libc/targets/z80/gg/global.s b/gbdk-lib/libc/targets/z80/gg/global.s @@ -159,7 +159,18 @@ .SYSTEM_PAL = 0x00 .SYSTEM_NTSC = 0x01 - + + ;; GBDK library screen modes + + .T_MODE = 0x02 ; Text mode (bit 2) + .T_MODE_OUT = 0x02 ; Text mode output only + .T_MODE_INOUT = 0x03 ; Text mode with input + .M_NO_SCROLL = 0x04 ; Disables scrolling of the screen in text mode + .M_NO_INTERP = 0x08 ; Disables special character interpretation + + ;; Global variables + .globl .mode + ;; Interrupt routines .globl _INT_ISR .globl _NMI_ISR diff --git a/gbdk-lib/libc/targets/z80/memset_small.s b/gbdk-lib/libc/targets/z80/memset_small.s @@ -0,0 +1,14 @@ + .include "global.s" + + .title "memset small" + .module MemSetSmall + + .area _HOME + +.memset_small:: +1$: + ld (hl), a + inc hl + dec b + jp nz, 1$ + ret diff --git a/gbdk-lib/libc/targets/z80/metasprites_hide.s b/gbdk-lib/libc/targets/z80/metasprites_hide.s @@ -11,7 +11,7 @@ ; void __hide_metasprite(uint8_t id) __z88dk_fastcall __preserves_regs(iyh,iyl); ___hide_metasprite:: - ld e, l + ld e, a ld hl, (___current_metasprite) diff --git a/gbdk-lib/libc/targets/z80/mode.s b/gbdk-lib/libc/targets/z80/mode.s @@ -0,0 +1,22 @@ + .include "global.s" + + .title "screen modes" + .module Modes + + .area _HOME + +_mode:: + pop de + pop hl + push hl + push de + +.set_mode:: + ld a, l + ld (.mode), a + ret + +_get_mode:: + ld hl, #.mode + ld l, (hl) + ret + diff --git a/gbdk-lib/libc/targets/z80/sms/Makefile b/gbdk-lib/libc/targets/z80/sms/Makefile @@ -12,9 +12,12 @@ ASSRC = outi.s vmemcpy.s \ set_tile_map.s set_tile_map_xy.s set_tile_map_compat.s set_tile_map_xy_compat.s \ metasprites.s metasprites_hide.s metasprites_hide_spr.s \ f_ibm_full.s f_ibm_sh.s f_italic.s f_min.s f_spect.s \ + font.s \ palette.s \ pad.s pad_ex.s \ int.s nmi.s \ + mode.s \ + memset_small.s \ __sdcc_bcall.s \ crt0.s diff --git a/gbdk-lib/libc/targets/z80/sms/global.s b/gbdk-lib/libc/targets/z80/sms/global.s @@ -159,7 +159,18 @@ .SYSTEM_PAL = 0x00 .SYSTEM_NTSC = 0x01 - + + ;; GBDK library screen modes + + .T_MODE = 0x02 ; Text mode (bit 2) + .T_MODE_OUT = 0x02 ; Text mode output only + .T_MODE_INOUT = 0x03 ; Text mode with input + .M_NO_SCROLL = 0x04 ; Disables scrolling of the screen in text mode + .M_NO_INTERP = 0x08 ; Disables special character interpretation + + ;; Global variables + .globl .mode + ;; Interrupt routines .globl _INT_ISR .globl _NMI_ISR

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.