@@ -432,7 +432,7 @@ def_prim_serialize(chip_digital_write) {
432
432
for (int pin = 0 ; pin < NUM_DIGITAL_PINS; pin++) {
433
433
auto *state = new IOStateElement ();
434
434
state->key = " p" + std::to_string (pin);
435
- state->output = MODES[pin] == 0x01 ;
435
+ state->output = MODES[pin] == 0x02 ;
436
436
state->value = PINS[pin];
437
437
external_state.push_back (state);
438
438
}
@@ -573,6 +573,7 @@ void install_primitives() {
573
573
574
574
install_primitive (chip_pin_mode);
575
575
install_primitive (chip_digital_write);
576
+ install_primitive_reverse (chip_digital_write);
576
577
install_primitive (chip_delay);
577
578
install_primitive (chip_digital_read);
578
579
install_primitive (chip_analog_read);
@@ -641,24 +642,16 @@ bool resolve_external_memory(char *symbol, Memory **val) {
641
642
// ------------------------------------------------------
642
643
void restore_external_state (Module *m,
643
644
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);
662
655
}
663
656
}
664
657
}
0 commit comments