-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.js
executable file
·660 lines (539 loc) · 14.6 KB
/
Core.js
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// Run State 3 = Paused(KeyPress), 2 = Go, 1 = Paused, 0 = Stop, -1 = Error
var rS = 2;
// Executable list
var eList = [];
// Procedure Stack
var pS = [];
// Call and return from procedure index flags
var cpW = -1, rpW = -1;
// Pause Waiting flag
var pW = 0;
// Procedure Offset
var pc = 0;
// Procedure DSF Offset
var pdsf = 0;
// Cache the bytes of the procedure being run
var pB = [];
// Create the Data Stack Frame with 64K memory
var DSF = new Mem(1024 * 64);
// Program Flags
var gUPDATEON = 1;
var opCodeFuncs = [];
var calcMem = 0;
// Stores wO values
var debugWin = null;
var debugMsgs = [];
var debugMsgMax = 50;
var stackWin = null;
// Keypress information
var lastKeyPress = 0;
// Initialiser
function init() {
document.write('Psion OPO Runtime<br />');
document.write('Created by Kevin Pfister<br />');
document.write('<br />');
// Memory Test code
//DSF.set(0, cf64(3.141592653));
//document.write(f64(DSF.m, 0));
//return;
// Push the initial graphical window.
Renderer.Init();
Renderer.gCREATE(0, 0, 640, 240, 1, 4);
debugWin = document.createElement('div');
stackWin = document.createElement('div');
document.write('<br /><br />');
document.body.appendChild(debugWin);
debugWin.width = 640;
debugWin.height = 480;
// Add an event listener for keypresses
document.addEventListener( "keypress", doKeyDown, false )
// Load and store the executable
eList.push(new exe(Hex2Bytes(strap3)));
// Visualise the memory
Debugger.VisualiseMemory();
document.write('<br /><b>Stack Contents:</b><br />');
document.body.appendChild(stackWin);
stackWin.width = 640;
stackWin.height = 480;
// Unknown opcodes
document.write('<br /><br />');
document.write('<b>Unknown Opcodes:</b><br />');
// Call the first procedure in the file
CProc(0);
// Allocate memory to the virtual Calculator register
calcMem = DSF.alloc(8);
// Begin processing opcodes
pOp();
}
function doKeyDown(e) {
if (DialogManager.dlgActive) {
wO(' Key Pressed: ' + e.keyCode);
DialogManager.ProcessKey(e.keyCode);
} else {
wO(' Key Pressed: ' + e.keyCode);
lastKeyPress = e.KeyCode;
}
}
// Process Opcodes
function pOp() {
// while (rS == 2 && !DialogManager.dlgActive ) {
if (rS == 2 && !DialogManager.dlgActive ) {
pNOp();
// Render the screen.
Renderer.Composite(false);
DialogManager.Composite();
Stack.visualise();
if (cpW != -1) {
// Calling Procedure
CProc(cpW);
}
if (rpW != -1) {
RProc();
}
if (pS.length != 0 && pc >= pS[pS.length-1].p.q) {
wO('Reached the end of the procedure');
RProc();
}
}
// Visualise the memory
Debugger.VisualiseMemory();
if (rS == 1 || DialogManager.dlgActive) {
// Internal Pause state
setTimeout(pOp,500);
return;
} else if (rS == 2) {
// Running state
if (pW > 0)
{
// Pause for a certain time period
setTimeout(pOp, pW);
pW = 0;
}
else
{
setTimeout(pOp, 100);
}
return;
} else if (rS == 3) {
// Paused state, awaiting keypress
if (lastKeyPress != 0)
{
// Key has been pressed.
rS = 2;
pW = 0; // Cancel any pause state.
lastKeyPress = 0;
}
if (pW > 0)
{
// The system was in a pause awaiting a keypress.
pW -= 10;
if (pW<=0)
{
pW = 0;
rS = 2; // Return to running.
}
}
setTimeout(pOp, 10);
}
// rS = 0
wO('Application has terminated');
}
// Process next available OpCode
function pNOp() {
var op = pB[pc];
var args = 0;
switch (op) {
case 0x53:
wO('0x53: Call PROC EE');
var rc = i16(pB, pc + 1);
wO('* Call Procedure, Reference: ' + rc);
pc += 2;
cpW = getCPI(rc);
if (cpW == -1) {
// Unable to find correct CPW
ErrorHandler.Raise(-99);
} else {
wO('* Procedure CPI Reference: ' + cpW);
}
break;
case 0x57:
pc++;
pNOp57();
break;
case 0x5B:
var IFarg = Stack.ppi16();
var IFjmp = i16(pB, pc + 1);
wO('0x5B: IF ' + IFarg + ' == 0 THEN GOTO +' + IFjmp);
if (IFarg == 0) {
pc += IFjmp - 1;
wO(' * IF condition successful');
} else {
pc+=2;
wO(' * IF condition failed');
}
break;
case 0x63:
var val1 = Stack.ppi32();
wO('0x63: push& VV%(' + val1 + ')');
Stack.pi32(val1);
break;
case 0x74:
case 0x75:
case 0x76:
case 0x77:
wO('0x74 - 0x77: RETURN');
rpW = 1;
break;
case 0x80:
case 0x81:
case 0x82:
case 0x83:
wO('0x80 - 0x83: POP and discard');
Stack.ppd();
break;
case 0xA9:
var arg = pB[++pc];
wO('0xA9: ESCAPE ' + arg + ' - TODO');
break;
case 0xB1:
var arg = u16(pB, pc + 1);
ErrorHandler.HandlerOffset = pc + arg;
wO('0xB1: ONERR jump to (' + ErrorHandler.HandlerOffset + ')');
pc += 2;
break;
case 0xBF:
var arg = i16(pB, pc + 1);
wO('0xBF: GOTO ' + arg);
pc += arg - 1;
break;
case 0xC0:
wO('0xC0: RETURN POP+');
Stack.ppd();
rpW = 1;
break;
case 0xED:
wO('0xED: Extended Dialog Box Commands');
pc++;
pNOpED();
break;
case 0xF1:
var arg = pB[++pc];
wO('0xF1: LOCK ' + arg + ' - TODO');
break;
case 0xFF:
wO('0xFF: Extended Commands');
pc++;
pNOpFF();
break;
default:
// Check to see if it is in the OpCodeFuncs
var fnd = false;
for (var i = 0; i < opCodeFuncs.length; i++) {
if (opCodeFuncs[i].length == 2) {
if (opCodeFuncs[i][0] == op) {
opCodeFuncs[i][1]();
fnd = true;
break;
}
}
}
if (!fnd) {
document.write('Opcode: ' + op + ' at Offset ' + pc + '<br />');
}
}
pc++;
}
function pNOp57() {
var op = pB[pc];
switch (op) {
case 0x02:
var arg = pB[++pc];
wO('CALL ' + arg + ' arguments');
w_CALL(arg);
break;
default:
// Check to see if it is in the OpCodeFuncs
var fnd = false;
for (var i = 0; i < opCodeFuncs.length; i++) {
if (opCodeFuncs[i].length == 3) {
if (opCodeFuncs[i][0] == op && opCodeFuncs[i][2] == 0x57) {
opCodeFuncs[i][1]();
fnd = true;
break;
}
}
}
if (!fnd) {
document.write('Opcode: 0x57 -> ' + op + ' at Offset ' + pc + '<br />');
}
}
}
function pNOpED() {
// These are for Extended Dialog Box Commands
var op = pB[pc];
// Check to see if it is in the OpCodeFuncs
var fnd = false;
for (var i = 0; i < opCodeFuncs.length; i++) {
if (opCodeFuncs[i].length == 3) {
if (opCodeFuncs[i][0] == op && opCodeFuncs[i][2] == 0xED) {
opCodeFuncs[i][1]();
fnd = true;
break;
}
}
}
if (!fnd) {
document.write('Opcode: 0xED -> ' + op + ' at Offset ' + pc + '<br />');
}
}
function pNOpFF() {
// These are for Extended Dialog Box Commands
var op = pB[pc];
// Check to see if it is in the OpCodeFuncs
var fnd = false;
for (var i = 0; i < opCodeFuncs.length; i++) {
if (opCodeFuncs[i].length == 3) {
if (opCodeFuncs[i][0] == op && opCodeFuncs[i][2] == 0xFF) {
opCodeFuncs[i][1]();
fnd = true;
break;
}
}
}
if (!fnd) {
document.write('Opcode: 0xFF -> ' + op + ' at Offset ' + pc + '<br />');
}
}
// Get the index of an externally referenced procedure
function getCPI(ee) {
var CPn = getEEn(ee);
wO('EE value of ' + ee + ' corresponds to ' + CPn);
for (var i = 0; i < pList.length; i++) {
if (strcmp(pList[i].n, CPn) == 0) {
return i;
}
}
return -1;
}
// Get the name of an externally referenced variable or proc
function getEEn(ee) {
var tP = pS[pS.length - 1].p;
var pID = tP.n;
wO(' * Get the procedure ID of the running PROC ' + pID);
wO(' * Check Global Variables defined in the Procedure');
var EEo = getEEo(ee, tP.gvs);
if (EEo) {return EEo.n;}
wO(' * Check Called Procedures');
EEo = getEEo(ee, tP.cp);
if (EEo) {return EEo.n;}
wO(' * Check Global Variables Referenced');
EEo = getEEo(ee, tP.gr);
if (EEo) {return EEo.n;}
wO(' * ERROR: EE Reference not found!');
return "";
}
// Returns the object which corresponds to an EE index if found
function getEEo(ee, a) {
if (a.length > 0) {
wO('Minimum EE val: ' + a[0].EEi);
} else {
wO('No values to query EE');
}
for (var i = 0; i < a.length; i++) {
wO(' * EE [' + a[i].n + '] : ' + a[i].EEi);
if (a[i].EEi == ee) {
return a[i];
}
}
}
// Return the type of an External
function getEEt(ee) {
wO(' * Get the procedure ID of the running PROC');
var tP = pS[pS.length - 1].p;
wO(' * Check Global Variables defined in the Procedure');
if (getEEo(ee, tP.gvs)) {return 0}
wO(' * Check Called Procedures');
if (getEEo(ee, tP.cp)) {return 1}
wO(' * Check Global Variables Referenced');
if (getEEo(ee, tP.gr)) {return 2}
wO(' * ERROR: EE Reference not found!');
// Stop Execution
rS = 0;
}
// Return the value of an External
function getEEv(ee) {
wO(' * Get the procedure ID of the running PROC');
var tP = pS[pS.length - 1].p;
var pID = tP.i;
wO(' * Check Global Variables defined in the Procedure');
var EEo = getEEo(ee, tP.gvs);
if (EEo) {
wO(' EE value references a declared global variable: ' + EEo.n);
// Return a value based on the type
return getEEoV(EEo);
}
wO(' * Check Global Variables Referenced');
EEo = getEEo(ee, tP.gr);
if (EEo) {
wO(' EE value references a referenced global variable: ' + EEo.n);
// Return a value based on the type
return getEEoV(EEo);
}
wO(' * ERROR: EE Reference not found!');
// Stop Execution
rS = 0;
}
function getEEa(ee) {
wO(' * Get the procedure ID of the running PROC');
var tP = pS[pS.length - 1].p;
var pID = tP.i;
wO(' * Check Global Variables defined in the Procedure');
var EEo = getEEo(ee, tP.gvs);
if (EEo) {
wO(' EE value references a declared global variable: ' + EEo.n);
// Return a value based on the type
return EEo.d;
}
wO(' * Check Global Variables Referenced');
EEo = getEEo(ee, tP.gr);
if (EEo) {
wO(' EE value references a referenced global variable: ' + EEo.n);
// Return a value based on the type
return EEo.d;
}
wO(' * ERROR: EE Reference not found!');
// Stop Execution
rS = 0;
}
function getEEoV(EEo) {
if (EEo.t == 0) {
return i16(DSF.m, EEo.d);
} else if (EEo.t == 1) {
return i32(DSF.m, EEo.d);
} else if (EEo.t == 2) {
return f64(DSF.m, EEo.d);
} else if (EEo.t == 3) {
// String (Offset 0 is the max string length)
return CStr(DSF.m, EEo.d + 1);
}
}
// Return the value type of an External
function getEEvt(ee) {
var tP = pS[pS.length - 1].p;
var pID = pS[pS.length - 1].i;
wO(' * Get the procedure ID of the running PROC [' + pID + ']');
wO(' * GVS: ' + tP.gvs.length);
wO(' * CP: ' + tP.cp.length);
wO(' * P: ' + tP.p.length);
wO(' * GR: ' + tP.gr.length);
if (tP.gvs.length > 0)
{
wO('Query EE value references in declared global variables');
var EEo = getEEo(ee, tP.gvs);
if (EEo) {
wO(' EE value references a declared global variable: ' + EEo.n);
return EEo.t;
}
}
if (tP.p.length > 0)
{
wO('Query EE value references in Parameters');
EEo = getEEo(ee, tP.p);
if (EEo) {
wO(' EE value references a Parameter: ' + EEo.n);
return EEo.t;
}
}
if (tP.gr.length > 0)
{
wO('Query EE value references in referenced global variables');
EEo = getEEo(ee, tP.gr);
if (EEo) {
wO(' EE value references a referenced global variable: ' + EEo.n);
return EEo.t;
}
}
wO(' * ERROR: EE Reference (' + ee + ') not found!');
// Stop Execution
rS = 0;
}
// Call Procedure
function CProc(i) {
wO('Calling Procedure: ' + pList[i].n + ':');
// Clear the Call Procedure Waiting Flag
cpW = -1;
if (pS.length == 255) {
ErrorHandler.Raise(-13);
return;
}
// Procedure Stack Item
var psi = new Object();
// Store Index
psi.i = i;
psi.pc = pc;
// Store a link to the procedure Object
psi.p = pList[i];
// Store the offset to the DSF proc cache
psi.pdsf = pdsf;
wO('QCode Length of PROC: ' + psi.p.q);
pS.push(psi);
// Reset Application counter
pc = 0;
// Cache QCode
pB = pS[pS.length - 1].p.qc;
// Alloc space and save the link to the DSF position for the procedure
pdsf = DSF.alloc(pList[i].d, 2);
// Go through and initialise space for variables.
wO('Initialising procedure variables: ');
for (var i = 0; i< psi.p.ac.length; i++) {
wO('Initialising array - Offset: ' + (pdsf + psi.p.ac[i].dso) + ' Size: ' + psi.p.ac[i].s);
if (psi.p.ac[i].s < 255)
{
DSF.set((pdsf + psi.p.ac[i].dso), [0, psi.p.ac[i].s]);
}
else
{
// Todo: Fix for 16bit values (greater than 255)
DSF.set((pdsf + psi.p.ac[i].dso), [psi.p.ac[i].s,0]);
}
}
for (var i = 0; i< psi.p.sc.length; i++) {
wO('Initialising string - Offset: ' + (pdsf + psi.p.sc[i].dso) + ' Size: ' + psi.p.sc[i].s );
DSF.set((pdsf + psi.p.sc[i].dso), [psi.p.sc[i].s, 0]);
}
// Visualise the memory
Debugger.VisualiseMemory();
}
// Return from a procedure
function RProc() {
DSF.dealloc(pdsf);
// Cancel error handling
ErrorHandler.HandlerOffset = -1;
if (pS.length == 0) {
rS = 0;
wO('Proc Stack is now empty - Terminating execution.');
} else {
pc = pS[pS.length - 1].pc;
pB = pS[pS.length - 1].p.qc;
pdsf = pS[pS.length - 1].pdsf;
wO('Returning control to PROC ' + pS[pS.length - 1].p.n + ':');
wO(' - Executing from: ' + pc);
pS.pop();
}
// Reset the Return Procedure Waiting flag
rpW = -1;
}
// Write output to chosen method
function wO(m) {
debugMsgs.push(m);
if (debugMsgs.length > debugMsgMax) {
debugMsgs.shift();
}
if (debugWin != null) {
var debugTxt = '';
for(var i=0; i< debugMsgs.length; i++) {
debugTxt += debugMsgs[i] + '<br />';
}
debugWin.innerHTML = debugTxt;
}
}