Skip to content

Commit

Permalink
Implement vm_check_winner
Browse files Browse the repository at this point in the history
on #30
  • Loading branch information
rizky committed May 1, 2018
1 parent b9a7351 commit 945577c
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 20 deletions.
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,24 @@
"args": [
"-v",
"4",
"tests/vm/op/sti_1.cor"
"tests/vm/op/add_carry.cor"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
},
{
"preLaunchTask": "make debug",
"name": "(lldb) Launch VM -dump 2400",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/corewar",
"args": [
"-dump",
"2400",
"tests/vm/op/add_carry.cor"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: rnugroho <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/11/01 20:07:00 by rnugroho #+# #+# #
# Updated: 2018/05/01 17:06:38 by rnugroho ### ########.fr #
# Updated: 2018/05/01 17:52:39 by rnugroho ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -256,7 +256,7 @@ tests_vm_dump: corewar
@$(foreach x, $(T_VM_FILES_OP), $(MAKE) X=$x T_VM_DIR=$(T_VM_DIR_OP) test_vm_dump;)
@$(foreach x, $(T_VM_FILES_HC), $(MAKE) X=$x T_VM_DIR=$(T_VM_DIR_HC) test_vm_dump;)

NUMBERS = 1 20 50 80 150 200 600 800 1400 2400 5000
NUMBERS = 1 20 50 80 150 200 600 800 1400 2400 5000 8000 10000 50000
tests_vm_dump_loop: corewar
@$(foreach x, $(NUMBERS), $(MAKE) DUMP=$x T_VM_DIR=$(T_VM_DIR_OP) tests_vm_dump;)

Expand Down
5 changes: 4 additions & 1 deletion include/ft_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/19 21:39:11 by rnugroho #+# #+# */
/* Updated: 2018/05/01 15:14:01 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 17:35:49 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -76,6 +76,9 @@ typedef struct s_vm
t_champ champ[4];
int process_size;
int champ_size;
int last_dead_champ;
int last_live_champ;
int winner;
} t_vm;

unsigned char g_memory[MEM_SIZE];
Expand Down
10 changes: 5 additions & 5 deletions src/ft_vm/ft_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/19 21:38:33 by rnugroho #+# #+# */
/* Updated: 2018/05/01 15:46:55 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 17:59:14 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -100,15 +100,15 @@ int
vm_load_champs(&vm, g_memory);
while (g_cycles == 1 || vm_checker(&vm))
{
(vm.v_lvl[V_LVL_2]) ? ft_printfln("It is now cycle %d", g_cycles) : 0;
vm_decompiler(&vm);
(vm.dump && vm.cycles == g_cycles) ? vm_print_memory(g_memory) : 0;
(vm.v_lvl[V_LVL_0]) ? vm_print_memory_cursor(g_memory, vm) : 0;
vm_executor(&vm);
if (vm.v_lvl[V_LVL_1] && vm_start_ncurse(&start, vm) == -1)
break ;
g_cycles++;
g_cycles_to++;
}
(!vm.dump || g_cycles < vm.cycles) ?
ft_printfln("Contestant %d, \"%s\", has won !",
vm.winner + 1, vm.champ[vm.winner].header.prog_name) : 0;
(vm.v_lvl[V_LVL_1]) ? draw_end(&g_draw_win) : 0;
return (0);
}
13 changes: 6 additions & 7 deletions src/ft_vm/vm_checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/25 12:15:39 by rnugroho #+# #+# */
/* Updated: 2018/05/01 17:15:57 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 18:01:17 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -50,6 +50,7 @@ void
p = &(((t_process*)vm->champ[i].processes->data)[j]);
p->live_nbr = 0;
}
vm->champ[i].live_nbr = 0;
}
}

Expand Down Expand Up @@ -109,17 +110,12 @@ int
vm_checker(t_vm *vm)
{
int live_nbr;
int winner;

live_nbr = vm_checker_livenbr(*vm);
if (g_cycles_to == g_cycles_to_die)
{
if (vm_checker_processalive(*vm, &winner) < 1)
{
(!vm->dump) ? ft_printfln("Contestant %d, \"%s\", has won !",
winner + 1, vm->champ[winner].header.prog_name) : 0;
if (vm_checker_processalive(*vm, &(vm->winner)) < 1)
return (0);
}
g_cycles_to = 0;
if (live_nbr > NBR_LIVE || ++g_max_check == MAX_CHECKS)
{
Expand All @@ -131,6 +127,9 @@ int
vm_reset_livenbr(vm);
}
if (g_cycles_to_die < 0)
{
vm->winner = vm->last_live_champ;
return (0);
}
return (1);
}
3 changes: 1 addition & 2 deletions src/ft_vm/vm_decompiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/24 15:59:39 by rnugroho #+# #+# */
/* Updated: 2018/05/01 15:42:11 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 17:57:45 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -117,5 +117,4 @@ void
}
}
}
vm_executor(vm);
}
6 changes: 5 additions & 1 deletion src/ft_vm/vm_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/23 11:23:54 by rnugroho #+# #+# */
/* Updated: 2018/05/01 15:45:03 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 17:59:37 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,6 +43,7 @@ void
t_process *p;

i = vm->champ_size - 1;
vm_decompiler(vm);
while (i >= 0)
{
j = -1;
Expand All @@ -59,4 +60,7 @@ void
}
i--;
}
(vm->v_lvl[V_LVL_2]) ? ft_printfln("It is now cycle %d", g_cycles) : 0;
(vm->dump && vm->cycles == g_cycles) ? vm_print_memory(g_memory) : 0;
(vm->v_lvl[V_LVL_0]) ? vm_print_memory_cursor(g_memory, *vm) : 0;
}
7 changes: 6 additions & 1 deletion src/ft_vm/vm_op_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rnugroho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/26 08:27:57 by fpetras #+# #+# */
/* Updated: 2018/05/01 15:10:31 by rnugroho ### ########.fr */
/* Updated: 2018/05/01 17:28:38 by rnugroho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,6 +27,11 @@ void vm_op_live(t_vm *vm, t_process *p)
{
(void)vm;
p->live_nbr++;
if (p->op.params[0].value == (p->champ * -1))
{
vm->champ[p->champ].live_nbr++;
vm->last_live_champ = p->champ;
}
vm_op_inc(vm, p);
}

Expand Down

0 comments on commit 945577c

Please sign in to comment.