summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/ladybug.cpp
blob: 040c8cd69669024078958c40359eabd2f011c52b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria
/***************************************************************************

  video.c

  Functions to emulate the video hardware of the machine.

***************************************************************************/

#include "emu.h"
#include "ladybug.h"
#include "includes/ladybug.h"

#include "video/resnet.h"

#include <algorithm>


DEFINE_DEVICE_TYPE(LADYBUG_VIDEO,  ladybug_video_device,  "ladybug_video",  "Lady Bug/Space Raider background")
DEFINE_DEVICE_TYPE(ZEROHOUR_STARS, zerohour_stars_device, "zerohour_stars", "Zero Hour/Red Clash/Space Raider starfield")


/***************************************************************************

  ladybug/sraider video

***************************************************************************/

ladybug_video_device::ladybug_video_device(machine_config const &mconfig, char const *tag, device_t *owner, u32 clock)
	: device_t(mconfig, LADYBUG_VIDEO, tag, owner, clock)
	, m_gfxdecode(*this, finder_base::DUMMY_TAG)
	, m_spr_ram()
	, m_bg_ram()
	, m_bg_tilemap(nullptr)
{
}

WRITE8_MEMBER(ladybug_video_device::bg_w)
{
	m_bg_ram[offset & 0x07ff] = data;
	m_bg_tilemap->mark_tile_dirty(offset & 0x03ff);
}

void ladybug_video_device::draw(screen_device &screen, bitmap_ind16 &bitmap, rectangle const &cliprect, bool flip)
{
	// TODO: confirm whether sraider hardware actually does this - not used by the game
	for (unsigned offs = 0; offs < 32; ++offs)
	{
		int const scroll = m_bg_ram[((offs & 0x03) << 5) | (offs >> 2)];
		m_bg_tilemap->set_scrollx(offs, flip ? -scroll : scroll);
	}

	m_bg_tilemap->draw(screen, bitmap, cliprect, 0);

	for (int offs = 0x400 - (0x40 << 1); (0x40 << 1) <= offs; offs -= 0x40)
	{
		int i = 0;
		while ((0x40 > i) && m_spr_ram[offs + i])
			i += 4;

		while (0 < i)
		{
			/*
			 abccdddd eeeeeeee fffghhhh iiiiiiii

			 a enable?
			 b size (0 = 8x8, 1 = 16x16)
			 cc flip
			 dddd y offset
			 eeeeeeee sprite code (shift right 2 bits for 16x16 sprites)
			 fff unknown
			 g sprite bank
			 hhhh color
			 iiiiiiii x position
			*/
			i -= 4;
			bool const enable(m_spr_ram[offs + i] & 0x80);
			if (enable)
			{
				bool const big(m_spr_ram[offs + i] & 0x40);
				bool const xflip(m_spr_ram[offs + i] & 0x20);
				bool const yflip(m_spr_ram[offs + i] & 0x10);
				int const code(m_spr_ram[offs + i + 1] | (BIT(m_spr_ram[offs + i + 2], 4) << 8));
				int const color(m_spr_ram[offs + i + 2] & 0x0f);
				int const xpos(m_spr_ram[offs + i + 3]);
				int const ypos((offs >> 2) | (m_spr_ram[offs + i] & 0x0f));
				if (big) // 16x16
					m_gfxdecode->gfx(1)->transpen(bitmap, cliprect, code >> 2, color, xflip, yflip, xpos, ypos - 8, 0);
				else // 8x8
					m_gfxdecode->gfx(2)->transpen(bitmap, cliprect, code, color, xflip, yflip, xpos, ypos, 0);
			}
		}
	}
}

void ladybug_video_device::device_start()
{
	m_spr_ram = std::make_unique<u8 []>(0x0400);
	m_bg_ram = std::make_unique<u8 []>(0x0800);
	std::fill_n(m_spr_ram.get(), 0x0400, 0);
	std::fill_n(m_bg_ram.get(), 0x0800, 0);

	m_bg_tilemap = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(*this, FUNC(ladybug_video_device::get_bg_tile_info)), TILEMAP_SCAN_ROWS, 8, 8, 32, 32);
	m_bg_tilemap->set_scroll_rows(32);
	m_bg_tilemap->set_transparent_pen(0);

	save_pointer(NAME(m_spr_ram), 0x0400);
	save_pointer(NAME(m_bg_ram), 0x0800);
}

TILE_GET_INFO_MEMBER(ladybug_video_device::get_bg_tile_info)
{
	int const code = m_bg_ram[tile_index] + (BIT(m_bg_ram[0x0400 | tile_index], 3) << 8);
	int const color = m_bg_ram[0x0400 | tile_index] & 0x07;

	SET_TILE_INFO_MEMBER(0, code, color, 0);
}


/***************************************************************************

  zerohour/redclash/sraider stars

  These functions emulate the star generator board
  All this comes from the schematics for Zero Hour

  It has a 17-bit LFSR which has a period of 2^17-1 clocks
  (This is one pixel shy of "two screens" worth.)
  So, there are two starfields drawn on alternate frames
  These will scroll at a rate controlled by the speed register

  I'm basically doing the same thing by drawing each
  starfield on alternate frames, and then offseting them

***************************************************************************/

zerohour_stars_device::zerohour_stars_device(machine_config const &mconfig, char const *tag, device_t *owner, u32 clock)
	: device_t(mconfig, ZEROHOUR_STARS, tag, owner, clock)
	, m_enable(0)
	, m_speed(0)
	, m_offset(0)
	, m_count(0)
{
}

// This line can reset the LFSR to zero and disables the star generator
void zerohour_stars_device::set_enable(bool on)
{
	if (!m_enable && on)
		m_offset = 0;

	m_enable = on ? 1 : 0;
}

// This sets up which starfield to draw and the offset, to be called from screen_vblank_*()
void zerohour_stars_device::update_state()
{
	if (m_enable)
	{
		m_count = m_count ? 0 : 1;
		if (!m_count)
		{
			m_offset += ((m_speed * 2) - 0x09);
			m_offset %= 256 * 256;
			m_state = 0;
		}
		else
		{
			m_state = 0x1fc71;
		}
	}
}

// Set the speed register (3 bits)
void zerohour_stars_device::set_speed(u8 speed, u8 mask)
{
	// 0 left/down fastest (-9/2 pix per frame)
	// 1 left/down faster  (-7/2 pix per frame)
	// 2 left/down fast    (-5/2 pix per frame)
	// 3 left/down medium  (-3/2 pix per frame)
	// 4 left/down slow    (-1/2 pix per frame)
	// 5 right/up slow     (+1/2 pix per frame)
	// 6 right/up medium   (+3/2 pix per frame)
	// 7 right/up fast     (+5/2 pix per frame)
	m_speed = (m_speed & ~mask) | (speed & mask);
}

// Draw the stars
// Space Raider doesn't use the Va bit, and it is also set up to window the stars to a certain x range
void zerohour_stars_device::draw(bitmap_ind16 &bitmap, rectangle const &cliprect, u8 pal_offs, bool has_va, u8 firstx, u8 lastx)
{
	if (m_enable)
	{
		u32 state(m_state);
		for (int i = 0; (256 * 256) > i; ++i)
		{
			u8 const xloc((m_offset + i) & 0x00ff);
			u8 const yloc(((m_offset + i) >> 8) & 0x00ff);

			bool const tempbit(!(state & 0x10000));
			bool const feedback((state & 0x00020) ? !tempbit : tempbit);

			bool const hcond(BIT(xloc + 8, 4));
			bool const vcond(!has_va || BIT(yloc, 0));

			if (cliprect.contains(xloc, yloc) && (hcond == vcond))
			{
				if (((state & 0x000ff) == 0x000ff) && !feedback && (xloc >= firstx) && (xloc <= lastx))
					bitmap.pix16(yloc, xloc) = pal_offs + ((state >> 9) & 0x1f);
			}

			// update LFSR state
			state = ((state << 1) & 0x1fffe) | (feedback ? 1 : 0);
		}
	}
}

void zerohour_stars_device::device_start()
{
	save_item(NAME(m_enable));
	save_item(NAME(m_speed));
	save_item(NAME(m_state));
	save_item(NAME(m_offset));
	save_item(NAME(m_count));
}

void zerohour_stars_device::device_reset()
{
	m_enable = 0;
	m_speed = 0;
	m_state = 0;
	m_offset = 0;
	m_count = 0;
}


/***************************************************************************

  Convert the color PROMs into a more useable format.

  Lady Bug has a 32 bytes palette PROM and a 32 bytes sprite color lookup
  table PROM.
  The palette PROM is connected to the RGB output this way:

  bit 7 -- inverter -- 220 ohm resistor  -- BLUE
        -- inverter -- 220 ohm resistor  -- GREEN
        -- inverter -- 220 ohm resistor  -- RED
        -- inverter -- 470 ohm resistor  -- BLUE
        -- unused
        -- inverter -- 470 ohm resistor  -- GREEN
        -- unused
  bit 0 -- inverter -- 470 ohm resistor  -- RED

***************************************************************************/
/***************************************************************************

  Convert the color PROMs into a more useable format.

  Lady Bug has a 32 bytes palette PROM and a 32 bytes sprite color lookup
  table PROM.
  The palette PROM is connected to the RGB output this way:

  bit 7 -- inverter -- 220 ohm resistor  -- BLUE
        -- inverter -- 220 ohm resistor  -- GREEN
        -- inverter -- 220 ohm resistor  -- RED
        -- inverter -- 470 ohm resistor  -- BLUE
        -- unused
        -- inverter -- 470 ohm resistor  -- GREEN
        -- unused
  bit 0 -- inverter -- 470 ohm resistor  -- RED

***************************************************************************/

void ladybug_base_state::palette_init_common(palette_device &palette, const uint8_t *color_prom,
								int r_bit0, int r_bit1, int g_bit0, int g_bit1, int b_bit0, int b_bit1) const
{
	static constexpr int resistances[2] = { 470, 220 };

	// compute the color output resistor weights
	double rweights[2], gweights[2], bweights[2];
	compute_resistor_weights(0, 255, -1.0,
			2, resistances, rweights, 470, 0,
			2, resistances, gweights, 470, 0,
			2, resistances, bweights, 470, 0);

	// create a lookup table for the palette
	for (int i = 0; i < 0x20; i++)
	{
		int bit0, bit1;

		// red component
		bit0 = BIT(~color_prom[i], r_bit0);
		bit1 = BIT(~color_prom[i], r_bit1);
		int const r = combine_weights(rweights, bit0, bit1);

		// green component
		bit0 = BIT(~color_prom[i], g_bit0);
		bit1 = BIT(~color_prom[i], g_bit1);
		int const g = combine_weights(gweights, bit0, bit1);

		// blue component
		bit0 = BIT(~color_prom[i], b_bit0);
		bit1 = BIT(~color_prom[i], b_bit1);
		int const b = combine_weights(bweights, bit0, bit1);

		palette.set_indirect_color(i, rgb_t(r, g, b));
	}

	// color_prom now points to the beginning of the lookup table
	color_prom += 0x20;

	// characters
	for (int i = 0; i < 0x20; i++)
	{
		uint8_t const ctabentry = ((i << 3) & 0x18) | ((i >> 2) & 0x07);
		palette.set_pen_indirect(i, ctabentry);
	}

	// sprites
	for (int i = 0; i < 0x20; i++)
	{
		uint8_t ctabentry;

		ctabentry = bitswap<4>((color_prom[i] >> 0) & 0x0f, 0,1,2,3);
		palette.set_pen_indirect(i + 0x20, ctabentry);

		ctabentry = bitswap<4>((color_prom[i] >> 4) & 0x0f, 0,1,2,3);
		palette.set_pen_indirect(i + 0x40, ctabentry);
	}
}


void ladybug_state::ladybug_palette(palette_device &palette) const
{
	palette_init_common(palette, memregion("proms")->base(), 0, 5, 2, 6, 4, 7);
}

WRITE_LINE_MEMBER(ladybug_state::flipscreen_w)
{
	if (flip_screen() != state)
	{
		flip_screen_set(state);
		machine().tilemap().mark_all_dirty();
	}
}

uint32_t ladybug_state::screen_update_ladybug(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	bitmap.fill(0, cliprect);
	m_video->draw(screen, bitmap, cliprect, flip_screen());
	return 0;
}


void sraider_state::sraider_palette(palette_device &palette) const
{
	const uint8_t *color_prom = memregion("proms")->base();

	// the resistor net may be probably different than Lady Bug
	palette_init_common(palette, color_prom, 3, 0, 5, 4, 7, 6);

	// star colors
	for (int i = 0; i < 0x20; i++)
	{
		int bit0, bit1;

		// red component
		bit0 = BIT(i, 3);
		bit1 = BIT(i, 4);
		int const b = 0x47 * bit0 + 0x97 * bit1;

		// green component
		bit0 = BIT(i, 1);
		bit1 = BIT(i, 2);
		int const g = 0x47 * bit0 + 0x97 * bit1;

		// blue component
		bit0 = BIT(i, 0);
		int const r = 0x47 * bit0;

		palette.set_indirect_color(i + 0x20, rgb_t(r, g, b));
	}

	for (int i = 0; i < 0x20; i++)
		palette.set_pen_indirect(i + 0x60, i + 0x20);

	// stationary part of grid
	palette.set_pen_indirect(0x81, 0x40);
}

TILE_GET_INFO_MEMBER(sraider_state::get_grid_tile_info)
{
	if (tile_index < 512)
	{
		SET_TILE_INFO_MEMBER(3, tile_index, 0, 0);
	}
	else
	{
		int temp = tile_index / 32;
		tile_index = (31 - temp) * 32 + (tile_index % 32);
		SET_TILE_INFO_MEMBER(4, tile_index, 0, 0);
	}
}

WRITE8_MEMBER(sraider_state::sraider_io_w)
{
	// bit7 = flip
	// bit6 = grid red
	// bit5 = grid green
	// bit4 = grid blue
	// bit3 = enable stars
	// bit210 = stars speed/dir

	if (flip_screen() != (data & 0x80))
	{
		flip_screen_set(data & 0x80);
		machine().tilemap().mark_all_dirty();
	}

	m_grid_color = data & 0x70;

	m_stars->set_enable(BIT(data, 3));

	// There must be a subtle clocking difference between
	// Space Raider and the other games using this star generator,
	// hence the -1 here
	m_stars->set_speed((data & 0x07) - 1, 0x07);
}

void sraider_state::video_start()
{
	ladybug_base_state::video_start();

	m_grid_tilemap = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(*this, FUNC(sraider_state::get_grid_tile_info)), TILEMAP_SCAN_ROWS, 8, 8, 32, 32);
	m_grid_tilemap->set_scroll_rows(32);
	m_grid_tilemap->set_transparent_pen(0);
}

WRITE_LINE_MEMBER(sraider_state::screen_vblank_sraider)/* update starfield position */
{
	// falling edge
	if (!state)
		m_stars->update_state();
}

uint32_t sraider_state::screen_update_sraider(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	// clear the bg bitmap
	bitmap.fill(0, cliprect);

	// draw the stars
	if (flip_screen())
		m_stars->draw(bitmap, cliprect, 0x60, false, 0x27, 0xff);
	else
		m_stars->draw(bitmap, cliprect, 0x60, false, 0x00, 0xd8);

	// draw the gridlines
	m_palette->set_indirect_color(0x40, rgb_t(
			m_grid_color & 0x40 ? 0xff : 0,
			m_grid_color & 0x20 ? 0xff : 0,
			m_grid_color & 0x10 ? 0xff : 0));
	m_grid_tilemap->draw(screen, bitmap, cliprect, 0, flip_screen());

	for (unsigned i = 0; i < 0x100; i++)
	{
		if (m_grid_data[i] != 0)
		{
			uint8_t x = i;
			int height = cliprect.max_y - cliprect.min_y + 1;

			if (flip_screen())
				x = ~x;

			bitmap.plot_box(x, cliprect.min_y, 1, height, 0x81);
		}
	}

	// now the chars/sprites
	m_video->draw(screen, bitmap, cliprect, flip_screen());

	return 0;
}