用Verilog实现寻找第一个1和最后一个1的位置

用Verilog实现寻找数据 第一个1和最后一个1的位置
寻找数据中的第一个1的位置

// find fist one
module find_ones(
input [7:0] x,
output [2:0] y);

wire [3:0] data_4;
wire [1:0] data_2;


assign y[2] = | x[7:4];
assign data_4= y[2] ? x[7:4] : x[3:0] ;
assign y[1] = | data_4[3:2];
assign data_2 = y[1] ? data_4[3:2] : data_4[1:0];
assign y[0] = data_2[1];

endmodule

寻找数据中最后一个1的位置

// find the last one
module find_one(
input [7:0] data,
output [2:0] index);

wire [3:0] data_4;
wire [1:0] data_2;

assign index[2] = ~|data[3:0];
assign data_4 = index[2] ? data[7:4]:data[3:0];
assign index[1] = ~|data_4[1:0];
assign data_2 = index[1] ? data_4[3:2] : data_4[1:0];
assign index[0] = ~data_2[0];

endmodule

testbench(sv)

// 类定义,产生随机变量
class data_rand;
rand logic [7:0]data;
endclass
//-----------------------------------
// 接口定义
interface find_if;
logic [7:0] x;
logic [2:0] y;

modport DUT (input x, output y);
modport TB (input y, output x);
endinterface
//-------------------------
// 顶层top 只例化模块
module tb();
find_if if0();					// the  () is must;
tb_find_one tb_find_one(if0);
find_one find_one(
                .data(if0.x),
                .index(if0.y));

endmodule
//------------------------------
// 激励产生模块
module tb_find_one(find_if.TB tb_if);

data_rand dr;		//声明类指针

initial begin
dr=new();				// 为类分配内存

tb_if.x=8'b00010000;
#30
tb_if.x=8'b01010100;
#30
tb_if.x=8'b01000000;
#30
tb_if.x=8'b01100000;

#30
assert(dr.randomize());	// 随机数的产生和断言
tb_if.x=dr.data;
#30
assert(dr.randomize());
tb_if.x=dr.data;
#30
assert(dr.randomize());
tb_if.x=dr.data;
#20
$finish;		// 终止仿真
end

endmodule
  • 10
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值