-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfixp.asm
79 lines (75 loc) · 1.17 KB
/
fixp.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
; Convert floating point between -1.0 and +1.0 to signed integer between -127 and 127
; WARNING!!! Bounds not checked, does wreak havoc for out of bounds input.
; Input: DE = floating point number in the -1.0 .. +1.0 range
; Output: A = signed integer
; Pollutes: F,AF',BC,DE,HL
FFIX: LD BC,F7F
CALL FMUL
LD A,H
ADD A,A
EX AF,AF' ; Save sign to F'
RES 7,H
LD A,$47
SUB H
CP 8
JR NC,FFIX0
LD B,A
LD A,L
SCF
FFIXL: RRA
OR A
DJNZ FFIXL
RRA
ADC A,B ; Proper rounding
EX AF,AF'
JR NC,FFIXP
EX AF,AF'
NEG
RET
FFIZP: EX AF,AF'
RET
FFIX0: XOR A
RET
; Convert floating point array as above
; In: HL = floating point array, DE = fixed point array, B = array size
; Pollutes: AF,AF',B,BC',DE,DE',HL,HL'
FFIXA: LD A,(HL)
INC HL
EX AF,AF'
LD A,(HL)
INC HL
EXX
LD D,A
EX AF,AF'
LD E,A
CALL FFIX
EXX
LD (DE),A
INC DE
DJNZ FFIXA
RET
; Multiply two signed integers to be interpreted as above
; In: D,E = multiplicands
; Out: A = product
; Pollutes: F, HL
MULFIX: LD H,SIGSQRT/256
LD A,E
ADD A,D
JP PO,MULFIX1
LD L,A
LD A,(HL)
LD L,E
SUB A,(HL)
LD L,D
SUB A,(HL)
RET
MULFIX1:LD A,E
SUB A,D
LD L,A
LD A,(HL)
NEG
LD L,E
ADD A,(HL)
LD L,D
ADD A,(HL)
RET