c70e73f2bedb37f58e5175ccd8e3b8e8dfa16a98
[fceu.git] / input / fkb.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <string.h>
22 #include "share.h"
23 #include "fkb.h"
24 #define AK2(x,y)        ( (FKB_##x) | (FKB_##y <<8) )
25 #define AK(x)           FKB_##x
26
27 static uint8 bufit[0x49];
28 static uint8 ksmode;
29 static uint8 ksindex;
30
31
32 static uint16 matrix[9][2][4]=
33 {
34 {{AK(F8),AK(RETURN),AK(BRACKETLEFT),AK(BRACKETRIGHT)},   
35         {AK(KANA),AK(RIGHTSHIFT),AK(BACKSLASH),AK(STOP)}},
36 {{AK(F7),AK(AT),AK(COLON),AK(SEMICOLON)},               
37         {AK(UNDERSCORE),AK(SLASH),AK(MINUS),AK(CARET)}},
38 {{AK(F6),AK(O),AK(L),AK(K)},   
39         {AK(PERIOD),AK(COMMA),AK(P),AK(0)}},
40 {{AK(F5),AK(I),AK(U),AK(J)},    
41         {AK(M),AK(N),AK(9),AK(8)}},
42 {{AK(F4),AK(Y),AK(G),AK(H)},  
43         {AK(B),AK(V),AK(7),AK(6)}},
44 {{AK(F3),AK(T),AK(R),AK(D)},  
45         {AK(F),AK(C),AK(5),AK(4)}},
46 {{AK(F2),AK(W),AK(S),AK(A)},  
47         {AK(X),AK(Z),AK(E),AK(3)}},
48 {{AK(F1),AK(ESCAPE),AK(Q),AK(CONTROL)},    
49         {AK(LEFTSHIFT),AK(GRAPH),AK(1),AK(2)}},
50 {{AK(CLEAR),AK(UP),AK(RIGHT),AK(LEFT)},    
51         {AK(DOWN),AK(SPACE),AK(DELETE),AK(INSERT)}},
52 };
53
54 static void FP_FASTAPASS(1) FKB_Write(uint8 v)
55 {
56  v>>=1;
57  if(v&2)
58  {
59   if((ksmode&1) && !(v&1))
60    ksindex=(ksindex+1)%9;
61  }
62  ksmode=v;
63 }
64
65 static uint8 FP_FASTAPASS(2) FKB_Read(int w, uint8 ret)
66 {
67  //printf("$%04x, %d, %d\n",w+0x4016,ksindex,ksmode&1);
68  if(w)
69  {
70   int x;
71
72   ret&=~0x1E;
73   for(x=0;x<4;x++)
74     if(bufit[ matrix[ksindex][ksmode&1][x]&0xFF ] || bufit[ matrix[ksindex][ksmode&1][x]>>8])
75     {
76      ret|=1<<(x+1);
77     }
78   ret^=0x1E;
79  }
80  return(ret);
81 }
82
83 static void FKB_Strobe(void)
84 {
85  ksmode=0;
86  ksindex=0;
87  //printf("strobe\n");
88 }
89
90 static void FP_FASTAPASS(2) FKB_Update(void *data, int arg)
91 {
92  memcpy(bufit+1,data,0x48);
93 }
94
95 static INPUTCFC FKB={FKB_Read,FKB_Write,FKB_Strobe,FKB_Update,0,0};
96
97 INPUTCFC *FCEU_InitFKB(void)
98 {
99  memset(bufit,0,sizeof(bufit));
100  ksmode=ksindex=0;
101  return(&FKB);
102 }