gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 42780fe0912932b6c62174afa67f75f8a06c559f parent 340ecd63042ee1483cf5424f5715b3be0c4ace68 Author: Toxa <untoxa@mail.ru> Date: Wed, 10 Feb 2021 13:22:27 +0300 set_bkg_tile_xy()/set_win_tile_xy + example Diffstat:
| A | gbdk-lib/examples/gb/scroller/Makefile | 17 | +++++++++++++++++ |
| A | gbdk-lib/examples/gb/scroller/text_scroller.c | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | gbdk-lib/include/gb/gb.h | 41 | +++++++++++++++++++++++++++++++++++++++++ |
| M | gbdk-lib/libc/gb/Makefile | 3 | ++- |
| A | gbdk-lib/libc/gb/get_addr.s | 20 | ++++++++++++++++++++ |
| A | gbdk-lib/libc/gb/set_tile.s | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 files changed, 211 insertions(+), 1 deletion(-)
diff --git a/gbdk-lib/examples/gb/scroller/Makefile b/gbdk-lib/examples/gb/scroller/Makefile @@ -0,0 +1,17 @@ +CC = ../../../bin/lcc -Wa-l -Wl-m -Wl-j + +BINS = text_scroller.gb + +all: $(BINS) + +make.bat: Makefile + @echo "REM Automatically generated from Makefile" > make.bat + @make -sn | sed y/\\//\\\\/ | grep -v make >> make.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/gb/scroller/text_scroller.c b/gbdk-lib/examples/gb/scroller/text_scroller.c @@ -0,0 +1,70 @@ +#include <gb/gb.h> +#include <stdio.h> + +#include <gb/bgb_emu.h> + +const UBYTE scanline_offsets_tbl[] = {0, 1, 2, 3, 3, 2, 1, 0, 0, 1, 2, 3, 3, 2, 1, 0}; +const UBYTE * scanline_offsets = scanline_offsets_tbl; + +#define SCROLL_POS 15 +#define SCROLL_POS_PIX_START (SCROLL_POS * 8) - 1 +#define SCROLL_POS_PIX_END ((SCROLL_POS + 1) * 8) - 1 + +UBYTE scroller_x = 0; + +void scanline_isr() { + switch (LYC_REG) { + case 0: + SCX_REG = 0; + LYC_REG = SCROLL_POS_PIX_START; + break; + case SCROLL_POS_PIX_START: + SCX_REG = scroller_x; + LYC_REG = SCROLL_POS_PIX_END; + break; + case SCROLL_POS_PIX_END: + SCX_REG = LYC_REG = 0; + break; + } +} + +const UBYTE scroller_text[] = "This is a text scroller demo for GBDK-2020. You can use ideas, that are "\ +"shown in this demo, to make different parallax effects, scrolling of tilemaps which are larger than 32x32 "\ +"tiles and TEXT SCROLLERS, of course! Need to write something else to make this text longer than 256 characters. "\ +"The quick red fox jumps over the lazy brown dog. 0123456789. "; + +const UBYTE * scroller_next_char = scroller_text; +UBYTE * scroller_vram_addr; +UWORD base, limit; + +void main() { + printf("Scrolling %d chars", sizeof(scroller_text) - 1); + + CRITICAL { + STAT_REG |= 0b01000000; LYC_REG = 0; + add_LCD(scanline_isr); + set_interrupts(VBL_IFLAG | LCD_IFLAG); + } + + scroller_vram_addr = set_bkg_tile_xy(20, SCROLL_POS, *scroller_next_char - 0x20); + + base = (UWORD) scroller_vram_addr & 0xffe0; + limit = base + 0x20; + + while (1) { + scroller_x++; + if ((scroller_x & 0x07) == 0) { + // next letter + scroller_next_char++; + if (*scroller_next_char == 0) scroller_next_char = scroller_text; + + // next vram position + scroller_vram_addr++; + if (scroller_vram_addr == (UBYTE *)limit) scroller_vram_addr = (UBYTE *)base; + + // put next char + set_vram_byte(scroller_vram_addr, *scroller_next_char - 0x20); + } + wait_vbl_done(); + } +} + diff --git a/gbdk-lib/include/gb/gb.h b/gbdk-lib/include/gb/gb.h @@ -609,6 +609,21 @@ void hiramcpy(UINT8 dst, +/** + * Set byte in vram at given memory location + * + * @param addr address to write to + * @param v value + */ +void set_vram_byte(UBYTE * addr, UINT8 v) __preserves_regs(b, c); + + +/** + * Get base address of background map + */ +UINT8 * get_bkg_map_addr() __preserves_regs(b, c, h, l); + + /** Sets VRAM Tile Pattern data for the Background / Window @param first_tile Index of the first tile to write @@ -744,6 +759,17 @@ void get_bkg_tiles(UINT8 x, unsigned char *tiles) NONBANKED __preserves_regs(b, c); +/** + * Set single tile t on background layer at x,y + * @param x X-coordinate + * @param y Y-coordinate + * @param t tile index + * @return returns the address of tile, so you may use faster set_vram_byte() later + */ +UINT8 * set_bkg_tile_xy(UBYTE x, UBYTE y, UBYTE t) __preserves_regs(b, c); + + + /** Moves the Background Layer to the position specified in __x__ and __y__ in pixels. @param x X axis screen coordinate for Left edge of the Background @@ -777,6 +803,11 @@ inline void scroll_bkg(INT8 x, INT8 y) { +/** + * Get base address of window map + */ +UINT8 * get_win_map_addr() __preserves_regs(b, c, h, l); + /** Sets VRAM Tile Pattern data for the Window / Background @param first_tile Index of the first tile to write @@ -878,6 +909,16 @@ void get_win_tiles(UINT8 x, unsigned char *tiles) NONBANKED __preserves_regs(b, c); +/** + * Set single tile t on window layer at x,y + * @param x X-coordinate + * @param y Y-coordinate + * @param t tile index + * @return returns the address of tile, so you may use faster set_vram_byte() later + */ +UINT8 * set_win_tile_xy(UBYTE x, UBYTE y, UBYTE t) __preserves_regs(b, c); + + /** Moves the Window to the __x__, __y__ position on the screen. @param x X coordinate for Left edge of the Window (actual displayed location will be X - 7) diff --git a/gbdk-lib/libc/gb/Makefile b/gbdk-lib/libc/gb/Makefile @@ -12,9 +12,10 @@ ASSRC = cgb.s cpy_data.s \ f_ibm_full.s f_ibm_sh.s f_italic.s f_min.s f_spect.s \ get_bk_t.s get_data.s \ get_wi_t.s get_xy_t.s global.s \ + get_addr.s \ hiramcpy.s init_tt.s input.s \ pad.s sample.s \ - serial.s set_bk_t.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 \ diff --git a/gbdk-lib/libc/gb/get_addr.s b/gbdk-lib/libc/gb/get_addr.s @@ -0,0 +1,20 @@ +.include "global.s" + +.area _CODE + +_get_win_map_addr:: + ldh a,(.LCDC) + bit 6,a + jr z,.is98 + jr .is9c + +_get_bkg_map_addr:: + ldh a,(.LCDC) + bit 3,a + jr nz,.is9c +.is98: + ld de,#0x9800 ; DE = origin + ret +.is9c: + ld de,#0x9C00 ; DE = origin + ret diff --git a/gbdk-lib/libc/gb/set_tile.s b/gbdk-lib/libc/gb/set_tile.s @@ -0,0 +1,60 @@ +.include "global.s" + +.area _CODE + +_set_vram_byte:: + ; de = addr, hl = &t + ldhl sp,#2 + ld a,(hl+) + ld e, a + ld a,(hl+) + ld d, a + + WAIT_STAT + + ; Write tile + ld a,(hl) + ld (de),a + ret + +_set_win_tile_xy:: + ldh a,(.LCDC) + bit 6,a + jr z,.is98 + jr .is9c +_set_bkg_tile_xy:: + ldh a,(.LCDC) + bit 3,a + jr nz,.is9c +.is98: + ld de,#0x9800 ; DE = origin + jr .set_tile_xy +.is9c: + ld de,#0x9C00 ; DE = origin + +.set_tile_xy: + push bc + ldhl sp,#4 + + ld a, (hl+) + ld b, a + ld a, (hl+) + + ld e, d + swap a + rlca + ld c, a + and #0x03 + add e + ld d, a + ld a, #0xE0 + and c + add b + ld e, a ; dest DE = BASE + 0x20 * Y + X + + WAIT_STAT + ld a, (hl) + ld (de), a + + pop bc + ret
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.