phanerozoic's picture
Replace the two-bank machine with a K-bank ring, prove its reproduction in Rocq, and revise the paper
a9ea017
Raw History Blame Contribute Delete
9.52 kB
(* The ring machine as a netlist of ternary threshold units, unit for unit in the order of the
generator, for a window of 2^al cells, K banks of S0 slots and K tapes of L cells. *)
From Stdlib Require Import List Bool Arith ZArith Lia.
Import ListNotations.
From TH Require Import TwoHead Net Gates.
Section Phys.
Variables al K S0 L : nat.
Definition W : nat := 2 ^ al.
Definition B : nat := recw al.
Definition N : nat := S0 * B.
Definition nw : nat := W - 4.
(* the state: the window cells from 4 on, kappa one-hot, each bank with the tracks of the slot and the
bit of its head, each tape with the track of its head *)
Definition okap : nat := nw.
Definition bsz : nat := N + S0 + B.
Definition obank (k : nat) : nat := nw + K + k * bsz.
Definition otape (k : nat) : nat := nw + K + K * bsz + k * (2 * L).
Definition nst : nat := nw + K + K * bsz + K * (2 * L).
Definition act (k : nat) : sref := SIn (okap + k mod K).
Definition idl (k : nat) : sref := SIn (okap + (k + K - 1) mod K).
Definition qref (k p : nat) : sref := SIn (obank k + p).
Definition href (k s : nat) : sref := SIn (obank k + N + s).
Definition jref (k j : nat) : sref := SIn (obank k + N + S0 + j).
Definition dref (k i : nat) : sref := SIn (otape k + i).
Definition tref (k i : nat) : sref := SIn (otape k + L + i).
Definition zero : sref := SRef 0 0 0.
Definition fld (x : nat) : sref := SRef 2 x K.
Definition vref : sref := SRef 21 0 0.
Definition req (c : nat) : sref := SRef 22 (c - 4) 2.
Definition bitsof (a t : nat) : bool := Nat.testbit a (al - 1 - t).
Definition cellref (a : nat) : sref :=
if Nat.eqb a 0 then zero
else if Nat.eqb a 1 || Nat.eqb a 2 || Nat.eqb a 3 then SRef 5 (a - 1) K
else SIn (a - 4).
(* 0: a unit that never fires *)
Definition blk_zero : block := Blk 1 1 (fun _ => [sOR []]).
(* 1: bit j of the record at the slot of the head of bank k, member k B + j *)
Definition blk_rec : block := Blk (K * B) (S0 + 1) (fun m =>
map (fun s => sAND [(href (m / B) s, true); (qref (m / B) (s * B + m mod B), true)]) (seq 0 S0)
++ [sOR (map (fun s => (SRef 1 m s, true)) (seq 0 S0))]).
(* 2: bit j of the record at the active head *)
Definition blk_fld : block := Blk B (K + 1) (fun j =>
map (fun k => sAND [(act k, true); (SRef 1 (k * B + j) S0, true)]) (seq 0 K)
++ [sOR (map (fun k => (SRef 2 j k, true)) (seq 0 K))]).
(* 3: the cell under the head of tape k *)
Definition blk_tc : block := Blk K (L + 1) (fun k =>
map (fun i => sAND [(tref k i, true); (dref k i, true)]) (seq 0 L)
++ [sOR (map (fun i => (SRef 3 k i, true)) (seq 0 L))]).
(* 4: the bit under the arm of bank k *)
Definition blk_ac : block := Blk K (B + 1) (fun k =>
map (fun j => sAND [(jref k j, true); (SRef 1 (k * B + j) S0, true)]) (seq 0 B)
++ [sOR (map (fun j => (SRef 4 k j, true)) (seq 0 B))]).
(* 5: the cells under the read head, the arm and the copy head *)
Definition headsel (m k : nat) : su :=
match m with
| 0 => sAND [(act k, true); (SRef 3 k L, true)]
| 1 => sAND [(idl k, true); (SRef 4 k B, true)]
| _ => sAND [(idl k, true); (SRef 3 k L, true)]
end.
Definition blk_head : block := Blk 3 (K + 1) (fun m =>
map (headsel m) (seq 0 K) ++ [sOR (map (fun k => (SRef 5 m k, true)) (seq 0 K))]).
(* 6, 7, 8: the decode lines of the two operand addresses and the output address *)
Definition blk_dec (o : nat) : block :=
Blk W 1 (fun a => [sAND (map (fun t => (fld (o + t), bitsof a t)) (seq 0 al))]).
(* 9 to 12: the two operands *)
Definition blk_term (bdec : nat) : block := Blk W 1 (fun a => [sAND [(SRef bdec a 0, true); (cellref a, true)]]).
Definition blk_xs (bterm : nat) : block := Blk 1 1 (fun _ => [sOR (map (fun a => (SRef bterm a 0, true)) (seq 0 W))]).
Definition xsref (i : nat) : sref := if Nat.eqb i 0 then SRef 10 0 0 else SRef 12 0 0.
(* 13 to 21: the weights, the bias and the threshold *)
Definition blk_pos : block := Blk 2 1 (fun i => [sAND [(fld (2 * i), false); (fld (2 * i + 1), true)]]).
Definition blk_neg : block := Blk 2 1 (fun i => [sAND [(fld (2 * i), true); (fld (2 * i + 1), false)]]).
Definition blk_mt : block := Blk 2 1 (fun i => [sAND [(SRef 13 i 0, true); (xsref i, true)]]).
Definition blk_nt : block := Blk 2 1 (fun i => [sAND [(SRef 14 i 0, true); (xsref i, true)]]).
Definition blk_pge : block :=
Blk 5 1 (fun i => [SU (2 - Z.of_nat i)
[(SRef 15 0 0, 1%Z); (SRef 15 1 0, 1%Z); (SRef 16 0 0, (-1)%Z); (SRef 16 1 0, (-1)%Z)]]).
Definition blk_b4 : block :=
Blk 1 4 (fun _ => [sOR [(fld 5, true); (fld 6, true); (fld 7, true)]; sOR [(fld 5, true); (fld 6, true)];
sAND [(fld 5, true); (fld 6, true); (fld 7, true)]; sAND [(fld 5, true); (fld 6, true)]]).
Definition blk_bge : block :=
Blk 1 5 (fun _ => [sAND [(fld 4, false)]; sAND [(fld 4, false); (SRef 18 0 0, true)];
sAND [(fld 4, false); (SRef 18 0 1, true)];
sOR [(fld 4, false); (SRef 18 0 2, true)]; sOR [(fld 4, false); (SRef 18 0 3, true)]]).
Definition vand_bge (i : nat) : nat := match i with 0 => 2 | 1 => 1 | 2 => 0 | 3 => 3 | _ => 4 end.
Definition blk_vand : block := Blk 5 1 (fun i => [sAND [(SRef 17 i 0, true); (SRef 19 0 (vand_bge i), true)]]).
Definition blk_v : block := Blk 1 1 (fun _ => [sOR (map (fun i => (SRef 20 i 0, true)) (seq 0 5))]).
(* 22: the window after the write, member c - 4 for cell c *)
Definition blk_xhat : block := Blk nw 3 (fun c => mux3 22 c 0 (SRef 8 (c + 4) 0) vref (SIn c)).
(* 23: kappa after the hand-over *)
Definition blk_kap : block := Blk K 3 (fun k => mux3 23 k 0 (req HO) (idl k) (act k)).
(* 24, 25: the write under the arm and the clearing of the idle bank, and the bits of the banks *)
Definition blk_bw : block :=
Blk K 2 (fun k => [sAND [(idl k, true); (SRef 8 ARM 0, true)]; sAND [(idl k, true); (req CB, true)]]).
Definition blk_q : block := Blk (K * N) 4 (fun m =>
[sAND [(SRef 24 (m / N) 0, true); (href (m / N) (m mod N / B), true); (jref (m / N) (m mod N mod B), true)];
sAND [(SRef 25 m 0, true); (vref, true); (SRef 24 (m / N) 1, false)];
sAND [(SRef 25 m 0, false); (qref (m / N) (m mod N), true); (SRef 24 (m / N) 1, false)];
sOR [(SRef 25 m 1, true); (SRef 25 m 2, true)]]).
(* 26 to 28: the heads of the banks: the arm rewound or moved, the slot of the active head turned *)
Definition blk_hq : block := Blk K 4 (fun k =>
[sAND [(idl k, true); (req RA, true)];
sAND [(idl k, true); (req RA, false); (req AA, true)];
sAND [(SRef 26 k 1, true); (jref k (B - 1), true)];
sOR [(act k, true); (SRef 26 k 2, true)]]).
(* the next bit m of a one-hot track of n cells: one cell on if adv, the first if rew, else itself *)
Definition track (bk m n : nat) (adv rew cur prev : sref) : list su :=
[sAND [(adv, true); (prev, true)];
sAND [(rew, false); (adv, false); (cur, true)];
if Nat.eqb (m mod n) 0 then sAND [(rew, true)] else sAND [(rew, true); (zero, true)];
sOR [(SRef bk m 0, true); (SRef bk m 1, true); (SRef bk m 2, true)]].
Definition blk_hs : block := Blk (K * S0) 4 (fun m =>
track 27 m S0 (SRef 26 (m / S0) 3) (SRef 26 (m / S0) 0) (href (m / S0) (m mod S0))
(href (m / S0) ((m mod S0 + S0 - 1) mod S0))).
Definition blk_hj : block := Blk (K * B) 4 (fun m =>
track 28 m B (SRef 26 (m / B) 1) (SRef 26 (m / B) 0) (jref (m / B) (m mod B))
(jref (m / B) ((m mod B + B - 1) mod B))).
(* 29 to 31: the tapes: the write under the copy head, the clearing, the rewinds and the moves *)
Definition blk_tw : block := Blk K 8 (fun k =>
[sAND [(idl k, true); (SRef 8 CPY 0, true)];
sAND [(idl k, true); (req CT, true)];
sAND [(act k, true); (req RR, true)];
sAND [(idl k, true); (req RC, true)];
sOR [(SRef 29 k 2, true); (SRef 29 k 3, true)];
sAND [(act k, true); (req RR, false); (req AR, true)];
sAND [(idl k, true); (req RC, false); (req AC, true)];
sOR [(SRef 29 k 5, true); (SRef 29 k 6, true)]]).
Definition blk_d : block := Blk (K * L) 4 (fun m =>
[sAND [(SRef 29 (m / L) 0, true); (tref (m / L) (m mod L), true)];
sAND [(SRef 30 m 0, true); (vref, true); (SRef 29 (m / L) 1, false)];
sAND [(SRef 30 m 0, false); (dref (m / L) (m mod L), true); (SRef 29 (m / L) 1, false)];
sOR [(SRef 30 m 1, true); (SRef 30 m 2, true)]]).
Definition blk_t : block := Blk (K * L) 4 (fun m =>
track 31 m L (SRef 29 (m / L) 7) (SRef 29 (m / L) 4) (tref (m / L) (m mod L))
(tref (m / L) ((m mod L + L - 1) mod L))).
Definition phys_blocks : list block :=
[blk_zero; blk_rec; blk_fld; blk_tc; blk_ac; blk_head; blk_dec 8; blk_dec (8 + al); blk_dec (8 + 2 * al);
blk_term 6; blk_xs 9; blk_term 7; blk_xs 11; blk_pos; blk_neg; blk_mt; blk_nt; blk_pge; blk_b4; blk_bge;
blk_vand; blk_v; blk_xhat; blk_kap; blk_bw; blk_q; blk_hq; blk_hs; blk_hj; blk_tw; blk_d; blk_t].
(* the unit computing the next value of each state bit *)
Definition out_ref (i : nat) : sref :=
if i <? nw then (if i + 4 <=? HO then zero else SRef 22 i 2)
else if i <? nw + K then SRef 23 (i - nw) 2
else if i <? nw + K + K * bsz then
let r := i - (nw + K) in let k := r / bsz in let p := r mod bsz in
if p <? N then SRef 25 (k * N + p) 3
else if p <? N + S0 then SRef 27 (k * S0 + (p - N)) 3
else SRef 28 (k * B + (p - N - S0)) 3
else
let r := i - (nw + K + K * bsz) in let k := r / (2 * L) in let p := r mod (2 * L) in
if p <? L then SRef 30 (k * L + p) 3 else SRef 31 (k * L + (p - L)) 3.
Definition phys_units : list tu := compile nst phys_blocks.
Definition phys_next : list nat := map (fun i => idx nst phys_blocks (out_ref i)) (seq 0 nst).
End Phys.