12 #include "star/starcpu.h"
20 #include "musa/m68k.h"
26 #include "cyclone/Cyclone.h"
31 #include "mz80/mz80.h"
36 #include "cz80/cz80.h"
40 #include "drz80/drz80.h"
44 #include "dz80/types.h"
45 #include "dz80/dissz80.h"
48 //#define BUILD_YM2612
56 // Debugging macros and support functions. They look like this because C++98
57 // lacks support for variadic macros. Not my fault.
62 static inline const char *debug_basename_(const char *name
)
66 for (tmp
= name
; (*tmp
!= '\0'); ++tmp
)
72 static inline void debug_printf__()
77 static inline void (*debug_printf_(const char *s
, ...))()
82 vfprintf(stderr
, s
, vl
);
84 return debug_printf__
;
87 static inline void (*(*debug_(const char *file
, unsigned int line
,
88 const char *func
))(const char *, ...))()
90 fprintf(stderr
, "%s:%u: %s: ", debug_basename_(file
), line
, func
);
94 #define DEBUG(s) (((debug_(__FILE__, __LINE__, __func__))s)(), (void)0)
96 #define DEBUG(s) (void)0
99 /* ROM is always byteswapped when StarScream or Cyclone are enabled. */
100 #if defined(WITH_STAR) || defined(WITH_CYCLONE)
102 #define ROM_ADDR(a) ((a) ^ 1)
105 #define ROM_ADDR(a) (a)
112 extern "C" int test_ctv(unsigned char *dest
, int len
);
113 extern "C" int blur_bitmap_16(unsigned char *dest
, int len
);
114 extern "C" int blur_bitmap_15(unsigned char *dest
, int len
);
116 struct bmap
{ unsigned char *data
; int w
,h
; int pitch
; int bpp
; };
118 // New struct, happily encapsulates all the sound info
121 unsigned int len
; /* number of stereo samples */
124 int get_md_palette(unsigned char pal
[256],unsigned char *cram
);
130 // Next three lines are the state of the VDP
131 // They have to be public so we can save and load states
132 // and draw the screen.
133 uint8_t mem
[(0x10100 + 0x35)]; //0x20+0x10+0x4+1 for dirt
134 uint8_t *vram
, *cram
, *vsram
;
136 int rw_mode
,rw_addr
,rw_dma
;
139 bool cmd_pending
; // set when first half of command arrives
140 int sprite_overflow_line
;
142 int poke_vram (int addr
,unsigned char d
);
143 int poke_cram (int addr
,unsigned char d
);
144 int poke_vsram(int addr
,unsigned char d
);
147 unsigned char dma_mem_read(int addr
);
148 int putword(unsigned short d
);
149 int putbyte(unsigned char d
);
150 // Used by draw_scanline to render the different display components
151 void draw_tile1(int which
, int line
, unsigned char *where
);
152 void draw_tile1_solid(int which
, int line
, unsigned char *where
);
153 void draw_tile2(int which
, int line
, unsigned char *where
);
154 void draw_tile2_solid(int which
, int line
, unsigned char *where
);
155 void draw_tile3(int which
, int line
, unsigned char *where
);
156 void draw_tile3_solid(int which
, int line
, unsigned char *where
);
157 void draw_tile4(int which
, int line
, unsigned char *where
);
158 void draw_tile4_solid(int which
, int line
, unsigned char *where
);
159 void draw_window(int line
, int front
);
160 void draw_sprites(int line
, bool front
);
161 void draw_plane_back0(int line
);
162 void draw_plane_back1(int line
);
163 void draw_plane_front0(int line
);
164 void draw_plane_front1(int line
);
166 uint8_t* sprite
; // sprite location
167 uint32_t* tile
; // array of tiles (th * tw)
168 int x
; // X position in pixels
169 int y
; // Y position in pixels
170 int tw
; // width in tiles
171 int th
; // height in tiles
172 int w
; // width in pixels
173 int h
; // height in pixels
174 unsigned int prio
:1; // high priority bit
175 unsigned int inter
:1; // interlaced mode (8x16 tiles)
176 unsigned int xflip
:1; // X-flipped
177 unsigned int yflip
:1; // Y-flipped
179 inline void get_sprite_info(struct sprite_info
&, int);
180 inline void sprite_mask_add(uint8_t*, int, struct sprite_info
&, int);
181 // Working variables for the above
182 unsigned char sprite_order
[0x101], *sprite_base
;
183 uint8_t sprite_mask
[512][512];
185 int masking_sprite_index_cache
;
188 unsigned int Bpp_times8
;
195 // These are called by MEM.CPP
196 int command(uint16_t cmd
);
197 unsigned short readword();
198 unsigned char readbyte();
199 int writeword(unsigned short d
);
200 int writebyte(unsigned char d
);
202 unsigned char *dirt
; // Bitfield: what has changed VRAM/CRAM/VSRAM/Reg
205 uint32_t highpal
[64];
207 void sprite_masking_overflow(int line
);
208 void sprite_mask_generate();
209 void draw_scanline(struct bmap
*bits
, int line
);
210 void draw_pixel(struct bmap
*bits
, int x
, int y
, uint32_t rgb
);
211 void write_reg(uint8_t addr
, uint8_t data
);
214 /* Generic structures for dumping and restoring M68K and Z80 states. */
217 /* Stored MSB first (big endian) */
218 uint32_t d
[8]; /* D0-D7 */
219 uint32_t a
[8]; /* A0-A7 */
225 /* Stored LSB first (little endian) */
231 } alt
[2]; /* [1] = alternate registers */
238 uint8_t iff
; /* x x x x x x IFF2 IFF1 */
239 uint8_t im
; /* interrupt mode */
240 uint8_t irq_asserted
; /* IRQ asserted */
241 uint8_t irq_vector
; /* IRQ vector */
244 #define MCLK_CYCLES_PER_LINE 3416 /* XXX ideally 3415.597 */
245 #define M68K_CYCLES_PER_LINE (MCLK_CYCLES_PER_LINE / 7)
246 #define M68K_CYCLES_HBLANK ((M68K_CYCLES_PER_LINE * 36) / 209)
247 #define M68K_CYCLES_VDELAY ((M68K_CYCLES_PER_LINE * 36) / 156)
248 #define Z80_CYCLES_PER_LINE (MCLK_CYCLES_PER_LINE / 15)
249 #define Z80_CYCLES_HBLANK ((Z80_CYCLES_PER_LINE * 36) / 209)
250 #define Z80_CYCLES_VDELAY ((Z80_CYCLES_PER_LINE * 36) / 156)
251 #define NTSC_LINES 262
252 #define NTSC_VBLANK 224
254 #define NTSC_MCLK (MCLK_CYCLES_PER_LINE * NTSC_LINES * NTSC_HZ)
255 #define PAL_LINES 312
256 #define PAL_VBLANK 240
258 #define PAL_MCLK (MCLK_CYCLES_PER_LINE * PAL_LINES * PAL_HZ)
260 #define MD_UP_MASK (1) // 0x00001
261 #define MD_DOWN_MASK (1 << 1) // 0x00002
262 #define MD_LEFT_MASK (1 << 2) // 0x00004
263 #define MD_RIGHT_MASK (1 << 3) // 0x00008
264 #define MD_B_MASK (1 << 4) // 0x00010
265 #define MD_C_MASK (1 << 5) // 0x00020
266 #define MD_A_MASK (1 << 12) // 0x01000
267 #define MD_START_MASK (1 << 13) // 0x02000
268 #define MD_Z_MASK (1 << 16) // 0x10000
269 #define MD_Y_MASK (1 << 17) // 0x20000
270 #define MD_X_MASK (1 << 18) // 0x40000
271 #define MD_MODE_MASK (1 << 19) // 0x80000
272 #define MD_PAD_UNTOUCHED 0xf303f
274 #define MD_PICO_PENBTN_MASK (1 << 7) // 0x00080
280 static const uint8_t no_rom
[];
281 static const size_t no_rom_size
;
283 // Get default NTSC/PAL, Hz, VBLANK, lines number and memory byte
284 // for region, which is identified by a single character (J, X, U, E).
285 static void region_info(uint8_t region
, int* pal
, int* hz
,
286 int* vblank
, int* lines
, uint8_t* mem
)
288 // Make region code uppercase and replace space with 0.
292 // Japan (PAL). This region code isn't found in ROMs
293 // but I wanted to label it somehow.
295 *mem
= (0x00 | 0x40); // local + PAL
304 *vblank
= PAL_VBLANK
;
308 *mem
= (0x80 | 0x40); // overseas + PAL
314 *mem
= 0x00; // local
323 *vblank
= NTSC_VBLANK
;
327 *mem
= 0x80; // overseas
333 static class md
* md_musa
;
334 unsigned int md_musa_ref
;
335 class md
* md_musa_prev
;
337 bool md_set_musa(bool set
);
338 void md_set_musa_sync(bool push
);
341 static class md
* md_cyclone
;
342 unsigned int md_cyclone_ref
;
343 class md
* md_cyclone_prev
;
345 bool md_set_cyclone(bool set
);
346 void md_set_cyclone_sync(bool push
);
347 uintptr_t checkpc(uintptr_t pc
);
350 static class md
* md_star
;
351 unsigned int md_star_ref
;
352 class md
* md_star_prev
;
354 bool md_set_star(bool set
);
355 void md_set_star_sync(bool push
);
358 unsigned int md_cz80_ref
;
360 bool md_set_cz80(bool set
);
361 void md_set_cz80_sync(bool push
);
364 static class md
* md_mz80
;
365 unsigned int md_mz80_ref
;
366 class md
* md_mz80_prev
;
368 bool md_set_mz80(bool set
);
369 void md_set_mz80_sync(bool push
);
372 static class md
* md_drz80
;
373 unsigned int md_drz80_ref
;
374 class md
* md_drz80_prev
;
376 bool md_set_drz80(bool set
);
377 void md_set_drz80_sync(bool push
);
379 void md_set(bool set
);
381 unsigned int mclk
; // Master clock
382 unsigned int clk0
; // MCLK/15 for Z80, SN76489
383 unsigned int clk1
; // MCLK/7 for M68K, YM2612
388 unsigned int vblank(); // Return first vblank line
391 static bool lock
; // Prevent other MD objects
394 unsigned int ok_ym2612
: 1; // YM2612
395 unsigned int ok_sn76496
: 1; // SN76496
398 unsigned char *mem
,*rom
,*ram
,*z80ram
;
400 unsigned char *saveram
; // The actual saveram buffer
401 unsigned save_start
, save_len
; // Start address and length
402 int save_prot
, save_active
; // Flags set from $A130F1
405 m68k_state_t m68k_state
;
406 z80_state_t z80_state
;
407 void m68k_state_dump();
408 void m68k_state_restore();
409 void z80_state_dump();
410 void z80_state_restore();
413 struct mz80context z80
;
419 struct DrZ80 drz80
__attribute__((packed
)); // See drz80.h and drz80.s.
420 friend uintptr_t drz80_rebaseSP(uint16_t new_sp
);
421 uintptr_t drz80_rebase_pc(uint16_t address
);
422 friend uintptr_t drz80_rebasePC(uint16_t new_pc
);
423 uintptr_t drz80_rebase_sp(uint16_t address
);
424 friend void drz80_irq_callback();
428 struct S68000CONTEXT cpu
;
429 STARSCREAM_PROGRAMREGION
*fetch
;
430 STARSCREAM_DATAREGION
*readbyte
,*readword
,*writebyte
,*writeword
;
432 friend void star_irq_callback(void);
436 void musa_memory_map();
437 m68k_mem_t musa_memory
[3];
438 friend int musa_irq_callback(int);
441 struct Cyclone cyclonecpu
;
442 friend int cyclone_irq_callback(int);
445 uint32_t z80_bank68k
;
446 unsigned int z80_st_busreq
: 1; // in BUSREQ state
447 unsigned int z80_st_reset
: 1; // in RESET state
448 unsigned int z80_st_running
: 1; // Z80 is running
449 unsigned int z80_st_irq
: 1; // Z80 IRQ asserted
450 unsigned int m68k_st_running
: 1; // M68K is running
451 int z80_irq_vector
; // Z80 IRQ vector
461 // Note order is (0) Vblank end -------- Vblank Start -- (HIGH)
462 // So int6 happens in the middle of the count
464 int aoo3_toggle
,aoo5_toggle
,aoo3_six
,aoo5_six
;
465 int aoo3_six_timeout
, aoo5_six_timeout
;
466 unsigned char calculate_coo8();
467 unsigned char calculate_coo9();
468 int may_want_to_get_pic(struct bmap
*bm
,unsigned char retpal
[256],int mark
);
469 int may_want_to_get_sound(struct sndinfo
*sndi
);
471 // Horizontal counter table
472 uint8_t hc_table
[512][2];
474 unsigned int m68k_read_pc(); // PC data
475 int m68k_odo(); // M68K odometer
476 void m68k_run(); // Run M68K to odo.m68k_max
477 void m68k_busreq_request(); // Issue BUSREQ
478 void m68k_busreq_cancel(); // Cancel BUSREQ
479 void m68k_irq(int i
); // Trigger M68K IRQ
480 void m68k_vdp_irq_trigger(); // Trigger IRQ from VDP status
481 void m68k_vdp_irq_handler(); // Called when interrupts are acknowledged
483 int z80_odo(); // Z80 odometer
484 void z80_run(); // Run Z80 to odo.z80_max
485 void z80_sync(int fake
); // Synchronize Z80 with M68K
486 void z80_irq(int vector
); // Trigger Z80 IRQ
487 void z80_irq_clear(); // Clear Z80 IRQ
489 // Number of microseconds spent in current frame
490 unsigned int frame_usecs();
492 int fm_timer_callback();
493 int myfm_read(int a
);
494 int mysn_write(int v
);
499 signed short fm_reg
[2][0x100]; // All of them (-1 = not def'd yet)
501 uint8_t dac_data
[0x400];
502 unsigned int dac_len
;
505 void dac_submit(uint8_t d
);
506 void dac_enable(uint8_t d
);
508 uint8_t m68k_ROM_read(uint32_t a
);
509 uint8_t m68k_IO_read(uint32_t a
);
510 uint8_t m68k_VDP_read(uint32_t a
);
511 void m68k_ROM_write(uint32_t, uint8_t);
512 void m68k_IO_write(uint32_t, uint8_t);
516 int myfm_write(int a
,int v
,int md
);
520 uint32_t vgm_dump_samples_total
;
521 uint32_t vgm_dump_dac_wait
;
522 unsigned int vgm_dump_dac_samples
;
524 void vgm_dump_ym2612(uint8_t a1
, uint8_t reg
, uint8_t data
);
525 void vgm_dump_sn76496(uint8_t data
);
526 int vgm_dump_start(const char *name
);
527 void vgm_dump_stop();
528 void vgm_dump_frame();
531 // public struct, full with data from the cartridge header
533 char system_name
[0x10]; // "SEGA GENESIS ", "SEGA MEGA DRIVE "
534 char copyright
[0x10]; // "(C)SEGA 1988.JUL"
535 char domestic_name
[0x30]; // Domestic game name
536 char overseas_name
[0x30]; // Overseas game name
537 char product_no
[0xe]; // "GM XXXXXXXX-XX" or "GM MK-XXXX -00"
538 unsigned short checksum
; // ROM checksum
539 char control_data
[0x10]; // I/O use data (usually only joystick)
540 unsigned rom_start
, rom_end
; // ROM start & end addresses
541 unsigned ram_start
, ram_end
; // RAM start & end addresses
542 unsigned short save_magic
; // 0x5241("RA") if saveram, else 0x2020
543 unsigned short save_flags
; // Misc saveram info
544 unsigned save_start
, save_end
; // Saveram start & end
545 unsigned short modem_magic
; // 0x4d4f("MO") if uses modem, else 0x2020
546 char modem_firm
[4]; // Modem firm name (should be same as (c))
547 char modem_ver
[4]; // yy.z, yy=ID number, z=version
548 char memo
[0x28]; // Extra data
549 char countries
[0x10]; // Country code
551 char region
; // Emulator region.
552 uint8_t region_guess();
553 int one_frame(struct bmap
*bm
,unsigned char retpal
[256],struct sndinfo
*sndi
);
559 uint16_t pico_pen_coords
[2];
561 // c000004 bit 1 write fifo empty, bit 0 write fifo full (???)
562 // c000005 vint happened, (sprover, coll, oddinint)
563 // invblank, inhblank, dma busy, pal
564 unsigned char coo4
,coo5
;
565 int okay() {return ok
;}
567 md(bool pal
, char region
);
571 int plug_in(unsigned char *cart
,int len
);
573 int load(const char *name
);
577 uint8_t misc_readbyte(uint32_t a
);
578 void misc_writebyte(uint32_t a
, uint8_t d
);
579 uint16_t misc_readword(uint32_t a
);
580 void misc_writeword(uint32_t a
, uint16_t d
);
584 uint8_t z80_read(uint16_t a
);
585 void z80_write(uint16_t a
, uint8_t d
);
586 uint8_t z80_port_read(uint16_t a
);
587 void z80_port_write(uint16_t a
, uint8_t d
);
616 } cpu_emu
; // OK to read it but call cycle_cpu() to change it
620 mz80context
& z80_context() {return z80
;}
622 int import_gst(FILE *hand
);
623 int export_gst(FILE *hand
);
630 void fix_rom_checksum();
632 // List of patches currently applied.
634 struct patch_elem
*next
;
638 // Added by Joe Groff:
639 // Patch the ROM code, using Game Genie/Hex codes
640 int patch(const char *list
, unsigned int *errors
,
641 unsigned int *applied
, unsigned int *reverted
);
642 // Get/put the battery save RAM
644 int get_save_ram(FILE *from
);
645 int put_save_ram(FILE *into
);
648 // Added by Phillip K. Hornung <redx@pknet.com>
649 void init_joysticks();
650 void deinit_joysticks();
658 #define M68K_SR_CARRY (1<<0)
659 #define M68K_SR_OVERFLOW (1<<1)
660 #define M68K_SR_ZERO (1<<2)
661 #define M68K_SR_NEGATIVE (1<<3)
662 #define M68K_SR_EXTEND (1<<4)
664 #define M68K_SR_IP_MASK1 (1<<8)
665 #define M68K_SR_IP_MASK2 (1<<9)
666 #define M68K_SR_IP_MASK3 (1<<10)
667 #define M68K_SR_MI_STATE (1<<12)
668 #define M68K_SR_SUP_STATE (1<<13)
669 #define M68K_SR_TRACE_EN1 (1<<14)
670 #define M68K_SR_TRACE_EN2 (1<<15)
672 #define Z80_SR_CARRY (1<<0)
673 #define Z80_SR_ADD_SUB (1<<1)
674 #define Z80_SR_PARITY_OVERFLOW (1<<2)
675 #define Z80_SR_HALF_CARRY (1<<4)
676 #define Z80_SR_ZERO (1<<6)
677 #define Z80_SR_SIGN (1<<7)
679 struct dgen_bp debug_bp_m68k
[MAX_BREAKPOINTS
];
680 struct dgen_wp debug_wp_m68k
[MAX_WATCHPOINTS
];
681 unsigned int debug_step_m68k
;
682 unsigned int debug_trace_m68k
;
683 struct dgen_bp debug_bp_z80
[MAX_BREAKPOINTS
];
684 struct dgen_wp debug_wp_z80
[MAX_WATCHPOINTS
];
685 unsigned int debug_step_z80
;
686 unsigned int debug_trace_z80
;
688 unsigned long debug_m68k_instr_count
;
689 unsigned long debug_z80_instr_count
;
690 bool debug_instr_count_enabled
;
695 bool debug_is_m68k_bp_set();
696 bool debug_is_m68k_wp_set();
697 int debug_next_free_wp_m68k();
698 int debug_next_free_bp_m68k();
699 bool debug_is_z80_bp_set();
700 bool debug_is_z80_wp_set();
701 int debug_next_free_wp_z80();
702 int debug_next_free_bp_z80();
704 int debug_find_bp_m68k(uint32_t);
705 int debug_find_wp_m68k(uint32_t);
706 void debug_print_m68k_wp(int);
707 int debug_should_m68k_wp_fire(struct dgen_wp
*w
);
708 int debug_find_bp_z80(uint16_t);
709 int debug_find_wp_z80(uint16_t);
710 void debug_print_z80_wp(int);
711 int debug_should_z80_wp_fire(struct dgen_wp
*w
);
712 uint32_t m68k_get_pc();
713 uint16_t z80_get_pc();
714 bool debug_m68k_check_bps();
715 bool debug_m68k_check_wps();
716 bool debug_z80_check_bps();
717 bool debug_z80_check_wps();
718 void debug_rm_bp_m68k(int);
719 void debug_rm_wp_m68k(int);
720 void debug_rm_bp_z80(int);
721 void debug_rm_wp_z80(int);
722 void debug_list_bps_m68k();
723 void debug_list_wps_m68k();
724 void debug_list_bps_z80();
725 void debug_list_wps_z80();
726 int debug_set_bp_m68k(uint32_t);
727 int debug_set_bp_z80(uint16_t);
731 struct dgen_debugger_cmd
{
734 int (md::*handler
)(int, char **);
736 const static struct md::dgen_debugger_cmd debug_cmd_list
[];
739 int debug_despatch_cmd(int n_toks
, char **args
);
740 int debug_cmd_cont(int n_args
, char **args
);
741 int debug_cmd_reg(int n_args
, char **args
);
742 int debug_cmd_help(int n_args
, char **args
);
743 int debug_cmd_break(int n_args
, char **args
);
744 int debug_cmd_quit(int n_args
, char **args
);
745 int debug_cmd_step(int n_args
, char **args
);
746 int debug_cmd_trace(int n_args
, char **args
);
747 int debug_cmd_minus_break(int n_args
, char **args
);
748 int debug_cmd_cpu(int n_args
, char **args
);
749 int debug_cmd_dis(int n_args
, char **args
);
750 int debug_cmd_mem(int n_args
, char **args
);
751 int debug_cmd_setbwlr(int n_args
, char **args
, unsigned int type
);
752 int debug_cmd_setb(int n_args
, char **args
);
753 int debug_cmd_setw(int n_args
, char **args
);
754 int debug_cmd_setl(int n_args
, char **args
);
755 int debug_cmd_setr(int n_args
, char **args
);
756 int debug_cmd_count(int n_args
, char **args
);
757 int debug_cmd_watch(int n_args
, char **args
);
758 int debug_cmd_minus_watch(int n_args
, char **args
);
760 int debug_enter(void);
761 void debug_leave(void);
762 void debug_update_m68k_wp_cache(struct dgen_wp
*w
);
763 void debug_update_z80_wp_cache(struct dgen_wp
*w
);
764 void debug_update_fired_m68k_wps(void);
765 void debug_update_fired_z80_wps(void);
766 void debug_set_wp_m68k(uint32_t start_addr
, uint32_t end_addr
);
767 void debug_set_wp_z80(uint16_t start_addr
, uint16_t end_addr
);
768 void debug_print_m68k_disassemble(uint32_t from
, int len
);
769 void debug_print_z80_disassemble(uint16_t, unsigned int);
770 void debug_show_m68k_regs(void);
771 void debug_show_z80_regs(void);
772 void debug_dump_mem(uint32_t addr
, uint32_t len
);
776 inline int md::has_save_ram()