summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-07 14:30:21 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-07 14:30:21 +0530
commit965bf81879b22a25fd9b0d17ce6defd3d51c6173 (patch)
treeeeadd04afb52e950a3345c7fa4bc51d94446a658 /src
parent3c540db464f4034d0a393b076c79a26851f68f47 (diff)
engine can now send updates to uci
Diffstat (limited to 'src')
-rw-r--r--src/engine.c83
-rw-r--r--src/ipc.c14
-rw-r--r--src/ipc.h15
-rw-r--r--src/uci.c61
-rw-r--r--src/uci.h2
-rw-r--r--src/uci/response.c81
-rw-r--r--src/uci/response.h13
-rw-r--r--src/uci/state.h3
8 files changed, 162 insertions, 110 deletions
diff --git a/src/engine.c b/src/engine.c
index c5843a4..944c0f4 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+#include <pthread.h>
#include "bitboard.h"
#include "engine/moves.h"
@@ -9,61 +10,37 @@
#include "uci.h"
#include "ipc.h"
-// struct sqrstr {
-// char data[2];
-// };
-// struct sqrstr sqr_to_str(int i) {
-// assert(i < 64);
-// return (struct sqrstr) { .data = {(i / 10) + '0', (i % 10) + '0' } };
-// }
-
int main(int argc, char** argv) {
- comms *state = create_shared_memory(sizeof(comms));
- printf("ENGINE: %p\n", state);
- state->uci_message_ready = 69;
- if (fork() == 0) {
- return uci(state);
+ comms *comms = malloc(sizeof(*comms));
+ comms->engine_messages = malloc(sizeof(*comms->engine_messages));
+
+ comms->engine_messages->data = malloc(
+ sizeof(*comms->engine_messages->data) * 1 // TODO: GET FROM UCI SETTINGS
+ );
+ comms->engine_messages->count = 1;
+ comms->engine_messages->data[0] = malloc(sizeof(
+ *comms->engine_messages->data[0]
+ ));
+ comms->engine_messages->data[0]->ready = 0;
+
+ printf("ENGINE: %p\nENGINE MESSAGE: %p\n", comms, comms->engine_messages);
+
+ pthread_t uci_thread;
+ pthread_create(&uci_thread, NULL, &uci, (void*)comms);
+ int k = 0;
+ while (1) {
+ if (k == 0) {
+ struct engine_message *message = comms->engine_messages->data[0];
+ message->depth = 69;
+ message->pv = comm_moves_init();
+ add_comm_move(&message->pv, (struct uci_move) { 14, 24 });
+ message->next = malloc(sizeof(struct engine_message*));
+ printf("\n%p\n", message->pv.moves);
+
+ message->ready = 1;
+ k++;
+ }
}
- while (1) {}
- // while (1) {
- // char* x = read_queue(rx);
- // log_infof("Received: %s", x);
- //
- // moves_t moves = moves_init();
- // struct fen_load result = load_fen(x);
- // free(x);
- //
- // if (result.failed) {
- // log_warnf("Invalid fen");
- // send_queue(tx, "Failed", 7);
- // return 0;
- // }
- // log_infof("Valid fen");
- //
- // position_t position = result.position;
- //
- // get_moves(&moves, position);
- // send_queue(tx, "Sending", 8);
- // char buf[65];
- // int k = 0;
- // for (int i = 0; i < moves.length; i++) {
- // struct sqrstr from_str = sqr_to_str(moves.moves[i].from);
- // struct sqrstr to_str = sqr_to_str(moves.moves[i].to);
- // buf[k++] = from_str.data[0];
- // buf[k++] = from_str.data[1];
- // buf[k++] = to_str.data[0];
- // buf[k++] = to_str.data[1];
- // if (k != 64) continue;
- // buf[k] = 0;
- // send_queue(tx, buf, sizeof(buf));
- // k = 0;
- // }
- // if (k != 0) send_queue(tx, buf, sizeof(buf));
- // char end_transmission[] = "END-TRANSMISSION";
- // int n = strlen(end_transmission);
- // log_infof("%s", end_transmission);
- // send_queue(tx, end_transmission, n + 1);
- // }
return 0;
}
diff --git a/src/ipc.c b/src/ipc.c
index 9c4dfcc..99332ae 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -3,21 +3,9 @@
#include "ipc.h"
-// https://stackoverflow.com/a/5656561
-void* create_shared_memory(size_t size) {
- int protection = PROT_READ | PROT_WRITE;
- int visibility = MAP_SHARED | MAP_ANONYMOUS;
- return mmap(
- NULL,
- size,
- protection, visibility,
- -1, 0
- );
-}
-
comm_moves comm_moves_init() {
return (comm_moves) {
- create_shared_memory(sizeof(struct uci_move) * 50),
+ malloc(sizeof(struct uci_move) * 50),
0,
50
};
diff --git a/src/ipc.h b/src/ipc.h
index 98ce6d3..1c901b8 100644
--- a/src/ipc.h
+++ b/src/ipc.h
@@ -1,14 +1,14 @@
#ifndef IPC_H
#define IPC_H
+// NOT TRUE IPC: I REALISED I AM STUPID AND COULD HAVE JUST USED THREADS
+
#include <stdlib.h>
#include <sys/mman.h>
#include "fen.h"
#include "uci/state.h"
-void* create_shared_memory(size_t size);
-
typedef struct {
struct uci_move *moves;
int count;
@@ -16,6 +16,8 @@ typedef struct {
} comm_moves;
struct engine_message {
+ int ready;
+
int depth;
int seldepth;
int multipv;
@@ -30,9 +32,16 @@ struct engine_message {
struct engine_message *next;
};
+typedef struct {
+ struct engine_message **data;
+ int count;
+} engine_messages;
+
enum { MESSAGE_FILLED, MESSAGE_READ, MESSAGE_PROCESSED };
typedef struct {
- struct engine_message engine_message;
+ engine_messages *engine_messages;
+
+ uci_state state;
int uci_message_ready;
struct fen_load from_position;
diff --git a/src/uci.c b/src/uci.c
index 3bfd8d3..9e3c8c7 100644
--- a/src/uci.c
+++ b/src/uci.c
@@ -34,69 +34,68 @@ void load_from_message(ucicmd* cmd, const char* message) {
ucicmd_add(cmd, token);
}
-int uci(comms *com) {
- printf("%d\n", com->uci_message_ready);
-
+void *uci(void *_com) {
+ comms *com = (comms*)_com;
// https://stackoverflow.com/a/41559081
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
- uci_state state = {0};
- strncpy(state.name, "Gacrux", 32);
- strncpy(state.author, "Aargh Rai <aarghrai.com>", 32);
+ com->state = (uci_state) {0};
+ strncpy(com->state.name, "Gacrux", 32);
+ strncpy(com->state.author, "Aargh Rai <aarghrai.com>", 32);
int k = 0;
- state.option_settings[k++] = option_setting_spin(
+ com->state.option_settings[k++] = option_setting_spin(
"Threads",
1, 5, 1,
- &state.threads
+ &com->state.threads
);
- state.option_settings[k++] = option_setting_spin(
+ com->state.option_settings[k++] = option_setting_spin(
"Hash",
0, 512, 256,
- &state.hash
+ &com->state.hash
);
- state.option_settings[k++] = option_setting_button(
+ com->state.option_settings[k++] = option_setting_button(
"Clear Hash",
- &state.clear_hash
+ &com->state.clear_hash
);
- state.option_settings[k++] = option_setting_check(
+ com->state.option_settings[k++] = option_setting_check(
"UCI_ShowCurrLine",
false,
- &state.uci_showcurrline
+ &com->state.uci_showcurrline
);
- state.option_settings[k++] = option_setting_check(
+ com->state.option_settings[k++] = option_setting_check(
"UCI_ShowRefutations",
false,
- &state.uci_showrefutations
+ &com->state.uci_showrefutations
);
- state.option_settings[k++] = option_setting_check(
+ com->state.option_settings[k++] = option_setting_check(
"UCI_LimitStrength",
false,
- &state.uci_limitstrength
+ &com->state.uci_limitstrength
);
- state.option_settings[k++] = option_setting_spin(
+ com->state.option_settings[k++] = option_setting_spin(
"UCI_Elo",
100, 3500, 800,
- &state.uci_elo
+ &com->state.uci_elo
);
- state.option_settings[k++] = option_setting_check(
+ com->state.option_settings[k++] = option_setting_check(
"UCI_AnalyseMode",
false,
- &state.uci_analysemode
+ &com->state.uci_analysemode
);
- state.option_settings[k++] = option_setting_string(
+ com->state.option_settings[k++] = option_setting_string(
"UCI_Opponent",
"<empty>",
- &state.uci_opponent
+ &com->state.uci_opponent
);
- state.option_settings[k++] = option_setting_string(
+ com->state.option_settings[k++] = option_setting_string(
"UCI_EngineAbout",
"Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux",
- &state.uci_engineabout
+ &com->state.uci_engineabout
);
- state.option_settings[k++] = option_setting_string(
+ com->state.option_settings[k++] = option_setting_string(
"UCI_SetPositionValue",
"<empty>",
- &state.uci_setpositionvalue
+ &com->state.uci_setpositionvalue
);
ucicmd cmd = ucicmd_init();
@@ -110,7 +109,11 @@ int uci(comms *com) {
if (n > 0) {
load_from_message(&cmd, buf);
}
- handle_uci(&state, cmd);
+ handle_uci(
+ &com->state,
+ com->engine_messages,
+ cmd
+ );
}
}
diff --git a/src/uci.h b/src/uci.h
index 426ff77..b3ee4cc 100644
--- a/src/uci.h
+++ b/src/uci.h
@@ -3,6 +3,6 @@
#include "ipc.h"
-int uci(comms *com);
+void *uci(void *com);
#endif // UCI_H
diff --git a/src/uci/response.c b/src/uci/response.c
index 83a8ee1..dbf4614 100644
--- a/src/uci/response.c
+++ b/src/uci/response.c
@@ -84,7 +84,11 @@ enum {
};
int current_state = STATE_INITIAL;
-void handle_uci(uci_state *state, ucicmd cmd) {
+void handle_uci(
+ uci_state *state,
+ engine_messages *engine_message,
+ ucicmd cmd
+) {
if (current_state == STATE_INITIAL) {
return handle_initial(state, cmd);
}
@@ -98,7 +102,7 @@ void handle_uci(uci_state *state, ucicmd cmd) {
return handle_ping(state, cmd);
}
if (current_state == STATE_ACTIVE) {
- return handle_active(state, cmd);
+ return handle_active(state, engine_message, cmd);
}
return handle_halt(state, cmd);
}
@@ -117,7 +121,9 @@ void handle_initial(uci_state *state, ucicmd cmd) {
current_state = STATE_IDLE;
}
-void handle_idle(uci_state *state, ucicmd cmd) {
+void handle_idle(
+ uci_state *state,
+ ucicmd cmd) {
if (cmd.empty) return;
if (strcmp(cmd.root, "debug") == 0) {
state->debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 0;
@@ -139,7 +145,11 @@ void handle_idle(uci_state *state, ucicmd cmd) {
*(--cursor) = 0;
apply_option(state, name, buffer);
} else if (strcmp(cmd.root, "ucinewgame") == 0) {
- // clear game state here
+ if (state->moves != NULL) {
+ free(state->moves);
+ state->moves = NULL;
+ state->moves_count = 0;
+ }
} else if (strcmp(cmd.root, "position") == 0) {
int k = 0;
char* mode = ucicmd_get_arg(cmd, 0);
@@ -152,16 +162,30 @@ void handle_idle(uci_state *state, ucicmd cmd) {
if (cmd.args_count >= k) return;
assert(strcmp(ucicmd_get_arg(cmd, k++), "moves") == 0);
- struct uci_move *moves = malloc((cmd.args_count - k) * sizeof(struct uci_move));
+ assert(state->moves_count <= cmd.args_count - k);
+ int old_move_count = state->moves_count;
+ state->moves_count = cmd.args_count - k;
+ if (state->moves == NULL) {
+ state->moves = malloc(
+ state->moves_count * sizeof(struct uci_move)
+ );
+ } else {
+ state->moves = realloc(
+ state->moves,
+ state->moves_count * sizeof(struct uci_move)
+ );
+ }
int l = 0;
+ k += old_move_count;
+ l += old_move_count;
for (; k < cmd.args_count; k++) {
char* move = ucicmd_get_arg(cmd, k);
- moves[l++] = (struct uci_move) {
+ state->moves[l++] = (struct uci_move) {
(move[0] - 'a') * 8 + (move[1] - '1'),
(move[2] - 'a') * 8 + (move[3] - '1')
};
}
- // SENT state->position & struct uci_move to ENGINE
+ state->go = 1;
} else if (strcmp(cmd.root, "stop") == 0) {
} else if (strcmp(cmd.root, "quit") == 0) {
free(cmd.args);
@@ -206,11 +230,50 @@ void handle_ping(uci_state *state, ucicmd cmd) {
current_state = STATE_ACTIVE;
}
-void handle_active(uci_state *state, ucicmd cmd) {
+void handle_active(
+ uci_state *state,
+ engine_messages* engine_messages,
+ ucicmd cmd
+) {
+ for (int i = 0; i < engine_messages->count; i++) {
+ struct engine_message *old = engine_messages->data[i];
+ if (!old->ready) continue;
+ printf("INITIALIZED\n");
+ old->ready = 0;
+
+ printf(
+ "info depth %d seldepth %d multipv %d score cp %d nodes %d nps %d hashfull %d tbhits %d time %d pv",
+ old->depth,
+ old->seldepth,
+ old->multipv,
+ old->score_cp,
+ old->nodes,
+ old->nps,
+ old->hashfull,
+ old->tbhits,
+ old->time
+ );
+
+ for (int j = 0; j < old->pv.count; j++) {
+ struct uci_move move = old->pv.moves[i];
+ printf(
+ " %c%c%c%c",
+ (move.from / 8) + 'a',
+ (move.from % 8) + '1',
+ (move.to / 8) + 'a',
+ (move.to % 8) + '1'
+ );
+ }
+ printf("\n");
+ // // info depth 1 seldepth 1 multipv 1 score cp 18 nodes 20 nps 4000 hashfull 0 tbhits 0 time 5 pv e2e4
+ //
+ // engine_messages->data[i] = old->next;
+ // free(old);
+ }
+ // TODO: this section will call the engine to analysis
if (cmd.empty) {
return;
}
- // TODO: this section will call the engine to analysis
if (strcmp(cmd.root, "isready") == 0) {
current_state = STATE_PING;
} else if (strcmp(cmd.root, "stop") == 0) {
diff --git a/src/uci/response.h b/src/uci/response.h
index 17204f5..16652d2 100644
--- a/src/uci/response.h
+++ b/src/uci/response.h
@@ -1,10 +1,19 @@
#include "command.h"
#include "state.h"
+#include "../ipc.h"
-void handle_uci(uci_state *state, ucicmd cmd);
+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, ucicmd cmd);
+void handle_active(
+ uci_state *state,
+ engine_messages* engine_message,
+ ucicmd cmd
+);
void handle_halt(uci_state *state, ucicmd cmd);
diff --git a/src/uci/state.h b/src/uci/state.h
index 849a08a..9b09774 100644
--- a/src/uci/state.h
+++ b/src/uci/state.h
@@ -80,7 +80,10 @@ typedef struct {
char author[32];
bool debug;
+ int go;
struct fen_load position;
+ struct uci_move *moves;
+ int moves_count;
option_setting_t option_settings[11];