-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.asm
129 lines (114 loc) · 2.24 KB
/
maths.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
126
127
128
129
;----------------------------------------------
; MATHS ROUTINES
;==============================================
; Pseudo-random number generator - by Jon Ritman, Simon Brattel, Neil Mottershead
; We don't need to know anything here, just that it works and returns a random 16-bit number in HL.
maths.seed: dm "o}_/" ; Any seed will do
maths.rand:
ld hl,(maths.seed+2)
ld d,l
add hl,hl
add hl,hl
ld c,h
ld hl,(maths.seed)
ld b,h
rl b
ld e,h
rl e
rl d
add hl,bc
ld (maths.seed),hl
ld hl,(maths.seed+2)
adc hl,de
res 7,h
ld (maths.seed+2),hl
jp m,@+skip
ld hl,maths.seed
@loop:
inc (hl)
inc hl
jp z,@-loop
@skip:
ld hl,(maths.seed)
ret
maths.rand.safe:
push bc
push de
push hl
call maths.rand
pop hl
pop de
pop bc
ret
;==============================================
; Other maths routines taken from http://map.grauw.nl/sources/external/z80bits.html
; By Milos Bazelides.
maths.multDEBC:
; Return DE:HL = DE*BC
ld hl,0
sla e ; optimised 1st iteration
rl d
jp nc,$+5
ld h,b
ld l,c
@loop: equ for 14
add hl,hl
rl e
rl d
jp nc,$+8 ; Skip to next iteration if top bit of D not set
add hl,bc
jp nc,$+4
inc de ; Increment most significant word if carried
next @loop
@final_iteration:
add hl,hl
rl e
rl d
ret nc
add hl,bc
ret nc
inc de
ret
;----------------------------------------------
maths.multADE:
; Input: A = Multiplier, DE = Multiplicand, HL = 0, C = 0
; Output: A:HL = Product
ld hl,0
ld c,0
add a
jr nc,@+skip
ld h,d
ld l,e
@skip:
@loop: equ for 6
add hl,hl
rla
jr nc,$+4
add hl,de
adc c
next @loop
add hl,hl
rla
ret nc
add hl,de
adc c
ret
;----------------------------------------------
maths.divHLC:
; Return HL/C with HL = Quotient, A = Remainder
xor a
@loop: equ for 15
add hl,hl
rla
cp c
jp c,$+5
sub c
inc l
next @loop
@final_iteration:
add hl,hl
rla
cp c
ret c
inc l
ret