1 /*
2  * PicoDrive
3  * (c) Copyright Dave, 2004
4  * (C) notaz, 2006-2010
5  *
6  * This work is licensed under the terms of MAME license.
7  * See COPYING file in the top-level directory.
8  */
9 
10 #ifndef PICO_H
11 #define PICO_H
12 
13 #include <stdint.h> // [u]int<n>_t
14 #include <stdlib.h> // size_t
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 // message log
21 extern void lprintf(const char *fmt, ...);
22 
23 // external funcs for Sega/Mega CD
24 extern int  mp3_get_bitrate(void *f, int size);
25 extern void mp3_start_play(void *f, int pos);
26 extern void mp3_update(int *buffer, int length, int stereo);
27 
28 // this function should write-back d-cache and invalidate i-cache
29 // on a mem region [start_addr, end_addr)
30 // used by dynarecs
31 extern void cache_flush_d_inval_i(void *start_addr, void *end_addr);
32 
33 // attempt to alloc mem at specified address.
34 // alloc anywhere else if that fails (callers should handle that)
35 extern void *plat_mmap(unsigned long addr, size_t size, int need_exec, int is_fixed);
36 extern void *plat_mremap(void *ptr, size_t oldsize, size_t newsize);
37 extern void  plat_munmap(void *ptr, size_t size);
38 
39 // memory for the dynarec; plat_mem_get_for_drc() can just return NULL
40 extern void *plat_mem_get_for_drc(size_t size);
41 extern int   plat_mem_set_exec(void *ptr, size_t size);
42 
43 // this one should handle display mode changes
44 extern void emu_video_mode_change(int start_line, int line_count, int is_32cols);
45 
46 // this must switch to 16bpp mode
47 extern void emu_32x_startup(void);
48 
49 // optional 32X BIOS, should be left NULL if not used
50 // must be 256, 2048, 1024 bytes
51 extern void *p32x_bios_g, *p32x_bios_m, *p32x_bios_s;
52 
53 // Pico.c
54 #define POPT_EN_FM          (1<< 0) // 00 000x
55 #define POPT_EN_PSG         (1<< 1)
56 #define POPT_EN_Z80         (1<< 2)
57 #define POPT_EN_STEREO      (1<< 3)
58 #define POPT_ALT_RENDERER   (1<< 4) // 00 00x0
59 #define POPT_EN_YM2413      (1<< 5)
60 // unused                   (1<< 6)
61 #define POPT_ACC_SPRITES    (1<< 7)
62 #define POPT_DIS_32C_BORDER (1<< 8) // 00 0x00
63 #define POPT_EXT_FM         (1<< 9)
64 #define POPT_EN_MCD_PCM     (1<<10)
65 #define POPT_EN_MCD_CDDA    (1<<11)
66 #define POPT_EN_MCD_GFX     (1<<12) // 00 x000
67 // unused                   (1<<13)
68 #define POPT_EN_SOFTSCALE   (1<<14)
69 #define POPT_EN_MCD_RAMCART (1<<15)
70 #define POPT_DIS_VDP_FIFO   (1<<16) // 0x 0000
71 #define POPT_EN_DRC         (1<<17)
72 #define POPT_DIS_SPRITE_LIM (1<<18)
73 #define POPT_DIS_IDLE_DET   (1<<19)
74 #define POPT_EN_32X         (1<<20) // x0 0000
75 #define POPT_EN_PWM         (1<<21)
76 #define POPT_PWM_IRQ_OPT    (1<<22)
77 #define POPT_DIS_FM_SSGEG   (1<<23)
78 
79 #define PAHW_MCD  (1<<0)
80 #define PAHW_32X  (1<<1)
81 #define PAHW_SVP  (1<<2)
82 #define PAHW_PICO (1<<3)
83 #define PAHW_SMS  (1<<4)
84 
85 #define PQUIRK_FORCE_6BTN   (1<<0)
86 
87 // the emulator is configured and some status is reported
88 // through this global state (not saved in savestates)
89 typedef struct
90 {
91 	unsigned int opt; // POPT_* bitfield
92 
93 	unsigned short pad[2];     // Joypads, format is MXYZ SACB RLDU
94 	unsigned short padInt[2];  // internal copy
95 	unsigned short AHW;        // active addon hardware: PAHW_* bitfield
96 
97 	unsigned short skipFrame;      // skip rendering frame, but still do sound (if enabled) and emulation stuff
98 	unsigned short regionOverride; // override the region detection 0: auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe
99 	unsigned short autoRgnOrder;   // packed priority list of regions, for example 0x148 means this detection order: EUR, USA, JAP
100 
101 	unsigned short quirks;         // game-specific quirks: PQUIRK_*
102 	unsigned short overclockM68k;  // overclock the emulated 68k, in %
103 
104 	int sndRate;                   // rate in Hz
105 	unsigned short sndFilter;      // Set low pass sound filter 0: off, 1: on (use integer in case we want to add other filter types later)
106 	int32_t sndFilterRange;        // Low pass sound filter range [0, 65536]
107 	short *sndOut;                 // PCM output buffer
108 	void (*writeSound)(int len);   // write .sndOut callback, called once per frame
109 
110 	void (*osdMessage)(const char *msg); // output OSD message from emu, optional
111 
112 	void (*mcdTrayOpen)(void);
113 	void (*mcdTrayClose)(void);
114 } PicoInterface;
115 
116 extern PicoInterface PicoIn;
117 
118 void PicoInit(void);
119 void PicoExit(void);
120 void PicoPower(void);
121 int  PicoReset(void);
122 void PicoLoopPrepare(void);
123 void PicoFrame(void);
124 void PicoFrameDrawOnly(void);
125 typedef enum { PI_ROM, PI_ISPAL, PI_IS40_CELL, PI_IS240_LINES } pint_t;
126 typedef union { int vint; void *vptr; } pint_ret_t;
127 void PicoGetInternal(pint_t which, pint_ret_t *ret);
128 
129 struct PicoEState;
130 
131 // pico.c
132 #define XPCM_BUFFER_SIZE (320+160)
133 typedef struct
134 {
135 	int pen_pos[2];
136 	int page;
137 	// internal
138 	int fifo_bytes;      // bytes in FIFO
139 	int fifo_bytes_prev;
140 	int fifo_line_bytes; // float part, << 16
141 	int line_counter;
142 	unsigned short r1, r12;
143 	unsigned char xpcm_buffer[XPCM_BUFFER_SIZE+4];
144 	unsigned char *xpcm_ptr;
145 } picohw_state;
146 extern picohw_state PicoPicohw;
147 
148 // area.c
149 int PicoState(const char *fname, int is_save);
150 int PicoStateLoadGfx(const char *fname);
151 void *PicoTmpStateSave(void);
152 void  PicoTmpStateRestore(void *data);
153 extern void (*PicoStateProgressCB)(const char *str);
154 
155 // cd/cdd.c
156 int cdd_load(const char *filename, int type);
157 int cdd_unload(void);
158 
159 // Cart.c
160 typedef enum
161 {
162 	PMT_UNCOMPRESSED = 0,
163 	PMT_ZIP,
164 	PMT_CSO
165 } pm_type;
166 typedef struct
167 {
168 	void *file;		/* file handle */
169 	void *param;		/* additional file related field */
170 	unsigned int size;	/* size */
171 	pm_type type;
172 	char ext[4];
173 } pm_file;
174 pm_file *pm_open(const char *path);
175 size_t   pm_read(void *ptr, size_t bytes, pm_file *stream);
176 int      pm_seek(pm_file *stream, long offset, int whence);
177 int      pm_close(pm_file *fp);
178 int PicoCartLoad(pm_file *f,unsigned char **prom,unsigned int *psize,int is_sms);
179 int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_cfg);
180 void PicoCartUnload(void);
181 extern void (*PicoCartLoadProgressCB)(int percent);
182 extern void (*PicoCDLoadProgressCB)(const char *fname, int percent);
183 extern int PicoGameLoaded;
184 
185 // Draw.c
186 // for line-based renderer, set conversion
187 // from internal 8 bit representation in 'HighCol' to:
188 typedef enum
189 {
190 	PDF_NONE = 0,    // no conversion
191 	PDF_RGB555,      // RGB/BGR output, depends on compile options
192 	PDF_8BIT,        // 8-bit out (handles shadow/hilight mode, sonic water)
193 } pdso_t;
194 void PicoDrawSetOutFormat(pdso_t which, int use_32x_line_mode);
195 void PicoDrawSetOutBuf(void *dest, int increment);
196 void PicoDrawSetCallbacks(int (*begin)(unsigned int num), int (*end)(unsigned int num));
197 // utility
198 #ifdef _ASM_DRAW_C
199 void vidConvCpyRGB565(void *to, void *from, int pixels);
200 #endif
201 void PicoDoHighPal555(int sh, int line, struct PicoEState *est);
202 // internals, NB must keep in sync with ASM draw functions
203 #define PDRAW_SPRITES_MOVED (1<<0) // SAT address modified
204 #define PDRAW_WND_DIFF_PRIO (1<<1) // not all window tiles use same priority
205 #define PDRAW_PARSE_SPRITES (1<<2) // SAT needs parsing
206 #define PDRAW_INTERLACE     (1<<3)
207 #define PDRAW_DIRTY_SPRITES (1<<4) // SAT modified
208 #define PDRAW_SONIC_MODE    (1<<5) // mid-frame palette changes for 8bit renderer
209 #define PDRAW_PLANE_HI_PRIO (1<<6) // have layer with all hi prio tiles (mk3)
210 #define PDRAW_SHHI_DONE     (1<<7) // layer sh/hi already processed
211 #define PDRAW_32_COLS       (1<<8) // 32 column mode
212 #define PDRAW_BORDER_32     (1<<9) // center H32 in buffer (32 px border)
213 #define PDRAW_SKIP_FRAME   (1<<10) // frame is skipped
214 extern int rendstatus_old;
215 extern int rendlines;
216 
217 // draw.c
218 void PicoDrawUpdateHighPal(void);
219 void PicoDrawSetInternalBuf(void *dest, int line_increment);
220 
221 // draw2.c
222 // stuff below is optional
223 extern unsigned short *PicoCramHigh; // pointer to CRAM buff (0x40 shorts), converted to native device color (works only with 16bit for now)
224 extern void (*PicoPrepareCram)();    // prepares PicoCramHigh for renderer to use
225 
226 // pico.c (32x)
227 #ifndef NO_32X
228 
229 void Pico32xSetClocks(int msh2_hz, int ssh2_hz);
230 
231 #else
232 
233 #define Pico32xSetClocks(msh2_khz, ssh2_khz)
234 
235 #endif
236 
237 // normally 68k clock (7670442) * 3, in reality but much lower
238 // because of high memory latencies
239 #define PICO_MSH2_HZ ((int)(7670442.0 * 2.4))
240 #define PICO_SSH2_HZ ((int)(7670442.0 * 2.4))
241 
242 // sound.c
243 extern void (*PsndMix_32_to_16l)(short *dest, int *src, int count);
244 void PsndRerate(int preserve_state);
245 
246 // media.c
247 enum media_type_e {
248   PM_BAD_DETECT = -1,
249   PM_ERROR = -2,
250   PM_BAD_CD = -3,
251   PM_BAD_CD_NO_BIOS = -4,
252   PM_MD_CART = 1,	/* also 32x */
253   PM_MARK3,
254   PM_CD,
255 };
256 
257 enum cd_img_type
258 {
259   CIT_NOT_CD = 0,
260   CIT_ISO,
261   CIT_BIN,
262   CIT_CUE
263 };
264 
265 enum media_type_e PicoLoadMedia(const char *filename,
266   const char *carthw_cfg_fname,
267   const char *(*get_bios_filename)(int *region, const char *cd_fname),
268   void (*do_region_override)(const char *media_filename));
269 int PicoCdCheck(const char *fname_in, int *pregion);
270 
271 extern unsigned char media_id_header[0x100];
272 
273 // memory.c
274 enum input_device {
275   PICO_INPUT_NOTHING,
276   PICO_INPUT_PAD_3BTN,
277   PICO_INPUT_PAD_6BTN,
278 };
279 void PicoSetInputDevice(int port, enum input_device device);
280 
281 #ifdef __cplusplus
282 } // End of extern "C"
283 #endif
284 
285 #endif // PICO_H
286