Mercurial > repos > blastem
comparison z80.cpu @ 1706:c2324849a5e5
Initial checkin of new WIP Z80 core using CPU DSL
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 28 Jan 2019 21:16:41 -0800 |
parents | |
children | 0a9a88b3d061 |
comparison
equal
deleted
inserted
replaced
1705:9ab64ef5cba0 | 1706:c2324849a5e5 |
---|---|
1 info | |
2 prefix z80_ | |
3 opcode_size 8 | |
4 extra_tables cb ed dded fded dd fd | |
5 body z80_run_op | |
6 include z80_util.c | |
7 header z80.h | |
8 | |
9 regs | |
10 main 8 b c d e h l f a | |
11 alt 8 b' c' d' e' h' l' f' a' | |
12 i 8 | |
13 r 8 | |
14 iff1 8 | |
15 iff2 8 | |
16 imode 8 | |
17 sp 16 | |
18 ix 16 | |
19 iy 16 | |
20 pc 16 | |
21 wz 16 | |
22 nflag 8 | |
23 last_flag_result 8 | |
24 pvflag 8 | |
25 chflags 8 | |
26 zflag 8 | |
27 scratch1 16 | |
28 scratch2 16 | |
29 | |
30 flags | |
31 register f | |
32 S 7 sign last_flag_result.7 | |
33 Z 6 zero zflag | |
34 Y 5 bit-5 last_flag_result.5 | |
35 H 4 half-carry chflags.3 | |
36 P 2 parity pvflag | |
37 V 2 overflow pvflag | |
38 X 3 bit-3 last_flag_result.3 | |
39 N 1 none nflag | |
40 C 0 carry chflags.7 | |
41 | |
42 | |
43 z80_op_fetch | |
44 cycles 1 | |
45 add 1 r r | |
46 mov pc scratch1 | |
47 ocall read_8 | |
48 add 1 pc pc | |
49 | |
50 z80_run_op | |
51 z80_op_fetch | |
52 dispatch scratch1 | |
53 | |
54 11001011 cb_prefix | |
55 z80_op_fetch | |
56 dispatch scratch1 cb | |
57 | |
58 11011101 dd_prefix | |
59 z80_op_fetch | |
60 dispatch scratch1 dd | |
61 | |
62 11011101 ed_prefix | |
63 z80_op_fetch | |
64 dispatch scratch1 ed | |
65 | |
66 11111101 fd_prefix | |
67 z80_op_fetch | |
68 dispatch scratch1 fd | |
69 | |
70 z80_check_cond | |
71 arg cond 8 | |
72 local invert 8 | |
73 switch cond | |
74 case 0 | |
75 meta istrue invert | |
76 lnot zflag invert | |
77 | |
78 case 1 | |
79 meta istrue zflag | |
80 | |
81 case 2 | |
82 meta istrue invert | |
83 not chflags invert | |
84 and 0x80 invert invert | |
85 | |
86 case 3 | |
87 meta istrue invert | |
88 and 0x80 invert invert | |
89 | |
90 case 4 | |
91 meta istrue invert | |
92 lnot pvflag invert | |
93 | |
94 case 5 | |
95 meta istrue pvflag | |
96 | |
97 case 6 | |
98 meta istrue invert | |
99 not last_flag_result invert | |
100 and 0x80 invert invert | |
101 | |
102 case 7 | |
103 meta istrue invert | |
104 and 0x80 last_flag_result invert | |
105 | |
106 end | |
107 | |
108 z80_fetch_hl | |
109 lsl h 8 scratch1 | |
110 or l scratch1 scratch1 | |
111 ocall read_8 | |
112 | |
113 z80_store_hl | |
114 lsl h 8 scratch2 | |
115 or l scratch2 scratch2 | |
116 ocall write_8 | |
117 | |
118 z80_fetch_immed | |
119 mov pc scratch1 | |
120 ocall read_8 | |
121 add 1 pc pc | |
122 | |
123 z80_fetch_immed16 | |
124 mov pc scratch1 | |
125 ocall read_8 | |
126 mov scratch1 wz | |
127 add 1 pc pc | |
128 mov pc scratch1 | |
129 ocall read_8 | |
130 add 1 pc pc | |
131 lsl scratch1 8 scratch1 | |
132 or scratch1 wz wz | |
133 | |
134 z80_fetch_immed_reg16 | |
135 mov pc scratch1 | |
136 ocall read_8 | |
137 mov scratch1 low | |
138 add 1 pc pc | |
139 mov pc scratch1 | |
140 ocall read_8 | |
141 mov scratch1 high | |
142 add 1 pc pc | |
143 | |
144 z80_fetch_immed_to_reg16 | |
145 mov pc scratch1 | |
146 ocall read_8 | |
147 mov scratch1 reg | |
148 add 1 pc pc | |
149 mov pc scratch1 | |
150 ocall read_8 | |
151 add 1 pc pc | |
152 lsl scratch1 8 scratch1 | |
153 or scratch1 reg reg | |
154 | |
155 z80_calc_index | |
156 z80_fetch_immed | |
157 add scratch1 index wz | |
158 cycles 5 | |
159 | |
160 z80_fetch_index | |
161 z80_calc_index | |
162 mov wz scratch1 | |
163 ocall read_8 | |
164 | |
165 z80_fetch_ix | |
166 meta index ix | |
167 z80_fetch_index | |
168 | |
169 z80_fetch_iy | |
170 meta index iy | |
171 z80_fetch_index | |
172 | |
173 01RRR110 ld_from_hl | |
174 z80_fetch_hl | |
175 mov scratch1 main.R | |
176 | |
177 01DDDSSS ld_from_reg | |
178 mov main.S main.D | |
179 | |
180 dd 01RRR110 ld_from_ix | |
181 z80_fetch_ix | |
182 mov scratch1 main.R | |
183 | |
184 fd 01RRR110 ld_from_iy | |
185 z80_fetch_iy | |
186 mov scratch1 main.R | |
187 | |
188 00RRR110 ld_immed | |
189 z80_fetch_immed | |
190 mov scratch1 main.R | |
191 | |
192 01110RRR ld_to_hl | |
193 mov main.R scratch1 | |
194 z80_store_hl | |
195 | |
196 dd 01110RRR ld_to_ix | |
197 meta index ix | |
198 z80_calc_index | |
199 mov wz scratch2 | |
200 mov main.R scratch1 | |
201 ocall write_8 | |
202 | |
203 fd 01110RRR ld_to_iy | |
204 meta index iy | |
205 z80_calc_index | |
206 mov wz scratch2 | |
207 mov main.R scratch1 | |
208 ocall write_8 | |
209 | |
210 00110110 ld_to_hl_immed | |
211 z80_fetch_immed | |
212 z80_store_hl | |
213 | |
214 00001010 ld_a_from_bc | |
215 lsl b 8 scratch1 | |
216 or c scratch2 scratch1 | |
217 ocall read_8 | |
218 mov scratch1 a | |
219 | |
220 00011010 ld_a_from_de | |
221 lsl d 8 scratch1 | |
222 or e scratch2 scratch1 | |
223 ocall write_8 | |
224 mov scratch1 a | |
225 | |
226 00111010 ld_a_from_immed | |
227 z80_fetch_immed16 | |
228 mov wz scratch1 | |
229 ocall read_8 | |
230 mov scratch1 a | |
231 | |
232 00000010 ld_a_to_bc | |
233 lsl b 8 scratch2 | |
234 or c scratch2 scratch2 | |
235 mov a scratch1 | |
236 ocall write_8 | |
237 | |
238 00010010 ld_a_to_de | |
239 lsl d 8 scratch2 | |
240 or e scratch2 scratch2 | |
241 mov a scratch1 | |
242 ocall write_8 | |
243 | |
244 00110010 ld_a_to_immed | |
245 z80_fetch_immed16 | |
246 mov wz scratch2 | |
247 mov a scratch1 | |
248 ocall write_8 | |
249 | |
250 ed 01000111 ld_i_a | |
251 mov a i | |
252 cycles 1 | |
253 | |
254 ed 01001111 ld_r_a | |
255 mov a r | |
256 cycles 1 | |
257 | |
258 00000001 ld_bc_immed | |
259 meta high b | |
260 meta low c | |
261 z80_fetch_immed_reg16 | |
262 | |
263 00010001 ld_de_immed | |
264 meta high d | |
265 meta low e | |
266 z80_fetch_immed_reg16 | |
267 | |
268 00100001 ld_hl_immed | |
269 meta high h | |
270 meta low l | |
271 z80_fetch_immed_reg16 | |
272 | |
273 00110001 ld_sp_immed | |
274 meta reg sp | |
275 z80_fetch_immed_to_reg16 | |
276 | |
277 dd 00100001 ld_ix_immed | |
278 meta reg ix | |
279 z80_fetch_immed_to_reg16 | |
280 | |
281 fd 00100001 ld_iy_immed | |
282 meta reg iy | |
283 z80_fetch_immed_to_reg16 | |
284 | |
285 z80_fetch16_from_immed | |
286 z80_fetch_immed16 | |
287 mov wz scratch1 | |
288 ocall read_8 | |
289 mov scratch1 low | |
290 add 1 wz wz | |
291 mov wz scratch1 | |
292 ocall read_8 | |
293 mov scratch1 high | |
294 | |
295 00101010 ld_hl_from_immed | |
296 meta low l | |
297 meta high h | |
298 z80_fetch16_from_immed | |
299 | |
300 ed 01001011 ld_bc_from_immed | |
301 meta low c | |
302 meta high b | |
303 z80_fetch16_from_immed | |
304 | |
305 ed 01011011 ld_de_from_immed | |
306 meta low e | |
307 meta high c | |
308 z80_fetch16_from_immed | |
309 | |
310 ed 01101011 ld_hl_from_immed_slow | |
311 meta low l | |
312 meta high h | |
313 z80_fetch16_from_immed | |
314 | |
315 z80_fetch_reg16_from_immed | |
316 z80_fetch_immed16 | |
317 mov wz scratch1 | |
318 ocall read_8 | |
319 mov scratch1 reg | |
320 add 1 wz wz | |
321 mov wz scratch1 | |
322 ocall read_8 | |
323 lsl scratch1 8 scratch1 | |
324 or scratch1 reg reg | |
325 | |
326 ed 01111011 ld_sp_from_immed | |
327 meta reg sp | |
328 z80_fetch_reg16_from_immed | |
329 | |
330 dd 00101010 ld_ix_from_immed | |
331 meta reg ix | |
332 z80_fetch_reg16_from_immed | |
333 | |
334 fd 00101010 ld_iy_from_immed | |
335 meta reg iy | |
336 z80_fetch_reg16_from_immed | |
337 | |
338 00100010 ld_hl_to_immed | |
339 z80_fetch_immed16 | |
340 mov wz scratch2 | |
341 mov l scratch1 | |
342 ocall write_8 | |
343 add 1 wz wz | |
344 mov wz scratch2 | |
345 mov h scratch1 | |
346 ocall write_8 | |
347 | |
348 z80_regpair_to_immed | |
349 z80_fetch_immed16 | |
350 mov wz scratch2 | |
351 mov low scratch1 | |
352 ocall write_8 | |
353 add 1 wz wz | |
354 mov high scratch1 | |
355 mov wz scratch2 | |
356 ocall write_8 | |
357 | |
358 ed 01000011 ld_bc_to_immed | |
359 meta low c | |
360 meta high b | |
361 z80_regpair_to_immed | |
362 | |
363 ed 01010011 ld_de_to_immed | |
364 meta low e | |
365 meta high d | |
366 z80_regpair_to_immed | |
367 | |
368 ed 01100011 ld_hl_to_immed_slow | |
369 meta low l | |
370 meta high h | |
371 z80_regpair_to_immed | |
372 | |
373 ed 01110011 ld_sp_to_immed | |
374 meta low sp | |
375 local sph 8 | |
376 lsr sp 8 sph | |
377 meta high sph | |
378 z80_regpair_to_immed | |
379 | |
380 11111001 ld_sp_hl | |
381 cycles 2 | |
382 lsl h 8 sp | |
383 or l sp sp | |
384 mov wz scratch2 | |
385 mov sp scratch1 | |
386 ocall write_8 | |
387 add 1 wz wz | |
388 lsr sp 8 scratch1 | |
389 mov wz scratch2 | |
390 ocall write_8 | |
391 | |
392 z80_push | |
393 cycles 1 | |
394 sub 1 sp sp | |
395 mov sp scratch2 | |
396 mov high scratch1 | |
397 ocall write_8 | |
398 sub 1 sp sp | |
399 mov sp scratch2 | |
400 mov low scratch1 | |
401 ocall write_8 | |
402 | |
403 11000101 push_bc | |
404 meta high b | |
405 meta low c | |
406 z80_push | |
407 | |
408 11010101 push_de | |
409 meta high d | |
410 meta low e | |
411 z80_push | |
412 | |
413 11100101 push_hl | |
414 meta high h | |
415 meta low l | |
416 z80_push | |
417 | |
418 11110101 push_af | |
419 meta high a | |
420 meta low f | |
421 z80_push | |
422 | |
423 dd 11100101 push_ix | |
424 local ixh 8 | |
425 lsr ix 8 ixh | |
426 meta high ixh | |
427 meta low ix | |
428 z80_push | |
429 | |
430 fd 11100101 push_iy | |
431 local iyh 8 | |
432 lsr iy 8 iyh | |
433 meta high iyh | |
434 meta low iy | |
435 z80_push | |
436 | |
437 z80_pop | |
438 mov sp scratch1 | |
439 ocall read_8 | |
440 add 1 sp sp | |
441 mov scratch1 low | |
442 mov sp scratch1 | |
443 ocall read_8 | |
444 add 1 sp sp | |
445 mov scratch1 high | |
446 | |
447 11000001 pop_bc | |
448 meta high b | |
449 meta low c | |
450 z80_pop | |
451 | |
452 11010001 pop_de | |
453 meta high d | |
454 meta low e | |
455 z80_pop | |
456 | |
457 11100001 pop_hl | |
458 meta high h | |
459 meta low l | |
460 z80_pop | |
461 | |
462 11110001 pop_af | |
463 meta high a | |
464 meta low f | |
465 z80_pop | |
466 | |
467 dd 11100001 pop_ix | |
468 local ixh 16 | |
469 meta high ixh | |
470 meta low ix | |
471 z80_pop | |
472 lsl ixh 8 ixh | |
473 or ixh ix ix | |
474 | |
475 fd 11100001 pop_iy | |
476 local iyh 16 | |
477 meta high iyh | |
478 meta low iy | |
479 z80_pop | |
480 lsl iyh 8 iyh | |
481 or iyh iy iy | |
482 | |
483 11101011 ex_de_hl | |
484 xchg e l | |
485 xchg d h | |
486 | |
487 00001000 ex_af_af | |
488 xchg a a' | |
489 xchg f f' | |
490 | |
491 11011001 exx | |
492 xchg b b' | |
493 xchg c c' | |
494 xchg d d' | |
495 xchg e e' | |
496 xchg h h' | |
497 xchg l l' | |
498 | |
499 11100011 ex_sp_hl | |
500 mov sp scratch1 | |
501 ocall read_8 | |
502 xchg l scratch1 | |
503 cycles 1 | |
504 mov sp scratch2 | |
505 ocall write_8 | |
506 add 1 sp wz | |
507 mov wz scratch2 | |
508 ocall read_8 | |
509 xchg h scratch1 | |
510 cycles 2 | |
511 mov wz scratch2 | |
512 ocall write_8 | |
513 | |
514 10000RRR add_reg | |
515 add a main.R a | |
516 update_flags SZYHVXN0C | |
517 | |
518 10000110 add_hl | |
519 z80_fetch_hl | |
520 add a scratch1 a | |
521 update_flags SZYHVXN0C | |
522 | |
523 11000110 add_immed | |
524 z80_fetch_immed | |
525 add a scratch1 a | |
526 update_flags SZYHVXN0C | |
527 | |
528 10001RRR adc_reg | |
529 adc a main.R a | |
530 update_flags SZYHVXN0C | |
531 | |
532 10001110 adc_hl | |
533 z80_fetch_hl | |
534 adc a scratch1 a | |
535 update_flags SZYHVXN0C | |
536 | |
537 11001110 adc_immed | |
538 z80_fetch_immed | |
539 adc a scratch1 a | |
540 update_flags SZYHVXN0C | |
541 | |
542 10010RRR sub_reg | |
543 sub main.R a a | |
544 update_flags SZYHVXN1C | |
545 | |
546 10010110 sub_hl | |
547 z80_fetch_hl | |
548 sub scratch1 a a | |
549 update_flags SZYHVXN1C | |
550 | |
551 11010110 sub_immed | |
552 z80_fetch_immed | |
553 sub scratch1 a a | |
554 update_flags SZYHVXN1C | |
555 | |
556 10011RRR sbc_reg | |
557 sbc main.R a a | |
558 update_flags SZYHVXN1C | |
559 | |
560 10011110 sbc_hl | |
561 z80_fetch_hl | |
562 sbc scratch1 a a | |
563 update_flags SZYHVXN1C | |
564 | |
565 11011110 sbc_immed | |
566 z80_fetch_immed | |
567 sbc scratch1 a a | |
568 update_flags SZYHVXN1C | |
569 | |
570 10100RRR and_reg | |
571 and a main.R a | |
572 update_flags SZYH1PXN0C0 | |
573 | |
574 10100110 and_hl | |
575 z80_fetch_hl | |
576 and a scratch1 a | |
577 update_flags SZYH1PXN0C0 | |
578 | |
579 11100110 and_immed | |
580 z80_fetch_immed | |
581 and a scratch1 a | |
582 update_flags SZYH1PXN0C0 | |
583 | |
584 10110RRR or_reg | |
585 or a main.R a | |
586 update_flags SZYH1PXN0C0 | |
587 | |
588 10110110 or_hl | |
589 z80_fetch_hl | |
590 or a scratch1 a | |
591 update_flags SZYH1PXN0C0 | |
592 | |
593 11110110 or_immed | |
594 z80_fetch_immed | |
595 or a scratch1 a | |
596 update_flags SZYH1PXN0C0 | |
597 | |
598 10101RRR xor_reg | |
599 xor a main.R a | |
600 update_flags SZYH1PXN0C0 | |
601 | |
602 10101110 xor_hl | |
603 z80_fetch_hl | |
604 xor a scratch1 a | |
605 update_flags SZYH1PXN0C0 | |
606 | |
607 11101110 xor_immed | |
608 z80_fetch_immed | |
609 xor a scratch1 a | |
610 update_flags SZYH1PXN0C0 | |
611 | |
612 10111RRR cp_reg | |
613 cmp main.R a | |
614 update_flags SZYHVXN1C | |
615 | |
616 10111110 cp_hl | |
617 z80_fetch_hl | |
618 cmp scratch1 a | |
619 update_flags SZYHVXN1C | |
620 | |
621 11111110 cp_immed | |
622 z80_fetch_immed | |
623 cmp scratch1 a | |
624 update_flags SZYHVXN1C | |
625 | |
626 00RRR100 inc_reg | |
627 add 1 main.R main.R | |
628 update_flags SZYHVXN0 | |
629 | |
630 00110100 inc_hl | |
631 z80_fetch_hl | |
632 #TODO: fix size | |
633 add 1 scratch1 scratch1 | |
634 update_flags SZYHVXN0 | |
635 z80_store_hl | |
636 | |
637 00RRR101 dec_reg | |
638 add 1 main.R main.R | |
639 update_flags SZYHVXN0 | |
640 | |
641 00110101 dec_hl | |
642 z80_fetch_hl | |
643 #TODO: fix size | |
644 add 1 scratch1 scratch1 | |
645 update_flags SZYHVXN0 | |
646 z80_store_hl | |
647 | |
648 00101111 cpl | |
649 not a a | |
650 update_flags YH1XN1 | |
651 | |
652 11101101 neg | |
653 neg a a | |
654 update_flags SZYHVXN1C | |
655 | |
656 00111111 ccf | |
657 xor 0x80 chflags chflags | |
658 | |
659 00111111 scf | |
660 or 0x80 chflags chflags | |
661 | |
662 00000000 nop | |
663 | |
664 11110011 di | |
665 mov 0 iff1 | |
666 mov 0 iff2 | |
667 #TODO: update interrupt/sync cycle | |
668 | |
669 11111011 ei | |
670 mov 1 iff1 | |
671 mov 1 iff2 | |
672 #TODO: update interrupt/sync cycle | |
673 | |
674 ed 01000110 im0 | |
675 mov 0 imode | |
676 | |
677 ed 01010110 im1 | |
678 mov 1 imode | |
679 | |
680 ed 01011110 im2 | |
681 mov 2 imode | |
682 | |
683 11000011 jp | |
684 z80_fetch_immed16 | |
685 mov wz pc | |
686 | |
687 11CCC010 jp_cc | |
688 z80_check_cond C | |
689 z80_fetch_immed16 | |
690 if istrue | |
691 | |
692 mov wz pc | |
693 | |
694 end | |
695 | |
696 00011000 jr | |
697 z80_fetch_immed | |
698 #TODO: determine if this updates wz | |
699 sext 16 scratch1 scratch1 | |
700 add scratch1 pc pc | |
701 cycles 5 | |
702 | |
703 001CC000 jr_cc | |
704 z80_check_cond C | |
705 z80_fetch_immed | |
706 | |
707 if istrue | |
708 | |
709 sext 16 scratch1 scratch1 | |
710 add scratch1 pc pc | |
711 cycles 5 | |
712 | |
713 end | |
714 | |
715 00010000 djnz | |
716 cycles 1 | |
717 z80_fetch_immed | |
718 sub 1 b b | |
719 | |
720 if b | |
721 | |
722 sext 16 scratch1 scratch1 | |
723 add scratch1 pc pc | |
724 cycles 5 | |
725 | |
726 end | |
727 | |
728 | |
729 11001101 call_uncond | |
730 z80_fetch_immed16 | |
731 local pch 8 | |
732 lsr pc 8 pch | |
733 meta high pch | |
734 meta low pc | |
735 z80_push | |
736 mov wz pc | |
737 | |
738 11TTT111 rst | |
739 local pch 8 | |
740 lsr pc 8 pch | |
741 meta high pch | |
742 meta low pc | |
743 z80_push | |
744 lsl T 3 scratch1 | |
745 mov scratch1 pc | |
746 | |
747 11001001 ret | |
748 #TODO: confirm this goes through wz | |
749 local wzh 16 | |
750 meta high wzh | |
751 meta low wz | |
752 z80_pop | |
753 lsl wzh 8 wzh | |
754 or wzh wz wz | |
755 mov wz pc | |
756 | |
757 11011011 in_abs | |
758 z80_fetch_immed | |
759 ocall io_read8 | |
760 mov scratch1 a | |
761 | |
762 ed 01RRR000 in_bc | |
763 lsl b 8 scratch1 | |
764 or c scratch1 scratch1 | |
765 ocall io_read8 | |
766 mov scratch1 main.R | |
767 | |
768 11010011 out_abs | |
769 z80_fetch_immed | |
770 mov scratch1 scratch2 | |
771 mov a scratch1 | |
772 ocall io_write8 | |
773 | |
774 ed 01RRR001 out_bc | |
775 lsl b 8 scratch2 | |
776 or c scratch2 scratch2 | |
777 mov main.R scratch1 | |
778 ocall io_write8 |