-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cpu.c
327 lines (252 loc) · 6.66 KB
/
Cpu.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#define G_LOG_USE_STRUCTURED
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdint.h>
// There is no real gtk+ code here, so g_boolean is not used.
#include <stdbool.h>
#include "Cpu.h"
#include "Wiring.h"
#include "Sound.h"
#include "E803-types.h"
#include "wg-definitions.h"
#include "Emulate.h"
extern E803word WG;
unsigned int WG_ControlButtons;
//unsigned int WG_ControlButtonPresses;
//unsigned int WG_ControlButtonReleases;
bool WG_operate_pressed = false;
unsigned int volume;
static
void dumpWord(E803word word)
{
unsigned int f1,n1,b,f2,n2;
f1 = (word >> 33) & 077;
n1 = (word >> 20) & 017777;
b = (word >> 19) & 01;
f2 = (word >> 13) & 077;
n2 = word & 017777;
printf("%02o %4d %c %02o %4d\n",f1,n1,b?'/':':',f2,n2);
}
// Wire handlers
static void setF1(unsigned int value)
{
E803word bits = value;
g_info("%s called value = %d\n",__FUNCTION__,value);
WG &= 00077777777777; // F1
WG |= 07700000000000 & (bits << 33);
dumpWord(WG);
}
static void setN1(unsigned int value)
{
E803word bits = value;
g_info("%s called value = %d\n",__FUNCTION__,value);
WG &= 07700001777777; // N1 + B
WG |= 00077776000000 & (bits << 19);
dumpWord(WG);
}
static void setF2(unsigned int value)
{
E803word bits = value;
g_info("%s called value = %d\n",__FUNCTION__,value);
WG &= 07777776017777; // F1
WG |= 00000001760000 & (bits << 13);
dumpWord(WG);
}
static void setN2(unsigned int value)
{
E803word bits = value;
g_info("%s called value = %d\n",__FUNCTION__,value);
WG &= 07777777760000; // N2
WG |= 00000000017777 & bits;
dumpWord(WG);
}
static void setRON(unsigned int button)
{
g_info("%s called value = %d\n",__FUNCTION__,button);
WG_ControlButtons &= ~(WG_read | WG_normal | WG_obey);
WG_ControlButtons |= button;
}
static void setMD(unsigned int value)
{
g_info("%s called value = %d\n",__FUNCTION__,value);
if(value == 0)
WG_ControlButtons &= ~WG_manual_data;
else
WG_ControlButtons |= WG_manual_data;
}
static void setRESET(unsigned int value)
{
g_info("%s called value = %d\n",__FUNCTION__,value);
if(value == 0)
WG_ControlButtons &= ~WG_reset;
else
WG_ControlButtons |= WG_reset;
}
static void setCS(unsigned int value)
{
g_info("%s called value = %d\n",__FUNCTION__,value);
if(value == 0)
WG_ControlButtons &= ~WG_clear_store;
else
WG_ControlButtons |= WG_clear_store;
}
static void setSS(unsigned int value)
{
g_info("%s called value = %d\n",__FUNCTION__,value);
if(value == 0)
{
WG_ControlButtons &= ~WG_selected_stop;
//WG_ControlButtonReleases |= WG_selected_stop;
}
else
{
WG_ControlButtons |= WG_selected_stop;
//WG_ControlButtonPresses |= WG_selected_stop;
}
}
static void setOPERATE(unsigned int value)
{
g_info("%s called value = %d\n",__FUNCTION__,value);
if(value == 0)
{
WG_ControlButtons &= ~WG_operate;
//WG_ControlButtonReleases |= WG_operate;
}
else
{
WG_ControlButtons |= WG_operate;
WG_operate_pressed = true;
//WG_ControlButtonPresses |= WG_operate;
}
}
static void CPU_sound(__attribute__((unused)) void *buffer,
__attribute__((unused))int sampleCount,
__attribute__((unused))double bufferTime,
int wordTimes)
{
static bool first = true;
static bool updateFlag = true;
static int updateRate = UPDATE_RATE;
static int callCount = 1;
if (first)
{
first = false;
PreEmulate(false);
}
else
{
if(updateRate == UPDATE_RATE)
{
updateRate = 1;
updateFlag = true;
//printf("updateFlag true at %d calls\n",callCount);
}
else
{
updateRate += 1;
updateFlag = false;
}
}
// Simplified version for testing during development
PostEmulate(updateFlag);
PreEmulate(updateFlag);
//doExternalDeviceFSM(SOUNDTICK);
Emulate(wordTimes);
//WG_ControlButtonPresses = WG_ControlButtonReleases = 0;
WG_operate_pressed = false;
//gtk_widget_queue_draw(CoreDrawingArea);
callCount += 1;
}
extern E803word *CoreStore;
void CpuTidy(GString *userPath,gchar *coreFileName)
{
GString *CoreImageFileName = NULL;
GError *error = NULL;
GFile *gf;
GFileOutputStream *gfos;
gboolean writeOk;
gsize written;
CoreImageFileName = g_string_new(userPath->str);
if(coreFileName != NULL)
{
g_string_append(CoreImageFileName,coreFileName);
}
else
{
g_string_append(CoreImageFileName,"CoreImage");
}
gf = g_file_new_for_path(CoreImageFileName->str);
gfos = g_file_replace (gf,
NULL,
TRUE,
G_FILE_CREATE_NONE,
NULL,
&error);
writeOk = g_output_stream_write_all (G_OUTPUT_STREAM(gfos),
CoreStore,
8192 * sizeof(E803word),
&written,
NULL,
&error);
g_output_stream_close (G_OUTPUT_STREAM(gfos),NULL,&error);
g_info("writeOk is %s\n",writeOk?"true":"false");
g_info("written = %zu\n",written);
g_object_unref(gf);
g_string_free(CoreImageFileName,TRUE);
}
void CpuInit(__attribute__((unused)) GtkBuilder *builder,
__attribute__((unused)) GString *sharedPath,
__attribute__((unused)) GString *userPath,
gchar *coreFileName)
{
GString *CoreImageFileName = NULL;
gchar *core = NULL;
GError *error = NULL;
//g_info("%s called\n",__FUNCTION__);
connectWires(F1WIRES,setF1);
connectWires(N1WIRES,setN1);
connectWires(F2WIRES,setF2);
connectWires(N2WIRES,setN2);
connectWires(RONWIRES,setRON);
connectWires(MDWIRE,setMD);
connectWires(RESETWIRE,setRESET);
connectWires(CSWIRE,setCS);
connectWires(SSWIRE,setSS);
connectWires(OPERATEWIRE,setOPERATE);
connectWires(VOLUME_CONTROL,setCPUVolume);
CoreImageFileName = g_string_new(userPath->str);
if(coreFileName != NULL)
{
g_string_append(CoreImageFileName,coreFileName);
}
else
{
g_string_append(CoreImageFileName,"CoreImage");
}
{
GFile *gf;
GBytes *gb;
GByteArray *gba;
gf = g_file_new_for_path(CoreImageFileName->str);
gb = g_file_load_bytes(gf,
NULL,NULL,&error);
if(error != NULL)
{
g_warning("Failed to open core file %s (%s), using a clear store\n",
CoreImageFileName->str,error->message);
core = calloc(8192,sizeof(E803word));
}
else
{
// Don't unref the gba as it's data IS the core store.
gba = g_bytes_unref_to_array(gb);
g_byte_array_ref(gba);
core = (char *) gba->data;
g_info("Core image file Length = %u\n",gba->len);
}
g_object_unref(gf);
}
StartEmulate(core);
g_string_free(CoreImageFileName,TRUE);
addSoundHandler(CPU_sound);
}