-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuservia.cc
281 lines (242 loc) · 9.17 KB
/
uservia.cc
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
280
281
/****************************************************************************/
/* Beebem - (c) David Alan Gilbert 1994 */
/* ------------------------------------ */
/* This program may be distributed freely within the following restrictions:*/
/* */
/* 1) You may not charge for this program or for any part of it. */
/* 2) This copyright message must be distributed with all copies. */
/* 3) This program must be distributed complete with source code. Binary */
/* only distribution is not permitted. */
/* 4) The author offers no warrenties, or guarentees etc. - you use it at */
/* your own risk. If it messes something up or destroys your computer */
/* thats YOUR problem. */
/* 5) You may use small sections of code from this program in your own */
/* applications - but you must acknowledge its use. If you plan to use */
/* large sections then please ask the author. */
/* */
/* If you do not agree with any of the above then please do not use this */
/* program. */
/* Please report any problems to the author at beebem@treblig.org */
/****************************************************************************/
/* User VIA support file for the beeb emulator - David Alan Gilbert 11/12/94 */
/* Modified from the system via */
#include <iostream>
#include "6502core.h"
#include "uservia.h"
#include "via.h"
extern int DumpAfterEach;
/* My raw VIA state */
VIAState UserVIAState;
/*--------------------------------------------------------------------------*/
static void UpdateIFRTopBit(void) {
/* Update top bit of IFR */
if (UserVIAState.ifr&(UserVIAState.ier&0x7f))
UserVIAState.ifr|=0x80;
else
UserVIAState.ifr&=0x7f;
intStatus&=~(1<<userVia);
intStatus|=((UserVIAState.ifr & 128)?(1<<userVia):0);
}; /* UpdateIFRTopBit */
/*--------------------------------------------------------------------------*/
/* Address is in the range 0-f - with the fe60 stripped out */
void UserVIAWrite(int Address, int Value) {
/* std::cerr << "UserVIAWrite: Address=0x" << hex << Address << " Value=0x" << Value << dec << " at " << TotalCycles << "\n";
DumpRegs(); */
switch (Address) {
case 0:
UserVIAState.orb=Value & 0xff;
if ((UserVIAState.ifr & 1) && ((UserVIAState.pcr & 2)==0)) {
UserVIAState.ifr&=0xfe;
UpdateIFRTopBit();
};
break;
case 1:
UserVIAState.ora=Value & 0xff;
UserVIAState.ifr&=0xfc;
UpdateIFRTopBit();
break;
case 2:
UserVIAState.ddrb=Value & 0xff;
break;
case 3:
UserVIAState.ddra=Value & 0xff;
break;
case 4:
case 6:
/*std::cerr << "UserVia Reg4/6 Timer1 lo Counter Write val=0x " << hex << Value << dec << " at " << TotalCycles << "\n"; */
UserVIAState.timer1l&=0xff00;
UserVIAState.timer1l|=(Value & 0xff);
break;
case 5:
/*std::cerr << "UserVia Reg5 Timer1 hi Counter Write val=0x" << hex << Value << dec << " at " << TotalCycles << "\n"; */
UserVIAState.timer1l&=0xff;
UserVIAState.timer1l|=(Value & 0xff)<<8;
UserVIAState.timer1c=UserVIAState.timer1l * 2;
UserVIAState.ifr &=0xbf; /* clear timer 1 ifr */
UserVIAState.timer1hasshot=0;
/* If PB7 toggling enabled, then lower PB7 now */
if (UserVIAState.acr & 128) {
UserVIAState.orb&=0x7f;
UserVIAState.irb&=0x7f;
};
UpdateIFRTopBit();
break;
case 7:
/*std::cerr << "UserVia Reg7 Timer1 hi latch Write val=0x" << hex << Value << dec << " at " << TotalCycles << "\n"; */
UserVIAState.timer1l&=0xff;
UserVIAState.timer1l|=(Value & 0xff)<<8;
break;
case 8:
/* std::cerr << "UserVia Reg8 Timer2 lo Counter Write val=0x" << hex << Value << dec << "\n"; */
UserVIAState.timer2l&=0xff00;
UserVIAState.timer2l|=(Value & 0xff);
break;
case 9:
/* std::cerr << "UserVia Reg9 Timer2 hi Counter Write val=0x" << hex << Value << dec << "\n";
core_dumpstate(); */
UserVIAState.timer2l&=0xff;
UserVIAState.timer2l|=(Value & 0xff)<<8;
UserVIAState.timer2c=UserVIAState.timer2l * 2;
UserVIAState.ifr &=0xdf; /* clear timer 2 ifr */
UpdateIFRTopBit();
break;
case 10:
break;
case 11:
UserVIAState.acr=Value & 0xff;
break;
case 12:
UserVIAState.pcr=Value & 0xff;
break;
case 13:
UserVIAState.ifr&=~(Value & 0xff);
UpdateIFRTopBit();
break;
case 14:
/* std::cerr << "User VIA Write ier Value=" << Value << "\n"; */
if (Value & 0x80)
UserVIAState.ier|=Value & 0xff;
else
UserVIAState.ier&=~(Value & 0xff);
UserVIAState.ier&=0x7f;
UpdateIFRTopBit();
break;
case 15:
UserVIAState.ora=Value & 0xff;
break;
} /* Address switch */
} /* UserVIAWrite */
/*--------------------------------------------------------------------------*/
/* Address is in the range 0-f - with the fe60 stripped out */
int UserVIARead(int Address) {
int tmp;
/* std::cerr << "UserVIARead: Address=0x" << hex << Address << dec << " at " << TotalCycles << "\n";
DumpRegs(); */
switch (Address) {
case 0: /* IRB read */
tmp=(UserVIAState.orb & UserVIAState.ddrb) | (UserVIAState.irb & (~UserVIAState.ddrb));;
return(tmp);
case 2:
return(UserVIAState.ddrb);
case 3:
return(UserVIAState.ddra);
case 4: /* Timer 1 lo counter */
tmp=UserVIAState.timer1c / 2;
UserVIAState.ifr&=0xbf; /* Clear bit 6 - timer 1 */
UpdateIFRTopBit();
if (UserVIAState.timer1c<=(UserVIAState.timer1l*2))
return(tmp & 0xff);
else
return(0xff);
case 5: /* Timer 1 ho counter */
tmp=UserVIAState.timer1c /512;
if (UserVIAState.timer1c<=(UserVIAState.timer1l*2))
return(tmp & 0xff);
else
return(0xff);
case 6: /* Timer 1 lo latch */
return(UserVIAState.timer1l & 0xff);
case 7: /* Timer 1 ho latch */
return((UserVIAState.timer1l / 256) & 0xff);
case 8: /* Timer 2 lo counter */
tmp=UserVIAState.timer2c / 2;
UserVIAState.ifr&=0xdf; /* Clear bit 5 - timer 2 */
UpdateIFRTopBit();
if (UserVIAState.timer2c<=(UserVIAState.timer2l*2))
return(tmp & 0xff);
else
return(0xff);
case 9: /* Timer 2 ho counter */
if (UserVIAState.timer2c<=(UserVIAState.timer2l*2))
return((UserVIAState.timer2c / 512) & 0xff);
else
return(0xff);
case 13:
UpdateIFRTopBit();
return(UserVIAState.ifr);
break;
case 14:
return(UserVIAState.ier | 0x80);
case 1:
UserVIAState.ifr&=0xfc;
UpdateIFRTopBit();
case 15:
return(255);
break;
} /* Address switch */
return(0xff);
} /* UserVIARead */
/*--------------------------------------------------------------------------*/
void UserVIATriggerCA1Int(void) {
/* We should be concerned with active edges etc. */
UserVIAState.ifr|=2; /* CA1 */
UpdateIFRTopBit();
}; /* UserVIATriggerCA1Int */
/*--------------------------------------------------------------------------*/
void UserVIA_poll_real(void) {
int tCycles;
if (UserVIAState.timer1c<0) {
tCycles=abs(UserVIAState.timer1c);
/*std::cerr << "UserVIA timer1c\n"; */
UserVIAState.timer1c=(UserVIAState.timer1l * 2)-(tCycles-4);
if ((UserVIAState.timer1hasshot==0) || (UserVIAState.acr & 0x40)) {
/*std::cerr << "UserVIA timer1c - int at " << TotalCycles << "\n"; */
UserVIAState.ifr|=0x40; /* Timer 1 interrupt */
UpdateIFRTopBit();
if (UserVIAState.acr & 0x80) {
UserVIAState.orb^=0x80; /* Toggle PB7 */
UserVIAState.irb^=0x80; /* Toggle PB7 */
};
};
UserVIAState.timer1hasshot=1;
} /* timer1c underflow */
if (UserVIAState.timer2c<0) {
tCycles=abs(UserVIAState.timer2c);
/* std::cerr << "UserVIA timer2c\n"; */
UserVIAState.timer2c=(UserVIAState.timer2l * 2)-(tCycles-4);
if (!(UserVIAState.ifr&=0x20)) {
/* std::cerr << "UserVIA timer2c - int\n"; */
UserVIAState.ifr|=0x20; /* Timer 2 interrupt */
UpdateIFRTopBit();
};
} /* timer2c underflow */
} /* UserVIA_poll */
/*--------------------------------------------------------------------------*/
void UserVIAReset(void) {
VIAReset(&UserVIAState);
} /* UserVIAReset */
/*-------------------------------------------------------------------------*/
//==void SaveUserVIAState(unsigned char *StateData) {
//## handled in via.cc now
//== SaveVIAState(&UserVIAState, StateData);
//==}
/*-------------------------------------------------------------------------*/
//==void RestoreUserVIAState(unsigned char *StateData) {
//== handled in via.cc now
//== RestoreVIAState(&UserVIAState, StateData);
//==}
/*--------------------------------------------------------------------------*/
void uservia_dumpstate(void) {
std::cerr << "Uservia:\n";
via_dumpstate(&UserVIAState);
}; /* uservia_dumpstate */