summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.c15
-rw-r--r--src/bitboard.c (renamed from src/engine/bitboard.c)0
-rw-r--r--src/bitboard.h (renamed from src/engine/bitboard.h)0
-rw-r--r--src/engine.c112
-rw-r--r--src/engine/moves.c2
-rw-r--r--src/engine/moves.h4
-rw-r--r--src/fen.c (renamed from src/engine/fen.c)1
-rw-r--r--src/fen.h (renamed from src/engine/fen.h)0
-rw-r--r--src/ints.h (renamed from src/engine/ints.h)0
-rw-r--r--src/ipc.c133
-rw-r--r--src/ipc.h45
-rw-r--r--src/uci.c8
-rw-r--r--src/uci.h8
-rw-r--r--src/uci/response.c6
-rw-r--r--src/uci/state.h2
15 files changed, 156 insertions, 180 deletions
diff --git a/build.c b/build.c
index 663d47c..5d64333 100644
--- a/build.c
+++ b/build.c
@@ -7,6 +7,10 @@
#include <string.h>
#define CC "gcc", "-O3"
+#define ENGINE_O "output/engine.o"
+#define UCI_O "output/uci.o"
+#define MAIN_O "output/main.o"
+#define IPC_O "output/ipc.o"
int status;
@@ -51,9 +55,6 @@ int needs_rebuild(char* bin_file, char* source_file) {
return input_path_time > output_path_time;
}
-#define ENGINE_O "output/engine.o"
-#define UCI_O "output/uci.o"
-
int main(int argc, char** argv) {
if (needs_rebuild(argv[0], __FILE__)) {
RUN_CMD({CC, __FILE__, "-o", argv[0], NULL});
@@ -102,12 +103,14 @@ int test_mode(int run_mode) {
int uci_mode(int run_mode) {
if (run_mode) {
- char* args[] = {UCI_O, NULL};
+ char* args[] = {MAIN_O, NULL};
print_cmd(args);
execvp(args[0], args);
}
- RUN_CMD({CC, "src/engine.c", "-o", ENGINE_O, NULL});
- RUN_CMD({CC, "src/uci.c", "-o", UCI_O, ENGINE_O, NULL});
+ RUN_CMD({CC, "-c", "src/ipc.c", "-o", IPC_O, NULL});
+ RUN_CMD({CC, "-c", "src/engine.c", "-o", ENGINE_O, NULL});
+ RUN_CMD({CC, "-c", "src/uci.c", "-o", UCI_O, NULL});
+ RUN_CMD({CC, UCI_O, ENGINE_O, IPC_O, "-o", MAIN_O, NULL});
return status != 0;
}
diff --git a/src/engine/bitboard.c b/src/bitboard.c
index 565f425..565f425 100644
--- a/src/engine/bitboard.c
+++ b/src/bitboard.c
diff --git a/src/engine/bitboard.h b/src/bitboard.h
index ecd8c35..ecd8c35 100644
--- a/src/engine/bitboard.h
+++ b/src/bitboard.h
diff --git a/src/engine.c b/src/engine.c
index 3c64cce..c5843a4 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -1,63 +1,71 @@
-#include <assert.h>
+#include <stdio.h>
#include <string.h>
-#include "engine/bitboard.h"
+#include <unistd.h>
+
+#include "bitboard.h"
#include "engine/moves.h"
-#include "engine/fen.h"
+#include "fen.h"
#include "fcntl.h"
-#include "ipc.c"
-
-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(void) {
- 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");
+#include "uci.h"
+#include "ipc.h"
- position_t position = result.position;
+// 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' } };
+// }
- 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);
+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);
}
+ 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;
}
-#include "engine/bitboard.c"
#include "engine/moves.c"
-#include "engine/fen.c"
diff --git a/src/engine/moves.c b/src/engine/moves.c
index d338cc2..797e770 100644
--- a/src/engine/moves.c
+++ b/src/engine/moves.c
@@ -1,5 +1,5 @@
#include "moves.h"
-#include "bitboard.h"
+#include "../bitboard.h"
#include "moves/vec.c"
#include "moves/king.c"
#include "moves/knight.c"
diff --git a/src/engine/moves.h b/src/engine/moves.h
index bd88eeb..86c4844 100644
--- a/src/engine/moves.h
+++ b/src/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;
diff --git a/src/engine/fen.c b/src/fen.c
index aa258c9..cdbee49 100644
--- a/src/engine/fen.c
+++ b/src/fen.c
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdbool.h>
+
#include "fen.h"
#include "bitboard.h"
diff --git a/src/engine/fen.h b/src/fen.h
index d45240e..d45240e 100644
--- a/src/engine/fen.h
+++ b/src/fen.h
diff --git a/src/engine/ints.h b/src/ints.h
index 4a0cfe3..4a0cfe3 100644
--- a/src/engine/ints.h
+++ b/src/ints.h
diff --git a/src/ipc.c b/src/ipc.c
index d7ef968..9c4dfcc 100644
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -1,133 +1,40 @@
#ifndef IPC_C
#define IPC_C
-#include <stdlib.h>
-#include <sys/mman.h>
-
-#include "engine/fen.h"
-
-typedef struct {
- struct uci_move *moves;
- int count;
- int capacity;
-} comm_moves;
-
-void add_comm_move(comm_moves *moves, struct uci_move move) {
- if (moves->count + 1 > moves->capacity) {
- moves->capacity += 50;
- moves->moves = realloc(moves->moves, moves->capacity * sizeof(struct uci_move));
- }
- moves->moves[moves->count++] = move;
-}
-
-typedef struct {
- bool engine_message_ready;
- int depth;
- int seldepth;
- int multipv;
- int score_cp;
- int nodes;
- int nps;
- int hashfull;
- int tbhits;
- int time;
- comm_moves pv;
-
- bool uci_message_ready;
- struct fen_load from_position;
- comm_moves moves;
-} comms;
+#include "ipc.h"
// https://stackoverflow.com/a/5656561
-comms* create_shared_memory() {
+void* create_shared_memory(size_t size) {
int protection = PROT_READ | PROT_WRITE;
int visibility = MAP_SHARED | MAP_ANONYMOUS;
- comms* state = (comms*)mmap(
+ return mmap(
NULL,
- sizeof(comms),
+ size,
protection, visibility,
-1, 0
);
- state->engine_message_ready = false;
- state->depth = 0;
- state->seldepth = 0;
- state->multipv = 0;
- state->score_cp = 0;
- state->nodes = 0;
- state->nps = 0;
- state->hashfull = 0;
- state->tbhits = 0;
- state->time = 0;
- state->pv = (comm_moves) {
- (comms*)mmap(
- NULL,
- sizeof(struct uci_move) * 50,
- protection, visibility,
- -1, 0
- ), 0, 50
- };
- state->uci_message_ready = false;
- state->from_position = {0};
- state->moves = (comm_moves) {
- (comms*)mmap(
- NULL,
- sizeof(struct uci_move) * 50,
- protection, visibility,
- -1, 0
- ), 0, 50
- };
-
- return state;
-}
-
-void send_uci_message(
- comms *comms,
- struct fen_load from_position
-) {
- comms->from_position = from_position;
- comms->uci_message_ready = true;
-}
-
-void send_engine_message(
- comms *comms,
- int depth,
- int seldepth,
- int multipv,
- int score_cp,
- int nodes,
- int nps,
- int hashfull,
- int tbhits,
- int time
-) {
- comms->depth = depth;
- comms->seldepth = seldepth;
- comms->multipv = multipv;
- comms->score_cp = score_cp;
- comms->nodes = nodes;
- comms->nps = nps;
- comms->hashfull = hashfull;
- comms->tbhits = tbhits;
- comms->time = time;
- comms->engine_message_ready = true;
}
-bool receive_uci_message(comms *comms) {
- if (comms->uci_message_ready) {
- comms->uci_message_ready = false;
- return true;
- }
- return false;
+comm_moves comm_moves_init() {
+ return (comm_moves) {
+ create_shared_memory(sizeof(struct uci_move) * 50),
+ 0,
+ 50
+ };
}
-// TODO: think about dealing with multiple engine messages
-bool receive_engine_message(comms *comms) {
- if (comms->engine_message_ready) {
- comms->engine_message_ready = false;
- return true;
+void add_comm_move(comm_moves *moves, struct uci_move move) {
+ if (moves->count + 1 > moves->capacity) {
+ moves->capacity += 50;
+ moves->moves = realloc(
+ moves->moves,
+ moves->capacity * sizeof(struct uci_move)
+ );
}
- return false;
+ moves->moves[moves->count++] = move;
}
+#include "fen.c"
+#include "bitboard.c"
#endif // IPC_C
diff --git a/src/ipc.h b/src/ipc.h
new file mode 100644
index 0000000..98ce6d3
--- /dev/null
+++ b/src/ipc.h
@@ -0,0 +1,45 @@
+#ifndef IPC_H
+#define IPC_H
+
+#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;
+ int capacity;
+} comm_moves;
+
+struct engine_message {
+ int depth;
+ int seldepth;
+ int multipv;
+ int score_cp;
+ int nodes;
+ int nps;
+ int hashfull;
+ int tbhits;
+ int time;
+ comm_moves pv;
+
+ struct engine_message *next;
+};
+
+enum { MESSAGE_FILLED, MESSAGE_READ, MESSAGE_PROCESSED };
+typedef struct {
+ struct engine_message engine_message;
+
+ 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);
+
+#endif // IPC_H
diff --git a/src/uci.c b/src/uci.c
index a31411f..3bfd8d3 100644
--- a/src/uci.c
+++ b/src/uci.c
@@ -4,11 +4,13 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
-#include <errno.h>
+#include <assert.h>
#include "uci/state.h"
#include "uci/command.h"
#include "uci/response.h"
+#include "uci.h"
+#include "ipc.h"
void load_from_message(ucicmd* cmd, const char* message) {
char token[MAX_TOKEN_SIZE];
@@ -32,7 +34,9 @@ void load_from_message(ucicmd* cmd, const char* message) {
ucicmd_add(cmd, token);
}
-int main(int argc, char** argv) {
+int uci(comms *com) {
+ printf("%d\n", com->uci_message_ready);
+
// https://stackoverflow.com/a/41559081
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
diff --git a/src/uci.h b/src/uci.h
new file mode 100644
index 0000000..426ff77
--- /dev/null
+++ b/src/uci.h
@@ -0,0 +1,8 @@
+#ifndef UCI_H
+#define UCI_H
+
+#include "ipc.h"
+
+int uci(comms *com);
+
+#endif // UCI_H
diff --git a/src/uci/response.c b/src/uci/response.c
index a7e53fd..83a8ee1 100644
--- a/src/uci/response.c
+++ b/src/uci/response.c
@@ -3,8 +3,8 @@
#include <stdlib.h>
#include "response.h"
-#include "fen.h"
-#include "bitboard.h"
+#include "../fen.h"
+#include "../bitboard.h"
void apply_option(uci_state *state, char *name, char *buffer) {
int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
@@ -151,7 +151,7 @@ void handle_idle(uci_state *state, ucicmd cmd) {
} else { assert(0); }
if (cmd.args_count >= k) return;
- assert(ucicmd_get_arg(cmd, k++), "moves") == 0);
+ assert(strcmp(ucicmd_get_arg(cmd, k++), "moves") == 0);
struct uci_move *moves = malloc((cmd.args_count - k) * sizeof(struct uci_move));
int l = 0;
for (; k < cmd.args_count; k++) {
diff --git a/src/uci/state.h b/src/uci/state.h
index cb0d63f..849a08a 100644
--- a/src/uci/state.h
+++ b/src/uci/state.h
@@ -1,7 +1,7 @@
#ifndef UCI_STATE_H
#define UCI_STATE_H
-#include "engine/fen.h"
+#include "../fen.h"
typedef struct {
char* data;