Mercurial > repos > blastem
comparison sms.c @ 1769:8fe162bdb038 mame_interp
Merge from default
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 01 Mar 2019 14:17:29 -0800 |
parents | 95e387e1d63c 6e4faa10f9ee |
children | 0a26f3657295 |
comparison
equal
deleted
inserted
replaced
1768:63256371046f | 1769:8fe162bdb038 |
---|---|
7 #include "util.h" | 7 #include "util.h" |
8 #include "debug.h" | 8 #include "debug.h" |
9 #include "saves.h" | 9 #include "saves.h" |
10 #include "bindings.h" | 10 #include "bindings.h" |
11 | 11 |
12 #ifdef NEW_CORE | |
13 #define Z80_CYCLE cycles | |
14 #define Z80_OPTS opts | |
15 #define z80_handle_code_write(...) | |
16 #else | |
17 #define Z80_CYCLE current_cycle | |
18 #define Z80_OPTS options | |
19 #endif | |
20 | |
12 static void *memory_io_write(uint32_t location, void *vcontext, uint8_t value) | 21 static void *memory_io_write(uint32_t location, void *vcontext, uint8_t value) |
13 { | 22 { |
14 z80_context *z80 = vcontext; | 23 z80_context *z80 = vcontext; |
15 sms_context *sms = z80->system; | 24 sms_context *sms = z80->system; |
16 if (location & 1) { | 25 if (location & 1) { |
17 uint8_t fuzzy_ctrl_0 = sms->io.ports[0].control, fuzzy_ctrl_1 = sms->io.ports[1].control; | 26 uint8_t fuzzy_ctrl_0 = sms->io.ports[0].control, fuzzy_ctrl_1 = sms->io.ports[1].control; |
18 io_control_write(sms->io.ports, (~value) << 5 & 0x60, z80->current_cycle); | 27 io_control_write(sms->io.ports, (~value) << 5 & 0x60, z80->Z80_CYCLE); |
19 fuzzy_ctrl_0 |= sms->io.ports[0].control; | 28 fuzzy_ctrl_0 |= sms->io.ports[0].control; |
20 io_control_write(sms->io.ports+1, (~value) << 3 & 0x60, z80->current_cycle); | 29 io_control_write(sms->io.ports+1, (~value) << 3 & 0x60, z80->Z80_CYCLE); |
21 fuzzy_ctrl_1 |= sms->io.ports[1].control; | 30 fuzzy_ctrl_1 |= sms->io.ports[1].control; |
22 if ( | 31 if ( |
23 (fuzzy_ctrl_0 & 0x40 & (sms->io.ports[0].output ^ (value << 1)) & (value << 1)) | 32 (fuzzy_ctrl_0 & 0x40 & (sms->io.ports[0].output ^ (value << 1)) & (value << 1)) |
24 || (fuzzy_ctrl_0 & 0x40 & (sms->io.ports[1].output ^ (value >> 1)) & (value >> 1)) | 33 || (fuzzy_ctrl_0 & 0x40 & (sms->io.ports[1].output ^ (value >> 1)) & (value >> 1)) |
25 ) { | 34 ) { |
26 //TH is an output and it went from 0 -> 1 | 35 //TH is an output and it went from 0 -> 1 |
27 vdp_run_context(sms->vdp, z80->current_cycle); | 36 vdp_run_context(sms->vdp, z80->Z80_CYCLE); |
28 vdp_latch_hv(sms->vdp); | 37 vdp_latch_hv(sms->vdp); |
29 } | 38 } |
30 io_data_write(sms->io.ports, value << 1, z80->current_cycle); | 39 io_data_write(sms->io.ports, value << 1, z80->Z80_CYCLE); |
31 io_data_write(sms->io.ports + 1, value >> 1, z80->current_cycle); | 40 io_data_write(sms->io.ports + 1, value >> 1, z80->Z80_CYCLE); |
32 } else { | 41 } else { |
33 //TODO: memory control write | 42 //TODO: memory control write |
34 } | 43 } |
35 return vcontext; | 44 return vcontext; |
36 } | 45 } |
37 | 46 |
38 static uint8_t hv_read(uint32_t location, void *vcontext) | 47 static uint8_t hv_read(uint32_t location, void *vcontext) |
39 { | 48 { |
40 z80_context *z80 = vcontext; | 49 z80_context *z80 = vcontext; |
41 sms_context *sms = z80->system; | 50 sms_context *sms = z80->system; |
42 vdp_run_context(sms->vdp, z80->current_cycle); | 51 vdp_run_context(sms->vdp, z80->Z80_CYCLE); |
43 uint16_t hv = vdp_hv_counter_read(sms->vdp); | 52 uint16_t hv = vdp_hv_counter_read(sms->vdp); |
44 if (location & 1) { | 53 if (location & 1) { |
45 return hv; | 54 return hv; |
46 } else { | 55 } else { |
47 return hv >> 8; | 56 return hv >> 8; |
50 | 59 |
51 static void *sms_psg_write(uint32_t location, void *vcontext, uint8_t value) | 60 static void *sms_psg_write(uint32_t location, void *vcontext, uint8_t value) |
52 { | 61 { |
53 z80_context *z80 = vcontext; | 62 z80_context *z80 = vcontext; |
54 sms_context *sms = z80->system; | 63 sms_context *sms = z80->system; |
55 psg_run(sms->psg, z80->current_cycle); | 64 psg_run(sms->psg, z80->Z80_CYCLE); |
56 psg_write(sms->psg, value); | 65 psg_write(sms->psg, value); |
57 return vcontext; | 66 return vcontext; |
58 } | 67 } |
59 | 68 |
60 static void update_interrupts(sms_context *sms) | 69 static void update_interrupts(sms_context *sms) |
61 { | 70 { |
62 uint32_t vint = vdp_next_vint(sms->vdp); | 71 uint32_t vint = vdp_next_vint(sms->vdp); |
63 uint32_t hint = vdp_next_hint(sms->vdp); | 72 uint32_t hint = vdp_next_hint(sms->vdp); |
73 #ifdef NEW_CORE | |
74 sms->z80->int_cycle = vint < hint ? vint : hint; | |
75 z80_sync_cycle(sms->z80, sms->z80->sync_cycle); | |
76 #else | |
64 sms->z80->int_pulse_start = vint < hint ? vint : hint; | 77 sms->z80->int_pulse_start = vint < hint ? vint : hint; |
78 #endif | |
65 } | 79 } |
66 | 80 |
67 static uint8_t vdp_read(uint32_t location, void *vcontext) | 81 static uint8_t vdp_read(uint32_t location, void *vcontext) |
68 { | 82 { |
69 z80_context *z80 = vcontext; | 83 z80_context *z80 = vcontext; |
70 sms_context *sms = z80->system; | 84 sms_context *sms = z80->system; |
71 vdp_run_context(sms->vdp, z80->current_cycle); | 85 vdp_run_context(sms->vdp, z80->Z80_CYCLE); |
72 if (location & 1) { | 86 if (location & 1) { |
73 uint8_t ret = vdp_control_port_read(sms->vdp); | 87 uint8_t ret = vdp_control_port_read(sms->vdp); |
74 sms->vdp->flags2 &= ~(FLAG2_VINT_PENDING|FLAG2_HINT_PENDING); | 88 sms->vdp->flags2 &= ~(FLAG2_VINT_PENDING|FLAG2_HINT_PENDING); |
75 update_interrupts(sms); | 89 update_interrupts(sms); |
76 return ret; | 90 return ret; |
82 static void *vdp_write(uint32_t location, void *vcontext, uint8_t value) | 96 static void *vdp_write(uint32_t location, void *vcontext, uint8_t value) |
83 { | 97 { |
84 z80_context *z80 = vcontext; | 98 z80_context *z80 = vcontext; |
85 sms_context *sms = z80->system; | 99 sms_context *sms = z80->system; |
86 if (location & 1) { | 100 if (location & 1) { |
87 vdp_run_context_full(sms->vdp, z80->current_cycle); | 101 vdp_run_context_full(sms->vdp, z80->Z80_CYCLE); |
88 vdp_control_port_write_pbc(sms->vdp, value); | 102 vdp_control_port_write_pbc(sms->vdp, value); |
89 update_interrupts(sms); | 103 update_interrupts(sms); |
90 } else { | 104 } else { |
91 vdp_run_context(sms->vdp, z80->current_cycle); | 105 vdp_run_context(sms->vdp, z80->Z80_CYCLE); |
92 vdp_data_port_write_pbc(sms->vdp, value); | 106 vdp_data_port_write_pbc(sms->vdp, value); |
93 } | 107 } |
94 return vcontext; | 108 return vcontext; |
95 } | 109 } |
96 | 110 |
97 static uint8_t io_read(uint32_t location, void *vcontext) | 111 static uint8_t io_read(uint32_t location, void *vcontext) |
98 { | 112 { |
99 z80_context *z80 = vcontext; | 113 z80_context *z80 = vcontext; |
100 sms_context *sms = z80->system; | 114 sms_context *sms = z80->system; |
101 if (location == 0xC0 || location == 0xDC) { | 115 if (location == 0xC0 || location == 0xDC) { |
102 uint8_t port_a = io_data_read(sms->io.ports, z80->current_cycle); | 116 uint8_t port_a = io_data_read(sms->io.ports, z80->Z80_CYCLE); |
103 uint8_t port_b = io_data_read(sms->io.ports+1, z80->current_cycle); | 117 uint8_t port_b = io_data_read(sms->io.ports+1, z80->Z80_CYCLE); |
104 return (port_a & 0x3F) | (port_b << 6); | 118 return (port_a & 0x3F) | (port_b << 6); |
105 } | 119 } |
106 if (location == 0xC1 || location == 0xDD) { | 120 if (location == 0xC1 || location == 0xDD) { |
107 uint8_t port_a = io_data_read(sms->io.ports, z80->current_cycle); | 121 uint8_t port_a = io_data_read(sms->io.ports, z80->Z80_CYCLE); |
108 uint8_t port_b = io_data_read(sms->io.ports+1, z80->current_cycle); | 122 uint8_t port_b = io_data_read(sms->io.ports+1, z80->Z80_CYCLE); |
109 return (port_a & 0x40) | (port_b >> 2 & 0xF) | (port_b << 1 & 0x80) | 0x10; | 123 return (port_a & 0x40) | (port_b >> 2 & 0xF) | (port_b << 1 & 0x80) | 0x10; |
110 } | 124 } |
111 return 0xFF; | 125 return 0xFF; |
112 } | 126 } |
113 | 127 |
341 static uint8_t load_state(system_header *system, uint8_t slot) | 355 static uint8_t load_state(system_header *system, uint8_t slot) |
342 { | 356 { |
343 sms_context *sms = (sms_context *)system; | 357 sms_context *sms = (sms_context *)system; |
344 char *statepath = get_slot_name(system, slot, "state"); | 358 char *statepath = get_slot_name(system, slot, "state"); |
345 uint8_t ret; | 359 uint8_t ret; |
346 #ifdef USE_NATIVE | 360 #if !defined(NEW_CORE) && defined(USE_NATIVE) |
347 if (!sms->z80->native_pc) { | 361 if (!sms->z80->native_pc) { |
348 ret = get_modification_time(statepath) != 0; | 362 ret = get_modification_time(statepath) != 0; |
349 if (ret) { | 363 if (ret) { |
350 system->delayed_load_slot = slot + 1; | 364 system->delayed_load_slot = slot + 1; |
351 } | 365 } |
360 } | 374 } |
361 | 375 |
362 static void run_sms(system_header *system) | 376 static void run_sms(system_header *system) |
363 { | 377 { |
364 sms_context *sms = (sms_context *)system; | 378 sms_context *sms = (sms_context *)system; |
365 uint32_t target_cycle = sms->z80->current_cycle + 3420*16; | 379 uint32_t target_cycle = sms->z80->Z80_CYCLE + 3420*16; |
366 //TODO: PAL support | 380 //TODO: PAL support |
367 render_set_video_standard(VID_NTSC); | 381 render_set_video_standard(VID_NTSC); |
368 while (!sms->should_return) | 382 while (!sms->should_return) |
369 { | 383 { |
370 #ifdef USE_NATIVE | 384 #ifdef USE_NATIVE |
376 if (system->enter_debugger && sms->z80->pc) { | 390 if (system->enter_debugger && sms->z80->pc) { |
377 system->enter_debugger = 0; | 391 system->enter_debugger = 0; |
378 zdebugger(sms->z80, sms->z80->pc); | 392 zdebugger(sms->z80, sms->z80->pc); |
379 } | 393 } |
380 #endif | 394 #endif |
395 #ifdef NEW_CORE | |
396 if (sms->z80->nmi_cycle == CYCLE_NEVER) { | |
397 #else | |
381 if (sms->z80->nmi_start == CYCLE_NEVER) { | 398 if (sms->z80->nmi_start == CYCLE_NEVER) { |
399 #endif | |
382 uint32_t nmi = vdp_next_nmi(sms->vdp); | 400 uint32_t nmi = vdp_next_nmi(sms->vdp); |
383 if (nmi != CYCLE_NEVER) { | 401 if (nmi != CYCLE_NEVER) { |
384 z80_assert_nmi(sms->z80, nmi); | 402 z80_assert_nmi(sms->z80, nmi); |
385 } | 403 } |
386 } | 404 } |
387 z80_run(sms->z80, target_cycle); | 405 z80_run(sms->z80, target_cycle); |
388 if (sms->z80->reset) { | 406 if (sms->z80->reset) { |
389 z80_clear_reset(sms->z80, sms->z80->current_cycle + 128*15); | 407 z80_clear_reset(sms->z80, sms->z80->Z80_CYCLE + 128*15); |
390 } | 408 } |
391 target_cycle = sms->z80->current_cycle; | 409 target_cycle = sms->z80->Z80_CYCLE; |
392 vdp_run_context(sms->vdp, target_cycle); | 410 vdp_run_context(sms->vdp, target_cycle); |
393 psg_run(sms->psg, target_cycle); | 411 psg_run(sms->psg, target_cycle); |
394 | 412 |
395 #ifdef USE_NATIVE | 413 #ifdef USE_NATIVE |
396 if (system->save_state) { | 414 if (system->save_state) { |
397 while (!sms->z80->pc) { | 415 while (!sms->z80->pc) { |
398 //advance Z80 to an instruction boundary | 416 //advance Z80 to an instruction boundary |
399 z80_run(sms->z80, sms->z80->current_cycle + 1); | 417 z80_run(sms->z80, sms->z80->Z80_CYCLE + 1); |
400 } | 418 } |
401 save_state(sms, system->save_state - 1); | 419 save_state(sms, system->save_state - 1); |
402 system->save_state = 0; | 420 system->save_state = 0; |
403 } | 421 } |
404 #endif | 422 #endif |
405 | 423 |
406 target_cycle += 3420*16; | 424 target_cycle += 3420*16; |
407 if (target_cycle > 0x10000000) { | 425 if (target_cycle > 0x10000000) { |
408 uint32_t adjust = sms->z80->current_cycle - 3420*262*2; | 426 uint32_t adjust = sms->z80->Z80_CYCLE - 3420*262*2; |
409 io_adjust_cycles(sms->io.ports, sms->z80->current_cycle, adjust); | 427 io_adjust_cycles(sms->io.ports, sms->z80->Z80_CYCLE, adjust); |
410 io_adjust_cycles(sms->io.ports+1, sms->z80->current_cycle, adjust); | 428 io_adjust_cycles(sms->io.ports+1, sms->z80->Z80_CYCLE, adjust); |
411 z80_adjust_cycles(sms->z80, adjust); | 429 z80_adjust_cycles(sms->z80, adjust); |
412 vdp_adjust_cycles(sms->vdp, adjust); | 430 vdp_adjust_cycles(sms->vdp, adjust); |
413 sms->psg->cycles -= adjust; | 431 sms->psg->cycles -= adjust; |
414 target_cycle -= adjust; | 432 target_cycle -= adjust; |
415 } | 433 } |
455 } | 473 } |
456 | 474 |
457 static void soft_reset(system_header *system) | 475 static void soft_reset(system_header *system) |
458 { | 476 { |
459 sms_context *sms = (sms_context *)system; | 477 sms_context *sms = (sms_context *)system; |
460 z80_assert_reset(sms->z80, sms->z80->current_cycle); | 478 z80_assert_reset(sms->z80, sms->z80->Z80_CYCLE); |
461 #ifdef USE_NATIVE | 479 #if !defined(NEW_CORE) && defined(USE_NATIVE) |
462 sms->z80->target_cycle = sms->z80->sync_cycle = sms->z80->current_cycle; | 480 sms->z80->target_cycle = sms->z80->sync_cycle = sms->z80->current_cycle; |
463 #endif | 481 #endif |
464 } | 482 } |
465 | 483 |
466 static void free_sms(system_header *system) | 484 static void free_sms(system_header *system) |
467 { | 485 { |
468 sms_context *sms = (sms_context *)system; | 486 sms_context *sms = (sms_context *)system; |
469 vdp_free(sms->vdp); | 487 vdp_free(sms->vdp); |
470 z80_options_free(sms->z80->options); | 488 z80_options_free(sms->z80->Z80_OPTS); |
471 free(sms->z80); | 489 free(sms->z80); |
472 psg_free(sms->psg); | 490 psg_free(sms->psg); |
473 free(sms); | 491 free(sms); |
474 } | 492 } |
475 | 493 |
481 static void request_exit(system_header *system) | 499 static void request_exit(system_header *system) |
482 { | 500 { |
483 sms_context *sms = (sms_context *)system; | 501 sms_context *sms = (sms_context *)system; |
484 sms->should_return = 1; | 502 sms->should_return = 1; |
485 #ifdef USE_NATIVE | 503 #ifdef USE_NATIVE |
486 sms->z80->target_cycle = sms->z80->sync_cycle = sms->z80->current_cycle; | 504 #ifndef NEW_CORE |
505 sms->z80->target_cycle = sms->z80->sync_cycle = sms->z80->Z80_CYCLE; | |
506 #endif | |
487 #endif | 507 #endif |
488 } | 508 } |
489 | 509 |
490 static void inc_debug_mode(system_header *system) | 510 static void inc_debug_mode(system_header *system) |
491 { | 511 { |
587 memcpy(sms->header.info.map, memory_map, sizeof(memmap_chunk) * sms->header.info.map_chunks); | 607 memcpy(sms->header.info.map, memory_map, sizeof(memmap_chunk) * sms->header.info.map_chunks); |
588 z80_options *zopts = malloc(sizeof(z80_options)); | 608 z80_options *zopts = malloc(sizeof(z80_options)); |
589 init_z80_opts(zopts, sms->header.info.map, sms->header.info.map_chunks, io_map, 4, 15, 0xFF); | 609 init_z80_opts(zopts, sms->header.info.map, sms->header.info.map_chunks, io_map, 4, 15, 0xFF); |
590 sms->z80 = init_z80_context(zopts); | 610 sms->z80 = init_z80_context(zopts); |
591 sms->z80->system = sms; | 611 sms->z80->system = sms; |
592 sms->z80->options->gen.debug_cmd_handler = debug_commands; | 612 sms->z80->Z80_OPTS->gen.debug_cmd_handler = debug_commands; |
593 | 613 |
594 sms->rom = media->buffer; | 614 sms->rom = media->buffer; |
595 sms->rom_size = rom_size; | 615 sms->rom_size = rom_size; |
596 if (sms->header.info.map_chunks > 2) { | 616 if (sms->header.info.map_chunks > 2) { |
597 sms->z80->mem_pointers[0] = sms->rom; | 617 sms->z80->mem_pointers[0] = sms->rom; |