-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
1631 lines (1549 loc) · 49.6 KB
/
cpu.go
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
package main
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"log"
"math/rand/v2"
"os"
"strconv"
"strings"
"time"
"github.com/gopxl/pixel"
"github.com/gopxl/pixel/imdraw"
"github.com/gopxl/pixel/pixelgl"
"golang.org/x/image/colornames"
)
type CPU struct {
program_counter int
cycles int
stack_pointer int
accumulator int
register_x int
register_y int
cpu_status Status
}
var memory Memory
var const_head Const = Const{}
var head Program_Code = Program_Code{}
func string_to_int(str string) int {
split := strings.TrimPrefix(str, "#")
if len(strings.Split(split, "$")) > 1 {
split = strings.TrimPrefix(split, "$")
split = strings.TrimLeft(split, "0")
value, err := strconv.ParseInt(split, 16, 64)
if err != nil {
fmt.Printf("Conversion failed: %s\n", err)
} else {
return int(value)
}
}
val, er := strconv.Atoi(split)
if er != nil {
fmt.Printf("Conversion failed: %s\n", er)
}
return int(val)
}
func split(val string, char string) []string {
split_val := strings.Split(val, char)
ret_val := []string{}
for i := 0; i < len(split_val); i++ {
if !(split_val[i] == "" || split_val[i] == " ") {
ret_val = append(ret_val, split_val[i])
}
}
return ret_val
}
func get_addr_mode_val(split_line []string) (addr_mode, int) {
var ret_addr_mode addr_mode
var ret_value int
if split_line[1] == "a" {
ret_addr_mode = accumulator_type
ret_value = 0
} else {
split_addr_1 := split(split_line[1], ",")
if strings.Contains(split_line[1], "#") {
ret_addr_mode = immediate_type
ret_value = string_to_int(split_line[1])
} else if len(split_addr_1) > 1 && !strings.Contains(split_line[1], "(") {
ret_value = string_to_int(split_addr_1[0])
if strings.ToUpper(split_addr_1[1]) == "Y" {
if len(split(split_addr_1[0], "")) == 5 {
ret_addr_mode = absolute_y_type
} else {
ret_addr_mode = zero_page_y_type
}
} else if strings.ToUpper(split_addr_1[1]) == "X" {
if len(split(split_addr_1[0], "")) == 5 {
ret_addr_mode = absolute_x_type
} else {
ret_addr_mode = zero_page_x_type
}
}
} else {
if !strings.Contains(split_line[1], "(") {
ret_value = string_to_int(split_line[1])
if len(split(split_addr_1[0], "")) == 5 {
ret_addr_mode = absolute_type
} else {
ret_addr_mode = zero_page_type
}
} else {
addr := strings.TrimLeft(split_line[1], "(")
splita := split(addr, "")
if splita[len(splita)-1] == ")" {
ret_addr_mode = indexed_indirect_type
addr = split(addr, ",")[0]
ret_value = string_to_int(addr)
} else {
addr = split(addr, ",")[0]
addr = strings.TrimRight(addr, ")")
ret_value = string_to_int(addr)
ret_addr_mode = indirect_indexed_type
}
}
}
}
return ret_addr_mode, ret_value
}
func find_const(val string) string {
var local_ptr = &const_head
for local_ptr.next != nil {
val = strings.Replace(val, local_ptr.name, local_ptr.val, 1)
local_ptr = local_ptr.next
}
return val
}
func (cpu *CPU) read_asm() {
f, err := os.Open(os.Args[3])
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
var ptr *Program_Code
ptr = &head
var var_ptr *Const
var_ptr = &const_head
index := 0
for scanner.Scan() {
curr_line := scanner.Text()
curr_line = strings.TrimSpace(curr_line)
if curr_line == "" {
continue
} else if strings.Contains(curr_line, "define") {
split_line := split(curr_line, " ")
var_ptr.name = split_line[1]
if split_line[1] == "sysRandom" {
var_ptr.val = strconv.FormatInt(int64(rand.IntN(255)), 16)
var_ptr.val = "#$" + var_ptr.val
} else {
var_ptr.val = split_line[2]
}
var_ptr.next = &Const{}
var_ptr = var_ptr.next
continue
}
ptr.index = index
cpu.program_counter++
if strings.Contains(curr_line, ":") {
ptr.code_type = function_definition
ptr.destination = split(curr_line, ":")[0]
} else {
ptr.code_type = op_code
split_line := split(curr_line, " ")
split_line[0] = strings.ToUpper(split_line[0])
if len(split_line) > 1 {
if !strings.Contains(split_line[1], "$") {
split_line[1] = find_const(split_line[1])
}
if strings.Contains(split_line[1], "$") || strings.Contains(split_line[1], "#") || split_line[1] == "a" {
var input_addr_1 int
ptr.addr_mode, input_addr_1 = get_addr_mode_val(split_line)
switch split_line[0] {
case "LSR":
ptr.op_code = LSR_cmd
ptr.mem_1 = input_addr_1
case "ORA":
ptr.op_code = ORA_cmd
ptr.mem_1 = input_addr_1
case "EOR":
ptr.op_code = EOR_cmd
ptr.mem_1 = input_addr_1
case "ASL":
ptr.op_code = ASL_cmd
ptr.mem_1 = input_addr_1
case "ROR":
ptr.op_code = ROR_cmd
ptr.mem_1 = input_addr_1
case "ROL":
ptr.op_code = ROL_cmd
ptr.mem_1 = input_addr_1
case "AND":
ptr.op_code = AND_cmd
ptr.mem_1 = input_addr_1
case "DEC":
ptr.op_code = DEC_cmd
ptr.mem_1 = input_addr_1
case "LDX":
ptr.op_code = LDX_cmd
ptr.mem_1 = input_addr_1
case "LDY":
ptr.op_code = LDY_cmd
ptr.mem_1 = input_addr_1
case "LDA":
ptr.op_code = LDA_cmd
ptr.mem_1 = input_addr_1
case "BIT":
ptr.op_code = BIT_cmd
ptr.mem_1 = input_addr_1
case "SBC":
ptr.op_code = SBC_cmd
ptr.mem_1 = input_addr_1
case "STA":
ptr.op_code = STA_cmd
ptr.mem_1 = input_addr_1
case "ADC":
ptr.op_code = ADC_cmd
ptr.mem_1 = input_addr_1
case "INC":
ptr.op_code = INC_cmd
ptr.mem_1 = input_addr_1
case "STY":
ptr.op_code = STY_cmd
ptr.mem_1 = input_addr_1
case "STX":
ptr.op_code = STX_cmd
ptr.mem_1 = input_addr_1
case "CPX":
ptr.op_code = CPX_cmd
ptr.mem_1 = input_addr_1
case "CMP":
ptr.op_code = CMP_cmd
ptr.mem_1 = input_addr_1
case "CPY":
ptr.op_code = CPY_cmd
ptr.mem_1 = input_addr_1
case "JMP":
//copied from below!
ptr.op_code = JMP_cmd
if strings.Contains(split_line[1], "(") {
ptr.addr_mode = indirect_type
addr := strings.TrimLeft(split_line[1], "(")
addr = strings.TrimRight(addr, ")")
ptr.destination = addr
} else {
ptr.addr_mode = absolute_type
ptr.destination = split_line[1]
}
}
} else {
ptr.code_type = op_code
switch split_line[0] {
case "BNE":
ptr.op_code = BNE_cmd
ptr.destination = split_line[1]
case "BEQ":
ptr.op_code = BEQ_cmd
ptr.destination = split_line[1]
case "BVC":
ptr.op_code = BVC_cmd
ptr.destination = split_line[1]
case "BMI":
ptr.op_code = BMI_cmd
ptr.destination = split_line[1]
case "BVS":
ptr.op_code = BVS_cmd
ptr.destination = split_line[1]
case "BCS":
ptr.op_code = BCS_cmd
ptr.destination = split_line[1]
case "BCC":
ptr.op_code = BCC_cmd
ptr.destination = split_line[1]
case "BPL":
ptr.op_code = BPL_cmd
ptr.destination = split_line[1]
case "JSR":
ptr.op_code = JSR_cmd
ptr.destination = split_line[1]
case "JMP":
//copied from above!
ptr.op_code = JMP_cmd
if strings.Contains(split_line[1], "(") {
ptr.addr_mode = indirect_type
addr := strings.TrimLeft(split_line[1], "(")
addr = strings.TrimRight(addr, ")")
ptr.destination = addr
} else {
ptr.addr_mode = absolute_type
ptr.destination = split_line[1]
}
}
}
} else {
split_line := split(curr_line, " ")
split_line[0] = strings.ToUpper(split_line[0])
switch split_line[0] {
case "PHP":
ptr.op_code = PHP_cmd
case "PLP":
ptr.op_code = PLP_cmd
case "LSR":
ptr.op_code = LSR_cmd
case "ROR":
ptr.op_code = ROR_cmd
case "ROL":
ptr.op_code = ROL_cmd
case "RTS":
ptr.op_code = RTS_cmd
case "BRK":
ptr.op_code = BRK_cmd
case "TAX":
ptr.op_code = TAX_cmd
case "TAY":
ptr.op_code = TAY_cmd
case "TYA":
ptr.op_code = TYA_cmd
case "INX":
ptr.op_code = INX_cmd
case "INY":
ptr.op_code = INY_cmd
case "DEX":
ptr.op_code = DEX_cmd
case "DEY":
ptr.op_code = DEY_cmd
case "SEI":
ptr.op_code = SEI_cmd
case "SED":
ptr.op_code = SED_cmd
case "SEC":
ptr.op_code = SEC_cmd
case "CLC":
ptr.op_code = CLC_cmd
case "CLD":
ptr.op_code = CLD_cmd
case "CLI":
ptr.op_code = CLI_cmd
case "CLV":
ptr.op_code = CLV_cmd
case "PLA":
ptr.op_code = PLA_cmd
case "PHA":
ptr.op_code = PHA_cmd
case "TXS":
ptr.op_code = TXS_cmd
case "TSX":
ptr.op_code = TSX_cmd
case "TXA":
ptr.op_code = TXA_cmd
case "NOP":
ptr.op_code = NOP_cmd
}
}
}
ptr.next = &Program_Code{}
ptr = ptr.next
index++
}
dump_program()
dump_contents(cpu)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func reset() {
cpu.stack_pointer = 0xFD
cpu.cycles = 7
}
func twos_compliment(val int) int {
//try two's compliment
if val&0b10000000 != 0 {
inversion := val & 0b01111111
in_val := 0b1111111 ^ inversion
return -1 * in_val
} else {
return val
}
}
const iNESFileMagic = 0x1a53454e
type iNESFileHeader struct {
Magic uint32 // iNES magic number
NumPRG byte // number of PRG-ROM banks (16KB each)
NumCHR byte // number of CHR-ROM banks (8KB each)
Control1 byte // control bits
Control2 byte // control bits
NumRAM byte // PRG-RAM size (x 8KB)
_ [7]byte // unused padding
}
func (cpu *CPU) LoadNESFile(path string, ppu *PPU, win *pixelgl.Window) {
// open file
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
//was this the fogelman code??
// read file header
header := iNESFileHeader{}
if err := binary.Read(file, binary.LittleEndian, &header); err != nil {
return
}
// verify header magic number
if header.Magic != iNESFileMagic {
return
}
// mapper type
// mapper1 := header.Control1 >> 4
// mapper2 := header.Control2 >> 4
// mapper := mapper1 | mapper2<<4
// // mirroring type
// mirror1 := header.Control1 & 1
// mirror2 := (header.Control1 >> 3) & 1
// mirror := mirror1 | mirror2<<1
// // battery-backed RAM
// battery := (header.Control1 >> 1) & 1
// read trainer if present (unused)
if header.Control1&4 == 4 {
trainer := make([]byte, 512)
if _, err := io.ReadFull(file, trainer); err != nil {
return
}
}
// read prg-rom bank(s)
prg := make([]byte, int(header.NumPRG)*16384)
if _, err := io.ReadFull(file, prg); err != nil {
return
}
// read chr-rom bank(s)
chr := make([]byte, int(header.NumCHR)*8192)
if _, err := io.ReadFull(file, chr); err != nil {
return
}
// provide chr-rom/ram if not in file
if header.NumCHR == 0 {
chr = make([]byte, 8192)
}
i := 0
for cnt := 0xC000; cnt < 0xFABF; cnt++ {
*memory.absolute_addr(cnt, cpu) = int(prg[cnt-0xC000])
}
reset()
cpu.cpu_status.interrupt_disable = true
cpu.program_counter = 0xC000
for {
imd := imdraw.New(nil)
var x float64 = 0
var y float64 = 0
for mem_ad := 0; mem_ad < 8; mem_ad++ {
for ad_bit := 0b10000000; ad_bit > 0; ad_bit = ad_bit >> 1 {
curr_color := ppu.chr_rom[mem_ad] & byte(ad_bit)
curr_color = curr_color << 4
curr_color += ppu.chr_rom[mem_ad+8] & byte(ad_bit)
println(curr_color)
imd.Color = systemColorPalette[curr_color]
imd.Push(pixel.V(x, y))
imd.Push(pixel.V(x+1, y+1))
imd.Rectangle(1)
x += 2
y += 2
}
}
if !win.Closed() {
win.Clear(colornames.Aliceblue)
imd.Draw(win)
win.Update()
}
// if cpu.program_counter == 0xE3BD {
// dump_contents(cpu)
// }
// index := fmt.Sprintf("%x", cpu.program_counter)
opcode := *memory.absolute_addr(cpu.program_counter, cpu)
// if run_tests(fmt.Sprint(strings.ToUpper(index), " ", "opcode", " ", "mem", " A:", strings.ToUpper(fmt.Sprintf("%x", cpu.accumulator)), " X:", strings.ToUpper(fmt.Sprintf("%x", cpu.register_x)), " Y:", strings.ToUpper(fmt.Sprintf("%x", cpu.register_y)), " P:", strings.ToUpper(fmt.Sprintf("%x", get_flag_sum())), " SP:", strings.ToUpper(fmt.Sprintf("%x", cpu.stack_pointer)), " CYC:", cpu.cycles), i) {
// break
// }
i++
// ptr.index = program_counter
switch opcode {
case 0x69:
ADC(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0x65:
ADC(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0x75:
ADC(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x6D:
ADC(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x7D:
ADC(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x79:
ADC(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x61:
ADC(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x71:
ADC(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0x29:
AND(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0x25:
AND(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0x35:
AND(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x2D:
AND(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x3D:
AND(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x39:
AND(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x21:
AND(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x31:
AND(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0x0A:
ASL(&cpu.accumulator)
cpu.program_counter++
cpu.cycles += 2
case 0x06:
ASL(memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 5
case 0x16:
ASL(memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x0E:
ASL(memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x1E:
ASL(memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), false, cpu))
cpu.program_counter++
cpu.cycles += 7
case 0x90:
cpu.program_counter++
if !cpu.cpu_status.carry_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0xB0:
cpu.program_counter++
if cpu.cpu_status.carry_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0xF0:
cpu.program_counter++
if cpu.cpu_status.zero_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0x24:
BIT(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0x2C:
BIT(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x30:
cpu.program_counter++
if cpu.cpu_status.negative_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0xD0:
cpu.program_counter++
if !cpu.cpu_status.zero_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0x10:
cpu.program_counter++
if !cpu.cpu_status.negative_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0x00:
cpu.cpu_status.break_command = true
// cpu.cycles += 7
case 0x50:
cpu.program_counter++
if !cpu.cpu_status.overflow_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0x70:
cpu.program_counter++
if cpu.cpu_status.overflow_flag {
cpu.cycles++
break_to("", twos_compliment(int(prg[cpu.program_counter-0xC000]))+cpu.program_counter+1, cpu.program_counter-0xA000)
} else {
cpu.program_counter++
}
cpu.cycles += 2
case 0x18:
CLC()
cpu.program_counter++
cpu.cycles += 2
case 0xD8:
CLD()
cpu.program_counter++
cpu.cycles += 2
case 0x58:
CLI()
cpu.program_counter++
cpu.cycles += 2
case 0xB8:
CLV()
cpu.program_counter++
cpu.cycles += 2
case 0xC9:
CMP(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xC5:
CMP(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xD5:
CMP(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xCD:
CMP(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xDD:
CMP(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xD9:
CMP(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xC1:
CMP(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xD1:
CMP(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0xE0:
CPX(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xE4:
CPX(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xEC:
CPX(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xC0:
CPY(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xC4:
CPY(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xCC:
CPY(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xC6:
DEC(memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 5
case 0xD6:
DEC(memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xCE:
DEC(memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xDE:
DEC(memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), false, cpu))
cpu.program_counter++
cpu.cycles += 7
case 0xCA:
DEX()
cpu.program_counter++
cpu.cycles += 2
case 0x88:
DEY()
cpu.program_counter++
cpu.cycles += 2
case 0x49:
EOR(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0x45:
EOR(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0x55:
EOR(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x4D:
EOR(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x5D:
EOR(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x59:
EOR(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x41:
EOR(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x51:
EOR(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0xE6:
INC(memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 5
case 0xF6:
INC(memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xEE:
INC(memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xFE:
INC(memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), false, cpu))
cpu.program_counter++
cpu.cycles += 7
case 0xE8:
INX()
cpu.program_counter++
cpu.cycles += 2
case 0xC8:
INY()
cpu.program_counter++
cpu.cycles += 2
case 0x4C:
dest := double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu)))
break_to("", dest, dest)
cpu.cycles += 3
case 0x6C:
mem_addr := double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu)))
upper_mem := mem_addr + 1
if mem_addr&0b11111111 == 0xFF {
upper_mem = mem_addr & 0b1111111100000000
}
lower := (*memory.absolute_addr(mem_addr, cpu))
upper := (*memory.absolute_addr(upper_mem, cpu))
loc := upper << 8
loc += lower
break_to("", loc, loc)
cpu.cycles += 5
case 0x20:
next := cpu.program_counter + 2
lower := next & 0b11111111
upper := next >> 8
memory.system_stack[cpu.stack_pointer] = upper
cpu.stack_pointer--
memory.system_stack[cpu.stack_pointer] = lower
cpu.stack_pointer--
dest := double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu)))
break_to("", dest, dest)
cpu.cycles += 6
case 0xA9:
LDA(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xA5:
LDA(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xB5:
LDA(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xAD:
LDA(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xBD:
LDA(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xB9:
LDA(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xA1:
LDA(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0xB1:
LDA(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0xA2:
LDX(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xA6:
LDX(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xB6:
LDX(*memory.zero_page_y_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xAE:
LDX(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xBE:
LDX(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xA0:
LDY(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0xA4:
LDY(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0xB4:
LDY(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xAC:
LDY(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0xBC:
LDY(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x4A:
LSR(&cpu.accumulator)
cpu.program_counter++
cpu.cycles += 2
case 0x46:
LSR(memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 5
case 0x56:
LSR(memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x4E:
LSR(memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x5E:
LSR(memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), false, cpu))
cpu.program_counter++
cpu.cycles += 7
case 0xEA:
time.Sleep(5 * time.Microsecond)
cpu.program_counter++
cpu.cycles += 2
case 0x09:
ORA(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))))
cpu.program_counter++
cpu.cycles += 2
case 0x05:
ORA(*memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 3
case 0x15:
ORA(*memory.zero_page_x_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x0D:
ORA(*memory.absolute_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x1D:
ORA(*memory.absolute_x_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), true, cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x19:
ORA(*memory.absolute_y_addr(double_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)), byte(*memory.absolute_addr(cpu.program_counter+2, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 4
case 0x01:
ORA(*memory.indexed_indirect_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 6
case 0x11:
ORA(*memory.indirect_indexed_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu))), cpu))
cpu.program_counter++
cpu.cycles += 5
case 0x48:
PHA()
cpu.program_counter++
cpu.cycles += 3
case 0x08:
PHP()
cpu.program_counter++
cpu.cycles += 3
case 0x68:
PLA()
cpu.program_counter++
cpu.cycles += 4
case 0x28:
PLP()
cpu.program_counter++
cpu.cycles += 4
case 0x2A:
ROL(&cpu.accumulator)
cpu.program_counter++
cpu.cycles += 2
case 0x26:
ROL(memory.zero_page_addr(single_addr(byte(*memory.absolute_addr(cpu.program_counter+1, cpu)))))
cpu.program_counter++
cpu.cycles += 5