-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathz3.vl
1625 lines (1547 loc) · 43.1 KB
/
z3.vl
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
module boss(clk, adcDout, reset, data, waddress, we, pe, romCS, ramCS, led0, led1, led2, lcdCS, lcdRS, lcdReset, nadcCS, a17, a18);
input clk;
input adcDout;
input reset;
inout [7:0] data;
output [16:0] waddress;
output we;
output pe;
output romCS;
output ramCS;
output led0;
output led1;
output led2;
output lcdCS;
output lcdRS;
output lcdReset;
output nadcCS;
output a17;
output a18;
reg [16:0] address;
reg [7:0] dataOut;
reg printEnable;
reg writeEnable;
reg rlcdRS;
reg rlcdReset;
reg adcCS;
reg a17;
reg a18;
wire [16:0] waddress;
wire we;
wire pe;
wire romCS;
wire ramCS;
wire led0;
wire led1;
wire led2;
wire lcdCS;
wire lcdRS;
wire lcdReset;
wire nadcCS;
//`define HARDWARE_PRINT
//`define REAL_HARDWARE
`define CALLBACK_BASE 'h1E58B
`define INIT_CALLBACK 'h0
`define PRINT_CALLBACK 'h1
`define PRINTCHAR_CALLBACK 'h2
`define PRINTNUM_CALLBACK 'h3
`define READ_CALLBACK 'h4
`define STATUS_CALLBACK 'h5
`define QUIT_CALLBACK 'h6
`define EXCEPTION_CALLBACK 'h7
`define OPER_LARGE 2'b00
`define OPER_SMALL 2'b01
`define OPER_VARI 2'b10
`define OPER_OMIT 2'b11
`define STATE_RESET 0
`define STATE_FETCH_OP 1
`define STATE_READ_TYPES 2
`define STATE_READ_OPERS 3
`define STATE_READ_INDIRECT 4
`define STATE_READ_STORE 5
`define STATE_READ_BRANCH 6
`define STATE_DO_OP 7
`define STATE_READ_FUNCTION 8
`define STATE_CALL_FUNCTION 9
`define STATE_RET_FUNCTION 10
`define STATE_STORE_REGISTER 11
`define STATE_PRINT 12
`define STATE_DIVIDE 13
`define STATE_HALT 14
`define STATE_PRINT_CHAR 15
`define OP_0 2'b00
`define OP_1 2'b01
`define OP_2 2'b10
`define OP_VAR 2'b11
`define OP0_RTRUE 'h0
`define OP0_RFALSE 'h1
`define OP0_PRINT 'h2
`define OP0_PRINTRET 'h3
`define OP0_NOP 'h4
`define OP0_SAVE 'h5
`define OP0_RESTORE 'h6
`define OP0_RESTART 'h7
`define OP0_RETPOP 'h8
`define OP0_POP 'h9
`define OP0_QUIT 'hA
`define OP0_NEWLINE 'hB
`define OP0_SHOWSTATUS 'hC
`define OP0_VERIFY 'hD
`define OP0_DYNAMIC 'hF
`define OP1_JZ 'h0
`define OP1_GETSIBLING 'h1
`define OP1_GETCHILD 'h2
`define OP1_GETPARENT 'h3
`define OP1_GETPROPLEN 'h4
`define OP1_INC 'h5
`define OP1_DEC 'h6
`define OP1_PRINTADDR 'h7
`define OP1_SWITCHBANK 'h8
`define OP1_REMOVEOBJ 'h9
`define OP1_PRINTOBJ 'hA
`define OP1_RET 'hB
`define OP1_JUMP 'hC
`define OP1_PRINTPADDR 'hD
`define OP1_LOAD 'hE
`define OP1_NOT 'hF
`define OP2_JE 'h1
`define OP2_JL 'h2
`define OP2_JG 'h3
`define OP2_DEC_CHK 'h4
`define OP2_INC_CHK 'h5
`define OP2_JIN 'h6
`define OP2_TEST 'h7
`define OP2_OR 'h8
`define OP2_AND 'h9
`define OP2_TESTATTR 'hA
`define OP2_SETATTR 'hB
`define OP2_CLEARATTR 'hC
`define OP2_STORE 'hD
`define OP2_INSERTOBJ 'hE
`define OP2_LOADW 'hF
`define OP2_LOADB 'h10
`define OP2_GETPROP 'h11
`define OP2_GETPROPADDR 'h12
`define OP2_GETNEXTPROP 'h13
`define OP2_ADD 'h14
`define OP2_SUB 'h15
`define OP2_MUL 'h16
`define OP2_DIV 'h17
`define OP2_MOD 'h18
`define OP2_WRITEREG 'h1E
`define OPVAR_CALL 'h0
`define OPVAR_STOREW 'h1
`define OPVAR_STOREB 'h2
`define OPVAR_PUTPROP 'h3
`define OPVAR_SREAD 'h4
`define OPVAR_PRINTCHAR 'h5
`define OPVAR_PRINTNUM 'h6
`define OPVAR_RANDOM 'h7
`define OPVAR_PUSH 'h8
`define OPVAR_PULL 'h9
`define OPVAR_GETTOUCH 'h1E
`define OPVAR_BLIT1 'h1F
`define PRINTEFFECT_FETCH 0
`define PRINTEFFECT_FETCHAFTER 1
`define PRINTEFFECT_RET1 2
`define PRINTEFFECT_ABBREVRET 3
reg forceStaticRead;
reg forceDynamicRead;
reg [3:0] state;
reg [3:0] phase;
reg [16:0] pc;
reg [16:0] curPC;
reg [15:0] globalsAddress;
reg [15:0] objectTable;
reg readHigh;
reg [4:0] op;
reg [1:0] operNum;
reg [1:0] operTypes [4:0];
reg [15:0] operand [3:0];
reg [2:0] operandIdx;
reg [13:0] branch;
reg [7:0] store;
reg negate;
reg delayedBranch;
reg divideAddOne;
reg [15:0] returnValue;
reg [6:0] opsToRead;
reg [6:0] currentLocal;
reg [16:0] csStack;
reg [15:0] stackAddress;
reg [16:0] temp;
reg [15:0] random;
reg [1:0] alphabet;
reg [1:0] long;
reg nextLoadIsDynamic;
reg lastWriteWasDraw;
reg adcClk;
reg adcDin;
reg [7:0] cachedReg;
reg [15:0] cachedValue;
// xp<=adcConfig[0]?0:'bZ;
// yp<=adcConfig[1]?1:'bZ;
// xm<=adcConfig[2]?1:'bZ;
// ym<=adcConfig[3]?0:'bZ;
// XP dataOut[6]
// XM lcsRS
// YP dataOut[7]
// YM lcdWR
initial
begin
state=`STATE_RESET;
forceDynamicRead=0;
forceStaticRead=0;
writeEnable=0;
printEnable=0;
phase=0;
readHigh=0;
nextLoadIsDynamic=0;
adcCS=0;
a17=0;
a18=0;
cachedReg=0;
cachedValue=0;
lastWriteWasDraw=0;
end
assign waddress[16:2] = address[16:2];
assign waddress[0] = adcCS?adcClk:address[0];
assign waddress[1] = adcCS?adcDin:address[1];
assign data[5:0] = (writeEnable || printEnable)?dataOut[5:0]:6'bZ;
assign data[6] = (writeEnable || printEnable)?dataOut[6]:((adcCS && operand[0][0])?0:'bZ);
assign data[7] = (writeEnable || printEnable)?dataOut[7]:((adcCS && operand[0][1])?1:'bZ);
assign romCS = !(!adcCS && !printEnable && !writeEnable && !forceDynamicRead && (forceStaticRead || address[16]==1));
assign ramCS = !(!adcCS && !printEnable && (writeEnable || forceDynamicRead || (!forceStaticRead && address[16]==0)));
assign pe = adcCS?(operand[0][3]?0:'bZ):(!printEnable || !clk);
assign we = !writeEnable;
assign led0 = !address[12];
assign led1 = !address[13];
assign led2 = !address[14];
assign lcdCS = !printEnable || !clk;
assign lcdRS = adcCS?(operand[0][2]?1:'bZ):rlcdRS;
assign lcdReset = rlcdReset;
assign nadcCS = !adcCS;
task StoreB;
begin
if (phase==0) begin
cachedReg<=15; nextLoadIsDynamic<=0; address<=operand[0]+operand[1]; writeEnable<=1; dataOut<=operand[2][7:0]; phase<=phase+1;
end else begin
address<=pc; writeEnable<=0; state<=`STATE_FETCH_OP;
end
end
endtask
task StoreW;
begin
case (phase)
0: begin cachedReg<=15; nextLoadIsDynamic<=0; address<=operand[0]+operand[1]*2; writeEnable<=1; dataOut<=operand[2][15:8]; phase<=phase+1; end
1: begin address<=address+1; dataOut<=operand[2][7:0]; phase<=phase+1; end
default: begin address<=pc; writeEnable<=0; state<=`STATE_FETCH_OP; end
endcase
end
endtask
task StoreRegisterAndBranch;
input [7:0] regNum;
input [15:0] value;
input doBranch;
begin
state<=`STATE_STORE_REGISTER;
store<=regNum;
returnValue<=value;
delayedBranch<=doBranch;
phase<=0;
end
endtask
task StoreResultAndBranch;
input [15:0] result;
input doBranch;
begin
if (store>=16) begin
address<=globalsAddress+2*(store-16);
end else if (store==0) begin
address<=2*stackAddress;
stackAddress<=stackAddress+1;
end else begin
address<=csStack+8+2*(store-1);
end
dataOut<=result[15:8];
writeEnable<=1;
state<=`STATE_STORE_REGISTER;
returnValue[7:0]<=result[7:0];
delayedBranch<=doBranch;
phase<=1;
end
endtask
task StoreResult;
input [15:0] result;
begin
StoreResultAndBranch(result, negate); // negate means won't branch
end
endtask
task StoreResultSlow;
input [15:0] result;
begin
StoreRegisterAndBranch(store, result, negate); // negate means won't branch
end
endtask
task ReturnFunction;
input [15:0] value;
begin
returnValue<=value;
phase<=0;
state<=`STATE_RET_FUNCTION;
end
endtask
task CallFunction;
input doubleReturn;
input noStoreOnReturn;
begin
negate<=doubleReturn;
delayedBranch<=noStoreOnReturn;
phase<=0;
state<=`STATE_CALL_FUNCTION;
end
endtask
task DoBranch;
input result;
begin
if ((!result)==negate) begin
if (branch==0) begin
ReturnFunction(0);
end else if (branch==1) begin
ReturnFunction(1);
end else begin
pc<=$signed(pc)+$signed(branch)-2;
address<=$signed(pc)+$signed(branch)-2;
state<=`STATE_FETCH_OP;
end
end else begin
address<=pc;
state<=`STATE_FETCH_OP;
end
end
endtask
task LoadAndStore;
input [16:0] loadAddress;
input word;
begin
if (phase==0) begin
store<=data;
forceDynamicRead<=nextLoadIsDynamic;
nextLoadIsDynamic<=0;
address<=loadAddress;
temp[7:0]<=0;
phase<=word?phase+1:2;
end else if (phase==1) begin
temp[7:0]<=data;
address<=address+1;
phase<=phase+1;
end else begin
forceDynamicRead<=0;
StoreResultSlow((temp[7:0]<<8)|data);
end
end
endtask
function [16:0] GetObjectAddr;
input [7:0] object;
begin
GetObjectAddr=objectTable+2*31+9*(object-1);
end
endfunction
task TestAttr;
begin
if (phase==0) begin
address<=GetObjectAddr(operand[0])+(operand[1]/8); phase<=phase+1;
end else begin
DoBranch(data[7-(operand[1]&7)]);
end
end
endtask
task SetAttr;
begin
case (phase)
0: begin address<=GetObjectAddr(operand[0])+(operand[1]/8); phase<=phase+1; end
1: begin dataOut<=data|(1<<(7-(operand[1]&7))); writeEnable<=1; phase<=phase+1; end
default: begin writeEnable<=0; address<=pc; state=`STATE_FETCH_OP; end
endcase
end
endtask
task ClearAttr;
begin
case (phase)
0: begin address<=GetObjectAddr(operand[0])+(operand[1]/8); phase<=phase+1; end
1: begin dataOut<=data&(~(1<<(7-(operand[1]&7)))); writeEnable<=1; phase<=phase+1; end
default: begin writeEnable<=0; address<=pc; state=`STATE_FETCH_OP; end
endcase
end
endtask
task JumpIfParent;
begin
if (phase==0) begin
address<=GetObjectAddr(operand[0])+4; phase<=phase+1;
end else begin
DoBranch(operand[1]==data);
end
end
endtask
task GetRelative;
input [1:0] offset;
input branch;
begin
if (phase==0) begin
address<=GetObjectAddr(operand[0])+4+offset; phase<=phase+1;
end else begin
StoreResultAndBranch(data, branch?data!=0:negate);
end
end
endtask
task FindProp;
begin
case (phase[2:0])
0: begin store<=data; address<=GetObjectAddr(operand[0])+7; phase<=phase+1; end
1: begin temp[16]<=0; temp[15:8]<=data; temp[7:0]<=0; address<=address+1; phase<=phase+1; end
2: begin address<=temp|data; phase<=phase+1; end
3: begin address<=address+2*data+1; phase<=phase+1; end
4: begin
if (data==0) begin // end of search (get default)
address<=objectTable+2*(operand[1]-1);
phase<=phase+1;
end else if (data[4:0]==operand[1]) begin // found property
address<=address+1;
if (data[7:5]==0) // only 1 byte
phase<=6;
else
phase<=5;
end else begin // skip over data
address<=address+data[7:5]+2;
end
end
5: begin temp[7:0]<=data; address<=address+1; phase<=phase+1; end
default: begin StoreResultSlow((temp[7:0]<<8)|data); end
endcase
end
endtask
task SetProp;
begin
case (phase[2:0])
0: begin address<=GetObjectAddr(operand[0])+7; phase<=phase+1; end
1: begin temp[16]<=0; temp[15:8]<=data; temp[7:0]<=0; address<=address+1; phase<=phase+1; end
2: begin address<=temp|data; phase<=phase+1; end
3: begin address<=address+2*data+1; phase<=phase+1; end
4: begin
`ifndef REAL_HARDWARE
if (data==0) begin // end of search
state<=`STATE_HALT;
end else
`endif
if (data[4:0]==operand[1]) begin // found property
if (data[7:5]==0) begin // only 1 byte
dataOut<=operand[2][7:0];
phase<=6;
address<=address+1;
writeEnable<=1;
end else begin
dataOut<=operand[2][15:8];
phase<=5;
address<=address+1;
writeEnable<=1;
end
end else begin // skip over data
address<=address+data[7:5]+2;
end
end
5: begin dataOut<=operand[2][7:0]; address<=address+1; phase<=phase+1; end
default: begin state<=`STATE_FETCH_OP; address<=pc; writeEnable<=0; end
endcase
end
endtask
task FindPropAddrLen;
begin
case (phase[2:0])
0: begin store<=data; address<=GetObjectAddr(operand[0])+7; phase<=phase+1; end
1: begin temp[16]<=0; temp[15:8]<=data; temp[7:0]<=0; address<=address+1; phase<=phase+1; end
2: begin address<=temp|data; phase<=phase+1; end
3: begin address<=address+2*data+1; phase<=phase+1; end
default: begin
if (data==0) begin // end of search
StoreResultSlow(0);
end else if (data[4:0]==operand[1]) begin // found property
StoreResultSlow(address+1);
end else begin // skip over data
address<=address+data[7:5]+2;
end
end
endcase
end
endtask
task FindNextProp;
begin
case (phase[2:0])
0: begin store<=data; address<=GetObjectAddr(operand[0])+7; phase<=phase+1; end
1: begin temp[16]<=0; temp[15:8]<=data; temp[7:0]<=0; address<=address+1; phase<=phase+1; end
2: begin address<=temp|data; phase<=phase+1; end
3: begin address<=address+2*data+1; phase<=phase+1; end
default: begin
if (operand[1]==0)
StoreResultSlow(data[4:0]);
`ifndef REAL_HARDWARE
else if (data==0) // end of search
state<=`STATE_HALT;
`endif
else begin // skip over data
if (data[4:0]==operand[1])
operand[1]<=0;
address<=address+data[7:5]+2;
end
end
endcase
end
endtask
task GetPropLen;
begin
if (phase==0) begin
address<=operand[0]-1; phase<=phase+1;
end else begin
StoreResultSlow((operand[0]==0)?0:(data[7:5]+1));
end
end
endtask
task Pull;
input return;
begin
case (phase)
0: begin
address<=2*(stackAddress-1);
forceDynamicRead<=1;
phase<=phase+1;
end
1: begin
temp[7:0]<=data;
stackAddress<=address>>1;
address<=address+1;
phase<=phase+1;
end
default: begin
forceDynamicRead<=0;
if (return) begin
ReturnFunction((temp[7:0]<<8)|data);
end else begin
if (operand[0]==0)
stackAddress<=stackAddress-1;
StoreRegisterAndBranch(operand[0], (temp[7:0]<<8)|data, negate);
end
end
endcase
end
endtask
task Print;
input [16:0] addr;
input [1:0] effect;
begin
temp<=addr;
state<=`STATE_PRINT;
returnValue[1:0]<=effect;
delayedBranch<=0;
phase<=0;
end
endtask
task PrintObj;
begin
case (phase[1:0])
0: begin address<=GetObjectAddr(operand[0])+7; phase<=phase+1; end
1: begin store<=data; address<=address+1; phase<=phase+1; end
default: Print((store<<8)+data+1, `PRINTEFFECT_FETCH);
endcase
end
endtask
task RemoveObject;
begin
case (phase[3:0])
0: begin address<=GetObjectAddr(operand[0])+4; /*obj.parent*/ phase<=phase+1; end
1: begin
if (data==0) begin
phase<=(operand[1]!=0)?6:9;
end else begin
phase<=phase+1;
end
temp[15:8]<=data;
writeEnable<=1;
dataOut<=operand[1];
end
2: begin writeEnable<=0; address<=address+1; /*obj.sibling*/ phase<=phase+1; end
3: begin temp[7:0]<=data; writeEnable<=1; dataOut<=0; phase<=phase+1; end
4: begin writeEnable<=0; address<=GetObjectAddr(temp[15:8])+6; /*parent.child*/ phase<=phase+1; end
5: begin
if (data==operand[0]) begin // found object
dataOut<=temp[7:0];
writeEnable<=1;
phase<=(operand[1]!=0)?phase+1:9;
end else begin
address<=GetObjectAddr(data)+5; /*follow sibling*/
end
end
6: begin address<=GetObjectAddr(operand[1])+6; writeEnable<=0; phase<=phase+1; end
7: begin temp[7:0]<=data; writeEnable<=1; dataOut<=operand[0]; phase<=phase+1; end
8: begin address<=GetObjectAddr(operand[0])+5; dataOut<=temp[7:0]; phase<=phase+1; end
default: begin
writeEnable<=0;
state<=`STATE_FETCH_OP;
address<=pc;
end
endcase
end
endtask
task Random;
begin
case (phase)
0: begin if ($signed(operand[0])<0) begin random<=operand[0]; state<=`STATE_FETCH_OP; end else begin random<=random^(random<<13); end phase<=phase+1; end
1: begin random<=random^(random>>9); phase<=phase+1; end
2: begin random<=random^(random<<7); phase<=phase+1; end
default: begin state<=`STATE_DIVIDE; delayedBranch<=1; divideAddOne<=1; operand[1]<=operand[0]; operand[0]<=random&'h7FFF; phase<=0; end
endcase
end
endtask
task PrintNum;
begin
`ifdef HARDWARE_PRINT
case (phase)
0: begin negate<=0; operand[1]<=1; operand[2][2:0]<=2; operand[3][3:0]<=0; phase<=phase+1; end
1,2,3,4: begin operand[1]<=(operand[1]<<3)+(operand[1]<<1); printEnable<=0; phase<=phase+1; end
5: begin
if (operand[0]>=operand[1]) begin
operand[0]<=operand[0]-operand[1];
operand[3][3:0]<=operand[3][3:0]+1;
negate<=1;
printEnable<=0;
end else begin
printEnable<=negate;
dataOut<=operand[3][3:0]+48;
operand[3][3:0]<=0;
operand[1]<=1;
operand[2][2:0]<=operand[2][2:0]+1;
phase<=operand[2][2:0];
end
end
default: begin printEnable<=0; state<=`STATE_FETCH_OP; end
endcase
`else
operand[0]<=`PRINTNUM_CALLBACK;
operand[1]<=operand[0];
operTypes[1]<=`OPER_LARGE;
CallFunction(0, 1);
`endif
end
endtask
task PrintChar;
input [7:0] char;
begin
$display("Print char: %d\n", char);
`ifdef HARDWARE_PRINT
printEnable<=1;
dataOut<=char;
state<=`STATE_PRINT_CHAR;
`else
operand[0]<=`PRINTCHAR_CALLBACK;
operand[1]<=char;
operTypes[1]<=`OPER_LARGE;
CallFunction(0, 1);
`endif
end
endtask
task SRead;
begin
operand[0]<=`READ_CALLBACK;
operand[2]<=operand[0]; // Am swapping arguments just to save space on FPGA
operTypes[2]<=`OPER_LARGE;
CallFunction(0, 1);
end
endtask
task WriteReg;
begin
case (phase)
0: begin lastWriteWasDraw<=(operand[0]=='h22); if (operand[0]=='h22 && lastWriteWasDraw) begin rlcdRS<=1; phase<=4; end else begin rlcdRS<=0; phase<=phase+1; end end
1: begin dataOut<=operand[0][15:8]; printEnable<=1; phase<=phase+1; end
2: begin dataOut<=operand[0][7:0]; if (operand[0]==0) phase<=4; else phase<=phase+1; end
3: begin rlcdRS<=1; printEnable<=0; phase<=phase+1; end
4: begin dataOut<=operand[1][15:8]; printEnable<=1; phase<=phase+1; end
5: begin dataOut<=operand[1][7:0]; phase<=phase+1; end
default: begin printEnable<=0; address<=pc; state<=`STATE_FETCH_OP; end
endcase
end
endtask
task Blit1;
begin
case (phase)
0: begin forceDynamicRead<=1; rlcdRS<=0; phase<=phase+1; end
1: begin dataOut<=0; printEnable<=1; phase<=phase+1; end
2: begin address<=operand[0]*2; dataOut<=34; returnValue<=0; phase<=phase+1; end
3: begin
rlcdRS<=1;
printEnable<=0;
if (operand[1]==0) begin
forceDynamicRead<=0;
address<=pc;
state<=`STATE_FETCH_OP;
end
phase<=phase+1;
end
4: begin
store<=data;
dataOut<=data[7]?operand[3][15:8]:operand[2][15:8];
printEnable<=1;
phase<=6;
end
5: begin
dataOut<=store[7]?operand[3][15:8]:operand[2][15:8];
printEnable<=1;
phase<=phase+1;
end
default: begin
dataOut<=store[7]?operand[3][7:0]:operand[2][7:0];
store[7:1]=store[6:0];
returnValue<=returnValue+1;
if (returnValue[2:0]==7) begin
operand[1]=operand[1]-1;
phase<=3;
address<=address+1;
end else begin
phase<=5;
end
end
endcase
end
endtask
task GetTouch;
begin
case (phase)
0: begin
adcCS<=1;
adcClk<=0;
adcDin<=1;
operand[1]<='hffff;
operand[2]<=1;
phase<=1;
end
1: begin
operand[2][3:0]<=operand[2][3:0]+1;
if (operand[2][3:0]==0) begin // Max clock rate for ADC is ~ 1MHz so divide our clock by 16
adcClk<=1;
operand[1]<=(operand[1]<<1)+adcDout;
phase<=(!operand[1][9])?3:2;
end
end
2: begin
operand[2][3:0]<=operand[2][3:0]+1;
if (operand[2][3:0]==0) begin // Max clock rate for ADC is ~ 1MHz so divide our clock by 16
operand[0][8:4]<=(operand[0][8:4]>>1);
adcDin<=operand[0][4];
adcClk<=0;
phase<=1;
end
end
default: begin adcCS<=0; StoreResultSlow(operand[1]&'h3FF); end
endcase
end
endtask
task DoOp;
begin
case (operNum)
`OP_0: begin
if (phase==0)
$display("PC:%h Doing op0:%d Store:%h Branch:%h/%d", curPC, op, operand[0], store, branch, negate);
case (op[3:0])
`OP0_RTRUE: ReturnFunction(1);
`OP0_RFALSE: ReturnFunction(0);
`OP0_PRINT: Print(pc, `PRINTEFFECT_FETCHAFTER);
`OP0_PRINTRET: Print(pc, `PRINTEFFECT_RET1);
`OP0_SAVE,`OP0_RESTORE: DoBranch(0);
`OP0_RESTART: state<=`STATE_RESET;
`OP0_RETPOP: Pull(1);
`OP0_QUIT: begin operand[0]<=`QUIT_CALLBACK; CallFunction(0,1); end
`OP0_NEWLINE: PrintChar(10);
`OP0_SHOWSTATUS: begin operand[0]<=`STATUS_CALLBACK; CallFunction(0,1); end
`OP0_VERIFY: DoBranch(1);
default: state<=`STATE_HALT;
endcase
end
`OP_1: begin
if (phase==0)
$display("PC:%h Doing op1:%d Operands:%h Store:%h Branch:%h/%d", curPC, op, operand[0], store, branch, negate);
case (op[3:0])
`OP1_JZ: DoBranch(operand[0]==0);
`OP1_GETSIBLING: GetRelative(1,1);
`OP1_GETCHILD: GetRelative(2,1);
`OP1_GETPARENT: GetRelative(0,0);
`OP1_GETPROPLEN: GetPropLen();
`OP1_INC: StoreResult(operand[0]+1);
`OP1_DEC: StoreResult(operand[0]-1);
`OP1_PRINTADDR: Print(operand[0], `PRINTEFFECT_FETCH);
`OP1_SWITCHBANK: begin a17<=operand[0][0]; a18<=operand[0][1]; state<=`STATE_RESET; end
`OP1_REMOVEOBJ: begin operNum<=`OP_2; op<=`OP2_INSERTOBJ; operand[1]<=0; end
`OP1_PRINTOBJ: PrintObj();
`OP1_RET: ReturnFunction(operand[0]);
`OP1_JUMP: begin pc<=$signed(pc)+$signed(operand[0])-2; address<=$signed(pc)+$signed(operand[0])-2; state<=`STATE_FETCH_OP; end
`OP1_PRINTPADDR: Print(2*operand[0], `PRINTEFFECT_FETCH);
`OP1_LOAD: StoreResult(operand[0]);
`OP1_NOT: StoreResult(~operand[0]);
default: state<=`STATE_HALT;
endcase
end
`OP_2: begin
if (phase==0)
$display("PC:%h Doing op2:%d Operands:%h %h Store:%h Branch:%h/%d/%h", curPC, op, operand[0], operand[1], store, branch, negate, $signed(pc)+$signed(branch-2));
case (op)
`OP2_JE: DoBranch(operand[0]==operand[1] || (operTypes[2]!=`OPER_OMIT && operand[0]==operand[2]) || (operTypes[3]!=`OPER_OMIT && operand[0]==operand[3]));
`OP2_JL: DoBranch($signed(operand[0])<$signed(operand[1]));
`OP2_JG: DoBranch($signed(operand[0])>$signed(operand[1]));
`OP2_INC_CHK: StoreResultAndBranch(operand[0]+1, $signed(operand[0])+1>$signed(operand[1]));
`OP2_DEC_CHK: StoreResultAndBranch(operand[0]-1, $signed(operand[0])-1<$signed(operand[1]));
`OP2_JIN: JumpIfParent();
`OP2_TEST: DoBranch((operand[0]&operand[1])==operand[1]);
`OP2_OR: StoreResult(operand[0]|operand[1]);
`OP2_AND: StoreResult(operand[0]&operand[1]);
`OP2_TESTATTR: TestAttr();
`OP2_SETATTR: SetAttr();
`OP2_CLEARATTR: ClearAttr();
`OP2_STORE: begin if (operand[0]==0) stackAddress<=stackAddress-1; StoreRegisterAndBranch(operand[0], operand[1], negate); end
`OP2_INSERTOBJ: RemoveObject();
`OP2_LOADW: LoadAndStore(operand[0]+2*operand[1], 1);
`OP2_LOADB: LoadAndStore(operand[0]+operand[1], 0);
`OP2_GETPROP: FindProp();
`OP2_GETPROPADDR: FindPropAddrLen();
`OP2_GETNEXTPROP: FindNextProp();
`OP2_ADD: StoreResult($signed(operand[0])+$signed(operand[1]));
`OP2_SUB: StoreResult($signed(operand[0])-$signed(operand[1]));
`OP2_MUL: StoreResult($signed(operand[0])*$signed(operand[1]));
`OP2_DIV: begin store<=data; state=`STATE_DIVIDE; delayedBranch<=0; divideAddOne<=0; end
`OP2_MOD: begin store<=data; state=`STATE_DIVIDE; delayedBranch<=1; divideAddOne<=0; end
`OP2_WRITEREG: WriteReg();
default: state<=`STATE_HALT;
endcase
end
default: begin // 'OP_VAR
if (phase==0) begin
if (operTypes[0]==`OPER_OMIT)
$display("PC:%h Doing opvar:%d Operands: Store:%h Branch:%h/%d/%h", curPC, op, store, branch, negate, $signed(pc)+$signed(branch-2));
else if (operTypes[1]==`OPER_OMIT)
$display("PC:%h Doing opvar:%d Operands:%h Store:%h Branch:%h/%d/%h", curPC, op, operand[0], store, branch, negate, $signed(pc)+$signed(branch-2));
else if (operTypes[2]==`OPER_OMIT)
$display("PC:%h Doing opvar:%d Operands:%h %h Store:%h Branch:%h/%d/%h", curPC, op, operand[0], operand[1], store, branch, negate, $signed(pc)+$signed(branch-2));
else if (operTypes[3]==`OPER_OMIT)
$display("PC:%h Doing opvar:%d Operands:%h %h %h Store:%h Branch:%h/%d/%h", curPC, op, operand[0], operand[1], operand[2], store, branch, negate, $signed(pc)+$signed(branch-2));
else
$display("PC:%h Doing opvar:%d Operands:%h %h %h %h Store:%h Branch:%h/%d/%h", curPC, op, operand[0], operand[1], operand[2], operand[3], store, branch, negate, $signed(pc)+$signed(branch-2));
end
case (op)
`OPVAR_CALL: if (operand[0]==0) StoreResultSlow(0); else CallFunction(0, 0);
`OPVAR_STOREW: StoreW();
`OPVAR_STOREB: StoreB();
`OPVAR_PUTPROP: SetProp();
`OPVAR_SREAD: SRead();
`OPVAR_PRINTCHAR: PrintChar(operand[0]);
`OPVAR_PRINTNUM: PrintNum();
`OPVAR_RANDOM: Random();
`OPVAR_PUSH: StoreRegisterAndBranch(0, operand[0], negate);
`OPVAR_PULL: Pull(0);
`OPVAR_GETTOUCH: GetTouch();
`OPVAR_BLIT1: Blit1();
default: state<=`STATE_HALT;
endcase
end
endcase
end
endtask
`ifdef HARDWARE_PRINT
task DoPrint;
input [4:0] char;
begin
if (delayedBranch) begin
store[4:0]<=char;
operand[1]<=phase+1;
delayedBranch<=0;
phase<=6;
end else if (long==1) begin
operand[3][4:0]=char;
long<=long+1;
end else if (long==2) begin
printEnable<=1;
dataOut<=(operand[3][4:0]<<5)|char;
long<=0;
end else begin
phase<=phase+1;
if (char==0) begin
printEnable<=1;
dataOut<=32;
end else if (char==4 || char==5) begin
printEnable<=0;
alphabet=char-3;
end else if (char>=6) begin
printEnable<=!(alphabet==2 && char==6);
if (alphabet==2) begin
case (char)
6: long<=1;
7: dataOut<=10;
8,9,10,11,12,13,14,15,16,17: dataOut<=char-8+48;
18: dataOut<=46;
19: dataOut<=44;
20: dataOut<=33;
21: dataOut<=63;
22: dataOut<=95;
23: dataOut<=35;
24: dataOut<=39;
25: dataOut<=34;
26: dataOut<=47;
27: dataOut<=92;
28: dataOut<=45;
29: dataOut<=58;
30: dataOut<=40;
31: dataOut<=41;
endcase
end else begin
dataOut<=char-6+((alphabet==0)?97:65);
end
alphabet<=0;
end else begin
printEnable<=0;
store[7:5]<=(char-1);
delayedBranch<=1;
end
end
end
endtask
`endif
task FinishReadingOps;
begin
case (operNum)
`OP_1: begin
case (op[3:0])
`OP1_INC, `OP1_DEC, `OP1_LOAD: state<=`STATE_READ_INDIRECT;
`OP1_GETSIBLING,`OP1_GETCHILD,`OP1_GETPARENT,`OP1_GETPROPLEN,`OP1_NOT: state<=`STATE_READ_STORE;
`OP1_JZ: state<=`STATE_READ_BRANCH;
default: state<=`STATE_DO_OP;
endcase
pc<=pc+1;
end
`OP_2: begin
case (op[4:0])
`OP2_INC_CHK,`OP2_DEC_CHK: begin pc<=pc+1; state<=`STATE_READ_INDIRECT; end
`OP2_OR,`OP2_AND,`OP2_ADD,`OP2_SUB,`OP2_MUL: begin pc<=pc+1; state<=`STATE_READ_STORE; end
`OP2_LOADW,`OP2_LOADB,`OP2_GETPROP ,`OP2_GETPROPADDR,`OP2_GETNEXTPROP,`OP2_DIV,`OP2_MOD: begin pc<=pc+2; state<=`STATE_DO_OP; end
`OP2_JE,`OP2_JL,`OP2_JG,`OP2_JIN,`OP2_TEST,`OP2_TESTATTR: begin pc<=pc+1; state<=`STATE_READ_BRANCH; end
default: begin pc<=pc+1; state<=`STATE_DO_OP; end
endcase
end