;------------------------------------------------------------------------------
; FILENAME: c2xxprog.asm - Generic name
;           
;
; DESCRIPTION:
;   Flash Programmer control module with flash algorithms -P
;   called by the JTAG loader, PRG2xxw.exe
;   Uses the on-chip SARAM for algorithms and flash data buffer
;
; GLOBALS:
;
;   unsigned   PRG_options      Option selection word.
;
;   unsigned * PRG_bufaddr      Address of buffer for flash/program data
;   unsigned   PRG_bufsize      Size of program data buffer
;   unsigned   PRG_devsize      Size of programmable device
;   unsigned * PRG_paddr        First programming address
;   unsigned   PRG_page         Programming page
;   unsigned   PRG_length       Length of programming data
;   unsigned   PRG_status       Status of programming functions
;
; PUBLIC FUNCTIONS:
;   PRG_init                    Initialize system for programming
;   PRG_program                 Program a block
;   PRG_erase                   Erase a programmable device
;   PRG_verify                  Verify a block
;
; PRIVIATE FUNCTIONS:
;   None
;
; USAGE/LIMITATIONS
;   The global and public function symbol values must be available in the
;   COFF file for the loader to work properly. The functions are not
;   intended to be called by another program.
;
; NOTES:
;   The program needs three memory sections to operate:
;   Actual location of these sections in the memory is defined in the
;   linker command file - *.cmd
;
;       PRG_text    Executable section for this program
;       PRG_parms   Variable section for this program
;       PRG_data    Data buffer used to hold programming data
;
;   The size and location of each section  is user defined.  The
;   loader program will read the value of PRG_bufaddr and PRG_bufsize to
;   determine the location and size of the PRG_data section.
;
; JTAG loader command file format
;   PRG2xxw  -[options] c2xxprog.out       Flashcode.out
;  <JTAG loader)      <Flash algorithm>  <COFF file to be flashed>
;
;   Source : TI Tools group, Houston
;   
;   Modification History:
;   ---------------------
;   -Modified for F2xx devices:                                     12/24/96
;   -Added appropriate init code for PLL & WD disable               01/15/97
;   -Added Conditional assembly for PLL Mult factor selection       02/26/97
;   -Modified for: Compatibility with -s option and new algorithms  09/10/97
;   -Modified to suit 24x-Lite -                                    03/19/98
;   -Modified to interface to the API algos                         05/25/01
;   
;------------------------------------------------------------------------------
; INCLUDE FILES
;------------------------------------------------------------------------------

	    .INCLUDE	"..\include\VAR.h"	;Get the flash function call protos and
                                        ; the variable declarations.

;------------------------------------------------------------------------------
; PUBLIC DECLARATIONS
;------------------------------------------------------------------------------
        .global PRG_init, PRG_program, PRG_erase, PRG_verify, PRG_stop
        .global PRG_bufaddr, PRG_bufsize, PRG_devsize, PRG_paddr, PRG_page
        .global PRG_length, PRG_status,PRG_options,PARMS
        .global PROTECT, SEG_ST,SEG_END

	

;------------------------------------------------------------------------------
; PRIVATE DECLARATIONS
;------------------------------------------------------------------------------
BUFFER_SIZE .set    200         ;Size of program buffer size
                                ;Can be increased based available
                                ;memory

DEVICE_SIZE .set    0ffffh      ;Size of device to be programmed.
                                ;Default is maximum address range for
                                ;the F2xx

;------------------------------------------------------------------------------
; Define the PRG_parm section
;------------------------------------------------------------------------------
    .sect   "PRG_parm"          ;Actual location in memory defined is
PARMS:                          ;linker command file
PRG_bufaddr .word   PrgBuffer   ;Address of buffer for program
PRG_bufsize .word   BUFFER_SIZE ;Tells host of buffer size
PRG_devsize .word   DEVICE_SIZE ;Tells host of device size

*                               ;The following parameters will be redefined by
*                               ;PRG2xxw based on the flashcode.out
                                ;Algorithm Array variables
PRG_options .word   0h          ;Default to disable all sectors.
                                ;This will be re-initiatlized by -s option.
PRG_paddr   .word   0           ;First address to program
PRG_page    .word   0           ;Page to program
PRG_length  .word   0           ;Length of block to program
PRG_status  .word   0           ;Status of programming functions
Temp        .word   0           ;Temp location for verify
PROTECT     .word   0FF00h      ;Enable all 8 segments
SEG_ST      .word   00000h      ;Segment start address. Initialize to
                                ;segment0/flash0.
SEG_END     .word   03FFFh      ;Segment end address. Initialize to
                                ;segment7/flash0.

;------------------------------------------------------------------------------
; Define the PRG_data section
;------------------------------------------------------------------------------
    .sect   "PRG_data"          ;Flash code data buffer 
PrgBuffer   .space  BUFFER_SIZE*16  ;Initializes buffer for program data
    .sect   "ary_var"           ; Initialize buffer to 0x0000
            .space  16*16
;
;------------------------------------------------------------------------------
; Define the PRG_text section
;------------------------------------------------------------------------------
    .sect   "PRG_text"          ; Control and algorithm module
;------------------------------------------------------------------------------
; Name: PRG_init
; 
; Description
;   Initialize the F2xx device for programming
; 
; Inputs
;   None
; Outputs
;   int PRG_status              Pass =0; Fail = 1;
;
; Notes: Can be used to include device specific initialization before executing
;        Flash algorithms in PRG_erase,PRG_program, PRG_verify and PRG_stop
;
;------------------------------------------------------------------------------
 
PRG_init:   
	LDP	#0E0h
	SPLK	#PLL_RATIO_CONST,7018h
	SPLK	#000bh,7019h
	SPLK	#6fh,7029h

    LDP     #0
    LACC    #40h            ; Get Address of the Key Area.
    RPT     #3
    TBLR    0060h           ; Do Table Reads to update the 
                            ; lock register.


	ldp     #PARMS		; Defines data page
	splk    #0,PRG_status
    b       PRG_stop
;
;------------------------------------------------------------------------------
; Name: PRG_program
; 
; Description
;   Transfers a block of data from RAM to Flash/programmable memory
;   PRG2xxw will execute this after PRG_init by default. If the command
;   line does not have any options -e or -v.
;   If the option specified is -e  then the execution sequence will be:
;   PRG_init > PRG_erase > PRG_program and then PRG_stop
;   If the option specified is -v, then the execution sequence will be:
;   PRG_init > PRG_program, PRG_verify and then PRG_stop
; Inputs
;   unsigned *PRG_paddr         Pointer to first address to program
;   unsigned  PRG_page          Page to program, Program == 0, Data == 1
;   unsigned  PRG_length        Number of words to program
;   unsigned *PRG_bufaddr       Pointer to program data buffer
;
; Outputs
;   int PRG_status              Pass =0; Fail = 1;
;
; Notes:
;   The PRG_page value is setup by the host but not used in this case.
;   The assumption is that the programmable device is in program space.
;------------------------------------------------------------------------------
 
PRG_program:    ldp #PARMS              ;Point to data page 
                                        ;no  calls for Flash algorithms
              
err2:           splk    #1,PRG_status   ;Error in Program stage
                b       PRG_stop
;
;------------------------------------------------------------------------------
; Name: PRG_erase
; 
; Description
;   Erase a programmed device. This module will be executed if the PRG2xxw
;   command line option specified is -e.
; Inputs
;   None:
;
; Outputs
;   int PRG_status      Pass =0; Fail = 1;
;
; Notes:
;   The erase is device specific. 
;------------------------------------------------------------------------------
PRG_erase:    

	    LDP	    #0E0h                         ;Setup DP
	    SPLK	#PLL_RATIO_CONST,7018h    ;Set up PLL mult.
	    SPLK	#000bh,7019h              ;Set up SCSR2.BRL=0,MC/=0,PON=1,DON=1
	    SPLK	#6fh,7029h                ;Stop watchdog.

CALL_CLEAR:
        LDP     #PRG_options              ;Get sector command from cmd line.
        LACC    PRG_options               ;
 
        LDP     #flashAlgoVars.SECTOR_CMD ; Store it in SECTOR_MASK.
        SACL    flashAlgoVars.SECTOR_CMD              

        CALL    CLEAR_FLASH

CHECK_ERROR:
        LACC    flashAlgoVars.ALGO_STATUS ; Get the algo error code.
        LDP     #PRG_status
        SACL    PRG_status                ; Copy the error back to the variable
                                          ; which prg2xx looks at.
        b       PRG_stop

;
;------------------------------------------------------------------------------ 
; Name: PRG_verify
; 
; Description
;   Verify a block of programmed data. This module will be excuted if the
;   command line option in PRG2xxw is -v.
; s
; Inputs                        Defined by host
;   unsigned *PRG_paddr         Pointer to first address to verify
;   unsigned  PRG_page          Page to verify, Program == 0, Data == 1
;   unsigned  PRG_length        Number of words to verify
;   unsigned *PRG_bufaddr       Pointer to verify data buffer
;
; Outputs
;   int PRG_status      Pass =0; Fail = 1;
;
; Notes:
;   The PRG_page value is setup by the host but not used in this case.
;   The assumption is that the programmable device is in program space.
;------------------------------------------------------------------------------ 
 
PRG_verify:
                ldp     #PARMS
error:          splk    #1,PRG_status   ;
                b       PRG_stop        ;Generate a breakpoint

;
;------------------------------------------------------------------------------ 
; Name: PRG_stop
; 
; Description
;       Exit routine for all programming functions.
; 
; Inputs
;       None
;
;
; Outputs
;       None
;
; Notes:
;       Exit point for all programming functions
;       The ESTOP opcode gets executed as a NOP when not in emulation
;       mode.  The "b $" will keep the program from running off if
;       not in emulation mode.
;
;------------------------------------------------------------------------------ 
PRG_stop:

        nop
        .word   0BE90h                  ;SWI instruction
        nop
        b $
                .end



