⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tmmon.c

📁 PNX系列设备驱动 PNX系列设备驱动
💻 C
📖 第 1 页 / 共 3 页
字号:
}

/* Get pathname to TCS. */

static void getTCSpath(char *path)
{
	CSParam		myPB;
	Boolean		done = false;
	Boolean		found = false;
	StringPtr 	volName = nil;
	short		myVRefNum;
	long		myDirID;
	OSErr		myErr;
	Str255		string;
	CInfoPBRec	mySpec1;
	CInfoPBRec	mySpec2;
	FSSpec		myMatches[kMaxMatches];
	char		*mySearchCache;
	
	/*
		Find TCS by looking where the file "tm1000.md" is located. The main
		reason tmmon needs to know the TCS path is to find system libraries for
		dynamic loading. If we can't find this file then TCS probably is
		not installed.
		
		This search is not bulletproof because it searches only the default volume, 
		which is not complete since TCS may reside on other volumes, such as the 
		network. Also there may be more than one file of this name.###		
	*/
	
	mySearchCache = malloc(kOptBufferSize);
	if (!mySearchCache)
	{
		printf("###Out of memory.\n");
		path[0] = 0;
		return;
	}
	
	strcpy((char *) string, "tm1000.md");
	c2pstr((char *) string);
	
	/* Stuff the search parameter block. */
			
	if (HGetVol(volName, &myVRefNum, &myDirID) == noErr)
	{
		myPB.ioCompletion = nil;				// no completion routine
		myPB.ioNamePtr = nil;					// no volume name; use vRefNum
		myPB.ioVRefNum = myVRefNum;				// volume to search
		myPB.ioMatchPtr	= myMatches;			// points to results buffer
		myPB.ioReqMatchCount = kMaxMatches;		// number of matches
		myPB.ioSearchBits = fsSBFullName		// search on full name
						  + fsSBFlAttrib;		// search on file attributes

		myPB.ioSearchInfo1	= &mySpec1;			// points to first criteria set
		myPB.ioSearchInfo2	= &mySpec2;			// points to second criteria set
		myPB.ioSearchTime = 0;					// no timeout on searches
		myPB.ioCatPosition.initialize = 0;		// set hint to 0
		myPB.ioOptBuffer = mySearchCache; 		// point to search cache
		myPB.ioOptBufSize = kOptBufferSize;		// size of search cache
		
		mySpec1.hFileInfo.ioNamePtr = string; 	// point to string to find
		mySpec1.hFileInfo.ioFlAttrib = 0;		// clear bit 4 to ask for files

		mySpec2.hFileInfo.ioNamePtr = nil;
		mySpec2.hFileInfo.ioFlAttrib = 0x10;	// set mask for bit 4

		while (1)
		{
			myErr = PBCatSearchSync(&myPB);
			done = (myErr == eofErr);
			
			/*
				If we find it the pathname will be something like
				
				"HD:CWPro 3:Metrowerks CodeWarrior:TriMedia:lib:tm1000.md"				
			*/
			
			if ((myErr == noErr || done) && (myPB.ioActMatchCount > 0))
			{
				int		i;
				for (i = 0; i < myPB.ioActMatchCount; i++)
				{
					if ((fss2path(&myMatches[i], path, false)) == noErr)
					{
						if (strstr(path, "CodeWarrior"))
						{
							char 	*p;
							
							/* Go up two levels. */
							p = strrchr(path, kPathNameSeparator);
							*p = 0;
							p = strrchr(path, kPathNameSeparator);
							*p = 0;
							found = true;
							break;
						}
					}
				}
				break;		
			}
			
			if (myErr != noErr)
				break;
		}
	}

	if (mySearchCache)
		free(mySearchCache);
	
	if (!found)
	{
		printf("###Cannot find TriMedia Compilation System. Dynamic loading may fail.\n");
	}
}

/* Convert Mac File System spec into a Unix-style pathname. */

static OSErr fss2path(FSSpecPtr spec, char *pathName, Boolean keepname)
{
	CInfoPBRec		myPB;
	Str255			dirName;
	OSErr			err;
	char			temppath[FILENAME_MAX + 1];	/* Temp buffer for building pathnames. */
	int				len;

	*pathName = '\0';
	temppath[0] = '\0';
	myPB.hFileInfo.ioNamePtr = dirName;
	myPB.hFileInfo.ioVRefNum = spec->vRefNum;
	myPB.dirInfo.ioDrParID = spec->parID;
	myPB.hFileInfo.ioFDirIndex = -1;

	while (1)
	{
		myPB.dirInfo.ioDrDirID = myPB.dirInfo.ioDrParID;
		if ((err = PBGetCatInfoSync(&myPB)) != noErr)
			return err;
		p2cstr(dirName);
		
		strcat ((char *) dirName, ":");

		strcpy (temppath, (char *) dirName);			// Save current dirname with colon
		strcat (temppath, pathName);		// Prefix current dirname with pathname
		strcpy (pathName, temppath);		// Save current pathname
		c2pstr((char *) dirName);
		if (myPB.dirInfo.ioDrDirID == fsRtDirID)
			break;
	}
	
	/* Append the file name. */
	
	if (keepname)
	{
		len = strlen(pathName);
		if (len > 0)
			copy_pascal_to_c_str(spec->name, &pathName[len]);
	}
			
	return err;	
}

static void copy_pascal_to_c_str(StringPtr src, char * dst)
{
	size_t len = src[0];
	
	memcpy(&dst[0], &src[1], len);
	
	dst[len] = '\0';
}

static void	PrepareToLoad(char *filename, int argc, char *argv[], char *actualFilename, int *actualArgc, char *actualArgv[])
{
	TMObj_Module_Rec	header;
	FILE				*f;
	int					i;

	if ((f = fopen(filename, "rb")) == Null)
	{
		strcpy(actualFilename, filename);
		return;
	}
	fread( &header, sizeof(header), 1, f);
	fclose(f);

	/*
		If we're loading a TMObj_AppSegment object module we have to load the appropriate
		appshell.out instead, passing the app name as its first command line arg followed
		by the remaining command line arguments.
	*/
		
	if (header.type == TMObj_AppSegment) 
	{
		strcpy(actualFilename, tcspath);
		strcat(actualFilename, ":lib:");
		strcat(actualFilename, header.code_endian ? "el" : "eb");
		strcat(actualFilename, ":MacOS:appshell.out");
		
		*actualArgc = argc + 1;
		actualArgv[0] = actualFilename;
		actualArgv[1] = filename;
		for (i = 0; i < argc; i++)
			actualArgv[i + 2] = argv[i + 1];
	}
	else
	
	/* Just copy as is. */
	
	{
		strcpy(actualFilename, filename);
		*actualArgc = argc;
		for (i = 0; i < argc; i++)
			actualArgv[i] = argv[i];
	}
}


/************************************* Main Loop ********************************************/

void main()
{
	char Command[8];	// allows for 8-character commands
	char *argv[ 30 ];
	UInt32  i, j;
	Int32 n;
	unsigned char* arr;
	int numOfArgs;
	int status;
	TMDwnLdr_Status		tmstatus;
	TMDwnLdr_SharedSectionTab_Handle 	shared_sections;
	char CommandLine[132];
	char syscommand[132];
	char buf[256];
    Boolean codeLoaded = false, user_interrupt;
	ThreadID RPCServThreadID = 0;
	extern UInt	TM_frequency;
	int node;
	Ptr				host_memory = NULL;
	unsigned long	host_memory_size = 0;
	
	SIOUXSettings.asktosaveonclose = true;
	SIOUXSettings.autocloseonquit = true;

	SIOUXSettings.columns = 80;	
	SIOUXSettings.rows = 40;
	SIOUXSettings.toppixel = 40;	
	SIOUXSettings.leftpixel = 10;	

	printf("***** TMMON - TriMedia monitor and host communication tool.  *****\n\n");
	
	printf("Searching for TCS and DLL pathnames...\n");
	
    OpenDll_add_dll_path(".");
    if (GetTMPreference("dllpath", buf) && strlen(buf) > 0)
		OpenDll_add_dll_path(buf);

	/*	Find TCS. */
	
	getTCSpath(tcspath);
	
	if (tcspath[0])
	{
		strcpy(buf, tcspath);
		strcat(buf, ":lib:eb");
		OpenDll_add_dll_path(buf);
		strcat(buf, ":MacOS");
		OpenDll_add_dll_path(buf);

		strcpy(buf, tcspath);
		strcat(buf, ":lib:el");
		OpenDll_add_dll_path(buf);
		strcat(buf, ":MacOS");
		OpenDll_add_dll_path(buf);

		strcpy(buf, tcspath);
		strcat(buf, ":OS:pSOS:pSOSystem:sys:os");
		OpenDll_add_dll_path(buf);
	}
	else
		strcpy(tcspath, "Where_Is_TCS");

	/* Find all the TriMedia's in the system. */

	printf("Searching for TriMedia's...\n\n");

    FindTMs();
	
	if (NumberOfTMs > 0)
	{

		printf("Found the following TriMedia's in the system:\n");
		for (i = 0; i < NumberOfTMs; i++)
		{
			InitTM(false, i);
			printf("\nNode number:\t\t%d\n", i);
			printf("Node name:\t\t\t%s\n", TMInfo[i].node_name);
			printf("Clock frequency:\t%d (%d MHz)\n", TMInfo[i].frequency, TMInfo[i].frequency / 1000000);
			printf("SDRAM base:\t\t\t0x%08X\n", MMIO_M(i, DRAM_BASE));
			printf("SDRAM size:\t\t\t0x%08X\n", MMIO_M(i, DRAM_LIMIT) - MMIO_M(i, DRAM_BASE));
			printf("MMIO base:\t\t\t0x%08X\n",MMIO_M(i, MMIO_BASE));
			printf("Revision ID:\t\t0x%08X\n", TMInfo[i].revision_id);
		}	
	}
	else
	{
		printf("Did not find any TriMedia's in the system (sigh).\n");
		exit(0);
	}
	
	/* Set stdout, stdin, and stderr to console. */
	
	stdin_file[0] = '\0';
	stdout_file[0] = '\0';
	stderr_file[0] = '\0';
	stdin_fd = STDIN_HANDLE;
	stdout_fd = STDOUT_HANDLE;
	stderr_fd = STDERR_HANDLE;
	
	while (1) 
	{
		printf(">");
		gets(CommandLine);
		{
			char			*s;
			Boolean			istext;
			Boolean			iquote;

			/* 
				Convert the command string into argc, argv format. Pick up any
				single quote-delimited parts as a single argument. They may be
				file pathnames. 
			 */
			
			strcpy(syscommand, CommandLine);
			s = syscommand;
			numOfArgs = 0;
			istext = !isspace((int) *s);
			iquote = ISQUOTE(*s);
			if (iquote)
			{
				argv[numOfArgs] = ++s;
				numOfArgs = 1;			
			}
			else if (istext)
			{
				argv[numOfArgs] = s++;
				numOfArgs = 1;
			}
			while (*s)
			{
				/* Was quote, accumulate until the next quote. */
				if (iquote)
				{
					while (*s)
					{
						if (ISQUOTE(*s))
						{
							*s++ = '\0';
							break;
						}
						s++;
					}
					iquote = false;
					continue;
				}
				else if (istext)
				{
					if (iquote = ISQUOTE(*s))	// Was text; now it's a quote
					{
						argv[numOfArgs] = ++s;
						numOfArgs++;					
					}
					else if (!(istext = !isspace((int) *s)))	// Was text; now it's space
						*s++ = '\0';
					else
						s++;
				}
				else
				{
					if (iquote = ISQUOTE(*s))	// Was space; now it's a quote
					{
						argv[numOfArgs] = ++s;
						numOfArgs++;					
					}
					else if (istext = !isspace((int) *s))	// Was space; now it's text
					{
						argv[numOfArgs] = s++;
						numOfArgs++;
					}
					else
						s++;
				}
			}
			argv[numOfArgs] = '\0';	// Required by standard
			strcpy(Command, argv[0]);
		}

		ToUpperString((Ptr)Command);
		
		status = noErr;
						
		switch(Command[0]) 
		{
			case	0:					/* Null command. */
				break;
				
			case '?' :					/* help */
			case 'H' :					/* help */
				Help();
				break;

			case 'A':					/* Allocate host memory */
				if (numOfArgs > 1)
				{
					OSErr	err;
					
					j = strtoul(argv[1], NULL, 10);
					if (j < 1 || j > 0x7FFFFFFF)
					{
						printf("Unable to allocate %d bytes of host memory.\n", j);
						break;
					}
					host_memory_size = j;
					if (host_memory != NULL)
					{
						err = UnlockMemory(host_memory, host_memory_size);
						DisposePtr(host_memory);
						host_memory = NULL;
					}
					host_memory = NewPtr(host_memory_size);
//					err = LockMemoryContiguous(host_memory, host_memory_size);
					err = LockMemory(host_memory, host_memory_size);
					if (host_memory == NULL)
						printf("Unable to allocate %d bytes of host memory.\n", host_memory_size);
					else if (err != noErr)
						printf("Unable to allocate %d bytes of locked host memory.\n", host_memory_size);
					else
					{
    					unsigned long 	nrof_entries;
    					MemoryBlock		blocks[MEMORY_BLOCKS];
    					
						blocks[0].address = host_memory;
						blocks[0].count = host_memory_size;
    					nrof_entries = MEMORY_BLOCKS-1;       
    					err = GetPhysical( (void*)blocks, &nrof_entries);					
						printf("Allocated %d bytes of host memory at logical address 0x%08x\n", host_memory_size, host_memory);
						printf("consisting of %d physical block(s) as follows:\n", nrof_entries);
						for (i = 1; i <= nrof_entries; i++)
							printf("Block %d of %d bytes at physical address 0x%08x.\n", i, blocks[i].count, blocks[i].address);						
					}
			    }				
				break;

			case 'C' :					/* Display config data. */
				if (numOfArgs == 2)		/* Display specific TM configuration space. */	
				{
					n = strtoul(argv[1], NULL, 10);
					if (n < NumberOfTMs)
					{
						DisplayConfiguration(&TMInfo[n].NodeID);
					}
					else
					{
			   	 		printf("TriMedia node %d doesn't exits.\n", n);
			   	 		break;
					}
			    }
			    else					/* Display them all. */
			    {
			    	for (i = 0; i < NumberOfTMs; i++)
			    	{
			    		printf("\nConfiguration space for node %d:\n", i);
			    		DisplayConfiguration(&TMInfo[i].NodeID);
			    	}
			    }
				break;
								
			case 'F':					/* Set or display clock frequency. */	
			    switch(numOfArgs)
			    {
			    	case 1:				/* Display frequency for all nodes. */
				    	for (i = 0; i < NumberOfTMs; i++)
				    	{
							printf("Frequency for node %d is set to %d (%d MHz)\n", 
								i, TMInfo[i].frequency, TMInfo[i].frequency / 1000000);
				    	}
				    	break;
			    	
			    	case 2:				/* Set frequency for all nodes. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -