|
我设计了一个双向端口的RAM,代码如下:
`timescale 10ns/1ns
module RAM(D_Re, D_Im, Addr, Cs_b, Re_b, We_b);
parameter WORD_SIZE=16;
parameter ADDR_SIZE=10;
parameter MEM_DEPTH=2048;
parameter Hi_Z_pattern=16'bzzzz_zzzz_zzzz_zzzz;
inout[WORD_SIZE-1:0] D_Re, D_Im;
input[ADDR_SIZE-1:0] Addr;
input Cs_b, Re_b, We_b;
reg[WORD_SIZE-1:0]RAM[MEM_DEPTH-1:0];
reg[WORD_SIZE-1:0] Data_Re, Data_Im;
assign D_Re=((Cs_b==0)&&(Re_b==0)&&(We_b==1))?Data_Re:Hi_Z_pattern;
assign D_Im=((Cs_b==0)&&(Re_b==0)&&(We_b==1))?Data_Im:Hi_Z_pattern;
always@(Cs_b or We_b or Re_b or D_Re or D_Im or Addr)
begin
Data_Re=Hi_Z_pattern;
Data_Im=Hi_Z_pattern;
if((Cs_b==0)&&(We_b==0)&&(Re_b==1))
begin
RAM[{Addr,1'b0}]=D_Re;
RAM[{Addr,1'b1}]=D_Im;
end
else if((Cs_b==0)&&(We_b==1)&&(Re_b==0))
begin
Data_Re=RAM[{Addr,1'b0}];
Data_Im=RAM[{Addr,1'b1}];
end
end
endmodule
用如下的test_bench进行测试,可读可写,没有问题
`timescale 10ns/1ns
module test_RAM;
parameter BIT_NUM=15;
reg[BIT_NUM:0] Re_in, Im_in;
reg[9:0] Addr;
reg Cs_b, Re_b, We_b;
wire[BIT_NUM:0] Re_out, Im_out;
wire[BIT_NUM:0] Re_t, Im_t;
RAM U1_RAM(.D_Re(Re_t),
.D_Im(Im_t),
.Addr(Addr),
.Cs_b(Cs_b),
.Re_b(Re_b),
.We_b(We_b));
assign Re_t=((~We_b)&&Re_b)?Re_in:16'bzzzz_zzzz_zzzz_zzzz;
assign Im_t=((~We_b)&&Re_b)?Im_in:16'bzzzz_zzzz_zzzz_zzzz;
assign Re_out=((~Re_b)&&We_b)?Re_t:16'bzzzz_zzzz_zzzz_zzzz;
assign Im_out=((~Re_b)&&We_b)?Im_t:16'bzzzz_zzzz_zzzz_zzzz;
reg[9:0] i,j;
initial
begin
for(i=0;i<1023;i=i+1)
begin
#10 Addr=i;
Re_in=i;
Im_in=4*i;
Cs_b=1'b0;
Re_b=1'b1;
We_b=1'b0;
end
for(j=0;j<1023;j=j+1)
begin
#10 Addr=j;
Cs_b=1'b0;
Re_b=1'b0;
We_b=1'b1;
end
end
endmodule
但是我用如下三态总线连接时却不能读也不能写,请高手帮我指点下,谢谢先啦!
`timescale 10ns/1ns
module Bi_Dir_Data_Bus(Re_to_from_bus,
Im_to_from_bus,
Re_to_Data_Bus,
Im_to_Data_Bus,
Re_from_Data_Bus,
Im_from_Data_Bus,
Cs_b,
Re_b,
We_b);
parameter BIT_NUM=15;
inout[BIT_NUM:0] Re_to_from_bus, Im_to_from_bus;
input Cs_b, Re_b, We_b;
input[BIT_NUM:0]Re_to_Data_Bus, Im_to_Data_Bus;
output[BIT_NUM:0]Re_from_Data_Bus, Im_from_Data_Bus;
assign Re_from_Data_Bus=((Cs_b==0)&&(Re_b==0)&&(We_b==1))?Re_to_from_bus:16'bzzzz_zzzz_zzzz_zzzz;
assign Im_from_Data_Bus=((Cs_b==0)&&(Re_b==0)&&(We_b==1))?Im_to_from_bus:16'bzzzz_zzzz_zzzz_zzzz;
assign Re_to_from_Bus=((Cs_b==0)&&(Re_b==1)&&(We_b==0))?Re_to_Data_Bus:16'bzzzz_zzzz_zzzz_zzzz;
assign Im_to_from_Bus=((Cs_b==0)&&(Re_b==1)&&(We_b==0))?Im_to_Data_Bus:16'bzzzz_zzzz_zzzz_zzzz;
endmodule |
|