-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.cpp
More file actions
423 lines (349 loc) · 12.2 KB
/
Copy pathCommon.cpp
File metadata and controls
423 lines (349 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* This file is part of NEMIGABTL.
NEMIGABTL is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
NEMIGABTL 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
NEMIGABTL. If not, see <http://www.gnu.org/licenses/>. */
// Common.cpp
#include "stdafx.h"
#include "Emulator.h"
//////////////////////////////////////////////////////////////////////
DWORD m_dwTotalEmulatorUptime = 0; // Total uptime, seconds
int m_nCommon_TestsStarted = 0;
BOOL m_okCommon_CurrentTestFailed = FALSE;
int m_nCommon_TestsFailed = 0;
BOOL AssertFailedLine(LPCSTR lpszFileName, int nLine)
{
DebugPrintFormat(_T("ASSERT in %s at line %n"), lpszFileName, nLine);
return FALSE;
}
void AlertWarning(LPCTSTR sMessage)
{
Test_Log('*', sMessage);
}
void AlertWarningFormat(LPCTSTR /*sFormat*/, ...)
{
//TODO: Implement in this environment
}
BOOL AlertOkCancel(LPCTSTR /*sMessage*/)
{
//TODO: Implement in this environment
return FALSE;
}
//////////////////////////////////////////////////////////////////////
// DebugPrint and DebugLog
#if !defined(PRODUCT)
void DebugPrint(LPCTSTR message)
{
Test_Log('d', message);
}
void DebugPrintFormat(LPCTSTR pszFormat, ...)
{
TCHAR buffer[512];
va_list ptr;
va_start(ptr, pszFormat);
_vsntprintf_s(buffer, 512, 512 - 1, pszFormat, ptr);
va_end(ptr);
Test_Log('d', buffer);
}
const LPCTSTR TRACELOG_FILE_NAME = _T("trace.log");
const LPCTSTR TRACELOG_NEWLINE = _T("\r\n");
HANDLE Common_LogFile = NULL;
void DebugLogCreateFile()
{
if (Common_LogFile == NULL)
{
Common_LogFile = ::CreateFile(TRACELOG_FILE_NAME,
GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
void DebugLogCloseFile()
{
if (Common_LogFile == NULL)
return;
::CloseHandle(Common_LogFile);
Common_LogFile = NULL;
}
void DebugLogClear()
{
DebugLogCreateFile();
if (Common_LogFile != NULL)
{
// Trunkate to zero length
::SetFilePointer(Common_LogFile, 0, 0, 0);
::SetEndOfFile(Common_LogFile);
}
}
void DebugLog(LPCTSTR message)
{
DebugLogCreateFile();
SetFilePointer(Common_LogFile, 0, NULL, FILE_END);
DWORD dwLength = lstrlen(message) * sizeof(TCHAR);
char ascii[256]; *ascii = 0;
WideCharToMultiByte(CP_ACP, 0, message, dwLength, ascii, 256, NULL, NULL);
DWORD dwBytesWritten = 0;
//WriteFile(Common_LogFile, message, dwLength, &dwBytesWritten, NULL);
WriteFile(Common_LogFile, ascii, (DWORD)strlen(ascii), &dwBytesWritten, NULL);
//dwLength = lstrlen(TRACELOG_NEWLINE) * sizeof(TCHAR);
//WriteFile(Common_LogFile, TRACELOG_NEWLINE, dwLength, &dwBytesWritten, NULL);
}
void DebugLogFormat(LPCTSTR pszFormat, ...)
{
TCHAR buffer[512];
va_list ptr;
va_start(ptr, pszFormat);
_vsntprintf_s(buffer, 512, 512 - 1, pszFormat, ptr);
va_end(ptr);
DebugLog(buffer);
}
#endif // !defined(PRODUCT)
//////////////////////////////////////////////////////////////////////
// Íàçâàíèÿ ðåãèñòðîâ ïðîöåññîðà
const TCHAR* REGISTER_NAME[] = { _T("R0"), _T("R1"), _T("R2"), _T("R3"), _T("R4"), _T("R5"), _T("SP"), _T("PC") };
// Print octal 16-bit value to buffer
// buffer size at least 7 characters
void PrintOctalValue(TCHAR* buffer, WORD value)
{
for (int p = 0; p < 6; p++)
{
int digit = value & 7;
buffer[5 - p] = _T('0') + (TCHAR)digit;
value = (value >> 3);
}
buffer[6] = 0;
}
// Print hex 16-bit value to buffer
// buffer size at least 5 characters
void PrintHexValue(TCHAR* buffer, WORD value)
{
for (int p = 0; p < 4; p++)
{
int digit = value & 15;
buffer[3 - p] = (digit < 10) ? _T('0') + (TCHAR)digit : _T('a') + (TCHAR)(digit - 10);
value = (value >> 4);
}
buffer[4] = 0;
}
// Print binary 16-bit value to buffer
// buffer size at least 17 characters
void PrintBinaryValue(TCHAR* buffer, WORD value)
{
for (int b = 0; b < 16; b++)
{
int bit = (value >> b) & 1;
buffer[15 - b] = bit ? _T('1') : _T('0');
}
buffer[16] = 0;
}
// Parse octal value from text
BOOL ParseOctalValue(LPCTSTR text, WORD* pValue)
{
WORD value = 0;
TCHAR* pChar = (TCHAR*) text;
for (int p = 0; ; p++)
{
if (p > 6) return FALSE;
TCHAR ch = *pChar; pChar++;
if (ch == 0) break;
if (ch < _T('0') || ch > _T('7')) return FALSE;
value = (value << 3);
TCHAR digit = ch - _T('0');
value += digit;
}
*pValue = value;
return TRUE;
}
// NEMIGA to Unicode conversion table
const TCHAR BK_CHAR_CODES[] =
{
0x3C0, 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,
0x44E, 0x430, 0x431, 0x446, 0x434, 0x435, 0x444, 0x433, 0x445, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E,
0x43F, 0x44F, 0x440, 0x441, 0x442, 0x443, 0x436, 0x432, 0x44C, 0x44B, 0x437, 0x448, 0x44D, 0x449, 0x447, 0x44A,
0x42E, 0x410, 0x411, 0x426, 0x414, 0x415, 0x424, 0x413, 0x425, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E,
0x41F, 0x42F, 0x420, 0x421, 0x422, 0x423, 0x416, 0x412, 0x42C, 0x42B, 0x417, 0x428, 0x42D, 0x429, 0x427, 0x42A,
};
// Translate one KOI8-R character to Unicode character
TCHAR Translate_BK_Unicode(BYTE ch)
{
if (ch < 32) return _T('·');
if (ch < 127) return (TCHAR) ch;
if (ch == 127) return (TCHAR) 0x25A0;
if (ch >= 128 && ch < 160) return _T('·');
return BK_CHAR_CODES[ch - 160];
}
// KOI7-R (Russian) to Unicode conversion table
const TCHAR KOI7R_CODES[] =
{
0x44E, 0x430, 0x431, 0x446, 0x434, 0x435, 0x444, 0x433, 0x445, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43E,
0x43F, 0x44F, 0x440, 0x441, 0x442, 0x443, 0x436, 0x432, 0x44C, 0x44B, 0x437, 0x448, 0x44D, 0x449, 0x447, 0x44A,
0x42E, 0x410, 0x411, 0x426, 0x414, 0x415, 0x424, 0x413, 0x425, 0x418, 0x419, 0x41A, 0x41B, 0x41C, 0x41D, 0x41E,
0x41F, 0x42F, 0x420, 0x421, 0x422, 0x423, 0x416, 0x412, 0x42C, 0x42B, 0x417, 0x428, 0x42D, 0x429, 0x427, 0x42A,
};
// Translate one KOI7 character to Cyrillic Unicode character
TCHAR Translate_KOI7R_Unicode(BYTE ch)
{
if (ch < 64 || ch >= 128) return (TCHAR) ch;
return KOI7R_CODES[ch - 64];
}
//////////////////////////////////////////////////////////////////////
// Path funcations
LPCTSTR GetFileNameFromFilePath(LPCTSTR lpfilepath)
{
LPCTSTR lpfilename = _tcsrchr(lpfilepath, _T('\\'));
if (lpfilename == NULL)
return lpfilepath;
else
return lpfilename + 1;
}
//////////////////////////////////////////////////////////////////////
void Test_Log(char eventtype, LPCTSTR message)
{
HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
WORD fgcolor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
if (eventtype == 'E')
{
fgcolor = FOREGROUND_RED | FOREGROUND_INTENSITY;
m_okCommon_CurrentTestFailed = true;
}
else if (eventtype == '!')
fgcolor = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
else if (eventtype == '*')
fgcolor = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
//TODO: Show BK uptime
SYSTEMTIME stm;
::GetLocalTime(&stm);
::SetConsoleTextAttribute(hStdOut, fgcolor);
printf("%02d:%02d:%02d.%03d %c %S\n",
stm.wHour, stm.wMinute, stm.wSecond, stm.wMilliseconds,
eventtype, message);
::SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void Test_LogFormat(char eventtype, LPCTSTR format, ...)
{
TCHAR buffer[512];
va_list ptr;
va_start(ptr, format);
_vsntprintf_s(buffer, 512, 512 - 1, format, ptr);
va_end(ptr);
Test_Log(eventtype, buffer);
}
void Test_Init(LPCTSTR sTestTitle, WORD configuration)
{
Test_Log('!', sTestTitle);
m_nCommon_TestsStarted++;
m_okCommon_CurrentTestFailed = false;
if (! Emulator_Init())
Test_Log('E', _T("FAILED to initialize the emulator."));
if (! Emulator_InitConfiguration(configuration))
Test_Log('E', _T("FAILED to initialize the emulator configuration."));
}
void Test_ChangeConfiguration(WORD configuration)
{
if (!Emulator_Init())
Test_Log('E', _T("FAILED to initialize the emulator."));
if (!Emulator_InitConfiguration(configuration))
Test_Log('E', _T("FAILED to initialize the emulator configuration."));
}
void Test_Done()
{
m_dwTotalEmulatorUptime += Emulator_GetUptime();
Emulator_Done();
if (m_okCommon_CurrentTestFailed)
m_nCommon_TestsFailed++;
}
void Test_LogSummary()
{
Test_LogFormat('i', _T("Emulator time spent: %u seconds"), m_dwTotalEmulatorUptime);
char evtype = (m_nCommon_TestsFailed == 0) ? '!' : 'E';
Test_LogFormat(evtype, _T("TOTAL tests started: %u, failed: %u"), m_nCommon_TestsStarted, m_nCommon_TestsFailed);
}
void Test_AssertFailedLine(LPCSTR lpszFileName, int nLine)
{
Test_LogFormat('E', _T("Test assertion failed: File: %S Line: %d"), lpszFileName, nLine);
}
void Test_AttachFloppyImage(int slot, LPCTSTR sFilePath)
{
BOOL res = Emulator_AttachFloppyImage(slot, sFilePath);
if (!res)
Test_LogFormat('E', _T("FAILED to attach floppy image %s"), sFilePath);
}
void Test_SaveScreenshot(LPCTSTR sFileName)
{
if (Emulator_SaveScreenshot(sFileName))
Test_LogFormat('*', _T("Saved screenshot %s"), sFileName);
else
Test_LogFormat('E', _T("FAILED to save screenshot %s"), sFileName);
}
void Test_SaveScreenshotSeria(LPCTSTR sFileNameTemplate, int count, int frameStep)
{
TCHAR buffer[255];
for (int i = 0; i < count; i++)
{
swprintf(buffer, 255, sFileNameTemplate, i);
Test_SaveScreenshot(buffer);
Emulator_Run(frameStep);
}
}
void Test_CheckScreenshot(LPCTSTR sFileName)
{
int diff = Emulator_CheckScreenshot(sFileName);
if (diff == 0)
{
Test_LogFormat('i', _T("Checked screenshot %s"), sFileName);
return;
}
if (diff < 0)
{
Test_LogFormat('E', _T("ERROR checking screenshot %s"), sFileName);
exit(1);
}
Test_LogFormat('E', _T("TEST FAILED checking screenshot %s, diff %d"), sFileName, diff);
}
void Test_CopyFile(LPCTSTR sFileNameFrom, LPCTSTR sFileNameTo)
{
if (!CopyFile(sFileNameFrom, sFileNameTo, FALSE))
{
Test_LogFormat('E', _T("ERROR copying file %s to %s"), sFileNameFrom, sFileNameTo);
exit(1);
}
Test_LogFormat('i', _T("Copyed file %s to %s"), sFileNameFrom, sFileNameTo);
}
void Test_CreateDiskImage(LPCTSTR sFileName)
{
LONG fileSize = 235392; // = 80 * 23 * 128 - 128
HANDLE hFile = ::CreateFile(sFileName,
GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
Test_LogFormat('E', _T("FAILED to create disk image %s"), sFileName);
return;
}
// Zero-fill the file
::SetFilePointer(hFile, fileSize, NULL, FILE_BEGIN);
::SetEndOfFile(hFile);
::CloseHandle(hFile);
}
void Test_SaveStateImage(LPCTSTR sFileName)
{
if (Emulator_SaveImage(sFileName))
Test_LogFormat('i', _T("Saved state image %s"), sFileName);
else
Test_LogFormat('E', _T("FAILED to save state image %s"), sFileName);
}
void Test_LoadStateImage(LPCTSTR sFileName)
{
if (Emulator_LoadImage(sFileName))
Test_LogFormat('i', _T("Loaded state image %s"), sFileName);
else
Test_LogFormat('E', _T("FAILED to load state image %s"), sFileName);
}
void Test_AssertFailed(LPCSTR sFileName, int nLine)
{
Test_LogFormat('E', _T("ASSERT FAILED in %S at line %d"), sFileName, nLine);
}
//////////////////////////////////////////////////////////////////////