Skip to content

Commit

Permalink
implement basic game func
Browse files Browse the repository at this point in the history
  • Loading branch information
Alx-Lai authored and john0312 committed Jul 31, 2024
1 parent 14fccf7 commit bdf9ebb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
25 changes: 21 additions & 4 deletions fw/Core/Hitcon/Logic/game.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "game.h"

#include <Logic/NvStorage.h>
#include <Logic/RandomPool.h>

namespace hitcon {
namespace game {

GameQueue game_queue;

// Implements the ln() with approximate polynomial.
// The output is in Q9.22 fixed point number, while the input is in
// uint32_t integer format.
Expand All @@ -15,15 +18,26 @@ int32_t q22_ln(uint32_t) {
game_cache_t game_cache;

grid_cell_t random_grid() {
// TODO
return (grid_cell_t){0};
grid_cell_t cell;
for (uint8_t i = 0; i < kDataSize; i++)
cell.data[i] = g_fast_random_pool.GetRandom();
return cell;
}

score_t grid_score(const grid_cell_t *grid) {
// TODO
// Note: This probably has to be reimplemented because it spans multiple
// tasks.
return 0;
sha3_context priv;

// TODO: split into tasks
if (sha3_Init256(&priv, 256) != SHA3_RETURN_OK) return 0;
// TODO: update with col key
// sha3_UpdateWord();
for (int i = 0; i < kDataSize; i++) sha3_UpdateWord(priv, grid->data[i]);

// TODO: return what??
return q22_ln(priv);
}

static void init_game_storage() {
Expand Down Expand Up @@ -82,7 +96,10 @@ void __game_receive_and_update_column(int column, void *event_data) {
// TODO
}

void game_accept_data(int col, uint8_t *data) {}
void game_accept_data(int col, uint8_t *data) {
// TODO: schedule task deal with accepting data
game_queue.push(col, data);
}
bool get_random_cell_data_for_ir_transmission(uint8_t *out_data, int *out_col) {
return true;
}
Expand Down
28 changes: 28 additions & 0 deletions fw/Core/Hitcon/Logic/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ uint8_t* get_data_cell(int col, int row);
// Get a random data cell for sending over IR. The data is written to out_data, while the column is written to out_col.
bool get_random_cell_data_for_ir_transmission(uint8_t* out_data, int* out_col);

struct GameQueue {
std::pair<int, grid_cell_t> datas[kQueueSize];
// [start, end)
uint8_t queue_start = 0;
uint8_t queue_end = 0;
uint8_t size() {
return (queue_end - queue_start) > 0 ?
(queue_end - queue_start) :
(queue_end - queue_start + kQueueSize);
}
bool push(int col, grid_cell_t data) {
if (size() == 0) return false;
data[queue_end++] = std::make_pair(col, data);
if(queue_end == kQueueSize)
queue_end = 0;
return true;
}
std::pair<int, grid_cell_t> pop() {
if (size() == 0) return (grid_cell_t)0;
std::pair<int, grid_cell_t> ret = data[queue_start--];
if (queue_start < 0)
queue_start += kQueueSize;
return ret;
}
};

extern GameQueue game_queue;

} // namespace game
} // namespace hitcon

Expand Down
3 changes: 3 additions & 0 deletions fw/Core/Hitcon/Logic/game_param.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ constexpr size_t IrAllowBroadcastCnt = sizeof(IrAllowedBroadcastCol)/sizeof(IrAl
constexpr int InternalGenCol[] = {0, 1, 2};
constexpr size_t InternalGenColCnt = sizeof(InternalGenCol)/sizeof(InternalGenCol[0]);

// circular queue size
constexpr size_t kQueueSize = 32;

} // namespace game
} // namespace hitcon

Expand Down

0 comments on commit bdf9ebb

Please sign in to comment.