📄 ps2key.v
字号:
`timescale 1ns / 1ps//////////////////////////////////////////////////////////////////////////////////// Company: // Engineer: // // Create Date: 20:19:33 03/10/2007 // Design Name: // Module Name: ps2key // Project Name: // Target Devices: // Tool versions: // Description: //// Dependencies: //// Revision: // Revision 0.01 - File Created// Additional Comments: ////////////////////////////////////////////////////////////////////////////////////`define LEFT_SHIFT 8'h12//16'h12????`define RIGHT_SHIFT 8'h59`define RELEASE_CODE 8'hf0//`define EXTEND_CODEmodule ps2key(clk, rst_n, ps2c, ps2d, dout, ready); input clk; input rst_n; input ps2c; input ps2d; output [7:0] dout; output ready;reg ready;reg ps2c_delay;//reg ps2d_delay;wire ps2clk_l;//wire ps2data;wire clk10u;wire release_key;wire shift_on;reg pull_down;reg enstart;reg [8:0] cnt10u;//84reg [4 : 0] scan_count;always @ (posedge clk or negedge rst_n)if(!rst_n) cnt10u <= 0 ;else cnt10u <= cnt10u + 1;assign clk10u = (cnt10u == 9'b111111111) ;//wire clk10u_up ;//assign clk10u= (cnt10u == 5'b11111); always @ (posedge clk)if(clk10u) ps2c_delay <= ps2c; reg [15:0] delay1ms; //15//debounce delay (prevent error fall)always @ (posedge clk or negedge rst_n)if(!rst_n) delay1ms <= 0 ;else if(delay1ms == 16'hffff) delay1ms <=0;else if(enstart) delay1ms <= delay1ms + 1;always @ (posedge clk)if (!rst_n) enstart <= 0 ;else if(delay1ms == 16'hffff) enstart <= 0 ;else if(ps2clk_l&&(scan_count == 0)) enstart <= 1;assign ps2clk_l =~ps2c&&(ps2c_delay); //if ps2c down >>>ps2clk_1 set up always @ (posedge clk or negedge rst_n)if(!rst_n) scan_count <= 0 ;else if ((scan_count == 11)||(~enstart)) scan_count <= 0 ;else if (ps2clk_l&&clk10u) scan_count <= scan_count + 1;reg [10 :0 ] rxbuf ;/*reg [3:0] ready_delay; always @ (posedge clk or negedge rst_n)if(!rst_n) ready_delay <= 0 ;else if (scan_count == 11) ready_delay <= 0 ;else if ((scan_count == 10) && ready_delay <= 4'b1110) ready_delay <= ready_delay + 1 ;*/ always @ (posedge clk or negedge rst_n)if(!rst_n) rxbuf<=0 ;//else if (ps2clk_l) rxbuf[scan_count]<=ps2d;reg [7:0] keydata;reg [8:0] q;always @ (posedge clk or negedge rst_n)if (!rst_n) q <= 0 ;else q <= rxbuf [9:1] ;always @ (posedge clk or negedge rst_n)if(!rst_n) ready <= 0 ;else if( (q[0]^q[1]^q[2]^q[3]^q[4]^q[5]^q[6]^q[7]==(~q[8]))&& (scan_count == 10) &&// ~ready_delay[3]&& ps2clk_l && (~shift_on) && pull_down) ready <= 1 ;else ready <= 0 ;reg rx_shift_on;assign release_key =( q[7:0] == `RELEASE_CODE) ;assign shift_on =((q[7:0] ==`LEFT_SHIFT)||(q[7:0] ==`RIGHT_SHIFT));always @ (posedge clk or negedge rst_n)if(!rst_n) rx_shift_on <= 0 ;else if ((~pull_down)&&(shift_on)) rx_shift_on <= 0;//else if ()else if (shift_on&&(scan_count == 9)) rx_shift_on <= 1;always @ (posedge clk or negedge rst_n)if(!rst_n) pull_down <= 1;//1else if (release_key) pull_down <= 0 ;else begin if((scan_count == 11)&& (~release_key)) pull_down <= 1'b1 ; endalways @ (posedge clk or negedge rst_n)beginif(!rst_n) keydata <= 0;else if(pull_down && (~shift_on)) begin if ((scan_count == 10)) keydata <= q[7:0]; endend//assign ready = (scan_count == 10);wire [11:0] keycode;reg [7:0] ascii;//assign rx_shift_on = 0 ;assign keycode = {3'b0,rx_shift_on,keydata};always @(keycode)begin casez (keycode) 12'h?66 : ascii <= 8'h08; // Backspace ("backspace" key) 12'h?0d : ascii <= 8'h09; // Horizontal Tab 12'h?5a : ascii <= 8'h0d; // Carriage return ("enter" key) 12'h?76 : ascii <= 8'h1b; // Escape ("esc" key) 12'h?29 : ascii <= 8'h20; // Space 12'h116 : ascii <= 8'h21; // ! 12'h152 : ascii <= 8'h22; // " 12'h126 : ascii <= 8'h23; // # 12'h125 : ascii <= 8'h24; // $ 12'h12e : ascii <= 8'h25; // % 12'h13d : ascii <= 8'h26; // & 12'h052 : ascii <= 8'h27; // ' 12'h146 : ascii <= 8'h28; // ( 12'h145 : ascii <= 8'h29; // ) 12'h13e : ascii <= 8'h2a; // * 12'h155 : ascii <= 8'h2b; // + 12'h041 : ascii <= 8'h2c; // , 12'h04e : ascii <= 8'h2d; // - 12'h049 : ascii <= 8'h2e; // . 12'h04a : ascii <= 8'h2f; // / 12'h045 : ascii <= 8'h30; // 0 12'h016 : ascii <= 8'h31; // 1 12'h01e : ascii <= 8'h32; // 2 12'h026 : ascii <= 8'h33; // 3 12'h025 : ascii <= 8'h34; // 4 12'h02e : ascii <= 8'h35; // 5 12'h036 : ascii <= 8'h36; // 6 12'h03d : ascii <= 8'h37; // 7 12'h03e : ascii <= 8'h38; // 8 12'h046 : ascii <= 8'h39; // 9 12'h14c : ascii <= 8'h3a; // : 12'h04c : ascii <= 8'h3b; // ; 12'h141 : ascii <= 8'h3c; // < 12'h055 : ascii <= 8'h3d; // = 12'h149 : ascii <= 8'h3e; // > 12'h14a : ascii <= 8'h3f; // ? 12'h11e : ascii <= 8'h40; // @ 12'h11c : ascii <= 8'h41; // A 12'h132 : ascii <= 8'h42; // B 12'h121 : ascii <= 8'h43; // C 12'h123 : ascii <= 8'h44; // D 12'h124 : ascii <= 8'h45; // E 12'h12b : ascii <= 8'h46; // F 12'h134 : ascii <= 8'h47; // G 12'h133 : ascii <= 8'h48; // H 12'h143 : ascii <= 8'h49; // I 12'h13b : ascii <= 8'h4a; // J 12'h142 : ascii <= 8'h4b; // K 12'h14b : ascii <= 8'h4c; // L 12'h13a : ascii <= 8'h4d; // M 12'h131 : ascii <= 8'h4e; // N 12'h144 : ascii <= 8'h4f; // O 12'h14d : ascii <= 8'h50; // P 12'h115 : ascii <= 8'h51; // Q 12'h12d : ascii <= 8'h52; // R 12'h11b : ascii <= 8'h53; // S 12'h12c : ascii <= 8'h54; // T 12'h13c : ascii <= 8'h55; // U 12'h12a : ascii <= 8'h56; // V 12'h11d : ascii <= 8'h57; // W 12'h122 : ascii <= 8'h58; // X 12'h135 : ascii <= 8'h59; // Y 12'h11a : ascii <= 8'h5a; // Z 12'h054 : ascii <= 8'h5b; // [ 12'h05d : ascii <= 8'h5c; // \ 12'h05b : ascii <= 8'h5d; // ] 12'h136 : ascii <= 8'h5e; // ^ 12'h14e : ascii <= 8'h5f; // _ 12'h00e : ascii <= 8'h60; // ` 12'h01c : ascii <= 8'h61; // a 12'h032 : ascii <= 8'h62; // b 12'h021 : ascii <= 8'h63; // c 12'h023 : ascii <= 8'h64; // d 12'h024 : ascii <= 8'h65; // e 12'h02b : ascii <= 8'h66; // f 12'h034 : ascii <= 8'h67; // g 12'h033 : ascii <= 8'h68; // h 12'h043 : ascii <= 8'h69; // i 12'h03b : ascii <= 8'h6a; // j 12'h042 : ascii <= 8'h6b; // k 12'h04b : ascii <= 8'h6c; // l 12'h03a : ascii <= 8'h6d; // m 12'h031 : ascii <= 8'h6e; // n 12'h044 : ascii <= 8'h6f; // o 12'h04d : ascii <= 8'h70; // p 12'h015 : ascii <= 8'h71; // q 12'h02d : ascii <= 8'h72; // r 12'h01b : ascii <= 8'h73; // s 12'h02c : ascii <= 8'h74; // t 12'h03c : ascii <= 8'h75; // u 12'h02a : ascii <= 8'h76; // v 12'h01d : ascii <= 8'h77; // w 12'h022 : ascii <= 8'h78; // x 12'h035 : ascii <= 8'h79; // y 12'h01a : ascii <= 8'h7a; // z 12'h154 : ascii <= 8'h7b; // { 12'h15d : ascii <= 8'h7c; // | 12'h15b : ascii <= 8'h7d; // }// 12'h10e : ascii <= 8'h7e; // ~ 12'h0f0 : ascii <= 8'h7e; // ~ 12'h?71 : ascii <= 8'h7f; // (Delete OR DEL on numeric keypad)
// SHORT KEY
12'h079 : ascii <= 8'h2b; // +
12'h07c : ascii <= 8'h2a; // *
12'h070 : ascii <= 8'h30; // 0 12'h069 : ascii <= 8'h31; // 1 12'h072 : ascii <= 8'h32; // 2 12'h07a : ascii <= 8'h33; // 3 12'h06b : ascii <= 8'h34; // 4 12'h073 : ascii <= 8'h35; // 5 12'h074 : ascii <= 8'h36; // 6 12'h06c : ascii <= 8'h37; // 7 12'h075 : ascii <= 8'h38; // 8 12'h07d : ascii <= 8'h39; // 9
12'h07b : ascii <= 8'h2d; // -
12'h04a : ascii <= 8'h2f; // /
12'h066 : ascii <= 8'h08; // Backspace ("backspace" key)
12'h071 : ascii <= 8'h02;// (Delete OR DEL on numeric keypad)
12'h077 : ascii <= 8'h04;// numeric keypad)
12'h05a : ascii <= 8'h01; // Carriage return ("enter" key)
//short key
default : ascii <= 8'h2e; // '.' used for unlisted characters. endcaseendassign dout = ascii ;endmodule
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -