1 // license:BSD-3-Clause
2 // copyright-holders:Barry Rodewald,Carl
3 /*****************************************************************************
4  *
5  * includes/x68k.h
6  *
7  * Sharp X68000
8  *
9  ****************************************************************************/
10 
11 #ifndef MAME_INCLUDES_X68K_H
12 #define MAME_INCLUDES_X68K_H
13 
14 #pragma once
15 
16 #include "cpu/m68000/m68000.h"
17 #include "imagedev/floppy.h"
18 #include "machine/8530scc.h"
19 #include "machine/hd63450.h"
20 #include "machine/i8255.h"
21 #include "machine/mb89352.h"
22 #include "machine/mc68901.h"
23 #include "machine/ram.h"
24 #include "machine/rp5c15.h"
25 #include "machine/upd765.h"
26 #include "sound/flt_vol.h"
27 #include "sound/okim6258.h"
28 #include "sound/ym2151.h"
29 #include "video/x68k_crtc.h"
30 #include "bus/x68k/x68kexp.h"
31 
32 #include "emupal.h"
33 #include "screen.h"
34 #include "tilemap.h"
35 
36 #define GFX16     0
37 #define GFX256    1
38 #define GFX65536  2
39 
40 class x68k_state : public driver_device
41 {
42 public:
x68k_state(const machine_config & mconfig,device_type type,const char * tag)43 	x68k_state(const machine_config &mconfig, device_type type, const char *tag)
44 		: driver_device(mconfig, type, tag)
45 		, m_maincpu(*this, "maincpu")
46 		, m_okim6258(*this, "okim6258")
47 		, m_hd63450(*this, "hd63450")
48 		, m_ram(*this, RAM_TAG)
49 		, m_crtc(*this, "crtc")
50 		, m_gfxdecode(*this, "gfxdecode")
51 		, m_gfxpalette(*this, "gfxpalette")
52 		, m_pcgpalette(*this, "pcgpalette")
53 		, m_mfpdev(*this, "mc68901")
54 		, m_rtc(*this, "rp5c15")
55 		, m_scc(*this, "scc")
56 		, m_ym2151(*this, "ym2151")
57 		, m_ppi(*this, "ppi8255")
58 		, m_screen(*this, "screen")
59 		, m_upd72065(*this, "upd72065")
60 		, m_expansion(*this, "exp%u", 1U)
61 		, m_adpcm_out(*this, {"adpcm_outl", "adpcm_outr"})
62 		, m_options(*this, "options")
63 		, m_mouse1(*this, "mouse1")
64 		, m_mouse2(*this, "mouse2")
65 		, m_mouse3(*this, "mouse3")
66 		, m_xpd1lr(*this, "xpd1lr")
67 		, m_ctrltype(*this, "ctrltype")
68 		, m_joy1(*this, "joy1")
69 		, m_joy2(*this, "joy2")
70 		, m_md3b(*this, "md3b")
71 		, m_md6b(*this, "md6b")
72 		, m_md6b_extra(*this, "md6b_extra")
73 		, m_eject_drv_out(*this, "eject_drv%u", 0U)
74 		, m_ctrl_drv_out(*this, "ctrl_drv%u", 0U)
75 		, m_access_drv_out(*this, "access_drv%u", 0U)
76 		, m_nvram(0x4000/sizeof(uint16_t))
77 		, m_tvram(0x80000/sizeof(uint16_t))
78 		, m_gvram(0x80000/sizeof(uint16_t))
79 		, m_spritereg(0x8000/sizeof(uint16_t), 0)
80 	{ }
81 
82 	void x68000_base(machine_config &config);
83 	void x68000(machine_config &config);
84 
85 	virtual void driver_init() override;
86 
87 protected:
88 	enum
89 	{
90 		TIMER_X68K_LED,
91 		TIMER_X68K_SCC_ACK,
92 		TIMER_MD_6BUTTON_PORT1_TIMEOUT,
93 		TIMER_MD_6BUTTON_PORT2_TIMEOUT,
94 		TIMER_X68K_BUS_ERROR,
95 		TIMER_X68K_FDC_TC,
96 		TIMER_X68K_ADPCM
97 	};
98 
99 	template <typename CpuType, typename AddrMap, typename Clock>
add_cpu(machine_config & config,CpuType && type,AddrMap && map,Clock && clock)100 	void add_cpu(machine_config &config, CpuType &&type, AddrMap &&map, Clock &&clock)
101 	{
102 		type(config, m_maincpu, std::forward<Clock>(clock));
103 		m_maincpu->set_addrmap(AS_PROGRAM, std::forward<AddrMap>(map));
104 		m_maincpu->set_addrmap(m68000_base_device::AS_CPU_SPACE, &x68k_state::cpu_space_map);
105 	}
106 
107 	required_device<m68000_base_device> m_maincpu;
108 	required_device<okim6258_device> m_okim6258;
109 	required_device<hd63450_device> m_hd63450;
110 	required_device<ram_device> m_ram;
111 	required_device<x68k_crtc_device> m_crtc;
112 	required_device<gfxdecode_device> m_gfxdecode;
113 	required_device<palette_device> m_gfxpalette;
114 	required_device<palette_device> m_pcgpalette;
115 	required_device<mc68901_device> m_mfpdev;
116 	required_device<rp5c15_device> m_rtc;
117 	required_device<scc8530_legacy_device> m_scc;
118 	required_device<ym2151_device> m_ym2151;
119 	required_device<i8255_device> m_ppi;
120 	required_device<screen_device> m_screen;
121 	required_device<upd72065_device> m_upd72065;
122 	required_device_array<x68k_expansion_slot_device, 2> m_expansion;
123 
124 	required_device_array<filter_volume_device, 2> m_adpcm_out;
125 
126 	required_ioport m_options;
127 	required_ioport m_mouse1;
128 	required_ioport m_mouse2;
129 	required_ioport m_mouse3;
130 	required_ioport m_xpd1lr;
131 	required_ioport m_ctrltype;
132 	required_ioport m_joy1;
133 	required_ioport m_joy2;
134 	required_ioport m_md3b;
135 	required_ioport m_md6b;
136 	required_ioport m_md6b_extra;
137 
138 	output_finder<4> m_eject_drv_out;
139 	output_finder<4> m_ctrl_drv_out;
140 	output_finder<4> m_access_drv_out;
141 
142 	std::vector<uint16_t> m_nvram;
143 	std::vector<uint16_t> m_tvram;
144 	std::vector<uint16_t> m_gvram;
145 	std::vector<uint16_t> m_spritereg;
146 
147 	bitmap_ind16 m_pcgbitmap;
148 	bitmap_ind16 m_gfxbitmap;
149 	bitmap_ind16 m_special;
150 
151 	void floppy_load_unload(bool load, floppy_image_device *dev);
152 	image_init_result floppy_load(floppy_image_device *dev);
153 	void floppy_unload(floppy_image_device *dev);
154 	DECLARE_FLOPPY_FORMATS( floppy_formats );
155 
156 	struct
157 	{
158 		int sram_writeprotect;
159 		int monitor;
160 		int contrast;
161 		int keyctrl;
162 		uint16_t cputype;
163 	} m_sysport;
164 	struct
165 	{
166 		floppy_image_device *floppy[4];
167 		int led_ctrl[4];
168 		int led_eject[4];
169 		int eject[4];
170 		int motor;
171 		int control_drives;
172 		int select_drive;
173 	} m_fdc;
174 	struct
175 	{
176 		int ioc7;  // "Function B operation of joystick # one option"
177 		int ioc6;  // "Function A operation of joystick # one option"
178 		int joy1_enable;  // IOC4
179 		int joy2_enable;  // IOC5
180 	} m_joy;
181 	struct
182 	{
183 		int rate;  // ADPCM sample rate
184 		int pan;  // ADPCM output switch
185 		int clock;  // ADPCM clock speed
186 	} m_adpcm;
187 	struct
188 	{   // video controller at 0xe82000
189 		unsigned short reg[3];
190 		int text_pri;
191 		int sprite_pri;
192 		int gfx_pri;
193 		int gfxlayer_pri[4];  // block displayed for each priority level
194 		int tile8_dirty[1024];
195 		int tile16_dirty[256];
196 		int bg_visible_height;
197 		int bg_visible_width;
198 		int bg_hshift;
199 		int bg_vshift;
200 		int bg_hvres;  // bits 0,1 = H-Res, bits 2,3 = V-Res, bit 4 = L/H Freq (0=15.98kHz, 1=31.5kHz)
201 		int bg_double;  // 1 if PCG is to be doubled.
202 	} m_video;
203 	struct
204 	{
205 		uint8_t irqstatus;
206 		uint8_t fdcvector;
207 		uint8_t fddvector;
208 		uint8_t hdcvector;
209 		uint8_t prnvector;
210 	} m_ioc;
211 	struct
212 	{
213 		int inputtype;  // determines which input is to be received
214 		bool irqactive;  // true if IRQ is being serviced
215 		uint8_t irqvector;
216 		char last_mouse_x;  // previous mouse x-axis value
217 		char last_mouse_y;  // previous mouse y-axis value
218 		int bufferempty;  // non-zero if buffer is empty
219 	} m_mouse;
220 	struct
221 	{
222 		// port A
223 		int mux1;  // multiplexer value
224 		int seq1;  // part of 6-button input sequence.
225 		emu_timer* io_timeout1;
226 		// port B
227 		int mux2;  // multiplexer value
228 		int seq2;  // part of 6-button input sequence.
229 		emu_timer* io_timeout2;
230 	} m_mdctrl;
231 	uint8_t m_ppi_port[3];
232 	bool m_dmac_int;
233 	bool m_mfp_int;
234 	bool m_exp_irq2[2];
235 	bool m_exp_irq4[2];
236 	bool m_exp_nmi[2];
237 	uint8_t m_current_ipl;
238 	int m_led_state;
239 	emu_timer* m_mouse_timer;
240 	emu_timer* m_led_timer;
241 	unsigned char m_scc_prev;
242 	uint16_t m_ppi_prev;
243 	emu_timer* m_fdc_tc;
244 	emu_timer* m_adpcm_timer;
245 	emu_timer* m_bus_error_timer;
246 	uint16_t* m_spriteram;
247 	tilemap_t* m_bg0_8;
248 	tilemap_t* m_bg1_8;
249 	tilemap_t* m_bg0_16;
250 	tilemap_t* m_bg1_16;
251 	int m_sprite_shift;
252 	bool m_is_32bit;
253 
254 	TILE_GET_INFO_MEMBER(get_bg0_tile);
255 	TILE_GET_INFO_MEMBER(get_bg1_tile);
256 	TILE_GET_INFO_MEMBER(get_bg0_tile_16);
257 	TILE_GET_INFO_MEMBER(get_bg1_tile_16);
258 	virtual void video_start() override;
259 	uint32_t screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
260 	TIMER_CALLBACK_MEMBER(led_callback);
261 	TIMER_CALLBACK_MEMBER(scc_ack);
262 	TIMER_CALLBACK_MEMBER(md_6button_port1_timeout);
263 	TIMER_CALLBACK_MEMBER(md_6button_port2_timeout);
264 	TIMER_CALLBACK_MEMBER(bus_error);
265 	uint8_t ppi_port_a_r();
266 	uint8_t ppi_port_b_r();
267 	uint8_t ppi_port_c_r();
268 	void ppi_port_c_w(uint8_t data);
269 	DECLARE_WRITE_LINE_MEMBER(fdc_irq);
270 	void ct_w(uint8_t data);
271 	void adpcm_w(offs_t offset, uint8_t data);
272 	DECLARE_WRITE_LINE_MEMBER(mfp_irq_callback);
273 
274 	//dmac
275 	DECLARE_WRITE_LINE_MEMBER(dma_irq);
276 	void dma_end(offs_t offset, uint8_t data);
277 
278 	int read_mouse();
279 	void set_adpcm();
280 	uint8_t md_3button_r(int port);
281 	void md_6button_init();
282 	uint8_t md_6button_r(int port);
283 	uint8_t xpd1lr_r(int port);
284 
285 	DECLARE_WRITE_LINE_MEMBER(fm_irq);
286 	template <int N> DECLARE_WRITE_LINE_MEMBER(irq2_line);
287 	template <int N> DECLARE_WRITE_LINE_MEMBER(irq4_line);
288 	template <int N> DECLARE_WRITE_LINE_MEMBER(nmi_line);
289 
290 	void scc_w(offs_t offset, uint16_t data);
291 	uint16_t scc_r(offs_t offset);
292 	void fdc_w(offs_t offset, uint16_t data);
293 	uint16_t fdc_r(offs_t offset);
294 	void ioc_w(offs_t offset, uint16_t data);
295 	uint16_t ioc_r(offs_t offset);
296 	void sysport_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
297 	uint16_t sysport_r(offs_t offset);
298 	void ppi_w(offs_t offset, uint16_t data);
299 	uint16_t ppi_r(offs_t offset);
300 	void sram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
301 	uint16_t sram_r(offs_t offset);
302 	void vid_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
303 	uint16_t vid_r(offs_t offset);
304 	uint16_t areaset_r();
305 	void areaset_w(uint16_t data);
306 	void enh_areaset_w(offs_t offset, uint16_t data);
307 	uint16_t rom0_r(offs_t offset, uint16_t mem_mask = ~0);
308 	void rom0_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
309 	uint16_t emptyram_r(offs_t offset, uint16_t mem_mask = ~0);
310 	void emptyram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
311 	uint16_t exp_r(offs_t offset, uint16_t mem_mask = ~0);
312 	void exp_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
313 
314 	uint16_t spritereg_r(offs_t offset);
315 	void spritereg_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
316 	uint16_t spriteram_r(offs_t offset);
317 	void spriteram_w(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
318 	uint16_t tvram_read(offs_t offset);
319 	void tvram_write(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
320 	uint16_t gvram_read(offs_t offset);
321 	void gvram_write(offs_t offset, uint16_t data, uint16_t mem_mask = ~0);
322 
323 	void update_ipl();
324 	uint8_t iack1();
325 	uint8_t iack2();
326 	uint8_t iack4();
327 	uint8_t iack5();
328 
329 	void x68k_base_map(address_map &map);
330 	void x68k_map(address_map &map);
331 	void cpu_space_map(address_map &map);
332 
333 	inline void plot_pixel(bitmap_rgb32 &bitmap, int x, int y, uint32_t color);
334 	void draw_text(bitmap_rgb32 &bitmap, int xscr, int yscr, rectangle rect);
335 	bool draw_gfx_scanline(bitmap_ind16 &bitmap, rectangle cliprect, uint8_t priority);
336 	void draw_gfx(bitmap_rgb32 &bitmap,rectangle cliprect);
337 	void draw_sprites(bitmap_ind16 &bitmap, int priority, rectangle cliprect);
338 
339 public:
340 	static rgb_t GGGGGRRRRRBBBBBI(uint32_t raw);
341 
342 protected:
343 	virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override;
344 	virtual void machine_start() override;
345 	virtual void machine_reset() override;
346 	void set_bus_error(uint32_t address, bool write, uint16_t mem_mask);
347 	bool m_bus_error;
348 };
349 
350 class x68ksupr_state : public x68k_state
351 {
352 public:
x68ksupr_state(const machine_config & mconfig,device_type type,const char * tag)353 	x68ksupr_state(const machine_config &mconfig, device_type type, const char *tag)
354 		: x68k_state(mconfig, type, tag)
355 		, m_scsictrl(*this, "mb89352")
356 	{
357 	}
358 
359 	void x68ksupr_base(machine_config &config);
360 	void x68kxvi(machine_config &config);
361 	void x68ksupr(machine_config &config);
362 
363 	virtual void driver_init() override;
364 
365 protected:
366 	DECLARE_WRITE_LINE_MEMBER(scsi_irq);
367 	DECLARE_WRITE_LINE_MEMBER(scsi_drq);
368 
369 	required_device<mb89352_device> m_scsictrl;
370 
371 	void x68kxvi_map(address_map &map);
372 };
373 
374 class x68030_state : public x68ksupr_state
375 {
376 public:
x68030_state(const machine_config & mconfig,device_type type,const char * tag)377 	x68030_state(const machine_config &mconfig, device_type type, const char *tag)
378 		: x68ksupr_state(mconfig, type, tag)
379 	{
380 	}
381 
382 	void x68030(machine_config &config);
383 
384 	virtual void driver_init() override;
385 
386 protected:
387 	void x68030_map(address_map &map);
388 };
389 
390 #endif // MAME_INCLUDES_X68K_H
391