-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdcpu.js
More file actions
1095 lines (883 loc) · 24.9 KB
/
Copy pathdcpu.js
File metadata and controls
1095 lines (883 loc) · 24.9 KB
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function isdigit(c)
{
return c >= "0" && c <= "9";
}
function ishexdigit(c)
{
return (c >= "A" && c <= "F") || (c >= "a" && c <= "f") || isdigit(c);
}
function isalpha(c)
{
return (c >= "A" && c <= "Z") || (c >= "a" && c <= "z");
}
function isalnum(c)
{
return isdigit(c) || isalpha(c);
}
// =====================================================================================================================
// Log/Warning/Error Reporter
// =====================================================================================================================
function dcpuReport()
{
this.Content = [ ];
this.Reset = function()
{
this.Content = [ ];
}
this.Log = function(text, line)
{
this.Content.push([ text, line ]);
}
this.Error = function(text, line)
{
this.Log("ERROR -- " + text, line);
}
this.UnexpectedToken = function(token, line)
{
this.Error("Unexpected token '" + token + "'", line);
}
this.ExpectingToken = function(token, got, line)
{
this.Error("Expecting token '" + token + "' but got '" + got + "'", line);
}
}
// =====================================================================================================================
// DCPU-16 Basics
// =====================================================================================================================
dcpuOpcodes = {
// Breakpoint/halt instruction
BRK: 0x0000,
// Basic opcodes
SET: 0x01,
ADD: 0x02,
SUB: 0x03,
MUL: 0x04,
DIV: 0x05,
MOD: 0x06,
SHL: 0x07,
SHR: 0x08,
AND: 0x09,
BOR: 0x0A,
XOR: 0x0B,
IFE: 0x0C,
IFN: 0x0D,
IFG: 0x0E,
IFB: 0x0F,
// Non-basic opcodes
JSR: 0x10
};
dcpuRegisters = {
// Arithmetic registers
A: 0x00,
B: 0x01,
C: 0x02,
X: 0x03,
Y: 0x04,
Z: 0x05,
I: 0x06,
J: 0x07,
// Control registers
SP: 0x1B,
PC: 0x1C,
O: 0x1D,
// Psuedo registers
POP: 0x18,
PEEK: 0x19,
PUSH: 0x1A
};
dcpuVideoColors={
0: {R: 0x00, G: 0x00, B: 0x00},
1: {R: 0x00, G: 0x00, B: 0x7F},
2: {R: 0x00, G: 0x7F, B: 0x00},
3: {R: 0x00, G: 0x7F, B: 0x7F},
4: {R: 0x7F, G: 0x00, B: 0x00},
5: {R: 0x7F, G: 0x00, B: 0x7F},
6: {R: 0x7F, G: 0x7F, B: 0x00},
7: {R: 0x7F, G: 0x7F, B: 0x7F},
8: {R: 0x30, G: 0x30, B: 0x30},
9: {R: 0x00, G: 0x00, B: 0xFF},
10: {R: 0x00, G: 0xFF, B: 0x00},
11: {R: 0x00, G: 0xFF, B: 0xFF},
12: {R: 0xFF, G: 0x00, B: 0x00},
13: {R: 0xFF, G: 0x00, B: 0xFF},
14: {R: 0xFF, G: 0xFF, B: 0x00},
15: {R: 0xFF, G: 0xFF, B: 0xFF}
};
// =====================================================================================================================
// Lexer for the DCPU Assembly Language
// =====================================================================================================================
var dcpuTokens = {
INVALID:"INVALID",
END:"END OF LINE",
// Basic characters
COLON:":",
COMMENT:";",
COMMA:",",
LBRACKET:"[",
RBRACKET:"]",
PLUS:"+",
// Keywords/text
INSTRUCTION:"INSTRUCTION",
REGISTER:"REGISTER",
DOTDIRECTIVE:"DOT DIRECTIVE",
LABEL:"LABEL",
DATA:"DATA",
// Values
NUMBER:"NUMBER",
STRING:"STRING"
};
function dcpuLexer(report)
{
// Initialise defaults
this.Report = report;
this.Text = "";
this.Length = 0;
this.Pos = 0;
this.Line = 0;
this.SetText = function(text, line)
{
this.Text = text;
this.Length = this.Text.length;
this.Pos = 0;
this.Line = line;
}
this.ParseNumber = function()
{
var number_text = "";
// Skip the hex prefix
var p = this.Pos;
if (p + 2 <= this.Length)
{
if (this.Text[p] == "0" && this.Text[p + 1] == "x")
{
number_text = "0x";
this.Pos += 2;
}
}
// Pull together the text for the number
while (this.Pos < this.Length)
{
var c = this.Text[this.Pos];
if (!ishexdigit(c))
break;
number_text += c;
this.Pos++;
}
// Check for parse errors before returning
// NOTE: Not sure how to check for NaN - very dodgy code
var num = parseInt(number_text);
if (num.toString() == "NaN")
return [ dcpuTokens.INVALID, null ];
if (num > 0xFFFF)
{
this.Report.Error("Number '" + num + "' out of range", this.Line);
return [ dcpuTokens.INVALID, null ];
}
return [ dcpuTokens.NUMBER, num ];
}
this.ParseString = function()
{
var text = "";
// Skip entry
this.Pos++;
// Always increment parsing text to consume the closing quote
var end = false;
while (this.Pos < this.Length)
{
var c = this.Text[this.Pos++];
if (c == '"')
{
end = true;
break;
}
text += c;
}
if (end == false)
{
this.Report.Error("Unexpected end of file looking for string end", this.Line);
return [ dcpuTokens.INVALID, null ];
}
return [ dcpuTokens.STRING, text ];
}
this.ParseText = function()
{
var text = this.Text[this.Pos++];
// Parse the text, only incrementing on success
while (this.Pos < this.Length)
{
var c = this.Text[this.Pos];
if (!isalnum(c) && c != "." && c != "_")
break;
text += c;
this.Pos++;
}
// Promote all text to uppercase
text = text.toUpperCase();
// First check to see if the text can be recognised as something other than a label
if (text in dcpuOpcodes)
return [ dcpuTokens.INSTRUCTION, dcpuOpcodes[text] ];
if (text in dcpuRegisters)
return [ dcpuTokens.REGISTER, dcpuRegisters[text] ];
if (text != "" && text[0] == '.')
return [ dcpuTokens.DOTDIRECTIVE, text ];
if (text == "DAT")
return [ dcpuTokens.DATA, text ];
return [ dcpuTokens.LABEL, text ];
}
this.SkipComment = function()
{
this.Pos = this.Length;
return [ dcpuTokens.COMMENT, null ];
}
this.NextToken = function()
{
while (this.Pos < this.Length)
{
var c = this.Text[this.Pos];
switch (c)
{
// Basic character token values are the same as their characters
case (":"):
case (","):
case ("["):
case ("]"):
case ("+"):
this.Pos++;
return [ c, null ];
case (";"):
return this.SkipComment();
case ('"'):
return this.ParseString();
default:
// Skip whitespace
if (c <= " ")
{
this.Pos++;
}
else if (isdigit(c))
{
return this.ParseNumber();
}
else if (isalpha(c) || c == "." || c == "_")
{
return this.ParseText();
}
else
{
this.Report.Error("Invalid character '" + c + "'", this.Line);
return [ dcpuTokens.INVALID, null ];
}
}
}
return [ dcpuTokens.END, null ];
}
this.ConsumeExpectToken = function(expected)
{
var token = this.NextToken();
if (token[0] != expected)
{
this.Report.ExpectingToken(expected, token[0], this.Line);
return false;
}
return true;
}
}
// =====================================================================================================================
// DCPU-16 Assembly to Byte Code
// =====================================================================================================================
function dcpuAssembler(report)
{
this.Report = report;
this.Labels = { };
this.LabelPatches = [ ];
this.WordCode = [ ];
this.Line = 0;
this.PCToLine = { };
this.AddLabelPatch = function(label, offset)
{
this.LabelPatches.push([ label, this.WordCode.length + offset, this.Line ]);
}
this.ParseData = function()
{
while (true)
{
// Check for exit
var token = this.Lexer.NextToken();
if (token[0] == dcpuTokens.END)
break;
if (token[0] == dcpuTokens.INVALID)
{
this.Report.UnexpectedToken(token[0], this.Line);
break;
}
// Push directly into byte-code depending upon the token type
var value = token[1];
switch (token[0])
{
case (dcpuTokens.NUMBER):
this.WordCode.push(value);
break;
case (dcpuTokens.STRING):
for (var i in value)
this.WordCode.push(value.charCodeAt(i));
break;
}
}
}
this.ParseLabel = function()
{
// Get the label name
var token = this.Lexer.NextToken();
if (token[0] != dcpuTokens.LABEL)
{
this.Report.ExpectingToken(dcpuTokens.LABEL, token[0], this.Line);
return;
}
this.Labels[token[1]] = this.WordCode.length;
}
this.ParseAddressArgument = function(extra_words)
{
//
// Possibilities:
//
// Reg -> 0x00-0x07 (and pseudo/ctrl)
// Imm -> 0x20-0x3f for 5-bit numbers or 0x1f + next word
// Lbl -> 0x1f + next word (5-bit numbers not allowed)
// [Reg] -> 0x08-0x0f (and pseudo/ctrl)
// [Imm] -> 0x1e + next word
// [Lbl] -> 0x1e + next word
// [Imm+Reg] -> 0x10-0x17 (pseudo/ctrl not allowed, value in next word)
// [Lbl+Reg] -> 0x10-0x17 (pseudo/ctrl not allowed, value in next word)
//
var token = this.Lexer.NextToken();
var value = token[1];
// Only arithmetic registers can be used as address operands
if (token[0] == dcpuTokens.REGISTER)
{
if (value > 0x07)
{
this.Report.Error("Can't use register '" + value + "' as an address operand", this.Line);
return [ false, 0 ];
}
// Also double-check that the closing token is the bracket
return [ this.Lexer.ConsumeExpectToken(dcpuTokens.RBRACKET), 0x08 + value ];
}
// Either add an immediate or label patch
if (token[0] == dcpuTokens.NUMBER)
{
extra_words.push(value);
}
else if (token[0] == dcpuTokens.LABEL)
{
this.AddLabelPatch(value, 1 + extra_words.length);
extra_words.push(0);
}
else
{
this.Report.UnexpectedToken(token[0], this.Line);
return [ false, 0 ];
}
// Leave if there's no offset
token = this.Lexer.NextToken();
if (token[0] == dcpuTokens.RBRACKET)
return [ true, 0x1E ];
// Parse the register offset
if (token[0] == dcpuTokens.PLUS)
{
token = this.Lexer.NextToken();
if (token[0] != dcpuTokens.REGISTER)
{
this.Report.ExpectingToken(dcpuTokens.REGISTER, token[0], this.Line);
return [ false, 0 ];
}
// Only arithmetic registers can be used as address operands
if (token[1] > 0x07)
{
this.Report.Error("Can't use register '" + token[1] + "' as an address operand", this.Line);
return [ false, 0 ];
}
// Also double-check that the closing token is the bracket
return [ this.Lexer.ConsumeExpectToken(dcpuTokens.RBRACKET), 0x10 + token[1] ];
}
this.Report.UnexpectedToken(token[0], this.Line);
return [ false, 0 ];
}
this.ParseArgument = function(extra_words)
{
var token = this.Lexer.NextToken();
var value = token[1];
switch (token[0])
{
// Registers are simple!
case (dcpuTokens.REGISTER):
return [ true, value ];
// 5-bit numbers can be compacted into the register details, otherwise they're in the next word
case (dcpuTokens.NUMBER):
// NOTE: Disabled for now as the emulator reads/writes with memory addresses
//if (value <= 0x1F)
// return [ true, value + 0x20 ];
extra_words.push(value);
return [ true, 0x1F ];
// Add a label patch note for the literal
case (dcpuTokens.LABEL):
this.AddLabelPatch(value, 1 + extra_words.length);
extra_words.push(0);
return [ true, 0x1F ];
// This is quite complicated...
case (dcpuTokens.LBRACKET):
return this.ParseAddressArgument(extra_words);
case (dcpuTokens.INVALID):
case (dcpuTokens.END):
default:
this.Report.UnexpectedToken(token[0], this.Line);
return [ false, 0 ];
}
return [ true, 0 ];
}
this.ParseInstruction = function(token)
{
// Start off with the opcode
var word = token[1];
var extra_words = [ ];
// Is this a complex opcode?
if ((word & 0xF) == 0)
{
if (word == dcpuOpcodes.JSR)
{
// Parse the single argument
var extra_words = [ ];
var a = this.ParseArgument(extra_words);
if (!a[0])
return false;
// Merge instruction details
word |= a[1] << 10;
}
}
else
{
// Parse both arguments
var a = this.ParseArgument(extra_words);
if (!a[0])
return false;
var token = this.Lexer.NextToken();
if (token[0] != dcpuTokens.COMMA)
{
this.Report.ExpectingToken(dcpuTokens.COMMA, token[0], this.Line);
return false;
}
var b = this.ParseArgument(extra_words);
if (!b[0])
return false;
// Merge instruction details
word |= a[1] << 4;
word |= b[1] << 10;
}
// Add to the generated code
this.WordCode.push(word);
for (var j in extra_words)
this.WordCode.push(extra_words[j]);
}
this.ParseLine = function(token)
{
switch (token[0])
{
// Skip comments and empty lines
case (dcpuTokens.COMMENT):
case (dcpuTokens.END):
break;
// Skip .size, .text, .data, etc directives
case (dcpuTokens.DOTDIRECTIVE):
break;
case (dcpuTokens.DATA):
this.ParseData();
break;
// Only accept labels and instructions
case (dcpuTokens.COLON):
this.ParseLabel();
break;
case (dcpuTokens.INSTRUCTION):
this.PCToLine[this.WordCode.length] = this.Line;
this.ParseInstruction(token);
break;
default:
this.Report.UnexpectedToken(token[0], this.Line);
break;
}
}
this.Assemble = function(asm)
{
// Reset assembler state
this.Lexer = new dcpuLexer(this.Report);
this.Labels = { };
this.LabelPatches = [ ];
this.WordCode = [ ];
this.Line = 0;
this.PCToLine = { };
// Split into lines and run the lexer on them individually
var lines = asm.split("\n");
for (var i in lines)
{
this.Line = parseInt(i) + 1;
this.Lexer.SetText(lines[i], this.Line);
var token = this.Lexer.NextToken();
this.ParseLine(token);
// Catch any second instructions
token = this.Lexer.NextToken();
if (token[0] != dcpuTokens.END && token[0] != dcpuTokens.COMMENT)
{
console.log(token);
this.ParseLine(token);
}
}
// Patch up all label references
for (var i in this.LabelPatches)
{
var patch = this.LabelPatches[i];
// Ensure the labels exist first!
var label_name = patch[0];
if (!(label_name in this.Labels))
{
this.Report.Error("Unresolved reference to label '" + label_name + "'", patch[2]);
continue;
}
// Set the offset in the byte code
var label_offset = this.Labels[label_name];
var reference_offset = patch[1];
this.WordCode[reference_offset] = label_offset;
}
}
}
// =====================================================================================================================
// DCPU-16 Emulator
// =====================================================================================================================
function dcpuEmulator()
{
this.Reset = function()
{
// Registers are mapped to >128k memory to make emulator code a little easier
this.WordMem = new Array(0x10000 + 8 + 3);
this.Registers = 0x10000;
this.PC = this.Registers + 8 + 0;
this.SP = this.Registers + 8 + 1;
this.O = this.Registers + 8 + 2;
// I can't believe I'm doing this... is there a better way?
for (var i = 0; i < this.WordMem.length; i++)
this.WordMem[i] = 0;
this.CodeLength = 0;
// Reset registers
this.WordMem[this.PC] = 0;
this.WordMem[this.SP] = 0xFFFF;
this.WordMem[this.O] = 0;
this.CurrentLine = 0;
}
// Initial reset on create
this.Reset();
this.UploadCode = function(word_code, pc_to_line)
{
this.Reset();
// Copy each word manually
this.CodeLength = word_code.length;
for (var i = 0; i < this.CodeLength; i++)
{
var word = word_code[i];
this.WordMem[i] = word;
}
// Debug info maps program counter to line
this.PCToLine = pc_to_line;
}
this.DecodeArgumentAddr = function(instr, shift)
{
// Get the correct argument
var arg = (instr >> shift) & 0x3F;
// Memory and registers are part of the same memory map, allowing this single function to return
// the address of both source and destinaton operands.
// Register values and immediate values in the code segment
if (arg < 0x08)
return this.Registers + arg;
if (arg < 0x10)
return this.WordMem[this.Registers + (arg - 0x08)];
if (arg < 0x18)
return this.WordMem[this.WordMem[this.PC]++] + this.WordMem[this.Registers + (arg - 0x10)];
// Stack values that CAN NOT be used as address operands; this is implied by the instruction
if (arg == dcpuRegisters.POP)
return this.WordMem[this.SP];
if (arg == dcpuRegisters.PEEK)
return this.WordMem[this.SP];
if (arg == dcpuRegisters.PUSH)
return --this.WordMem[this.SP];
// Raw control register values
if (arg == dcpuRegisters.SP)
return this.SP;
if (arg == dcpuRegisters.PC)
return this.PC;
if (arg == dcpuRegisters.O)
return this.O;
// Using immediate values as address operands
if (arg == 0x1E)
return this.WordMem[this.WordMem[this.PC]++];
// This is for immediate values. The spec mentions that writing to an immediate silently fails
// whereas this code will effectively write to the code segment.
if (arg == 0x1F)
return this.WordMem[this.PC]++
// TODO: 5-bit compact immediate values! (add another variable at the end of the mmap and use that)
}
// Accessors for each register
this.GetReg = function(r) { return this.WordMem[this.Registers + r]; }
this.GetA = function() { return this.GetReg(0); }
this.GetB = function() { return this.GetReg(1); }
this.GetC = function() { return this.GetReg(2); }
this.GetX = function() { return this.GetReg(3); }
this.GetY = function() { return this.GetReg(4); }
this.GetZ = function() { return this.GetReg(5); }
this.GetI = function() { return this.GetReg(6); }
this.GetJ = function() { return this.GetReg(7); }
this.GetPC = function() { return this.GetReg(8); }
this.GetSP = function() { return this.GetReg(9); }
this.GetO = function() { return this.GetReg(10); }
// Mutators for each register
this.SetReg = function(r, v) { this.WordMem[this.Registers + r] = v & 0xFFFF; }
this.SetA = function(v) { this.SetReg(0, v); }
this.SetB = function(v) { this.SetReg(1, v); }
this.SetC = function(v) { this.SetReg(2, v); }
this.SetX = function(v) { this.SetReg(3, v); }
this.SetY = function(v) { this.SetReg(4, v); }
this.SetZ = function(v) { this.SetReg(5, v); }
this.SetI = function(v) { this.SetReg(6, v); }
this.SetJ = function(v) { this.SetReg(7, v); }
this.SetPC = function(v) { this.SetReg(8, v); }
this.SetSP = function(v) { this.SetReg(9, v); }
this.SetO = function(v) { this.SetReg(10, v); }
// Simple instructions listed by index using their opcode - brittle but fast
this.SimpleInstructions = [
null,
function(a, b) { return b; },
function(a, b) { var r = a + b; this.SetO(r > 0xFFFF ? 1 : 0); return r; },
function(a, b) { var r = a - b; this.SetO(r < 0 ? 0xFFFF : 0); return r < 0 ? r + 0x10000 : r; },
function(a, b) { var r = a * b; this.SetO(r >> 16); return r; },
function(a, b) { if (b == 0) { this.SetO(0); return 0; } var r = a / b; this.SetO((a << 16) / b); return r; },
function(a, b) { var r = b == 0 ? 0: a % b; return r; },
function(a, b) { var r = a << b; this.SetO(r >> 16); return r; },
function(a, b) { var r = a >> b; this.SetO((a << 16) >> b); return r; },
function(a, b) { return a & b; },
function(a, b) { return a | b; },
function(a, b) { return a ^ b; },
function(a, b) { if (a != b) this.SkipNextInstruction = true; },
function(a, b) { if (a == b) this.SkipNextInstruction = true; },
function(a, b) { if (a <= b) this.SkipNextInstruction = true; },
function(a, b) { if ((a & b) == 0) this.SkipNextInstruction = true; }
]
this.SimpleOp = function(instr, op)
{
// Decode both arguments (forwards PC appropriately)
var a = this.DecodeArgumentAddr(instr, 4);
var b = this.DecodeArgumentAddr(instr, 10);
if (this.SkipNextInstruction)
{
this.SkipNextInstruction = false;
}
else
{
// Run the instruction and commit any needed results to the first argument
var r = op.apply(this, [ this.WordMem[a], this.WordMem[b] ]);
if (r != null)
this.WordMem[a] = r & 0xFFFF;
// Only pop requires post-modification of its value (for the moment), so do it here
var arg = (instr >> 10) & 0x3F;
if (arg == dcpuRegisters.POP)
this.WordMem[this.SP]++;
}
}
this.JSR = function(instr)
{
// Decode the only argument
var a = this.DecodeArgumentAddr(instr, 10);
if (this.SkipNextInstruction)
{
this.SkipNextInstruction = false;
}
else
{
// Push address of next instruction
var pc = this.WordMem[this.PC];
this.WordMem[--this.WordMem[this.SP]] = pc;
// Jump to subroutine
this.WordMem[this.PC] = this.WordMem[a];
}
}
this.DecodeInstruction = function(skip_breakpoint)
{
// Don't execute into the ether!
if (this.WordMem[this.PC] >= this.CodeLength)
return false;
// Get next instruction and leave on BRK
var instr = this.WordMem[this.WordMem[this.PC]];
if (instr == 0 && !skip_breakpoint)
return false;
this.WordMem[this.PC]++;
// Decode and execute simple instructions
var simple_op = instr & 0xF;
if (this.SimpleInstructions[simple_op])
this.SimpleOp(instr, this.SimpleInstructions[simple_op]);
// Only one complex op for the moment but that will no doubt change at a later date - update then
else if ((instr & 0x3F0) == dcpuOpcodes.JSR)
this.JSR(instr);
// Map back to the original line the next instruction is at
this.CurrentLine = this.PCToLine[this.WordMem[this.PC]];
return true;
}
}
function dcpuVideoDisplay()
{
this.GenerateCharacterSet = function(image)
{
// Create a canvas and blit the image into that
var buffer = document.createElement("canvas");
buffer.width = image.width;
buffer.height = image.height;
var ctx = buffer.getContext("2d");
ctx.drawImage(image, 0, 0);
// Get access to the pixels
var image_data = ctx.getImageData(0, 0, buffer.width, buffer.height);
var data = image_data.data;
var width_bytes = buffer.width * 4;
function SetColumn(data, width_bytes, offset_x, shift_offset)
{
var w = 0;
for (var y = 0, o = offset_x; y < 8; y++, o += width_bytes)
{
var p = data[o];
if (!p)
w |= 1 << (y + shift_offset);
}
return w;
}
// Set each column from the bitmap for each charactr
var wordmem = [ ];
for (var i = 0; i < buffer.width; i += 4)
{
var px = i * 4;
var w = SetColumn(data, width_bytes, px + 0, 0);
w |= SetColumn(data, width_bytes, px + 4, 8);
wordmem[(32 + i / 4) * 2] = w;
var w = SetColumn(data, width_bytes, px + 8, 0);
w |= SetColumn(data, width_bytes, px + 12, 8);
wordmem[(32 + i / 4) * 2 + 1] = w;
}
return wordmem;
}
this.UploadCharacterSet = function(emulator, wordmem)
{
for (var i = 0; i < wordmem.length; i++)
emulator.WordMem[0x9000 + i] = wordmem[i];
}
function DrawColumn(data, po, line_width, c, fg, bg)
{
for (var i = 0; i < 8; i++)
{
if (c & 1)
{
data[po+0] = dcpuVideoColors[fg].R;
data[po+1] = dcpuVideoColors[fg].G;
data[po+2] = dcpuVideoColors[fg].B;
}
else if (bg != 0) {
data[po+0] = dcpuVideoColors[bg].R;
data[po+1] = dcpuVideoColors[bg].G;
data[po+2] = dcpuVideoColors[bg].B;
}
po += line_width;
c >>= 1;
}
}
this.GenerateCanvas = function(emulator)
{
// Create the canvas on demand
if (!this.Canvas)
{
this.Canvas = document.createElement("canvas");
this.Canvas.width = 32 * 4;
this.Canvas.height = 12 * 8;
this.Canvas.Ctx = this.Canvas.getContext("2d");
}
// Clear the canvas