-
Notifications
You must be signed in to change notification settings - Fork 22
/
pakinterface.c
608 lines (548 loc) · 15.2 KB
/
pakinterface.c
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
/*
Copyright 2015 by Joseph Forgione
This file is part of VCC (Virtual Color Computer).
VCC (Virtual Color Computer) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
VCC (Virtual Color Computer) 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
*/
#include <windows.h>
#include <windowsx.h>
#include "commdlg.h"
#include <stdio.h>
#include <process.h>
#include "defines.h"
#include "tcc1014mmu.h"
#include "pakinterface.h"
#include "config.h"
#include "Vcc.h"
#include "mc6821.h"
#include "logger.h"
#include "fileops.h"
#define HASCONFIG 1
#define HASIOWRITE 2
#define HASIOREAD 4
#define NEEDSCPUIRQ 8
#define DOESDMA 16
#define NEEDHEARTBEAT 32
#define ANALOGAUDIO 64
#define CSWRITE 128
#define CSREAD 256
#define RETURNSSTATUS 512
#define CARTRESET 1024
#define SAVESINI 2048
#define ASSERTCART 4096
// Storage for Pak ROMs
static uint8_t *ExternalRomBuffer = nullptr;
static bool RomPackLoaded = false;
extern SystemState EmuState;
static unsigned int BankedCartOffset=0;
static char DllPath[256]="";
static unsigned short ModualParms=0;
static HINSTANCE hinstLib;
static bool DialogOpen=false;
typedef void (*DYNAMICMENUCALLBACK)( char *,int, int);
typedef void (*GETNAME)(char *,char *,DYNAMICMENUCALLBACK);
typedef void (*CONFIGIT)(unsigned char);
typedef void (*HEARTBEAT) (void);
typedef unsigned char (*PACKPORTREAD)(unsigned char);
typedef void (*PACKPORTWRITE)(unsigned char,unsigned char);
typedef void (*ASSERTINTERUPT) (unsigned char,unsigned char);
typedef unsigned char (*MEMREAD8)(unsigned short);
typedef void (*SETCART)(unsigned char);
typedef void (*MEMWRITE8)(unsigned char,unsigned short);
typedef void (*MODULESTATUS)(char *);
typedef void (*DMAMEMPOINTERS) ( MEMREAD8,MEMWRITE8);
typedef void (*SETCARTPOINTER)(SETCART);
typedef void (*SETINTERUPTCALLPOINTER) (ASSERTINTERUPT);
typedef unsigned short (*MODULEAUDIOSAMPLE)(void);
typedef void (*MODULERESET)(void);
typedef void (*SETINIPATH)(char *);
static void (*GetModuleName)(char *,char *,DYNAMICMENUCALLBACK)=NULL;
static void (*ConfigModule)(unsigned char)=NULL;
static void (*SetInteruptCallPointer) ( ASSERTINTERUPT)=NULL;
static void (*DmaMemPointer) (MEMREAD8,MEMWRITE8)=NULL;
static void (*HeartBeat)(void)=NULL;
static void (*PakPortWrite)(unsigned char,unsigned char)=NULL;
static unsigned char (*PakPortRead)(unsigned char)=NULL;
static void (*PakMemWrite8)(unsigned char,unsigned short)=NULL;
static unsigned char (*PakMemRead8)(unsigned short)=NULL;
static void (*ModuleStatus)(char *)=NULL;
static unsigned short (*ModuleAudioSample)(void)=NULL;
static void (*ModuleReset) (void)=NULL;
static void (*SetIniPath) (char *)=NULL;
static void (*PakSetCart)(SETCART)=NULL;
static char PakPath[MAX_PATH];
static char Did=0;
int FileID(char *);
typedef struct {
char MenuName[512];
int MenuId;
int Type;
} Dmenu;
static Dmenu MenuItem[100];
static unsigned char MenuIndex=0;
static HMENU hMenu = NULL;
static HMENU hSubMenu[64] ;
static char Modname[MAX_PATH]="Blank";
void PakTimer(void)
{
if (HeartBeat != NULL)
HeartBeat();
return;
}
void ResetBus(void)
{
BankedCartOffset=0;
if (ModuleReset !=NULL)
ModuleReset();
return;
}
void GetModuleStatus(SystemState *SMState)
{
if (ModuleStatus!=NULL)
ModuleStatus(SMState->StatusLine);
else
sprintf(SMState->StatusLine,"");
return;
}
unsigned char PackPortRead (unsigned char port)
{
if (PakPortRead != NULL)
return(PakPortRead(port));
else
return(NULL);
}
void PackPortWrite(unsigned char Port,unsigned char Data)
{
if (PakPortWrite != NULL)
{
PakPortWrite(Port,Data);
return;
}
if ((Port == 0x40) && (RomPackLoaded == true)) {
BankedCartOffset = (Data & 15) << 14;
}
return;
}
unsigned char PackMem8Read (unsigned short Address)
{
if (PakMemRead8!=NULL)
return(PakMemRead8(Address&32767));
if (ExternalRomBuffer!=NULL)
return(ExternalRomBuffer[(Address & 32767)+BankedCartOffset]);
return(0);
}
void PackMem8Write(unsigned char Port,unsigned char Data)
{
return;
}
unsigned short PackAudioSample(void)
{
if (ModuleAudioSample !=NULL)
return(ModuleAudioSample());
return(NULL);
}
int LoadCart(void)
{
OPENFILENAME ofn ;
char szFileName[MAX_PATH]="";
char temp[MAX_PATH];
GetIniFilePath(temp);
GetPrivateProfileString("DefaultPaths", "PakPath", "", PakPath, MAX_PATH, temp);
memset(&ofn,0,sizeof(ofn));
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = EmuState.WindowHandle;
ofn.lpstrFilter = "Program Packs\0*.ROM;*.ccc;*.DLL\0\0"; // filter string
ofn.nFilterIndex = 1 ; // current filter index
ofn.lpstrFile = szFileName ; // contains full path and filename on return
ofn.nMaxFile = MAX_PATH; // sizeof lpstrFile
ofn.lpstrFileTitle = NULL; // filename and extension only
ofn.nMaxFileTitle = MAX_PATH ; // sizeof lpstrFileTitle
ofn.lpstrInitialDir = PakPath; // initial directory
ofn.lpstrTitle = TEXT("Load Program Pack") ; // title bar string
ofn.Flags = OFN_HIDEREADONLY;
if ( GetOpenFileName (&ofn))
if (!InsertModule(szFileName)) {
string tmp = ofn.lpstrFile;
int idx;
idx = tmp.find_last_of("\\");
tmp=tmp.substr(0, idx);
strcpy(PakPath, tmp.c_str());
WritePrivateProfileString("DefaultPaths", "PakPath", PakPath, temp);
return(0);
}
return(1);
}
int InsertModule (char *ModulePath)
{
// char Modname[MAX_LOADSTRING]="Blank";
char CatNumber[MAX_LOADSTRING]="";
char Temp[MAX_LOADSTRING]="";
char String[1024]="";
char TempIni[MAX_PATH]="";
unsigned char FileType=0;
FileType=FileID(ModulePath);
switch (FileType)
{
case 0: //File doesn't exist
return(NOMODULE);
break;
case 2: //File is a ROM image
UnloadDll();
load_ext_rom(ModulePath);
strncpy(Modname,ModulePath,MAX_PATH);
PathStripPath(Modname);
DynamicMenuCallback( "",0, 0); //Refresh Menus
DynamicMenuCallback( "",1, 0);
EmuState.ResetPending=2;
SetCart(1);
return(NOMODULE);
break;
case 1: //File is a DLL
UnloadDll();
hinstLib = LoadLibrary(ModulePath);
if (hinstLib ==NULL)
return(NOMODULE);
SetCart(0);
GetModuleName=(GETNAME)GetProcAddress(hinstLib, "ModuleName");
ConfigModule=(CONFIGIT)GetProcAddress(hinstLib, "ModuleConfig");
PakPortWrite=(PACKPORTWRITE) GetProcAddress(hinstLib, "PackPortWrite");
PakPortRead=(PACKPORTREAD) GetProcAddress(hinstLib, "PackPortRead");
SetInteruptCallPointer=(SETINTERUPTCALLPOINTER)GetProcAddress(hinstLib, "AssertInterupt");
DmaMemPointer=(DMAMEMPOINTERS) GetProcAddress(hinstLib, "MemPointers");
HeartBeat=(HEARTBEAT) GetProcAddress(hinstLib, "HeartBeat");
PakMemWrite8=(MEMWRITE8) GetProcAddress(hinstLib, "PakMemWrite8");
PakMemRead8=(MEMREAD8) GetProcAddress(hinstLib, "PakMemRead8");
ModuleStatus=(MODULESTATUS) GetProcAddress(hinstLib, "ModuleStatus");
ModuleAudioSample=(MODULEAUDIOSAMPLE) GetProcAddress(hinstLib, "ModuleAudioSample");
ModuleReset=(MODULERESET) GetProcAddress(hinstLib, "ModuleReset");
SetIniPath=(SETINIPATH) GetProcAddress(hinstLib,"SetIniPath");
PakSetCart=(SETCARTPOINTER) GetProcAddress(hinstLib,"SetCart");
if (GetModuleName == NULL)
{
FreeLibrary(hinstLib);
hinstLib=NULL;
return(NOTVCC);
}
BankedCartOffset=0;
if (DmaMemPointer!=NULL)
DmaMemPointer(MemRead8,MemWrite8);
if (SetInteruptCallPointer!=NULL)
SetInteruptCallPointer(CPUAssertInterupt);
GetModuleName(Modname,CatNumber,DynamicMenuCallback); //Instanciate the menus from HERE!
sprintf(Temp,"Configure %s",Modname);
strcat(String,"Module Name: ");
strcat(String,Modname);
strcat(String,"\n");
if (ConfigModule!=NULL)
{
ModualParms|=1;
strcat(String,"Has Configurable options\n");
}
if (PakPortWrite!=NULL)
{
ModualParms|=2;
strcat(String,"Is IO writable\n");
}
if (PakPortRead!=NULL)
{
ModualParms|=4;
strcat(String,"Is IO readable\n");
}
if (SetInteruptCallPointer!=NULL)
{
ModualParms|=8;
strcat(String,"Generates Interupts\n");
}
if (DmaMemPointer!=NULL)
{
ModualParms|=16;
strcat(String,"Generates DMA Requests\n");
}
if (HeartBeat!=NULL)
{
ModualParms|=32;
strcat(String,"Needs Heartbeat\n");
}
if (ModuleAudioSample!=NULL)
{
ModualParms|=64;
strcat(String,"Analog Audio Outputs\n");
}
if (PakMemWrite8!=NULL)
{
ModualParms|=128;
strcat(String,"Needs ChipSelect Write\n");
}
if (PakMemRead8!=NULL)
{
ModualParms|=256;
strcat(String,"Needs ChipSelect Read\n");
}
if (ModuleStatus!=NULL)
{
ModualParms|=512;
strcat(String,"Returns Status\n");
}
if (ModuleReset!=NULL)
{
ModualParms|=1024;
strcat(String,"Needs Reset Notification\n");
}
if (SetIniPath!=NULL)
{
ModualParms|=2048;
GetIniFilePath(TempIni);
SetIniPath(TempIni);
}
if (PakSetCart!=NULL)
{
ModualParms|=4096;
strcat(String,"Can Assert CART\n");
PakSetCart(SetCart);
}
strcpy(DllPath,ModulePath);
EmuState.ResetPending=2;
return(0);
break;
}
return(NOMODULE);
}
/**
Load a ROM pack
return total bytes loaded, or 0 on failure
*/
int load_ext_rom(char filename[MAX_PATH])
{
constexpr size_t PAK_MAX_MEM = 0x40000;
// If there is an existing ROM, ditch it
if (ExternalRomBuffer != nullptr) {
free(ExternalRomBuffer);
}
// Allocate memory for the ROM
ExternalRomBuffer = (uint8_t*)malloc(PAK_MAX_MEM);
// If memory was unable to be allocated, fail
if (ExternalRomBuffer == nullptr) {
MessageBox(0, "cant allocate ram", "Ok", 0);
return 0;
}
// Open the ROM file, fail if unable to
FILE *rom_handle = fopen(filename, "rb");
if (rom_handle == nullptr) return 0;
// Load the file, one byte at a time.. (TODO: Get size and read entire block)
size_t index=0;
while ((feof(rom_handle) == 0) && (index < PAK_MAX_MEM)) {
ExternalRomBuffer[index++] = fgetc(rom_handle);
}
fclose(rom_handle);
UnloadDll();
BankedCartOffset=0;
RomPackLoaded=true;
return index;
}
void UnloadDll(void)
{
if ((DialogOpen==true) & (EmuState.EmulationRunning==1))
{
MessageBox(0,"Close Configuration Dialog before unloading","Ok",0);
return;
}
GetModuleName=NULL;
ConfigModule=NULL;
PakPortWrite=NULL;
PakPortRead=NULL;
SetInteruptCallPointer=NULL;
DmaMemPointer=NULL;
HeartBeat=NULL;
PakMemWrite8=NULL;
PakMemRead8=NULL;
ModuleStatus=NULL;
ModuleAudioSample=NULL;
ModuleReset=NULL;
if (hinstLib !=NULL)
FreeLibrary(hinstLib);
hinstLib=NULL;
DynamicMenuCallback( "",0, 0); //Refresh Menus
DynamicMenuCallback( "",1, 0);
// DynamicMenuCallback("",0,0);
return;
}
void GetCurrentModule(char *DefaultModule)
{
strcpy(DefaultModule,DllPath);
return;
}
void UpdateBusPointer(void)
{
if (SetInteruptCallPointer!=NULL)
SetInteruptCallPointer(CPUAssertInterupt);
return;
}
void UnloadPack(void)
{
UnloadDll();
strcpy(DllPath,"");
strcpy(Modname,"Blank");
RomPackLoaded=false;
SetCart(0);
if (ExternalRomBuffer != nullptr) {
free(ExternalRomBuffer);
}
ExternalRomBuffer=nullptr;
EmuState.ResetPending=2;
DynamicMenuCallback( "",0, 0); //Refresh Menus
DynamicMenuCallback( "",1, 0);
return;
}
int FileID(char *Filename)
{
FILE *DummyHandle=NULL;
char Temp[3]="";
DummyHandle=fopen(Filename,"rb");
if (DummyHandle==NULL)
return(0); //File Doesn't exist
Temp[0]=fgetc(DummyHandle);
Temp[1]=fgetc(DummyHandle);
Temp[2]=0;
fclose(DummyHandle);
if (strcmp(Temp,"MZ")==0)
return(1); //DLL File
return(2); //Rom Image
}
void DynamicMenuActivated(unsigned char MenuItem)
{
switch (MenuItem)
{
case 1:
LoadPack();
break;
case 2:
UnloadPack();
break;
default:
if (ConfigModule !=NULL)
ConfigModule(MenuItem);
break;
}
return;
}
void DynamicMenuCallback( char *MenuName,int MenuId, int Type)
{
char Temp[256]="";
//MenuId=0 Flush Buffer MenuId=1 Done
switch (MenuId)
{
case 0:
MenuIndex=0;
DynamicMenuCallback( "Cartridge",6000,HEAD); //Recursion is fun
DynamicMenuCallback( "Load Cart",5001,SLAVE);
sprintf(Temp,"Eject Cart: ");
strcat(Temp,Modname);
DynamicMenuCallback( Temp,5002,SLAVE);
break;
case 1:
RefreshDynamicMenu();
break;
default:
strcpy(MenuItem[MenuIndex].MenuName,MenuName);
MenuItem[MenuIndex].MenuId=MenuId;
MenuItem[MenuIndex].Type=Type;
MenuIndex++;
break;
}
return;
}
void RefreshDynamicMenu(void)
{
MENUITEMINFO Mii;
char MenuTitle[32]="Cartridge";
unsigned char TempIndex=0,Index=0;
static HWND hOld;
int SubMenuIndex=0;
if ((hMenu==NULL) | (EmuState.WindowHandle != hOld))
hMenu=GetMenu(EmuState.WindowHandle);
else
DeleteMenu(hMenu,3,MF_BYPOSITION);
hOld=EmuState.WindowHandle;
hSubMenu[SubMenuIndex]=CreatePopupMenu();
memset(&Mii,0,sizeof(MENUITEMINFO));
Mii.cbSize= sizeof(MENUITEMINFO);
Mii.fMask = MIIM_TYPE | MIIM_SUBMENU | MIIM_ID;
Mii.fType = MFT_STRING;
Mii.wID = 4999;
Mii.hSubMenu = hSubMenu[SubMenuIndex];
Mii.dwTypeData = MenuTitle;
Mii.cch=strlen(MenuTitle);
InsertMenuItem(hMenu,3,TRUE,&Mii);
SubMenuIndex++;
for (TempIndex=0;TempIndex<MenuIndex;TempIndex++)
{
if (strlen(MenuItem[TempIndex].MenuName) ==0)
MenuItem[TempIndex].Type=STANDALONE;
//Create Menu item in title bar if no exist already
switch (MenuItem[TempIndex].Type)
{
case HEAD:
SubMenuIndex++;
hSubMenu[SubMenuIndex]=CreatePopupMenu();
memset(&Mii,0,sizeof(MENUITEMINFO));
Mii.cbSize= sizeof(MENUITEMINFO);
Mii.fMask = MIIM_TYPE | MIIM_SUBMENU | MIIM_ID;
Mii.fType = MFT_STRING;
Mii.wID = MenuItem[TempIndex].MenuId;
Mii.hSubMenu =hSubMenu[SubMenuIndex];
Mii.dwTypeData = MenuItem[TempIndex].MenuName;
Mii.cch=strlen(MenuItem[TempIndex].MenuName);
InsertMenuItem(hSubMenu[0],0,FALSE,&Mii);
break;
case SLAVE:
memset(&Mii,0,sizeof(MENUITEMINFO));
Mii.cbSize= sizeof(MENUITEMINFO);
Mii.fMask = MIIM_TYPE | MIIM_ID;
Mii.fType = MFT_STRING;
Mii.wID = MenuItem[TempIndex].MenuId;
Mii.hSubMenu = hSubMenu[SubMenuIndex];
Mii.dwTypeData = MenuItem[TempIndex].MenuName;
Mii.cch=strlen(MenuItem[TempIndex].MenuName);
InsertMenuItem(hSubMenu[SubMenuIndex],0,FALSE,&Mii);
break;
case STANDALONE:
if (strlen(MenuItem[TempIndex].MenuName) ==0)
{
memset(&Mii,0,sizeof(MENUITEMINFO));
Mii.cbSize= sizeof(MENUITEMINFO);
Mii.fMask = MIIM_TYPE | MIIM_ID;
Mii.fType = MF_SEPARATOR;
// Mii.fType = MF_MENUBARBREAK;
// Mii.fType = MFT_STRING;
Mii.wID = MenuItem[TempIndex].MenuId;
Mii.hSubMenu = hMenu;
Mii.dwTypeData = MenuItem[TempIndex].MenuName;
Mii.cch=strlen(MenuItem[TempIndex].MenuName);
InsertMenuItem(hSubMenu[0],0,FALSE,&Mii);
}
else
{
memset(&Mii,0,sizeof(MENUITEMINFO));
Mii.cbSize= sizeof(MENUITEMINFO);
Mii.fMask = MIIM_TYPE | MIIM_ID;
Mii.fType = MFT_STRING;
Mii.wID = MenuItem[TempIndex].MenuId;
Mii.hSubMenu = hMenu;
Mii.dwTypeData = MenuItem[TempIndex].MenuName;
Mii.cch=strlen(MenuItem[TempIndex].MenuName);
InsertMenuItem(hSubMenu[0],0,FALSE,&Mii);
}
break;
}
}
DrawMenuBar(EmuState.WindowHandle);
return;
}