马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 记忆的沙漏 于 2011-5-3 10:48 编辑
自己编了一个代码,modelsim编译通过了,但是仿真出不来,求大神指教
`timescale 1 ns/
1ns
module ts_mux(
ts_in,
clk,
sync,
rst,
din,
en,
ts_out
);
parameter P=8'hff; //判断第三字节是否全为1
parameter a1=2'b00,a2=2'b01,a3=2'b11,a4=2'b10; //四个状态
input [7:0] ts_in;
output [7:0] ts_out;
input rst;
input en,clk;
input sync; //同步头,每帧的开始,均为h47
input [7:0] din; //控制数据
reg [7:0] ts_out; //输出
reg [3:0] acount;
reg read_in; //fifo写信号
reg [1:0] state;
wire stack_full,stack_empty,stack_almost_full;
reg [2:0] read_ptr,write_ptr;
always@(posedge clk or posedge rst)
if(rst)begin
ts_out[7:0]<=8'h00;
acount<=0;
state<=a1;
read_in<=0;
end
else case(state)
a1: begin //check the sync
acount<=0;
if(sync==1)begin
state<=a2;
ts_out[7:0]<=ts_in[7:0];
end
else begin
state<=a1;
ts_out[7:0]<=ts_in[7:0];
end
end
a2: begin //secound
if(ts_in[4:0]==5'b11111)
begin
state<=a3;
ts_out[7:0]<=ts_in[7:0];
end
else begin
state<=a1;
ts_out[7:0]<=ts_in[7:0];
end
end
a3: begin //third
if(ts_in==P&&stack_almost_full==1)
begin
state<=a4;
ts_out[7:0]<=ts_in[7:0];
read_in<=1; //read_in ,read from the stack
end
else begin
state<=a1;
ts_out[7:0]<=ts_in[7:0];
end
end
a4 : begin
acount<=acount+1;
if(acount==8'd7)begin
state<=a1;
acount<=0;
read_in<=0;
end
end
default :begin
state<=a1;
acount<=0;
read_in<=0;
ts_out[7:0]<=ts_in[7:0];
end
endcase
fifo_buffer M1(din,ts_out,en,read_in,stack_almost_full,stack_full,stack_empty,clk,rst); //调用fifo模块
endmodule
module fifo_buffer(
Data_in ,
Data_out,
write_to_stack, // write signalling depends on the en signal
read_from_stack,
stack_almost_full,
stack_full,
stack_empty,
clk,
rst
);
output [7:0] Data_out;
input [7:0] Data_in ;
input write_to_stack,read_from_stack;
input clk,rst;
output stack_full,stack_empty,stack_almost_full;
reg [stack_ptr_width-1:0] read_ptr,write_ptr;
reg [stack_ptr_width:0] ptr;
reg [7:0] Data_out;
reg [7:0] stack[stack_height-1:0];
parameter stack_height=10;
parameter AF_level=7; //判断是不是大于7个字节
parameter stack_ptr_width=3;
//info of stack
assign stack_full=(ptr==stack_height);
assign stack_empty=(ptr==0);
assign stack_almost_full=(ptr>=AF_level);
always@(posedge clk or posedge rst)
if(rst)begin
read_ptr<=0;
write_ptr<=0;
ptr<=0;
end
else if(write_to_stack&&(!stack_full)&&(!read_from_stack))begin
stack[write_ptr]<=Data_in ; //write
write_ptr<=write_ptr+1;
ptr<=ptr+1;
end
else if((!write_to_stack)&&(!stack_empty)&&read_from_stack)begin
Data_out<= stack[read_ptr]; //read
read_ptr<=read_ptr+1;
ptr<=ptr-1;
end
else if(write_to_stack&&stack_full&&read_from_stack)begin
Data_out<= stack[read_ptr]; //stack is full,only read
read_ptr<=read_ptr+1;
ptr<=ptr-1;
end
else if(write_to_stack&&stack_empty&&read_from_stack)begin
stack[write_ptr]<=Data_in ; //stack is emtpty,only write
write_ptr<=write_ptr+1;
ptr<=ptr+1;
end
else if(write_to_stack&&(!stack_empty)&&read_from_stack&&(!stack_full))begin
Data_out<=stack[read_ptr]; //read and write
stack[write_ptr]<=Data_in ;
write_ptr<=write_ptr+1;
read_ptr<=read_ptr+1;
end
endmodule |