%产生两个正弦信号sin(x)和sin(8x)叠加后的信号,取128个点,将信号放大,
%转换成无符号数据,存入ROM中作为信号源
clear all
clc
depth =128;
width =16;
x =0:2*pi/(depth-1):2*pi;
y =sin(x)+sin(8*x);plot(x,sin(x),'r')%红 {MOD}为sin(x)函数
hold on
plot(x,sin(8*x),'g')%绿 {MOD}为sin(8x)函数
hold on
plot(x,y,'b')%蓝 {MOD}为生成的混合信号
grid
y =(y/2)*32768;%将信号放大32768倍
b =signed2unsigned(y,width);%转换为无符号数输入
%下面函数重新新建一个脚本文件
%需要调用了如下函数,将有符号数转换成无符号数
function b =signed2unsigned(a,wl)%This function covert an signed integer number into an unsinged integer
%number. a is the input vector while wl means the width of input number;%Example: a =[-2,-1,0,1];%signed2unsigned(a,3); THEN return[2,3,0,1]
k =2^(wl)*(a<0);
b = k + a;;
b =fix(b+0.5);for i =1:length(a)if(b(i)==65536)b(i)=0;
end
end
编写mif文件
%编写mif文件
fid =fopen('sinx.mif','wt');%将信号写入一个.mif文件中
fprintf(fid,'WIDTH=%d;
',width);%写入存储位宽8位
fprintf(fid,'DEPTH=%d;
',depth);%写入存储深度1024fprintf(fid,'ADDRESS_RADIX=UNS;
');%写入地址类型为无符号整型
fprintf(fid,'DATA_RADIX=UNS;');%写入数据类型为无符号整型
fprintf(fid,'CONTENT BEGIN
');%起始内容
for num=0:127fprintf(fid,'%d:%16.0f;
',num,b(num+1));
end
fclose(fid);
运行整套代码我们也可以看见:
红 {MOD}是2MHz的正弦波,绿 {MOD}是8MHz的正弦波,蓝 {MOD}是混合信号.
我们可以用移位寄存器来达到输入信号的延时.
//pipeline 1
always @ (posedge CLK_50M or negedge RST_N)
begin
if(!RST_N)begin
data_shift[0]<=0;
data_shift[1]<=0;
data_shift[2]<=0;
data_shift[3]<=0;
data_shift[4]<=0;
data_shift[5]<=0;
data_shift[6]<=0;
data_shift[7]<=0;
data_shift[8]<=0;
data_shift[9]<=0;
data_shift[10]<=0;
end
else begin
data_shift[10]<= data_shift[9];
data_shift[9]<= data_shift[8];
data_shift[8]<= data_shift[7];
data_shift[7]<= data_shift[6];
data_shift[6]<= data_shift[5];
data_shift[5]<= data_shift[4];
data_shift[4]<= data_shift[3];
data_shift[3]<= data_shift[2];
data_shift[2]<= data_shift[1];
data_shift[1]<= data_shift[0];
data_shift[0]<= data_rom;
end
end
part2-乘法器
这里本来想例化乘法器ip来写的,但是鉴于很懒,所以就直接在verilog上面的乘号来代替,将优化丢给了编译器.(所以下面的代码框架是别人家的)
always @ (posedge CLK_50M or negedge RST_N)
begin
if(!RST_N)
mul_data[0]<=0;else
mul_data[0]<= data_shift[0]* COEFF1;
end
always @(posedge CLK_50M or negedge RST_N)begin
if(!RST_N)
mul_data[1]<=0;else
mul_data[1]<= data_shift[1]* COEFF2;
end
………………………………………………………………………
always @(posedge CLK_50M or negedge RST_N)begin
if(!RST_N)
mul_data[9]<=0;else
mul_data[9]<= data_shift[9]* COEFF10;
end
always @(posedge CLK_50M or negedge RST_N)begin
if(!RST_N)
mul_data[10]<=0;else
mul_data[10]<= data_shift[10]* COEFF11;
end
part3-加法器
这里给出两种写法,顺道说明一下fpga里面的些许技巧
类c写法
always @(posedge CLK_50M or negedge RST_N)begin
if(!RST_N)begin
dout_r <=0;
end
else
dout_r <= mul_data[0]+mul_data[1]+mul_data[2]+mul_data[3]+mul_data[4]+mul_data[5]+mul_data[6]+mul_data[7]+mul_data[8]+mul_data[9]+mul_data[10];
end