-
Notifications
You must be signed in to change notification settings - Fork 1
/
ksim.c
1572 lines (1388 loc) · 44.4 KB
/
ksim.c
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
// ksim: an 8080 simulator
// Copyright 2011, 2014 Eric Smith <spacewar@gmail.com>
// SPDX-License-Identifier: GPL-3.0
// ksim was written with the intent of having an accurate 8080 simulator
// for reference. Maximum simulation performance is not a goal of ksim,
// and there is little performance optimization. There are many available
// Z80 simulators, but few for the 8080, and even fewer that simulate the
// processor flags correctly. While it is certainly possible that there
// may be some remaining bugs in ksim, it does pass several published
// 8080 exerciser programs, including one which tests the processor flags
// fairly extensively:
// http://www.idb.me.uk/sunhillow/8080.html
// This version of ksim does not include any interrupts, and has only
// rudimentary I/O support, which is only intended as an example. Console
// I/O is supported via stdin and stdout, using input port 0 for input
// status, input port 1 for character input, and output port 0 for character
// output. It is assumed that stdin and stdout are a tty device, which is
// put into raw mode. Because of this use of raw mode, the interrupt
// character (usually Control-C) will not stop the program, so use of
// another shell to send a TERM signal (typically by use of the "kill"
// command) is required if the simulated program does not halt.
// LICENSE:
// This program is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU General Public License as
// published by the Free Software Foundation.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <termios.h>
#include <unistd.h>
#include "ksim.h"
#include "bdos.h"
// The following definition is used to suppress warnings about unused
// function parameters, but might not work with compilers other than
// GCC. It can either be defined differently for those compilers, or
// declared with an empty defnition.
#define UNUSED __attribute__((unused))
char *progname;
void usage (FILE *f)
{
fprintf (f, "usage: %s [options...] <bootstrap>\n", progname);
fprintf (f, "options:\n");
fprintf (f, " -b bootstrap is a binary file\n");
fprintf (f, " -h bootstrap is an Intel hex file\n");
fprintf (f, " -t <tracefile> trace instruction execution\n");
fprintf (f, " --bdos simulate a tiny subset of CP/M BDOS\n");
}
// generate fatal error message to stderr, doesn't return
void fatal (int ret, char *format, ...) __attribute__ ((noreturn));
void fatal (int ret, char *format, ...)
{
va_list ap;
if (format)
{
fprintf (stderr, "fatal error: ");
va_start (ap, format);
vfprintf (stderr, format, ap);
va_end (ap);
}
if (ret == EX_USAGE)
usage (stderr);
exit (ret);
}
FILE *trace_f = NULL;
struct termios tty_cooked;
struct termios tty_raw;
void console_init (void)
{
tcgetattr (STDIN_FILENO, & tty_cooked);
tcgetattr (STDIN_FILENO, & tty_raw);
tty_raw.c_iflag = 0;
tty_raw.c_oflag = 0;
tty_raw.c_lflag = 0;
tty_raw.c_cc [VTIME] = 0;
tty_raw.c_cc [VMIN] = 1;
tcsetattr (STDIN_FILENO, TCSAFLUSH, & tty_raw);
}
void console_cleanup (void)
{
tcsetattr (STDIN_FILENO, TCSAFLUSH, & tty_cooked);
}
bool bdos_sim = false;
static bool even_parity [0x100];
uint64_t cycle_count;
uint16_t pc;
uint16_t reg_pair [4];
uint8_t reg_a;
bool halted;
bool interrupt_enabled;
uint8_t flag_sp_data;
bool flag_z;
bool flag_ac;
bool flag_cy;
#define SET_FLAG_SZP(data) do { flag_sp_data = data; flag_z = ((data) == 0); } while (0)
#define FLAG_Z (flag_z)
#define FLAG_S (flag_sp_data >> 7)
#define FLAG_P (even_parity [flag_sp_data])
#define FLAG_AC (flag_ac)
#define FLAG_CY (flag_cy)
// On 8085, flag bit 1 is two's complement overflow for 8-bit and 16-bit
// arithmetic operations
// On 8085, flag bit 5 is UI, which is underflow indication for DCX,
// and overflow indication for INX
// UI = O1 x O2 + O1 x R + O2 x R
// where O1 = sign of operand 1
// O2 = sign of operand 2
// R = sign of result
// For subtraction and comparisons, replace O2 with not O2.
void set_f (uint8_t f_value)
{
flag_sp_data = (f_value & 0x80) | (((f_value >> 5) ^ ~f_value) & 0x04);
// S P flag_sp_data
// - - ------------
// 0 0 00000100
// 0 1 00000000
// 1 0 10000000
// 1 1 10000100
flag_z = (f_value >> 6) & 1;
flag_ac = (f_value >> 4) & 1;
flag_cy = f_value & 1;
}
uint8_t get_f (void)
{
return ((FLAG_S << 7) | (FLAG_Z << 6) | (FLAG_AC << 4) |
(FLAG_P << 2) | (1 << 1) | (FLAG_CY << 0));
}
// static const char *reg_name [8] = { "B", "C", "D", "E", "H", "L", "M", "A" };
// static const char *rp_name [4] = { "BC", "DE", "HL", "SP" };
uint8_t mem [0x10000];
static bool eval_cond (int cond)
{
switch (cond)
{
case 0: // NZ not zero
return ! FLAG_Z;
case 1: // Z zero
return FLAG_Z;
case 2: // NC not carry
return ! FLAG_CY;
case 3: // C carry
return FLAG_CY;
case 4: // PO parity odd
return ! FLAG_P;
case 5: // PE parity even
return FLAG_P;
case 6: // P positive
return ! FLAG_S;
case 7: // M minus
return FLAG_S;
}
return false; // should never get here
}
static void illegal_op (uint8_t opcode)
{
// $$$
halted = 1;
printf ("illegal opcode %04o at pc=%04x\n", opcode, pc);
}
#define DISK_BUF 0xfbfa /* 16-bit */
#define DISK_SECT 0xfbfc
#define DISK_CYL 0xfbfd /* 16-bit */
#define DISK_DRIVE 0xfbff /* 1=A, 2=B */
#define MAX_DISK_DRIVE 2
FILE *disk [MAX_DISK_DRIVE];
static void op_disk_io (uint8_t opcode UNUSED)
{
uint8_t byte2 = mem_read ((pc) & 0xffff);
uint8_t byte3 = mem_read ((pc+1) & 0xffff);
if ((byte2 != 0xed) || ((byte3 & 0xfe) != 0x02))
{
halted = 1;
printf ("bad inst ED %02x %02x\n", byte2, byte3);
return;
}
bool write = byte3 == 0x03;
uint16_t buf_addr = mem_read (DISK_BUF) | (mem_read (DISK_BUF + 1) << 8);
uint8_t sector = mem_read (DISK_SECT);
uint16_t cylinder = mem_read (DISK_CYL) | (mem_read (DISK_CYL + 1) << 8);
uint8_t drive = mem_read (DISK_DRIVE);
if ((drive < 1) || (drive > MAX_DISK_DRIVE))
{
halted = 1;
printf ("bad disk drive number %d\n", drive);
return;
}
drive--;
#if 0
printf ("disk %s drive %u cyl %5u sect %3u buffer %04x\r\n",
write ? "write" : "read ",
drive, cylinder, sector, buf_addr);
#endif
if (! disk [drive])
{
char *fn = drive ? "a.img" : "b.img";
disk [drive] = fopen (fn, "rb+");
if (! disk [drive])
{
halted = 1;
printf ("can't open image file '%s'\n", fn);
return;
}
}
fseek (disk [drive], ((cylinder * 128) + sector) * 128, SEEK_SET);
if (write)
fwrite (mem + buf_addr, // ptr
128, // size
1, // nmemb
disk [drive]);
else
fread (mem + buf_addr, // ptr
128, // size
1, // nmemb
disk [drive]);
REG_A = 0x00; // success
pc += 2;
}
// ------------------------------------------------------------
// Data Transfer Group
// Flags not affected by any instruction in this group
// ------------------------------------------------------------
static void op_MOV (uint8_t opcode)
{
// no flags affected
uint8_t data;
int dst = (opcode >> 3) & 7;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
if (dst == REG_IDX_M)
mem_write (REG_PAIR_HL, data);
else
REG (dst) = data;
}
static void op_MVI (uint8_t opcode)
{
// no flags affected
uint8_t data;
int op543 = (opcode >> 3) & 7;
data = mem_read (pc++);
if (op543 == REG_IDX_M)
mem_write (REG_PAIR_HL, data);
else
REG (op543) = data;
}
static void op_LXI (uint8_t opcode)
{
// no flags affected
uint16_t data;
int op54 = (opcode >> 4) & 3;
data = mem_read (pc++);
data |= (mem_read (pc++) << 8);
REG_PAIR (op54) = data;
}
static void op_LDA (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
REG_A = mem_read (addr);
}
static void op_STA (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
mem_write (addr, REG_A);
}
static void op_LHLD (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
REG_L = mem_read (addr++);
REG_H = mem_read (addr);
}
static void op_SHLD (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
mem_write (addr++, REG_L);
mem_write (addr, REG_H);
}
static void op_LDAX (uint8_t opcode)
{
// no flags affected
int op54 = (opcode >> 4) & 3;
REG_A = mem_read (REG_PAIR (op54));
}
static void op_STAX (uint8_t opcode)
{
// no flags affected
int op54 = (opcode >> 4) & 3;
mem_write (REG_PAIR (op54), REG_A);
}
static void op_XCHG (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t temp;
temp = REG_PAIR_DE;
REG_PAIR_DE = REG_PAIR_HL;
REG_PAIR_HL = temp;
}
// ------------------------------------------------------------
// Arithmetic Group
// Most instructions affect Z, S, P, CY, AC by standard rules
// ------------------------------------------------------------
static void op_ADD (uint8_t opcode)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
FLAG_AC = ((REG_A & 0x0f) + (data & 0x0f)) >> 4;
sum = REG_A + data;
FLAG_CY = sum >> 8;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_ADI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
data = mem_read (pc++);
FLAG_AC = ((REG_A & 0x0f) + (data & 0x0f)) >> 4;
sum = REG_A + data;
FLAG_CY = sum >> 8;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_ADC (uint8_t opcode)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + FLAG_CY) >> 4) & 1;
sum = REG_A + data + FLAG_CY;
FLAG_CY = sum >> 8;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_ACI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
data = mem_read (pc++);
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + FLAG_CY) >> 4) & 1;
sum = REG_A + data + FLAG_CY;
FLAG_CY = sum >> 8;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_SUB (uint8_t opcode)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + 1) >> 4) & 1;
sum = REG_A + data + 1;
FLAG_CY = (sum >> 8) ^ 1;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_SUI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
data = mem_read (pc++);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + 1) >> 4) & 1;
sum = REG_A + data + 1;
FLAG_CY = (sum >> 8) ^ 1;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_SBB (uint8_t opcode)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + (FLAG_CY ^ 1)) >> 4) & 1;
sum = REG_A + data + (FLAG_CY ^ 1);
FLAG_CY = (sum >> 8) ^ 1;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_SBI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
uint8_t data;
uint16_t sum;
data = mem_read (pc++);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + (FLAG_CY ^ 1)) >> 4) & 1;
sum = REG_A + data + (FLAG_CY ^ 1);
FLAG_CY = (sum >> 8) ^ 1;
REG_A = sum & 0xff;
SET_FLAG_SZP (REG_A);
}
static void op_INR (uint8_t opcode)
{
// affects Z, S, P, AC -- DOES NOT affect CY
int reg = (opcode >> 3) & 7;
uint8_t data;
// We could do this more efficiently if we didn't need to set AC.
// Does the real 8080 actually do that?
if (reg == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (reg);
FLAG_AC = (((data & 0x0f) + 1) >> 4) & 1;
data++;
if (reg == REG_IDX_M)
mem_write (REG_PAIR_HL, data);
else
REG (reg) = data;
SET_FLAG_SZP (data);
}
static void op_DCR (uint8_t opcode)
{
// affects Z, S, P, AC -- DOES NOT affect CY
int reg = (opcode >> 3) & 7;
uint8_t data;
// We could do this more efficiently if we didn't need to set AC.
// Does the real 8080 actually do that?
if (reg == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (reg);
FLAG_AC = (((data & 0x0f) + 0xf) >> 4) & 1;
data--;
if (reg == REG_IDX_M)
mem_write (REG_PAIR_HL, data);
else
REG (reg) = data;
SET_FLAG_SZP (data);
}
static void op_INX (uint8_t opcode)
{
// no flags affected
int op54 = (opcode >> 4) & 3;
REG_PAIR (op54)++;
}
static void op_DCX (uint8_t opcode)
{
// no flags affected
int op54 = (opcode >> 4) & 3;
REG_PAIR (op54)--;
}
static void op_DAD (uint8_t opcode)
{
// affects CY flag ONLY
int op54 = (opcode >> 4) & 3;
uint32_t sum;
sum = REG_PAIR_HL + REG_PAIR (op54);
REG_PAIR_HL = sum & 0xffff;
FLAG_CY = (sum >> 16) & 1;
}
static void op_DAA (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
uint8_t adjust = 0x00;
if (((REG_A & 0xf) > 9) || FLAG_AC)
adjust = 0x06;
if ((((REG_A >> 4) > 9) || FLAG_CY) ||
(((REG_A >> 4) == 9) && (FLAG_CY || ((REG_A & 0xf) > 9))))
adjust |= 0x60;
FLAG_AC = (REG_A & 0xf) >= 0xa;
if ((((REG_A >> 4) >= 9) && ((REG_A & 0xf) >= 0xa)) ||
((REG_A >> 4) >= 0xa))
FLAG_CY = 1;
REG_A += adjust;
SET_FLAG_SZP (REG_A);
}
// "Intel 8080 Microcomputer Systems Users Manual" of Sept. 1975
// classifies CMP and CPI as logical instructions!
static void op_CMP (uint8_t opcode)
{
// affects Z, S, P, CY, AC
// Z set if A=r
// CY set if A<r
// $$$ I've assumed that CMP is just SUB without writing result
// to accumulator. Better test that!
uint8_t data;
uint16_t sum;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + 1) >> 4) & 1;
sum = REG_A + data + 1;
FLAG_CY = (sum >> 8) ^ 1;
SET_FLAG_SZP (sum & 0xff);
}
static void op_CPI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC
// Z set if A=r
// CY set if A<r
// $$$ I've assumed that CPI is just SUI without writing result
// to accumulator. Better test that!
uint8_t data;
uint16_t sum;
data = mem_read (pc++);
data ^= 0xff;
FLAG_AC = (((REG_A & 0x0f) + (data & 0x0f) + 1) >> 4) & 1;
sum = REG_A + data + 1;
FLAG_CY = (sum >> 8) ^ 1;
SET_FLAG_SZP (sum & 0xff);
}
// ------------------------------------------------------------
// Logical Group
// Most instructions affect Z, S, P, CY, AC by standard rules
// ------------------------------------------------------------
static void op_ANA (uint8_t opcode)
{
// affects Z, S, P, CY, AC - CY is cleared
// 8080: AC set to the logical OR of bits 3 of the operands
// 8085: AC set to 1
uint8_t data;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
FLAG_AC = ((REG_A | data) >> 3) & 1; // 8080 only, 8085 sets to 1
REG_A &= data;
FLAG_CY = 0;
SET_FLAG_SZP (REG_A);
}
static void op_ANI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC - CY is cleared
// 8080: AC set to the logical OR of bits 3 of the operands
// 8085: AC set to 1
uint8_t data;
data = mem_read (pc++);
FLAG_AC = ((REG_A | data) >> 3) & 1; // 8080 only, 8085 sets to 1
REG_A &= data;
FLAG_CY = 0;
SET_FLAG_SZP (REG_A);
}
static void op_XRA (uint8_t opcode)
{
// affects Z, S, P, CY, AC - CY and AC are cleared
uint8_t data;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
REG_A ^= data;
FLAG_CY = 0;
FLAG_AC = 0;
SET_FLAG_SZP (REG_A);
}
static void op_XRI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC - CY and AC are cleared
uint8_t data;
data = mem_read (pc++);
REG_A ^= data;
FLAG_CY = 0;
FLAG_AC = 0;
SET_FLAG_SZP (REG_A);
}
static void op_ORA (uint8_t opcode)
{
// affects Z, S, P, CY, AC - CY and AC are cleared
uint8_t data;
int src = opcode & 7;
if (src == REG_IDX_M)
data = mem_read (REG_PAIR_HL);
else
data = REG (src);
REG_A |= data;
FLAG_CY = 0;
FLAG_AC = 0;
SET_FLAG_SZP (REG_A);
}
static void op_ORI (uint8_t opcode UNUSED)
{
// affects Z, S, P, CY, AC - CY and AC are cleared
uint8_t data;
data = mem_read (pc++);
REG_A |= data;
FLAG_CY = 0;
FLAG_AC = 0;
SET_FLAG_SZP (REG_A);
}
static void op_RLC (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
REG_A = (REG_A << 1) | (REG_A >> 7);
FLAG_CY = REG_A & 0x01;
}
static void op_RRC (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
REG_A = (REG_A >> 1) | (REG_A << 7);
FLAG_CY = (REG_A >> 7) & 1;
}
static void op_RAL (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
uint16_t data;
data = (REG_A << 1) | FLAG_CY;
REG_A = data;
FLAG_CY = (data >> 8) & 1;
}
static void op_RAR (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
uint16_t data;
data = (FLAG_CY << 8) | REG_A;
REG_A = data >> 1;
FLAG_CY = data & 1;
}
static void op_CMA (uint8_t opcode UNUSED)
{
// no flags affected
REG_A ^= 0xff;
}
static void op_CMC (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
FLAG_CY ^= 1;
}
static void op_STC (uint8_t opcode UNUSED)
{
// affects CY flag ONLY
FLAG_CY = 1;
}
// ------------------------------------------------------------
// Branch Group
// Flags not affected by any instruction in this group
// ------------------------------------------------------------
static void op_JMP (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc) << 8);
if (bdos_sim && (addr == 0x0000))
{
printf ("jump to CP/M warm boot from pc=%04x\n", (pc - 2) & 0xffff);
halted = true;
}
else
pc = addr;
}
static void op_Jcond (uint8_t opcode)
{
// no flags affected
int cond = (opcode >> 3) & 7;
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
if (eval_cond (cond))
pc = addr;
}
static void op_CALL (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
if (bdos_sim && (addr == 0x0005))
bdos_call ();
else
{
mem_write (--REG_SP, pc >> 8);
mem_write (--REG_SP, pc & 0xff);
pc = addr;
}
}
static void op_Ccond (uint8_t opcode)
{
// no flags affected
int cond = (opcode >> 3) & 7;
uint16_t addr;
addr = mem_read (pc++);
addr |= (mem_read (pc++) << 8);
if (eval_cond (cond))
{
mem_write (--REG_SP, pc >> 8);
mem_write (--REG_SP, pc & 0xff);
pc = addr;
cycle_count += 6;
}
}
static void op_RET (uint8_t opcode UNUSED)
{
// no flags affected
pc = mem_read (REG_SP++);
pc |= (mem_read (REG_SP++) << 8);
}
static void op_Rcond (uint8_t opcode)
{
// no flags affected
int cond = (opcode >> 3) & 7;
if (eval_cond (cond))
{
pc = mem_read (REG_SP++);
pc |= (mem_read (REG_SP++) << 8);
cycle_count += 6;
}
}
static void op_RST (uint8_t opcode)
{
// no flags affected
mem_write (--REG_SP, pc >> 8);
mem_write (--REG_SP, pc & 0xff);
pc = opcode & 070;
}
static void op_PCHL (uint8_t opcode UNUSED)
{
// no flags affected
pc = REG_PAIR_HL;
}
// ------------------------------------------------------------
// Stack, I/O and Machine Control Group
// Unless otherwise specified, flags are not affected by any
// instruction in this group
// ------------------------------------------------------------
static void op_PUSH (uint8_t opcode)
{
// no flags affected
int rp = (opcode >> 4) & 3;
uint16_t data;
if (rp == 3) // PSW
data = (REG_A << 8) | get_f ();
else
data = REG_PAIR (rp);
mem_write (--REG_SP, data >> 8);
mem_write (--REG_SP, data & 0xff);
}
static void op_POP (uint8_t opcode)
{
// POP BC, DE, HL: no flags affected
// POP PSW: all flags affected
int rp = (opcode >> 4) & 3;
uint16_t data;
data = mem_read (REG_SP++);
data |= (mem_read (REG_SP++) << 8);
if (rp == 3) // PSW
{
REG_A = data >> 8;
set_f (data & 0xff);
}
else
REG_PAIR (rp) = data;
}
static void op_XTHL (uint8_t opcode UNUSED)
{
// no flags affected
uint16_t data;
data = mem_read (REG_SP++);
data |= (mem_read (REG_SP) << 8);
mem_write (REG_SP--, REG_H);
mem_write (REG_SP, REG_L);
REG_PAIR_HL = data;
}
static void op_SPHL (uint8_t opcode UNUSED)
{
// no flags affected
REG_SP = REG_PAIR_HL;
}
static void op_IN (uint8_t opcode UNUSED)
{
// no flags affected
uint8_t port;
uint8_t c;
fd_set read_fds, write_fds, except_fds;
int status;
struct timeval timeout;
port = mem_read (pc++);
switch (port)
{
case 0x00:
// read key pressed status
FD_ZERO (& read_fds);
FD_ZERO (& write_fds);
FD_ZERO (& except_fds);
FD_SET (STDIN_FILENO, & read_fds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
status = select (1, & read_fds, & write_fds, & except_fds, & timeout);
if (status < 0)
fatal (EX_IOERR, "select() error %d", status);
REG_A = FD_ISSET (STDIN_FILENO, & read_fds);
break;
case 0x01:
// read key
read (STDIN_FILENO, & c, 1);
REG_A = c;
break;
case 0xf8: // Sol-20 UART status port
REG_A = 0x80; // transmit buffer empty
break;
}
}
static void op_OUT (uint8_t opcode UNUSED)
{
// no flags affected
// $$$
uint8_t port;
uint8_t c;
c = REG_A;
port = mem_read (pc++);
switch (port)
{
case 0x00:
case 0xf9: // Sol-20 UART data port
write (STDOUT_FILENO, & c, 1);
break;
}
}
static void op_EI (uint8_t opcode UNUSED)
{
// no flags affected
interrupt_enabled = true;
}
static void op_DI (uint8_t opcode UNUSED)
{
// no flags affected
interrupt_enabled = false;
}
static void op_HLT (uint8_t opcode UNUSED)
{
// no flags affected
halted = true;
printf ("halt at pc=%04x\n", pc);
}
static void op_NOP (uint8_t opcode UNUSED)
{
// no flags affected
// do nothing