Corewar documentation 1.0.0
Loading...
Searching...
No Matches
corewar.h
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** Corewar
4** File description:
5** The header of the corewar project
6*/
12#include "mymemory.h"
13#include "myop.h"
14#include "ncurses.h"
15
16#ifndef COREWAR_H_
17 #define COREWAR_H_
18
19// MACROS :
20
21 #define CHAMPION ((champion_t *)tmp->data)
22
23 #define ARENA_LINE_NUMBER 96
24
25
26// STRUCTURES :
27
32typedef struct cell_s {
33 int address; // The address of the cell
34 unsigned char value; // The value of the cell
35 char *owner; // The last champion who wrote in the cell
37
41typedef struct champion_s {
42 char *file_path; // The path of the file of the champion
43 char *prog_name; // The program name
44 char *comment; // The comment of the program
45 int prog_size; // The program size
46 int prog_number; // The program number
47 int address; // The address of the program in the arena
48 int reg[REG_NUMBER]; // The registers of the program
49 int cooldown; // The cooldown of the program
50 bool alive; // The alive status of the program
51 int program_counter; // The next instruction to execute
52 bool carry; // The carry of the program
54
58typedef struct corewar_s {
59 int nbr_cycle; // The number of cycle to die
60 int nbr_live; // The number of live done
61 int total_cycle; // The total of cycle done
62 int dump_cycle; // The cycle number to dump the memory
63 node_t *champions; // The champions list
64 champion_t *initial_champion[5]; // The initial champion
65 node_t *arena; // The arena
66 bool pause; // The pause status
67 int speed; // The speed for the arena
68 int visual_index; // The visual index for the arena
69 int max_visual_index; // The max visual index for the arena
70 int visual_color; // The visual color for the arena
72
73
74
75// FUNCTIONS PROTOTYPES :
76
77 // Corewar functions :
78
84
89void free_corewar(void);
90
95int corewar(void);
96
97
98 // Parsing functions :
99
106int parse_args(int argc, char **argv);
107
108
109 // Champion functions :
110
116
122int champion_number_exist(int number);
123
128int analyse_champions(void);
129
136
137
138 // Update functions :
139
146void update_program_counter(champion_t *champion, int value);
147
154void update_carry(champion_t *champion, int value);
155
156
157 // Read files functions :
158
166char *read_file(char *filepath, int size, int start);
167
168
169 // Header functions :
170
176int get_prog_size(char *header);
177
183int is_valid_magic(char *header);
184
185
186 // Arena functions :
187
192int build_arena(void);
193
198void display_arena(void);
199
205node_t *get_arena_node(int address);
206
207
208 // State functions :
209
214void check_alive_champions(void);
215
221void display_winner(node_t *champions);
222
229int check_champions_number(node_t *champions);
230
231
232 // Instructions functions :
233
239void execute_instructions(champion_t *champion);
240
247int get_int_from_address(node_t *address, int size);
248
255void write_int_in_address(champion_t *champion, node_t *address, int value);
256
263int read_int_in_address(node_t *address, int size);
264
265
266 // Coding byte functions :
267
274char get_code_byte_index(char coding_byte, int index);
275
282char get_size_from_type(char type, bool max_size);
283
290char get_size_of_coding_byte(char coding_byte, bool max_size);
291
292
293 // All instructions functions :
294
301void exec_add(champion_t *champion, node_t *address);
302
309void exec_aff(champion_t *champion, node_t *address);
310
317void exec_and(champion_t *champion, node_t *address);
318
325void exec_fork(champion_t *champion, node_t *address);
326
333void exec_ld(champion_t *champion, node_t *address);
334
341void exec_ldi(champion_t *champion, node_t *address);
342
349void exec_lfork(champion_t *champion, node_t *address);
350
357void exec_live(champion_t *champion, node_t *address);
358
365void exec_lld(champion_t *champion, node_t *address);
366
373void exec_lldi(champion_t *champion, node_t *address);
374
381void exec_or(champion_t *champion, node_t *address);
382
389void exec_st(champion_t *champion, node_t *address);
390
397void exec_sti(champion_t *champion, node_t *address);
398
405void exec_sub(champion_t *champion, node_t *address);
406
413void exec_xor(champion_t *champion, node_t *address);
414
421void exec_zjmp(champion_t *champion, node_t *address);
422
423
424 // Touch functions :
425
432
439
440
441 // Visual functions :
442
449
456void set_color(int address, char *owner);
457
464
465#endif /* COREWAR_H_ */
char get_size_of_coding_byte(char coding_byte, bool max_size)
Get the size of the coding byte.
Definition coding_byte.c:48
void exec_sti(champion_t *champion, node_t *address)
The sti function.
Definition sti.c:71
void display_corewar(corewar_t *corewar)
Display the corewar.
Definition display.c:191
struct corewar_s corewar_t
The corewar structure.
int analyse_champions(void)
Analyse each champion files and complete the champion structure.
Definition champion.c:72
void exec_st(champion_t *champion, node_t *address)
The st function.
Definition st.c:58
char get_code_byte_index(char coding_byte, int index)
Get the type of byte with the coding byte at the index.
Definition coding_byte.c:20
int read_int_in_address(node_t *address, int size)
Read int in address.
Definition instructions.c:83
void check_alive_champions(void)
Check if the champions are alive.
Definition state.c:18
int get_prog_size(char *header)
Get the program size.
Definition header.c:37
void init_display(corewar_t *corewar)
Initialize the display.
Definition display.c:19
void exec_ldi(champion_t *champion, node_t *address)
The ldi function.
Definition ldi.c:86
void exec_ld(champion_t *champion, node_t *address)
The ld function.
Definition ld.c:57
void update_max_visual_index(corewar_t *corewar)
Update the max visual index.
Definition touch.c:31
int parse_args(int argc, char **argv)
Parse the arguments.
Definition parsing.c:104
void exec_xor(champion_t *champion, node_t *address)
The xor function.
Definition xor.c:86
corewar_t * get_corewar(void)
Get the corewar structure.
Definition corewar.c:18
char get_size_from_type(char type, bool max_size)
Get the size of the type.
Definition coding_byte.c:31
int champion_number_exist(int number)
Check if a champion number exist.
Definition champion.c:41
void execute_instructions(champion_t *champion)
Execute the instructions.
Definition instructions.c:19
struct champion_s champion_t
The champion structure.
struct cell_s cell_t
The cell structure.
void exec_lld(champion_t *champion, node_t *address)
The lld function.
Definition lld.c:57
void exec_lfork(champion_t *champion, node_t *address)
The lfork function.
Definition lfork.c:54
int get_int_from_address(node_t *address, int size)
Get int from address.
Definition instructions.c:49
int is_valid_magic(char *header)
Check if the magic number is valid.
Definition header.c:19
void update_program_counter(champion_t *champion, int value)
Update the program counter of a champion.
Definition update.c:20
void exec_sub(champion_t *champion, node_t *address)
The sub function.
Definition sub.c:20
void exec_lldi(champion_t *champion, node_t *address)
The lldi function.
Definition lldi.c:86
int check_touch(corewar_t *corewar)
Check the touch.
Definition touch.c:68
void set_color(int address, char *owner)
Set the color of the texte.
Definition display.c:48
void exec_fork(champion_t *champion, node_t *address)
The fork function.
Definition fork.c:54
void exec_aff(champion_t *champion, node_t *address)
The aff function.
Definition aff.c:20
int build_arena(void)
Build the arena.
Definition arena.c:40
void exec_and(champion_t *champion, node_t *address)
The and function.
Definition and.c:86
void exec_zjmp(champion_t *champion, node_t *address)
The zjmp function.
Definition zjmp.c:20
void exec_or(champion_t *champion, node_t *address)
The or function.
Definition or.c:86
void free_corewar(void)
Free the corewar structure and all the memory allocated.
Definition corewar.c:38
void exec_live(champion_t *champion, node_t *address)
The live function.
Definition live.c:20
void update_carry(champion_t *champion, int value)
Update the carry of a champion.
Definition update.c:34
node_t * get_arena_node(int address)
Get the arena node from an address.
Definition arena.c:88
int get_next_champion_number(void)
Get the next champion number disponible.
Definition champion.c:18
int corewar(void)
The virtual machine of the corewar.
Definition corewar.c:55
void exec_add(champion_t *champion, node_t *address)
The add function.
Definition add.c:20
void display_winner(node_t *champions)
Display the winner.
Definition state.c:39
char * read_file(char *filepath, int size, int start)
Read a file and return the content.
Definition read_file.c:21
int check_champions_number(node_t *champions)
Check the number of champions.
Definition state.c:58
champion_t * get_champion_by_number(int number)
Get a champion by its number.
Definition champion.c:99
void display_arena(void)
Display the arena.
Definition arena.c:67
void write_int_in_address(champion_t *champion, node_t *address, int value)
Write int in address.
Definition instructions.c:66
The header file of the libmymemory.
The header file of the op.
The cell structure.
Definition corewar.h:32
The champion structure.
Definition corewar.h:41
The corewar structure.
Definition corewar.h:58
Definition mylist.h:43