aboutsummaryrefslogtreecommitdiff
path: root/sim/ppc/devices.c
blob: 96b8109b326b70b493f366e37af54107b8af74ab (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*  This file is part of the program psim.

    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */


#ifndef _DEVICES_C_
#define _DEVICES_C_

#ifndef STATIC_INLINE_DEVICES
#define STATIC_INLINE_DEVICES STATIC_INLINE
#endif

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

#include "basics.h"
#include "device_tree.h"
#include "devices.h"
#include "events.h"

#include "cpu.h" /* drats */

/* Helper functions */

STATIC_INLINE_DEVICES void
parse_device_address(char *name,
		     unsigned *base,
		     unsigned *flags)
{
  /* extract the two arguments */
  name = strchr(name, '@');
  if (name == NULL)
    error("missing address for device %s\n", name);
  name++;
  *base = strtol(name, &name, 0);
  *flags = (*name == ','
	    ? strtol(name+1, &name, 0)
	    : 0);
}


/* Simple console device:

   Implements a simple text output device that is attached to stdout
   of the process running the simulation.  The devices has four
   word registers:

   0: read
   4: read-status
   8: write
   c: write-status

   Where a nonzero status register indicates that the device is ready
   (input fifo contains a character or output fifo has space).

   Illustrates: Mapping read/write to device operations onto actual
   registers.

   */

typedef struct _console_buffer {
  char buffer;
  int status;
  event_entry_tag event_tag;
} console_buffer;

typedef struct _console_device {
  unsigned_word my_base_address;
  int interrupt_delay;
  console_buffer input;
  console_buffer output;
} console_device;

typedef enum {
  console_read_buffer = 0,
  console_read_status = 4,
  console_write_buffer = 8,
  console_write_status = 12,
  console_offset_mask = 0xc,
  console_size = 16,
} console_offsets;


STATIC_INLINE_DEVICES unsigned64
console_read_callback(device_node *device,
		      unsigned_word base,
		      unsigned nr_bytes,
		      cpu *processor,
		      unsigned_word cia)
{
  console_device *con = (console_device*)device->data;
  TRACE(trace_console_device,
	("device=0x%x, base=0x%x, nr_bytes=%d\n",
	 device, base, nr_bytes));

  /* handle the request */

  switch (base & console_offset_mask) {

  case console_read_buffer:
    return con->input.buffer;

  case console_read_status:
    { /* check for input */
      int flags;
      int status;
      /* get the old status */
      flags = fcntl(0, F_GETFL, 0);
      if (flags == -1) {
	perror("console");
	return 0;
      }
      /* temp, disable blocking IO */
      status = fcntl(0, F_SETFL, flags | O_NDELAY);
      if (status == -1) {
	perror("console");
	return 0;
      }
      /* try for input */
      status = read(0, &con->input.buffer, 1);
      if (status == 1) {
	con->input.status = 1;
      }
      else {
	con->input.status = 0;
      }
      /* return to regular vewing */
      fcntl(0, F_SETFL, flags);
    }
    return con->input.status;

  case console_write_buffer:
    return con->output.buffer;

  case console_write_status:
    return con->output.status;

  default:
    error("console_read_callback() internal error\n");
    return 0;

  }

}

STATIC_INLINE_DEVICES void
console_write_callback(device_node *device,
		       unsigned_word base,
		       unsigned nr_bytes,
		       unsigned64 val,
		       cpu *processor,
		       unsigned_word cia)
{
  console_device *con = (console_device*)device->data;

  TRACE(trace_console_device,
	("device=0x%x, base=0x%x, nr_bytes=%d, val=0x%x\n",
	 device, base, nr_bytes, val));

  /* check for bus error */
  if (base & 0x3) {
    error("%s - misaligned base address, base=0x%x, nr_bytes=%d\n",
	  "console_write_callback", base, nr_bytes);
  }

  switch (base & console_offset_mask) {
  case console_read_buffer: con->input.buffer = val; break;
  case console_read_status: con->input.status = val; break;
  case console_write_buffer:
    TRACE(trace_console_device,
	  ("<%c:%d>", val, val));
    printf_filtered("%c", val);
    con->output.buffer = val;
    con->output.status = 1;
    break;
  case console_write_status:
    con->output.status = val;
    break;
  }

}

static device_callbacks console_callbacks = {
  console_read_callback,
  console_write_callback,
};

STATIC_INLINE_DEVICES device_node *
console_create(device_node *parent,
	       char *name)
{
  device_node *device;
  unsigned address_base;
  unsigned address_flags;

  /* create the descriptor */
  console_device *console = ZALLOC(console_device);

  /* extract the two arguments */
  parse_device_address(name, &address_base, &address_flags);

  /* fill in the details */
  console->my_base_address = address_base;
  console->interrupt_delay = address_flags;
  console->output.status = 1;
  console->output.buffer = '\0';
  console->input.status = 0;
  console->input.buffer = '\0';

  /* insert into the device tree along with its address info */
  device = device_node_create(parent, name, sequential_device,
			      &console_callbacks, console);
  device_node_add_address(device,
			  address_base,
			  console_size,
			  device_is_read_write_exec,
			  NULL);

  return device;
}

static device_descriptor console_descriptor = {
  "console",
  console_create,
};


/* ICU device:

   Single 4 byte register.  Read returns processor number.  Write
   interrupts specified processor.

   Illustrates passing of events to parent device. Passing of
   interrupts to parent bus.

   NB: For the sake of illustrating the passing of interrupts.  This
   device doesn't pass interrupt events to its parent.  Instead it
   passes them back to its self. */

STATIC_INLINE_DEVICES unsigned64
icu_read_callback(device_node *device,
		  unsigned_word base,
		  unsigned nr_bytes,
		  cpu *processor,
		  unsigned_word cia)
{
  TRACE(trace_icu_device,
	("device=0x%x, base=0x%x, nr_bytes=%d\n",
	 device, base, nr_bytes));
  return cpu_nr(processor);
}

STATIC_INLINE_DEVICES void
icu_write_callback(device_node *device,
		   unsigned_word base,
		   unsigned nr_bytes,
		   unsigned64 val,
		   cpu *processor,
		   unsigned_word cia)
{
  psim *system = cpu_system(processor);
  device_node *parent = device; /* NB: normally would be device->parent */
  TRACE(trace_icu_device,
	("device=0x%x, base=0x%x, nr_bytes=%d, val=0x%x\n",
	 device, base, nr_bytes, val));
  /* tell the parent device that the interrupt lines have changed.
     For this fake ICU.  The interrupt lines just indicate the cpu to
     interrupt next */
  parent->callbacks->interrupt_callback(parent, val, device, processor, cia);
}

STATIC_INLINE_DEVICES void
icu_do_interrupt(event_queue *queue,
		 void *data)
{
  cpu *target = (cpu*)data;
  /* try to interrupt the processor.  If the attempt fails, try again
     on the next tick */
  if (!external_interrupt(target))
    event_queue_schedule(queue, 1, icu_do_interrupt, target);
}

STATIC_INLINE_DEVICES void
icu_interrupt_callback(device_node *me,
		       int interrupt_status,
		       device_node *device,
		       cpu *processor,
		       unsigned_word cia)
{
  /* the interrupt controler can't interrupt a cpu at any time.
     Rather it must synchronize with the system clock before
     performing an interrupt on the given processor */
  psim *system = cpu_system(processor);
  cpu *target = psim_cpu(system, interrupt_status);
  if (target != NULL) {
    event_queue *events = cpu_event_queue(target);
    event_queue_schedule(events, 1, icu_do_interrupt, target);
  }
}

static device_callbacks icu_callbacks = {
  icu_read_callback,
  icu_write_callback,
  icu_interrupt_callback,
};

STATIC_INLINE_DEVICES device_node *
icu_create(device_node *parent,
	       char *name)
{
  device_node *device;
  unsigned address_base;
  unsigned address_flags;

  /* extract the two arguments */
  parse_device_address(name, &address_base, &address_flags);

  /* insert into the device tree along with its address info */
  device = device_node_create(parent, name, sequential_device,
			      &icu_callbacks, 0);
  device_node_add_address(device,
			  address_base,
			  4,
			  device_is_read_write_exec,
			  NULL);

  return device;
}

static device_descriptor icu_descriptor = {
  "icu",
  icu_create,
};





/* HALT device:

   With real hardware, the processor operation is normally terminated
   through a reset.  This device illustrates how a reset device could
   be attached to an address */

STATIC_INLINE_DEVICES unsigned64
halt_read_callback(device_node *device,
		   unsigned_word base,
		   unsigned nr_bytes,
		   cpu *processor,
		   unsigned_word cia)
{
  cpu_halt(processor, cia, was_exited, 0);
  return 0;
}

STATIC_INLINE_DEVICES void
halt_write_callback(device_node *device,
		    unsigned_word base,
		    unsigned nr_bytes,
		    unsigned64 val,
		    cpu *processor,
		    unsigned_word cia)
{
  cpu_halt(processor, cia, was_exited, 0);
}


static device_callbacks halt_callbacks = {
  halt_read_callback,
  halt_write_callback,
};

STATIC_INLINE_DEVICES device_node *
halt_create(device_node *parent,
	    char *name)
{
  device_node *device;
  unsigned address_base;
  unsigned address_flags;

  parse_device_address(name, &address_base, &address_flags);
  device = device_node_create(parent, name, other_device,
			      &halt_callbacks, NULL);
  device_node_add_address(device,
			  address_base,
			  4,
			  device_is_read_write_exec,
			  NULL);
  return device;
}

static device_descriptor halt_descriptor = {
  "halt",
  halt_create,
};


static device_descriptor *devices[] = {
  &console_descriptor,
  &halt_descriptor,
  &icu_descriptor,
  NULL,
};


INLINE_DEVICES device_descriptor *
find_device_descriptor(char *name)
{
  device_descriptor **device;
  int name_len;
  char *chp;
  chp = strchr(name, '@');
  name_len = (chp == NULL ? strlen(name) : chp - name);
  for (device = devices; *device != NULL; device++) {
    if (strncmp(name, (*device)->name, name_len) == 0
	&& ((*device)->name[name_len] == '\0'
	    || (*device)->name[name_len] == '@'))
      return *device;
  }
  return NULL;
}

#endif /* _DEVICES_C_ */