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
24
25// STRUCTURES :
26
31typedef struct cell_s {
32 int address; // The address of the cell
33 unsigned char value; // The value of the cell
34 char *owner; // The last champion who wrote in the cell
36
40typedef struct champion_s {
41 char *file_path; // The path of the file of the champion
42 char *prog_name; // The program name
43 char *comment; // The comment of the program
44 int prog_size; // The program size
45 int prog_number; // The program number
46 int address; // The address of the program in the arena
47 int reg[REG_NUMBER]; // The registers of the program
48 int cooldown; // The cooldown of the program
49 bool alive; // The alive status of the program
50 int program_counter; // The next instruction to execute
51 bool carry; // The carry of the program
53
57typedef struct corewar_s {
58 int nbr_cycle; // The number of cycle to die
59 int nbr_live; // The number of live done
60 int total_cycle; // The total of cycle done
61 int dump_cycle; // The cycle number to dump the memory
62 node_t *champions; // The champions list
63 champion_t *initial_champion[5]; // The initial champion
64 node_t *arena; // The arena
66
67
68
69// FUNCTIONS PROTOTYPES :
70
71 // Corewar functions :
72
78
83void free_corewar(void);
84
89int corewar(void);
90
91
92 // Parsing functions :
93
100int parse_args(int argc, char **argv);
101
102
103 // Champion functions :
104
110
116int champion_number_exist(int number);
117
122int analyse_champions(void);
123
130
131
132 // Update functions :
133
140void update_program_counter(champion_t *champion, int value);
141
148void update_carry(champion_t *champion, int value);
149
150
151 // Read files functions :
152
160char *read_file(char *filepath, int size, int start);
161
162
163 // Header functions :
164
170int get_prog_size(char *header);
171
177int is_valid_magic(char *header);
178
179
180 // Arena functions :
181
186int build_arena(void);
187
192void display_arena(void);
193
199node_t *get_arena_node(int address);
200
201
202 // State functions :
203
208void check_alive_champions(void);
209
215void display_winner(node_t *champions);
216
223int check_champions_number(node_t *champions);
224
225
226 // Instructions functions :
227
233void execute_instructions(champion_t *champion);
234
241int get_int_from_address(node_t *address, int size);
242
249void write_int_in_address(champion_t *champion, node_t *address, int value);
250
257int read_int_in_address(node_t *address, int size);
258
259
260 // Coding byte functions :
261
268char get_code_byte_index(char coding_byte, int index);
269
276char get_size_from_type(char type, bool max_size);
277
284char get_size_of_coding_byte(char coding_byte, bool max_size);
285
286
287 // All instructions functions :
288
295void exec_add(champion_t *champion, node_t *address);
296
303void exec_aff(champion_t *champion, node_t *address);
304
311void exec_and(champion_t *champion, node_t *address);
312
319void exec_fork(champion_t *champion, node_t *address);
320
327void exec_ld(champion_t *champion, node_t *address);
328
335void exec_ldi(champion_t *champion, node_t *address);
336
343void exec_lfork(champion_t *champion, node_t *address);
344
351void exec_live(champion_t *champion, node_t *address);
352
359void exec_lld(champion_t *champion, node_t *address);
360
367void exec_lldi(champion_t *champion, node_t *address);
368
375void exec_or(champion_t *champion, node_t *address);
376
383void exec_st(champion_t *champion, node_t *address);
384
391void exec_sti(champion_t *champion, node_t *address);
392
399void exec_sub(champion_t *champion, node_t *address);
400
407void exec_xor(champion_t *champion, node_t *address);
408
415void exec_zjmp(champion_t *champion, node_t *address);
416
417
418
419#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
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 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
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
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