OpenCores
URL https://opencores.org/ocsvn/zx_ula/zx_ula/trunk

Subversion Repositories zx_ula

[/] [zx_ula/] [branches/] [xilinx/] [spectrum_48k_spartan3a_for_gameduino_mod_vga_timex_hicolor_ulaplus/] [teclado_ps2.v] - Blame information for rev 29

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 29 mcleod_ide
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company:        Dept. Architecture and Computing Technology. University of Seville
4
// Engineer:       Miguel Angel Rodriguez Jodar. rodriguj@atc.us.es
5
// 
6
// Create Date:    19:13:39 4-Apr-2012 
7
// Design Name:    ZX Spectrum
8
// Module Name:    PS2 keyboard interface for ZX Spectrum.
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 1.00 - File Created
18
// Additional Comments: GPL License policies apply to the contents of this file.
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module ps2kbd (
22
    input clk,
23
         input reset,
24
         input clkps2,
25
         input dataps2,
26
         output ledextended,
27
         output ledreleased,
28
         output ledmayus,
29
         output [7:0] scancode,
30
         input [7:0] semifila,
31
         output [4:0] columna
32
    );
33
 
34
        reg rload = 0;
35
        reg [1:0] estado = 0;
36
 
37
        wire [7:0] tecla;
38
        wire recibida;
39
 
40
        reg [7:0] rscancode=8'h00;
41
        assign scancode = rscancode;
42
 
43
        reg extended = 0;
44
        reg released = 0;
45
        reg mayus = 0;
46
        assign ledextended = extended;
47
        assign ledreleased = released;
48
        assign ledmayus = mayus;
49
 
50
        ps2_keyboard ps2_get_scancode (
51
                .clk(clk),
52
                .reset(reset),
53
                .ps2_clk(clkps2),
54
                .ps2_data(dataps2),
55
                .interrupt(recibida),
56
                .rx_scan_code(tecla)
57
        );
58
 
59
        matrix scancode_to_zx_keyboard (
60
                .clk(clk),
61
                .keyreceived(rload),
62
                .scancode(tecla),
63
                .mayus(mayus),
64
                .extended(extended),
65
                .released(released),
66
                .semifila(semifila),
67
                .columna(columna)
68
        );
69
 
70
        always @(posedge clk)
71
        begin
72
                case (estado)
73
                        0:       if (recibida) // if a new key has just been received at the PS2 port...
74
                                        begin
75
                                                if (tecla==8'h12 || tecla==8'h59) begin  // if it's one of the two shift keys,
76
                                                        mayus <= 1;                           // signal it.
77
                                                end
78
                                                else if (tecla==8'hE0) begin // if this is the beginning of an extended key
79
                                                        extended <= 1;            // signal it...
80
                                                        released <= 0;                 // (this is not a released key)
81
                                                        estado <= 1;              // ...and wait for the next code
82
                                                end
83
                                                else if (tecla==8'hF0) begin // if this is the beginning of a released key
84
                                                        extended <= 0;            // then it's not an extended key
85
                                                        released <= 1;            // signal it...
86
                                                        estado <= 2;              // ... and wait for the next code
87
                                                end
88
                                                else if (tecla!=8'hE0 && tecla!=8'hF0 && tecla!=8'h12 && tecla!=8'h59) begin  // if this is a "normal" key
89
                                                        rscancode <= tecla;                                                      // store its scancode
90
                                                        extended <= 0;                         //
91
                                                        released <= 0;                         // signal it as "normal": not released, not extended
92
                                                        rload <= 1; //no estaba                // a new key has arrived. Signal that to the matrix module
93
                                                        estado <= 0; //3;                      // and go for another key (the next clock will have "recibida" = 0, so
94
                                                end                                       // it will go to the "else" part of this "if" statement, to deassert "rload")
95
                                        end
96
                                else
97
                                        rload <= 0;  // if a new key has not been received, just turn off the "new key arrived" signal
98
 
99
                        1:      if (recibida) // if a new extended key has just been received at the PS2 port...
100
                                        begin
101
                                                if (tecla==8'hF0) begin  // if this is the beginning of a released extended key...
102
                                                        released <= 1;        // ... signal it, and
103
                                                        estado <= 2;          // ... wait for the actual scancode of the released key
104
                                                end
105
                                                else if (tecla!=8'hE0 && tecla!=8'hF0 && tecla!=8'h12 && tecla!=8'h59) begin  // if this is the actual scancode of a new pressed extended key...
106
                                                        rscancode <= tecla;                                                           // store it...
107
                                                        rload <= 1; //no estaba                                                    // assert "rload"...
108
                                                        estado <= 0; //3;                                                          // and go for another key... 
109
                                                end
110
                                        end
111
                                else
112
                                        rload <= 0;
113
 
114
                        2:      if (recibida) // if a new released key has just been received at the PS2 port...
115
                                        begin
116
                                                if (tecla==8'h12 || tecla==8'h59) begin  // if it is one of the shift keys
117
                                                        mayus <= 0;                           // deassert "mayus" to signal that shift is no longer pressed
118
                                                        estado <= 0;
119
                                                end
120
                                                else if (tecla!=8'hE0 && tecla!=8'hF0 && tecla!=8'h12 && tecla!=8'h59) begin  // if this is any other key...
121
                                                        rscancode <= tecla;                                                           // store it...
122
                                                        rload <= 1;                                                                // assert "rload"
123
                                                        estado <= 0;                                                               // and go for another key
124
                                                end
125
                                        end
126
                                else
127
                                        rload <= 0;
128
                endcase
129
        end
130
endmodule
131
 
132
module matrix (
133
   input clk,
134
        input keyreceived,
135
        input [7:0] scancode,
136
        input mayus,
137
        input extended,
138
        input released,
139
        input [7:0] semifila,
140
        output [4:0] columna
141
        );
142
 
143
        reg [4:0] thematrix[0:7]; // memory representing 40 keys
144
        initial begin                                   // initially, all keys are released (0=pressed)
145
                thematrix[0] = 5'b11111;
146
                thematrix[1] = 5'b11111;
147
                thematrix[2] = 5'b11111;
148
                thematrix[3] = 5'b11111;
149
                thematrix[4] = 5'b11111;
150
                thematrix[5] = 5'b11111;
151
                thematrix[6] = 5'b11111;
152
                thematrix[7] = 5'b11111;
153
        end
154
 
155
        // output bits to ULA are composed with data from the eight half-rows, depending upon which
156
        // bits were resetted at CPU address bus bits 8-15 (row selection)
157
        assign columna = ((!semifila[0])? thematrix[0] : 5'b11111) &
158
                                                  ((!semifila[1])? thematrix[1] : 5'b11111) &
159
                                                  ((!semifila[2])? thematrix[2] : 5'b11111) &
160
                                                  ((!semifila[3])? thematrix[3] : 5'b11111) &
161
                                                  ((!semifila[4])? thematrix[4] : 5'b11111) &
162
                                                  ((!semifila[5])? thematrix[5] : 5'b11111) &
163
                                                  ((!semifila[6])? thematrix[6] : 5'b11111) &
164
                                                  ((!semifila[7])? thematrix[7] : 5'b11111);
165
 
166
        // ROM's containing the ZX Spectrum equivalent key(s) for each PS2 key. For Spectrum shifted keys
167
   //   (such as = * + ! CURSOR-LEFT, DELETE, EDIT, etc), a memory location contains two values: one of
168
        // them is either CAPS SHIFT or SYMBOL SHIFT. The other one is the key that would have to be pressed along
169
        // with the shift key on the ZX Spectrum keyboard.
170
        // These ROM's are filled in the file "mapa_es.inc". This file contains the mapping for a spanish keyboard.
171
        // There is a ROM for normal PS2 keys, another one for PS2 shifted keys, another one for extended, non shifted
172
        // keys, and the last one is for extended shifted keys.
173
        // Cada posicion tiene: CODSEMIFILA1(3) , VALORSEMIFILA1(5) , CODSEMIFILA1(3) , VALORSEMIFILA1(5)
174
        // These ROM's will be infered as BLOCK RAM because the address will be registered
175
        reg [15:0] mapa_noshift_noext[0:131];
176
        reg [15:0] mapa_shift_noext[0:131];
177
        reg [15:0] mapa_noshift_ext[0:131];
178
        reg [15:0] mapa_shift_ext[0:131];
179
 
180
`include "mapa_es.inc"
181
 
182
        reg [2:0] row_key1_nosh_noex;
183
        reg [2:0] row_key2_nosh_noex;
184
        reg [2:0] row_key1_sh_noex;
185
        reg [2:0] row_key2_sh_noex;
186
        reg [2:0] row_key1_nosh_ex;
187
        reg [2:0] row_key2_nosh_ex;
188
        reg [2:0] row_key1_sh_ex;
189
        reg [2:0] row_key2_sh_ex;
190
 
191
        reg [4:0] col_key1_nosh_noex;
192
        reg [4:0] col_key2_nosh_noex;
193
        reg [4:0] col_key1_sh_noex;
194
        reg [4:0] col_key2_sh_noex;
195
        reg [4:0] col_key1_nosh_ex;
196
        reg [4:0] col_key2_nosh_ex;
197
        reg [4:0] col_key1_sh_ex;
198
        reg [4:0] col_key2_sh_ex;
199
 
200
        reg [7:0] addrmap; // to make the addr registered, so XST will infer block RAM.
201
        reg [4:0] matrixstate = 0;
202
 
203
        always @(posedge clk) begin
204
                case (matrixstate)
205
 
206
                                        if (keyreceived) begin  // wait until a key is received.
207
                                                matrixstate <= 1;
208
                                                addrmap <= scancode;
209
                                        end
210
                                 end
211
                        1 : begin   // paralell read the same in the four possible combinations
212
                                        {row_key1_nosh_noex,
213
                                         col_key1_nosh_noex,
214
                                         row_key2_nosh_noex,
215
                                         col_key2_nosh_noex} <= mapa_noshift_noext[addrmap];
216
                                        {row_key1_sh_noex,
217
                                         col_key1_sh_noex,
218
                                         row_key2_sh_noex,
219
                                         col_key2_sh_noex} <= mapa_shift_noext[addrmap];
220
                                        {row_key1_nosh_ex,
221
                                         col_key1_nosh_ex,
222
                                         row_key2_nosh_ex,
223
                                         col_key2_nosh_ex} <= mapa_noshift_ext[addrmap];
224
                                        {row_key1_sh_ex,
225
                                         col_key1_sh_ex,
226
                                         row_key2_sh_ex,
227
                                         col_key2_sh_ex} <= mapa_shift_ext[addrmap];
228
                                        matrixstate <= 3;
229
                                 end
230
                        3 : begin  // apply matrix updates according to the actual combination pressed or released
231
                                        case ( {mayus,extended,released} )
232
                                                3'b000 : matrixstate <= 4;  //non shifted, non extended, key pressed
233
                                                3'b001 : matrixstate <= 6;  //non shifted, non extended, key released
234
                                                3'b010 : matrixstate <= 10; //non shifted, extended...
235
                                                3'b011 : matrixstate <= 12; //
236
                                                3'b100 : matrixstate <= 16; //shifted, non extended...
237
                                                3'b101 : matrixstate <= 18; //
238
                                                3'b110 : matrixstate <= 20; //shifted, extended...
239
                                                3'b111 : matrixstate <= 22; //
240
                                                default : matrixstate <= 0;
241
                                        endcase
242
                                 end
243
                        4 : begin // non shifted, non extended, key pressed, update key1 status
244
                                        thematrix[row_key1_nosh_noex] <= thematrix[row_key1_nosh_noex] & col_key1_nosh_noex;
245
                                        matrixstate <= 5;
246
                                 end
247
                        5 : begin // update key2 status
248
                                        thematrix[row_key2_nosh_noex] <= thematrix[row_key2_nosh_noex] & col_key2_nosh_noex;
249
                                        matrixstate <= 0; // go for another key
250
                                 end
251
 
252
                        6 : begin // non shifted, non extended, key released...
253
                                        thematrix[row_key1_nosh_noex] <= thematrix[row_key1_nosh_noex] | ~col_key1_nosh_noex;
254
                                        matrixstate <= 7;
255
                                 end
256
                        7 : begin
257
                                        thematrix[row_key2_nosh_noex] <= thematrix[row_key2_nosh_noex] | ~col_key2_nosh_noex;
258
                                        matrixstate <= 8;
259
                                 end
260
                        8 : begin // when a non shifted key releases, it released shifted version too...
261
                                        thematrix[row_key1_sh_noex] <= thematrix[row_key1_sh_noex] | ~col_key1_sh_noex;
262
                                        matrixstate <= 9;
263
                                 end
264
                        9 : begin
265
                                        thematrix[row_key2_sh_noex] <= thematrix[row_key2_sh_noex] | ~col_key2_sh_noex;
266
                                        matrixstate <= 0;
267
                                 end
268
 
269
                        10: begin // non shifted, extended, key pressed...
270
                                        thematrix[row_key1_nosh_ex] <= thematrix[row_key1_nosh_ex] & col_key1_nosh_ex;
271
                                        matrixstate <= 11;
272
                                 end
273
                        11: begin
274
                                        thematrix[row_key2_nosh_ex] <= thematrix[row_key2_nosh_ex] & col_key2_nosh_ex;
275
                                        matrixstate <= 0; // go for another key
276
                                 end
277
 
278
                        12: begin // non shifted, extended, key released...
279
                                        thematrix[row_key1_nosh_ex] <= thematrix[row_key1_nosh_ex] | ~col_key1_nosh_ex;
280
                                        matrixstate <= 13;
281
                                 end
282
                        13: begin
283
                                        thematrix[row_key2_nosh_ex] <= thematrix[row_key2_nosh_ex] | ~col_key2_nosh_ex;
284
                                        matrixstate <= 14;
285
                                 end
286
                        14: begin // when a non shifted key releases, it released shifted version too...
287
                                        thematrix[row_key1_sh_ex] <= thematrix[row_key1_sh_ex] | ~col_key1_sh_ex;
288
                                        matrixstate <= 15;
289
                                 end
290
                        15: begin
291
                                        thematrix[row_key2_sh_ex] <= thematrix[row_key2_sh_ex] | ~col_key2_sh_ex;
292
                                        matrixstate <= 0;
293
                                 end
294
 
295
                        16: begin // shifted, non extended, key pressed, update key1 status
296
                                        thematrix[row_key1_sh_noex] <= thematrix[row_key1_sh_noex] & col_key1_sh_noex;
297
                                        matrixstate <= 17;
298
                                 end
299
                        17: begin // update key2 status
300
                                        thematrix[row_key2_sh_noex] <= thematrix[row_key2_sh_noex] & col_key2_sh_noex;
301
                                        matrixstate <= 0; // go for another key
302
                                 end
303
 
304
                        18: begin // shifted, non extended, key released...
305
                                        thematrix[row_key1_sh_noex] <= thematrix[row_key1_sh_noex] | ~col_key1_sh_noex;
306
                                        matrixstate <= 19;
307
                                 end
308
                        19: begin
309
                                        thematrix[row_key2_sh_noex] <= thematrix[row_key2_sh_noex] | ~col_key2_sh_noex;
310
                                        matrixstate <= 0;
311
                                 end
312
 
313
                        20: begin // shifted, extended, key pressed, update key1 status
314
                                        thematrix[row_key1_sh_ex] <= thematrix[row_key1_sh_ex] & col_key1_sh_ex;
315
                                        matrixstate <= 21;
316
                                 end
317
                        21: begin // update key2 status
318
                                        thematrix[row_key2_sh_ex] <= thematrix[row_key2_sh_ex] & col_key2_sh_ex;
319
                                        matrixstate <= 0; // go for another key
320
                                 end
321
 
322
                        22: begin // shifted, extended, key released...
323
                                        thematrix[row_key1_sh_ex] <= thematrix[row_key1_sh_ex] | ~col_key1_sh_ex;
324
                                        matrixstate <= 23;
325
                                 end
326
                        23: begin
327
                                        thematrix[row_key2_sh_ex] <= thematrix[row_key2_sh_ex] | ~col_key2_sh_ex;
328
                                        matrixstate <= 0;
329
                                 end
330
                endcase
331
        end
332
endmodule
333
 
334
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.