
*   This program will check, program and verify the MC68701 or the
*   MC68701U4 EPROM.  It also determines whether a MC68701(U4) is
*   being programmed.

*                       Equates

P1DDR   equ     $00     Port 1 data dir
P1DR    equ     $02     Port 1 data
TCSR    equ     $08     Timer control/stat
Timer   equ     $09     Timer counter
OutCmp  equ     $0B     Timer output compare
EPMCnt  equ     $14     RAM/EPROM control
TCR2    equ     $18     Timer control reg 2

*                       Local Variables

        org     $80
IMBeg   rmb     2       start of memory block
IMEnd   rmb     2       last byte of memory block
Pntr    rmb     2       first byte of EPROM to be pgm'd
Wait    rmb     2       counter value

        org     $B850
Start   lds     #$FF    init stack
        ldaa    #$17    init port 1
        staa    P1DDR           ddr
        staa    P1DR            data (LEDs and Vpp off)

*                       determine whether an 01 or 01U4 is being
*                       programmed.

        ldaa    TCR2    TCR2 = $03 on reset
        cmpa    #%00000011 If U4, this value
        beq     P4K     Goto U4 memory setup
        ldaa    #$FE    second check
        staa    TCR2    write a zero to tcr2-0 (clock)
        ldaa    TCR2    now read it back
        anda    #$01    mask clock bit
        beq     P4K     U4 if "Z" = 1

*                       initialize EPROM mem size to 2K (701)

        ldd     #$7800  start of EPROM
        std     IMBeg
        ldd     #$F800  start of '701 EPROM
        std     Pntr
        bra     BlkROM

*                       initialize EPROM mem size to 4K (701U4)

P4K     ldd     #$7000  start of EPROM
        std     IMBeg
        ldd     #$F000  start of '701U4 EPROM
        std     Pntr
        page
*                       blank check

BlkROM  ldx     Pntr    check if EPROM erased
        ldab    #$00    get ready for compr.
Erase   ldaa    0,x     load EPROM contents
        cba             compare to zero
        bne     Error1  branch if not zero
        cpx     #$FFFF  check if done
        beq     Next    if so branch
        inx             go again
        bra     Erase

Next    ldaa    #$16    turn on erased LED
        staa    P1DR

*                       delay loop (3.5 sec)

        stx     Wait
        ldx     #70     get ready for 70 times thru loop
Stall1  dex
        ldd     #$C350  init 50 mS loop
        addd    Timer   bump current value
        clr     TCSR    clear ocf
        std     OutCmp  set output compare
        ldaa    #$40    now wait for OCF
Stall2  bita    TCSR
        beq     Stall2  not yet
        cpx     #0      70 times yet?
        bne     Stall1  nope
        bra     PgInt

Error1  ldaa    #$02    light error and erase LED
        staa    P1DR
        bra     Self

PgInt   ldx     #$7FFF  init IMEnd
        stx     IMEnd
        ldx     #$C350  init Wait (4.0 MHz)
        stx     Wait
        page
*                       programming loop

EPROM   ldaa    #$07    turn off LEDs and apply Vpp
        staa    P1DR
        ldx     Pntr    save calling argument
        pshx            restore when done
        ldx     IMBeg   use stack
EPR002  pshx            save pointer on stack
        ldaa    #$FE    remove Vpp, set latch
        staa    EPMCnt  PPC=1, PLC=0
        ldaa    0,x     move data memory-to-latch
        ldx     Pntr    get where to put it
        staa    0,x     stash and latch
        inx             next addr.
        stx     Pntr    allset for next
        ldaa    #$FC    enable EPROM power (Vpp)
        staa    EPMCnt  PPC=0, PLC=0

*               wait 50 mSec timeout using compare

        ldd     Wait    get cycle counter
        addd    Timer   bump current value
        clr     TCSR    clear OCF
        std     OutCmp  set output compare
        ldaa    #$40    now wait for OCF
EPR004  bita    TCSR
        beq     EPR004  not yet
        pulx            set up for next one
        inx             next
        cpx     IMEnd   maybe done
        bls     EPR002  not yet
        ldaa    #$17    remove Vpp at pin
        staa    P1DR
        ldaa    #$ff    remove Vpp, inhibit latch
        staa    EPMCnt  EPROM can now be read
        pulx            restore Pntr
        stx Pntr

*               verify new code

        ldx     IMBeg   set up pointer
Verf2   pshx            save pinter on stack
        ldaa    0,x     get desired data
        ldx     Pntr    get EPROM addr.
        ldab    0,x     get data to be checked
        cba             check if same
        bne     Error2  branch if error (light LED)
        inx             next addr
        stx     Pntr    all set for next
        pulx            setup for next one
        inx             next
        cpx     #$8000  maybe done
        bne     Verf2   not yet

        ldaa    #$15
        staa    P1DR    light verify LED
        page
Self    bra     Self    wait forever

Error2  ldaa    #$13    light error LED
        staa    P1DR
        bra Self

*               Restart and intr. vectors

        org $BFF0
        fdb Self
        fdb Self
        fdb Self
        fdb Self
        fdb Self
        fdb Self
        fdb Self
        fdb Start
        end
