1 // license:BSD-3-Clause
2 // copyright-holders:Barry Rodewald
3 #ifndef MAME_INCLUDES_FMTOWNS_H
4 #define MAME_INCLUDES_FMTOWNS_H
5 
6 #pragma once
7 
8 #include "cpu/i386/i386.h"
9 #include "imagedev/chd_cd.h"
10 #include "imagedev/floppy.h"
11 #include "machine/fm_scsi.h"
12 #include "machine/fmt_icmem.h"
13 #include "machine/nvram.h"
14 #include "machine/pic8259.h"
15 #include "machine/pit8253.h"
16 #include "machine/ram.h"
17 #include "machine/upd71071.h"
18 #include "machine/wd_fdc.h"
19 #include "machine/i8251.h"
20 #include "machine/msm58321.h"
21 #include "sound/2612intf.h"
22 #include "sound/cdda.h"
23 #include "sound/rf5c68.h"
24 #include "sound/spkrdev.h"
25 
26 #include "bus/generic/carts.h"
27 #include "bus/generic/slot.h"
28 #include "bus/rs232/rs232.h"
29 #include "bus/fmt_scsi/fmt_scsi.h"
30 #include "bus/fmt_scsi/fmt121.h"
31 
32 #include "formats/fmtowns_dsk.h"
33 
34 #include "emupal.h"
35 
36 
37 #define IRQ_LOG 0  // set to 1 to log IRQ line activity
38 
39 struct towns_cdrom_controller
40 {
41 	uint8_t command;
42 	uint8_t status;
43 	uint8_t cmd_status[4];
44 	uint8_t cmd_status_ptr;
45 	uint8_t extra_status;
46 	uint8_t parameter[8];
47 	uint8_t mpu_irq_enable;
48 	uint8_t dma_irq_enable;
49 	uint8_t buffer[2048];
50 	int32_t buffer_ptr;
51 	uint32_t lba_current;
52 	uint32_t lba_last;
53 	uint32_t cdda_current;
54 	uint32_t cdda_length;
55 	bool software_tx;
56 	emu_timer* read_timer;
57 };
58 
59 struct towns_video_controller
60 {
61 	uint8_t towns_vram_wplane;
62 	uint8_t towns_vram_rplane;
63 	uint8_t towns_vram_page_sel;
64 	uint8_t towns_palette_select;
65 	uint8_t towns_palette_r[256];
66 	uint8_t towns_palette_g[256];
67 	uint8_t towns_palette_b[256];
68 	uint8_t towns_degipal[8];
69 	uint8_t towns_dpmd_flag;
70 	uint8_t towns_crtc_mix;
71 	uint8_t towns_crtc_sel;  // selected CRTC register
72 	uint16_t towns_crtc_reg[32];
73 	uint8_t towns_video_sel;  // selected video register
74 	uint8_t towns_video_reg[2];
75 	uint8_t towns_sprite_sel;  // selected sprite register
76 	uint8_t towns_sprite_reg[8];
77 	uint8_t towns_sprite_flag;  // sprite drawing flag
78 	uint8_t towns_sprite_page;  // VRAM page (not layer) sprites are drawn to
79 	uint8_t towns_tvram_enable;
80 	uint16_t towns_kanji_offset;
81 	uint8_t towns_kanji_code_h;
82 	uint8_t towns_kanji_code_l;
83 	rectangle towns_crtc_layerscr[2];  // each layer has independent sizes
84 	uint8_t towns_display_plane;
85 	uint8_t towns_display_page_sel;
86 	uint8_t towns_vblank_flag;
87 	uint8_t towns_layer_ctrl;
88 	emu_timer* sprite_timer;
89 };
90 
91 class towns_state : public driver_device
92 {
93 	public:
towns_state(const machine_config & mconfig,device_type type,const char * tag)94 	towns_state(const machine_config &mconfig, device_type type, const char *tag)
95 		: driver_device(mconfig, type, tag)
96 		, m_ram(*this, RAM_TAG)
97 		, m_maincpu(*this, "maincpu")
98 		, m_dma(*this, "dma_%u", 1U)
99 		, m_scsi(*this, "fmscsi")
100 		, m_flop(*this, "fdc:%u", 0U)
101 		, m_speaker(*this, "speaker")
102 		, m_pic_master(*this, "pic8259_master")
103 		, m_pic_slave(*this, "pic8259_slave")
104 		, m_pit(*this, "pit")
105 		, m_palette(*this, "palette256")
106 		, m_palette16(*this, "palette16_%u", 0U)
107 		, m_fdc(*this, "fdc")
108 		, m_icmemcard(*this, "icmemcard")
109 		, m_i8251(*this, "i8251")
110 		, m_rs232(*this, "rs232c")
111 		, m_screen(*this, "screen")
112 		, m_rtc(*this, "rtc58321")
113 		, m_dma_1(*this, "dma_1")
114 		, m_cdrom(*this, "cdrom")
115 		, m_cdda(*this, "cdda")
116 		, m_scsi_slot(*this, "scsislot")
117 		, m_bank_cb000_r(*this, "bank_cb000_r")
118 		, m_bank_cb000_w(*this, "bank_cb000_w")
119 		, m_bank_f8000_r(*this, "bank_f8000_r")
120 		, m_bank_f8000_w(*this, "bank_f8000_w")
121 		, m_nvram(*this, "nvram")
122 		, m_nvram16(*this, "nvram16")
123 		, m_ctrltype(*this, "ctrltype")
124 		, m_kb_ports(*this, "key%u", 1U)
125 		, m_joy1(*this, "joy1")
126 		, m_joy2(*this, "joy2")
127 		, m_joy1_ex(*this, "joy1_ex")
128 		, m_joy2_ex(*this, "joy2_ex")
129 		, m_6b_joy1(*this, "6b_joy1")
130 		, m_6b_joy2(*this, "6b_joy2")
131 		, m_6b_joy1_ex(*this, "6b_joy1_ex")
132 		, m_6b_joy2_ex(*this, "6b_joy2_ex")
133 		, m_mouse1(*this, "mouse1")
134 		, m_mouse2(*this, "mouse2")
135 		, m_mouse3(*this, "mouse3")
136 		, m_user(*this,"user")
137 		, m_serial(*this,"serial")
138 	{ }
139 
140 	void towns_base(machine_config &config);
141 	void towns(machine_config &config);
142 	void townsftv(machine_config &config);
143 	void townshr(machine_config &config);
144 	void townsmx(machine_config &config);
145 	void townssj(machine_config &config);
146 
147 	INTERRUPT_GEN_MEMBER(towns_vsync_irq);
148 
149 protected:
150 	uint16_t m_towns_machine_id;  // default is 0x0101
151 
152 	void marty_mem(address_map &map);
153 	void pcm_mem(address_map &map);
154 	void towns16_io(address_map &map);
155 	void towns_io(address_map &map);
156 	void towns_1g_io(address_map &map);
157 	void towns2_io(address_map &map);
158 	void townsux_io(address_map &map);
159 	void towns_mem(address_map &map);
160 	void ux_mem(address_map &map);
161 
162 	virtual void driver_start() override;
163 
164 	required_device<ram_device> m_ram;
165 	required_device<cpu_device> m_maincpu;
166 
167 	required_device_array<upd71071_device, 2> m_dma;
168 	optional_device<fmscsi_device> m_scsi;
169 	required_device_array<floppy_connector, 2> m_flop;
170 	DECLARE_FLOPPY_FORMATS(floppy_formats);
171 
172 	DECLARE_WRITE_LINE_MEMBER(towns_scsi_irq);
173 	DECLARE_WRITE_LINE_MEMBER(towns_scsi_drq);
174 
175 private:
176 	/* devices */
177 	required_device<speaker_sound_device> m_speaker;
178 	required_device<pic8259_device> m_pic_master;
179 	required_device<pic8259_device> m_pic_slave;
180 	required_device<pit8253_device> m_pit;
181 	required_device<palette_device> m_palette;
182 	required_device_array<palette_device, 2> m_palette16;
183 	required_device<mb8877_device> m_fdc;
184 	required_device<fmt_icmem_device> m_icmemcard;
185 	required_device<i8251_device> m_i8251;
186 	required_device<rs232_port_device> m_rs232;
187 	required_device<screen_device> m_screen;
188 	required_device<msm58321_device> m_rtc;
189 	required_device<upd71071_device> m_dma_1;
190 	required_device<cdrom_image_device> m_cdrom;
191 	required_device<cdda_device> m_cdda;
192 	optional_device<fmt_scsi_slot_device> m_scsi_slot;
193 
194 	required_memory_bank m_bank_cb000_r;
195 	required_memory_bank m_bank_cb000_w;
196 	required_memory_bank m_bank_f8000_r;
197 	required_memory_bank m_bank_f8000_w;
198 
199 	uint16_t m_ftimer;
200 	uint16_t m_freerun_timer;
201 	emu_timer* m_towns_freerun_counter;
202 	uint16_t m_intervaltimer2_period;
203 	uint8_t m_intervaltimer2_irqmask;
204 	uint8_t m_intervaltimer2_timeout_flag;
205 	uint8_t m_intervaltimer2_timeout_flag2;
206 	emu_timer* m_towns_intervaltimer2;
207 	uint8_t m_nmi_mask;
208 	uint8_t m_compat_mode;
209 	uint8_t m_towns_system_port;
210 	uint32_t m_towns_ankcg_enable;
211 	uint32_t m_towns_mainmem_enable;
212 	uint32_t m_towns_ram_enable;
213 	std::unique_ptr<uint32_t[]> m_towns_vram;
214 	std::unique_ptr<uint8_t[]> m_towns_gfxvram;
215 	std::unique_ptr<uint8_t[]> m_towns_txtvram;
216 	int m_towns_selected_drive;
217 	uint8_t m_towns_fdc_irq6mask;
218 	std::unique_ptr<uint8_t[]> m_towns_serial_rom;
219 	int m_towns_srom_position;
220 	uint8_t m_towns_srom_clk;
221 	uint8_t m_towns_srom_reset;
222 	uint8_t m_towns_rtc_select;
223 	uint8_t m_towns_rtc_data;
224 	uint8_t m_towns_timer_mask;
225 	uint8_t m_towns_kb_status;
226 	uint8_t m_towns_kb_irq1_enable;
227 	uint8_t m_towns_kb_output;  // key output
228 	uint8_t m_towns_kb_extend;  // extended key output
229 	emu_timer* m_towns_kb_timer;
230 	emu_timer* m_towns_mouse_timer;
231 	uint8_t m_towns_fm_irq_flag;
232 	uint8_t m_towns_pcm_irq_flag;
233 	uint8_t m_towns_pcm_channel_flag;
234 	uint8_t m_towns_pcm_channel_mask;
235 	uint8_t m_towns_pad_mask;
236 	uint8_t m_towns_mouse_output;
237 	uint8_t m_towns_mouse_x;
238 	uint8_t m_towns_mouse_y;
239 	uint8_t m_towns_volume[4];  // volume ports
240 	uint8_t m_towns_volume_select;
241 	uint8_t m_towns_scsi_control;
242 	uint8_t m_towns_scsi_status;
243 	uint8_t m_towns_spkrdata;
244 	uint8_t m_pit_out0;
245 	uint8_t m_pit_out1;
246 	uint8_t m_pit_out2;
247 	uint8_t m_timer0;
248 	uint8_t m_timer1;
249 
250 	uint8_t m_serial_irq_source;
251 	uint8_t m_serial_irq_enable;  // RS232 interrupt control
252 
253 	enum
254 	{
255 		TXC_EXTERNAL      = 0x80,
256 		RXC_EXTERNAL      = 0x40,
257 		ER_CONTROL        = 0x20,
258 		CI_IRQ_ENABLE     = 0x10,
259 		CS_IRQ_ENABLE     = 0x08,
260 		SYNDET_IRQ_ENABLE = 0x04,
261 		RXRDY_IRQ_ENABLE  = 0x02,
262 		TXRDY_IRQ_ENABLE  = 0x01
263 	};
264 
265 	emu_timer* m_towns_wait_timer;
266 	emu_timer* m_towns_status_timer;
267 	emu_timer* m_towns_cdda_timer;
268 	struct towns_cdrom_controller m_towns_cd;
269 	struct towns_video_controller m_video;
270 
271 	uint32_t m_kb_prev[4];
272 	uint8_t m_prev_pad_mask;
273 	uint8_t m_prev_x;
274 	uint8_t m_prev_y;
275 
276 	optional_shared_ptr<uint32_t> m_nvram;
277 	optional_shared_ptr<uint16_t> m_nvram16;
278 
279 	virtual void machine_start() override;
280 	virtual void machine_reset() override;
281 	virtual void video_start() override;
282 	uint32_t screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
283 	virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override;
284 
285 	uint8_t towns_system_r(offs_t offset);
286 	void towns_system_w(offs_t offset, uint8_t data);
287 	uint8_t towns_intervaltimer2_r(offs_t offset);
288 	void towns_intervaltimer2_w(offs_t offset, uint8_t data);
289 	uint8_t towns_sys6c_r();
290 	void towns_sys6c_w(uint8_t data);
291 	template<int Chip> uint8_t towns_dma_r(offs_t offset);
292 	template<int Chip> void towns_dma_w(offs_t offset, uint8_t data);
293 	uint8_t towns_floppy_r(offs_t offset);
294 	void towns_floppy_w(offs_t offset, uint8_t data);
295 	uint8_t towns_keyboard_r(offs_t offset);
296 	void towns_keyboard_w(offs_t offset, uint8_t data);
297 	uint8_t towns_port60_r();
298 	void towns_port60_w(uint8_t data);
299 	uint8_t towns_sys5e8_r(offs_t offset);
300 	void towns_sys5e8_w(offs_t offset, uint8_t data);
301 	uint8_t towns_sound_ctrl_r(offs_t offset);
302 	void towns_sound_ctrl_w(offs_t offset, uint8_t data);
303 	uint8_t towns_padport_r(offs_t offset);
304 	void towns_pad_mask_w(uint8_t data);
305 	uint8_t towns_cmos_low_r(offs_t offset);
306 	void towns_cmos_low_w(offs_t offset, uint8_t data);
307 	uint8_t towns_cmos_r(offs_t offset);
308 	void towns_cmos_w(offs_t offset, uint8_t data);
309 	uint8_t towns_sys480_r();
310 	void towns_sys480_w(uint8_t data);
311 	uint8_t towns_video_404_r();
312 	void towns_video_404_w(uint8_t data);
313 	uint8_t towns_cdrom_r(offs_t offset);
314 	void towns_cdrom_w(offs_t offset, uint8_t data);
315 	uint8_t towns_rtc_r();
316 	void towns_rtc_w(uint8_t data);
317 	void towns_rtc_select_w(uint8_t data);
318 	uint8_t towns_volume_r(offs_t offset);
319 	void towns_volume_w(offs_t offset, uint8_t data);
320 	uint8_t unksnd_r();
321 	uint8_t towns_41ff_r();
322 
323 	uint8_t towns_gfx_high_r(offs_t offset);
324 	void towns_gfx_high_w(offs_t offset, uint8_t data);
325 	uint8_t towns_gfx_packed_r(offs_t offset);
326 	void towns_gfx_packed_w(offs_t offset, uint8_t data);
327 	uint8_t towns_gfx_r(offs_t offset);
328 	void towns_gfx_w(offs_t offset, uint8_t data);
329 	uint8_t towns_video_cff80_r(offs_t offset);
330 	void towns_video_cff80_w(offs_t offset, uint8_t data);
331 	uint8_t towns_video_cff80_mem_r(offs_t offset);
332 	void towns_video_cff80_mem_w(offs_t offset, uint8_t data);
333 	uint8_t towns_video_440_r(offs_t offset);
334 	void towns_video_440_w(offs_t offset, uint8_t data);
335 	uint8_t towns_video_5c8_r(offs_t offset);
336 	void towns_video_5c8_w(offs_t offset, uint8_t data);
337 	uint8_t towns_video_fd90_r(offs_t offset);
338 	void towns_video_fd90_w(offs_t offset, uint8_t data);
339 	uint8_t towns_video_ff81_r();
340 	uint8_t towns_video_unknown_r();
341 	void towns_video_ff81_w(uint8_t data);
342 	uint8_t towns_spriteram_low_r(offs_t offset);
343 	void towns_spriteram_low_w(offs_t offset, uint8_t data);
344 	uint8_t towns_spriteram_r(offs_t offset);
345 	void towns_spriteram_w(offs_t offset, uint8_t data);
346 
347 	DECLARE_WRITE_LINE_MEMBER(mb8877a_irq_w);
348 	DECLARE_WRITE_LINE_MEMBER(mb8877a_drq_w);
349 	DECLARE_WRITE_LINE_MEMBER(pit_out2_changed);
350 
351 	DECLARE_WRITE_LINE_MEMBER(towns_serial_irq);
352 	DECLARE_WRITE_LINE_MEMBER(towns_rxrdy_irq);
353 	DECLARE_WRITE_LINE_MEMBER(towns_txrdy_irq);
354 	DECLARE_WRITE_LINE_MEMBER(towns_syndet_irq);
355 	uint8_t towns_serial_r(offs_t offset);
356 	void towns_serial_w(offs_t offset, uint8_t data);
357 
358 	DECLARE_WRITE_LINE_MEMBER(rtc_d0_w);
359 	DECLARE_WRITE_LINE_MEMBER(rtc_d1_w);
360 	DECLARE_WRITE_LINE_MEMBER(rtc_d2_w);
361 	DECLARE_WRITE_LINE_MEMBER(rtc_d3_w);
362 	DECLARE_WRITE_LINE_MEMBER(rtc_busy_w);
363 
364 	RF5C68_SAMPLE_END_CB_MEMBER(towns_pcm_irq);
365 
366 	void towns_update_video_banks();
367 	void init_serial_rom();
368 	void kb_sendcode(uint8_t scancode, int release);
369 	uint8_t speaker_get_spk();
370 	void speaker_set_spkrdata(uint8_t data);
371 	uint8_t towns_cdrom_read_byte_software();
372 	void cdda_db_to_gain(float db);
373 
374 	required_ioport m_ctrltype;
375 	required_ioport_array<4> m_kb_ports;
376 	required_ioport m_joy1;
377 	required_ioport m_joy2;
378 	required_ioport m_joy1_ex;
379 	required_ioport m_joy2_ex;
380 	required_ioport m_6b_joy1;
381 	required_ioport m_6b_joy2;
382 	required_ioport m_6b_joy1_ex;
383 	required_ioport m_6b_joy2_ex;
384 	required_ioport m_mouse1;
385 	required_ioport m_mouse2;
386 	required_ioport m_mouse3;
387 	required_memory_region m_user;
388 	optional_memory_region m_serial;
389 
390 	static const device_timer_id TIMER_FREERUN = 1;
391 	static const device_timer_id TIMER_INTERVAL2 = 2;
392 	static const device_timer_id TIMER_KEYBOARD = 3;
393 	static const device_timer_id TIMER_MOUSE = 4;
394 	static const device_timer_id TIMER_WAIT = 5;
395 	static const device_timer_id TIMER_CDSTATUS = 6;
396 	static const device_timer_id TIMER_CDDA = 7;
397 	void freerun_inc();
398 	void intervaltimer2_timeout();
399 	void poll_keyboard();
400 	void mouse_timeout();
401 	void wait_end();
402 	void towns_cd_set_status(uint8_t st0, uint8_t st1, uint8_t st2, uint8_t st3);
403 	void towns_cdrom_execute_command(cdrom_image_device* device);
404 	void towns_cdrom_play_cdda(cdrom_image_device* device);
405 	void towns_cdrom_read(cdrom_image_device* device);
406 	void towns_cd_status_ready();
407 	void towns_delay_cdda(cdrom_image_device* dev);
408 
409 	u8 m_rtc_d;
410 	bool m_rtc_busy;
411 	u8 m_vram_mask[4];
412 	u8 m_vram_mask_addr;
413 
414 	TIMER_CALLBACK_MEMBER(towns_cdrom_read_byte);
415 	TIMER_CALLBACK_MEMBER(towns_sprite_done);
416 	TIMER_CALLBACK_MEMBER(towns_vblank_end);
417 	DECLARE_WRITE_LINE_MEMBER(towns_pit_out0_changed);
418 	DECLARE_WRITE_LINE_MEMBER(towns_pit_out1_changed);
419 	DECLARE_WRITE_LINE_MEMBER(pit2_out1_changed);
420 	uint8_t get_slave_ack(offs_t offset);
421 	DECLARE_WRITE_LINE_MEMBER(towns_fm_irq);
422 	void towns_crtc_refresh_mode();
423 	void towns_update_kanji_offset();
424 	void towns_update_palette();
425 	void render_sprite_4(uint32_t poffset, uint32_t coffset, uint16_t x, uint16_t y, bool xflip, bool yflip, bool xhalfsize, bool yhalfsize, bool rotation, const rectangle* rect);
426 	void render_sprite_16(uint32_t poffset, uint16_t x, uint16_t y, bool xflip, bool yflip, bool xhalfsize, bool yhalfsize, bool rotation, const rectangle* rect);
427 	void draw_sprites(const rectangle* rect);
428 	void towns_crtc_draw_scan_layer_hicolour(bitmap_rgb32 &bitmap,const rectangle* rect,int layer,int line,int scanline);
429 	void towns_crtc_draw_scan_layer_256(bitmap_rgb32 &bitmap,const rectangle* rect,int line,int scanline);
430 	void towns_crtc_draw_scan_layer_16(bitmap_rgb32 &bitmap,const rectangle* rect,int layer,int line,int scanline);
431 	void towns_crtc_draw_layer(bitmap_rgb32 &bitmap,const rectangle* rect,int layer);
432 	void render_text_char(uint8_t x, uint8_t y, uint8_t ascii, uint16_t jis, uint8_t attr);
433 	void draw_text_layer();
434 	inline uint8_t byte_to_bcd(uint8_t val);
435 	inline uint8_t bcd_to_byte(uint8_t val);
436 	inline uint32_t msf_to_lbafm(uint32_t val);  // because the CDROM core doesn't provide this;
437 	uint16_t towns_fdc_dma_r();
438 	void towns_fdc_dma_w(uint16_t data);
439 	void towns_cdrom_set_irq(int line,int state);
440 	uint8_t towns_cd_get_track();
441 	uint16_t towns_cdrom_dma_r();
442 };
443 
444 class towns16_state : public towns_state
445 {
446 	public:
towns16_state(const machine_config & mconfig,device_type type,const char * tag)447 	towns16_state(const machine_config &mconfig, device_type type, const char *tag)
448 		: towns_state(mconfig, type, tag)
449 	{ }
450 	void townsux(machine_config &config);
451 };
452 
453 class marty_state : public towns_state
454 {
455 	public:
marty_state(const machine_config & mconfig,device_type type,const char * tag)456 	marty_state(const machine_config &mconfig, device_type type, const char *tag)
457 		: towns_state(mconfig, type, tag)
458 	{ }
459 
460 	virtual void driver_start() override;
461 	void marty(machine_config &config);
462 };
463 
464 #endif // MAME_INCLUDES_FMTOWNS_H
465