1 // license:BSD-3-Clause
2 // copyright-holders:AJR
3 /************************************************************************************************************
4 
5 Control Data Corporation CDC 721 Terminal (Viking)
6 
7 2013-08-13 Skeleton
8 
9 
10 *************************************************************************************************************/
11 
12 #include "emu.h"
13 #include "bus/rs232/rs232.h"
14 #include "cpu/z80/z80.h"
15 #include "machine/bankdev.h"
16 #include "machine/i8255.h"
17 #include "machine/input_merger.h"
18 #include "machine/ins8250.h"
19 #include "machine/nvram.h"
20 #include "machine/output_latch.h"
21 #include "machine/z80ctc.h"
22 #include "video/tms9927.h"
23 #include "emupal.h"
24 #include "screen.h"
25 
26 
27 class cdc721_state : public driver_device
28 {
29 public:
cdc721_state(const machine_config & mconfig,device_type type,const char * tag)30 	cdc721_state(const machine_config &mconfig, device_type type, const char *tag)
31 		: driver_device(mconfig, type, tag)
32 		, m_maincpu(*this, "maincpu")
33 		, m_bank_16k(*this, {"block0", "block4", "block8", "blockc"})
34 		, m_crtc(*this, "crtc")
35 		, m_rom_chargen(*this, "chargen")
36 		, m_ram_chargen(*this, "chargen")
37 		, m_videoram(*this, "videoram")
38 		, m_nvram(*this, "nvram")
39 	{ }
40 
41 	void cdc721(machine_config &config);
42 
43 protected:
44 	virtual void machine_start() override;
45 	virtual void machine_reset() override;
46 
47 private:
48 	uint32_t screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect);
49 	void cdc721_palette(palette_device &palette) const;
50 	void interrupt_mask_w(u8 data);
51 	void misc_w(u8 data);
52 	void block_select_w(u8 data);
53 	void nvram_w(offs_t offset, u8 data);
54 
55 	template<int Line> DECLARE_WRITE_LINE_MEMBER(int_w);
56 	TIMER_CALLBACK_MEMBER(update_interrupts);
57 	IRQ_CALLBACK_MEMBER(restart_cb);
58 
59 	template<int Bit> DECLARE_WRITE_LINE_MEMBER(foreign_char_bank_w);
60 
61 	void io_map(address_map &map);
62 	void mem_map(address_map &map);
63 	void block0_map(address_map &map);
64 	void block4_map(address_map &map);
65 	void block8_map(address_map &map);
66 	void blockc_map(address_map &map);
67 
68 	u8 m_flashcnt;
69 	u8 m_foreign_char_bank;
70 
71 	u8 m_pending_interrupts;
72 	u8 m_active_interrupts;
73 	u8 m_interrupt_mask;
74 
75 	required_device<cpu_device> m_maincpu;
76 	required_device_array<address_map_bank_device, 4> m_bank_16k;
77 	required_device<crt5037_device> m_crtc;
78 	required_region_ptr<u8> m_rom_chargen;
79 	required_shared_ptr<u8> m_ram_chargen;
80 	required_shared_ptr<u8> m_videoram;
81 	required_shared_ptr<u8> m_nvram;
82 };
83 
interrupt_mask_w(u8 data)84 void cdc721_state::interrupt_mask_w(u8 data)
85 {
86 	m_interrupt_mask = data ^ 0xff;
87 	machine().scheduler().synchronize(timer_expired_delegate(FUNC(cdc721_state::update_interrupts), this));
88 }
89 
90 template <int Line>
WRITE_LINE_MEMBER(cdc721_state::int_w)91 WRITE_LINE_MEMBER(cdc721_state::int_w)
92 {
93 	if (BIT(m_pending_interrupts, Line) == state)
94 		return;
95 
96 	if (state)
97 		m_pending_interrupts |= 0x01 << Line;
98 	else
99 		m_pending_interrupts &= ~(0x01 << Line);
100 
101 	machine().scheduler().synchronize(timer_expired_delegate(FUNC(cdc721_state::update_interrupts), this));
102 }
103 
TIMER_CALLBACK_MEMBER(cdc721_state::update_interrupts)104 TIMER_CALLBACK_MEMBER(cdc721_state::update_interrupts)
105 {
106 	m_active_interrupts = m_pending_interrupts & m_interrupt_mask;
107 	m_maincpu->set_input_line(INPUT_LINE_IRQ0, m_active_interrupts != 0 ? ASSERT_LINE : CLEAR_LINE);
108 }
109 
IRQ_CALLBACK_MEMBER(cdc721_state::restart_cb)110 IRQ_CALLBACK_MEMBER(cdc721_state::restart_cb)
111 {
112 	// IM 2 vector is produced through a SN74LS148N priority encoder plus some buffers and a latch
113 	// The CTC vector is not even fetched
114 	u8 vector = 0x00;
115 	u8 active = m_active_interrupts;
116 	while (vector < 0x0e && !BIT(active, 0))
117 	{
118 		active >>= 1;
119 		vector += 0x02;
120 	}
121 	return vector;
122 }
123 
misc_w(u8 data)124 void cdc721_state::misc_w(u8 data)
125 {
126 	// 7: Stop Refresh Operation
127 	// 6: Enable RAM Char Gen
128 	// 5: Enable Block Cursor
129 	// 4: Reverse Video
130 	// 3: 132/80
131 	// 2: Enable Blink
132 	// 1: Enable Blinking Cursor
133 	// 0: Alarm
134 
135 	logerror("%s: %d-column display selected\n", machine().describe_context(), BIT(data, 3) ? 132 : 80);
136 }
137 
block_select_w(u8 data)138 void cdc721_state::block_select_w(u8 data)
139 {
140 	logerror("%s: Bank select = %02X\n", machine().describe_context(), data);
141 	for (int b = 0; b < 4; b++)
142 	{
143 		m_bank_16k[b]->set_bank(data & 3);
144 		data >>= 2;
145 	}
146 }
147 
nvram_w(offs_t offset,u8 data)148 void cdc721_state::nvram_w(offs_t offset, u8 data)
149 {
150 	m_nvram[offset] = data & 0x0f;
151 }
152 
153 template<int Bit>
WRITE_LINE_MEMBER(cdc721_state::foreign_char_bank_w)154 WRITE_LINE_MEMBER(cdc721_state::foreign_char_bank_w)
155 {
156 	if (state)
157 		m_foreign_char_bank |= 1 << Bit;
158 	else
159 		m_foreign_char_bank &= ~(1 << Bit);
160 }
161 
mem_map(address_map & map)162 void cdc721_state::mem_map(address_map &map)
163 {
164 	map(0x0000, 0x3fff).rw("block0", FUNC(address_map_bank_device::read8), FUNC(address_map_bank_device::write8));
165 	map(0x4000, 0x7fff).rw("block4", FUNC(address_map_bank_device::read8), FUNC(address_map_bank_device::write8));
166 	map(0x8000, 0xbfff).rw("block8", FUNC(address_map_bank_device::read8), FUNC(address_map_bank_device::write8));
167 	map(0xc000, 0xffff).rw("blockc", FUNC(address_map_bank_device::read8), FUNC(address_map_bank_device::write8));
168 }
169 
block0_map(address_map & map)170 void cdc721_state::block0_map(address_map &map)
171 {
172 	map(0x0000, 0x3fff).rom().region("resident", 0);
173 	map(0x8000, 0xbfff).ram();
174 }
175 
block4_map(address_map & map)176 void cdc721_state::block4_map(address_map &map)
177 {
178 	map(0x0000, 0x00ff).mirror(0x2700).ram().share("nvram").w(FUNC(cdc721_state::nvram_w));
179 	map(0x0800, 0x0bff).mirror(0x2400).ram().share("chargen"); // 2x P2114AL-2
180 	map(0x8000, 0xbfff).rom().region("16krom", 0);
181 	map(0xc000, 0xffff).ram();
182 }
183 
block8_map(address_map & map)184 void cdc721_state::block8_map(address_map &map)
185 {
186 	map(0x0000, 0x3fff).rom().region("rompack", 0);
187 	map(0x4000, 0x7fff).ram();
188 }
189 
blockc_map(address_map & map)190 void cdc721_state::blockc_map(address_map &map)
191 {
192 	map(0x0000, 0x1fff).ram();
193 	map(0x2000, 0x3fff).ram().share("videoram");
194 	map(0x4000, 0x40ff).mirror(0x2700).ram().share("nvram").w(FUNC(cdc721_state::nvram_w));
195 	map(0x4800, 0x4bff).mirror(0x2400).ram().share("chargen");
196 }
197 
io_map(address_map & map)198 void cdc721_state::io_map(address_map &map)
199 {
200 	map.global_mask(0xff);
201 	map(0x00, 0x03).rw("ctc", FUNC(z80ctc_device::read), FUNC(z80ctc_device::write));
202 	map(0x10, 0x1f).rw("crtc", FUNC(crt5037_device::read), FUNC(crt5037_device::write));
203 	map(0x20, 0x27).rw("kbduart", FUNC(ins8250_device::ins8250_r), FUNC(ins8250_device::ins8250_w));
204 	map(0x30, 0x33).rw("ppi", FUNC(i8255_device::read), FUNC(i8255_device::write));
205 	map(0x40, 0x47).rw("comuart", FUNC(ins8250_device::ins8250_r), FUNC(ins8250_device::ins8250_w));
206 	map(0x50, 0x50).w("ledlatch", FUNC(output_latch_device::write));
207 	map(0x70, 0x70).w(FUNC(cdc721_state::block_select_w));
208 	map(0x80, 0x87).rw("pauart", FUNC(ins8250_device::ins8250_r), FUNC(ins8250_device::ins8250_w));
209 	map(0x90, 0x97).rw("pbuart", FUNC(ins8250_device::ins8250_r), FUNC(ins8250_device::ins8250_w));
210 }
211 
INPUT_PORTS_START(cdc721)212 static INPUT_PORTS_START( cdc721 )
213 INPUT_PORTS_END
214 
215 void cdc721_state::machine_reset()
216 {
217 	for (auto &bank : m_bank_16k)
218 		bank->set_bank(0);
219 }
220 
machine_start()221 void cdc721_state::machine_start()
222 {
223 	m_pending_interrupts = 0;
224 	m_active_interrupts = 0;
225 	m_interrupt_mask = 0;
226 	m_foreign_char_bank = 0;
227 
228 	save_item(NAME(m_pending_interrupts));
229 	save_item(NAME(m_active_interrupts));
230 	save_item(NAME(m_interrupt_mask));
231 	save_item(NAME(m_foreign_char_bank));
232 }
233 
234 /* F4 Character Displayer */
235 static const gfx_layout cdc721_charlayout =
236 {
237 	8, 16,                  /* 8 x 16 characters */
238 	256,                    /* 256 characters */
239 	1,                  /* 1 bits per pixel */
240 	{ 0 },                  /* no bitplanes */
241 	/* x offsets */
242 	{ 7, 6, 5, 4, 3, 2, 1, 0 },
243 	/* y offsets */
244 	{ 0*8, 0x100*8, 0x200*8, 0x300*8, 0x400*8, 0x500*8, 0x600*8, 0x700*8,
245 	0x800*8, 0x900*8, 0xa00*8, 0xb00*8, 0xc00*8, 0xd00*8, 0xe00*8, 0xf00*8 },
246 	8                   /* every char takes 16 x 1 bytes */
247 };
248 
249 static GFXDECODE_START( gfx_cdc721 )
250 	GFXDECODE_ENTRY( "chargen", 0x0000, cdc721_charlayout, 0, 1 )
251 GFXDECODE_END
252 
cdc721_palette(palette_device & palette) const253 void cdc721_state::cdc721_palette(palette_device &palette) const
254 {
255 	palette.set_pen_color(0, 0, 0, 0 );     // Black
256 	palette.set_pen_color(1, 0, 255, 0 );   // Full
257 	palette.set_pen_color(2, 0, 128, 0 );   // Dimmed
258 }
259 
screen_update(screen_device & screen,bitmap_ind16 & bitmap,const rectangle & cliprect)260 uint32_t cdc721_state::screen_update(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
261 {
262 	uint16_t sy=0;
263 	m_flashcnt++;
264 
265 	for (uint8_t y = 0; y < 30; y++)
266 	{
267 		uint16_t ma = m_videoram[y * 2] | m_videoram[y * 2 + 1] << 8;
268 
269 		for (uint8_t ra = 0; ra < 15; ra++)
270 		{
271 			uint16_t *p = &bitmap.pix(sy++);
272 
273 			for (uint16_t x = 0; x < 160; x+=2)
274 			{
275 				uint8_t pen = 1;
276 				uint8_t chr = m_videoram[(x + ma) & 0x1fff];
277 				uint8_t attr = m_videoram[(x + ma + 1) & 0x1fff];
278 				uint8_t gfx = m_rom_chargen[chr | (ra << 8) ];
279 				if (BIT(attr, 0))  // blank
280 					pen = 0;
281 				if (BIT(attr, 1) && (ra == 14)) // underline
282 					gfx = 0xff;
283 				if (BIT(attr, 4)) // dim
284 					pen = 2;
285 				if (BIT(attr, 2)) // rv
286 					gfx ^= 0xff;
287 				if (BIT(attr, 3) && BIT(m_flashcnt, 6)) // blink
288 					gfx = 0;
289 
290 				/* Display a scanline of a character */
291 				*p++ = BIT(gfx, 0) ? pen : 0;
292 				*p++ = BIT(gfx, 1) ? pen : 0;
293 				*p++ = BIT(gfx, 2) ? pen : 0;
294 				*p++ = BIT(gfx, 3) ? pen : 0;
295 				*p++ = BIT(gfx, 4) ? pen : 0;
296 				*p++ = BIT(gfx, 5) ? pen : 0;
297 				*p++ = BIT(gfx, 6) ? pen : 0;
298 				*p++ = BIT(gfx, 7) ? pen : 0;
299 			}
300 		}
301 	}
302 	return 0;
303 }
304 
cdc721(machine_config & config)305 void cdc721_state::cdc721(machine_config &config)
306 {
307 	// basic machine hardware
308 	Z80(config, m_maincpu, 6_MHz_XTAL); // Zilog Z8400B (Z80B)
309 	m_maincpu->set_addrmap(AS_PROGRAM, &cdc721_state::mem_map);
310 	m_maincpu->set_addrmap(AS_IO, &cdc721_state::io_map);
311 	m_maincpu->set_irq_acknowledge_callback(FUNC(cdc721_state::restart_cb));
312 
313 	ADDRESS_MAP_BANK(config, "block0").set_map(&cdc721_state::block0_map).set_options(ENDIANNESS_LITTLE, 8, 32, 0x4000);
314 	ADDRESS_MAP_BANK(config, "block4").set_map(&cdc721_state::block4_map).set_options(ENDIANNESS_LITTLE, 8, 32, 0x4000);
315 	ADDRESS_MAP_BANK(config, "block8").set_map(&cdc721_state::block8_map).set_options(ENDIANNESS_LITTLE, 8, 32, 0x4000);
316 	ADDRESS_MAP_BANK(config, "blockc").set_map(&cdc721_state::blockc_map).set_options(ENDIANNESS_LITTLE, 8, 32, 0x4000);
317 
318 	NVRAM(config, "nvram", nvram_device::DEFAULT_ALL_0); // MCM51L01C45 (256x4) + battery
319 
320 	/* video hardware */
321 	screen_device &screen(SCREEN(config, "screen", SCREEN_TYPE_RASTER));
322 	screen.set_raw(12.936_MHz_XTAL, 800, 0, 640, 539, 0, 450);
323 	screen.set_screen_update(FUNC(cdc721_state::screen_update));
324 	screen.set_palette("palette");
325 	PALETTE(config, "palette", FUNC(cdc721_state::cdc721_palette), 3);
326 	GFXDECODE(config, "gfxdecode", "palette", gfx_cdc721);
327 
328 	CRT5037(config, m_crtc, 12.936_MHz_XTAL / 8).set_char_width(8);
329 	m_crtc->set_screen("screen");
330 
331 	z80ctc_device& ctc(Z80CTC(config, "ctc", 6_MHz_XTAL)); // Zilog Z8430B (M1 pulled up)
332 	ctc.intr_callback().set(FUNC(cdc721_state::int_w<6>));
333 	ctc.zc_callback<1>().set("ctc", FUNC(z80ctc_device::trg2));
334 	//ctc.zc_callback<2>().set("comuart", FUNC(ins8250_device::rclk_w));
335 
336 	i8255_device &ppi(I8255A(config, "ppi"));
337 	ppi.out_pb_callback().set(FUNC(cdc721_state::interrupt_mask_w));
338 	ppi.out_pc_callback().set(FUNC(cdc721_state::misc_w));
339 
340 	output_latch_device &ledlatch(OUTPUT_LATCH(config, "ledlatch"));
341 	ledlatch.bit_handler<0>().set_output("error").invert();
342 	ledlatch.bit_handler<1>().set_output("alert").invert();
343 	ledlatch.bit_handler<2>().set_output("lock").invert();
344 	ledlatch.bit_handler<3>().set_output("message").invert();
345 	ledlatch.bit_handler<4>().set_output("prog1").invert();
346 	ledlatch.bit_handler<5>().set_output("prog2").invert();
347 	ledlatch.bit_handler<6>().set_output("prog3").invert();
348 	ledlatch.bit_handler<7>().set_output("dsr").invert();
349 
350 	ins8250_device &comuart(INS8250(config, "comuart", 1.8432_MHz_XTAL));
351 	comuart.out_int_callback().set(FUNC(cdc721_state::int_w<0>));
352 	comuart.out_tx_callback().set("comm", FUNC(rs232_port_device::write_txd));
353 	comuart.out_dtr_callback().set("comm", FUNC(rs232_port_device::write_dtr));
354 	comuart.out_rts_callback().set("comm", FUNC(rs232_port_device::write_rts));
355 
356 	rs232_port_device &comm(RS232_PORT(config, "comm", default_rs232_devices, nullptr));
357 	comm.rxd_handler().set("comuart", FUNC(ins8250_device::rx_w));
358 	comm.dsr_handler().set("comuart", FUNC(ins8250_device::dsr_w));
359 	comm.dcd_handler().set("comuart", FUNC(ins8250_device::dcd_w));
360 	comm.cts_handler().set("comuart", FUNC(ins8250_device::cts_w));
361 	comm.ri_handler().set("comuart", FUNC(ins8250_device::ri_w));
362 
363 	ins8250_device &kbduart(INS8250(config, "kbduart", 1.8432_MHz_XTAL));
364 	kbduart.out_int_callback().set(FUNC(cdc721_state::int_w<5>));
365 	kbduart.out_dtr_callback().set(FUNC(cdc721_state::foreign_char_bank_w<2>));
366 	//kbduart.out_rts_callback().set(FUNC(cdc721_state::alarm_high_low_w));
367 	kbduart.out_out1_callback().set(FUNC(cdc721_state::foreign_char_bank_w<1>));
368 	kbduart.out_out2_callback().set(FUNC(cdc721_state::foreign_char_bank_w<0>));
369 
370 	ins8250_device &pauart(INS8250(config, "pauart", 1.8432_MHz_XTAL));
371 	pauart.out_int_callback().set("int2", FUNC(input_merger_device::in_w<1>));
372 	pauart.out_tx_callback().set("cha", FUNC(rs232_port_device::write_txd));
373 	pauart.out_dtr_callback().set("cha", FUNC(rs232_port_device::write_dtr));
374 	pauart.out_rts_callback().set("cha", FUNC(rs232_port_device::write_rts));
375 
376 	rs232_port_device &cha(RS232_PORT(config, "cha", default_rs232_devices, nullptr));
377 	cha.rxd_handler().set("pauart", FUNC(ins8250_device::rx_w));
378 	cha.dsr_handler().set("pauart", FUNC(ins8250_device::dsr_w));
379 	cha.dcd_handler().set("pauart", FUNC(ins8250_device::dcd_w));
380 	cha.cts_handler().set("pauart", FUNC(ins8250_device::cts_w));
381 	cha.ri_handler().set("pauart", FUNC(ins8250_device::ri_w));
382 
383 	ins8250_device &pbuart(INS8250(config, "pbuart", 1.8432_MHz_XTAL));
384 	pbuart.out_int_callback().set("int2", FUNC(input_merger_device::in_w<0>));
385 	pbuart.out_tx_callback().set("chb", FUNC(rs232_port_device::write_txd));
386 	pbuart.out_dtr_callback().set("chb", FUNC(rs232_port_device::write_dtr));
387 	pbuart.out_rts_callback().set("chb", FUNC(rs232_port_device::write_rts));
388 
389 	rs232_port_device &chb(RS232_PORT(config, "chb", default_rs232_devices, nullptr));
390 	chb.rxd_handler().set("pbuart", FUNC(ins8250_device::rx_w));
391 	chb.dsr_handler().set("pbuart", FUNC(ins8250_device::dsr_w));
392 	chb.dcd_handler().set("pbuart", FUNC(ins8250_device::dcd_w));
393 	chb.cts_handler().set("pbuart", FUNC(ins8250_device::cts_w));
394 	chb.ri_handler().set("pbuart", FUNC(ins8250_device::ri_w));
395 
396 	INPUT_MERGER_ANY_HIGH(config, "int2").output_handler().set(FUNC(cdc721_state::int_w<2>)); // 74S05 (open collector)
397 }
398 
399 ROM_START( cdc721 )
400 	ROM_REGION( 0x4000, "resident", 0 )
401 	ROM_LOAD( "66315359", 0x0000, 0x2000, CRC(20ff3eb4) SHA1(5f15cb14893d75a46dc66d3042356bb054d632c2) )
402 	ROM_LOAD( "66315361", 0x2000, 0x2000, CRC(21d59d09) SHA1(9c087537d68c600ddf1eb9b009cf458231c279f4) )
403 
404 	ROM_REGION( 0x4000, "16krom", 0 )
405 	ROM_LOAD( "66315360", 0x0000, 0x1000, CRC(feaa0fc5) SHA1(f06196553a1f10c07b2f7e495823daf7ea26edee) )
406 	//ROM_FILL(0x0157,1,0xe0)
407 
408 	ROM_REGION( 0x4000, "rompack", ROMREGION_ERASE00 )
409 
410 	ROM_REGION( 0x2000, "chargen", 0 )
411 	ROM_LOAD( "66315039", 0x0000, 0x1000, CRC(5c9aa968) SHA1(3ec7c5f25562579e6ed3fda7562428ff5e6b9550) )
412 	ROM_LOAD( "66307828", 0x1000, 0x1000, CRC(ac97136f) SHA1(0d280e1aa4b9502bd390d260f83af19bf24905cd) ) // foreign character ROM
413 
414 	// Graphics Firmware pack
415 	ROM_REGION( 0x4000, "gfxfw", 0 ) // load at 0x8000
416 	ROM_LOAD( "66315369.bin", 0x0000, 0x2000, CRC(224d3368) SHA1(e335ef6cd56d77194235f5a2a7cf2af9ebf42342) )
417 	ROM_LOAD( "66315370.bin", 0x2000, 0x2000, CRC(2543bf32) SHA1(1ac73a0e475d9fd86fba054e1a7a443d5bad1987) )
418 ROM_END
419 
420 COMP( 1981, cdc721, 0, 0, cdc721, cdc721, cdc721_state, empty_init, "Control Data Corporation", "721 Display Terminal", MACHINE_NOT_WORKING | MACHINE_NO_SOUND_HW )
421