Loading...
 

LH 2.4 Math Functions

I've start posting here important math routines the LH 2.4 uses
in it's fuel calculations. I call them "library" functions, since they are present in all versions, both 5xx and 9xx, also in Saab and Porche LH 2.3 bins. And even some motronic versions uses exact the same calculating building blocks.

The code posted here is from 0 280 000 950 bin, but it is valid in all else binaries - only the offset is different.



Multiplication and Division functions



16-bit R4:R5 (HI:LO) by 8-bit A multiplication, with 24-bit result A:R4:R5 (HI:MID:LO)
L25A0:
    MOV B,A         ;25A0 F5 F0
    XCH A,R4        ;25A2    CC
    MUL AB          ;25A3    A4
    XCH A,R5        ;25A4    CD
    XCH A,B         ;25A5    C5 F0
    XCH A,R4        ;25A7    CC
    MUL AB          ;25A8    A4
    XCH A,R5        ;25A9    CD
    ADD A,B         ;25AA    25 F0
    XCH A,R4        ;25AC    CC
    ADDC A,#0x00    ;25AD    34 00
    RET             ;25AF    22


Fixed point 16.0 by fixed point 8.8 multiplication, input R2:R3 (HI:LO), A:R4 (HI:LO), result 24-bit in A:R4:R5 (HI:MID:LO)
L25B0:
    MOV 0x05,R2     ;25B0   8A 05
    MOV 0x01,R3     ;25B2   8B 01

    MOV B,A         ;25B4   F5 F0
    XCH A,R5        ;25B6   CD
    MUL AB          ;25B7   A4

    XCH A,R3        ;25B8   CB
    XCH A,B         ;25B9   C5 F0
    XCH A,R5        ;25BB   CD
    MUL AB          ;25BC   A4

    XCH A,R3        ;25BD   CB
    ADD A,B         ;25BE   25 F0
    XCH A,R5        ;25C0   CD
    ADDC A,#0x00    ;25C1   34 00

    XCH A,R4        ;25C3   CC
    MOV B,A         ;25C4   F5 F0
    XCH A,R2        ;25C6   CA
    MUL AB          ;25C7   A4

    XCH A,R1        ;25C8   C9
    XCH A,B         ;25C9   C5 F0
    XCH A,R2        ;25CB   CA
    MUL AB          ;25CC   A4

    MOV C,ACC.7     ;25CD   A2 E7
    XCH A,R3        ;25CF   CB
    ADDC A,B        ;25D0   35 F0
    JNC L25D5       ;25D2   50 01
    INC R2          ;25D4   0A

L25D5:
    ADD A,R1        ;25D5   29
    XCH A,R5        ;25D6   CD
    ADDC A,R2       ;25D7   3A
    XCH A,R4        ;25D8   CC
    ADDC A,#0x00    ;25D9   34 00
    RET             ;25DB   22




Signed 8-bit multiplication. Input A, B. Result signed 16-bit word A:B (HI:LO); range A, B = [-128:127] == result signed short [-32768:32767], HI(A), LO(B)
L26BA_SMUL8x8_AB:
    JB  ACC.7,L26C1     ;26BA   20 E7 04
    MUL AB              ;26BD   A4
    XCH A,B             ;26BE   C5 F0
    RET                 ;26C0   22

L26C1:
    CPL A               ;26C1   F4
    INC A               ;26C2   04
    MUL AB              ;26C3   A4
    CPL A               ;26C4   F4
    ADD A,#0x01         ;26C5   24 01
    XCH A,B             ;26C7   C5 F0
    CPL A               ;26C9   F4
    ADDC A,#0x00        ;26CA   34 00
    RET                 ;26CC   22



Signed 16-bit division by 32; Input A:B, Output A:B (HI:LO)
L2805_SIGNED_16BIT_DIV_BY_32:
    MOV R4,#0x05        ;2805   7C 05

LOOP:
    MOV C,ACC.7         ;2807   A2 E7
    RRC A               ;2809   13          A /= 2
    XCH A,B             ;280A   C5 F0       B = A, A = B
    RRC A               ;280C   13          A /= 2
    XCH A,B             ;280D   C5 F0       B = A, A = B
    DJNZ R4,LOOP        ;280F   DC F6
    RET                 ;2811   22



Shift functions

Although shift can be considered as a logic/bit operation,
these are mostly used for quick division or multiplication by power of two


Shifts left 24-bit word in A:R4:R5 (HI:MID:LO) by R2
L25DC:
    MOV C,ACC.7     ;25DC   A2 E7
    JC L25EB_RETURN ;25DE   40 0B

L25E0:
    XCH A,R5        ;25E0   CD
    RLC A           ;25E1   33
    XCH A,R5        ;25E2   CD
    XCH A,R4        ;25E3   CC
    RLC A           ;25E4   33
    XCH A,R4        ;25E5   CC
    RLC A           ;25E6   33
    JC L25EB_RETURN ;25E7   40 02
    DJNZ R2,L25E0   ;25E9   DA F5
L25EB_RETURN:
    RET             ;25EB   22


Shifts right 24-bit word in A:R4:R5 (HI:MID:LO) by R2
L25EC:
    CLR C           ;25EC   C3
    RRC A           ;25ED   13
    XCH A,R4        ;25EE   CC
    RRC A           ;25EF   13
    XCH A,R4        ;25F0   CC
    XCH A,R5        ;25F1   CD
    RRC A           ;25F2   13
    XCH A,R5        ;25F3   CD
    DJNZ R2,L25EC   ;25F4   DA F6
    RET             ;25F6   22


Addition and subtraction functions


Saturated signed 16-bit addition of R4:R5 (HI:LO) and R2:A (HI:LO). Result is in R4:R5 (HI:LO)
L2663_S16ADD_SATURATED_R4R5_R2A:
    ADD A,R5            ;2663   2D
    MOV R5,A            ;2664   FD
    MOV A,R2            ;2665   EA
    ADDC A,R4           ;2666   3C
    MOV R4,A            ;2667   FC
    JNB OV,L2676_RET    ;2668   30 D2 0B
    JNC L2672           ;266B   50 05
    MOV R4,#0x80        ;266D   7C 80
    MOV R5,#0x01        ;266F   7D 01
    RET                 ;2671   22       -32768
L2672:
    MOV R4,#0x7F        ;2672   7C 7F
    MOV R5,#0xFF        ;2674   7D FF    32767
L2676_RET:
    RET                 ;2676   22



Unsigned 16-bit addition. Adds R0:R1 (HI:LO) to A:B (HI:LO); Result in A:B
L27CF_16_BIT_ADD_R0_R1_TO_A_B:
    XCH A,B                     ;27CF   C5 F0
    ADD A,R0                    ;27D1   28          B+=R0
    XCH A,B                     ;27D2   C5 F0       A+=R1+CARRY
    ADDC A,R1                   ;27D4   39
    RET                         ;27D5   22



Misc functions

Some utility functions that don't fit into above categories

Casts unsigned 8-bit value, into signed 8-bit, with saturation. Input A, Output A
L27AD_SAT_SIGNED_CHAR_CAST:
    JC  L27B6           ;27AD   40 07
    JNB ACC.7,L27BB     ;27AF   30 E7 09
    MOV A,#0x7F         ;27B2   74 7F
    SJMP L27BB          ;27B4   80 05

L27B6:
    JB  ACC.7,L27BB     ;27B6   20 E7 02
    MOV A,#0x80         ;27B9   74 80

L27BB:
    RET                 ;27BB   22


Signed 16-bit unary minus. Word in R2:A (HI:LO) is returned as -R2:A
L265A_NEGATE_16BIT_R2_A:
    CPL A               ;265A   F4
    ADD A,#0x01         ;265B   24 01
    XCH A,R2            ;265D   CA
    CPL A               ;265E   F4
    ADDC A,#0x00        ;265F   34 00
    XCH A,R2            ;2661   CA
    RET                 ;2662   22




Online Users

24 online users

Last Visitors

  1. You
    04:02