summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-13 21:58:18 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-13 21:58:18 +0530
commitf8c0dc54e36cc201c657aa12e5a22029b092e1bc (patch)
treea0dd1a42c9681be98a7508c128db0dacacefc2e7 /include
parent2e8b78ab0ef01eaacd8094ff839ac93b4451e9c3 (diff)
restructed all the files + uci done now
also broke the symbol analyser in the process, i didn't help much, i had to pull out the pen & paper, but it made a cool graph never the less multithreading was a pain! but it got it in the end fixed the structs naming convention, if it's used outside by other files, it's typedeffed, otherwise it's not fixed the recursive imports issued i had in go_args*, where i was casting it to void* in uci to make it work now response.c doesn't deal with ipc, really happy with that change now every single thing has its own responsibility
Diffstat (limited to 'include')
-rw-r--r--include/engine/moves.h36
-rw-r--r--include/engine/threads.h53
-rw-r--r--include/ipc.h78
-rw-r--r--include/ipc/engine_message.h67
-rw-r--r--include/ipc/go_args.h25
-rw-r--r--include/ipc/moves.h19
-rw-r--r--include/ipc/state.h42
-rw-r--r--include/ipc/threads.h9
-rw-r--r--include/ipc/uci.h9
-rw-r--r--include/uci.h8
-rw-r--r--include/uci/command.h21
-rw-r--r--include/uci/internals/command.h21
-rw-r--r--include/uci/internals/response.h24
-rw-r--r--include/uci/response.h24
-rw-r--r--include/uci/state.h77
15 files changed, 332 insertions, 181 deletions
diff --git a/include/engine/moves.h b/include/engine/moves.h
index 86c4844..4032557 100644
--- a/include/engine/moves.h
+++ b/include/engine/moves.h
@@ -1,8 +1,8 @@
#ifndef MOVES_H
#define MOVES_H
-#include "../ints.h"
-#include "../bitboard.h"
+#include "ints.h"
+#include "bitboard.h"
typedef u8 square_t;
@@ -10,9 +10,9 @@ typedef u8 square_t;
// e4, Nf6, Qh3+, Qe1#, a8=Q, i need to store the information needed to
// recreate this in move_t
enum {
- MOVE_CAPTURE = 1,
- MOVE_SHORT_CASTLE = 1 << 1,
- MOVE_LONG_CASTLE = 1 << 2,
+ MOVE_CAPTURE = 1,
+ MOVE_SHORT_CASTLE = 1 << 1,
+ MOVE_LONG_CASTLE = 1 << 2,
// technically these promotation flags can be compressed to only use 2 bits..
MOVE_PROMOTE_Q = 1 << 3, // 1 << 5
@@ -28,10 +28,10 @@ typedef struct {
square_t to;
u16 flags;
} move_t;
-void position_make_move(position_t* position, move_t* move);
+void position_make_move(position_t *position, move_t *move);
typedef struct {
- move_t* moves;
+ move_t *moves;
u32 length;
u32 capacity;
} moves_t;
@@ -50,32 +50,34 @@ struct add_move_params {
(struct add_move_params) { .flags = 0, __VA_ARGS__ }\
)
void _add_move(
- moves_t* moves,
+ moves_t *moves,
square_t from,
square_t to,
struct add_move_params params
);
-void get_pawn_moves(moves_t* moves, position_t position);
-void get_knight_moves(moves_t* moves, position_t position);
-void get_king_moves(moves_t* moves, position_t position);
-void get_rook_moves(moves_t* moves, position_t position);
-void get_bishop_moves(moves_t* moves, position_t position);
-void get_queen_moves(moves_t* moves, position_t position);
+void get_pawn_moves(moves_t *moves, position_t position);
+void get_knight_moves(moves_t *moves, position_t position);
+void get_king_moves(moves_t *moves, position_t position);
+void get_rook_moves(moves_t *moves, position_t position);
+void get_bishop_moves(moves_t *moves, position_t position);
+void get_queen_moves(moves_t *moves, position_t position);
-void get_moves(moves_t* moves, position_t position);
+void get_moves(moves_t *moves, position_t position);
+#ifdef MOVES_INTERNAL
void __forloop_rook_moves_gen(
- moves_t* moves,
+ moves_t *moves,
bitboard_t friendly_type,
bitboard_t friendly_pieces,
bitboard_t enemy_pieces
);
void __forloop_bishop_moves_gen(
- moves_t* moves,
+ moves_t *moves,
bitboard_t friendly_type,
bitboard_t friendly_pieces,
bitboard_t enemy_pieces
);
+#endif // MOVES_INTERNAL
#endif // !MOVES_H
diff --git a/include/engine/threads.h b/include/engine/threads.h
new file mode 100644
index 0000000..4815a4c
--- /dev/null
+++ b/include/engine/threads.h
@@ -0,0 +1,53 @@
+#ifndef ENGINE_THREADS_H
+#define ENGINE_THREADS_H
+
+#include <pthread.h>
+#include <stdatomic.h>
+
+#include "bitboard.h"
+#include "ipc/engine_message.h"
+#include "ipc/go_args.h"
+
+struct thread_args {
+ // signal sent by threads manager
+ atomic_int stop;
+
+ // signal sent by thread
+ atomic_int finished;
+
+ int id;
+ position_t *position;
+ go_args_t *go_args;
+ engine_message_handler_t *message;
+};
+
+struct threads {
+ pthread_t *threads;
+ struct thread_args *args;
+ size_t count;
+ size_t capacity;
+
+ int running_count;
+ engine_messages_t *engine_messages;
+ go_args_t *go_args;
+ position_t *position;
+};
+
+#ifdef ENGINE_THREADS_INTERNAL
+void __threads_args_init(
+ struct thread_args* args,
+ struct threads *handler,
+ int i
+);
+#endif
+void threads_init(struct threads *handler, size_t capacity);
+void threads_deinit(struct threads *handler);
+void threads_reserve(struct threads *handler, size_t capacity);
+void threads_go(struct threads *handler);
+bool threads_go_loop(struct threads *handler);
+bool threads_all_done(struct threads *handler);
+void threads_cleanup(struct threads *handler);
+
+void *engine_thread(void *args);
+
+#endif // ENGINE_THREADS_H
diff --git a/include/ipc.h b/include/ipc.h
deleted file mode 100644
index 90a2bcc..0000000
--- a/include/ipc.h
+++ /dev/null
@@ -1,78 +0,0 @@
-#ifndef IPC_H
-#define IPC_H
-
-// NOT TRUE IPC: I REALISED I AM STUPID AND COULD HAVE JUST USED THREADS
-
-#include <sys/mman.h>
-#include <stdlib.h>
-
-#include "fen.h"
-#include "uci/state.h"
-
-typedef struct {
- struct uci_move *moves;
- int count;
- int capacity;
-} comm_moves;
-
-struct engine_message {
- int id;
-
- int ready;
-
- int depth;
- int seldepth;
- int multipv;
- int score_cp;
- int nodes;
- int nps;
- int hashfull;
- int tbhits;
- int time;
-
- int mate;
- bool node_limit;
- comm_moves pv;
-
- struct uci_move best_move;
- struct uci_move ponder;
-
- struct engine_message *next;
-};
-
-typedef struct {
- struct engine_message **data;
- int count;
-} engine_messages;
-
-enum { MESSAGE_FILLED, MESSAGE_READ, MESSAGE_PROCESSED };
-typedef struct {
- engine_messages *engine_messages;
- uci_state state;
- int uci_state_initialized;
- int uci_message_ready;
- struct fen_load from_position;
- comm_moves moves;
-} comms;
-
-comm_moves comm_moves_init();
-void add_comm_move(comm_moves *moves, struct uci_move move);
-
-struct go_args {
- comm_moves searchmoves;
- bool ponder;
- int wtime;
- int btime;
- int winc;
- int binc;
- int movestogo;
- int depth;
- int nodes;
- int mate;
- int movetime;
- bool infinite;
- int perft;
-};
-bool should_continue(struct engine_message *message, struct go_args* go);
-
-#endif // IPC_H
diff --git a/include/ipc/engine_message.h b/include/ipc/engine_message.h
new file mode 100644
index 0000000..a651bc2
--- /dev/null
+++ b/include/ipc/engine_message.h
@@ -0,0 +1,67 @@
+#ifndef ENGINE_MESSAGE_H
+#define ENGINE_MESSAGE_H
+
+#include "ipc/moves.h"
+#include <stdatomic.h>
+
+typedef struct engine_message {
+ atomic_int ready;
+
+ int depth;
+ int seldepth;
+ int multipv;
+ int score_cp;
+ int nodes;
+ int nps;
+ int hashfull;
+ int tbhits;
+ int time;
+
+ int mate;
+ bool node_limit;
+ ipc_moves_t pv;
+
+ ipc_move_t best_move;
+ ipc_move_t ponder;
+
+ struct engine_message *next;
+} engine_message_t;
+
+typedef struct {
+ engine_message_t *oldest_valid;
+ engine_message_t *newest_valid;
+ int count;
+
+ // mainly for debugging purposes
+ int popped_count;
+ int pushed_count;
+
+} engine_message_handler_t;
+
+typedef struct {
+ engine_message_handler_t **data;
+ int count;
+ int capacity;
+} engine_messages_t;
+
+void engine_message_handler_init(engine_message_handler_t *handle);
+void engine_message_handler_acquire(engine_message_handler_t *handle);
+void engine_message_handler_release(engine_message_handler_t *handle);
+bool engine_message_handler_can_pop(engine_message_handler_t *handle);
+engine_message_t *engine_message_handler_pop(
+ engine_message_handler_t *handle
+);
+engine_message_t *engine_message_handler_push(
+ engine_message_handler_t *handle
+);
+
+void engine_messages_init(engine_messages_t *messages);
+void engine_messages_deinit(engine_messages_t *messages);
+
+void engine_message_init(engine_message_t *message);
+void engine_messages_refresh(engine_messages_t *messages);
+void engine_message_mark_ready(engine_message_t *message);
+bool engine_message_is_ready(engine_message_t *message);
+void engine_message_deinit(engine_message_t *message);
+
+#endif // ENGINE_MESSAGE_H
diff --git a/include/ipc/go_args.h b/include/ipc/go_args.h
new file mode 100644
index 0000000..7121255
--- /dev/null
+++ b/include/ipc/go_args.h
@@ -0,0 +1,25 @@
+#ifndef GO_ARGS_H
+#define GO_ARGS_H
+
+#include "ipc/moves.h"
+#include "ipc/engine_message.h"
+
+typedef struct {
+ ipc_moves_t searchmoves;
+ bool ponder;
+ int wtime;
+ int btime;
+ int winc;
+ int binc;
+ int movestogo;
+ int depth;
+ int nodes;
+ int mate;
+ int movetime;
+ bool infinite;
+ int perft;
+} go_args_t;
+
+bool should_continue(engine_message_t *message, go_args_t *go);
+
+#endif // GO_ARGS_H
diff --git a/include/ipc/moves.h b/include/ipc/moves.h
new file mode 100644
index 0000000..f08c424
--- /dev/null
+++ b/include/ipc/moves.h
@@ -0,0 +1,19 @@
+#ifndef IPC_MOVES_H
+#define IPC_MOVES_H
+
+typedef struct {
+ int from;
+ int to;
+} ipc_move_t;
+
+typedef struct {
+ ipc_move_t *data;
+ int count;
+ int capacity;
+} ipc_moves_t;
+
+void ipc_moves_init(ipc_moves_t *moves);
+void ipc_moves_deinit(ipc_moves_t *moves);
+void add_ipc_move(ipc_moves_t *moves, ipc_move_t move);
+
+#endif // IPC_MOVES_H
diff --git a/include/ipc/state.h b/include/ipc/state.h
new file mode 100644
index 0000000..6a4455d
--- /dev/null
+++ b/include/ipc/state.h
@@ -0,0 +1,42 @@
+#ifndef IPC_STATE_H
+#define IPC_STATE_H
+
+#include <stdatomic.h>
+
+#include "bitboard.h"
+#include "ipc/moves.h"
+#include "ipc/engine_message.h"
+#include "uci/state.h"
+
+enum {
+ EVENT_INIT,
+ EVENT_STATE_INITIALISED,
+ EVENT_UCI_STATE_INITIALISED,
+ EVENT_UCI_RUNNING,
+ EVENT_UCI_STOP,
+ EVENT_UCI_QUIT,
+ EVENT_ENGINE_GO,
+ EVENT_ENGINE_RUNNING,
+ EVENT_ENGINE_STOP,
+
+ EVENTS_COUNT
+};
+typedef struct {
+ atomic_int event;
+
+ engine_messages_t engine_messages;
+ uci_state_t uci_state;
+
+ // reset on new game
+ position_t from_position;
+ ipc_moves_t moves;
+} ipc_state_t;
+
+void ipc_state_init(ipc_state_t *state);
+void ipc_state_deinit(ipc_state_t *state);
+void ipc_state_game_reset(ipc_state_t *state);
+void ipc_state_wait_for_event(ipc_state_t *state, size_t event);
+bool ipc_state_is_event(ipc_state_t *state, size_t event);
+void ipc_state_set_event(ipc_state_t *state, size_t event);
+
+#endif // IPC_STATE_H
diff --git a/include/ipc/threads.h b/include/ipc/threads.h
new file mode 100644
index 0000000..2d76459
--- /dev/null
+++ b/include/ipc/threads.h
@@ -0,0 +1,9 @@
+#ifndef IPC_THREADS
+#define IPC_THREADS
+
+#include "engine/threads.h"
+#include "ipc/state.h"
+
+void threads_link(struct threads *handler, ipc_state_t *ipc_state);
+
+#endif // IPC_THREADS
diff --git a/include/ipc/uci.h b/include/ipc/uci.h
new file mode 100644
index 0000000..cbe98f2
--- /dev/null
+++ b/include/ipc/uci.h
@@ -0,0 +1,9 @@
+#ifndef UCI_H
+#define UCI_H
+
+#include "ipc/state.h"
+
+void *uci(ipc_state_t *state);
+void handle_ipc(ipc_state_t *state);
+
+#endif // UCI_H
diff --git a/include/uci.h b/include/uci.h
deleted file mode 100644
index b3ee4cc..0000000
--- a/include/uci.h
+++ /dev/null
@@ -1,8 +0,0 @@
-#ifndef UCI_H
-#define UCI_H
-
-#include "ipc.h"
-
-void *uci(void *com);
-
-#endif // UCI_H
diff --git a/include/uci/command.h b/include/uci/command.h
deleted file mode 100644
index ba9c51d..0000000
--- a/include/uci/command.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef UCI_COMMAND_H
-#define UCI_COMMAND_H
-
-#define MAX_TOKEN_SIZE 32
-
-typedef struct {
- char root[MAX_TOKEN_SIZE];
- int args_count;
- int args_capacity;
- char* args;
- bool empty;
-} ucicmd;
-
-ucicmd ucicmd_init();
-void ucicmd_add(ucicmd* cmd, const char* token);
-void ucicmd_set_root(ucicmd* cmd, const char* token);
-void ucicmd_append_arg(ucicmd* cmd, const char* token);
-char* ucicmd_get_arg(ucicmd cmd, int i);
-void ucicmd_deinit(ucicmd cmd);
-
-#endif // UCI_COMMAND_H
diff --git a/include/uci/internals/command.h b/include/uci/internals/command.h
new file mode 100644
index 0000000..2994b04
--- /dev/null
+++ b/include/uci/internals/command.h
@@ -0,0 +1,21 @@
+#ifndef UCI_COMMAND_H
+#define UCI_COMMAND_H
+
+#define MAX_TOKEN_SIZE 32
+
+typedef struct {
+ char root[MAX_TOKEN_SIZE];
+ int args_count;
+ int args_capacity;
+ char* args;
+ bool empty;
+} uci_cmd_t;
+
+uci_cmd_t uci_cmd_init();
+void uci_cmd_add(uci_cmd_t *cmd, const char *token);
+void uci_cmd_set_root(uci_cmd_t *cmd, const char *token);
+void uci_cmd_append_arg(uci_cmd_t *cmd, const char *token);
+char *uci_cmd_get_arg(uci_cmd_t cmd, int i);
+void uci_cmd_deinit(uci_cmd_t cmd);
+
+#endif // UCI_COMMAND_H
diff --git a/include/uci/internals/response.h b/include/uci/internals/response.h
new file mode 100644
index 0000000..a0a6e99
--- /dev/null
+++ b/include/uci/internals/response.h
@@ -0,0 +1,24 @@
+#ifndef UCI_RESPONSE_H
+#define UCI_RESPONSE_H
+
+#include "uci/internals/command.h"
+#include "uci/state.h"
+
+enum {
+ UCI_STATE_INITIAL,
+ UCI_STATE_IDLE,
+ UCI_STATE_SYNC,
+ UCI_STATE_PING,
+ UCI_STATE_ACTIVE,
+ UCI_STATE_HALT,
+};
+
+void handle_uci(uci_state_t *state, uci_cmd_t cmd);
+void handle_initial(uci_state_t *state, uci_cmd_t cmd);
+void handle_idle(uci_state_t *state, uci_cmd_t cmd);
+void handle_sync(uci_state_t *state, uci_cmd_t cmd);
+void handle_ping(uci_state_t *state, uci_cmd_t cmd);
+void handle_active(uci_state_t *state, uci_cmd_t cmd);
+void handle_halt(uci_state_t *state, uci_cmd_t cmd);
+
+#endif // UCI_RESPONSE_H
diff --git a/include/uci/response.h b/include/uci/response.h
deleted file mode 100644
index 320b1f0..0000000
--- a/include/uci/response.h
+++ /dev/null
@@ -1,24 +0,0 @@
-#ifndef RESPONSE_H
-#define RESPONSE_H
-
-#include "command.h"
-#include "state.h"
-#include "../ipc.h"
-
-void handle_uci(
- uci_state *state,
- engine_messages* engine_message,
- ucicmd cmd
-);
-void handle_initial(uci_state *state, ucicmd cmd);
-void handle_idle(uci_state *state, ucicmd cmd);
-void handle_sync(uci_state *state, ucicmd cmd);
-void handle_ping(uci_state *state, ucicmd cmd);
-void handle_active(
- uci_state *state,
- engine_messages* engine_message,
- ucicmd cmd
-);
-void handle_halt(uci_state *state, ucicmd cmd);
-
-#endif // RESPONSE_H
diff --git a/include/uci/state.h b/include/uci/state.h
index a1df891..51207f3 100644
--- a/include/uci/state.h
+++ b/include/uci/state.h
@@ -1,11 +1,12 @@
#ifndef UCI_STATE_H
#define UCI_STATE_H
-#include <stdatomic.h>
-#include "../fen.h"
+#include "ipc/go_args.h"
+#include "ipc/moves.h"
+#include "bitboard.h"
typedef struct {
- char* data;
+ char *data;
int length;
} str_t;
@@ -19,7 +20,7 @@ typedef int combo_t;
typedef bool button_t;
typedef struct {
- char** combination;
+ char **combination;
int count;
int default_index;
} option_combo_setting_t;
@@ -36,7 +37,7 @@ typedef struct {
option_combo_setting_t combo;
option_spin_setting_t spin;
bool check_default;
- const char* string_default;
+ const char *string_default;
} data;
union {
combo_t *combo;
@@ -47,52 +48,63 @@ typedef struct {
} value;
} option_setting_t;
-option_setting_t option_setting_combo(
- char* option_name,
- char** combinations,
+void option_setting_combo(
+ option_setting_t *target,
+ char *option_name,
+ char **combinations,
int combinations_count,
int default_index,
combo_t *combo
);
-option_setting_t option_setting_spin(
- const char* option_name,
+void option_setting_spin(
+ option_setting_t *target,
+ const char *option_name,
int min,
int max,
int default_value,
spin_t *spin
);
-option_setting_t option_setting_check(
- const char* option_name,
+void option_setting_check(
+ option_setting_t *target,
+ const char *option_name,
bool default_value,
check_t *check
);
-option_setting_t option_setting_string(
- const char* option_name,
- const char* default_value,
+void option_setting_string(
+ option_setting_t *target,
+ const char *option_name,
+ const char *default_value,
str_t *string
);
-option_setting_t option_setting_button(
- const char* option_name,
+void option_setting_button(
+ option_setting_t *target,
+ const char *option_name,
button_t *button
);
+void print_setting(option_setting_t setting);
+
+enum {
+ UCI_SIGNAL_NONE,
+ UCI_SIGNAL_QUIT,
+ UCI_SIGNAL_GO,
+ UCI_SIGNAL_GO_LOOP,
+ UCI_SIGNAL_STOP, // game stop, don't stop the program
+
+ UCI_SIGNAL_COUNT
+};
typedef struct {
+ size_t signal;
+ size_t current_state;
+
char name[32];
char author[32];
bool debug;
- atomic_int go;
- atomic_int go_ready_receive;
- atomic_int quit;
- atomic_int stop;
- atomic_int cleanup;
-
- void* go_args; // TODO: fix the information flow so i don't need to do this trick
+ go_args_t go_args;
position_t position;
- struct uci_move *moves;
- int moves_count;
-
- option_setting_t option_settings[11];
+ ipc_moves_t *moves;
+option_setting_t option_settings[11];
// options
spin_t threads;
@@ -112,11 +124,10 @@ typedef struct {
str_t uci_engineabout;
// str_t uci_shredderbasespath;
str_t uci_setpositionvalue;
-} uci_state;
+} uci_state_t;
-struct uci_move {
- int from;
- int to;
-};
+void uci_state_init(uci_state_t *state);
+void uci_state_deinit(uci_state_t *state);
+void apply_option(uci_state_t *state, char *name, char *buffer);
#endif // UCI_STATE_H