/*
 *
 *  ALL03
 *
 *  26.07.2002
 *
 *  by Fabio Sturman
 *
 *  fabio.sturman@tiscali.it
 *
 *  90s8515.h
 *
 *  main module include file
 *
 */

/* Copyright (c) 2002 Fabio Sturman (fabio.sturman@tiscali.it)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/*- AT90S8515 Level 1 --------------------------------------*/

#define NRESET  9
#define NOE    12
#define NWR    13
#define BS     14
#define XA0    15
#define XA1    16
#define XTAL1  19
#define RDY    11

#define wr_noe(x) all03_wr_pin(NOE,x)
#define wr_nwr(x) all03_wr_pin(NWR,x)
#define wr_bs(x) all03_wr_pin(BS,x)
#define wr_xa0(x) all03_wr_pin(XA0,x)
#define wr_xa1(x) all03_wr_pin(XA1,x)
#define wr_xtal1(x) all03_wr_pin(XTAL1,x)
#define rd_rdy() all03_rd_pin(RDY)
#define wr_rdy(x) all03_wr_pin(RDY,x)
#define wr_nreset(x) all03_wr_pin(NRESET,x)
#define set_nreset_12v() all03_set_vhh_pin(NREST,1)

/*- AT90S8515 Level 2 --------------------------------------*/

#define COMMAND_CHIP_ERASE            0x80
#define COMMAND_READ_SIGNATURE        0x08
#define COMMAND_READ_FLASH            0x02
#define COMMAND_WRITE_FLASH           0x10
#define COMMAND_READ_EEPROM           0x03
#define COMMAND_WRITE_EEPROM          0x11
#define COMMAND_WRITE_FUSE_BITS       0x40
#define COMMAND_WRITE_LOCK_BITS       0x20
#define COMMAND_READ_LOCK_FUSE_BITS   0x04

#define MAX_FLASH_ADDRESS_H           0x10
#define MAX_EEPROM_ADDRESS_H          0x02

#define PROC_STRING "\nInsert AT90S8515 and press <ENTER>\n\n"


/*- AT90S8515 Level 1 --------------------------------------*/

/* write to portb of uC */
void wr_pb(unsigned char byt);
/* read portb of uC */
unsigned char rd_pb(void);
/* load command into uC */
void ld_cmd(int bs, unsigned char cmd);
/* load address (low if bs=0, high if bs=1) into uC */
void ld_addr(int bs, unsigned char addr);
/* load data into uC */
void ld_data(int bs, unsigned char dat);
/* read data from uC */
unsigned char rd_chip_data(void);
/* put chip in programming mode */
void chip_init(void);
/* negative pulse /wr */
void pulse_nwr(void);
/* wait while rdy=0  with timeout
   if timeout returns 1
   no timeout returns 0
*/
int wait_rdy(void);

/*- AT90S8515 Level 2 --------------------------------------*/

/*
 *  disp_lock_fuse()
 *
 *  display lock bits and fuses
 *
 */
void disp_lock_fuse(void);
/*
 *  edit_lock_fuse()
 *
 *  edit lock bits and fuses
 *
 */
void edit_lock_fuse(void);
/*
 *  read_lock_fuse()
 *
 *  read lock bits and fuses
 *  from chip
 *
 */
void read_lock_fuse(void);
/*
 *  write_fuse()
 *
 *  write fuses
 *
 */
void write_fuse(void);
/*
 *  write_lock()
 *
 *  write lock bits
 *
 */
void write_lock(void);
/*
 *  erase()
 *
 *  erase flash, eeprom and lock bits
 *  fuses unchanged
 *
 */
void erase(void);
/*
 *  read_signature()
 *
 *  reads signature bytes
 *
 *  SIG 0 - 0x00 manifactured by Atmel
 *  SIG 1 - 0x93 8k flash
 *  SIG 2 - 0x01 indicates AT90S8515 if SIG 1=0x93
 *
 */
void read_signature(void);
/*
 *  write_flash()
 *
 *  write flash buffer to uC flash memory
 *
 */
void write_flash(void);
/*
 *  read_flash()
 *
 *  reads uC flash memory into flash buffer
 *
 */
void read_flash(void);
/*
 *  read_eeprom()
 *
 *  reads uC eeprom memory into eeprom buffer
 *
 */
void read_eeprom(void);
/*
 *  blank_check_eeprom()
 *
 *  verifies if all eeproms cells =0xff
 *
 */
void blank_check_eeprom(void);
/*
 *  verify_eeprom()
 *
 *  verifies uC eeprom with eeprom buffer
 *
 */
void verify_eeprom(void);
/*
 *  write_eeprom()
 *
 *  writes eeprom buffer into uC eeprom memory
 *
 */
void write_eeprom(void);
/*
 *  blak_check()
 *
 *  verifies if all uC flash memory cells=0xff
 *
 */
void blank_check(void);
/*
 *  verify_flash()
 *
 *  verifies uC flash memory with flash buffer
 *
 */
void verify_flash(void);
/*
 *  read_flash_buf_from_file()
 *
 *  reads data for flash buffer from disk
 *
 */
void read_flash_buf_from_file(void);
/*
 *  write_flash_buf_to_file()
 *
 *  writes data from flash buffer to disk
 *
 */
void write_flash_buf_to_file(void);
/*
 *  read_eeprom_buf_from_file()
 *
 *  reads data for eeprom buffer from disk
 *
 */
void read_eeprom_buf_from_file(void);
/*
 *  write_eeprom_buf_to_file()
 *
 *  writes data from eeprom buffer to disk
 *
 */
void write_eeprom_buf_to_file(void);
/* displays help screen */
void display_help(void);
/* main program */
int main(int argc, char *argv[]);

