/*

  Zerocat Chipflasher --- Flash free firmware, kick the Management Engine.

  Copyright (C) 2017, 2018, 2020, 2021, 2022  Kai Mertens <kmx@posteo.net>

  This file is part of Zerocat Chipflasher.

  Zerocat Chipflasher 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 3 of the
  License, or (at your option) any later version.

  Zerocat Chipflasher 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 Zerocat Chipflasher.
  If not, see <http://www.gnu.org/licenses/>.


***/


# ifndef __LINESPEC_H__
#   define __LINESPEC_H__

// Size of the linespec array (we have two file types only)
#   define LINESPEC_ARRAYSIZE   4

// Typical header of a data file created by the chipflasher.
//
// With additional Information:
//
// - Names of the chip according to chipspec database
// - Type of memory, i.e.: STR_SOTP or STR_ARRAY
//
//  Check SIZE_STRBUF:
//  Size=48+19+'0'=68:  ...     "0123456789012345678901234567890123456789012345678901234567890123456789"
#   define HEADER               "Zerocat Chipflasher (http://www.zerocat.org/) | %s%s"

#   define STR_SOTP             " (SOTP Area)"
#   define STR_ARRAY            " (Main Array)"
#   define FMT_HEADHEXD         "#header: %s" CRNL
#   define FMT_TAILHEXD         "#lines: %d"  CRNL
#   define STR_HEXD             "Hex-Dump"
#   define STR_SRECORD          "S-Record"

// Base payload for all line types.
#   define BASE_PAYLOAD         16

// Maximal payload of an S-Record line:
// 252 for S1, 251 for S2, 250 for S3 Record Types
// A maximal payload > 250 will be adjusted during runtime.
#   define MAX_PAYLOAD_SREC     252

// Maximal payload of an hexadecimal dump line.
#   define MAX_PAYLOAD_HEXD     BASE_PAYLOAD

// Default payload of an S-Record line.
#   define PAYLOAD_SREC         BASE_PAYLOAD << 2

// Default, fixed payload of an hexadecimal dump line.
#   define PAYLOAD_HEXD         BASE_PAYLOAD

// Maximal size of a data line. This value should match Motorola S specifications.
#   define SIZE_LINEBUF         520

// Size (number of characters) of a line frame, Motorola S Record.
#   define FRAME_SREC           17

// Size (number of characters) of a line frame, Hex-Dump.
#   define FRAME_HEXD           15

//modes for mode_0xff, used by chip_read()
// This flag is not a flag but exact information about the 0xff runlength, 0x1f bytes as maximum.
#   define MODE_INLINE          0x1F

// The flag forces data lines to be split into lines of data and lines of just 0xff bytes.
#   define MODE_STRIP           0x20

// This flag forces lines of just 0xff bytes to be deleted.
#   define MODE_SPLIT           0x40

// This mode bit is not strictly related to “0xff data handling”.
#   define MODE_SCREEN_OUTPUT   0x80

// Recommended settings for a Hex-Dump Line: No modification, no runlength for 0xff bytes.
#   define FFMODE_HEXD          (MODE_INLINE & 0x00)

// Recommended settings for a Motorola S Line: Split and strip 0xff data, but allow a maximal length of 0x10 bytes.
#   define FFMODE_SREC          ((MODE_INLINE & 0x10) | MODE_SPLIT | MODE_STRIP)

// In contrast to Motorola S, the length of a Hex-Dump line can be calculated right here.
#   define HEXD_LINELEN         (10 + PAYLOAD_HEXD * 4 + 2 + 2)


enum LINETYPE_t {
  LINETYPE_SREC = 0,            //Line type indicator: Motorola S with hexadecimal or binary payload
  LINETYPE_HEXD                 //Line type indicator: hexadecimal dump (no checksum per line)
};

enum TYPEOFPAYLOAD_t {
  PAYLOAD_BIN = 0,              //used with rxline_SREC: payload has binary values
  PAYLOAD_HEX,                  //used with rxline_SREC: payload has character pairs
  PAYLOAD_SOTP_AREA,            //used with rxline_HEXD: payload should go to SOTP region
  PAYLOAD_MAIN_ARRAY            //used with rxline_HEXD: payload should NOT go to SOTP region
};

enum LINESPECINDEX_t {
  LINESPECINDEX_SRECHEX = 0,
  LINESPECINDEX_SRECBIN,
  LINESPECINDEX_HEXDMAIN,
  LINESPECINDEX_HEXDSOTP
};

// Line type specifications
struct LINESPEC_t {
  const enum LINETYPE_t linetype;                 //Type of the line, i.e. LINETYPE_HEXD or LINETYPE_SREC.
  enum TYPEOFPAYLOAD_t type_of_payload;           //Payload type or intention
  unsigned char payload;                          //Size of payload, i.e.: number of bytes
  unsigned char mode_0xff;                        //How to deal with 0xff data?
  const char * linetype_name;                     //String that holds the name of the line type.
};

# endif
/* __LINESPEC_H__ */

