gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 9083b38f8ad1323c48a8af6dd9bfc97b08381c5a parent 038e699195de289972164b134af99ccf03640a75 Author: Toxa <untoxa@mail.ru> Date: Fri, 20 Aug 2021 15:10:21 +0300 SMS/GG: console.h support Diffstat:
18 files changed, 431 insertions(+), 116 deletions(-)
diff --git a/gbdk-lib/examples/gg/fonts/Makefile b/gbdk-lib/examples/gg/fonts/Makefile @@ -0,0 +1,30 @@ +CC = ../../../bin/lcc -mz80:gg -Wm-yoA -Wa-l -Wl-j + +BINS = fonts.gg + +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 *.gg *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi + diff --git a/gbdk-lib/examples/gg/fonts/fonts.c b/gbdk-lib/examples/gg/fonts/fonts.c @@ -0,0 +1,61 @@ +/* + 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; + + /* 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/examples/gg/metasprites/metasprites.c b/gbdk-lib/examples/gg/metasprites/metasprites.c @@ -27,13 +27,6 @@ uint8_t idx, rot; // main funxction void main(void) { - // Set compatible palette - for (uint8_t i = 0; i != 2; i++) { - set_palette_entry(i, 0, RGB8(255, 255, 255)); - set_palette_entry(i, 1, RGB8(128, 128, 128)); - set_palette_entry(i, 2, RGB8( 64, 64, 64)); - set_palette_entry(i, 3, RGB8( 0, 0, 0)); - } // Fill the screen background with a single tile pattern // fill_bkg_rect(0, 0, 20, 18, 0); set_bkg_2bpp_data(0, 1, pattern); diff --git a/gbdk-lib/examples/sms/metasprites/metasprites.c b/gbdk-lib/examples/sms/metasprites/metasprites.c @@ -27,13 +27,6 @@ uint8_t idx, rot; // main funxction void main(void) { - // Set compatible palette - for (uint8_t i = 0; i != 2; i++) { - set_palette_entry(i, 0, RGB8(255, 255, 255)); - set_palette_entry(i, 1, RGB8(128, 128, 128)); - set_palette_entry(i, 2, RGB8( 64, 64, 64)); - set_palette_entry(i, 3, RGB8( 0, 0, 0)); - } // Fill the screen background with a single tile pattern // fill_bkg_rect(0, 0, 20, 18, 0); set_bkg_2bpp_data(0, 1, pattern); diff --git a/gbdk-lib/include/stdio.h b/gbdk-lib/include/stdio.h @@ -11,13 +11,11 @@ #include <types.h> -#if defined(__PORT_gbz80) -void putchar(char c); -#elif defined(__PORT_z80) -void putchar(char c) __z88dk_fastcall; -#else - #error Unrecognised port -#endif +/** Print char to stdout. + @param c Character to print + */ + +void putchar(char c) NONBANKED; /** Print the string and arguments given by format to stdout. diff --git a/gbdk-lib/libc/targets/gbz80/font_color.s b/gbdk-lib/libc/targets/gbz80/font_color.s @@ -1,6 +1,6 @@ .include "global.s" - .globl .fg_colour, .bg_colour:: + .globl .fg_colour, .bg_colour .area _BASE _font_color:: diff --git a/gbdk-lib/libc/targets/z80/cls.s b/gbdk-lib/libc/targets/z80/cls.s @@ -0,0 +1,30 @@ + .include "global.s" + + .title "console utilities" + .module ConsoleUtils + + .area _HOME + +_cls:: + DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT + ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS * .VDP_MAP_WIDTH) * 2)) + WRITE_VDP_CMD_HL + + ld bc, #(.SCREEN_HEIGHT * .VDP_MAP_WIDTH) + inc b + inc c + jr 1$ +2$: + ld a, #.SPACE + out (.VDP_DATA), a + VDP_DELAY + xor a + out (.VDP_DATA), a +1$: + dec c + jr nz, 2$ + dec b + jr nz, 2$ + + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT + ret + diff --git a/gbdk-lib/libc/targets/z80/font.s b/gbdk-lib/libc/targets/z80/font.s @@ -4,6 +4,8 @@ .module FontUtils .globl .memset_small + .globl _cls + .globl _font_ibm ; Structure offsets sfont_handle_sizeof = 3 @@ -18,25 +20,18 @@ ; 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: +font_current:: .ds sfont_handle_sizeof ; Cached copy of the first free tile font_first_free_tile: @@ -79,6 +74,8 @@ _font_init:: xor a ld (.bg_colour), a + call _cls + ret _font_set:: @@ -104,6 +101,10 @@ _font_color:: ld (hl), d ret +font_load_ibm:: + ld hl, #_font_ibm + jp font_load + _font_load:: pop de pop hl @@ -195,7 +196,7 @@ load_font_tiles: ld h, a ; HL = destination in VRAM DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT - SMS_WRITE_VDP_CMD h, l + WRITE_VDP_CMD_HL ld h, b ld l, c @@ -235,16 +236,13 @@ load_font_tiles: ld a, (plane0) out (.VDP_DATA), a - jr 22$ -22$: + VDP_DELAY ld a, (plane1) out (.VDP_DATA), a - jr 23$ -23$: + VDP_DELAY ld a, (plane2) out (.VDP_DATA), a - jr 24$ -24$: + VDP_DELAY ld a, (plane3) out (.VDP_DATA), a @@ -273,75 +271,3 @@ load_font_tiles: 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 @@ -13,6 +13,8 @@ ASSRC = outi.s vmemcpy.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 \ + putchar.s \ + scroll.s cls.s gotoxy.s \ palette.s \ pad.s pad_ex.s \ int.s nmi.s \ diff --git a/gbdk-lib/libc/targets/z80/gg/global.s b/gbdk-lib/libc/targets/z80/gg/global.s @@ -168,6 +168,19 @@ .M_NO_SCROLL = 0x04 ; Disables scrolling of the screen in text mode .M_NO_INTERP = 0x08 ; Disables special character interpretation + ;; Screen dimentions in tiles + + .SCREEN_X_OFS = 6 + .SCREEN_Y_OFS = 3 + .SCREEN_WIDTH = 20 + .SCREEN_HEIGHT = 18 + .VDP_MAP_HEIGHT = 28 + .VDP_MAP_WIDTH = 32 + + ; characters + .CR = 0x0A + .SPACE = 0x00 + ;; Global variables .globl .mode @@ -209,6 +222,20 @@ lbl: out (#.VDP_CMD), a .endm +.macro VDP_DELAY ?lbl + nop + jr lbl +lbl: +.endm + +.macro WRITE_VDP_CMD_HL + rst 0x10 +.endm + +.macro WRITE_VDP_DATA_HL + rst 0x20 +.endm + .macro CALL_HL rst 0x30 .endm diff --git a/gbdk-lib/libc/targets/z80/gg/palette.s b/gbdk-lib/libc/targets/z80/gg/palette.s @@ -88,10 +88,10 @@ _set_palette:: jp (hl) .CRT_DEFAULT_PALETTE:: - .dw 0b0000000000000000 - .dw 0b0000010001000100 - .dw 0b0000100010001000 .dw 0b0000111111111111 + .dw 0b0000100010001000 + .dw 0b0000010001000100 + .dw 0b0000000000000000 .dw 0b0000000000001000 .dw 0b0000000010000000 .dw 0b0000100000000000 diff --git a/gbdk-lib/libc/targets/z80/gotoxy.s b/gbdk-lib/libc/targets/z80/gotoxy.s @@ -0,0 +1,34 @@ + .include "global.s" + + .title "console utilities" + .module ConsoleUtils + + .globl .curx, .cury + + .area _HOME + +_gotoxy:: + pop hl + pop de + push de + push hl + + ld a, #.SCREEN_X_OFS + add e + ld (.curx), a + ld a, #.SCREEN_Y_OFS + add d + ld (.cury), a + ret + +_posx:: + ld a, (.curx) + sub #.SCREEN_X_OFS + ld l, a + ret + +_posy:: + ld a, (.cury) + sub #.SCREEN_Y_OFS + ld l, a + 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, a + ld e, l ld hl, (___current_metasprite) diff --git a/gbdk-lib/libc/targets/z80/putchar.s b/gbdk-lib/libc/targets/z80/putchar.s @@ -0,0 +1,125 @@ + .include "global.s" + + .title "putchar" + .module putchar + + .globl font_current, font_set, font_load_ibm + .globl _scroll_viewport + + ; 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 + + .area _INITIALIZED + ; The current font +.curx:: + .ds 1 +.cury:: + .ds 1 + + .area _INITIALIZER + .db #.SCREEN_X_OFS ; .curx + .db #.SCREEN_Y_OFS ; .cury + + .area _HOME + +_setchar:: +_putchar:: + pop hl + pop de + push de + push hl + + cp #.CR + jr nz, 0$ + + ld a, #.SCREEN_X_OFS + ld (.curx), a + jp 2$ +0$: + ld hl, (font_current+sfont_handle_font) + ld a, h + or l + jr nz, 6$ + + push de + call font_load_ibm + ld a, h + or l + ret z + call font_set + pop de + ld hl, (font_current+sfont_handle_font) +6$: + 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 + WRITE_VDP_CMD_HL + + ld a, e + out (.VDP_DATA), a + VDP_DELAY + xor a + out (.VDP_DATA), a + + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT + + ld a, (.curx) + inc a + cp #(.SCREEN_X_OFS + .SCREEN_WIDTH) + jr c, 5$ + ld a, #.SCREEN_X_OFS +5$: + ld (.curx), a + ret nz +2$: + ld a, (.cury) + inc a + cp #(.SCREEN_Y_OFS + .SCREEN_HEIGHT) + jr c, 3$ + ld a, #(.SCREEN_Y_OFS + .SCREEN_HEIGHT - 1) + ld (.cury), a + + call _scroll_viewport + ret +3$: + ld (.cury), a + ret diff --git a/gbdk-lib/libc/targets/z80/scroll.s b/gbdk-lib/libc/targets/z80/scroll.s @@ -0,0 +1,64 @@ + .include "global.s" + + .title "console utilities" + .module ConsoleUtils + + .globl .curx, .cury + + .area _HOME + +_scroll_viewport:: + DISABLE_VBLANK_COPY ; switch OFF copy shadow SAT + ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS + 1) * .VDP_MAP_WIDTH * 2)) + + ld bc, #((.SCREEN_HEIGHT - 1) * .VDP_MAP_WIDTH * 2) + inc b + inc c + jr 1$ +2$: + res 6, h + WRITE_VDP_CMD_HL + in a, (.VDP_DATA) + ld e, a + + ld a, l + sub #(.VDP_MAP_WIDTH * 2) + ld l, a + ld a, h + sbc #0 + ld h, a + + set 6, h + WRITE_VDP_CMD_HL + ld a, e + out (.VDP_DATA), a + + ld a, #((.VDP_MAP_WIDTH * 2) + 1) + add l + ld l, a + adc h + sub l + ld h, a + +1$: + dec c + jr nz, 2$ + dec b + jr nz, 2$ + + ld hl, #(.VDP_TILEMAP + ((.SCREEN_Y_OFS + .SCREEN_HEIGHT - 1) * .VDP_MAP_WIDTH * 2)) + WRITE_VDP_CMD_HL + + ld c, #.VDP_MAP_WIDTH +3$: + ld a, #.SPACE + out (.VDP_DATA), a + VDP_DELAY + xor a + out (.VDP_DATA), a + + dec c + jr nz, 3$ + + ENABLE_VBLANK_COPY ; switch ON copy shadow SAT + ret + diff --git a/gbdk-lib/libc/targets/z80/sms/Makefile b/gbdk-lib/libc/targets/z80/sms/Makefile @@ -13,6 +13,8 @@ ASSRC = outi.s vmemcpy.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 \ + putchar.s \ + scroll.s cls.s gotoxy.s \ palette.s \ pad.s pad_ex.s \ int.s nmi.s \ diff --git a/gbdk-lib/libc/targets/z80/sms/global.s b/gbdk-lib/libc/targets/z80/sms/global.s @@ -168,6 +168,19 @@ .M_NO_SCROLL = 0x04 ; Disables scrolling of the screen in text mode .M_NO_INTERP = 0x08 ; Disables special character interpretation + ;; Screen dimentions in tiles + + .SCREEN_X_OFS = 0 + .SCREEN_Y_OFS = 0 + .SCREEN_WIDTH = 32 + .SCREEN_HEIGHT = 24 + .VDP_MAP_HEIGHT = 28 + .VDP_MAP_WIDTH = 32 + + ; characters + .CR = 0x0A + .SPACE = 0x00 + ;; Global variables .globl .mode @@ -209,6 +222,20 @@ lbl: out (#.VDP_CMD), a .endm +.macro VDP_DELAY ?lbl + nop + jr lbl +lbl: +.endm + +.macro WRITE_VDP_CMD_HL + rst 0x10 +.endm + +.macro WRITE_VDP_DATA_HL + rst 0x20 +.endm + .macro CALL_HL rst 0x30 .endm diff --git a/gbdk-lib/libc/targets/z80/sms/palette.s b/gbdk-lib/libc/targets/z80/sms/palette.s @@ -84,10 +84,10 @@ _set_palette:: jp (hl) .CRT_DEFAULT_PALETTE:: - .db 0b00000000 - .db 0b00010101 - .db 0b00101010 .db 0b00111111 + .db 0b00101010 + .db 0b00010101 + .db 0b00000000 .db 0b00000010 .db 0b00001000 .db 0b00100000
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.