1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

circuitjsの作法 その54

Posted at

概要

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

練習問題

elixirで論理回路アセンブラをコンパイルして、ネットリストを高位合成せよ。
nandだけでXORを書け。

投入したソース

make 3
in 0 1
out 2 3
nand 0 1 2
nand 0 2 3
nand 1 2 4
nand 3 4 5

生成したネットリスト

$ 1 0.000005 10.20027730826997 50 5 50 5e-11
w 208 240 208 272 0
w 208 240 208 208 0
w 320 192 320 224 0
w 320 256 320 288 0
w 96 176 96 224 0
w 96 176 208 176 0
w 96 256 96 304 0
w 96 304 208 304 0
M 432 240 480 240 0 2.5
L 96 176 48 176 0 0 false 5 0
L 96 304 48 304 0 0 false 5 0
151 96 240 208 240 0 2 5 5
151 208 192 320 192 0 2 5 5
151 208 288 320 288 0 2 5 5
151 320 240 432 240 0 2 0 5

写真

image.png

成果物

サンプルコード


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 ->
            str = "150 224 144 368 144 0 2 0 5\n"
            %{v | u: v.u <> str}
		end)
    end
    def xor0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "154 224 240 368 240 0 2 0 5\n"
            %{v | u: v.u <> str}
		end)
    end
    def or0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "152 224 144 368 144 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 208 240 208 272 0"
            IO.puts "w 208 240 208 208 0"
            IO.puts "w 320 192 320 224 0"
            IO.puts "w 320 256 320 288 0"
            IO.puts "w 96 176 96 224 0"
            IO.puts "w 96 176 208 176 0"
            IO.puts "w 96 256 96 304 0"
            IO.puts "w 96 304 208 304 0"
            IO.puts "M 432 240 480 240 0 2.5"
            IO.puts "L 96 176 48 176 0 0 false 5 0"
            IO.puts "L 96 304 48 304 0 0 false 5 0"
            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 3
in 0 1
out 2 3
nand 0 1 2
nand 0 2 3
nand 1 2 4
nand 3 4 5
""")



以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?