Implement basic UCI framework

This commit is contained in:
Quinten Kock 2020-06-19 17:20:13 +02:00
parent 11dacef42b
commit db63b54764
2 changed files with 80 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include "Move.h"
#include "Movegen.h"
#include "Types.h"
#include "Uci.h"
unsigned long pseudo_perft(byte depth) {
// only checks pseudolegality
@ -182,4 +183,5 @@ void setup() {
void loop() {
// put your main code here, to run repeatedly:
handle_uci();
}

78
Uci.h Normal file
View File

@ -0,0 +1,78 @@
#ifndef __UCI_H_INC
#define __UCI_H_INC
typedef void uci_return;
typedef uci_return (*uci_handler)();
struct uci_cmd {
char* command;
uci_handler handler;
};
uci_return uci_hello() {
Serial.println(F("id name ArduChess\nid author Quinten Kock\nuciok"));
}
uci_return uci_unimpl() {
Serial.println(F("Function not implemented yet"));
}
uci_return uci_unknown() {
Serial.println(F("Not an UCI command"));
}
const uci_cmd UCI_COMMANDS[] = {
{"uci", &uci_hello},
{"debug", &uci_unimpl},
{"isready", &uci_unimpl},
{"setoption", &uci_unimpl},
{"ucinewgame", &uci_unimpl},
{"position", &uci_unimpl},
{"go", &uci_unimpl},
{"stop", &uci_unimpl},
{"ponderhit", &uci_unimpl},
{"quit", &uci_unimpl},
};
const uci_cmd UCI_INVALID = {"", &uci_unknown};
uci_cmd get_uci_command(char* command) {
size_t command_num = sizeof(UCI_COMMANDS) / sizeof(uci_cmd);
for(int i = 0; i < command_num; i++) {
int ci = 0;
uci_cmd to_try = UCI_COMMANDS[i];
while(true) {
if(to_try.command[ci] != command[ci]) {
break;
}
if(command[ci] == '\0') {
return to_try;
}
ci++;
}
}
return UCI_INVALID;
}
String read_word() {
int incoming = Serial.read();
String str = String();
do {
if(incoming != -1) {
str += (char)incoming;
}
incoming = Serial.read();
} while(incoming != '\n' && incoming != ' ');
return str;
}
uci_return handle_uci() {
if(Serial.available()) {
// There is input available; so likely a command
String command = read_word();
uci_cmd handler = get_uci_command(command.c_str());
handler.handler();
}
}
#endif