This commit is contained in:
Jeremy FLEURY
2019-07-17 11:22:24 +02:00
commit 748d10f4f3
174 changed files with 8953 additions and 0 deletions

20
srcs/srcs_vm/check_args.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/22 13:34:11 by tmaze #+# #+# */
/* Updated: 2019/05/22 13:41:20 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
int check_file(char *path)
{
char *ext;
return (path && (ext = ft_strrchr(path, '.')) && ft_strequ(ext, ".cor"));
}

61
srcs/srcs_vm/cw_add_sub.c Normal file
View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_add_sub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:48:20 by tmaze #+# #+# */
/* Updated: 2019/07/10 15:12:46 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
char cw_math_common(t_vm *vm, t_process *p, t_args *ag, int *res)
{
init_args(ag, p, vm);
res[3] = cw_ocp(3, ag, 4);
if (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2)
&& ag[1].t == REG_CODE && check_reg(vm, p->pc + 3)
&& ag[2].t == REG_CODE && check_reg(vm, p->pc + 4))
{
res[0] = read_mem(vm, p->pc + 2);
res[1] = read_mem(vm, p->pc + 3);
res[2] = read_mem(vm, p->pc + 4);
return (1);
}
return (0);
}
void cw_add(t_process *p, t_vm *vm)
{
t_args ag[4];
int res[4];
if (cw_math_common(vm, p, ag, res))
{
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | add r%d r%d r%d\n", p->pid, res[0], res[1]
, res[2]);
p->regs[res[2]] = p->regs[res[0]] + p->regs[res[1]];
p->carry = (!p->regs[res[2]]) ? 1 : 0;
}
cw_move_pc(vm, p, res[3]);
}
void cw_sub(t_process *p, t_vm *vm)
{
t_args ag[4];
int res[4];
if (cw_math_common(vm, p, ag, res))
{
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | sub r%d r%d r%d\n", p->pid, res[0], res[1]
, res[2]);
p->regs[res[2]] = p->regs[res[0]] - p->regs[res[1]];
p->carry = (!p->regs[res[2]]) ? 1 : 0;
}
cw_move_pc(vm, p, res[3]);
}

35
srcs/srcs_vm/cw_aff.c Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_aff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:58:20 by tmaze #+# #+# */
/* Updated: 2019/07/10 17:23:37 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_aff(t_process *p, t_vm *vm)
{
t_args ag[4];
int jmp;
int val;
char c;
init_args(ag, p, vm);
jmp = cw_ocp(1, ag, 4);
if (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2))
{
val = p->regs[read_mem(vm, p->pc + 2)];
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | aff %d\n", p->pid, val);
c = val % 256;
p->carry = (!c) ? 1 : 0;
if (vm->args & AG_AFF)
ft_printf("Aff: %c\n", c);
}
cw_move_pc(vm, p, jmp);
}

View File

@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_and_or_xor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:52:19 by tmaze #+# #+# */
/* Updated: 2019/07/10 15:09:34 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
char cw_logic_check_args(t_vm *vm, t_process *p, t_args *ag)
{
return (((ag[0].t != 0 && ag[0].t != REG_CODE)
|| (ag[0].t == REG_CODE && check_reg(vm, p->pc + 2)))
&& ((ag[1].t != 0 && ag[1].t != REG_CODE)
|| (ag[1].t == REG_CODE && check_reg(vm, p->pc + ag[1].i)))
&& ag[1].t != 0 && ag[2].t == REG_CODE
&& check_reg(vm, p->pc + ag[2].i));
}
char cw_logic_common(t_vm *vm, t_process *p, t_args *ag, int *res)
{
init_args(ag, p, vm);
res[3] = cw_ocp(3, ag, 4);
if (cw_logic_check_args(vm, p, ag))
{
if (ag[0].t == IND_CODE)
res[0] = read_four(vm, p->pc + (read_two(vm, p->pc + 2) % IDX_MOD));
else if (ag[0].t == DIR_CODE)
res[0] = read_four(vm, p->pc + 2);
else
res[0] = p->regs[read_mem(vm, p->pc + 2)];
if (ag[1].t == IND_CODE)
res[1] = read_four(vm, p->pc + (read_two(vm, p->pc + ag[1].i)
% IDX_MOD));
else if (ag[1].t == DIR_CODE)
res[1] = read_four(vm, p->pc + ag[1].i);
else
res[1] = p->regs[read_mem(vm, p->pc + ag[1].i)];
res[2] = read_mem(vm, p->pc + ag[2].i);
return (1);
}
return (0);
}
void cw_and(t_process *p, t_vm *vm)
{
t_args ag[4];
int res[4];
if (cw_logic_common(vm, p, ag, res))
{
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | and %d %d r%d\n", p->pid, res[0], res[1]
, res[2]);
p->regs[res[2]] = res[0] & res[1];
p->carry = (!p->regs[res[2]]) ? 1 : 0;
}
cw_move_pc(vm, p, res[3]);
}
void cw_or(t_process *p, t_vm *vm)
{
t_args ag[4];
int res[4];
if (cw_logic_common(vm, p, ag, res))
{
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | or %d %d r%d\n", p->pid, res[0], res[1]
, res[2]);
p->regs[res[2]] = res[0] | res[1];
p->carry = (!p->regs[res[2]]) ? 1 : 0;
}
cw_move_pc(vm, p, res[3]);
}
void cw_xor(t_process *p, t_vm *vm)
{
t_args ag[4];
int res[4];
if (cw_logic_common(vm, p, ag, res))
{
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | xor %d %d r%d\n", p->pid, res[0], res[1]
, res[2]);
p->regs[res[2]] = res[0] ^ res[1];
p->carry = (!p->regs[res[2]]) ? 1 : 0;
}
cw_move_pc(vm, p, res[3]);
}

54
srcs/srcs_vm/cw_args.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/10 11:03:16 by tmaze #+# #+# */
/* Updated: 2019/07/12 12:30:58 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void arg_dump(char **av, t_vm *vm, int i)
{
if (!(vm->args & AG_VISUAL) && ft_isnumb(av[i + 1])
&& !ft_atois(av[i + 1], &(vm->dump)))
{
if (ft_strequ(av[i], "-dump"))
{
vm->args &= ~AG_DUMP64;
vm->args |= AG_DUMP32;
}
else if (ft_strequ(av[i], "-d"))
{
vm->args &= ~AG_DUMP32;
vm->args |= AG_DUMP64;
}
}
}
void arg_verb(char **av, t_vm *vm, int i)
{
if (!(vm->args & AG_VISUAL) && ft_isnumb(av[i + 1])
&& !ft_atois(av[i + 1], &(vm->verb)))
vm->args |= AG_VERB;
}
void arg_visual(char **av, t_vm *vm, int i)
{
av = NULL;
i = 0;
vm->args &= ~(AG_VERB | AG_DUMP32 | AG_DUMP64 | AG_AFF);
vm->args |= AG_VISUAL;
}
void arg_aff(char **av, t_vm *vm, int i)
{
av = NULL;
i = 0;
if (!(vm->args & AG_VISUAL))
vm->args |= AG_AFF;
}

48
srcs/srcs_vm/cw_error.c Normal file
View File

@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/11 16:54:36 by igarbuz #+# #+# */
/* Updated: 2019/07/13 17:21:07 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
int err_msg(int err, t_vm *vm)
{
if (err == 1)
ft_printf("Error champ %d: %s\n", vm->nb_j, strerror(errno));
else if (err == 2)
ft_printf("Error champ %d: Wrong magic number\n", vm->nb_j);
else if (err == 3)
ft_printf("Error champ %d: Champ too large (%d > %d)\n"
, vm->nb_j, vm->pls[vm->nb_j].prog_size, CHAMP_MAX_SIZE);
else if (err == 4)
ft_printf("Error champ no.%d: File is not .cor\n", vm->nb_j);
else if (err == 5)
ft_printf("Error champ no.%d: Number %d already in use\n", vm->nb_j
, vm->pls[vm->nb_j].num);
else if (err == 6)
{
ft_printf("Error dump: Missing or Invalid number\n");
return (-1);
}
else if (err == 7)
ft_printf("Error: ncurses new window initialization\n");
else if (err == 8)
ft_printf("Error: cannot open file with logo\n");
else if (err == 9)
ft_printf("Program ended by user\n");
return (1);
}
void error(t_vm *vm, int err)
{
clear_process(vm);
err_msg(err, vm);
exit(EXIT_FAILURE);
}

View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_fork_lfork.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:53:45 by tmaze #+# #+# */
/* Updated: 2019/07/10 17:57:49 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_prcs_cpy(t_vm *vm, int *arg, t_process *p, t_process *new)
{
new->op = 0;
new->pid = vm->pid_ct++;
new->new = 1;
new->carry = p->carry;
new->wait = 0;
arg[2] = -1;
while (++arg[2] <= REG_NUMBER)
new->regs[arg[2]] = p->regs[arg[2]];
new->pc = arg[1];
new->lst_live = p->lst_live;
}
void cw_fork_common(t_vm *vm, t_process *p, char *name)
{
t_process *new;
int arg[3];
if ((new = (t_process*)ft_memalloc(sizeof(t_process))) == NULL)
{
ft_printf("Error at process init: %s\n", strerror(errno));
destroy_ncurses(vm);
clear_process(vm);
exit(0);
}
arg[0] = read_two(vm, p->pc + 1);
arg[1] = p->pc + ((name[0] == 'l') ? arg[0] : (arg[0] % IDX_MOD));
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | %s %d (%d)\n", p->pid, name, arg[0], arg[1]);
arg[1] = ft_mod(arg[1], MEM_SIZE);
cw_prcs_cpy(vm, arg, p, new);
new->next = vm->tl;
vm->tl = new;
vm->p_ct++;
cw_move_pc(vm, p, 3);
}
void cw_fork(t_process *p, t_vm *vm)
{
cw_fork_common(vm, p, "fork");
}
void cw_lfork(t_process *p, t_vm *vm)
{
cw_fork_common(vm, p, "lfork");
}

View File

@@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_init_ncurs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/09 14:45:09 by igarbuz #+# #+# */
/* Updated: 2019/07/13 17:41:48 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static int print_logo(t_vm *vm)
{
int fd;
char buf[ART_MAX_SIZE + 1];
int rd;
fd = open("./art/corewar_3D_logo.txt", O_RDONLY);
if ((rd = read(fd, &buf, ART_MAX_SIZE)) == -1)
{
destroy_ncurses_exit(vm);
error(vm, 8);
}
buf[rd] = '\0';
wprintw(vm->vis.stat, "%s", &buf);
close(fd);
return (0);
}
static void init_colors(void)
{
init_color(COLOR_WHITE, 220, 220, 220);
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_YELLOW, COLOR_BLACK);
init_pair(3, COLOR_CYAN, COLOR_BLACK);
init_pair(2, COLOR_RED, COLOR_BLACK);
init_pair(5, COLOR_GREEN, COLOR_BLACK);
init_pair(6, COLOR_YELLOW, COLOR_BLACK);
init_pair(7, COLOR_BLACK, COLOR_BLUE);
init_pair(8, COLOR_BLACK, COLOR_MAGENTA);
init_pair(9, COLOR_BLACK, COLOR_CYAN);
init_pair(10, COLOR_BLACK, COLOR_RED);
init_pair(11, COLOR_BLACK, COLOR_GREEN);
init_pair(12, COLOR_BLACK, COLOR_YELLOW);
init_pair(13, COLOR_BLACK, COLOR_GREEN);
init_pair(14, COLOR_GREEN, COLOR_BLACK);
}
void init_ncurses(t_vm *vm)
{
if (vm->args & AG_VISUAL)
{
initscr();
noecho();
cbreak();
start_color();
init_colors();
curs_set(FALSE);
if ((vm->vis.arena = newwin(66, 195, 0, 0)) == 0)
error(vm, 7);
if ((vm->vis.stat = newwin(66, 100, 0, 196)) == 0)
{
delwin(vm->vis.arena);
error(vm, 7);
}
keypad(vm->vis.arena, 1);
print_logo(vm);
wborder(vm->vis.arena, '|', '|', '-', '-', '+', '+', '+', '+');
wborder(vm->vis.stat, '|', '|', '-', '-', '+', '+', '+', '+');
}
}
void destroy_ncurses_exit(t_vm *vm)
{
if (vm->args & AG_VISUAL)
{
delwin(vm->vis.arena);
delwin(vm->vis.stat);
endwin();
}
}
void destroy_ncurses(t_vm *vm)
{
if (vm->args & AG_VISUAL)
{
wrefresh(vm->vis.arena);
wrefresh(vm->vis.stat);
keypad(vm->vis.arena, 0);
nodelay(vm->vis.arena, 0);
wgetch(vm->vis.arena);
delwin(vm->vis.arena);
delwin(vm->vis.stat);
endwin();
}
}

50
srcs/srcs_vm/cw_ld_lld.c Normal file
View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_ld_lld.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:36:54 by tmaze #+# #+# */
/* Updated: 2019/07/10 15:08:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_ld_common(t_process *p, t_vm *vm, char *name)
{
int ag1;
int ag2;
int jmp;
t_args ag[4];
init_args(ag, p, vm);
jmp = cw_ocp(2, ag, 4);
if (ag[0].t == IND_CODE)
ag1 = read_two(vm, p->pc + 2);
if (ag[0].t != 0 && ag[0].t != REG_CODE && ag[1].t == REG_CODE
&& (ag2 = check_reg(vm, p->pc + ag[1].i)))
{
if (ag[0].t == DIR_CODE)
ag1 = read_four(vm, p->pc + 2);
p->carry = (ag1 == 0) ? 1 : 0;
if (ag[0].t == IND_CODE)
ag1 = read_four(vm, p->pc + ((name[1] == 'd') ? (ag1 % IDX_MOD)
: ag1));
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | %s %d r%d\n", p->pid, name, ag1, ag2);
p->regs[ag2] = ag1;
}
cw_move_pc(vm, p, jmp);
}
void cw_ld(t_process *p, t_vm *vm)
{
cw_ld_common(p, vm, "ld");
}
void cw_lld(t_process *p, t_vm *vm)
{
cw_ld_common(p, vm, "lld");
}

View File

@@ -0,0 +1,74 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_ldi_lldi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:43:31 by tmaze #+# #+# */
/* Updated: 2019/07/16 14:20:24 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void print_ldi(t_process *p, char *name, int *arg)
{
ft_printf("P %4d | %s %d %d r%d\n", \
p->pid, name, arg[0], arg[1], arg[2]);
if (name[1] == 'd')
ft_printf("%7c| -> load from %d + %d = %d (with pc and mod %d)\n"
, ' ', arg[0], arg[1], arg[4], arg[5]);
else
ft_printf("%7c| -> load from %d + %d = %d (with pc %d)\n"
, ' ', arg[0], arg[1], arg[4], arg[5]);
}
int cw_ldi_args(t_process *p, t_vm *vm, t_args *ag, int *arg)
{
return (((ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2)))
|| (ag[0].t != 0 && ag[0].t != REG_CODE))
&& ((ag[1].t == REG_CODE
&& (arg[1] = check_reg(vm, p->pc + ag[1].i)))
|| ag[1].t == DIR_CODE) && ag[2].t == REG_CODE
&& (arg[2] = check_reg(vm, p->pc + ag[2].i)));
}
void cw_ldi_common(t_process *p, t_vm *vm, char *n)
{
t_args ag[4];
int arg[6];
init_args(ag, p, vm);
arg[3] = cw_ocp(3, ag, 2);
if (cw_ldi_args(p, vm, ag, arg))
{
if (ag[0].t == DIR_CODE)
arg[0] = read_two(vm, p->pc + 2);
else if (ag[0].t == IND_CODE)
arg[0] = read_four(vm, p->pc + (read_two(vm, p->pc + 2) % IDX_MOD));
else
arg[0] = p->regs[arg[0]];
if (ag[1].t == DIR_CODE)
arg[1] = read_two(vm, p->pc + ag[1].i);
else
arg[1] = p->regs[arg[1]];
arg[4] = arg[0] + arg[1];
arg[5] = (n[1] != 'd') ? (p->pc + arg[4]) % MEM_SIZE
: (p->pc + (arg[4] % IDX_MOD)) % MEM_SIZE;
if (vm->args & AG_VERB && vm->verb & VB_OPS)
print_ldi(p, n, arg);
p->regs[arg[2]] = read_four(vm, arg[5]);
}
cw_move_pc(vm, p, arg[3]);
}
void cw_ldi(t_process *p, t_vm *vm)
{
cw_ldi_common(p, vm, "ldi");
}
void cw_lldi(t_process *p, t_vm *vm)
{
cw_ldi_common(p, vm, "lldi");
}

51
srcs/srcs_vm/cw_live.c Normal file
View File

@@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_live.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/15 12:38:01 by tmaze #+# #+# */
/* Updated: 2019/07/14 14:10:52 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static void cw_print_live(t_vm *vm, int j)
{
if (vm->args & AG_VERB && vm->verb & VB_LIVE)
ft_printf("Player %d (%s) is said to be alive\n", j + 1
, vm->pls[j].prog_name);
else if (!(vm->args & AG_VERB) && !(vm->args & AG_VISUAL))
ft_printf("un processus dit que le joueur %d(%s) est en vie\n"
, vm->pls[j].num, vm->pls[j].prog_name);
}
void cw_live(t_process *p, t_vm *vm)
{
int pl;
short int j;
unsigned short int i;
i = 0;
pl = read_four(vm, p->pc + 1);
j = 0;
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | live %d\n", p->pid, pl);
vm->live_ct++;
p->lst_live = vm->cycles;
while (j < vm->nb_j)
{
if (pl == vm->pls[j].num)
{
cw_print_live(vm, j);
vm->pls[j].live_ct++;
vm->pls[j].lst_live = vm->cycles;
vm->live = pl;
break ;
}
j++;
}
cw_move_pc(vm, p, 5);
}

View File

@@ -0,0 +1,90 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_ops_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/09 13:54:26 by tmaze #+# #+# */
/* Updated: 2019/07/15 23:52:08 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
unsigned int l2b_endian(unsigned int num)
{
uint32_t b0;
uint32_t b1;
uint32_t b2;
uint32_t b3;
uint32_t res;
b0 = (num & 0x000000ff) << 24u;
b1 = (num & 0x0000ff00) << 8u;
b2 = (num & 0x00ff0000) >> 8u;
b3 = (num & 0xff000000) >> 24u;
res = b0 | b1 | b2 | b3;
return (res);
}
void init_args(t_args *ag, t_process *p, t_vm *vm)
{
int i;
i = -1;
while (++i < 4)
{
ag[i].t = 0;
ag[i].i = 0;
}
ag[3].t = read_mem(vm, p->pc + 1);
}
int check_reg(t_vm *vm, int ind)
{
int reg;
reg = read_mem(vm, ind);
return ((reg > 0 && reg <= REG_NUMBER) ? reg : 0);
}
void cw_move_pc(t_vm *vm, t_process *p, int jp)
{
if (vm->verb != -1 && vm->verb & VB_PC)
ft_printf("ADV %d (0x%04x -> 0x%04x)", jp, p->pc, p->pc + jp);
p->pc = (p->pc + jp) % MEM_SIZE;
while (jp && vm->verb != -1 && vm->verb & VB_PC)
ft_printf(" %02x", vm->mem[((unsigned)(p->pc - jp--)) % MEM_SIZE]);
if (vm->verb != -1 && vm->verb & VB_PC)
ft_printf(" \n");
}
int cw_ocp(char nb_ag, t_args *ag, char size_dir)
{
int res;
int i;
res = 2;
i = -1;
while (++i < nb_ag)
{
ag[i].i = res;
if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == IND_CODE)
{
res += 2;
ag[i].t = IND_CODE;
}
else if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == REG_CODE)
{
res += 1;
ag[i].t = REG_CODE;
}
else if (((ag[3].t & (0xC0 >> (2 * i))) >> (2 * (3 - i))) == DIR_CODE)
{
res += size_dir;
ag[i].t = DIR_CODE;
}
}
return (res);
}

117
srcs/srcs_vm/cw_parser.c Normal file
View File

@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/20 18:20:48 by tmaze #+# #+# */
/* Updated: 2019/07/13 17:22:28 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static int p_parser(char *p, t_player *pl)
{
int fd;
if ((fd = open(p, O_RDONLY)) == -1 || read(fd, &(pl->magic), 4) == -1)
return (1);
pl->magic = l2b_endian(pl->magic);
if (pl->magic != COREWAR_EXEC_MAGIC)
return (2);
if (read(fd, &(pl->prog_name), PROG_NAME_LENGTH) < 1
|| lseek(fd, 136, SEEK_SET) == -1
|| read(fd, &(pl->prog_size), 4) < 1)
return (1);
pl->prog_size = l2b_endian(pl->prog_size);
if (pl->prog_size > CHAMP_MAX_SIZE)
return (3);
if (read(fd, &(pl->comment), COMMENT_LENGTH) < 1
|| lseek(fd, 2192, SEEK_SET) == -1
|| read(fd, &(pl->code), pl->prog_size) != pl->prog_size
|| close(fd) == -1)
return (1);
return (0);
}
static int get_num(t_vm *vm, int ac, char **av, int *i)
{
static int pl_i = -1;
short int j;
if (ft_strequ(av[*i], "-n") && (*i + 2 < ac) && ft_isnumb(av[*i + 1])
&& (j = -1) && !ft_atois(av[(*i) + 1], &(vm->pls[vm->nb_j].num)))
{
while (++j < vm->nb_j)
{
if (vm->pls[j].num == vm->pls[vm->nb_j].num)
return (1);
}
*i += 2;
}
else if (!ft_strequ(av[*i], "-n") && (j = -1))
{
while (++j < vm->nb_j)
if (pl_i == vm->pls[j].num && (j = -1))
pl_i--;
vm->pls[vm->nb_j].num = pl_i--;
}
else
return (1);
return (0);
}
static int cw_get_args(int ac, char **av, t_vm *vm)
{
int i;
int j;
static t_pargs args[5] = {{"-dump", &arg_dump, 2}, {"-d", &arg_dump, 2}
, {"-v", &arg_verb, 2}, {"-a", &arg_aff, 1}
, {"-visu", &arg_visual, 1}};
i = 1;
while (i < ac && av[i][0] == '-' && !ft_strequ("-n", av[i]) && !(j = 0))
{
while (j < 5 && !ft_strequ(args[j].arg, av[i]))
j++;
if (j < 5 && i + args[j].nb_ag < ac)
{
args[j].func(av, vm, i);
i += args[j].nb_ag;
}
else
{
cw_print_usage();
exit(1);
}
}
return (i);
}
int cw_parser(int ac, char **av, t_vm *vm)
{
int i;
int err;
i = cw_get_args(ac, av, vm);
if (i >= ac)
{
cw_print_usage();
return (1);
}
while (i < ac && vm->nb_j < 4)
{
if (get_num(vm, ac, av, &i))
return (err_msg(5, vm));
if (!check_file(av[i]))
return (err_msg(4, vm));
if ((err = p_parser(av[i], &(vm->pls[vm->nb_j]))))
return (err_msg(err, vm));
vm->live = vm->pls[vm->nb_j].num;
vm->nb_j++;
i++;
}
return (0);
}

View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_print_usage.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/11 13:30:52 by tmaze #+# #+# */
/* Updated: 2019/07/16 16:11:24 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_print_usage(void)
{
ft_putstr("Usage: ./corewar [[-dump N|-d N] -v Z -visu] [-n Z] <champion1.c"
"or> <...>\n N being a natural number\n Z being an integer\n#### "
"TEXT OUTPUT MODE #####################################################"
"####\n -a : Prints output from \"aff\" (Default is to hide i"
"t)\n -d N : Dumps memory after N cycles w/ 64 byes per line\n "
" -dump N : Dumps memory after N cycles w/ 32 bytes per line\n -"
"v N : Verbosity levels, can be added together to enable several\n"
" - 0 : Show only essentials\n - 1 : Show"
"lives\n - 2 : Show cycles\n - 4 : Show o"
"perations (Params are NOT litteral ...)\n - 8 : Show de"
"aths\n - 16 : Show PC movements (Except for jumps)\n###"
"# VISUAL OUTPUT MODE #################################################"
"######\n -visu : Activates graphical visualizer\n "
"(OVERRIDES ALL TEXT OUTPUT OPTIONS)\n#### CONTROLS\n - "
"SPACE : Pause\n - KEY_UP : Increase speed\n "
" - KEY_DOWN : Decrease speed\n - q :"
" Exit\n");
}

115
srcs/srcs_vm/cw_process.c Normal file
View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/14 12:46:19 by tmaze #+# #+# */
/* Updated: 2019/07/16 17:30:57 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void clear_process(t_vm *vm)
{
t_process *tmp;
while (vm->tl)
{
tmp = vm->tl;
vm->tl = tmp->next;
ft_memdel((void**)&tmp);
}
}
void init_process(t_vm *vm)
{
t_process *new;
short int i;
i = -1;
while (++i < vm->nb_j)
{
if ((new = (t_process*)ft_memalloc(sizeof(t_process))) == NULL)
{
ft_printf("Error at process init: %s\n", strerror(errno));
clear_process(vm);
exit(2);
}
new->op = 0;
new->pid = vm->pid_ct++;
new->new = 1;
new->carry = 0;
new->wait = 0;
ft_bzero(new->regs, (REG_NUMBER + 1) * REG_SIZE);
new->pc = (MEM_SIZE / vm->nb_j) * i;
new->regs[1] = vm->pls[i].num;
new->lst_live = 0;
new->next = vm->tl;
vm->tl = new;
vm->p_ct++;
}
}
void del_prcs_start(t_vm *vm)
{
t_process *tmp;
while (vm->tl && (vm->cycles - vm->tl->lst_live) >= vm->ctd)
{
if (vm->verb != -1 && vm->verb & VB_DEATH)
ft_printf("Process %d hasn't lived for %lu cycles (CTD %d)\n",
vm->tl->pid, vm->cycles - vm->tl->lst_live, vm->ctd);
tmp = vm->tl;
vm->tl = vm->tl->next;
ft_memdel((void**)&tmp);
vm->p_ct--;
}
}
void del_prcs_mid(t_vm *vm)
{
t_process *it;
t_process *tmp;
it = vm->tl;
while (it && it->next)
{
if ((vm->cycles - it->next->lst_live) >= vm->ctd)
{
if (vm->verb != -1 && vm->verb & VB_DEATH)
ft_printf("Process %d hasn't lived for %lu cycles (CTD %d)\n",
it->next->pid, vm->cycles - it->next->lst_live, vm->ctd);
tmp = it->next;
it->next = it->next->next;
ft_memdel((void**)&tmp);
vm->p_ct--;
}
else
it = it->next;
}
}
void del_prcs(t_vm *vm)
{
static int nb_checks = 0;
int i;
del_prcs_start(vm);
del_prcs_mid(vm);
nb_checks++;
vm->ctd_c = 0;
if (vm->live_ct >= NBR_LIVE || nb_checks == MAX_CHECKS - 1)
{
vm->ctd -= CYCLE_DELTA;
nb_checks = 0;
if (vm->verb != -1 && vm->verb & VB_CYCLES)
ft_printf("Cycle to die is now %d\n", vm->ctd);
}
vm->live_ct = 0;
i = -1;
while (++i < vm->nb_j)
vm->pls[i].live_ct = 0;
}

View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_read_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/04 14:30:46 by tmaze #+# #+# */
/* Updated: 2019/07/04 23:04:16 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
unsigned char read_mem(t_vm *vm, int ind)
{
return (vm->mem[ft_mod(ind, MEM_SIZE)]);
}
int read_four(t_vm *vm, size_t index)
{
int i;
int ret;
i = -1;
while (++i < 4)
ret = ret << 8 | read_mem(vm, index + i);
return (ret);
}
short int read_two(t_vm *vm, size_t index)
{
int i;
short int ret;
i = -1;
ret = 0;
while (++i < 2)
ret = ret << 8 | read_mem(vm, index + i);
return (ret);
}

42
srcs/srcs_vm/cw_st.c Normal file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_st.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:40:53 by tmaze #+# #+# */
/* Updated: 2019/06/27 11:07:38 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
char cw_st_check_args(t_vm *vm, t_process *p, t_args *ag, int *arg)
{
return (ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2))
&& ((ag[1].t == REG_CODE && (arg[1] = check_reg(vm, p->pc + 3)))
|| ag[1].t == IND_CODE));
}
void cw_st(t_process *p, t_vm *vm)
{
int arg[3];
t_args ag[4];
init_args(ag, p, vm);
arg[2] = cw_ocp(2, ag, 4);
arg[0] = 0;
if (cw_st_check_args(vm, p, ag, arg))
{
if (ag[1].t == IND_CODE)
arg[1] = read_two(vm, p->pc + 3);
if (vm->verb != -1 && vm->verb & VB_OPS)
ft_printf("P %4d | st r%d %d\n", p->pid, arg[0], arg[1]);
if (ag[1].t == REG_CODE)
p->regs[arg[1]] = p->regs[arg[0]];
else
write_four(vm, p->pc + (arg[1] % IDX_MOD), p->regs[arg[0]], p->pc);
}
cw_move_pc(vm, p, arg[2]);
}

57
srcs/srcs_vm/cw_sti.c Normal file
View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_sti.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:46:59 by tmaze #+# #+# */
/* Updated: 2019/07/15 23:59:22 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static int cw_check_sti(t_process *p, t_vm *vm, t_args *ag, int *arg)
{
return (ag[0].t == REG_CODE && (arg[0] = check_reg(vm, p->pc + 2))
&& ((ag[1].t == REG_CODE && (arg[1] = check_reg(vm, p->pc + ag[1].i)))
|| (ag[1].t != 0 && ag[1].t != REG_CODE))
&& ((ag[2].t == REG_CODE && (arg[2] = check_reg(vm, p->pc + ag[2].i)))
|| ag[2].t == DIR_CODE));
}
static void cw_print_sti(t_process *p, int *arg)
{
ft_printf("P %4d | sti r%d %d %d\n", p->pid, arg[0], arg[1], arg[2]);
ft_printf("%7c| -> store to %d + %d = %d (with pc and mod %d)\n", ' '
, arg[1], arg[2], arg[4], arg[5]);
}
void cw_sti(t_process *p, t_vm *vm)
{
t_args ag[4];
int arg[6];
init_args(ag, p, vm);
arg[3] = cw_ocp(3, ag, 2);
if (cw_check_sti(p, vm, ag, arg))
{
if (ag[1].t == REG_CODE)
arg[1] = p->regs[arg[1]];
else if (ag[1].t == IND_CODE)
arg[1] = read_four(vm, p->pc + (read_two(vm, p->pc + 3) % IDX_MOD));
else
arg[1] = read_two(vm, p->pc + 3);
if (ag[2].t == DIR_CODE)
arg[2] = read_two(vm, p->pc + ag[2].i);
else
arg[2] = p->regs[arg[2]];
arg[4] = arg[1] + arg[2];
arg[5] = p->pc + (arg[4] % IDX_MOD);
if (vm->args & AG_VERB && vm->verb & VB_OPS)
cw_print_sti(p, arg);
write_four(vm, ft_mod(arg[5], MEM_SIZE), p->regs[arg[0]], p->pc);
}
cw_move_pc(vm, p, arg[3]);
}

122
srcs/srcs_vm/cw_visual.c Normal file
View File

@@ -0,0 +1,122 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_visual.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/08 12:12:51 by igarbuz #+# #+# */
/* Updated: 2019/07/13 19:14:56 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static void print_lives(t_player *pl, WINDOW *stat)
{
int i;
int cycles;
if (pl->live_ct < 560)
cycles = pl->live_ct / 10;
else
cycles = 56;
wattron(stat, COLOR_PAIR(pl->col));
wprintw(stat, "\n\n Lives for: ");
wprintw(stat, "%s", pl->prog_name);
wprintw(stat, " => %-5d \n", pl->live_ct);
waddstr(stat, " |");
i = -1;
while (++i < cycles)
waddch(stat, ACS_HLINE | A_REVERSE);
while (i++ < 56)
waddch(stat, ACS_HLINE);
waddch(stat, '|');
wattroff(stat, COLOR_PAIR(pl->col));
}
static void print_stats(t_vm *vm, WINDOW *stat)
{
short int i;
wmove(stat, 10, 1);
wclrtobot(stat);
i = -1;
while (++i < vm->nb_j)
print_lives(&vm->pls[i], stat);
wattron(stat, COLOR_PAIR(14));
wprintw(stat, "\n\n Cycle: %-10lld\n\n \
Processes: %-10u\n\n \
Total Number of lives: %3d/%-10d\n\n \
Players number: %hd/%d\n\n \
Decrease cycle to die with: %d\n\n \
Cycles to die: %d/%d\n\n",
vm->cycles, vm->p_ct, vm->live_ct, NBR_LIVE, \
vm->nb_j, MAX_PLAYERS, CYCLE_DELTA, vm->ctd_c, vm->ctd);
wprintw(stat, "\n\n Delay: %8dμs ", vm->vis.delay & 0x3FFFFF);
wattroff(stat, COLOR_PAIR(14));
wborder(stat, '|', '|', '-', '-', '+', '+', '+', '+');
wrefresh(stat);
}
void control_delay(t_vm *vm)
{
int key;
while (vm->args & AG_VISUAL)
{
key = wgetch(vm->vis.arena);
if (key == ' ' && vm->vis.paused == 0)
{
nodelay(vm->vis.arena, 0);
vm->vis.paused = 1;
}
else if (key == ' ' && vm->vis.paused == 1)
{
nodelay(vm->vis.arena, 1);
vm->vis.paused = 0;
}
else if (key == KEY_DOWN && vm->vis.delay < 100000)
vm->vis.delay = (vm->vis.delay + 1) * 1.1;
else if (key == KEY_UP)
vm->vis.delay = (vm->vis.delay * 0.9);
check_user_exit(vm, key);
if (vm->vis.paused == 1)
visual_delay_update(vm);
if (vm->vis.paused == 0)
break ;
}
}
void print_arena(t_vm *vm)
{
int i;
if (vm->args & AG_VISUAL)
{
i = 0;
werase(vm->vis.arena);
while (i < MEM_SIZE)
{
if ((i & 0x3F) == 0)
wprintw(vm->vis.arena, "\n ");
wattron(vm->vis.arena, COLOR_PAIR(vm->col[i]));
wprintw(vm->vis.arena, " %02x", 0xFF & vm->mem[i]);
wattroff(vm->vis.arena, COLOR_PAIR(vm->col[i]));
wattroff(vm->vis.arena, COLOR_PAIR(1));
i++;
}
wborder(vm->vis.arena, '|', '|', '-', '-', '+', '+', '+', '+');
print_stats(vm, vm->vis.stat);
}
}
void print_pc(t_vm *vm, unsigned short int pc, char col)
{
if (vm->args & AG_VISUAL)
{
mvwchgat(vm->vis.arena, 1 + pc / 64, 2 + 3 * (pc & 63),\
2, A_REVERSE, col, NULL);
usleep(vm->vis.delay);
}
}

View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_visual_win.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/13 21:15:56 by igarbuz #+# #+# */
/* Updated: 2019/07/16 19:09:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static void print_logo_win(t_vm *vm, int i)
{
int j;
wmove(vm->vis.stat, 0, 0);
j = 0;
while (++j < 9)
mvwchgat(vm->vis.stat, j, 1, 93, A_BLINK, vm->pls[i].col, NULL);
}
void visual_delay_update(t_vm *vm)
{
wmove(vm->vis.stat, (vm->nb_j - 1) * 3 + 29, 15);
wattron(vm->vis.stat, COLOR_PAIR(14));
wprintw(vm->vis.stat, "%8dμs ", vm->vis.delay & 0x3FFFFF);
wattroff(vm->vis.stat, COLOR_PAIR(14));
wrefresh(vm->vis.stat);
}
void check_user_exit(t_vm *vm, int key)
{
if (key == 'q')
{
destroy_ncurses_exit(vm);
error(vm, 9);
}
}
void visual_winners(t_vm *vm, int i)
{
int k;
wmove(vm->vis.stat, 40, 5);
wclrtobot(vm->vis.stat);
wattron(vm->vis.stat, COLOR_PAIR(14));
k = -1;
while (++k < 90)
waddch(vm->vis.stat, '*');
wprintw(vm->vis.stat, "\n\n Contestant %lu, \"%s\", has won !\n", \
i + 1, vm->pls[i].prog_name);
wprintw(vm->vis.stat, "\n\n Press any key to exit\n");
wattroff(vm->vis.stat, COLOR_PAIR(14));
print_logo_win(vm, i);
wborder(vm->vis.stat, '|', '|', '-', '-', '+', '+', '+', '+');
wrefresh(vm->vis.stat);
}

91
srcs/srcs_vm/cw_vm.c Normal file
View File

@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_vm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/14 12:52:51 by tmaze #+# #+# */
/* Updated: 2019/07/16 14:22:34 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
static void init_vm_vars(t_vm *vm)
{
g_bcn = 0;
g_buf[g_bcn] = '\0';
vm->args = 0;
vm->pid_ct = 1;
vm->cycles = 0;
vm->ctd = CYCLE_TO_DIE;
vm->ctd_c = 0;
vm->nb_j = 0;
vm->tl = NULL;
vm->live = 0;
vm->live_ct = 0;
vm->dump = -1;
vm->verb = -1;
vm->p_ct = 0;
vm->vis.delay = 10000;
vm->vis.paused = TRUE;
vm->vis.arena = NULL;
vm->vis.stat = NULL;
}
void init_vm(t_vm *vm, int i)
{
ft_bzero(vm->mem, MEM_SIZE);
ft_bzero(vm->col, MEM_SIZE);
init_vm_vars(vm);
i = -1;
while (++i < MAX_PLAYERS)
{
vm->pls[i].magic = 0;
ft_bzero(vm->pls[i].prog_name, PROG_NAME_LENGTH + 1);
vm->pls[i].prog_size = 0;
ft_bzero(vm->pls[i].comment, COMMENT_LENGTH + 1);
vm->pls[i].num = 0;
ft_bzero(vm->pls[i].code, CHAMP_MAX_SIZE);
vm->pls[i].col = 0;
vm->pls[i].live_ct = 0;
vm->pls[i].lst_live = 0;
}
}
int load_champ(t_vm *vm)
{
short int i;
int inc;
inc = 0;
i = 0;
while (i < vm->nb_j)
{
ft_memmove(&(vm->mem[inc]), vm->pls[i].code, vm->pls[i].prog_size);
ft_memset(&(vm->col[inc]), i + 1, vm->pls[i].prog_size);
vm->pls[i].col = i + 1;
inc += (MEM_SIZE / vm->nb_j);
i++;
}
return (0);
}
void mem_dump(t_vm *vm)
{
int i;
int nb;
i = 0;
nb = (vm->args & AG_DUMP64) ? 64 : 32;
while (i < MEM_SIZE)
{
if (i % nb == 0)
ft_printf("0x%04x : ", i);
ft_printf("%02x ", vm->mem[i]);
if ((i + 1) % nb == 0)
ft_printf("\n");
i++;
}
}

View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_write_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/14 11:31:10 by tmaze #+# #+# */
/* Updated: 2019/07/13 17:17:08 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void write_mem(t_vm *vm, unsigned int index, char val, unsigned short int pc)
{
vm->mem[ft_mod(index, MEM_SIZE)] = val;
vm->col[index & MEM_SIZE_M] = vm->col[pc & MEM_SIZE_M];
}
void write_four(t_vm *vm, unsigned int index, int val, unsigned short int pc)
{
int mask;
int i;
mask = 0x000000FF;
i = 0;
while (i < 4)
{
write_mem(vm, (index + (3 - i)), (val & mask) >> (8 * i), pc);
mask = mask << 8;
i++;
}
}

27
srcs/srcs_vm/cw_zjump.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cw_zjump.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/19 20:57:44 by tmaze #+# #+# */
/* Updated: 2019/07/10 15:05:17 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_zjump(t_process *p, t_vm *vm)
{
int ind;
ind = read_two(vm, p->pc + 1);
if (vm->args & AG_VERB && vm->verb & VB_OPS)
ft_printf("P %4d | zjmp %d %s\n", p->pid, ind
, (p->carry) ? "OK" : "FAILED");
if (p->carry)
p->pc = ft_mod(p->pc + (read_two(vm, p->pc + 1) % IDX_MOD), MEM_SIZE);
else
cw_move_pc(vm, p, 3);
}

117
srcs/srcs_vm/main.c Normal file
View File

@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/16 18:54:57 by tmaze #+# #+# */
/* Updated: 2019/07/16 17:44:18 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "corewar.h"
void cw_intro(t_vm *vm)
{
short int i;
ft_printf("Introducing contestants...\n");
i = 0;
while (i < vm->nb_j)
{
ft_printf("* Player %hd, weighing %d bytes, \"%s\" (\"%s\") !\n"
, i + 1, vm->pls[i].prog_size, vm->pls[i].prog_name
, vm->pls[i].comment);
i++;
}
}
static void corewar_run(t_vm *vm, t_process *it)
{
static t_ops ops[16] = {{10, &cw_live}, {5, &cw_ld}, {5, &cw_st}
, {10, &cw_add}, {10, &cw_sub}, {6, &cw_and}, {6, &cw_or}
, {6, &cw_xor}, {20, &cw_zjump}, {25, &cw_ldi}
, {25, &cw_sti}, {800, &cw_fork}, {10, &cw_lld}
, {50, &cw_lldi}, {1000, &cw_lfork}, {2, &cw_aff}};
while (it)
{
print_pc(vm, it->pc, vm->col[it->pc]);
if (it->wait)
{
it->wait--;
if (it->wait == 0)
ops[it->op - 1].func(it, vm);
}
else if (vm->mem[it->pc] > 0 && vm->mem[it->pc] < 17)
{
it->op = vm->mem[it->pc];
it->wait = ops[it->op - 1].wait - 1;
}
else
it->pc = (it->pc + 1) % MEM_SIZE;
if (it)
it = it->next;
}
}
static void corewar(t_vm *vm)
{
init_ncurses(vm);
while (vm->tl && (!(vm->args & (AG_DUMP32 | AG_DUMP64))
|| vm->cycles < vm->dump))
{
print_arena(vm);
vm->cycles++;
vm->ctd_c++;
if (vm->args & AG_VERB && vm->verb & VB_CYCLES)
ft_printf("It is now cycle %d\n", vm->cycles);
corewar_run(vm, vm->tl);
wrefresh(vm->vis.arena);
if (vm->ctd_c == vm->ctd || vm->ctd <= 0)
del_prcs(vm);
control_delay(vm);
}
if (!vm->tl)
vm->args &= ~(AG_DUMP32 & AG_DUMP64);
}
static void cw_print_win(t_vm *vm, int i)
{
if (vm->args & AG_VISUAL)
visual_winners(vm, i);
else if (vm->args & AG_VERB)
ft_printf("Contestant %d, \"%s\", has won !\n", i + 1
, vm->pls[i].prog_name);
else
ft_printf("le joueur %d(%s) a gagne\n", vm->pls[i].num
, vm->pls[i].prog_name);
}
int main(int ac, char **av)
{
t_vm vm;
short int i;
init_vm(&vm, 0);
if (cw_parser(ac, av, &vm))
return (1);
init_process(&vm);
load_champ(&vm);
if (vm.args & AG_VERB)
cw_intro(&vm);
corewar(&vm);
if (vm.args & AG_DUMP32 || vm.args & AG_DUMP64)
mem_dump(&vm);
else
{
i = 0;
while (i < vm.nb_j && vm.pls[i].num != vm.live)
i++;
cw_print_win(&vm, i);
}
destroy_ncurses(&vm);
clear_process(&vm);
return (0);
}