push
This commit is contained in:
138
include/asm.h
Normal file
138
include/asm.h
Normal file
@@ -0,0 +1,138 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* asm.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/29 16:43:12 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 11:40:38 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ASM_H
|
||||
# define ASM_H
|
||||
|
||||
# define LABEL 1
|
||||
# define INSTRUCTION 2
|
||||
# define DIRECT 3
|
||||
# define DIRECT_LABEL 4
|
||||
# define INDIRECT 5
|
||||
# define INDIRECT_LABEL 6
|
||||
# define REGISTER 7
|
||||
# define STRING 8
|
||||
# define SEPERATOR 9
|
||||
# define COMMENT 10
|
||||
# define COMMAND 11
|
||||
|
||||
# include "libft.h"
|
||||
# include "ft_printf.h"
|
||||
# include "op.h"
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <fcntl.h>
|
||||
|
||||
/*
|
||||
** Struct
|
||||
*/
|
||||
|
||||
typedef struct s_token
|
||||
{
|
||||
int type;
|
||||
char *contents;
|
||||
int line;
|
||||
struct s_token *next;
|
||||
} t_token;
|
||||
|
||||
typedef struct s_label
|
||||
{
|
||||
char *contents;
|
||||
int place;
|
||||
struct s_label *next;
|
||||
} t_label;
|
||||
|
||||
typedef struct s_lst_tk
|
||||
{
|
||||
t_token *token;
|
||||
struct s_lst_tk *next;
|
||||
} t_lst_tk;
|
||||
|
||||
typedef struct s_pos_lab
|
||||
{
|
||||
t_label *lab;
|
||||
int pos;
|
||||
} t_pos_lab;
|
||||
|
||||
typedef struct s_inst
|
||||
{
|
||||
char *name;
|
||||
int nb_params;
|
||||
char poss[3];
|
||||
} t_inst;
|
||||
|
||||
/*
|
||||
** Global
|
||||
*/
|
||||
|
||||
extern int g_matrice_automaton[30][17];
|
||||
extern char *g_string_automaton[16];
|
||||
extern int g_nb_line;
|
||||
extern int g_nb_char;
|
||||
extern int g_nb_token;
|
||||
extern int g_place;
|
||||
extern t_op g_tab[17];
|
||||
|
||||
/*
|
||||
** ASM
|
||||
*/
|
||||
|
||||
t_label *get_label(t_token *tk, t_label *label);
|
||||
void ft_convert(t_lst_tk *lst, t_header *head, char **argv,
|
||||
t_label *list_lab);
|
||||
void ft_free_all(t_lst_tk *list, t_label *label, t_header *head);
|
||||
unsigned char *ft_conv_instru(t_token *tk, int *size_instru,
|
||||
t_pos_lab pos_lab);
|
||||
unsigned char *ft_strextend_nm(unsigned char *s1, char unsigned *s2,
|
||||
int n, int m);
|
||||
|
||||
/*
|
||||
** Lexer
|
||||
*/
|
||||
|
||||
t_token *ft_lexer_token(char **line, t_label **list_label);
|
||||
int ft_lexer(t_lst_tk **lst_tk, t_label **list_label,
|
||||
char **argv);
|
||||
int ft_automaton(char *line, t_token *token, t_label *label,
|
||||
int *nb_char_token);
|
||||
int ft_line_strchr(char *str, char c);
|
||||
int ft_empty_line(char *line);
|
||||
int ft_append_lst_tk(t_token *list_token, t_lst_tk **list);
|
||||
char **ft_cut_line(char **line, int nb_char_token);
|
||||
char *ft_create_string(char *line, int nb_char_token);
|
||||
char *ft_check_line(char *line, int fd);
|
||||
void ft_append_token_label(t_token *token, t_token **list_token,
|
||||
t_label *label, t_label **list_label);
|
||||
void ft_create_token_label(char *str, int type, t_token *token,
|
||||
t_label *label);
|
||||
void ft_free_list(t_lst_tk *list, t_label *label);
|
||||
|
||||
/*
|
||||
** Parser
|
||||
*/
|
||||
|
||||
int ft_tklen(t_token *tk);
|
||||
int ft_parse_instruct(t_token *tk, t_label *label,
|
||||
unsigned int *size);
|
||||
int parse_header(t_lst_tk **lst, t_header *head);
|
||||
int ft_parser(t_lst_tk *lst, t_label *label, t_header *head);
|
||||
int is_inst(char *name);
|
||||
char *ft_error(char *msg);
|
||||
char *ft_parse_command(t_token *tk);
|
||||
char *ft_parse_command2(t_token *tk, t_header *head);
|
||||
char *ft_strnew_1(size_t size);
|
||||
char *ft_strcpy_1(char *dest, const char *src);
|
||||
unsigned char *ft_strcpy_n(unsigned char *dest, const unsigned char *src,
|
||||
int n, int m);
|
||||
unsigned char *ft_conv_hexa(int nbr, int size);
|
||||
|
||||
#endif
|
233
include/corewar.h
Normal file
233
include/corewar.h
Normal file
@@ -0,0 +1,233 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* corewar.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/08 11:54:03 by tmaze #+# #+# */
|
||||
/* Updated: 2019/07/15 19:02:57 by igarbuz ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef COREWAR_H
|
||||
# define COREWAR_H
|
||||
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdio.h>
|
||||
# include <fcntl.h>
|
||||
# include <errno.h>
|
||||
# include "libft.h"
|
||||
# include "ft_printf.h"
|
||||
# include <ncurses.h>
|
||||
|
||||
# define IND_SIZE 2
|
||||
# define REG_SIZE 4
|
||||
# define DIR_SIZE REG_SIZE
|
||||
|
||||
# define REG_CODE 1
|
||||
# define DIR_CODE 2
|
||||
# define IND_CODE 3
|
||||
|
||||
# define MAX_ARGS_NUMBER 4
|
||||
# define MAX_PLAYERS 4
|
||||
# define MEM_SIZE (4*1024)
|
||||
# define IDX_MOD (MEM_SIZE / 8)
|
||||
# define CHAMP_MAX_SIZE (MEM_SIZE / 6)
|
||||
|
||||
/*
|
||||
** Added MEM_SIZE overflow protection binary mask. MEM_SIZE must be 2^x number.
|
||||
** MEM_SIZE_M must be MEM_SIZE - 1;
|
||||
*/
|
||||
# define MEM_SIZE_M MEM_SIZE - 1
|
||||
|
||||
# define COMMENT_CHAR '#'
|
||||
# define LABEL_CHAR ':'
|
||||
# define DIRECT_CHAR '%'
|
||||
# define SEPARATOR_CHAR ','
|
||||
|
||||
# define LABEL_CHARS "abcdefghijklmnopqrstuvwxyz_0123456789"
|
||||
|
||||
# define NAME_CMD_STRING ".name"
|
||||
# define COMMENT_CMD_STRING ".comment"
|
||||
|
||||
# define REG_NUMBER 16
|
||||
|
||||
# define CYCLE_TO_DIE 1536
|
||||
# define CYCLE_DELTA 50
|
||||
# define NBR_LIVE 21
|
||||
# define MAX_CHECKS 10
|
||||
|
||||
# define T_REG 1
|
||||
# define T_DIR 2
|
||||
# define T_IND 4
|
||||
|
||||
# define PROG_NAME_LENGTH (128)
|
||||
# define COMMENT_LENGTH (2048)
|
||||
# define COREWAR_EXEC_MAGIC 0xea83f3
|
||||
|
||||
# define AG_VERB 0x0001
|
||||
# define AG_DUMP32 0x0002
|
||||
# define AG_DUMP64 0x0004
|
||||
# define AG_VISUAL 0x0008
|
||||
# define AG_AFF 0x0010
|
||||
|
||||
# define VB_LIVE 0x0001
|
||||
# define VB_CYCLES 0x0002
|
||||
# define VB_OPS 0x0004
|
||||
# define VB_DEATH 0x0008
|
||||
# define VB_PC 0x0010
|
||||
|
||||
# define ART_MAX_SIZE 1023
|
||||
|
||||
char g_buf[BUF_SIZE];
|
||||
|
||||
typedef struct s_player
|
||||
{
|
||||
unsigned int magic;
|
||||
char prog_name[PROG_NAME_LENGTH + 1];
|
||||
unsigned int prog_size;
|
||||
char comment[COMMENT_LENGTH + 1];
|
||||
int num;
|
||||
char col;
|
||||
unsigned int live_ct;
|
||||
unsigned int lst_live;
|
||||
unsigned char code[CHAMP_MAX_SIZE];
|
||||
} t_player;
|
||||
|
||||
typedef struct s_process
|
||||
{
|
||||
size_t pid;
|
||||
unsigned char op;
|
||||
unsigned char carry;
|
||||
char new;
|
||||
unsigned int wait;
|
||||
int regs[REG_NUMBER + 1];
|
||||
int lst_live;
|
||||
unsigned short int pc;
|
||||
struct s_process *next;
|
||||
} t_process;
|
||||
|
||||
typedef struct s_visual
|
||||
{
|
||||
WINDOW *arena;
|
||||
WINDOW *stat;
|
||||
bool paused;
|
||||
int delay;
|
||||
} t_visual;
|
||||
|
||||
typedef struct s_vm
|
||||
{
|
||||
unsigned char mem[MEM_SIZE];
|
||||
unsigned char col[MEM_SIZE];
|
||||
t_player pls[MAX_PLAYERS];
|
||||
int args;
|
||||
int dump;
|
||||
int verb;
|
||||
int live;
|
||||
int live_ct;
|
||||
int cycles;
|
||||
int ctd_c;
|
||||
int ctd;
|
||||
short int nb_j;
|
||||
size_t p_ct;
|
||||
size_t pid_ct;
|
||||
t_process *tl;
|
||||
t_visual vis;
|
||||
} t_vm;
|
||||
|
||||
typedef struct s_ops
|
||||
{
|
||||
unsigned int wait;
|
||||
void (*func)(t_process*, t_vm*);
|
||||
} t_ops;
|
||||
|
||||
typedef struct s_args
|
||||
{
|
||||
char t;
|
||||
short int i;
|
||||
} t_args;
|
||||
|
||||
typedef struct s_pargs
|
||||
{
|
||||
char *arg;
|
||||
void (*func)(char**, t_vm*, int);
|
||||
char nb_ag;
|
||||
} t_pargs;
|
||||
|
||||
void cw_print_usage(void);
|
||||
|
||||
void clear_process(t_vm *vm);
|
||||
void init_process(t_vm *vm);
|
||||
void del_prcs(t_vm *vm);
|
||||
|
||||
void init_vm(t_vm *vm, int i);
|
||||
int load_champ(t_vm *vm);
|
||||
void mem_dump(t_vm *vm);
|
||||
|
||||
void arg_dump(char **av, t_vm *vm, int i);
|
||||
void arg_verb(char **av, t_vm *vm, int i);
|
||||
void arg_visual(char **av, t_vm *vm, int i);
|
||||
void arg_aff(char **av, t_vm *vm, int i);
|
||||
void fgqarg_aff(char **av, t_vm *vm, int i);
|
||||
|
||||
int cw_parser(int ac, char **av, t_vm *vm);
|
||||
|
||||
int check_file(char *path);
|
||||
|
||||
int read_four(t_vm *vm, size_t index);
|
||||
short int read_two(t_vm *vm, size_t index);
|
||||
unsigned char read_mem(t_vm *vm, int ind);
|
||||
|
||||
void write_mem(t_vm *vm, unsigned int index, char val
|
||||
, unsigned short int pc);
|
||||
void write_four(t_vm *vm, unsigned int index, int val
|
||||
, unsigned short int pc);
|
||||
|
||||
unsigned int l2b_endian(unsigned int num);
|
||||
void cw_move_pc(t_vm *vm, t_process *p, int jp);
|
||||
void init_args(t_args *ag, t_process *p, t_vm *vm);
|
||||
int check_reg(t_vm *vm, int ind);
|
||||
int cw_ocp(char nb_ag, t_args *ag, char size_dir);
|
||||
|
||||
void cw_live(t_process *p, t_vm *vm);
|
||||
void cw_ld(t_process *p, t_vm *vm);
|
||||
void cw_st(t_process *p, t_vm *vm);
|
||||
void cw_add(t_process *p, t_vm *vm);
|
||||
void cw_sub(t_process *p, t_vm *vm);
|
||||
void cw_and(t_process *p, t_vm *vm);
|
||||
void cw_or(t_process *p, t_vm *vm);
|
||||
void cw_xor(t_process *p, t_vm *vm);
|
||||
void cw_zjump(t_process *p, t_vm *vm);
|
||||
void cw_ldi(t_process *p, t_vm *vm);
|
||||
void cw_sti(t_process *p, t_vm *vm);
|
||||
void cw_fork(t_process *p, t_vm *vm);
|
||||
void cw_lld(t_process *p, t_vm *vm);
|
||||
void cw_lldi(t_process *p, t_vm *vm);
|
||||
void cw_lfork(t_process *p, t_vm *vm);
|
||||
void cw_aff(t_process *p, t_vm *vm);
|
||||
|
||||
int err_msg(int err, t_vm *vm);
|
||||
void error(t_vm *vm, int err);
|
||||
|
||||
/*
|
||||
** cw_visual.c
|
||||
*/
|
||||
void print_arena(t_vm *vm);
|
||||
void print_pc(t_vm *vm, unsigned short int pc, char col);
|
||||
void control_delay(t_vm *vm);
|
||||
|
||||
/*
|
||||
** cw_init_ncurs.c
|
||||
*/
|
||||
void init_ncurses(t_vm *vm);
|
||||
void destroy_ncurses(t_vm *vm);
|
||||
void destroy_ncurses_exit(t_vm *vm);
|
||||
void check_user_exit(t_vm *vm, int key);
|
||||
/*
|
||||
** cw_visual_win.c
|
||||
*/
|
||||
void visual_winners(t_vm *vm, int i);
|
||||
void visual_delay_update(t_vm *vm);
|
||||
#endif
|
69
include/op.h
Normal file
69
include/op.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* op.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2013/10/04 11:33:27 by zaz #+# #+# */
|
||||
/* Updated: 2019/07/15 18:14:53 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef OP_H
|
||||
# define OP_H
|
||||
|
||||
# define IND_SIZE 2
|
||||
# define REG_SIZE 4
|
||||
# define DIR_SIZE REG_SIZE
|
||||
# define REG_CODE 1
|
||||
# define DIR_CODE 2
|
||||
# define IND_CODE 3
|
||||
# define MAX_ARGS_NUMBER 4
|
||||
# define MAX_PLAYERS 4
|
||||
# define MEM_SIZE (4*1024)
|
||||
# define IDX_MOD (MEM_SIZE / 8)
|
||||
# define CHAMP_MAX_SIZE (MEM_SIZE / 6)
|
||||
# define COMMENT_CHAR '#'
|
||||
# define LABEL_CHAR ':'
|
||||
# define DIRECT_CHAR '%'
|
||||
# define SEPARATOR_CHAR ','
|
||||
# define LABEL_CHARS "abcdefghijklmnopqrstuvwxyz_0123456789"
|
||||
# define NAME_CMD_STRING ".name"
|
||||
# define COMMENT_CMD_STRING ".comment"
|
||||
# define REG_NUMBER 16
|
||||
# define CYCLE_TO_DIE 1536
|
||||
# define CYCLE_DELTA 50
|
||||
# define NBR_LIVE 21
|
||||
# define MAX_CHECKS 10
|
||||
# define T_REG 1
|
||||
# define T_DIR 2
|
||||
# define T_IND 4
|
||||
# define T_LAB 8
|
||||
# define PROG_NAME_LENGTH (128)
|
||||
# define COMMENT_LENGTH (2048)
|
||||
# define COREWAR_EXEC_MAGIC 0xea83f3
|
||||
|
||||
typedef char t_arg_type;
|
||||
|
||||
typedef struct s_header
|
||||
{
|
||||
unsigned int magic;
|
||||
char prog_name[PROG_NAME_LENGTH + 1];
|
||||
unsigned int prog_size;
|
||||
char comment[COMMENT_LENGTH + 1];
|
||||
} t_header;
|
||||
|
||||
typedef struct s_op
|
||||
{
|
||||
char *name;
|
||||
int nb_param;
|
||||
char poss[3];
|
||||
char op_code;
|
||||
int cycle;
|
||||
char *comp_name;
|
||||
int byte;
|
||||
int size;
|
||||
} t_op;
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user