-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameboy Basic.cpp
1978 lines (1830 loc) · 62.5 KB
/
Gameboy Basic.cpp
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
// Gameboy Basic.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <bitset>
#include <string>
#include <sstream>
//#include <SDL2/SDL.h>
#include <random>
//Modules
//MMU - Started Structure - Just Reporting ATM
//CPU -- Function mapping and Opcode mapping - Current TASK
//Timers -- 50% Need to actually update them
//Interrupts - 0%
//FlagHandling - DONE 100%
//Register combining - DONE 100%
//Gfx - Researching SDL Structure Groundwork ready
//Sound - 0%
//Input - 0%
//SDL support
//SDL_Renderer* renderer = NULL;
int min;
int max;
std::string ROM = "tetris.gb";
unsigned char memoryT[0xFFFF];
unsigned char memoryA[0xFFFF];
char letter;
unsigned char addr;
unsigned short opcode;
unsigned char opLen;
std::string functType;
unsigned short $aabb;
bool nullRender = true;
std::string Dm;
std::string Dreg;
std::string Sreg;
std::string Starget;
std::string Dtarget;
std::stringstream funcText;
///////////
unsigned short _16bitIn;
unsigned long fullOpcode;
unsigned char $bb;
unsigned char $aa;
signed char $xx;
unsigned char _8bitIn3;
unsigned char TempRf;
/////////////
class gb
{
public:
unsigned short Raf, Rbc, Rde, Rhl;
unsigned char Ra, Rf, Rb, Rc, Rd, Re, Rl, Rh;
unsigned short pc, sp;
unsigned char opclock = 0;
unsigned long cpuClock = 0;
unsigned char mathTC1 = 0;
unsigned char mathTC2 = 0;
unsigned char mathTS1 = 0;
unsigned char mathTS2 = 0;
unsigned char Z,N,H,C;
float Rm, Rt;
///////////////////////////////////////////////////////////////////////////////////////
//- Graphics Related code
//- This section can be enabled or disabled, but is meant for research
///////////////////////////////////////////////////////////////////////////////////////
/*
//
//SDL_Rect pixel[144][164]; // la pantalla es de 256 x 256 pero 144 y 164 son visibles
void gfxHandler()
{
unsigned int rowN;
unsigned int colN;
unsigned long gpuClock;
unsigned int modeClock;
gpuClock += opclock;
rowN = 0;
//cpu clock and times accesing Vram and OAM
//scanline access OAM - gpuMode 2 - 80 cycles
//scanline access VRAM - gpuMode 3 - 172 cyc
//horizontal blank - gpuMode 0 - 204 cycles
//one line scan and blank - 456 cycles
//vertical blank - gpuamode 1 - 4560 10lines
//full frame 70224
// 144 * 456 + (14 * 4560)?? - 204
int gpuMode;
switch (gpuMode) {
case 2: //access OAM for scanline read mode
if (gpuClock == 80) {
gpuClock = 0;
gpuMode = 3;
//semi proceso 1/2
}
break;
case 3: //access VRAM for scanline read mode
if (gpuClock == 172) {
gpuClock = 0;
gpuMode = 0;
//fin semi proceso 2/2
}
break;
case 0: // horizontal blanking
if (gpuClock == 204) {
//--scanea para regresar al punyo de partida
//pasa la info al framebuffer
gpuClock = 0;
rowN++;
if (rowN == 145) {
gpuMode = 1;
rowN = 0;
}
else
gpuMode = 2;
}
break;
case 1: //vertical blanking
if (gpuMode == 4560) {
//grab all lines from fb and place them on the screen
cpuClock = 0;
gpuClock = 0;
gpuMode = 2;
//ready for another frame
}
break;
}
//framebuffer shennanigans
//mem 8000-87FF TS1 0-127
//mem 8800-8FFF TS1 128-255 TS0 -1 a -127
//mem 9000-97FF TS0 0-127
//mem 9800-9BFF TM0
//mem 9C00-9FFF TM1
//tile system 8x8 dibujos de 64pixeles
//tile map 32x32 fondos de 256 scrolleables
//8x16 dice gbcuman
//unique tiles in mem 256
//total tiles 384
//paletas controlado por el background pallete GPU register
// valor color
//0 255,255,255
//1 192,192,192
//2 96,96,96
//3 0,0,0
// 7 6 | 5 4 | 3 2 | 1 0
// 3 2 1 0
//acceso a paleta, orden y preferencia
for (int z = 0; z<300; z++)
{
for (int j = 0; j<145; j++)
{
for (int i = 0; i<165; i++) {
pixel[j][i].h = 5;
pixel[j][i].w = 5;
pixel[j][i].x = 140 + (i * 5);
pixel[j][i].y = 200 + (j * 5);
//randomGen(0,255);
int cr, cg, cb;
//way of selecting RED
if ((rand() % 100) == 0){
cr = 96;
cg = 96;
cb = 96;
}
- // //way of selecting GREEN
// else if ((rand() % 100 == 1)){
// cr=0;
// cg=255;
// cb=0;
// }
//way of selecting BLUE
else if ((rand() % 100 == 2)) {
cr = 0;
cg = 0;
cb = 0;
}
//way of selecting Black
else if ((rand() % 100 == 4)) {
cr = 192;
cg = 192;
cb = 192;
}
//way of selecting white
else if ((rand() % 100 == 5)) {
cr = 255;
cg = 255;
cb = 255;
}
SDL_SetRenderDrawColor(renderer, cr, cg, cb, 255);
SDL_RenderFillRect(renderer, &pixel[j][i]);
//SDL_RenderPresent(renderer);
//SDL_Delay(5);
}
}
SDL_RenderPresent(renderer);
}//Video support
}//gfxHandler
*/
///////////////////////////////////////////////////////////////////////////////////////
//- Im using the MMU as a BP for when the CPU is mature enough to start accesing VRAM
//- This will be a Major milestone in the project
///////////////////////////////////////////////////////////////////////////////////////
void MMU() {
if (pc < 0x4000) {
std::cout << "MMU ROM bank0" << "\n";
if (pc == 0x2817)
{
std::cout << "//////////////////////////////////////////////////////////////////////" << "\n";
std::cout << "Drawing Starting\n";
std::cout << "//////////////////////////////////////////////////////////////////////" << "\n";
std::cin.get();
}
}
else if (pc >= 0x4000 && pc < 0x8000) { std::cout << "MMU ROM bank1" << "\n"; }
else if (pc >= 0x8000 && pc < 0xA000) { std::cout << "MMU GFX" << "\n"; std::cin.get(); } //Break point GFX
else if (pc >= 0xA000 && pc < 0xC000) { std::cout << "MMU EXT RAM bank" << "\n"; }
else if (pc >= 0xC000 && pc < 0xE000) { std::cout << "MMU WORKING RAM" << "\n"; }
else if (pc >= 0xE000 && pc < 0xFE00) { std::cout << "MMU WORKING RAM SHADOW" << "\n"; }
else if (pc >= 0xFE00 && pc < 0xFF00) { std::cout << "MMU GFX SPRITES" << "\n"; std::cin.get(); } //Break point GFX
else if (pc >= 0xFF00 && pc < 0xFF80) { std::cout << "MMU INPUT" << "\n"; }
else if (pc >= 0xFF80 && pc < 0xFFFF) { std::cout << "MMU ZERO PAGE RAM" << "\n"; }
}
///////////////////////////////////////////////////////////////////////////////////////
//- Register Combiner and Flag managing
//- This Section is DONE
///////////////////////////////////////////////////////////////////////////////////////
#pragma region FlagCalculator
void FLGH(unsigned char Z, unsigned char N, unsigned char H, unsigned char C, unsigned short calcT1, unsigned short calcT2, unsigned char Optype) {
//opTypes
// 1 - ADD
// 2 - SUB
// 3 - Comp
// 4 - AND
// 5 - OR
// 6 - XOR
unsigned short TT1, TT2, Tresult;
TT1 = calcT1;
TT2 = calcT2;
std::bitset<8> Rf_as_bit(Rf);
switch (Z) {
case 0: Rf_as_bit.reset(7); break;
case 1: Rf_as_bit.set(7); break;
case 2: //Calculate Flag based on result
switch (Optype) {
case 1:
if (TT1 == 0 && TT2 == 0) { Rf_as_bit.set(7); }
else { Rf_as_bit.reset(7); }
break;
case 2:
Rf_as_bit.set(6);
Tresult = TT1 - TT2;
if (Tresult == 0) { Rf_as_bit.set(7); } else { Rf_as_bit.reset(7); }
break;
case 3:
Tresult = TT1 - TT2;
if (Tresult == 0) { Rf_as_bit.set(7); } else { Rf_as_bit.reset(7); }
break;
case 4:
Tresult = TT1 & TT2;
if (Tresult == 0) { Rf_as_bit.set(7); } else { Rf_as_bit.reset(7); }
break;
case 5:
Tresult = TT1 | TT2;
if (Tresult == 0) { Rf_as_bit.set(7); } else { Rf_as_bit.reset(7); }
break;
case 6:
Tresult = TT1 ^ TT2;
if (Tresult == 0) { Rf_as_bit.set(7); } else { Rf_as_bit.reset(7); }
break;
} // End Switch Z - (Optype)
break;
case 3: //Ignore Flag
break;
} //END switch (Z)
switch (N) {
case 0: Rf_as_bit.reset(6); break;
case 1: Rf_as_bit.set(6); break;
case 2: //Calculate Flag based on result
if (Optype == 2) { Rf_as_bit.set(6); } else { Rf_as_bit.reset(6); }
break;
case 3: //Ignore Flag
break;
} //END switch (N)
switch (H) {
case 0: Rf_as_bit.reset(5); break;
case 1: Rf_as_bit.set(5); break;
case 2: //Calculate Flag based on result
switch (Optype) {
case 1:
Tresult = TT1 + TT2;
if (((TT1 & 0xF) + (TT2 & 0xF)) & 0x10 == 0x10) { Rf_as_bit.set(5); }
else { Rf_as_bit.reset(5); }
break;
case 2:
Rf_as_bit.set(6);
if (((TT1 & 0xF) - (TT2 & 0xF)) & 0x10 == 0x10) { Rf_as_bit.set(5); std::cout << " H SET " << "\n"; }
else { Rf_as_bit.reset(5); }
break;
}// END N - Optype Switch
break;
case 3: //Ignore Flag
break;
} //END switch (H)
switch (C) {
case 0: Rf_as_bit.reset(4); break;
case 1: Rf_as_bit.set(4); break;
case 2: //Calculate Flag based on result
switch (Optype) {
case 1:
Tresult = TT1 + TT2;
if (Tresult > 0xFF) { Rf_as_bit.set(4); }
else {}
break;
case 2:
Rf_as_bit.set(6);
Tresult = TT1 - TT2;
if (Tresult < 0x0) { Rf_as_bit.set(4); }
else {}
break;
}// END C - Optype Switch
break;
case 3: //Ignore Flag
break;
} //END switch (C)
// Return Bits in Temp containers for Usage as Carry container
if (Rf_as_bit.test(7)) { TempRf = 0; std::bitset<8> Rf_as_Tbit(TempRf); Rf_as_Tbit.set(7); TempRf = Rf_as_Tbit.to_ulong(); }
//Display Box for Flags - Maybe Switch position
std::cout << "Z[" << Rf_as_bit.test(7) << "] N[" << Rf_as_bit.test(6) << "] H[" << Rf_as_bit.test(5) << "] C[" << Rf_as_bit.test(4) << "]\n";
Rf = Rf_as_bit.to_ulong();
}
#pragma endregion
#pragma region RegisterCombine
void Rcomb(unsigned char regPair) {
switch (regPair) {
case 0: Raf = (Ra << 8) | Rf; break;
case 1: Rbc = (Rb << 8) | Rc; break;
case 2: Rde = (Rd << 8) | Re; break;
case 3: Rhl = (Rh << 8) | Rl; break;
}
}
#pragma endregion
void RegComb() {
if (pc != 0x100) {
gb::Rcomb(0);
gb::Rcomb(1);
gb::Rcomb(2);
gb::Rcomb(3);
}
//-Done
std::cout << "\nAF: " << std::hex << (int)Raf << "\n";
std::cout << "BC: " << std::hex << (int)Rbc << "\n";
std::cout << "DE: " << std::hex << (int)Rde << "\n";
std::cout << "HL: " << std::hex << (int)Rhl << "\n";
}
void RegRecombiner(std::string Dtarget) {
if (Dtarget == "AF") { Ra = Raf >> 8; Rf = (Raf & 0xF); }
else if (Dtarget == "BC") { Rb = Rbc >> 8; Rc = (Rbc & 0xF); }
else if (Dtarget == "DE") { Re = Rde >> 8; Re = (Rde & 0xF); }
else if (Dtarget == "HL") { Rh = Rhl >> 8; Rl = (Rhl & 0xF); }
}
#pragma region InstructionMapping
///////////////////////////////////////////////////////////////////////////////////////
//- Function Backbone - Order of execution is given by the preset
//- This will be a Major milestone in the project (Current TASK)
///////////////////////////////////////////////////////////////////////////////////////
//Instruction Preset
/*
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 2, 2, $any, Ra, 1 );
Ra = ($any + TempRf);
funcText << "ADC " << Dtarget << ", " << Starget << "(" << std::hex << (int)$any << ")";
functType = funcText.str();
opDeb();
pc += opLen;
*/ ////-- Instruction preset
//Hidden Instruction Preset //
//Instruction Preset
void ADC8RhlM(unsigned short &$any, std::string Dtarget, std::string Starget) { // n + carry flag to A
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 2, 2, $any, Ra, 1 );
Ra = ($any + TempRf);
funcText << "ADC " << Dtarget << ", " << Starget << "(" << std::hex << (int)$any << ")";
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void ADCA$xx(unsigned short $any, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 2;
FLGH(2, 0, 2, 2, $any, Ra, 1);
Ra = ($any + TempRf);
funcText << "ADC " << Dtarget << ", " << Starget << "(" << std::hex << (int)$any << ")";
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void ADCR8(unsigned short &$any, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 2, 2, $any, Ra, 1);
Ra = ($any + TempRf);
funcText << "ADC " << Dtarget << ", " << Starget << "(" << std::hex << (int)$any << ")";
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void ADDAhlM(std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
opDeb();
FLGH(2, 0, 2, 2, Ra, memoryA[Rhl], 1);
Ra += memoryA[Rhl];
funcText << "ADD " << Dtarget << ", " << Starget << "(" << std::hex << (int)memoryA[Rhl] << ")";
functType = funcText.str();
pc += opLen;
////-- Instruction preset
}
void ADDA$xx(unsigned char _8int) {
//FLGH(2, 0, 2, 2);
//CALCFLG(Ra, _8int, 1);
Ra += _8int;
opclock = 2;
functType = "ADD $xx ";
}
void ADDA8R(unsigned char &_8r, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 1, 0, Ra, _8r, 1);
Ra += _8r;
funcText << "AND " << Dtarget << ", " << Starget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void ADDHL16R(unsigned short &_R16) {
//FLGH(3, 0, 2, 2);
//CALCFLG(Rhl, _R1, 1);
Rhl += _R16;
opclock = 1;
functType = "ADD HL 16R ";
}
void ADDSP8OFF(unsigned char _8off) {
//FLGH(0, 0, 2, 2);
//CALCFLG(sp, _8off, 1);
sp = +_8off;
opclock = 2;
functType = "ADD SP 8off";
}
void ANDAhlM() {
//FLGH(2, 0, 1, 0);
//CALCFLG(Ra, memoryA[Rhl], 1);
//Ra &= memoryA[Rhl];
opclock = 1;
functType = "AND A (HL) ";
}
void ANDA$xx(unsigned char _8int) {
//FLGH(2, 0, 1, 0);
//CALCFLG(Ra, _8int, 3); //set optype3 as AND
Ra &= _8int;
opclock = 2;
functType = "AND A $xx ";
}
void ANDA8R(unsigned char &_8r, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 1, 0, Ra, _8r, 4);
Ra &= _8r;
funcText << "AND " << Dtarget << ", " << Starget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void BIThlM(unsigned char bitnum) {
switch (memoryA[Rhl] << bitnum) { // refinar 0x1
case 1:
std::cout << "tested positive\n";
case 0:
std::cout << "tested negative\n";
}
//FLGH(2, 0, 1, 3);
opclock = 2;
functType = "BIT (HL) ";
//CALCFLG(Ra, _8int, 3) //set optype4 op de revision valor memoryA[Rhl] o hacer directo aca
//bit test agains
//update timers
}
void BIT8R(unsigned char bitnum, unsigned char &_8r) {
switch (_8r << bitnum) { // refinar 0x1
case 1:
std::cout << "tested positive\n";
case 0:
std::cout << "tested negative\n";
}
//FLGH(2, 0, 1, 3);
opclock = 2;
functType = "BIT 8R ";
//CALCFLG(Ra, _8int, 3) //set optype4 op de revision valor memoryA[Rhl] o hacer directo aca
//bit test agains
//update timers
}
void CALLpcF$aabb(int flagcc, unsigned short _16BA) {
if (flagcc == 1)
{
pc = _16BA;
}
//FLGH(3, 3, 3, 3);
opclock = 3;
functType = "CALL pc IF F $aabb ";
}
void CALLpc16BA(unsigned short _16BA) {
pc = _16BA;
// mal la dif entre jump y call
//skip
opclock = 3;
functType = "CALL pc $aabb ";
//FLGH(3, 3, 3, 3);
}
void CARRYFLAG() {
//FLGH(3, 0, 0, 2);
//CALCFLG(Ra, _8int, 3) //set optype4 op de revision valor memoryA[Rhl] o hacer directo aca
opclock = 1;
functType = "CARRY FLAG ";
//FLGH(3, 3, 3, 1);
}
void COMPhlM() {
//FLGH(2, 1, 2, 2);
opclock = 1;
functType = "COMPLEMENT (HL) ";
}
void COMP8INT(unsigned char _8int) {
//FLGH(2, 1, 2, 2);
opclock = 2;
functType = "COMPLEMENT $xx ";
}
void COMP8R(unsigned char &_8r) {
//FLGH(2, 1, 2, 2);
opclock = 1;
functType = "COMPLEMENT 8R ";
}
void CPL() {
//FLGH(3, 1, 1, 3);
opclock = 1;
functType = "COMPLEMENT L";
}
void DAA() {
//FLGH(2, 3, 0, 2);
opclock = 1;
functType = "DAA ";
}
void DEChlM() {
//FLGH(2, 1, 2, 3);
memoryA[Rhl]--;
opclock = 1;
functType = "DEC (HL) ";
}
void DECR16(unsigned short &_16r) {
_16r--;
opclock = 1;
//update flags
functType = "DEC R16 ";
//FLGH(3, 3, 3, 3);
}
void DEC8R(unsigned char &_r8, std::string Dtarget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 1, 2, 3, _r8, 1, 2);
_r8--;
funcText << "DEC " << Dtarget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void DI() {
opclock = 1;
opLen = 1;
pc += opLen;
functType = "DISABLE INTERRUPTS ";
std::cin.get();
//FLGH(3, 3, 3, 3);
}
void EI() {
opclock = 1;
opLen = 1;
pc += opclock;
//FLGH(3, 3, 3, 3);
}
void HALT() {
opclock = 1;
//FLGH(3, 3, 3, 3);
}
void INChlM() {
//FLGH(2, 0, 2, 3);
memoryA[Rhl]++;
opclock = 1;
//update timers
//update flags
functType = "INC (HL) ";
}
void INC16R(unsigned short &_16r, std::string Dtarget) {
////-- Instruction preset
opLen = 1;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
_16r++;
RegRecombiner(Dtarget);
funcText << "INC " << Dtarget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void INC8R(unsigned char &_r8, std::string Dtarget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 2, 3, _r8, 1, 2);
_r8++;
funcText << "INC " << Dtarget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void JUMPhlM() {
pc = memoryA[Rhl];
//update timers
opclock = 1;
//update flags
functType = "JP (HL) ";
//FLGH(3, 3, 3, 3);
}
void JUMpcC$aabb(int flagcc, unsigned short _16BA) {
if (flagcc == 1)
pc = _16BA;
else
pc++;
opclock = 3;
functType = "JUMP CC $aabb ";
//FLGH(3, 3, 3, 3);
}
void JP$aabb(unsigned short _$aabb) {
////-- Instruction preset
opLen = 3;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
funcText << "JUMP pc -> " << std::hex << std::setw(4) << std::setfill('0') << (int)_$aabb;
functType = funcText.str();
opDeb();
pc = _$aabb;
////-- Instruction preset
}
void JRCC$xx(std::string flagOp, signed char _$xx) {
////-- Instruction preset
opLen = 2;
//std::cout << "8bit int inm " << (int)_$xx << "\n";
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
funcText << "JR + $xx IF FlagBit = TRUE ->";
functType = funcText.str();
opDeb();
std::bitset<8> flagChecker(Rf);
unsigned char byteNum;
unsigned char testCase;
if (flagOp == "Z"){ byteNum = 7; testCase = 1; }
else if (flagOp == "NZ"){ byteNum = 7; testCase = 0; }
else if (flagOp == "C"){ byteNum = 4; testCase = 1; }
else if (flagOp == "NC"){ byteNum = 4; testCase = 0; }
if (flagChecker.test(byteNum) == testCase)
{
std::string result = "TRUE\n";
std::cout << "Case " << flagOp << "tested " << result;
pc = pc + _$xx + opLen;
}
else if (flagChecker.test(byteNum) != testCase)
{
std::string result = "FALSE\n";
std::cout << "Case " << flagOp << "tested " << result;
pc += opLen;
}
////-- Instruction preset
}
void JR$xx(unsigned char _8int) {
pc = memoryA[_8int];
//skip
opclock = 2;
functType = "JR $xx ";
//FLGH(3, 3, 3, 3);
}
void LOADCMRA() {
Ra = memoryA[Rc];
//update timers
opclock = 1;
//update flags
functType = "LOAD C (A) ";
//FLGH(3, 3, 3, 3);
}
void LOADhlM$xx(unsigned char _8int) {
memoryA[Rhl] = _8int;
//update timers
opclock = 2;
//update flags
functType = "LOAD (HL) $xx ";
//FLGH(3, 3, 3, 3);
}
void LOADhlM8R(unsigned char &_8r) {
memoryA[Rhl] = _8r;
//update timers
opclock = 1;
//update flags
functType = "LOAD (HL) R8 ";
///FLGH(3, 3, 3, 3);
}
void LOAD$aabbMRA(unsigned short _16ba) {
memoryA[_16ba] = Ra;
//update timers
opclock = 3;
//update flags
functType = "LOAD $aabb (A) ";
//FLGH(3, 3, 3, 3);
}
void LOAD$aabbSP(unsigned short _16ba) {
memoryA[_16ba] = sp;
//update timers
opclock = 3;
//update flags
functType = "LOAD $aabb SP ";
//FLGH(3, 3, 3, 3);
}
void LOAD16RMRA(unsigned short &_16r) {
memoryA[_16r] = Ra;
//update timers
opclock = 1;
//update flags
functType = "LOAD (16R) A ";
//FLGH(3, 3, 3, 3);
}
void LOADRACM(unsigned char &_8r) {
Ra = memoryA[_8r];
//update timers
opclock = 1;
//update flags
functType = "LOAD A (C) - REVISAR ";
//FLGH(3, 3, 3, 3);
///////////////////REVISAR PUEDE ESTAR MALO
}
void LOADRA$aabb(unsigned short _16ba) {
Ra = memoryA[_16ba];
//update timers
opclock = 3;
//update flags
functType = "LOAD A $aabb ";
//FLGH(3, 3, 3, 3);
}
void LOADRA16RM(unsigned short &_16r) {
Ra = memoryA[_16r];
//update timers
opclock = 1;
//update flags
functType = "LOAD A (16R) ";
//FLGH(3, 3, 3, 3);
}
void LOADRHLSPOFF(unsigned char _8boff) {
//FLGH(0, 0, 2, 2);
Rhl = (sp + _8boff);
//update timers
opclock = 2;
//update flags
functType = "LOAD HL SPOFF";
}
void LOADR16$aabb(unsigned short &_16r, unsigned short $_aabb, std::string Dtarget) {
////-- Instruction preset
opLen = 3;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
_16r = $_aabb;
funcText << "LOAD " << Dtarget << ", " << std::hex << std::setw(4) << std::setfill('0') << (int)$_aabb;
functType = funcText.str();
opDeb();
if (Dtarget == "HL") { Rh = $aa; Rl = $bb; }
pc += opLen;
////-- Instruction preset
}
void LOADR8hlM(unsigned char &_8r) {
_8r = memoryA[Rhl];
functType = "LD (HL)";
//gb::DebugReg(functType);
//update timers
opclock = 1;
opLen = 1;
pc += opLen;
// FLGH(3, 3, 3, 3);
//update flags
}
void LDR8$xx(unsigned char &_8r, unsigned char _$xx, std::string Dtarget) {
////-- Instruction preset
opLen = 2;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
_8r = _$xx;
funcText << "LOAD " << Dtarget << ", " << std::hex << std::setw(4) << std::setfill('0') << (int)_$xx;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void LOADR8R8(unsigned char &_8r, unsigned char &_8r2, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
_8r = _8r2;
funcText << "LOAD " << Dtarget << ", " << Starget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}
void LOADSPHL(unsigned short &SP, unsigned short &HL) {
SP = HL;
//update timers
opclock = 1;
//update flags
functType = "SP HL ";
//FLGH(3, 3, 3, 3);
}
void LOADDR8hlM() {
Ra = memoryA[Rhl];
Rhl--;
//update timers
opclock = 1;
//update flags
functType = "R8 (HL) ";
//FLGH(3, 3, 3, 3);
opLen = 1;
}
void LOADDhlMR8(unsigned char &_R8, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(3, 3, 3, 3, NULL, NULL, NULL);
memoryA[Rhl] = _R8;
funcText << "LOADD " << "(HL) ," << Starget;
functType = funcText.str();
opDeb();
if (Dtarget == "(HL)") { Rl--; }
pc += opLen;
////-- Instruction preset
}
void LOADHAOFF(unsigned char off) { //LDH
memoryA[0xff00 + off] = Ra;
//update timers
opclock = 2;
opLen = 2;
pc += opLen;
//update flags
functType = "LD";
//DebugReg(functType);
//FLGH(3, 3, 3, 3);
}
void LOADAHOFF(unsigned char off) { //LDH
Ra = memoryA[0xff00 + off];
//update timers
opclock = 2;
//update flags
functType = "LOAD A OFF ";
//FLGH(3, 3, 3, 3);
}
void LDIhlMRA() {
memoryA[Rhl] = Ra;
Ra++;
//update timers
opclock = 1;
//update flags
functType = " LDI (HL) A ";
//FLGH(3, 3, 3, 3);
}
void LDIRAhlM() {
Ra = memoryA[Rhl];
memoryA[Rhl]++;
//update timers
opclock = 1;
//update flags
functType = "LDI A (HL) ";
// FLGH(3, 3, 3, 3);
}
void NOP() {
opDeb();
//FLGH(3, 3, 3, 3);
//pc += opLen;
functType = "NOP ";
}
void ORhlMRA() {
// FLGH(2, 0, 0, 0);
memoryA[Rhl] |= Ra;
opclock = 1;
functType = "OR (HL) A ";
}
void ORRA$xx(unsigned char _8int) {
// FLGH(2, 0, 0, 0);
Ra |= _8int;
opclock = 2;
functType = "OR ADC R8 ";
}
void ORRA8R(unsigned char &_r8, std::string Dtarget, std::string Starget) {
////-- Instruction preset
opLen = 1;
FLGH(2, 0, 0, 0, Ra, _r8, 5);
Ra |= _r8;
funcText << "OR " << Dtarget << ", " << Starget;
functType = funcText.str();
opDeb();
pc += opLen;
////-- Instruction preset
}