1 // license:BSD-3-Clause
2 // copyright-holders:Ryan Holtz
3 /***********************************************************************
4 
5     Philips SAA1043 Universal Sync Generator
6 
7     TOOD:
8     - Everything.
9 
10 ***********************************************************************/
11 
12 #include "emu.h"
13 #include "saa1043.h"
14 
15 #include <algorithm>
16 
17 
18 DEFINE_DEVICE_TYPE(SAA1043, saa1043_device, "saa1043", "Philips SAA1043")
19 
20 /*static*/ const uint32_t saa1043_device::s_line_counts[4] = { 624, 624, 524, 524 };
21 
saa1043_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)22 saa1043_device::saa1043_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
23 	: device_t(mconfig, SAA1043, tag, owner, clock)
24 	, m_outputs(*this)
25 	, m_type(PAL)
26 {
27 	std::fill(std::begin(m_outputs_hooked), std::end(m_outputs_hooked), false);
28 }
29 
device_start()30 void saa1043_device::device_start()
31 {
32 	m_h = attotime::from_ticks(320, clock() * 2);
33 	m_line_count = s_line_counts[m_type];
34 
35 	// resolve callbacks
36 	for (uint32_t i = 0; i < OUT_COUNT; i++)
37 	{
38 		m_outputs[i].resolve_safe();
39 		if (m_outputs_hooked[i])
40 		{
41 			m_timers[i] = timer_alloc(i);
42 			switch(i)
43 			{
44 				case V2:
45 					m_timers[V2]->adjust(m_h * 6, 1);
46 					break;
47 				default:
48 					// Not yet implemented
49 					break;
50 			}
51 		}
52 	}
53 }
54 
device_reset()55 void saa1043_device::device_reset()
56 {
57 	// Clear any existing clock states
58 	for (uint32_t i = 0; i < OUT_COUNT; i++)
59 	{
60 		m_outputs[i](CLEAR_LINE);
61 	}
62 	m_outputs[V2](ASSERT_LINE);
63 }
64 
device_timer(emu_timer & timer,device_timer_id id,int param,void * ptr)65 void saa1043_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
66 {
67 	switch (id)
68 	{
69 		case V2:
70 			m_outputs[V2](1 - param);
71 			if (param)
72 				m_timers[V2]->adjust(m_h * (m_line_count - 9), 0);
73 			else
74 				m_timers[V2]->adjust(m_h * 9, 1);
75 			break;
76 
77 		default:
78 			// Not yet implemented
79 			break;
80 	}
81 }
82