wslでelixir その118

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
練習問題、やってみた。

# 練習問題
Livebookで高位合成してverilogを生成せよ。

# 方針

- 組み合わせ回路の高位合成
- コンパイラ方式
- kino.js使う。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/365e57ea-ece0-6a6d-e94b-a4f6a418397f.png)

# 投入したソース

```
make 10
in 0 2
wire 3 8
out 9 9
not 2 3
and 0 1 4
and 1 2 5
not 0 6
and 3 4 7
and 5 6 8
or 7 8 9
```

# 生成したverilog

```
module x(input a, input b, input c, output j);
    assign d = ~(c);
    assign e = a & b;
    assign f = b & c;
    assign g = ~(a);
    assign h = d & e;
    assign i = f & g;
    assign j = h | i;
endmodule

module testbench;
    reg a, b, c;
    x u(.a(a), .b(b), .c(c), .j(j));
    initial
    begin
        $display("a b c j ");
        $monitor("%b %b %b %b ", a, b, c, j);
        a = 0; b = 0; c = 0; #10;
        a = 0; b = 0; c = 1; #10;
        a = 0; b = 1; c = 0; #10;
        a = 0; b = 1; c = 1; #10;
        a = 1; b = 0; c = 0; #10;
        a = 1; b = 0; c = 1; #10;
        a = 1; b = 1; c = 0; #10;
        a = 1; b = 1; c = 1; #10;
        $finish;
    end
endmodule
```

# サンプルコード

```
defmodule KinoOhi.Asm do
  use Kino.JS

  def new(html) do
    Kino.JS.new(__MODULE__, html)
  end

  asset "main.js" do
    """
    function n2s(n) {
    var s = String.fromCharCode(n + 97);
    return s;
    }
    function run(str) {
    var in0 = "";
    var in1 = "";
    var in2 = "";
    var out0 = "";
    var make0 = "    x u(";
    var make1 = "    initial\\n    begin\\n        $display(\\"";
    var make2 = "        $monitor(\\"";
    var make3 = "";
    var u = "";
    var codes = str.split("\\n");  
    var len = codes.length;
    var i, 
    j;
    for (i = 0; i < len; i++)
    {
    var code = codes[i].split(" ");
    switch (code[0])
    {
    case "":
    break;
    case "wire":
    break;
    case "make":
    break;
    case "in":
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      switch (b)
      {
      case 0:
        in2 = 
          "        a = 0; #10;\\n" +
          "        a = 1; #10;\\n";
      break;
      case 1:
        in2 = 
          "        a = 0; b = 0; #10;\\n" +
          "        a = 0; b = 1; #10;\\n" +
          "        a = 1; b = 0; #10;\\n" +
          "        a = 1; b = 1; #10;\\n";
      break;
      case 2:
        in2 = 
          "        a = 0; b = 0; c = 0; #10;\\n" +
          "        a = 0; b = 0; c = 1; #10;\\n" +
          "        a = 0; b = 1; c = 0; #10;\\n" +
          "        a = 0; b = 1; c = 1; #10;\\n" +
          "        a = 1; b = 0; c = 0; #10;\\n" +
          "        a = 1; b = 0; c = 1; #10;\\n" +
          "        a = 1; b = 1; c = 0; #10;\\n" +
          "        a = 1; b = 1; c = 1; #10;\\n";
      break;
      case 3:
        in2 = 
          "        a = 0; b = 0; c = 0; d = 0; #10;\\n" +
          "        a = 0; b = 0; c = 0; d = 1; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 0; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 1; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 0; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 1; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 0; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 1; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 0; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 1; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 0; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 1; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 0; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 1; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 0; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 1; #10;\\n";
      break;
      case 4:
        in2 = 
          "        a = 0; b = 0; c = 0; d = 0; e = 0; #10;\\n" +
          "        a = 0; b = 0; c = 0; d = 0; e = 1; #10;\\n" +
          "        a = 0; b = 0; c = 0; d = 1; e = 0; #10;\\n" +
          "        a = 0; b = 0; c = 0; d = 1; e = 1; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 0; e = 0; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 0; e = 1; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 1; e = 0; #10;\\n" +
          "        a = 0; b = 0; c = 1; d = 1; e = 1; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 0; e = 0; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 0; e = 1; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 1; e = 0; #10;\\n" +
          "        a = 0; b = 1; c = 0; d = 1; e = 1; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 0; e = 0; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 0; e = 1; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 1; e = 0; #10;\\n" +
          "        a = 0; b = 1; c = 1; d = 1; e = 1; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 0; e = 0; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 0; e = 1; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 1; e = 0; #10;\\n" +
          "        a = 1; b = 0; c = 0; d = 1; e = 1; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 0; e = 0; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 0; e = 1; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 1; e = 0; #10;\\n" +
          "        a = 1; b = 0; c = 1; d = 1; e = 1; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 0; e = 0; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 0; e = 1; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 1; e = 0; #10;\\n" +
          "        a = 1; b = 1; c = 0; d = 1; e = 1; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 0; e = 0; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 0; e = 1; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 1; e = 0; #10;\\n" +
          "        a = 1; b = 1; c = 1; d = 1; e = 1; #10;\\n";        
      break;
      }
      b++;
      for (j = 0; j < b; j++)
      {
        make0 += "." + n2s(j) + "(" + n2s(j) + "), ";
        make1 += n2s(j) + " ";
        make2 += "%b ";
        make3 += n2s(j) + ", ";
      }
      for (j = a; j < b; j++)
      {
        in0 += "input " + n2s(j) + ", ";
        in1 += n2s(j) + ", ";
      }
    in1 = in1.substr(0, in1.length - 2);
    break;
    case "out":
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      b++;
      for (j = a; j < b; j++)
      {
        out0 += "output " + n2s(j) + ", ";
        make0 += "." + n2s(j) + "(" + n2s(j) + "), ";
        make1 += n2s(j) + " ";
        make2 += "%b ";
        make3 += n2s(j) + ", ";
      }
    out0 = out0.substr(0, out0.length - 2);
    	make0 = make0.substr(0, make0.length - 2);
    	make3 = make3.substr(0, make3.length - 2);
      make0 += ");\\n";
      make1 += "\\");\\n";
      make2 += "\\", ";
      make3 += ");\\n";
    break;
    case "nand":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      u += "    assign " + n2s(c) + " = ~(" + n2s(a) + " & " + n2s(b) + ");\\n";
    break;
    case "xor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      u += "    assign " + n2s(c) + " = " + n2s(a) + " ^ " + n2s(b) + ";\\n";
    break;
    case "and":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      u += "    assign " + n2s(c) + " = " + n2s(a) + " & " + n2s(b) + ";\\n";
    break;
    case "or":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      u += "    assign " + n2s(c) + " = " + n2s(a) + " | " + n2s(b) + ";\\n";
    break;
    case "nor":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      var c = parseInt(code[3]);
      u += "    assign " + n2s(c) + " = ~(" + n2s(a) + " | " + n2s(b) + ");\\n";
    break;
    case "not":  
      var a = parseInt(code[1]);
      var b = parseInt(code[2]);
      u += "    assign " + n2s(b) + " = ~(" + n2s(a) + ");\\n";
    break;
    default:
      alert(code[0]);
    break;
    }
    }
    var out = document.getElementById("out");
    out.value = "module x(" + in0 + out0 + ");\\n" +
    u +
    "endmodule\\n\\n" +
    "module testbench;\\n" +
    "    reg " + in1 + ";\\n" +
    make0 + make1 + make2 + make3 +
    in2 +
    "        $finish;\\n" +
    "    end\\n" +
    "endmodule";
    }

    export function init(ctx, html) {
      const out0 = document.createElement("textarea");
      out0.id = "out";
      out0.style.width = "90%";
      out0.style.height = "360px";
      ctx.root.appendChild(out0);
      run(html);
    }
    """
  end
end

KinoOhi.Asm.new("""
make 10
in 0 2
wire 3 8
out 9 9
not 2 3
and 0 1 4
and 1 2 5
not 0 6
and 3 4 7
and 5 6 8
or 7 8 9
""")
```

以上。