verilog 通过DPI-C调用C 流水灯模拟

verilog 通过DPI-C调用C简单示例, verillator模拟

ledloop.v

module ledloop(
  input wire clk,
  output wire[3:0] LED
);

  reg[31:0] cnt = 32'h00000000;

  always @(posedge clk)
     cnt <= cnt + 1;

  assign LED = 4'b0001 << cnt[21:20];
endmodule

电脑模拟较慢,2M计数位移一次,可以自行通过计数位调整。

转接
led_adaptor.v

module led_adaptor(
  input wire clk,
  input wire[3:0] LED
);
import "DPI-C"  function void  print(int a); 
reg[3:0] state ;

always @(posedge clk)
  if (state != LED) begin
    print(int'(LED));
    state <= LED;
  end

endmodule

tb
top.v

module top;
wire[3:0] led;
reg clk = 1'b0;

initial begin
  forever #1  clk = ~clk;
end

ledloop  loop1(.clk(clk),
          .LED(led));

led_adaptor  adaptor1(.clk(clk),
		.LED(led));

endmodule

print.c

#define BITWIDTH 4
#include <stdio.h>
#include <unistd.h>
#include "svdpi.h"
extern "C"
void print(int a) {
    unsigned  b = (unsigned)a;
    printf("\r");  //移至行首
    printf("\033[K");  //清除光标后字符
    for (int i = 0; i < BITWIDTH; i++){
      if (b&1)
        printf("* ");
      else
        printf("  ");
      b = b >> 1;
    }   
    fflush(stdout);
}          

Makefile

.PHONY:clean

VERILATOR = verilator

OUTDIR=out
VERILATOR_FLAGS =  -Wall -top top -Mdir $(OUTDIR) -cc -binary -build -j 2

default: run

run: print.c top.v  ledloop.v led_adaptor.v
	$(VERILATOR) $(VERILATOR_FLAGS) $^
	./$(OUTDIR)/Vtop

clean:
	-rm -rf $(OUTDIR)

先安装verilator
直接

make 

github上传了下直接
git clone https://github.com/yses/fpgasim

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yvee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值