-
Notifications
You must be signed in to change notification settings - Fork 6
/
smult7.a
223 lines (200 loc) · 3.72 KB
/
smult7.a
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
; smult7.a
; from codebase64 by Oswald/Resource: https://www.codebase64.org/doku.php?id=base:fast_8bit_multiplication_16bit_product
; with BUG FIX by TobyLobster to handle -128 * -128
;
; 8 bit x 8 bit signed multiply, 16 bit result
; Average cycles: 88.50
; 1400 bytes
XTMP = $02 ;temporary for X reg
RL = $03 ;result lo
RH = $04 ;result hi
* = $0200
; Tables must be aligned with page boundary
SQRL
!for i, 0, 511 {
!byte <((i*i)/4)
}
SQRH
!for i, 0, 511 {
!byte >((i*i)/4)
}
ABS
!for i, 0, 255 {
!if (i < 128) {
!byte i
} else {
!byte 256 - i
}
}
; ------- MULTIPLY ----------------------
; 8x8bits -> 16 bits, signed input and output
; x*y -> y(hi) & x(lo)
;
; warning: there are quite a few undeclared
; zero page addresses used by the mulgen subroutine
;
; the routine is based on this equation:
;
; a*b = ((a+b)/2)^2-((a-b)/2)^2
;
; Oswald/Resource
MUL
stx XTMP ; storing X for later use
tya
eor XTMP ; getting the sign of the final product
bmi NEG ; take another routine if the final product will be negative
lda ABS,X ; this is the (a+b) part, we strip a&b from their signs using the abs table.
clc ; it is safe to force both numbers to be positive knowing the final sign of the product which we will set later
adc ABS,Y ; this is done to avoid overflows, and the extra code/tables needed to handle them.
sta XTMP
bcs special ; BUG FIX: special case for -128 * -128
lda ABS,X ; (abs(a)-abs(b))
sec
sbc ABS,Y
tay
ldx ABS,Y ; ((a-b)/2)^2 will be always positive so its safe to do abs(a-b)
ldy XTMP ; we do this since the sqr table can only handle positive numbers
;now we have a+b in Y and a-b in X
;low 8 bits of the product calculated here
lda SQRL,Y ;((a+b)/2)^2
sec
sbc SQRL,X ;-((a-b)/2)^2
sta RL
;same as above for high 8 bits
lda SQRH,Y
sbc SQRH,X
tay
ldx RL
rts
;case for negative final product, all the same except inverting the result at the end.
NEG
lda ABS,X
clc
adc ABS,Y
sta XTMP
lda ABS,X
sec
sbc ABS,Y
tay
ldx ABS,Y
ldy XTMP
lda SQRL,Y
sec
sbc SQRL,X
sta RL
lda SQRH,Y
sbc SQRH,X
sta RH
;inverting the result's sign
lda RL
eor #$FF
clc
adc #$01
sta RL
lda RH
eor #$FF
adc #$00
sta RH
ldy RH
ldx RL
rts
special
ldy #$40
sty RH
ldx #0
stx RL
rts
; generating a 16 bit table with 512 entrys where x=(x*x)/4
;
; MULGEN
; lda #1
; sta $F0
; lda #0
; sta $F1
;
; lda #0
; sta $F4
; sta $F5
; sta $F6
; sta SQRL
; sta SQRH
;
; lda #<SQRH
; sta $FE
; lda #>SQRH
; sta $FF
;
; lda #<SQRL
; sta $FA
; lda #>SQRL
; sta $FB
;
; ldx #$01
; ldy #$01
; FFV2
;
; FFV
; lda $F0
; clc
; adc $F4
; sta $F4
;
; lda $F1
; adc $F5
; sta $F5
;
; lda $F6
; adc #$00
; sta $F6
;
; lda $F6
; sta $B2
; lda $F5
; sta $B1
; lda $F4
; sta $B0
;
; lsr $B2
; ror $B1
; ror $B0
;
; lsr $B2
; ror $B1
; ror $B0
;
; lda $B0
; sta ($FA),Y
; lda $B1
; sta ($FE),Y
;
; lda $F0
; clc
; adc #2
; sta $F0
; bcc *+4
; inc $F1
;
;
; iny
; bne FFV
;
; ldy #$00
; inc $FF
; inc $FB
; dex
; bpl FFV2
;
; MKABS
; ldx #$00 ;generating a table to get the absolute value of signed numbers
; ABSLP
; txa
; bpl POS
; eor #$FF
; clc
; adc #$01
; POS
; sta ABS,X
; dex
; bne ABSLP
; rts
;