Example #1
0
int cpu_start(Cpu *cpu){
	cpu->run = 1;
	int ret_val = 0;
	while ((NPC < cpu->prog_len) && cpu->run){
		switch (cpu->firmware[NPC]){
			case push_num:
				cpu_push_num(cpu, cpu->firmware[NPC + 1]);
				NPC_INC(2);
				break;
			case pop:
				cpu_pop(cpu, cpu->firmware[NPC + 1]);
                            	if (p_errno){
					break;
				}
				NPC_INC(2);
				break;
			case push_reg:
				cpu_push_reg(cpu, cpu->firmware[NPC + 1]);
				NPC_INC(2);
				break;
			case add:
				cpu_add(cpu);
				if (p_errno){
                                        break;
                                }
				NPC_INC(1);
				break;
			case mul:
				cpu_mul(cpu);
                                if (p_errno){
                                        break;
                                }
				NPC_INC(1);
                                break;
			case sub:
                                cpu_sub(cpu);
                                if (p_errno){
                                        break;
                                }
				NPC_INC(1);
                                break;
			case divide:
                                cpu_div(cpu);
                                if (p_errno){
                                        break;
                                }
				NPC_INC(1);
                                break;
			case tr:
				cpu_tr(cpu, cpu->firmware[NPC + 1]);
				break;
			case trip:
				cpu_trip(cpu, cpu->firmware[NPC + 1]);
				break;
			case trin:
                                cpu_trin(cpu, cpu->firmware[NPC + 1]);
                                break;
			case triz:
                                cpu_triz(cpu, cpu->firmware[NPC + 1]);
                                break;
			case gsp:
				cpu_gsp(cpu, cpu->firmware[NPC + 1]);
				NPC_INC(2);
				break;
			case ssp:
				cpu_ssp(cpu, cpu->firmware[NPC + 1]);
				NPC_INC(2);
				break;
			case syscall:
				cpu_syscall(cpu, &ret_val);
				NPC_INC(1);
				break;
			case out:
				cpu_out(cpu);
				NPC_INC(1);
				break; 
			default:
				NPC_INC(1);

		}
	if (p_errno){
		printf("\nError at %d", NPC);
		return 1;
	}
	}
	cpu->run = 0;
	return ret_val;
}
Example #2
0
int calculator(FILE* strin, FILE* strout, cpu* my_cpu)
{
	VERIFY(my_cpu != NULL);
	const char MAXLINE = 10;
	char word[MAXLINE] = {};
	const char PUSH_MAXLINE = 50;
	char push_word[PUSH_MAXLINE] = {};
	int c = 0;

	double temp = 0;
	int cond = 0;
	
	fprintf(stdout,	"# This is prototype of processor unit which calculates expressions\n"
				"# written in inverted Polish way (or something like that)\n"
				"# There are commands available:\n"
				"\n push {number}\n"
				" pop\n"
				" add\n"
				" sub\n"
				" mul\n"
				" div\n"
				" sin\n"
				" cos\n"
				" tan\n"
				" sqrt\n"
				" pow\n"
				" end\n"
				"PLEASE, USE [.] TO DIVIDE FLOAT PART\n"
				"Here you go\n");

	while (true)
	{
		cond = cpu_check(my_cpu);
		VERIFY(cond == CPU_CHECK_OK);
		
		fscanf(strin,"%s", &word);

		c = CHECK_COMMAND(word, PUSH)
			CHECK_COMMAND(word, POP)
			CHECK_COMMAND(word, ADD)
			CHECK_COMMAND(word, SUB)
			CHECK_COMMAND(word, MUL)
			CHECK_COMMAND(word, DIV)
			CHECK_COMMAND(word, SIN)
			CHECK_COMMAND(word, COS)
			CHECK_COMMAND(word, TAN)
			CHECK_COMMAND(word, SQRT)
			CHECK_COMMAND(word, POW)
			CHECK_COMMAND(word, END)
			CHECK_COMMAND(word, DUMP)
			CMD_NONE;

		switch (c)
		{
		case CMD_PUSH:
			cond = fscanf(strin, "%s", push_word);
			VERIFY(cond);
			cond = cpu_catch_error(strout, my_cpu, cpu_push(my_cpu, push_word));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			if (cond == CPU_ERROR_PUSH_BAD_TOKEN) 
			{
				fprintf(strout, "\nPUSH BAD TOKEN [%s]\n", push_word);
				break;
			}

			break;
		case CMD_POP:
			cond = cpu_catch_error(strout, my_cpu, cpu_pop(strout, my_cpu, &temp));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_ADD:
			cond = cpu_catch_error(strout, my_cpu, cpu_add(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_SUB:
			cond = cpu_catch_error(strout, my_cpu, cpu_sub(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_MUL:
			cond = cpu_catch_error(strout, my_cpu, cpu_mul(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_DIV:
			cond = cpu_catch_error(strout, my_cpu, cpu_div(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_SIN:
			cond = cpu_catch_error(strout, my_cpu, cpu_sin(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_COS:
			cond = cpu_catch_error(strout, my_cpu, cpu_cos(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_TAN:
			cond = cpu_catch_error(strout, my_cpu, cpu_tan(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_SQRT:
			cond = cpu_catch_error(strout, my_cpu, cpu_sqrt(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_POW:
			cond = cpu_catch_error(strout, my_cpu, cpu_pow(my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_DUMP:
			cond = cpu_catch_error(strout, my_cpu, cpu_dump(strout, my_cpu));
			//if (cond == CPU_ERROR_CATCHER_BAD) VERIFY(!"ERROR HAS BEEN CAUGHT");
			break;
		case CMD_END:
			return 0;
		default:
			fprintf(strout, "WRONG TOKEN [%s]\n", word);
			break;
		}
		cond = cpu_check(my_cpu);
		VERIFY(cond == CPU_CHECK_OK);
	}
}