forked from redcode/SpecEmu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecEmu.Asm
4643 lines (3572 loc) · 186 KB
/
SpecEmu.Asm
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
; save/load keyboard and IF2ROM SZX blocks
; SP_DISPLAYLINE SEGMENT PAGE PUBLIC FLAT 'CODE'
; [code]
; SP_DISPLAYLINE ENDS
; #########################################################################
.686
.mmx
.model flat, stdcall
option casemap :none ; case sensitive
; #########################################################################
PACMAN equ 1
.const
IDC_STATUS equ 1
; version info - use SPECEMU_FULLVERSIONSTR or SPECEMU_VERSIONSTR to get textual version data
SPECEMU_MAJOR equ 3
SPECEMU_MINOR equ 2
SPECEMU_VERNUMSTR equ <3.2>
;LOGGING equ TRUE ; to "E:\SpecEmuLog.txt"
LOGOPCODES equ TRUE
WANTSOUND equ TRUE
SINGLEINSTANCE equ TRUE
ENABLEASSEMBLER equ TRUE
;FULLER_BOX equ TRUE ;enabling breaks Multiface 128/3
FULLER_AY_REGISTER equ 63
FULLER_AY_DATA equ 95
FULLER_JOYSTICK equ 127
FULLER_ORATOR_WR equ 159
IFDEF DEBUGBUILD
;SHOW_FPS equ TRUE
;ULATUNING equ TRUE
;KEYSTATE_INFO equ TRUE
ENDIF
AUTOTAPEPLAYFRAMES equ 50
AUTOTAPEFRAMESKIP equ 8
; Memory[Read/Write]Event equates:
MEMACCESSNONE equ 0 ;0
MEMACCESSBYTE equ 1 ;0
MEMACCESSWORD equ 2 ;1
; Mapped Memory flags defined in SpecEmu.inc
MouseOn MACRO
push eax
invoke ShowCursor, TRUE
mov MenuNoUnattach, TRUE
mov MouseNoUnattach, TRUE
pop eax
ENDM
MouseOff MACRO
push eax
mov MenuNoUnattach, FALSE
mov MouseNoUnattach, FALSE
invoke ShowCursor, FALSE
pop eax
ENDM
STARTTIMER MACRO
align 16
cpuid
cpuid
cpuid
cpuid
rdtsc
mov edi, eax
mov ebx, ebx ; nop
mov edx, edx ; nop
ENDM ; Tested code starts on 16 byte boundary
ENDTIMER MACRO
cpuid
rdtsc
sub eax, edi
ENDM ; Result in eax
DISABLEINTS MACRO
mov currentMachine.iff1, FALSE
mov currentMachine.iff2, FALSE
ENDM
ENABLEINTS MACRO
mov currentMachine.iff1, TRUE
mov currentMachine.iff2, TRUE
ENDM
FULLSPEEDFRAMECOUNT equ 1 ; breaks shit like +3 floating bus when > 1
SPECCOLOURTABLESIZE equ 2*8*8*256*8
TIMEXCOLOURTABLESIZE equ SPECCOLOURTABLESIZE*2
ULA64COLOURTABLESIZE equ 2*2*8*8*256*8
.code
;--------------------------------------------------------------------------------
UPDATEWINDOW MACRO
invoke InvalidateRect, hWnd, NULL, FALSE
invoke UpdateWindow, hWnd
ENDM
CLEARSOUNDBUFFERS MACRO
IFDEF WANTSOUND
call ClearSoundBuffers
ENDIF
ENDM
; ##########################################################################
include SpecEmu.inc
include GenieZlib.inc
include sebasic.inc
include gdos-pd.inc
include AutoloadSnaps.inc
include Macros.inc
include Protos.inc
include Vars.inc
.code
include Messages.asm
include CommandLine.asm
; ##########################################################################
; PROTOs
PrepSpecColourTables PROTO
PrepTimexColourTables PROTO
PrepULA64ColourTables PROTO
MaskSpeccyCorners PROTO
TopXY PROTO :DWORD,:DWORD
SetClientSize PROTO :HWND,:DWORD,:DWORD
AddMainWinToolBar PROTO :HWND
AddMainWinStatus PROTO :HWND
SetStatusPartSizes PROTO :HWND,:HWND,:DWORD,:DWORD
SetStatusPartText PROTO :HWND,:DWORD,:DWORD
SetMachineStatusBar PROTO
GetFilePath PROTO :DWORD,:DWORD
GetFN_Hook PROTO :DWORD,:DWORD,:DWORD,:DWORD
SaveTempSZXFile PROTO
LoadTempSZXFile PROTO
DeleteTempSZXFile PROTO
SZXSaveTapeBlockDialogProc PROTO :DWORD,:DWORD,:DWORD,:DWORD
LoadTRDosROM PROTO
LoadRegsFromSNASNX PROTO
SaveRegsToSNASNX PROTO
; ##########################################################################
dwStyle equ WS_CAPTION or WS_POPUPWINDOW or WS_OVERLAPPEDWINDOW or WS_SIZEBOX or WS_VISIBLE
dwExStyle equ WS_EX_LEFT or WS_EX_ACCEPTFILES
;; HardwareMode definitions:-
;; --------------------------
; RESETENUM 0
;
; ENUM HW_16, HW_48
; ENUM HW_128, HW_PLUS2
; ENUM HW_PLUS2A, HW_PLUS3
; ENUM HW_PENTAGON128
; ENUM HW_TC2048
; ENUM HW_TK90X
;
; ENUM HW_ENDLIST
;
;HW_FIRSTMACHINE EQU HW_16
;HW_LASTMACHINE EQU HW_ENDLIST - 1
IDM_FIRSTMACHINE equ IDM_HW16k
IDM_LASTMACHINE equ IDM_TK90X
BEEPERHIGH equ 7500
BEEPERCENTRE equ BEEPERHIGH/2
BEEPERLOW equ 0
BEEPERMICBOOST equ 1200
EarHighVal EQU 2000
EarLowVal EQU 0
MICHighVal EQU 3750
MICLowVal EQU 0
; ########################################################################
; Constant data area.
; ============================================================
.const
; max size of the backbuffer
DIBWidth equ 96+512+96 ; width of our 8 bit DIB
DIBHeight equ 296 ; height of our 8 bit DIB
;DDWidth equ 3840 ; directdraw fullscreen dimensions
;DDHeight equ 2160
DDWidth equ 640 ; directdraw fullscreen dimensions
DDHeight equ 480
DDBpp equ 32 ;16
SPECEMU_FULLVERSIONSTR db "Version "
%SPECEMU_VERSIONSTR db "&SPECEMU_VERNUMSTR",0
builddate catstr <">,@Date,<">
SPECEMU_BUILDDATE db "Build ",builddate,0
Rom48Filename db "48.rom", 0
Rom128Filename db "128.rom", 0
RomPlus2Filename db "plus2.rom", 0
RomPlus3Filename db "plus3.rom", 0
;RomPlus3Filename db "plus3.41.rom", 0
;RomPlus3Filename db "plus3-spanish.rom", 0
RomTK90xFilename db "tk90x.rom",0
RomPentagon128Filename db "pentagon128.rom", 0
RomTrdosFilename db "trdos.rom", 0
RomTC2048Filename db "tc2048.rom", 0
Mf48Filename db "MF1.rom", 0
Mf128Filename db "MF128.rom", 0
Mf3Filename db "MF3.rom", 0
RomMicroSourceFilename db "MicroSource.rom", 0
Rom_CBIFilename db "CBI-24.BIN", 0
;Rom_CBIFilename db "CAS-1987-16K.BIN", 0
GETMODELNAME macro reg32:REQ
movzx reg32, HardwareMode
mov reg32, [_MachineNames_+reg32*4]
endm
STRINGLIST _MachineNames_, \
"ZX Spectrum 16K", "ZX Spectrum 48K", \
"ZX Spectrum 128K", "ZX Spectrum +2", \
"ZX Spectrum +2A", "ZX Spectrum +3", \
"Pentagon 128K", \
"Timex TC2048", "TK90X"
_ZXTape_str db "ZXTape!",26
SZXExt db "szx",0
Z80Ext db "z80",0
SNAExt db "sna",0
TAPExt db "tap",0
TZXExt db "tzx",0
DSKExt db "dsk",0
SCRExt db "scr",0
WAVExt db "wav",0
HDFExt db "hdf",0
ROMExt db "rom",0
RZXExt db "rzx",0
LOGExt db "log",0
ASMExt db "asm",0
MAPExt db "map",0
NullExt LABEL BYTE
NULL_String db 0
debugstart db "Debug Start",0
debugend db "Debug End",0
; Initialised data area.
; ============================================================
.data
SwitchingModes db FALSE
WindowSizeMove db FALSE
MessageBoxDisplayed db FALSE ; True when a messagebox is displayed by ShowMessageBox function; disallows WM_COPYDATA when True
; vars which *must* be pre-initialised
align 4
_tapfileptr dd 0
WorkFH HANDLE 0
TapeFileHandle HANDLE 0
AudioFH HANDLE 0
gl_Courier_New_6 dd 0
gl_Courier_New_9 dd 0
; DIB BITMAPINFO structure
BITMAPINFO_struct dd sizeof BITMAPINFOHEADER
dd DIBWidth
dd -DIBHeight
dw 1
dw 8 ; bits per pixel
dd BI_RGB
dd 0 ; size image
dd 200
dd 200
dd 33
dd 33
SpectrumPalette dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
dd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
BITMAPINFOSIZE equ $-BITMAPINFO_struct
NUMENTRIES equ 16
; 16 - 31 = Spectrum colours
SpectrumColours dd 00000000h, 000000CCh, 00CC0000h, 00CC00CCh, 0000CC00h, 0000CCCCh, 00CCCC00h, 00CCCCCCh
dd 00000000h, 000000FFh, 00FF0000h, 00FF00FFh, 0000FF00h, 0000FFFFh, 00FFFF00h, 00FFFFFFh
CLR_SPECBASE equ 16 ; colour offset for 32 Spectrum colours (4 bright levels * 8 colours)
CLR_MASKEDCORNER equ CLR_SPECBASE + 32 ; masked corners are black
.data?
align 16
; table of 256 colours
; 0 - 15 : reserved for GDI
; 16 - 31 : Spectrum colour table depth converted to BPP
ddSurfacePalette DWORD 256 DUP (?)
ddULAplusPalette DWORD 256 DUP (?)
align 4
WindowRect RECT <?> ; window rect when switching window/fullscreen modes
TimerID_1sec DWORD ?
TIMER_1_SECOND equ 1
hCopyDataStruct COPYDATASTRUCT <?>
hClipboardData HANDLE ?
align 4
DebugResult DWORD ?
Time1 DWORD ?
TimeTook DWORD ?
AudioFileLength DWORD ? ; num bytes written to audio file
TickCnt DWORD ?
MenuHandle DWORD ?
MenuTimeout DWORD ?
displayptr DWORD ?
initZ80PC WORD ?
MenuAttached BYTE ?
MenuNoUnattach BYTE ?
MouseNoUnattach BYTE ?
MenuIgnoreMouseMoveCnt BYTE ?
ActiveState BYTE ? ; TRUE = App is active Else App is inactive; based on WM_ACTIVATE message
TargetDrive BYTE ? ; Drive unit for the disk to be inserted to
align 4
FDCHandle LPFDCHANDLE ?
IDEHandle DWORD ?
TRDOSHandle DWORD ?
CBIHandle DWORD ?
PLUSDHandle DWORD ?
MAXTZXBLOCKS equ 16384
TZXBlockPtrs DWORD ?
TZXBlockCount WORD ?
align 4
CompatibleDC DWORD ?
lpDIBSection DWORD ?
lpDIBBits DWORD ? ; ptr to DIB bits for direct access
lpEndDIBBits DWORD ? ; ptr to end of DIB bitmap
lpBITMAPINFO DWORD ?
winWidth DWORD ?
winHeight DWORD ?
sWid DWORD ?
sHgt DWORD ?
winClientRect RECT <?>
mainwin_hToolBar HWND ?
mainwin_hStatus HWND ?
MinimumWidth DWORD ?
MinimumHeight DWORD ?
ToolBarHeight DWORD ?
StatusHeight DWORD ?
GlobalhInst DWORD ?
PCLog_FH DWORD ?
Filename DWORD ?
ReadStart DWORD ?
ReadLen DWORD ?
WriteStart DWORD ?
WriteLen DWORD ?
hCursor DWORD ?
TickCount DWORD ?
inhibit_recent_file DWORD ? ; TRUE inhibits an entry being added into the recent files list
oldzPC WORD ?
ShowDebugzPC WORD ?
OldFrameSkipCounter BYTE ?
Snapshot_OK BYTE ?
LoadError BYTE ?
RealTapeSaveSpeed BYTE ? ; TRUE = emulating normal tape saving speed
NewKey BYTE ?
UsePrevzPC BYTE ?
SpecKey BYTE ?
FirstFrame BYTE ? ; TRUE = Draw blank scanlines if in fullscreen mode
Sound_Available BYTE ?
trdos_active_frames BYTE ?
GotMultiface48Rom BYTE ? ; TRUE if Multiface 48 Rom file is present
GotMultiface128Rom BYTE ? ; TRUE if Multiface 128 Rom file is present
GotMultiface3Rom BYTE ? ; TRUE if Multiface 3 Rom file is present
MultifacePaged BYTE ? ; TRUE when Multiface ROM is paged in
Multiface_LockOut BYTE ? ; TRUE when Multiface paging is locked out (default on reset until first NMI)
SoftRomPaged BYTE ? ; TRUE when SoftRom RAM is paged in
MicroSourcePaged BYTE ? ; TRUE when SoftRom RAM is paged in
TrDos_Paged BYTE ? ; TRUE when TRDOS ROM is paged in
PLUSD_Paged BYTE ? ; TRUE when the +D ROM is paged in
uSpeech_Paged BYTE ? ; TRUE when uSpeech ROM is paged in
uSpeechStatus BYTE ? ; uSpeech status register
align 4
uSpeech_Ticks DWORD ?
uSpeech_Output WORD ?
Covox_Output WORD ?
SpecDrum_Output WORD ?
Covox_LastVol BYTE ? ; used when saving to Covox SZX blocks
SpecDrum_LastVol BYTE ? ; used when saving to SpecDrum SZX blocks
SafeRun BYTE ? ; TRUE when emulator frames are safe to run in main message loop
EmuRunning BYTE ?
AutoTapeStarted BYTE ?
AutoTapeStopFrames BYTE ? ; no. of frames before auto tape stop
sna_snx_header BYTE 27 dup (?)
.code
include C:\RadAsm\Masm\Projects\CustomControls\RawText\RawText.asm
.data?
align 4
iccex INITCOMMONCONTROLSEX <?>
;--------------------------------------------------------------------------------
; Program entry point
.code
start:
mov have_mmx, $fnc (IsProcessorFeaturePresent, PF_MMX_INSTRUCTIONS_AVAILABLE)
invoke GetCurrentDirectory, sizeof startup_currentdirectory, addr startup_currentdirectory
invoke GetAppPath, addr appPath
; see if Pasmo assembler is present in SpecEmu's directory
invoke szMultiCat, 2, addr DummyMem, offset appPath, CTXT ("pasmo.exe")
mov have_pasmo, $fnc (exist, addr DummyMem)
IFDEF PACMAN
invoke GetPacmanFilepath, addr pacmanfilepath
invoke LoadPacmanROMs
IFDEF DEBUGBUILD
.if eax == FALSE
invoke ShowMessageBox, $fnc (GetActiveWindow), SADD ("Pac-Man ROMs missing or incorrect set"), addr szWindowName, MB_OK or MB_ICONINFORMATION
.endif
ENDIF
ENDIF ; /PACMAN
mov iccex.dwICC, ICC_WIN95_CLASSES or ICC_STANDARD_CLASSES or ICC_USEREX_CLASSES
mov iccex.dwSize, sizeof (INITCOMMONCONTROLSEX)
invoke InitCommonControlsEx, addr iccex
invoke InitRawTextControl
mov hScintilla, $fnc (LoadLibrary, SADD ("Scintilla.dll"))
.if hScintilla == NULL
FATAL "Scintilla.dll could not be loaded"
.endif
mov hInstance, $fnc (GetModuleHandle, NULL)
mov CommandLine, $fnc (GetCommandLine)
mov hIcon, $fnc (LoadIcon, hInstance, 20000) ; "spectrum.ico" - icon ID
mov hCursor, $fnc (LoadCursor, NULL, IDC_ARROW)
mov sWid, $fnc (GetSystemMetrics, SM_CXSCREEN)
mov sHgt, $fnc (GetSystemMetrics, SM_CYSCREEN)
mov ProcessID, $fnc (GetCurrentProcessId)
invoke dw2hex, ProcessID, addr szProcessID
invoke GetWindowDC, $fnc (GetDesktopWindow)
.if eax != NULL
push ebx
mov ebx, eax
invoke GetSystemPaletteEntries, ebx, 0, 256, addr SpectrumPalette
invoke ReleaseDC, $fnc (GetDesktopWindow), ebx
pop ebx
.endif
memcpy addr SpectrumColours, addr SpectrumPalette+(16*4), NUMENTRIES
invoke ExitProcess, $fnc (WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT)
; ########################################################################
; special TZX block IDs for external files
BLOCK_WAV equ 0A0h
BLOCK_CSW equ 0A1h
include z80assembler.asm
include uSpeech.asm
include Machines.inc
IFDEF WANTSOUND
include DirectSound.asm ; DirectSound API stuff
ENDIF
include SoundCapture.asm
include atapi.asm
include DivIDE.asm
include wd1793.asm
include MainOptions.asm ; main options propertysheet handlers
include Tools1.asm
include MainWinMemoryViewer.asm
include PLUS-D.asm
include Keyboard.asm
include Joystick.asm
include DisplayRecording.asm
include DirectDraw.asm
include Palettes.asm ; palette handling code
include PaletteULAplus.asm ; ULAplus palette handling code
include Z80Macros.inc ; Z80 support macros and functions
include Display.asm ; Spectrum display renderer
include Z80Core.asm ; Z80 Emulation Core
include PortRead.asm
include PortWrite.asm
include RunFrame.asm
include DialogHandlers.asm ; other dialog handlers
include Machines.asm
include Debugger.asm
include Compare.asm
include RZX_BlockLists.asm
include RZX_Play.asm
include RZX_Record.asm
include RZX_Stream.asm
include RZX_Finalise.asm
include DrawIcons.asm
include TapeHandling.asm
include IniProfile.asm
include RecentFiles.asm
include Opcodes.asm ; opcode mnemonics for disassembler
include PCLogging.asm
include Assembler.asm
include CreateTape.asm
include Pacman.asm
include x86gen.asm
include Snapshots\Format_SZX.asm
; ########################################################################
WinMain proc uses ebx esi edi,
hInst: DWORD,
hPrevInst: DWORD,
CmdLine: DWORD,
CmdShow: DWORD
local wc: WNDCLASSEX
local msg: MSG
local hMutex: DWORD
local hPrevWnd: DWORD
local temp1: DWORD
; ##################################################
invoke OleInitialize, NULL
.if eax != S_OK
LOG "OleInitialize ne S_OK"
.endif
invoke SetAppDataPath
mov SafeRun, FALSE
mov hMutex, NULL
IFDEF SINGLEINSTANCE
; the control key bypasses the single instance code
invoke GetAsyncKeyState, VK_CONTROL
test ax, 8000h
.if ZERO?
mov hMutex, $fnc (CreateMutex, NULL, TRUE, SADD ("_T_SpecEmu_0A33-4644-SpecEmu-6721"))
invoke GetLastError
.if eax == ERROR_ALREADY_EXISTS
; we have another running instance
mov hPrevWnd, $fnc (FindWindow, addr szClassName, NULL)
.if hPrevWnd != NULL
.if $fnc (IsIconic, hPrevWnd) != NULL
invoke ShowWindow, hPrevWnd, SW_RESTORE
.endif
invoke SetForegroundWindow, hPrevWnd
; pass any arg to previous instance
.if $fnc (getcl_ex, 1, addr szFileName) == 1
mov [hCopyDataStruct.dwData], "SPEC"
mov [hCopyDataStruct.cbData], sizeof szFileName
mov [hCopyDataStruct.lpData], offset szFileName
invoke SendMessage, hPrevWnd, WM_COPYDATA, NULL, addr hCopyDataStruct
.endif
.endif
return FALSE ; exit this instance
.elseif eax != ERROR_SUCCESS
FATAL "Internal Mutex error"
return FALSE
.endif
.endif
ENDIF
m2m GlobalhInst, hInst
.if $fnc (Atapi_LoadDLL) == FALSE
FATAL "atapi.dll unavailable or incorrect version"
.endif
.if $fnc (wd1793_LoadDLL) == FALSE
FATAL "wd1793.dll unavailable or incorrect version"
.endif
IDE_Initialise ; initialise any hard disk image
mov IDEHandle, eax
wd1793_Initialise
mov TRDOSHandle, eax
wd1793_Initialise
mov PLUSDHandle, eax
wd1793_Initialise
mov CBIHandle, eax
call AllocateResources
.if eax == FALSE
call FreeResources ; free up resources that were allocated before the failure
FATAL "Couldn't allocate resources"
.endif
call ReadProfile
invoke SetFileAssociations
; after reading saved profile options we can insert any previously selected IDE Hard Disk files
IDE_SelectHDF IDEHandle, 0, offset IDEUnit0Filename
IDE_SelectHDF IDEHandle, 1, offset IDEUnit1Filename
; and select the DivIDE Firmware selected
invoke DivIDE_LoadFirmware
invoke SetDirtyLines
invoke SetDisplayTable
invoke LoadCustomPalettes
call LoadFiles
.if LoadError == TRUE
FATAL "ROM file(s) missing from application folder"
.endif
invoke u765_Initialise
mov FDCHandle, eax
invoke SetUserConfig ; set user's config settings before reset
call InitPort
call ClearDIB
mov EmuRunning, TRUE
mov SPGfx.FrameCnt, 0
mov SPGfx.FlashInverterByte, 0
mov FirstFrame, TRUE
mov AYTimer, 0
mov SampleTimer, 0
mov TVRandomSeed, 1
mov ActiveState, TRUE
;==================================================
; Fill WNDCLASSEX structure with required variables
;==================================================
mov wc.cbSize, sizeof WNDCLASSEX
mov wc.style, CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc, offset WndProc
mov wc.cbClsExtra, NULL
mov wc.cbWndExtra, NULL
m2m wc.hInstance, hInst
mov wc.hbrBackground, $fnc (GetStockObject, LTGRAY_BRUSH)
mov wc.lpszMenuName, NULL
mov wc.lpszClassName, offset szClassName
m2m wc.hIcon, hIcon
m2m wc.hIconSm, hIcon
m2m wc.hCursor, hCursor
invoke RegisterClassEx, addr wc
mov DesktopBPP, $fnc (GetDesktopBPP) ; get desktop colour depth for rendering
mov hWnd, $fnc (CreateWindowEx, dwExStyle, addr szClassName, addr szWindowName, dwStyle, 0, 0, 0, 0, NULL, NULL, hInst, NULL)
.if eax == NULL
call FreeResources ; free up allocated resources
FATAL "Failed to create main window"
.endif
invoke LoadRecentFileList
invoke NewList, addr PageTableList
invoke NewList, addr RZXPLAY.BlockList
invoke NewList, addr RZXREC.BlockList
mov rzx_mode, RZX_NONE
invoke RZX_EnableMenuItems
invoke GetWindowRect, hWnd, addr WindowRect ; save the window position
; We must initialise DirectDraw before DirectSound
mov FullScreenMode, FALSE ; directdraw starts in windowed mode
invoke InitDirectDraw
invoke InitSurfaces, hWnd
IFDEF WANTSOUND
mov MACHINE.FramesPerSecond, 50 ; valid FPS for initial directsound buffer size
call StartupDirectSound
call InitAudio
ENDIF
; reset has to come after audio initialised
mov al, HardwareMode ; initial value read by 'ReadProfile'
invoke Machine_Create, addr currentMachine, al
.if eax == False
FATAL "Cannot create a Spectrum machine instance"
.endif
mov HardReset, TRUE
call ResetSpectrum
invoke ShowWindow, hWnd, CmdShow
invoke UpdateWindow, hWnd
call InitTape
; ===== simple startup hardware sanity checks =====
ifc HardwareMode gt HW_PLUS3 then mov DivIDEEnabled, FALSE
ifc DivIDEEnabled eq TRUE then mov PLUSD_Enabled, FALSE
ifc PLUSD_Enabled eq TRUE then mov CBI_Enabled, FALSE
ifc CBI_Enabled eq TRUE then mov SoftRomEnabled, FALSE
ifc SoftRomEnabled eq TRUE then mov MultifaceEnabled, FALSE else mov MultifaceEnabled, TRUE
; ===== end simple startup hardware sanity checks =====
mov MultifacePaged, FALSE ; multiface memory not paged in
mov SoftRomPaged, FALSE ; softrom memory not paged in
mov MicroSourcePaged, FALSE ; microsource ROM not paged in
mov PLUSD_Paged, FALSE ; +D ROM not paged in
mov uSpeech_Paged, FALSE ; uSpeech ROM not paged in
invoke ResetJoystickStates
mov SoftRomDlg, $fnc (CreateDialogParam, hInst, IDD_SOFTROM, hWnd, addr SoftRomDialogProc, NULL)
ifc SoftRomEnabled eq TRUE then invoke ShowWindow, SoftRomDlg, SW_SHOW
; create the Messages dialog and show it in Debug build
mov MessagesDlg, $fnc (CreateDialogParam, hInst, IDD_MESSAGESDLG, hWnd, addr MessagesDialogProc, NULL)
IFDEF DEBUGBUILD
invoke ShowWindow, MessagesDlg, SW_SHOW
ENDIF
invoke x86_Breakpoint
; handle command line parameters (this needs doing before Tools1 dialog is created which steals CurrentDirectory setting)
invoke Handle_Command_Line
mov Tools1Dlg, $fnc (CreateDialogParam, hInst, IDD_TOOLS1, hWnd, addr Tools1DialogProc, NULL)
mov MW_MemViewDlg, $fnc (CreateDialogParam, hInst, IDD_MAINWINMEMVIEWDLG, hWnd, addr MainWinMemViewDialogProc, NULL)
mov AssemblerDlg, $fnc (CreateDialogParam, hInst, IDD_ASSEMBLERDLG, hWnd, addr AssemblerDialogProc, NULL)
invoke SetFocus, hWnd
IFDEF LOGGING
invoke ShowMessageBox, hWnd, SADD ("Logging is enabled in this build"), addr szWindowName, MB_OK or MB_ICONINFORMATION
ENDIF
.if StartFullscreen == TRUE
invoke FlipDisplayMode
invoke AttachMenu, hWnd
.endif
mov SafeRun, TRUE ; always last thing before entering message loop
;===================================
; Loop until PostQuitMessage is sent
;===================================
mov TickCount, $fnc (GetTickCount)
StartLoop:
.if $fnc (PeekMessage, addr msg, NULL, 0, 0, PM_REMOVE) != 0
cmp msg.message, WM_QUIT
je ExitLoop ; received WM_QUIT
invoke TranslateMessage, addr msg
invoke DispatchMessage, addr msg
jmp StartLoop
.else
.if EmuRunning == FALSE
.if (SPGfx.FrameBlit == TRUE) && (SurfacesReady == TRUE)
invoke BlitScreen, hWnd
ifc eax eq DD_OK then mov SPGfx.FrameBlit, FALSE
invoke Sleep, 1
.else
invoke WaitMessage
invoke GetPausedKeyState ; check for message window combo
.endif
jmp StartLoop
.endif
.if ActiveState == FALSE
.if (FullScreenMode == TRUE) || (Pause_On_Lost_Focus == TRUE)
invoke WaitMessage
jmp StartLoop
.endif
.endif
.if (SafeRun == TRUE) && (WindowSizeMove == FALSE)
; if logging, register values are dumped before the first opcode of each frame
mov InitialLogOpcode, TRUE
.if rzx_mode != RZX_NONE
.if rzx_mode == RZX_PLAY
invoke ExecTaskQueueTask ; execute any pending queued tasks in RZX playback frame code (suspended if emulator paused)
invoke RZX_Play_Frame
.else
invoke GetSpeccyInputStates
invoke RZX_Rec_Frame
.endif
.elseif (RunDebugFrame1 == NULL) && (RunDebugFrame2 == NULL)
invoke ExecTaskQueueTask ; execute any pending queued tasks in normal frame code (suspended if emulator paused)
invoke GetSpeccyInputStates
call Run_Frame
; invoke MW_Populate_Memory ; re-populates main win memory viewer if enabled
.else
invoke GetSpeccyInputStates
invoke Run_DebugFrame
.if eax == TRUE
; a trap causes the debugger to activate
; simulated by a forced ESC keypress
invoke PostMessage, hWnd, WM_KEYDOWN, VK_ESCAPE, "BRK" ; debugger traps always set lParam to "BRK"
.endif
.endif
.if SPGfx.FrameChanged == TRUE
mov SPGfx.FrameChanged, FALSE
ifc RecordingFrames eq TRUE then invoke RecordFrameDataFile
mov SPGfx.FrameBlit, TRUE
ifc FullScreenMode eq TRUE then invoke MaskSpeccyCorners
.if SurfacesReady == TRUE
invoke DIBToScreen, hWnd
ifc eax eq DD_OK then mov SPGfx.FrameBlit, FALSE
.endif
.else
; else handle any pending screen updates or the vertical offset scrolling
.if (SPGfx.FrameBlit == TRUE) || (SPGfx.VerticalOffset > 0)
.if SurfacesReady == TRUE
; invoke BlitScreen, hWnd
invoke DIBToScreen, hWnd
ifc eax eq DD_OK then mov SPGfx.FrameBlit, FALSE
.endif
.endif
.endif
ifc MAXIMUMDISKSPEED gt 0 then dec MAXIMUMDISKSPEED
IFNDEF WANTSOUND
.if FULLSPEEDMODE != NULL
mov TickCount, $fnc (GetTickCount)
.else
movzx eax, FrameSkipCounter
mov ecx, 20
mul ecx
add TickCount, eax
invoke GetTickCount
.while eax < TickCount
invoke Sleep, 1
invoke GetTickCount
.endw
mov TickCount, eax
.endif
ELSE
.if (MuteSound == TRUE) || (Sound_Available == FALSE)
.if (FULLSPEEDMODE != NULL) || ((TapePlaying == TRUE) && (FastTapeLoading == TRUE))
mov TickCount, $fnc (GetTickCount)
.else
movzx eax, FrameSkipCounter
mov ecx, 20
mul ecx
add TickCount, eax
invoke GetTickCount
.while eax < TickCount
invoke Sleep, 1
invoke GetTickCount
.endw
mov TickCount, eax
.endif
.endif
ENDIF
.endif
jmp StartLoop
.endif
ExitLoop: invoke Stop_PC_Logging
LOG "Exiting SpecEmu"
invoke DestroyIcon, hIcon
ifc hMutex ne NULL then invoke ReleaseMutex, hMutex
invoke OleUninitialize
return msg.wParam
WinMain endp
; #########################################################################
include WndProc.asm
PauseResumeEmulation proc uses ebx
CLEARSOUNDBUFFERS
movzx ebx, EmuRunning
xor EmuRunning, TRUE
invoke SendMessage, mainwin_hToolBar, TB_CHECKBUTTON, IDM_PAUSE, ebx
.if DebuggerActive == TRUE
invoke SendMessage, hDbgToolBar, TB_CHECKBUTTON, IDM_PAUSE, ebx
.endif
ret