OSDN Git Service

afce0af0840a11a28b831b5e6a95feca45de809f
[csp-qt/common_source_project-fm7.git] / source / src / vm / multi8 / keyboard.cpp
1 /*
2         MITSUBISHI Electric MULTI8 Emulator 'EmuLTI8'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.09.15 -
6
7         [ keyboard ]
8 */
9
10 #include "keyboard.h"
11
12 // f9 (78)=hard copy
13 // f10(79)=break
14 // f11(7a)=erase line
15 namespace MULTI8 {
16
17 static const uint8_t matrix_normal[256] = {
18         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x08,0x09,0x00,0x00, 0x00,0x0d,0x00,0x00,     // 00
19         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x1b, 0x00,0x00,0x00,0x00,     // 10
20         0x20,0x00,0x00,0x00, 0x0b,0x1d,0x1e,0x1c, 0x1f,0x00,0x00,0x00, 0x00,0x12,0x7f,0x00,     // 20
21         0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
22         0x00,0x41,0x42,0x43, 0x44,0x45,0x46,0x47, 0x48,0x49,0x4a,0x4b, 0x4c,0x4d,0x4e,0x4f,     // 40
23         0x50,0x51,0x52,0x53, 0x54,0x55,0x56,0x57, 0x58,0x59,0x5a,0x00, 0x00,0x00,0x00,0x00,     // 50
24         0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x2b, 0x00,0x00,0x2e,0x00,     // 60
25         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
26         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
27         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
28         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
29         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x3a,0x3b, 0x2c,0x2d,0x2e,0x2f,     // b0
30         0x40,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
31         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x5b, 0x5c,0x5d,0x5e,0x00,     // d0
32         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
33         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
34 };
35 static const uint8_t matrix_shift[256] = {
36         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x08,0x09,0x00,0x00, 0x00,0x0d,0x00,0x00,     // 00
37         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x1b, 0x00,0x00,0x00,0x00,     // 10
38         0x20,0x00,0x00,0x00, 0x0b,0x1d,0x1e,0x1c, 0x1f,0x00,0x00,0x00, 0x00,0x12,0x7f,0x00,     // 20
39         0x00,0x21,0x22,0x23, 0x24,0x25,0x26,0x27, 0x28,0x29,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
40         0x00,0x61,0x62,0x63, 0x64,0x65,0x66,0x67, 0x68,0x69,0x6a,0x6b, 0x6c,0x6d,0x6e,0x6f,     // 40
41         0x70,0x71,0x72,0x73, 0x74,0x75,0x76,0x77, 0x78,0x79,0x7a,0x00, 0x00,0x00,0x00,0x00,     // 50
42         0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x2b, 0x00,0x00,0x2e,0x00,     // 60
43         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
44         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
45         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
46         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
47         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x2a,0x2b, 0x3c,0x3d,0x3e,0x3f,     // b0
48         0x60,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
49         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x7b, 0x7c,0x7d,0x7e,0x00,     // d0
50         0x00,0x00,0x5f,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
51         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
52 };
53 static const uint8_t matrix_ctrl[256] = {
54         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 00
55         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 10
56         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 20
57         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
58         0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,     // 40
59         0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17, 0x18,0x19,0x1a,0x00, 0x00,0x00,0x00,0x00,     // 50
60         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 60
61         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
62         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
63         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
64         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
65         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // b0
66         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
67         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // d0
68         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
69         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
70 };
71 static const uint8_t matrix_graph[256] = {
72         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x08,0x00,0x00,0x00, 0x00,0x0d,0x00,0x00,     // 00
73         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x1b, 0x00,0x00,0x00,0x00,     // 10
74         0x20,0x00,0x00,0x00, 0x0b,0x1d,0x1e,0x1c, 0x1f,0x00,0x00,0x00, 0x00,0x12,0x7f,0x00,     // 20
75         0xf7,0xf8,0xf9,0xfa, 0xfb,0xf2,0xf3,0xf4, 0xf5,0xf6,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
76         0x00,0xec,0x84,0x82, 0x9f,0x9d,0xe6,0xe7, 0xf0,0xe8,0xea,0xeb, 0x8e,0x86,0x85,0xe9,     // 40
77         0x8d,0xed,0xe4,0x9e, 0xe5,0xef,0x83,0x9c, 0x81,0xee,0x80,0x00, 0x00,0x00,0x00,0x00,     // 50
78         0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x2b, 0x00,0x00,0x2e,0x00,     // 60
79         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
80         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
81         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
82         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
83         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x94,0x89, 0x87,0x8c,0x88,0x97,     // b0
84         0x8a,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
85         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xe1, 0xf1,0xe2,0x8b,0x00,     // d0
86         0x00,0x00,0xe3,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
87         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
88 };
89 static const uint8_t matrix_kana[256] = {
90         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x08,0x09,0x00,0x00, 0x00,0x0d,0x00,0x00,     // 00
91         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x1b, 0x00,0x00,0x00,0x00,     // 10
92         0x20,0x00,0x00,0x00, 0x0b,0x1d,0x1e,0x1c, 0x1f,0x00,0x00,0x00, 0x00,0x12,0x7f,0x00,     // 20
93         0xdc,0xc7,0xcc,0xb1, 0xb3,0xb4,0xb5,0xd4, 0xd5,0xd6,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
94         0x00,0xc1,0xba,0xbf, 0xbc,0xb2,0xca,0xb7, 0xb8,0xc6,0xcf,0xc9, 0xd8,0xd3,0xd0,0xd7,     // 40
95         0xbe,0xc0,0xbd,0xc4, 0xb6,0xc5,0xcb,0xc3, 0xbb,0xdd,0xc2,0x00, 0x00,0x00,0x00,0x00,     // 50
96         0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37, 0x38,0x39,0x00,0x2b, 0x00,0x00,0x2e,0x00,     // 60
97         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
98         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
99         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
100         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
101         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0xb9,0xda, 0xc8,0xce,0xd9,0xd2,     // b0
102         0xde,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
103         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xdf, 0xb0,0xd1,0xcd,0x00,     // d0
104         0x00,0x00,0xdb,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
105         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
106 };
107 static const uint8_t matrix_shiftkana[256] = {
108         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 00
109         0x00,0x00,0x00,0x13, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 10
110         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 20
111         0xa6,0x00,0x00,0xa7, 0xa9,0xaa,0xab,0xac, 0xad,0xae,0x00,0x00, 0x00,0x00,0x00,0x00,     // 30
112         0x00,0x00,0x00,0x00, 0x00,0xa8,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 40
113         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0xaf,0x00, 0x00,0x00,0x00,0x00,     // 50
114         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 60
115         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x10,0x03,0x15,0x00, 0x00,0x00,0x00,0x00,     // 70
116         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 80
117         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // 90
118         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // a0
119         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0xa4,0x00,0xa1,0xa5,     // b0
120         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // c0
121         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xa2, 0x00,0xa3,0x00,0x00,     // d0
122         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,     // e0
123         0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00      // f0
124 };
125
126 void KEYBOARD::initialize()
127 {
128         key_stat = emu->get_key_buffer();
129         register_frame_event(this);
130 }
131
132 void KEYBOARD::reset()
133 {
134         caps = caps_prev = false;
135         graph = graph_prev = false;
136         kana = kana_prev = false;
137         init = 1;
138         code = code_prev = 0;
139         stat = 0x8;
140 }
141
142 void KEYBOARD::write_io8(uint32_t addr, uint32_t data)
143 {
144         switch(addr & 0xff) {
145         case 0x00:
146                 break;
147         case 0x01:
148                 break;
149         }
150 }
151
152 uint32_t KEYBOARD::read_io8(uint32_t addr)
153 {
154         switch(addr & 0xff) {
155         case 0x00:
156                 if(init == 1) {
157                         init = 2;
158                         return 3;
159                 }
160                 if(code) {
161                         code_prev = code;
162                 }
163                 stat &= 0xfe;
164                 return code_prev;
165         case 0x01:
166                 if(init == 1) {
167                         return 1;
168                 } else if(init == 2) {
169                         init = 3;
170                         return 1;
171                 } else if(init == 3) {
172                         init = 0;
173                         return 0;
174                 }
175                 return stat;
176         }
177         return 0xff;
178 }
179
180 void KEYBOARD::event_frame()
181 {
182         bool shift = (key_stat[0x10] != 0);
183         bool ctrl = (key_stat[0x11] != 0);
184         caps = (key_stat[0x14] && !caps_prev) ? !caps : caps;
185         graph = (key_stat[0x12] && !graph_prev) ? !graph : graph;
186         kana = (key_stat[0x15] && !kana_prev) ? !kana : kana;
187         bool function = false;
188         
189         caps_prev = (key_stat[0x14] != 0);
190         graph_prev = (key_stat[0x12] != 0);
191         kana_prev = (key_stat[0x15] != 0);
192         
193         uint8_t next_stat, next_code = 0;
194         
195         if(key_stat[0x70]) {
196                 next_code = 0;
197                 function = true;
198         } else if(key_stat[0x71]) {
199                 next_code = 1;
200                 function = true;
201         } else if(key_stat[0x72]) {
202                 next_code = 2;
203                 function = true;
204         } else if(key_stat[0x73]) {
205                 next_code = 3;
206                 function = true;
207         } else {
208                 const uint8_t* matrix = matrix_normal;
209                 if(ctrl) {
210                         matrix = matrix_ctrl;
211                 } else if(graph) {
212                         matrix = matrix_graph;
213                 } else if(kana && shift) {
214                         matrix = matrix_shiftkana;
215                 } else if(kana && !shift) {
216                         matrix = matrix_kana;
217                 } else if(shift) {
218                         matrix = matrix_shift;
219                 }
220                 for(int i = 0; i < 256; i++) {
221                         if(key_stat[i]) {
222                                 next_code = matrix[i];
223                         }
224                         if(next_code) {
225                                 break;
226                         }
227                 }
228                 if(caps) {
229                         if('a' <= next_code && next_code <= 'z') {
230                                 next_code -= 0x20;
231                         } else if('A' <= next_code && next_code <= 'Z') {
232                                 next_code += 0x20;
233                         }
234                 }
235         }
236         bool press = (next_code || function);
237         next_stat = (shift ? 0x80 : 0) | (function ? 0x40 : 0) | (press ? 0 : 0x08);
238         
239         if(next_code != code && press) {
240                 next_stat |= 0x01;
241         }
242         code = next_code;
243         stat = next_stat;
244 }
245
246 #define STATE_VERSION   1
247
248 bool KEYBOARD::process_state(FILEIO* state_fio, bool loading)
249 {
250         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
251                 return false;
252         }
253         if(!state_fio->StateCheckInt32(this_device_id)) {
254                 return false;
255         }
256         state_fio->StateInt32(init);
257         state_fio->StateUint8(code);
258         state_fio->StateUint8(code_prev);
259         state_fio->StateUint8(stat);
260         state_fio->StateBool(caps);
261         state_fio->StateBool(caps_prev);
262         state_fio->StateBool(graph);
263         state_fio->StateBool(graph_prev);
264         state_fio->StateBool(kana);
265         state_fio->StateBool(kana_prev);
266         return true;
267 }
268
269 }