1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2026-03-10 21:25:31 +00:00

gb: hello world based on GingerBread library

This commit is contained in:
Steven Hugg
2026-03-07 15:27:20 +01:00
parent b62b15ce78
commit 4093bb9fa1
2 changed files with 1373 additions and 102 deletions

1268
presets/gb/gingerbread.sgb Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,116 +1,119 @@
; Game Boy Background Example in Z80 Assembly (sdasgb syntax)
; Hardware register definitions
.equ LCDC, 0xFF40 ; LCD Control register
.equ BGP, 0xFF47 ; Background palette register
.equ LY, 0xFF44 ; LCD Y coordinate register
.include "gingerbread.sgb"
; VRAM addresses
.equ VRAM_TILES, 0x9000 ; Tile data area
.equ VRAM_MAP, 0x9800 ; Background tile map
; Set the size of the ROM file here. 0 means 32 kB, 1 means 64 kB, 2 means 128 kB and so on.
ROM_SIZE = 1
; LCDC flags
.equ LCDC_ON, 0x80 ; LCD enable
.equ LCDC_BG_ON, 0x01 ; Background enable
; Set the size of save RAM inside the cartridge.
; If printed to real carts, it needs to be small enough to fit.
; 0 means no RAM, 1 means 2 kB, 2 -> 8 kB, 3 -> 32 kB, 4 -> 128 kB
RAM_SIZE = 1
.area _CODE
; Macro for mapping ASCII character to custom tile data
.macro DLTR ch
.db (ch)-0x41+0x14
.endm
.globl _main
SomeText:
; "HELLO WORLD" message
DLTR('H')
DLTR('E')
DLTR('L')
DLTR('L')
DLTR('O')
.db 0x00
DLTR('W')
DLTR('O')
DLTR('R')
DLTR('L')
DLTR('D')
.db 0x02 ; happy
.db 0x30 ; end
_main:
; Disable interrupts
di
; GingerBread assumes that the label "begin" is where the game should start
begin:
; Turn off LCD
ld a, #0x00
ldh (LCDC), a
ld hl, #hello_world_tile_data
ld de, #TILEDATA_START
ld bc, #hello_world_tile_data_size
call mCopyVRAM
ld b, #0x30 ; end character
ld c, #0 ; draw to background
ld d, #4 ; X start position (0-19)
ld e, #8 ; Y start position (0-17)
ld hl, #SomeText ; text to write
call RenderTextToEnd
; Load tile data into VRAM
; Copy tile data to VRAM tile 1 (tile 0 is blank by default)
ld hl, #tile_data
ld de, #(VRAM_TILES + 0x10) ; Tile 1 starts at offset 0x10
ld bc, #16 ; 16 bytes per tile
call memcpy
call StartLCD
; Set up background map
; Fill background map with tile index 1
ld hl, #VRAM_MAP
ld a, #0x01 ; Tile index 1
ld bc, #(20*18) ; 20x18 tiles = 360 tiles
fill_bg_loop:
ld (hl), a
inc hl
dec bc
ld a, b
or c
jr nz, fill_bg_loop
main:
halt
nop
; Set background palette
ld a, #0x12 ; 11 10 01 00 - darkest to lightest
ldh (BGP), a
; Turn on LCD with background enabled
ld a, #(LCDC_ON | LCDC_BG_ON)
ldh (LCDC), a
; Main loop
main_loop:
call wait_vblank
jr main_loop
jr main
; Wait for vertical blank
wait_vblank:
ldh a, (LY)
cp #144 ; VBlank starts at line 144
jr nz, wait_vblank
ret
; /////////////////
; // //
; // Constants //
; // //
; /////////////////
; Simple memory copy routine
; hl = source, de = destination, bc = count
memcpy:
ld a, (hl)
ld (de), a
inc hl
inc de
dec bc
ld a, b
or c
jr nz, memcpy
ret
hello_world_tile_data_size = 0x02E0
hello_world_tile_count = 0x2E
; Tile data
tile_data:
.db 1,2,4,8,0x10,0x20,0x40,0x80
.db 0x80,0x40,0x20,0x10,8,4,2,1
; /////////////////
; // //
; // Tile Data //
; // //
; /////////////////
; ROM header (required for Game Boy)
.area _HEADER (ABS)
.org 0x0100
nop
jp _main
.org 0x0104
.db 0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B
.db 0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D
.db 0x00, 0x08, 0x11, 0x1F, 0x88, 0x89, 0x00, 0x0E
.db 0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99
.db 0xBB, 0xBB, 0x67, 0x63, 0x6E, 0x0E, 0xEC, 0xCC
.db 0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E
.org 0x0134
.ascii "BGTEST" ; Title (11 bytes max)
.org 0x013F
.db 0x00 ; New licensee code
.org 0x0147
.db 0x00 ; Cartridge type (ROM only)
.db 0x00 ; ROM size (32KB)
.db 0x00 ; RAM size (none)
.db 0x01 ; Destination (non-Japanese)
.db 0x33 ; Old licensee code
.db 0x00 ; ROM version
.org 0x014D
.db 0x00 ; Header checksum (will be calculated)
.db 0x00, 0x00 ; Global checksum (will be calculated)
hello_world_tile_data:
;;{w:8,h:8,bpp:1,count:46,brev:1,np:2,pofs:1,sl:2};;
.db 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
.db 0x81,0x81,0x42,0x42,0x24,0x24,0x18,0x18,0x18,0x18,0x24,0x24,0x42,0x42,0x81,0x81
.db 0x00,0x00,0x24,0x24,0x00,0x00,0x00,0x00,0x24,0x24,0x18,0x18,0x00,0x00,0x00,0x00
.db 0x00,0x00,0x24,0x24,0x00,0x00,0x00,0x00,0x18,0x18,0x24,0x24,0x00,0x00,0x00,0x00
.db 0x00,0x00,0x36,0x36,0x49,0x49,0x41,0x41,0x22,0x22,0x14,0x14,0x08,0x08,0x00,0x00
.db 0x00,0x00,0x00,0x00,0x08,0x08,0x04,0x04,0x7E,0x7E,0x04,0x04,0x08,0x08,0x00,0x00
.db 0x00,0x00,0x00,0x00,0x10,0x10,0x20,0x20,0x7E,0x7E,0x20,0x20,0x10,0x10,0x00,0x00
.db 0x00,0x00,0x10,0x10,0x38,0x38,0x54,0x54,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00
.db 0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x54,0x54,0x38,0x38,0x10,0x10,0x00,0x00
.db 0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x7C,0x7C,0x10,0x10,0x10,0x10,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x1C,0x1C,0x00,0x00
.db 0x00,0x00,0x06,0x06,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x02,0x02,0x04,0x04,0x08,0x08,0x10,0x10,0x1E,0x1E,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x02,0x02,0x0C,0x0C,0x02,0x02,0x02,0x02,0x1C,0x1C,0x00,0x00
.db 0x00,0x00,0x12,0x12,0x12,0x12,0x1E,0x1E,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00
.db 0x00,0x00,0x1E,0x1E,0x10,0x10,0x1E,0x1E,0x02,0x02,0x02,0x02,0x1E,0x1E,0x00,0x00
.db 0x00,0x00,0x0C,0x0C,0x10,0x10,0x1C,0x1C,0x12,0x12,0x12,0x12,0x0C,0x0C,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x04,0x04,0x0E,0x0E,0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x22,0x22,0x1C,0x1C,0x22,0x22,0x22,0x22,0x1C,0x1C,0x00,0x00
.db 0x00,0x00,0x1E,0x1E,0x12,0x12,0x1E,0x1E,0x02,0x02,0x02,0x02,0x02,0x02,0x00,0x00
.db 0x00,0x00,0x18,0x18,0x24,0x24,0x3C,0x3C,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00
.db 0x00,0x00,0x38,0x38,0x24,0x24,0x38,0x38,0x24,0x24,0x24,0x24,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x1C,0x1C,0x00,0x00
.db 0x00,0x00,0x38,0x38,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x3C,0x3C,0x20,0x20,0x38,0x38,0x20,0x20,0x20,0x20,0x3C,0x3C,0x00,0x00
.db 0x00,0x00,0x3C,0x3C,0x20,0x20,0x38,0x38,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00
.db 0x00,0x00,0x18,0x18,0x20,0x20,0x2C,0x2C,0x24,0x24,0x24,0x24,0x18,0x18,0x00,0x00
.db 0x00,0x00,0x24,0x24,0x24,0x24,0x3C,0x3C,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00
.db 0x00,0x00,0x38,0x38,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x3C,0x3C,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x24,0x24,0x28,0x28,0x30,0x30,0x28,0x28,0x24,0x24,0x24,0x24,0x00,0x00
.db 0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x22,0x22,0x36,0x36,0x2A,0x2A,0x22,0x22,0x22,0x22,0x22,0x22,0x00,0x00
.db 0x00,0x00,0x24,0x24,0x34,0x34,0x2C,0x2C,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00
.db 0x00,0x00,0x18,0x18,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x18,0x18,0x00,0x00
.db 0x00,0x00,0x38,0x38,0x24,0x24,0x38,0x38,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00
.db 0x00,0x00,0x18,0x18,0x24,0x24,0x24,0x24,0x24,0x24,0x2C,0x2C,0x1C,0x1C,0x02,0x02
.db 0x00,0x00,0x38,0x38,0x24,0x24,0x38,0x38,0x24,0x24,0x24,0x24,0x24,0x24,0x00,0x00
.db 0x00,0x00,0x1C,0x1C,0x20,0x20,0x18,0x18,0x04,0x04,0x04,0x04,0x38,0x38,0x00,0x00
.db 0x00,0x00,0x3E,0x3E,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00
.db 0x00,0x00,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x18,0x18,0x00,0x00
.db 0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x14,0x14,0x08,0x08,0x00,0x00
.db 0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x2A,0x2A,0x14,0x14,0x00,0x00
.db 0x00,0x00,0x22,0x22,0x14,0x14,0x08,0x08,0x08,0x08,0x14,0x14,0x22,0x22,0x00,0x00
.db 0x00,0x00,0x22,0x22,0x14,0x14,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00
.db 0x00,0x00,0x3C,0x3C,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x3C,0x3C,0x00,0x00
;;