-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.inc
144 lines (133 loc) · 3.84 KB
/
utility.inc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
; Miscellaneuous utility functions
; Copyright 2018-2024 J.B. Langston
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
; jumps to target label if the specified value is in A
; can be used to build a concise dispatch routine...
;
; Dispatch:
; JumpIf 0, Target0
; JumpIf 1, Target1
; ; ...
; ret
macro JumpIf value, target
cp value
jp z, target
endmacro
; CharOut must be defined elsewhere for HexByte and HexBytes to work
; Output a byte in hex
; A = byte to output
HexByte:
push af
rra
rra
rra
rra
call HexNybble
call CharOut
pop af
call HexNybble
jp CharOut
; Output byte buffer in hex
; HL = address of byte buffer
; B = length of byte buffer
HexBytes:
ld a, (hl)
call HexByte
call Space
inc hl
djnz HexBytes
ret
; convert lower nybble of A to hex (also in A)
; from http://map.grauw.nl/sources/external/z80bits.html#5.1
HexNybble:
or 0f0h
daa
add a, 0a0h
adc a, 40h
ret
; RandomSeed sets the seed from four bytes in screen buffer data offset by refresh register.
RandomSeed:
ld hl, ScreenBuffer
ld a, r
ld d, 0
ld e, a
add hl, de
ld b, 4
ld de, Seed1
RandomSeedLoop:
ld a, (hl)
xor l
ld (de), a
inc hl
inc de
djnz RandomSeedLoop
ret
; RandomNumber generates a random number using combined LFSR/LCG PRNG with 16-bit seeds
; https://wikiti.brandonw.net/index.php?title=Z80_Routines:Math:Random
RandomNumber:
ld hl, (Seed1)
ld b, h
ld c, l
add hl, hl
add hl, hl
inc l
add hl, bc
ld (Seed1), hl
ld hl, (Seed2)
add hl, hl
sbc a, a
and %00101101
xor l
ld l, a
ld (Seed2), hl
add hl, bc
ret
Seed1:
DefPointer 0
Seed2:
DefPointer 0
; RandomSeries generates series of random numbers
; b = number of random numbers to generate
; c = mask for random numbers
; d = offset for random numbers
RandomSeries:
push bc
push hl
call RandomNumber
ld a, l
or a
pop hl
pop bc
call m, RandomNeg
call p, RandomPos
ld (hl), a
inc hl
djnz RandomSeries
ret
RandomPos:
and c
add a, d
ret
RandomNeg:
and c
add a, d
cpl
inc a
ret