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の作法 その53

Last updated at Posted at 2025-01-18

概要

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

練習問題

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

投入したソース

make 4
in 0 1
out 2 3
and 0 1 2
xor 0 1 3

生成したネットリスト

$ 1 0.000005 10.20027730826997 50 5 50 5e-11
L 128 160 80 160 2 0 false 5 0
L 128 224 80 224 2 0 false 5 0
w 128 224 160 224 0
w 160 224 160 128 0
w 160 128 224 128 0
w 160 224 224 224 0
w 128 160 192 160 0
w 192 160 192 256 0
w 192 256 224 256 0
w 192 160 224 160 0
M 368 144 416 144 2 2.5
M 368 240 416 240 2 2.5
150 224 144 368 144 0 2 0 5
154 224 240 368 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 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 "L 128 160 80 160 2 0 false 5 0"
            IO.puts "L 128 224 80 224 2 0 false 5 0"
            IO.puts "w 128 224 160 224 0"
            IO.puts "w 160 224 160 128 0"
            IO.puts "w 160 128 224 128 0"
            IO.puts "w 160 224 224 224 0"
            IO.puts "w 128 160 192 160 0"
            IO.puts "w 192 160 192 256 0"
            IO.puts "w 192 256 224 256 0"
            IO.puts "w 192 160 224 160 0"
            IO.puts "M 368 144 416 144 2 2.5"
            IO.puts "M 368 240 416 240 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) == "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 4
in 0 1
out 2 3
and 0 1 2
xor 0 1 3
""")



以上。

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?