OSDN Git Service

fd360354a0fb66ee1736fe63c4c856d5af60bf4f
[csp-qt/common_source_project-fm7.git] / source / src / vm / sc3000 / keyboard.cpp
1 /*
2         SEGA SC-3000 Emulator 'eSC-3000'
3
4         Author : Takeda.Toshiya
5         Date   : 2010.08.17-
6
7         [ keyboard ]
8 */
9
10 #include "keyboard.h"
11 #include "../i8255.h"
12
13 static const uint8 key_map[8][12] = {
14         { 0x31, 0x51, 0x41, 0x5a, 0x15, 0xbc, 0x4b, 0x49, 0x38, 0x00, 0x00, 0x00 },
15         { 0x32, 0x57, 0x53, 0x58, 0x20, 0xbe, 0x4c, 0x4f, 0x39, 0x00, 0x00, 0x00 },
16         { 0x33, 0x45, 0x44, 0x43, 0x24, 0xbf, 0xbb, 0x50, 0x30, 0x00, 0x00, 0x00 },
17         { 0x34, 0x52, 0x46, 0x56, 0x2e, 0xe2, 0xba, 0xc0, 0xbd, 0x00, 0x00, 0x00 },
18         { 0x35, 0x54, 0x47, 0x42, 0x00, 0x28, 0xdd, 0xdb, 0xde, 0x00, 0x00, 0x00 },
19         { 0x36, 0x59, 0x48, 0x4e, 0x00, 0x25, 0x0d, 0x00, 0xdc, 0x00, 0x00, 0x29 },
20         { 0x37, 0x55, 0x4a, 0x4d, 0x00, 0x27, 0x26, 0x00, 0x13, 0x12, 0x11, 0x10 },
21         { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x81, 0x82, 0x84, 0x88, 0x90, 0xa0 }
22 };
23
24 void KEYBOARD::initialize()
25 {
26         key_stat = emu->key_buffer();
27         joy_stat = emu->joy_buffer();
28         
29         column = 0;
30         break_pressed = false;
31         
32         // register event to update the key status
33         register_frame_event(this);
34 }
35
36 void KEYBOARD::event_frame()
37 {
38         bool new_pressed = (key_stat[0x13] != 0);
39         if(new_pressed && !break_pressed) {
40                 d_cpu->write_signal(SIG_CPU_NMI, 1, 1);
41         }
42         break_pressed = new_pressed;
43         
44         update_keyboard();
45 }
46
47 void KEYBOARD::write_signal(int id, uint32 data, uint32 mask)
48 {
49         if(column != (data & mask)) {
50                 column = data & mask;
51                 update_keyboard();
52         }
53 }
54
55 void KEYBOARD::update_keyboard()
56 {
57         uint32 data = 0;
58         
59         if(column < 7) {
60                 // keyboard
61                 for(int i = 0; i < 12; i++) {
62                         if(key_stat[key_map[column][i]]) {
63                                 data |= (1 << i);
64                         }
65                 }
66         } else {
67                 // joystick
68                 for(int i = 0; i < 12; i++) {
69                         uint8 map = key_map[7][i];
70                         uint8 stat = (map & 0x80) ? joy_stat[1] : joy_stat[0];
71                         if(stat & (map & 0x3f)) {
72                                 data |= (1 << i);
73                         }
74                 }
75         }
76         d_pio->write_signal(SIG_I8255_PORT_A, ~data, 0xff);
77         data >>= 8;
78         d_pio->write_signal(SIG_I8255_PORT_B, ~data, 0x0f);
79 }
80
81 #define STATE_VERSION   1
82
83 void KEYBOARD::save_state(FILEIO* state_fio)
84 {
85         state_fio->FputUint32(STATE_VERSION);
86         state_fio->FputInt32(this_device_id);
87         
88         state_fio->FputUint8(column);
89         state_fio->FputBool(break_pressed);
90 }
91
92 bool KEYBOARD::load_state(FILEIO* state_fio)
93 {
94         if(state_fio->FgetUint32() != STATE_VERSION) {
95                 return false;
96         }
97         if(state_fio->FgetInt32() != this_device_id) {
98                 return false;
99         }
100         column = state_fio->FgetUint8();
101         break_pressed = state_fio->FgetBool();
102         return true;
103 }
104