//
// 第一題 : 數據提取模塊
//
// 輸入:
// clk
// rst_n
// din[255:0]
// offset[6:0]
// 輸出
// dout[127:0]
//
// 要求:
// 1. 要求根據偏移量offset,從din中提取出128bit到dout中。用僞代碼表示如下:
// dout = din[offset+128:offset]
// 2. 可以流水線操作,din和offset每個時鐘都會變化
// 3. 在合理的延遲下,儘可能的降低資源的使用
//
module test1 (
input clk,
input rst_n,
input [255:0] din,
input [6:0] offset,
output reg [127:0] dout
);
always @(posedge clk ) begin
if(!rst_n)
dout <= 128'b0;
else case(offset[6:4])
0: dout <= din[0+offset[3:0]+:128]
1: dout <= din[16+offset[3:0]+:128]
2: dout <= din[32+offset[3:0]+:128]
3: dout <= din[48+offset[3:0]+:128]
4: dout <= din[64+offset[3:0]+:128]
5: dout <= din[80+offset[3:0]+:128]
6: dout <= din[96+offset[3:0]+:128]
7: dout <= din[112+offset[3:0]+:128]
default:;
endcase
end
endmodule
上題考的是資源的佔用
如果直接用dout = din[offset+:128],則爲錯誤答案
//
// 第二題 :頻率產生模塊
//
// 輸入:
// clk
// rst_n
//
// 輸出
// fs_step
//
// 要求:
// 1. clk主頻爲25Mhz
// 2. 要求產生一個週期爲4.092Mhz的脈衝fs_step,脈衝寬度爲1個時鐘週期
// 3. 在合理的延遲下,儘可能的降低資源的使用
//
//
//
// 25_000_000 4_092_000
// __________ = __________ ==> X= 703_000_247
//
// 2^32 X
//
//
module test2 (
input clk,
input rst_n,
output reg fs_step
);
reg [31:0] cnt;
always @(posedge clk ) begin
if(!rst_n)
cnt <= 'b0;
else
cnt <= cnt + 32'd703_000_247;
end
always @(posedge clk ) begin
if(!rst_n)
fs_step <= 'b0;
else if(cnt==0)
fs_step <= 1'b1;
else
fs_step <= 'b0;
end
endmodule