示例#1
0
文件: main.c 项目: jbarbier/corewar
int main(int ac, char** av)
{
	t_vm*		vm		= vm_initialize();
	t_process*	process = (t_process*) malloc(sizeof(t_process));
	int32		i;
	t_display*		display;
	int32			update_display = 0;
	int32			was_pressed = 0;
	t_ring_buffer*	ring_buffer;

	ring_buffer = ring_buffer_initialize(10, free);

	if (load_cores(vm, ac, av) <= 0)
		return -1;

	display = display_initialize(800, 600);
	vm_set_print_callback(vm, main_debug_print, ring_buffer);

	if (1)
	{
		while (vm->process_count && !display_should_exit(display))
		{
			vm->cycle_current++;
			update_display = 1;
			int process_count = vm->process_count;
			for (i = 0; i < process_count; ++i)
			{
				t_process* process = vm->processes[i];
				if (process->cycle_wait <= 0)
				{
					update_display = 0;

					vm_reset_process_io_op(process);
					if (process->current_opcode)
						vm_execute(vm, process);
					vm_get_opcode(vm, process);
				}
				else
					process->cycle_wait--;
			}

			if (vm->cycle_current > vm->cycle_to_die)
			{
				vm->cycle_current = 0;
				vm_kill_process_if_no_live(vm);
			}

			vm_clean_dead_process(vm);
//			update_display = 0;
			if (display_update_input(display) || update_display == 0)
			{
				display_print_ring_buffer(display, 0, 0, ring_buffer);
				display_step(vm, display);
			}

		}
	}
	else
	{
		int32 execute_one = 0;

		int32 current_keys_state[GLFW_KEY_LAST];
		int32 previous_keys_state[GLFW_KEY_LAST];

		memset(previous_keys_state, 0, GLFW_KEY_LAST * sizeof(int32));
		memset(current_keys_state, 0, GLFW_KEY_LAST * sizeof(int32));
		display_step(vm, display);
		while (vm->process_count && !display_should_exit(display))
		{
			int32 executed = 0;
			int32 print_processes;
			int32 process_count = 0;

			current_keys_state[GLFW_KEY_S] = display_key_pressed(display, GLFW_KEY_S);
			current_keys_state[GLFW_KEY_P] = display_key_pressed(display, GLFW_KEY_P);

			if (!execute_one)
				execute_one = previous_keys_state[GLFW_KEY_S] && !current_keys_state[GLFW_KEY_S];
			print_processes = previous_keys_state[GLFW_KEY_P] && !current_keys_state[GLFW_KEY_P];
			memcpy(previous_keys_state, current_keys_state, sizeof(int32) * GLFW_KEY_LAST);

			if (execute_one)
				vm->cycle_current++;
			for (i = 0; i < vm->process_count; ++i)
			{
				t_process* process = vm->processes[i];
				if (print_processes)
					vm_debug_print_process(vm, process);
				if (execute_one)
				{
					if (process->cycle_wait <= 0)
					{
						vm_reset_process_io_op(process);
						vm_execute(vm, process);
						vm_get_opcode(vm, process);
						executed++;
						if (vm->live_count >= NBR_LIVE)
						{
							vm->live_count = 0;
							vm->cycle_to_die -= vm->cycle_delta;
						}
					}
					process->cycle_wait--;
				}
			}
			if (executed)
				execute_one = 0;
			if (vm->cycle_current > vm->cycle_to_die)
			{
				vm->cycle_current = 0;
				vm_kill_process_if_no_live(vm);
			}
			vm_clean_dead_process(vm);
			executed += display_update_input(display);
			if (executed)
				display_step(vm, display);
			else
				glfwPollEvents();
		}
	}


	ring_buffer_destroy(ring_buffer);
	display_destroy(display);
	vm_destroy(vm);
}
示例#2
0
int main(int ac, char** av) {
	vm_t*					vm;
	display_gl_t*	display = NULL;
	int						bound;
	int						i;
	debugger_t*		debugger = NULL;

	vm = vm_initialize();

	if ( (parse_arguments(vm, ac, av) <= 0) || (check_core_endianess(vm) < 0)) {
		return -1;
	}

	bound = VM_MEMORY_SIZE / (vm->core_count - 1);
	for (i = 0; i < vm->core_count; ++i) {
		vm->cores[i]->bound.start = vm->cores[i]->start_address;
		vm->cores[i]->bound.size = bound;
	}

	memory_access_initialize(is_cpu_big_endian() != vm->big_endian);

#if defined(_DEBUG)
		vm->full_screen = 0;
#endif

#ifdef RENDER_GL
	display = display_gl_initialize(1980, 1080, vm->full_screen);
	if (vm->step != -1) {
		debugger = debugger_init(vm->dbg_same_window ? display_gl_get_window(display) : NULL);
	}
#endif

	while (vm->process_count && !display_gl_should_exit(display)) {
		int32 i;
		int update_display = 0;


		if (vm->cycle_barrier == vm->cycle_total) {
			for (i = 0; i < vm->core_count; ++i) {
				vm->cores[i]->bound.start = 0;
				vm->cores[i]->bound.size = vm->memory_size;
			}
		}

		if (vm->step != 0) {
			vm->cycle_current++;
			vm->cycle_total++;

			int32 process_count = vm->process_count;
			for (i = process_count; i > 0; --i) {
				process_t* process = vm->processes[i - 1];
				process->cycle_wait--;
				if (process->cycle_wait <= 0) {
					vm_reset_process_io_op(process);
					vm_execute(vm, process);
					update_display = 1;
					if (vm->step > 0 && (vm->step_process == NULL || vm->step_process == process) ) {
						vm->step --;
					}
				}
			}

			if (vm->cycle_current > vm->cycle_to_die) {
				vm->cycle_current = 0;
				vm_kill_process_if_no_live(vm);
				vm_clean_dead_process(vm);
			}
		} else {
			update_display = 1;
		}

	#ifdef RENDER_GL
		update_display |= display_gl_update_input(display);
		if (update_display)
		{
			float y = 1;
			y = display_gl_text(display, 0, y, 0xffffffff, "cycle to die   %d", vm->cycle_to_die);
			y = display_gl_text(display, 0, y, 0xffffffff, "live count     %d ", vm->live_count);
			y = display_gl_text(display, 0, y, 0xffffffff, "process count  %d ", vm->process_count);
			y = display_gl_text(display, 0, y, 0xffffffff, "cycle          %d ", vm->cycle_total);
			y = display_gl_text(display, 0, y, 0xffffffff, "barrier        %d ", vm->cycle_barrier);
			for (i = 0; i < vm->core_count; ++i) {
				core_t* core = vm->cores[i];
				char* name = core->header ? core->header->name : "Unknow";
				y = display_gl_text(display, 0, y, 0xffffffff, "%s %d", name, core->live_count);
			}
			display_gl_step(vm, display);
			display_gl_swap(display);
		}

		if (debugger && vm->step != 1) {
			debugger_render(debugger, vm);
		}
	#endif
	}

	print_winning_core(vm);
	if (debugger) {
		debugger_destroy(debugger);
	}
#ifdef RENDER_GL
	display_gl_destroy(display);
#endif
	vm_destroy(vm);
}