/*************************************************
*            BK Emulator for Window 3.0          *
*************************************************/

/*
File: Globals.h
Version: 1.0.0
Written by: Yuriy Kalmykov <kalmykov@stoik.com>
    Copyright (c) 2002-2004 Yuriy Kalmykov

    BK Emulator is a program emulated hardware environment for running
code for BK 0010(01) in different configurations.

    This code may be used in compiled form in any way you desire.
This file or it's parts can't be redistributed without the authors
written consent, but can be modified for your private needs.
    Providing that this notice and the authors name and all copyright
notices remains intact.

    Please, an email me to know how you are using it and where. You can
ask me for any information about this below code or any attendant
knowledge.

    This file is provided "as is" with no expressed or implied warranty.
The author accepts no liability for any damage or loss of business that
this product may cause.
*/


// Globals.h

#pragma once
#include "resource.h"
#include "Defines.h"
#include "Config.h"
#include "MSF.h"

extern CConfig g_Config;


CString         WordToOctString(WORD word);
CString         ByteToOctString(BYTE byte);
WORD            OctStringToWord(const CString &str);
CString         IntToString(int iInt, int radix = 10);
CString         IntToFileLengthString(int iInt);
CString         MsTimeToTimeString(int msTime);

CString BKToUNICODE(BYTE *pBuff, int size);
TCHAR BKToUNICODE_Byte(BYTE b);
void UNICODEtoBK(CString &ustr, BYTE *pBuff, int bufSize, BOOL bFillBuf);
void ANSIToBK(BYTE &ch);

void GetLastErrorOut(LPTSTR lpszFunction);


inline int GetDigit(WORD word, int pos)
{
	return (pos ? (word >> ((pos << 1) + pos)) : word) & 7;
}

static CString getCompileDate(const LPCTSTR &p_format = _T("%Y-%m-%d"))
{
	COleDateTime tCompileDate;
	tCompileDate.ParseDateTime(_T(__DATE__), LOCALE_NOUSEROVERRIDE, 1033);
	return tCompileDate.Format(p_format).GetString();
}

static CString getCompileTime(const LPCTSTR &p_format = _T("%H-%M-%S"))
{
	COleDateTime tCompileDate;
	tCompileDate.ParseDateTime(_T(__TIME__), LOCALE_NOUSEROVERRIDE, 1033);
	return tCompileDate.Format(p_format).GetString();
}

/* это правильная таблица, но на бк всё по особому, даже кои8 там нестандартный
//таблица соответствия верхней половины аскии кодов с 128 по 255, включая псевдографику
static const TCHAR koi8tbl[128] = {
    0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590,
    0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2219, 0x221A, 0x2248, 0x2264, 0x2265, 0xA0,   0x2321, 0xB0,   0xB2,   0xB7,   0xF7,
    0x2550, 0x2551, 0x2552, 0x451,  0x2553, 0x2554, 0x2555, 0x2556, 0x2557, 0x2558, 0x2559, 0x255A, 0x255B, 0x255C, 0x255D, 0x255E,
    0x255F, 0x2560, 0x2561, 0x401,  0x2562, 0x2563, 0x2564, 0x2565, 0x2566, 0x2567, 0x2568, 0x2569, 0x256A, 0x256B, 0x256C, 0xA9,
    _T('ю'), _T('а'), _T('б'), _T('ц'), _T('д'), _T('е'), _T('ф'), _T('г'), _T('х'), _T('и'), _T('й'), _T('к'), _T('л'), _T('м'), _T('н'), _T('о'),
    _T('п'), _T('я'), _T('р'), _T('с'), _T('т'), _T('у'), _T('ж'), _T('в'), _T('ь'), _T('ы'), _T('з'), _T('ш'), _T('э'), _T('щ'), _T('ч'), _T('ъ'),
    _T('Ю'), _T('А'), _T('Б'), _T('Ц'), _T('Д'), _T('Е'), _T('Ф'), _T('Г'), _T('Х'), _T('И'), _T('Й'), _T('К'), _T('Л'), _T('М'), _T('Н'), _T('О'),
    _T('П'), _T('Я'), _T('Р'), _T('С'), _T('Т'), _T('У'), _T('Ж'), _T('В'), _T('Ь'), _T('Ы'), _T('З'), _T('Ш'), _T('Э'), _T('Щ'), _T('Ч'), _T('Ъ')
};
*/
//вот бкшная таблица
//таблица соответствия верхней половины аскии кодов с 128 по 255, включая псевдографику
static const TCHAR koi8tbl[128] =
{
	//{200..237} этих символов на бк нету.
	_T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '),
	_T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '),
	_T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '),
	_T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '), _T(' '),
	//{240..277}
	0xb6,    0x2534,  0x2665,  0x2510,  0x2561,  0x251c,  0x2514,  0x2550,
	0x2564,  0x2660,  0x250c,  0x252c,  0x2568,  0x2193,  0x253c,  0x2551,
	0x2524,  0x2190,  0x256c,  0x2191,  0x2663,  0x2500,  0x256b,  0x2502,
	0x2666,  0x2518,  0x256a,  0x2565,  0x2567,  0x255e,  0x2192,  0x2593,
	//{300..337}
	_T('ю'), _T('а'), _T('б'), _T('ц'), _T('д'), _T('е'), _T('ф'), _T('г'),
	_T('х'), _T('и'), _T('й'), _T('к'), _T('л'), _T('м'), _T('н'), _T('о'),
	_T('п'), _T('я'), _T('р'), _T('с'), _T('т'), _T('у'), _T('ж'), _T('в'),
	_T('ь'), _T('ы'), _T('з'), _T('ш'), _T('э'), _T('щ'), _T('ч'), _T('ъ'),
	//{340..377}
	_T('Ю'), _T('А'), _T('Б'), _T('Ц'), _T('Д'), _T('Е'), _T('Ф'), _T('Г'),
	_T('Х'), _T('И'), _T('Й'), _T('К'), _T('Л'), _T('М'), _T('Н'), _T('О'),
	_T('П'), _T('Я'), _T('Р'), _T('С'), _T('Т'), _T('У'), _T('Ж'), _T('В'),
	_T('Ь'), _T('Ы'), _T('З'), _T('Ш'), _T('Э'), _T('Щ'), _T('Ч'), _T('Ъ')
};

static const unsigned char bk_codes[] =
{
	('б'), ('в'), ('ч'), ('з'), ('д'), ('е'), ('ц'), ('ъ'), ('й'), ('к'), ('л'), ('м'), ('н'), ('о'), ('п'), ('р'),
	('т'), ('у'), ('ф'), ('х'), ('ж'), ('и'), ('г'), ('ю'), ('ы'), ('э'), ('я'), ('щ'), ('ш'), ('ь'), ('а'), ('с'),
	('Б'), ('В'), ('Ч'), ('З'), ('Д'), ('Е'), ('Ц'), ('Ъ'), ('Й'), ('К'), ('Л'), ('М'), ('Н'), ('О'), ('П'), ('Р'),
	('Т'), ('У'), ('Ф'), ('Х'), ('Ж'), ('И'), ('Г'), ('Ю'), ('Ы'), ('Э'), ('Я'), ('Щ'), ('Ш'), ('Ь'), ('А'), ('С')
};

//последний элемент - идентификатор. что к данному приводу ничего не подключено.
static const CString g_mstrDrives[5] =
{
	_T("A:"), _T("B:"), _T("C:"), _T("D:"), _T("<empty>")
};
static const int g_mnDrivesIndx[4] =
{
	IDS_INI_DRIVEA, IDS_INI_DRIVEB, IDS_INI_DRIVEC, IDS_INI_DRIVED
};

//тут надо бы придумать более человеческий способ
//а то и енум и массив должны совпадать.
enum
{
	CONFIG_BKMODEL_STRING_BK_0010_01 = 0,
	CONFIG_BKMODEL_STRING_BK_0010_01_MSTD,
	CONFIG_BKMODEL_STRING_BK_0010_01_EXT32RAM,
	CONFIG_BKMODEL_STRING_BK_0010_01_FDD,
	CONFIG_BKMODEL_STRING_BK_0010_01_A16M,
	CONFIG_BKMODEL_STRING_BK_0010_01_SMK512,
	CONFIG_BKMODEL_STRING_BK_0011,
	CONFIG_BKMODEL_STRING_BK_0011_FDD,
	CONFIG_BKMODEL_STRING_BK_0011_SMK512,
	CONFIG_BKMODEL_STRING_BK_0011M,
	CONFIG_BKMODEL_STRING_BK_0011M_FDD,
	CONFIG_BKMODEL_STRING_BK_0011M_A16M,
	CONFIG_BKMODEL_STRING_BK_0011M_SMK512,
	CONFIG_BKMODEL_STRINGS_NUMBER
};

struct BK_MODEL_PARAMETERS
{
	CString BKModelName;
	int BKModel;
	BK_FDD_MODEL BKFDDModel;
};

static const BK_MODEL_PARAMETERS g_mstrConfigBKModelParameters[] =
{
	{_T("BK-0010-01"),          MSF_CONF_BK1001,        DEV_FDD_NONE},
	{_T("BK-0010-01_MSTD"),     MSF_CONF_BK1001_MSTD,   DEV_FDD_NONE},
	{_T("BK-0010-01_EXT32RAM"), MSF_CONF_BK1001_EXT32,  DEV_FDD_NONE},
	{_T("BK-0010-01_FDD"),      MSF_CONF_BK1001_FDD,    DEV_FDD_STD},
	{_T("BK-0010-01_A16M"),     MSF_CONF_BK1001_FDD,    DEV_FDD_A16M},
	{_T("BK-0010-01_SMK512"),   MSF_CONF_BK1001_FDD,    DEV_FDD_SMK512},
	{_T("BK-0011"),             MSF_CONF_BK11,          DEV_FDD_NONE},
	{_T("BK-0011_FDD"),         MSF_CONF_BK11_FDD,      DEV_FDD_STD},
	{_T("BK-0011_SMK512"),      MSF_CONF_BK11_FDD,      DEV_FDD_SMK512},
	{_T("BK-0011M"),            MSF_CONF_BK11M,         DEV_FDD_NONE},
	{_T("BK-0011M_FDD"),        MSF_CONF_BK11M_FDD,     DEV_FDD_STD},
	{_T("BK-0011M_A16M"),       MSF_CONF_BK11M_FDD,     DEV_FDD_A16M},
	{_T("BK-0011M_SMK512"),     MSF_CONF_BK11M_FDD,     DEV_FDD_SMK512}
};
