1 // license:BSD-3-Clause
2 // copyright-holders:Aaron Giles
3 /***************************************************************************
4 
5     Atari Batman hardware
6 
7 ****************************************************************************/
8 
9 #include "emu.h"
10 #include "includes/batman.h"
11 
12 
13 
14 /*************************************
15  *
16  *  Tilemap callbacks
17  *
18  *************************************/
19 
TILE_GET_INFO_MEMBER(batman_state::get_alpha_tile_info)20 TILE_GET_INFO_MEMBER(batman_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 ? TILE_FORCE_LAYER0 : 0);
27 }
28 
29 
TILE_GET_INFO_MEMBER(batman_state::get_playfield_tile_info)30 TILE_GET_INFO_MEMBER(batman_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 & 0x7fff;
35 	int color = 0x10 + (data2 & 0x0f);
36 	tileinfo.set(0, code, color, (data1 >> 15) & 1);
37 	tileinfo.category = (data2 >> 4) & 3;
38 }
39 
40 
TILE_GET_INFO_MEMBER(batman_state::get_playfield2_tile_info)41 TILE_GET_INFO_MEMBER(batman_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 & 0x7fff;
46 	int color = data2 & 0x0f;
47 	tileinfo.set(0, code, color, (data1 >> 15) & 1);
48 	tileinfo.category = (data2 >> 4) & 3;
49 }
50 
51 
52 
53 /*************************************
54  *
55  *  Video system start
56  *
57  *************************************/
58 
59 const atari_motion_objects_config batman_state::s_mob_config =
60 {
61 	1,                  /* index to which gfx system */
62 	1,                  /* number of motion object banks */
63 	1,                  /* are the entries linked? */
64 	0,                  /* are the entries split? */
65 	1,                  /* render in reverse order? */
66 	0,                  /* render in swapped X/Y order? */
67 	0,                  /* does the neighbor bit affect the next object? */
68 	8,                  /* pixels per SLIP entry (0 for no-slip) */
69 	0,                  /* pixel offset for SLIPs */
70 	0,                  /* maximum number of links to visit/scanline (0=all) */
71 
72 	0x100,              /* base palette entry */
73 	0x100,              /* maximum number of colors */
74 	0,                  /* transparent pen index */
75 
76 	{{ 0x03ff,0,0,0 }}, /* mask for the link */
77 	{{ 0,0x7fff,0,0 }}, /* mask for the code index */
78 	{{ 0,0,0x000f,0 }}, /* mask for the color */
79 	{{ 0,0,0xff80,0 }}, /* mask for the X position */
80 	{{ 0,0,0,0xff80 }}, /* mask for the Y position */
81 	{{ 0,0,0,0x0070 }}, /* mask for the width, in tiles*/
82 	{{ 0,0,0,0x0007 }}, /* mask for the height, in tiles */
83 	{{ 0,0x8000,0,0 }}, /* mask for the horizontal flip */
84 	{{ 0 }},            /* mask for the vertical flip */
85 	{{ 0,0,0x0070,0 }}, /* mask for the priority */
86 	{{ 0 }},            /* mask for the neighbor */
87 	{{ 0 }},            /* mask for absolute coordinates */
88 
89 	{{ 0 }},            /* mask for the special value */
90 	0,                  /* resulting value to indicate "special" */
91 };
92 
93 
94 
95 /*************************************
96  *
97  *  Main refresh
98  *
99  *************************************/
100 
screen_update_batman(screen_device & screen,bitmap_ind16 & bitmap,const rectangle & cliprect)101 uint32_t batman_state::screen_update_batman(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
102 {
103 	// start drawing
104 	m_vad->mob().draw_async(cliprect);
105 
106 	/* draw the playfield */
107 	bitmap_ind8 &priority_bitmap = screen.priority();
108 	priority_bitmap.fill(0, cliprect);
109 	m_vad->playfield().draw(screen, bitmap, cliprect, 0, 0x00);
110 	m_vad->playfield().draw(screen, bitmap, cliprect, 1, 0x01);
111 	m_vad->playfield().draw(screen, bitmap, cliprect, 2, 0x02);
112 	m_vad->playfield().draw(screen, bitmap, cliprect, 3, 0x03);
113 	m_vad->playfield2().draw(screen, bitmap, cliprect, 0, 0x80);
114 	m_vad->playfield2().draw(screen, bitmap, cliprect, 1, 0x84);
115 	m_vad->playfield2().draw(screen, bitmap, cliprect, 2, 0x88);
116 	m_vad->playfield2().draw(screen, bitmap, cliprect, 3, 0x8c);
117 
118 	// draw and merge the MO
119 	bitmap_ind16 &mobitmap = m_vad->mob().bitmap();
120 	for (const sparse_dirty_rect *rect = m_vad->mob().first_dirty_rect(cliprect); rect != nullptr; rect = rect->next())
121 		for (int y = rect->top(); y <= rect->bottom(); y++)
122 		{
123 			uint16_t const *const mo = &mobitmap.pix(y);
124 			uint16_t *const pf = &bitmap.pix(y);
125 			uint8_t const *const pri = &priority_bitmap.pix(y);
126 			for (int x = rect->left(); x <= rect->right(); x++)
127 				if (mo[x] != 0xffff)
128 				{
129 					/* verified on real hardware:
130 
131 					    for all MO colors, MO priority 0:
132 					        obscured by low fg playfield pens priority 1-3
133 					        obscured by high fg playfield pens priority 3 only
134 					        obscured by bg playfield priority 3 only
135 
136 					    for all MO colors, MO priority 1:
137 					        obscured by low fg playfield pens priority 2-3
138 					        obscured by high fg playfield pens priority 3 only
139 					        obscured by bg playfield priority 3 only
140 
141 					    for all MO colors, MO priority 2-3:
142 					        obscured by low fg playfield pens priority 3 only
143 					        obscured by high fg playfield pens priority 3 only
144 					        obscured by bg playfield priority 3 only
145 					*/
146 					int const mopriority = mo[x] >> atari_motion_objects_device::PRIORITY_SHIFT;
147 
148 					/* upper bit of MO priority signals special rendering and doesn't draw anything */
149 					if (mopriority & 4)
150 						continue;
151 
152 					/* foreground playfield case */
153 					if (pri[x] & 0x80)
154 					{
155 						int const pfpriority = (pri[x] >> 2) & 3;
156 
157 						/* playfield priority 3 always wins */
158 						if (pfpriority == 3)
159 							;
160 
161 						/* priority is consistent for upper pens in playfield */
162 						else if (pf[x] & 0x08)
163 							pf[x] = mo[x] & atari_motion_objects_device::DATA_MASK;
164 
165 						/* otherwise, we need to compare */
166 						else if (mopriority >= pfpriority)
167 							pf[x] = mo[x] & atari_motion_objects_device::DATA_MASK;
168 					}
169 
170 					/* background playfield case */
171 					else
172 					{
173 						int const pfpriority = pri[x] & 3;
174 
175 						/* playfield priority 3 always wins */
176 						if (pfpriority == 3)
177 							;
178 
179 						/* otherwise, MOs get shown */
180 						else
181 							pf[x] = mo[x] & atari_motion_objects_device::DATA_MASK;
182 					}
183 
184 					/* don't erase yet -- we need to make another pass later */
185 				}
186 		}
187 
188 	/* add the alpha on top */
189 	m_vad->alpha().draw(screen, bitmap, cliprect, 0, 0);
190 
191 	/* now go back and process the upper bit of MO priority */
192 	for (const sparse_dirty_rect *rect = m_vad->mob().first_dirty_rect(cliprect); rect != nullptr; rect = rect->next())
193 		for (int y = rect->top(); y <= rect->bottom(); y++)
194 		{
195 			uint16_t const *const mo = &mobitmap.pix(y);
196 			uint16_t *const pf = &bitmap.pix(y);
197 			for (int x = rect->left(); x <= rect->right(); x++)
198 				if (mo[x] != 0xffff)
199 				{
200 					int const mopriority = mo[x] >> atari_motion_objects_device::PRIORITY_SHIFT;
201 
202 					/* upper bit of MO priority might mean palette kludges */
203 					if (mopriority & 4)
204 					{
205 						/* if bit 2 is set, start setting high palette bits */
206 						if (mo[x] & 2)
207 							m_vad->mob().apply_stain(bitmap, pf, mo, x, y);
208 					}
209 				}
210 		}
211 	return 0;
212 }
213