Skip to content

Commit 36f965b

Browse files
authored
Update restore_external_state to match the zephyr definition and register reverse prim for digital write (#312)
1 parent e06eafd commit 36f965b

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/Primitives/emulated.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def_prim_serialize(chip_digital_write) {
432432
for (int pin = 0; pin < NUM_DIGITAL_PINS; pin++) {
433433
auto *state = new IOStateElement();
434434
state->key = "p" + std::to_string(pin);
435-
state->output = MODES[pin] == 0x01;
435+
state->output = MODES[pin] == 0x02;
436436
state->value = PINS[pin];
437437
external_state.push_back(state);
438438
}
@@ -573,6 +573,7 @@ void install_primitives() {
573573

574574
install_primitive(chip_pin_mode);
575575
install_primitive(chip_digital_write);
576+
install_primitive_reverse(chip_digital_write);
576577
install_primitive(chip_delay);
577578
install_primitive(chip_digital_read);
578579
install_primitive(chip_analog_read);
@@ -641,24 +642,16 @@ bool resolve_external_memory(char *symbol, Memory **val) {
641642
//------------------------------------------------------
642643
void restore_external_state(Module *m,
643644
const std::vector<IOStateElement> &external_state) {
644-
uint8_t opcode = *m->pc_ptr;
645-
// TODO: Maybe primitives can also be called using the other call
646-
// instructions such as call_indirect
647-
// maybe there should just be a function that checks if a certain function
648-
// is being called that handles all these cases?
649-
if (opcode == 0x10) { // call opcode
650-
uint8_t *pc_copy = m->pc_ptr + 1;
651-
uint32_t fidx = read_LEB_32(&pc_copy);
652-
if (fidx < m->import_count) {
653-
for (auto &primitive : primitives) {
654-
if (!strcmp(primitive.name, m->functions[fidx].import_field)) {
655-
if (primitive.f_reverse) {
656-
debug("Reversing action for primitive %s\n",
657-
primitive.name);
658-
primitive.f_reverse(m, external_state);
659-
}
660-
return;
661-
}
645+
std::set<std::string> prim_names;
646+
for (uint32_t i = 0; i < m->import_count; i++) {
647+
prim_names.emplace(m->functions[i].import_field);
648+
}
649+
650+
for (PrimitiveEntry &p : primitives) {
651+
if (prim_names.find(p.name) != prim_names.end()) {
652+
if (p.f_reverse) {
653+
printf("EMU: Reversing state for primitive %s\n", p.name);
654+
p.f_reverse(m, external_state);
662655
}
663656
}
664657
}

src/Primitives/zephyr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ void restore_external_state(Module *m,
553553
}
554554
}
555555

556-
std::vector<IOStateElement *> get_io_state(Module *m) {
556+
std::vector<IOStateElement *> get_io_state(Module *) {
557557
std::vector<IOStateElement *> ioState;
558558
for (auto &primitive : primitives) {
559559
if (primitive.f_serialize_state) {

0 commit comments

Comments
 (0)