集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 3031|回复: 5

求助:关于Verilog的问题

[复制链接]
ATA 发表于 2010-6-27 23:25:06 | 显示全部楼层 |阅读模式
我设计的模块中输入信号datain是8位的数字,它在时钟上升沿连续的输入不同的数字,我用寄存器datacount统计输入数字的个数,然后用寄存器regdata[23:0]来存储输入的第一个至第三个数字,datacount=1时,第一个数字存储在regdata[23:16],
datacount=2时,第二个数字存储在regdata[15:8],datacount=3时,第三个数字存储在regdata[7:0]中,之后想把这三个数字从regdata[23:0]中作为一个整体调用,可是我编写的程序在进行功能仿真时发现在datacount=2时,regdata[23:16]和regdata[15:8]中数据都变成了第二个数字,以后的也类似,都被新输入的数字覆盖了,请问一下想实现上述操作应该怎样编写这部分代码?我的这部分代码如下,请问有什么问题呢?应该怎么改呢?
always@(datacount)
begin
  if(datacount==3'b001)
   regdata[23:16]=datain;
else if(datacount==3'b010)
   regdata[15:8]=datain;
else if(datacount==3'b011)
  regdata[7:0]=datain;
end
ICE 发表于 2010-6-28 00:34:29 | 显示全部楼层
以下是个人意见:<br>
首先,如果你的程序中只有这一个always模块对regdata进行赋值的话,会有问题:<br>
1,没有初始化,因为你是位选择,所以有些位就会出现不定态或是高阻,仿真时可能会是红线。<br>
2,这一模块是组合逻辑,并且中间出现锁存现象,对于你所要实现的目的,建议使用时序逻辑,使用时钟驱动,并加上reset控制。<br>
3,分析你仿真出现问题,一个可能的原因是你仿真的数据设置不合适,因为这一赋值模块不和时钟同步,所以很可能出现所得非所求的现象。
inter 发表于 2010-6-28 01:05:44 | 显示全部楼层
分成三个寄存器来存这三个数据<br>
用 datacount 做使能信号<br>
最后合成一路读出
CHANG 发表于 2010-6-28 01:25:53 | 显示全部楼层
原帖由 Harva 于 2006-9-8 20:53 发表<br>
以下是个人意见:<br>
首先,如果你的程序中只有这一个always模块对regdata进行赋值的话,会有问题:<br>
1,没有初始化,因为你是位选择,所以有些位就会出现不定态或是高阻,仿真时可能会是红线。<br>
2,这一模块是组合 ... 同意,这里不使用时钟驱动的话会引入锁存器
 楼主| ATA 发表于 2010-6-28 02:50:30 | 显示全部楼层
我个人根据你的描述编写的程序,不知有没有帮助:<br>
module datareg(<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //input <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; clk,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; reset,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; datain,<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; //output<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; regdata<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; );<br>
//input description<br>
input clk;<br>
input reset;<br>
input [7:0] datain;<br>
//output description<br>
output [23:0] regdata;<br>
//internal signal description<br>
reg [1:0] datacount;<br>
reg [7:0] data;<br>
<br>
//此模块用以计数,记录输入datain在时钟沿时不同的三个时刻,<br>
//不知道这样的设计是不是楼主想要的,也就是此时决不会出现regdata的前后中3个8位数会完全不同,而不是只要时钟来时就采样,是采不同的值然后三个并行输出<br>
always@(posedge clk or negedge reset)<br>
begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;if(!reset)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; datacount&lt;=1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; data&lt;=8'bx;<br>
&nbsp; &nbsp;&nbsp; &nbsp;else if(data!=datain)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; data&lt;=datain;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;//此举用以判断时钟沿时输入是不是和前面的相同<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if(datacount&lt;=3)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;datacount&lt;=1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;else<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; datacount&lt;=datacount+1;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;end&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br>
end<br>
<br>
always@(posedge clk or negedge reset)<br>
begin<br>
&nbsp; &nbsp;&nbsp; &nbsp;if(!reset)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;regdata&lt;=0;<br>
&nbsp; &nbsp;&nbsp;&nbsp;else <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;case(datacount)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 2'01: regdata[23:16]&lt;=datain;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 2'10: regdata[15:8]&lt;=datain;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 2'11: regdata[7:0]&lt;=datain;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; default: regdata&lt;=24'x;<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;endcase<br>
end<br>
endmodule<br>
<br>
[ 本帖最后由 Harva 于 2006-9-8 21:12 编辑 ]
ngtim 发表于 2010-6-28 04:31:41 | 显示全部楼层
其实在这个模块里还有一个data_ready信号,当data_ready=1时表示有数据输入,计数器datacount加1。data_ready是在时钟的上升沿触发的。<br>
我按照你们的意见,修改了一下,用时钟作为触发信号,并分成三个8位的寄存器regdata0,regdata1,regdata2来分别存放输入的数,最后合成一个数,可结果还是一样,请问还存在什么问题呢?程序如下:<br>
always@(clk16x)<br>
begin<br>
&nbsp;&nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;if(datacount==3'b001)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; regdata0=dout;&nbsp; &nbsp;<br>
&nbsp; &nbsp;&nbsp; &nbsp;else if(datacount==3'b010)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; regdata1=dout;<br>
&nbsp; &nbsp;&nbsp; &nbsp;else if(datacount==3'b011) <br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;regdata2=dout;<br>
&nbsp; &nbsp;&nbsp; &nbsp;else if(datacount==3'b100)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cmpdata[20:16]=dout;<br>
&nbsp; &nbsp;&nbsp;&nbsp;else if(datacount==3'b101)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cmpdata[15:8]=dout;<br>
&nbsp; &nbsp;&nbsp;&nbsp;else if(datacount==3'b110)<br>
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;cmpdata[7:0]=dout;<br>
end<br>
<br>
always @(posedge clk16x)<br>
begin<br>
&nbsp; &nbsp;regdataout={regdata0,regdata1,regdata2};<br>
end
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|fpga论坛|fpga设计论坛 ( 京ICP备20003123号-1 )

GMT+8, 2024-12-24 10:18 , Processed in 7.483272 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表