Skip to content

Commit

Permalink
Add transfer to RFC call
Browse files Browse the repository at this point in the history
  • Loading branch information
tolauwae committed Feb 9, 2025
1 parent b42ae4b commit 7d57709
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ if (BUILD_EMULATOR)
src/Oop/proxy.cpp
src/Oop/proxy_supervisor.cpp
src/Oop/RFC.cpp
src/Oop/stateful.cpp
)

add_definitions(-DINFO=0)
Expand Down Expand Up @@ -98,6 +99,7 @@ if (BUILD_UNITTEST)
src/Oop/proxy.cpp
src/Oop/proxy_supervisor.cpp
src/Oop/RFC.cpp
src/Oop/stateful.cpp
)

# Set default compile flags for GCC
Expand Down
14 changes: 14 additions & 0 deletions src/Oop/RFC.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
#include "RFC.h"

#include <cstdlib>
#include <cstring>

RFC::RFC(Module *m, uint32_t id, Type *t_type, StackValue *t_args)
: m(m), fidx(id), args(t_args), type(t_type) {}

SerializeData *merge(SerializeData a, SerializeData b) {
auto data = new SerializeData;
data->size = a.size + b.size + 2;
data->raw = (unsigned char *)calloc(sizeof(char), data->size);
std::memcpy(data->raw, a.raw, a.size);
*(data->raw + a.size) = '\n';
std::memcpy(data->raw + a.size + 1, b.raw, b.size);
*(data->raw + a.size + b.size + 1) = '\n';
return data;
}
4 changes: 3 additions & 1 deletion src/Oop/RFC.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ struct StackValue;
struct Type;

struct SerializeData {
const unsigned char *raw;
unsigned char *raw;
uint32_t size;
};

SerializeData *merge(SerializeData a, SerializeData b);

class RFC {
public:
Module *m;
Expand Down
9 changes: 8 additions & 1 deletion src/Oop/proxy_supervisor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "../../lib/json/single_include/nlohmann/json.hpp"
#endif

#include "../Primitives/primitives.h"
#include "../Utils/macros.h"
#include "../Utils/util.h"
#include "../WARDuino/CallbackHandler.h"
Expand Down Expand Up @@ -195,7 +196,13 @@ struct SerializeData *ProxySupervisor::serializeRFC(RFC *callee) {
auto *ser = new SerializeData;
ser->size = hexa_size + 1;
ser->raw = hexa;
return ser;

auto *transfer = get_transfer(callee->m, callee->fidx);
auto *message = merge(*transfer, *ser);

delete ser;
free(transfer);
return message;
}

void ProxySupervisor::deserializeRFCResult(RFC *rfc) {
Expand Down
31 changes: 29 additions & 2 deletions src/Primitives/emulated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <thread>

#include "../Memory/mem.h"
#include "../Oop/stateful.h"
#include "../Utils/macros.h"
#include "../Utils/util.h"
#include "../WARDuino/CallbackHandler.h"
Expand All @@ -46,6 +47,7 @@ double sensor_emu = 0;
if (prim_index < ALL_PRIMITIVES) { \
PrimitiveEntry *p = &primitives[prim_index++]; \
p->name = #prim_name; \
p->index = prim_index - 1; \
p->f = &(prim_name); \
p->f_reverse = nullptr; \
p->f_serialize_state = nullptr; \
Expand Down Expand Up @@ -73,6 +75,9 @@ double sensor_emu = 0;
void function_name##_serialize( \
std::vector<IOStateElement *> &external_state)

#define def_prim_transfer(function_name) \
SerializeData &function_name##_transfer([[maybe_unused]] Module *m)

// TODO: use fp
#define pop_args(n) m->sp -= n
#define get_arg(m, arg) m->stack[(m)->sp - (arg)].value
Expand Down Expand Up @@ -256,16 +261,22 @@ def_prim(dummy, twoToOneU32) {
return true;
}

def_prim_transfer(dummy) {
uint32_t start = arg1.uint32;
uint32_t end = arg0.uint32;
return sync_memory(m, start, end);
}

def_prim(millis, NoneToOneU64) {
struct timeval tv {};
struct timeval tv{};
gettimeofday(&tv, nullptr);
unsigned long millis = 1000 * tv.tv_sec + tv.tv_usec;
pushUInt64(millis);
return true;
}

def_prim(micros, NoneToOneU64) {
struct timeval tv {};
struct timeval tv{};
gettimeofday(&tv, nullptr);
unsigned long micros = 1000000 * tv.tv_sec + tv.tv_usec;
pushUInt64(micros);
Expand Down Expand Up @@ -646,4 +657,20 @@ std::vector<IOStateElement *> get_io_state(Module *) {
return ioState;
}

SerializeData *get_transfer(Module *m, uint32_t index) {
SerializeData *nil = new SerializeData;
nil->raw = nullptr;
nil->size = 0;
for (auto &primitive : primitives) {
if (index == primitive.index) {
if (primitive.f_transfer) {
return primitive.f_transfer(m);
} else {
return nil;
}
}
}
return nil;
}

#endif // ARDUINO
2 changes: 2 additions & 0 deletions src/Primitives/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,6 @@ void invoke_primitive(Module *m, const std::string &function_name, Ts... args) {
primitive(m);
}

SerializeData *get_transfer(Module *m, uint32_t index);

#endif
4 changes: 3 additions & 1 deletion src/WARDuino.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <vector>

#include "Debug/debugger.h"
#include "Oop/proxy_supervisor.h"
#include "Interpreter/interpreter.h"
#include "Oop/proxy_supervisor.h"
#include "WARDuino/internals.h"

// Constants
Expand Down Expand Up @@ -71,9 +71,11 @@ struct IOStateElement {

typedef struct PrimitiveEntry {
const char *name;
uint32_t index;
Primitive f;
void (*f_reverse)(Module *m, std::vector<IOStateElement>);
void (*f_serialize_state)(std::vector<IOStateElement *> &);
SerializeData *(*f_transfer)(Module *m);
Type t;
} PrimitiveEntry;

Expand Down

0 comments on commit 7d57709

Please sign in to comment.