/
os345interrupts.c
183 lines (149 loc) · 6.05 KB
/
os345interrupts.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// os345interrupts.c - pollInterrupts 08/08/2013
// ***********************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the BYU CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <time.h>
#include <assert.h>
#include "os345.h"
#include "os345config.h"
#include "os345signals.h"
// **********************************************************************
// local prototypes
//
void pollInterrupts(void);
static void keyboard_isr(void);
static void timer_isr(void);
// **********************************************************************
// **********************************************************************
// global semaphores
extern Semaphore *keyboard; // keyboard semaphore
extern Semaphore *charReady; // character has been entered
extern Semaphore *inBufferReady; // input buffer ready semaphore
extern Semaphore *tics1sec; // 1 second semaphore
extern Semaphore *tics10sec; // 10 second semaphore
extern Semaphore *tics10thsec; // 1/10 second semaphore
extern char inChar; // last entered character
extern int charFlag; // 0 => buffered input
extern int inBufIndx; // input pointer into input buffer
extern char inBuffer[INBUF_SIZE + 1]; // character input buffer
extern time_t oldTime1; // old 1sec time
extern time_t oldTime10; // old 10sec time
extern clock_t myClkTime;
extern clock_t myOldClkTime;
extern int pollClock; // current clock()
extern int lastPollClock; // last pollClock
extern int superMode; // system mode
// **********************************************************************
// **********************************************************************
// simulate asynchronous interrupts by polling events during idle loop
//
void pollInterrupts(void) {
// check for task monopoly
pollClock = clock();
assert("Timeout" && ((pollClock - lastPollClock) < MAX_CYCLES));
lastPollClock = pollClock;
// check for keyboard interrupt
if ((inChar = GET_CHAR) > 0) {
keyboard_isr();
}
// timer interrupt
timer_isr();
return;
} // end pollInterrupts
// **********************************************************************
// keyboard interrupt service routine
//
static void keyboard_isr() {
// assert system mode
assert("keyboard_isr Error" && superMode);
semSignal(charReady); // SIGNAL(charReady) (No Swap)
if (charFlag == 0) {
switch (inChar) {
case '\r':
case '\n':
inBufIndx = 0; // EOL, signal line ready
semSignal(inBufferReady); // SIGNAL(inBufferReady)
break;
case 127:
if (inBufIndx > 0) {
inBuffer[--inBufIndx] = 0;
printf("\b \b");
}
break;
case 0x12: // ^R
printf("^R");
sigSignal(-1, mySIGCONT);
for (int taskId = 0; taskId < MAX_TASKS; taskId++) {
sigClearSignal(-1, mySIGSTOP);
sigClearSignal(-1, mySIGTSTP);
}
break;
case 0x17: // ^W
printf("^W");
sigSignal(-1, mySIGTSTP);
break;
case 0x18: // ^X
printf("^X");
inBufIndx = 0;
inBuffer[0] = 0;
sigSignal(0, mySIGINT); // interrupt task 0
semSignal(inBufferReady); // SEM_SIGNAL(inBufferReady)
break;
default:
inBuffer[inBufIndx++] = inChar;
inBuffer[inBufIndx] = 0;
printf("%c", inChar); // echo character
}
} else {
// single character mode
inBufIndx = 0;
inBuffer[inBufIndx] = 0;
}
return;
} // end keyboard_isr
// **********************************************************************
// timer interrupt service routine
//
static void timer_isr() {
time_t currentTime; // current time
// assert system mode
assert("timer_isr Error" && superMode);
// capture current time
time(¤tTime);
// one second timer
if ((currentTime - oldTime1) >= 1) {
// signal 1 second
semSignal(tics1sec);
oldTime1 += 1;
}
if ((currentTime - oldTime10) >= 10) {
semSignal(tics10sec);
oldTime10 += 10;
}
// sample fine clock
myClkTime = clock();
if ((myClkTime - myOldClkTime) >= ONE_TENTH_SEC) {
myOldClkTime = myOldClkTime + ONE_TENTH_SEC; // update old
semSignal(tics10thsec);
}
// ?? add other timer sampling/signaling code here for project 2
return;
} // end timer_isr