Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ SRC = src/main.c \
src/instructions/instructions_calc.c \
src/core/execute_processes.c \
src/instructions/basic_instructions.c \
src/instructions/duplication_instructions.c
src/instructions/duplication_instructions.c \
src/instructions/instructions_mem.c


LIB_SRC = lib/my/my_strcmp.c \
Expand Down
4 changes: 4 additions & 0 deletions include/corewar.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ void op_sub(global_t *global, vm_t *vm, process_t *proc);
void op_and(global_t *global, vm_t *vm, process_t *proc);
void op_or(global_t *global, vm_t *vm, process_t *proc);
void op_xor(global_t *global, vm_t *vm, process_t *proc);
int get_val(vm_t *vm, process_t *proc, int type, int *pos);
void op_ld(global_t *global, vm_t *vm, process_t *proc);
void op_st(global_t *global, vm_t *vm, process_t *proc);
void op_lld(global_t *global, vm_t *vm, process_t *proc);
void nbextremum(void);
void isextrem(int nb);
int my_put_nbr(int nb);
Expand Down
4 changes: 2 additions & 2 deletions src/core/execute_processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ static void cooldown(process_t *curr)
static void execution(global_t *global, vm_t *vm, process_t *curr)
{
void (* const actions[17])(global_t *, vm_t *, process_t *) = {
NULL, exec_live, NULL, NULL, op_add, op_sub, op_and, op_or,
op_xor, exec_zjmp, NULL, NULL, exec_fork, NULL, NULL,
NULL, exec_live, op_ld, op_st, op_add, op_sub, op_and, op_or,
op_xor, exec_zjmp, NULL, NULL, exec_fork, op_lld, NULL,
exec_lfork, exec_print
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/game_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static void delete_node(vm_t *vm)

static void handle_verification(vm_t *vm)
{
if (vm->current_cycle == vm->cycle_to_die) {
if (vm->current_cycle >= vm->cycle_to_die) {
delete_node(vm);
if (vm->live_count >= NBR_LIVE)
vm->cycle_to_die -= CYCLE_DELTA;
Expand Down
16 changes: 16 additions & 0 deletions src/instructions/basic_instructions.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ void exec_print(global_t *global, vm_t *vm, process_t *curr)
}
curr->pc = (curr->pc + 3) % MEM_SIZE;
}

void op_add(global_t *global, vm_t *vm, process_t *proc)
{
int r1 = vm->arena[(proc->pc + 2) % MEM_SIZE] - 1;
int r2 = vm->arena[(proc->pc + 3) % MEM_SIZE] - 1;
int r3 = vm->arena[(proc->pc + 4) % MEM_SIZE] - 1;

(void)global;
if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
return;
if (r3 < 0 || r3 >= REG_NUMBER)
return;
Comment on lines +76 to +79
proc->registers[r3] = proc->registers[r1] + proc->registers[r2];
proc->carry = (proc->registers[r3] == 0);
proc->pc = (proc->pc + 5) % MEM_SIZE;
}
25 changes: 6 additions & 19 deletions src/instructions/instructions_calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ static int get_val_dir(vm_t *vm, int *pos)

static int get_val_ind(vm_t *vm, process_t *proc, int *pos)
{
int addr = 0;
short addr = 0;
int target = 0;
int val = 0;

for (int i = 0; i < 2; i++)
addr = (addr << 8) | vm->arena[(*pos + i) % MEM_SIZE];
target = (proc->pc + (addr % IDX_MOD)) % MEM_SIZE;
if (proc->current_opcode == 13)
target = (proc->pc + addr) % MEM_SIZE;
else
target = (proc->pc + (addr % IDX_MOD)) % MEM_SIZE;
Comment on lines +39 to +42
if (target < 0)
target += MEM_SIZE;
for (int i = 0; i < 4; i++)
Expand All @@ -45,7 +48,7 @@ static int get_val_ind(vm_t *vm, process_t *proc, int *pos)
return val;
}

static int get_val(vm_t *vm, process_t *proc, int type, int *pos)
int get_val(vm_t *vm, process_t *proc, int type, int *pos)
{
if (type == T_REG)
return get_val_reg(vm, proc, pos);
Expand All @@ -56,22 +59,6 @@ static int get_val(vm_t *vm, process_t *proc, int type, int *pos)
return 0;
}

void op_add(global_t *global, vm_t *vm, process_t *proc)
{
int r1 = vm->arena[(proc->pc + 2) % MEM_SIZE] - 1;
int r2 = vm->arena[(proc->pc + 3) % MEM_SIZE] - 1;
int r3 = vm->arena[(proc->pc + 4) % MEM_SIZE] - 1;

(void)global;
if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
return;
if (r3 < 0 || r3 >= REG_NUMBER)
return;
proc->registers[r3] = proc->registers[r1] + proc->registers[r2];
proc->carry = (proc->registers[r3] == 0);
proc->pc = (proc->pc + 5) % MEM_SIZE;
}

void op_sub(global_t *global, vm_t *vm, process_t *proc)
{
int r1 = vm->arena[(proc->pc + 2) % MEM_SIZE] - 1;
Expand Down
80 changes: 80 additions & 0 deletions src/instructions/instructions_mem.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
** EPITECH PROJECT, 2026
** instructions_mem.c
** File description:
** corewar
*/

#include "../../include/corewar.h"

void op_ld(global_t *global, vm_t *vm, process_t *proc)
{
int types[4];
int pos = (proc->pc + 2) % MEM_SIZE;
int val;
int reg;

(void)global;
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
val = get_val(vm, proc, types[0], &pos);
reg = vm->arena[pos % MEM_SIZE] - 1;
Comment on lines +18 to +20
if (reg >= 0 && reg < REG_NUMBER) {
proc->registers[reg] = val;
proc->carry = (val == 0);
}
proc->pc = (pos + 1) % MEM_SIZE;
}

static void write_st_ind(vm_t *vm, process_t *proc, int val, int *pos)
{
short addr = 0;
int target;

for (int i = 0; i < 2; i++)
addr = (addr << 8) | vm->arena[(*pos + i) % MEM_SIZE];
*pos += 2;
target = (proc->pc + (addr % IDX_MOD)) % MEM_SIZE;
if (target < 0)
target += MEM_SIZE;
for (int i = 0; i < 4; i++)
vm->arena[(target + i) % MEM_SIZE] = (val >> (8 * (3 - i))) & 0xFF;
}

void op_st(global_t *global, vm_t *vm, process_t *proc)
{
int types[4];
int pos = (proc->pc + 2) % MEM_SIZE;
int val;
int reg;

(void)global;
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
val = get_val(vm, proc, types[0], &pos);
if (types[1] == T_REG) {
reg = vm->arena[pos % MEM_SIZE] - 1;
if (reg >= 0 && reg < REG_NUMBER)
proc->registers[reg] = val;
pos += 1;
} else if (types[1] == T_IND) {
write_st_ind(vm, proc, val, &pos);
}
proc->pc = pos % MEM_SIZE;
}

void op_lld(global_t *global, vm_t *vm, process_t *proc)
{
int types[4];
int pos = (proc->pc + 2) % MEM_SIZE;
int val;
int reg;

(void)global;
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
val = get_val(vm, proc, types[0], &pos);
reg = vm->arena[pos % MEM_SIZE] - 1;
if (reg >= 0 && reg < REG_NUMBER) {
proc->registers[reg] = val;
proc->carry = (val == 0);
}
proc->pc = (pos + 1) % MEM_SIZE;
}