/[gxemul]/trunk/src/devices/dev_8259.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 /trunk/src/devices/dev_8259.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations)
Mon Oct 8 16:19:11 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 7587 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1004 2005/10/27 14:01:10 debug Exp $
20051011        Passing -A as the default boot arg for CATS (works fine with
                OpenBSD/cats).
20051012	Fixing the VGA cursor offset bug, and speeding up framebuffer
		redraws if character cells contain the same thing as during
		the last redraw.
20051013	Adding a slow strd ARM instruction hack.
20051017	Minor updates: Adding a dummy i80321 Verde controller (for
		XScale emulation), fixing the disassembly of the ARM "ldrd"
		instruction, adding "support" for less-than-4KB pages for ARM
		(by not adding them to translation tables).
20051020	Continuing on some HPCarm stuff. A NetBSD/hpcarm kernel prints
		some boot messages on an emulated Jornada 720.
		Making dev_ram work better with dyntrans (speeds up some things
		quite a bit).
20051021	Automatically generating some of the most common ARM load/store
		multiple instructions.
20051022	Better statistics gathering for the ARM load/store multiple.
		Various other dyntrans and device updates.
20051023	Various minor updates.
20051024	Continuing; minor device and dyntrans fine-tuning. Adding the
		first "reasonable" instruction combination hacks for ARM (the
		cores of NetBSD/cats' memset and memcpy).
20051025	Fixing a dyntrans-related bug in dev_vga. Also changing the
		dyntrans low/high access notification to only be updated on
		writes, not reads. Hopefully it will be enough. (dev_vga in
		charcell mode now seems to work correctly with both reads and
		writes.)
		Experimenting with gathering dyntrans statistics (which parts
		of emulated RAM that are actually executed), and adding
		instruction combination hacks for cache cleaning and a part of
		NetBSD's scanc() function.
20051026	Adding a bitmap for ARM emulation which indicates if a page is
		(specifically) user accessible; loads and stores with the t-
		flag set can now use the translation arrays, which results in
		a measurable speedup.
20051027	Dyntrans updates; adding an extra bitmap array for 32-bit
		emulation modes, speeding up the check whether a physical page
		has any code translations or not (O(n) -> O(1)). Doing a
		similar reduction of O(n) to O(1) by avoiding the scan through
		the translation entries on a translation update (32-bit mode
		only).
		Various other minor hacks.
20051029	Quick release, without any testing at all.

==============  RELEASE 0.3.6.2  ==============


1 /*
2 * Copyright (C) 2005 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_8259.c,v 1.15 2005/10/26 14:37:03 debug Exp $
29 *
30 * 8259 Programmable Interrupt Controller.
31 *
32 * See the following URL for more details:
33 * http://www.nondot.org/sabre/os/files/MiscHW/8259pic.txt
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "cpu.h"
41 #include "device.h"
42 #include "devices.h"
43 #include "emul.h"
44 #include "machine.h"
45 #include "memory.h"
46 #include "misc.h"
47
48
49 #define DEV_8259_LENGTH 2
50
51 /* #define DEV_8259_DEBUG */
52
53
54 /*
55 * dev_8259_access():
56 */
57 int dev_8259_access(struct cpu *cpu, struct memory *mem,
58 uint64_t relative_addr, unsigned char *data, size_t len,
59 int writeflag, void *extra)
60 {
61 struct pic8259_data *d = (struct pic8259_data *) extra;
62 uint64_t idata = 0, odata = 0;
63 int i;
64
65 if (writeflag == MEM_WRITE)
66 idata = memory_readmax64(cpu, data, len);
67
68 #ifdef DEV_8259_DEBUG
69 if (writeflag == MEM_READ)
70 fatal("[ 8259: read from 0x%x ]\n", (int)relative_addr);
71 else
72 fatal("[ 8259: write to 0x%x: 0x%x ]\n",
73 (int)relative_addr, (int)idata);
74 #endif
75
76 switch (relative_addr) {
77 case 0x00:
78 if (writeflag == MEM_WRITE) {
79 if ((idata & 0x10) == 0x10) {
80 /* Bit 3: 0=edge, 1=level */
81 if (idata & 0x08)
82 fatal("[ 8259: TODO: Level "
83 "triggered (MCA bus) ]\n");
84 if (idata & 0x04)
85 fatal("[ 8259: WARNING: Bit 2 set ]\n");
86 /* Bit 1: 0=cascade, 1=single */
87 /* Bit 0: 1=4th init byte */
88 /* This happens on non-x86 systems:
89 if (!(idata & 0x01))
90 fatal("[ 8259: WARNING: Bit 0 NOT set!"
91 "!! ]\n"); */
92 d->init_state = 1;
93 break;
94 }
95
96 /* TODO: Is it ok to abort init state when there
97 is a non-init command? */
98 if (d->init_state)
99 fatal("[ 8259: WARNING: Was in init-state, but"
100 " it was aborted? ]\n");
101 d->init_state = 0;
102
103 switch (idata) {
104 case 0x0a:
105 d->current_command = 0x0a;
106 break;
107 case 0x0b:
108 d->current_command = 0x0b;
109 break;
110 case 0x0c:
111 /* Put Master in Buffered Mode */
112 d->current_command = 0x0c;
113 break;
114 case 0x20: /* End Of Interrupt */
115 /*
116 * TODO: in buffered mode, is this an EOI 0?
117 */
118 d->irr &= ~d->isr;
119 d->isr = 0;
120 /* Recalculate interrupt assertions: */
121 cpu_interrupt(cpu, d->irq_nr);
122 break;
123 case 0x21: /* Specific EOI */
124 case 0x22:
125 case 0x23:
126 case 0x24:
127 case 0x25:
128 case 0x26:
129 case 0x27:
130 case 0x60:
131 case 0x61:
132 case 0x62:
133 case 0x63:
134 case 0x64:
135 case 0x65:
136 case 0x66:
137 case 0x67: /* Specific EOI */
138 d->irr &= ~(1 << (idata & 7));
139 d->isr &= ~(1 << (idata & 7));
140 /* Recalculate interrupt assertions: */
141 cpu_interrupt(cpu, d->irq_nr);
142 break;
143 case 0x68: /* Set Special Mask Mode */
144 /* TODO */
145 break;
146 case 0xc0:
147 case 0xc1:
148 case 0xc2:
149 case 0xc3:
150 case 0xc4:
151 case 0xc5:
152 case 0xc6:
153 case 0xc7: /* Set IRQ Priority Order */
154 /* TODO */
155 break;
156 default:
157 fatal("[ 8259: unimplemented command 0x%02x"
158 " ]\n", idata);
159 cpu->running = 0;
160 }
161 } else {
162 switch (d->current_command) {
163 case 0x0a:
164 odata = d->irr;
165 break;
166 case 0x0b:
167 odata = d->isr;
168 break;
169 case 0x0c:
170 /* Buffered mode. */
171 default:
172 odata = 0x00;
173 for (i=0; i<8; i++)
174 if ((d->irr >> i) & 1) {
175 odata = 0x80 | i;
176 break;
177 }
178 break;
179 /*
180 * TODO: The "default" label should really do
181 * something like this:
182 *
183 * fatal("[ 8259: unimplemented command 0x%02x"
184 * " while reading ]\n", d->current_command);
185 * cpu->running = 0;
186 *
187 * but Linux seems to read from the secondary PIC
188 * in a manner which works better the way things
189 * are coded right now.
190 */
191 }
192 }
193 break;
194 case 0x01:
195 if (d->init_state > 0) {
196 if (d->init_state == 1) {
197 d->irq_base = idata & 0xf8;
198 /* This happens on non-x86 machines:
199 if (idata & 7)
200 fatal("[ 8259: WARNING! Lowest"
201 " bits in Init Cmd 1 are"
202 " non-zero! ]\n"); */
203 d->init_state = 2;
204 } else if (d->init_state == 2) {
205 /* Slave attachment. TODO */
206 d->init_state = 3;
207 } else if (d->init_state == 3) {
208 if (idata & 0x02)
209 fatal("[ 8259: WARNING! Bit 1 i"
210 "n Init Cmd 4 is set! ]\n");
211 if (!(idata & 0x01))
212 fatal("[ 8259: WARNING! Bit 0 "
213 "in Init Cmd 4 is not"
214 " set! ]\n");
215 d->init_state = 0;
216 }
217 break;
218 }
219
220 if (writeflag == MEM_WRITE) {
221 d->ier = idata;
222 /* Recalculate interrupt assertions: */
223 cpu_interrupt(cpu, d->irq_nr);
224 } else {
225 odata = d->ier;
226 }
227 break;
228 default:
229 if (writeflag == MEM_WRITE) {
230 fatal("[ 8259: unimplemented write to address 0x%x"
231 " data=0x%02x ]\n", (int)relative_addr, (int)idata);
232 cpu->running = 0;
233 } else {
234 fatal("[ 8259: unimplemented read from address 0x%x "
235 "]\n", (int)relative_addr);
236 cpu->running = 0;
237 }
238 }
239
240 if (writeflag == MEM_READ)
241 memory_writemax64(cpu, data, len, odata);
242
243 return 1;
244 }
245
246
247 /*
248 * devinit_8259():
249 *
250 * Initialize an 8259 PIC. Important notes:
251 *
252 * x) Most systems use _TWO_ 8259 PICs. These should be registered
253 * as separate devices.
254 *
255 * x) The irq number specified is the number used to re-calculate
256 * CPU interrupt assertions. It is _not_ the irq number at
257 * which the PIC is connected. (That is left to machine specific
258 * code in src/machine.c.)
259 */
260 int devinit_8259(struct devinit *devinit)
261 {
262 struct pic8259_data *d = malloc(sizeof(struct pic8259_data));
263 char *name2;
264 size_t nlen = strlen(devinit->name) + 20;
265
266 if (d == NULL) {
267 fprintf(stderr, "out of memory\n");
268 exit(1);
269 }
270 memset(d, 0, sizeof(struct pic8259_data));
271 d->irq_nr = devinit->irq_nr;
272
273 name2 = malloc(nlen);
274 snprintf(name2, nlen, "%s", devinit->name);
275 if ((devinit->addr & 0xfff) == 0xa0) {
276 strlcat(name2, " [secondary]", nlen);
277 d->irq_base = 8;
278 }
279
280 memory_device_register(devinit->machine->memory, name2,
281 devinit->addr, DEV_8259_LENGTH, dev_8259_access, d,
282 MEM_DEFAULT, NULL);
283
284 devinit->return_ptr = d;
285 return 1;
286 }
287

  ViewVC Help
Powered by ViewVC 1.1.26