*---------------------------------------------------------------*
*  File:        prog11.s07                                      *
*  Version:     2/6/93                                          *
*---------------------------------------------------------------*
	NAME    prog11
* Constants
MDA     EQU     $20
SMOD    EQU     $40
TDRE    EQU     $80
RDRF    EQU     $20
RWU     EQU     $02
MS10    EQU     10000/3

* Registers
DDRC    EQU     $07
DDRD    EQU     $09
PORTC   EQU     $03
PORTD   EQU     $08
PPROG   EQU     $3B
HPRIO   EQU     $3C
BAUD    EQU     $2B
SCCR1   EQU     $2C
SCCR2   EQU     $2D
SCSR    EQU     $2E
SCDR    EQU     $2F
SPCR    EQU     $28
CONFIG  EQU     $103F
BPROT   EQU     $35
*---------------------------------------------------------------*
*  RCODE - Where the program actually starts                    *
*---------------------------------------------------------------*
	org     $0000
*--------------------------------
* START OF PROGRAM, INITIALIZE
*--------------------------------
prog    sei                     * DISABLE INTERRUPTS
	lds     #$1ff           * SET THE STACK POINTER FOR E9
	ldx     #$1000          * SET X TO REGISTER BASE, ALWAYS = $1000

*---------------------------------------------------------------------
* CLEAR THE BLOCK PROTECT REGISTER. SEEMED TO ONLY WORK BY FORCING
* SPECIAL TEST MODE.
*---------------------------------------------------------------------
	CLR     BPROT,X         * ENABLE ACCESS TO EEPROM
        BSET    HPRIO,X,#MDA    * FORCE SPECIAL TEST MODE
        BCLR    HPRIO,X,#SMOD   * FORCE Expanded Mult Mode
*       BCLR    HPRIO,X,#MDA    * FORCE BOOTSTRAP MODE
*---------------------------------------------------------------------
* TURN ON THE LED SO WE KNOW THE DOWNLOAD WORKED.
*---------------------------------------------------------------------
	LDAA    #$22            * SET DATA DIRECTION FOR PORT D
	STAA    DDRD,X          *



        LDX     #$1000
        JSR     <LED_ON          * TURN ON THE LED.
CHKERR  LDX     #$1000
*---------------------------------------------------------------------
* SETUP THS SERIAL INTERFACE AND THEN WAIT FOR A CHARACTER.
*---------------------------------------------------------------------
	CLR     SCCR1,X         * NO WAKEUP
	LDD     #$300C          * 9600 BAUD, 8 DATA, 1 STOP, NO PARITY
	STAA    BAUD,X
	STAB    SCCR2,X

*--------------------------------
* READ IN THE COMMAND BYTE
*--------------------------------
GETN    ldx     #$1000
        JSR     GETCHAR
	CMPB    #'w'            * CHECK FOR A WRITE BLOCK COMMAND
        BNE     GETNR           *
        BNE     GETNA           *
        JSR     GET_ADDR_DATA   * GET ADDRESS IN Y, NUMBER OF BYTES IN B
	TBA                     * TRANSFER COUNTER TO A
GETwL
        BRCLR   SCSR,X,#RDRF,*  * WAIT FOR A CHARACTER TO ARRIVE
	LDAB    SCDR,X          * READ THE CHARACTER INTO B
	STAB    0,Y             * STORE THE BYTE
	LDAB    0,Y             * READ THE BYTE
        BRCLR   SCSR,X,#TDRE,*  * WAIT FOR PREVIOUS TRANSMISSION TO COMPLETE
	STAB    SCDR,X          * WRITE THE CURRENT IN B TO THE SCI
	INY                     * POINT TO NEXT STORAGE LOCATION
	DECA
	BNE     GETwL           * LOOP BACK
	LDAB    #$ee
	JSR     PUTCHAR
	BRA     GETN            * GET NEXT COMMAND
GETNA   CMPB    #'A'            * CHECK FOR ADDRESS COMMAND
        BNE     GETNR
        BSR     GET_ADDR_DATA   * GET THE ADDRESS AND DATA
        BSR     PROGRAM         * PROGRAM THE BYTE
        LDAB    ,Y              * READ THE BYTE JUST PROGRAMMED
        JSR     PUTCHAR         * WRITE BACK THE PROGRAMMED VALUE
        BRA     GETN
GETNR   CMPB    #'R'            * CHECK FOR READ COMMAND
	BNE     GETNrr
	BSR     GET_ADDR_DATA   * GET THE ADDRESS AND DATA
	TBA                     * TRANSFER COUNTER TO A
PCH
        cpy     #$5000          * Check if a CAN address read
        blo     PCH0
        cpy     #$6000          * Check if a CAN address read
        bhs     PCH0
        ldab    0,Y             * Read the byte
        ldab    $6004
        bra     PCH1
PCH0    LDAB    ,Y              * READ THE BYTE JUST PROGRAMMED
PCH1    JSR     PUTCHAR         * WRITE BACK THE PROGRAMMED VALUE
	INY                     * POINT TO THE NEXT TO READ
	DECA                    * DEC THE LOOP COUNTER
	BNE     PCH             * BR BACK IF NOT DONE
GETNrr  CMPB    #'f'            * CHECK FOR PROGRAM FLASH COMMAND
        BEQ     FLASH
BADCMMD JMP     GETN

*---------------------------------------------------------------------
*       FLASH
*---------------------------------------------------------------------
FLASH   bsr     GET_ADDR        * Get the address, returns in Y
        ldaa    #64             * Program 64 bytes at a time
        ldx     #$6000          * Point to RAM buffer
FL0     ldab    0,X             *
        stab    0,Y
        inx
        iny
        DECA
        bne     FL0
        jsr     DLY10
        ldx     #$1000
        ldab    #$aa
        jsr     PUTCHAR
        BRA     GETN
*---------------------------------------------------------------------
* subroutine:   GET_ADDR_DATA
* description:  Receives 3 bytes of address and data. Each byte
*               echoed back.
* returns:      Y = address
*               B = data
*---------------------------------------------------------------------
GET_ADDR
        JSR     GETCHAR         * Read in the MSB of the address
	TBA                     * Form the MSB in D
        JSR     GETCHAR         * Read in the LSB of the address
	XGDY                    * Set Y pointer to address received
	RTS
GET_ADDR_DATA
	BSR     GET_ADDR        * Get the address to Y
        JMP     GETCHAR         * Get the data, return it in B, rts from GETCHAR
*---------------------------------------------------------------------
* subroutine:   TOGGLE_LED, LED_ON, LED_OFF
*---------------------------------------------------------------------
TOGGLE_LED
        BRCLR   PORTD,X,#$20,LED_OFF
LED_ON
        BCLR    PORTD,X,#$20    * SET PORTD LED ON
	RTS
LED_OFF
        BSET    PORTD,X,#$20    * SET PORTD LED OFF
	RTS
*------------------------------------------------
* SUBROUTINE:   PROGRAM
* DESCRIPTION:  PROGRAM A BYTE.
*------------------------------------------------
ERASE_EEPROM:
	LDAA    #$06            * ERASE ALL
	BRA     PROG00
*------------------------------------------------
* SUBROUTINE:   PROGRAM
* DESCRIPTION:  PROGRAM A BYTE.
*               X, Y, B NOT CHANGED.
*------------------------------------------------
PROGRAM
	CMPB    ,Y              * CHECK IF SAME
	BEQ     PROGRTS
	LDAA    #$16            * DEFAULT TO BYTE ERASE FOR NOW
	CPY     #CONFIG         *
	BNE     PROG01
	BSR     LED_ON
	LDAA    #$06
PROG01  BSR     PROG00
	LDAA    #2
PROG00  STAA    PPROG,X         * ENABLE INTERNAL ADDR/DATA LATCHES
	STAB    ,Y              * WRITE TO THE REQUIRED ADDRESS
	INC     PPROG,X         * ENABLE INTERNAL PROGRAMMING VOLTAGE
	PSHX
	LDX     #MS10           * DELAY 10MILLISECONDS
PWAIT   DEX
	BNE     PWAIT
	PULX
	DEC     PPROG,X         * DISABLE INTERNAL PROGRAMMING VOLTAGE
	CLR     PPROG,X         * RELEASE INTERNAL ADDR/DATA LATCHES
PROGRTS RTS

*------------------------------------------------
* SUBROUTINE:   PROG
* DESCRIPTION:  PROG THE NVM BYTE AT THE LOCATION
*               POINTED TO BY Y WITH THE VALUE
*               IN Y.
*
* USES A.
*------------------------------------------------
PROG    LDAA    #$16
	STAA    $103B
	STAB    0,Y
	LDAA    #$17
	STAA    $103B
	BSR     DLY10
	CLR     $103B
	LDAA    #$02
	STAA    $103B
	STAB    0,Y
	LDAA    #$03
	STAA    $103B
	BSR     DLY10
	CLR     $103B
	BSR     LED_ON
	RTS
*------------------------------------------------
* SUBROUTINE:   DLY10
* DESCRIPTION:  DELAY 10 MILLISECONDS.
*
* USES X BUT IS RESTORED
*------------------------------------------------
DLY10   PSHX
	LDX     #MS10           * DELAY 10MILLISECONDS
DWAIT   DEX
	BNE     DWAIT
	PULX
	RTS
*------------------------------------------------
* SUBROUTINE:   GETCHAR
* DESCRIPTION:  READ A BYTE FROM THE SCI, ECHO IT
*               BY FALLING THROUGH INTO PUTCHAR
*               AND RETURN VALUE IN B.
* INPUT:        X MUST POINT TO $1000
* OUTPUT:       B CONTAINS VALUE READ
*------------------------------------------------
GETCHAR
        pshy
        ldy     #10000
gc      dey
        bne     gs
        jsr     TOGGLE_LED
        ldy     #10000
gs      BRCLR   SCSR,X,#RDRF,gc
        puly
	LDAB    SCDR,X          * READ THE CHARACTER INTO B
*------------------------------------------------
* SUBROUTINE:   PUTCHAR
* DESCRIPTION:  WRITE A BYTE TO THE SCI.
* INPUT:        B CONTAINS VALUE TO WRITE
*               X MUST POINT TO $1000
*------------------------------------------------
PUTCHAR BRCLR   SCSR,X,#TDRE,*  * WAIT FOR PREVIOUS TRANSMISSION TO COMPLETE
	STAB    SCDR,X          * WRITE THE CURRENT IN B TO THE SCI
	RTS

	ldaa    #$ff
	staa    DDRC
	clra
b02     staa    PORTC
	ldaa    #10
b01     ldx     #3333
b00     dex
	bne     b00
	deca
	bne     b01
	ldaa    PORTC
	eora    #$80
	bra     b02
progend rts

	end
