summaryrefslogtreecommitdiff
path: root/z80.cpu
blob: 43424cedd759be6debcad52d78c2bc3c509f646b (plain)
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
info
	prefix z80_
	opcode_size 8
	extra_tables cb ed dded fded dd fd
	body z80_run_op
	include z80_util.c
	header z80.h
	
regs
	main 8 b c d e h l f a
	alt 8 b' c' d' e' h' l' f' a'
	i 8
	r 8
	iff1 8
	iff2 8
	imode 8
	sp 16
	ix 16
	iy 16
	pc 16
	wz 16
	nflag 8
	last_flag_result 8
	pvflag 8
	chflags 8
	zflag 8
	scratch1 16
	scratch2 16
	
flags
	register f
	S 7 sign last_flag_result.7
	Z 6 zero zflag
	Y 5 bit-5 last_flag_result.5
	H 4 half-carry chflags.3
	P 2 parity pvflag
	V 2 overflow pvflag
	X 3 bit-3 last_flag_result.3
	N 1 none nflag
	C 0 carry chflags.7

	
z80_op_fetch
	cycles 1
	add 1 r r
	mov pc scratch1
	ocall read_8
	add 1 pc pc
	
z80_run_op
	z80_op_fetch
	dispatch scratch1
	
11001011 cb_prefix
	z80_op_fetch
	dispatch scratch1 cb

11011101 dd_prefix
	z80_op_fetch
	dispatch scratch1 dd

11011101 ed_prefix
	z80_op_fetch
	dispatch scratch1 ed

11111101 fd_prefix
	z80_op_fetch
	dispatch scratch1 fd
	
z80_check_cond
	arg cond 8
	local invert 8
	switch cond
	case 0
	meta istrue invert
	lnot zflag invert
	
	case 1
	meta istrue zflag
	
	case 2
	meta istrue invert
	not chflags invert
	and 0x80 invert invert
	
	case 3
	meta istrue invert
	and 0x80 invert invert
	
	case 4
	meta istrue invert
	lnot pvflag invert
	
	case 5
	meta istrue pvflag
	
	case 6
	meta istrue invert
	not last_flag_result invert
	and 0x80 invert invert
	
	case 7
	meta istrue invert
	and 0x80 last_flag_result invert
	
	end
	
z80_fetch_hl
	lsl h 8 scratch1
	or l scratch1 scratch1
	ocall read_8
	
z80_store_hl
	lsl h 8 scratch2
	or l scratch2 scratch2
	ocall write_8

z80_fetch_immed
	mov pc scratch1
	ocall read_8
	add 1 pc pc
	
z80_fetch_immed16
	mov pc scratch1
	ocall read_8
	mov scratch1 wz
	add 1 pc pc
	mov pc scratch1
	ocall read_8
	add 1 pc pc
	lsl scratch1 8 scratch1
	or scratch1 wz wz

z80_fetch_immed_reg16
	mov pc scratch1
	ocall read_8
	mov scratch1 low
	add 1 pc pc
	mov pc scratch1
	ocall read_8
	mov scratch1 high
	add 1 pc pc
	
z80_fetch_immed_to_reg16
	mov pc scratch1
	ocall read_8
	mov scratch1 reg
	add 1 pc pc
	mov pc scratch1
	ocall read_8
	add 1 pc pc
	lsl scratch1 8 scratch1
	or scratch1 reg reg
	
z80_calc_index
	z80_fetch_immed
	add scratch1 index wz
	cycles 5
	
z80_fetch_index
	z80_calc_index
	mov wz scratch1
	ocall read_8

z80_fetch_ix
	meta index ix
	z80_fetch_index

z80_fetch_iy
	meta index iy
	z80_fetch_index

01RRR110 ld_from_hl
	z80_fetch_hl
	mov scratch1 main.R

01DDDSSS ld_from_reg
	mov main.S main.D
	
dd 01RRR110 ld_from_ix
	z80_fetch_ix
	mov scratch1 main.R

fd 01RRR110 ld_from_iy
	z80_fetch_iy
	mov scratch1 main.R

00RRR110 ld_immed
	z80_fetch_immed
	mov scratch1 main.R

01110RRR ld_to_hl
	mov main.R scratch1
	z80_store_hl

dd 01110RRR ld_to_ix
	meta index ix
	z80_calc_index
	mov wz scratch2
	mov main.R scratch1
	ocall write_8

fd 01110RRR ld_to_iy
	meta index iy
	z80_calc_index
	mov wz scratch2
	mov main.R scratch1
	ocall write_8

00110110 ld_to_hl_immed
	z80_fetch_immed
	z80_store_hl

00001010 ld_a_from_bc
	lsl b 8 scratch1
	or c scratch2 scratch1
	ocall read_8
	mov scratch1 a

00011010 ld_a_from_de
	lsl d 8 scratch1
	or e scratch2 scratch1
	ocall write_8
	mov scratch1 a

00111010 ld_a_from_immed
	z80_fetch_immed16
	mov wz scratch1
	ocall read_8
	mov scratch1 a
	
00000010 ld_a_to_bc
	lsl b 8 scratch2
	or c scratch2 scratch2
	mov a scratch1
	ocall write_8
	
00010010 ld_a_to_de
	lsl d 8 scratch2
	or e scratch2 scratch2
	mov a scratch1
	ocall write_8

00110010 ld_a_to_immed
	z80_fetch_immed16
	mov wz scratch2
	mov a scratch1
	ocall write_8

ed 01000111 ld_i_a
	mov a i
	cycles 1

ed 01001111 ld_r_a
	mov a r
	cycles 1

00000001 ld_bc_immed
	meta high b
	meta low c
	z80_fetch_immed_reg16

00010001 ld_de_immed
	meta high d
	meta low e
	z80_fetch_immed_reg16

00100001 ld_hl_immed
	meta high h
	meta low l
	z80_fetch_immed_reg16

00110001 ld_sp_immed
	meta reg sp
	z80_fetch_immed_to_reg16

dd 00100001 ld_ix_immed
	meta reg ix
	z80_fetch_immed_to_reg16

fd 00100001 ld_iy_immed
	meta reg iy
	z80_fetch_immed_to_reg16
	
z80_fetch16_from_immed
	z80_fetch_immed16
	mov wz scratch1
	ocall read_8
	mov scratch1 low
	add 1 wz wz
	mov wz scratch1
	ocall read_8
	mov scratch1 high

00101010 ld_hl_from_immed
	meta low l
	meta high h
	z80_fetch16_from_immed

ed 01001011 ld_bc_from_immed
	meta low c
	meta high b
	z80_fetch16_from_immed

ed 01011011 ld_de_from_immed
	meta low e
	meta high c
	z80_fetch16_from_immed

ed 01101011 ld_hl_from_immed_slow
	meta low l
	meta high h
	z80_fetch16_from_immed
	
z80_fetch_reg16_from_immed
	z80_fetch_immed16
	mov wz scratch1
	ocall read_8
	mov scratch1 reg
	add 1 wz wz
	mov wz scratch1
	ocall read_8
	lsl scratch1 8 scratch1
	or scratch1 reg reg

ed 01111011 ld_sp_from_immed
	meta reg sp
	z80_fetch_reg16_from_immed

dd 00101010 ld_ix_from_immed
	meta reg ix
	z80_fetch_reg16_from_immed

fd 00101010 ld_iy_from_immed
	meta reg iy
	z80_fetch_reg16_from_immed

00100010 ld_hl_to_immed
	z80_fetch_immed16
	mov wz scratch2
	mov l scratch1
	ocall write_8
	add 1 wz wz
	mov wz scratch2
	mov h scratch1
	ocall write_8
	
z80_regpair_to_immed
	z80_fetch_immed16
	mov wz scratch2
	mov low scratch1
	ocall write_8
	add 1 wz wz
	mov high scratch1
	mov wz scratch2
	ocall write_8
	
ed 01000011 ld_bc_to_immed
	meta low c
	meta high b
	z80_regpair_to_immed

ed 01010011 ld_de_to_immed
	meta low e
	meta high d
	z80_regpair_to_immed
	
ed 01100011 ld_hl_to_immed_slow
	meta low l
	meta high h
	z80_regpair_to_immed
	
ed 01110011 ld_sp_to_immed
	meta low sp
	local sph 8
	lsr sp 8 sph
	meta high sph
	z80_regpair_to_immed

11111001 ld_sp_hl
	cycles 2
	lsl h 8 sp
	or l sp sp
	mov wz scratch2
	mov sp scratch1
	ocall write_8
	add 1 wz wz
	lsr sp 8 scratch1
	mov wz scratch2
	ocall write_8

z80_push
	cycles 1
	sub 1 sp sp
	mov sp scratch2
	mov high scratch1
	ocall write_8
	sub 1 sp sp
	mov sp scratch2
	mov low scratch1
	ocall write_8

11000101 push_bc
	meta high b
	meta low c
	z80_push

11010101 push_de
	meta high d
	meta low e
	z80_push

11100101 push_hl
	meta high h
	meta low l
	z80_push

11110101 push_af
	meta high a
	meta low f
	z80_push
	
dd 11100101 push_ix
	local ixh 8
	lsr ix 8 ixh
	meta high ixh
	meta low ix
	z80_push

fd 11100101 push_iy
	local iyh 8
	lsr iy 8 iyh
	meta high iyh
	meta low iy
	z80_push

z80_pop
	mov sp scratch1
	ocall read_8
	add 1 sp sp
	mov scratch1 low
	mov sp scratch1
	ocall read_8
	add 1 sp sp
	mov scratch1 high

11000001 pop_bc
	meta high b
	meta low c
	z80_pop

11010001 pop_de
	meta high d
	meta low e
	z80_pop

11100001 pop_hl
	meta high h
	meta low l
	z80_pop

11110001 pop_af
	meta high a
	meta low f
	z80_pop

dd 11100001 pop_ix
	local ixh 16
	meta high ixh
	meta low ix
	z80_pop
	lsl ixh 8 ixh
	or ixh ix ix

fd 11100001 pop_iy
	local iyh 16
	meta high iyh
	meta low iy
	z80_pop
	lsl iyh 8 iyh
	or iyh iy iy

11101011 ex_de_hl
	xchg e l
	xchg d h

00001000 ex_af_af
	xchg a a'
	xchg f f'

11011001 exx
	xchg b b'
	xchg c c'
	xchg d d'
	xchg e e'
	xchg h h'
	xchg l l'

11100011 ex_sp_hl
	mov sp scratch1
	ocall read_8
	xchg l scratch1
	cycles 1
	mov sp scratch2
	ocall write_8
	add 1 sp wz
	mov wz scratch2
	ocall read_8
	xchg h scratch1
	cycles 2
	mov wz scratch2
	ocall write_8

10000RRR add_reg
	add a main.R a
	update_flags SZYHVXN0C
	
10000110 add_hl
	z80_fetch_hl
	add a scratch1 a
	update_flags SZYHVXN0C

11000110 add_immed
	z80_fetch_immed
	add a scratch1 a
	update_flags SZYHVXN0C
	
10001RRR adc_reg
	adc a main.R a
	update_flags SZYHVXN0C

10001110 adc_hl
	z80_fetch_hl
	adc a scratch1 a
	update_flags SZYHVXN0C

11001110 adc_immed
	z80_fetch_immed
	adc a scratch1 a
	update_flags SZYHVXN0C

10010RRR sub_reg
	sub main.R a a
	update_flags SZYHVXN1C
	
10010110 sub_hl
	z80_fetch_hl
	sub scratch1 a a
	update_flags SZYHVXN1C

11010110 sub_immed
	z80_fetch_immed
	sub scratch1 a a
	update_flags SZYHVXN1C

10011RRR sbc_reg
	sbc main.R a a
	update_flags SZYHVXN1C
	
10011110 sbc_hl
	z80_fetch_hl
	sbc scratch1 a a
	update_flags SZYHVXN1C

11011110 sbc_immed
	z80_fetch_immed
	sbc scratch1 a a
	update_flags SZYHVXN1C

10100RRR and_reg
	and a main.R a
	update_flags SZYH1PXN0C0
	
10100110 and_hl
	z80_fetch_hl
	and a scratch1 a
	update_flags SZYH1PXN0C0

11100110 and_immed
	z80_fetch_immed
	and a scratch1 a
	update_flags SZYH1PXN0C0
	
10110RRR or_reg
	or a main.R a
	update_flags SZYH1PXN0C0
	
10110110 or_hl
	z80_fetch_hl
	or a scratch1 a
	update_flags SZYH1PXN0C0

11110110 or_immed
	z80_fetch_immed
	or a scratch1 a
	update_flags SZYH1PXN0C0
	
10101RRR xor_reg
	xor a main.R a
	update_flags SZYH1PXN0C0
	
10101110 xor_hl
	z80_fetch_hl
	xor a scratch1 a
	update_flags SZYH1PXN0C0

11101110 xor_immed
	z80_fetch_immed
	xor a scratch1 a
	update_flags SZYH1PXN0C0

10111RRR cp_reg
	cmp main.R a
	update_flags SZYHVXN1C
	
10111110 cp_hl
	z80_fetch_hl
	cmp scratch1 a
	update_flags SZYHVXN1C

11111110 cp_immed
	z80_fetch_immed
	cmp scratch1 a
	update_flags SZYHVXN1C

00RRR100 inc_reg
	add 1 main.R main.R
	update_flags SZYHVXN0
	
00110100 inc_hl
	z80_fetch_hl
	#TODO: fix size
	add 1 scratch1 scratch1
	update_flags SZYHVXN0
	z80_store_hl

00RRR101 dec_reg
	add 1 main.R main.R
	update_flags SZYHVXN0
	
00110101 dec_hl
	z80_fetch_hl
	#TODO: fix size
	add 1 scratch1 scratch1
	update_flags SZYHVXN0
	z80_store_hl

00101111 cpl
	not a a
	update_flags YH1XN1

11101101 neg
	neg a a
	update_flags SZYHVXN1C

00111111 ccf
	xor 0x80 chflags chflags

00111111 scf
	or 0x80 chflags chflags

00000000 nop

11110011 di
	mov 0 iff1
	mov 0 iff2
	#TODO: update interrupt/sync cycle

11111011 ei
	mov 1 iff1
	mov 1 iff2
	#TODO: update interrupt/sync cycle

ed 01000110 im0
	mov 0 imode

ed 01010110 im1
	mov 1 imode

ed 01011110 im2
	mov 2 imode
	
11000011 jp
	z80_fetch_immed16
	mov wz pc
	
11CCC010 jp_cc
	z80_check_cond C
	z80_fetch_immed16
	if istrue
	
	mov wz pc
	
	end
	
00011000 jr
	z80_fetch_immed
	#TODO: determine if this updates wz
	sext 16 scratch1 scratch1
	add scratch1 pc pc
	cycles 5
	
001CC000 jr_cc
	z80_check_cond C
	z80_fetch_immed
	
	if istrue
	
	sext 16 scratch1 scratch1
	add scratch1 pc pc
	cycles 5
	
	end
	
00010000 djnz
	cycles 1
	z80_fetch_immed
	sub 1 b b
	
	if b
	
	sext 16 scratch1 scratch1
	add scratch1 pc pc
	cycles 5
	
	end
	

11001101 call_uncond
	z80_fetch_immed16
	local pch 8
	lsr pc 8 pch
	meta high pch
	meta low pc
	z80_push
	mov wz pc
	
11TTT111 rst
	local pch 8
	lsr pc 8 pch
	meta high pch
	meta low pc
	z80_push
	lsl T 3 scratch1
	mov scratch1 pc

11001001 ret
	#TODO: confirm this goes through wz
	local wzh 16
	meta high wzh
	meta low wz
	z80_pop
	lsl wzh 8 wzh
	or wzh wz wz
	mov wz pc

11011011 in_abs
	z80_fetch_immed
	ocall io_read8
	mov scratch1 a
	
ed 01RRR000 in_bc
	lsl b 8 scratch1
	or c scratch1 scratch1
	ocall io_read8
	mov scratch1 main.R
	
11010011 out_abs
	z80_fetch_immed
	mov scratch1 scratch2
	mov a scratch1
	ocall io_write8
	
ed 01RRR001 out_bc
	lsl b 8 scratch2
	or c scratch2 scratch2
	mov main.R scratch1
	ocall io_write8