译使用iStyle格式化Verilog代码

66次阅读

共计 1197 个字符,预计需要花费 3 分钟才能阅读完成。

原文:Verilog でコード整形

安装

iStyle 可以从 GitHub 上 clone、make 自行编译出可执行文件,也可以直接下载已编译好的可执行文件。这里都给出来。

Github

https://github.com/thomasruss…

可执行文件

https://github.com/HayasiKei/…

格式化选项

以下是一些格式化时常用的选项及效果示例。

待格式化代码

reg [3:0] cnt;
always @(posedge clk or posedge rst) begin
if(rst) begin
cnt<=4'h0;
end else begin
cnt<=cnt+4'h1;
end
end

–style

ANSI style

./iStyle --style=ansi test.v
reg [3:0] cnt;
always @(posedge clk or posedge rst)
begin
    if(rst)
    begin
        cnt<=4'h0;
    end
    else
    begin
        cnt<=cnt+4'h1;
    end
end

Kernighan&Ritchie style

./iStyle --style=kr test.v
reg [3:0] cnt;
always @(posedge clk or posedge rst) begin
    if(rst) begin
        cnt<=4'h0;
    end
    else begin
        cnt<=cnt+4'h1;
    end
end

GNU style

./iStyle --style=gnu test.v
reg [3:0] cnt;
always @(posedge clk or posedge rst)
  begin
    if(rst)
      begin
        cnt<=4'h0;
      end
    else
      begin
        cnt<=cnt+4'h1;
      end
  end

-s

./iStyle -s2 test.v

该选项指定缩进时的空格数量,-s2 表示每次缩进使用 2 个空格(如果是 -s4 则表示每次用 4 个空格缩进)。

reg [3:0] cnt;
always @(posedge clk or posedge rst) begin
  if(rst) begin
    cnt<=4'h0;
  end else begin
    cnt<=cnt+4'h1;
  end
end

-p

- p 选项指定在运算符两侧插入空格。

reg [3: 0] cnt;
always @(posedge clk or posedge rst) begin
    if (rst)
    begin
        cnt <= 4'h0;
    end else
    begin
        cnt <= cnt + 4'h1;
    end
end

-P

- P 选项指定在运算符和括号周围插入空格。

reg [3: 0] cnt;
always @(posedge clk or posedge rst) begin
    if (rst)
    begin
        cnt <= 4'h0;
    end else
    begin
        cnt <= cnt + 4'h1;
    end
end

小结

虽然文中没有写,module 声明的缩进感觉并不是很好。verilog 有各种各样的代码风格,因此有一个强大的格式化程序是很有用的。

正文完
 0