push
This commit is contained in:
10
srcs/art/corewar_3D_logo.txt
Normal file
10
srcs/art/corewar_3D_logo.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
________ ________ ________ _______ ___ __ ________ ________
|
||||
|\ ____\|\ __ \|\ __ \|\ ___ \ |\ \ |\ \|\ __ \|\ __ \
|
||||
\ \ \___|\ \ \|\ \ \ \|\ \ \ __/| \ \ \ \ \ \ \ \|\ \ \ \|\ \
|
||||
\ \ \ \ \ \\\ \ \ _ _\ \ \_|/__ \ \ \ __\ \ \ \ __ \ \ _ _\
|
||||
\ \ \____\ \ \\\ \ \ \\ \\ \ \_|\ \ \ \ \|\__\_\ \ \ \ \ \ \ \\ \|
|
||||
\ \_______\ \_______\ \__\\ _\\ \_______\ \ \____________\ \__\ \__\ \__\\ _\
|
||||
\|_______|\|_______|\|__|\|__|\|_______| \|____________|\|__|\|__|\|__|\|__|
|
||||
|
||||
***************************************************************************************************
|
140
srcs/srcs_asm/convert/conv_instru.c
Normal file
140
srcs/srcs_asm/convert/conv_instru.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* conv_instru.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/09 11:56:37 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 12:44:33 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_calc_byte(t_token *tk)
|
||||
{
|
||||
char *str_size;
|
||||
int size;
|
||||
char *complete;
|
||||
|
||||
str_size = ft_strnew(0);
|
||||
while (tk)
|
||||
{
|
||||
if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
tk = tk->next;
|
||||
if (tk->type == DIRECT || tk->type == DIRECT_LABEL)
|
||||
str_size = ft_strextend(str_size, "10");
|
||||
else if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL)
|
||||
str_size = ft_strextend(str_size, "11");
|
||||
else if (tk->type == REGISTER)
|
||||
str_size = ft_strextend(str_size, "01");
|
||||
tk = tk->next;
|
||||
}
|
||||
complete = ft_strnew_1(8 - ft_strlen(str_size));
|
||||
str_size = ft_strextend(str_size, complete);
|
||||
size = ft_atoi_bin(str_size);
|
||||
ft_strdel(&complete);
|
||||
ft_strdel(&str_size);
|
||||
return (size);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_lab(t_token *tk, int inst,
|
||||
int *size_param, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *param;
|
||||
|
||||
param = NULL;
|
||||
if (tk->type == DIRECT_LABEL)
|
||||
{
|
||||
*size_param = (g_tab[inst].size) ? 2 : 4;
|
||||
param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place
|
||||
- pos_lab.pos, *size_param);
|
||||
}
|
||||
else if (tk->type == INDIRECT_LABEL)
|
||||
{
|
||||
*size_param = 2;
|
||||
param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place
|
||||
- pos_lab.pos, *size_param);
|
||||
}
|
||||
return (param);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_one(t_token *tk, int inst,
|
||||
int *size_param, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *param;
|
||||
|
||||
*size_param = 0;
|
||||
param = NULL;
|
||||
if (tk->type == DIRECT)
|
||||
{
|
||||
*size_param = (g_tab[inst].size) ? 2 : 4;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param);
|
||||
}
|
||||
else if (tk->type == INDIRECT)
|
||||
{
|
||||
*size_param = 2;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents), *size_param);
|
||||
}
|
||||
else if (tk->type == REGISTER)
|
||||
{
|
||||
*size_param = 1;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param);
|
||||
}
|
||||
else
|
||||
param = ft_convert_lab(tk, inst, size_param, pos_lab);
|
||||
return (param);
|
||||
}
|
||||
|
||||
unsigned char *ft_str_params(t_token *tk, int inst,
|
||||
int *size_params, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *params;
|
||||
unsigned char *one;
|
||||
int size_one;
|
||||
|
||||
*size_params = 0;
|
||||
if (!(params = (unsigned char *)malloc(sizeof(*params))))
|
||||
return (NULL);
|
||||
while (tk)
|
||||
{
|
||||
if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
tk = tk->next;
|
||||
else if (tk->type == COMMENT)
|
||||
break ;
|
||||
one = ft_convert_one(tk, inst, &size_one, pos_lab);
|
||||
params = ft_strextend_nm(params, one, *size_params, size_one);
|
||||
tk = tk->next;
|
||||
*size_params += size_one;
|
||||
}
|
||||
return (params);
|
||||
}
|
||||
|
||||
unsigned char *ft_conv_instru(t_token *tk, int *size_instru,
|
||||
t_pos_lab pos_lab)
|
||||
{
|
||||
int inst;
|
||||
unsigned char *instru;
|
||||
unsigned char *params;
|
||||
int size_params;
|
||||
|
||||
inst = is_inst(tk->contents);
|
||||
if (!(instru = (unsigned char *)malloc(sizeof(*instru)
|
||||
* (1 + g_tab[inst].byte))))
|
||||
return (NULL);
|
||||
instru[0] = g_tab[inst].op_code;
|
||||
params = ft_str_params(tk->next, inst, &size_params, pos_lab);
|
||||
if (g_tab[inst].byte)
|
||||
{
|
||||
instru[1] = ft_calc_byte(tk);
|
||||
instru = ft_strextend_nm(instru, params, 2, size_params);
|
||||
*size_instru = 2 + size_params;
|
||||
}
|
||||
else
|
||||
{
|
||||
instru = ft_strextend_nm(instru, params, 1, size_params);
|
||||
*size_instru = 1 + size_params;
|
||||
}
|
||||
return (instru);
|
||||
}
|
36
srcs/srcs_asm/convert/conv_utils.c
Normal file
36
srcs/srcs_asm/convert/conv_utils.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* conv_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/11 12:17:14 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/11 13:28:14 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_label *get_label(t_token *tk, t_label *label)
|
||||
{
|
||||
char *name;
|
||||
|
||||
if (tk->type == INDIRECT_LABEL)
|
||||
name = ft_strdup(tk->contents + 1);
|
||||
else if (tk->type == DIRECT_LABEL)
|
||||
name = ft_strdup(tk->contents + 2);
|
||||
else
|
||||
return (NULL);
|
||||
while (label)
|
||||
{
|
||||
if (ft_strequ(label->contents, name))
|
||||
{
|
||||
ft_strdel(&name);
|
||||
return (label);
|
||||
}
|
||||
label = label->next;
|
||||
}
|
||||
ft_strdel(&name);
|
||||
return (NULL);
|
||||
}
|
101
srcs/srcs_asm/convert/convert.c
Normal file
101
srcs/srcs_asm/convert/convert.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* convert.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/08 11:56:22 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 12:45:37 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_create_file(char **argv, unsigned char *buffer, int len)
|
||||
{
|
||||
char *str;
|
||||
int len_argv;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
len_argv = ft_strlen(argv[1]);
|
||||
if (!(str = malloc(sizeof(char) * len_argv - 1)))
|
||||
return (0);
|
||||
str[len_argv - 2] = 0;
|
||||
i = 0;
|
||||
while (argv[1][i] != '.')
|
||||
{
|
||||
str[i] = argv[1][i];
|
||||
i++;
|
||||
}
|
||||
str = ft_strextend(str, ".cor");
|
||||
ft_printf("Writing output program to %s\n", str);
|
||||
fd = open(str, O_CREAT, S_IRWXU, O_RDWR);
|
||||
close(fd);
|
||||
fd = open(str, O_RDWR);
|
||||
free(str);
|
||||
write(fd, buffer, len);
|
||||
close(fd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_head(t_header *head)
|
||||
{
|
||||
unsigned char *buffer;
|
||||
unsigned char *conv_progsize;
|
||||
|
||||
if (!(buffer = (unsigned char *)ft_strnew(sizeof(*buffer) * 2192)))
|
||||
return (NULL);
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 234;
|
||||
buffer[2] = 131;
|
||||
buffer[3] = 243;
|
||||
ft_strcpy_n(buffer, (unsigned char *)head->prog_name, 4, 128);
|
||||
conv_progsize = ft_conv_hexa(head->prog_size, 4);
|
||||
ft_strcpy_n(buffer, conv_progsize, 136, 4);
|
||||
ft_strcpy_n(buffer, (unsigned char *)head->comment, 140, 2048);
|
||||
ft_strdel((char **)&conv_progsize);
|
||||
return (buffer);
|
||||
}
|
||||
|
||||
void init_convert(unsigned char **buffer, t_header *head, int *pos)
|
||||
{
|
||||
unsigned char *conv_head;
|
||||
|
||||
*buffer = (unsigned char *)ft_strnew(2192 + head->prog_size);
|
||||
conv_head = ft_convert_head(head);
|
||||
*buffer = ft_strcpy_n(*buffer, conv_head, 0, 2192);
|
||||
*pos = 2192;
|
||||
ft_strdel((char **)&conv_head);
|
||||
}
|
||||
|
||||
void ft_convert(t_lst_tk *lst, t_header *head, char **argv,
|
||||
t_label *lab)
|
||||
{
|
||||
unsigned char *buffer;
|
||||
int pos;
|
||||
int size_instru;
|
||||
t_pos_lab pos_lab;
|
||||
|
||||
init_convert(&buffer, head, &pos);
|
||||
pos_lab.lab = lab;
|
||||
while (lst)
|
||||
{
|
||||
pos_lab.pos = pos - 2192;
|
||||
size_instru = 0;
|
||||
if (lst->token->type == INSTRUCTION)
|
||||
buffer = ft_strextend_nm(buffer, ft_conv_instru(lst->token,
|
||||
&size_instru, pos_lab), pos, size_instru);
|
||||
else if (lst->token->type == LABEL)
|
||||
{
|
||||
if (lst->token->next && lst->token->next->type != COMMENT)
|
||||
buffer = ft_strextend_nm(buffer, ft_conv_instru(
|
||||
lst->token->next, &size_instru, pos_lab), pos, size_instru);
|
||||
}
|
||||
lst = lst->next;
|
||||
pos += size_instru;
|
||||
}
|
||||
ft_create_file(argv, buffer, 2192 + head->prog_size);
|
||||
ft_strdel((char **)&buffer);
|
||||
}
|
65
srcs/srcs_asm/lexer/ft_automaton.c
Normal file
65
srcs/srcs_asm/lexer/ft_automaton.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_automaton.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 14:48:26 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 14:30:23 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_cal_i_string(int i_matrice, char *line, int *nb_char_token)
|
||||
{
|
||||
int i_string;
|
||||
|
||||
i_string = 0;
|
||||
if (line[*nb_char_token] == 0
|
||||
|| (i_matrice == 21 && line[*nb_char_token] != '\n'))
|
||||
i_string = 16;
|
||||
else
|
||||
while (!(ft_line_strchr(g_string_automaton[i_string],
|
||||
line[*nb_char_token])) && i_string < 15)
|
||||
i_string++;
|
||||
return (i_string);
|
||||
}
|
||||
|
||||
static int ft_end_error(int *nb_char_token, char *line)
|
||||
{
|
||||
g_nb_char = g_nb_char + *nb_char_token;
|
||||
ft_printf("error: syntax %d:%d char: %c\n",
|
||||
g_nb_line, g_nb_char + 1, line[*nb_char_token]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_automaton(char *line, t_token *token, t_label *label,
|
||||
int *nb_char_token)
|
||||
{
|
||||
int i_matrice;
|
||||
int i_string;
|
||||
int type;
|
||||
|
||||
i_matrice = 0;
|
||||
while (1)
|
||||
{
|
||||
type = i_matrice;
|
||||
i_string = ft_cal_i_string(i_matrice, line, nb_char_token);
|
||||
i_matrice = g_matrice_automaton[i_matrice][i_string];
|
||||
if (i_matrice == -2 || i_matrice == -3)
|
||||
{
|
||||
if (i_matrice == -3)
|
||||
*nb_char_token = *nb_char_token - 1;
|
||||
g_nb_char = g_nb_char + *nb_char_token;
|
||||
ft_create_token_label(ft_create_string(line, *nb_char_token),
|
||||
type, token, label);
|
||||
return (1);
|
||||
}
|
||||
else if (i_matrice == -1)
|
||||
return (ft_end_error(nb_char_token, line));
|
||||
else
|
||||
*nb_char_token = *nb_char_token + 1;
|
||||
}
|
||||
}
|
95
srcs/srcs_asm/lexer/ft_lexer.c
Normal file
95
srcs/srcs_asm/lexer/ft_lexer.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lexer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/30 10:48:23 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 16:20:57 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_free_line(char **line, char **tmp)
|
||||
{
|
||||
free(*line);
|
||||
free(*tmp);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void ft_free_error(t_token *token, t_label *label,
|
||||
t_token *list_token)
|
||||
{
|
||||
t_token *tmp;
|
||||
|
||||
free(token);
|
||||
free(label);
|
||||
if (list_token != NULL)
|
||||
{
|
||||
while (list_token != NULL)
|
||||
{
|
||||
tmp = list_token->next;
|
||||
free(list_token->contents);
|
||||
free(list_token);
|
||||
list_token = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t_token *ft_lexer_token(char **line, t_label **list_label)
|
||||
{
|
||||
t_token *token;
|
||||
t_label *label;
|
||||
t_token *list_token;
|
||||
int nb_char_token;
|
||||
|
||||
list_token = NULL;
|
||||
while ((*line)[0] != '\n' && (*line)[0] != 0)
|
||||
{
|
||||
if (!(token = malloc(sizeof(t_token))))
|
||||
return (NULL);
|
||||
if (!(label = malloc(sizeof(t_label))))
|
||||
return (NULL);
|
||||
nb_char_token = 0;
|
||||
if (!(ft_automaton(*line, token, label, &nb_char_token)))
|
||||
{
|
||||
ft_free_error(token, label, list_token);
|
||||
return (NULL);
|
||||
}
|
||||
ft_append_token_label(token, &list_token, label, list_label);
|
||||
if (!(line = ft_cut_line(line, nb_char_token)))
|
||||
break ;
|
||||
}
|
||||
return (list_token);
|
||||
}
|
||||
|
||||
int ft_lexer(t_lst_tk **list, t_label **list_label, char **argv)
|
||||
{
|
||||
char *line;
|
||||
int fd;
|
||||
static char *tmp;
|
||||
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
line = NULL;
|
||||
while (get_next_line(fd, &line, &tmp) == 1)
|
||||
{
|
||||
g_nb_char = 0;
|
||||
g_nb_line++;
|
||||
if (!(line = ft_check_line(line, fd)))
|
||||
return (ft_free_line(&line, &tmp));
|
||||
if (!(ft_empty_line(line)))
|
||||
{
|
||||
free(line);
|
||||
continue ;
|
||||
}
|
||||
if (!(ft_append_lst_tk(ft_lexer_token(&line, list_label), list)))
|
||||
return (ft_free_line(&line, &tmp));
|
||||
free(line);
|
||||
}
|
||||
if (line != NULL)
|
||||
free(line);
|
||||
free(tmp);
|
||||
return (1);
|
||||
}
|
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c
Normal file
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_append_lst_tk.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 11:38:38 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/01 11:22:10 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_lst_tk *ft_create_lst_tk(t_token *token)
|
||||
{
|
||||
t_lst_tk *new;
|
||||
|
||||
if (token == NULL)
|
||||
return (NULL);
|
||||
if (!(new = (t_lst_tk*)malloc(sizeof(t_lst_tk))))
|
||||
return (NULL);
|
||||
new->token = token;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
int ft_append_lst_tk(t_token *token, t_lst_tk **list)
|
||||
{
|
||||
t_lst_tk *begin;
|
||||
|
||||
begin = *list;
|
||||
if (token == NULL)
|
||||
return (0);
|
||||
if (*list == NULL)
|
||||
{
|
||||
if (!(*list = ft_create_lst_tk(token)))
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((*list)->next != NULL)
|
||||
*list = (*list)->next;
|
||||
if (!((*list)->next = ft_create_lst_tk(token)))
|
||||
return (1);
|
||||
*list = begin;
|
||||
}
|
||||
return (1);
|
||||
}
|
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c
Normal file
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_append_token_label.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:42:16 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 12:56:53 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_append_label(t_label *label, t_label **list_label)
|
||||
{
|
||||
t_label *tmp_label;
|
||||
|
||||
if (*list_label == NULL)
|
||||
*list_label = label;
|
||||
else
|
||||
{
|
||||
tmp_label = *list_label;
|
||||
while (tmp_label->next != NULL)
|
||||
tmp_label = tmp_label->next;
|
||||
tmp_label->next = label;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_append_token_label(t_token *token, t_token **list_token,
|
||||
t_label *label, t_label **list_label)
|
||||
{
|
||||
t_token *tmp_token;
|
||||
|
||||
if (*list_token == NULL)
|
||||
*list_token = token;
|
||||
else
|
||||
{
|
||||
tmp_token = *list_token;
|
||||
while (tmp_token->next != NULL)
|
||||
tmp_token = tmp_token->next;
|
||||
tmp_token->next = token;
|
||||
tmp_token->line = g_nb_line;
|
||||
}
|
||||
if (token->type == 1)
|
||||
ft_append_label(label, list_label);
|
||||
else
|
||||
free(label);
|
||||
}
|
64
srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c
Normal file
64
srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_check_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 11:24:43 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 18:19:05 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_cal_state(char *line, int state)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
state = 0;
|
||||
while (line[i] != 0)
|
||||
{
|
||||
if (line[i] == '"')
|
||||
state = state + 1;
|
||||
i++;
|
||||
}
|
||||
return (state);
|
||||
}
|
||||
|
||||
static char *ft_error_gnl(char *str, char *line, char *tmp)
|
||||
{
|
||||
ft_strdel(&str);
|
||||
ft_strdel(&line);
|
||||
ft_strdel(&tmp);
|
||||
ft_printf("error: quote `\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_check_line(char *line, int fd)
|
||||
{
|
||||
char *str;
|
||||
static char *tmp;
|
||||
int i;
|
||||
int state;
|
||||
|
||||
i = 0;
|
||||
state = 0;
|
||||
state = ft_cal_state(line, state);
|
||||
while (state % 2 == 1)
|
||||
{
|
||||
if (get_next_line(fd, &str, &tmp) != 1)
|
||||
return (ft_error_gnl(str, line, tmp));
|
||||
if (!(line = ft_strextend(line, "\n")))
|
||||
return (NULL);
|
||||
if (!(line = ft_strextend(line, str)))
|
||||
return (NULL);
|
||||
ft_strdel(&str);
|
||||
g_nb_line++;
|
||||
g_nb_char = 0;
|
||||
state = ft_cal_state(line, state);
|
||||
}
|
||||
line = ft_strextend(line, "\n");
|
||||
return (line);
|
||||
}
|
57
srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c
Normal file
57
srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_create_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:57:44 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/09 10:44:33 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static char *ft_cut_string(char *str)
|
||||
{
|
||||
char *str2;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = ft_strlen(str);
|
||||
j = 0;
|
||||
while (str[i - 1] == ' ' || str[i - 1] == '\t'
|
||||
|| str[i - 1] == '\v' || str[i - 1] == '\n')
|
||||
i--;
|
||||
if (!(str2 = ft_strnew(i + 1)))
|
||||
return (NULL);
|
||||
while (j < i)
|
||||
{
|
||||
str2[j] = str[j];
|
||||
j++;
|
||||
}
|
||||
free(str);
|
||||
str = str2;
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_create_string(char *line, int nb_char_token)
|
||||
{
|
||||
char *str;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v')
|
||||
i++;
|
||||
if (!(str = ft_strnew(nb_char_token - i)))
|
||||
return (NULL);
|
||||
while (j <= nb_char_token - i - 1)
|
||||
{
|
||||
str[j] = line[j + i];
|
||||
j++;
|
||||
}
|
||||
str = ft_cut_string(str);
|
||||
return (str);
|
||||
}
|
27
srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c
Normal file
27
srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_create_token_label.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 14:03:27 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 11:03:20 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
void ft_create_token_label(char *str, int type, t_token *token,
|
||||
t_label *label)
|
||||
{
|
||||
if (type == 1)
|
||||
{
|
||||
label->contents = str;
|
||||
label->next = NULL;
|
||||
}
|
||||
token->type = type;
|
||||
token->line = g_nb_line;
|
||||
token->contents = str;
|
||||
token->next = NULL;
|
||||
}
|
54
srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c
Normal file
54
srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_cut_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:53:27 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/09 10:44:50 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_check_line2(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] != ' ' && str[i] != '\n' && str[i] != '\t' && str[i] != '\v')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char **ft_cut_line(char **line, int nb_char_token)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
len = ft_strlen(*line);
|
||||
if (len < 1 || len == nb_char_token)
|
||||
return (NULL);
|
||||
if (!(str = ft_strnew(len - nb_char_token + 1)))
|
||||
return (NULL);
|
||||
while ((*line)[i + nb_char_token] != 0)
|
||||
{
|
||||
str[i] = (*line)[nb_char_token + i];
|
||||
i++;
|
||||
}
|
||||
if (!(ft_check_line2(str)))
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
free(*line);
|
||||
*line = str;
|
||||
return (line);
|
||||
}
|
26
srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c
Normal file
26
srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_empty_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 15:13:26 by jfleury #+# #+# */
|
||||
/* Updated: 2019/06/24 15:16:17 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_empty_line(char *line)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v'
|
||||
|| line[i] == '\n')
|
||||
i++;
|
||||
if (line[i] == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
62
srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c
Normal file
62
srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_free_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/25 11:04:41 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/13 15:11:14 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_free_token(t_token *token)
|
||||
{
|
||||
t_token *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (token != NULL)
|
||||
{
|
||||
tmp = token->next;
|
||||
ft_strdel(&token->contents);
|
||||
free(token);
|
||||
token = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_free_label(t_label *label)
|
||||
{
|
||||
t_label *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (label != NULL)
|
||||
{
|
||||
tmp = label->next;
|
||||
free(label);
|
||||
label = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_free_list(t_lst_tk *list, t_label *label)
|
||||
{
|
||||
t_lst_tk *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (list != NULL)
|
||||
{
|
||||
tmp = list->next;
|
||||
ft_free_token(list->token);
|
||||
free(list);
|
||||
list = tmp;
|
||||
}
|
||||
ft_free_label(label);
|
||||
}
|
||||
|
||||
void ft_free_all(t_lst_tk *list, t_label *label, t_header *head)
|
||||
{
|
||||
if (head)
|
||||
free(head);
|
||||
ft_free_list(list, label);
|
||||
}
|
27
srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c
Normal file
27
srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_line_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:55:57 by jfleury #+# #+# */
|
||||
/* Updated: 2019/06/14 13:56:11 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_line_strchr(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
73
srcs/srcs_asm/lexer/src_variable/var_automaton.c
Normal file
73
srcs/srcs_asm/lexer/src_variable/var_automaton.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* var_automaton.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 10:13:09 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/02 17:22:36 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int g_nb_line = 0;
|
||||
|
||||
int g_nb_char = 0;
|
||||
|
||||
int g_nb_token = 0;
|
||||
|
||||
int g_matrice_automaton[30][17] =
|
||||
{
|
||||
{23, 23, -1, 24, 26, 17, 19, 9, 29, -1, 12, 21, 21, 22, 0, 0, -1},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{12, 12, -1, 12, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, -1, 1, -1},
|
||||
{13, 13, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, -1},
|
||||
{-1, -1, -1, 14, -1, -1, -1, 3, -1, -1, -1, 3, 3, -1, 3, 3, -1},
|
||||
{15, 15, -1, 15, -1, -1, -1, 4, -1, -1, 15, 4, 4, -1, 4, 4, -1},
|
||||
{-1, -1, -1, 16, -1, -1, -1, 5, -1, -1, -1, 5, 5, -1, 5, 5, -1},
|
||||
{17, 17, -1, 17, -1, -1, -1, 6, -1, -1, 17, 6, 6, -1, 6, 6, -1},
|
||||
{-1, -1, -1, 18, -1, -1, -1, 7, -1, -1, -1, 7, 7, -1, 7, 7, -1},
|
||||
{19, 19, 19, 19, 19, 19, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19},
|
||||
{-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, -1, -1, -1, -1, 9, 9, -1},
|
||||
{21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 10, 21},
|
||||
{22, 22, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 11, 11, -1},
|
||||
{23, 23, -1, 25, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, 2, 2, -1},
|
||||
{12, 12, -1, 24, -1, 1, -1, 5, -1, -1, 12, -1, -1, -1, 5, 5, -1},
|
||||
{12, 12, -1, 25, -1, 1, -1, 7, -1, -1, 12, 7, 7, -1, 7, 7, -1},
|
||||
{-1, -1, -1, 14, -1, 15, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, -1},
|
||||
{-1, -1, -1, 16, -1, 17, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1},
|
||||
{-1, -1, -1, 28, -1, -1, -1, 6, -1, -1, -1, 6, 6, -1, 6, 6, -1},
|
||||
{-1, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
|
||||
};
|
||||
|
||||
char *g_string_automaton[16] =
|
||||
{
|
||||
"r",
|
||||
"abcdefghijklmnopqstuvwxyz",
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
"0123456789",
|
||||
"%",
|
||||
":",
|
||||
"\"",
|
||||
",",
|
||||
"-",
|
||||
"+",
|
||||
"_",
|
||||
"#",
|
||||
";",
|
||||
".",
|
||||
"\t\f ",
|
||||
"\n"
|
||||
};
|
112
srcs/srcs_asm/main_asm.c
Normal file
112
srcs/srcs_asm/main_asm.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main_asm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/28 17:08:18 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 16:23:43 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_check_fd(int fd, char **argv)
|
||||
{
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_printf("error: no such file or directory: '%s'\n", argv[1]);
|
||||
close(fd);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_check_arg(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int fd;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
ft_printf("usage: ./asm <file.s>\n");
|
||||
exit(0);
|
||||
}
|
||||
if (argc > 2)
|
||||
{
|
||||
ft_printf("error: too many files / "
|
||||
"only one file accepted\n");
|
||||
exit(0);
|
||||
}
|
||||
i = ft_strlen(argv[1]);
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
ft_check_fd(fd, argv);
|
||||
close(fd);
|
||||
if (i >= 3 && argv[1][i - 1] == 's' && argv[1][i - 2] == '.')
|
||||
return ;
|
||||
else
|
||||
{
|
||||
ft_printf("error: invalid file\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
t_header *init_header(void)
|
||||
{
|
||||
t_header *head;
|
||||
int i;
|
||||
|
||||
if (!(head = (t_header *)ft_memalloc(sizeof(t_header))))
|
||||
return (NULL);
|
||||
head->magic = 15369203;
|
||||
head->prog_size = 0;
|
||||
i = -1;
|
||||
while (++i < PROG_NAME_LENGTH)
|
||||
head->prog_name[i] = 0;
|
||||
head->prog_name[i] = '\0';
|
||||
i = -1;
|
||||
while (++i < COMMENT_LENGTH)
|
||||
head->comment[i] = 0;
|
||||
head->comment[i] = '\0';
|
||||
return (head);
|
||||
}
|
||||
|
||||
static int ft_lexer_parser(t_header *head, t_label *list_label,
|
||||
t_lst_tk *lst_tk, char **argv)
|
||||
{
|
||||
if (!(ft_lexer(&lst_tk, &list_label, argv)))
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (0);
|
||||
}
|
||||
if (lst_tk == NULL)
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
ft_printf("error: empty file / directory file\n");
|
||||
return (0);
|
||||
}
|
||||
if (!(ft_parser(lst_tk, list_label, head)))
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (0);
|
||||
}
|
||||
ft_convert(lst_tk, head, argv, list_label);
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_header *head;
|
||||
t_label *list_label;
|
||||
t_lst_tk *lst_tk;
|
||||
|
||||
ft_check_arg(argc, argv);
|
||||
if (!(head = init_header()))
|
||||
return (0);
|
||||
list_label = NULL;
|
||||
lst_tk = NULL;
|
||||
if (!(ft_lexer_parser(head, list_label, lst_tk, argv)))
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
39
srcs/srcs_asm/op.c
Normal file
39
srcs/srcs_asm/op.c
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* op.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/13 12:13:23 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/15 18:30:09 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_op g_tab[17] =
|
||||
{
|
||||
{"live", 1, {T_DIR}, 1, 10, "alive", 0, 0},
|
||||
{"ld", 2, {T_DIR | T_IND, T_REG}, 2, 5, "load", 1, 0},
|
||||
{"st", 2, {T_REG, T_IND | T_REG}, 3, 5, "store", 1, 0},
|
||||
{"add", 3, {T_REG, T_REG, T_REG}, 4, 10, "addition", 1, 0},
|
||||
{"sub", 3, {T_REG, T_REG, T_REG}, 5, 10, "soustraction", 1, 0},
|
||||
{"and", 3, {T_REG | T_DIR | T_IND, T_REG | T_IND | T_DIR, T_REG}, 6, 6,
|
||||
"et (and r1, r2, r3 r1&r2 -> r3", 1, 0},
|
||||
{"or", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 7, 6,
|
||||
"ou (or r1, r2, r3 r1 | r2 -> r3", 1, 0},
|
||||
{"xor", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 8, 6,
|
||||
"ou (xor r1, r2, r3 r1^r2 -> r3", 1, 0},
|
||||
{"zjmp", 1, {T_DIR | T_IND}, 9, 20, "jump if zero", 0, 1},
|
||||
{"ldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG | T_IND, T_REG}, 10, 25,
|
||||
"load index", 1, 1},
|
||||
{"sti", 3, {T_REG, T_REG | T_DIR | T_IND, T_DIR | T_REG}, 11, 25,
|
||||
"store index", 1, 1},
|
||||
{"fork", 1, {T_DIR | T_IND}, 12, 800, "fork", 0, 1},
|
||||
{"lld", 2, {T_DIR | T_IND, T_REG}, 13, 10, "long load", 1, 0},
|
||||
{"lldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 14, 50,
|
||||
"long load index", 1, 1},
|
||||
{"lfork", 1, {T_DIR}, 15, 1000, "long fork", 0, 1},
|
||||
{"aff", 1, {T_REG}, 16, 2, "aff", 1, 0}
|
||||
};
|
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_cal_progsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/02 15:20:20 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/13 15:26:28 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static unsigned char *ft_resize(unsigned char *str, int size)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
int j;
|
||||
unsigned char *str2;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
len = ft_strlen((char*)str);
|
||||
if (!(str2 = malloc(sizeof(char) * (size * 2))))
|
||||
return (NULL);
|
||||
while (i < size * 2 - len)
|
||||
{
|
||||
str2[i] = '0';
|
||||
i++;
|
||||
}
|
||||
while (i < size * 2)
|
||||
{
|
||||
str2[i] = str[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
free(str);
|
||||
return (str2);
|
||||
}
|
||||
|
||||
static unsigned char ft_atoi_hex(unsigned char *str)
|
||||
{
|
||||
unsigned char nb;
|
||||
|
||||
if (str[0] >= '0' && str[0] <= '9')
|
||||
str[0] = str[0] - 48;
|
||||
if (str[1] >= '0' && str[1] <= '9')
|
||||
str[1] = str[1] - 48;
|
||||
if (str[0] >= 'a' && str[0] <= 'f')
|
||||
str[0] = str[0] - 87;
|
||||
if (str[1] >= 'a' && str[1] <= 'f')
|
||||
str[1] = str[1] - 87;
|
||||
if (str[0] != 0)
|
||||
nb = (str[0] * 16) + str[1];
|
||||
else
|
||||
nb = str[1];
|
||||
return (nb);
|
||||
}
|
||||
|
||||
unsigned char *ft_conv_hexa(int nbr, int size)
|
||||
{
|
||||
unsigned char *str;
|
||||
unsigned char *final_str;
|
||||
int len;
|
||||
|
||||
if (!(final_str = (unsigned char*)ft_strnew(size)))
|
||||
return (NULL);
|
||||
if (size == 2)
|
||||
str = (unsigned char *)ft_itoa_base_short(nbr, 16);
|
||||
else
|
||||
str = (unsigned char *)ft_itoa_base_int(nbr, 16);
|
||||
len = ft_strlen((char*)str);
|
||||
if (len < size * 2)
|
||||
str = ft_resize(str, size);
|
||||
while (size * 2 > 0)
|
||||
{
|
||||
final_str[size - 1] = ft_atoi_hex(str + ((size * 2) - 2));
|
||||
size--;
|
||||
}
|
||||
ft_strdel((char **)&str);
|
||||
return (final_str);
|
||||
}
|
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_command.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 11:24:46 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:35:23 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_parse_name(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion name in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > PROG_NAME_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion name too long (Max length 128)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion name\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->prog_name, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parse_champ_cmt(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > COMMENT_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion comment too long "
|
||||
"(Max length 2048)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion comment\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->comment, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parse_header(t_lst_tk **lst, t_header *head)
|
||||
{
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, NAME_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_name((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no name in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, COMMENT_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_champ_cmt((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
return (1);
|
||||
}
|
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_instruc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 16:17:56 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:36:42 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int is_inst(char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!name)
|
||||
return (-1);
|
||||
i = 0;
|
||||
while (i < 16 && ft_strcmp(name, g_tab[i].name) != 0)
|
||||
i++;
|
||||
if (i == 16)
|
||||
return (-1);
|
||||
else
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_verif_param(t_token *tk, int inst, int num_param, t_label *label)
|
||||
{
|
||||
int type;
|
||||
|
||||
if (tk->type == INDIRECT
|
||||
|| (tk->type == INDIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_IND;
|
||||
else if (tk->type == DIRECT
|
||||
|| (tk->type == DIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_DIR;
|
||||
else if (tk->type == REGISTER && ft_atoi(tk->contents + 1) < 100)
|
||||
type = T_REG;
|
||||
else
|
||||
type = 0;
|
||||
if (g_tab[inst].poss[num_param] & type)
|
||||
return (1);
|
||||
else
|
||||
{
|
||||
ft_printf("error: l%d : Invalid parameter %d for instruction %s\n",
|
||||
tk->line, num_param, g_tab[inst].name);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int ft_correct_instru(t_token *tk, int i, t_label *label, int line)
|
||||
{
|
||||
int num_param;
|
||||
|
||||
num_param = 0;
|
||||
while (tk && num_param < g_tab[i].nb_param)
|
||||
{
|
||||
if (ft_verif_param(tk, i, num_param, label))
|
||||
{
|
||||
tk = tk->next;
|
||||
if (!tk && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && tk->type == COMMENT
|
||||
&& !tk->next && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
num_param++;
|
||||
else
|
||||
return (ft_printf("error: l%d: bad param %d for instru %s\n",
|
||||
line, num_param + 1, g_tab[i].name) && 0);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
tk = tk->next;
|
||||
}
|
||||
return (ft_printf("error: l%d: bad params for instru %s\n",
|
||||
line, g_tab[i].name) && 0);
|
||||
}
|
||||
|
||||
int ft_calc_size(t_token *tk, int inst)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 1 + g_tab[inst].byte;
|
||||
while (tk)
|
||||
{
|
||||
if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL)
|
||||
size += 2;
|
||||
else if (tk->type == DIRECT || tk->type == DIRECT_LABEL)
|
||||
size += (g_tab[inst].size) ? 2 : 4;
|
||||
else if (tk->type == REGISTER)
|
||||
size += 1;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
int ft_parse_instruct(t_token *tk, t_label *label, unsigned int *size)
|
||||
{
|
||||
int inst;
|
||||
|
||||
inst = is_inst(tk->contents);
|
||||
if (inst < 0)
|
||||
{
|
||||
return (ft_printf("error: l%d: invalid instruction '%s'\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (!tk->next)
|
||||
{
|
||||
return (ft_printf("error: l%d: no params for instru %s\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (ft_correct_instru(tk->next, inst, label, tk->line))
|
||||
{
|
||||
*size += ft_calc_size(tk->next, inst);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/30 10:48:19 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 12:23:55 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
void ft_label_pos(t_label *label, char *tk_lab, int size)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
if (ft_strequ(label->contents, tk_lab))
|
||||
{
|
||||
label->place = size;
|
||||
}
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
void init_label(t_label *label)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
label->contents[ft_strlen(label->contents) - 1] = '\0';
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
int ft_parse_label(t_label *label, t_token *token, unsigned int *size)
|
||||
{
|
||||
ft_label_pos(label, token->contents, *size);
|
||||
if (!token->next || token->next->type == COMMENT)
|
||||
return (1);
|
||||
else if (!ft_parse_instruct(token->next, label, size))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parser(t_lst_tk *lst, t_label *label, t_header *head)
|
||||
{
|
||||
unsigned int size;
|
||||
|
||||
init_label(label);
|
||||
size = 0;
|
||||
if (!parse_header(&lst, head))
|
||||
return (0);
|
||||
while (lst)
|
||||
{
|
||||
if (lst->token->type == INSTRUCTION)
|
||||
{
|
||||
if (!ft_parse_instruct(lst->token, label, &size))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type == LABEL)
|
||||
{
|
||||
if (!(ft_parse_label(label, lst->token, &size)))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type != COMMENT)
|
||||
return (0);
|
||||
lst = lst->next;
|
||||
}
|
||||
if ((head->prog_size = size) == 0)
|
||||
return (ft_printf("error: no instruction in the file\n") && 0);
|
||||
return (1);
|
||||
}
|
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 17:08:25 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/13 17:14:11 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
unsigned char *ft_strextend_nm(unsigned char *s1,
|
||||
char unsigned *s2, int n, int m)
|
||||
{
|
||||
unsigned char *str;
|
||||
int i;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
i = n + m;
|
||||
if (!(str = (unsigned char *)malloc(sizeof(*str) * i + 1)))
|
||||
return (NULL);
|
||||
ft_strcpy_n(str, s1, 0, n);
|
||||
ft_strcpy_n(str, s2, n, m);
|
||||
ft_strdel((char **)&s1);
|
||||
ft_strdel((char **)&s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
int ft_tklen(t_token *tk)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!tk)
|
||||
return (0);
|
||||
while (tk)
|
||||
{
|
||||
i++;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
unsigned char *ft_strcpy_n(unsigned char *dest,
|
||||
const unsigned char *src, int n, int m)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
i++;
|
||||
j = 0;
|
||||
while (j < m)
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
||||
char *ft_strnew_1(size_t size)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (!(str = (char*)malloc(sizeof(char) * size + 1)))
|
||||
return (NULL);
|
||||
ft_memset(str, '0', size);
|
||||
str[size] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strcpy_1(char *dest, const char *src)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (src[j] != '\0')
|
||||
{
|
||||
if (src[j] != '"')
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
20
srcs/srcs_vm/check_args.c
Normal file
20
srcs/srcs_vm/check_args.c
Normal 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
61
srcs/srcs_vm/cw_add_sub.c
Normal 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
35
srcs/srcs_vm/cw_aff.c
Normal 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);
|
||||
}
|
96
srcs/srcs_vm/cw_and_or_xor.c
Normal file
96
srcs/srcs_vm/cw_and_or_xor.c
Normal 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
54
srcs/srcs_vm/cw_args.c
Normal 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
48
srcs/srcs_vm/cw_error.c
Normal 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);
|
||||
}
|
61
srcs/srcs_vm/cw_fork_lfork.c
Normal file
61
srcs/srcs_vm/cw_fork_lfork.c
Normal 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");
|
||||
}
|
100
srcs/srcs_vm/cw_init_ncurs.c
Normal file
100
srcs/srcs_vm/cw_init_ncurs.c
Normal 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
50
srcs/srcs_vm/cw_ld_lld.c
Normal 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");
|
||||
}
|
74
srcs/srcs_vm/cw_ldi_lldi.c
Normal file
74
srcs/srcs_vm/cw_ldi_lldi.c
Normal 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
51
srcs/srcs_vm/cw_live.c
Normal 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);
|
||||
}
|
90
srcs/srcs_vm/cw_ops_tools.c
Normal file
90
srcs/srcs_vm/cw_ops_tools.c
Normal 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
117
srcs/srcs_vm/cw_parser.c
Normal 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);
|
||||
}
|
34
srcs/srcs_vm/cw_print_usage.c
Normal file
34
srcs/srcs_vm/cw_print_usage.c
Normal 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
115
srcs/srcs_vm/cw_process.c
Normal 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;
|
||||
}
|
41
srcs/srcs_vm/cw_read_mem.c
Normal file
41
srcs/srcs_vm/cw_read_mem.c
Normal 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
42
srcs/srcs_vm/cw_st.c
Normal 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
57
srcs/srcs_vm/cw_sti.c
Normal 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
122
srcs/srcs_vm/cw_visual.c
Normal 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);
|
||||
}
|
||||
}
|
60
srcs/srcs_vm/cw_visual_win.c
Normal file
60
srcs/srcs_vm/cw_visual_win.c
Normal 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
91
srcs/srcs_vm/cw_vm.c
Normal 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++;
|
||||
}
|
||||
}
|
34
srcs/srcs_vm/cw_write_mem.c
Normal file
34
srcs/srcs_vm/cw_write_mem.c
Normal 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
27
srcs/srcs_vm/cw_zjump.c
Normal 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
117
srcs/srcs_vm/main.c
Normal 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);
|
||||
}
|
Reference in New Issue
Block a user