1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// cia.c
//
// 20060803 Markku Alén
#include "cia.h"
#define TA_BIT 0
#define TA_MASK (1<<TA_BIT)
#define TB_BIT 1
#define TB_MASK (1<<TB_BIT)
#define ALRM_BIT 2
#define ALRM_MASK (1<<ALRM_BIT)
#define SP_BIT 3
#define SP_MASK (1<<SP_BIT)
#define FLAG_BIT 4
#define FLAG_MASK (1<<FLAG_BIT)
#define IR_BIT 7
#define IR_MASK (7<<IR_BIT)
#define SC_BIT 7
#define SC_MASK (7<<SC_BIT)
#define START_BIT 0
#define START_MASK (1<<START_BIT)
#define PBON_BIT 1
#define PBON_MASK (1<<PBON_BIT)
#define OUTMODE_BIT 2
#define OUTMODE_MASK (1<<OUTMODE_BIT)
#define RUNMODE_BIT 3
#define RUNMODE_MASK (1<<RUNMODE_BIT)
#define LOAD_BIT 4
#define LOAD_MASK (1<<LOAD_BIT)
#define SPMODE_BIT 6
#define SPMODE_MASK (1<<SPMODE_BIT)
#define TODIN_BIT 7
#define TODIN_MASK (1<<TODMODE_BIT)
#define INMODEA_SHIFT 5
#define INMODEA_MASK (0x1<<INMODEA_SHIFT)
#define TA_CLK (0<<INMODEA_SHIFT)
#define TA_CNT (1<<INMODEA_SHIFT)
#define INMODEB_SHIFT 5
#define INMODEB_MASK (0x3<<INMODEB_SHIFT)
#define TB_CLK (0<<INMODEB_SHIFT)
#define TB_CNT (1<<INMODEB_SHIFT)
#define TB_TMRA (2<<INMODEB_SHIFT)
#define TB_TMRA_CNT (3<<INMODEB_SHIFT)
#define ALARM_BIT 7
#define ALARM_MASK (1<<ALARM_BIT)
static unsigned char regs[16];
static void cia_interrupt_init(cia_interrupt_t *cia_interrupt)
{
cia_interrupt->mask = 0;
cia_interrupt->request = 0;
}
static void cia_interrupt_raise(cia_interrupt_t *cia_interrupt)
{
cia_interrupt->request = 1;
}
static int cia_interrupt_get_request(cia_interrupt_t *cia_interrupt)
{
int request;
request = cia_interrupt->request;
cia_interrupt->request = 0;
return request;
}
static void cia_port_init(cia_port_t *cia_port)
{
cia_port->input = 0x00;
cia_port->output = 0x00;
cia_port->direction = 0x00;
UPDATE_CIA_PORT_PINS(cia_port);
}
static void cia_port_set_data(cia_port_t *cia_port, int data)
{
cia_port->output = data;
UPDATE_CIA_PORT_PINS(cia_port);
}
static void cia_port_set_direction(cia_port_t *cia_port, int direction)
{
cia_port->direction = direction;
UPDATE_CIA_PORT_PINS(cia_port);
}
static void cia_timer_init(cia_timer_t *cia_timer)
{
cia_timer->latch = 0x0000;
cia_timer->counter = 0x0000;
cia_timer->running = 0;
cia_timer->toggle_mode = 0;
cia_timer->one_shot = 0;
cia_interrupt_init(&cia_timer->interrupt);
}
static int cia_timer_exec(cia_timer_t *cia_timer, unsigned int cycles)
{
if(!cia_timer->running)
return 0;
cia_timer->counter -= cycles;
if(cia_timer->counter >= 0)
return 0;
cia_timer->counter += cia_timer->latch;
cia_interrupt_raise(&cia_timer->interrupt);
if(cia_timer->one_shot)
cia_timer->running = 0;
return 1;
}
void cia_init(cia_t *cia)
{
cia_port_init(&cia->port_a);
cia_port_init(&cia->port_b);
cia_timer_init(&cia->timer_a);
cia_timer_init(&cia->timer_b);
cia->inmode_a = 0;
cia->inmode_b = 0;
cia->irq_pin = 0;
}
int cia_read(cia_t *cia, int address)
{
switch(address & 0x000f)
{
case 0x0000:
return cia->port_a.pins;
case 0x0001:
return cia->port_b.pins;
case 0x0002:
return cia->port_a.direction;
case 0x0003:
return cia->port_b.direction;
case 0x0004:
return cia->timer_a.counter % 256;
case 0x0005:
return cia->timer_a.counter / 256;
case 0x0006:
return cia->timer_b.counter % 256;
case 0x0007:
return cia->timer_b.counter / 256;
case 0x000d:
return 0
| (cia_interrupt_get_request(&cia->timer_a.interrupt) != 0 ? TA_MASK : 0)
| (cia_interrupt_get_request(&cia->timer_b.interrupt) != 0 ? TB_MASK : 0)
| (cia->irq_pin != 0 ? IR_MASK : 0)
;
case 0x000e:
return 0
| cia->timer_a.running
| cia->timer_a.toggle_mode
| cia->timer_a.one_shot
| cia->inmode_a
;
case 0x000f:
return 0
| cia->timer_b.running
| cia->timer_b.toggle_mode
| cia->timer_b.one_shot
| cia->inmode_b
;
default:
return regs[address % sizeof(regs)];
}
}
void cia_write(cia_t *cia, int address, int data)
{
switch(address & 0x000f)
{
case 0x0000:
cia_port_set_data(&cia->port_a, data);
break;
case 0x0001:
cia_port_set_data(&cia->port_b, data);
break;
case 0x0002:
cia_port_set_direction(&cia->port_a, data);
break;
case 0x0003:
cia_port_set_direction(&cia->port_b, data);
break;
case 0x0004:
cia->timer_a.latch = (cia->timer_a.latch & 0xff00) | (data & 0xff);
break;
case 0x0005:
cia->timer_a.latch = (cia->timer_a.latch & 0x00ff) | ((data & 0xff) << 8);
if(!cia->timer_a.running)
cia->timer_a.counter = cia->timer_a.latch;
break;
case 0x0006:
cia->timer_b.latch = (cia->timer_b.latch & 0xff00) | (data & 0xff);
break;
case 0x0007:
cia->timer_b.latch = (cia->timer_b.latch & 0x00ff) | ((data & 0xff) << 8);
if(!cia->timer_b.running)
cia->timer_b.counter = cia->timer_b.latch;
break;
case 0x000d:
if((data & TA_MASK) != 0)
cia->timer_a.interrupt.mask = data & SC_MASK;
if((data & TB_MASK) != 0)
cia->timer_b.interrupt.mask = data & SC_MASK;
break;
case 0x000e:
cia->timer_a.running = data & START_MASK;
cia->timer_a.toggle_mode = data & OUTMODE_MASK;
cia->timer_a.one_shot = data & RUNMODE_MASK;
if((data & LOAD_MASK) != 0)
cia->timer_a.counter = cia->timer_a.latch;
cia->inmode_a = data & INMODEA_MASK;
break;
case 0x000f:
cia->timer_b.running = data & START_MASK;
cia->timer_b.toggle_mode = data & OUTMODE_MASK;
cia->timer_b.one_shot = data & RUNMODE_MASK;
if((data & LOAD_MASK) != 0)
cia->timer_b.counter = cia->timer_b.latch;
cia->inmode_b = data & INMODEB_MASK;
break;
default:
regs[address % sizeof(regs)] = data;
}
}
int cia_a_port_output(cia_t *cia)
{
return cia->port_a.pins;
}
int cia_b_port_output(cia_t *cia)
{
return cia->port_b.pins;
}
void cia_port_a_input(cia_t *cia, int data)
{
cia->port_a.input = data;
UPDATE_CIA_PORT_PINS(&cia->port_a);
}
void cia_port_b_input(cia_t *cia, int data)
{
cia->port_b.input = data;
UPDATE_CIA_PORT_PINS(&cia->port_b);
}
void cia_exec(cia_t *cia, unsigned int cycles)
{
int timer_a_underflowed;
switch(cia->inmode_a)
{
case TA_CLK:
timer_a_underflowed = cia_timer_exec(&cia->timer_a, cycles);
break;
default:
timer_a_underflowed = 0;
}
switch(cia->inmode_b)
{
case TB_CLK:
cia_timer_exec(&cia->timer_b, cycles);
break;
case TB_TMRA:
if(timer_a_underflowed)
cia_timer_exec(&cia->timer_b, 1);
break;
}
cia->irq_pin = 0
| (cia->timer_a.interrupt.mask != 0 ? cia->timer_a.interrupt.request : 0)
| (cia->timer_b.interrupt.mask != 0 ? cia->timer_b.interrupt.request : 0)
;
}
|