            include     "hardware/custom.i"
            include     "hardware/dmabits.i"
            include     "hardware/intbits.i"
            include     "exec/libraries.i"

            XREF        Main
            XREF        bp1
            XREF        bp2
            XREF        bp3
            XREF        bp4
            XREF        tempscr
            XREF        tempscr2
            XREF        smoothflg
            XREF        BrightVal
            XREF        ContrastVal
            XREF        sc_Function
            XREF        sc_Value
            XREF        ChangeTitle
            XREF        mathbase
            XREF        _LVOSPMul
            XREF        _LVOSPDiv
            XREF        _LVOSPAdd
            XREF        _LVOSPSub
            XREF        _LVOSPFix
            XREF        _LVOSPFlt
            XREF        ToggleCP
            XREF        ThreshHold
            XREF        BandWFlg

            XDEF        ZapIt6x4
            XDEF        ScreenBack6x4
            XDEF        SaveScreen6x4

custom      EQU         $dff000



ZapIt6x4:
            move.b      #$00,$bfe301   ;parallel in
            move.b      #$02,$bfd200   ; in out in x x x x x

            move.w      #$4000,intena+custom
            move.w      #$0200,dmacon+custom

            move.b      #$02,$bfd000
            move.b      #$ffff,d0

Zdelay:
            move.l      #$00,d1
            move.l      #$ff,d1
            move.l      #$00,d1
            move.l      #$ff,d1
            move.l      #$00,d1
            move.l      #$ff,d1
            move.l      #$00,d1
            move.l      #$ff,d1
            move.l      #$00,d1
            move.l      #$ff,d1
            move.l      #$00,d1
            move.l      #$ff,d1

            dbra        d0,Zdelay

            move.l      tempscr,a0
            move.w      #$ffff,d1
bInitSync:
            move.b      $bfed01,d0
            andi.b      #$10,d0
            bne         StGrabin
            dbra        d1,bInitSync
            move.w      #$c000,intena+custom
            move.w      #$8200,dmacon+custom

            lea.l       sc_Function,a0
            move.l      #'No V',(a0)+
            move.l      #'ideo',(a0)+
            move.l      #' Inp',(a0)+
            move.l      #'ut! ',(a0)+

            jsr         ChangeTitle

            move.w      #$ffff,d0
nvloop:
            dbra        d0,nvloop
            rts

StGrabin:

            move.b      #$00,$bfd000
            move.l      #100,d2
bDelay1:
            andi.b      #$10,$bfed01
            beq         bDelay1
            dbra        d2,bDelay1

            move.l      #639,d2
            bra         bVertSync
bWaitVSync:
            andi.b      #$10,$bfed01
            bne         bVertSync
            bra         bWaitVSync

bVertSync:
            move.l      #175,d1
bPreHlp:

            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            dbra        d1,bPreHlp

            move.l      #399,d1

bHlp:
            move.b      $bfe101,(a0)+

            move.l      #$ffffffff,d6

            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6
            divu        #1,d6

            dbra        d1,bHlp

            bchg        #$1,$bfe001
            dbra        d2,bWaitVSync

            move.w      #$c000,intena+custom
            move.w      #$8200,dmacon+custom

;*************************************************
SaveScreen6x4:
            move.l      #63999,d0
            move.l      tempscr,a5
            move.l      tempscr2,a4
savescrn:
            move.l      (a5)+,(a4)+
            dbra        d0,savescrn

ScreenBack6x4:
            move.l      #63999,d0
            move.l      tempscr,a5
            move.l      tempscr2,a4
scrnbk:
            move.l      (a4)+,(a5)+
            dbra        d0,scrnbk
;*******************************************

            tst.w       smoothflg
            beq         Stretch

            lea.l       sc_Function,a0
            move.l      #'Smoo',(a0)+
            move.l      #'thin',(a0)+
            move.l      #'g...',(a0)+
            move.l      #'    ',(a0)+

            jsr         ChangeTitle


            move.l      #2,d2
            move.l      #63998,d0
            move.l      tempscr,a5

csmooth:
            clr.l       d1
            clr.l       d3
            move.b      (a5),d1
            move.b      1(a5),d3

            add.w       d1,d3
            divu.w      #2,d3
            move.b      d3,1(a5)+
            adda.l      #1,a5

            dbra        d0,csmooth
            dbra        d2,csmooth


;*****************************************
Stretch:

            lea.l       sc_Function,a0
            move.l      #'Stre',(a0)+
            move.l      #'tchi',(a0)+
            move.l      #'ng..',(a0)+
            move.l      #'.   ',(a0)+

            jsr         ChangeTitle

            tst.w       BandWFlg
            beq         GrayScale

            clr.l       d0
            move.w      ThreshHold,d0
            divu.w      #257,d0
            andi.w      #$ff,d0
            move.l      d0,ThreshVal

            move.l      tempscr,a5
            move.l      #2,d2

BWAdj:
            move.l      #63999,d1
BWAdj2:
            clr.l       d0
            move.l      ThreshVal,d3
            move.b      (a5),d0
            cmp.b       d3,d0
            bhi         BWMax
            bra         BWMin

BWMax:
            move.b      #255,(a5)+
            dbra        d1,BWAdj2
            dbra        d2,BWAdj
            bra         Divide

BWMin:
            move.b      #0,(a5)+
            dbra        d1,BWAdj2
            dbra        d2,BWAdj
            bra         Divide





GrayScale:

            clr.l       d0
            move.w      ContrastVal,d0
            divu.w      #257,d0
            andi.w      #$ff,d0
            move.l      d0,ContVal

            move.l      tempscr,a5
            move.l      #2,d2

ContAdj:
            move.l      #63999,d1
ContAdj2:
            clr.l       d0
            move.l      ContVal,d3
            move.b      (a5),d0
            cmp.b       d3,d0
            bhi         Max
            eori.b      #$ff,d3
            cmp.b       d3,d0
            bls         Min
            move.b      d0,(a5)+
            dbra        d1,ContAdj2
            dbra        d2,ContAdj
            bra         Divide

Max:
            move.b      #255,(a5)+
            dbra        d1,ContAdj2
            dbra        d2,ContAdj
            bra         Divide

Min:
            move.b      #0,(a5)+
            dbra        d1,ContAdj2
            dbra        d2,ContAdj
            bra         Divide


;****************************************
Divide:
            lea.l       sc_Function,a0
            move.l      #'Scal',(a0)+
            move.l      #'ing.',(a0)+
            move.l      #'..  ',(a0)+
            move.l      #'    ',(a0)+

            jsr         ChangeTitle


            move.l      #0,count3
            move.l      tempscr,a0

            move.l      #2,d2
            move.l      #63999,d0
            move.l      tempscr,a5
            clr.l       d3
            move.w      BrightVal,d3
            divu.w      #257,d3
            move.b      d3,d1
            andi.b      #$80,d1
            bne         Darker

Brighter:
            andi.w      #$7f,d3
            eori.w      #$7f,d3
            move.l      #Table1,a2

bdivlp2:
            move.l      #63999,d0
bdivlp:
            clr.l       d1
            move.b      (a5),d1
            add.w       d3,d1

chkclip:
            cmpi.w      #$ff,d1
            ble         SkipDivClip
            move.w      #$ff,d1
SkipDivClip:
            move.b      0(a2,d1),(a5)+
            dbra        d0,bdivlp
            dbra        d2,bdivlp2
            bra         Mkpic:

Darker:
            andi.w      #$7f,d3
            move.l      #Table1,a2

adivlp2:
            move.l      #63999,d0
adivlp:
            clr.l       d1
            move.b      (a5),d1
            cmp.w       d3,d1
            ble         SkipDivClip2
            sub.w       d3,d1
            bra         SkipDivClip3
SkipDivClip2:
            move.w      #$00,d1
SkipDivClip3:
            move.b      0(a2,d1),(a5)+
            dbra        d0,adivlp
            dbra        d2,adivlp2

Mkpic:
            jsr         ToggleCP

            lea.l       sc_Function,a0
            move.l      #'Disp',(a0)+
            move.l      #'layi',(a0)+
            move.l      #'ng..',(a0)+
            move.l      #'.   ',(a0)+

            jsr         ChangeTitle



            move.l      bp1,a1
            move.l      bp2,a2
            move.l      bp3,a3
            move.l      bp4,a4
            move.l      #0,a5


            move.l      #399,d1
            move.l      d1,count2
bcollp:
            move.l      d1,count2
            move.l      #79,d0
            move.l      d0,count
browlp:
            move.l      d0,count
            move.b      (a0),d0
            move.b      400(a0),d1
            move.b      800(a0),d2
            move.b      1200(a0),d3
            move.b      1600(a0),d4
            move.b      2000(a0),d5
            move.b      2400(a0),d6
            move.b      2800(a0),d7

            andi.b      #1,d0
            andi.b      #1,d1
            andi.b      #1,d2
            andi.b      #1,d3
            andi.b      #1,d4
            andi.b      #1,d5
            andi.b      #1,d6
            andi.b      #1,d7

            lsl.b       #7,d0
            lsl.b       #6,d1
            lsl.b       #5,d2
            lsl.b       #4,d3
            lsl.b       #3,d4
            lsl.b       #2,d5
            lsl.b       #1,d6

            or.b        d0,d1
            or.b        d1,d2
            or.b        d2,d3
            or.b        d3,d4
            or.b        d4,d5
            or.b        d5,d6
            or.b        d6,d7

            move.b      d7,(a1)


            move.b      (a0),d0
            move.b      400(a0),d1
            move.b      800(a0),d2
            move.b      1200(a0),d3
            move.b      1600(a0),d4
            move.b      2000(a0),d5
            move.b      2400(a0),d6
            move.b      2800(a0),d7

            andi.b      #2,d0
            andi.b      #2,d1
            andi.b      #2,d2
            andi.b      #2,d3
            andi.b      #2,d4
            andi.b      #2,d5
            andi.b      #2,d6
            andi.b      #2,d7

            lsl.b       #6,d0
            lsl.b       #5,d1
            lsl.b       #4,d2
            lsl.b       #3,d3
            lsl.b       #2,d4
            lsl.b       #1,d5
            lsr.b       #1,d7

            or.b        d0,d1
            or.b        d1,d2
            or.b        d2,d3
            or.b        d3,d4
            or.b        d4,d5
            or.b        d5,d6
            or.b        d6,d7

            move.b      d7,(a2)

            move.b      (a0),d0
            move.b      400(a0),d1
            move.b      800(a0),d2
            move.b      1200(a0),d3
            move.b      1600(a0),d4
            move.b      2000(a0),d5
            move.b      2400(a0),d6
            move.b      2800(a0),d7

            andi.b      #4,d0
            andi.b      #4,d1
            andi.b      #4,d2
            andi.b      #4,d3
            andi.b      #4,d4
            andi.b      #4,d5
            andi.b      #4,d6
            andi.b      #4,d7

            lsl.b       #5,d0
            lsl.b       #4,d1
            lsl.b       #3,d2
            lsl.b       #2,d3
            lsl.b       #1,d4
            lsr.b       #1,d6
            lsr.b       #2,d7

            or.b        d0,d1
            or.b        d1,d2
            or.b        d2,d3
            or.b        d3,d4
            or.b        d4,d5
            or.b        d5,d6
            or.b        d6,d7

            move.b      d7,(a3)

            move.b      (a0),d0
            move.b      400(a0),d1
            move.b      800(a0),d2
            move.b      1200(a0),d3
            move.b      1600(a0),d4
            move.b      2000(a0),d5
            move.b      2400(a0),d6
            move.b      2800(a0),d7

            andi.b      #8,d0
            andi.b      #8,d1
            andi.b      #8,d2
            andi.b      #8,d3
            andi.b      #8,d4
            andi.b      #8,d5
            andi.b      #8,d6
            andi.b      #8,d7

            lsl.b       #4,d0
            lsl.b       #3,d1
            lsl.b       #2,d2
            lsl.b       #1,d3
            lsr.b       #1,d5
            lsr.b       #2,d6
            lsr.b       #3,d7

            or.b        d0,d1
            or.b        d1,d2
            or.b        d2,d3
            or.b        d3,d4
            or.b        d4,d5
            or.b        d5,d6
            or.b        d6,d7

            move.b      d7,(a4)

            addi.l      #1600,a0
            addi.l      #1,a1
            addi.l      #1,a2
            addi.l      #1,a3
            addi.l      #1,a4
            move.l      count,d0
            dbra        d0,browlp

            move.l      tempscr,a0
            addi.l      #1,a5
            add.l       a5,a0
            move.l      count2,d1
            dbra        d1,bcollp

            lea.l       sc_Function,a0
            move.l      #'    ',(a0)+
            move.l      #'    ',(a0)+
            move.l      #'    ',(a0)+
            move.l      #'    ',(a0)+

            jsr         ChangeTitle

            jsr         ToggleCP

            rts
;********************************************

count       ds.l    1
count2      ds.l    1
count3      ds.l    1


Table1:
                dcb.b      8,0
                dc.b       0,1,0,1,0,1,1,1
                dcb.b      8,1
                dc.b       1,2,1,2,1,2,2,2
                dcb.b      8,2
                dc.b       2,3,2,3,2,3,3,3
                dcb.b      8,3
                dc.b       3,4,3,4,3,4,4,4
                dcb.b      8,4
                dc.b       4,5,4,5,4,5,5,5
                dcb.b      8,5
                dc.b       5,6,5,6,5,6,6,6
                dcb.b      8,6
                dc.b       6,7,6,7,6,7,7,7
                dcb.b      8,7
                dc.b       7,8,7,8,7,8,8,8
                dcb.b      8,8
                dc.b       8,9,8,9,8,9,9,9
                dcb.b      8,9
                dc.b       9,10,9,10,9,10,10,10
                dcb.b      8,10
                dc.b       10,11,10,11,10,11,11,11
                dcb.b      8,11
                dc.b       11,12,11,12,11,12,12,12
                dcb.b      8,12
                dc.b       12,13,12,13,12,13,13,13
                dcb.b      8,13
                dc.b       13,14,13,14,13,14,14,14
                dcb.b      8,14
                dc.b       14,15,14,15,14,15,15,15
                dcb.b      16,15


                ds.b        255

                cnop        0,2

HistoTable:
                ds.l        256
                cnop        0,2

High_Bin        ds.l        1
Low_Bin         ds.l        1
ContVal         ds.l        1
ThreshVal       ds.l        1

FFPVal1          dc.l        0
FFPVal2          dc.l        0
StepVal         dc.l        0
CountVal        dc.l        0


Table2:
                ds.b        256

