土曜日, 8月 16, 2025
土曜日, 8月 16, 2025
- Advertisment -
ホームニューステックニュースClaude CodeにVerylのコードを書かせてみた

Claude CodeにVerylのコードを書かせてみた



Verylとは現在開発中の新しいハードウェア記述言語です。

https://veryl-lang.org

最近Model Context Protocol(MCP)なるものが流行っているようで「Verylでも対応すると面白いかな」などと思っていたところ、lsmcpというツールが登場しました。これはMCP経由でLanguage Serverに対してリクエストを投げられるもので、LSPに対応した任意の言語で利用することができます。(原理的には。実際にはLSP実装毎の挙動差などで苦労されているようです…)

https://zenn.dev/mizchi/articles/introduce-lsmcp

今回VerylのLSP実装であるveryl-lsをlsmcpから呼ぶことができたので、Claude CodeにVerylのコードを書かせてみました。

現在のリリースされているveryl-lsのv0.16.3にはバグがあってlsmcpと連携させることができません。手元でビルドするか、(2025/08/16以降の)最新のnightlyビルドを使ってください。

$ verylup install nightly

まずVerylのサンプルプロジェクトをcloneし、nightlyビルドを使うようにオーバーライドを設定し、lsmcpの設定をします。
(完全に新規のプロジェクトを作ってもよかったのですが、多少参考になるコードをClaudeに読ませた方がいいかと思い既存のプロジェクトにしました)

$ git clone https://github.com/veryl-lang/sample
$ cd sample
$ verylup override set nightly
$ claude mcp add veryl npx --scope local -- -y @mizchi/lsmcp --bin="veryl-ls"

あとは claude コマンドでClaude Codeを起動して適当なモジュールを書いてもらいます。

> create new module named "Add" in a new file "src/add.veryl". The module has two input port "a" and "b", and one output port "c". "c" is sum of "a" and "b".

● I'll create a new Veryl module named "Add" in the file src/add.veryl with the specified input and output ports.

● Write(src/add.veryl)
  ⎿  Wrote 7 lines to src/add.veryl
     module Add (
         a: input  logic,
         b: input  logic,
         c: output logic,
     ) {
         assign c = a + b;
     }
● The Add module has been created with two input ports a and b, and one output port c that outputs the sum of the inputs.

ビット幅について言及しなかったので1bitになっていますが、特に問題なさそうです。
次にコンパイルエラーのチェックとフォーマットをしてもらいます。

> check compile error, and format by mcp

● I'll check for compile errors and format the code using MCP tools.

● veryl - get_diagnostics (MCP)(root: "/home/hatta/work/repos/veryl_sample", filePath: "src/add.veryl")
  ⎿  Found 0 errors and 0 warnings in src/add.veryl

      Debug Info: push method, 52 attempts, 5215ms
     … +2 lines (ctrl+r to expand)
                   
● veryl - format_document (MCP)(root: "/home/hatta/work/repos/veryl_sample", filePath: "src/add.veryl", applyChanges: true)
  ⎿  Formatting changes for src/add.veryl:

     Line 1:1: "module Add (\n    a: input  logic,\n    b: input  ..." → "module Add (\n    a: input  logic,\n    b: input  ..."
     … +4 lines (ctrl+r to expand)

● No compile errors found in the Add module. The code has been formatted and is working correctly. The module compiles without any errors or warnings.

(MCP) と表示されている通り、lsmcpを通してveryl-lsに問い合わせを行い、コンパイルエラーを確認し、フォーマットしてくれています。

続いて c を1クロック遅延させてみましょう。

> add 1cycle delay to "c" of "add" module

● I'll modify the Add module to add a 1-cycle delay to the output "c".

● Read(src/add.veryl)
  ⎿  Read 8 lines (ctrl+r to expand)

● Update(src/add.veryl)
  ⎿  Updated src/add.veryl with 4 additions and 1 removal
        1    module Add (
        2 +      clk: input  clock,
        3        a: input  logic,
        4        b: input  logic,
        5        c: output logic,
        6    ) {
        7 -      assign c = a + b;
        7 +      always_ff {
        8 +          c 

特に言及していませんが、ちゃんとクロックポートを追加し、always_ff文により1クロック遅延になっています。ですが、Verylでは always_ff 文中の代入は ではなく = です。
コンパイルエラーを確認して直してもらいましょう。

> check compile error, and format by mcp

● veryl - get_diagnostics (MCP)(root: "/home/hatta/work/repos/veryl_sample", filePath: "src/add.veryl")
  ⎿  Found 1 error(s) in src/add.veryl

      Debug Info: push method, 22 attempts, 2203ms
      … +5 lines (ctrl+r to expand)

  veryl - format_document (MCP)(root: "/home/hatta/work/repos/veryl_sample", filePath: "src/add.veryl", applyChanges: true)
  ⎿  No formatting changes needed for src/add.veryl

● There's a syntax error at line 8. Let me check the current content and fix it:

● Read(src/add.veryl)
  ⎿  Read 11 lines (ctrl+r to expand)

● Let me check the existing Veryl code to understand the correct syntax for clocked assignments:

● Search(pattern: "src/*.veryl")
  ⎿  Found 4 files (ctrl+r to expand)

● Read(src/delay.veryl)
  ⎿  Read 29 lines (ctrl+r to expand)

● I can see the correct Veryl syntax now. The always_ff block needs clock and reset signals specified. Let me fix the Add module:

  Update(src/add.veryl)
  ⎿  Updated src/add.veryl with 11 additions and 6 removals
        1    module Add (
        2 -      clk: input  clock,
        3 -      a: input  logic,
        4 -      b: input  logic,
        5 -      c: output logic,
        2 +      i_clk  : input  clock,
        3 +      i_rst_n: input  reset,
        4 +      a      : input  logic,
        5 +      b      : input  logic,
        6 +      c      : output logic,
        7    ) {
        8 -      always_ff {
        9 -          c 

という感じで、ちゃんとMCP経由でコンパイルエラーの位置を確認し、既存のソースコード delay.veryl の記述を参考に正しく直してきました。
実際には always_ff (i_clk, i_rst_n) のクロック・リセット指定は省略可能ですし、i_rst_n の接尾辞 _n はVeryl側では付けない方がいいですが、参考にした delay.veryl が(ちょっと古いコードなので)そのようなスタイルになっており、それをそのまま引き継いでいます。このあたりはもう少し最新のコードを読ませれば解決すると思います。

おそらく現時点では学習ソースに含まれるVerylのコードはほとんどないと思われますが、多少のサンプルソースとLSPがあれば結構いけそうな感じです。



Source link

Views: 0

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -