Statistics
| Branch: | Revision:

root / hw / cs4231.c @ 3e3f6754

History | View | Annotate | Download (4.6 kB)

1 b8174937 bellard
/*
2 b8174937 bellard
 * QEMU Crystal CS4231 audio chip emulation
3 b8174937 bellard
 *
4 b8174937 bellard
 * Copyright (c) 2006 Fabrice Bellard
5 b8174937 bellard
 *
6 b8174937 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 b8174937 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 b8174937 bellard
 * in the Software without restriction, including without limitation the rights
9 b8174937 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 b8174937 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 b8174937 bellard
 * furnished to do so, subject to the following conditions:
12 b8174937 bellard
 *
13 b8174937 bellard
 * The above copyright notice and this permission notice shall be included in
14 b8174937 bellard
 * all copies or substantial portions of the Software.
15 b8174937 bellard
 *
16 b8174937 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 b8174937 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 b8174937 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 b8174937 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 b8174937 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 b8174937 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 b8174937 bellard
 * THE SOFTWARE.
23 b8174937 bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "sun4m.h"
26 b8174937 bellard
27 b8174937 bellard
/* debug CS4231 */
28 b8174937 bellard
//#define DEBUG_CS
29 b8174937 bellard
30 b8174937 bellard
/*
31 b8174937 bellard
 * In addition to Crystal CS4231 there is a DMA controller on Sparc.
32 b8174937 bellard
 */
33 b8174937 bellard
#define CS_MAXADDR 0x3f
34 5aca8c3b blueswir1
#define CS_SIZE (CS_MAXADDR + 1)
35 b8174937 bellard
#define CS_REGS 16
36 b8174937 bellard
#define CS_DREGS 32
37 b8174937 bellard
#define CS_MAXDREG (CS_DREGS - 1)
38 b8174937 bellard
39 b8174937 bellard
typedef struct CSState {
40 b8174937 bellard
    uint32_t regs[CS_REGS];
41 b8174937 bellard
    uint8_t dregs[CS_DREGS];
42 b8174937 bellard
    void *intctl;
43 b8174937 bellard
} CSState;
44 b8174937 bellard
45 b8174937 bellard
#define CS_RAP(s) ((s)->regs[0] & CS_MAXDREG)
46 b8174937 bellard
#define CS_VER 0xa0
47 b8174937 bellard
#define CS_CDC_VER 0x8a
48 b8174937 bellard
49 b8174937 bellard
#ifdef DEBUG_CS
50 b8174937 bellard
#define DPRINTF(fmt, args...)                           \
51 b8174937 bellard
    do { printf("CS: " fmt , ##args); } while (0)
52 b8174937 bellard
#else
53 b8174937 bellard
#define DPRINTF(fmt, args...)
54 b8174937 bellard
#endif
55 b8174937 bellard
56 b8174937 bellard
static void cs_reset(void *opaque)
57 b8174937 bellard
{
58 b8174937 bellard
    CSState *s = opaque;
59 b8174937 bellard
60 b8174937 bellard
    memset(s->regs, 0, CS_REGS * 4);
61 b8174937 bellard
    memset(s->dregs, 0, CS_DREGS);
62 b8174937 bellard
    s->dregs[12] = CS_CDC_VER;
63 b8174937 bellard
    s->dregs[25] = CS_VER;
64 b8174937 bellard
}
65 b8174937 bellard
66 b8174937 bellard
static uint32_t cs_mem_readl(void *opaque, target_phys_addr_t addr)
67 b8174937 bellard
{
68 b8174937 bellard
    CSState *s = opaque;
69 b8174937 bellard
    uint32_t saddr, ret;
70 b8174937 bellard
71 b8174937 bellard
    saddr = (addr & CS_MAXADDR) >> 2;
72 b8174937 bellard
    switch (saddr) {
73 b8174937 bellard
    case 1:
74 b8174937 bellard
        switch (CS_RAP(s)) {
75 b8174937 bellard
        case 3: // Write only
76 b8174937 bellard
            ret = 0;
77 b8174937 bellard
            break;
78 b8174937 bellard
        default:
79 b8174937 bellard
            ret = s->dregs[CS_RAP(s)];
80 b8174937 bellard
            break;
81 b8174937 bellard
        }
82 b8174937 bellard
        DPRINTF("read dreg[%d]: 0x%8.8x\n", CS_RAP(s), ret);
83 f930d07e blueswir1
        break;
84 b8174937 bellard
    default:
85 b8174937 bellard
        ret = s->regs[saddr];
86 b8174937 bellard
        DPRINTF("read reg[%d]: 0x%8.8x\n", saddr, ret);
87 f930d07e blueswir1
        break;
88 b8174937 bellard
    }
89 b8174937 bellard
    return ret;
90 b8174937 bellard
}
91 b8174937 bellard
92 b8174937 bellard
static void cs_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
93 b8174937 bellard
{
94 b8174937 bellard
    CSState *s = opaque;
95 b8174937 bellard
    uint32_t saddr;
96 b8174937 bellard
97 b8174937 bellard
    saddr = (addr & CS_MAXADDR) >> 2;
98 b8174937 bellard
    DPRINTF("write reg[%d]: 0x%8.8x -> 0x%8.8x\n", saddr, s->regs[saddr], val);
99 b8174937 bellard
    switch (saddr) {
100 b8174937 bellard
    case 1:
101 b8174937 bellard
        DPRINTF("write dreg[%d]: 0x%2.2x -> 0x%2.2x\n", CS_RAP(s), s->dregs[CS_RAP(s)], val);
102 b8174937 bellard
        switch(CS_RAP(s)) {
103 b8174937 bellard
        case 11:
104 b8174937 bellard
        case 25: // Read only
105 b8174937 bellard
            break;
106 b8174937 bellard
        case 12:
107 b8174937 bellard
            val &= 0x40;
108 b8174937 bellard
            val |= CS_CDC_VER; // Codec version
109 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
110 b8174937 bellard
            break;
111 b8174937 bellard
        default:
112 b8174937 bellard
            s->dregs[CS_RAP(s)] = val;
113 b8174937 bellard
            break;
114 b8174937 bellard
        }
115 b8174937 bellard
        break;
116 b8174937 bellard
    case 2: // Read only
117 b8174937 bellard
        break;
118 b8174937 bellard
    case 4:
119 b8174937 bellard
        if (val & 1)
120 b8174937 bellard
            cs_reset(s);
121 b8174937 bellard
        val &= 0x7f;
122 b8174937 bellard
        s->regs[saddr] = val;
123 b8174937 bellard
        break;
124 b8174937 bellard
    default:
125 b8174937 bellard
        s->regs[saddr] = val;
126 f930d07e blueswir1
        break;
127 b8174937 bellard
    }
128 b8174937 bellard
}
129 b8174937 bellard
130 b8174937 bellard
static CPUReadMemoryFunc *cs_mem_read[3] = {
131 b8174937 bellard
    cs_mem_readl,
132 b8174937 bellard
    cs_mem_readl,
133 b8174937 bellard
    cs_mem_readl,
134 b8174937 bellard
};
135 b8174937 bellard
136 b8174937 bellard
static CPUWriteMemoryFunc *cs_mem_write[3] = {
137 b8174937 bellard
    cs_mem_writel,
138 b8174937 bellard
    cs_mem_writel,
139 b8174937 bellard
    cs_mem_writel,
140 b8174937 bellard
};
141 b8174937 bellard
142 b8174937 bellard
static void cs_save(QEMUFile *f, void *opaque)
143 b8174937 bellard
{
144 b8174937 bellard
    CSState *s = opaque;
145 b8174937 bellard
    unsigned int i;
146 b8174937 bellard
147 b8174937 bellard
    for (i = 0; i < CS_REGS; i++)
148 b8174937 bellard
        qemu_put_be32s(f, &s->regs[i]);
149 b8174937 bellard
150 b8174937 bellard
    qemu_put_buffer(f, s->dregs, CS_DREGS);
151 b8174937 bellard
}
152 b8174937 bellard
153 b8174937 bellard
static int cs_load(QEMUFile *f, void *opaque, int version_id)
154 b8174937 bellard
{
155 b8174937 bellard
    CSState *s = opaque;
156 b8174937 bellard
    unsigned int i;
157 b8174937 bellard
158 b8174937 bellard
    if (version_id > 1)
159 b8174937 bellard
        return -EINVAL;
160 b8174937 bellard
161 b8174937 bellard
    for (i = 0; i < CS_REGS; i++)
162 b8174937 bellard
        qemu_get_be32s(f, &s->regs[i]);
163 b8174937 bellard
164 b8174937 bellard
    qemu_get_buffer(f, s->dregs, CS_DREGS);
165 b8174937 bellard
    return 0;
166 b8174937 bellard
}
167 b8174937 bellard
168 b8174937 bellard
void cs_init(target_phys_addr_t base, int irq, void *intctl)
169 b8174937 bellard
{
170 b8174937 bellard
    int cs_io_memory;
171 b8174937 bellard
    CSState *s;
172 b8174937 bellard
173 b8174937 bellard
    s = qemu_mallocz(sizeof(CSState));
174 b8174937 bellard
    if (!s)
175 b8174937 bellard
        return;
176 b8174937 bellard
177 b8174937 bellard
    cs_io_memory = cpu_register_io_memory(0, cs_mem_read, cs_mem_write, s);
178 5aca8c3b blueswir1
    cpu_register_physical_memory(base, CS_SIZE, cs_io_memory);
179 b8174937 bellard
    register_savevm("cs4231", base, 1, cs_save, cs_load, s);
180 b8174937 bellard
    qemu_register_reset(cs_reset, s);
181 b8174937 bellard
    cs_reset(s);
182 b8174937 bellard
}