Repository navigation
Suggest useful macros here! #2053
Rangi42
started this conversation in
Discussions
Replies: 8 comments 18 replies
|
Macros for working with the component bytes, nybbles, or crumbs of larger word or byte values: ; Load bytes
; `lb reg16, hi, lo` acts like `ld reg16, hi << 8 | lo`
MACRO? lb
check_truncation 8, \2, \3
ld \1, LOW(\2) << 8 | LOW(\3)
ENDM
; Load nybbles
; `ln reg8, hi, lo` acts like `ld reg8, hi << 4 | lo`
; `ln reg16, hi1, lo1, hi2, lo2` acts like `ld reg16, (hi1 << 4 | lo1) << 8 | (hi2 << 4 | lo2)`
MACRO? ln
if _NARG == 3
check_truncation 4, \2, \3
ld \1, ((\2) & $f) << 4 | ((\3) & $f)
else
check_truncation 4, \2, \3, \4, \5
ld \1, (((\2) & $f) << 4 | ((\3) & $f)) << 8 | ((\4) & $f) << 4 | ((\5) & $f)
endc
ENDM; Define nybbles
; `dn hi, lo, ...` acts like `db hi << 4 | lo, ...`
MACRO? dn
rept? _NARG / 2
check_truncation 4, \1, \2
db (\1) << 4 | (\2)
shift 2
endr
ENDM
; Define crumbs
; `dc c1, c2, c3, c4, ...` acts like `db c1 << 6 | c2 << 4 | c3 << 2 | c1, ...`
MACRO? dc ; "crumbs"
rept? _NARG / 4
check_truncation 2, \1, \2, \3, \4
db ((\1) << 6) | ((\2) << 4) | ((\3) << 2) | (\4)
shift 4
endr
ENDMThose use a MACRO? check_truncation
DEF check_truncation_size = \1
shift
rept? _NARG
assert -2**(check_truncation_size - 1) <= (\1) && (\1) < 2**check_truncation_size, \
"Expression must be {d:check_truncation_size}-bit: \1"
shift
endr
ENDM |
1 reply
|
Defining bank and address data together: ; Define bank and address
; `dba label, ...` acts like `db BANK(label) :: dw label :: ...`
MACRO? dba
rept? _NARG
db BANK(\1)
dw \1
shift
endr
ENDM
; Define address and bank
; `dab label, ...` acts like `dw label :: db BANK(label) :: ...`
MACRO? dab
rept? _NARG
dw \1
db BANK(\1)
shift
endr
ENDM |
0 replies
|
Use MACRO? jmp
jp \#
; Sign-extend a 16-bit address difference to the 32-bit arithmetic space;
; this handles jumps which overflow/underflow the HRAM/ROM0 boundary.
assert warn, \
((((\<_NARG>) - @ + $8000) & $ffff) - $8000) > 127 || \
((((\<_NARG>) - @ + $8000) & $ffff) - $8000) < -129, \
"jp can be jr"
ENDMNote the use of |
12 replies
|
Defining Game Boy RGB555 colors: ; Define RGB555 colors
; `RGB r, g, b, ...` acts like `dw b << 10 | g << 5 | r, ...`
MACRO? RGB
rept? _NARG / 3
check_rgb \1, \2, \3
dw (\3) << 10 | (\2) << 5 | (\1)
shift 3
endr
ENDM
MACRO? check_rgb
rept? _NARG
assert 0 <= (\1) && (\1) < 32, "Expression must be unsigned 5-bit: \1"
shift
endr
ENDM |
0 replies
|
We use ; GBC palette entry
; usage:
; rgb #F8F888, #000000, #10A840, #F8B888
; outputs:
; db $FF, $47, $00, $00, $A2, $22, $FF, $46
MACRO? rgb
rept? _NARG
REDEF rgb_eval EQUS STRRPL("\1", "#", "$")
REDEF rgb_arg EQU {rgb_eval}
REDEF rgb_red EQU ((rgb_arg & $FF0000) >> 16) / 8
REDEF rgb_grn EQU ((rgb_arg & $00FF00) >> 8) / 8
REDEF rgb_blu EQU ((rgb_arg & $0000FF) >> 0) / 8
dw rgb_red | rgb_grn << 5 | rgb_blu << 10
shift
endr
ENDM |
3 replies
|
To ensure an exact address without creating a whole new MACRO? org
align 16, \1
ENDM |
1 reply
|
A complete ASCII charmap: pushc
newcharmap ascii
DEF PRINTABLE_ASCII EQUS " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz\{|\}~"
for i, STRLEN(#PRINTABLE_ASCII)
charmap STRSLICE(#PRINTABLE_ASCII, i, i + 1), i + $20
endr
charmap "\0", $00
charmap "\t", $09
charmap "\n", $0a
charmap "\r", $0d
popc |
0 replies
|
Save a couple lines of MACRO? withcharmap
pushc \1
shift
\#
popc
ENDM
withcharmap utf8, ld a, CHARLEN("火")MACRO? withopt
pusho \1
shift
\#
popo
ENDM
withopt Wno-assert, assert warn, 2 + 2 == 5 |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Sometimes someone will ask about a missing language feature, and it's not built-in because it can be done in just a few lines as a macro. There are certain macros that have become common across projects. We're interested in some kind of "standard library" of RGBASM macros, sort of like the gb-boilerplate project but more focused. Even aside from that, this discussion can be considered as a collection to manually copy from and add to.
Note that it can be useful to silence simple macros by writing "
MACRO?" (as well as "rept?" or "for?" if they use internal loops). That way when the user gets an error, the location they see in the error message will be where they used the macro, not wherever the failingassertis (since it's the usage site that they need to edit to fix the error).All reactions