2
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?

言語実装Advent Calendar 2023

Day 21

wslでelixir その119

Last updated at Posted at 2023-12-13

概要

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

練習問題

Livebookで高位合成してverilogを生成せよ。

方針

  • 組み合わせ回路の高位合成
  • 論理式は使わない。
  • コンパイラ方式
  • kino.js使う。

写真

image.png

投入したソース

lut 4
lin 3
lout 1
code 0 0
code 1 0
code 2 0
code 3 1
code 4 0
code 5 0
code 6 1
code 7 0

生成したverilog

module lut(input wire [2:0] data, output reg [0:0] code);
	always @(data)
	begin
		case(data)
		4'h0:
			code = 8'h0;
		4'h1:
			code = 8'h0;
		4'h2:
			code = 8'h0;
		4'h3:
			code = 8'h1;
		4'h4:
			code = 8'h0;
		4'h5:
			code = 8'h0;
		4'h6:
			code = 8'h1;
		4'h7:
			code = 8'h0;
		endcase
	end
endmodule

module testbench;
	reg [2:0] data;
	wire [0:0] code;
	lut u(.data(data), .code(code));
	initial
	begin
		$display("data code");
		$monitor("%h  %b", data, code);
		data = 4'h0; #10;
		data = 4'h1; #10;
		data = 4'h2; #10;
		data = 4'h3; #10;
		data = 4'h4; #10;
		data = 4'h5; #10;
		data = 4'h6; #10;
		data = 4'h7; #10;
		$finish;
	end
endmodule

サンプルコード

defmodule KinoOhi.Asm2 do
  use Kino.JS

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

  asset "main.js" do
    """
    function run(str) {
    var in0 = "";
    var in1 = "";
    var out0 = "";
    var out1 = "";
    var m = "";
    var u = "";
    //var str = src.value;
    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 "lut":
    break;
    case "lin":
      var a = parseInt(code[1]) - 1;
      in0 = "input wire [" + a + ":0] data,";
      in1 = "\\treg [" + a + ":0] data;\\n";
    break;
    case "lout":
      var a = parseInt(code[1]) - 1;
      out0 = " output reg [" + a + ":0] code";
      out1 = "\\twire [" + a + ":0] code;\\n";
    break;
    case "code":  
      var a = code[1];
      var b = code[2];
      u += "\\t\\t4'h" + a + ":\\n\\t\\t\\tcode = 8'h" + b + ";\\n";
      m += "\\t\\tdata = 4'h" + a + "; #10;\\n";
    break;
    default:
      alert(code[0]);
    break;
    }
    }
    var out = document.getElementById("out");
    out.value = "module lut(" + in0 + out0 + ");\\n" +
    "\\talways @(data)\\n" +
    "\\tbegin\\n" +
    "\\t\\tcase(data)\\n" +
    u +
    "\\t\\tendcase\\n" +
    "\\tend\\n" +
    "endmodule\\n\\n" +
    "module testbench;\\n" +
    in1 + 
    out1 +
    "\\tlut u(.data(data), .code(code));\\n" +
    "\\tinitial\\n" +
    "\\tbegin\\n" +
    '\\t\\t$display("data code");\\n' +
    '\\t\\t$monitor("%h  %b", data, code);\\n' +
    m +
    "\\t\\t$finish;\\n" +
    "\\tend\\n" +
    "endmodule";
    }
    export function init(ctx, html) {
      const out0 = document.createElement("textarea");
      out0.id = "out";
      out0.style.width = "90%";
      out0.style.height = "550px";
      ctx.root.appendChild(out0);
      run(html);
    }
    """
  end
end

KinoOhi.Asm2.new("""
lut 4
lin 3
lout 1
code 0 0
code 1 0
code 2 0
code 3 1
code 4 0
code 5 0
code 6 1
code 7 0
""")

以上。

2
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
2
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?