1 // license:BSD-3-Clause
2 // copyright-holders:David Haywood, Pierpaolo Prazzoli
3 #include "emu.h"
4 #include "includes/scotrsht.h"
5 
6 
7 // Similar as Iron Horse
scotrsht_palette(palette_device & palette) const8 void scotrsht_state::scotrsht_palette(palette_device &palette) const
9 {
10 	const uint8_t *color_prom = memregion("proms")->base();
11 
12 	// create a lookup table for the palette
13 	for (int i = 0; i < 0x100; i++)
14 	{
15 		int const r = pal4bit(color_prom[i | 0x000]);
16 		int const g = pal4bit(color_prom[i | 0x100]);
17 		int const b = pal4bit(color_prom[i | 0x200]);
18 
19 		palette.set_indirect_color(i, rgb_t(r, g, b));
20 	}
21 
22 	// color_prom now points to the beginning of the lookup table
23 	color_prom += 0x300;
24 
25 	// characters use colors 0x80-0xff, sprites use colors 0-0x7f
26 	for (int i = 0; i < 0x200; i++)
27 	{
28 		for (int j = 0; j < 8; j++)
29 		{
30 			uint8_t const ctabentry = ((~i & 0x100) >> 1) | (j << 4) | (color_prom[i] & 0x0f);
31 			palette.set_pen_indirect(((i & 0x100) << 3) | (j << 8) | (i & 0xff), ctabentry);
32 		}
33 	}
34 }
35 
videoram_w(offs_t offset,uint8_t data)36 void scotrsht_state::videoram_w(offs_t offset, uint8_t data)
37 {
38 	m_videoram[offset] = data;
39 	m_bg_tilemap->mark_tile_dirty(offset);
40 }
41 
colorram_w(offs_t offset,uint8_t data)42 void scotrsht_state::colorram_w(offs_t offset, uint8_t data)
43 {
44 	m_colorram[offset] = data;
45 	m_bg_tilemap->mark_tile_dirty(offset);
46 }
47 
charbank_w(uint8_t data)48 void scotrsht_state::charbank_w(uint8_t data)
49 {
50 	if (m_charbank != (data & 0x01))
51 	{
52 		m_charbank = data & 0x01;
53 		m_bg_tilemap->mark_all_dirty();
54 	}
55 
56 	// other bits unknown
57 }
58 
palettebank_w(uint8_t data)59 void scotrsht_state::palettebank_w(uint8_t data)
60 {
61 	if (m_palette_bank != ((data & 0x70) >> 4))
62 	{
63 		m_palette_bank = ((data & 0x70) >> 4);
64 		m_bg_tilemap->mark_all_dirty();
65 	}
66 
67 	machine().bookkeeping().coin_counter_w(0, data & 1);
68 	machine().bookkeeping().coin_counter_w(1, data & 2);
69 
70 	// data & 4 unknown
71 }
72 
73 
TILE_GET_INFO_MEMBER(scotrsht_state::get_bg_tile_info)74 TILE_GET_INFO_MEMBER(scotrsht_state::get_bg_tile_info)
75 {
76 	int attr = m_colorram[tile_index];
77 	int code = m_videoram[tile_index] + (m_charbank << 9) + ((attr & 0x40) << 2);
78 	int color = (attr & 0x0f) + m_palette_bank * 16;
79 	int flag = 0;
80 
81 	if(attr & 0x10) flag |= TILE_FLIPX;
82 	if(attr & 0x20) flag |= TILE_FLIPY;
83 
84 	// data & 0x80 -> tile priority?
85 
86 	tileinfo.set(0, code, color, flag);
87 }
88 
89 /* Same as Jailbreak + palette bank */
draw_sprites(bitmap_ind16 & bitmap,const rectangle & cliprect)90 void scotrsht_state::draw_sprites(bitmap_ind16 &bitmap, const rectangle &cliprect )
91 {
92 	for (int i = 0; i < m_spriteram.bytes(); i += 4)
93 	{
94 		int attr = m_spriteram[i + 1];    // attributes = ?tyxcccc
95 		int code = m_spriteram[i] + ((attr & 0x40) << 2);
96 		int color = (attr & 0x0f) + m_palette_bank * 16;
97 		int flipx = attr & 0x10;
98 		int flipy = attr & 0x20;
99 		int sx = m_spriteram[i + 2] - ((attr & 0x80) << 1);
100 		int sy = m_spriteram[i + 3];
101 
102 		if (flip_screen())
103 		{
104 			sx = 240 - sx;
105 			sy = 240 - sy;
106 			flipx = !flipx;
107 			flipy = !flipy;
108 		}
109 
110 		m_gfxdecode->gfx(1)->transmask(bitmap,cliprect, code, color, flipx, flipy,
111 			sx, sy,
112 			m_palette->transpen_mask(*m_gfxdecode->gfx(1), color, m_palette_bank * 16));
113 	}
114 }
115 
video_start()116 void scotrsht_state::video_start()
117 {
118 	m_bg_tilemap = &machine().tilemap().create(*m_gfxdecode, tilemap_get_info_delegate(*this, FUNC(scotrsht_state::get_bg_tile_info)), TILEMAP_SCAN_ROWS, 8, 8, 64, 32);
119 
120 	m_bg_tilemap->set_scroll_cols(64);
121 
122 	save_item(NAME(m_irq_enable));
123 	save_item(NAME(m_charbank));
124 	save_item(NAME(m_palette_bank));
125 }
126 
screen_update(screen_device & screen,bitmap_ind16 & bitmap,const rectangle & cliprect)127 uint32_t scotrsht_state::screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
128 {
129 	for (int col = 0; col < 32; col++)
130 		m_bg_tilemap->set_scrolly(col, m_scroll[col]);
131 
132 	m_bg_tilemap->draw(screen, bitmap, cliprect, 0, 0);
133 	draw_sprites(bitmap, cliprect);
134 	return 0;
135 }
136