/*------------------------------------------------------- 
 * インテルHEXデコーダー
 *-------------------------------------------------------*/
#include <xc.h>
#include <stdint.h>
#include "cons.h"
#include "ihex.h"
#include "devinfo.h"

static uint32_t ihex_base = 0;

static enum ihex_ret_e ihex_read_line_main(uint8_t dev,
                                           enum ihex_cmd_e cmd);
static void ihex_read_start(void);
static char ihex_read_char(void);
static void ihex_skip_to_newline(void);

/* デコーダー初期化 */
void ihex_init(void)
{
  ihex_base = 0;
}

/* １行デコード */
enum ihex_ret_e ihex_read_line(uint8_t dev,
                               enum ihex_cmd_e cmd)
{
  ihex_read_start();
  enum ihex_ret_e r = ihex_read_line_main(dev, cmd);
  ihex_skip_to_newline();
  return r;
}

/* １行デコード本体 */
static enum ihex_ret_e ihex_read_line_main(uint8_t dev,
                                           enum ihex_cmd_e cmd)
{
  uint16_t ihex_addr   = 0;
  uint32_t ihex_exaddr = 0;
  uint8_t  ihex_bytcnt = 0;
  uint8_t  ihex_rectyp = 0;
  uint8_t  ihex_data   = 0;
  uint8_t  ihex_sum    = 0;
  uint8_t  ihex_chksum = 0;
  uint8_t  v0 = 0;
  uint8_t  v1 = 0;
  uint8_t  v2 = 0;
  uint8_t  v3 = 0;
  int16_t  v = 0;
  uint8_t  i = 0;
  char ch;
  /* 行開始 */
  ihex_sum = 0;
  /* 開始コード */
  if (ihex_read_char() != ':') {
    return IHEX_ERR_FORMAT;
  }
  /* バイトカウント */
  if ((v = ihex_read_1byte()) < 0) {
    return IHEX_ERR_FORMAT;
  }
  v0 = (uint8_t) v;
  ihex_sum += v0;
  ihex_bytcnt = v0;
  /* アドレス */
  if ((v = ihex_read_1byte()) < 0) {
    return IHEX_ERR_FORMAT;
  }
  v0 = (uint8_t) v;
  ihex_sum += v0;
  if ((v = ihex_read_1byte()) < 0) {
    return IHEX_ERR_FORMAT;
  }
  v1 = (uint8_t) v;
  ihex_sum += v1;
  ihex_addr = ((uint16_t) v0 << 8) | ((uint16_t) v1);
  /* レコード型 */
  if ((v = ihex_read_1byte()) < 0) {
    return IHEX_ERR_FORMAT;
  }
  ihex_sum += (uint8_t)v;
  ihex_rectyp = (uint8_t)v;
  enum ihex_ret_e r = IHEX_OK;
  switch (ihex_rectyp) {
  case 0: /* データ, 16ビットアドレッシング */
    ihex_exaddr = ihex_addr + ihex_base;
    if (cmd == IHEX_CMD_WRITE) {
      (*devs[dev].f_write_begin)(dev, ihex_exaddr, ihex_bytcnt);
    } else if (cmd == IHEX_CMD_VERIFY) {
      (*devs[dev].f_read_begin)(dev, ihex_exaddr, ihex_bytcnt);
    }
    for ( ; ihex_bytcnt != 0; ihex_bytcnt--, ihex_exaddr++) {
      if (ihex_exaddr >= devs[dev].size_in_byte) {
        return IHEX_ERR_ADDR;
      }
      if ((v = ihex_read_1byte()) < 0) {
        return IHEX_ERR_FORMAT;
      }
      ihex_sum += (uint8_t)v;
      ihex_data = (uint8_t)v;
      if (cmd == IHEX_CMD_WRITE) {
        (*devs[dev].f_write_byte)(dev, ihex_data);
      } else if (cmd == IHEX_CMD_VERIFY) {
        uint8_t mem_data = (*devs[dev].f_read_byte)(dev);
        if (mem_data != ihex_data) {
          r = IHEX_ERR_VERIFY;
        }
      }
    }
    if (cmd == IHEX_CMD_WRITE) {
      (*devs[dev].f_write_done)(dev);
    } else if (cmd == IHEX_CMD_VERIFY) {
      (*devs[dev].f_read_done)(dev);
    }
    if (r != IHEX_OK) {
      return r;
    }
    break;
  case 1: /* ファイル終端 */
    if (ihex_bytcnt != 0) {
      return IHEX_ERR_FORMAT;
    }
    break;
  case 2: /* 20ビットアドレッシング */
  case 4: /* 32ビットアドレッシング */
    if (ihex_bytcnt != 2) {
      return IHEX_ERR_FORMAT;
    }
    ihex_base = 0;
    for (i = 0; i < 2; i++) {
      if ((v = ihex_read_1byte()) < 0) {
        return IHEX_ERR_FORMAT;
      }
      ihex_sum += (uint8_t)v;
      ihex_base = (ihex_base << 8) | (uint8_t)v;
    }
    if (ihex_rectyp == 2) {
      ihex_base = ihex_base <<  4;
    } else {
      ihex_base = ihex_base << 16;
    }
    break;
  case 3: /*未対応*/
  case 5: /*未対応*/
    return IHEX_ERR_UNSUPP;
  }
  /* チェックサム */
  if ((v = ihex_read_1byte()) < 0) {
    return IHEX_ERR_FORMAT;
  }
  ihex_chksum = (uint8_t)v;
  if (((ihex_chksum + ihex_sum) & 0xFF) != 0) {
    return IHEX_ERR_CHECKSUM;
  }
  /* 改行文字 */
  if (ihex_read_char() != CHAR_CR) {
    return IHEX_ERR_FORMAT;
  }
  /* １行処理終了 */
  if (ihex_rectyp == 1) {
    return IHEX_EOF;
  }
  return IHEX_OK;
}

/* 16進数1文字を数値へ */
int16_t ihex_read_1byte(void)
{
  int16_t value = 0;
  uint8_t n;
  for  (n = 0; n < 2; n++) {
    char ch = ihex_read_char();
    int8_t v4 = 0;
    if (('0' <= ch) && (ch <= '9')) {
      v4 = ch - '0';
    } else if (('A' <= ch) && (ch <= 'F')) {
      v4 = ch - 'A' + 10;
    } else if (('a' <= ch) && (ch <= 'f')) {
      v4 = ch - 'a' + 10;
    } else {
      value = -1;
      break;
    }
    value = (value << 4) + v4;
  }
  return value;
}

/* 1文字読み込み & 改行までの読み飛ばし */

enum ihex_char_pos_e {
  IHEX_CHAR_POS_MOL = 0, /* 行中 */
  IHEX_CHAR_POS_EOL      /* 行末 */
};

static uint8_t ihex_read_newline = IHEX_CHAR_POS_MOL;

static void ihex_read_start(void)
{
  ihex_read_newline = IHEX_CHAR_POS_MOL;
}

static void ihex_skip_to_newline(void)
{
  while (ihex_read_newline != IHEX_CHAR_POS_EOL) {
    ihex_read_char();
  }
}

static char ihex_read_char(void)
{
  char ch = CHAR_CR;
  if (ihex_read_newline == IHEX_CHAR_POS_MOL) {
    int16_t c = 0;
    while ((c = cons_getc()) < 0) {
      CLRWDT();
    }
    ch = (char)c;
    if ((ch == CHAR_CR) || (ch == CHAR_LF)) {
      ihex_read_newline = IHEX_CHAR_POS_EOL;
      ch = CHAR_CR;
    }
  }
  return ch;
}
