2 * ColdFire Interrupt Controller emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
10 #include "exec-memory.h"
23 static void mcf_intc_update(mcf_intc_state
*s
)
30 active
= (s
->ipr
| s
->ifr
) & s
->enabled
& ~s
->imr
;
34 for (i
= 0; i
< 64; i
++) {
35 if ((active
& 1) != 0 && s
->icr
[i
] >= best_level
) {
36 best_level
= s
->icr
[i
];
42 s
->active_vector
= ((best
== 64) ? 24 : (best
+ 64));
43 m68k_set_irq_level(s
->env
, best_level
, s
->active_vector
);
46 static uint64_t mcf_intc_read(void *opaque
, target_phys_addr_t addr
,
50 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
52 if (offset
>= 0x40 && offset
< 0x80) {
53 return s
->icr
[offset
- 0x40];
57 return (uint32_t)(s
->ipr
>> 32);
59 return (uint32_t)s
->ipr
;
61 return (uint32_t)(s
->imr
>> 32);
63 return (uint32_t)s
->imr
;
65 return (uint32_t)(s
->ifr
>> 32);
67 return (uint32_t)s
->ifr
;
68 case 0xe0: /* SWIACK. */
69 return s
->active_vector
;
70 case 0xe1: case 0xe2: case 0xe3: case 0xe4:
71 case 0xe5: case 0xe6: case 0xe7:
73 hw_error("mcf_intc_read: LnIACK not implemented\n");
79 static void mcf_intc_write(void *opaque
, target_phys_addr_t addr
,
80 uint64_t val
, unsigned size
)
83 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
85 if (offset
>= 0x40 && offset
< 0x80) {
86 int n
= offset
- 0x40;
89 s
->enabled
&= ~(1ull << n
);
91 s
->enabled
|= (1ull << n
);
97 /* Ignore IPR writes. */
100 s
->imr
= (s
->imr
& 0xffffffff) | ((uint64_t)val
<< 32);
103 s
->imr
= (s
->imr
& 0xffffffff00000000ull
) | (uint32_t)val
;
106 hw_error("mcf_intc_write: Bad write offset %d\n", offset
);
112 static void mcf_intc_set_irq(void *opaque
, int irq
, int level
)
114 mcf_intc_state
*s
= (mcf_intc_state
*)opaque
;
118 s
->ipr
|= 1ull << irq
;
120 s
->ipr
&= ~(1ull << irq
);
124 static void mcf_intc_reset(mcf_intc_state
*s
)
130 memset(s
->icr
, 0, 64);
131 s
->active_vector
= 24;
134 static const MemoryRegionOps mcf_intc_ops
= {
135 .read
= mcf_intc_read
,
136 .write
= mcf_intc_write
,
137 .endianness
= DEVICE_NATIVE_ENDIAN
,
140 qemu_irq
*mcf_intc_init(MemoryRegion
*sysmem
,
141 target_phys_addr_t base
,
146 s
= g_malloc0(sizeof(mcf_intc_state
));
150 memory_region_init_io(&s
->iomem
, &mcf_intc_ops
, s
, "mcf", 0x100);
151 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);
153 return qemu_allocate_irqs(mcf_intc_set_irq
, s
, 64);