shithub: orca

Download patch

ref: f5748b1113bded584a937283c4f1c817375b760b
parent: 8a5e7fe3aa272da0b9b02671989b6290d67c820b
author: cancel <cancel@cancel.fm>
date: Sat Dec 29 10:39:40 EST 2018

Move logic from phase1 to phase0 with legacy guard

--- a/sim.c
+++ b/sim.c
@@ -167,18 +167,14 @@
 typedef struct {
   Bank* bank;
   Usz bank_size;
-  Glyph* vars_slots;
-} Oper_phase0_extras;
-
-typedef struct {
-  Bank* bank;
-  Usz bank_size;
   Bank_cursor cursor;
-  Glyph const* vars_slots;
+  Glyph* vars_slots;
   Piano_bits piano_bits;
   Oevent_list* oevent_list;
 } Oper_phase1_extras;
 
+typedef Oper_phase1_extras Oper_phase0_extras;
+
 static void oper_bank_store(Oper_phase0_extras* extra_params, Usz width, Usz y,
                             Usz x, I32* restrict vals, Usz num_vals) {
   assert(num_vals > 0);
@@ -336,6 +332,10 @@
   oper_bank_load(extra_params, width, y, x, _i32_array,                        \
                  ORCA_ARRAY_COUNTOF(_i32_array))
 
+#define LEGACY_PHASE_GUARD                                                     \
+  if (!IS_AWAKE)                                                               \
+  return
+
 #define IN Mark_flag_input
 #define OUT Mark_flag_output
 #define NONLOCKING Mark_flag_lock
@@ -427,8 +427,8 @@
     PORT(0, 1, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_SOLO_PHASE_1(keys)
+
+  LEGACY_PHASE_GUARD;
   Glyph g = PEEK(0, 1);
   Piano_bits pb = piano_bits_of(g);
   // instead of this extra branch, could maybe just leave output port unlocked
@@ -442,6 +442,8 @@
     o = '*';
   POKE(1, 0, o);
 END_PHASE
+BEGIN_SOLO_PHASE_1(keys)
+END_PHASE
 
 BEGIN_SOLO_PHASE_0(comment)
   if (!IS_AWAKE)
@@ -476,8 +478,8 @@
       PORT(0, (Isz)i, IN);
     }
   END_PORTS
-END_PHASE
-BEGIN_SOLO_PHASE_1(midi)
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_NOT_BANGED;
   Glyph channel_g = PEEK(0, 1);
   Glyph octave_g = PEEK(0, 2);
@@ -504,6 +506,8 @@
   oe->velocity = midi_velocity_of(velocity_g);
   oe->bar_divisor = (U8)usz_clamp(index_of(length_g), 1, Glyphs_index_max);
 END_PHASE
+BEGIN_SOLO_PHASE_1(midi)
+END_PHASE
 
 BEGIN_SOLO_PHASE_0(osc)
   BEGIN_ACTIVE_PORTS
@@ -516,8 +520,8 @@
       PORT(0, (Isz)i + 1, IN);
     }
   END_PORTS
-END_PHASE
-BEGIN_SOLO_PHASE_1(osc)
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_NOT_BANGED;
   Glyph g = PEEK(0, -2);
   if (g != '.') {
@@ -538,6 +542,8 @@
     }
   }
 END_PHASE
+BEGIN_SOLO_PHASE_1(osc)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(add)
   REALIZE_DUAL;
@@ -546,12 +552,13 @@
     PORT(0, 2, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(add)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   POKE(1, 0, glyphs_add(PEEK(0, 1), PEEK(0, 2)));
 END_PHASE
+BEGIN_DUAL_PHASE_1(add)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(banger)
   REALIZE_DUAL;
@@ -559,9 +566,8 @@
     PORT(0, 1, IN | NONLOCKING);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(banger)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Glyph g = PEEK(0, 1);
   Glyph result;
@@ -576,6 +582,8 @@
   }
   POKE(1, 0, result);
 END_PHASE
+BEGIN_DUAL_PHASE_1(banger)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(clock)
   REALIZE_DUAL;
@@ -586,9 +594,8 @@
     PORT(0, 1, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(clock)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Usz mod_num = index_of(PEEK(0, 1)) + 1;
   Usz rate = index_of(PEEK(0, -1)) + 1;
@@ -595,6 +602,8 @@
   Glyph g = glyph_of(Tick_number / rate % mod_num);
   POKE(1, 0, g);
 END_PHASE
+BEGIN_DUAL_PHASE_1(clock)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(delay)
   REALIZE_DUAL;
@@ -603,9 +612,7 @@
     PORT(0, -1, IN | HASTE);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(delay)
-  REALIZE_DUAL;
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Usz offset = index_of(PEEK(0, 1));
   Usz rate = index_of(PEEK(0, -1)) + 1;
@@ -612,6 +619,8 @@
   Glyph g = (Tick_number + offset) % rate == 0 ? '*' : '.';
   POKE(1, 0, g);
 END_PHASE
+BEGIN_DUAL_PHASE_1(delay)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(if)
   REALIZE_DUAL;
@@ -620,14 +629,15 @@
     PORT(0, 2, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(if)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Glyph g0 = PEEK(0, 1);
   Glyph g1 = PEEK(0, 2);
   POKE(1, 0, g0 == g1 ? '*' : '.');
 END_PHASE
+BEGIN_DUAL_PHASE_1(if)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(generator)
   REALIZE_DUAL;
@@ -654,11 +664,9 @@
       PORT(out_y, out_x + i, OUT | NONLOCKING);
     }
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(generator)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
-  I32 data[3];
   if (LOAD(data)) {
     I32 out_x = data[0];
     I32 out_y = data[1] + 1;
@@ -671,6 +679,8 @@
     }
   }
 END_PHASE
+BEGIN_DUAL_PHASE_1(generator)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(halt)
   REALIZE_DUAL;
@@ -688,9 +698,8 @@
     PORT(0, 2, IN);
     PORT(1, 0, IN | OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(increment)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Usz min = index_of(PEEK(0, 1));
   Usz max = index_of(PEEK(0, 2));
@@ -702,6 +711,8 @@
     val = min;
   POKE(1, 0, glyph_of(val));
 END_PHASE
+BEGIN_DUAL_PHASE_1(increment)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(jump)
   REALIZE_DUAL;
@@ -709,12 +720,13 @@
     PORT(-1, 0, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(jump)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   POKE(1, 0, PEEK(-1, 0));
 END_PHASE
+BEGIN_DUAL_PHASE_1(jump)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(kill)
   REALIZE_DUAL;
@@ -746,9 +758,8 @@
       m[i] |= Mark_flag_lock;
     }
   }
-END_PHASE
-BEGIN_DUAL_PHASE_1(loop)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   I32 len_data[1];
   // todo should at least stun the 1 column if columns is 1
@@ -778,6 +789,8 @@
     }
   }
 END_PHASE
+BEGIN_DUAL_PHASE_1(loop)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(modulo)
   REALIZE_DUAL;
@@ -786,14 +799,15 @@
     PORT(0, 2, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(modulo)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Usz ia = index_of(PEEK(0, 1));
   Usz ib = index_of(PEEK(0, 2));
   POKE(1, 0, indexed_glyphs[ib == 0 ? 0 : (ia % ib)]);
 END_PHASE
+BEGIN_DUAL_PHASE_1(modulo)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(offset)
   REALIZE_DUAL;
@@ -811,11 +825,9 @@
     PORT(coords[0], coords[1], IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(offset)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
-  I32 coords[2];
   if (!LOAD(coords)) {
     coords[0] = 0;
     coords[1] = 1;
@@ -822,6 +834,8 @@
   }
   POKE(1, 0, PEEK(coords[0], coords[1]));
 END_PHASE
+BEGIN_DUAL_PHASE_1(offset)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(push)
   REALIZE_DUAL;
@@ -842,16 +856,16 @@
     PORT(0, 1, IN);
     PORT(1, (Isz)write_val_x, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(push)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
-  I32 write_val_x[1];
   if (!LOAD(write_val_x)) {
     write_val_x[0] = 0;
   }
   POKE(1, write_val_x[0], PEEK(0, 1));
 END_PHASE
+BEGIN_DUAL_PHASE_1(push)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(query)
   REALIZE_DUAL;
@@ -879,11 +893,9 @@
       PORT(1, out_x + i, OUT);
     }
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(query)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
-  I32 data[3];
   if (LOAD(data)) {
     I32 in_x = data[0] + 1;
     I32 in_y = data[1];
@@ -897,6 +909,8 @@
     // }
   }
 END_PHASE
+BEGIN_DUAL_PHASE_1(query)
+END_PHASE
 
 static Usz hash32_shift_mult(Usz key) {
   Usz c2 = UINT32_C(0x27d4eb2d);
@@ -915,9 +929,8 @@
     PORT(0, 2, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(random)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Usz a = index_of(PEEK(0, 1));
   Usz b = index_of(PEEK(0, 2));
@@ -937,6 +950,8 @@
   Usz val = key % (max + 1 - min) + min;
   POKE(1, 0, glyph_of(val));
 END_PHASE
+BEGIN_DUAL_PHASE_1(random)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(track)
   REALIZE_DUAL;
@@ -958,9 +973,8 @@
     PORT(0, (Isz)read_val_x, IN);
     PORT(1, 0, OUT);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(track)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   I32 ival[1];
   if (!LOAD(ival)) {
@@ -968,6 +982,8 @@
   }
   POKE(1, 0, PEEK(0, ival[0]));
 END_PHASE
+BEGIN_DUAL_PHASE_1(track)
+END_PHASE
 
 static Isz const uturn_data[] = {
     // clang-format off
@@ -990,9 +1006,8 @@
       PORT(uturn_data[i + 0], uturn_data[i + 1], IN | OUT | HASTE | NONLOCKING);
     }
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(uturn)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   for (Usz i = 0; i < Uturn_loop_limit; i += Uturn_per) {
     Isz dy = uturn_data[i + 0];
@@ -1004,6 +1019,8 @@
     }
   }
 END_PHASE
+BEGIN_DUAL_PHASE_1(uturn)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(variable)
   REALIZE_DUAL;
@@ -1027,9 +1044,8 @@
       return;
     extra_params->vars_slots[var_idx] = right;
   }
-END_PHASE
-BEGIN_DUAL_PHASE_1(variable)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
   Glyph left = PEEK(0, -1);
   if (left != '.')
@@ -1048,6 +1064,8 @@
     return;
   POKE(1, 0, result);
 END_PHASE
+BEGIN_DUAL_PHASE_1(variable)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(teleport)
   REALIZE_DUAL;
@@ -1065,11 +1083,9 @@
     PORT(0, 1, IN);
     PORT(coords[0], coords[1], OUT | NONLOCKING);
   END_PORTS
-END_PHASE
-BEGIN_DUAL_PHASE_1(teleport)
-  REALIZE_DUAL;
+
+  LEGACY_PHASE_GUARD;
   STOP_IF_DUAL_INACTIVE;
-  I32 coords[2];
   if (!LOAD(coords)) {
     coords[0] = 1;
     coords[1] = 0;
@@ -1076,6 +1092,8 @@
   }
   POKE_STUNNED(coords[0], coords[1], PEEK(0, 1));
 END_PHASE
+BEGIN_DUAL_PHASE_1(teleport)
+END_PHASE
 
 BEGIN_DUAL_PHASE_0(zig)
   if (!IS_AWAKE)
@@ -1186,17 +1204,13 @@
   memset(vars_slots, '.', sizeof(vars_slots));
   mbuffer_clear(mbuf, height, width);
   oevent_list_clear(oevent_list);
-  Oper_phase0_extras phase0_extras;
-  phase0_extras.bank = bank;
-  phase0_extras.bank_size = 0;
-  phase0_extras.vars_slots = &vars_slots[0];
-  sim_phase_0(gbuf, mbuf, height, width, tick_number, &phase0_extras);
   Oper_phase1_extras phase1_extras;
   phase1_extras.bank = bank;
-  phase1_extras.bank_size = phase0_extras.bank_size;
+  phase1_extras.bank_size = 0;
   bank_cursor_reset(&phase1_extras.cursor);
   phase1_extras.vars_slots = &vars_slots[0];
   phase1_extras.piano_bits = piano_bits;
   phase1_extras.oevent_list = oevent_list;
+  sim_phase_0(gbuf, mbuf, height, width, tick_number, &phase1_extras);
   sim_phase_1(gbuf, mbuf, height, width, tick_number, &phase1_extras);
 }