Binary math question
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Binary math question
Im working on the $0D ecm at the moment and a math op. in the lag filter routine has me a bit confused...
Heres the code:
The thing thats confusing me is the operations that follow when the old value is greater then the new value and the results of subtracting the old value from the new value are negative.
From there they branch to LF0EO to multiply the negative delta value by the filter coeff and subtract out the filter coeff x 256.
The ultimate function of this is to give the filterd output in D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256, when the subtraction op. results in a negative delta value.
I know that it does work, and it gives the same results as the above equation, but how? Could somebody maybe give me a walkthrough of how the binary operations work when the code is dealing with the negative values? Is it just creative use of the overflows that will result with a 16 bit accumulator to get the desired result?
Heres the code:
Code:
; ;~~~~~~~~~~~~~~~~~~~~~~~ ; ; Filter coeff routine ; ;~~~~~~~~~~~~~~~~~~~~~~~ ; ; Enter with: ; D = New value ; X = Old value ; Y = Addr. of filter coeff ; ; Exit with: ; D = Filtered value ; ;-General form: D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256 ; ; LF0D3 PSHX ;Old value to the stack TSX ;Get current stack pointer into X SUBD 0,X ;New value - old value PSHB ;LSB to stack LDAB 0,Y ;Load filter coeff BCS LF0E0 ;Bra if old value was greater ; ;-Here if old value was <= ; MUL ;MSB of Delta value x filter coeff BRA LF0E4 ; ; ;-Here if old value was > ; LF0E0 MUL ;-MSB of Delta value x filter coeff SUBA 0,Y ;-filter coeff x 256 ; LF0E4 ADDD 0,X ;Add in old value ; STD 0,X ;Save result to the stack in place of old value PULA ;Get LSB of delta value back LDAB 0,Y ;Load filter coeff. MUL ;LSB x filter coeff. ADCA 1,X ;Add in LSB of previous result on stack and round if needed TAB ;MSB of (LSB x filter coeff) to B LDAA 0,X ;Load up MSB of previous result on stack ADCA #0 ;Perform carry from previous addition op. if applicable ; ;-Clean up the stack ; PULX ; RTS ;Return ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From there they branch to LF0EO to multiply the negative delta value by the filter coeff and subtract out the filter coeff x 256.
The ultimate function of this is to give the filterd output in D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256, when the subtraction op. results in a negative delta value.
I know that it does work, and it gives the same results as the above equation, but how? Could somebody maybe give me a walkthrough of how the binary operations work when the code is dealing with the negative values? Is it just creative use of the overflows that will result with a 16 bit accumulator to get the desired result?
Last edited by dimented24x7; Jun 22, 2005 at 12:49 AM.
Joined: Apr 2004
Posts: 3,180
Likes: 3
From: Browns Town
Car: 86 Monte SS (730,$8D,G3,AP,4K,S_V4)
Engine: 406 Hyd Roller 236/242
Transmission: 700R4 HomeBrew, 2.4K stall
Axle/Gears: 3:73 Posi, 7.5 Soon to break
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
The thing is that theyre doing operations on negative numbers using an unsigned 16 bit result from the mult. and subtraction operations. Im sure theres a stupid little trick or tricks that allow them to do that and get the right results in the end. Ive been tempted to do something similar with stuff Ive done but not having signed values doesnt seem kosher.
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Ok, the operation at LF0E0 is equivelent to saying: -filter coeff x (Delta value + 1) for the MSB of the filtered value. Is the "+1" basically to account for the carry that would occur when the negative result is added into a positive value in the later operations?
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Yes, I just put it as it appears in the code, but why subtract out the filter coeff x 256 after multiplication when the result of the delta is negative? IOW, what operation does that accomplish?
Trending Topics
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
This is on the late model TBI pcm, so its the 68HC11, or something similar. The older procs in the early carb(I think), tbi, and tpi ecms also use the same instruction set, minus some of the more advanced instructions.
Re: Binary math question
Originally posted by dimented24x7
Im working on the $0D ecm at the moment and a math op. in the lag filter routine has me a bit confused...
Heres the code:
The thing thats confusing me is the operations that follow when the old value is greater then the new value and the results of subtracting the old value from the new value are negative.
From there they branch to LF0EO to multiply the negative delta value by the filter coeff and subtract out the filter coeff x 256.
The ultimate function of this is to give the filterd output in D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256, when the subtraction op. results in a negative delta value.
I know that it does work, and it gives the same results as the above equation, but how? Could somebody maybe give me a walkthrough of how the binary operations work when the code is dealing with the negative values? Is it just creative use of the overflows that will result with a 16 bit accumulator to get the desired result?
Im working on the $0D ecm at the moment and a math op. in the lag filter routine has me a bit confused...
Heres the code:
Code:
; ;~~~~~~~~~~~~~~~~~~~~~~~ ; ; Filter coeff routine ; ;~~~~~~~~~~~~~~~~~~~~~~~ ; ; Enter with: ; D = New value ; X = Old value ; Y = Addr. of filter coeff ; ; Exit with: ; D = Filtered value ; ;-General form: D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256 ; ; LF0D3 PSHX ;Old value to the stack TSX ;Get current stack pointer into X SUBD 0,X ;New value - old value PSHB ;LSB to stack LDAB 0,Y ;Load filter coeff BCS LF0E0 ;Bra if old value was greater ; ;-Here if old value was <= ; MUL ;MSB of Delta value x filter coeff BRA LF0E4 ; ; ;-Here if old value was > ; LF0E0 MUL ;-MSB of Delta value x filter coeff SUBA 0,Y ;-filter coeff x 256 ; LF0E4 ADDD 0,X ;Add in old value ; STD 0,X ;Save result to the stack in place of old value PULA ;Get LSB of delta value back LDAB 0,Y ;Load filter coeff. MUL ;LSB x filter coeff. ADCA 1,X ;Add in LSB of previous result on stack and round if needed TAB ;MSB of (LSB x filter coeff) to B LDAA 0,X ;Load up MSB of previous result on stack ADCA #0 ;Perform carry from previous addition op. if applicable ; ;-Clean up the stack ; PULX ; RTS ;Return ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From there they branch to LF0EO to multiply the negative delta value by the filter coeff and subtract out the filter coeff x 256.
The ultimate function of this is to give the filterd output in D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256, when the subtraction op. results in a negative delta value.
I know that it does work, and it gives the same results as the above equation, but how? Could somebody maybe give me a walkthrough of how the binary operations work when the code is dealing with the negative values? Is it just creative use of the overflows that will result with a 16 bit accumulator to get the desired result?
;-General form: D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256
it sort of drives me nuts. I don't think it helps to explain what is going on very well. Let's take a simpler look. It is really:
New filtered value = old filtered value + (new raw value - old filtered value) * Filter coeff.
So for an example, if the old value was 100, the new raw value is 110 and the filter coeff is 0.50, it looks like this:
New filtered value = 100 + (110-100) * 0.5
New filtered value = 100 + (10) * 0.5 = 105.
Easy to see that way.
Now the problem that you have in trying to "see" what is going on is complicated by the way it is presented in that awful equation. The "new value being higher" case is easy to see from the way it is documented in your code. The "new value being lower" case is not.
Maybe this will make it easier, it is from the new value being lower branch.
Code:
LF0E0 MUL ;-MSB of Delta value x filter coeff SUBA 0,Y ;-filter coeff x 256 ; LF0E4 ADDD 0,X ;Add in old value ;
So from this, implied in the muliplication is:
(1-delta) times the Fc.
The result after the muliplication is:
(Fc-scaled delta).
So the next line after the multiplication is subtracting out the Fc. This results in:
(Fc - scaled delta) - Fc or:
-scaled delta.
The - scaled delta (a negative value) is then added into the old result, resulting in the filter action that we wanted.
If that is not clear, maybe I can try it again....
ScotSea
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Re: Re: Binary math question
Originally posted by ScotSea
[B]Well..... When I see these things:
it sort of drives me nuts. I don't think it helps to explain what is going on very well.
Now the problem that you have in trying to "see" what is going on is complicated by the way it is presented in that awful equation.
[B]Well..... When I see these things:
it sort of drives me nuts. I don't think it helps to explain what is going on very well.
Now the problem that you have in trying to "see" what is going on is complicated by the way it is presented in that awful equation.
And yes, thats perfectly clear. Thank you. I have zero formal training so even when I wrote it out in the human-freindly form above, I still didnt think to think 'two's conpontent'.
Thread Starter
Supreme Member
iTrader: (2)
Joined: Jan 2002
Posts: 9,962
Likes: 5
From: Moorestown, NJ
Car: 88 Camaro SC
Engine: SFI'd 350
Transmission: TKO 500
Axle/Gears: 9-bolt w/ 3.23's
Aw man, I really feel even more stupid then usual now. I just went to clear the stack of papers off that I was scribbling on the last time I worked on the hack and I had the answer right there: FCx(deltax256-256) = -FC(|delta|) = NEG??? (negate)
Im going to go get some sleep to clear the pea soup fog around my head from squeezing working overtime/trying to fix both of my cars+old mans' car/exercise/sleep/drink beer + watch tv into one day for an entire week.
Im going to go get some sleep to clear the pea soup fog around my head from squeezing working overtime/trying to fix both of my cars+old mans' car/exercise/sleep/drink beer + watch tv into one day for an entire week.
Junior Member
Joined: Nov 2003
Posts: 30
Likes: 0
From: Stuck in the 80's
Car: G-bodies & Corvette
Engine: L98
Transmission: 700R4
Think of it this way, if it really were binary math you would have to figure it all out with just zero's and one's. Don't you feel better now?
Thread
Thread Starter
Forum
Replies
Last Post





