1 // license:BSD-3-Clause
2 // copyright-holders:Bryan McPhail,Ernesto Corvi,Andrew Prime,Zsolt Vasvari
3 // thanks-to:Fuzz
4 /*************************************************************************
5 
6     Neo-Geo hardware
7 
8 *************************************************************************/
9 #ifndef MAME_INCLUDES_NEOGEO_H
10 #define MAME_INCLUDES_NEOGEO_H
11 
12 #pragma once
13 
14 #include "cpu/m68000/m68000.h"
15 #include "cpu/z80/z80.h"
16 #include "sound/2610intf.h"
17 #include "machine/74259.h"
18 #include "machine/gen_latch.h"
19 #include "machine/input_merger.h"
20 #include "machine/upd1990a.h"
21 #include "machine/ng_memcard.h"
22 #include "video/neogeo_spr.h"
23 
24 #include "bus/neogeo/slot.h"
25 #include "bus/neogeo/carts.h"
26 #include "bus/neogeo_ctrl/ctrl.h"
27 
28 #include "emupal.h"
29 #include "screen.h"
30 
31 
32 // On scanline 224, /VBLANK goes low 56 mclks (14 pixels) from the rising edge of /HSYNC.
33 // Two mclks after /VBLANK goes low, the hardware sets a pending IRQ1 flip-flop.
34 #define NEOGEO_VBLANK_IRQ_HTIM (attotime::from_ticks(56+2, NEOGEO_MASTER_CLOCK))
35 
36 
37 class neogeo_base_state : public driver_device
38 {
39 public:
40 	DECLARE_CUSTOM_INPUT_MEMBER(get_memcard_status);
41 	DECLARE_CUSTOM_INPUT_MEMBER(get_audio_result);
42 
43 protected:
neogeo_base_state(const machine_config & mconfig,device_type type,const char * tag)44 	neogeo_base_state(const machine_config &mconfig, device_type type, const char *tag)
45 		: driver_device(mconfig, type, tag)
46 		, m_maincpu(*this, "maincpu")
47 		, m_audiocpu(*this, "audiocpu")
48 		, m_ym(*this, "ymsnd")
49 		, m_sprgen(*this, "spritegen")
50 		, m_screen(*this, "screen")
51 		, m_palette(*this, "palette")
52 		, m_memcard(*this, "memcard")
53 		, m_systemlatch(*this, "systemlatch")
54 		, m_soundlatch(*this, "soundlatch")
55 		, m_soundlatch2(*this, "soundlatch2")
56 		, m_region_maincpu(*this, "maincpu")
57 		, m_region_sprites(*this, "sprites")
58 		, m_region_fixed(*this, "fixed")
59 		, m_region_fixedbios(*this, "fixedbios")
60 		, m_region_mainbios(*this, "mainbios")
61 		, m_region_audiobios(*this, "audiobios")
62 		, m_region_audiocpu(*this, "audiocpu")
63 		, m_bank_audio_main(*this, "audio_main")
64 		, m_edge(*this, "edge")
65 		, m_ctrl1(*this, "ctrl1")
66 		, m_ctrl2(*this, "ctrl2")
67 		, m_use_cart_vectors(0)
68 		, m_use_cart_audio(0)
69 		, m_slots(*this, "cslot%u", 1U)
70 		, m_audionmi(*this, "audionmi")
71 	{ }
72 
73 	uint16_t memcard_r(offs_t offset);
74 	void memcard_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
75 	uint8_t audio_cpu_bank_select_r(offs_t offset);
76 	void audio_cpu_enable_nmi_w(offs_t offset, uint8_t data);
77 	uint16_t unmapped_r(address_space &space);
78 	uint16_t paletteram_r(offs_t offset);
79 	void paletteram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
80 	uint16_t video_register_r(address_space &space, offs_t offset, uint16_t mem_mask = ~0);
81 	void video_register_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
82 
83 	TIMER_CALLBACK_MEMBER(display_position_interrupt_callback);
84 	TIMER_CALLBACK_MEMBER(display_position_vblank_callback);
85 	TIMER_CALLBACK_MEMBER(vblank_interrupt_callback);
86 
87 	uint32_t screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
88 
89 	virtual void io_control_w(offs_t offset, uint8_t data);
90 	void audio_command_w(uint8_t data);
91 	DECLARE_WRITE_LINE_MEMBER(set_use_cart_vectors);
92 	DECLARE_WRITE_LINE_MEMBER(set_use_cart_audio);
93 	uint16_t banked_vectors_r(offs_t offset);
94 	void write_banksel(uint16_t data);
95 	void write_bankprot(uint16_t data);
96 	void write_bankprot_pvc(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
97 	void write_bankprot_ms5p(offs_t offset, uint16_t data);
98 	void write_bankprot_kf2k3bl(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
99 	void write_bankprot_kof10th(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
100 	uint16_t read_lorom_kof10th(offs_t offset);
101 
102 	DECLARE_WRITE_LINE_MEMBER(set_screen_shadow);
103 	DECLARE_WRITE_LINE_MEMBER(set_palette_bank);
104 
105 	void neogeo_base(machine_config &config);
106 	void neogeo_stereo(machine_config &config);
107 
108 	void base_main_map(address_map &map);
109 	void audio_io_map(address_map &map);
110 	void audio_map(address_map &map);
111 
112 	// device overrides
113 	virtual void machine_start() override;
114 	virtual void machine_reset() override;
115 
116 	virtual void device_post_load() override;
117 
118 	// devices
119 	required_device<cpu_device> m_maincpu;
120 	required_device<cpu_device> m_audiocpu;
121 	// MVS-specific devices
122 	optional_device<ym2610_device> m_ym;
123 	required_device<neosprite_optimized_device> m_sprgen;
124 
125 	required_device<screen_device> m_screen;
126 	optional_device<palette_device> m_palette;
127 	optional_device<ng_memcard_device> m_memcard;
128 	required_device<hc259_device> m_systemlatch;
129 	required_device<generic_latch_8_device> m_soundlatch;
130 	required_device<generic_latch_8_device> m_soundlatch2;
131 
132 	// memory
133 	optional_memory_region m_region_maincpu;
134 	optional_memory_region m_region_sprites;
135 	optional_memory_region m_region_fixed;
136 	optional_memory_region m_region_fixedbios;
137 	optional_memory_region m_region_mainbios;
138 	optional_memory_region m_region_audiobios;
139 	optional_memory_region m_region_audiocpu;
140 	optional_memory_bank   m_bank_audio_main; // optional because of neocd
141 	memory_bank           *m_bank_audio_cart[4];
142 	memory_bank           *m_bank_cartridge;
143 
144 	optional_device<neogeo_ctrl_edge_port_device> m_edge;
145 	optional_device<neogeo_control_port_device> m_ctrl1;
146 	optional_device<neogeo_control_port_device> m_ctrl2;
147 
148 	// video hardware, including maincpu interrupts
149 	// TODO: make into a device
150 	virtual void video_start() override;
151 	virtual void video_reset() override;
152 
153 	const pen_t *m_bg_pen;
154 	uint8_t      m_vblank_level;
155 	uint8_t      m_raster_level;
156 
157 	int m_use_cart_vectors;
158 	int m_use_cart_audio;
159 
160 	void set_slot_idx(int slot);
161 
162 	// cart slots
163 	void init_cpu();
164 	void init_audio();
165 	void init_ym();
166 	void init_sprites();
167 	// temporary helper to restore memory banking while bankswitch is handled in the driver...
168 	uint32_t m_bank_base;
169 
170 	optional_device_array<neogeo_cart_slot_device, 6> m_slots;
171 
172 	int m_curr_slot;
173 
174 private:
175 	void update_interrupts();
176 	void create_interrupt_timers();
177 	void start_interrupt_timers();
178 	void acknowledge_interrupt(uint16_t data);
179 
180 	void adjust_display_position_interrupt_timer();
181 	void set_display_position_interrupt_control(uint16_t data);
182 	void set_display_counter_msb(uint16_t data);
183 	void set_display_counter_lsb(uint16_t data);
184 	void set_video_control(uint16_t data);
185 
186 	void create_rgb_lookups();
187 	void set_pens();
188 
189 	// internal state
190 	bool       m_recurse;
191 
192 	emu_timer  *m_display_position_interrupt_timer;
193 	emu_timer  *m_display_position_vblank_timer;
194 	emu_timer  *m_vblank_interrupt_timer;
195 	uint32_t     m_display_counter;
196 	uint8_t      m_vblank_interrupt_pending;
197 	uint8_t      m_display_position_interrupt_pending;
198 	uint8_t      m_irq3_pending;
199 	uint8_t      m_display_position_interrupt_control;
200 
201 	uint16_t get_video_control();
202 
203 	required_device<input_merger_device> m_audionmi;
204 
205 	// color/palette related
206 	std::vector<uint16_t> m_paletteram;
207 	uint8_t      m_palette_lookup[32][4];
208 	int          m_screen_shadow;
209 	int          m_palette_bank;
210 };
211 
212 
213 class ngarcade_base_state : public neogeo_base_state
214 {
215 public:
216 	DECLARE_CUSTOM_INPUT_MEMBER(startsel_edge_joy_r);
217 
218 protected:
ngarcade_base_state(const machine_config & mconfig,device_type type,const char * tag)219 	ngarcade_base_state(const machine_config &mconfig, device_type type, const char *tag)
220 		: neogeo_base_state(mconfig, type, tag)
221 		, m_save_ram(*this, "saveram")
222 		, m_upd4990a(*this, "upd4990a")
223 		, m_dsw(*this, "DSW")
224 	{
225 	}
226 
227 	virtual void machine_start() override;
228 	virtual void machine_reset() override;
229 
230 	virtual void io_control_w(offs_t offset, uint8_t data) override;
231 	DECLARE_WRITE_LINE_MEMBER(set_save_ram_unlock);
232 	void save_ram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
233 	uint16_t in0_edge_r();
234 	uint16_t in0_edge_joy_r();
235 	uint16_t in1_edge_r();
236 	uint16_t in1_edge_joy_r();
237 
238 	void neogeo_arcade(machine_config &config);
239 	void neogeo_mono(machine_config &config);
240 
241 	void neogeo_main_map(address_map &map);
242 
243 private:
244 	required_shared_ptr<uint16_t> m_save_ram;
245 	required_device<upd4990a_device> m_upd4990a;
246 	required_ioport m_dsw;
247 
248 	uint8_t m_save_ram_unlocked;
249 };
250 
251 
252 class aes_base_state : public neogeo_base_state
253 {
254 public:
255 	DECLARE_INPUT_CHANGED_MEMBER(aes_jp1);
256 
257 protected:
aes_base_state(const machine_config & mconfig,device_type type,const char * tag)258 	aes_base_state(const machine_config &mconfig, device_type type, const char *tag)
259 		: neogeo_base_state(mconfig, type, tag)
260 		, m_io_in2(*this, "IN2")
261 	{
262 	}
263 
264 	uint16_t aes_in2_r();
265 
266 	virtual void machine_start() override;
267 
268 	void aes_base_main_map(address_map &map);
269 
270 private:
271 	required_ioport m_io_in2;
272 };
273 
274 
275 /*----------- defined in drivers/neogeo.c -----------*/
276 
277 INPUT_PORTS_EXTERN(neogeo);
278 INPUT_PORTS_EXTERN(aes);
279 
280 #endif // MAME_INCLUDES_NEOGEO_H
281