/[gxemul]/upstream/0.4.4.1/src/devices/dev_cons.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/0.4.4.1/src/devices/dev_cons.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 37 - (show annotations)
Mon Oct 8 16:21:43 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 4431 byte(s)
0.4.4.1
1 /*
2 * Copyright (C) 2003-2007 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: dev_cons.c,v 1.40 2007/02/03 20:14:23 debug Exp $
29 *
30 * A simple console device, useful for simple tests.
31 *
32 * This device provides memory mapped I/O for a simple console supporting
33 * putchar (writing to memory) and getchar (reading from memory), and
34 * support for halting the emulator. (This is useful for regression tests,
35 * Hello World-style test programs, and other simple experiments.)
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "console.h"
43 #include "cpu.h"
44 #include "device.h"
45 #include "machine.h"
46 #include "memory.h"
47 #include "misc.h"
48
49 #include "testmachine/dev_cons.h"
50
51 #define CONS_TICK_SHIFT 14
52
53 struct cons_data {
54 int console_handle;
55 int in_use;
56 struct interrupt irq;
57 };
58
59
60 DEVICE_TICK(cons)
61 {
62 struct cons_data *d = extra;
63
64 INTERRUPT_DEASSERT(d->irq);
65
66 if (console_charavail(d->console_handle))
67 INTERRUPT_ASSERT(d->irq);
68 }
69
70
71 DEVICE_ACCESS(cons)
72 {
73 struct cons_data *d = extra;
74 unsigned int i;
75
76 /* Exit the emulator: */
77 if (relative_addr == DEV_CONS_HALT) {
78 /* cpu->running = 0;
79 cpu->machine->exit_without_entering_debugger = 1;
80 return 1; */
81 /* TODO: this doesn't work yet. for now, let's
82 simply use exit() */
83 exit(1);
84 }
85
86 if (writeflag == MEM_WRITE) {
87 for (i=0; i<len; i++) {
88 if (data[i] != 0) {
89 if (cpu->machine->register_dump ||
90 cpu->machine->instruction_trace)
91 debug("putchar '");
92
93 console_putchar(d->console_handle, data[i]);
94
95 if (cpu->machine->register_dump ||
96 cpu->machine->instruction_trace)
97 debug("'\n");
98 fflush(stdout);
99 }
100 }
101 } else {
102 int ch = console_readchar(d->console_handle);
103 if (ch < 0)
104 ch = 0;
105 for (i=0; i<len; i++)
106 data[i] = ch;
107 }
108
109 dev_cons_tick(cpu, extra);
110
111 return 1;
112 }
113
114
115 DEVINIT(cons)
116 {
117 struct cons_data *d;
118 char *name3;
119 size_t nlen;
120
121 d = malloc(sizeof(struct cons_data));
122 if (d == NULL) {
123 fprintf(stderr, "out of memory\n");
124 exit(1);
125 }
126 memset(d, 0, sizeof(struct cons_data));
127
128 nlen = strlen(devinit->name) + 10;
129 if (devinit->name2 != NULL)
130 nlen += strlen(devinit->name2) + 10;
131 name3 = malloc(nlen);
132 if (name3 == NULL) {
133 fprintf(stderr, "out of memory in dev_cons_init()\n");
134 exit(1);
135 }
136 if (devinit->name2 != NULL && devinit->name2[0])
137 snprintf(name3, nlen, "%s [%s]", devinit->name, devinit->name2);
138 else
139 snprintf(name3, nlen, "%s", devinit->name);
140
141 INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
142
143 d->in_use = devinit->in_use;
144 d->console_handle = console_start_slave(devinit->machine, name3,
145 d->in_use);
146
147 memory_device_register(devinit->machine->memory, name3,
148 devinit->addr, DEV_CONS_LENGTH, dev_cons_access, d,
149 DM_DEFAULT, NULL);
150 machine_add_tickfunction(devinit->machine, dev_cons_tick,
151 d, CONS_TICK_SHIFT, 0.0);
152
153 /* NOTE: Ugly cast into pointer */
154 devinit->return_ptr = (void *)(size_t)d->console_handle;
155 return 1;
156 }
157

  ViewVC Help
Powered by ViewVC 1.1.26