#|HDLBits 系列(29)PS/2 mouse protocol(PS/2 packet parser and datapath)
目录
序言
原题传送
题目解释
我的设计
序言 上篇博客:
HDLBits 系列(28)PS/2 mouse protocol(PS/2 packet parser)
只对PS/2 mouse protocol的数据流检测到了之后输出检测到了的标志done,但是没有输出检测数据,这一篇博客接着设计。
原题传送 Fsm ps2data
Now that you have a state machine that will identify three-byte messages in a PS/2 byte stream, add a datapath that will also output the 24-bit (3 byte) message whenever a packet is received (out_bytes[23:16] is the first byte, out_bytes[15:8] is the second byte, etc.).
【#|HDLBits 系列(29)PS/2 mouse protocol(PS/2 packet parser and datapath)】out_bytes needs to be valid whenever the done signal is asserted. You may output anything at other times (i.e., don't-care).
文章图片
题目解释 有几个重点是:
done有效的时候要输出接受数据,这就意味着在状态DONE时刻,不能在改变输出数据了,然而,在DONE状态下有可能输入的数据是第一个字节,所以怎么办呢?
我的设计 那就将输入数据和状态延迟一拍吧,在D2状态里面处理,我的设计如下:
module top_module(
input clk,
input [7:0] in,
input reset,// Synchronous reset
output [23:0] out_bytes,
output done);
//reg [1:0] state, next_state;
localparam D1 = 0, D2 = 1, D3 = 2, DONE = 3;
// State transition logic (combinational)
always@(*) begin
case(state)
D1: begin
if(in[3] == 1) next_state = D2;
else next_state = D1;
end
D2: begin
next_state = D3;
end
D3: begin
next_state = DONE;
end
DONE: begin
if(in[3] == 1) next_state = D2;
else next_state = D1;
end
default: begin
next_state = D1;
end
endcase
endalways@(posedge clk) begin
if(reset) state <= D1;
else state <= next_state;
end// New: Datapath to store incoming bytes.
reg [7:0] in_r;
reg [1:0] state_r;
always@(posedge clk) begin
if(reset) begin
in_r <= 0;
state_r <= D1;
end
else begin
state_r <= state;
in_r <= in;
end
endreg [23:0] out_bytes_mid;
always@(*) begin
case(state)
D1: begin
if(in[3] == 1) out_bytes_mid[23:16] = in;
end
D2: begin
if(state_r == DONE && in_r[3] == 1) begin
out_bytes_mid[15:8] = in;
out_bytes_mid[23:16] = in_r;
end
else out_bytes_mid[15:8] = in;
end
D3: begin
out_bytes_mid[7:0] = in;
end
DONE: begin
;
end
default: begin
;
end
endcase
end
assign out_bytes = (done == 1)? out_bytes_mid:'bx;
assign done = (state == DONE)?1:0;
endmodule
测试成功。
推荐阅读
- 【欢喜是你·三宅系列①】⑶
- 你不可不知的真相系列之科学
- 人脸识别|【人脸识别系列】| 实现自动化妆
- 2018-06-13金句系列7(金句结构-改编古现代诗词)
- Unity和Android通信系列文章2——扩展UnityPlayerActivity
- 乡野村趣系列之烧仙草
- Java内存泄漏分析系列之二(jstack生成的Thread|Java内存泄漏分析系列之二:jstack生成的Thread Dump日志结构解析)
- 15、IDEA学习系列之其他设置(生成javadoc、缓存和索引的清理等)
- 【年终激励系列】之五(年终奖如何与考核紧密相连)
- 剥削劳动力系列(企业家剥削你时,他要付出巨大的代价)