/*-----------------------------------------------------------------------------------
!
!                              COPYRIGHT 2015 BY
!                            MegaChips Corporation
!
!------------------------------------------------------------------------------------
! These coded instructions, statements, and computer programs include
! intellectual property and know-how belonging to MegaChips
! Corporaiton ("MCC") and its partner companies.
! Therefore, do not use these specifications for any purpose other than
! the purpose
! expressly agreed to by MCCs customer and MCC.  Also, do not duplicate,
! copy,
! or reproduce these specifications, nor reveal these specifications to
! any third party
! without MCCs prior written consent.
!
*/
/*-----------------------------------------------------------------------------------
  FILE        : sha_reg.h
  DESCRIPTION : sha Register
  Revision    : $Revision: 4 $
  Author      : $Author: shirait $
  Date        : $Date: 2015-09-09 15:19:58 +0900 (水, 09 9 2015) $
-----------------------------------------------------------------------------------*/
#ifndef ___SHA_REG_H_INCLUDED___
#define ___SHA_REG_H_INCLUDED___

#include <stdint.h>

#define SHA_REG_BASE (0x40007000u)
#define SHAR_HASH_LEN    (8*4)
#define SHAR_MGF_MAX_LEN (56*4)
#define SHAR_MSG_MAX_LEN (256*4)

typedef struct _tagShaReg {
	
	// #0000h
	union {
		uint8_t byte;
		struct {
			uint8_t en       : 1;
			uint8_t reserved : 7;
		} bit;
	} en;

	// #0001h - 0003h
	uint8_t reserved1[3];

	// #0004h
	union {
		uint8_t byte;
		struct {
			uint8_t s1fin    : 1;
			uint8_t s2fin    : 1;
			uint8_t smfin    : 1;
			uint8_t dmfin    : 1;
			uint8_t reserved : 4;
		} bit;
	} intst;

	// #0005h
	union {
		uint8_t byte;
		struct {
			uint8_t s1fin    : 1;
			uint8_t s2fin    : 1;
			uint8_t smfin    : 1;
			uint8_t dmfin    : 1;
			uint8_t reserved : 4;
		} bit;
	} inten;

	// #0006h - 0007h
	uint8_t reserved2[2];

	// #0008h - 0009h
	uint16_t dcnt;

	// #000Ah - 000Bh
	uint8_t reserved3[2];

	// #000Ch
	union {
		uint8_t byte;
		struct {
			uint8_t mode     : 2;
			uint8_t reserved : 6;
		} bit;
	} mode;

	// #000Dh - 000Fh
	uint8_t reserved4[3];

	// #0010h
	union {
		uint8_t byte;
		struct {
			uint8_t busy     : 1;
			uint8_t reserved1: 1;
			uint8_t sha2     : 1;
			uint8_t smgf     : 1;
			uint8_t dmgf     : 1;
			uint8_t reserved2: 3;
		} bit;
	} status;

	// #0011h - 0013h
	uint8_t reserved5[3];

	// #0014h
	union {
		uint8_t byte;
		struct {
			uint8_t sha2_mode : 3;
			uint8_t reserved  : 5;
		} bit;
	} sha2_mode;

	// #0015h
	union {
		uint8_t byte;
		struct {
			uint8_t key_sel   : 3;
			uint8_t reserved  : 5;
		} bit;
	} key_sel;

	// #0016h - 0017h
	uint8_t reserved6[2];
	
	// #0018h - 003Ch
	uint8_t reserved7[40];
	
	// #0040h
	union {
		uint8_t byte;
		struct {
			uint8_t keyin_sel : 1;
			uint8_t reserved  : 7;
		} bit;
	} keyin_sel;

	// #0041h - 0043h
	uint8_t reserved8[3];
	
	uint8_t reserved9[188];
	char    hash[SHAR_HASH_LEN];
	uint8_t reserved10[224];
	uint8_t mgf_msk[SHAR_MGF_MAX_LEN];
	uint8_t reserved11[288];
	uint8_t mes[SHAR_MSG_MAX_LEN];
} ShaReg;

#define SHAR (*(volatile ShaReg*)(SHA_REG_BASE))

#define SHAR_EN_EN (0x01)

#define SHAR_INTST_DMFIN (0x08u)
#define SHAR_INTST_SMFIN (0x04u)
#define SHAR_INTST_S2FIN (0x02u)
#define SHAR_INTST_S1FIN (0x01u)
#define SHAR_INTST_ALL_CLR (0x0fu)

#define SHAR_INTEN_DMFIN (0x08u)
#define SHAR_INTEN_SMFIN (0x04u)
#define SHAR_INTEN_S2FIN (0x02u)
#define SHAR_INTEN_S1FIN (0x01u)

#define SHAR_MODE_SHA1   (0x00u)
#define SHAR_MODE_SHA256 (0x01u)
#define SHAR_MODE_SM     (0x02u)
#define SHAR_MODE_DM     (0x03u)

#define SHAR_STATUS_DMGF (0x10u)
#define SHAR_STATUS_SMGF (0x08u)
#define SHAR_STATUS_SHA2 (0x04u)
#define SHAR_STATUS_SHA1 (0x02u)
#define SHAR_STATUS_BUSY (0x01u)

#define SHAR_SHA2MODE_NORMAL (0x00u)
#define SHAR_SHA2MODE_INIT   (0x04u)
#define SHAR_SHA2MODE_MID    (0x05u)
#define SHAR_SHA2MODE_LAST   (0x06u)
#define SHAR_SHA2MODE_KEY    (0x07u)

#define MAX_CALC_SIZE (1024)

#endif // ___SHA_REG_H_INCLUDED___
