Revision 9
FINAL
lab3.c | ||
---|---|---|
6 | 6 |
#include <stdbool.h> |
7 | 7 |
#include "i8254.h" |
8 | 8 |
#include <stdint.h> |
9 |
#include "i8042.h" |
|
10 |
#include "keyboard.h" |
|
9 | 11 |
//#include <timer.c> |
10 | 12 |
|
11 | 13 |
|
12 |
#define IRQ_KB 1 |
|
14 |
/* #define IRQ_KB 1
|
|
13 | 15 |
#define DELAY_US 20000 |
14 | 16 |
#define EXIT_CODE 0x81 |
15 |
#define I_O_BUFFER 0x60 |
|
17 |
#define I_O_BUFFER 0x60 */
|
|
16 | 18 |
|
17 |
static uint32_t scancode; |
|
19 |
uint32_t cnt=0; |
|
20 |
uint32_t scancode; |
|
21 |
int flag=0; |
|
18 | 22 |
|
19 | 23 |
int main(int argc, char *argv[]) { |
20 | 24 |
// sets the language of LCF messages (can be either EN-US or PT-PT) |
... | ... | |
40 | 44 |
return 0; |
41 | 45 |
} |
42 | 46 |
|
43 |
void(kbc_ih)() {} |
|
44 | 47 |
|
48 |
|
|
45 | 49 |
int(kbd_test_scan)() { |
46 | 50 |
/* To be completed by the students */ |
47 | 51 |
int ipc_status; |
48 | 52 |
uint8_t r, bit_no = 0; |
49 | 53 |
message msg; |
50 | 54 |
int hook_id = bit_no; |
51 |
uint8_t scan_arr[2] = {0, 0}; |
|
55 |
/* uint8_t scan_arr[2] = {0, 0};
|
|
52 | 56 |
|
53 |
int flag = 0; |
|
54 |
uint32_t cnt=0; |
|
57 |
int flag = 0; */ |
|
55 | 58 |
|
56 | 59 |
printf("%d\n", |
57 | 60 |
sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id)); |
... | ... | |
84 | 87 |
case HARDWARE: /* hardware interrupt notification */ |
85 | 88 |
if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */ |
86 | 89 |
|
87 |
#ifdef LAB3 |
|
88 |
cnt++; |
|
89 |
#endif |
|
90 |
sys_inb(0x64, &scancode); |
|
91 |
if (scancode & 0x01) { |
|
92 |
#ifdef LAB3 |
|
93 |
cnt++; |
|
94 |
#endif |
|
95 |
sys_inb(0x60, &scancode); |
|
96 |
if (scancode != 0xe0 && flag == 0) { |
|
97 |
// tickdelay(micros_to_ticks(DELAY_US)); |
|
98 |
scan_arr[0] = scancode; |
|
99 |
kbd_print_scancode(!(scancode & 0x80), 1, scan_arr); |
|
90 |
kbc_ih(); |
|
100 | 91 |
} |
101 | 92 |
|
102 |
else if (scancode == 0xe0) { |
|
103 |
scan_arr[0] = scancode; |
|
104 |
flag = 1; |
|
105 |
} |
|
106 |
|
|
107 |
else if (flag == 1) { |
|
108 |
scan_arr[1] = scancode; |
|
109 |
kbd_print_scancode(!(scancode & 0x80), 2, scan_arr); |
|
110 |
flag = 0; |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
} |
|
93 |
|
|
115 | 94 |
// printf("msg:%x\n",msg.m_notify.interrupts); |
116 | 95 |
break; |
117 | 96 |
default: |
... | ... | |
134 | 113 |
/* To be completed by the students */ |
135 | 114 |
uint32_t key=0; |
136 | 115 |
uint8_t scan_arr[2] = {0, 0}; |
137 |
int flag = 0; |
|
138 |
uint32_t cnt=0; |
|
139 |
sys_outb(0x64,0x20); //enable interrupts again |
|
140 |
|
|
141 |
sys_outb(0x64, 0x60); |
|
142 |
sys_inb(0x60,&key); |
|
116 |
//int flag = 0; |
|
117 |
//uint32_t cnt=0; |
|
118 |
|
|
143 | 119 |
while(scancode != EXIT_CODE) |
144 | 120 |
{ |
145 | 121 |
tickdelay(micros_to_ticks(DELAY_US)); |
146 | 122 |
#ifdef LAB3 |
147 | 123 |
cnt++; |
148 | 124 |
#endif |
149 |
sys_inb(0x64, &scancode);
|
|
150 |
if (scancode & 0x01) {
|
|
125 |
sys_inb(IN_BUF, &scancode);
|
|
126 |
if (scancode & O_BUFFER_FULL) {
|
|
151 | 127 |
#ifdef LAB3 |
152 | 128 |
cnt++; |
153 | 129 |
#endif |
154 |
sys_inb(0x60, &scancode);
|
|
155 |
if (scancode != 0xe0 && flag == 0) {
|
|
130 |
sys_inb(I_O_BUFFER, &scancode);
|
|
131 |
if (scancode != TWO_BYTES && flag == 0) {
|
|
156 | 132 |
scan_arr[0] = scancode; |
157 |
kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
|
|
133 |
kbd_print_scancode(!(scancode & PAR_ERR), 1, scan_arr);
|
|
158 | 134 |
} |
159 | 135 |
|
160 |
else if (scancode == 0xe0) {
|
|
136 |
else if (scancode == TWO_BYTES) {
|
|
161 | 137 |
scan_arr[0] = scancode; |
162 | 138 |
flag = 1; |
163 | 139 |
} |
164 | 140 |
|
165 | 141 |
else if (flag == 1) { |
166 | 142 |
scan_arr[1] = scancode; |
167 |
kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
|
|
143 |
kbd_print_scancode(!(scancode & PAR_ERR), 2, scan_arr);
|
|
168 | 144 |
flag = 0; |
169 | 145 |
} |
170 | 146 |
} |
171 | 147 |
} |
148 |
sys_outb(IN_BUF, READ_CMD); |
|
149 |
sys_outb(IN_BUF, I_O_BUFFER); |
|
150 |
|
|
151 |
#ifdef LAB3 |
|
152 |
cnt++; |
|
153 |
#endif |
|
154 |
|
|
155 |
sys_inb(I_O_BUFFER,&key); |
|
156 |
|
|
157 |
sys_outb(IN_BUF,I_O_BUFFER); //enable interrupts again |
|
158 |
sys_outb(I_O_BUFFER, key | BIT(0)); |
|
159 |
|
|
172 | 160 |
kbd_print_no_sysinb(cnt); |
173 |
sys_outb(0x64,0x60); //enable interrupts again |
|
174 |
sys_outb(0x60, key | BIT(0)); //enable interrupts again |
|
175 | 161 |
return 0; |
176 | 162 |
} |
177 | 163 |
|
178 | 164 |
int(kbd_test_timed_scan)(uint8_t n) { |
179 | 165 |
/* To be completed by the students */ |
180 |
uint8_t scan_arr[2] = {0, 0}; |
|
181 |
int flag=0; |
|
166 |
/* uint8_t scan_arr[2] = {0, 0};
|
|
167 |
int flag=0; */
|
|
182 | 168 |
int ipc_status; |
183 | 169 |
message msg; |
184 | 170 |
uint8_t r; |
185 | 171 |
uint8_t bit_no_timer = 0, bit_no_kbd = 1; |
186 | 172 |
int hook_id_kbd=bit_no_kbd, hook_id_timer=bit_no_timer; |
187 |
int cont=0,cnt=0;
|
|
173 |
int cont=0;//cnt=0;
|
|
188 | 174 |
|
189 | 175 |
sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id_timer); |
190 | 176 |
sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id_kbd); |
... | ... | |
212 | 198 |
if (msg.m_notify.interrupts & BIT(bit_no_kbd)) |
213 | 199 |
{ |
214 | 200 |
|
215 |
#ifdef LAB3 |
|
216 |
cnt++; |
|
217 |
#endif |
|
218 |
sys_inb(0x64, &scancode); |
|
219 |
if (scancode & 0x01) { |
|
220 |
#ifdef LAB3 |
|
221 |
cnt++; |
|
222 |
#endif |
|
223 |
sys_inb(0x60, &scancode); |
|
224 |
if (scancode != 0xe0 && flag == 0) |
|
225 |
{ |
|
226 |
|
|
227 |
scan_arr[0] = scancode; |
|
228 |
kbd_print_scancode(!(scancode & 0x80), 1, scan_arr); |
|
229 |
} |
|
230 |
|
|
231 |
else if (scancode == 0xe0) |
|
232 |
{ |
|
233 |
scan_arr[0] = scancode; |
|
234 |
flag = 1; |
|
235 |
} |
|
236 |
|
|
237 |
else if (flag == 1) |
|
238 |
{ |
|
239 |
scan_arr[1] = scancode; |
|
240 |
kbd_print_scancode(!(scancode & 0x80), 2, scan_arr); |
|
241 |
flag = 0; |
|
242 |
} |
|
201 |
kbc_ih(); |
|
243 | 202 |
cont=0; |
244 | 203 |
} |
245 | 204 |
//printf("msg:%x\n",msg.m_notify.interrupts); |
... | ... | |
252 | 211 |
// printf("3\n"); /* received a standard message, not a notification */ |
253 | 212 |
/* no standard messages expected: do nothing */ |
254 | 213 |
|
214 |
|
|
255 | 215 |
} |
256 |
} |
|
257 | 216 |
kbd_print_no_sysinb(cnt); |
258 | 217 |
|
259 | 218 |
if(sys_irqrmpolicy(&hook_id_timer)!=0) return 1; |
Also available in: Unified diff