| 1 |
53 |
root |
//-------------------------------------------------------------------------------------
|
| 2 |
|
|
//
|
| 3 |
|
|
// Author: John Clayton
|
| 4 |
|
|
// Date : April 30, 2001
|
| 5 |
|
|
// Update: 4/30/01 copied this file from lcd_2.v (pared down).
|
| 6 |
|
|
// Update: 5/24/01 changed the first module from "ps2_keyboard_receiver"
|
| 7 |
|
|
// to "ps2_keyboard_interface"
|
| 8 |
|
|
// Update: 5/29/01 Added input synchronizing flip-flops. Changed state
|
| 9 |
|
|
// encoding (m1) for good operation after part config.
|
| 10 |
|
|
// Update: 5/31/01 Added low drive strength and slow transitions to ps2_clk
|
| 11 |
|
|
// and ps2_data in the constraints file. Added the signal
|
| 12 |
|
|
// "tx_shifting_done" as distinguished from "rx_shifting_done."
|
| 13 |
|
|
// Debugged the transmitter portion in the lab.
|
| 14 |
|
|
// Update: 6/01/01 Added horizontal tab to the ascii output.
|
| 15 |
|
|
// Update: 6/01/01 Added parameter TRAP_SHIFT_KEYS.
|
| 16 |
|
|
// Update: 6/05/01 Debugged the "debounce" timer functionality.
|
| 17 |
|
|
// Used 60usec timer as a "watchdog" timeout during
|
| 18 |
|
|
// receive from the keyboard. This means that a keyboard
|
| 19 |
|
|
// can now be "hot plugged" into the interface, without
|
| 20 |
|
|
// messing up the bit_count, since the bit_count is reset
|
| 21 |
|
|
// to zero during periods of inactivity anyway. This was
|
| 22 |
|
|
// difficult to debug. I ended up using the logic analyzer,
|
| 23 |
|
|
// and had to scratch my head quite a bit.
|
| 24 |
|
|
// Update: 6/06/01 Removed extra comments before the input synchronizing
|
| 25 |
|
|
// flip-flops. Used the correct parameter to size the
|
| 26 |
|
|
// 5usec_timer_count. Changed the name of this file from
|
| 27 |
|
|
// ps2.v to ps2_keyboard.v
|
| 28 |
|
|
// Update: 6/06/01 Removed "&& q[7:0]" in output_strobe logic. Removed extra
|
| 29 |
|
|
// commented out "else" condition in the shift register and
|
| 30 |
|
|
// bit counter.
|
| 31 |
|
|
// Update: 6/07/01 Changed default values for 60usec timer parameters so that
|
| 32 |
|
|
// they correspond to 60usec for a 49.152MHz clock.
|
| 33 |
|
|
//
|
| 34 |
|
|
//
|
| 35 |
|
|
//
|
| 36 |
|
|
//
|
| 37 |
|
|
//
|
| 38 |
|
|
// Description
|
| 39 |
|
|
//-------------------------------------------------------------------------------------
|
| 40 |
|
|
// This is a state-machine driven serial-to-parallel and parallel-to-serial
|
| 41 |
|
|
// interface to the ps2 style keyboard interface. The details of the operation
|
| 42 |
|
|
// of the keyboard interface were obtained from the following website:
|
| 43 |
|
|
//
|
| 44 |
|
|
// http://www.beyondlogic.org/keyboard/keybrd.htm
|
| 45 |
|
|
//
|
| 46 |
|
|
// Some aspects of the keyboard interface are not implemented (e.g, parity
|
| 47 |
|
|
// checking for the receive side, and recognition of the various commands
|
| 48 |
|
|
// which the keyboard sends out, such as "power on selt test passed," "Error"
|
| 49 |
|
|
// and "Resend.") However, if the user wishes to recognize these reply
|
| 50 |
|
|
// messages, the scan code output can always be used to extend functionality
|
| 51 |
|
|
// as desired.
|
| 52 |
|
|
//
|
| 53 |
|
|
// Note that the "Extended" (0xE0) and "Released" (0xF0) codes are recognized.
|
| 54 |
|
|
// The rx interface provides separate indicator flags for these two conditions
|
| 55 |
|
|
// with every valid character scan code which it provides. The shift keys are
|
| 56 |
|
|
// also trapped by the interface, in order to provide correct uppercase ASCII
|
| 57 |
|
|
// characters at the ascii output, although the scan codes for the shift keys
|
| 58 |
|
|
// are still provided at the scan code output. So, the left/right ALT keys
|
| 59 |
|
|
// can be differentiated by the presence of the rx_entended signal, while the
|
| 60 |
|
|
// left/right shift keys are differentiable by the different scan codes
|
| 61 |
|
|
// received.
|
| 62 |
|
|
//
|
| 63 |
|
|
// The interface to the ps2 keyboard uses ps2_clk clock rates of
|
| 64 |
|
|
// 30-40 kHz, dependent upon the keyboard itself. The rate at which the state
|
| 65 |
|
|
// machine runs should be at least twice the rate of the ps2_clk, so that the
|
| 66 |
|
|
// states can accurately follow the clock signal itself. Four times
|
| 67 |
|
|
// oversampling is better. Say 200kHz at least. The upper limit for clocking
|
| 68 |
|
|
// the state machine will undoubtedly be determined by delays in the logic
|
| 69 |
|
|
// which decodes the scan codes into ASCII equivalents. The maximum speed
|
| 70 |
|
|
// will be most likely many megahertz, depending upon target technology.
|
| 71 |
|
|
// In order to run the state machine extremely fast, synchronizing flip-flops
|
| 72 |
|
|
// have been added to the ps2_clk and ps2_data inputs of the state machine.
|
| 73 |
|
|
// This avoids poor performance related to slow transitions of the inputs.
|
| 74 |
|
|
//
|
| 75 |
|
|
// Because this is a bi-directional interface, while reading from the keyboard
|
| 76 |
|
|
// the ps2_clk and ps2_data lines are used as inputs. While writing to the
|
| 77 |
|
|
// keyboard, however (which may be done at any time. If writing interrupts a
|
| 78 |
|
|
// read from the keyboard, the keyboard will buffer up its data, and send
|
| 79 |
|
|
// it later) both the ps2_clk and ps2_data lines are occasionally pulled low,
|
| 80 |
|
|
// and pullup resistors are used to bring the lines high again, by setting
|
| 81 |
|
|
// the drivers to high impedance state.
|
| 82 |
|
|
//
|
| 83 |
|
|
// The tx interface, for writing to the keyboard, does not provide any special
|
| 84 |
|
|
// pre-processing. It simply transmits the 8-bit command value to the
|
| 85 |
|
|
// keyboard.
|
| 86 |
|
|
//
|
| 87 |
|
|
// Pullups MUST BE USED on the ps2_clk and ps2_data lines for this design,
|
| 88 |
|
|
// whether they be internal to an FPGA I/O pad, or externally placed.
|
| 89 |
|
|
// If internal pullups are used, they may be fairly weak, causing bounces
|
| 90 |
|
|
// due to crosstalk, etc. There is a "debounce timer" implemented in order
|
| 91 |
|
|
// to eliminate erroneous state transitions which would occur based on bounce.
|
| 92 |
|
|
//
|
| 93 |
|
|
// Parameters are provided in order to configure and appropriately size the
|
| 94 |
|
|
// counter of a 60 microsecond timer used in the transmitter, depending on
|
| 95 |
|
|
// the clock frequency used. The 60 microsecond period is guaranteed to be
|
| 96 |
|
|
// more than one period of the ps2_clk_s signal.
|
| 97 |
|
|
//
|
| 98 |
|
|
// Also, a smaller 5 microsecond timer has been included for "debounce".
|
| 99 |
|
|
// This is used because, with internal pullups on the ps2_clk and ps2_data
|
| 100 |
|
|
// lines, there is some bouncing around which occurs
|
| 101 |
|
|
//
|
| 102 |
|
|
// A parameter TRAP_SHIFT_KEYS allows the user to eliminate shift keypresses
|
| 103 |
|
|
// from producing scan codes (along with their "undefined" ASCII equivalents)
|
| 104 |
|
|
// at the output of the interface. If TRAP_SHIFT_KEYS is non-zero, the shift
|
| 105 |
|
|
// key status will only be reported by rx_shift_key_on. No ascii or scan
|
| 106 |
|
|
// codes will be reported for the shift keys. This is useful for those who
|
| 107 |
|
|
// wish to use the ASCII data stream, and who don't want to have to "filter
|
| 108 |
|
|
// out" the shift key codes.
|
| 109 |
|
|
//
|
| 110 |
|
|
//-------------------------------------------------------------------------------------
|
| 111 |
|
|
|
| 112 |
|
|
|
| 113 |
|
|
`resetall
|
| 114 |
|
|
`timescale 1ns/100ps
|
| 115 |
|
|
|
| 116 |
|
|
`define TOTAL_BITS 11
|
| 117 |
|
|
`define EXTEND_CODE 16'hE0
|
| 118 |
|
|
`define RELEASE_CODE 16'hF0
|
| 119 |
|
|
`define LEFT_SHIFT 16'h12
|
| 120 |
|
|
`define RIGHT_SHIFT 16'h59
|
| 121 |
|
|
|
| 122 |
|
|
|
| 123 |
|
|
module ps2_keyboard_interface (
|
| 124 |
|
|
clk,
|
| 125 |
|
|
reset,
|
| 126 |
|
|
ps2_clk,
|
| 127 |
|
|
ps2_data,
|
| 128 |
|
|
rx_extended,
|
| 129 |
|
|
rx_released,
|
| 130 |
|
|
rx_shift_key_on,
|
| 131 |
|
|
rx_scan_code,
|
| 132 |
|
|
rx_ascii,
|
| 133 |
|
|
rx_data_ready, // rx_read_o
|
| 134 |
|
|
rx_read, // rx_read_ack_i
|
| 135 |
|
|
tx_data,
|
| 136 |
|
|
tx_write,
|
| 137 |
|
|
tx_write_ack_o,
|
| 138 |
|
|
tx_error_no_keyboard_ack
|
| 139 |
|
|
);
|
| 140 |
|
|
|
| 141 |
|
|
// Parameters
|
| 142 |
|
|
|
| 143 |
|
|
// The timer value can be up to (2^bits) inclusive.
|
| 144 |
|
|
parameter TIMER_60USEC_VALUE_PP = 2950; // Number of sys_clks for 60usec.
|
| 145 |
|
|
parameter TIMER_60USEC_BITS_PP = 12; // Number of bits needed for timer
|
| 146 |
|
|
parameter TIMER_5USEC_VALUE_PP = 186; // Number of sys_clks for debounce
|
| 147 |
|
|
parameter TIMER_5USEC_BITS_PP = 8; // Number of bits needed for timer
|
| 148 |
|
|
parameter TRAP_SHIFT_KEYS_PP = 0; // Default: No shift key trap.
|
| 149 |
|
|
|
| 150 |
|
|
// State encodings, provided as parameters
|
| 151 |
|
|
// for flexibility to the one instantiating the module.
|
| 152 |
|
|
// In general, the default values need not be changed.
|
| 153 |
|
|
|
| 154 |
|
|
// State "m1_rx_clk_l" has been chosen on purpose. Since the input
|
| 155 |
|
|
// synchronizing flip-flops initially contain zero, it takes one clk
|
| 156 |
|
|
// for them to update to reflect the actual (idle = high) status of
|
| 157 |
|
|
// the I/O lines from the keyboard. Therefore, choosing 0 for m1_rx_clk_l
|
| 158 |
|
|
// allows the state machine to transition to m1_rx_clk_h when the true
|
| 159 |
|
|
// values of the input signals become present at the outputs of the
|
| 160 |
|
|
// synchronizing flip-flops. This initial transition is harmless, and it
|
| 161 |
|
|
// eliminates the need for a "reset" pulse before the interface can operate.
|
| 162 |
|
|
|
| 163 |
|
|
parameter m1_rx_clk_h = 1;
|
| 164 |
|
|
parameter m1_rx_clk_l = 0;
|
| 165 |
|
|
parameter m1_rx_falling_edge_marker = 13;
|
| 166 |
|
|
parameter m1_rx_rising_edge_marker = 14;
|
| 167 |
|
|
parameter m1_tx_force_clk_l = 3;
|
| 168 |
|
|
parameter m1_tx_first_wait_clk_h = 10;
|
| 169 |
|
|
parameter m1_tx_first_wait_clk_l = 11;
|
| 170 |
|
|
parameter m1_tx_reset_timer = 12;
|
| 171 |
|
|
parameter m1_tx_wait_clk_h = 2;
|
| 172 |
|
|
parameter m1_tx_clk_h = 4;
|
| 173 |
|
|
parameter m1_tx_clk_l = 5;
|
| 174 |
|
|
parameter m1_tx_wait_keyboard_ack = 6;
|
| 175 |
|
|
parameter m1_tx_done_recovery = 7;
|
| 176 |
|
|
parameter m1_tx_error_no_keyboard_ack = 8;
|
| 177 |
|
|
parameter m1_tx_rising_edge_marker = 9;
|
| 178 |
|
|
parameter m2_rx_data_ready = 1;
|
| 179 |
|
|
parameter m2_rx_data_ready_ack = 0;
|
| 180 |
|
|
|
| 181 |
|
|
|
| 182 |
|
|
// I/O declarations
|
| 183 |
|
|
input clk;
|
| 184 |
|
|
input reset;
|
| 185 |
|
|
inout ps2_clk;
|
| 186 |
|
|
inout ps2_data;
|
| 187 |
|
|
output rx_extended;
|
| 188 |
|
|
output rx_released;
|
| 189 |
|
|
output rx_shift_key_on;
|
| 190 |
|
|
output [7:0] rx_scan_code;
|
| 191 |
|
|
output [7:0] rx_ascii;
|
| 192 |
|
|
output rx_data_ready;
|
| 193 |
|
|
input rx_read;
|
| 194 |
|
|
input [7:0] tx_data;
|
| 195 |
|
|
input tx_write;
|
| 196 |
|
|
output tx_write_ack_o;
|
| 197 |
|
|
output tx_error_no_keyboard_ack;
|
| 198 |
|
|
|
| 199 |
|
|
reg rx_extended;
|
| 200 |
|
|
reg rx_released;
|
| 201 |
|
|
reg [7:0] rx_scan_code;
|
| 202 |
|
|
reg [7:0] rx_ascii;
|
| 203 |
|
|
reg rx_data_ready;
|
| 204 |
|
|
reg tx_error_no_keyboard_ack;
|
| 205 |
|
|
|
| 206 |
|
|
// Internal signal declarations
|
| 207 |
|
|
wire timer_60usec_done;
|
| 208 |
|
|
wire timer_5usec_done;
|
| 209 |
|
|
wire extended;
|
| 210 |
|
|
wire released;
|
| 211 |
|
|
wire shift_key_on;
|
| 212 |
|
|
|
| 213 |
|
|
// NOTE: These two signals used to be one. They
|
| 214 |
|
|
// were split into two signals because of
|
| 215 |
|
|
// shift key trapping. With shift key
|
| 216 |
|
|
// trapping, no event is generated externally,
|
| 217 |
|
|
// but the "hold" data must still be cleared
|
| 218 |
|
|
// anyway regardless, in preparation for the
|
| 219 |
|
|
// next scan codes.
|
| 220 |
|
|
wire rx_output_event; // Used only to clear: hold_released, hold_extended
|
| 221 |
|
|
wire rx_output_strobe; // Used to produce the actual output.
|
| 222 |
|
|
|
| 223 |
|
|
wire tx_parity_bit;
|
| 224 |
|
|
wire rx_shifting_done;
|
| 225 |
|
|
wire tx_shifting_done;
|
| 226 |
|
|
wire [11:0] shift_key_plus_code;
|
| 227 |
|
|
|
| 228 |
|
|
reg [`TOTAL_BITS-1:0] q;
|
| 229 |
|
|
reg [3:0] m1_state;
|
| 230 |
|
|
reg [3:0] m1_next_state;
|
| 231 |
|
|
reg m2_state;
|
| 232 |
|
|
reg m2_next_state;
|
| 233 |
|
|
reg [3:0] bit_count;
|
| 234 |
|
|
reg enable_timer_60usec;
|
| 235 |
|
|
reg enable_timer_5usec;
|
| 236 |
|
|
reg [TIMER_60USEC_BITS_PP-1:0] timer_60usec_count;
|
| 237 |
|
|
reg [TIMER_5USEC_BITS_PP-1:0] timer_5usec_count;
|
| 238 |
|
|
reg [7:0] ascii; // "REG" type only because a case statement is used.
|
| 239 |
|
|
reg left_shift_key;
|
| 240 |
|
|
reg right_shift_key;
|
| 241 |
|
|
reg hold_extended; // Holds prior value, cleared at rx_output_strobe
|
| 242 |
|
|
reg hold_released; // Holds prior value, cleared at rx_output_strobe
|
| 243 |
|
|
reg ps2_clk_s; // Synchronous version of this input
|
| 244 |
|
|
reg ps2_data_s; // Synchronous version of this input
|
| 245 |
|
|
reg ps2_clk_hi_z; // Without keyboard, high Z equals 1 due to pullups.
|
| 246 |
|
|
reg ps2_data_hi_z; // Without keyboard, high Z equals 1 due to pullups.
|
| 247 |
|
|
|
| 248 |
|
|
//--------------------------------------------------------------------------
|
| 249 |
|
|
// Module code
|
| 250 |
|
|
|
| 251 |
|
|
assign ps2_clk = ps2_clk_hi_z?1'bZ:1'b0;
|
| 252 |
|
|
assign ps2_data = ps2_data_hi_z?1'bZ:1'b0;
|
| 253 |
|
|
|
| 254 |
|
|
// Input "synchronizing" logic -- synchronizes the inputs to the state
|
| 255 |
|
|
// machine clock, thus avoiding errors related to
|
| 256 |
|
|
// spurious state machine transitions.
|
| 257 |
|
|
always @(posedge clk)
|
| 258 |
|
|
begin
|
| 259 |
|
|
ps2_clk_s <= ps2_clk;
|
| 260 |
|
|
ps2_data_s <= ps2_data;
|
| 261 |
|
|
end
|
| 262 |
|
|
|
| 263 |
|
|
// State register
|
| 264 |
|
|
always @(posedge clk)
|
| 265 |
|
|
begin : m1_state_register
|
| 266 |
|
|
if (reset) m1_state <= m1_rx_clk_h;
|
| 267 |
|
|
else m1_state <= m1_next_state;
|
| 268 |
|
|
end
|
| 269 |
|
|
|
| 270 |
|
|
// State transition logic
|
| 271 |
|
|
always @(m1_state
|
| 272 |
|
|
or q
|
| 273 |
|
|
or tx_shifting_done
|
| 274 |
|
|
or tx_write
|
| 275 |
|
|
or ps2_clk_s
|
| 276 |
|
|
or ps2_data_s
|
| 277 |
|
|
or timer_60usec_done
|
| 278 |
|
|
or timer_5usec_done
|
| 279 |
|
|
)
|
| 280 |
|
|
begin : m1_state_logic
|
| 281 |
|
|
|
| 282 |
|
|
// Output signals default to this value, unless changed in a state condition.
|
| 283 |
|
|
ps2_clk_hi_z <= 1;
|
| 284 |
|
|
ps2_data_hi_z <= 1;
|
| 285 |
|
|
tx_error_no_keyboard_ack <= 0;
|
| 286 |
|
|
enable_timer_60usec <= 0;
|
| 287 |
|
|
enable_timer_5usec <= 0;
|
| 288 |
|
|
|
| 289 |
|
|
case (m1_state)
|
| 290 |
|
|
|
| 291 |
|
|
m1_rx_clk_h :
|
| 292 |
|
|
begin
|
| 293 |
|
|
enable_timer_60usec <= 1;
|
| 294 |
|
|
if (tx_write) m1_next_state <= m1_tx_reset_timer;
|
| 295 |
|
|
else if (~ps2_clk_s) m1_next_state <= m1_rx_falling_edge_marker;
|
| 296 |
|
|
else m1_next_state <= m1_rx_clk_h;
|
| 297 |
|
|
end
|
| 298 |
|
|
|
| 299 |
|
|
m1_rx_falling_edge_marker :
|
| 300 |
|
|
begin
|
| 301 |
|
|
enable_timer_60usec <= 0;
|
| 302 |
|
|
m1_next_state <= m1_rx_clk_l;
|
| 303 |
|
|
end
|
| 304 |
|
|
|
| 305 |
|
|
m1_rx_rising_edge_marker :
|
| 306 |
|
|
begin
|
| 307 |
|
|
enable_timer_60usec <= 0;
|
| 308 |
|
|
m1_next_state <= m1_rx_clk_h;
|
| 309 |
|
|
end
|
| 310 |
|
|
|
| 311 |
|
|
|
| 312 |
|
|
m1_rx_clk_l :
|
| 313 |
|
|
begin
|
| 314 |
|
|
enable_timer_60usec <= 1;
|
| 315 |
|
|
if (tx_write) m1_next_state <= m1_tx_reset_timer;
|
| 316 |
|
|
else if (ps2_clk_s) m1_next_state <= m1_rx_rising_edge_marker;
|
| 317 |
|
|
else m1_next_state <= m1_rx_clk_l;
|
| 318 |
|
|
end
|
| 319 |
|
|
|
| 320 |
|
|
m1_tx_reset_timer:
|
| 321 |
|
|
begin
|
| 322 |
|
|
enable_timer_60usec <= 0;
|
| 323 |
|
|
m1_next_state <= m1_tx_force_clk_l;
|
| 324 |
|
|
end
|
| 325 |
|
|
|
| 326 |
|
|
m1_tx_force_clk_l :
|
| 327 |
|
|
begin
|
| 328 |
|
|
enable_timer_60usec <= 1;
|
| 329 |
|
|
ps2_clk_hi_z <= 0; // Force the ps2_clk line low.
|
| 330 |
|
|
if (timer_60usec_done) m1_next_state <= m1_tx_first_wait_clk_h;
|
| 331 |
|
|
else m1_next_state <= m1_tx_force_clk_l;
|
| 332 |
|
|
end
|
| 333 |
|
|
|
| 334 |
|
|
m1_tx_first_wait_clk_h :
|
| 335 |
|
|
begin
|
| 336 |
|
|
enable_timer_5usec <= 1;
|
| 337 |
|
|
ps2_data_hi_z <= 0; // Start bit.
|
| 338 |
|
|
if (~ps2_clk_s && timer_5usec_done)
|
| 339 |
|
|
m1_next_state <= m1_tx_clk_l;
|
| 340 |
|
|
else
|
| 341 |
|
|
m1_next_state <= m1_tx_first_wait_clk_h;
|
| 342 |
|
|
end
|
| 343 |
|
|
|
| 344 |
|
|
// This state must be included because the device might possibly
|
| 345 |
|
|
// delay for up to 10 milliseconds before beginning its clock pulses.
|
| 346 |
|
|
// During that waiting time, we cannot drive the data (q[0]) because it
|
| 347 |
|
|
// is possibly 1, which would cause the keyboard to abort its receive
|
| 348 |
|
|
// and the expected clocks would then never be generated.
|
| 349 |
|
|
m1_tx_first_wait_clk_l :
|
| 350 |
|
|
begin
|
| 351 |
|
|
ps2_data_hi_z <= 0;
|
| 352 |
|
|
if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
|
| 353 |
|
|
else m1_next_state <= m1_tx_first_wait_clk_l;
|
| 354 |
|
|
end
|
| 355 |
|
|
|
| 356 |
|
|
m1_tx_wait_clk_h :
|
| 357 |
|
|
begin
|
| 358 |
|
|
enable_timer_5usec <= 1;
|
| 359 |
|
|
ps2_data_hi_z <= q[0];
|
| 360 |
|
|
if (ps2_clk_s && timer_5usec_done)
|
| 361 |
|
|
m1_next_state <= m1_tx_rising_edge_marker;
|
| 362 |
|
|
else
|
| 363 |
|
|
m1_next_state <= m1_tx_wait_clk_h;
|
| 364 |
|
|
end
|
| 365 |
|
|
|
| 366 |
|
|
m1_tx_rising_edge_marker :
|
| 367 |
|
|
begin
|
| 368 |
|
|
ps2_data_hi_z <= q[0];
|
| 369 |
|
|
m1_next_state <= m1_tx_clk_h;
|
| 370 |
|
|
end
|
| 371 |
|
|
|
| 372 |
|
|
m1_tx_clk_h :
|
| 373 |
|
|
begin
|
| 374 |
|
|
ps2_data_hi_z <= q[0];
|
| 375 |
|
|
if (tx_shifting_done) m1_next_state <= m1_tx_wait_keyboard_ack;
|
| 376 |
|
|
else if (~ps2_clk_s) m1_next_state <= m1_tx_clk_l;
|
| 377 |
|
|
else m1_next_state <= m1_tx_clk_h;
|
| 378 |
|
|
end
|
| 379 |
|
|
|
| 380 |
|
|
m1_tx_clk_l :
|
| 381 |
|
|
begin
|
| 382 |
|
|
ps2_data_hi_z <= q[0];
|
| 383 |
|
|
if (ps2_clk_s) m1_next_state <= m1_tx_wait_clk_h;
|
| 384 |
|
|
else m1_next_state <= m1_tx_clk_l;
|
| 385 |
|
|
end
|
| 386 |
|
|
|
| 387 |
|
|
m1_tx_wait_keyboard_ack :
|
| 388 |
|
|
begin
|
| 389 |
|
|
if (~ps2_clk_s && ps2_data_s)
|
| 390 |
|
|
m1_next_state <= m1_tx_error_no_keyboard_ack;
|
| 391 |
|
|
else if (~ps2_clk_s && ~ps2_data_s)
|
| 392 |
|
|
m1_next_state <= m1_tx_done_recovery;
|
| 393 |
|
|
else m1_next_state <= m1_tx_wait_keyboard_ack;
|
| 394 |
|
|
end
|
| 395 |
|
|
|
| 396 |
|
|
m1_tx_done_recovery :
|
| 397 |
|
|
begin
|
| 398 |
|
|
if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
|
| 399 |
|
|
else m1_next_state <= m1_tx_done_recovery;
|
| 400 |
|
|
end
|
| 401 |
|
|
|
| 402 |
|
|
m1_tx_error_no_keyboard_ack :
|
| 403 |
|
|
begin
|
| 404 |
|
|
tx_error_no_keyboard_ack <= 1;
|
| 405 |
|
|
if (ps2_clk_s && ps2_data_s) m1_next_state <= m1_rx_clk_h;
|
| 406 |
|
|
else m1_next_state <= m1_tx_error_no_keyboard_ack;
|
| 407 |
|
|
end
|
| 408 |
|
|
|
| 409 |
|
|
default : m1_next_state <= m1_rx_clk_h;
|
| 410 |
|
|
endcase
|
| 411 |
|
|
end
|
| 412 |
|
|
|
| 413 |
|
|
// State register
|
| 414 |
|
|
always @(posedge clk)
|
| 415 |
|
|
begin : m2_state_register
|
| 416 |
|
|
if (reset) m2_state <= m2_rx_data_ready_ack;
|
| 417 |
|
|
else m2_state <= m2_next_state;
|
| 418 |
|
|
end
|
| 419 |
|
|
|
| 420 |
|
|
// State transition logic
|
| 421 |
|
|
always @(m2_state or rx_output_strobe or rx_read)
|
| 422 |
|
|
begin : m2_state_logic
|
| 423 |
|
|
case (m2_state)
|
| 424 |
|
|
m2_rx_data_ready_ack:
|
| 425 |
|
|
begin
|
| 426 |
|
|
rx_data_ready <= 1'b0;
|
| 427 |
|
|
if (rx_output_strobe) m2_next_state <= m2_rx_data_ready;
|
| 428 |
|
|
else m2_next_state <= m2_rx_data_ready_ack;
|
| 429 |
|
|
end
|
| 430 |
|
|
m2_rx_data_ready:
|
| 431 |
|
|
begin
|
| 432 |
|
|
rx_data_ready <= 1'b1;
|
| 433 |
|
|
if (rx_read) m2_next_state <= m2_rx_data_ready_ack;
|
| 434 |
|
|
else m2_next_state <= m2_rx_data_ready;
|
| 435 |
|
|
end
|
| 436 |
|
|
default : m2_next_state <= m2_rx_data_ready_ack;
|
| 437 |
|
|
endcase
|
| 438 |
|
|
end
|
| 439 |
|
|
|
| 440 |
|
|
// This is the bit counter
|
| 441 |
|
|
always @(posedge clk)
|
| 442 |
|
|
begin
|
| 443 |
|
|
if ( reset
|
| 444 |
|
|
|| rx_shifting_done
|
| 445 |
|
|
|| (m1_state == m1_tx_wait_keyboard_ack) // After tx is done.
|
| 446 |
|
|
) bit_count <= 0; // normal reset
|
| 447 |
|
|
else if (timer_60usec_done
|
| 448 |
|
|
&& (m1_state == m1_rx_clk_h)
|
| 449 |
|
|
&& (ps2_clk_s)
|
| 450 |
|
|
) bit_count <= 0; // rx watchdog timer reset
|
| 451 |
|
|
else if ( (m1_state == m1_rx_falling_edge_marker) // increment for rx
|
| 452 |
|
|
||(m1_state == m1_tx_rising_edge_marker) // increment for tx
|
| 453 |
|
|
)
|
| 454 |
|
|
bit_count <= bit_count + 1;
|
| 455 |
|
|
end
|
| 456 |
|
|
// This signal is high for one clock at the end of the timer count.
|
| 457 |
|
|
assign rx_shifting_done = (bit_count == `TOTAL_BITS);
|
| 458 |
|
|
assign tx_shifting_done = (bit_count == `TOTAL_BITS-1);
|
| 459 |
|
|
|
| 460 |
|
|
// This is the signal which enables loading of the shift register.
|
| 461 |
|
|
// It also indicates "ack" to the device writing to the transmitter.
|
| 462 |
|
|
assign tx_write_ack_o = ( (tx_write && (m1_state == m1_rx_clk_h))
|
| 463 |
|
|
||(tx_write && (m1_state == m1_rx_clk_l))
|
| 464 |
|
|
);
|
| 465 |
|
|
|
| 466 |
|
|
// This is the ODD parity bit for the transmitted word.
|
| 467 |
|
|
assign tx_parity_bit = ~^tx_data;
|
| 468 |
|
|
|
| 469 |
|
|
// This is the shift register
|
| 470 |
|
|
always @(posedge clk)
|
| 471 |
|
|
begin
|
| 472 |
|
|
if (reset) q <= 0;
|
| 473 |
|
|
else if (tx_write_ack_o) q <= {1'b1,tx_parity_bit,tx_data,1'b0};
|
| 474 |
|
|
else if ( (m1_state == m1_rx_falling_edge_marker)
|
| 475 |
|
|
||(m1_state == m1_tx_rising_edge_marker) )
|
| 476 |
|
|
q <= {ps2_data_s,q[`TOTAL_BITS-1:1]};
|
| 477 |
|
|
end
|
| 478 |
|
|
|
| 479 |
|
|
// This is the 60usec timer counter
|
| 480 |
|
|
always @(posedge clk)
|
| 481 |
|
|
begin
|
| 482 |
|
|
if (~enable_timer_60usec) timer_60usec_count <= 0;
|
| 483 |
|
|
else if (~timer_60usec_done) timer_60usec_count <= timer_60usec_count + 1;
|
| 484 |
|
|
end
|
| 485 |
|
|
assign timer_60usec_done = (timer_60usec_count == (TIMER_60USEC_VALUE_PP - 1));
|
| 486 |
|
|
|
| 487 |
|
|
// This is the 5usec timer counter
|
| 488 |
|
|
always @(posedge clk)
|
| 489 |
|
|
begin
|
| 490 |
|
|
if (~enable_timer_5usec) timer_5usec_count <= 0;
|
| 491 |
|
|
else if (~timer_5usec_done) timer_5usec_count <= timer_5usec_count + 1;
|
| 492 |
|
|
end
|
| 493 |
|
|
assign timer_5usec_done = (timer_5usec_count == TIMER_5USEC_VALUE_PP - 1);
|
| 494 |
|
|
|
| 495 |
|
|
|
| 496 |
|
|
// Create the signals which indicate special scan codes received.
|
| 497 |
|
|
// These are the "unlatched versions."
|
| 498 |
|
|
assign extended = (q[8:1] == `EXTEND_CODE) && rx_shifting_done;
|
| 499 |
|
|
assign released = (q[8:1] == `RELEASE_CODE) && rx_shifting_done;
|
| 500 |
|
|
|
| 501 |
|
|
// Store the special scan code status bits
|
| 502 |
|
|
// Not the final output, but an intermediate storage place,
|
| 503 |
|
|
// until the entire set of output data can be assembled.
|
| 504 |
|
|
always @(posedge clk)
|
| 505 |
|
|
begin
|
| 506 |
|
|
if (reset || rx_output_event)
|
| 507 |
|
|
begin
|
| 508 |
|
|
hold_extended <= 0;
|
| 509 |
|
|
hold_released <= 0;
|
| 510 |
|
|
end
|
| 511 |
|
|
else
|
| 512 |
|
|
begin
|
| 513 |
|
|
if (rx_shifting_done && extended) hold_extended <= 1;
|
| 514 |
|
|
if (rx_shifting_done && released) hold_released <= 1;
|
| 515 |
|
|
end
|
| 516 |
|
|
end
|
| 517 |
|
|
|
| 518 |
|
|
|
| 519 |
|
|
// These bits contain the status of the two shift keys
|
| 520 |
|
|
always @(posedge clk)
|
| 521 |
|
|
begin
|
| 522 |
|
|
if (reset) left_shift_key <= 0;
|
| 523 |
|
|
else if ((q[8:1] == `LEFT_SHIFT) && rx_shifting_done && ~hold_released)
|
| 524 |
|
|
left_shift_key <= 1;
|
| 525 |
|
|
else if ((q[8:1] == `LEFT_SHIFT) && rx_shifting_done && hold_released)
|
| 526 |
|
|
left_shift_key <= 0;
|
| 527 |
|
|
end
|
| 528 |
|
|
|
| 529 |
|
|
always @(posedge clk)
|
| 530 |
|
|
begin
|
| 531 |
|
|
if (reset) right_shift_key <= 0;
|
| 532 |
|
|
else if ((q[8:1] == `RIGHT_SHIFT) && rx_shifting_done && ~hold_released)
|
| 533 |
|
|
right_shift_key <= 1;
|
| 534 |
|
|
else if ((q[8:1] == `RIGHT_SHIFT) && rx_shifting_done && hold_released)
|
| 535 |
|
|
right_shift_key <= 0;
|
| 536 |
|
|
end
|
| 537 |
|
|
|
| 538 |
|
|
assign rx_shift_key_on = left_shift_key || right_shift_key;
|
| 539 |
|
|
|
| 540 |
|
|
// Output the special scan code flags, the scan code and the ascii
|
| 541 |
|
|
always @(posedge clk)
|
| 542 |
|
|
begin
|
| 543 |
|
|
if (reset)
|
| 544 |
|
|
begin
|
| 545 |
|
|
rx_extended <= 0;
|
| 546 |
|
|
rx_released <= 0;
|
| 547 |
|
|
rx_scan_code <= 0;
|
| 548 |
|
|
rx_ascii <= 0;
|
| 549 |
|
|
end
|
| 550 |
|
|
else if (rx_output_strobe)
|
| 551 |
|
|
begin
|
| 552 |
|
|
rx_extended <= hold_extended;
|
| 553 |
|
|
rx_released <= hold_released;
|
| 554 |
|
|
rx_scan_code <= q[8:1];
|
| 555 |
|
|
rx_ascii <= ascii;
|
| 556 |
|
|
end
|
| 557 |
|
|
end
|
| 558 |
|
|
|
| 559 |
|
|
// Store the final rx output data only when all extend and release codes
|
| 560 |
|
|
// are received and the next (actual key) scan code is also ready.
|
| 561 |
|
|
// (the presence of rx_extended or rx_released refers to the
|
| 562 |
|
|
// the current latest scan code received, not the previously latched flags.)
|
| 563 |
|
|
assign rx_output_event = (rx_shifting_done
|
| 564 |
|
|
&& ~extended
|
| 565 |
|
|
&& ~released
|
| 566 |
|
|
);
|
| 567 |
|
|
|
| 568 |
|
|
assign rx_output_strobe = (rx_shifting_done
|
| 569 |
|
|
&& ~extended
|
| 570 |
|
|
&& ~released
|
| 571 |
|
|
&& ( (TRAP_SHIFT_KEYS_PP == 0)
|
| 572 |
|
|
|| ( (q[8:1] != `RIGHT_SHIFT)
|
| 573 |
|
|
&&(q[8:1] != `LEFT_SHIFT)
|
| 574 |
|
|
)
|
| 575 |
|
|
)
|
| 576 |
|
|
);
|
| 577 |
|
|
|
| 578 |
|
|
// This part translates the scan code into an ASCII value...
|
| 579 |
|
|
// Only the ASCII codes which I considered important have been included.
|
| 580 |
|
|
// if you want more, just add the appropriate case statement lines...
|
| 581 |
|
|
// (You will need to know the keyboard scan codes you wish to assign.)
|
| 582 |
|
|
// The entries are listed in ascending order of ASCII value.
|
| 583 |
|
|
assign shift_key_plus_code = {3'b0,rx_shift_key_on,q[8:1]};
|
| 584 |
|
|
always @(shift_key_plus_code)
|
| 585 |
|
|
begin
|
| 586 |
|
|
casez (shift_key_plus_code)
|
| 587 |
|
|
12'h?66 : ascii <= 8'h08; // Backspace ("backspace" key)
|
| 588 |
|
|
12'h?0d : ascii <= 8'h09; // Horizontal Tab
|
| 589 |
|
|
12'h?5a : ascii <= 8'h0d; // Carriage return ("enter" key)
|
| 590 |
|
|
12'h?76 : ascii <= 8'h1b; // Escape ("esc" key)
|
| 591 |
|
|
12'h?29 : ascii <= 8'h20; // Space
|
| 592 |
|
|
12'h116 : ascii <= 8'h21; // !
|
| 593 |
|
|
12'h152 : ascii <= 8'h22; // "
|
| 594 |
|
|
12'h126 : ascii <= 8'h23; // #
|
| 595 |
|
|
12'h125 : ascii <= 8'h24; // $
|
| 596 |
|
|
12'h12e : ascii <= 8'h25; // %
|
| 597 |
|
|
12'h13d : ascii <= 8'h26; // &
|
| 598 |
|
|
12'h052 : ascii <= 8'h27; // '
|
| 599 |
|
|
12'h146 : ascii <= 8'h28; // (
|
| 600 |
|
|
12'h145 : ascii <= 8'h29; // )
|
| 601 |
|
|
12'h13e : ascii <= 8'h2a; // *
|
| 602 |
|
|
12'h155 : ascii <= 8'h2b; // +
|
| 603 |
|
|
12'h041 : ascii <= 8'h2c; // ,
|
| 604 |
|
|
12'h04e : ascii <= 8'h2d; // -
|
| 605 |
|
|
12'h049 : ascii <= 8'h2e; // .
|
| 606 |
|
|
12'h04a : ascii <= 8'h2f; // /
|
| 607 |
|
|
12'h045 : ascii <= 8'h30; // 0
|
| 608 |
|
|
12'h016 : ascii <= 8'h31; // 1
|
| 609 |
|
|
12'h01e : ascii <= 8'h32; // 2
|
| 610 |
|
|
12'h026 : ascii <= 8'h33; // 3
|
| 611 |
|
|
12'h025 : ascii <= 8'h34; // 4
|
| 612 |
|
|
12'h02e : ascii <= 8'h35; // 5
|
| 613 |
|
|
12'h036 : ascii <= 8'h36; // 6
|
| 614 |
|
|
12'h03d : ascii <= 8'h37; // 7
|
| 615 |
|
|
12'h03e : ascii <= 8'h38; // 8
|
| 616 |
|
|
12'h046 : ascii <= 8'h39; // 9
|
| 617 |
|
|
12'h14c : ascii <= 8'h3a; // :
|
| 618 |
|
|
12'h04c : ascii <= 8'h3b; // ;
|
| 619 |
|
|
12'h141 : ascii <= 8'h3c; // <
|
| 620 |
|
|
12'h055 : ascii <= 8'h3d; // =
|
| 621 |
|
|
12'h149 : ascii <= 8'h3e; // >
|
| 622 |
|
|
12'h14a : ascii <= 8'h3f; // ?
|
| 623 |
|
|
12'h11e : ascii <= 8'h40; // @
|
| 624 |
|
|
12'h11c : ascii <= 8'h41; // A
|
| 625 |
|
|
12'h132 : ascii <= 8'h42; // B
|
| 626 |
|
|
12'h121 : ascii <= 8'h43; // C
|
| 627 |
|
|
12'h123 : ascii <= 8'h44; // D
|
| 628 |
|
|
12'h124 : ascii <= 8'h45; // E
|
| 629 |
|
|
12'h12b : ascii <= 8'h46; // F
|
| 630 |
|
|
12'h134 : ascii <= 8'h47; // G
|
| 631 |
|
|
12'h133 : ascii <= 8'h48; // H
|
| 632 |
|
|
12'h143 : ascii <= 8'h49; // I
|
| 633 |
|
|
12'h13b : ascii <= 8'h4a; // J
|
| 634 |
|
|
12'h142 : ascii <= 8'h4b; // K
|
| 635 |
|
|
12'h14b : ascii <= 8'h4c; // L
|
| 636 |
|
|
12'h13a : ascii <= 8'h4d; // M
|
| 637 |
|
|
12'h131 : ascii <= 8'h4e; // N
|
| 638 |
|
|
12'h144 : ascii <= 8'h4f; // O
|
| 639 |
|
|
12'h14d : ascii <= 8'h50; // P
|
| 640 |
|
|
12'h115 : ascii <= 8'h51; // Q
|
| 641 |
|
|
12'h12d : ascii <= 8'h52; // R
|
| 642 |
|
|
12'h11b : ascii <= 8'h53; // S
|
| 643 |
|
|
12'h12c : ascii <= 8'h54; // T
|
| 644 |
|
|
12'h13c : ascii <= 8'h55; // U
|
| 645 |
|
|
12'h12a : ascii <= 8'h56; // V
|
| 646 |
|
|
12'h11d : ascii <= 8'h57; // W
|
| 647 |
|
|
12'h122 : ascii <= 8'h58; // X
|
| 648 |
|
|
12'h135 : ascii <= 8'h59; // Y
|
| 649 |
|
|
12'h11a : ascii <= 8'h5a; // Z
|
| 650 |
|
|
12'h054 : ascii <= 8'h5b; // [
|
| 651 |
|
|
12'h05d : ascii <= 8'h5c; // \
|
| 652 |
|
|
12'h05b : ascii <= 8'h5d; // ]
|
| 653 |
|
|
12'h136 : ascii <= 8'h5e; // ^
|
| 654 |
|
|
12'h14e : ascii <= 8'h5f; // _
|
| 655 |
|
|
12'h00e : ascii <= 8'h60; // `
|
| 656 |
|
|
12'h01c : ascii <= 8'h61; // a
|
| 657 |
|
|
12'h032 : ascii <= 8'h62; // b
|
| 658 |
|
|
12'h021 : ascii <= 8'h63; // c
|
| 659 |
|
|
12'h023 : ascii <= 8'h64; // d
|
| 660 |
|
|
12'h024 : ascii <= 8'h65; // e
|
| 661 |
|
|
12'h02b : ascii <= 8'h66; // f
|
| 662 |
|
|
12'h034 : ascii <= 8'h67; // g
|
| 663 |
|
|
12'h033 : ascii <= 8'h68; // h
|
| 664 |
|
|
12'h043 : ascii <= 8'h69; // i
|
| 665 |
|
|
12'h03b : ascii <= 8'h6a; // j
|
| 666 |
|
|
12'h042 : ascii <= 8'h6b; // k
|
| 667 |
|
|
12'h04b : ascii <= 8'h6c; // l
|
| 668 |
|
|
12'h03a : ascii <= 8'h6d; // m
|
| 669 |
|
|
12'h031 : ascii <= 8'h6e; // n
|
| 670 |
|
|
12'h044 : ascii <= 8'h6f; // o
|
| 671 |
|
|
12'h04d : ascii <= 8'h70; // p
|
| 672 |
|
|
12'h015 : ascii <= 8'h71; // q
|
| 673 |
|
|
12'h02d : ascii <= 8'h72; // r
|
| 674 |
|
|
12'h01b : ascii <= 8'h73; // s
|
| 675 |
|
|
12'h02c : ascii <= 8'h74; // t
|
| 676 |
|
|
12'h03c : ascii <= 8'h75; // u
|
| 677 |
|
|
12'h02a : ascii <= 8'h76; // v
|
| 678 |
|
|
12'h01d : ascii <= 8'h77; // w
|
| 679 |
|
|
12'h022 : ascii <= 8'h78; // x
|
| 680 |
|
|
12'h035 : ascii <= 8'h79; // y
|
| 681 |
|
|
12'h01a : ascii <= 8'h7a; // z
|
| 682 |
|
|
12'h154 : ascii <= 8'h7b; // {
|
| 683 |
|
|
12'h15d : ascii <= 8'h7c; // |
|
| 684 |
|
|
12'h15b : ascii <= 8'h7d; // }
|
| 685 |
|
|
12'h10e : ascii <= 8'h7e; // ~
|
| 686 |
|
|
12'h?71 : ascii <= 8'h7f; // (Delete OR DEL on numeric keypad)
|
| 687 |
|
|
default : ascii <= 8'h2e; // '.' used for unlisted characters.
|
| 688 |
|
|
endcase
|
| 689 |
|
|
end
|
| 690 |
|
|
|
| 691 |
|
|
|
| 692 |
|
|
endmodule
|
| 693 |
|
|
|
| 694 |
|
|
//`undefine TOTAL_BITS
|
| 695 |
|
|
//`undefine EXTEND_CODE
|
| 696 |
|
|
//`undefine RELEASE_CODE
|
| 697 |
|
|
//`undefine LEFT_SHIFT
|
| 698 |
|
|
//`undefine RIGHT_SHIFT
|
| 699 |
|
|
|