1 // license:BSD-3-Clause
2 // copyright-holders:Aaron Giles, Phil Bennett
3 /***************************************************************************
4 
5     Atari Cyberstorm hardware
6 
7 ****************************************************************************/
8 
9 #include "emu.h"
10 #include "includes/cybstorm.h"
11 
12 
13 
14 /*************************************
15  *
16  *  Tilemap callbacks
17  *
18  *************************************/
19 
TILE_GET_INFO_MEMBER(cybstorm_state::get_alpha_tile_info)20 TILE_GET_INFO_MEMBER(cybstorm_state::get_alpha_tile_info)
21 {
22 	uint16_t data = m_vad->alpha().basemem_read(tile_index);
23 	int code = ((data & 0x400) ? (m_alpha_tile_bank * 0x400) : 0) + (data & 0x3ff);
24 	int color = (data >> 11) & 0x0f;
25 	int opaque = data & 0x8000;
26 	tileinfo.set(2, code, color, opaque ? TILEMAP_PIXEL_LAYER0 : 0);
27 }
28 
29 
TILE_GET_INFO_MEMBER(cybstorm_state::get_playfield_tile_info)30 TILE_GET_INFO_MEMBER(cybstorm_state::get_playfield_tile_info)
31 {
32 	uint16_t data1 = m_vad->playfield().basemem_read(tile_index);
33 	uint16_t data2 = m_vad->playfield().extmem_read(tile_index) & 0xff;
34 	int code = data1;
35 	int color = 8 + (data2 & 0x07);
36 	tileinfo.set(0, code, color, data2 & 0x80 ? TILE_FLIPX : 0);
37 	tileinfo.category = (data2 >> 4) & 3;
38 }
39 
40 
TILE_GET_INFO_MEMBER(cybstorm_state::get_playfield2_tile_info)41 TILE_GET_INFO_MEMBER(cybstorm_state::get_playfield2_tile_info)
42 {
43 	uint16_t data1 = m_vad->playfield2().basemem_read(tile_index);
44 	uint16_t data2 = m_vad->playfield2().extmem_read(tile_index) >> 8;
45 	int code = data1;
46 	int color = data2 & 0x07;
47 	tileinfo.set(0, code, color, data2 & 0x80 ? TILE_FLIPX : 0);
48 	tileinfo.category = (data2 >> 4) & 3;
49 }
50 
TILEMAP_MAPPER_MEMBER(cybstorm_state::playfield_scan)51 TILEMAP_MAPPER_MEMBER(cybstorm_state::playfield_scan)
52 {
53 	int bank = 1 - (col / (num_cols / 2));
54 	return bank * (num_rows * num_cols / 2) + row * (num_cols / 2) + (col % (num_cols / 2));
55 }
56 
57 
58 
59 /*************************************
60  *
61  *  Video system start
62  *
63  *************************************/
64 
65 const atari_motion_objects_config cybstorm_state::s_mob_config =
66 {
67 	1,                  /* index to which gfx system */
68 	1,                  /* number of motion object banks */
69 	1,                  /* are the entries linked? */
70 	0,                  /* are the entries split? */
71 	1,                  /* render in reverse order? */
72 	0,                  /* render in swapped X/Y order? */
73 	0,                  /* does the neighbor bit affect the next object? */
74 	8,                  /* pixels per SLIP entry (0 for no-slip) */
75 	0,                  /* pixel offset for SLIPs */
76 	0,                  /* maximum number of links to visit/scanline (0=all) */
77 
78 	0x1000,             /* base palette entry */
79 	0x1000,             /* maximum number of colors */
80 	0,                  /* transparent pen index */
81 
82 	{{ 0x03ff,0,0,0 }}, /* mask for the link */
83 	{{ 0,0x7fff,0,0 }, { 0x3c00,0,0,0 }},   /* mask for the code index */
84 	{{ 0,0,0x000f,0 }}, /* mask for the color */
85 	{{ 0,0,0xff80,0 }}, /* mask for the X position */
86 	{{ 0,0,0,0xff80 }}, /* mask for the Y position */
87 	{{ 0,0,0,0x0070 }}, /* mask for the width, in tiles*/
88 	{{ 0,0,0,0x0007 }}, /* mask for the height, in tiles */
89 	{{ 0,0x8000,0,0 }}, /* mask for the horizontal flip */
90 	{{ 0 }},            /* mask for the vertical flip */
91 	{{ 0,0,0x0070,0 }}, /* mask for the priority */
92 	{{ 0 }},            /* mask for the neighbor */
93 	{{ 0 }},            /* mask for absolute coordinates */
94 
95 	{{ 0 }},            /* mask for the special value */
96 	0,                  /* resulting value to indicate "special" */
97 };
98 
video_start()99 void cybstorm_state::video_start()
100 {
101 	/* motion objects have 256 color granularity */
102 	m_gfxdecode->gfx(1)->set_granularity(256);
103 }
104 
105 
106 
107 /*************************************
108  *
109  *  Main refresh
110  *
111  *************************************/
112 
screen_update_cybstorm(screen_device & screen,bitmap_ind16 & bitmap,const rectangle & cliprect)113 uint32_t cybstorm_state::screen_update_cybstorm(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
114 {
115 	m_vad->mob().draw_async(cliprect);
116 
117 	/* draw the playfield */
118 	bitmap_ind8 &priority_bitmap = screen.priority();
119 	priority_bitmap.fill(0, cliprect);
120 	m_vad->playfield().draw(screen, bitmap, cliprect, 0, 0x00);
121 	m_vad->playfield().draw(screen, bitmap, cliprect, 1, 0x01);
122 	m_vad->playfield().draw(screen, bitmap, cliprect, 2, 0x02);
123 	m_vad->playfield().draw(screen, bitmap, cliprect, 3, 0x03);
124 	m_vad->playfield2().draw(screen, bitmap, cliprect, 0, 0x80);
125 	m_vad->playfield2().draw(screen, bitmap, cliprect, 1, 0x84);
126 	m_vad->playfield2().draw(screen, bitmap, cliprect, 2, 0x88);
127 	m_vad->playfield2().draw(screen, bitmap, cliprect, 3, 0x8c);
128 
129 	/* draw and merge the MO */
130 	bitmap_ind16 &mobitmap = m_vad->mob().bitmap();
131 	for (const sparse_dirty_rect *rect = m_vad->mob().first_dirty_rect(cliprect); rect != nullptr; rect = rect->next())
132 		for (int y = rect->top(); y <= rect->bottom(); y++)
133 		{
134 			uint16_t const *const mo = &mobitmap.pix(y);
135 			uint16_t *const pf = &bitmap.pix(y);
136 			uint8_t const *const pri = &priority_bitmap.pix(y);
137 			for (int x = rect->left(); x <= rect->right(); x++)
138 				if (mo[x])
139 				{
140 					int const mopriority = mo[x] >> atari_motion_objects_device::PRIORITY_SHIFT;
141 
142 					/* upper bit of MO priority signals special rendering and doesn't draw anything */
143 					if (mopriority & 4)
144 					{
145 						if ((mopriority & 0x3) != 0)
146 							continue;
147 					}
148 
149 					/* foreground playfield case */
150 					if (pri[x] & 0x80)
151 					{
152 						int const pfpriority = (pri[x] >> 2) & 3;
153 
154 						if (mopriority > pfpriority)
155 							pf[x] = (mo[x] & atari_motion_objects_device::DATA_MASK) | 0x1000;
156 					}
157 
158 					/* background playfield case */
159 					else
160 					{
161 						int const pfpriority = pri[x] & 3;
162 
163 						/* playfield priority 3 always wins */
164 						if (pfpriority == 3)
165 							;
166 
167 						/* otherwise, MOs get shown */
168 						else
169 							pf[x] = (mo[x] & atari_motion_objects_device::DATA_MASK) | 0x1000;
170 					}
171 
172 					/* don't erase yet -- we need to make another pass later */
173 				}
174 		}
175 
176 	/* now go back and process the upper bit of MO priority */
177 	for (const sparse_dirty_rect *rect = m_vad->mob().first_dirty_rect(cliprect); rect != nullptr; rect = rect->next())
178 		for (int y = rect->top(); y <= rect->bottom(); y++)
179 		{
180 			uint16_t *const mo = &mobitmap.pix(y);
181 			uint16_t *const pf = &bitmap.pix(y);
182 			int count = 0;
183 			for (int x = rect->left(); x <= rect->right() || (count && x < bitmap.width()); x++)
184 			{
185 				const uint16_t START_MARKER = ((4 << atari_motion_objects_device::PRIORITY_SHIFT) | 3);
186 				const uint16_t END_MARKER =   ((4 << atari_motion_objects_device::PRIORITY_SHIFT) | 7);
187 				const uint16_t MASK = ((4 << atari_motion_objects_device::PRIORITY_SHIFT) | 0x3f);
188 
189 				// TODO: Stain pixels should not overlap sprites!
190 				if ((mo[x] & MASK) == START_MARKER)
191 				{
192 					count++;
193 				}
194 
195 				if (count)
196 				{
197 					// Only applies to PF pixels
198 					if ((pf[x] & 0x1000) == 0)
199 					{
200 						pf[x] |= 0x2000;
201 					}
202 				}
203 
204 				if ((mo[x] & MASK) == END_MARKER)
205 				{
206 					count--;
207 				}
208 
209 				/* erase behind ourselves */
210 				mo[x] = 0;
211 			}
212 		}
213 
214 	/* add the alpha on top */
215 	m_vad->alpha().draw(screen, bitmap, cliprect, 0, 0);
216 
217 	return 0;
218 }
219