-
Notifications
You must be signed in to change notification settings - Fork 6
/
mult65.a
67 lines (61 loc) · 1.44 KB
/
mult65.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
; mult65.a
; from Nick Jameson's 3D Demo for the BBC Micro (1994), https://github.com/simondotm/bbc-micro-3d/blob/master/source/culling.asm
; with tables from https://github.com/simondotm/bbc-micro-3d/blob/master/source/fastmultiply.asm
;
; 8 bit x 8bit unsigned multiply, 16 bit result
; Average cycles: 47.49
; 1061 bytes
multiplicand = $02 ; 1 byte
multiplier = $03 ; 1 byte
result = $04 ; 2 bytes
lmul0 = $06 ; pointer into square table low
lmul1 = $08 ; pointer into square table high
prod_low = $0a
* = $0200
; Align tables to start of page
squaretable1_lsb
!for i, 0, 511 {
!byte <((i*i)/4)
}
squaretable1_msb
!for i, 0, 511 {
!byte >((i*i)/4)
}
; multiply
;
; f(x) = x*x/4
;
; if (Y>=X) return f(X+Y) - f(Y-X)
; else return f(X+Y) - f(X-Y)
;
; 8 bit x 8bit unsigned multiply, 16 bit result
;
; On Entry:
; X: multiplier
; Y: multiplicand
; On Exit:
; (prod_low, A): product
mult
stx lmul0
stx lmul1
tya
sec
sbc lmul0
bcs +
sbc #0 ; negate A
eor #$ff
+
tax
lda (lmul0),Y
sbc squaretable1_lsb,X
sta prod_low
lda (lmul1),Y
sbc squaretable1_msb,X
rts
; call this once to initialise high bytes of pointers to table
mult_init
lda #>squaretable1_lsb ; high byte (#2 in this instance)
sta lmul0+1
lda #>squaretable1_msb ; high byte (#4 in this instance)
sta lmul1+1
rts