jweinst1
1/17/2019 - 12:57 AM

old implementation of the cells vm

old implementation of the cells vm

extern unsigned
cells_run_code(unsigned char* code)
{
	int jump_c;
	unsigned char* code_start = code;
	while(*code != CELLINS_X_STOP)
	{
		switch(*code++)
		{
			case CELLINS_X_STOP:
			   return 1;
			case CELLINS_X_INC_PTR:
			   assert((++CELL_PTR) != CELL_END);
			   break;
			case CELLINS_X_DEC_PTR:
			   assert((CELL_PTR--) != CELL_BEGIN);
			   break;
			case CELLINS_X_PUT_IN:
			   CELL_PTR->_in = *(int*)(code);
			   code += sizeof(int);
			   break;
			case CELLINS_X_PUT_OUT:
			   CELL_PTR->_out = *(int*)(code);
			   code += sizeof(int);
			   break;
			case CELLINS_X_PUT_OP:
			   CELL_PTR->_op = *(int*)(code);
			   code += sizeof(int);
			   break;
			case CELLINS_X_PRINT_IN:
			   printf("%d\n", CELL_PTR->_in);
			   break;
			case CELLINS_X_PRINT_OUT:
			   printf("%d\n", CELL_PTR->_out);
			case CELLINS_X_PUSH_BACK:
			   assert(CELL_PTR != CELL_BEGIN);
			   (CELL_PTR - 1)->_in = CELL_PTR->_out;
			   break;
			case CELLINS_X_PUSH_NEXT:
			   assert((CELL_PTR + 1) != CELL_END);
			   (CELL_PTR + 1)->_in = CELL_PTR->_out;
			   break;
			case CELLINS_X_CALL_OP:
			   // not currently used.
			   break;
			case CELLINS_X_END_OP:
			   // not currently used.
			   break;
			case CELLINS_X_SUB:
			   CELL_COPY_IN_OUT(CELL_PTR);
			   CELL_PTR->_out -= *(int*)(code);
			   code += sizeof(int);
			   break;
			case CELLINS_X_PLUS:
			   CELL_COPY_IN_OUT(CELL_PTR);
			   CELL_PTR->_out += *(int*)(code);
			   code += sizeof(int);
			   break;
			case CELLINS_X_JUMP:
			   jump_c = (*(int*)(code));
			   code = code_start + jump_c;
			   break;
			default:
			   fprintf(stderr, "cells_run_code ERROR: Unkown instruction code byte %u\n", *code);
			   cells_core_dump(50);
			   exit(2);
		}
	}
	return code - code_start;
}

int main(int argc, char const *argv[])
{
	cells_clean();
	cells_core_dump(10);
	return 0;
}