Project

General

Profile

Statistics
| Revision:

root / lab3 / lab3.c @ 7

History | View | Annotate | Download (4.37 KB)

1
#include <lcom/lcf.h>
2
#include <minix/sysutil.h>
3

    
4
#include <lcom/lab3.h>
5

    
6
#include <stdbool.h>
7
#include <stdint.h>
8
//#include <timer.c>
9

    
10

    
11
#define IRQ_KB 1
12
#define DELAY_US 20000
13
#define EXIT_CODE 0x81
14
#define I_O_BUFFER 0x60
15

    
16
static uint32_t scancode;
17

    
18
int main(int argc, char *argv[]) {
19
  // sets the language of LCF messages (can be either EN-US or PT-PT)
20
  lcf_set_language("EN-US");
21

    
22
  // enables to log function invocations that are being "wrapped" by LCF
23
  // [comment this out if you don't want/need it]
24
  lcf_trace_calls("/home/lcom/labs/lab3/trace.txt");
25

    
26
  // enables to save the output of printf function calls on a file
27
  // [comment this out if you don't want/need it]
28
  lcf_log_output("/home/lcom/labs/lab3/output.txt");
29

    
30
  // handles control over to LCF
31
  // [LCF handles command line arguments and invokes the right function]
32
  if (lcf_start(argc, argv))
33
    return 1;
34

    
35
  // LCF clean up tasks
36
  // [must be the last statement before return]
37
  lcf_cleanup();
38

    
39
  return 0;
40
}
41

    
42
void(kbc_ih)() {}
43

    
44
int(kbd_test_scan)() {
45
  /* To be completed by the students */
46
  int ipc_status;
47
  uint8_t r, bit_no = 0;
48
  message msg;
49
  int hook_id = bit_no;
50
  uint8_t scan_arr[2] = {0, 0};
51
  scan_arr[0] = scancode & 0xFF;
52
  scan_arr[1] = scancode & 0xFF00;
53
  int flag = 0;
54
  uint32_t cnt=0;
55

    
56
  printf("%d\n",
57
         sys_irqsetpolicy(IRQ_KB, IRQ_REENABLE | IRQ_EXCLUSIVE, &hook_id));
58
  /* sys_outb(0x64, 0x20);
59
  sys_inb(0x64, &scancode);
60
  printf("%x\n", scancode);
61
  if (scancode & 0x10) {
62
    scancode = (scancode & 0xEF);
63
  }
64

65
  printf("%x\n", scancode);
66
  sys_outb(0x64, 0x60);
67
  sys_outb(0x64, scancode); */
68

    
69
  printf("%x\n", scancode);
70
  //kbc_ih();
71
  printf("\n");
72
  while (scancode !=
73
         EXIT_CODE) { /* You may want to use a different condition */
74
    /* Get a request message. */
75
    // printf("1\n");
76
    // printf("cont:%d, time:%d\n\n",cont/60,time);
77
    if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
78
      printf("driver_receive failed with: %d", r);
79
      continue;
80
    }
81
    if (is_ipc_notify(ipc_status)) { /* received notification */
82
      switch (_ENDPOINT_P(msg.m_source)) {
83
      case HARDWARE: /* hardware interrupt notification */
84
        if (msg.m_notify.interrupts & BIT(0)) { /* subscribed interrupt */
85

    
86
          #ifdef LAB3 
87
          cnt++; 
88
          #endif
89
          sys_inb(0x64, &scancode);
90
          if (scancode & 0x01) {
91
            #ifdef LAB3 
92
            cnt++; 
93
            #endif
94
            sys_inb(0x60, &scancode);
95
          if (scancode != 0xe0 && flag == 0) {
96
            // tickdelay(micros_to_ticks(DELAY_US));
97
            scan_arr[0] = scancode;
98
            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
99
          }
100

    
101
          else if (scancode == 0xe0) {
102
            scan_arr[0] = scancode;
103
            flag = 1;
104
          }
105

    
106
          else if (flag == 1) {
107
            scan_arr[1] = scancode;
108
            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
109
            flag = 0;
110
          }
111
          }
112

    
113
        }
114
        // printf("msg:%x\n",msg.m_notify.interrupts);
115
        break;
116
      default:
117
        // printf("2\n");
118
        break; /* no other notifications expected: do nothing */
119
      }
120
    } else {
121
      // printf("3\n"); /* received a standard message, not a notification */
122
      /* no standard messages expected: do nothing */
123
    }
124
  }
125
  kbd_print_no_sysinb(cnt);
126
  if (sys_irqrmpolicy(&hook_id) != 0)
127
    return 1;
128

    
129
  return 0;
130
}
131

    
132
int(kbd_test_poll)() {
133
  /* To be completed by the students */
134
  uint8_t scan_arr[2] = {0, 0};
135
  int flag = 0;
136
  uint32_t cnt=0;
137
  while(scancode != EXIT_CODE)
138
  {
139
    tickdelay(micros_to_ticks(DELAY_US));
140
    #ifdef LAB3 
141
    cnt++; 
142
    #endif
143
    sys_inb(0x64, &scancode);
144
          if (scancode & 0x01) {
145
            #ifdef LAB3 
146
            cnt++;
147
             #endif
148
            sys_inb(0x60, &scancode);
149
          if (scancode != 0xe0 && flag == 0) {
150
            scan_arr[0] = scancode;
151
            kbd_print_scancode(!(scancode & 0x80), 1, scan_arr);
152
          }
153

    
154
          else if (scancode == 0xe0) {
155
            scan_arr[0] = scancode;
156
            flag = 1;
157
          }
158

    
159
          else if (flag == 1) {
160
            scan_arr[1] = scancode;
161
            kbd_print_scancode(!(scancode & 0x80), 2, scan_arr);
162
            flag = 0;
163
          }
164
        }
165

    
166
  }
167
  kbd_print_no_sysinb(cnt);
168

    
169
  return 1;
170
}
171

    
172
int(kbd_test_timed_scan)(uint8_t n) {
173
  /* To be completed by the students */
174
  printf("%s is not yet implemented!\n", __func__);
175

    
176
  return 1;
177
}