OSDN Git Service

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