107107#include < stdio.h>
108108#include < ctype.h>
109109#include < sys/stat.h>
110-
111110#include " sdc.h"
112111
112+
113113// ======================================================================
114114// Functions
115115// ======================================================================
@@ -118,23 +118,17 @@ bool LoadRom(void);
118118void SDCWrite (unsigned char data,unsigned char port);
119119unsigned char SDCRead (unsigned char port);
120120void SDCInit (void );
121-
122121void AssertInterupt (unsigned char ,unsigned char );
123122void MemWrite (unsigned char ,unsigned short );
124123unsigned char MemRead (unsigned short );
125-
126- static char IniFile[MAX_PATH]={0 }; // Ini file name from config
127-
128124typedef void (*ASSERTINTERUPT) (unsigned char ,unsigned char );
129125typedef void (*DYNAMICMENUCALLBACK)( char *,int , int );
130126typedef unsigned char (*MEMREAD8)(unsigned short );
131127typedef void (*MEMWRITE8)(unsigned char ,unsigned short );
132-
133128static void (*AssertInt)(unsigned char ,unsigned char )=NULL;
134129static void (*DynamicMenuCallback)( char *,int , int )=NULL;
135130static unsigned char (*MemRead8)(unsigned short )=NULL;
136131static void (*MemWrite8)(unsigned char ,unsigned short )=NULL;
137-
138132LRESULT CALLBACK SDC_Config (HWND, UINT, WPARAM, LPARAM);
139133
140134void LoadConfig (void );
@@ -207,9 +201,10 @@ Interface IF;
207201char PakRom[0x4000 ];
208202
209203// Host paths for SDC
210- char SDCard[MAX_PATH] = {}; // SD card root directory
211- char CurDir[256 ] = {}; // SDC current directory
212- char SeaDir[MAX_PATH] = {}; // Last directory searched
204+ char IniFile[MAX_PATH] = {}; // Vcc ini file name
205+ char SDCard[MAX_PATH] = {}; // SD card root directory
206+ char CurDir[256 ] = {}; // SDC current directory
207+ char SeaDir[MAX_PATH] = {}; // Last directory searched
213208
214209// Packed file records for SDC file commands
215210#pragma pack(1)
@@ -265,6 +260,9 @@ unsigned int stream_lsn;
265260
266261FILE *f_ROM = NULL ;
267262
263+ char Status[16 ] = {};
264+ bool StartupComplete = false ;
265+
268266// ======================================================================
269267// DLL exports
270268// ======================================================================
@@ -322,24 +320,31 @@ extern "C"
322320 return ;
323321 }
324322
325- // Capture the Fuction transfer point for the CPU assert interupt
323+ // Capture the Function transfer point for the CPU assert interupt
326324 __declspec (dllexport) void AssertInterupt(ASSERTINTERUPT Dummy)
327325 {
328326 AssertInt=Dummy;
329327 return ;
330328 }
331329
330+ static int idle_ctr = 0 ;
332331 // Return SDC status.
333332 __declspec (dllexport) void ModuleStatus(char *MyStatus)
334333 {
335- strcpy (MyStatus," SDC" );
334+ strncpy (MyStatus,Status,12 );
335+ if (idle_ctr < 100 ) {
336+ idle_ctr++;
337+ } else {
338+ idle_ctr = 0 ;
339+ strncpy (Status," SDC:Idle" ,12 );
340+ }
336341 return ;
337342 }
338343
339344 // Set ini file path and Initialize SDC
340345 __declspec (dllexport) void SetIniPath (char *IniFilePath)
341346 {
342- strcpy (IniFile,IniFilePath);
347+ strncpy (IniFile,IniFilePath,MAX_PATH );
343348 return ;
344349 }
345350
@@ -366,6 +371,8 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID rsvd)
366371{
367372 if (reason == DLL_PROCESS_ATTACH) {
368373 hinstDLL = hinst;
374+ StartupComplete = false ;
375+
369376 } else if (reason == DLL_PROCESS_DETACH) {
370377 CloseDrive (0 );
371378 CloseDrive (1 );
@@ -374,6 +381,7 @@ BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID rsvd)
374381 hConfDlg = NULL ;
375382 }
376383 }
384+
377385 return TRUE ;
378386}
379387
@@ -450,16 +458,18 @@ SDC_Config(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
450458 SelectCardBox ();
451459 break ;
452460 case ID_SD_BOX:
453- if (HIWORD (wParam) == EN_CHANGE)
454- GetWindowText (hSDCardBox,SDCard,MAX_PATH);
461+ if (HIWORD (wParam) == EN_CHANGE) {
462+ char tmp[MAX_PATH];
463+ GetWindowText (hSDCardBox,tmp,MAX_PATH);
464+ if (*tmp != ' \0 ' ) strncpy (SDCard,tmp,MAX_PATH);
465+ }
455466 break ;
456467 case ID_STARTUP_BANK:
457468 if (HIWORD (wParam) == EN_CHANGE) {
458469 char tmp[4 ];
459470 GetWindowText (hStartupBank,tmp,4 );
460471 StartupBank = atoi (tmp);
461472 if (StartupBank > 7 ) StartupBank &= 7 ;
462- _DLOG (" Startup Bank %d" ,StartupBank);
463473 }
464474 break ;
465475 case IDOK:
@@ -485,15 +495,14 @@ void LoadConfig(void)
485495{
486496 GetPrivateProfileString
487497 (" SDC" , " SDCardPath" , " " , SDCard, MAX_PATH, IniFile);
498+
488499 if (!IsDirectory (SDCard)) {
489- MessageBox
490- (0 ," Invalid or missing SDCard Path in VCC init" ," Error" ,0 );
491- return ;
500+ MessageBox (0 ," Invalid SDCard Path in VCC init" ," Error" ,0 );
492501 }
493502
494503 for (int i=0 ;i<8 ;i++) {
495504 char txt[32 ];
496- sprintf (txt," FlashFile_%d" ,i);
505+ snprintf (txt,MAX_PATH ," FlashFile_%d" ,i);
497506 GetPrivateProfileString
498507 (" SDC" , txt, " " , FlashFile[i], MAX_PATH, IniFile);
499508 }
@@ -547,7 +556,15 @@ void SDCInit(void)
547556
548557 // Load SDC settings
549558 LoadConfig ();
550- CurrentBank = StartupBank;
559+ if (!StartupComplete){
560+ CurrentBank = StartupBank;
561+ StartupComplete = true ;
562+ } else if (CurrentBank != StartupBank) {
563+ StartupComplete = false ;
564+ }
565+
566+ LoadRom ();
567+
551568 SetCurDir (" " ); // May be changed by ParseStartup()
552569
553570 memset ((void *) &Disk,0 ,sizeof (Disk));
@@ -558,7 +575,6 @@ void SDCInit(void)
558575 // init the interface
559576 memset (&IF,0 ,sizeof (IF));
560577
561- LoadRom ();
562578 return ;
563579}
564580
@@ -581,18 +597,18 @@ void InitFlashBox(void)
581597 // Add items to the listbox
582598 for (int index=0 ; index<8 ; index++) {
583599 // Compact the path to fit in flash box. 385 is width pixels
584- strcpy (path,FlashFile[index]);
600+ strncpy (path,FlashFile[index],MAX_PATH );
585601 PathCompactPath (hdc,path,385 );
586602 sprintf (text," %d %s" ,index,path);
587603 SendMessage (hFlashBox, LB_ADDSTRING, 0 , (LPARAM)text);
588604 }
589-
590605 ReleaseDC (hFlashBox,hdc);
591606}
592607
593608// ------------------------------------------------------------
594609// Init SD card box
595610// ------------------------------------------------------------
611+
596612void InitCardBox (void )
597613{
598614 hSDCardBox = GetDlgItem (hConfDlg,ID_SD_BOX);
@@ -690,6 +706,7 @@ bool LoadRom()
690706{
691707 char * RomName;
692708 RomName = FlashFile[CurrentBank];
709+
693710 if ((*RomName == ' \0 ' ) || (strcmp (RomName," -empty-" ) == 0 )) {
694711 _DLOG (" LoadRom bank %d is empty\n " ,CurrentBank);
695712 if (CurrentBank == StartupBank) {
@@ -707,7 +724,7 @@ bool LoadRom()
707724 int ctr = 0 ;
708725 f_ROM = fopen (RomName, " rb" );
709726 if (f_ROM == NULL ) {
710- _DLOG (" LoadRom '%s' failed\n " ,RomName);
727+ _DLOG (" LoadRom '%s' failed errno %d \n " ,RomName,errno );
711728 return false ;
712729 }
713730
@@ -777,7 +794,7 @@ void ParseStartup(void)
777794// ----------------------------------------------------------------------
778795void SDCWrite (unsigned char data,unsigned char port)
779796{
780- if ((!IF.sdclatch ) && (port > 0x42 )) return ;
797+ if ((!IF.sdclatch ) && (port > 0x43 )) return ;
781798
782799 switch (port) {
783800 // Control Latch
@@ -1111,8 +1128,8 @@ void UpdateSD(void)
11111128// ----------------------------------------------------------------------
11121129void BankSelect (int data)
11131130{
1114- bool init = (CurrentBank != (data & 7 ));
1115- _DLOG (" BankSelect %02x\n " ,data);
1131+ bool init = (( CurrentBank & 7 ) != (data & 7 ));
1132+ _DLOG (" >>>> BankSelect %02x\n " ,data);
11161133 CurrentBank = data & 7 ;
11171134 if (init) SDCInit ();
11181135}
@@ -1179,7 +1196,7 @@ bool ReadDrive(unsigned char cmdcode, unsigned int lsn)
11791196 return false ;
11801197 }
11811198
1182- // _DLOG("ReadDrive %d sec %d\n ",drive,lsn);
1199+ sprintf (Status, " SDC:Rd %d, %d " ,drive,lsn);
11831200 LoadReply (buf,cnt);
11841201 return true ;
11851202}
@@ -1242,7 +1259,7 @@ void WriteSector(void)
12421259 DWORD cnt = 0 ;
12431260 int drive = IF.cmdcode & 1 ;
12441261 unsigned int lsn = (IF.param1 << 16 ) + (IF.param2 << 8 ) + IF.param3 ;
1245- _DLOG ( " WriteSector drive %d sec %d \n " ,drive,lsn);
1262+ sprintf (Status, " SDC:Wr %d, %d " ,drive,lsn);
12461263
12471264 if (Disk[drive].hFile == NULL ) {
12481265 IF.status = STA_FAIL;
@@ -1775,7 +1792,7 @@ bool SearchFile(const char * pattern)
17751792 // Save directory portion for prepending to results
17761793 char * pnam = strrchr (path,' /' );
17771794 if (pnam != NULL ) pnam[1 ] = ' \0 ' ;
1778- strcpy (SeaDir,path);
1795+ strncpy (SeaDir,path,MAX_PATH );
17791796 return true ;
17801797 }
17811798
0 commit comments