Monday, January 28, 2019

More COS routine multiply analysis.

Binary math really melts my brain. I'm not really designed to function in binary so I've been trying to figure out how the multiply routine ends up with a costable value * multiplicand *2.


I think it's useful to build a model to see exactly what's happening with the bits.




And if I run this in mame's lua console I can better see where all those bits are going:

7 bits of multiplier
mem00=7f
mem01=ff
LDX $01=ff
DEX
STX $01=fe
LDA $00=7f
LSR A=3f
STA $02=3f
LDA #$00 a=00
BEFORE ADC $01=fe  a=00 00000000
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=ff 11111111 c=0
BEFORE ROL: 0_11111111_00111111
ROL8 BEFORE= ff  carry= 0  11111111
ROL8 AFTER = 7f  carry= 1  01111111
ROL8 BEFORE= 3f  carry= 1  00111111
ROL8 AFTER = 9f  carry= 1  10011111
AFTER ROL:  1_01111111_10011111
1 7f 01111111 9f 10011111
BEFORE ADC $01=fe  a=7f 01111111
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=7e 01111110 c=1
BEFORE ROL: 1_01111110_10011111
ROL8 BEFORE= 7e  carry= 1  01111110
ROL8 AFTER = bf  carry= 0  10111111
ROL8 BEFORE= 9f  carry= 0  10011111
ROL8 AFTER = 4f  carry= 1  01001111
AFTER ROL:  1_10111111_01001111
1 bf 10111111 4f 01001111
BEFORE ADC $01=fe  a=bf 10111111
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=be 10111110 c=1
BEFORE ROL: 1_10111110_01001111
ROL8 BEFORE= be  carry= 1  10111110
ROL8 AFTER = df  carry= 0  11011111
ROL8 BEFORE= 4f  carry= 0  01001111
ROL8 AFTER = 27  carry= 1  00100111
AFTER ROL:  1_11011111_00100111
1 df 11011111 27 00100111
BEFORE ADC $01=fe  a=df 11011111
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=de 11011110 c=1
BEFORE ROL: 1_11011110_00100111
ROL8 BEFORE= de  carry= 1  11011110
ROL8 AFTER = ef  carry= 0  11101111
ROL8 BEFORE= 27  carry= 0  00100111
ROL8 AFTER = 13  carry= 1  00010011
AFTER ROL:  1_11101111_00010011
1 ef 11101111 13 00010011
BEFORE ADC $01=fe  a=ef 11101111
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=ee 11101110 c=1
BEFORE ROL: 1_11101110_00010011
ROL8 BEFORE= ee  carry= 1  11101110
ROL8 AFTER = f7  carry= 0  11110111
ROL8 BEFORE= 13  carry= 0  00010011
ROL8 AFTER = 09  carry= 1  00001001
AFTER ROL:  1_11110111_00001001
1 f7 11110111 09 00001001
BEFORE ADC $01=fe  a=f7 11110111
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=f6 11110110 c=1
BEFORE ROL: 1_11110110_00001001
ROL8 BEFORE= f6  carry= 1  11110110
ROL8 AFTER = fb  carry= 0  11111011
ROL8 BEFORE= 09  carry= 0  00001001
ROL8 AFTER = 04  carry= 1  00000100
AFTER ROL:  1_11111011_00000100
1 fb 11111011 04 00000100
BEFORE ADC $01=fe  a=fb 11111011
ADD    ADC (c=1+$01)=ff 11111111
AFTER  ADC $01=fe  a=fa 11111010 c=1
BEFORE ROL: 1_11111010_00000100
ROL8 BEFORE= fa  carry= 1  11111010
ROL8 AFTER = fd  carry= 0  11111101
ROL8 BEFORE= 04  carry= 0  00000100
ROL8 AFTER = 02  carry= 0  00000010
AFTER ROL:  0_11111101_00000010
0 fd 11111101 02 00000010
[MAME]> 
[MAME]> print(hex(0xff * 0x7f * 2))
fd02
[MAME]> 

No comments:

Post a Comment