搜索
bottom↓
回复: 3

大家帮忙看看这段verilog代码,仿真结果怎么会这样呢?

[复制链接]

出0入0汤圆

发表于 2010-12-9 20:43:32 | 显示全部楼层 |阅读模式
module cd16
(
  input start ,
  input clk,
  input rst,
  input [7:0] dat_i,
  
  output reg e,
  output reg rw,
  output reg di,
  output reg[7:0] dat

);

parameter DIV_WORK_CLOCK = 6600;//产生10kHz信号控制lcd.e,设置为100kHz时复位不灵敏
parameter DIV_WORK_CLOCK_WIDTH = 16;


//wire p_clk ;
//reg[wigth-1:0] clk_e_counter ;
reg [3:0] state ;
reg [3:0] instruction_state ;
reg [3:0] instruction_next_state ;
reg [9:0] instruction[3:0] ;
reg instruction_set ;
reg [3:0] instruction_length ;
reg [3:0] instruction_index ;

wire [9:0] action_struction ;
wire p_clk_e;//10kHz
reg[DIV_WORK_CLOCK_WIDTH-1:0] clk_e_counter;


assign action_struction = instruction[instruction_length-1-instruction_index] ;
assign p_clk_e = (clk_e_counter==0)?1'B1:1'B0;

always@(posedge clk,negedge rst,posedge instruction_set)
begin

  if(rst == 0)
           begin
                   e  <= 0 ;
                   rw <= 0 ;
                   di <= 0 ;
                   //instruction_set <= 0 ;
                   state <= 0 ;
           end
   else if(instruction_set == 1'B1)
       begin
           //instruction_set <= 0 ;
           instruction_index <= instruction_length - 1'B1 ;
           end
   else if(clk == 1'B1)
           begin
             case(state)
               4'd0 :
                 begin
                           e  <= 0 ;
                           rw <= 0 ;
                           di <= 0 ;
                           if(instruction_length > 0)
                                   begin
                                   state <= 4'd1 ;
                                   end
                 end
               
                4'd1:
                begin
                di <= action_struction[9] ;
                rw <= action_struction[8] ;
                dat <= action_struction[7:0] ;
                state <= 4'd2 ;
                end
                
                4'd2:
                begin
                e <= 0 ;
                if(p_clk_e == 1'b1)
                                begin
                                state <= 4'd3 ;
                                end
                end
                   
                4'd3:
                begin
                e <= 1'b1 ;
                if(p_clk_e == 1'b1)
                   begin
                   state <= 4'd4 ;
                   end
                end
                
                4'd4:
                begin
                e <= 0 ;
                if(p_clk_e == 1'b1)
                   begin
                     if(instruction_index == 1'B0)
                        begin
                        state <= 4'd5 ;
                        end
                      else
                        begin
                        instruction_index <= instruction_index - 1'b1 ;
                        state <= 4'd1 ;
                        end   
                   end
                end
                
               4'd5:
               begin
                 if(instruction_length == 0)
                 state <= 4'd0 ;
               end
               
              default: state <= 4'd0 ;
             
            endcase
      
           end
end

always@(posedge clk,negedge rst)
begin
   
   if(rst == 0)
     begin
     instruction_length <= 0 ;
     instruction_set <= 0 ;
     instruction_state <= 4'd1 ;
     end          
   else if(clk == 1'b1)
     begin
       case(instruction_state)
         4'd0:
           begin
           if(start == 1'b1)       instruction_state <= 4'd4;
           end
           
          4'd1:
          begin
          instruction[0] <= 10'b0000110000 ;
          instruction[1] <= 10'b0000001111 ;
          instruction[2] <= 10'b0000000110 ;
          instruction[3] <= 10'b0000000001 ;
          instruction_length <= 4 ;
          instruction_set <= 1'b1 ;
          instruction_next_state <= 4'd0;
          instruction_state <=4'd2 ;         
          end
         
          4'd2:
          begin
          instruction_set <= 1'B0 ;
          instruction_state <=4'd3 ;
          end
         
          4'd3:
          begin
          if(start == 1'b0)
          instruction_state <= instruction_next_state ;
          end
         
          4'd4:
          begin
          instruction[0] <= {2'b10,dat_i} ;
          instruction_length <= 1 ;
          instruction_set <= 1'b1 ;
          instruction_next_state <= 4'd0 ;
          instruction_state <= 4'd2 ;
          end
         
          default:
            instruction_state <= 4'd0 ;
         
        endcase   
     end  
     
end

always@(negedge clk, negedge rst)
        begin
                
                if (rst == 1'B0)
                        clk_e_counter <= 0;
                       
        else if(clk == 1'B0)
        begin
                if (clk_e_counter == DIV_WORK_CLOCK-1'B1)
                        clk_e_counter <= 0;
                else
                        clk_e_counter <= clk_e_counter+1'B1;
                end
        end
       
endmodule       

(原文件名:shixu2.png)
仿真光标所指处,两个状态机分别执行到了state=4'd2,instruction_state=4'd2。但是奇怪instruction_index怎么会变成0的?

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2010-12-9 23:38:29 | 显示全部楼层
看晕我了

出0入0汤圆

发表于 2010-12-10 09:01:31 | 显示全部楼层
看不晕的都是高人

出0入0汤圆

 楼主| 发表于 2010-12-14 22:01:29 | 显示全部楼层
原因找到了,上面的状态机写得有点乱!
导致我上回没有想明白的问题是第二个状态机少了个限制条件,从而导致整段代码完全不能按照我的要求来工作。
下面为1602驱动代码:
仿真产生信号ourdev_604689S2J3JX.txt(文件大小:334字节) (原文件名:clock.txt)
分频ourdev_604690JRX4WE.txt(文件大小:544字节) (原文件名:div_sim.txt)
计数模块ourdev_604691YL84QG.txt(文件大小:2K) (原文件名:counter.txt)
译码模块ourdev_604692ZSYNUR.txt(文件大小:1K) (原文件名:bcd_ascii.txt)
1602控制模块ourdev_604693C5WNIE.txt(文件大小:4K) (原文件名:cd16.txt)

modelsim仿真时序 (原文件名:时序1.jpg)

以上代码下载板子上测试过的
总是看大家的帖子,今天我也贡献下!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 15:31

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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