1 // license:BSD-3-Clause
2 // copyright-holders:David Haywood, Phil Bennett
3 /*************************************************************************
4 
5     Kyuukoukabakugekitai - Dive Bomber Squad
6 
7 *************************************************************************/
8 
9 #include "emu.h"
10 #include "includes/divebomb.h"
11 #include "video/resnet.h"
12 
13 
14 
15 /*************************************
16  *
17  *  Tilemap callbacks
18  *
19  *************************************/
20 
TILE_GET_INFO_MEMBER(divebomb_state::get_fg_tile_info)21 TILE_GET_INFO_MEMBER(divebomb_state::get_fg_tile_info)
22 {
23 	uint32_t code = m_fgram[tile_index + 0x000];
24 	uint32_t attr = m_fgram[tile_index + 0x400];
25 	uint32_t colour = attr >> 4;
26 
27 	code |= (attr & 0x3) << 8;
28 
29 	tileinfo.set(0, code, colour, 0);
30 }
31 
32 
33 
34 /*************************************
35  *
36  *  K051316 callbacks
37  *
38  *************************************/
39 
K051316_CB_MEMBER(divebomb_state::zoom_callback_1)40 K051316_CB_MEMBER(divebomb_state::zoom_callback_1)
41 {
42 	*code |= (*color & 0x03) << 8;
43 	*color = 0 + ((m_roz_pal >> 4) & 3);
44 }
45 
46 
K051316_CB_MEMBER(divebomb_state::zoom_callback_2)47 K051316_CB_MEMBER(divebomb_state::zoom_callback_2)
48 {
49 	*code |= (*color & 0x03) << 8;
50 	*color = 4 + (m_roz_pal & 3);
51 }
52 
53 
54 
55 /*************************************
56  *
57  *  Video hardware handlers
58  *
59  *************************************/
60 
fgram_w(offs_t offset,uint8_t data)61 void divebomb_state::fgram_w(offs_t offset, uint8_t data)
62 {
63 	m_fgram[offset] = data;
64 	m_fg_tilemap->mark_tile_dirty(offset & 0x3ff);
65 }
66 
67 
rozcpu_pal_w(uint8_t data)68 void divebomb_state::rozcpu_pal_w(uint8_t data)
69 {
70 	//.... ..xx  K051316 1 palette select
71 	//..xx ....  K051316 2 palette select
72 
73 	uint8_t old_pal = m_roz_pal;
74 	m_roz_pal = data;
75 
76 	if ((old_pal & 0x03) != (data & 0x03))
77 		m_k051316[1]->mark_tmap_dirty();
78 
79 	if ((old_pal & 0x30) != (data & 0x30))
80 		m_k051316[0]->mark_tmap_dirty();
81 
82 	if (data & 0xcc)
83 		logerror("rozcpu_port50_w %02x\n", data);
84 }
85 
86 
87 
88 /*************************************
89  *
90  *  Video hardware init
91  *
92  *************************************/
93 
decode_proms(palette_device & palette,const uint8_t * rgn,int size,int index,bool inv)94 void divebomb_state::decode_proms(palette_device &palette, const uint8_t * rgn, int size, int index, bool inv)
95 {
96 	static constexpr int resistances[4] = { 2000, 1000, 470, 220 };
97 
98 	// compute the color output resistor weights
99 	double rweights[4], gweights[4], bweights[4];
100 	compute_resistor_weights(0, 255, -1.0,
101 			4, resistances, rweights, 0, 0,
102 			4, resistances, gweights, 0, 0,
103 			4, resistances, bweights, 0, 0);
104 
105 	// create a lookup table for the palette
106 	for (uint32_t i = 0; i < size; ++i)
107 	{
108 		uint32_t const rdata = rgn[i + size*2] & 0x0f;
109 		uint32_t const r = combine_weights(rweights, BIT(rdata, 0), BIT(rdata, 1), BIT(rdata, 2), BIT(rdata, 3));
110 
111 		uint32_t const gdata = rgn[i + size] & 0x0f;
112 		uint32_t const g = combine_weights(gweights, BIT(gdata, 0), BIT(gdata, 1), BIT(gdata, 2), BIT(gdata, 3));
113 
114 		uint32_t const bdata = rgn[i] & 0x0f;
115 		uint32_t const b = combine_weights(bweights, BIT(bdata, 0), BIT(bdata, 1), BIT(bdata, 2), BIT(bdata, 3));
116 
117 		if (!inv)
118 			palette.set_pen_color(index + i, rgb_t(r, g, b));
119 		else
120 			palette.set_pen_color(index + (i ^ 0xff), rgb_t(r, g, b));
121 	}
122 }
123 
124 
divebomb_palette(palette_device & palette) const125 void divebomb_state::divebomb_palette(palette_device &palette) const
126 {
127 	decode_proms(palette, memregion("spr_proms")->base(), 0x100, 0x400 + 0x400 + 0x400, false);
128 	decode_proms(palette, memregion("fg_proms")->base(), 0x400, 0x400 + 0x400, false);
129 	decode_proms(palette, memregion("k051316_1_pr")->base(), 0x400, 0, true);
130 	decode_proms(palette, memregion("k051316_2_pr")->base(), 0x400, 0x400, true);
131 }
132 
133 
VIDEO_START_MEMBER(divebomb_state,divebomb)134 VIDEO_START_MEMBER(divebomb_state,divebomb)
135 {
136 	m_fg_tilemap = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(*this, FUNC(divebomb_state::get_fg_tile_info)), TILEMAP_SCAN_ROWS, 8, 8, 32, 32);
137 	m_fg_tilemap->set_transparent_pen(0);
138 	m_fg_tilemap->set_scrolly(0, 16);
139 }
140 
141 
142 
143 /*************************************
144  *
145  *  Main update
146  *
147  *************************************/
148 
draw_sprites(bitmap_ind16 & bitmap,const rectangle & cliprect)149 void divebomb_state::draw_sprites(bitmap_ind16 &bitmap, const rectangle &cliprect)
150 {
151 	const uint8_t *spriteram = m_spriteram;
152 
153 	for (uint32_t i = 0; i < m_spriteram.bytes(); i += 4)
154 	{
155 		uint32_t sy = spriteram[i + 3];
156 		uint32_t sx = spriteram[i + 0];
157 		uint32_t code = spriteram[i + 2];
158 		uint32_t attr = spriteram[i + 1];
159 
160 		code += (attr & 0x0f) << 8;
161 
162 		uint32_t colour = attr >> 4;
163 
164 		m_gfxdecode->gfx(1)->transpen(bitmap, cliprect, code, colour, 0, 0, sx, sy, 0);
165 		m_gfxdecode->gfx(1)->transpen(bitmap, cliprect, code, colour, 0, 0, sx, sy-256, 0);
166 	}
167 }
168 
169 
screen_update_divebomb(screen_device & screen,bitmap_ind16 & bitmap,const rectangle & cliprect)170 uint32_t divebomb_state::screen_update_divebomb(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
171 {
172 	bitmap.fill(m_palette->black_pen(), cliprect);
173 
174 	for (int chip = 1; chip >= 0; chip--)
175 	{
176 		if (m_roz_enable[chip])
177 		{
178 			m_k051316[chip]->zoom_draw(screen, bitmap, cliprect, 0, 0);
179 		}
180 	}
181 
182 	draw_sprites(bitmap, cliprect);
183 
184 	m_fg_tilemap->draw(screen, bitmap, cliprect, 0, 0);
185 
186 	return 0;
187 }
188