Skip to content

Commit 8c3cb13

Browse files
committed
first synthesis merge (fgalar), disasm, vm: corewar1.0
1 parent dd81f98 commit 8c3cb13

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1833
-254
lines changed

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
# By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2020/08/20 22:37:03 by ciglesia #+# #+# #
9-
# Updated: 2020/09/05 22:25:31 by ciglesia ### ########.fr #
9+
# Updated: 2020/09/27 17:14:26 by ciglesia ### ########.fr #
1010
# #
1111
#******************************************************************************#
1212

13-
DEPENDENCIES = Makefile libft/Makefile vm/Makefile assembler/Makefile
13+
DEPENDENCIES = Makefile libft/Makefile vm/Makefile assembler/Makefile disassembler/Makefile
1414

1515
ECHO = /bin/echo -e
1616

@@ -20,16 +20,19 @@ all: $(DEPENDENCIES)
2020
@$(MAKE) -C libft/
2121
@$(MAKE) -C vm/
2222
@$(MAKE) -C assembler/
23+
@$(MAKE) -C disassembler/
2324

2425
clean:
2526
@$(MAKE) -C libft/ clean
2627
@$(MAKE) -C vm/ clean
2728
@$(MAKE) -C assembler/ clean
29+
@$(MAKE) -C disassembler/ clean
2830

2931
fclean: clean
3032
@$(MAKE) -C libft/ fclean
3133
@$(MAKE) -C vm/ fclean
3234
@$(MAKE) -C assembler/ fclean
35+
@$(MAKE) -C disassembler/ fclean
3336

3437
re:
3538
@echo "Recompiling"

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@ This inspired A. K. Dewdney to coin the idea for Core War.
55

66
The idea was simple. You compete by designing viruses to overtake a computer. You win by protecting your own program and overwriting your opponent's programs. This is all happening on a virtual computer. Think, a simple computer within your computer.
77

8-
![](corewar2.gif?raw=true "corewar")
8+
![](docs/corewar2.gif?raw=true "corewar")
9+
10+
=======
11+
12+
Le Corewar est un jeu très particulier. Il consiste à rassembler autour d’une "machine virtuelle" des "joueurs", lesquels vont y charger des "champions" qui vont se battre à l’aide de "processus", dans le but, entre autres, de faire en sorte qu’on dise d’eux qu’ils sont "en vie".
13+
14+
• Les processus s’exécutent séquentiellement au sein de la même machine virtuelle, et du même espace mémoire. Ils peuvent donc, entre autre chose, s’écrire les uns sur les autres afin de se corrompre mutuellement, de forcer les autres à exécuter des instructions qui leur font du mal.
15+
• Le jeu se termine quand plus aucun processus n’est en vie. À ce moment là, le gagnant est le dernier joueur à avoir été rapporté comme étant "en vie".

a.s

-9
This file was deleted.

a.s~

-6
This file was deleted.

asm

-105 KB
Binary file not shown.

assembler/Makefile

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
# By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2020/08/20 19:19:55 by ciglesia #+# #+# #
9-
# Updated: 2020/09/15 21:07:48 by ciglesia ### ########.fr #
9+
# Updated: 2020/09/29 18:08:55 by fgarault ### ########.fr #
10+
# Updated: 2020/09/27 17:14:55 by ciglesia ### ########.fr #
1011
# #
1112
#******************************************************************************#
1213

13-
NAME = asm
14+
NAME = ../asm
1415

1516
INC = ./include/
1617
INC2 = ../libft/include/
@@ -27,9 +28,9 @@ DIROP = ./src/op/
2728
DIRVALID = ./src/validation/
2829

2930
SRC = asm_main.c
30-
LOADING = init_structures.c load_file.c add_node.c load_instructions.c
31+
LOADING = init_structures.c load_file.c add_node.c load_instructions.c destructors.c
3132
ANALYSIS = lexer.c parsing.c analysis.c
32-
SYNTHESIS = translation.c
33+
SYNTHESIS = translation.c collect_codebytes.c utils.c collect_addr.c write_binary.c
3334
OP = op.c
3435
VALIDATION = valid_file.c valid_params.c print_err.c
3536

assembler/include/asm.h

+42-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/09/05 22:18:22 by ciglesia #+# #+# */
9-
/* Updated: 2020/09/16 12:27:10 by ciglesia ### ########.fr */
9+
/* Updated: 2020/09/30 17:51:53 by fgarault ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -16,21 +16,35 @@
1616
# include "libft.h"
1717
# include "op.h"
1818

19+
# define MAX_ARG_SIZE 4
20+
21+
typedef struct s_args
22+
{
23+
int reg;
24+
char *dir;
25+
char *ind;
26+
int size;
27+
t_uchar hex[MAX_ARG_SIZE];
28+
struct s_args *next;
29+
} t_args;
30+
1931
typedef struct s_instr
2032
{
33+
int line;
2134
t_uchar opcode;
2235
t_uchar nargs;
2336
t_uchar acb;
24-
int reg;
25-
char *dir;
26-
char *ind;
37+
t_args *args;
38+
int size;
39+
int mem_pos;
2740
struct s_instr *next;
2841
} t_instruction;
2942

3043
typedef struct s_code
3144
{
3245
char *label;
3346
t_instruction *instr;
47+
int mem_pos;
3448
struct s_code *next;
3549
} t_code;
3650

@@ -39,38 +53,56 @@ typedef struct s_file
3953
int fd;
4054
int line;
4155
int quotes;
56+
int prog_size;
57+
int exec_magic;
58+
t_uchar type;
4259
char *name;
4360
char comment[COMMENT_LENGTH];
4461
char playername[PROG_NAME_LENGTH];
45-
unsigned char code[CHAMP_MAX_SIZE];
62+
t_uchar code[CHAMP_MAX_SIZE];
4663
t_code *code_tab;
4764
} t_file;
4865

4966
int translate(t_file *file, int verbosity);
5067

51-
void file_init(t_file *file);
68+
void file_init(t_file *file, char *name);
5269
t_code *new_label(void);
5370
t_instruction *new_instruction(void);
71+
t_args *new_args(void);
72+
73+
int file_destructor(t_file *file);
5474
void fill_header(char *dest, char *inst, int size, int err);
5575
void add_label(t_file *file, char *label);
56-
void add_instruction(t_file *file, char **cmd, int i, int line);
57-
int load_params(char **cmd, int x, int i, t_instruction *instr);
58-
int load_op(char **cmd, int i, int line, t_instruction *instr);
76+
void add_instruction(t_file *file, char **cmd, int i);
77+
void add_arg(t_instruction *instr, int reg, char *dir,
78+
char *ind);
79+
void load_params(char **cmd, int x, int i, t_instruction *instr);
80+
void load_op(char **cmd, int i, t_instruction *instr);
5981

60-
int valid_input(char *filename, t_file *file);
82+
int valid_input(t_file *file);
6183
int valid_reg(char **cmd, int i);
6284
int valid_dir(char **cmd, int i);
6385
int valid_ind(char **cmd, int i);
6486
int valid_params(char **cmd, int x, int i, int line);
87+
int unknown_labels(t_code *code);
6588
int valid_separator(char **cmd, int i, int nargs, int line);
6689
int end_quote(char **cmd, t_file *file, int modif);
6790
int lexicon_error(char **cmd, int i, char *err, int line);
6891
int syntax_error(char **cmd, int i, char *err, int line);
92+
int a_error(char *type, char *err);
6993
int load_error(char **cmd, int i, char *err, int line);
94+
void print_usage(char *str, char *end, char *flag,
95+
char *messflag);
7096

7197
int verify_code(t_file *file, char *line, int l, int s);
7298
int is_head(char **cmd, char *str, int line, unsigned int i);
7399
int is_label(char **cmd);
74100
int is_opcode(char **cmd, int i, int line);
75101

102+
void collecting_codebytes(t_file *file);
103+
void get_addr(t_code *code_tab);
104+
int writing_exec(t_file *file);
105+
106+
void itob(t_uchar *dest, unsigned int nb, int size);
107+
int ft_power(int nb, int power);
76108
#endif

assembler/include/disasm.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* disasm.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/09/22 14:46:23 by ciglesia #+# #+# */
9+
/* Updated: 2020/09/27 18:03:24 by ciglesia ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef DISASM_H
14+
# define DISASM_H
15+
16+
# include "asm.h"
17+
18+
# define REG 1
19+
# define DIR 2
20+
# define IND 3
21+
22+
typedef struct s_w
23+
{
24+
char *name;
25+
t_uchar nb_arg;
26+
t_arg_type args[3];
27+
t_uchar opcode;
28+
unsigned int cycle;
29+
char *description;
30+
t_uchar octal;
31+
t_uchar label;
32+
void (*f)(t_file *file, int *pos);
33+
} t_w;
34+
35+
int valid_header(t_file *file);
36+
37+
int load_exec(t_file *file);
38+
39+
int init_writing(t_file *file);
40+
41+
int disassemble(t_file *file);
42+
int octal_shift(t_uchar acb, t_uchar label_size,
43+
t_uchar narg);
44+
int reverse_bytes(t_uchar *code, unsigned int pc,
45+
int bytes);
46+
int is_argsize(t_uchar ir, t_uchar acb,
47+
t_uchar narg);
48+
int p_acb(t_uchar acb, int p_number);
49+
int is_reg(t_uchar *code, int nb);
50+
51+
void get_arg(t_uchar *code, int pos, t_uchar *move,
52+
t_file *file);
53+
void disect_args(t_uchar *code, int *pos, int l,
54+
t_file *file);
55+
void w_live(t_file *file, int *pos);
56+
void w_ld(t_file *file, int *pos);
57+
void w_st(t_file *file, int *pos);
58+
void w_add(t_file *file, int *pos);
59+
void w_sub(t_file *file, int *pos);
60+
void w_and(t_file *file, int *pos);
61+
void w_or(t_file *file, int *pos);
62+
void w_xor(t_file *file, int *pos);
63+
void w_zjmp(t_file *file, int *pos);
64+
void w_ldi(t_file *file, int *pos);
65+
void w_sti(t_file *file, int *pos);
66+
void w_fork(t_file *file, int *pos);
67+
void w_lld(t_file *file, int *pos);
68+
void w_lldi(t_file *file, int *pos);
69+
void w_lfork(t_file *file, int *pos);
70+
void w_aff(t_file *file, int *pos);
71+
72+
extern t_w g_w_tab[17];
73+
74+
#endif

assembler/src/analysis/analysis.c

+15-12
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
/* By: ciglesia <ciglesia@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/09/07 18:11:34 by ciglesia #+# #+# */
9-
/* Updated: 2020/09/15 21:07:19 by ciglesia ### ########.fr */
9+
/* Updated: 2020/09/25 17:57:14 by ciglesia ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

13-
#include "asm.h"
13+
#include "asm.h"
1414

1515
void free_instruction(char **cmd, char *pitcher, char *line, int fd)
1616
{
@@ -47,31 +47,36 @@ int invalid_line(char **cmd, int line, t_file *file)
4747
int capture_syntax(char **cmd, t_file *file, char *inst, int pos)
4848
{
4949
int err;
50+
int quotes;
5051

5152
err = 0;
52-
if (file->quotes)
53-
end_quote(cmd, file, 0);
54-
if (file->quotes == 1 || (!err && ((err = is_head(cmd, ".name", file->line,
53+
quotes = file->quotes;
54+
(quotes) ? end_quote(cmd, file, 1) : 0;
55+
if (quotes == 1 || (!err && ((err = is_head(cmd, ".name", file->line,
5556
0)) == 1 || err == 2) && !file->playername[0]))
5657
{
5758
fill_header(file->playername, inst, PROG_NAME_LENGTH, err);
58-
file->quotes = (err == 1) ? 1 : 0;
59+
file->quotes = (err == 1) ? 1 : file->quotes;
5960
return (0);
6061
}
61-
if (file->quotes == 2 || (!err && ((err = is_head(cmd, ".comment",
62+
if (quotes == 2 || (!err && ((err = is_head(cmd, ".comment",
6263
file->line, 0)) < 3 && err) && !file->comment[0]))
6364
{
6465
fill_header(file->comment, inst, COMMENT_LENGTH, err);
65-
file->quotes = (err == 1) ? 2 : 0;
66+
file->quotes = (err == 1) ? 2 : file->quotes;
6667
return (0);
6768
}
6869
if ((pos = is_label(cmd)))
6970
add_label(file, cmd[0]);
7071
if (err == 0 && (err = is_opcode(cmd, pos, file->line)) == 1)
71-
add_instruction(file, cmd, pos, file->line);
72+
add_instruction(file, cmd, pos);
7273
return (0);
7374
}
7475

76+
/*
77+
** verif semantics consistency
78+
*/
79+
7580
int verify_code(t_file *file, char *line, int l, int s)
7681
{
7782
char *pitcher;
@@ -96,8 +101,6 @@ int verify_code(t_file *file, char *line, int l, int s)
96101
i++;
97102
free_instruction(cmd, NULL, line, -1);
98103
}
99-
ft_printf(GREEN"NAME: "CYAN"%s\n"E0M, file->playername);
100-
ft_printf(GREEN"COMMENT: "CYAN"%s\n"E0M, file->comment);
101104
free_instruction(NULL, NULL, line, file->fd);
102-
return (EXIT_SUCCESS);// verif semantics consistency
105+
return (unknown_labels(file->code_tab) ? EXIT_FAILURE : EXIT_SUCCESS);
103106
}

0 commit comments

Comments
 (0)