//#define USE_LOGGING
//======================================================================
// This file is part of VCC (Virtual Color Computer).
// Vcc is Copyright 2015 by Joseph Forgione
//
// VCC (Virtual Color Computer) is free software, you can redistribute
// 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
// .
//
//======================================================================
/********************************************************************************************
* fd502.cpp : Defines the entry point for the DLL application. *
* DLL for use with Vcc 1.0 or higher. DLL interface 1.0 Beta *
* This Module will emulate a Tandy Floppy Disk Model FD-502 With 3 DSDD drives attached *
* Copyright 2006 (c) by Joseph Forgione *
*********************************************************************************************/
#pragma warning( disable : 4800 ) // For legacy builds
#include
#include
#include
#include "resource.h"
#include "wd1793.h"
#include "distortc.h"
#include "fd502.h"
#include
#include
#include
#include
// Three includes added for PakGetMenuItem
#include
#include
#include
//include becker code if COMBINE_BECKER is defined in fd502.h
#ifdef COMBINE_BECKER
#include "becker.h"
#endif
// Settings module groups used
const std::string MS_FD502 = "FD-502";
const std::string MS_BECKER = "DW Becker";
const std::string MS_PATHS = "DefaultPaths";
constexpr auto EXTROMSIZE = 16384u;
using namespace std;
extern DiskInfo gVirtualDrive[5];
static unsigned char ExternalRom[EXTROMSIZE];
static unsigned char DiskRom[EXTROMSIZE];
static unsigned char RGBDiskRom[EXTROMSIZE];
static char FloppyPath[MAX_PATH];
static char RomFileName[MAX_PATH]="";
static char TempRomFileName[MAX_PATH]="";
slot_id_type gSlotId {};
PakAssertInteruptHostCallback AssertInt = nullptr;
unsigned char PhysicalDriveA=0,PhysicalDriveB=0,OldPhysicalDriveA=0,OldPhysicalDriveB=0;
static unsigned char *RomPointer[3]={ExternalRom,DiskRom,RGBDiskRom};
static unsigned char SelectRom=0;
unsigned char SetChip(unsigned char);
static unsigned char NewDiskNumber=0,CreateFlag=0;
static unsigned char PersistDisks=0;
static unsigned char TempSelectRom=0;
static unsigned char ClockEnabled=1;
LRESULT CALLBACK Config(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK NewDisk(HWND,UINT, WPARAM, LPARAM);
void LoadConfig();
void SaveConfig();
long CreateDiskHeader(const char *,unsigned char,unsigned char,unsigned char);
void Unload_Disk(unsigned char);
void Load_Disk(unsigned char);
static HWND g_hConfDlg = nullptr;
static HINSTANCE gModuleInstance;
static unsigned long RealDisks=0;
long CreateDisk (unsigned char);
static char gSelectedDiskFile[MAX_PATH]="";
unsigned char LoadExtRom( unsigned char, const char *);
// Added for PakGetMenuItem export
bool get_menu_item(menu_item_entry*, size_t index);
int BeckerEnabled=0;
char BeckerAddr[MAX_PATH]="";
char BeckerPort[32]="";
// Added for PakGetMenuItem export
static HWND gVccWnd = nullptr;
static VCC::Util::settings* gpSettings = nullptr; // Pointer to settings object
//----------------------------------------------------------------------------
extern "C"
{
__declspec(dllexport) const char* PakGetName()
{
static char string_buffer[MAX_LOADSTRING];
LoadString(gModuleInstance, IDS_MODULE_NAME, string_buffer, MAX_LOADSTRING);
return string_buffer;
}
__declspec(dllexport) const char* PakGetCatalogId()
{
static char string_buffer[MAX_LOADSTRING];
LoadString(gModuleInstance, IDS_CATNUMBER, string_buffer, MAX_LOADSTRING);
return string_buffer;
}
__declspec(dllexport) const char* PakGetDescription()
{
static char string_buffer[MAX_LOADSTRING];
LoadString(gModuleInstance, IDS_DESCRIPTION, string_buffer, MAX_LOADSTRING);
return string_buffer;
}
__declspec(dllexport) void PakInitialize(
slot_id_type SlotId,
const char* const configuration_path,
HWND hVccWnd,
const cpak_callbacks* const callbacks)
{
DLOG_C("fd502 callbacks write=%p cart=%p read=%p int=%p\n",
callbacks->write_memory_byte,
callbacks->assert_cartridge_line,
callbacks->read_memory_byte,
callbacks->assert_interrupt);
gSlotId = SlotId;
gVccWnd = hVccWnd;
AssertInt = callbacks->assert_interrupt;
// Must create settings object before LoadConfig
gpSettings = new VCC::Util::settings(configuration_path);
RealDisks = InitController();
LoadConfig();
SendMessage(gVccWnd,WM_VCC_UPD_MENU,(WPARAM) 0,(LPARAM) 0);
}
__declspec(dllexport) void PakReset()
{
LoadExtRom(External,RomFileName);
}
__declspec(dllexport) void PakTerminate()
{
#ifdef COMBINE_BECKER
becker_enable(0);
#endif
CloseCartDialog(g_hConfDlg);
for (unsigned char dnum = 0; dnum <= 3; dnum++)
{
unmount_disk_image(dnum);
}
}
//PakGetMenuItem export
__declspec(dllexport) bool PakGetMenuItem(menu_item_entry* item, size_t index)
{
return get_menu_item(item, index);
}
__declspec(dllexport) void PakMenuItemClicked(unsigned char MenuID)
{
HWND h_own = GetActiveWindow();
switch (MenuID)
{
case 10:
Load_Disk(0);
break;
case 11:
Unload_Disk(0);
break;
case 12:
Load_Disk(1);
break;
case 13:
Unload_Disk(1);
break;
case 14:
Load_Disk(2);
break;
case 15:
Unload_Disk(2);
break;
case 17:
Load_Disk(3);
break;
case 18:
Unload_Disk(3);
break;
case 16:
if (g_hConfDlg == nullptr)
g_hConfDlg = CreateDialog(
gModuleInstance,(LPCTSTR)IDD_CONFIG,h_own,(DLGPROC)Config);
ShowWindow(g_hConfDlg,1);
break;
}
return;
}
__declspec(dllexport) void PakWritePort(unsigned char Port,unsigned char Data)
{
#ifdef COMBINE_BECKER
if (BeckerEnabled && (Port == 0x42)) {
becker_write(Data,Port);
} else //if ( ((Port == 0x50) | (Port==0x51)) & ClockEnabled) {
#endif
if ( ((Port == 0x50) | (Port==0x51)) & ClockEnabled) {
write_time(Data,Port);
} else {
disk_io_write(Data,Port);
}
return;
}
__declspec(dllexport) unsigned char PakReadPort(unsigned char Port)
{
#ifdef COMBINE_BECKER
if (BeckerEnabled && ((Port == 0x41) | (Port== 0x42 )))
return(becker_read(Port));
#endif
if ( ((Port == 0x50) | (Port== 0x51 )) & ClockEnabled)
return(read_time(Port));
return(disk_io_read(Port));
}
__declspec(dllexport) void PakProcessHorizontalSync()
{
PingFdc();
return;
}
__declspec(dllexport) unsigned char PakReadMemoryByte(unsigned short Address)
{
return(RomPointer[SelectRom][Address & (EXTROMSIZE-1)]);
}
__declspec(dllexport) void PakGetStatus(char* text_buffer, size_t buffer_size)
{
char diskstat[64];
DiskStatus(diskstat, sizeof(diskstat));
strcpy(text_buffer,diskstat);
#ifdef COMBINE_BECKER
char beckerstat[64];
if(BeckerEnabled) {
becker_status(beckerstat);
strcat(text_buffer," | ");
strcat(text_buffer,beckerstat);
}
#endif
return ;
}
}
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD reason, // reason for calling function
LPVOID lpReserved ) // reserved
{
if (reason == DLL_PROCESS_ATTACH) {
gModuleInstance = hinstDLL;
} else if (reason == DLL_PROCESS_DETACH) {
PakTerminate();
}
return TRUE;
}
LRESULT CALLBACK Config(HWND hDlg, UINT message, WPARAM wParam, LPARAM /*lParam*/)
{
static unsigned char temp=0,temp2=0;
long ChipChoice[3]={IDC_EXTROM,IDC_TRSDOS,IDC_RGB};
long gVirtualDrive[2]={IDC_DISKA,IDC_DISKB};
char VirtualNames[5][16]={"None","Drive 0","Drive 1","Drive 2","Drive 3"};
switch (message)
{
case WM_CLOSE:
DestroyWindow(hDlg);
g_hConfDlg = nullptr;
return TRUE;
case WM_INITDIALOG:
CenterDialog(hDlg);
TempSelectRom=SelectRom;
if (!RealDisks)
{
EnableWindow(GetDlgItem(hDlg, IDC_DISKA), FALSE);
EnableWindow(GetDlgItem(hDlg, IDC_DISKB), FALSE);
PhysicalDriveA=0;
PhysicalDriveB=0;
}
SendDlgItemMessage(hDlg,IDC_CLOCK,BM_SETCHECK,ClockEnabled,0);
OldPhysicalDriveA=PhysicalDriveA;
OldPhysicalDriveB=PhysicalDriveB;
strcpy(TempRomFileName,RomFileName);
SendDlgItemMessage(hDlg,IDC_TURBO,BM_SETCHECK,SetTurboDisk(QUERY),0);
SendDlgItemMessage(hDlg,IDC_PERSIST,BM_SETCHECK,PersistDisks,0);
for (temp=0;temp>8);
HeaderBuffer[4]=(IgnoreDensity<<7) | (SingleDensity<<6) | ((!DblSided)<<4);
HeaderBuffer[0xC]=0;
HeaderBuffer[0xD]=0;
HeaderBuffer[0xE]=0;
HeaderBuffer[0xF]=0;
HeaderSize=0x10;
FileSize= HeaderSize + (HeaderBuffer[1] * TrackSize * (DblSided+1) );
break;
case JVC: // has variable header size
HeaderSize=0;
HeaderBuffer[0]=18; //18 Sectors
HeaderBuffer[1]=DblSided+1; // Double or single Sided;
FileSize = (HeaderBuffer[0] * 0x100 * TrackTable[Tracks] * (DblSided+1));
if (DblSided)
{
FileSize+=2;
HeaderSize=2;
}
break;
case VDK:
HeaderBuffer[9]=DblSided+1;
HeaderSize=12;
FileSize = ( 18 * 0x100 * TrackTable[Tracks] * (DblSided+1));
FileSize+=HeaderSize;
break;
case 3:
HeaderBuffer[0]=0;
HeaderBuffer[1]=TrackTable[Tracks];
HeaderBuffer[2]=(TrackSize & 0xFF);
HeaderBuffer[3]=(TrackSize >>8);
HeaderBuffer[4]=((!DblSided)<<4);
HeaderBuffer[0xC]=0x12;
HeaderBuffer[0xD]=0x34;
HeaderBuffer[0xE]=0x56;
HeaderBuffer[0xF]=0x78;
HeaderSize=0x10;
FileSize=1;
break;
}
SetFilePointer(hr,0,nullptr,FILE_BEGIN);
WriteFile(hr,HeaderBuffer,HeaderSize,&BytesWritten,nullptr);
SetFilePointer(hr,FileSize-1,nullptr,FILE_BEGIN);
WriteFile(hr,&Dummy,1,&BytesWritten,nullptr);
CloseHandle(hr);
return 0;
}
void LoadConfig() // Called on SetIniPath
{
unsigned char Index=0;
char Temp[16]="";
char DiskRomPath[MAX_PATH], RGBRomPath[MAX_PATH];
char DiskName[MAX_PATH]="";
unsigned int RetVal=0;
#ifdef COMBINE_BECKER
BeckerEnabled=Setting().read(MS_BECKER,"DWEnable", 0);
Setting().read(MS_BECKER,"DWServerAddr","127.0.0.1",BeckerAddr,MAX_PATH);
Setting().read(MS_BECKER,"DWServerPort","65504",BeckerPort,32);
if(*BeckerAddr == '\0') strcpy(BeckerAddr,"127.0.0.1");
if(*BeckerPort == '\0') strcpy(BeckerPort,"65504");
becker_sethost(BeckerAddr,BeckerPort);
becker_enable(BeckerEnabled);
#endif
Setting().read(MS_PATHS, "FloppyPath", "", FloppyPath, MAX_PATH);
SelectRom = Setting().read(MS_FD502,"DiskRom",1); //0 External 1=TRSDOS 2=RGB Dos
Setting().read(MS_FD502,"RomPath","",RomFileName,MAX_PATH);
PersistDisks=Setting().read(MS_FD502,"Persist",1);
CheckPath(RomFileName);
LoadExtRom(External,RomFileName); //JF
GetModuleFileName(nullptr, DiskRomPath, MAX_PATH);
PathRemoveFileSpec(DiskRomPath);
strcpy(RGBRomPath, DiskRomPath);
strcat(DiskRomPath, "disk11.rom");
strcat(RGBRomPath, "rgbdos.rom");
LoadExtRom(TandyDisk, DiskRomPath); // Why load?
LoadExtRom(RGBDisk, RGBRomPath); // Why load?
if (PersistDisks) // TODO: Fix this, checkbox was removed
for (Index=0;Index<4;Index++)
{
sprintf(Temp,"Disk#%i",Index);
Setting().read(MS_FD502,Temp,"",DiskName,MAX_PATH);
if (strlen(DiskName))
{
RetVal=mount_disk_image(DiskName,Index);
//MessageBox(0, "Disk load attempt", "OK", 0);
if (RetVal)
{
if (!strcmp(DiskName,"*Floppy A:")) //RealDisks
PhysicalDriveA=Index+1;
if (!strcmp(DiskName,"*Floppy B:"))
PhysicalDriveB=Index+1;
}
}
}
ClockEnabled=Setting().read(MS_FD502,"ClkEnable",1);
SetTurboDisk(Setting().read(MS_FD502, "TurboDisk", 1));
SendMessage(gVccWnd,WM_VCC_UPD_MENU,(WPARAM) 0,(LPARAM) 0);
return;
}
void SaveConfig()
{
unsigned char Index=0;
char Temp[16]="";
ValidatePath(RomFileName);
Setting().write(MS_FD502,"DiskRom",SelectRom );
Setting().write(MS_FD502,"RomPath",RomFileName);
Setting().write(MS_FD502,"Persist",PersistDisks );
if (PersistDisks)
for (Index=0;Index<4;Index++)
{
sprintf(Temp,"Disk#%i",Index);
Setting().write(MS_FD502,Temp,gVirtualDrive[Index].ImageName);
}
Setting().write(MS_FD502,"ClkEnable",ClockEnabled );
Setting().write(MS_FD502, "TurboDisk", SetTurboDisk(QUERY));
if (strcmp(FloppyPath, "") != 0) {
Setting().write(MS_PATHS, "FloppyPath", FloppyPath);
}
#ifdef COMBINE_BECKER
Setting().write(MS_BECKER,"DWEnable", BeckerEnabled);
Setting().write(MS_BECKER,"DWServerAddr", BeckerAddr);
Setting().write(MS_BECKER,"DWServerPort", BeckerPort);
#endif
return;
}
unsigned char LoadExtRom( unsigned char RomType,const char *FilePath) //Returns 1 on if loaded
{
FILE *rom_handle=nullptr;
unsigned short index=0;
unsigned char RetVal=0;
unsigned char *ThisRom[3]={ExternalRom,DiskRom,RGBDiskRom};
rom_handle=fopen(FilePath,"rb");
if (rom_handle==nullptr)
memset(ThisRom[RomType],0xFF,EXTROMSIZE);
else
{
while ((feof(rom_handle)==0) & (index