DIY PROM Do It Yourself PROM chip burning help. No PROM begging. No PROMs for sale. No commercial exchange. Not a referral service.

Binary math question

Thread Tools
 
Search this Thread
 
Old Jun 22, 2005 | 12:45 AM
  #1  
dimented24x7's Avatar
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:

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
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?

Last edited by dimented24x7; Jun 22, 2005 at 12:49 AM.
Reply
Old Jun 22, 2005 | 11:40 AM
  #2  
JP86SS's Avatar
Supreme Member
20 Year Member
iTrader: (1)
 
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
This "may" help.
Negative binaries hurt my head
http://www.allaboutcircuits.com/vol_4/chpt_2/3.html
Reply
Old Jun 22, 2005 | 11:54 AM
  #3  
dimented24x7's Avatar
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.
Reply
Old Jun 22, 2005 | 12:37 PM
  #4  
dimented24x7's Avatar
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?
Reply
Old Jun 23, 2005 | 10:33 AM
  #5  
AlexJH's Avatar
TGO Supporter
 
Joined: Jul 2000
Posts: 812
Likes: 1
Engine: 5.7L V8
Transmission: 700R4
D = Old value x 256 + (Filter coeff x (New value - Old value)x256)/256

is the same as saying

Old value x 256 - (Filter coeff x (Old value - New value)x256)/256

Right?
Reply
Old Jun 23, 2005 | 11:44 AM
  #6  
dimented24x7's Avatar
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?
Reply
Old Jun 23, 2005 | 07:33 PM
  #7  
Apeiron's Avatar
Moderator
 
Joined: Jan 2000
Posts: 20,981
Likes: 11
From: Mercedes Norte, Heredia, Costa Rica
Car: 1984 Z28 Hardtop
Engine: 383 Carb
Transmission: 4L60
Axle/Gears: 3.54 Dana 44
Same instruction set as the 68HC11, right?
Reply
Old Jun 23, 2005 | 10:57 PM
  #8  
dimented24x7's Avatar
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.
Reply
Old Jun 24, 2005 | 03:43 PM
  #9  
ScotSea's Avatar
Junior Member
iTrader: (2)
 
Joined: Jun 2001
Posts: 71
Likes: 0
From: Sayre, PA
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:

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
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?
Well..... When I see these things:

;-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
;
In the above case we have a negative hex number after the subtraction. It is represented using two's complement, or in other words, it is one minus the number, or it equals (1-delta)

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
Reply
Old Jun 24, 2005 | 05:26 PM
  #10  
dimented24x7's Avatar
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.
The reason all of it will be represented that way is so I know at a glance exactly how the code flows. It looks like **** as a hack, and led to the same initial self-induced confusion with the 8063, but when it came time to do a massive overhaul, it paid off, for me at least. Having the exact representation allowed me to see at a quick glance how the code fits together without having to trace through it. Itll cause some pain initially, but hopefully it should equate to faster changes in the future. So thats the explanation for that...

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'.
Reply
Old Jun 25, 2005 | 01:07 AM
  #11  
dimented24x7's Avatar
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.
Reply
Old Jun 29, 2005 | 04:09 PM
  #12  
gbody5's Avatar
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?
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Bubbajones_ya
Electronics
4
Aug 31, 2015 12:02 PM
ezobens
DIY PROM
8
Aug 19, 2015 10:29 PM
Jake_92RS
Tech / General Engine
3
Aug 17, 2015 09:42 AM
redmaroz
LTX and LSX
7
Aug 16, 2015 11:40 PM
Armored91Camaro
DIY PROM
3
Aug 12, 2015 09:41 AM




All times are GMT -5. The time now is 07:42 PM.