-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSim8080.cpp
More file actions
2020 lines (1678 loc) · 41.3 KB
/
Sim8080.cpp
File metadata and controls
2020 lines (1678 loc) · 41.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Sim8080.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#pragma comment(lib, "wsock32.lib")
#define STUDENT_NUMBER "13021034"
#define IP_ADDRESS_SERVER "127.0.0.1"
#define PORT_SERVER 0x1984 // We define a port that we are going to use.
#define PORT_CLIENT 0x1985 // We define a port that we are going to use.
#define WORD unsigned short
#define DWORD unsigned long
#define BYTE unsigned char
#define MAX_FILENAME_SIZE 500
#define MAX_BUFFER_SIZE 500
SOCKADDR_IN server_addr;
SOCKADDR_IN client_addr;
SOCKET sock; // This is our socket, it is the handle to the IO address to read/write packets
WSADATA data;
char InputBuffer [MAX_BUFFER_SIZE];
char hex_file [MAX_BUFFER_SIZE];
char trc_file [MAX_BUFFER_SIZE];
//////////////////////////
// Intel 8080 Registers //
//////////////////////////
#define REGISTER_B 0
#define REGISTER_C 1
#define REGISTER_D 2
#define REGISTER_E 3
#define REGISTER_H 4
#define REGISTER_L 5
#define REGISTER_M 6
#define REGISTER_A 7
#define FLAG_S 0x80
#define FLAG_Z 0x40
#define FLAG_A 0x10
#define FLAG_P 0x04
#define FLAG_C 0x01
BYTE Registers[8];
BYTE Flags;
WORD ProgramCounter;
WORD StackPointer;
////////////
// Memory //
////////////
#define K_1 1024
#define MEMORY_SIZE K_1
BYTE Memory[MEMORY_SIZE];
///////////////////////
// Control variables //
///////////////////////
bool memory_in_range = true;
bool halt = false;
///////////////////////
// Disassembly table //
///////////////////////
char opcode_mneumonics[][12] =
{
"NOP ",
"LXI B,data ",
"STAX B ",
"INX B ",
"INR B ",
"DCR B ",
"MVI B,data ",
"RLC ",
".BYTE 0x08 ",
"DAD B ",
"LDAX B ",
"DCX B ",
"INR C ",
"DCR C ",
"MVI C,data ",
"RRC ",
".BYTE 0x10 ",
"LXI D,data ",
"STAX D ",
"INX D ",
"INR D ",
"DCR D ",
"MVI D,data ",
"RAL ",
".BYTE 0x18 ",
"DAD D ",
"LDAX D ",
"DCX D ",
"INR E ",
"DCR E ",
"MVI E,data ",
"RAR ",
"RIM ",
"LXI H,data ",
"SHLD ",
"INX H ",
"INR H ",
"DCR H ",
"MVI H,data ",
"DAA ",
".BYTE 0x28 ",
"DAD H ",
"LHLD ",
"DCX H ",
"INR L ",
"DCR L ",
"MVI L,data ",
"CMA ",
"SIM ",
"LXI SP,data",
"STA ",
"INX SP ",
"INR M ",
"DCR M ",
"MVI M,data ",
"STC ",
".BYTE 0x38 ",
"DAD SP ",
"LDA ",
"DCX SP ",
"INR A ",
"DCR A ",
"MVI A,data ",
"CMC ",
"MOV B,B ",
"MOV B,C ",
"MOV B,D ",
"MOV B,E ",
"MOV B,H ",
"MOV B,L ",
"MOV B,M ",
"MOV B,A ",
"MOV C,B ",
"MOV C,C ",
"MOV C,D ",
"MOV C,E ",
"MOV C,H ",
"MOV C,L ",
"MOV C,M ",
"MOV C,A ",
"MOV D,B ",
"MOV D,C ",
"MOV D,D ",
"MOV D,E ",
"MOV D,H ",
"MOV D,L ",
"MOV D,M ",
"MOV D,A ",
"MOV E,B ",
"MOV E,C ",
"MOV E,D ",
"MOV E,E ",
"MOV E,H ",
"MOV E,L ",
"MOV E,M ",
"MOV E,A ",
"MOV H,B ",
"MOV H,C ",
"MOV H,D ",
"MOV H,E ",
"MOV H,H ",
"MOV H,L ",
"MOV H,M ",
"MOV H,A ",
"MOV L,B ",
"MOV L,C ",
"MOV L,D ",
"MOV L,E ",
"MOV L,H ",
"MOV L,L ",
"MOV L,M ",
"MOV L,A ",
"MOV M,B ",
"MOV M,C ",
"MOV M,D ",
"MOV M,E ",
"MOV M,H ",
"MOV M,L ",
"HLT ",
"MOV M,A ",
"MOV A,B ",
"MOV A,C ",
"MOV A,D ",
"MOV A,E ",
"MOV A,H ",
"MOV A,L ",
"MOV A,M ",
"MOV A,A ",
"ADD B ",
"ADD C ",
"ADD D ",
"ADD E ",
"ADD H ",
"ADD L ",
"ADD M ",
"ADD A ",
"ADC B ",
"ADC C ",
"ADC D ",
"ADC E ",
"ADC H ",
"ADC L ",
"ADC M ",
"ADC A ",
"SUB B ",
"SUB C ",
"SUB D ",
"SUB E ",
"SUB H ",
"SUB L ",
"SUB M ",
"SUB A ",
"SBB B ",
"SBB C ",
"SBB D ",
"SBB E ",
"SBB H ",
"SBB L ",
"SBB M ",
"SBB A ",
"ANA B ",
"ANA C ",
"ANA D ",
"ANA E ",
"ANA H ",
"ANA L ",
"ANA M ",
"ANA A ",
"XRA B ",
"XRA C ",
"XRA D ",
"XRA E ",
"XRA H ",
"XRA L ",
"XRA M ",
"XRA A ",
"ORA B ",
"ORA C ",
"ORA D ",
"ORA E ",
"ORA H ",
"ORA L ",
"ORA M ",
"ORA A ",
"CMP B ",
"CMP C ",
"CMP D ",
"CMP E ",
"CMP H ",
"CMP L ",
"CMP M ",
"CMP A ",
"RNZ ",
"POP B ",
"JNZ ",
"JMP ",
"CNZ ",
"PUSH B ",
"ADI ",
"RST 0 ",
"RZ ",
"RET ",
"JZ ",
".BYTE 0xCB ",
"CZ ",
"CALL ",
"ACI ",
"RST 1 ",
"RNC ",
"POP D ",
"JNC ",
"OUT ",
"CNC ",
"PUSH D ",
"SUI ",
"RST 2 ",
"RC ",
".BYTE 0xD9 ",
"JC ",
"IN ",
"CC ",
".BYTE 0xDD ",
"SBI ",
"RST 3 ",
"RPO ",
"POP H ",
"JPO ",
"XTHL ",
"CPO ",
"PUSH H ",
"ANI ",
"RST 4 ",
"RPE ",
"PCHL ",
"JPE ",
"XCHG ",
"CPE ",
".BYTE 0xED ",
"XRI ",
"RST 5 ",
"RP ",
"POP PSW ",
"JP ",
"DI ",
"CP ",
"PUSH PSW ",
"ORI ",
"RST 6 ",
"RM ",
"SPHL ",
"JM ",
"EI ",
"CM ",
".BYTE 0xFD ",
"CPI ",
"RST 7 "
};
////////////////////////////////////////////////////////////////////////////////
// Intel 8080 Simulator/Emulator (Start) //
////////////////////////////////////////////////////////////////////////////////
BYTE fetch()
{
BYTE byte = 0;
if ((ProgramCounter >= 0) && (ProgramCounter <= MEMORY_SIZE))
{
memory_in_range = true;
byte = Memory[ProgramCounter];
ProgramCounter++;
}
else
{
memory_in_range = false;
}
return byte;
}
// Add any instruction implementing routines here...
/*
* Author: Anthony Scully (13021034)
* Created: 17/10/2013
* Revised: 26/11/2013
* Revisions:
* Added comments
* Implemented instruction sets from week 13
* Achieved maximum score of 60 .
* User advice:
* Read over the custom method listed below.
* Descriptions of the methods are available when hovering over them throughout the method name.
*/
/*
* NOTE:
* The methods below are pieces of code that were re-used regularly.
* Pointers are primarily used for consistency. (Not all functions require the variable to be pointed to.)
*/
// | (or) activates the flag
// & (and) (0xFF - FLAG_MARKER) disables the flag
// Sets flags Z, S and P based on the value set by regA
void set_flags(BYTE regA)
{
BYTE regACheck = 0x01;
int bit_set_count;
if (regA == 0) Flags = Flags | FLAG_Z;
else Flags = Flags & (0xFF - FLAG_Z);
if ((regA & 0x80) != 0) Flags = Flags | FLAG_S;
else Flags = Flags & (0xFF - FLAG_S);
bit_set_count = 0;
for (int i = 0; i < 8; i++)
{
if ((regA & regACheck) != 0) bit_set_count++;
regACheck = regACheck << 1;
}
if ((bit_set_count & 0x01) != 1) Flags = Flags | FLAG_P;
else Flags = Flags & (0xFF - FLAG_P);
}
/*
Uses a value (usually temp_word) to either clear or set FLAG_C in variable Flags.
To set Flag C to 0, input a value less than 0x100. Else any other value will set Flag C to 1.
*/
void flag_c_handler(WORD temp_word)
{
if (temp_word >= 0x100) Flags = Flags | FLAG_C;
else Flags = Flags & (0xFF - FLAG_C);
}
/*
Handles storing and loading of instructions from a single registry.
If the bool isLoadingToRegister parameter is true, the value in the memory address will be stored to the register.
If false, the value in the register will be stored to the memory address.
*/
void single_registry_store_and_load(BYTE * Reg1, WORD address, bool isLoadingToRegister)
{
if (isLoadingToRegister)
{
if ((address >= 0) && (address < MEMORY_SIZE))
*Reg1 = Memory[address];
else *Reg1 = 0;
}
else
{
if ((address >= 0) && (address < MEMORY_SIZE))
Memory[address] = *Reg1;
}
}
/*
Handles storing and loading of instructions from registry pairs.
If the bool isLoadingToRegister parameter is true, the value in the memory address will be stored to the the two registers.
If false, the value in the two registers will be stored to the memory address.
*/
void pair_registry_store_and_load(BYTE * Reg1,BYTE * Reg2, WORD address, bool isLoadingToRegister)
{
if (isLoadingToRegister)
{
if ((address >= 0) && (address < MEMORY_SIZE-1))
{
*Reg1 = Memory[address];
*Reg2 = Memory[address+1];
}
else
{
*Reg1 = 0;
*Reg2 = 0;
}
}
else
{
if ((address >= 0) && (address < MEMORY_SIZE-1))
{
Memory[address] = *Reg1;
Memory[address+1] = *Reg2;
}
}
}
// Pushes the contents of the registries into the memory pointed to by the StackPointer (PUSH)
void push_stack_pointer(BYTE * Reg1, BYTE * Reg2)
{
//Pointers are used as the Flags variable would be difficult to check for using variables.
if ((StackPointer >=2) && (StackPointer < (MEMORY_SIZE+1))) //Uses >=2 and +1 because we will be decrementing
{
StackPointer--;
Memory[StackPointer] = *Reg1;
StackPointer--;
Memory[StackPointer] = *Reg2;
}
}
// Pops the contents from memory pointed to by the StackPointer into the registries. (POP)
void pop_stack_pointer(BYTE * Reg1, BYTE * Reg2)
{
if ((StackPointer >=0) && (StackPointer < (MEMORY_SIZE - 2 ))) //Uses -2 as we will be incrementing twice.
{
*Reg1 = Memory[StackPointer];
StackPointer++;
*Reg2 = Memory[StackPointer];
StackPointer++;
}
}
// Pushes the program counter on to the stack and puts the address into the program counter. (CALL)
void call_handler(WORD address)
{
//Checks if the memory address is valid
if ((address >=0) && (address < MEMORY_SIZE))
{
//Checks if the address held in the stack pointer is valid
if ((StackPointer >=2) && (StackPointer <(MEMORY_SIZE+1)))
{
StackPointer--;
Memory[StackPointer] = (BYTE)((ProgramCounter >> 8) & 0xFF);
StackPointer--;
Memory[StackPointer] = (BYTE)(ProgramCounter & 0xFF);
ProgramCounter = address;
}
}
}
/*
Takes the data in memory pointed to by the stack pointer
and sets it to the program counter using lb and hb. (RET)
*/
void ret_handler(BYTE * lb, BYTE * hb)
{
WORD address;
if ((StackPointer >=0) && (StackPointer < (MEMORY_SIZE-2)))
{
*lb = Memory[StackPointer];
StackPointer++;
*hb = Memory[StackPointer];
StackPointer++;
address = ((WORD)*hb << 8) + (WORD)*lb;
//Checks if the memory address is valid
if ((address >=0) && (address < MEMORY_SIZE))
ProgramCounter = ((WORD)*hb << 8) + (WORD)*lb;
}
}
// Jumps to the address specified by lb and hb. (JMP)
void jmp_handler(BYTE * lb, BYTE * hb)
{
WORD address = ((WORD)*hb << 8) + (WORD)*lb;
if ((address >=0) && (address < MEMORY_SIZE))
ProgramCounter = address;
}
/*
Increments or decrements registry pair based on the isIncrement bool.
If isIncrement is true, the registry pair is increased by 1 (INX)
Else is false, the registry pair is decreased by 1 (DCX)
*/
void inx_dcx_handler(BYTE * Reg1, BYTE * Reg2, bool isIncrement)
{
WORD temp_word;
temp_word = ((WORD)*Reg1 << 8) + (WORD)*Reg2;
if (isIncrement) temp_word++;
else temp_word--;
*Reg1 = (BYTE)((temp_word >> 8) & 0xFF);
*Reg2 = (BYTE)(temp_word & 0xFF);
}
/*
Stores the program counter into the memory pointed to by the stack pointer
The program counter value is then set to destination multiplied by 8 found in the opcode
ProgramCounter = destination * 8
*/
void rst_handler(BYTE opcode)
{
BYTE destination = (opcode >> 3) & 0x07;
if ((StackPointer >= 2) && (StackPointer < MEMORY_SIZE + 1))
{
StackPointer--;
Memory[StackPointer] = ((BYTE)(ProgramCounter >> 8) & 0xFF);
StackPointer--;
Memory[StackPointer] = (BYTE)(ProgramCounter & 0xFF);
}
ProgramCounter = destination * 8;
}
// Swaps the values of the two registries
void register_swap(BYTE * Reg1, BYTE * Reg2)
{
*Reg1 = *Reg1 ^ *Reg2;
*Reg2 = *Reg1 ^ *Reg2;
*Reg1 = *Reg1 ^ *Reg2;
}
void block_00_instructions(BYTE opcode)
{
BYTE ls3bits;
BYTE destination;
WORD address;
BYTE lb;
BYTE hb;
long temp;
long hl;
WORD temp_word;
BYTE temp_byte;
ls3bits = opcode & 0x07;
switch (ls3bits)
{
case 0x00: // NOP - No instruction is called.
break;
case 0x01: // LXI and DAD
if ((opcode & 0x08) == 0) //LXI - Load register pair immediate
{
lb = fetch();
hb = fetch();
switch (opcode)
{
case 0x01: //LXI BC
Registers[REGISTER_B] = hb;
Registers[REGISTER_C] = lb;
break;
case 0x11: //LXI DE
Registers[REGISTER_D] = hb;
Registers[REGISTER_E] = lb;
break;
case 0x21: //LXI HL
Registers[REGISTER_H] = hb;
Registers[REGISTER_L] = lb;
break;
default:
StackPointer = ((WORD)hb << 8) + (WORD)lb;
break;
}
}
else //DAD - Add register pair to HL (16 bit add)
{
temp_word = (((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L]);
switch (opcode)
{
case 0x09: //BC
temp_word += (((WORD)Registers[REGISTER_B] << 8) + (WORD)Registers[REGISTER_C]);
break;
case 0x19: //DE
temp_word += (((WORD)Registers[REGISTER_D] << 8) + (WORD)Registers[REGISTER_E]);
break;
case 0x29: //HL
temp_word += temp_word;
break;
case 0x39: //StackPointer
temp_word += StackPointer;
break;
}
flag_c_handler(temp_word & 0x10000); // Activates flag C if temp_word is larger than 16 bits
Registers[REGISTER_H] = (BYTE)((temp_word >> 8) & 0xFF);
Registers[REGISTER_L] = (BYTE)(temp_word & 0xFF);
}
break;
case 0x02: // Load and Store instructions
switch (opcode)
{
case 0x3A: // LDA - Load A from memory
lb = fetch();
hb = fetch();
address = ((WORD)hb << 8) + (WORD)lb;
single_registry_store_and_load(&Registers[REGISTER_A],address,true);
break;
case 0x2A: // LHLD - Load H:L from memory
lb = fetch();
hb = fetch();
address = ((WORD)hb) + (WORD)lb;
pair_registry_store_and_load(&Registers[REGISTER_L],&Registers[REGISTER_H],address,true);
break;
case 0x32: //STA - Store A to memory
lb = fetch();
hb = fetch();
address = ((WORD)hb << 8) + (WORD)lb;
single_registry_store_and_load(&Registers[REGISTER_A],address,false);
break;
case 0x22: // SHLD - Store H:L to memory
lb = fetch();
hb = fetch();
address = ((WORD)hb << 8) + (WORD)lb;
pair_registry_store_and_load(&Registers[REGISTER_L],&Registers[REGISTER_H],address,false);
break;
case 0x02: //STAX B - Store indirect through BC
address = ((WORD)Registers[REGISTER_B] << 8) + (WORD)Registers[REGISTER_C];
single_registry_store_and_load(&Registers[REGISTER_A],address,false);
break;
case 0x12: //STAX D - Store indirect through DE
address = ((WORD)Registers[REGISTER_D] << 8) + (WORD)Registers[REGISTER_E];
single_registry_store_and_load(&Registers[REGISTER_A],address,false);
break;
case 0x0A: //LDAX B - Load indirect through BC
address = ((WORD)Registers[REGISTER_B] << 8) + (WORD)Registers[REGISTER_C];
single_registry_store_and_load(&Registers[REGISTER_A],address,true);
break;
case 0x1A: //LDAX D - Load indirect through DE
address = ((WORD)Registers[REGISTER_D] << 8) + (WORD)Registers[REGISTER_E];
single_registry_store_and_load(&Registers[REGISTER_A],address,true);
break;
default:
break;
}
break;
case 0x03: // INX and DCX
if ((opcode & 0x08) == 0) // INX - Increment register pair
{
switch (opcode)
{
case 0x03: // BC
inx_dcx_handler(&Registers[REGISTER_B],&Registers[REGISTER_C],true);
break;
case 0x13: // DE
inx_dcx_handler(&Registers[REGISTER_D],&Registers[REGISTER_E],true);
break;
case 0x23: // HL
inx_dcx_handler(&Registers[REGISTER_H],&Registers[REGISTER_L],true);
break;
case 0x33: // Stack Pointer
StackPointer++;
break;
}
}
else // DCX - Decrement register pair
{
switch (opcode)
{
case 0x0B: // BC
inx_dcx_handler(&Registers[REGISTER_B],&Registers[REGISTER_C],false);
break;
case 0x1B: // DE
inx_dcx_handler(&Registers[REGISTER_D],&Registers[REGISTER_E],false);
break;
case 0x2B: // HL
inx_dcx_handler(&Registers[REGISTER_H],&Registers[REGISTER_L],false);
break;
case 0x3B: // Stack Pointer
StackPointer--;
break;
}
}
break;
case 0x04: // INR D - Increment register
destination = (opcode >> 3) & 0x07;
if (destination == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
if ((address >=0) && (address < MEMORY_SIZE))
{
Memory[address]++;
set_flags(Memory[address]);
}
}
else
{
Registers[destination]++;
set_flags(Registers[destination]);
}
break;
case 0x05: // DCR D - Decrement register
destination = (opcode >> 3) & 0x07;
if (destination == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
if ((address >=0) && (address < MEMORY_SIZE))
{
Memory[address]--;
set_flags(Memory[address]);
}
}
else
{
Registers[destination]--;
set_flags(Registers[destination]);
}
break;
case 0x06: // MVI - Move immediate to register
destination = (opcode >> 3) & 0x07;
Registers[destination] = fetch();
if (destination == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
single_registry_store_and_load(&Registers[REGISTER_M],address,false);
}
break;
default: // Rotates,DAA and carry instructions
switch (opcode)
{
case 0x07: // RLC - Rotate A left
temp_word = (WORD)Registers[REGISTER_A];
temp_word = temp_word << 1;
flag_c_handler(temp_word);
if ((Flags & FLAG_C) != 0)
{
temp_word = temp_word | 0x01;
}
Registers[REGISTER_A] = (BYTE)(temp_word & 0xFF);
break;
case 0x17: // RAL - Rotate A left through carry
temp_word = (WORD)Registers[REGISTER_A];
temp_word = temp_word << 1;
if ((Flags & FLAG_C) == FLAG_C)
{
temp_word = temp_word | 0x01;
}
flag_c_handler(temp_word);
Registers[REGISTER_A] = (BYTE)(temp_word & 0xFF);
break;
case 0x0F: // RRC - Rotate A right
temp_word = (WORD)Registers[REGISTER_A];
//Checks if the the lowest bit is 1 or 0
if ((temp_word & 0x01) != 0)
{
temp_word = temp_word >> 1;
temp_word = temp_word | 0x80;
flag_c_handler(0x100);
}
else
{
temp_word = temp_word >> 1;
flag_c_handler(0);
}
Registers[REGISTER_A] = (BYTE)(temp_word & 0xFF);
break;
case 0x1F: // RAR - Rotate A right through carry
temp_word = (WORD)Registers[REGISTER_A];
if ((Flags & FLAG_C) != 0)
{
temp_word = temp_word | 0x100;
}
//Checks if the lowest bit is 1 or 0. Sets FLAG_C equal to the result
if ((temp_word & 0x01) != 0)
flag_c_handler(0x100);
else
flag_c_handler(0);
temp_word = temp_word >> 1;
Registers[REGISTER_A] = (BYTE)(temp_word & 0xFF);
break;
case 0x2F: // CMA -Compliment A
Registers[REGISTER_A] = (BYTE)((WORD)Registers[REGISTER_A] ^ 0xFF);
break;
case 0x37: // STC - Set Carry flag
Flags = Flags | FLAG_C;
break;
case 0x3F: // CMC - Compliment Carry flag
Flags = Flags ^ FLAG_C;
break;
default:
break;
}
break;
}
}
void MOV_and_HLT_instructions(BYTE opcode)
{
BYTE source;
BYTE destination;
BYTE temp;
WORD address;
if (opcode == 0x76)
halt = true; // HLT - halt microprocessor
else // MOV - Move register to register
{
source = opcode & 0x07;
destination = (opcode >> 3) & 0x07;
if (source == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
single_registry_store_and_load(&Registers[REGISTER_M],address,true);
}
Registers[destination] = Registers[source];
if (destination == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
single_registry_store_and_load(&Registers[REGISTER_M],address,false);
}
}
}
void block_10_instructions(BYTE opcode)
{
BYTE source;
BYTE instruction_type;
WORD temp_word = 0;
WORD address;
instruction_type = (opcode >> 3) & 0x07;
source = opcode & 0x07;
if (source == REGISTER_M)
{
address = ((WORD)Registers[REGISTER_H] << 8) + (WORD)Registers[REGISTER_L];
single_registry_store_and_load(&Registers[REGISTER_M],address,true);
}
temp_word = (WORD)Registers[source];
switch (instruction_type)
{
case 0x00: // ADD S - Add register to A
temp_word = (WORD)Registers[REGISTER_A] + temp_word;
break;
case 0x01: // ADC - Add register to A with carry
temp_word = (WORD)Registers[REGISTER_A] + temp_word;
if ((Flags & FLAG_C) !=0)
temp_word++;
break;
case 0x03: // SBB - Subtract register from A with borrow
temp_word = (WORD)Registers[REGISTER_A] - temp_word;
if ((Flags & FLAG_C) !=0)
temp_word--;
break;
case 0x02: // SUB S - Subtract register from A
temp_word = (WORD)Registers[REGISTER_A] - temp_word;
break;
case 0x04: // ANA - AND register with A
temp_word = (WORD)Registers[REGISTER_A] & temp_word;
break;
case 0x05: // XRA
temp_word = (WORD)Registers[REGISTER_A] ^ temp_word;
break;
case 0x06: // ORA - OR register with A
temp_word = (WORD)Registers[REGISTER_A] | temp_word;
break;
case 0x07: // CMP - Compare register with A (Does not set Register A, only the flags)
temp_word = (WORD)Registers[REGISTER_A] - temp_word;
break;
}
// Does not assign temp_word to Register A when the instruction code is CMP
if (instruction_type != 0x07)
Registers[REGISTER_A] = (BYTE)(temp_word & 0xFF);
flag_c_handler(temp_word);
set_flags((BYTE)(temp_word & 0xFF));
}
void block_11_instructions(BYTE opcode)
{
WORD address;
BYTE lb;
BYTE hb;
BYTE temp;