25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_8253.c,v 1.9 2006/02/09 20:02:58 debug Exp $ |
* $Id: dev_8253.c,v 1.13 2006/07/21 16:55:41 debug Exp $ |
|
* |
|
|
* 8253/8254 Programmable Interval Timer. |
|
29 |
* |
* |
30 |
* This is mostly bogus. |
* Intel 8253/8254 Programmable Interval Timer |
31 |
|
* |
32 |
|
* TODO: The timers don't really count down. Fix this when there is a generic |
33 |
|
* clock framework; also split counter[] into reset value and current value. |
34 |
*/ |
*/ |
35 |
|
|
36 |
#include <stdio.h> |
#include <stdio.h> |
45 |
#include "memory.h" |
#include "memory.h" |
46 |
#include "misc.h" |
#include "misc.h" |
47 |
|
|
48 |
|
#include "i8253reg.h" |
49 |
|
|
50 |
|
|
51 |
|
/* #define debug fatal */ |
52 |
|
|
53 |
#define DEV_8253_LENGTH 4 |
#define DEV_8253_LENGTH 4 |
54 |
#define TICK_SHIFT 14 |
#define TICK_SHIFT 14 |
55 |
|
|
56 |
|
|
57 |
struct pit8253_data { |
struct pit8253_data { |
|
int irq_nr; |
|
58 |
int in_use; |
int in_use; |
59 |
|
|
60 |
|
int irq0_nr; |
61 |
int counter_select; |
int counter_select; |
62 |
|
uint8_t mode_byte; |
63 |
|
|
64 |
|
int mode[3]; |
65 |
|
int counter[3]; |
66 |
}; |
}; |
67 |
|
|
68 |
|
|
69 |
/* |
DEVICE_TICK(8253) |
|
* dev_8253_tick(): |
|
|
*/ |
|
|
void dev_8253_tick(struct cpu *cpu, void *extra) |
|
70 |
{ |
{ |
71 |
struct pit8253_data *d = (struct pit8253_data *) extra; |
struct pit8253_data *d = (struct pit8253_data *) extra; |
72 |
|
|
73 |
if (!d->in_use) |
if (!d->in_use) |
74 |
return; |
return; |
75 |
|
|
76 |
cpu_interrupt(cpu, d->irq_nr); |
switch (d->mode[0] & 0x0e) { |
77 |
|
case I8253_TIMER_INTTC: |
78 |
|
/* TODO: Correct frequency! */ |
79 |
|
cpu_interrupt(cpu, d->irq0_nr); |
80 |
|
break; |
81 |
|
case I8253_TIMER_RATEGEN: |
82 |
|
break; |
83 |
|
default:fatal("[ 8253: unimplemented mode 0x%x ]\n", d->mode[0] & 0x0e); |
84 |
|
exit(1); |
85 |
|
} |
86 |
} |
} |
87 |
|
|
88 |
|
|
|
/* |
|
|
* dev_8253_access(): |
|
|
*/ |
|
89 |
DEVICE_ACCESS(8253) |
DEVICE_ACCESS(8253) |
90 |
{ |
{ |
91 |
struct pit8253_data *d = (struct pit8253_data *) extra; |
struct pit8253_data *d = (struct pit8253_data *) extra; |
97 |
d->in_use = 1; |
d->in_use = 1; |
98 |
|
|
99 |
/* TODO: ack somewhere else */ |
/* TODO: ack somewhere else */ |
100 |
cpu_interrupt_ack(cpu, d->irq_nr); |
cpu_interrupt_ack(cpu, d->irq0_nr); |
101 |
|
|
102 |
switch (relative_addr) { |
switch (relative_addr) { |
103 |
case 0x00: |
|
104 |
|
case I8253_TIMER_CNTR0: |
105 |
|
case I8253_TIMER_CNTR1: |
106 |
|
case I8253_TIMER_CNTR2: |
107 |
if (writeflag == MEM_WRITE) { |
if (writeflag == MEM_WRITE) { |
108 |
/* TODO */ |
switch (d->mode_byte & 0x30) { |
109 |
|
case I8253_TIMER_LSB: |
110 |
|
case I8253_TIMER_16BIT: |
111 |
|
d->counter[relative_addr] &= 0xff00; |
112 |
|
d->counter[relative_addr] |= (idata & 0xff); |
113 |
|
break; |
114 |
|
case I8253_TIMER_MSB: |
115 |
|
d->counter[relative_addr] &= 0x00ff; |
116 |
|
d->counter[relative_addr] |= ((idata&0xff)<<8); |
117 |
|
debug("[ 8253: counter %i set to %i (%i Hz) " |
118 |
|
"]\n", relative_addr, d->counter[ |
119 |
|
relative_addr], (int)(I8253_TIMER_FREQ / |
120 |
|
(float)d->counter[relative_addr] + 0.5)); |
121 |
|
break; |
122 |
|
default:fatal("[ 8253: huh? writing to counter" |
123 |
|
" %i but neither from msb nor lsb? ]\n", |
124 |
|
relative_addr); |
125 |
|
} |
126 |
} else { |
} else { |
127 |
/* TODO */ |
switch (d->mode_byte & 0x30) { |
128 |
/* odata = 1; */ |
case I8253_TIMER_LSB: |
129 |
odata = random(); |
case I8253_TIMER_16BIT: |
130 |
|
odata = d->counter[relative_addr] & 0xff; |
131 |
|
break; |
132 |
|
case I8253_TIMER_MSB: |
133 |
|
odata = (d->counter[relative_addr] >> 8) & 0xff; |
134 |
|
break; |
135 |
|
default:fatal("[ 8253: huh? reading from counter" |
136 |
|
" %i but neither from msb nor lsb? ]\n", |
137 |
|
relative_addr); |
138 |
|
} |
139 |
} |
} |
140 |
|
|
141 |
|
/* Switch from LSB to MSB, if accessing as 16-bit word: */ |
142 |
|
if ((d->mode_byte & 0x30) == I8253_TIMER_16BIT) |
143 |
|
d->mode_byte &= ~I8253_TIMER_LSB; |
144 |
|
|
145 |
break; |
break; |
146 |
case 0x03: |
|
147 |
|
case I8253_TIMER_MODE: |
148 |
if (writeflag == MEM_WRITE) { |
if (writeflag == MEM_WRITE) { |
149 |
|
d->mode_byte = idata; |
150 |
|
|
151 |
d->counter_select = idata >> 6; |
d->counter_select = idata >> 6; |
152 |
/* TODO: other bits */ |
if (d->counter_select > 2) { |
153 |
|
debug("[ 8253: attempt to select counter 3," |
154 |
|
" which doesn't exist. ]\n"); |
155 |
|
d->counter_select = 0; |
156 |
|
} |
157 |
|
|
158 |
|
d->mode[d->counter_select] = idata & 0x0e; |
159 |
|
|
160 |
|
debug("[ 8253: select=%i mode=0x%x ", |
161 |
|
d->counter_select, d->mode[d->counter_select]); |
162 |
|
if (idata & 0x30) { |
163 |
|
switch (idata & 0x30) { |
164 |
|
case I8253_TIMER_LSB: |
165 |
|
debug("LSB "); |
166 |
|
break; |
167 |
|
case I8253_TIMER_16BIT: |
168 |
|
debug("LSB+"); |
169 |
|
case I8253_TIMER_MSB: |
170 |
|
debug("MSB "); |
171 |
|
} |
172 |
|
} |
173 |
|
debug("]\n"); |
174 |
|
|
175 |
|
if (idata & I8253_TIMER_BCD) { |
176 |
|
fatal("[ 8253: BCD not yet implemented ]\n"); |
177 |
|
exit(1); |
178 |
|
} |
179 |
} else { |
} else { |
180 |
odata = d->counter_select << 6; |
debug("[ 8253: read; can this actually happen? ]\n"); |
181 |
|
odata = d->mode_byte; |
182 |
} |
} |
183 |
break; |
break; |
184 |
default: |
|
185 |
if (writeflag == MEM_WRITE) { |
default:if (writeflag == MEM_WRITE) { |
186 |
fatal("[ 8253: unimplemented write to address 0x%x" |
fatal("[ 8253: unimplemented write to address 0x%x" |
187 |
" data=0x%02x ]\n", (int)relative_addr, (int)idata); |
" data=0x%02x ]\n", (int)relative_addr, (int)idata); |
188 |
} else { |
} else { |
189 |
fatal("[ 8253: unimplemented read from address 0x%x " |
fatal("[ 8253: unimplemented read from address 0x%x " |
190 |
"]\n", (int)relative_addr); |
"]\n", (int)relative_addr); |
191 |
} |
} |
192 |
|
exit(1); |
193 |
} |
} |
194 |
|
|
195 |
if (writeflag == MEM_READ) |
if (writeflag == MEM_READ) |
208 |
exit(1); |
exit(1); |
209 |
} |
} |
210 |
memset(d, 0, sizeof(struct pit8253_data)); |
memset(d, 0, sizeof(struct pit8253_data)); |
211 |
d->irq_nr = devinit->irq_nr; |
d->irq0_nr = devinit->irq_nr; |
212 |
d->in_use = devinit->in_use; |
d->in_use = devinit->in_use; |
213 |
|
|
214 |
|
/* Don't cause interrupt, by default. */ |
215 |
|
d->mode[0] = I8253_TIMER_RATEGEN; |
216 |
|
d->mode[1] = I8253_TIMER_RATEGEN; |
217 |
|
d->mode[2] = I8253_TIMER_RATEGEN; |
218 |
|
|
219 |
memory_device_register(devinit->machine->memory, devinit->name, |
memory_device_register(devinit->machine->memory, devinit->name, |
220 |
devinit->addr, DEV_8253_LENGTH, dev_8253_access, (void *)d, |
devinit->addr, DEV_8253_LENGTH, dev_8253_access, (void *)d, |
221 |
DM_DEFAULT, NULL); |
DM_DEFAULT, NULL); |
222 |
|
|
223 |
machine_add_tickfunction(devinit->machine, dev_8253_tick, |
machine_add_tickfunction(devinit->machine, dev_8253_tick, |
224 |
d, TICK_SHIFT); |
d, TICK_SHIFT, 0.0); |
225 |
|
|
226 |
return 1; |
return 1; |
227 |
} |
} |