2017-12-13 21:36:24 -08:00
|
|
|
; Syntactic sugar macros
|
|
|
|
|
|
|
|
lb: MACRO ; r, hi, lo
|
2018-07-08 13:29:46 -07:00
|
|
|
ld \1, ((\2) & $ff) << 8 | ((\3) & $ff)
|
2017-12-28 13:31:16 -08:00
|
|
|
ENDM
|
2017-12-13 21:36:24 -08:00
|
|
|
|
|
|
|
ln: MACRO ; r, hi, lo
|
2018-07-08 13:29:46 -07:00
|
|
|
ld \1, ((\2) & $f) << 4 | ((\3) & $f)
|
2017-12-28 13:31:16 -08:00
|
|
|
ENDM
|
2017-12-13 21:36:24 -08:00
|
|
|
|
|
|
|
; Design patterns
|
|
|
|
|
|
|
|
jumptable: MACRO
|
2020-07-02 08:46:30 -07:00
|
|
|
if !STRCMP("\2", "hl")
|
2020-07-01 10:13:49 -07:00
|
|
|
ld a, [hl]
|
|
|
|
else
|
2017-12-13 21:36:24 -08:00
|
|
|
ld a, [\2]
|
2020-07-01 10:13:49 -07:00
|
|
|
endc
|
2017-12-13 21:36:24 -08:00
|
|
|
ld e, a
|
|
|
|
ld d, 0
|
|
|
|
ld hl, \1
|
|
|
|
add hl, de
|
|
|
|
add hl, de
|
|
|
|
ld a, [hli]
|
|
|
|
ld h, [hl]
|
|
|
|
ld l, a
|
|
|
|
jp hl
|
2017-12-28 13:31:16 -08:00
|
|
|
ENDM
|
2017-12-13 21:36:24 -08:00
|
|
|
|
2017-12-28 04:23:44 -08:00
|
|
|
maskbits: MACRO
|
2018-05-23 09:48:14 -07:00
|
|
|
; masks just enough bits to cover the first argument
|
|
|
|
; the second argument is an optional shift amount
|
2018-01-16 14:27:50 -08:00
|
|
|
; e.g. "maskbits 26" becomes "and %00011111" (since 26 - 1 = %00011001)
|
2018-05-23 09:48:14 -07:00
|
|
|
; and "maskbits 3, 2" becomes "and %00001100" (since "maskbits 3" becomes %00000011)
|
2017-12-13 21:36:24 -08:00
|
|
|
; example usage in rejection sampling:
|
|
|
|
; .loop
|
|
|
|
; call Random
|
2018-02-03 10:26:34 -08:00
|
|
|
; maskbits 26
|
|
|
|
; cp 26
|
2017-12-13 21:36:24 -08:00
|
|
|
; jr nc, .loop
|
2020-10-26 19:24:38 -07:00
|
|
|
assert 0 < (\1) && (\1) <= $100, "bitmask must be 8-bit"
|
2017-12-13 21:36:24 -08:00
|
|
|
x = 1
|
|
|
|
rept 8
|
2018-01-16 14:27:50 -08:00
|
|
|
if x + 1 < (\1)
|
|
|
|
x = x << 1 | 1
|
2017-12-24 09:47:30 -08:00
|
|
|
endc
|
2017-12-13 21:36:24 -08:00
|
|
|
endr
|
2018-05-23 09:48:14 -07:00
|
|
|
if _NARG == 2
|
|
|
|
and x << (\2)
|
|
|
|
else
|
2017-12-13 21:36:24 -08:00
|
|
|
and x
|
2018-05-23 09:48:14 -07:00
|
|
|
endc
|
2017-12-28 13:31:16 -08:00
|
|
|
ENDM
|
2018-02-03 18:11:55 -08:00
|
|
|
|
|
|
|
calc_sine_wave: MACRO
|
|
|
|
; input: a = a signed 6-bit value
|
|
|
|
; output: a = d * sin(a * pi/32)
|
|
|
|
and %111111
|
2020-04-23 13:10:46 -07:00
|
|
|
cp %100000
|
2018-02-03 18:11:55 -08:00
|
|
|
jr nc, .negative\@
|
|
|
|
call .apply\@
|
|
|
|
ld a, h
|
|
|
|
ret
|
|
|
|
.negative\@
|
|
|
|
and %011111
|
|
|
|
call .apply\@
|
|
|
|
ld a, h
|
|
|
|
xor $ff
|
|
|
|
inc a
|
|
|
|
ret
|
|
|
|
.apply\@
|
|
|
|
ld e, a
|
|
|
|
ld a, d
|
|
|
|
ld d, 0
|
|
|
|
if _NARG == 1
|
|
|
|
ld hl, \1
|
|
|
|
else
|
|
|
|
ld hl, .sinetable\@
|
|
|
|
endc
|
|
|
|
add hl, de
|
|
|
|
add hl, de
|
|
|
|
ld e, [hl]
|
|
|
|
inc hl
|
|
|
|
ld d, [hl]
|
|
|
|
ld hl, 0
|
|
|
|
.multiply\@ ; factor amplitude
|
|
|
|
srl a
|
|
|
|
jr nc, .even\@
|
|
|
|
add hl, de
|
|
|
|
.even\@
|
|
|
|
sla e
|
|
|
|
rl d
|
|
|
|
and a
|
|
|
|
jr nz, .multiply\@
|
|
|
|
ret
|
|
|
|
if _NARG == 0
|
|
|
|
.sinetable\@
|
|
|
|
sine_table 32
|
|
|
|
endc
|
|
|
|
ENDM
|