aboutsummaryrefslogtreecommitdiff
path: root/io.c
blob: aa9b0275b09fe7fc11307550822c338b9d6fa0ec (plain)
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
184
185
186
187
188
189
190
191
192
/*
 * IO functionality
 * for MPX-OS
 * Ben Burwell & Averill Morash
 * CSI350 - Fall 2013
 */

#include <dos.h>
#include <stdio.h>
#include "mpx.h"

#define DS_OFFSET   6 // was 3 before

int count = 0;
void IO_sup(pcb *p) {
  char far *buffer;
  int far *length;
  unsigned *ds_add;
  int con_w = 0;
  int rc;

  disable();

  if (DEBUG_PRINTS) { printf("in io_sup \n");}

  //make the far pointers (see page 107)
  ds_add = p->stack_ptr + DS_OFFSET;
  buffer = MK_FP(*ds_add, p->parm_add->buffer);
  length = MK_FP(*ds_add, p->parm_add->length);

  //call com_read or equivalent
  switch (p->parm_add->op_number){
	case COM:
	  // IO_sched should have validated input
	  // Assume that op_type is either read or write
	  if (p->parm_add->op_type == READ) {
		 if (DEBUG_PRINTS){printf("com read req \n");}
		 com_eflag = 0;
		 rc = com_read(buffer, length);
	  } else {
		 if (DEBUG_PRINTS) {printf("com write req \n");}
		 com_eflag = 0;
		 rc = com_write(buffer, length);
	  }
	  break;
	case CON:
	  if (p->parm_add->op_type == READ) {
		if (DEBUG_PRINTS){printf("con read req\n");}
		con_eflag = 0;
		con.current_op = READ_OP;
		rc = con_read(buffer, length);
	  } else {
		if (DEBUG_PRINTS){printf("con_write req\n");}
		con_w = 1;
		rc = con_write(buffer, length);
	  }
	  break;
	case PRT:
	  if (DEBUG_PRINTS){printf("prt write req\n");}
	  prt_eflag = 0;
	  rc = prt_write(buffer, length);
	  break;
  }

  if (rc < 0) {
	printf("Somthing went wrong in io_sup\n");
  }

  // put the pcb back in the ready queue, if it was a con_write
  if (con_w == 1) {
	insert_pcb(&ready_queue_locked, p,0);
  }
  enable();
  return;
}

// Handle completion of an IO request
int IO_complete(int device, int *stk_ptr) {

  pcb *current;
  dcb *d;

  disable();
  switch (device){
	case CON:
	  d = &con;
	  d->current_op = NO_OP;
	  break;
	case COM:
	  d = &com;
	  break;
	case PRT:
	  d = &prt;
	  break;
  }

  if (DEBUG_PRINTS){printf("in io complete\n");}

  // save interrupted process's stack pointer
  cop->stack_ptr = stk_ptr;

  // insert running process into ready queue
  insert_pcb(&ready_queue_locked, cop, 0);

  // set running process to ready state
  cop->state = READY;

  // get address of PCB whose IO just completed
  current = d->current_pcb;

  // insert into ready queue
  insert_pcb(&ready_queue_locked, current, 0);

  // set its state to ready
  current->state = READY;

  if (strcmp(d->current_pcb->name, "iocom2")==0) {
	count++;
	if (count > 20) {
	d = &com;
	}
  }
  if (d->pcb_head != NULL) {
	// if there are pending IO requests for this device
	//schedule them (IO_sched)
	remove_pcb(&(d->pcb_head), d->pcb_head);
	IO_sched(d->pcb_head);
  }

  outportb(0x20,0x20); // EOI signal
  enable();
  dispatch();

  return 0;
}

// Service an IO request
// Returns:
//    0 for success
//   -1 for invalid operation
//   -2 for invalid device
int IO_sched(pcb *p) {

  dcb *d;
  int op_num;
  int op_type;

  if (DEBUG_PRINTS) {printf("in io sched\n");}

  // determine which device is requested
  op_num = p->parm_add->op_number;
  op_type = p->parm_add->op_type;

  switch (op_num) {
	case CON:
	  if (op_type != READ
		&& op_type != WRITE) {
		  return -1;
	  }
	  d = &con;
	  break;
	case COM:
	  if (op_type != READ
		&& op_type != WRITE) {
		  return -1;
	  }
	  d = &com;
	  break;
	case PRT:
	  if (op_type != WRITE) {
		return -1;
	  }
	  d = &prt;
	  break;
	default:
	  return -2;
  }

  if (d->current_op != NO_OP) {
	disable();
	// put pcb in IO queue
	insert_pcb(&(d->pcb_head), p, 1);
	// take pcb out of ready q
	remove_pcb(&ready_queue_locked, p);
	enable();
  } else {
	d->current_pcb = p;
	IO_sup(p);
  }

  return 0;
}