
    DEVICE AMSTRADCPC6128

    include "src/std/dsk.asm"

;=========================
; HL=DxE si 0<=D<=85 et 0<=E<=85
;-------------------------
; Durée : 23 NOPs
;-------------------------
; Sortie :
; HL = DxE
; A,DE,HL modifiés
;=========================
    macro MUL_D_E
      ld h,MULTAB2>>8   ; 2
      ld a,d            ; 1
      sub e             ; 1 => A = x-y
      ld l,a            ; 1 => HL pointe vers le poids faible de (x-y)²/4
      add e             ; 1
      add e             ; 1 => A = x-y +y+y = x+y
      ld e,(hl)         ; 2
      inc h             ; 1
      ld d,(hl)         ; 2 => DE = (x+y)²/4
      ld l,a            ; 1 => HL pointe vers le poids FORT de (x+y)²/4
      ld a,(hl)         ; 2
      dec h             ; 1
      ld l,(hl)         ; 2
      ld h,a            ; 1 => HL = (x+y)²/4
      sbc hl,de         ; 4 Attention carry => HL=x*y
    endm

    macro MATH_PUT_MULTAB2
        LUA ALLPASS
            _pl("    align 256")
            _pl("MULTAB2:")
            print(string.format("Début MULTAB2 : %04x",_c("$")))

            for x = 0,170,1
            do
                sj.add_byte(math.floor((x*x/4)) % 256)
            end
            for x = -85,-1,1
            do
                sj.add_byte(math.floor((x*x/4)) % 256)
            end
            for x = 0,170,1
            do
                sj.add_byte(math.floor((x*x/4)) >> 8)
            end
            for x = -85,-1,1
            do
                sj.add_byte(math.floor((x*x/4)) >> 8)
            end

            print(string.format("Fin MULTAB2 : %04x",_c("$-1")))
        ENDLUA
    endm

    org #8000

start:
    ld de,0000
    MUL_D_E
    ld (fincode),hl
    ret
fincode:
    display "Fincode = ",fincode
    nop
    nop

    MATH_PUT_MULTAB2



    LUA PASS3

        if(not dsk.read("dist/mul_d_e.dsk")) then
            sj.error("Can't read the DSK file")
        else
            if (not dsk.save("test.bin",dsk.AMSDOS_FILETYPE_BINARY,sj.get_label("start"),sj.current_address,sj.get_label("start"))) then
                sj.error("Can't save the file")
            else
                if(not dsk.write("dist/mul_d_e.dsk")) then
                    sj.error("Can't save the DSK file")
                else
                    print("DSK saved.")
                end
            end
        end
    ENDLUA
