circuitjsの作法 その55

# 概要
circuitjsを調査してみた。
練習問題やってみた。

# 練習問題
elixirで論理回路アセンブラをコンパイルして、ネットリストを高位合成せよ。
全加算器を書け。

# 投入したソース

```
make 5
in 0 2
out 3 4
and 1 2 3
xor 1 2 4
and 0 4 5
xor 0 4 6
or 2 5 7


```

# 生成したネットリスト

```

$ 1 0.000005 10.20027730826997 50 5 50 5e-11
w 272 272 304 272 0
w 304 272 336 272 0
w 336 160 272 160 0
w 336 240 272 240 0
w 272 240 272 224 0
w 272 224 144 224 0
w 272 160 272 224 0
w 288 128 464 128 0
M 592 160 624 160 2 2.5
w 112 288 144 288 0
w 144 112 80 112 0
w 80 112 80 256 0
w 80 256 144 256 0
L 80 256 48 256 2 0 false 5 0
L 112 288 48 288 2 0 false 5 0
L 144 224 48 224 2 0 false 5 0
w 144 144 112 144 0
w 112 144 112 288 0
w 464 128 464 144 0
w 336 192 304 192 0
w 304 192 304 272 0
w 464 256 576 256 0
w 576 256 576 208 0
M 576 208 624 208 2 2.5
154 144 272 272 272 0 2 0 5
150 144 128 288 128 0 2 0 5
154 336 256 464 256 0 2 0 5
150 336 176 464 176 0 2 0 55
152 464 160 592 160 0 2 0 5





```

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/238f0af8-f250-bf82-8985-8f3d4b9d834b.png)


# 成果物

https://paiza.io/projects/ZUEMChWT08OHm5XelZZKqA


# サンプルコード

```

defmodule Asm do
	def start_link do
		Agent.start_link(fn ->
			%{in0: "", in1: "", make0: "", u: ""}
		end, name: __MODULE__)
	end
    def make0(a) do
	end
    def in0(a, b) do
    end
    def out0(a, b) do
    end
    def nand0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
		    cond do
	        c == "2" ->
                str = "151 96 240 208 240 0 2 5 5\n"
                %{v | u: v.u <> str}
	        c == "3" ->
                str = "151 208 192 320 192 0 2 5 5\n"
                %{v | u: v.u <> str}
	        c == "4" ->
                str = "151 208 288 320 288 0 2 5 5\n"
                %{v | u: v.u <> str}
	        c == "5" ->
                str = "151 320 240 432 240 0 2 0 5\n"
                %{v | u: v.u <> str}
	        true ->
                str = "151 224 144 368 144 0 2 0 5\n"
                %{v | u: v.u <> str}
            end    
		end)
    end
    def and0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
		    cond do
	        c == "3" ->
                str = "154 144 272 272 272 0 2 0 5\n"
                %{v | u: v.u <> str}
	        c == "5" ->
                str = "154 336 256 464 256 0 2 0 5\n"
                %{v | u: v.u <> str}
	        true ->
                str = "151 224 144 368 144 0 2 0 5\n"
                %{v | u: v.u <> str}
            end    
		end)
    end
    def xor0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
		    cond do
	        c == "4" ->
                str = "150 144 128 288 128 0 2 0 5\n"
                %{v | u: v.u <> str}
	        c == "6" ->
                str = "150 336 176 464 176 0 2 0 55\n"
                %{v | u: v.u <> str}
	        true ->
                str = "151 224 144 368 144 0 2 0 5\n"
                %{v | u: v.u <> str}
            end    
		end)
    end
    def or0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "152 464 160 592 160 0 2 0 5"
            %{v | u: v.u <> str}
		end)
    end
    def print() do
		Agent.get(__MODULE__, fn v ->
            IO.puts "$ 1 0.000005 10.20027730826997 50 5 50 5e-11"
            IO.puts "w 272 272 304 272 0"
            IO.puts "w 304 272 336 272 0"
            IO.puts "w 336 160 272 160 0"
            IO.puts "w 336 240 272 240 0"
            IO.puts "w 272 240 272 224 0"
            IO.puts "w 272 224 144 224 0"
            IO.puts "w 272 160 272 224 0"
            IO.puts "w 288 128 464 128 0"
            IO.puts "M 592 160 624 160 2 2.5"
            IO.puts "w 112 288 144 288 0"
            IO.puts "w 144 112 80 112 0"
            IO.puts "w 80 112 80 256 0"
            IO.puts "w 80 256 144 256 0"
            IO.puts "L 80 256 48 256 2 0 false 5 0"
            IO.puts "L 112 288 48 288 2 0 false 5 0"
            IO.puts "L 144 224 48 224 2 0 false 5 0"
            IO.puts "w 144 144 112 144 0"
            IO.puts "w 112 144 112 288 0"
            IO.puts "w 464 128 464 144 0"
            IO.puts "w 336 192 304 192 0"
            IO.puts "w 304 192 304 272 0"
            IO.puts "w 464 256 576 256 0"
            IO.puts "w 576 256 576 208 0"
            IO.puts "M 576 208 624 208 2 2.5"
            IO.puts v.u
		end)
	end
end

defmodule Main do
	def run(str) do
    	Enum.map(String.split(str, "\n"), fn l ->
		    #IO.puts l
		    s = String.split(l, " ")
		    #IO.puts Enum.at(s, 0)
		    cond do
			Enum.at(s, 0) == "make" ->
				Asm.make0(Enum.at(s, 1))
			Enum.at(s, 0) == "in" ->
				Asm.in0(Enum.at(s, 1), Enum.at(s, 2))
			Enum.at(s, 0) == "out" ->
				Asm.out0(Enum.at(s, 1), Enum.at(s, 2))
			Enum.at(s, 0) == "nand" ->
				Asm.nand0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "and" ->
				Asm.and0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "nand" ->
				Asm.nand0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "nor" ->
				Asm.nor0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "xor" ->
				Asm.xor0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "or" ->
				Asm.or0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			true ->
				IO.puts ""
			end
	    end)
	    Asm.print
	end
end

Asm.start_link

Main.run("""
make 5
in 0 2
out 3 4
and 1 2 3
xor 1 2 4
and 0 4 5
xor 0 4 6
or 2 5 7
""")




```