-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhardware_keyboard.go
More file actions
128 lines (118 loc) · 2.09 KB
/
hardware_keyboard.go
File metadata and controls
128 lines (118 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package gemu
const (
CLEAR_BUFFER uint16 = 0
GET_NEXT = 1
CHECK_KEY = 2
SET_INT = 3
SET_MODE = 4
)
var keyboardClass = &HardwareClass{
Name: "keyboard",
Desc: "Generic Keyboard",
DevID: 0x30c17406,
VerID: 0x0001,
MfgID: 0x1c6c8b36,
}
func init() {
RegisterClass(keyboardClass)
}
type Keyboard struct {
Hardware
keycount int
keybuffer [8]uint8
keydown [8]uint8
interrupt uint16
mode uint16
}
func NewKeyboard() *Keyboard {
keyboard := &Keyboard{}
keyboard.Class = keyboardClass
return keyboard
}
func (K *Keyboard) HWI(D *DCPU) {
switch D.Reg[0] {
case CLEAR_BUFFER:
K.keycount = 0
case GET_NEXT:
if K.keycount > 0 {
D.Reg[2] = uint16(K.keybuffer[0])
for i := 0; i < 7; i++ {
K.keybuffer[i] = K.keybuffer[i+1]
}
K.keycount--
} else {
D.Reg[2] = 0
}
case CHECK_KEY:
D.Reg[2] = 0
for i := 0; i < 7; i++ {
if K.keydown[i] == uint8(D.Reg[1]) {
D.Reg[2] = 1
}
}
case SET_INT:
K.interrupt = D.Reg[1]
case SET_MODE:
K.mode = D.Reg[1]
K.keycount = 0
for i := range K.keydown {
K.keydown[i] = 0
}
}
}
func (K *Keyboard) RawKey(key uint16, state bool) {
if key >= 0x80 || K.mode == 1 {
if state {
handled := false
for i := 0; i < 8; i++ {
if K.keydown[i] == 0 {
K.keydown[i] = uint8(key)
handled = true
break
}
}
if !handled {
K.keydown[7] = uint8(key)
}
} else {
for i := 0; i < 8; i++ {
if K.keydown[i] == uint8(key) {
K.keydown[i] = 0
}
}
}
if !state {
key |= 0x8000
}
if state || K.mode == 1 {
K.queueKey(key)
}
}
}
func (K *Keyboard) ParsedKey(key uint16) {
if K.mode == 0 {
K.queueKey(key)
}
}
func (K *Keyboard) queueKey(key uint16) {
if K.keycount < 8 {
K.keybuffer[K.keycount] = uint8(key)
K.keycount++
} else {
copy(K.keybuffer[:], K.keybuffer[1:])
K.keybuffer[7] = uint8(key)
}
if K.interrupt != 0 {
if K.Up != nil {
if dcpu, ok := K.Up.(*DCPU); ok {
dcpu.Int(K.interrupt)
}
}
}
}
func (K *Keyboard) Reset() {
K.keycount = 0
for i := range K.keydown {
K.keydown[i] = 0
}
}