View difference between Paste ID: ivcgVp9a and 3890h7HF
SHOW: | | - or go back to the newest paste.
1
Project:
2
; hl is Z, de is Y, bc is X
3
; assume hl is stricly positive
4
; 265 bytes, +-600 TStates depending the input
5
	add	hl, hl
6
; return if neg
7
	ret	c
8
	ld	a, h
9
	or	a
10
	jr	z, _scale_up
11
_scale_down_loop:
12
	srl	a
13
	rr	l
14
	sra	d
15
	rr	e
16
	sra	b
17
	rr	c
18
	or	a
19
	jp	nz, _scale_down_loop
20
_scale_up:
21
; l, e, c are significant
22
	ld	a, l
23
	or	a
24
	ret	z
25
	cp	98
26
	jp	z, _no_bissect
27
	jr	nc, _scale_done
28
_scale_up_loop:
29
	sla	e
30
	sla	c
31
	add	a, a
32
	cp	98
33
	jp	c, _scale_up_loop
34
_scale_done:
35
	rra
36
	ld	l, a
37
; l=Z, e=X, c=Y
38
	xor	a
39
; set a to 0 and reset carry flag
40
	ld	h, a
41
	ld	d, a
42
	ld	b, a
43
; first optimized iters (h,d,b are 0)
44
gBissectFirstIter:
45
	ld	a, l
46
	rra
47
	cp	49
48
; calculate midpoint and classify
49
	jr	nc, $+16
50
; store to h	
51
	ld	h, a
52
	ld	a, e
53
	inc	a
54
	sra	a
55
	ld	d, a
56
	ld	a, c
57
	inc	a
58
	sra	a
59
	ld	b, a
60
	jp	$+10
61
; store it to l
62
	ld	l, a
63
	inc	e
64
	sra	e
65
	inc	c
66
	sra	c
67
; the bissection macro, unrolled to gain speed (and because we need all register)
68
#macro	gBissectIter()
69
	ld	a, l	
70
	add	a, h
71
	rra
72
	cp	49
73
; calculate midpoint and classify
74
	jr	nc, $+18
75
; store to h	
76
	ld	h, a
77
	ld	a, e
78
	inc	a
79
	add	a, d
80
	sra	a
81
	ld	d, a
82
	ld	a, c
83
	inc	a
84
	add	a, b
85
	sra	a
86
	ld	b, a
87
	jp	$+16
88
; store it to l
89
	ld	l, a
90
	ld	a, e
91
	inc	a
92
	add	a, d
93
	sra	a
94
	ld	e, a
95
	ld	a, c
96
	inc	a
97
	add	a, b
98
	sra	a
99
	ld	c, a
100
#endmacro
101
; 4 more iters to match precision
102
gBissectIter()
103
gBissectIter()
104
gBissectIter()
105
gBissectIter()
106
; finish:
107
	ld	a, e
108
	add	a, d
109-
; note : rra works because both d and e are either positive (no carry) or negative (carry set)
109+
; note : sra must be used...
110
	sra	a
111
	adc	a, 48
112
; or whatever
113
	ld	(gScreenX), a
114
	ld	a, c
115
	add	a, b
116
	sra	a
117
	ld	c, a
118
	ld	a, 32
119
	sbc	a, c
120
	ld	(gScreenY), a
121
	ret
122
_no_bissect:
123
	ld	a, e
124
	add	a, 48
125
	ld	(gScreenX), a
126
	ld	a, 32
127
	sub	c
128
	ld	(gScreenY), a
129
	ret