-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathi8085.cpp
More file actions
1663 lines (1506 loc) · 42.1 KB
/
Copy pathi8085.cpp
File metadata and controls
1663 lines (1506 loc) · 42.1 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
// license:BSD-3-Clause
// copyright-holders:Juergen Buchmueller, Roberto Fresca, Grull Osgo
// thanks-to:Marcel De Kogel
/*****************************************************************************
*
* Portable I8085A emulator V1.3
*
* Copyright Juergen Buchmueller
* Partially based on information out of Z80Em by Marcel De Kogel
*
* TODO:
* - not sure if 8085 DSUB H flag is correct
* - accurate 8085 undocumented V/K flags
* - most of those is_8085 can probably be done better, like function overrides
*
* ---------------------------------------------------------------------------
*
* changes in V1.3
* - Added undocumented opcodes for the 8085A, based on a german
* book about microcomputers: "Mikrocomputertechnik mit dem
* Prozessor 8085A".
* - This book also suggest that INX/DCX should modify the X flag bit
* for a LSB to MSB carry and
* - that jumps take 10 T-states only when they're executed, 7 when
* they're skipped.
* Thanks for the info and a copy of the tables go to Timo Sachsenberg
* <timo.sachsenberg@student.uni-tuebingen.de>
* changes in V1.2
* - corrected cycle counts for these classes of opcodes
* Thanks go to Jim Battle <frustum@pacbell.bet>
*
* 808x Z80
* DEC A 5 4 \
* INC A 5 4 \
* LD A,B 5 4 >-- Z80 is faster
* JP (HL) 5 4 /
* CALL cc,nnnn: 11/17 10/17 /
*
* INC HL 5 6 \
* DEC HL 5 6 \
* LD SP,HL 5 6 \
* ADD HL,BC 10 11 \
* INC (HL) 10 11 >-- 8080 is faster
* DEC (HL) 10 11 /
* IN A,(#) 10 11 /
* OUT (#),A 10 11 /
* EX (SP),HL 18 19 /
*
* Revisions:
*
* xx-xx-2002 Acho A. Tang
* - 8085 emulation was in fact never used. It's been treated as a plain 8080.
* - protected IRQ0 vector from being overwritten
* - modified interrupt handler to properly process 8085-specific IRQ's
* - corrected interrupt masking, RIM and SIM behaviors according to Intel's documentation
*
* 20-Jul-2002 Krzysztof Strzecha
* - SBB r instructions should affect parity flag.
* Fixed only for non x86 asm version (#define i8080_EXACT 1).
* There are probably more opcodes which should affect this flag, but don't.
* - JPO nnnn and JPE nnnn opcodes in disassembler were misplaced. Fixed.
* - Undocumented i8080 opcodes added:
* 08h, 10h, 18h, 20h, 28h, 30h, 38h - NOP
* 0CBh - JMP
* 0D9h - RET
* 0DDh, 0EDh, 0FDh - CALL
* Thanks for the info go to Anton V. Ignatichev.
*
* 08-Dec-2002 Krzysztof Strzecha
* - ADC r instructions should affect parity flag.
* Fixed only for non x86 asm version (#define i8080_EXACT 1).
* There are probably more opcodes which should affect this flag, but don't.
*
* 05-Sep-2003 Krzysztof Strzecha
* - INR r, DCR r, ADD r, SUB r, CMP r instructions should affect parity flag.
* Fixed only for non x86 asm version (#define i8080_EXACT 1).
*
* 23-Dec-2006 Tomasz Slanina
* - SIM fixed
*
* 28-Jan-2007 Zsolt Vasvari
* - Removed archaic i8080_EXACT flag.
*
* 08-June-2008 Miodrag Milanovic
* - Flag setting fix for some instructions and cycle count update
*
* August 2009, hap
* - removed DAA table
* - fixed accidental double memory reads due to macro overuse
* - fixed cycle deduction on unconditional CALL / RET
* - added cycle tables and cleaned up big switch source layout (1 tab = 4 spaces)
* - removed HLT cycle eating (earlier, HLT after EI could theoretically fail)
* - fixed parity flag on add/sub/cmp
* - renamed temp register XX to official name WZ
* - renamed flags from Z80 style S Z Y H X V N C to S Z X5 H X3 P V C, and
* fixed X5 / V flags where accidentally broken due to flag names confusion
*
* 21-Aug-2009, Curt Coder
* - added 8080A variant
* - refactored callbacks to use devcb
*
* October 2012, hap
* - fixed H flag on subtraction opcodes
* - on 8080, don't push the unsupported flags(X5, X3, V) to stack
* - it passes on 8080/8085 CPU Exerciser (ref: http://www.idb.me.uk/sunhillow/8080.html
* tests only 8080 opcodes, link is dead so go via archive.org)
*
* April 2025, Roberto Fresca
* - Reworked the DSUB (Double Subtraction) undocumented instruction.
* - Reworked the RDEL (Rotate D and E Left with Carry) undocumented instruction.
* (ref: https://robertofresca.com/files/New_8085_instruction.pdf)
*
*****************************************************************************/
#include "emu.h"
#include "i8085.h"
#include "8085dasm.h"
#include <bit>
#define VERBOSE 0
#include "logmacro.h"
/***************************************************************************
CONSTANTS
***************************************************************************/
constexpr u8 SF = 0x80;
constexpr u8 ZF = 0x40;
constexpr u8 KF = 0x20;
constexpr u8 HF = 0x10;
constexpr u8 X3F = 0x08;
constexpr u8 PF = 0x04;
constexpr u8 VF = 0x02;
constexpr u8 CF = 0x01;
constexpr u8 IM_SID = 0x80;
constexpr u8 IM_I75 = 0x40;
constexpr u8 IM_I65 = 0x20;
constexpr u8 IM_I55 = 0x10;
constexpr u8 IM_IE = 0x08;
constexpr u8 IM_M75 = 0x04;
constexpr u8 IM_M65 = 0x02;
constexpr u8 IM_M55 = 0x01;
constexpr u16 ADDR_TRAP = 0x0024;
constexpr u16 ADDR_RST55 = 0x002c;
constexpr u16 ADDR_RST65 = 0x0034;
constexpr u16 ADDR_RST75 = 0x003c;
/***************************************************************************
STATIC TABLES
***************************************************************************/
/* cycles lookup */
const u8 i8085a_cpu_device::lut_cycles_8080[256]={
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 4, 10,7, 5, 5, 5, 7, 4, 4, 10,7, 5, 5, 5, 7, 4,
/* 1 */ 4, 10,7, 5, 5, 5, 7, 4, 4, 10,7, 5, 5, 5, 7, 4,
/* 2 */ 4, 10,16,5, 5, 5, 7, 4, 4, 10,16,5, 5, 5, 7, 4,
/* 3 */ 4, 10,13,5, 10,10,10,4, 4, 10,13,5, 5, 5, 7, 4,
/* 4 */ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5,
/* 5 */ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5,
/* 6 */ 5, 5, 5, 5, 5, 5, 7, 5, 5, 5, 5, 5, 5, 5, 7, 5,
/* 7 */ 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 7, 5,
/* 8 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* 9 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* A */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* B */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* C */ 5, 10,10,10,11,11,7, 11,5, 10,10,10,11,11,7, 11,
/* D */ 5, 10,10,10,11,11,7, 11,5, 10,10,10,11,11,7, 11,
/* E */ 5, 10,10,18,11,11,7, 11,5, 5, 10,4, 11,11,7, 11,
/* F */ 5, 10,10,4, 11,11,7, 11,5, 5, 10,4, 11,11,7, 11 };
const u8 i8085a_cpu_device::lut_cycles_8085[256]={
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* 0 */ 4, 10,7, 6, 4, 4, 7, 4, 10,10,7, 6, 4, 4, 7, 4,
/* 1 */ 7, 10,7, 6, 4, 4, 7, 4, 10,10,7, 6, 4, 4, 7, 4,
/* 2 */ 4, 10,16,6, 4, 4, 7, 4, 10,10,16,6, 4, 4, 7, 4,
/* 3 */ 4, 10,13,6, 10,10,10,4, 10,10,13,6, 4, 4, 7, 4,
/* 4 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* 5 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* 6 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* 7 */ 7, 7, 7, 7, 7, 7, 5, 7, 4, 4, 4, 4, 4, 4, 7, 4,
/* 8 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* 9 */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* A */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* B */ 4, 4, 4, 4, 4, 4, 7, 4, 4, 4, 4, 4, 4, 4, 7, 4,
/* C */ 6, 10,7, 7, 9, 12,7, 12,6, 10,7, 6, 9, 9, 7, 12,
/* D */ 6, 10,7, 10,9, 12,7, 12,6, 10,7, 10,9, 7, 7, 12,
/* E */ 6, 10,7, 16,9, 12,7, 12,6, 6, 7, 4, 9, 10,7, 12,
/* F */ 6, 10,7, 4, 9, 12,7, 12,6, 6, 7, 4, 9, 7, 7, 12 };
/* special cases (partially taken care of elsewhere):
base c taken?
op_ret 8080 5 +6(11) (conditional)
op_ret 8085 6 +6(12) (conditional)
op_jmp 8080 10 +0
op_jmp 8085 7 +3(10)
op_call 8080 11 +6(17)
op_call 8085 9 +9(18)
*/
DEFINE_DEVICE_TYPE(I8080, i8080_cpu_device, "i8080", "Intel 8080")
DEFINE_DEVICE_TYPE(I8080A, i8080a_cpu_device, "i8080a", "Intel 8080A")
DEFINE_DEVICE_TYPE(I8085A, i8085a_cpu_device, "i8085a", "Intel 8085A")
i8085a_cpu_device::i8085a_cpu_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
: cpu_device(mconfig, type, tag, owner, clock)
, m_program_config("program", ENDIANNESS_LITTLE, 8, 16, 0)
, m_io_config("io", ENDIANNESS_LITTLE, 8, 8, 0)
, m_opcode_config("opcodes", ENDIANNESS_LITTLE, 8, 16, 0)
, m_in_inta_func(*this, 0)
, m_out_status_func(*this)
, m_out_inte_func(*this)
, m_in_sid_func(*this, 0)
, m_out_sod_func(*this)
, m_clk_out_func(*this)
{ }
i8085a_cpu_device::i8085a_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: i8085a_cpu_device(mconfig, I8085A, tag, owner, clock)
{ }
i8080_cpu_device::i8080_cpu_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
: i8085a_cpu_device(mconfig, type, tag, owner, clock)
{ }
i8080_cpu_device::i8080_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: i8080_cpu_device(mconfig, I8080, tag, owner, clock)
{ }
i8080a_cpu_device::i8080a_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
: i8080_cpu_device(mconfig, I8080A, tag, owner, clock)
{ }
device_memory_interface::space_config_vector i8085a_cpu_device::memory_space_config() const
{
return has_configured_map(AS_OPCODES) ? space_config_vector
{
std::make_pair(AS_PROGRAM, &m_program_config),
std::make_pair(AS_IO, &m_io_config),
std::make_pair(AS_OPCODES, &m_opcode_config)
} : space_config_vector
{
std::make_pair(AS_PROGRAM, &m_program_config),
std::make_pair(AS_IO, &m_io_config)
};
}
void i8085a_cpu_device::device_config_complete()
{
m_clk_out_func.resolve_safe();
m_clk_out_func(clock() / 2);
}
void i8085a_cpu_device::device_clock_changed()
{
m_clk_out_func(clock() / 2);
}
/***************************************************************************
CORE INITIALIZATION
***************************************************************************/
void i8085a_cpu_device::init_tables()
{
for (unsigned i = 0; i < 256; i++)
{
// cycles
lut_cycles[i] = is_8085() ? lut_cycles_8085[i] : lut_cycles_8080[i];
// flags
u8 zs = 0;
if (i == 0) zs |= ZF;
if (i & 0x80) zs |= SF;
u8 p = (std::popcount(i) & 1) ? 0 : PF;
lut_zs[i] = zs;
lut_zsp[i] = zs | p;
}
}
void i8085a_cpu_device::device_start()
{
m_PC.d = 0;
m_SP.d = 0;
m_AF.d = 0;
m_BC.d = 0;
m_DE.d = 0;
m_HL.d = 0;
m_WZ.d = 0;
m_halt = 0;
m_im = IM_IE; // INTE will go low at reset
m_status = 0;
m_after_ei = 0;
m_nmi_state = 0;
m_irq_state[3] = m_irq_state[2] = m_irq_state[1] = m_irq_state[0] = 0;
m_trap_pending = false;
m_trap_im_copy = 0;
m_sod_state = 1; // SOD will go low at reset
m_in_acknowledge = false;
m_ietemp = 0;
init_tables();
// set up the state table
state_add(I8085_PC, "PC", m_PC.w.l);
state_add(STATE_GENPC, "GENPC", m_PC.w.l).noshow();
state_add(STATE_GENPCBASE, "CURPC", m_PC.w.l).noshow();
state_add(I8085_SP, "SP", m_SP.w.l);
state_add(STATE_GENFLAGS, "GENFLAGS", m_AF.b.l).noshow().formatstr("%8s");
state_add(I8085_A, "A", m_AF.b.h).noshow();
state_add(I8085_B, "B", m_BC.b.h).noshow();
state_add(I8085_C, "C", m_BC.b.l).noshow();
state_add(I8085_D, "D", m_DE.b.h).noshow();
state_add(I8085_E, "E", m_DE.b.l).noshow();
state_add(I8085_F, "F", m_AF.b.l).noshow();
state_add(I8085_H, "H", m_HL.b.h).noshow();
state_add(I8085_L, "L", m_HL.b.l).noshow();
state_add(I8085_AF, "AF", m_AF.w.l);
state_add(I8085_BC, "BC", m_BC.w.l);
state_add(I8085_DE, "DE", m_DE.w.l);
state_add(I8085_HL, "HL", m_HL.w.l);
if (is_8085())
{
state_add(I8085_IM, "IM", m_im);
state_add(I8085_SOD, "SOD", m_sod_state).mask(0x1);
state_add(I8085_SID, "SID", m_ietemp).mask(0x1).callimport().callexport();
}
else
{
state_add(I8085_STATUS, "STATUS", m_status);
state_add(I8085_INTE, "INTE", m_ietemp).mask(0x1).callimport().callexport();
}
space(AS_PROGRAM).cache(m_cprogram);
space(AS_PROGRAM).specific(m_program);
space(has_space(AS_OPCODES) ? AS_OPCODES : AS_PROGRAM).cache(m_copcodes);
space(AS_IO).specific(m_io);
// register for state saving
save_item(NAME(m_PC.w.l));
save_item(NAME(m_SP.w.l));
save_item(NAME(m_AF.w.l));
save_item(NAME(m_BC.w.l));
save_item(NAME(m_DE.w.l));
save_item(NAME(m_HL.w.l));
save_item(NAME(m_halt));
save_item(NAME(m_im));
save_item(NAME(m_status));
save_item(NAME(m_after_ei));
save_item(NAME(m_nmi_state));
save_item(NAME(m_irq_state));
save_item(NAME(m_trap_pending));
save_item(NAME(m_trap_im_copy));
save_item(NAME(m_sod_state));
save_item(NAME(m_in_acknowledge));
set_icountptr(m_icount);
}
/***************************************************************************
COMMON RESET
***************************************************************************/
void i8085a_cpu_device::device_reset()
{
m_PC.d = 0;
m_halt = 0;
m_im &= ~IM_I75;
m_im |= IM_M55 | IM_M65 | IM_M75;
m_after_ei = 0;
m_trap_pending = false;
m_trap_im_copy = 0;
set_inte(0);
set_sod(0);
}
/***************************************************************************
COMMON STATE IMPORT/EXPORT
***************************************************************************/
void i8085a_cpu_device::state_import(const device_state_entry &entry)
{
switch (entry.index())
{
case I8085_SID:
if (m_ietemp)
{
m_im |= IM_SID;
}
else
{
m_im &= ~IM_SID;
}
break;
case I8085_INTE:
if (m_ietemp)
{
m_im |= IM_IE;
}
else
{
m_im &= ~IM_IE;
}
break;
default:
fatalerror("CPU_IMPORT_STATE(i808x) called for unexpected value\n");
}
}
void i8085a_cpu_device::state_export(const device_state_entry &entry)
{
switch (entry.index())
{
case I8085_SID:
m_ietemp = ((m_im & IM_SID) && m_in_sid_func()) ? 1 : 0;
break;
case I8085_INTE:
m_ietemp = (m_im & IM_IE) ? 1 : 0;
break;
default:
fatalerror("CPU_EXPORT_STATE(i808x) called for unexpected value\n");
}
}
void i8085a_cpu_device::state_string_export(const device_state_entry &entry, std::string &str) const
{
switch (entry.index())
{
case STATE_GENFLAGS:
str = string_format("%c%c%c%c.%c%c%c",
m_AF.b.l & 0x80 ? 'S':'.',
m_AF.b.l & 0x40 ? 'Z':'.',
m_AF.b.l & 0x20 ? 'K':'.', // X5
m_AF.b.l & 0x10 ? 'H':'.',
m_AF.b.l & 0x04 ? 'P':'.',
m_AF.b.l & 0x02 ? 'V':'.',
m_AF.b.l & 0x01 ? 'C':'.');
break;
}
}
void i8080_cpu_device::state_string_export(const device_state_entry &entry, std::string &str) const
{
switch (entry.index())
{
case STATE_GENFLAGS:
str = string_format("%c%c.%c.%c.%c",
m_AF.b.l & 0x80 ? 'S':'.',
m_AF.b.l & 0x40 ? 'Z':'.',
m_AF.b.l & 0x10 ? 'H':'.',
m_AF.b.l & 0x04 ? 'P':'.',
m_AF.b.l & 0x01 ? 'C':'.');
break;
}
}
std::unique_ptr<util::disasm_interface> i8085a_cpu_device::create_disassembler()
{
return std::make_unique<i8085_disassembler>();
}
/***************************************************************************
INTERRUPTS
***************************************************************************/
void i8085a_cpu_device::execute_set_input(int irqline, int state)
{
int newstate = (state != CLEAR_LINE);
// TRAP is level and edge-triggered NMI
if (irqline == I8085_TRAP_LINE)
{
if (!m_nmi_state && newstate)
m_trap_pending = true;
else if (!newstate)
m_trap_pending = false;
m_nmi_state = newstate;
}
// RST7.5 is edge-triggered
else if (irqline == I8085_RST75_LINE)
{
if (!m_irq_state[I8085_RST75_LINE] && newstate)
m_im |= IM_I75;
m_irq_state[I8085_RST75_LINE] = newstate;
}
// remaining sources are level triggered
else if (irqline < std::size(m_irq_state))
m_irq_state[irqline] = state;
}
void i8085a_cpu_device::break_halt_for_interrupt()
{
// de-halt if necessary
if (m_halt)
{
m_PC.w.l++;
m_halt = 0;
set_status(0x26); // int ack while halt
}
else
set_status(0x23); // int ack
m_in_acknowledge = true;
}
void i8085a_cpu_device::check_for_interrupts()
{
// TRAP is the highest priority
if (m_trap_pending)
{
// the first RIM after a TRAP reflects the original IE state; remember it here,
// setting the high bit to indicate it is valid
m_trap_im_copy = m_im | 0x80;
// reset the pending state
m_trap_pending = false;
// break out of HALT state and call the IRQ ack callback
break_halt_for_interrupt();
standard_irq_callback(I8085_TRAP_LINE, m_PC.w.l);
// push the PC and jump to $0024
op_push(m_PC);
set_inte(0);
m_PC.w.l = ADDR_TRAP;
m_icount -= 11;
}
// followed by RST7.5
else if ((m_im & IM_I75) && !(m_im & IM_M75) && (m_im & IM_IE))
{
// reset the pending state (which is CPU-visible via the RIM instruction)
m_im &= ~IM_I75;
// break out of HALT state and call the IRQ ack callback
break_halt_for_interrupt();
standard_irq_callback(I8085_RST75_LINE, m_PC.w.l);
// push the PC and jump to $003C
op_push(m_PC);
set_inte(0);
m_PC.w.l = ADDR_RST75;
m_icount -= 11;
}
// followed by RST6.5
else if (m_irq_state[I8085_RST65_LINE] && !(m_im & IM_M65) && (m_im & IM_IE))
{
// break out of HALT state and call the IRQ ack callback
break_halt_for_interrupt();
standard_irq_callback(I8085_RST65_LINE, m_PC.w.l);
// push the PC and jump to $0034
op_push(m_PC);
set_inte(0);
m_PC.w.l = ADDR_RST65;
m_icount -= 11;
}
// followed by RST5.5
else if (m_irq_state[I8085_RST55_LINE] && !(m_im & IM_M55) && (m_im & IM_IE))
{
// break out of HALT state and call the IRQ ack callback
break_halt_for_interrupt();
standard_irq_callback(I8085_RST55_LINE, m_PC.w.l);
// push the PC and jump to $002C
op_push(m_PC);
set_inte(0);
m_PC.w.l = ADDR_RST55;
m_icount -= 11;
}
// followed by classic INTR
else if (m_irq_state[I8085_INTR_LINE] && (m_im & IM_IE))
{
// break out of HALT state and call the IRQ ack callback
if (!m_in_inta_func.isunset())
standard_irq_callback(I8085_INTR_LINE, m_PC.w.l);
break_halt_for_interrupt();
u8 vector = read_inta();
// use the resulting vector as an opcode to execute
set_inte(0);
LOG("i8085 take int $%02x\n", vector);
execute_one(vector);
}
}
/***************************************************************************
OPCODE HELPERS
***************************************************************************/
void i8085a_cpu_device::set_sod(int state)
{
if (state != 0 && m_sod_state == 0)
{
m_sod_state = 1;
m_out_sod_func(m_sod_state);
}
else if (state == 0 && m_sod_state != 0)
{
m_sod_state = 0;
m_out_sod_func(m_sod_state);
}
}
void i8085a_cpu_device::set_inte(int state)
{
if (state != 0 && (m_im & IM_IE) == 0)
{
m_im |= IM_IE;
m_out_inte_func(1);
}
else if (state == 0 && (m_im & IM_IE) != 0)
{
m_im &= ~IM_IE;
m_out_inte_func(0);
}
}
void i8085a_cpu_device::set_status(u8 status)
{
if (!m_out_status_func.isunset() && status != m_status)
m_out_status_func(status);
m_status = status;
}
u8 i8085a_cpu_device::get_rim_value()
{
u8 result = m_im;
int sid = m_in_sid_func();
// copy live RST5.5 and RST6.5 states
result &= ~(IM_I65 | IM_I55);
if (m_irq_state[I8085_RST65_LINE]) result |= IM_I65;
if (m_irq_state[I8085_RST55_LINE]) result |= IM_I55;
// fetch the SID bit if we have a callback
result = (result & ~IM_SID) | (sid ? IM_SID : 0);
return result;
}
// memory access
u8 i8085a_cpu_device::read_arg()
{
set_status(0x82); // memory read
if (m_in_acknowledge)
return read_inta();
else
return m_cprogram.read_byte(m_PC.w.l++);
}
PAIR i8085a_cpu_device::read_arg16()
{
PAIR p;
set_status(0x82); // memory read
if (m_in_acknowledge)
{
p.b.l = read_inta();
p.b.h = read_inta();
}
else
{
p.b.l = m_cprogram.read_byte(m_PC.w.l++);
p.b.h = m_cprogram.read_byte(m_PC.w.l++);
}
return p;
}
u8 i8085a_cpu_device::read_op()
{
set_status(0xa2); // instruction fetch
return m_copcodes.read_byte(m_PC.w.l++);
}
u8 i8085a_cpu_device::read_inta()
{
if (m_in_inta_func.isunset())
return standard_irq_callback(I8085_INTR_LINE, m_PC.w.l);
else
return m_in_inta_func(m_PC.w.l);
}
u8 i8085a_cpu_device::read_mem(u32 a)
{
set_status(0x82); // memory read
return m_program.read_byte(a);
}
void i8085a_cpu_device::write_mem(u32 a, u8 v)
{
set_status(0x00); // memory write
m_program.write_byte(a, v);
}
void i8085a_cpu_device::op_push(PAIR p)
{
set_status(0x04); // stack push
m_program.write_byte(--m_SP.w.l, p.b.h);
m_program.write_byte(--m_SP.w.l, p.b.l);
}
PAIR i8085a_cpu_device::op_pop()
{
PAIR p;
set_status(0x86); // stack pop
p.b.l = m_program.read_byte(m_SP.w.l++);
p.b.h = m_program.read_byte(m_SP.w.l++);
return p;
}
// logical
void i8085a_cpu_device::op_ora(u8 v)
{
m_AF.b.h |= v;
m_AF.b.l = lut_zsp[m_AF.b.h];
}
void i8085a_cpu_device::op_xra(u8 v)
{
m_AF.b.h ^= v;
m_AF.b.l = lut_zsp[m_AF.b.h];
}
void i8085a_cpu_device::op_ana(u8 v)
{
u8 hc = ((m_AF.b.h | v) << 1) & HF;
m_AF.b.h &= v;
m_AF.b.l = lut_zsp[m_AF.b.h];
if (is_8085())
m_AF.b.l |= HF;
else
m_AF.b.l |= hc;
}
// increase / decrease
u8 i8085a_cpu_device::op_inr(u8 v)
{
u8 hc = ((v & 0x0f) == 0x0f) ? HF : 0;
m_AF.b.l = (m_AF.b.l & CF) | lut_zsp[++v] | hc;
return v;
}
u8 i8085a_cpu_device::op_dcr(u8 v)
{
u8 hc = ((v & 0x0f) != 0x00) ? HF : 0;
m_AF.b.l = (m_AF.b.l & CF) | lut_zsp[--v] | hc | VF;
return v;
}
// arithmetic
void i8085a_cpu_device::op_add(u8 v)
{
int q = m_AF.b.h + v;
m_AF.b.l = lut_zsp[q & 0xff] | ((q >> 8) & CF) | ((m_AF.b.h ^ q ^ v) & HF);
m_AF.b.h = q;
}
void i8085a_cpu_device::op_adc(u8 v)
{
int q = m_AF.b.h + v + (m_AF.b.l & CF);
m_AF.b.l = lut_zsp[q & 0xff] | ((q >> 8) & CF) | ((m_AF.b.h ^ q ^ v) & HF);
m_AF.b.h = q;
}
void i8085a_cpu_device::op_sub(u8 v)
{
int q = m_AF.b.h - v;
m_AF.b.l = lut_zsp[q & 0xff] | ((q >> 8) & CF) | (~(m_AF.b.h ^ q ^ v) & HF) | VF;
m_AF.b.h = q;
}
void i8085a_cpu_device::op_sbb(u8 v)
{
int q = m_AF.b.h - v - (m_AF.b.l & CF);
m_AF.b.l = lut_zsp[q & 0xff] | ((q >> 8) & CF) | (~(m_AF.b.h ^ q ^ v) & HF) | VF;
m_AF.b.h = q;
}
void i8085a_cpu_device::op_cmp(u8 v)
{
int q = m_AF.b.h - v;
m_AF.b.l = lut_zsp[q & 0xff] | ((q >> 8) & CF) | (~(m_AF.b.h ^ q ^ v) & HF) | VF;
}
void i8085a_cpu_device::op_dad(u16 v)
{
int q = m_HL.w.l + v;
m_AF.b.l = (m_AF.b.l & ~CF) | (q >> 16 & CF);
m_HL.w.l = q;
}
// jumps
void i8085a_cpu_device::op_jmp(int cond)
{
if (cond)
{
m_PC = read_arg16();
m_icount -= jmp_taken();
}
else
{
m_PC.w.l += 2;
}
}
void i8085a_cpu_device::op_call(int cond)
{
if (cond)
{
PAIR p = read_arg16();
m_icount -= call_taken();
op_push(m_PC);
m_PC = p;
}
else
{
m_PC.w.l += 2;
}
}
void i8085a_cpu_device::op_ret(int cond)
{
// conditional RET only
if (cond)
{
m_icount -= ret_taken();
m_PC = op_pop();
}
}
void i8085a_cpu_device::op_rst(u8 v)
{
op_push(m_PC);
m_PC.d = 8 * v;
}
/***************************************************************************
COMMON EXECUTION
***************************************************************************/
void i8085a_cpu_device::execute_run()
{
// check for TRAPs before diving in (can't do others because of after_ei)
if (m_trap_pending || m_after_ei == 0)
check_for_interrupts();
do
{
// the instruction after an EI does not take an interrupt, so
// we cannot check immediately; handle post-EI behavior here
if (m_after_ei != 0 && --m_after_ei == 0)
check_for_interrupts();
m_in_acknowledge = false;
debugger_instruction_hook(m_PC.d);
// here we go...
execute_one(read_op());
} while (m_icount > 0);
}
void i8085a_cpu_device::execute_one(int opcode)
{
m_icount -= lut_cycles[opcode];
switch (opcode)
{
case 0x00: // NOP
break;
case 0x01: // LXI B,nnnn
m_BC = read_arg16();
break;
case 0x02: // STAX B
write_mem(m_BC.d, m_AF.b.h);
break;
case 0x03: // INX B
m_BC.w.l++;
if (is_8085())
{
if (m_BC.w.l == 0x0000)
m_AF.b.l |= KF;
else
m_AF.b.l &= ~KF;
}
break;
case 0x04: // INR B
m_BC.b.h = op_inr(m_BC.b.h);
break;
case 0x05: // DCR B
m_BC.b.h = op_dcr(m_BC.b.h);
break;
case 0x06: // MVI B,nn
m_BC.b.h = read_arg();
break;
case 0x07: // RLC
m_AF.b.h = (m_AF.b.h << 1) | (m_AF.b.h >> 7);
m_AF.b.l = (m_AF.b.l & 0xfe) | (m_AF.b.h & CF);
break;
case 0x08: // 8085: undocumented DSUB (Double Subtraction, HL - BC)
if (is_8085())
{
// Low byte subtraction: L = L - C
int q_low = m_HL.b.l - m_BC.b.l;
u8 res_low = q_low & 0xff;
// Calculate flags for low byte
m_AF.b.l = lut_zs[res_low]
| ((q_low >> 8) & CF) // Carry
| ((m_HL.b.l ^ res_low ^ m_BC.b.l) & HF) // Half Carry
| (((m_BC.b.l ^ m_HL.b.l) & (m_HL.b.l ^ res_low) & SF)) >> 5; // Overflow
m_HL.b.l = res_low;
// High byte subtraction: H = H - B - carry_from_low
int q_high = m_HL.b.h - m_BC.b.h - (m_AF.b.l & CF);
u8 res_high = q_high & 0xff;
// Calculate flags for high byte
m_AF.b.l = lut_zs[res_high]
| ((q_high >> 8) & CF) // Carry
| ((m_HL.b.h ^ res_high ^ m_BC.b.h) & HF) // Half Carry
| (((m_BC.b.h ^ m_HL.b.h) & (m_HL.b.h ^ res_high) & SF)) >> 5; // Overflow
m_HL.b.h = res_high;
// Set Zero flag based on 16-bit result
m_AF.b.l = (m_AF.b.l & ~ZF) | (((m_HL.b.l | m_HL.b.h) == 0) ? ZF : 0);
}
break;
case 0x09: // DAD B
op_dad(m_BC.w.l);
break;
case 0x0a: // LDAX B
m_AF.b.h = read_mem(m_BC.d);
break;
case 0x0b: // DCX B
m_BC.w.l--;
if (is_8085())
{
if (m_BC.w.l == 0xffff)
m_AF.b.l |= KF;
else
m_AF.b.l &= ~KF;
}
break;
case 0x0c: // INR C
m_BC.b.l = op_inr(m_BC.b.l);
break;
case 0x0d: // DCR C
m_BC.b.l = op_dcr(m_BC.b.l);
break;
case 0x0e: // MVI C,nn
m_BC.b.l = read_arg();
break;
case 0x0f: // RRC
m_AF.b.l = (m_AF.b.l & 0xfe) | (m_AF.b.h & CF);
m_AF.b.h = (m_AF.b.h >> 1) | (m_AF.b.h << 7);
break;
case 0x10: // 8085: undocumented ARHL, otherwise undocumented NOP
if (is_8085())
{
m_AF.b.l = (m_AF.b.l & ~CF) | (m_HL.b.l & CF);
m_HL.w.l = (m_HL.w.l & 0x8000) | (m_HL.w.l >> 1);
}
break;
case 0x11: // LXI D,nnnn
m_DE = read_arg16();
break;
case 0x12: // STAX D
write_mem(m_DE.d, m_AF.b.h);
break;
case 0x13: // INX D