-
Notifications
You must be signed in to change notification settings - Fork 1
/
v12_unreadable.king.js
executable file
·1912 lines (1903 loc) · 173 KB
/
v12_unreadable.king.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
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 TI68kEmulatorCoreModule(stdlib){
"use strict";
var state=new Object();
state.version=1;
state.d0=0;
state.d1=0;
state.d2=0;
state.d3=0;
state.d4=0;
state.d5=0;
state.d6=0;
state.d7=0;
state.a0=0;
state.a1=0;
state.a2=0;
state.a3=0;
state.a4=0;
state.a5=0;
state.a6=0;
state.a7=0;
state.a8=0;
state.sr=0;
state.prev_pc=0;
state.pc=0;
state.pending_ints=0;
state.address_error_access_type=0;
state.address_error_address=0;
state.current_instruction=0;
state.reading_instruction=0;
state.rom=false;
state.ram=new Uint16Array(131072);
var cpu=new Object();
cpu.t=new Array(65536);
cpu.n=new Array(65536);
cpu.cycles=new Uint8Array(65536);
state.unhandled_count=0;
var main_interval_timer_id=0;
state.main_interval_timer_interval=11;
state.tracecount=0;
state.cycle_count=0;
state.overall=2500;
state.osc2_counter=0;
state.frames_counted=0;
state.total_time=0;
var newromready=false;
var newfileready=false;
var newflashfileready=false;
var ui=false;var link=false;
state.erase_ram_upon_reset=true;
state.port_600000=0x04;
state.vectorprotect=false;
state.wakemask=0;
state.lcd_address_high=9;
state.lcd_address_low=0x80;
state.lcd_address=0x4c00;
state.screen_width=240;
state.screen_height=128;
state.interrupt_control=0x1B;
state.interrupt_rate=0x200;
state.timer_min=0xB2;
state.timer_current=0;
state.keystatus=new Uint8Array(80);
state.keymaskhigh=0xFF;
state.keymasklow=0xFF;
state.port_60001A=0x02;
state.port_60001B=0;
state.port_60001C=0x01;
state.port_60001D=0x8D;
state.port_700017=0;
state.port_70001D=0;
state.port_70001F=0;
state.stopped=false;
state.hardware_model=1;
state.calculator_model=1;
state.pedrom=false;
state.punix=false;
state.jmp_tbl=0;
state.ROM_base=0;
state.FlashMemorySize=0;
state.large_flash_memory=false;
state.Protection_enabled=false;
var enable_kludge_in_lea_d_pc_a0=true;
state.hex_prefix="$";
state.flash_write_ready=0;
state.flash_write_phase=0x50;
state.flash_ret_or=0;
function save_state()
{var state=new Object();state.apiversion=apiversion();state.emu=_save_state();state.link=link._save_state();state.ui=ui._save_state();}
function _save_state()
{var emustate=new Object();return emustate;}
function restore_state(state)
{if(typeof(state)==="object"&&typeof(state.emu)==="object"&&typeof(state.link)==="object"&&typeof(state.ui)==="object"){stdlib.clearInterval(main_interval_timer_id);if(state.emu.apiversion!==apiversion){stdlib.console.log("API version has changed, funny results may occur...");}
_restore_state(state.emu);link._restore_state(state.link);ui._restore_state(state.ui);}
else{stdlib.console.log("Refusing to restore state from something not an object / from an object without the expected sub-objects");}}
function _restore_state(emustate)
{}
var hex_digits='0123456789ABCDEF';function to_hex(number,digits)
{var s="";if(number<0)
{number=-number;digits--;s="-";}
while(digits--)
{var digit=number%16;number=(number-digit)/16;s=hex_digits[digit]+s;}
return s;}
function to_hex2(number,digits)
{var s="";while(digits--)
{var digit=number&15;number=number>>4;s=hex_digits[digit]+s;}
return s;}
var rb_1_normal=function(address){return 0x14;}
var rb_1_flashspecial=function(address){return 0x14;}
var rw_1_normal=function(address){return 0x1400;}
var rw_1_flashspecial=function(address){return 0x1400;}
var wb_1_normal=function(address,value){}
var ww_1_normal=function(address,value){}
var ww_1_flashspecial=function(address,value){}
var rb_3_normal=function(address){return 0x14;}
var rb_3_flashspecial=function(address){return 0x14;}
var rw_3_normal=function(address){return 0x1400;}
var rw_3_flashspecial=function(address){return 0x1400;}
var wb_3_normal=function(address,value){}
var ww_3_normal=function(address,value){}
var ww_3_flashspecial=function(address,value){}
var rb_8_normal=function(address){return 0x14;}
var rb_8_flashspecial=function(address){return 0x14;}
var rw_8_normal=function(address){return 0x1400;}
var rw_8_flashspecial=function(address){return 0x1400;}
var wb_8_normal=function(address,value){}
var ww_8_normal=function(address,value){}
var ww_8_flashspecial=function(address,value){}
var rb_9_normal=function(address){return 0x14;}
var rb_9_flashspecial=function(address){return 0x14;}
var rw_9_normal=function(address){return 0x1400;}
var rw_9_flashspecial=function(address){return 0x1400;}
var wb_9_normal=function(address,value){}
var ww_9_normal=function(address,value){}
var ww_9_flashspecial=function(address,value){}
var rb=function(address){return 0x14;}
var rw=function(address){return 0x1400;}
var wb=function(address,value){}
var ww=function(address,value){}
function memory_dump(address,size,stride)
{address&=0xFFFFFF;size&=0xFFFFFF;stride&=0xFFFFFF;var end=address+size;var str=to_hex(address,6)+"\t";var i=0;while(address<end)
{if(i==stride){str+="\n"+to_hex(address,6)+"\t";i=0;}
str+=to_hex(rb(address),2)+" ";address++;i++;}
stdlib.console.log(str);}
function ROM_CALL(id)
{id&=0xFFFF;return rl(state.jmp_tbl+4*id);}
function HeapTable()
{if(state.pedrom&&state.ram[0x30>>>1]<=0x0080){return rl(0x5d58);}
else{if(ROM_CALL(-1)<0x441&&!state.pedrom){return rl(rw(ROM_CALL(0x96)+8));}
else{return ROM_CALL(0x441);}}}
function HeapDeref(id)
{id&=0xFFFF;return rl(HeapTable()+4*id);}
function HeapSizeAddress(address)
{if(!state.pedrom){return((rw(address-2)&0x7FFF)-1)<<1;}
else{if(address>=state.ROM_base){return rw(address)+2;}
else{return rl(address-6)-6;}}}
function HeapSize(id)
{id&=0xFFFF;return HeapSizeAddress(rl(HeapTable()+4*id));}
function PrintHeap()
{var address=HeapTable()+4;stdlib.console.log("0\tFFFFFF\tN/A");for(var i=1;i<2000;i++){var handle=rl(address);if(handle!=0){stdlib.console.log(i+"\t"+to_hex(handle,6)+"\t"+to_hex(HeapSizeAddress(handle),6));}
address+=4;}}
function disassemble_indexed_disp(disp)
{return((disp&0x8000)?"A":"D")+(((disp&0x7000)>>>12)&0x7)+((((disp&0x0800)>>>11)&1)?".L":".W")+")";}
function disassemble_regs_mask(regs8,prefix)
{var str="";var previous=0;var current;var start=-1;var end=-1;var i;for(i=0;i<8;i++){current=regs8&1;if(previous==0&¤t==1)start=i;if(previous==1&¤t==0)end=i-1;if(start==end&&start!=-1){str+=prefix+(i-1)+"/";end=-1;start=-1;}
else if(end>start){str+=prefix+start+"-"+prefix+end+"/";end=-1;start=-1;}
previous=current;regs8>>>=1;}
end=i-1;if(end>(start+1)&&start!=-1){str+=prefix+start+"-"+end;}
else if(start>0&&end>0){str+=prefix+start;}
else{str=str.substring(0,str.length-1);}
return str;}
function disassemble_regs_predec_mask(regs8,prefix)
{var str="";var previous=0;var current;var start=-1;var end=-1;var i;for(i=0;i<8;i++){current=(regs8&(1<<7))>>7;if(previous==0&¤t==1)start=i;if(previous==1&¤t==0)end=i-1;if(start==end&&start!=-1){str+=prefix+(i-1)+"/";end=-1;start=-1;}
else if(end>start){str+=prefix+start+"-"+prefix+end+"/";end=-1;start=-1;}
previous=current;regs8<<=1;}
end=i-1;if(end>(start+1)&&start!=-1){str+=prefix+start+"-"+end;}
else if(start>0&&end>0){str+=prefix+start;}
else{str=str.substring(0,str.length-1);}
return str;}
function disassemble(address,count)
{if(address&1){stdlib.console.log("Cowardly refusing to disassemble from an odd address");return;}
while(count>0){var opcode=rw(address);var rawinstr=cpu.n[opcode];var orig_address=address;var leftside;var rightside;var idx=rawinstr.indexOf(",");if(idx==-1){leftside=rawinstr;rightside="";}
else{leftside=rawinstr.substr(0,idx);rightside=rawinstr.substr(idx+1);}
address+=2;if(leftside!=""){if(leftside.indexOf("#xxxxxx")!=-1){leftside=leftside.replace("#xxxxxx","#"+state.hex_prefix+to_hex(rl(address),8));address+=4;}
else if(leftside.indexOf("#xxx")!=-1){leftside=leftside.replace("#xxx","#"+state.hex_prefix+to_hex(rw(address),4));address+=2;}
else if(leftside.indexOf("#xx")!=-1){leftside=leftside.replace("#xx","#"+state.hex_prefix+to_hex(rw(address)&0xFF,2));address+=2;}
else if(leftside.indexOf("xxx.W")!=-1){leftside=leftside.replace("xxx.W",state.hex_prefix+to_hex(rw(address),4)+".W");address+=2;}
else if(leftside.indexOf("xxx.L")!=-1){leftside=leftside.replace("xxx.L",state.hex_prefix+to_hex(rl(address),8)+".L");address+=4;}
else if(leftside.indexOf("d(A")!=-1){var disp=rw(address);if(rightside.indexOf("Dn)")==0){leftside=leftside.replace("d(A",state.hex_prefix+to_hex(disp&0xFF,2)+"(A");leftside+=","+disassemble_indexed_disp(disp);rightside=rightside.substring(4);}
else{leftside=leftside.replace("d(A",state.hex_prefix+to_hex(disp,4)+"(A");}
address+=2;}
else if(leftside.indexOf("d(PC")!=-1){var disp=rw(address);if(rightside.indexOf("Dn)")==0){leftside=leftside.replace("d(PC",state.hex_prefix+to_hex(disp&0xFF,2)+"(PC");leftside+=","+disassemble_indexed_disp(disp);rightside=rightside.substring(4);}
else{leftside=leftside.replace("d(PC",state.hex_prefix+to_hex(disp,4)+"(PC");}
address+=2;}
else if(leftside.indexOf(".W disp")!=-1){var disp=rw(address)+2;if(disp&0x8000){disp=0x10000-disp;leftside=leftside.replace("disp","-"+state.hex_prefix+to_hex(disp,4)+" ["+to_hex(orig_address-disp,6)+"]");}
else{leftside=leftside.replace("disp","+"+state.hex_prefix+to_hex(disp,4)+" ["+to_hex(orig_address+disp,6)+"]");}
address+=2;}
else if(leftside.indexOf(".S disp")!=-1){var disp=(opcode&0xFF)+2;if(disp&0x80){disp=0x100-disp;leftside=leftside.replace("disp","-"+state.hex_prefix+to_hex(disp,2)+" ["+to_hex(orig_address-disp,6)+"]");}
else{leftside=leftside.replace("disp","+"+state.hex_prefix+to_hex(disp,2)+" ["+to_hex(orig_address+disp,6)+"]");}}
else if(leftside.indexOf("regspredec")!=-1){var regsan=rb(address+1);var regsdn=rb(address);var str="";if(regsdn!=0){str=disassemble_regs_predec_mask(regsdn,"D");str+="/";}
if(regsan!=0){str+=disassemble_regs_predec_mask(regsan,"A");}
leftside=leftside.replace("regspredec",str);address+=2;}
else if(leftside.indexOf("regs")!=-1){var regsdn=rb(address+1);var regsan=rb(address);var str="";if(regsdn!=0){str=disassemble_regs_mask(regsdn,"D");str+="/";}
if(regsan!=0){str+=disassemble_regs_mask(regsan,"A");}
leftside=leftside.replace("regs",str);address+=2;}}
if(rightside!=""){if(rightside.indexOf("#xxx")!=-1){rightside=rightside.replace("#xxx","#"+state.hex_prefix+to_hex(rw(address),4));address+=2;}
else if(rightside.indexOf("xxx.W")!=-1){rightside=rightside.replace("xxx.W",state.hex_prefix+to_hex(rw(address),4)+".W");address+=2;}
else if(rightside.indexOf("xxx.L")!=-1){rightside=rightside.replace("xxx.L",state.hex_prefix+to_hex(rl(address),8)+".L");address+=4;}
else if(rightside.indexOf("d(A")!=-1){var disp=rw(address);if(rightside.indexOf(",Dn)")!=-1){rightside=rightside.replace("d(A",state.hex_prefix+to_hex(disp&0xFF,2)+"(A");rightside=rightside.replace("Dn)",disassemble_indexed_disp(disp));}
else{rightside=rightside.replace("d(A",state.hex_prefix+to_hex(disp,4)+"(A");}
address+=2;}
else if(rightside.indexOf("disp")!=-1){var disp=rw(address)+2;if(disp&0x8000){disp=0x10000-disp;rightside=rightside.replace("disp","-"+state.hex_prefix+to_hex(disp,4)+" ["+to_hex(orig_address-disp,6)+"]");}
else{rightside=rightside.replace("disp","+"+state.hex_prefix+to_hex(disp,4)+" ["+to_hex(orig_address+disp,6)+"]");}
address+=2;}
else if(rightside.indexOf("regs")!=-1){var regsdn=rb(address+1);var regsan=rb(address);var str="";if(regsdn!=0){str=disassemble_regs_mask(regsdn,"D");str+="/";}
if(regsan!=0){str+=disassemble_regs_mask(regsan,"A");}
rightside=rightside.replace("regs",str);address+=2;}
stdlib.console.log(to_hex(orig_address,6)+"\t"+leftside+","+rightside);}
else{stdlib.console.log(to_hex(orig_address,6)+"\t"+leftside);}
count--;}}
function print_status()
{stdlib.console.log('---')
var opcode=rw(state.pc);stdlib.console.log('PC='+to_hex(state.pc,9)+' SR='+to_hex(state.sr,4)+' opcode='+to_hex(opcode,4)+' '+cpu.n[opcode]);var a='';var d='';for(var r=0;r<8;r++){a+='A'+r+'='+to_hex(eval('state.a'+r),9)+' ';d+='D'+r+'='+to_hex(eval('state.d'+r),9)+' ';}
stdlib.console.log(d);stdlib.console.log(a);}
function print_status2()
{stdlib.console.log('---')
var opcode=rw(state.pc);for(var r=0;r<8;r++){stdlib.console.log('D'+r+'='+to_hex(eval('state.d'+r),9)+"\t"+'A'+r+'='+to_hex(eval('state.a'+r),9));}
stdlib.console.log('SR='+to_hex(state.sr,4)+"\tPC="+to_hex(state.pc,9));stdlib.console.log('T='+((state.sr&0x8000)>>>15)+"\tS="+((state.sr&0x2000)>>>13)+"\tM="+((state.sr&0x1000)>>>12)+"\tI="+((state.sr&0x0700)>>>8));stdlib.console.log('X='+((state.sr&0x0010)>>>4)+"\tN="+((state.sr&0x0008)>>>3)+"\tZ="+((state.sr&0x0004)>>>2)+"\tV="+((state.sr&0x0002)>>>1)+"\tC="+(state.sr&0x0001));stdlib.console.log('opcode='+to_hex(opcode,4)+"\t"+cpu.n[opcode]);}
function ebw(value)
{value=value&0xFF;return(value<=0x7F)?value:0xFF00+value;}
function ewl(value)
{value=value&0xFFFF;return(value<=0x7FFF)?value:4294901760+value;}
function subw(subtrahend,minuend)
{subtrahend&=0xFFFF;minuend&=0xFFFF;var complement=0x10000-subtrahend;var result=complement+minuend;var maskedresult=result>=0x10000?result-0x10000:result;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&0x8000)state.sr+=8;if(maskedresult<0)maskedresult+=0x10000;if(complement<0x8000&&minuend<0x8000&&maskedresult>=0x8000)state.sr+=2;if(complement>=0x8000&&minuend>=0x8000&&maskedresult<0x8000)state.sr+=2;if(subtrahend>minuend)state.sr+=0x11;return maskedresult;}
function cmpw(subtrahend,minuend)
{subtrahend&=0xFFFF;minuend&=0xFFFF;var complement=0x10000-subtrahend;var result=complement+minuend;var maskedresult=result>=0x10000?result-0x10000:result;state.sr&=0xFFF0;if(maskedresult==0)state.sr+=4;if(result&0x8000)state.sr+=8;if(maskedresult<0)maskedresult+=0x10000;if(complement<0x8000&&minuend<0x8000&&maskedresult>=0x8000)state.sr+=2;if(complement>=0x8000&&minuend>=0x8000&&maskedresult<0x8000)state.sr+=2;if(subtrahend>minuend)state.sr+=1;return maskedresult;}
function addw(x,y)
{x&=0xFFFF;y&=0xFFFF;var result=x+y;var maskedresult=result&0xFFFF;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&0x8000)state.sr+=8;if(result!=maskedresult)state.sr+=0x11;if(y<0x8000&&x<0x8000&&maskedresult>=0x8000)state.sr+=2;if(y>=0x8000&&x>=0x8000&&maskedresult<0x8000)state.sr+=2;return maskedresult;}
function subb(subtrahend,minuend)
{subtrahend&=0xFF;minuend&=0xFF;var complement=0x100-subtrahend;var result=complement+minuend;var maskedresult=result>=0x100?result-0x100:result;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&0x80)state.sr+=8;if(maskedresult<0)maskedresult+=0x100;if(complement<0x80&&minuend<0x80&&maskedresult>=0x80)state.sr+=2;if(complement>=0x80&&minuend>=0x80&&maskedresult<0x80)state.sr+=2;if(subtrahend>minuend)state.sr+=0x11;return maskedresult;}
function cmpb(subtrahend,minuend)
{subtrahend&=0xFF;minuend&=0xFF;var complement=0x100-subtrahend;var result=complement+minuend;var maskedresult=result>=0x100?result-0x100:result;state.sr&=0xFFF0;if(maskedresult==0)state.sr+=4;if(result&0x80)state.sr+=8;if(maskedresult<0)maskedresult+=0x100;if(complement<0x80&&minuend<0x80&&maskedresult>=0x80)state.sr+=2;if(complement>=0x80&&minuend>=0x80&&maskedresult<0x80)state.sr+=2;if(subtrahend>minuend)state.sr+=1;return maskedresult;}
function addb(x,y)
{x&=0xFF;y&=0xFF;var result=x+y;var maskedresult=result&0xFF;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&0x80)state.sr+=8;if(result!=maskedresult)state.sr+=0x11;if(y<0x80&&x<0x80&&maskedresult>=0x80)state.sr+=2;if(y>=0x80&&x>=0x80&&maskedresult<0x80)state.sr+=2;return maskedresult;}
function subl(subtrahend,minuend)
{var complement=4294967296-subtrahend;var result=complement+minuend;var maskedresult=result>=4294967296?result-4294967296:result;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&2147483648)state.sr+=8;if(maskedresult<0)maskedresult+=4294967296;if(complement<2147483648&&minuend<2147483648&&maskedresult>=2147483648)state.sr+=2;if(complement>=2147483648&&minuend>=2147483648&&maskedresult<2147483648)state.sr+=2;if(subtrahend>minuend)state.sr+=0x11;return maskedresult;}
function cmpl(subtrahend,minuend)
{var complement=4294967296-subtrahend;var result=complement+minuend;var maskedresult=result>=4294967296?result-4294967296:result;state.sr&=0xFFF0;if(maskedresult==0)state.sr+=4;if(result&2147483648)state.sr+=8;if(maskedresult<0)maskedresult+=4294967296;if(complement<2147483648&&minuend<2147483648&&maskedresult>=2147483648)state.sr+=2;if(complement>=2147483648&&minuend>=2147483648&&maskedresult<2147483648)state.sr+=2;if(subtrahend>minuend)state.sr+=1;return maskedresult;}
function addl(x,y)
{var result=x+y;var maskedresult=result>=4294967296?result-4294967296:result;state.sr&=0xFFE0;if(maskedresult==0)state.sr+=4;if(result&2147483648)state.sr+=8;if(result!=maskedresult)state.sr+=0x11;if(maskedresult<0)maskedresult+=4294967296;if(x<2147483648&&y<2147483648&&maskedresult>=2147483648)state.sr+=2;if(x>=2147483648&&y>=2147483648&&maskedresult<2147483648)state.sr+=2;return maskedresult;}
function sub(x,y,size)
{if(size==0)return subb(x,y);if(size==1)return subw(x,y);if(size==2)return subl(x,y);}
function cmp(x,y,size)
{if(size==0)return cmpb(x,y);if(size==1)return cmpw(x,y);if(size==2)return cmpl(x,y);}
function add(x,y,size)
{if(size==0)return addb(x,y);if(size==1)return addw(x,y);if(size==2)return addl(x,y);}
function abcd(x,y)
{var lowsum=(x&0xF)+(y&0xF);if(state.sr&0x10)lowsum++;var carrymid=0;if(lowsum>=10){lowsum-=10;carrymid=0x10;}
var highsum=(x&0xF0)+(y&0xF0)+carrymid;state.sr&=0xFFE4;if(highsum>=0xA0){highsum-=0xA0;state.sr|=0x11;}
var result=highsum+lowsum;if(result!=0)state.sr&=0xFFFB;return result;}
function sbcd(dst,src)
{src&=0xFF;dst&=0xFF;var subtrahend=(src>>>4)*10+(src&0xF);var minuend=(dst>>>4)*10+(dst&0xF);var result=minuend-subtrahend;if(state.sr&0x10)result--;state.sr&=0xFFE4;if(result<0){result=result+100;state.sr|=0x11;}
if(result!=0)state.sr&=0xFFFB;var lowdigit=result%10;var highdigit=(result-lowdigit)/10;var finalresult=highdigit*16+lowdigit;return finalresult;}
function nbcd(src)
{src&=0xFF;var result;if(state.sr&0x10){result=153-src;if(result<0)result+=256;}
else{result=154-src;if(result<0)result+=256;if(!(src&0xF)){result+=16;result&=0xF0;if(result==160||result==256)result=0;}}
state.sr&=0xFFE4;if(result&0x80)state.sr|=8;if(result!=0){state.sr&=0xFFFB;state.sr|=0x11;}
return result;}
function addx(x,y,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;var result=x+y;if(state.sr&0x10)result++;var maskedresult=result>=overflow?result-overflow:result;state.sr&=0xFFE0;if(result==0)state.sr|=4;if(result&neg)state.sr+=8;if(result!=maskedresult)state.sr+=0x11;if(maskedresult<0)maskedresult+=overflow;if(x<neg&&y<neg&&maskedresult>=neg)state.sr+=2;if(x>=neg&&y>=neg&&maskedresult<neg)state.sr+=2;return maskedresult;}
function subx(x,y,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;var result=y-x;if(state.sr&0x10)result--;state.sr&=0xFFE4;if(result<0)
{result+=overflow;state.sr|=0x11;}
if(result!=0)state.sr&=0xFFBF;if(result+result>=overflow)state.sr|=8;if(x>=neg&&y<neg&&result>=neg)state.sr|=2;if(x<neg&&y>=neg&&result<neg)state.sr|=2;return result;}
function muls(x,y)
{x=x&0xFFFF;y=y&0xFFFF;if(x>=0x8000)x-=0x10000;if(y>=0x8000)y-=0x10000;var product=x*y;state.sr&=0xFFF0;if(product<0){product+=4294967296;state.sr|=8;}
if(product==0)state.sr|=4;return product;}
function mulu(x,y)
{x=x&0xFFFF;y=y&0xFFFF;var product=x*y;state.sr&=0xFFF0;if(product>=2147483648)state.sr|=8;if(product==0)state.sr|=4;return product;}
function divu(divisor,dividend)
{if(divisor==0)fire_cpu_exception(5);divisor&=0xFFFF;var quotient=Math.floor(dividend/divisor)&4294967295;var remainder=(dividend%divisor)&0xFFFF;state.sr&=0xFFF0;if(quotient==0)state.sr|=4;if(quotient&4294901760){if(quotient>=2147483648)state.sr|=8;state.sr|=2;return dividend;}
if(quotient>0x10000||remainder>0x10000||quotient<0||remainder<0)stdlib.console.log("bad divide!");var result=quotient|(remainder<<16);if(result&0x8000)state.sr|=8;if(result<0)result+=4294967296;return result;}
function divs(divisor,dividend)
{divisor&=0xFFFF;if(divisor==0)fire_cpu_exception(5);var adivisor=divisor>=0x8000?divisor-0x10000:divisor;var adividend=dividend>=2147483648?dividend-4294967296:dividend;var quotient=Math.floor(adividend/adivisor)&4294967295;var remainder=(adividend%adivisor)&0xFFFF;state.sr&=0xFFF0;if(quotient>=2147483648)state.sr|=8;if(quotient==0)state.sr|=4;if(quotient>=0x8000||quotient<-32768){if(quotient>=2147483648)state.sr|=8;state.sr|=2;return dividend;}
if(quotient<0)quotient+=0x10000;if(remainder<0)remainder+=0x10000;var result=quotient+(remainder<<16);if(result<0)result+=4294967296;return result;}
function lsl(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFF0;while(shift>0)
{if(x&neg)state.sr|=0x11;else state.sr&=0xFFEE;x<<=1;if(x>=overflow){x-=overflow;}
shift--;}
x&=overflow-1;if(x&neg)state.sr|=8
if(x==0)state.sr|=4;return x;}
function asl(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFE1;if(shift>0)state.sr&=0xFFE0;while(shift>0)
{if(x&neg)state.sr|=0x11;else state.sr&=0xFFEE;var old=x;x<<=1;if(x>=overflow){x-=overflow;}
if((x&neg)!=(old&neg))state.sr|=2;shift--;}
x&=overflow-1;if(x&neg)state.sr|=8
if(x==0)state.sr|=4;return x;}
function lsr(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFE0;while(shift>0)
{if(x&1)state.sr|=0x11;else state.sr&=0xFFEE;x>>>=1;shift--;}
x&=overflow-1;if(x&neg)state.sr|=8
if(x==0)state.sr|=4;return x;}
function asr(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFF0;if(shift>0)state.sr&=0xFFEF;while(shift>0)
{if(x&1)state.sr|=0x11;else state.sr&=0xFFEE;if(x&neg){x>>>=1;x|=neg;}
else{x>>>=1;}
shift--;}
x&=overflow-1;if(x&neg)state.sr|=8
if(x==0)state.sr|=4;return x;}
function ror(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFF0;while(shift--)
{var out=x&1;x>>>=1;if(out)x=x+neg;}
x&=overflow-1;if(x&neg)state.sr|=0x9
if(x==0)state.sr|=4;return x;}
function rol(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFF0;while(shift>0)
{if(x&neg){x<<=1;x++;}
else{x<<=1;}
if(x>=overflow){x-=overflow;}
shift--;}
x&=overflow-1;if(x&neg)state.sr|=0x8;if(x&1)state.sr|=1;if(x==0)state.sr|=4;return x;}
function roxr(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;while(shift--)
{var out=x&1;x>>>=1;if(state.sr&0x10)x=x+neg;state.sr=state.sr&0xFFE0;if(out)state.sr+=0x10;}
x&=overflow-1;if(x&neg)state.sr|=0x9
if(x==0)state.sr|=4;if(state.sr&0x10)state.sr|=1;return x;}
function roxl(x,shift,size)
{var overflow=0x100;if(size==1)overflow=0x10000;if(size==2)overflow=4294967296;var neg=overflow/2;x&=overflow-1;state.sr&=0xFFF0;while(shift>0)
{var old=x;x<<=1;if(state.sr&0x10)x=x+1;if(old&neg){state.sr|=0x10;}
else{state.sr&=0xFFEF;}
if(x>=overflow){x-=overflow;}
shift--;}
x&=overflow-1;if(x&neg)state.sr|=0x8;if(state.sr&0x10)state.sr|=1;if(x==0)state.sr|=4;return x;}
function aline(){state.pc-=2;fire_cpu_exception(10);}
function fline(){state.pc-=2;fire_cpu_exception(11);}
function unhandled_instruction(){state.pc-=2;fire_cpu_exception(4);}
function update_sr(new_sr)
{if((new_sr^state.sr)&0x2000)
{var t=state.a7;state.a7=state.a8;state.a8=t;}
state.sr=new_sr&0xA71F;}
function an(reg)
{switch(reg){case 0:return state.a0;case 1:return state.a1;case 2:return state.a2;case 3:return state.a3;case 4:return state.a4;case 5:return state.a5;case 6:return state.a6;case 7:return state.a7;}}
function dn(reg)
{switch(reg){case 0:return state.d0;case 1:return state.d1;case 2:return state.d2;case 3:return state.d3;case 4:return state.d4;case 5:return state.d5;case 6:return state.d6;case 7:return state.d7;}}
var MODE_DREG=0;var MODE_AREG=1;var MODE_AREG_INDIRECT=2;var MODE_AREG_POSTINC=3;var MODE_AREG_PREDEC=4;var MODE_AREG_OFFSET=5;var MODE_AREG_INDEX=6;var MODE_MISC=7;var MISCMODE_SHORT=0;var MISCMODE_LONG=1;var MISCMODE_PC_OFFSET=2;var MISCMODE_PC_INDEX=3;var MISCMODE_IMM=4;var instruction_list=""
function insert_inst(opcode,code,name)
{instruction_list+="cpu.t["+opcode+"] = function() { "+code+"};";cpu.n[opcode]=name;}
function insert_inst2(opcode,code,name,count)
{instruction_list+="cpu.t["+opcode+"] = function() { "+code+"};";cpu.n[opcode]=name;cpu.cycles[opcode]=count;}
function valid_source(mode,reg)
{return mode<7||reg<=4}
function valid_dest(mode,reg)
{return mode<7||reg<=1}
function valid_calc_effective_address(mode,reg)
{return mode==MODE_AREG_INDIRECT||mode==MODE_AREG_OFFSET||mode==MODE_AREG_INDEX||(mode==MODE_MISC&&(reg<=3))}
function size_name(size)
{if(size==0)return".B"
if(size==1)return".W"
return".L"}
function get_read(size)
{if(size==0)return"rb"
if(size==1)return"rw"
if(size==2)return"rl"}
function get_write(size)
{if(size==0)return"wb"
if(size==1)return"ww"
if(size==2)return"wl"}
function amode_name(mode,reg,size)
{if(mode==MODE_DREG)return"D"+(reg)
if(mode==1)return"A"+(reg)
if(mode==2)return"(A"+(reg)+")"
if(mode==3)return"(A"+(reg)+")+"
if(mode==MODE_AREG_PREDEC)return"-(A"+(reg)+")"
if(mode==5)return"d(A"+(reg)+")"
if(mode==6)return"d(A"+(reg)+",Dn)"
if(mode==7&®==0)return"xxx.W"
if(mode==7&®==1)return"xxx.L"
if(mode==7&®==2)return"d(PC)"
if(mode==7&®==3)return"d(PC,Dn)"
if(mode==7&®==4){if(size==0)return"#xx";else if(size==1)return"#xxx";else if(size==2)return"#xxxxxx";}
return"unk"}
function size_imm(size)
{if(size==0)return" #xx,";else if(size==1)return" #xxx,";else if(size==2)return" #xxxxxx,";}
function read_pc(size,dest,sideeffects)
{if(size==0)
{var code="var "+dest+"=rb(state.pc+1);"
return sideeffects?code+"state.pc+=2;":code}
else if(size==1)
{var code="var "+dest+"=rw(state.pc);"
return sideeffects?code+"state.pc+=2;":code}
else if(size==2)
{var code="var "+dest+"=rl(state.pc);"
return sideeffects?code+"state.pc+=4;":code}}
function amode_read(mode,reg,size,sideeffects)
{var increment=size+1;if(increment==3)increment=4;if(increment==1&®==7)increment=2;if(mode==MODE_MISC&®==MISCMODE_IMM)
return read_pc(size,"s",sideeffects);if(mode==MODE_MISC&®==MISCMODE_PC_OFFSET)
{var code=read_pc(1,"o",sideeffects);code+="var a=state.pc+ewl(o)-2;"
code+="var s="+get_read(size)+"(a);"
return code;}
if(mode==MODE_MISC&®==MISCMODE_PC_INDEX)
{var code=read_pc(1,"e",sideeffects)
code+="var a=e&0xFF;"
code+="if(a>127)a-=256;"
code+="a+=state.pc-2;"
code+="var x=(e>>>12)&7;"
code+="var y=(e>32767)?an(x):dn(x);"
code+="if(!(e&0x800))y=ewl(y);"
code+="var s="+get_read(size)+"(y+a);"
return code;}
if(mode==MODE_MISC&®==MISCMODE_LONG)
{code=read_pc(2,"a",sideeffects)
code+="var s="+get_read(size)+"(a);"
return code;}
if(mode==MODE_MISC&®==MISCMODE_SHORT)
{code=read_pc(1,"a",sideeffects)
code+="var s="+get_read(size)+"(ewl(a));"
return code;}
if(mode==MODE_AREG_INDIRECT)
{return"var s="+get_read(size)+"(state.a"+reg+");"}
if(mode==MODE_AREG_POSTINC)
{var code="var s="+get_read(size)+"(state.a"+reg+");"
if(sideeffects)code+="state.a"+reg+"+="+increment+";"
return code;}
if(mode==MODE_AREG_PREDEC)
{if(sideeffects)
return"state.a"+reg+"-="+increment+";"+"var s="+get_read(size)+"(state.a"+reg+");"
else
return"var s="+get_read(size)+"(state.a"+reg+"-"+increment+");"}
if(mode==MODE_AREG_OFFSET)
{var code=read_pc(1,"o",sideeffects)
code+="var a=state.a"+reg+"+ewl(o);"
code+="var s="+get_read(size)+"(a);"
return code;}
if(mode==MODE_AREG_INDEX)
{var code=read_pc(1,"e",sideeffects)
code+="var a=e&255;"
code+="if (a>=128)a-=256;"
code+="a+=state.a"+reg+";"
code+="var x=(e>>>12)&7;"
code+="var y=(e>32767)?an(x):dn(x);"
code+="if(!(e&0x800))y=ewl(y);"
code+="var s="+get_read(size)+"(y+a);"
return code}
if(mode==MODE_DREG)
{if(size==0)
return"var s=state.d"+reg+"&255;"
if(size==1)
return"var s=state.d"+reg+"&65535;"
if(size==2)
return"var s=state.d"+reg+"; if(s<0)s+=4294967296; if(s>4294967295)s-=4294967296;"}
if(mode==MODE_AREG)
{if(size==1)
return"var s=state.a"+reg+"&65535;"
if(size==2)
return"var s=state.a"+reg+"; if(s<0)s+=4294967296; if(s>4294967295)s-=4294967296;"}
return"fire_cpu_exception(4);";}
function effective_address_calc(mode,reg)
{var code="fire_cpu_exception(4);"
if(mode==MODE_MISC&®==MISCMODE_PC_OFFSET)
{code=read_pc(1,"o",true)
code+="var z=state.pc-2+ewl(o);"
code+="if(z>4294967295)z-=4294967296;"}
if(mode==MODE_MISC&®==MISCMODE_PC_INDEX)
{code=read_pc(1,"e",true)
code+="var a=e&0xFF;"
code+="if(a>127)a-=256;"
code+="a+=state.pc-2;"
code+="var x=(e>>>12)&7;"
code+="var y=(e>32767)?an(x):dn(x);"
code+="if (!(e&0x800))y=ewl(y);"
code+="var z=y+a;"
code+="if(z>4294967295)z-=4294967296;"}
if(mode==MODE_AREG_INDEX)
{code=read_pc(1,"e",true)
code+="var a = e&0xFF;"
code+="if(a>127)a-=256;"
code+="a+=state.a"+reg+";"
code+="var x=(e>>>12)&7;"
code+="var y=(e>32767)?an(x):dn(x);"
code+="if (!(e&0x800))y=ewl(y);"
code+="var z=y+a;"
code+="if(z>4294967295)z-=4294967296;"}
if(mode==MODE_MISC&®==MISCMODE_LONG)
code=read_pc(2,"z",true)
if(mode==MODE_MISC&®==MISCMODE_SHORT)
{code=read_pc(1,"z",true)
code+="z=ewl(z);"}
if(mode==MODE_AREG_OFFSET)
{code=read_pc(1,"o",true)
code+="var z=state.a"+reg+"+ewl(o);"
code+="if(z>4294967295)z-=4294967296;"}
if(mode==MODE_AREG_INDIRECT)
code="var z=state.a"+reg+";"
return code}
function set_condition_flags_data(size,s)
{var code="state.sr &= 65520;"
code+="if("+s+"==0) state.sr += 4;"
if(size==0)return code+"if("+s+"&128) state.sr += 8;"
if(size==1)return code+"if("+s+"&32768) state.sr += 8;"
if(size==2)return code+"if("+s+"&2147483648) state.sr += 8;"}
function amode_write(mode,reg,size,data)
{var increment=size+1;if(increment==3)increment=4;if(increment==1&®==7)increment=2;if(mode==MODE_MISC&®==MISCMODE_LONG)
return"var addr = rl(state.pc); state.pc += 4; "+get_write(size)+"(addr,"+data+");"
if(mode==MODE_MISC&®==MISCMODE_SHORT)
return"var addr = ewl(rw(state.pc)); state.pc += 2; "+get_write(size)+"(addr,"+data+");"
if(mode==MODE_AREG)
{if(size==2)
return"state.a"+reg+"="+data+"&4294967295;"
if(size==1)
return"state.a"+reg+"=ewl("+data+")&4294967295;"}
if(mode==MODE_AREG_INDIRECT)
return get_write(size)+"(state.a"+reg+","+data+");"
if(mode==MODE_AREG_POSTINC)
return get_write(size)+"(state.a"+reg+","+data+"); state.a"+reg+"+="+increment+";"
if(mode==MODE_AREG_PREDEC){return"state.a"+reg+"-="+increment+"; "+get_write(size)+"(state.a"+reg+","+data+");"}
if(mode==MODE_AREG_OFFSET)
return read_pc(1,"o",true)+get_write(size)+"(state.a"+reg+"+ewl(o),"+data+");"
if(mode==MODE_AREG_INDEX)
{var code=read_pc(1,"e",true)
code+="var a=e%256;"
code+="if(a>127)a-=256;"
code+="a+=state.a"+reg+";"
code+="var x=(e>>>12)&7;"
code+="var y=(e>32767)?an(x):dn(x);"
code+="if(!(e&0x800))y=ewl(y);"
code+=get_write(size)+"(a+y,"+data+");"
return code;}
if(mode==MODE_DREG)
{if(size==2)
return"state.d"+reg+"="+data+"&4294967295;"
if(size==0)
return"state.d"+reg+"=((state.d"+reg+">>>8)*256)+("+data+"&255);"
if(size==1)
return"state.d"+reg+"=((state.d"+reg+">>>16)*65536)+("+data+"&65535);"}
return"fire_cpu_exception(4);"}
function effective_address_calculation_time(mode,reg,size)
{if(mode==MODE_DREG||mode==MODE_AREG){return 0;}
if(mode==MODE_AREG_INDIRECT){return(size!=2)?4:8;}
if(mode==MODE_AREG_POSTINC){return(size!=2)?4:8;}
if(mode==MODE_AREG_PREDEC){return(size!=2)?6:10;}
if(mode==MODE_AREG_OFFSET){return(size!=2)?8:12;}
if(mode==MODE_AREG_INDEX){return(size!=2)?10:14;}
if(mode==MODE_MISC){if(reg==MISCMODE_SHORT){return(size!=2)?8:12;}
if(reg==MISCMODE_LONG){return(size!=2)?12:16;}
if(reg==MISCMODE_PC_OFFSET){return(size!=2)?8:12;}
if(reg==MISCMODE_PC_INDEX){return(size!=2)?10:14;}
if(reg==MISCMODE_IMM){return(size!=2)?4:8;}}
return 0;}
function build_moveq()
{for(var data=0;data<=255;data++){for(var reg=0;reg<8;reg++){var opcode=0x7000+(reg<<9)+data;var code="state.sr &= 65520;";code+="state.d"+reg+" = ";if(data<128){code+=data+"; ";if(data==0)
code+="state.sr |= 4;";}
else
code+=(data+0xFFFFFF00)+"; state.sr |= 8; ";insert_inst2(opcode,code,"MOVEQ #"+state.hex_prefix+(data>=128?to_hex(data-256,2):to_hex(data,2))+",D"+reg,4);}}}
function build_addsubq()
{for(var offset=-8;offset<9;offset++){if(offset!=0){for(var mode=0;mode<8;mode++){for(var reg=0;reg<8;reg++){for(var size=0;size<3;size++){if(valid_dest(mode,reg)&&(mode!=MODE_AREG||size!=0))
{var name="";var opcode=0;if(offset>0)
{opcode=0x5000+(offset<<9)
if(offset==8)opcode=0x5000
opcode+=(size<<6)+(mode<<3)+reg
name="ADDQ"+size_name(size)+" #"+offset+","+amode_name(mode,reg,0)}
else
{opcode=0x5100+((-offset)<<9)
if(offset==-8)opcode=0x5100
opcode+=(size<<6)+(mode<<3)+reg
name="SUBQ"+size_name(size)+" #"+(-offset)+","+amode_name(mode,reg,0)}
var actualsize=(mode==MODE_AREG)?2:size;var cost=(size==2)?(((mode==MODE_DREG)||(mode==MODE_AREG))?4:8):(((mode==MODE_DREG)||(mode==MODE_AREG))?8:12);var code=amode_read(mode,reg,actualsize,false);if(mode==MODE_AREG)
{code+="var r=s+"+offset+";"
if(offset<0)code+="if(r<0)r+=4294967296;"
if(offset>0)code+="if(r>4294967295)r-=4294967296;"}
else
{if(size==0&&offset<0)
code+="var r=subb("+(-offset)+", s);"
if(size==0&&offset>0)
code+="var r=addb("+offset+", s);"
if(size==1&&offset<0)
code+="var r=subw("+(-offset)+", s);"
if(size==1&&offset>0)
code+="var r=addw("+offset+", s);"
if(size==2&&offset<0)
code+="var r=subl("+(-offset)+", s);"
if(size==2&&offset>0)
code+="var r=addl("+offset+", s);"
code+="state.sr = (state.sr&0xFFEF)|((state.sr&1)<<4);"}
code+=amode_write(mode,reg,actualsize,"r")
insert_inst2(opcode,code,name,cost+effective_address_calculation_time(mode,reg,size));}}}}}}}
function build_conditionals(condition,name,bits)
{var bcc_opcode=0x6000+(bits<<8)
var dbcc_opcode=0x50C8+(bits<<8)
var scc_opcode=0x50C0+(bits<<8)
for(var o=0;o<256;o++){var opcode=bcc_opcode+o;var iname="B"+name;var cost=10;if(iname=="BT"){iname="BRA";}
if(iname=="BF"){iname="BSR";cost=18;}
if(o==0){iname=iname+".W disp";}
else{iname=iname+".S disp";}
var code="";if(o==0)
{code="var o=rw(state.pc);"
if(name=="F")
{code+=amode_write(4,7,2,"(state.pc+2)")
code+="if(true) {"}
else
{code+=condition+"{";}
code+="state.pc+=ewl(o);";code+="if(state.pc>4294967295)state.pc-=4294967296;";code+="}";code+="else {";code+="state.pc += 2; state.cycle_count += 2;";code+="}";}
else
{if(name=="F")
code=amode_write(4,7,2,"state.pc")
else
code+=condition+"{";if(o<128)
code+="state.pc+="+o+";"
else
code+="state.pc-="+(256-o)+";"
if(name!="F"){code+="}";code+="else {";code+="state.cycle_count -= 2;";code+="}";}}
insert_inst2(opcode,code,iname,cost)}
for(var reg=0;reg<8;reg++){var opcode=dbcc_opcode+reg
var cost=10;var code=condition+"{ state.pc += 2; state.cycle_count += 2; } else {"
code+="var p=state.d"+reg+";"
code+="var u=(p>>>16)*65536;"
code+="var l=p%65536;"
code+="var m=(l - 1)&65535;"
code+="state.d"+reg+"=u+m;"
code+="if(m==65535) {"
code+="state.pc+=2; state.cycle_count += 4;"
code+="} else {"
code+="state.pc=(state.pc+ewl(rw(state.pc)))%4294967296;}"
code+="}"
insert_inst2(opcode,code,"DB"+name+" D"+reg+",disp",cost)}
for(var reg=0;reg<8;reg++){for(var mode=0;mode<8;mode++){if(valid_dest(mode,reg)&&mode!=1)
{var opcode=scc_opcode+reg+(mode<<3)
var cost=(mode==MODE_DREG)?4:8;var code=condition+"{"
code+=amode_write(mode,reg,0,"255");if(mode==MODE_DREG)code+="state.cycle_count += 2;"
code+="} else {"
code+=amode_write(mode,reg,0,"0")
code+="}"
insert_inst2(opcode,code,"S"+name+" "+amode_name(mode,reg,0),cost+effective_address_calculation_time(mode,reg,0))}}}}
function build_moves(name,size,pattern)
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){for(var dstmode=0;dstmode<8;dstmode++){if(size==0&&(dstmode==1||srcmode==1))continue;for(var dstreg=0;dstreg<8;dstreg++){if(valid_source(srcmode,srcreg)&&valid_dest(dstmode,dstreg))
{var opcode=pattern+(dstreg<<9)+(dstmode<<6)+(srcmode<<3)+srcreg
var fullname=((dstmode==1)?name.replace("MOVE","MOVEA"):name)+" "+amode_name(srcmode,srcreg,size)+","+amode_name(dstmode,dstreg,size)
var code=amode_read(srcmode,srcreg,size,true)
code+=amode_write(dstmode,dstreg,size,"s")
if(dstmode!=MODE_AREG){code+=set_condition_flags_data(size,"s")}
insert_inst2(opcode,code,fullname,4+effective_address_calculation_time(srcmode,srcreg,size)+effective_address_calculation_time(dstmode,dstreg,size))}}}}}}
function build_movep()
{for(var opmode=4;opmode<6;opmode++){for(var areg=0;areg<8;areg++){for(var dreg=0;dreg<8;dreg++){var opcode=0x0008+(dreg<<9)+(opmode<<6)+areg
var fullname="MOVEP"+((opmode&1)?".L ":".W ")+"d(A"+areg+"),D"+dreg;var code="state.pc += 2"
insert_inst2(opcode,code,fullname,((opmode&1)?24:16))}}}
for(var opmode=6;opmode<8;opmode++){for(var areg=0;areg<8;areg++){for(var dreg=0;dreg<8;dreg++){var opcode=0x0008+(dreg<<9)+(opmode<<6)+areg
var fullname="MOVEP"+((opmode&1)?".L ":".W ")+"D"+dreg+",d(A"+areg+")"
var code="state.pc += 2"
insert_inst2(opcode,code,fullname,((opmode&1)?24:16))}}}}
function build_operation(name,size,source,dest)
{var code="";if(size==0&&name=="ADD")code="var r=addb("+source+","+dest+");"
if(size==1&&name=="ADD")code="var r=addw("+source+","+dest+");"
if(size==2&&name=="ADD")code="var r=addl("+source+","+dest+");"
if(size==0&&name=="SUB")code="var r=subb("+source+","+dest+");"
if(size==1&&name=="SUB")code="var r=subw("+source+","+dest+");"
if(size==2&&name=="SUB")code="var r=subl("+source+","+dest+");"
if(name=="OR")code+="var r="+source+"|"+dest+";"
if(name=="AND")code+="var r="+source+"&"+dest+";"
if(name=="EOR")code+="var r="+source+"^"+dest+";"
if(name=="OR"||name=="AND"||name=="EOR")
{code+="if(r<0)r+=4294967296;"
if(size==0)code+="r&=255;"
if(size==1)code+="r&=65535;"
code+=set_condition_flags_data(size,"r")}
return code;}
function build_calc(name,bits)
{for(var dreg=0;dreg<8;dreg++){for(var reg=0;reg<8;reg++){for(var mode=0;mode<8;mode++){for(var size=0;size<3;size++){var opcode=bits+(dreg<<9)+(size<<6)+(mode<<3)+reg
if(valid_source(mode,reg)&&name!="EOR")
{var iname=name+size_name(size)+" "+amode_name(mode,reg,size)+",D"+dreg
var code=amode_read(mode,reg,size,true)
var cost=4;if(size==2){cost+=2;if(mode==MODE_DREG||(mode==MODE_MISC&®==MISCMODE_IMM)){cost+=2;}}
code+=build_operation(name,size,"s","state.d"+dreg+"")
code+=amode_write(MODE_DREG,dreg,size,"r")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(mode,reg,size))}
if(valid_dest(mode,reg)&&(mode!=MODE_DREG||name=="EOR")&&mode!=MODE_AREG)
{opcode=opcode+0x100
var iname=name+size_name(size)+" D"+dreg+","+amode_name(mode,reg,size)
var code=amode_read(mode,reg,size,false)
var cost=(size==2)?12:8;code+=build_operation(name,size,"state.d"+dreg,"s")
code+=amode_write(mode,reg,size,"r")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(mode,reg,size))}}}}}}
function build_muldiv(name,bits,calcfunc,cost)
{for(var dreg=0;dreg<8;dreg++){for(var mode=0;mode<8;mode++){for(var reg=0;reg<8;reg++){if(valid_source(mode,reg)&&mode!=MODE_AREG){var opcode=bits+(dreg<<9)+(mode<<3)+reg
var iname=name+" "+amode_name(mode,reg,1)+",D"+dreg
var code=amode_read(mode,reg,1,true)
code+="state.d"+dreg+" = "+calcfunc+"(s,state.d"+dreg+");";insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(mode,reg,1))}}}}}
function build_bit_operation(name,bits,registercost,memorycost)
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(srcmode!=1&&(valid_dest(srcmode,srcreg)||(name=='BTST'&&srcmode==MODE_MISC&&(srcreg==MISCMODE_PC_OFFSET||srcreg==MISCMODE_PC_INDEX)))){for(var dreg=0;dreg<=8;dreg++)
{var opcode,iname,code="";if(dreg==8)
{opcode=bits+(srcmode<<3)+srcreg;iname=name+" #xxx,"+amode_name(srcmode,srcreg,0)}
else
{opcode=bits+(srcmode<<3)+srcreg-0x700+(dreg<<9);iname=name+" D"+dreg+","+amode_name(srcmode,srcreg,0)}
if(dreg==8)
code=read_pc(1,"b",true)
if(srcmode<=1)
{if(dreg==8)
code+="b&=31;"
else
code+="var b=31&state.d"+dreg+";"
code+=amode_read(srcmode,srcreg,2,name=="BTST")}
else
{if(dreg==8)
code+="b&=7;"
else
code+="var b=7&state.d"+dreg+";"
code+=amode_read(srcmode,srcreg,0,name=="BTST")}
code+="state.sr|=4;"
code+="if (s&(1<<b))state.sr=state.sr&65531;"
if(name!="BTST")
{if(srcmode<=1)
{if(name=="BCLR")code+="s&=(4294967295-(1<<b));"
if(name=="BSET")code+="s|=(1<<b);"
if(name=="BCHG")code+="s^=(1<<b);"
code+="if(s<0)s+=4294967296;"
code+=amode_write(srcmode,srcreg,2,"s")}
else
{if(name=="BCLR")code+="s&=(255-(1<<b));"
if(name=="BSET")code+="s|=(1<<b);"
if(name=="BCHG")code+="s^=(1<<b);"
code+=amode_write(srcmode,srcreg,0,"s")}}
insert_inst(opcode,code,iname)}}}}}
function build_cmp()
{for(var size=0;size<3;size++){for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){for(var firstreg=0;firstreg<8;firstreg++){if(valid_source(srcmode,srcreg))
{var opcode=0xB000+(firstreg<<9)+(size<<6)+(srcmode<<3)+srcreg;var iname="CMP"+size_name(size)+" "+amode_name(srcmode,srcreg,size)+",D"+firstreg
var code=amode_read(srcmode,srcreg,size,true)
var cost=(size==2)?6:4;code+="var m=state.d"+firstreg+";"
if(size==1)code+="m=m&0xFFFF;"
if(size==0)code+="m=m&0xFF;"
if(size==0)code+="cmpb(s,m);"
if(size==1)code+="cmpw(s,m);"
if(size==2)code+="cmpl(s,m);"
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))}}}}}}
function build_adest()
{for(var areg=0;areg<8;areg++){for(var srcreg=0;srcreg<8;srcreg++){for(var srcmode=0;srcmode<8;srcmode++){for(var size=1;size<3;size++){if(valid_source(srcmode,srcreg))
{var opcode=0xB0C0+(areg<<9)+((size-1)<<8)+(srcmode<<3)+srcreg
var iname="CMPA"+size_name(size)+" "+amode_name(srcmode,srcreg,size)+",A"+areg
var code=amode_read(srcmode,srcreg,size,true)
var cost=6+effective_address_calculation_time(srcmode,srcreg,size);if(size==1)code+="s=ewl(s);"
code+="cmpl(s,state.a"+areg+");"
insert_inst2(opcode,code,iname,cost)
cost+=2;if(size==2&&srcmode!=MODE_DREG&&(srcmode!=MODE_MISC||srcreg!=MISCMODE_IMM)){cost-=2;}
opcode=0x90C0+(areg<<9)+((size-1)<<8)+(srcmode<<3)+srcreg
iname="SUBA"+size_name(size)+" "+amode_name(srcmode,srcreg,size)+",A"+areg
code=amode_read(srcmode,srcreg,size,true)
if(size==1)code+="s=ewl(s);"
code+="var r=state.a"+areg+" - s;"
code+="if(r<0)r+=4294967296;"
code+=amode_write(1,areg,2,"r")
insert_inst2(opcode,code,iname,cost)
opcode=0xD0C0+(areg<<9)+((size-1)<<8)+(srcmode<<3)+srcreg
iname="ADDA"+size_name(size)+" "+amode_name(srcmode,srcreg,size)+",A"+areg
code=amode_read(srcmode,srcreg,size,true)
if(size==1)code+="s=ewl(s);";cost+=2;code+="var r=state.a"+areg+" + s;"
code+="if(r>4294967295)r-=4294967296;"
code+=amode_write(1,areg,2,"r")
insert_inst2(opcode,code,iname,cost)}}}}}}
function build_shifts(name,mask,altmask,namelower)
{for(var reg=0;reg<8;reg++){for(var size=0;size<3;size++){for(var shift=0;shift<8;shift++){for(var mm=0;mm<2;mm++){var actualshift=shift==0?8:shift;var iname="";var opcode=mask+0x20+(size<<6)+reg+(shift<<9);var shiftamount;var cost=(size==2)?8:6;if(mm==0)
{opcode=opcode-0x20;iname=name+size_name(size)+" #"+actualshift+",D"+reg
shiftamount=actualshift}
else
{iname=name+size_name(size)+" D"+shift+",D"+reg
shiftamount="state.d"+shift+"&31";}
var src="";if(size==0)src="state.d"+reg+"&255"
else if(size==1)src="state.d"+reg+"&65535"
else if(size==2)src="state.d"+reg
var code=amode_write(MODE_DREG,reg,size,namelower+"("+src+","+shiftamount+","+size+")")
insert_inst2(opcode,code,iname,cost)}}}}
for(var reg=0;reg<8;reg++){for(var mode=0;mode<8;mode++){if(valid_dest(mode,reg)&&mode!=MODE_DREG&&mode!=MODE_AREG)
{var opcode=altmask+(mode<<3)+reg;var iname=name+".W "+amode_name(mode,reg,1)
var code=amode_read(mode,reg,1,false)
code+=amode_write(mode,reg,1,namelower+"(s,1,1)")
insert_inst2(opcode,code,iname,8+effective_address_calculation_time(mode,reg,1))}}}}
function build_immediate(name,mask,operation)
{for(var reg=0;reg<8;reg++){for(var mode=0;mode<8;mode++){for(var size=0;size<3;size++){if((valid_dest(mode,reg)&&mode!=MODE_AREG)||(mode==MODE_MISC&®==4&&size<2&&operation!=""))
{var opcode=mask+(size<<6)+(mode<<3)+reg
var mode_name=amode_name(mode,reg,size)
if(mode==MODE_MISC&®==4&&size==0)mode_name="CCR"
if(mode==MODE_MISC&®==4&&size==1)mode_name="SR"
var iname=name+size_name(size)+size_imm(size)+mode_name
var code=read_pc(size,"m",true)
var cost=(mode==MODE_DREG)?8:12;if(size==2){cost+=6;if(name!="ANDI"||mode!=MODE_DREG){cost+=2;}}
if(mode==MODE_MISC&®==4)
{if(size==1)code+="if(state.sr&0x2000==0)fire_cpu_exception(8);";if(size==0&&name=="ANDI")code+="m|=0xFF00;"
code+="update_sr(state.sr"+operation.substring(7,8)+"m);"
insert_inst2(opcode,code,iname,20)}
else
{code+=amode_read(mode,reg,size,false)
if(operation!="")
{code+=operation;code+=set_condition_flags_data(size,"r")}
else
{code+="var r="+name.substring(0,3).toLowerCase()+size_name(size).substring(1,2).toLowerCase()+"(m,s);"}
code+=amode_write(mode,reg,size,"r")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(mode,reg,size))}}}}}}
function build_ext(name,bits)
{for(var src=0;src<8;src++){for(var dst=0;dst<8;dst++){for(var size=0;size<3;size++){for(var mem=0;mem<2;mem++){var opcode=bits+(dst<<9)+(size<<6)+(mem<<3)+src
var iname=name+size_name(size)
if(mem==0)
iname+=" D"+src+",D"+dst+"'"
else
iname+=" -(A"+src+"),-(A"+dst+")'"
var mode=mem==0?MODE_DREG:MODE_AREG_PREDEC
var code=amode_read(mode,src,size,true)
code+="var c=s;"
code+=amode_read(mode,dst,size,false)
code+="var n="+name.toLowerCase()+"(c,s,"+size+");"
code+=amode_write(mode,dst,size,"n")
var cost=(mode==MODE_DREG)?((size==2)?8:4):((size==2)?30:18)
insert_inst2(opcode,code,iname,cost)}}}}}
function build_not_neg_clr_tst_tas()
{for(var size=0;size<3;size++){for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(valid_dest(srcmode,srcreg)&&srcmode!=MODE_AREG)
{var opcode=0x4600+(size<<6)+(srcmode<<3)+srcreg;var iname="NOT"+size_name(size)+" "+amode_name(srcmode,srcreg,size)
var code=amode_read(srcmode,srcreg,size,false)
var cost=(size==2)?6:4;if(srcmode!=MODE_DREG){cost*=2;}
if(size==0)code+="s=255-s;"
if(size==1)code+="s=65535-s;"
if(size==2)code+="s=4294967295-s;"
code+=set_condition_flags_data(size,"s")
code+=amode_write(srcmode,srcreg,size,"s")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))
opcode=0x4400+(size<<6)+(srcmode<<3)+srcreg;iname="NEG"+size_name(size)+" "+amode_name(srcmode,srcreg,size)
code=amode_read(srcmode,srcreg,size,false)
code+="state.sr &= 0xFFE0;"
if(size==0)code+="var r=s==0?0:256-s;if(r&0x80)state.sr|=8;"
if(size==1)code+="var r=s==0?0:65536-s;if(r&0x8000)state.sr|=8;"
if(size==2)code+="var r=s==0?0:4294967296-s;if(r&2147483647)state.sr|=8;"
code+="if(r==0)state.sr|=4;else state.sr|=17;"
code+=amode_write(srcmode,srcreg,size,"r")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))
opcode=0x4000+(size<<6)+(srcmode<<3)+srcreg;iname="NEGX"+size_name(size)+" "+amode_name(srcmode,srcreg,size)
code=amode_read(srcmode,srcreg,size,false)
code+="state.sr &= 0xFFF0;"
code+="if(state.sr&0x10)s++;"
if(size==0)code+="var r=256-s;"
if(size==1)code+="var r=65536-s;"
if(size==2)code+="var r=4294967296-s;if(r>4294967295)r=0;"
code+=set_condition_flags_data(size,"r")
code+=amode_write(srcmode,srcreg,size,"r")
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))
opcode=0x4200+(size<<6)+(srcmode<<3)+srcreg;iname="CLR"+size_name(size)+" "+amode_name(srcmode,srcreg,size)
code=amode_write(srcmode,srcreg,size,"0")
code+="state.sr|=4;"
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))
opcode=0x4a00+(size<<6)+(srcmode<<3)+srcreg;iname="TST"+size_name(size)+" "+amode_name(srcmode,srcreg,size)
code=amode_read(srcmode,srcreg,size,true)
code+=set_condition_flags_data(size,"s")
insert_inst2(opcode,code,iname,4+effective_address_calculation_time(srcmode,srcreg,size))
if(size==0)
{opcode=0x4ac0+(srcmode<<3)+srcreg;iname="TAS.B"+" "+amode_name(srcmode,srcreg,0)
code=amode_read(srcmode,srcreg,0,true)
code+=set_condition_flags_data(0,"s")
code+=amode_write(srcmode,srcreg,0,"s | 0x80")
insert_inst2(opcode,code,iname,(srcmode==MODE_DREG)?4:(14+effective_address_calculation_time(srcmode,srcreg,size)))}}}}}}
function build_lea()
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){for(var reg=0;reg<8;reg++){if(valid_calc_effective_address(srcmode,srcreg))
{var opcode=0x41C0+(reg<<9)+(srcmode<<3)+srcreg;var iname="LEA "+amode_name(srcmode,srcreg,1)+",A"+reg
var code=effective_address_calc(srcmode,srcreg)
code+="state.a"+reg+"=z;"
if(opcode==0x41FA&&enable_kludge_in_lea_d_pc_a0){code+="if((o == 7) && (state.pc < 0x40000) && (rw(state.pc) == 0x4210) && (rw(state.pc+2) == 0x6000) && (rw(state.pc+4) == 0x000A) && (rw(state.pc+6) == 0x0000) && (rw(state.pc+8) == 0x4E4C) && (rw(state.pc+10) == 0x534F) && (rw(state.pc+12) == 0x4AFC)) { state.pc += 14; }";}
insert_inst(opcode,code,iname)}}}}}
function build_cmpi()
{for(var size=0;size<3;size++){for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(valid_dest(srcmode,srcreg))
{var opcode=0xC00+(size<<6)+(srcmode<<3)+srcreg;var iname="CMPI"+size_name(size)+size_imm(size)+amode_name(srcmode,srcreg,size)
var code=read_pc(size,"subtrahend",true)
var cost=8;if(size==2){cost+=4;if(srcmode==MODE_DREG){cost+=2;}}
code+=amode_read(srcmode,srcreg,size,true)
if(size==0)code+="cmpb(subtrahend, s);"
if(size==1)code+="cmpw(subtrahend, s);"
if(size==2)code+="cmpl(subtrahend, s);"
insert_inst2(opcode,code,iname,cost+effective_address_calculation_time(srcmode,srcreg,size))}}}}}
function build_movem()
{for(var reg=0;reg<8;reg++){for(var mode=0;mode<8;mode++){for(var size=1;size<3;size++){var actualsize=size*2
if(mode==MODE_AREG_INDIRECT||mode==MODE_AREG_POSTINC||mode==MODE_AREG_OFFSET||mode==MODE_AREG_INDEX||(mode==MODE_MISC&&(reg==MISCMODE_SHORT||reg==MISCMODE_LONG||reg==MISCMODE_PC_OFFSET||reg==MISCMODE_PC_INDEX)))
{var opcode=0x4c80+((size-1)<<6)+(mode<<3)+reg
var iname="MOVEM"+size_name(size+1)+" "+amode_name(mode,reg,size)+",regs"
var code=read_pc(1,"regs",true)
if(mode==MODE_AREG_POSTINC)
code+="var newval = load_multiple_postinc(state.a"+reg+", regs, "+size+"); state.a"+reg+" = newval;";else
{code+=effective_address_calc(mode,reg);code+="load_multiple(z,regs,"+size+");"}
insert_inst(opcode,code,iname)}
if(mode==MODE_AREG_INDIRECT||mode==MODE_AREG_PREDEC||mode==MODE_AREG_OFFSET||mode==MODE_AREG_INDEX||(mode==MODE_MISC&&(reg==MISCMODE_SHORT||reg==MISCMODE_LONG)))
{var opcode=0x4880+((size-1)<<6)+(mode<<3)+reg
var iname="MOVEM"+size_name(size)+" regs,"+amode_name(mode,reg,size)
var code=read_pc(1,"regs",true)
if(mode==MODE_AREG_PREDEC)
{iname=iname.replace("regs","regspredec");code+="var newval = store_multiple_predec(state.a"+reg+", regs, "+size+"); state.a"+reg+" = newval;";}
else
{code+=effective_address_calc(mode,reg)
code+="store_multiple(z, regs, "+size+");"}
insert_inst(opcode,code,iname)}}}}}
function build_cmpm()
{for(var src=0;src<8;src++){for(var dest=0;dest<8;dest++){for(var size=0;size<3;size++){var opcode=0xB108+(dest<<9)+(size<<6)+src
var iname="CMPM"+size_name(size)+" (A"+src+")+,(A"+dest+")+'"
var code=amode_read(MODE_AREG_POSTINC,src,size,true)
code+="var u=s;"
code+=amode_read(MODE_AREG_POSTINC,dest,size,true)
if(size==0)code+="cmpb(u,s);"
if(size==1)code+="cmpw(u,s);"
if(size==2)code+="cmpl(u,s);"
insert_inst2(opcode,code,iname,(size==2)?20:12)}}}}
function build_bcd()
{for(var src=0;src<8;src++){for(var dest=0;dest<8;dest++){for(var m=1;m>=0;m--){for(var sub=0;sub<=1;sub++){var operation=sub==0?"ABCD":"SBCD"
var opcode=0x8100+(dest<<9)+src
if(operation=="ABCD")opcode+=0x4000
var iname=""
var cost=0;if(m!=0)
{opcode+=8
iname=operation+" -(A"+src+"),-(A"+dest+")"
cost=18;}
else
{iname=operation+" D"+src+",D"+dest
cost=6;}
var code=""
if(m!=0)
{code=amode_read(MODE_AREG_PREDEC,src,0,true)
code+="var other = s;"
code+=amode_read(MODE_AREG_PREDEC,dest,0,true)
code+=amode_write(MODE_AREG_INDIRECT,dest,0,operation.toLowerCase()+"(s,other)")}
else
{code="state.d"+dest+"+="+operation.toLowerCase()+"(state.d"+dest+",state.d"+src+")-state.d"+dest+"&0xFF;"}
insert_inst2(opcode,code,iname,cost)}}}}
for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(valid_dest(srcmode,srcreg)&&srcmode!=MODE_AREG)
{opcode=0x4800+(srcmode<<3)+srcreg;iname="NBCD "+amode_name(srcmode,srcreg,0)
code=amode_read(srcmode,srcreg,0,false)
code+="var r=nbcd(s);"
code+=amode_write(srcmode,srcreg,0,"r")
insert_inst2(opcode,code,iname,(srcmode==MODE_DREG)?6:(8+effective_address_calculation_time(srcmode,srcreg,0)))}}}}
function build_movesrccr()
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(valid_source(srcmode,srcreg)&&srcmode!=MODE_AREG)
{var opcode=0x46C0+(srcmode<<3)+srcreg;var iname="MOVE "+amode_name(srcmode,srcreg,1)+",SR"
var cost=12+effective_address_calculation_time(srcmode,srcreg,0);insert_inst2(opcode,"if(state.sr&0x2000==0)fire_cpu_exception(8);"+amode_read(srcmode,srcreg,1,true)+"update_sr(s);",iname,cost)
opcode=0x44C0+(srcmode<<3)+srcreg;iname="MOVE "+amode_name(srcmode,srcreg,0)+",CCR"
insert_inst2(opcode,amode_read(srcmode,srcreg,0,true)+"state.sr = (state.sr&0xFF00) + s;",iname,cost)}
if(valid_dest(srcmode,srcreg)&&srcmode!=MODE_AREG)
{var opcode=0x40C0+(srcmode<<3)+srcreg;var iname="MOVE SR,"+amode_name(srcmode,srcreg,1)
var cost=(srcmode==MODE_DREG)?6:8;insert_inst2(opcode,amode_write(srcmode,srcreg,1,"state.sr"),iname,cost+effective_address_calculation_time(srcmode,srcreg,1))}}}}
function build_exchange(xtype,ytype,bits)
{for(var x=0;x<8;x++){for(var y=0;y<8;y++){var opcode=bits+(x<<9)+y
var iname="EXG "+xtype+x+","+ytype+y
var xstr="state."+xtype.toLowerCase()+x
var ystr="state."+ytype.toLowerCase()+y
var code="var e="+xstr+";"
code+=xstr+"="+ystr+";"
code+=ystr+"=e;"
insert_inst2(opcode,code,iname,6)}}}
function build_jmpjsr()
{for(var mode=0;mode<8;mode++){for(var reg=0;reg<8;reg++){if(valid_calc_effective_address(mode,reg)){for(var jsr=1;jsr>=0;jsr--){var opcode=0x4EC0+(mode<<3)+reg-jsr*0x40;var iname=(jsr==1?"JSR ":"JMP ")+amode_name(mode,reg,0)
var code=effective_address_calc(mode,reg)
if(jsr==1)
code+=amode_write(4,7,2,"state.pc")
code+="state.pc=z;"
insert_inst(opcode,code,iname)}}}}}
function build_pea()
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){if(valid_calc_effective_address(srcmode,srcreg))
{var opcode=0x4840+(srcmode<<3)+srcreg;var iname="PEA "+amode_name(srcmode,srcreg,0)
insert_inst(opcode,effective_address_calc(srcmode,srcreg)+amode_write(4,7,2,"z"),iname)}}}}
function build_swap()
{for(var reg=0;reg<8;reg++){var code="var l = state.d"+reg+"&65535;"
code+="var h = state.d"+reg+" >>> 16;"
code+="state.d"+reg+" = (l * 65536) + h;"
insert_inst2(0x4840+reg,code,"SWAP D"+reg,4)}}
function build_chk()
{for(var srcmode=0;srcmode<8;srcmode++){for(var srcreg=0;srcreg<8;srcreg++){for(var reg=0;reg<8;reg++){if(valid_dest(srcmode,srcreg)&&srcmode!=MODE_AREG)
{var opcode=0x4180+(reg<<9)+(srcmode<<3)+srcreg;var iname="CHK.W "+amode_name(srcmode,srcreg,1)+",D"+reg
var code=amode_read(srcmode,srcreg,1,true)
code+="if (state.d"+reg+"<0) { state.sr |= 8; fire_cpu_exception(6); } if(state.d"+reg+"> s) { state.sr &= 0xFFF7; fire_cpu_exception(6); }"
insert_inst(opcode,code,iname)}}}}}
function build_initial_instructions_handlers()
{var i;for(i=0;i<0xA000;i++){cpu.t[i]=unhandled_instruction;cpu.n[i]='UNKNOWN';cpu.cycles[i]=0;}
for(i=0xA000;i<=0xAFFF;i++){cpu.t[i]=aline;cpu.n[i]="ALINE "+to_hex(i,3);cpu.cycles[i]=34;}
for(i=0xB000;i<0xF000;i++){cpu.t[i]=unhandled_instruction;cpu.n[i]='UNKNOWN';cpu.cycles[i]=0;}
for(i=0xF000;i<=0xFFFF;i++){cpu.t[i]=fline;cpu.n[i]="FLINE "+to_hex(i,3);cpu.cycles[i]=34;}}
function build_all_instructions()
{build_initial_instructions_handlers();build_moveq();build_addsubq();build_moves("MOVE.L",2,0x2000);build_moves("MOVE.W",1,0x3000);build_moves("MOVE.B",0,0x1000);build_movep();build_conditionals("if(true)","T",0)
build_conditionals("if(false)","F",1)
build_conditionals("if(!(state.sr&5))","HI",2)
build_conditionals("if(state.sr&5)","LS",3)
build_conditionals("if(!(state.sr&1))","CC",4)
build_conditionals("if(state.sr&1)","CS",5)
build_conditionals("if(!(state.sr&4))","NE",6)
build_conditionals("if(state.sr&4)","EQ",7)
build_conditionals("if(!(state.sr&2))","VC",8)
build_conditionals("if(state.sr&2)","VS",9)
build_conditionals("if(!(state.sr&8))","PL",10)
build_conditionals("if(state.sr&8)","MI",11)
build_conditionals("if(((state.sr&10)==0)||((state.sr&10)==10))","GE",12)
build_conditionals("if(((state.sr&10)==8)||((state.sr&10)==2))","LT",13)
build_conditionals("if((((state.sr&10)==0)||((state.sr&10)==10))&(!(state.sr&4)))","GT",14)
build_conditionals("if((state.sr&4)||((state.sr&10)==8)||((state.sr&10)==2))","LE",15)
build_calc("EOR",0xB000)
build_calc("ADD",0xD000)
build_calc("AND",0xC000)
build_calc("SUB",0x9000)
build_calc("OR",0x8000)
build_muldiv("DIVS",0x81C0,"divs",158)
build_muldiv("DIVU",0x80C0,"divu",140)
build_muldiv("MULS",0xC1C0,"muls",70)
build_muldiv("MULU",0xC0C0,"mulu",70)
build_bit_operation("BCHG",0x840,8,8)
build_bit_operation("BCLR",0x880,10,8)
build_bit_operation("BSET",0x8C0,8,8)
build_bit_operation("BTST",0x800,6,4)