1 // license:LGPL-2.1+
2 // copyright-holders:David Haywood, Angelo Salese, Olivier Galibert, Mariusz Wojcieszek, R. Belmont
3 #ifndef MAME_INCLUDES_SATURN_H
4 #define MAME_INCLUDES_SATURN_H
5 
6 #pragma once
7 
8 #include "bus/generic/slot.h"
9 #include "bus/generic/carts.h"
10 
11 #include "cpu/m68000/m68000.h"
12 #include "cpu/sh/sh2.h"
13 
14 #include "debug/debugcon.h"
15 #include "debug/debugcmd.h"
16 
17 #include "machine/315-5881_crypt.h"
18 #include "machine/315-5838_317-0229_comp.h"
19 #include "machine/sega_scu.h"
20 #include "machine/smpc.h"
21 #include "machine/timer.h"
22 
23 #include "sound/scsp.h"
24 
25 #include "debugger.h"
26 #include "emupal.h"
27 #include "screen.h"
28 
29 class saturn_state : public driver_device
30 {
31 public:
saturn_state(const machine_config & mconfig,device_type type,const char * tag)32 	saturn_state(const machine_config &mconfig, device_type type, const char *tag)
33 		: driver_device(mconfig, type, tag),
34 			m_rom(*this, "bios", 0x20000),
35 			m_workram_l(*this, "workram_l"),
36 			m_workram_h(*this, "workram_h"),
37 			m_sound_ram(*this, "sound_ram"),
38 			m_fake_comms(*this, "fake"),
39 			m_maincpu(*this, "maincpu"),
40 			m_slave(*this, "slave"),
41 			m_audiocpu(*this, "audiocpu"),
42 			m_scsp(*this, "scsp"),
43 			m_smpc_hle(*this, "smpc"),
44 			m_scu(*this, "scu"),
45 			m_gfxdecode(*this, "gfxdecode"),
46 			m_screen(*this, "screen"),
47 			m_palette(*this, "palette")
48 	{
49 	}
50 
51 	void scsp_irq(offs_t offset, uint8_t data);
52 
53 	// SMPC HLE delegates
54 	DECLARE_WRITE_LINE_MEMBER(master_sh2_reset_w);
55 	DECLARE_WRITE_LINE_MEMBER(master_sh2_nmi_w);
56 	DECLARE_WRITE_LINE_MEMBER(slave_sh2_reset_w);
57 	DECLARE_WRITE_LINE_MEMBER(sound_68k_reset_w);
58 	DECLARE_WRITE_LINE_MEMBER(system_reset_w);
59 	DECLARE_WRITE_LINE_MEMBER(system_halt_w);
60 	DECLARE_WRITE_LINE_MEMBER(dot_select_w);
61 
62 	DECLARE_WRITE_LINE_MEMBER(m68k_reset_callback);
63 
64 protected:
65 	required_region_ptr<uint32_t> m_rom;
66 	required_shared_ptr<uint32_t> m_workram_l;
67 	required_shared_ptr<uint32_t> m_workram_h;
68 	required_shared_ptr<uint16_t> m_sound_ram;
69 	optional_ioport m_fake_comms;
70 
71 	memory_region *m_cart_reg[4];
72 	std::unique_ptr<uint8_t[]>     m_backupram;
73 //  std::unique_ptr<uint32_t[]>    m_scu_regs;
74 	std::unique_ptr<uint16_t[]>    m_vdp2_regs;
75 	std::unique_ptr<uint32_t[]>    m_vdp2_vram;
76 	std::unique_ptr<uint32_t[]>    m_vdp2_cram;
77 	std::unique_ptr<uint32_t[]>    m_vdp1_vram;
78 	std::unique_ptr<uint16_t[]>    m_vdp1_regs;
79 
80 	uint8_t     m_en_68k;
81 
82 	int       m_minit_boost;
83 	int       m_sinit_boost;
84 	attotime  m_minit_boost_timeslice;
85 	attotime  m_sinit_boost_timeslice;
86 
87 	struct {
88 		uint16_t    **framebuffer_display_lines;
89 		int       framebuffer_mode;
90 		int       framebuffer_double_interlace;
91 		int       fbcr_accessed;
92 		int       framebuffer_width;
93 		int       framebuffer_height;
94 		int       framebuffer_current_display;
95 		int       framebuffer_current_draw;
96 		int       framebuffer_clear_on_next_frame;
97 		rectangle system_cliprect;
98 		rectangle user_cliprect;
99 		std::unique_ptr<uint16_t[]>   framebuffer[2];
100 		uint16_t    **framebuffer_draw_lines;
101 		std::unique_ptr<uint8_t[]>     gfx_decode;
102 		uint16_t    lopr;
103 		uint16_t    copr;
104 		uint16_t    ewdr;
105 
106 		int       local_x;
107 		int       local_y;
108 	}m_vdp1;
109 
110 	struct {
111 		std::unique_ptr<uint8_t[]>      gfx_decode;
112 		bitmap_rgb32 roz_bitmap[2];
113 		uint8_t     dotsel;
114 		uint8_t     pal;
115 		uint8_t     odd;
116 		uint16_t    h_count;
117 		uint16_t    v_count;
118 		uint8_t     exltfg;
119 		uint8_t     exsyfg;
120 		int       old_crmd;
121 		int       old_tvmd;
122 	}m_vdp2;
123 
124 	required_device<sh2_device> m_maincpu;
125 	required_device<sh2_device> m_slave;
126 	required_device<m68000_base_device> m_audiocpu;
127 	required_device<scsp_device> m_scsp;
128 	required_device<smpc_hle_device> m_smpc_hle;
129 	required_device<sega_scu_device> m_scu;
130 	required_device<gfxdecode_device> m_gfxdecode;
131 	required_device<screen_device> m_screen;
132 	required_device<palette_device> m_palette;
133 
134 	bitmap_rgb32 m_tmpbitmap;
135 	DECLARE_VIDEO_START(stv_vdp2);
136 	uint32_t screen_update_stv_vdp2(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
137 	TIMER_DEVICE_CALLBACK_MEMBER(saturn_scanline);
138 	TIMER_DEVICE_CALLBACK_MEMBER(saturn_slave_scanline);
139 
140 
141 	TIMER_CALLBACK_MEMBER(vdp1_draw_end);
142 	void saturn_soundram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
143 	uint16_t saturn_soundram_r(offs_t offset);
144 	void minit_w(uint32_t data);
145 	void sinit_w(uint32_t data);
146 	void saturn_minit_w(uint32_t data);
147 	void saturn_sinit_w(uint32_t data);
148 	uint8_t saturn_backupram_r(offs_t offset);
149 	void saturn_backupram_w(offs_t offset, uint8_t data);
150 
151 	int m_scsp_last_line;
152 
153 	uint16_t saturn_vdp1_regs_r(offs_t offset);
154 	uint32_t saturn_vdp1_vram_r(offs_t offset);
155 	uint32_t saturn_vdp1_framebuffer0_r(offs_t offset, uint32_t mem_mask = ~0);
156 
157 	void saturn_vdp1_regs_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
158 	void saturn_vdp1_vram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0);
159 	void saturn_vdp1_framebuffer0_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0);
160 
161 	uint32_t saturn_vdp2_vram_r(offs_t offset);
162 	uint32_t saturn_vdp2_cram_r(offs_t offset);
163 	uint16_t saturn_vdp2_regs_r(offs_t offset);
164 
165 	void saturn_vdp2_vram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0);
166 	void saturn_vdp2_cram_w(offs_t offset, uint32_t data, uint32_t mem_mask = ~0);
167 	void saturn_vdp2_regs_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
168 
169 
170 	/* VDP1 */
171 	void stv_set_framebuffer_config( void );
172 	void stv_prepare_framebuffers( void );
173 	void stv_vdp1_change_framebuffers( void );
174 	void video_update_vdp1( void );
175 	void stv_vdp1_process_list( void );
176 	void stv_vdp1_set_drawpixel( void );
177 
178 	void stv_vdp1_draw_normal_sprite(const rectangle &cliprect, int sprite_type);
179 	void stv_vdp1_draw_scaled_sprite(const rectangle &cliprect);
180 	void stv_vdp1_draw_distorted_sprite(const rectangle &cliprect);
181 	void stv_vdp1_draw_poly_line(const rectangle &cliprect);
182 	void stv_vdp1_draw_line(const rectangle &cliprect);
183 	int x2s(int v);
184 	int y2s(int v);
185 	void vdp1_fill_quad(const rectangle &cliprect, int patterndata, int xsize, const struct spoint *q);
186 	void vdp1_fill_line(const rectangle &cliprect, int patterndata, int xsize, int32_t y, int32_t x1, int32_t x2, int32_t u1, int32_t u2, int32_t v1, int32_t v2);
187 	void (saturn_state::*drawpixel)(int x, int y, int patterndata, int offsetcnt);
188 	void drawpixel_poly(int x, int y, int patterndata, int offsetcnt);
189 	void drawpixel_8bpp_trans(int x, int y, int patterndata, int offsetcnt);
190 	void drawpixel_4bpp_notrans(int x, int y, int patterndata, int offsetcnt);
191 	void drawpixel_4bpp_trans(int x, int y, int patterndata, int offsetcnt);
192 	void drawpixel_generic(int x, int y, int patterndata, int offsetcnt);
193 	void vdp1_fill_slope(const rectangle &cliprect, int patterndata, int xsize,
194 							int32_t x1, int32_t x2, int32_t sl1, int32_t sl2, int32_t *nx1, int32_t *nx2,
195 							int32_t u1, int32_t u2, int32_t slu1, int32_t slu2, int32_t *nu1, int32_t *nu2,
196 							int32_t v1, int32_t v2, int32_t slv1, int32_t slv2, int32_t *nv1, int32_t *nv2,
197 							int32_t _y1, int32_t y2);
198 	void stv_vdp1_setup_shading_for_line(int32_t y, int32_t x1, int32_t x2,
199 												int32_t r1, int32_t g1, int32_t b1,
200 												int32_t r2, int32_t g2, int32_t b2);
201 	void stv_vdp1_setup_shading_for_slope(
202 							int32_t x1, int32_t x2, int32_t sl1, int32_t sl2, int32_t *nx1, int32_t *nx2,
203 							int32_t r1, int32_t r2, int32_t slr1, int32_t slr2, int32_t *nr1, int32_t *nr2,
204 							int32_t g1, int32_t g2, int32_t slg1, int32_t slg2, int32_t *ng1, int32_t *ng2,
205 							int32_t b1, int32_t b2, int32_t slb1, int32_t slb2, int32_t *nb1, int32_t *nb2,
206 							int32_t _y1, int32_t y2);
207 	uint16_t stv_vdp1_apply_gouraud_shading( int x, int y, uint16_t pix );
208 	void stv_vdp1_setup_shading(const struct spoint* q, const rectangle &cliprect);
209 	uint8_t stv_read_gouraud_table( void );
210 	void stv_clear_gouraud_shading(void);
211 
212 	void stv_clear_framebuffer( int which_framebuffer );
213 	void stv_vdp1_state_save_postload( void );
214 	int stv_vdp1_start ( void );
215 
216 	struct stv_vdp1_poly_scanline
217 	{
218 		int32_t   x[2];
219 		int32_t   b[2];
220 		int32_t   g[2];
221 		int32_t   r[2];
222 		int32_t   db;
223 		int32_t   dg;
224 		int32_t   dr;
225 	};
226 
227 	struct stv_vdp1_poly_scanline_data
228 	{
229 		int32_t   sy, ey;
230 		struct  stv_vdp1_poly_scanline scanline[512];
231 	};
232 
233 	std::unique_ptr<struct stv_vdp1_poly_scanline_data> stv_vdp1_shading_data;
234 
235 	struct stv_vdp2_sprite_list
236 	{
237 		int CMDCTRL, CMDLINK, CMDPMOD, CMDCOLR, CMDSRCA, CMDSIZE, CMDGRDA;
238 		int CMDXA, CMDYA;
239 		int CMDXB, CMDYB;
240 		int CMDXC, CMDYC;
241 		int CMDXD, CMDYD;
242 
243 		int ispoly;
244 
245 	} stv2_current_sprite;
246 
247 	/* Gouraud shading */
248 
249 	struct _stv_gouraud_shading
250 	{
251 		/* Gouraud shading table */
252 		uint16_t  GA;
253 		uint16_t  GB;
254 		uint16_t  GC;
255 		uint16_t  GD;
256 	} stv_gouraud_shading;
257 
258 	uint16_t m_sprite_colorbank;
259 
260 	/* VDP1 Framebuffer handling */
261 	int      stv_sprite_priorities_used[8];
262 	int      stv_sprite_priorities_usage_valid;
263 	uint8_t    stv_sprite_priorities_in_fb_line[512][8];
264 
265 
266 	/* VDP2 */
267 
268 	uint8_t get_vblank( void );
269 	uint8_t get_hblank( void );
270 	int get_hcounter( void );
271 	int get_vcounter( void );
272 	int get_vblank_duration( void );
273 	int get_hblank_duration( void );
274 	int get_pixel_clock( void );
275 	uint8_t get_odd_bit( void );
276 	void stv_vdp2_dynamic_res_change( void );
277 	int get_vblank_start_position( void );
278 	int get_ystep_count( void );
279 
280 	void refresh_palette_data( void );
281 	inline int stv_vdp2_window_process(int x,int y);
282 	void stv_vdp2_get_window0_coordinates(int *s_x, int *e_x, int *s_y, int *e_y, int y);
283 	void stv_vdp2_get_window1_coordinates(int *s_x, int *e_x, int *s_y, int *e_y, int y);
284 	int get_window_pixel(int s_x,int e_x,int s_y,int e_y,int x, int y,uint8_t win_num);
285 	int stv_vdp2_apply_window_on_layer(rectangle &cliprect);
286 
287 	void stv_vdp2_draw_basic_tilemap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
288 	void stv_vdp2_draw_basic_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
289 	void draw_4bpp_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
290 	void draw_8bpp_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
291 	void draw_11bpp_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
292 	void draw_rgb15_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
293 	void draw_rgb32_bitmap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
294 
295 	void stv_vdp2_drawgfxzoom(bitmap_rgb32 &dest_bmp,const rectangle &clip,gfx_element *gfx, uint32_t code,uint32_t color,int flipx,int flipy,int sx,int sy,int transparency,int scalex, int scaley,int sprite_screen_width, int sprite_screen_height, int alpha);
296 	void stv_vdp2_drawgfxzoom_rgb555(bitmap_rgb32 &dest_bmp,const rectangle &clip,uint32_t code,uint32_t color,int flipx,int flipy,int sx,int sy,int transparency,int scalex, int scaley,int sprite_screen_width, int sprite_screen_height, int alpha);
297 	void stv_vdp2_drawgfx_rgb555( bitmap_rgb32 &dest_bmp, const rectangle &clip, uint32_t code, int flipx, int flipy, int sx, int sy, int transparency, int alpha);
298 	void stv_vdp2_drawgfx_rgb888( bitmap_rgb32 &dest_bmp, const rectangle &clip, uint32_t code, int flipx, int flipy, int sx, int sy, int transparency, int alpha);
299 
300 	void stv_vdp2_drawgfx_alpha(bitmap_rgb32 &dest_bmp,const rectangle &clip,gfx_element *gfx, uint32_t code,uint32_t color, int flipx,int flipy,int offsx,int offsy, int transparency, int alpha);
301 	void stv_vdp2_drawgfx_transpen(bitmap_rgb32 &dest_bmp,const rectangle &clip,gfx_element *gfx, uint32_t code,uint32_t color, int flipx,int flipy,int offsx,int offsy, int transparency);
302 
303 
304 	void stv_vdp2_draw_rotation_screen(bitmap_rgb32 &bitmap, const rectangle &cliprect, int iRP);
305 	void stv_vdp2_check_tilemap_with_linescroll(bitmap_rgb32 &bitmap, const rectangle &cliprect);
306 	void stv_vdp2_check_tilemap(bitmap_rgb32 &bitmap, const rectangle &cliprect);
307 	void stv_vdp2_copy_roz_bitmap(bitmap_rgb32 &bitmap, bitmap_rgb32 &roz_bitmap, const rectangle &cliprect, int iRP, int planesizex, int planesizey, int planerenderedsizex, int planerenderedsizey);
308 	inline bool stv_vdp2_roz_window(int x, int y);
309 	inline bool stv_vdp2_roz_mode3_window(int x, int y, int rot_parameter);
310 	inline int get_roz_window_pixel(int s_x,int e_x,int s_y,int e_y,int x, int y,uint8_t winenable,uint8_t winarea);
311 	void stv_vdp2_fill_rotation_parameter_table( uint8_t rot_parameter );
312 	uint8_t stv_vdp2_check_vram_cycle_pattern_registers( uint8_t access_command_pnmdr, uint8_t access_command_cpdr, uint8_t bitmap_enable );
313 	uint8_t stv_vdp2_is_rotation_applied(void);
314 	uint8_t stv_vdp2_are_map_registers_equal(void);
315 	void stv_vdp2_get_map_page( int x, int y, int *_map, int *_page );
316 
317 	void stv_vdp2_draw_mosaic(bitmap_rgb32 &bitmap, const rectangle &cliprect, uint8_t is_roz);
318 	void stv_vdp2_fade_effects( void );
319 	void stv_vdp2_compute_color_offset( int *r, int *g, int *b, int cor );
320 	void stv_vdp2_compute_color_offset_UINT32(rgb_t *rgb, int cor);
321 	void stv_vdp2_check_fade_control_for_layer( void );
322 
323 	void stv_vdp2_draw_line(bitmap_rgb32 &bitmap, const rectangle &cliprect);
324 	void stv_vdp2_draw_back(bitmap_rgb32 &bitmap, const rectangle &cliprect);
325 	void stv_vdp2_draw_NBG0(bitmap_rgb32 &bitmap, const rectangle &cliprect);
326 	void stv_vdp2_draw_NBG1(bitmap_rgb32 &bitmap, const rectangle &cliprect);
327 	void stv_vdp2_draw_NBG2(bitmap_rgb32 &bitmap, const rectangle &cliprect);
328 	void stv_vdp2_draw_NBG3(bitmap_rgb32 &bitmap, const rectangle &cliprect);
329 	void stv_vdp2_draw_RBG0(bitmap_rgb32 &bitmap, const rectangle &cliprect);
330 	void draw_sprites(bitmap_rgb32 &bitmap, const rectangle &cliprect, uint8_t pri);
331 	int true_vcount[263][4];
332 
333 	void stv_vdp2_state_save_postload( void );
334 	void stv_vdp2_exit ( void );
335 	int stv_vdp2_start ( void );
336 
337 	uint8_t m_vdpdebug_roz;
338 
339 	struct stv_vdp2_tilemap_capabilities
340 	{
341 		uint8_t  enabled;
342 		uint8_t  transparency;
343 		uint8_t  colour_calculation_enabled;
344 		uint8_t  colour_depth;
345 		uint8_t  alpha;
346 		uint8_t  tile_size;
347 		uint8_t  bitmap_enable;
348 		uint8_t  bitmap_size;
349 		uint8_t  bitmap_palette_number;
350 		uint8_t  bitmap_map;
351 		uint16_t map_offset[16];
352 		uint8_t  map_count;
353 
354 		uint8_t  pattern_data_size;
355 		uint8_t  character_number_supplement;
356 		uint8_t  special_priority_register;
357 		uint8_t  special_colour_control_register;
358 		uint8_t  supplementary_palette_bits;
359 		uint8_t  supplementary_character_bits;
360 
361 		int16_t scrollx;
362 		int16_t scrolly;
363 		uint32_t incx, incy;
364 
365 		uint8_t   linescroll_enable;
366 		uint8_t   linescroll_interval;
367 		uint32_t  linescroll_table_address;
368 		uint8_t   vertical_linescroll_enable;
369 		uint8_t   vertical_cell_scroll_enable;
370 		uint8_t   linezoom_enable;
371 
372 		uint8_t  plane_size;
373 		uint8_t  colour_ram_address_offset;
374 		uint8_t  fade_control;
375 		struct{
376 			uint8_t logic;
377 			uint8_t enabled[2];
378 			uint8_t area[2];
379 		}window_control;
380 
381 		uint8_t  line_screen_enabled;
382 		uint8_t  mosaic_screen_enabled;
383 		bool roz_mode3;
384 
385 		int layer_name; /* just to keep track */
386 	} stv2_current_tilemap;
387 
388 	struct rotation_table
389 	{
390 		int32_t   xst;
391 		int32_t   yst;
392 		int32_t   zst;
393 		int32_t   dxst;
394 		int32_t   dyst;
395 		int32_t   dx;
396 		int32_t   dy;
397 		int32_t   A;
398 		int32_t   B;
399 		int32_t   C;
400 		int32_t   D;
401 		int32_t   E;
402 		int32_t   F;
403 		int32_t   px;
404 		int32_t   py;
405 		int32_t   pz;
406 		int32_t   cx;
407 		int32_t   cy;
408 		int32_t   cz;
409 		int32_t   mx;
410 		int32_t   my;
411 		int32_t   kx;
412 		int32_t   ky;
413 		uint32_t  kast;
414 		int32_t   dkast;
415 		int32_t   dkax;
416 
417 	} stv_current_rotation_parameter_table;
418 
419 	struct _stv_vdp2_layer_data_placement
420 	{
421 		uint32_t  map_offset_min;
422 		uint32_t  map_offset_max;
423 		uint32_t  tile_offset_min;
424 		uint32_t  tile_offset_max;
425 	} stv_vdp2_layer_data_placement;
426 
427 	struct _stv_rbg_cache_data
428 	{
429 		uint8_t   watch_vdp2_vram_writes;
430 		uint8_t   is_cache_dirty;
431 
432 		uint32_t  map_offset_min[2];
433 		uint32_t  map_offset_max[2];
434 		uint32_t  tile_offset_min[2];
435 		uint32_t  tile_offset_max[2];
436 
437 		struct stv_vdp2_tilemap_capabilities    layer_data[2];
438 
439 	} stv_rbg_cache_data;
440 
441 //  DECLARE_WRITE_LINE_MEMBER(scudsp_end_w);
442 //  uint16_t scudsp_dma_r(offs_t offset);
443 //  void scudsp_dma_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
444 
445 //  void debug_scudma_command(int ref, const std::vector<std::string> &params);
446 //  void debug_scuirq_command(int ref, const std::vector<std::string> &params);
447 //  void debug_help_command(int ref, const std::vector<std::string> &params);
448 //  void debug_commands(int ref, const std::vector<std::string> &params);
449 };
450 
451 
452 // These two clocks are synthesized by the 315-5746
453 #define MASTER_CLOCK_352 XTAL(14'318'181)*4
454 #define MASTER_CLOCK_320 XTAL(14'318'181)*3.75
455 #define CEF_1   m_vdp1_regs[0x010/2]|=0x0002
456 #define CEF_0   m_vdp1_regs[0x010/2]&=~0x0002
457 #define BEF_1   m_vdp1_regs[0x010/2]|=0x0001
458 #define BEF_0   m_vdp1_regs[0x010/2]&=~0x0001
459 #define STV_VDP1_TVMR ((m_vdp1_regs[0x000/2])&0xffff)
460 #define STV_VDP1_VBE  ((STV_VDP1_TVMR & 0x0008) >> 3)
461 #define STV_VDP1_TVM  ((STV_VDP1_TVMR & 0x0007) >> 0)
462 
463 
464 extern gfx_decode_entry const gfx_stv[];
465 
466 #endif // MAME_INCLUDES_SATURN_H
467