1 /***************************************************************************
2
3 Ninja Gaiden / Tecmo Knights Video Hardware
4
5 ***************************************************************************/
6
7 #include "driver.h"
8 #include "vidhrdw/generic.h"
9
10 unsigned char *gaiden_videoram;
11 unsigned char *gaiden_videoram2;
12 unsigned char *gaiden_videoram3;
13
14 static struct tilemap *text_layer,*foreground,*background;
15
16
17 /***************************************************************************
18
19 Callbacks for the TileMap code
20
21 ***************************************************************************/
22
get_bg_tile_info(int tile_index)23 static void get_bg_tile_info(int tile_index)
24 {
25 UINT16 *videoram1 = (UINT16 *)&gaiden_videoram3[0x1000];
26 UINT16 *videoram2 = (UINT16 *)gaiden_videoram3;
27 SET_TILE_INFO(1,videoram1[tile_index] & 0xfff,(videoram2[tile_index] & 0xf0) >> 4)
28 }
29
get_fg_tile_info(int tile_index)30 static void get_fg_tile_info(int tile_index)
31 {
32 UINT16 *videoram1 = (UINT16 *)&gaiden_videoram2[0x1000];
33 UINT16 *videoram2 = (UINT16 *)gaiden_videoram2;
34 SET_TILE_INFO(2,videoram1[tile_index] & 0xfff,(videoram2[tile_index] & 0xf0) >> 4)
35 }
36
get_tx_tile_info(int tile_index)37 static void get_tx_tile_info(int tile_index)
38 {
39 UINT16 *videoram1 = (UINT16 *)&gaiden_videoram[0x0800];
40 UINT16 *videoram2 = (UINT16 *)gaiden_videoram;
41 SET_TILE_INFO(0,videoram1[tile_index] & 0x7ff,(videoram2[tile_index] & 0xf0) >> 4)
42 }
43
44
45 /***************************************************************************
46
47 Start the video hardware emulation.
48
49 ***************************************************************************/
50
gaiden_vh_start(void)51 int gaiden_vh_start(void)
52 {
53 background = tilemap_create(get_bg_tile_info,tilemap_scan_rows,TILEMAP_TRANSPARENT,16,16,64,32);
54 foreground = tilemap_create(get_fg_tile_info,tilemap_scan_rows,TILEMAP_TRANSPARENT,16,16,64,32);
55 text_layer = tilemap_create(get_tx_tile_info,tilemap_scan_rows,TILEMAP_TRANSPARENT, 8, 8,32,32);
56
57 if (!text_layer || !foreground || !background)
58 return 1;
59
60 background->transparent_pen = 0;
61 foreground->transparent_pen = 0;
62 text_layer->transparent_pen = 0;
63 palette_transparent_color = 0x200; /* background color */
64 return 0;
65 }
66
67
68
69 /***************************************************************************
70
71 Memory handlers
72
73 ***************************************************************************/
74
WRITE_HANDLER(gaiden_txscrollx_w)75 WRITE_HANDLER( gaiden_txscrollx_w )
76 {
77 static UINT16 oldword;
78 oldword = COMBINE_WORD(oldword,data);
79 tilemap_set_scrollx(text_layer,0,oldword);
80 }
81
WRITE_HANDLER(gaiden_txscrolly_w)82 WRITE_HANDLER( gaiden_txscrolly_w )
83 {
84 static UINT16 oldword;
85 oldword = COMBINE_WORD(oldword,data);
86 tilemap_set_scrolly(text_layer,0,oldword);
87 }
88
WRITE_HANDLER(gaiden_fgscrollx_w)89 WRITE_HANDLER( gaiden_fgscrollx_w )
90 {
91 static UINT16 oldword;
92 oldword = COMBINE_WORD(oldword,data);
93 tilemap_set_scrollx(foreground,0,oldword);
94 }
95
WRITE_HANDLER(gaiden_fgscrolly_w)96 WRITE_HANDLER( gaiden_fgscrolly_w )
97 {
98 static UINT16 oldword;
99 oldword = COMBINE_WORD(oldword,data);
100 tilemap_set_scrolly(foreground,0,oldword);
101 }
102
WRITE_HANDLER(gaiden_bgscrollx_w)103 WRITE_HANDLER( gaiden_bgscrollx_w )
104 {
105 static UINT16 oldword;
106 oldword = COMBINE_WORD(oldword,data);
107 tilemap_set_scrollx(background,0,oldword);
108 }
109
WRITE_HANDLER(gaiden_bgscrolly_w)110 WRITE_HANDLER( gaiden_bgscrolly_w )
111 {
112 static UINT16 oldword;
113 oldword = COMBINE_WORD(oldword,data);
114 tilemap_set_scrolly(background,0,oldword);
115 }
116
WRITE_HANDLER(gaiden_videoram3_w)117 WRITE_HANDLER( gaiden_videoram3_w )
118 {
119 int oldword = READ_WORD(&gaiden_videoram3[offset]);
120 int newword = COMBINE_WORD(oldword,data);
121
122 if (oldword != newword)
123 {
124 int tile_index = (offset/2)&0x7ff;
125 WRITE_WORD(&gaiden_videoram3[offset],newword);
126 tilemap_mark_tile_dirty(background,tile_index);
127 }
128 }
129
READ_HANDLER(gaiden_videoram3_r)130 READ_HANDLER( gaiden_videoram3_r )
131 {
132 return READ_WORD(&gaiden_videoram3[offset]);
133 }
134
WRITE_HANDLER(gaiden_videoram2_w)135 WRITE_HANDLER( gaiden_videoram2_w )
136 {
137 int oldword = READ_WORD(&gaiden_videoram2[offset]);
138 int newword = COMBINE_WORD(oldword,data);
139
140 if (oldword != newword)
141 {
142 int tile_index = (offset/2)&0x7ff;
143 WRITE_WORD(&gaiden_videoram2[offset],newword);
144 tilemap_mark_tile_dirty(foreground,tile_index);
145 }
146 }
147
READ_HANDLER(gaiden_videoram2_r)148 READ_HANDLER( gaiden_videoram2_r )
149 {
150 return READ_WORD(&gaiden_videoram2[offset]);
151 }
152
WRITE_HANDLER(gaiden_videoram_w)153 WRITE_HANDLER( gaiden_videoram_w )
154 {
155 int oldword = READ_WORD(&gaiden_videoram[offset]);
156 int newword = COMBINE_WORD(oldword,data);
157
158 if (oldword != newword)
159 {
160 int tile_index = (offset/2)&0x3ff;
161 WRITE_WORD(&gaiden_videoram[offset],newword);
162 tilemap_mark_tile_dirty(text_layer,tile_index);
163 }
164 }
165
READ_HANDLER(gaiden_videoram_r)166 READ_HANDLER( gaiden_videoram_r )
167 {
168 return READ_WORD(&gaiden_videoram[offset]);
169 }
170
171
172
173 /***************************************************************************
174
175 Display refresh
176
177 ***************************************************************************/
178
179 /* sprite format:
180 *
181 * word bit usage
182 * --------+-fedcba9876543210-+----------------
183 * 0 | ---------------x | flip x
184 * | --------------x- | flip y
185 * | -------------x-- | enable
186 * | ----------x----- | flicker
187 * | --------xx------ | sprite-tile priority
188 * 1 | xxxxxxxxxxxxxxxx | number
189 * 2 | --------xxxx---- | palette
190 * | --------------xx | size: 8x8, 16x16, 32x32, 64x64
191 * 3 | xxxxxxxxxxxxxxxx | y position
192 * 4 | xxxxxxxxxxxxxxxx | x position
193 * 5,6,7| | unused
194 */
195
196 #define NUM_SPRITES 128
197
mark_sprite_colors(void)198 static void mark_sprite_colors(void)
199 {
200 const UINT16 *source = (UINT16 *)spriteram;
201 const struct GfxElement *gfx = Machine->gfx[3];
202 int i;
203 for (i = 0;i < NUM_SPRITES;i++)
204 {
205 UINT32 attributes = source[0];
206 if (attributes & 0x04) /* visible */
207 {
208 UINT32 pen_usage = 0xfffe;
209 UINT32 color = (source[2] >> 4) & 0xf;
210 const UINT16 *pal_data = &gfx->colortable[gfx->color_granularity * color];
211 int indx = pal_data - Machine->remapped_colortable;
212 while (pen_usage)
213 {
214 if (pen_usage & 1) palette_used_colors[indx] = PALETTE_COLOR_USED;
215 pen_usage >>= 1;
216 indx++;
217 }
218 }
219 source += 8;
220 }
221 }
222
draw_sprites(struct osd_bitmap * bitmap)223 static void draw_sprites( struct osd_bitmap *bitmap )
224 {
225 const UINT8 layout[8][8] =
226 {
227 {0,1,4,5,16,17,20,21},
228 {2,3,6,7,18,19,22,23},
229 {8,9,12,13,24,25,28,29},
230 {10,11,14,15,26,27,30,31},
231 {32,33,36,37,48,49,52,53},
232 {34,35,38,39,50,51,54,55},
233 {40,41,44,45,56,57,60,61},
234 {42,43,46,47,58,59,62,63}
235 };
236
237 const struct rectangle *clip = &Machine->visible_area;
238 const struct GfxElement *gfx = Machine->gfx[3];
239 const UINT16 *source = (NUM_SPRITES-1)*8 + (UINT16 *)spriteram;
240 int count = NUM_SPRITES;
241
242 /* draw all sprites from front to back */
243 while( count-- )
244 {
245 UINT32 attributes = source[0];
246 if ( (attributes&0x04) && ((attributes&0x20)==0 || (cpu_getcurrentframe() & 1)) )
247 {
248 UINT32 priority = (attributes>>6)&3;
249 UINT32 number = (source[1]&0x7fff);
250 UINT32 color = source[2];
251 UINT32 size = 1<<(color&0x3); // 1,2,4,8
252 UINT32 flipx = (attributes&1);
253 UINT32 flipy = (attributes&2);
254 UINT32 priority_mask;
255 int ypos = source[3] & 0x1ff;
256 int xpos = source[4] & 0x1ff;
257 int col,row;
258 color = (color>>4)&0xf;
259
260 /* wraparound */
261 if( xpos >= 256) xpos -= 512;
262 if( ypos >= 256) ypos -= 512;
263
264 /* bg: 1; fg:2; text: 4 */
265 switch( priority )
266 {
267 default:
268 case 0x0: priority_mask = 0; break;
269 case 0x1: priority_mask = 0xf0; break; /* obscured by text layer */
270 case 0x2: priority_mask = 0xf0|0xcc; break; /* obscured by foreground */
271 case 0x3: priority_mask = 0xf0|0xcc|0xaa; break; /* obscured by bg and fg */
272 }
273
274 for( row=0; row<size; row++ )
275 {
276 for( col=0; col<size; col++ )
277 {
278 int sx = xpos + 8*(flipx?(size-1-col):col);
279 int sy = ypos + 8*(flipy?(size-1-row):row);
280 pdrawgfx(bitmap,gfx,
281 number + layout[row][col],
282 color,
283 flipx,flipy,
284 sx,sy,
285 clip,TRANSPARENCY_PEN,0,
286 priority_mask);
287 }
288 }
289 }
290 source -= 8;
291 }
292 }
293
gaiden_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)294 void gaiden_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
295 {
296 tilemap_update(ALL_TILEMAPS);
297
298 palette_init_used_colors();
299 mark_sprite_colors();
300 palette_used_colors[0x200] = PALETTE_COLOR_USED;
301
302 if (palette_recalc()) tilemap_mark_all_pixels_dirty(ALL_TILEMAPS);
303
304 tilemap_render(ALL_TILEMAPS);
305
306 fillbitmap(priority_bitmap,0,NULL);
307 fillbitmap(bitmap,Machine->pens[0x200],&Machine->visible_area);
308 tilemap_draw(bitmap,background,1<<16);
309 tilemap_draw(bitmap,foreground,2<<16);
310 tilemap_draw(bitmap,text_layer,4<<16);
311
312 draw_sprites( bitmap );
313 }
314