-
Notifications
You must be signed in to change notification settings - Fork 0
/
Emulator.cpp
1221 lines (1085 loc) · 41.1 KB
/
Emulator.cpp
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of MS0515BTL.
MS0515BTL 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.
MS0515BTL 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
MS0515BTL. If not, see <http://www.gnu.org/licenses/>. */
// Emulator.cpp
#include "stdafx.h"
#include <stdio.h>
#include <Share.h>
#include "Main.h"
#include "Emulator.h"
#include "Views.h"
#include "emubase\Emubase.h"
#include "SoundGen.h"
//////////////////////////////////////////////////////////////////////
CMotherboard* g_pBoard = nullptr;
int g_nEmulatorConfiguration; // Current configuration
bool g_okEmulatorRunning = false;
int m_wEmulatorCPUBpsCount = 0;
uint16_t m_EmulatorCPUBps[MAX_BREAKPOINTCOUNT + 1];
uint16_t m_wEmulatorTempCPUBreakpoint = 0177777;
int m_wEmulatorWatchesCount = 0;
uint16_t m_EmulatorWatches[MAX_BREAKPOINTCOUNT + 1];
bool m_okEmulatorSound = false;
uint16_t m_wEmulatorSoundSpeed = 100;
bool m_okEmulatorCovox = false;
int m_nEmulatorSoundChanges = 0;
bool m_okEmulatorParallel = false;
bool m_okEmulatorSerial = false;
HANDLE m_hEmulatorComPort = INVALID_HANDLE_VALUE;
FILE* m_fpEmulatorParallelOut = nullptr;
long m_nFrameCount = 0;
uint32_t m_dwTickCount = 0;
long m_nUptimeFrameCount = 0;
uint32_t m_dwTotalFrameCount = 0;
uint8_t* g_pEmulatorRam = nullptr; // RAM values - for change tracking
uint8_t* g_pEmulatorChangedRam = nullptr; // RAM change flags
uint16_t g_wEmulatorCpuPC = 0177777; // Current PC value
uint16_t g_wEmulatorPrevCpuPC = 0177777; // Previous PC value
void CALLBACK Emulator_SoundGenCallback(uint16_t value);
//////////////////////////////////////////////////////////////////////
//Ïðîòîòèï ôóíêöèè ïðåîáðàçîâàíèÿ ýêðàíà
// Input:
// pVideoBuffer Èñõîäíûå äàííûå, áèòû ýêðàíà ÁÊ
// pPalette Ïàëèòðà
// pImageBits Ðåçóëüòàò, 32-áèòíûé öâåò, ðàçìåð äëÿ êàæäîé ôóíêöèè ñâîé
// hires Ïðèçíàê ðåæèìà âûñîêîãî ðàçðåøåíèÿ 640x200
// border Íîìåð öâåòà áîðäþðà 0..7
// blink Ôàçà ìåðöàíèÿ
typedef void (CALLBACK* PREPARE_SCREEN_CALLBACK)(
const uint8_t* pVideoBuffer, const uint32_t* pPalette, void* pImageBits,
bool hires, uint8_t border, bool blink);
void CALLBACK Emulator_PrepareScreen640x200(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
void CALLBACK Emulator_PrepareScreen360x220(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
void CALLBACK Emulator_PrepareScreen720x440(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
void CALLBACK Emulator_PrepareScreen880x660(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
void CALLBACK Emulator_PrepareScreen1080x660(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
void CALLBACK Emulator_PrepareScreen1280x880(const uint8_t*, const uint32_t*, void*, bool, uint8_t, bool);
struct ScreenModeStruct
{
int width;
int height;
PREPARE_SCREEN_CALLBACK callback;
}
static ScreenModeReference[] =
{
// wid hei callback size scaleX scaleY notes
{ 640, 200, Emulator_PrepareScreen640x200 }, // 640x200 1 1 Debug mode
{ 360, 220, Emulator_PrepareScreen360x220 }, // 320x200 0.5 1
{ 720, 440, Emulator_PrepareScreen720x440 }, // 640x400 1 2
{ 880, 660, Emulator_PrepareScreen880x660 }, // 800x600 1.25 3 4:3
{ 1080, 660, Emulator_PrepareScreen1080x660 }, // 960x600 1.5 3 Interlaced
{ 1280, 880, Emulator_PrepareScreen1280x880 }, // 1120x800 1.75 4 Interlaced
};
const uint32_t Emulator_Palette[24] =
{
0x000000, 0x0000FF, 0xFF0000, 0xFF00FF, 0x00FF00, 0x00FFFF, 0xFFFF00, 0xFFFFFF,
0x000000, 0x00007F, 0x7F0000, 0x7F007F, 0x007F00, 0x007F7F, 0x7F7F00, 0x7F7F7F,
0x101010, 0x0000EF, 0xEF0000, 0xEF00EF, 0x00EF00, 0x00EFEF, 0xEFEF00, 0xEFEFEF, // Border palette
};
//////////////////////////////////////////////////////////////////////
const LPCTSTR FILENAME_ROM_MS0515 = _T("ms0515.rom");
//////////////////////////////////////////////////////////////////////
bool Emulator_Init()
{
ASSERT(g_pBoard == nullptr);
CProcessor::Init();
m_wEmulatorCPUBpsCount = 0;
for (int i = 0; i <= MAX_BREAKPOINTCOUNT; i++)
{
m_EmulatorCPUBps[i] = 0177777;
}
m_wEmulatorWatchesCount = 0;
for (int i = 0; i <= MAX_WATCHPOINTCOUNT; i++)
{
m_EmulatorWatches[i] = 0177777;
}
g_pBoard = new CMotherboard();
// Allocate memory for old RAM values
g_pEmulatorRam = (uint8_t*) ::calloc(65536, 1);
g_pEmulatorChangedRam = (uint8_t*) ::calloc(65536, 1);
g_pBoard->Reset();
if (m_okEmulatorSound)
{
SoundGen_Initialize(Settings_GetSoundVolume());
g_pBoard->SetSoundGenCallback(Emulator_SoundGenCallback);
}
return true;
}
void Emulator_Done()
{
ASSERT(g_pBoard != nullptr);
CProcessor::Done();
g_pBoard->SetSoundGenCallback(nullptr);
SoundGen_Finalize();
g_pBoard->SetSerialCallbacks(nullptr, nullptr);
if (m_hEmulatorComPort != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hEmulatorComPort);
m_hEmulatorComPort = INVALID_HANDLE_VALUE;
}
delete g_pBoard;
g_pBoard = nullptr;
// Free memory used for old RAM values
::free(g_pEmulatorRam);
::free(g_pEmulatorChangedRam);
}
bool Emulator_InitConfiguration(int configuration)
{
g_pBoard->SetConfiguration((uint16_t)configuration);
uint8_t buffer[16384];
// Load ROM from the file, if found
FILE* fpFile = ::_tfsopen(FILENAME_ROM_MS0515, _T("rb"), _SH_DENYWR);
if (fpFile != nullptr)
{
size_t dwBytesRead = ::fread(buffer, 1, 16384, fpFile);
::fclose(fpFile);
if (dwBytesRead != 16384)
{
AlertWarning(_T("Failed to load the ROM file."));
return false;
}
}
else // ms0515.rom not found, use ROM image from resources
{
int romresid = configuration == 1 ? IDR_ROMA : IDR_ROMB;
HRSRC hRes = NULL;
DWORD dwDataSize = 0;
HGLOBAL hResLoaded = NULL;
void * pResData = nullptr;
if ((hRes = ::FindResource(NULL, MAKEINTRESOURCE(romresid), _T("BIN"))) == NULL ||
(dwDataSize = ::SizeofResource(NULL, hRes)) < 16384 ||
(hResLoaded = ::LoadResource(NULL, hRes)) == NULL ||
(pResData = ::LockResource(hResLoaded)) == NULL)
{
AlertWarning(_T("Failed to load the ROM resource."));
return false;
}
::memcpy(buffer, pResData, 16384);
}
g_pBoard->LoadROM(buffer);
g_nEmulatorConfiguration = configuration;
g_pBoard->Reset();
m_nUptimeFrameCount = 0;
return true;
}
void Emulator_Start()
{
g_okEmulatorRunning = true;
// Set title bar text
MainWindow_UpdateWindowTitle();
MainWindow_UpdateMenu();
m_nFrameCount = 0;
m_dwTickCount = GetTickCount();
// For proper breakpoint processing
if (m_wEmulatorCPUBpsCount != 0)
{
g_pBoard->GetCPU()->ClearInternalTick();
}
}
void Emulator_Stop()
{
g_okEmulatorRunning = false;
Emulator_SetTempCPUBreakpoint(0177777);
if (m_fpEmulatorParallelOut != nullptr)
::fflush(m_fpEmulatorParallelOut);
// Reset title bar message
MainWindow_UpdateWindowTitle();
MainWindow_UpdateMenu();
// Reset FPS indicator
MainWindow_SetStatusbarText(StatusbarPartFPS, nullptr);
MainWindow_UpdateAllViews();
}
void Emulator_Reset()
{
ASSERT(g_pBoard != nullptr);
g_pBoard->Reset();
m_nUptimeFrameCount = 0;
m_dwTotalFrameCount = 0;
MainWindow_UpdateAllViews();
}
bool Emulator_AddCPUBreakpoint(uint16_t address)
{
if (m_wEmulatorCPUBpsCount == MAX_BREAKPOINTCOUNT - 1 || address == 0177777)
return false;
for (int i = 0; i < m_wEmulatorCPUBpsCount; i++) // Check if the BP exists
{
if (m_EmulatorCPUBps[i] == address)
return false; // Already in the list
}
for (int i = 0; i < MAX_BREAKPOINTCOUNT; i++) // Put in the first empty cell
{
if (m_EmulatorCPUBps[i] == 0177777)
{
m_EmulatorCPUBps[i] = address;
break;
}
}
m_wEmulatorCPUBpsCount++;
return true;
}
bool Emulator_RemoveCPUBreakpoint(uint16_t address)
{
if (m_wEmulatorCPUBpsCount == 0 || address == 0177777)
return false;
for (int i = 0; i < MAX_BREAKPOINTCOUNT; i++)
{
if (m_EmulatorCPUBps[i] == address)
{
m_EmulatorCPUBps[i] = 0177777;
m_wEmulatorCPUBpsCount--;
if (m_wEmulatorCPUBpsCount > i) // fill the hole
{
m_EmulatorCPUBps[i] = m_EmulatorCPUBps[m_wEmulatorCPUBpsCount];
m_EmulatorCPUBps[m_wEmulatorCPUBpsCount] = 0177777;
}
return true;
}
}
return false;
}
void Emulator_SetTempCPUBreakpoint(uint16_t address)
{
if (m_wEmulatorTempCPUBreakpoint != 0177777)
Emulator_RemoveCPUBreakpoint(m_wEmulatorTempCPUBreakpoint);
if (address == 0177777)
{
m_wEmulatorTempCPUBreakpoint = 0177777;
return;
}
for (int i = 0; i < MAX_BREAKPOINTCOUNT; i++)
{
if (m_EmulatorCPUBps[i] == address)
return; // We have regular breakpoint with the same address
}
m_wEmulatorTempCPUBreakpoint = address;
m_EmulatorCPUBps[m_wEmulatorCPUBpsCount] = address;
m_wEmulatorCPUBpsCount++;
}
const uint16_t* Emulator_GetCPUBreakpointList() { return m_EmulatorCPUBps; }
bool Emulator_IsBreakpoint()
{
uint16_t address = g_pBoard->GetCPU()->GetPC();
if (m_wEmulatorCPUBpsCount > 0)
{
for (int i = 0; i < m_wEmulatorCPUBpsCount; i++)
{
if (address == m_EmulatorCPUBps[i])
return true;
}
}
return false;
}
bool Emulator_IsBreakpoint(uint16_t address)
{
if (m_wEmulatorCPUBpsCount == 0)
return false;
for (int i = 0; i < m_wEmulatorCPUBpsCount; i++)
{
if (address == m_EmulatorCPUBps[i])
return true;
}
return false;
}
void Emulator_RemoveAllBreakpoints()
{
for (int i = 0; i < MAX_BREAKPOINTCOUNT; i++)
m_EmulatorCPUBps[i] = 0177777;
m_wEmulatorCPUBpsCount = 0;
}
bool Emulator_AddWatchpoint(uint16_t address)
{
if (m_wEmulatorWatchesCount == MAX_WATCHPOINTCOUNT - 1 || address == 0177777)
return false;
for (int i = 0; i < m_wEmulatorWatchesCount; i++) // Check if the BP exists
{
if (m_EmulatorWatches[i] == address)
return false; // Already in the list
}
for (int i = 0; i < MAX_BREAKPOINTCOUNT; i++) // Put in the first empty cell
{
if (m_EmulatorWatches[i] == 0177777)
{
m_EmulatorWatches[i] = address;
break;
}
}
m_wEmulatorWatchesCount++;
return true;
}
const uint16_t* Emulator_GetWatchpointList() { return m_EmulatorWatches; }
bool Emulator_RemoveWatchpoint(uint16_t address)
{
if (m_wEmulatorWatchesCount == 0 || address == 0177777)
return false;
for (int i = 0; i < MAX_WATCHPOINTCOUNT; i++)
{
if (m_EmulatorWatches[i] == address)
{
m_EmulatorWatches[i] = 0177777;
m_wEmulatorWatchesCount--;
if (m_wEmulatorWatchesCount > i) // fill the hole
{
m_EmulatorWatches[i] = m_EmulatorWatches[m_wEmulatorWatchesCount];
m_EmulatorWatches[m_wEmulatorWatchesCount] = 0177777;
}
return true;
}
}
return false;
}
void Emulator_RemoveAllWatchpoints()
{
for (int i = 0; i < MAX_WATCHPOINTCOUNT; i++)
m_EmulatorWatches[i] = 0177777;
m_wEmulatorWatchesCount = 0;
}
void Emulator_SetSpeed(uint16_t realspeed)
{
uint16_t speedpercent = 100;
switch (realspeed)
{
case 0: speedpercent = 500; break;
case 1: speedpercent = 100; break;
case 2: speedpercent = 200; break;
case 0x7fff: speedpercent = 50; break;
case 0x7ffe: speedpercent = 25; break;
default: speedpercent = 100; break;
}
m_wEmulatorSoundSpeed = speedpercent;
if (m_okEmulatorSound)
SoundGen_SetSpeed(m_wEmulatorSoundSpeed);
}
void Emulator_SetSound(bool soundOnOff)
{
if (m_okEmulatorSound != soundOnOff)
{
if (soundOnOff)
{
SoundGen_Initialize(Settings_GetSoundVolume());
SoundGen_SetSpeed(m_wEmulatorSoundSpeed);
g_pBoard->SetSoundGenCallback(Emulator_SoundGenCallback);
}
else
{
g_pBoard->SetSoundGenCallback(nullptr);
SoundGen_Finalize();
}
}
m_okEmulatorSound = soundOnOff;
}
bool CALLBACK Emulator_SerialIn_Callback(uint8_t* pByte)
{
DWORD dwBytesRead;
BOOL result = ::ReadFile(m_hEmulatorComPort, pByte, 1, &dwBytesRead, nullptr);
return result && (dwBytesRead == 1);
}
bool CALLBACK Emulator_SerialOut_Callback(uint8_t byte)
{
DWORD dwBytesWritten;
::WriteFile(m_hEmulatorComPort, &byte, 1, &dwBytesWritten, nullptr);
return (dwBytesWritten == 1);
}
bool Emulator_SetSerial(bool serialOnOff, LPCTSTR serialPort)
{
if (m_okEmulatorSerial != serialOnOff)
{
if (serialOnOff)
{
// Prepare port name
TCHAR port[15];
wsprintf(port, _T("\\\\.\\%s"), serialPort);
// Open port
m_hEmulatorComPort = ::CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (m_hEmulatorComPort == INVALID_HANDLE_VALUE)
{
uint32_t dwError = ::GetLastError();
AlertWarningFormat(_T("Failed to open COM port (0x%08lx)."), dwError);
return false;
}
// Set port settings
DCB dcb;
Settings_GetSerialConfig(&dcb);
if (!::SetCommState(m_hEmulatorComPort, &dcb))
{
uint32_t dwError = ::GetLastError();
::CloseHandle(m_hEmulatorComPort);
m_hEmulatorComPort = INVALID_HANDLE_VALUE;
AlertWarningFormat(_T("Failed to configure the COM port (0x%08lx)."), dwError);
return false;
}
// Set timeouts: ReadIntervalTimeout value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant
// and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the bytes
// that have already been received, even if no bytes have been received.
COMMTIMEOUTS timeouts;
::memset(&timeouts, 0, sizeof(timeouts));
timeouts.ReadIntervalTimeout = MAXDWORD;
timeouts.WriteTotalTimeoutConstant = 100;
if (!::SetCommTimeouts(m_hEmulatorComPort, &timeouts))
{
uint32_t dwError = ::GetLastError();
::CloseHandle(m_hEmulatorComPort);
m_hEmulatorComPort = INVALID_HANDLE_VALUE;
AlertWarningFormat(_T("Failed to set the COM port timeouts (0x%08lx)."), dwError);
return false;
}
// Clear port input buffer
::PurgeComm(m_hEmulatorComPort, PURGE_RXABORT | PURGE_RXCLEAR);
// Set callbacks
g_pBoard->SetSerialCallbacks(Emulator_SerialIn_Callback, Emulator_SerialOut_Callback);
}
else
{
g_pBoard->SetSerialCallbacks(nullptr, nullptr); // Reset callbacks
// Close port
if (m_hEmulatorComPort != INVALID_HANDLE_VALUE)
{
::CloseHandle(m_hEmulatorComPort);
m_hEmulatorComPort = INVALID_HANDLE_VALUE;
}
}
}
m_okEmulatorSerial = serialOnOff;
return true;
}
bool CALLBACK Emulator_ParallelOut_Callback(uint8_t byte)
{
if (m_fpEmulatorParallelOut != nullptr)
{
::fwrite(&byte, 1, 1, m_fpEmulatorParallelOut);
}
////DEBUG
//TCHAR buffer[32];
//_snwprintf_s(buffer, 32, _T("Printer: <%02x>\r\n"), byte);
//ConsoleView_Print(buffer);
return true;
}
void Emulator_SetParallel(bool parallelOnOff)
{
if (m_okEmulatorParallel == parallelOnOff)
return;
if (!parallelOnOff)
{
g_pBoard->SetParallelOutCallback(nullptr);
if (m_fpEmulatorParallelOut != nullptr)
::fclose(m_fpEmulatorParallelOut);
}
else
{
g_pBoard->SetParallelOutCallback(Emulator_ParallelOut_Callback);
m_fpEmulatorParallelOut = ::_tfopen(_T("printer.log"), _T("wb"));
}
m_okEmulatorParallel = parallelOnOff;
}
int Emulator_SystemFrame()
{
g_pBoard->SetCPUBreakpoints(m_wEmulatorCPUBpsCount > 0 ? m_EmulatorCPUBps : nullptr);
ScreenView_ScanKeyboard();
ScreenView_ProcessKeyboard();
if (!g_pBoard->SystemFrame())
return 0;
// Calculate frames per second
m_nFrameCount++;
uint32_t dwCurrentTicks = GetTickCount();
long nTicksElapsed = dwCurrentTicks - m_dwTickCount;
if (nTicksElapsed >= 1200)
{
double dFramesPerSecond = m_nFrameCount * 1000.0 / nTicksElapsed;
double dSpeed = dFramesPerSecond / 25.0 * 100;
TCHAR buffer[16];
swprintf_s(buffer, 16, _T("%03.f%%"), dSpeed);
MainWindow_SetStatusbarText(StatusbarPartFPS, buffer);
bool floppyEngine = g_pBoard->IsFloppyEngineOn();
MainWindow_SetStatusbarText(StatusbarPartFloppyEngine, floppyEngine ? _T("Motor") : nullptr);
m_nFrameCount = 0;
m_dwTickCount = dwCurrentTicks;
}
// Calculate emulator uptime (25 frames per second)
m_dwTotalFrameCount++;
m_nUptimeFrameCount++;
if (m_nUptimeFrameCount >= 25)
{
m_nUptimeFrameCount = 0;
uint32_t dwEmulatorUptime = m_dwTotalFrameCount / 25;
int seconds = (int) (dwEmulatorUptime % 60);
int minutes = (int) (dwEmulatorUptime / 60 % 60);
int hours = (int) (dwEmulatorUptime / 3600 % 60);
TCHAR buffer[20];
swprintf_s(buffer, 20, _T("Uptime: %02d:%02d:%02d"), hours, minutes, seconds);
MainWindow_SetStatusbarText(StatusbarPartUptime, buffer);
}
// Update "Sound" indicator every 5 frames
m_nEmulatorSoundChanges += g_pBoard->GetSoundChanges();
if (m_nUptimeFrameCount % 5 == 0)
{
bool soundOn = m_nEmulatorSoundChanges > 0;
MainWindow_SetStatusbarText(StatusbarPartSound, soundOn ? _T("Sound") : nullptr);
m_nEmulatorSoundChanges = 0;
}
return 1;
}
void CALLBACK Emulator_SoundGenCallback(uint16_t value)
{
SoundGen_FeedDAC(value);
}
// Update cached values after Run or Step
void Emulator_OnUpdate()
{
// Update stored PC value
g_wEmulatorPrevCpuPC = g_wEmulatorCpuPC;
g_wEmulatorCpuPC = g_pBoard->GetCPU()->GetPC();
// Update memory change flags
{
uint8_t* pOld = g_pEmulatorRam;
uint8_t* pChanged = g_pEmulatorChangedRam;
uint16_t addr = 0;
do
{
uint8_t newvalue = g_pBoard->GetLORAMByte(addr); //TODO
uint8_t oldvalue = *pOld;
*pChanged = (newvalue != oldvalue) ? 255 : 0;
*pOld = newvalue;
addr++;
pOld++; pChanged++;
}
while (addr < 65535);
}
}
// Get RAM change flag
// addrtype - address mode - see ADDRTYPE_XXX constants
uint16_t Emulator_GetChangeRamStatus(uint16_t address)
{
return *((uint16_t*)(g_pEmulatorChangedRam + address));
}
void Emulator_GetScreenSize(int scrmode, int* pwid, int* phei)
{
if (scrmode < 0 || scrmode >= sizeof(ScreenModeReference) / sizeof(ScreenModeStruct))
return;
ScreenModeStruct* pinfo = ScreenModeReference + scrmode;
*pwid = pinfo->width;
*phei = pinfo->height;
}
void Emulator_PrepareScreenRGB32(void* pImageBits, int screenMode)
{
if (pImageBits == nullptr) return;
const uint8_t* pVideoBuffer = g_pBoard->GetVideoBuffer();
ASSERT(pVideoBuffer != nullptr);
// Render to bitmap
bool hires = g_pBoard->GetPortView(0177604) & 010;
uint8_t border = g_pBoard->GetPortView(0177604) & 7;
bool blink = (m_dwTotalFrameCount % 75) > 37;
PREPARE_SCREEN_CALLBACK callback = ScreenModeReference[screenMode].callback;
callback(pVideoBuffer, Emulator_Palette, pImageBits, hires, border, blink);
}
const uint32_t * Emulator_GetPalette()
{
return Emulator_Palette;
}
// 1/2 part of "a" plus 1/2 part of "b"
#define AVERAGERGB(a, b) ( (((a) & 0xfefefeffUL) + ((b) & 0xfefefeffUL)) >> 1 )
// 1/4 part of "a" plus 3/4 parts of "b"
#define AVERAGERGB13(a, b) ( ((a) == (b)) ? a : (((a) & 0xfcfcfcffUL) >> 2) + ((b) - (((b) & 0xfcfcfcffUL) >> 2)) )
void CALLBACK Emulator_PrepareScreen640x200(
const uint8_t* pVideoBuffer, const uint32_t* palette, void* pImageBits, bool hires, uint8_t border, bool blink)
{
if (!hires)
{
uint32_t colorborder = palette[(border & 7) + 16];
for (int y = 0; y < 200; y++)
{
const uint16_t* pVideo = (uint16_t*)(pVideoBuffer + y * 320 / 4);
uint32_t* pBits = (uint32_t*)pImageBits + (200 - 1 - y) * 640;
for (int i = 0; i < 160; i++) // Left part of line
*pBits++ = colorborder;
for (int x = 0; x < 320 / 8; x++)
{
uint16_t value = *pVideo++;
uint32_t colorpaper = palette[(value >> 11) & 7];
uint32_t colorink = palette[(value >> 8) & 7];
if ((value & 0x8000) && blink)
{
uint32_t temp = colorink; colorink = colorpaper; colorpaper = temp;
}
uint16_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
*pBits++ = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
}
}
for (int i = 0; i < 160; i++) // Right part of line
*pBits++ = colorborder;
}
}
else // hires
{
uint32_t colorpaper = palette[border & 7];
uint32_t colorink = palette[(border & 7) ^ 7];
for (int y = 0; y < 200; y++)
{
const uint8_t* pVideo = (uint8_t*)(pVideoBuffer + y * 640 / 8);
uint32_t* pBits = (uint32_t*)pImageBits + (200 - 1 - y) * 640;
for (int x = 0; x < 640; x += 8)
{
uint8_t value = *pVideo++;
uint8_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
*pBits++ = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
}
}
}
}
}
// 320x200 plus 15 pix border at left/right, plus 10 pix border at top/bottom
void CALLBACK Emulator_PrepareScreen360x220(
const uint8_t* pVideoBuffer, const uint32_t* palette, void* pImageBits, bool hires, uint8_t border, bool blink)
{
uint32_t colorborder = palette[(border & 7) + 16];
for (int y = 0; y < 220; y++)
{
uint32_t* pBits = (uint32_t*)pImageBits + (220 - 1 - y) * 360;
if (y < 10 || y >= 210) // Border at the top/bottom
{
for (int i = 0; i < 360; i++)
*pBits++ = colorborder;
continue;
}
for (int i = 0; i < 20; i++) // Left part of line
*pBits++ = colorborder;
if (!hires)
{
const uint16_t* pVideo = (uint16_t*)(pVideoBuffer + (y - 10) * 320 / 4);
for (int x = 0; x < 320 / 8; x++)
{
uint16_t value = *pVideo++;
uint32_t colorpaper = palette[(value >> 11) & 7];
uint32_t colorink = palette[(value >> 8) & 7];
if ((value & 0x8000) && blink)
{
uint32_t temp = colorink; colorink = colorpaper; colorpaper = temp;
}
uint16_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
*pBits++ = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
}
}
}
else // hires
{
uint32_t colorpaper = palette[border & 7];
uint32_t colorink = palette[(border & 7) ^ 7];
const uint8_t* pVideo = (uint8_t*)(pVideoBuffer + (y - 10) * 640 / 8);
for (int x = 0; x < 320; x += 4)
{
uint8_t value = *pVideo++;
uint8_t mask = 0x80;
for (int f = 0; f < 8; f += 2)
{
uint32_t color1 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
uint32_t color2 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
*pBits++ = AVERAGERGB(color1, color2);
}
}
}
for (int i = 0; i < 20; i++) // Right part of line
*pBits++ = colorborder;
}
}
// 640x400, plus 40 pix border at left/right, plus 20 pix border at top/bottom
void CALLBACK Emulator_PrepareScreen720x440(
const uint8_t* pVideoBuffer, const uint32_t* palette, void* pImageBits, bool hires, uint8_t border, bool blink)
{
uint32_t colorborder = palette[(border & 7) + 16];
for (int y = 0; y < 220; y++)
{
uint32_t* pBits1 = (uint32_t*)pImageBits + (440 - 1 - y * 2) * 720;
uint32_t* pBits2 = (uint32_t*)pImageBits + (440 - 2 - y * 2) * 720;
if (y < 10 || y >= 210) // Border at the top/bottom
{
for (int i = 0; i < 720; i++)
*pBits1++ = *pBits2++ = colorborder;
continue;
}
for (int i = 0; i < 40; i++) // Border at the left
*pBits1++ = *pBits2++ = colorborder;
if (!hires)
{
const uint16_t* pVideo = (uint16_t*)(pVideoBuffer + (y - 10) * 320 / 4);
for (int x = 0; x < 320 / 8; x++)
{
uint16_t value = *pVideo++;
uint32_t colorpaper = palette[(value >> 11) & 7];
uint32_t colorink = palette[(value >> 8) & 7];
if ((value & 0x8000) && blink)
{
uint32_t temp = colorink; colorink = colorpaper; colorpaper = temp;
}
uint16_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
*pBits1++ = *pBits2++ = (value & mask) ? colorink : colorpaper;
*pBits1++ = *pBits2++ = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
}
}
}
else // hires
{
uint32_t colorpaper = palette[border & 7];
uint32_t colorink = palette[(border & 7) ^ 7];
const uint8_t* pVideo = (uint8_t*)(pVideoBuffer + (y - 10) * 640 / 8);
for (int x = 0; x < 640; x += 8)
{
uint8_t value = *pVideo++;
uint8_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
*pBits1++ = *pBits2++ = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
}
}
}
for (int i = 0; i < 40; i++) // Border at the right
*pBits1++ = *pBits2++ = colorborder;
}
}
// 800x600 plus 40 pix border for left/right sides, 30 pix border for top/bottom
void CALLBACK Emulator_PrepareScreen880x660(
const uint8_t* pVideoBuffer, const uint32_t* palette, void* pImageBits, bool hires, uint8_t border, bool blink)
{
uint32_t colorborder = palette[(border & 7) + 16];
for (int y = 0; y < 220; y++)
{
uint32_t* pBits1 = (uint32_t*)pImageBits + (660 - 1 - y * 3) * 880;
uint32_t* pBits2 = (uint32_t*)pImageBits + (660 - 2 - y * 3) * 880;
uint32_t* pBits3 = (uint32_t*)pImageBits + (660 - 3 - y * 3) * 880;
if (y < 10 || y >= 210) // Border at the top/bottom
{
for (int i = 0; i < 880; i++)
*pBits1++ = *pBits2++ = *pBits3++ = colorborder;
continue;
}
for (int i = 0; i < 40; i++) // Border at the left
*pBits1++ = *pBits2++ = *pBits3++ = colorborder;
if (!hires)
{
const uint16_t* pVideo = (uint16_t*)(pVideoBuffer + (y - 10) * 320 / 4);
for (int x = 0; x < 320 / 8; x++)
{
uint16_t value = *pVideo++;
uint32_t colorpaper = palette[(value >> 11) & 7];
uint32_t colorink = palette[(value >> 8) & 7];
if ((value & 0x8000) && blink)
{
uint32_t temp = colorink; colorink = colorpaper; colorpaper = temp;
}
uint16_t mask = 0x80;
for (int f = 0; f < 4; f++)
{
uint32_t color1 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
uint32_t color2 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
*pBits1++ = *pBits2++ = *pBits3++ = color1;
*pBits1++ = *pBits2++ = *pBits3++ = color1;
*pBits1++ = *pBits2++ = *pBits3++ = AVERAGERGB(color1, color2);
*pBits1++ = *pBits2++ = *pBits3++ = color2;
*pBits1++ = *pBits2++ = *pBits3++ = color2;
}
}
}
else // hires
{
const uint8_t* pVideo = (uint8_t*)(pVideoBuffer + (y - 10) * 640 / 8);
uint32_t colorpaper = palette[border & 7];
uint32_t colorink = palette[(border & 7) ^ 7];
for (int x = 0; x < 640; x += 8)
{
uint8_t value = *pVideo++;
uint16_t mask = 0x80;
for (int f = 0; f < 2; f++)
{
uint32_t color1 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
uint32_t color2 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
uint32_t color3 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
uint32_t color4 = (value & mask) ? colorink : colorpaper;
mask = mask >> 1;
*pBits1++ = *pBits2++ = *pBits3++ = color1;
*pBits1++ = *pBits2++ = *pBits3++ = AVERAGERGB13(color1, color2);
*pBits1++ = *pBits2++ = *pBits3++ = AVERAGERGB(color2, color3);
*pBits1++ = *pBits2++ = *pBits3++ = AVERAGERGB13(color4, color3);
*pBits1++ = *pBits2++ = *pBits3++ = color4;
}
}
}
for (int i = 0; i < 40; i++) // Border at the right
*pBits1++ = *pBits2++ = *pBits3++ = colorborder;
}
}
// 960x600 plus 60 pix border for left/right sides, 30 pix border for top/bottom
void CALLBACK Emulator_PrepareScreen1080x660(
const uint8_t* pVideoBuffer, const uint32_t* palette, void* pImageBits, bool hires, uint8_t border, bool blink)
{
uint32_t colorborder = palette[(border & 7) + 16];
for (int y = 0; y < 220; y++)
{
uint32_t* pBits1 = (uint32_t*)pImageBits + (660 - 1 - y * 3) * 1080;
uint32_t* pBits2 = (uint32_t*)pImageBits + (660 - 2 - y * 3) * 1080;
//uint32_t* pBits3 = (uint32_t*)pImageBits + (660 - 3 - y * 3) * 1080;
//for (int i = 0; i < 1080; i++)
// *pBits3++ = 0;
if (y < 10 || y >= 210) // Border at the top/bottom
{
for (int i = 0; i < 1080; i++)
*pBits1++ = *pBits2++ = colorborder;
continue;
}
for (int i = 0; i < 60; i++) // Border at the left
*pBits1++ = *pBits2++ = colorborder;
if (!hires)
{
const uint16_t* pVideo = (uint16_t*)(pVideoBuffer + (y - 10) * 320 / 4);
for (int x = 0; x < 320 / 8; x++)
{
uint16_t value = *pVideo++;
uint32_t colorpaper = palette[(value >> 11) & 7];
uint32_t colorink = palette[(value >> 8) & 7];
if ((value & 0x8000) && blink)
{
uint32_t temp = colorink; colorink = colorpaper; colorpaper = temp;
}
uint16_t mask = 0x80;
for (int f = 0; f < 8; f++)
{
uint32_t color = (value & mask) ? colorink : colorpaper;
*pBits1++ = *pBits2++ = color;
*pBits1++ = *pBits2++ = color;
*pBits1++ = *pBits2++ = color;
mask = mask >> 1;
}
}
}
else // hires
{
const uint8_t* pVideo = (uint8_t*)(pVideoBuffer + (y - 10) * 640 / 8);
uint32_t colorpaper = palette[border & 7];
uint32_t colorink = palette[(border & 7) ^ 7];
for (int x = 0; x < 640; x += 8)
{
uint8_t value = *pVideo++;
uint8_t mask1 = 0x80;
uint8_t mask2 = 0x40;