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

📄 term.cpp

📁 一个可以模拟MIPS汇编语言在硬件上运行的模拟器。 该模拟器对于43条最常用的指令进行了实现。而且实现了端口通信
💻 CPP
字号:
#include <cstdio>
#include <cstdlib>
#include <winsock.h>
#include <windows.h>
#include <cstring>
#include <conio.h>


const int SCRN_X = 80;
const int SCRN_Y = 100;
const int LimitConsoleBuffer = 4096;
char com_name[] = "COM0";
HANDLE com = INVALID_HANDLE_VALUE;
FILE *load_file;

struct Tconsole
{
	int mode;
	HANDLE hstdout;
	COORD scrn;
	CONSOLE_SCREEN_BUFFER_INFO sbinf;
	char buffer[LimitConsoleBuffer+1];
	Tconsole()
	{
		mode = 0;
		hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
		WORD attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE;
		SetConsoleTextAttribute(hstdout,attr);
		clearscrn();
		work();
	}	
	void clearscrn()
	{
		int size;
		scrn.X = SCRN_X;
		scrn.Y = SCRN_Y;
		SetConsoleScreenBufferSize(hstdout,scrn);
		GetConsoleScreenBufferInfo(hstdout,&sbinf);
		size = SCRN_X*SCRN_Y;
		scrn.X = scrn.Y = 0;
		FillConsoleOutputAttribute(hstdout,sbinf.wAttributes,size,scrn,NULL);
		FillConsoleOutputCharacter(hstdout,' ',size,scrn,NULL);
		gotoxy(0,0);
	}
	void gotoxy(int xpos,int ypos)
	{
		scrn.X = xpos;
		scrn.Y = ypos;
		SetConsoleCursorPosition(hstdout,scrn);
	}
	void gotoxy_cur(int next)
	{
		if (next == 0)
			return ;
		GetConsoleScreenBufferInfo(hstdout,&sbinf);
		int pos = sbinf.dwCursorPosition.Y*SCRN_X+sbinf.dwCursorPosition.X+next;
		if (next > 0)
		{
			if (pos > SCRN_X*SCRN_Y)
				return ;
		}
		else 
		{
			if (pos < 0)
				return ;
		}
		scrn.X = pos%SCRN_X;
		scrn.Y = pos/SCRN_X;
		SetConsoleCursorPosition(hstdout,scrn);
	}
	void goto_pos(int pos)
	{
		GetConsoleScreenBufferInfo(hstdout,&sbinf);
		if (pos == 0)
			pos = 0;
		else pos += sbinf.dwCursorPosition.X;
		if (pos < 0 || pos >= SCRN_X)
			return ;
		scrn.X = pos;
		scrn.Y = sbinf.dwCursorPosition.Y;
		SetConsoleCursorPosition(hstdout,scrn);
	}
	void work();
	void work_sim();
	void work_com(int );
	void parse_and_run(char [],int );
	int get_input();
};


int Tconsole::get_input()
{
	int len = 0, cur = 0;
	char ch;
	buffer[len] = '\0';
	while (true)
	{
		ch = getch();
		if (ch == 8)
		{
			if (cur != 0)
			{
				if (cur == len)
				{
					cur --;
					buffer[--len] = '\0';
					gotoxy_cur(-1);
					printf(" ");
					gotoxy_cur(-1);
				}
				else
				{
					memcpy(&buffer[cur-1],&buffer[cur],len-cur);
					buffer[--len] = '\0';
					printf("\b%s ",&buffer[--cur]);
					gotoxy_cur(cur-len-1);
				}
			}
		}
		else if (ch == -32)
		{
			ch = getch();
			switch (ch)
			{
			case 75: //left
				if (cur != 0)
				{
					cur --;
					gotoxy_cur(-1);
				}
				break;
			case 77: //right
				if (cur != len)
				{
					cur ++;
					gotoxy_cur(1);
				}
				break;
			case 71: //home
				gotoxy_cur(-cur);
				cur = 0;
				break;
			case 79: //end
				gotoxy_cur(len-cur);
				cur = len;
				break;
			case 83: //delete
				if (cur != len)
				{
					len--;
					memcpy(&buffer[cur],&buffer[cur+1],len-cur);
					buffer[len] = '\0';
					printf("%s ",&buffer[cur]);
					gotoxy_cur(cur-len-1);
				}
				break;
			}
		}
		else if (ch == 13)
			return len;
		else if (ch == 27)
		{
			for (int i=0;i<len;i++)
				buffer[i] = ' ';
			gotoxy_cur(-cur);
			printf("%s",buffer);
			gotoxy_cur(-len);
			cur = len = 0;
			buffer[len] = '\0';
		}
		else
		{
			if (ch <= 126 && ch >= 32)
			{
				if (cur == len)
				{
					if (len != LimitConsoleBuffer)
					{
						cur ++;
						buffer[len++] = ch;
						buffer[len] = '\0';
						printf("%c",ch);
					}
				}
				else
				{
					if (len == LimitConsoleBuffer)
					{
						memcpy(&buffer[cur+1],&buffer[cur],len-cur-1);
						buffer[cur] = ch;
						printf("%s",&buffer[cur++]);
						gotoxy_cur(cur-len);
					}
					else
					{
						memcpy(&buffer[cur+1],&buffer[cur],len-cur);
						buffer[++len] = '\0';
						buffer[cur] = ch;
						printf("%s",&buffer[cur++]);
						gotoxy_cur(cur-len);
					}
				}
			}
		}
	}
}

void Tconsole::parse_and_run(char buffer[],int len)
{
	if (strcmp("q",buffer) == 0)
	{
		mode = -1;
		printf("\n");
		return ;
	}
	char *argv[64];
	int in_command = 0;
	int argc = 0;
	for (int i=0;buffer[i] != '\0';i++)
	{
		if (buffer[i] != ' ')
		{
			if (in_command == 0)
				argv[argc++] = &buffer[i];
			in_command = 1;
		}
		else
		{
			buffer[i] = '\0';
			in_command = 0;
		}
	}
	if (argc == 1)
	{
		if (strcmp(argv[0],"help") == 0)
		{
			printf("Help....\n");
			return ;
		}
		else if (strcmp("com1",argv[0]) == 0 || strcmp(argv[0],"COM1") == 0)
			mode = 1;
		else if (strcmp("com2",argv[0]) == 0 || strcmp(argv[0],"COM2") == 0)
			mode = 2;
		else if (strcmp("sim",argv[0]) == 0 || strcmp("SIM",argv[0]) == 0)
			mode = 3;
		else printf("\n  ->Unknown command. Use ``help'' to list commands.\n");
	}
	else printf("\n  ->Unknown command. Use ``help'' to list commands.\n");
}

void Tconsole::work()
{
	while (true)
	{
		printf(">>");
		mode = 0;
		while (mode == 0)
		{
			int len = get_input();
			parse_and_run(buffer,len);
			if (mode == 0)
				printf("\n>>");
		}
		switch (mode)
		{
		case -1:
			exit(0);
		case 1:
			work_com(1);
			break;
		case 2:
			work_com(2);
			break;
		case 3:
			printf("\n");
			work_sim();
			break;
		}
	}
}

void Tconsole::work_sim()
{
	WSADATA wsaData;
	int errcode=WSAStartup(0x101,&wsaData);
	if(errcode != 0)
	{
		printf("Error ...\n");
		return ;
	}
	SOCKET client;
	sockaddr_in server;
	server.sin_family=AF_INET; //Address family
	server.sin_port=htons(8000);
	server.sin_addr.s_addr=inet_addr("127.0.0.1");
	client = socket(AF_INET,SOCK_STREAM,0);
	if(client == INVALID_SOCKET)
	{
		printf("Failed to create socket.\n");
		return ;
	}

	if(connect(client,(sockaddr*)&server,sizeof(server))!=0)
	{
		printf("Failed to bind.\n");
		return ;
	}

	int len = recv(client,buffer,LimitConsoleBuffer,0);

	if (len == 0)
	{
		printf("     Can not connect with Simulator...\n");
		closesocket(client);
		WSACleanup();
		return ;
	}
	if (len != 0)
	{
		buffer[len] = '\0';
		printf("   %s",buffer);
	}
	unsigned long ul = 1;
	unsigned short code;
	char hi, lo;
	ioctlsocket(client,FIONBIO,(unsigned long *)&ul);
	char ch;
	load_file = NULL;
	while (true)
	{
		if (kbhit())
		{
			ch = getch();
			if (ch == 7)
				goto break_while;
			else ungetch(ch);
		}
		switch (len = recv(client,&ch,1,0))
		{
		case -1:
			if (kbhit())
			{
				ch = getch();
				if (ch != 12)
				{
					ungetch(ch);
					while (kbhit())
					{
						ch = getch();
						if ((ch >= 21 && ch <= 125) || ch == 8 || ch == 13)
						{
							if (ch == 13)
								ch = 10;
							send(client,&ch,1,0);
							len = recv(client,&ch,1,0);
							if (len == 1)
							{
								printf("%c",ch);
								len = 0;
							}
						}
					}
				}
				else
				{
					printf(" -- Ctrl+L --\n");
					while (load_file == NULL)
					{
						printf("FILE NAME -->>");
						len = get_input();
						buffer[len] = 0;
						if (len != 0)
						{
							load_file = fopen(buffer,"rb");
							if (load_file == NULL)
								printf("  Can not open file %s.\n",buffer);
						}
					}
					printf("    loading....\n");
					while (fread(&code,sizeof(unsigned short),1,load_file) == 1)
					{
						hi = ((code & 0xFF00) >> 8);
						lo = (code & 0xFF);;
						send(client,&hi,1,0);
						send(client,&lo,1,0);	
					}
					printf("\n Send file %s.\n",buffer);
					fclose(load_file);
					load_file = NULL;
				}
			}
			break;
		case 0:
			printf("\n     Server lost...\n");
			goto break_while;
			break;
		default:
			printf("%c",ch);
		}
	}
break_while:
	closesocket(client);
	WSACleanup();
}

void Tconsole::work_com(int com_num)
{
	com_name[3] = '0'+com_num;
	DCB dcb;
	com = CreateFile(com_name,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
	if (com == INVALID_HANDLE_VALUE)
	{
		printf("\n   Can not open %s\n",com_name);
		return ;
	}
	GetCommState(com,&dcb);
	dcb.BaudRate = 9600;
	dcb.ByteSize = 8;
	dcb.StopBits = ONESTOPBIT;
	SetCommState(com,&dcb);
	dcb.ByteSize = 8;
	dcb.Parity = NOPARITY;
	dcb.StopBits = ONESTOPBIT;
	dcb.fBinary = TRUE;
	dcb.fParity = FALSE;
	SetCommState(com,&dcb);
	unsigned char ch;
	DWORD size;
	int len;
	load_file = NULL;
	unsigned short code;
	char hi, lo;
	printf("\n   Ok..  Connected with Simulator...\n");
	while (true)
	{
		if (kbhit())
		{
			ch = getch();
			if (ch == 7)
				goto break_loop;
			else ungetch(ch);
		}
		if (com == INVALID_HANDLE_VALUE)
		{
			printf("  COM lost...\n");
			goto break_loop;
		}
		ReadFile(com,&ch,1,&size,NULL);
		switch (size)
		{
		case 0:
			if (kbhit())
			{
				ch = getch();
				if (ch != 12)
				{
					ungetch(ch);
					while (kbhit())
					{
						ch = getch();
						if ((ch >= 21 && ch <= 125) || ch == 8 || ch == 13)
						{
							if (ch == 13)
								ch = 10;
							size = 0;
							while (size == 0) 
							{
								WriteFile(com,&ch,1,&size,NULL);
							}
							ReadFile(com,&ch,1,&size,NULL);
							if (size != 0)
								printf("%c",ch);
						}
					}
				}
				else
				{
					printf(" -- Ctrl+L --\n");
					while (load_file == NULL)
					{
						printf("FILE NAME -->>");
						len = get_input();
						buffer[len] = 0;
						if (len != 0)
						{
							load_file = fopen(buffer,"rb");
							if (load_file == NULL)
								printf("  Can not open file %s.\n",buffer);
						}
					}
					printf("    loading....\n");
					while (fread(&code,sizeof(unsigned short),1,load_file) == 1)
					{
						hi = ((code & 0xFF00) >> 8);
						lo = (code & 0xFF);
						size = 0;
						while (size == 0)
							WriteFile(com,&hi,1,&size,NULL);
						size = 0;
						while (size == 0)
							WriteFile(com,&lo,1,&size,NULL);
					}
					printf(" Send file %s.\n",buffer);
					fclose(load_file);
					load_file = NULL;
				}
			}
			break;
		case 1:
			printf("%c",ch);
			break ;
		}
	}
break_loop:
	return ;
}

int main()
{
	Tconsole console;
	return 0;
}

⌨️ 快捷键说明

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