This repository has been archived by the owner on Oct 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello.asm
125 lines (101 loc) · 3.34 KB
/
hello.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
; -----------------------------------------------------------------------------
; Example: Hello world - DMG ver.
; -----------------------------------------------------------------------------
; Font comes from ZX Spectrum - https://en.wikipedia.org/wiki/ZX_Spectrum_character_set
; More examples by tmk @ https://github.com/gitendo/helloworld
; -----------------------------------------------------------------------------
INCLUDE "hardware.inc" ; system defines
SECTION "Start",ROM0[$100] ; start vector, followed by header data applied by rgbfix.exe
nop
jp start
SECTION "Example",ROM0[$150] ; code starts here
start:
di ; disable interrupts
ld sp,$E000 ; setup stack
.wait_vbl ; wait for vblank to properly disable lcd
ld a,[rLY]
cp $90
jr nz,.wait_vbl
xor a
ld [rIF],a ; reset important registers
ld [rLCDC],a
ld [rSTAT],a
ld [rSCX],a
ld [rSCY],a
ld [rLYC],a
ld [rIE],a
ld hl,_RAM ; clear ram (fill with a which is 0 here)
ld bc,$2000-2 ; watch out for stack ;)
call fill
ld hl,_HRAM ; clear hram
ld c,$80 ; a = 0, b = 0 here, so let's save a byte and 4 cycles (ld c,$80 - 2/8 vs ld bc,$80 - 3/12)
call fill
ld hl,_VRAM ; clear vram
ld b,$18 ; a = 0, bc should be $1800; c = 0 here, so..
call fill
ld a,$20 ; ascii code for 'space' character
; no need to setup hl since _SCRN0 ($9800) and _SCRN1 ($9C00) are part of _VRAM, just continue
ld b,8 ; bc should be $800 (_SCRN0/1 are 32*32 bytes); c = 0 here, so..
call fill
ld a,%10010011 ; bits: 7-6 = 1st color, 5-4 = 2nd, 3-2 = 3rd and 1-0 = 4th color
; color values: 00 - light, 01 - gray, 10 - dark gray, 11 - dark
ld [rBGP],a ; bg palette
ld [rOBP0],a ; obj palettes (not used in this example)
ld [rOBP1],a
ld hl,font ; font data
ld de,_VRAM+$200 ; place it here to get ascii mapping ('space' code is $20, tile size $10)
ld bc,1776 ; font_8x8.chr file size
call copy
ld hl,text ; hello message
ld de,_SCRN0+$100 ; center it a bit
ld c,text_end-text ; b = 0, our string = 18 chars, so..
call copy ; lcdc is disabled so you have 'easy' access to vram
ld a,LCDCF_ON | LCDCF_BG8000 | LCDCF_BG9800 | LCDCF_OBJ8 | LCDCF_OBJOFF | LCDCF_WINOFF | LCDCF_BGON
; lcd setup: tiles at $8000, map at $9800, 8x8 sprites (disabled), no window, etc.
ld [rLCDC],a ; enable lcd
.the_end
halt ; save battery
; nop ; nop after halt is mandatory but rgbasm takes care of it :)
jr .the_end ; endless loop
;-------------------------------------------------------------------------------
copy:
;-------------------------------------------------------------------------------
; hl - source address
; de - destination
; bc - size
inc b
inc c
jr .skip
.copy
ld a,[hl+]
ld [de],a
inc de
.skip
dec c
jr nz,.copy
dec b
jr nz,.copy
ret
;-------------------------------------------------------------------------------
fill:
;-------------------------------------------------------------------------------
; a - byte to fill with
; hl - destination address
; bc - size of area to fill
inc b
inc c
jr .skip
.fill
ld [hl+],a
.skip
dec c
jr nz,.fill
dec b
jr nz,.fill
ret
;-------------------------------------------------------------------------------
font:
INCBIN "font_8x8.chr" ; converted with https://github.com/gitendo/bmp2cgb
text:
DB " Hello 8-bit world! "
text_end: