Previous 15994 Revisions Next

r13612 Saturday 17th December, 2011 at 23:24:20 UTC by R. Belmont
mac: source overhaul, begin componentizing more (nw)
[src/mess/drivers]mac.c
[src/mess/includes]mac.h
[src/mess/machine]mac.c macadb.c* macrtc.c*
[src/mess/video]mac.c

src/mess/machine/macrtc.c
r0r13612
1/***************************************************************************
2 
3  macrtc.c - the real-time clock & NVRAM chip used in Macs and the Apple IIgs.
4 
5  TODO: convert to a device, share between Mac and IIgs
6 
7***************************************************************************/
8
9#include "emu.h"
10#include "includes/mac.h"
11
12#ifdef MAME_DEBUG
13#define LOG_RTC         0
14#else
15#define LOG_RTC         0
16#endif
17
18enum
19{
20   RTC_STATE_NORMAL,
21   RTC_STATE_WRITE,
22   RTC_STATE_XPCOMMAND,
23   RTC_STATE_XPWRITE
24};
25
26/* init the rtc core */
27void mac_state::rtc_init()
28{
29   m_rtc_rTCClk = 0;
30
31   m_rtc_write_protect = 0;
32   m_rtc_rTCEnb = 0;
33   rtc_write_rTCEnb(1);
34   m_rtc_state = RTC_STATE_NORMAL;
35}
36
37/* write the rTCEnb state */
38void mac_state::rtc_write_rTCEnb(int val)
39{
40   if (val && (! m_rtc_rTCEnb))
41   {
42      /* rTCEnb goes high (inactive) */
43      m_rtc_rTCEnb = 1;
44      /* abort current transmission */
45      m_rtc_data_byte = m_rtc_bit_count = m_rtc_data_dir = m_rtc_data_out = 0;
46      m_rtc_state = RTC_STATE_NORMAL;
47   }
48   else if ((! val) && m_rtc_rTCEnb)
49   {
50      /* rTCEnb goes low (active) */
51      m_rtc_rTCEnb = 0;
52      /* abort current transmission */
53      m_rtc_data_byte = m_rtc_bit_count = m_rtc_data_dir = m_rtc_data_out = 0;
54      m_rtc_state = RTC_STATE_NORMAL;
55   }
56}
57
58/* shift data (called on rTCClk high-to-low transition (?)) */
59void mac_state::rtc_shift_data(int data)
60{
61   if (m_rtc_rTCEnb)
62      /* if enable line inactive (high), do nothing */
63      return;
64
65   if (m_rtc_data_dir)
66   {   /* RTC -> VIA transmission */
67      m_rtc_data_out = (m_rtc_data_byte >> --m_rtc_bit_count) & 0x01;
68      if (LOG_RTC)
69         logerror("RTC shifted new data %d\n", m_rtc_data_out);
70   }
71   else
72   {   /* VIA -> RTC transmission */
73      m_rtc_data_byte = (m_rtc_data_byte << 1) | (data ? 1 : 0);
74
75      if (++m_rtc_bit_count == 8)
76      {   /* if one byte received, send to command interpreter */
77         rtc_execute_cmd(m_rtc_data_byte);
78      }
79   }
80}
81
82/* called every second, to increment the Clock count */
83void mac_state::rtc_incticks()
84{
85   if (LOG_RTC)
86      logerror("rtc_incticks called\n");
87
88   if (++m_rtc_seconds[0] == 0)
89      if (++m_rtc_seconds[1] == 0)
90         if (++m_rtc_seconds[2] == 0)
91            ++m_rtc_seconds[3];
92
93   /*if (++m_rtc_seconds[4] == 0)
94        if (++m_rtc_seconds[5] == 0)
95            if (++m_rtc_seconds[6] == 0)
96                ++m_rtc_seconds[7];*/
97}
98
99/* Executes a command.
100Called when the first byte after "enable" is received, and when the data byte after a write command
101is received. */
102void mac_state::rtc_execute_cmd(int data)
103{
104   int i;
105
106   if (LOG_RTC)
107      printf("rtc_execute_cmd: data=%x, state=%x\n", data, m_rtc_state);
108
109   if (m_rtc_state == RTC_STATE_XPCOMMAND)
110   {
111      m_rtc_xpaddr = ((m_rtc_cmd & 7)<<5) | ((data&0x7c)>>2);
112      if ((m_rtc_cmd & 0x80) != 0)
113      {
114         // read command
115         if (LOG_RTC)
116            printf("RTC: Reading extended address %x = %x\n", m_rtc_xpaddr, m_rtc_ram[m_rtc_xpaddr]);
117
118         m_rtc_data_dir = 1;
119         m_rtc_data_byte = m_rtc_ram[m_rtc_xpaddr];
120         m_rtc_state = RTC_STATE_NORMAL;
121      }
122      else
123      {
124         // write command
125         m_rtc_state = RTC_STATE_XPWRITE;
126         m_rtc_data_byte = 0;
127         m_rtc_bit_count = 0;
128      }
129   }
130   else if (m_rtc_state == RTC_STATE_XPWRITE)
131   {
132      if (LOG_RTC)
133         printf("RTC: writing %x to extended address %x\n", data, m_rtc_xpaddr);
134      m_rtc_ram[m_rtc_xpaddr] = data;
135      m_rtc_state = RTC_STATE_NORMAL;
136   }
137   else if (m_rtc_state == RTC_STATE_WRITE)
138   {
139      m_rtc_state = RTC_STATE_NORMAL;
140
141      /* Writing an RTC register */
142      i = (m_rtc_cmd >> 2) & 0x1f;
143      if (m_rtc_write_protect && (i != 13))
144         /* write-protection : only write-protect can be written again */
145         return;
146      switch(i)
147      {
148      case 0: case 1: case 2: case 3:   /* seconds register */
149      case 4: case 5: case 6: case 7:   /* ??? (not described in IM III) */
150         /* after various tries, I assumed m_rtc_seconds[4+i] is mapped to m_rtc_seconds[i] */
151         if (LOG_RTC)
152            logerror("RTC clock write, address = %X, data = %X\n", i, (int) m_rtc_data_byte);
153         m_rtc_seconds[i & 3] = m_rtc_data_byte;
154         break;
155
156      case 8: case 9: case 10: case 11:   /* RAM address $10-$13 */
157         if (LOG_RTC)
158            printf("RTC RAM write, address = %X, data = %X\n", (i & 3) + 0x10, (int) m_rtc_data_byte);
159         m_rtc_ram[(i & 3) + 0x10] = m_rtc_data_byte;
160         break;
161
162      case 12:
163         /* Test register - do nothing */
164         if (LOG_RTC)
165            logerror("RTC write to test register, data = %X\n", (int) m_rtc_data_byte);
166         break;
167
168      case 13:
169         /* Write-protect register  */
170         if (LOG_RTC)
171            printf("RTC write to write-protect register, data = %X\n", (int) m_rtc_data_byte&0x80);
172         m_rtc_write_protect = (m_rtc_data_byte & 0x80) ? TRUE : FALSE;
173         break;
174
175      case 16: case 17: case 18: case 19:   /* RAM address $00-$0f */
176      case 20: case 21: case 22: case 23:
177      case 24: case 25: case 26: case 27:
178      case 28: case 29: case 30: case 31:
179         if (LOG_RTC)
180            printf("RTC RAM write, address = %X, data = %X\n", i & 15, (int) m_rtc_data_byte);
181         m_rtc_ram[i & 15] = m_rtc_data_byte;
182         break;
183
184      default:
185         printf("Unknown RTC write command : %X, data = %d\n", (int) m_rtc_cmd, (int) m_rtc_data_byte);
186         break;
187      }
188   }
189   else
190   {
191      // always save this byte to m_rtc_cmd
192      m_rtc_cmd = m_rtc_data_byte;
193
194      if ((m_rtc_cmd & 0x78) == 0x38)   // extended command
195      {
196         m_rtc_state = RTC_STATE_XPCOMMAND;
197         m_rtc_data_byte = 0;
198         m_rtc_bit_count = 0;
199      }
200      else
201      {
202         if (m_rtc_cmd & 0x80)
203         {
204            m_rtc_state = RTC_STATE_NORMAL;
205
206            /* Reading an RTC register */
207            m_rtc_data_dir = 1;
208            i = (m_rtc_cmd >> 2) & 0x1f;
209            switch(i)
210            {
211               case 0: case 1: case 2: case 3:
212               case 4: case 5: case 6: case 7:
213                  m_rtc_data_byte = m_rtc_seconds[i & 3];
214                  if (LOG_RTC)
215                     printf("RTC clock read, address = %X -> data = %X\n", i, m_rtc_data_byte);
216                  break;
217
218               case 8: case 9: case 10: case 11:
219                  if (LOG_RTC)
220                     printf("RTC RAM read, address = %X data = %x\n", (i & 3) + 0x10, m_rtc_ram[(i & 3) + 0x10]);
221                  m_rtc_data_byte = m_rtc_ram[(i & 3) + 0x10];
222                  break;
223
224               case 16: case 17: case 18: case 19:
225               case 20: case 21: case 22: case 23:
226               case 24: case 25: case 26: case 27:
227               case 28: case 29: case 30: case 31:
228                  if (LOG_RTC)
229                     printf("RTC RAM read, address = %X data = %x\n", i & 15, m_rtc_ram[i & 15]);
230                  m_rtc_data_byte = m_rtc_ram[i & 15];
231                  break;
232
233               default:
234                  if (LOG_RTC)
235                     logerror("Unknown RTC read command : %X\n", (int) m_rtc_cmd);
236                  m_rtc_data_byte = 0;
237                  break;
238            }
239         }
240         else
241         {
242            /* Writing an RTC register */
243            /* wait for extra data byte */
244            if (LOG_RTC)
245               logerror("RTC write, waiting for data byte : %X\n", (int) m_rtc_cmd);
246            m_rtc_state = RTC_STATE_WRITE;
247            m_rtc_data_byte = 0;
248            m_rtc_bit_count = 0;
249         }
250      }
251   }
252}
253
254/* should save PRAM to file */
255/* TODO : save write_protect flag, save time difference with host clock */
256NVRAM_HANDLER( mac )
257{
258   mac_state *mac = machine.driver_data<mac_state>();
259
260   if (read_or_write)
261   {
262      if (LOG_RTC)
263         logerror("Writing PRAM to file\n");
264      file->write(mac->m_rtc_ram, sizeof(mac->m_rtc_ram));
265   }
266   else
267   {
268      if (file)
269      {
270         if (LOG_RTC)
271            logerror("Reading PRAM from file\n");
272         file->read(mac->m_rtc_ram, sizeof(mac->m_rtc_ram));
273      }
274      else
275      {
276         UINT8 *pram = &mac->m_rtc_ram[0];
277
278         if (LOG_RTC)
279            logerror("trashing PRAM\n");
280
281         memset(mac->m_rtc_ram, 0, sizeof(mac->m_rtc_ram));
282
283         // some Mac ROMs are buggy in the presence of
284         // no NVRAM, so let's initialize it right
285         pram[0] = 0xa8;   // valid
286         pram[4] = 0xcc;
287         pram[5] = 0x0a;
288         pram[6] = 0xcc;
289         pram[7] = 0x0a;
290         pram[0xc] = 0x42;   // XPRAM valid for Plus/SE
291         pram[0xd] = 0x75;
292         pram[0xe] = 0x67;
293         pram[0xf] = 0x73;
294         pram[0x10] = 0x18;
295         pram[0x11] = 0x88;
296         pram[0x12] = 0x01;
297         pram[0x13] = 0x4c;
298
299         if (mac->m_model >= MODEL_MAC_II)
300         {
301            pram[0xc] = 0x4e;   // XPRAM valid is different for these
302            pram[0xd] = 0x75;
303            pram[0xe] = 0x4d;
304            pram[0xf] = 0x63;
305            pram[0x77] = 0x01;
306            pram[0x78] = 0xff;
307            pram[0x79] = 0xff;
308            pram[0x7a] = 0xff;
309            pram[0x7b] = 0xdf;
310
311            if (mac->m_model == MODEL_MAC_IICI)
312            {
313               pram[0] = 0xa8;
314               pram[1] = 0x80;
315               pram[2] = 0x4f;
316               pram[3] = 0x48;
317               pram[4] = 0xcc;
318               pram[5] = 0x0a;
319               pram[6] = 0xcc;
320               pram[7] = 0x0a;
321               pram[0x10] = 0x18;
322               pram[0x11] = 0x88;
323               pram[0x12] = 0x03;
324               pram[0x13] = 0xec;
325               pram[0x57] = 0x1f;
326               pram[0x58] = 0x83;
327               pram[0x59] = 0x86;
328               pram[0x81] = 0x86;
329               pram[0x8a] = 0x05;
330               pram[0xb9] = 0xb0;
331               pram[0xf1] = 0x01;
332               pram[0xf3] = 0x02;
333               pram[0xf9] = 0x01;
334               pram[0xfb] = 0x8e;
335            }
336         }
337      }
338
339      {
340         /* Now we copy the host clock into the Mac clock */
341         /* Cool, isn't it ? :-) */
342         system_time systime;
343         struct tm mac_reference;
344         UINT32 seconds;
345
346         machine.base_datetime(systime);
347
348         /* The count starts on 1st January 1904 */
349         mac_reference.tm_sec = 0;
350         mac_reference.tm_min = 0;
351         mac_reference.tm_hour = 0;
352         mac_reference.tm_mday = 1;
353         mac_reference.tm_mon = 0;
354         mac_reference.tm_year = 4;
355         mac_reference.tm_isdst = 0;
356
357         seconds = difftime((time_t) systime.time, mktime(& mac_reference));
358
359         if (LOG_RTC)
360            logerror("second count 0x%lX\n", (unsigned long) seconds);
361
362         mac->m_rtc_seconds[0] = seconds & 0xff;
363         mac->m_rtc_seconds[1] = (seconds >> 8) & 0xff;
364         mac->m_rtc_seconds[2] = (seconds >> 16) & 0xff;
365         mac->m_rtc_seconds[3] = (seconds >> 24) & 0xff;
366      }
367   }
368}
369
370
371
src/mess/machine/mac.c
r13611r13612
6969
7070     Cuda version spotting:
7171     341S0060 - 0x00020028 (2.40) - Performa/Quadra 6xx, PMac 6200, x400, some x500, Pippin, Gossamer G3, others?
72                                    (verified found in PMac 5500-225)
7273     341S0262 - 0x???????? (?.??) - some PMac 6500
7374     341S0285 - 0x???????? (?.??) - PMac 4400 + Mac clones
7475     341S0417 - 0x???????? (?.??) - Color Classic
r13611r13612
105106#include "machine/ram.h"
106107#include "debugger.h"
107108
108#define ADB_IS_BITBANG   ((mac->m_model == MODEL_MAC_SE || mac->m_model == MODEL_MAC_CLASSIC) || (mac->m_model >= MODEL_MAC_II && mac->m_model <= MODEL_MAC_IICI) || (mac->m_model == MODEL_MAC_SE30) || (mac->m_model == MODEL_MAC_QUADRA_700))
109#define ADB_IS_BITBANG_CLASS   ((m_model == MODEL_MAC_SE || m_model == MODEL_MAC_CLASSIC) || (m_model >= MODEL_MAC_II && m_model <= MODEL_MAC_IICI) || (m_model == MODEL_MAC_SE30) || (m_model == MODEL_MAC_QUADRA_700))
110#define ADB_IS_EGRET   (mac->m_model >= MODEL_MAC_LC && mac->m_model <= MODEL_MAC_CLASSIC_II) || ((mac->m_model >= MODEL_MAC_IISI) && (mac->m_model <= MODEL_MAC_IIVI))
111#define ADB_IS_PM   ((mac->m_model >= MODEL_MAC_PORTABLE && mac->m_model <= MODEL_MAC_PB100) || (mac->m_model >= MODEL_MAC_PB140 && mac->m_model <= MODEL_MAC_PBDUO_270c))
112#define ADB_IS_PM_VIA1   (mac->m_model >= MODEL_MAC_PORTABLE && mac->m_model <= MODEL_MAC_PB100)
113#define ADB_IS_PM_VIA2   (mac->m_model >= MODEL_MAC_PB140 && mac->m_model <= MODEL_MAC_PBDUO_270c)
114#define ADB_IS_PM_VIA1_CLASS   (m_model >= MODEL_MAC_PORTABLE && m_model <= MODEL_MAC_PB100)
115#define ADB_IS_PM_VIA2_CLASS   (m_model >= MODEL_MAC_PB140 && m_model <= MODEL_MAC_PBDUO_270c)
116#define ADB_IS_PM_CLASS   ((m_model >= MODEL_MAC_PORTABLE && m_model <= MODEL_MAC_PB100) || (m_model >= MODEL_MAC_PB140 && m_model <= MODEL_MAC_PBDUO_270c))
117
118109#define AUDIO_IS_CLASSIC (mac->m_model <= MODEL_MAC_CLASSIC)
119110#define MAC_HAS_VIA2   ((m_model >= MODEL_MAC_II) && (m_model != MODEL_MAC_IIFX))
120111
121112#define ASC_INTS_RBV   ((mac->m_model >= MODEL_MAC_IICI) && (mac->m_model <= MODEL_MAC_IIVI)) || ((mac->m_model >= MODEL_MAC_LC) && (mac->m_model <= MODEL_MAC_COLOR_CLASSIC))
122113
123114#ifdef MAME_DEBUG
115#define LOG_ADB         0
124116#define LOG_VIA         0
125#define LOG_RTC         0
126117#define LOG_MAC_IWM      0
127118#define LOG_GENERAL      0
128119#define LOG_KEYBOARD   0
129120#define LOG_MEMORY      0
130121#else
122#define LOG_ADB         0
131123#define LOG_VIA         0
132#define LOG_RTC         0
133124#define LOG_MAC_IWM      0
134125#define LOG_GENERAL      0
135126#define LOG_KEYBOARD   0
136127#define LOG_MEMORY      0
137128#endif
138129
139#define LOG_ADB         0
140#define LOG_ADB_MCU_CMD      0
141#define LOG_ADB_TALK_LISTEN 0
142
143// ADB states
144#define ADB_STATE_NEW_COMMAND   (0)
145#define ADB_STATE_XFER_EVEN   (1)
146#define ADB_STATE_XFER_ODD   (2)
147#define ADB_STATE_IDLE      (3)
148#define ADB_STATE_NOTINIT   (4)
149
150// ADB commands
151#define ADB_CMD_RESET      (0)
152#define ADB_CMD_FLUSH      (1)
153
130extern TIMER_CALLBACK(mac_adb_tick);   // macadb.c
131extern TIMER_CALLBACK(mac_pmu_tick);   // macadb.c
154132static TIMER_CALLBACK(mac_scanline_tick);
155static TIMER_CALLBACK(mac_adb_tick);
156133static TIMER_CALLBACK(mac_6015_tick);
157static TIMER_CALLBACK(mac_pmu_tick);
158134static int scan_keyboard(running_machine &machine);
159135static TIMER_CALLBACK(inquiry_timeout_func);
160136static void keyboard_receive(running_machine &machine, int val);
r13611r13612
11831159   scc8530_w(scc, offset, wdata);
11841160}
11851161
1186
1187
1188/* *************************************************************************
1189 * RTC
1190 *
1191 * Real Time Clock chip - contains clock information and PRAM.  This chip is
1192 * accessed through the VIA
1193 * *************************************************************************/
1194enum
1195{
1196   RTC_STATE_NORMAL,
1197   RTC_STATE_WRITE,
1198   RTC_STATE_XPCOMMAND,
1199   RTC_STATE_XPWRITE
1200};
1201
1202/* init the rtc core */
1203void mac_state::rtc_init()
1204{
1205   m_rtc_rTCClk = 0;
1206
1207   m_rtc_write_protect = 0;
1208   m_rtc_rTCEnb = 0;
1209   rtc_write_rTCEnb(1);
1210   m_rtc_state = RTC_STATE_NORMAL;
1211}
1212
1213/* write the rTCEnb state */
1214void mac_state::rtc_write_rTCEnb(int val)
1215{
1216   if (val && (! m_rtc_rTCEnb))
1217   {
1218      /* rTCEnb goes high (inactive) */
1219      m_rtc_rTCEnb = 1;
1220      /* abort current transmission */
1221      m_rtc_data_byte = m_rtc_bit_count = m_rtc_data_dir = m_rtc_data_out = 0;
1222      m_rtc_state = RTC_STATE_NORMAL;
1223   }
1224   else if ((! val) && m_rtc_rTCEnb)
1225   {
1226      /* rTCEnb goes low (active) */
1227      m_rtc_rTCEnb = 0;
1228      /* abort current transmission */
1229      m_rtc_data_byte = m_rtc_bit_count = m_rtc_data_dir = m_rtc_data_out = 0;
1230      m_rtc_state = RTC_STATE_NORMAL;
1231   }
1232}
1233
1234/* shift data (called on rTCClk high-to-low transition (?)) */
1235void mac_state::rtc_shift_data(int data)
1236{
1237   if (m_rtc_rTCEnb)
1238      /* if enable line inactive (high), do nothing */
1239      return;
1240
1241   if (m_rtc_data_dir)
1242   {   /* RTC -> VIA transmission */
1243      m_rtc_data_out = (m_rtc_data_byte >> --m_rtc_bit_count) & 0x01;
1244      if (LOG_RTC)
1245         logerror("RTC shifted new data %d\n", m_rtc_data_out);
1246   }
1247   else
1248   {   /* VIA -> RTC transmission */
1249      m_rtc_data_byte = (m_rtc_data_byte << 1) | (data ? 1 : 0);
1250
1251      if (++m_rtc_bit_count == 8)
1252      {   /* if one byte received, send to command interpreter */
1253         rtc_execute_cmd(m_rtc_data_byte);
1254      }
1255   }
1256}
1257
1258/* called every second, to increment the Clock count */
1259void mac_state::rtc_incticks()
1260{
1261   if (LOG_RTC)
1262      logerror("rtc_incticks called\n");
1263
1264   if (++m_rtc_seconds[0] == 0)
1265      if (++m_rtc_seconds[1] == 0)
1266         if (++m_rtc_seconds[2] == 0)
1267            ++m_rtc_seconds[3];
1268
1269   /*if (++m_rtc_seconds[4] == 0)
1270        if (++m_rtc_seconds[5] == 0)
1271            if (++m_rtc_seconds[6] == 0)
1272                ++m_rtc_seconds[7];*/
1273}
1274
1275/* Executes a command.
1276Called when the first byte after "enable" is received, and when the data byte after a write command
1277is received. */
1278void mac_state::rtc_execute_cmd(int data)
1279{
1280   int i;
1281
1282   if (LOG_RTC)
1283      printf("rtc_execute_cmd: data=%x, state=%x\n", data, m_rtc_state);
1284
1285   if (m_rtc_state == RTC_STATE_XPCOMMAND)
1286   {
1287      m_rtc_xpaddr = ((m_rtc_cmd & 7)<<5) | ((data&0x7c)>>2);
1288      if ((m_rtc_cmd & 0x80) != 0)
1289      {
1290         // read command
1291         if (LOG_RTC)
1292            printf("RTC: Reading extended address %x = %x\n", m_rtc_xpaddr, m_rtc_ram[m_rtc_xpaddr]);
1293
1294         m_rtc_data_dir = 1;
1295         m_rtc_data_byte = m_rtc_ram[m_rtc_xpaddr];
1296         m_rtc_state = RTC_STATE_NORMAL;
1297      }
1298      else
1299      {
1300         // write command
1301         m_rtc_state = RTC_STATE_XPWRITE;
1302         m_rtc_data_byte = 0;
1303         m_rtc_bit_count = 0;
1304      }
1305   }
1306   else if (m_rtc_state == RTC_STATE_XPWRITE)
1307   {
1308      if (LOG_RTC)
1309         printf("RTC: writing %x to extended address %x\n", data, m_rtc_xpaddr);
1310      m_rtc_ram[m_rtc_xpaddr] = data;
1311      m_rtc_state = RTC_STATE_NORMAL;
1312   }
1313   else if (m_rtc_state == RTC_STATE_WRITE)
1314   {
1315      m_rtc_state = RTC_STATE_NORMAL;
1316
1317      /* Writing an RTC register */
1318      i = (m_rtc_cmd >> 2) & 0x1f;
1319      if (m_rtc_write_protect && (i != 13))
1320         /* write-protection : only write-protect can be written again */
1321         return;
1322      switch(i)
1323      {
1324      case 0: case 1: case 2: case 3:   /* seconds register */
1325      case 4: case 5: case 6: case 7:   /* ??? (not described in IM III) */
1326         /* after various tries, I assumed m_rtc_seconds[4+i] is mapped to m_rtc_seconds[i] */
1327         if (LOG_RTC)
1328            logerror("RTC clock write, address = %X, data = %X\n", i, (int) m_rtc_data_byte);
1329         m_rtc_seconds[i & 3] = m_rtc_data_byte;
1330         break;
1331
1332      case 8: case 9: case 10: case 11:   /* RAM address $10-$13 */
1333         if (LOG_RTC)
1334            printf("RTC RAM write, address = %X, data = %X\n", (i & 3) + 0x10, (int) m_rtc_data_byte);
1335         m_rtc_ram[(i & 3) + 0x10] = m_rtc_data_byte;
1336         break;
1337
1338      case 12:
1339         /* Test register - do nothing */
1340         if (LOG_RTC)
1341            logerror("RTC write to test register, data = %X\n", (int) m_rtc_data_byte);
1342         break;
1343
1344      case 13:
1345         /* Write-protect register  */
1346         if (LOG_RTC)
1347            printf("RTC write to write-protect register, data = %X\n", (int) m_rtc_data_byte&0x80);
1348         m_rtc_write_protect = (m_rtc_data_byte & 0x80) ? TRUE : FALSE;
1349         break;
1350
1351      case 16: case 17: case 18: case 19:   /* RAM address $00-$0f */
1352      case 20: case 21: case 22: case 23:
1353      case 24: case 25: case 26: case 27:
1354      case 28: case 29: case 30: case 31:
1355         if (LOG_RTC)
1356            printf("RTC RAM write, address = %X, data = %X\n", i & 15, (int) m_rtc_data_byte);
1357         m_rtc_ram[i & 15] = m_rtc_data_byte;
1358         break;
1359
1360      default:
1361         printf("Unknown RTC write command : %X, data = %d\n", (int) m_rtc_cmd, (int) m_rtc_data_byte);
1362         break;
1363      }
1364   }
1365   else
1366   {
1367      // always save this byte to m_rtc_cmd
1368      m_rtc_cmd = m_rtc_data_byte;
1369
1370      if ((m_rtc_cmd & 0x78) == 0x38)   // extended command
1371      {
1372         m_rtc_state = RTC_STATE_XPCOMMAND;
1373         m_rtc_data_byte = 0;
1374         m_rtc_bit_count = 0;
1375      }
1376      else
1377      {
1378         if (m_rtc_cmd & 0x80)
1379         {
1380            m_rtc_state = RTC_STATE_NORMAL;
1381
1382            /* Reading an RTC register */
1383            m_rtc_data_dir = 1;
1384            i = (m_rtc_cmd >> 2) & 0x1f;
1385            switch(i)
1386            {
1387               case 0: case 1: case 2: case 3:
1388               case 4: case 5: case 6: case 7:
1389                  m_rtc_data_byte = m_rtc_seconds[i & 3];
1390                  if (LOG_RTC)
1391                     printf("RTC clock read, address = %X -> data = %X\n", i, m_rtc_data_byte);
1392                  break;
1393
1394               case 8: case 9: case 10: case 11:
1395                  if (LOG_RTC)
1396                     printf("RTC RAM read, address = %X data = %x\n", (i & 3) + 0x10, m_rtc_ram[(i & 3) + 0x10]);
1397                  m_rtc_data_byte = m_rtc_ram[(i & 3) + 0x10];
1398                  break;
1399
1400               case 16: case 17: case 18: case 19:
1401               case 20: case 21: case 22: case 23:
1402               case 24: case 25: case 26: case 27:
1403               case 28: case 29: case 30: case 31:
1404                  if (LOG_RTC)
1405                     printf("RTC RAM read, address = %X data = %x\n", i & 15, m_rtc_ram[i & 15]);
1406                  m_rtc_data_byte = m_rtc_ram[i & 15];
1407                  break;
1408
1409               default:
1410                  if (LOG_RTC)
1411                     logerror("Unknown RTC read command : %X\n", (int) m_rtc_cmd);
1412                  m_rtc_data_byte = 0;
1413                  break;
1414            }
1415         }
1416         else
1417         {
1418            /* Writing an RTC register */
1419            /* wait for extra data byte */
1420            if (LOG_RTC)
1421               logerror("RTC write, waiting for data byte : %X\n", (int) m_rtc_cmd);
1422            m_rtc_state = RTC_STATE_WRITE;
1423            m_rtc_data_byte = 0;
1424            m_rtc_bit_count = 0;
1425         }
1426      }
1427   }
1428}
1429
1430/* should save PRAM to file */
1431/* TODO : save write_protect flag, save time difference with host clock */
1432NVRAM_HANDLER( mac )
1433{
1434   mac_state *mac = machine.driver_data<mac_state>();
1435
1436   if (read_or_write)
1437   {
1438      if (LOG_RTC)
1439         logerror("Writing PRAM to file\n");
1440      file->write(mac->m_rtc_ram, sizeof(mac->m_rtc_ram));
1441   }
1442   else
1443   {
1444      if (file)
1445      {
1446         if (LOG_RTC)
1447            logerror("Reading PRAM from file\n");
1448         file->read(mac->m_rtc_ram, sizeof(mac->m_rtc_ram));
1449      }
1450      else
1451      {
1452         UINT8 *pram = &mac->m_rtc_ram[0];
1453
1454         if (LOG_RTC)
1455            logerror("trashing PRAM\n");
1456
1457         memset(mac->m_rtc_ram, 0, sizeof(mac->m_rtc_ram));
1458
1459         // some Mac ROMs are buggy in the presence of
1460         // no NVRAM, so let's initialize it right
1461         pram[0] = 0xa8;   // valid
1462         pram[4] = 0xcc;
1463         pram[5] = 0x0a;
1464         pram[6] = 0xcc;
1465         pram[7] = 0x0a;
1466         pram[0xc] = 0x42;   // XPRAM valid for Plus/SE
1467         pram[0xd] = 0x75;
1468         pram[0xe] = 0x67;
1469         pram[0xf] = 0x73;
1470         pram[0x10] = 0x18;
1471         pram[0x11] = 0x88;
1472         pram[0x12] = 0x01;
1473         pram[0x13] = 0x4c;
1474
1475         if (mac->m_model >= MODEL_MAC_II)
1476         {
1477            pram[0xc] = 0x4e;   // XPRAM valid is different for these
1478            pram[0xd] = 0x75;
1479            pram[0xe] = 0x4d;
1480            pram[0xf] = 0x63;
1481            pram[0x77] = 0x01;
1482            pram[0x78] = 0xff;
1483            pram[0x79] = 0xff;
1484            pram[0x7a] = 0xff;
1485            pram[0x7b] = 0xdf;
1486
1487            if (mac->m_model == MODEL_MAC_IICI)
1488            {
1489               pram[0] = 0xa8;
1490               pram[1] = 0x80;
1491               pram[2] = 0x4f;
1492               pram[3] = 0x48;
1493               pram[4] = 0xcc;
1494               pram[5] = 0x0a;
1495               pram[6] = 0xcc;
1496               pram[7] = 0x0a;
1497               pram[0x10] = 0x18;
1498               pram[0x11] = 0x88;
1499               pram[0x12] = 0x03;
1500               pram[0x13] = 0xec;
1501               pram[0x57] = 0x1f;
1502               pram[0x58] = 0x83;
1503               pram[0x59] = 0x86;
1504               pram[0x81] = 0x86;
1505               pram[0x8a] = 0x05;
1506               pram[0xb9] = 0xb0;
1507               pram[0xf1] = 0x01;
1508               pram[0xf3] = 0x02;
1509               pram[0xf9] = 0x01;
1510               pram[0xfb] = 0x8e;
1511            }
1512         }
1513      }
1514
1515      {
1516         /* Now we copy the host clock into the Mac clock */
1517         /* Cool, isn't it ? :-) */
1518         system_time systime;
1519         struct tm mac_reference;
1520         UINT32 seconds;
1521
1522         machine.base_datetime(systime);
1523
1524         /* The count starts on 1st January 1904 */
1525         mac_reference.tm_sec = 0;
1526         mac_reference.tm_min = 0;
1527         mac_reference.tm_hour = 0;
1528         mac_reference.tm_mday = 1;
1529         mac_reference.tm_mon = 0;
1530         mac_reference.tm_year = 4;
1531         mac_reference.tm_isdst = 0;
1532
1533         seconds = difftime((time_t) systime.time, mktime(& mac_reference));
1534
1535         if (LOG_RTC)
1536            logerror("second count 0x%lX\n", (unsigned long) seconds);
1537
1538         mac->m_rtc_seconds[0] = seconds & 0xff;
1539         mac->m_rtc_seconds[1] = (seconds >> 8) & 0xff;
1540         mac->m_rtc_seconds[2] = (seconds >> 16) & 0xff;
1541         mac->m_rtc_seconds[3] = (seconds >> 24) & 0xff;
1542      }
1543   }
1544}
1545
15461162/* ********************************** *
15471163 * IWM Code specific to the Mac Plus  *
15481164 * ********************************** */
r13611r13612
15811197      applefdc_w(fdc, (offset >> 8), data>>8);
15821198}
15831199
1584/* *************************************************************************
1585 * ADB (Mac II-style)
1586 * *************************************************************************/
1587
1588#if LOG_ADB
1589static const char *const adb_statenames[4] = { "NEW", "EVEN", "ODD", "IDLE" };
1590#endif
1591
1592int mac_state::adb_pollkbd(int update)
1593{
1594   int i, j, keybuf, report, codes[2], result;
1595   static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5" };
1596
1597   codes[0] = codes[1] = 0xff;   // key up
1598   report = result = 0;
1599
1600   for (i = 0; i < 6; i++)
1601   {
1602      keybuf = input_port_read(machine(), keynames[i]);
1603
1604      // any changes in this row?
1605      if ((keybuf != m_key_matrix[i]) && (report < 2))
1606      {
1607         // check each column bit
1608         for (j=0; j<16; j++)
1609         {
1610            if (((keybuf ^ m_key_matrix[i]) >> j) & 1)
1611            {
1612               // update m_key_matrix
1613               if (update)
1614               {
1615                  m_key_matrix[i] = (m_key_matrix[i] & ~ (1 << j)) | (keybuf & (1 << j));
1616               }
1617
1618               codes[report] = (i<<4)|j;
1619
1620               // key up?
1621               if (!(keybuf & (1 << j)))
1622               {
1623                  codes[report] |= 0x80;
1624               }
1625
1626               // update modifier state
1627               if (update)
1628               {
1629                  if (((i<<4)|j) == 0x39)
1630                  {
1631                     if (codes[report] & 0x80)
1632                     {
1633                        m_adb_modifiers &= ~0x20;
1634                     }
1635                     else
1636                     {
1637                        m_adb_modifiers |= 0x20;
1638                     }
1639                  }
1640                  if (((i<<4)|j) == 0x36)
1641                  {
1642                     if (codes[report] & 0x80)
1643                     {
1644                        m_adb_modifiers &= ~0x8;
1645                     }
1646                     else
1647                     {
1648                        m_adb_modifiers |= 0x08;
1649                     }
1650                  }
1651                  if (((i<<4)|j) == 0x38)
1652                  {
1653                     if (codes[report] & 0x80)
1654                     {
1655                        m_adb_modifiers &= ~0x4;
1656                     }
1657                     else
1658                     {
1659                        m_adb_modifiers |= 0x04;
1660                     }
1661                  }
1662                  if (((i<<4)|j) == 0x3a)
1663                  {
1664                     if (codes[report] & 0x80)
1665                     {
1666                        m_adb_modifiers &= ~0x2;
1667                     }
1668                     else
1669                     {
1670                        m_adb_modifiers |= 0x02;
1671                     }
1672                  }
1673                  if (((i<<4)|j) == 0x37)
1674                  {
1675                     if (codes[report] & 0x80)
1676                     {
1677                        m_adb_modifiers &= ~0x1;
1678                     }
1679                     else
1680                     {
1681                        m_adb_modifiers |= 0x01;
1682                     }
1683                  }
1684               }
1685
1686               // we run out of keys we can track?
1687               report++;
1688               if (report == 2)
1689               {
1690                  break;
1691               }
1692            }
1693         }
1694
1695         // we run out of keys we can track?
1696         if (report == 2)
1697         {
1698            break;
1699         }
1700      }
1701   }
1702
1703//  printf("ADB keyboard: update %d keys %02x %02x\n", update, codes[0], codes[1]);
1704
1705   // figure out if there was a change
1706   if ((m_adb_currentkeys[0] != codes[0]) || (m_adb_currentkeys[1] != codes[1]))
1707   {
1708      result = 1;
1709
1710      // if we want to update the current read, do so
1711      if (update)
1712      {
1713         m_adb_currentkeys[0] = codes[0];
1714         m_adb_currentkeys[1] = codes[1];
1715      }
1716   }
1717
1718   return result;
1719}
1720
1721int mac_state::adb_pollmouse()
1722{
1723   int NewX, NewY, NewButton;
1724
1725   if (!m_adb_mouse_initialized)
1726   {
1727      return 0;
1728   }
1729
1730   NewButton = input_port_read(machine(), "MOUSE0") & 0x01;
1731   NewX = input_port_read(machine(), "MOUSE2");
1732   NewY = input_port_read(machine(), "MOUSE1");
1733
1734   if ((NewX != m_adb_lastmousex) || (NewY != m_adb_lastmousey) || (NewButton != m_adb_lastbutton))
1735   {
1736      return 1;
1737   }
1738
1739   return 0;
1740}
1741
1742void mac_state::adb_accummouse( UINT8 *MouseX, UINT8 *MouseY )
1743{
1744   int MouseCountX = 0, MouseCountY = 0;
1745   int NewX, NewY;
1746
1747   NewX = input_port_read(machine(), "MOUSE2");
1748   NewY = input_port_read(machine(), "MOUSE1");
1749
1750   /* see if it moved in the x coord */
1751   if (NewX != m_adb_lastmousex)
1752   {
1753      int diff = NewX - m_adb_lastmousex;
1754
1755      /* check for wrap */
1756      if (diff > 0x80)
1757         diff = 0x100-diff;
1758      if  (diff < -0x80)
1759         diff = -0x100-diff;
1760
1761      MouseCountX += diff;
1762      m_adb_lastmousex = NewX;
1763   }
1764
1765   /* see if it moved in the y coord */
1766   if (NewY != m_adb_lastmousey)
1767   {
1768      int diff = NewY - m_adb_lastmousey;
1769
1770      /* check for wrap */
1771      if (diff > 0x80)
1772         diff = 0x100-diff;
1773      if  (diff < -0x80)
1774         diff = -0x100-diff;
1775
1776      MouseCountY += diff;
1777      m_adb_lastmousey = NewY;
1778   }
1779
1780   m_adb_lastbutton = input_port_read(machine(), "MOUSE0") & 0x01;
1781
1782   *MouseX = (UINT8)MouseCountX;
1783   *MouseY = (UINT8)MouseCountY;
1784}
1785
1786void mac_state::adb_talk()
1787{
1788   int addr, reg;
1789
1790   addr = (m_adb_command>>4);
1791   reg = (m_adb_command & 3);
1792
1793//  printf("Mac sent %x (cmd %d addr %d reg %d mr %d kr %d)\n", mac->m_adb_command, (mac->m_adb_command>>2)&3, addr, reg, m_adb_mouseaddr, m_adb_keybaddr);
1794
1795   if (m_adb_waiting_cmd)
1796   {
1797      switch ((m_adb_command>>2)&3)
1798      {
1799         case 0:
1800         case 1:
1801            switch (reg)
1802            {
1803               case ADB_CMD_RESET:
1804                  #if LOG_ADB || LOG_ADB_TALK_LISTEN
1805                  printf("ADB RESET: reg %x address %x\n", reg, addr);
1806                  #endif
1807                  m_adb_direction = 0;
1808                  m_adb_send = 0;
1809                  break;
1810
1811               case ADB_CMD_FLUSH:
1812                  #if LOG_ADB || LOG_ADB_TALK_LISTEN
1813                  printf("ADB FLUSH: reg %x address %x\n", reg, addr);
1814                  #endif
1815
1816                  m_adb_direction = 0;
1817                  m_adb_send = 0;
1818                  break;
1819
1820               default:   // reserved/unused
1821                  break;
1822            }
1823            break;
1824
1825         case 2:   // listen
1826            #if LOG_ADB || LOG_ADB_TALK_LISTEN
1827            printf("ADB LISTEN: reg %x address %x\n", reg, addr);
1828            #endif
1829
1830            m_adb_direction = 1;   // input from Mac
1831            m_adb_listenreg = reg;
1832            m_adb_listenaddr = addr;
1833            m_adb_command = 0;
1834            break;
1835
1836         case 3: // talk
1837            #if LOG_ADB || LOG_ADB_TALK_LISTEN
1838            printf("ADB TALK: reg %x address %x\n", reg, addr);
1839            #endif
1840
1841            // keep track of what device the Mac last TALKed to
1842            m_adb_last_talk = addr;
1843
1844            m_adb_direction = 0;   // output to Mac
1845            if (addr == m_adb_mouseaddr)
1846            {
1847               UINT8 mouseX, mouseY;
1848
1849               #if LOG_ADB || LOG_ADB_TALK_LISTEN
1850               printf("Talking to mouse, register %x\n", reg);
1851               #endif
1852
1853               switch (reg)
1854               {
1855                  // read mouse
1856                  case 0:
1857                     if (m_adb_srq_switch)
1858                     {
1859                        m_adb_srq_switch = 0;
1860                        mouseX = mouseY = 0;
1861                     }
1862                     else
1863                     {
1864                        this->adb_accummouse(&mouseX, &mouseY);
1865                     }
1866                     m_adb_buffer[0] = (m_adb_lastbutton & 0x01) ? 0x00 : 0x80;
1867                     m_adb_buffer[0] |= mouseX & 0x7f;
1868                     m_adb_buffer[1] = mouseY & 0x7f;
1869                     m_adb_datasize = 2;
1870                     break;
1871
1872                  // get ID/handler
1873                  case 3:
1874                     m_adb_buffer[0] = 0x60 | ((m_adb_mouseaddr<<8)&0xf);   // SRQ enable, no exceptional event
1875                     m_adb_buffer[1] = 0x01;   // handler 1
1876                     m_adb_datasize = 2;
1877
1878                     m_adb_mouse_initialized = 1;
1879                     break;
1880
1881                  default:
1882                     break;
1883               }
1884            }
1885            else if (addr == m_adb_keybaddr)
1886            {
1887               #if LOG_ADB || LOG_ADB_TALK_LISTEN
1888               printf("Talking to keyboard, register %x\n", reg);
1889               #endif
1890
1891               switch (reg)
1892               {
1893                  // read keyboard
1894                  case 0:
1895                     if (m_adb_srq_switch)
1896                     {
1897                        m_adb_srq_switch = 0;
1898                     }
1899                     else
1900                     {
1901                        this->adb_pollkbd(1);
1902                     }
1903//                          printf("keyboard = %02x %02x\n", m_adb_currentkeys[0], m_adb_currentkeys[1]);
1904                     m_adb_buffer[0] = m_adb_currentkeys[1];
1905                     m_adb_buffer[1] = m_adb_currentkeys[0];
1906                     m_adb_datasize = 2;
1907                     break;
1908
1909                  // read modifier keys
1910                  case 2:
1911                     this->adb_pollkbd(1);
1912                     m_adb_buffer[0] = m_adb_modifiers;   // nothing pressed
1913                     m_adb_buffer[1] = 0;
1914                     m_adb_datasize = 2;
1915                     break;
1916
1917                  // get ID/handler
1918                  case 3:
1919                     m_adb_buffer[0] = 0x60 | ((m_adb_keybaddr<<8)&0xf);   // SRQ enable, no exceptional event
1920                     m_adb_buffer[1] = 0x01;   // handler 1
1921                     m_adb_datasize = 2;
1922
1923                     m_adb_keybinitialized = 1;
1924                     break;
1925
1926                  default:
1927                     break;
1928               }
1929            }
1930            else
1931            {
1932               #if LOG_ADB || LOG_ADB_TALK_LISTEN
1933               printf("ADB: talking to unconnected device %d (K %d M %d)\n", addr, m_adb_keybaddr, m_adb_mouseaddr);
1934               #endif
1935               m_adb_buffer[0] = m_adb_buffer[1] = 0;
1936               m_adb_datasize = 0;
1937            }
1938            break;
1939      }
1940
1941      m_adb_waiting_cmd = 0;
1942   }
1943   else
1944   {
1945      #if LOG_ADB || LOG_ADB_TALK_LISTEN
1946      printf("Got LISTEN data %x for device %x reg %x\n", m_adb_command, m_adb_listenaddr, m_adb_listenreg);
1947      #endif
1948
1949      if (m_adb_listenaddr == m_adb_mouseaddr)
1950      {
1951         if ((m_adb_listenreg == 3) && (m_adb_command > 0) && (m_adb_command < 16))
1952         {
1953            #if LOG_ADB || LOG_ADB_TALK_LISTEN
1954            printf("MOUSE: moving to address %x\n", m_adb_command);
1955            #endif
1956            m_adb_mouseaddr = m_adb_command&0x0f;
1957         }
1958      }
1959      else if (m_adb_listenaddr == m_adb_keybaddr)
1960      {
1961         if ((m_adb_listenreg == 3) && (m_adb_command > 0) && (m_adb_command < 16))
1962         {
1963            #if LOG_ADB || LOG_ADB_TALK_LISTEN
1964            printf("KEYBOARD: moving to address %x\n", m_adb_command);
1965            #endif
1966            m_adb_keybaddr = m_adb_command&0x0f;
1967         }
1968      }
1969   }
1970}
1971
1972static TIMER_CALLBACK(mac_adb_tick)
1973{
1974   mac_state *mac = machine.driver_data<mac_state>();
1975
1976   // do one clock transition on CB1 to advance the VIA shifter
1977   mac->m_adb_extclock ^= 1;
1978   mac->m_via1->write_cb1(mac->m_adb_extclock);
1979   mac->m_adb_extclock ^= 1;
1980   mac->m_via1->write_cb1(mac->m_adb_extclock);
1981
1982   mac->m_adb_timer_ticks--;
1983   if (!mac->m_adb_timer_ticks)
1984   {
1985      mac->m_adb_timer->adjust(attotime::never);
1986
1987      if ((mac->m_adb_direction) && (ADB_IS_BITBANG))
1988      {
1989         mac->adb_talk();
1990      }
1991   }
1992   else
1993   {
1994      mac->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
1995   }
1996}
1997
19981200static READ8_DEVICE_HANDLER(mac_adb_via_in_cb2)
19991201{
20001202   UINT8 ret;
r13611r13612
20341236    }
20351237}
20361238
2037static void mac_adb_newaction(mac_state *mac, int state)
2038{
2039   if (state != mac->m_adb_state)
2040   {
2041      #if LOG_ADB
2042      printf("New ADB state: %s\n", adb_statenames[state]);
2043      #endif
2044
2045      mac->m_adb_state = state;
2046      mac->m_adb_timer_ticks = 8;
2047
2048      switch (state)
2049      {
2050         case ADB_STATE_NEW_COMMAND:
2051            mac->m_adb_command = mac->m_adb_send = 0;
2052            mac->m_adb_direction = 1;   // Mac is shifting us a command
2053            mac->m_adb_waiting_cmd = 1;   // we're going to get a command
2054            mac->m_adb_irq_pending = 0;
2055            mac->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2056            break;
2057
2058         case ADB_STATE_XFER_EVEN:
2059         case ADB_STATE_XFER_ODD:
2060            if (mac->m_adb_datasize > 0)
2061            {
2062               int i;
2063
2064               // is something trying to send to the Mac?
2065               if (mac->m_adb_direction == 0)
2066               {
2067                  // set up the byte
2068                  mac->m_adb_send = mac->m_adb_buffer[0];
2069                  mac->m_adb_datasize--;
2070
2071                  // move down the rest of the buffer, if any
2072                  for (i = 0; i < mac->m_adb_datasize; i++)
2073                  {
2074                     mac->m_adb_buffer[i] = mac->m_adb_buffer[i+1];
2075                  }
2076               }
2077
2078            }
2079            else
2080            {
2081               mac->m_adb_send = 0;
2082               mac->m_adb_irq_pending = 1;
2083            }
2084
2085            mac->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2086            break;
2087
2088         case ADB_STATE_IDLE:
2089            mac->m_adb_irq_pending = 0;
2090            break;
2091      }
2092   }
2093}
2094
2095static TIMER_CALLBACK(mac_pmu_tick)
2096{
2097   mac_state *mac = machine.driver_data<mac_state>();
2098
2099   // state 10 means this is in response to an ADB command
2100   if (mac->m_pm_state == 10)
2101   {
2102      #if LOG_ADB
2103      printf("PM: was state 10, chunk-chunking CB1\n");
2104      #endif
2105      mac->m_pm_state = 0;
2106
2107      // tick CB1, which should cause a PMU interrupt on PMU machines
2108      mac->m_adb_extclock ^= 1;
2109      mac->m_via1->write_cb1(mac->m_adb_extclock);
2110      mac->m_adb_extclock ^= 1;
2111      mac->m_via1->write_cb1(mac->m_adb_extclock);
2112   }
2113   else
2114   {
2115      #if LOG_ADB
2116      printf("PM: timer tick, lowering ACK\n");
2117      #endif
2118      mac->m_pm_ack &= ~2;   // lower ACK to handshake next step
2119   }
2120}
2121
2122static void pmu_one_byte_reply(mac_state *mac, UINT8 result)
2123{
2124   mac->m_pm_out[0] = mac->m_pm_out[1] = 1;   // length
2125   mac->m_pm_out[2] = result;
2126   mac->m_pm_slen = 3;
2127   mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2128}
2129
2130static void pmu_three_byte_reply(mac_state *mac, UINT8 result1, UINT8 result2, UINT8 result3)
2131{
2132   mac->m_pm_out[0] = mac->m_pm_out[1] = 3;   // length
2133   mac->m_pm_out[2] = result1;
2134   mac->m_pm_out[3] = result2;
2135   mac->m_pm_out[4] = result3;
2136   mac->m_pm_slen = 5;
2137   mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2138}
2139
2140static void pmu_exec(mac_state *mac)
2141{
2142   mac->m_pm_sptr = 0;   // clear send pointer
2143   mac->m_pm_slen = 0;   // and send length
2144   mac->m_pm_dptr = 0;   // and receive pointer
2145
2146//  printf("PMU: Command %02x\n", mac->m_pm_cmd[0]);
2147   switch (mac->m_pm_cmd[0])
2148   {
2149      case 0x10:   // subsystem power and clock ctrl
2150         break;
2151
2152      case 0x20:   // send ADB command (PMU must issue an IRQ on completion)
2153         #if 0
2154         printf("PMU: Send ADB %02x %02x cmd %02x flag %02x data %02x %02x\n",
2155               mac->m_pm_cmd[0],   // 0x20
2156               mac->m_pm_cmd[1],   // ???
2157               mac->m_pm_cmd[2],   // adb flags (2 for autopoll active, 3 to reset bus?)
2158               mac->m_pm_cmd[3],   // length of ADB data
2159               mac->m_pm_cmd[4],   // adb data
2160               mac->m_pm_cmd[5]);
2161         #endif
2162
2163         #if 0
2164         mac->m_pm_state = 10;
2165         mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2166         if (ADB_IS_PM_VIA1)
2167         {
2168            mac->m_pmu_int_status = 0x1;
2169         }
2170         else if (ADB_IS_PM_VIA2)
2171         {
2172            mac->m_pmu_int_status = 0x10;
2173         }
2174         else
2175         {
2176            fatalerror("mac: unknown ADB PMU type\n");
2177         }
2178         mac->m_pmu_last_adb_command = mac->m_pm_cmd[2];
2179         mac->m_adb_command = mac->m_pm_cmd[2];
2180         mac->m_adb_waiting_cmd = 1;
2181         mac->adb_talk();
2182
2183         if ((mac->m_pm_cmd[2] & 0xf) == 0xb)   // LISTEN register 3 (remap)
2184         {
2185            mac->m_adb_waiting_cmd = 0;
2186            mac->m_adb_command = mac->m_pm_cmd[5];
2187            mac->adb_talk();
2188         }
2189         #else
2190         if (((mac->m_pm_cmd[2] == 0xfc) || (mac->m_pm_cmd[2] == 0x2c)) && (mac->m_pm_cmd[3] == 4))
2191         {
2192//              printf("PMU: request to poll ADB, returning nothing\n");
2193            mac->m_pm_slen = 0;
2194            mac->m_pmu_int_status = 0;
2195         }
2196         else
2197         {
2198            mac->m_pm_state = 10;
2199            mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2200            if (ADB_IS_PM_VIA1)
2201            {
2202               mac->m_pmu_int_status = 0x1;
2203            }
2204            else if (ADB_IS_PM_VIA2)
2205            {
2206               mac->m_pmu_int_status = 0x10;
2207            }
2208            else
2209            {
2210               fatalerror("mac: unknown ADB PMU type\n");
2211            }
2212            mac->m_pmu_last_adb_command = mac->m_pm_cmd[2];
2213         }
2214
2215         mac->m_adb_command = mac->m_pm_cmd[2];
2216         mac->m_adb_waiting_cmd = 1;
2217         mac->adb_talk();
2218         #endif
2219         break;
2220
2221      case 0x21:   // turn ADB auto-poll off (does this need a reply?)
2222         break;
2223
2224      case 0x28:   // read ADB
2225         if (mac->m_adb_datasize > 0)
2226         {
2227            mac->m_adb_datasize = 1; // hack
2228
2229            mac->m_pm_out[0] = mac->m_pm_out[1] = 3 + mac->m_adb_datasize;
2230            mac->m_pm_out[2] = 0;
2231//              mac->m_pm_out[3] = mac->m_pmu_last_adb_command;
2232            mac->m_pm_out[3] = 0;
2233            mac->m_pm_out[4] = mac->m_adb_datasize;
2234            for (int i = 0; i < mac->m_adb_datasize; i++)
2235            {
2236               mac->m_pm_out[5+i] = 0; //mac->m_adb_buffer[i];
2237            }
2238            mac->m_pm_slen = 5 + mac->m_adb_datasize;
2239         }
2240         else
2241         {
2242            mac->m_pm_out[0] = mac->m_pm_out[1] = 4;
2243            mac->m_pm_out[2] = 0;
2244            mac->m_pm_out[3] = 0;
2245//              mac->m_pm_out[3] = mac->m_pmu_last_adb_command;
2246            mac->m_pm_out[4] = 1;   // length of following data
2247            mac->m_pm_out[5] = 0;
2248            mac->m_pm_slen = 6;
2249         }
2250/*          printf("ADB packet: ");
2251            for (int i = 0; i < mac->m_pm_slen; i++)
2252            {
2253                printf("%02x ", mac->m_pm_out[i]);
2254            }
2255            printf("\n");*/
2256         mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1000)));
2257         break;
2258
2259      case 0x31:  // write first 20 bytes of PRAM
2260         {
2261            for (int i = 0; i < 20; i++)
2262            {
2263               mac->m_rtc_ram[i] = mac->m_pm_cmd[1+i];
2264            }
2265         }
2266         break;
2267
2268      case 0x32:   // write extended PRAM byte(s).  cmd[2] = address, cmd[3] = length, cmd[4...] = data
2269         if ((mac->m_pm_cmd[2] + mac->m_pm_cmd[3]) < 0x100)
2270         {
2271            int i;
2272
2273            for (i = 0; i < mac->m_pm_cmd[3]; i++)
2274            {
2275               mac->m_rtc_ram[mac->m_pm_cmd[2] + i] = mac->m_pm_cmd[4+i];
2276            }
2277         }
2278         break;
2279
2280      case 0x38:  // read time
2281         {
2282            mac->m_pm_out[0] = mac->m_pm_out[1] = 4;
2283            mac->m_pm_out[2] = mac->m_rtc_seconds[0];
2284            mac->m_pm_out[3] = mac->m_rtc_seconds[1];
2285            mac->m_pm_out[4] = mac->m_rtc_seconds[2];
2286            mac->m_pm_out[5] = mac->m_rtc_seconds[3];
2287            mac->m_pm_slen = 6;
2288            mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2289         }
2290         break;
2291
2292      case 0x39:   // read first 20 bytes of PRAM
2293         {
2294            int i;
2295
2296            mac->m_pm_out[0] = mac->m_pm_out[1] = 20;
2297            for (i = 0; i < 20; i++)
2298            {
2299               mac->m_pm_out[2 + i] = mac->m_rtc_ram[i];
2300            }
2301            mac->m_pm_slen = 22;
2302            mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2303         }
2304         break;
2305
2306      case 0x3a:   // read extended PRAM byte(s).  cmd[2] = address, cmd[3] = length
2307         if ((mac->m_pm_cmd[2] + mac->m_pm_cmd[3]) < 0x100)
2308         {
2309            int i;
2310
2311            mac->m_pm_out[0] = mac->m_pm_out[1] = mac->m_pm_cmd[3];
2312            for (i = 0; i < mac->m_pm_cmd[3]; i++)
2313            {
2314               mac->m_pm_out[2 + i] = mac->m_rtc_ram[mac->m_pm_cmd[2] + i];
2315            }
2316            mac->m_pm_slen = mac->m_pm_out[0] + 2;
2317            mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2318         }
2319         break;
2320
2321      case 0x40:  // set screen contrast
2322         break;
2323
2324      case 0x41:
2325         break;
2326
2327      case 0x58:   // read internal modem status
2328         pmu_one_byte_reply(mac, 0);
2329         break;
2330
2331      case 0x60:   // set low power warning and cutoff battery levels
2332         break;
2333
2334      case 0x68:   // read battery/charger level
2335         pmu_three_byte_reply(mac, 255, 255, 255);
2336         break;
2337
2338      case 0x69:  // read battery/charger instantaneous level and status
2339         pmu_three_byte_reply(mac, 255, 255, 255);
2340         break;
2341
2342      case 0x6b:   // read extended battery/charger level and status (wants an 8 byte reply)
2343         mac->m_pm_out[0] = mac->m_pm_out[1] = 8;   // length
2344         mac->m_pm_out[2] = 255;
2345         mac->m_pm_out[3] = 255;
2346         mac->m_pm_out[4] = 255;
2347         mac->m_pm_out[5] = 255;
2348         mac->m_pm_out[6] = 255;
2349         mac->m_pm_out[7] = 255;
2350         mac->m_pm_out[8] = 255;
2351         mac->m_pm_out[9] = 255;
2352         mac->m_pm_slen = 10;
2353         mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2354         break;
2355
2356      case 0x6c:   // read battery ID
2357         pmu_one_byte_reply(mac, 1);
2358         break;
2359
2360      case 0x78:   // read interrupt flag
2361         if (ADB_IS_PM_VIA2)   // PB 140/170 use a "leaner" PMU protocol where you get the data for a PMU interrupt here
2362         {
2363            #if 0
2364            if ((mac->m_pmu_int_status&0xf0) == 0x10)
2365            {
2366               if (mac->m_adb_datasize > 0)
2367               {
2368                  mac->m_adb_datasize = 1; // hack
2369                  mac->m_pm_out[0] = mac->m_pm_out[1] = 2 + mac->m_adb_datasize;
2370                  mac->m_pm_out[2] = mac->m_pmu_int_status; // ADB status in low nibble
2371                  mac->m_pm_out[3] = mac->m_pmu_last_adb_command;        // ADB command that was sent
2372                  for (int i = 0; i < mac->m_adb_datasize; i++)
2373                  {
2374                     mac->m_pm_out[4+i] = 0; //mac->m_adb_buffer[i];
2375                  }
2376                  mac->m_pm_slen = 4 + mac->m_adb_datasize;
2377                  mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
2378
2379/*                      printf("ADB packet: ");
2380                        for (int i = 0; i < mac->m_pm_slen; i++)
2381                        {
2382                            printf("%02x ", mac->m_pm_out[i]);
2383                        }
2384                        printf("\n");*/
2385               }
2386               else
2387               {
2388                  mac->m_pm_out[0] = mac->m_pm_out[1] = 2;
2389                  mac->m_pm_out[2] = mac->m_pmu_int_status; // ADB status in low nibble
2390                  mac->m_pm_out[3] = mac->m_pmu_last_adb_command;        // ADB command that was sent OR 0x80 for extra error-ness
2391                  mac->m_pm_out[4] = 0;                       // return data
2392                  mac->m_pm_slen = 4;
2393                  mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
2394               }
2395            }
2396            else
2397            {
2398               pmu_one_byte_reply(mac, mac->m_pmu_int_status);
2399            }
2400            #else
2401            if ((mac->m_pmu_int_status&0xf0) == 0x10)
2402            {
2403               mac->m_pm_out[0] = mac->m_pm_out[1] = 2;
2404               mac->m_pm_out[2] = mac->m_pmu_int_status; // ADB status in low nibble
2405               mac->m_pm_out[3] = mac->m_pmu_last_adb_command;        // ADB command that was sent OR 0x80 for extra error-ness
2406               mac->m_pm_out[4] = 0;                       // return data
2407               mac->m_pm_slen = 4;
2408               mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
2409            }
2410            else
2411            {
2412               pmu_one_byte_reply(mac, mac->m_pmu_int_status);
2413            }
2414            #endif
2415         }
2416         else
2417         {
2418            pmu_one_byte_reply(mac, mac->m_pmu_int_status);
2419         }
2420
2421         mac->m_pmu_int_status = 0;
2422         break;
2423
2424      case 0x90: // sound power control
2425         break;
2426
2427      case 0x98:   // read sound power state
2428         pmu_one_byte_reply(mac, 1);
2429         break;
2430
2431      case 0xd8:   // read A/D converter (not sure what this does)
2432         pmu_one_byte_reply(mac, 0);
2433         break;
2434
2435      case 0xe0:   // write PMU internal RAM
2436         break;
2437
2438      case 0xe8:  // read PMU internal RAM (just return zeroes)
2439         {
2440            int i;
2441
2442            mac->m_pm_out[0] = mac->m_pm_out[1] = mac->m_pm_cmd[4];
2443//              printf("PMU read at %x\n", mac->m_pm_cmd[2] | (mac->m_pm_cmd[3]<<8));
2444
2445            // note: read at 0xEE00 0 = target disk mode, 0xff = normal bootup
2446            // (actually 0x00EE, the 50753 port 6)
2447
2448            for (i = 0; i < mac->m_pm_cmd[4]; i++)
2449            {
2450               mac->m_pm_out[2 + i] = 0xff;
2451            }
2452            mac->m_pm_slen = mac->m_pm_out[0] + 2;
2453            mac->m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2454         }
2455         break;
2456
2457      case 0xec:   // PMU self-test (send 1 count byte + reply)
2458         pmu_one_byte_reply(mac, 0);
2459         break;
2460
2461      default:
2462         fatalerror("PMU: Unhandled command %02x\n", mac->m_pm_cmd[0]);
2463         break;
2464   }
2465
2466   if (mac->m_pm_slen > 0)
2467   {
2468      mac->m_pm_state = 1;
2469   }
2470}
2471
2472void mac_state::adb_vblank()
2473{
2474   if ((m_adb_state == ADB_STATE_IDLE) || ((ADB_IS_PM_CLASS) && (m_pmu_poll)))
2475   {
2476      if (this->adb_pollmouse())
2477      {
2478         // if the mouse was the last TALK, we can just send the new data
2479         // otherwise we need to pull SRQ
2480         if ((m_adb_last_talk == m_adb_mouseaddr) && !(ADB_IS_PM_CLASS))
2481         {
2482            // repeat last TALK to get updated data
2483            m_adb_waiting_cmd = 1;
2484            this->adb_talk();
2485
2486            m_adb_timer_ticks = 8;
2487            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2488         }
2489         #if 0
2490         else if (ADB_IS_PM_CLASS)
2491         {
2492            m_adb_waiting_cmd = 1;
2493            this->adb_talk();
2494            m_pm_state = 10;
2495            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2496            if (ADB_IS_PM_VIA1_CLASS)
2497            {
2498               m_pmu_int_status = 0x1;
2499            }
2500            else if (ADB_IS_PM_VIA2_CLASS)
2501            {
2502               m_pmu_int_status = 0x10;
2503            }
2504         }
2505         #endif
2506         else
2507         {
2508            m_adb_irq_pending = 1;
2509            m_adb_command = m_adb_send = 0;
2510            m_adb_timer_ticks = 1;   // one tick should be sufficient to make it see  the IRQ
2511            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2512            m_adb_srq_switch = 1;
2513         }
2514      }
2515      else if (this->adb_pollkbd(0))
2516      {
2517         if ((m_adb_last_talk == m_adb_keybaddr) && !(ADB_IS_PM_CLASS))
2518         {
2519            // repeat last TALK to get updated data
2520            m_adb_waiting_cmd = 1;
2521            this->adb_talk();
2522
2523            m_adb_timer_ticks = 8;
2524            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2525         }
2526         #if 0
2527         else if (ADB_IS_PM_CLASS)
2528         {
2529            m_adb_waiting_cmd = 1;
2530            this->adb_talk();
2531            m_pm_state = 10;
2532            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
2533            m_pmu_int_status = 0x1;
2534         }
2535         #endif
2536         else
2537         {
2538            m_adb_irq_pending = 1;
2539            m_adb_command = m_adb_send = 0;
2540            m_adb_timer_ticks = 1;   // one tick should be sufficient to make it see  the IRQ
2541            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
2542            m_adb_srq_switch = 1;
2543         }
2544      }
2545   }
2546}
2547
2548void mac_state::adb_reset()
2549{
2550   int i;
2551
2552   m_adb_srq_switch = 0;
2553   m_adb_irq_pending = 0;      // no interrupt
2554   m_adb_timer_ticks = 0;
2555   m_adb_command = 0;
2556   m_adb_extclock = 0;
2557   m_adb_send = 0;
2558   m_adb_waiting_cmd = 0;
2559   m_adb_streaming = MCU_STREAMING_NONE;
2560   m_adb_state = 0;
2561   m_pmu_poll = 0;
2562   if (ADB_IS_BITBANG_CLASS)
2563   {
2564      m_adb_state = ADB_STATE_NOTINIT;
2565   }
2566   m_adb_direction = 0;
2567   m_adb_datasize = 0;
2568   m_adb_last_talk = -1;
2569
2570   // mouse
2571   m_adb_mouseaddr = 3;
2572   m_adb_lastmousex = m_adb_lastmousey = m_adb_lastbutton = 0;
2573   m_adb_mouse_initialized = 0;
2574
2575   // keyboard
2576   m_adb_keybaddr = 2;
2577   m_adb_keybinitialized = 0;
2578   m_adb_currentkeys[0] = m_adb_currentkeys[1] = 0xff;
2579   m_adb_modifiers = 0;
2580   for (i=0; i<7; i++)
2581   {
2582      m_key_matrix[i] = 0;
2583   }
2584}
2585
25861239/* *************************************************************************
25871240 * VIA
25881241 * *************************************************************************
r13611r13612
28071460            // check if length byte matches
28081461            if ((mac->m_pm_dptr >= 2) && (mac->m_pm_cmd[1] == (mac->m_pm_dptr-2)))
28091462            {
2810               pmu_exec(mac);
1463               mac->pmu_exec();
28111464               #if LOG_ADB
28121465               printf("PMU exec: command %02x length %d\n", mac->m_pm_cmd[0], mac->m_pm_cmd[1]);
28131466               #endif
r13611r13612
28821535
28831536   if (ADB_IS_BITBANG)
28841537   {
2885      mac_adb_newaction(mac, (data & 0x30) >> 4);
1538      mac->mac_adb_newaction((data & 0x30) >> 4);
28861539   }
28871540   else if (ADB_IS_EGRET)
28881541   {
r13611r13612
30601713            // check if length byte matches
30611714            if ((mac->m_pm_dptr >= 2) && (mac->m_pm_cmd[1] == (mac->m_pm_dptr-2)))
30621715            {
3063               pmu_exec(mac);
1716               mac->pmu_exec();
30641717               #if LOG_ADB
30651718               printf("PMU exec: command %02x length %d\n", mac->m_pm_cmd[0], mac->m_pm_cmd[1]);
30661719               #endif
r13611r13612
33492002   return address;
33502003}
33512004
2005READ32_MEMBER(mac_state::mac_read_id)
2006{
2007//    printf("Mac read ID reg @ PC=%x\n", cpu_get_pc(m_maincpu));
2008
2009   switch (m_model)
2010   {
2011      case MODEL_MAC_LC_III:
2012         return 0xa55a0001;   // 25 MHz LC III
2013
2014      case MODEL_MAC_LC_III_PLUS:
2015         return 0xa55a0003;   // 33 MHz LC III+
2016
2017      case MODEL_MAC_POWERMAC_6100:
2018         return 0xa55a3011;
2019
2020      case MODEL_MAC_POWERMAC_7100:
2021         return 0xa55a3012;
2022
2023      case MODEL_MAC_POWERMAC_8100:
2024         return 0xa55a3013;
2025
2026      case MODEL_MAC_PBDUO_210:
2027         return 0xa55a1004;
2028
2029      case MODEL_MAC_PBDUO_230:
2030         return 0xa55a1005;
2031
2032      case MODEL_MAC_PBDUO_250:
2033         return 0xa55a1006;
2034
2035      default:
2036         return 0;
2037   }
2038}
2039
33522040static void mac_driver_init(running_machine &machine, model_t model)
33532041{
33542042   mac_state *mac = machine.driver_data<mac_state>();
src/mess/machine/macadb.c
r0r13612
1/***************************************************************************
2 
3  macadb.c - handles various aspects of ADB on the Mac.
4 
5***************************************************************************/
6
7#include "emu.h"
8#include "includes/mac.h"
9
10#define LOG_ADB            0
11#define LOG_ADB_MCU_CMD      0
12#define LOG_ADB_TALK_LISTEN 0
13
14// ADB states
15#define ADB_STATE_NEW_COMMAND   (0)
16#define ADB_STATE_XFER_EVEN   (1)
17#define ADB_STATE_XFER_ODD   (2)
18#define ADB_STATE_IDLE      (3)
19#define ADB_STATE_NOTINIT   (4)
20
21// ADB commands
22#define ADB_CMD_RESET      (0)
23#define ADB_CMD_FLUSH      (1)
24
25/* *************************************************************************
26 * ADB (Mac II-style)
27 * *************************************************************************/
28
29#if LOG_ADB
30static const char *const adb_statenames[4] = { "NEW", "EVEN", "ODD", "IDLE" };
31#endif
32
33int mac_state::adb_pollkbd(int update)
34{
35   int i, j, keybuf, report, codes[2], result;
36   static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5" };
37
38   codes[0] = codes[1] = 0xff;   // key up
39   report = result = 0;
40
41   for (i = 0; i < 6; i++)
42   {
43      keybuf = input_port_read(machine(), keynames[i]);
44
45      // any changes in this row?
46      if ((keybuf != m_key_matrix[i]) && (report < 2))
47      {
48         // check each column bit
49         for (j=0; j<16; j++)
50         {
51            if (((keybuf ^ m_key_matrix[i]) >> j) & 1)
52            {
53               // update m_key_matrix
54               if (update)
55               {
56                  m_key_matrix[i] = (m_key_matrix[i] & ~ (1 << j)) | (keybuf & (1 << j));
57               }
58
59               codes[report] = (i<<4)|j;
60
61               // key up?
62               if (!(keybuf & (1 << j)))
63               {
64                  codes[report] |= 0x80;
65               }
66
67               // update modifier state
68               if (update)
69               {
70                  if (((i<<4)|j) == 0x39)
71                  {
72                     if (codes[report] & 0x80)
73                     {
74                        m_adb_modifiers &= ~0x20;
75                     }
76                     else
77                     {
78                        m_adb_modifiers |= 0x20;
79                     }
80                  }
81                  if (((i<<4)|j) == 0x36)
82                  {
83                     if (codes[report] & 0x80)
84                     {
85                        m_adb_modifiers &= ~0x8;
86                     }
87                     else
88                     {
89                        m_adb_modifiers |= 0x08;
90                     }
91                  }
92                  if (((i<<4)|j) == 0x38)
93                  {
94                     if (codes[report] & 0x80)
95                     {
96                        m_adb_modifiers &= ~0x4;
97                     }
98                     else
99                     {
100                        m_adb_modifiers |= 0x04;
101                     }
102                  }
103                  if (((i<<4)|j) == 0x3a)
104                  {
105                     if (codes[report] & 0x80)
106                     {
107                        m_adb_modifiers &= ~0x2;
108                     }
109                     else
110                     {
111                        m_adb_modifiers |= 0x02;
112                     }
113                  }
114                  if (((i<<4)|j) == 0x37)
115                  {
116                     if (codes[report] & 0x80)
117                     {
118                        m_adb_modifiers &= ~0x1;
119                     }
120                     else
121                     {
122                        m_adb_modifiers |= 0x01;
123                     }
124                  }
125               }
126
127               // we run out of keys we can track?
128               report++;
129               if (report == 2)
130               {
131                  break;
132               }
133            }
134         }
135
136         // we run out of keys we can track?
137         if (report == 2)
138         {
139            break;
140         }
141      }
142   }
143
144//  printf("ADB keyboard: update %d keys %02x %02x\n", update, codes[0], codes[1]);
145
146   // figure out if there was a change
147   if ((m_adb_currentkeys[0] != codes[0]) || (m_adb_currentkeys[1] != codes[1]))
148   {
149      result = 1;
150
151      // if we want to update the current read, do so
152      if (update)
153      {
154         m_adb_currentkeys[0] = codes[0];
155         m_adb_currentkeys[1] = codes[1];
156      }
157   }
158
159   return result;
160}
161
162int mac_state::adb_pollmouse()
163{
164   int NewX, NewY, NewButton;
165
166   if (!m_adb_mouse_initialized)
167   {
168      return 0;
169   }
170
171   NewButton = input_port_read(machine(), "MOUSE0") & 0x01;
172   NewX = input_port_read(machine(), "MOUSE2");
173   NewY = input_port_read(machine(), "MOUSE1");
174
175   if ((NewX != m_adb_lastmousex) || (NewY != m_adb_lastmousey) || (NewButton != m_adb_lastbutton))
176   {
177      return 1;
178   }
179
180   return 0;
181}
182
183void mac_state::adb_accummouse( UINT8 *MouseX, UINT8 *MouseY )
184{
185   int MouseCountX = 0, MouseCountY = 0;
186   int NewX, NewY;
187
188   NewX = input_port_read(machine(), "MOUSE2");
189   NewY = input_port_read(machine(), "MOUSE1");
190
191   /* see if it moved in the x coord */
192   if (NewX != m_adb_lastmousex)
193   {
194      int diff = NewX - m_adb_lastmousex;
195
196      /* check for wrap */
197      if (diff > 0x80)
198         diff = 0x100-diff;
199      if  (diff < -0x80)
200         diff = -0x100-diff;
201
202      MouseCountX += diff;
203      m_adb_lastmousex = NewX;
204   }
205
206   /* see if it moved in the y coord */
207   if (NewY != m_adb_lastmousey)
208   {
209      int diff = NewY - m_adb_lastmousey;
210
211      /* check for wrap */
212      if (diff > 0x80)
213         diff = 0x100-diff;
214      if  (diff < -0x80)
215         diff = -0x100-diff;
216
217      MouseCountY += diff;
218      m_adb_lastmousey = NewY;
219   }
220
221   m_adb_lastbutton = input_port_read(machine(), "MOUSE0") & 0x01;
222
223   *MouseX = (UINT8)MouseCountX;
224   *MouseY = (UINT8)MouseCountY;
225}
226
227void mac_state::adb_talk()
228{
229   int addr, reg;
230
231   addr = (m_adb_command>>4);
232   reg = (m_adb_command & 3);
233
234//  printf("Mac sent %x (cmd %d addr %d reg %d mr %d kr %d)\n", mac->m_adb_command, (mac->m_adb_command>>2)&3, addr, reg, m_adb_mouseaddr, m_adb_keybaddr);
235
236   if (m_adb_waiting_cmd)
237   {
238      switch ((m_adb_command>>2)&3)
239      {
240         case 0:
241         case 1:
242            switch (reg)
243            {
244               case ADB_CMD_RESET:
245                  #if LOG_ADB || LOG_ADB_TALK_LISTEN
246                  printf("ADB RESET: reg %x address %x\n", reg, addr);
247                  #endif
248                  m_adb_direction = 0;
249                  m_adb_send = 0;
250                  break;
251
252               case ADB_CMD_FLUSH:
253                  #if LOG_ADB || LOG_ADB_TALK_LISTEN
254                  printf("ADB FLUSH: reg %x address %x\n", reg, addr);
255                  #endif
256
257                  m_adb_direction = 0;
258                  m_adb_send = 0;
259                  break;
260
261               default:   // reserved/unused
262                  break;
263            }
264            break;
265
266         case 2:   // listen
267            #if LOG_ADB || LOG_ADB_TALK_LISTEN
268            printf("ADB LISTEN: reg %x address %x\n", reg, addr);
269            #endif
270
271            m_adb_direction = 1;   // input from Mac
272            m_adb_listenreg = reg;
273            m_adb_listenaddr = addr;
274            m_adb_command = 0;
275            break;
276
277         case 3: // talk
278            #if LOG_ADB || LOG_ADB_TALK_LISTEN
279            printf("ADB TALK: reg %x address %x\n", reg, addr);
280            #endif
281
282            // keep track of what device the Mac last TALKed to
283            m_adb_last_talk = addr;
284
285            m_adb_direction = 0;   // output to Mac
286            if (addr == m_adb_mouseaddr)
287            {
288               UINT8 mouseX, mouseY;
289
290               #if LOG_ADB || LOG_ADB_TALK_LISTEN
291               printf("Talking to mouse, register %x\n", reg);
292               #endif
293
294               switch (reg)
295               {
296                  // read mouse
297                  case 0:
298                     if (m_adb_srq_switch)
299                     {
300                        m_adb_srq_switch = 0;
301                        mouseX = mouseY = 0;
302                     }
303                     else
304                     {
305                        this->adb_accummouse(&mouseX, &mouseY);
306                     }
307                     m_adb_buffer[0] = (m_adb_lastbutton & 0x01) ? 0x00 : 0x80;
308                     m_adb_buffer[0] |= mouseX & 0x7f;
309                     m_adb_buffer[1] = mouseY & 0x7f;
310                     m_adb_datasize = 2;
311                     break;
312
313                  // get ID/handler
314                  case 3:
315                     m_adb_buffer[0] = 0x60 | ((m_adb_mouseaddr<<8)&0xf);   // SRQ enable, no exceptional event
316                     m_adb_buffer[1] = 0x01;   // handler 1
317                     m_adb_datasize = 2;
318
319                     m_adb_mouse_initialized = 1;
320                     break;
321
322                  default:
323                     break;
324               }
325            }
326            else if (addr == m_adb_keybaddr)
327            {
328               #if LOG_ADB || LOG_ADB_TALK_LISTEN
329               printf("Talking to keyboard, register %x\n", reg);
330               #endif
331
332               switch (reg)
333               {
334                  // read keyboard
335                  case 0:
336                     if (m_adb_srq_switch)
337                     {
338                        m_adb_srq_switch = 0;
339                     }
340                     else
341                     {
342                        this->adb_pollkbd(1);
343                     }
344//                          printf("keyboard = %02x %02x\n", m_adb_currentkeys[0], m_adb_currentkeys[1]);
345                     m_adb_buffer[0] = m_adb_currentkeys[1];
346                     m_adb_buffer[1] = m_adb_currentkeys[0];
347                     m_adb_datasize = 2;
348                     break;
349
350                  // read modifier keys
351                  case 2:
352                     this->adb_pollkbd(1);
353                     m_adb_buffer[0] = m_adb_modifiers;   // nothing pressed
354                     m_adb_buffer[1] = 0;
355                     m_adb_datasize = 2;
356                     break;
357
358                  // get ID/handler
359                  case 3:
360                     m_adb_buffer[0] = 0x60 | ((m_adb_keybaddr<<8)&0xf);   // SRQ enable, no exceptional event
361                     m_adb_buffer[1] = 0x01;   // handler 1
362                     m_adb_datasize = 2;
363
364                     m_adb_keybinitialized = 1;
365                     break;
366
367                  default:
368                     break;
369               }
370            }
371            else
372            {
373               #if LOG_ADB || LOG_ADB_TALK_LISTEN
374               printf("ADB: talking to unconnected device %d (K %d M %d)\n", addr, m_adb_keybaddr, m_adb_mouseaddr);
375               #endif
376               m_adb_buffer[0] = m_adb_buffer[1] = 0;
377               m_adb_datasize = 0;
378            }
379            break;
380      }
381
382      m_adb_waiting_cmd = 0;
383   }
384   else
385   {
386      #if LOG_ADB || LOG_ADB_TALK_LISTEN
387      printf("Got LISTEN data %x for device %x reg %x\n", m_adb_command, m_adb_listenaddr, m_adb_listenreg);
388      #endif
389
390      if (m_adb_listenaddr == m_adb_mouseaddr)
391      {
392         if ((m_adb_listenreg == 3) && (m_adb_command > 0) && (m_adb_command < 16))
393         {
394            #if LOG_ADB || LOG_ADB_TALK_LISTEN
395            printf("MOUSE: moving to address %x\n", m_adb_command);
396            #endif
397            m_adb_mouseaddr = m_adb_command&0x0f;
398         }
399      }
400      else if (m_adb_listenaddr == m_adb_keybaddr)
401      {
402         if ((m_adb_listenreg == 3) && (m_adb_command > 0) && (m_adb_command < 16))
403         {
404            #if LOG_ADB || LOG_ADB_TALK_LISTEN
405            printf("KEYBOARD: moving to address %x\n", m_adb_command);
406            #endif
407            m_adb_keybaddr = m_adb_command&0x0f;
408         }
409      }
410   }
411}
412
413TIMER_CALLBACK(mac_adb_tick)
414{
415   mac_state *mac = machine.driver_data<mac_state>();
416
417   // do one clock transition on CB1 to advance the VIA shifter
418   mac->m_adb_extclock ^= 1;
419   mac->m_via1->write_cb1(mac->m_adb_extclock);
420   mac->m_adb_extclock ^= 1;
421   mac->m_via1->write_cb1(mac->m_adb_extclock);
422
423   mac->m_adb_timer_ticks--;
424   if (!mac->m_adb_timer_ticks)
425   {
426      mac->m_adb_timer->adjust(attotime::never);
427
428      if ((mac->m_adb_direction) && (ADB_IS_BITBANG))
429      {
430         mac->adb_talk();
431      }
432   }
433   else
434   {
435      mac->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
436   }
437}
438
439void mac_state::mac_adb_newaction(int state)
440{
441   if (state != m_adb_state)
442   {
443      #if LOG_ADB
444      printf("New ADB state: %s\n", adb_statenames[state]);
445      #endif
446
447      m_adb_state = state;
448      m_adb_timer_ticks = 8;
449
450      switch (state)
451      {
452         case ADB_STATE_NEW_COMMAND:
453            m_adb_command = m_adb_send = 0;
454            m_adb_direction = 1;   // Mac is shifting us a command
455            m_adb_waiting_cmd = 1;   // we're going to get a command
456            m_adb_irq_pending = 0;
457            m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
458            break;
459
460         case ADB_STATE_XFER_EVEN:
461         case ADB_STATE_XFER_ODD:
462            if (m_adb_datasize > 0)
463            {
464               int i;
465
466               // is something trying to send to the Mac?
467               if (m_adb_direction == 0)
468               {
469                  // set up the byte
470                  m_adb_send = m_adb_buffer[0];
471                  m_adb_datasize--;
472
473                  // move down the rest of the buffer, if any
474                  for (i = 0; i < m_adb_datasize; i++)
475                  {
476                     m_adb_buffer[i] = m_adb_buffer[i+1];
477                  }
478               }
479
480            }
481            else
482            {
483               m_adb_send = 0;
484               m_adb_irq_pending = 1;
485            }
486
487            m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
488            break;
489
490         case ADB_STATE_IDLE:
491            m_adb_irq_pending = 0;
492            break;
493      }
494   }
495}
496
497TIMER_CALLBACK(mac_pmu_tick)
498{
499   mac_state *mac = machine.driver_data<mac_state>();
500
501   // state 10 means this is in response to an ADB command
502   if (mac->m_pm_state == 10)
503   {
504      #if LOG_ADB
505      printf("PM: was state 10, chunk-chunking CB1\n");
506      #endif
507      mac->m_pm_state = 0;
508
509      // tick CB1, which should cause a PMU interrupt on PMU machines
510      mac->m_adb_extclock ^= 1;
511      mac->m_via1->write_cb1(mac->m_adb_extclock);
512      mac->m_adb_extclock ^= 1;
513      mac->m_via1->write_cb1(mac->m_adb_extclock);
514   }
515   else
516   {
517      #if LOG_ADB
518      printf("PM: timer tick, lowering ACK\n");
519      #endif
520      mac->m_pm_ack &= ~2;   // lower ACK to handshake next step
521   }
522}
523
524void mac_state::pmu_one_byte_reply(UINT8 result)
525{
526   m_pm_out[0] = m_pm_out[1] = 1;   // length
527   m_pm_out[2] = result;
528   m_pm_slen = 3;
529   m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
530}
531
532void mac_state::pmu_three_byte_reply(UINT8 result1, UINT8 result2, UINT8 result3)
533{
534   m_pm_out[0] = m_pm_out[1] = 3;   // length
535   m_pm_out[2] = result1;
536   m_pm_out[3] = result2;
537   m_pm_out[4] = result3;
538   m_pm_slen = 5;
539   m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
540}
541
542void mac_state::pmu_exec()
543{
544   m_pm_sptr = 0;   // clear send pointer
545   m_pm_slen = 0;   // and send length
546   m_pm_dptr = 0;   // and receive pointer
547
548//  printf("PMU: Command %02x\n", mac->m_pm_cmd[0]);
549   switch (m_pm_cmd[0])
550   {
551      case 0x10:   // subsystem power and clock ctrl
552         break;
553
554      case 0x20:   // send ADB command (PMU must issue an IRQ on completion)
555         #if 0
556         printf("PMU: Send ADB %02x %02x cmd %02x flag %02x data %02x %02x\n",
557               m_pm_cmd[0],   // 0x20
558               m_pm_cmd[1],   // ???
559               m_pm_cmd[2],   // adb flags (2 for autopoll active, 3 to reset bus?)
560               m_pm_cmd[3],   // length of ADB data
561               m_pm_cmd[4],   // adb data
562               m_pm_cmd[5]);
563         #endif
564
565         #if 0
566         m_pm_state = 10;
567         m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
568         if (ADB_IS_PM_VIA1_CLASS)
569         {
570            m_pmu_int_status = 0x1;
571         }
572         else if (ADB_IS_PM_VIA2_CLASS)
573         {
574            m_pmu_int_status = 0x10;
575         }
576         else
577         {
578            fatalerror("mac: unknown ADB PMU type\n");
579         }
580         m_pmu_last_adb_command = m_pm_cmd[2];
581         m_adb_command = m_pm_cmd[2];
582         m_adb_waiting_cmd = 1;
583         adb_talk();
584
585         if ((m_pm_cmd[2] & 0xf) == 0xb)   // LISTEN register 3 (remap)
586         {
587            m_adb_waiting_cmd = 0;
588            m_adb_command = mac->m_pm_cmd[5];
589            adb_talk();
590         }
591         #else
592         if (((m_pm_cmd[2] == 0xfc) || (m_pm_cmd[2] == 0x2c)) && (m_pm_cmd[3] == 4))
593         {
594//              printf("PMU: request to poll ADB, returning nothing\n");
595            m_pm_slen = 0;
596            m_pmu_int_status = 0;
597         }
598         else
599         {
600            m_pm_state = 10;
601            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
602            if (ADB_IS_PM_VIA1_CLASS)
603            {
604               m_pmu_int_status = 0x1;
605            }
606            else if (ADB_IS_PM_VIA2_CLASS)
607            {
608               m_pmu_int_status = 0x10;
609            }
610            else
611            {
612               fatalerror("mac: unknown ADB PMU type\n");
613            }
614            m_pmu_last_adb_command = m_pm_cmd[2];
615         }
616
617         m_adb_command = m_pm_cmd[2];
618         m_adb_waiting_cmd = 1;
619         adb_talk();
620         #endif
621         break;
622
623      case 0x21:   // turn ADB auto-poll off (does this need a reply?)
624         break;
625
626      case 0x28:   // read ADB
627         if (m_adb_datasize > 0)
628         {
629            m_adb_datasize = 1; // hack
630
631            m_pm_out[0] = m_pm_out[1] = 3 + m_adb_datasize;
632            m_pm_out[2] = 0;
633//              m_pm_out[3] = m_pmu_last_adb_command;
634            m_pm_out[3] = 0;
635            m_pm_out[4] = m_adb_datasize;
636            for (int i = 0; i < m_adb_datasize; i++)
637            {
638               m_pm_out[5+i] = 0; //mac->m_adb_buffer[i];
639            }
640            m_pm_slen = 5 + m_adb_datasize;
641         }
642         else
643         {
644            m_pm_out[0] = m_pm_out[1] = 4;
645            m_pm_out[2] = 0;
646            m_pm_out[3] = 0;
647//              m_pm_out[3] = m_pmu_last_adb_command;
648            m_pm_out[4] = 1;   // length of following data
649            m_pm_out[5] = 0;
650            m_pm_slen = 6;
651         }
652/*          printf("ADB packet: ");
653            for (int i = 0; i < m_pm_slen; i++)
654            {
655                printf("%02x ", m_pm_out[i]);
656            }
657            printf("\n");*/
658         m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1000)));
659         break;
660
661      case 0x31:  // write first 20 bytes of PRAM
662         {
663            for (int i = 0; i < 20; i++)
664            {
665               m_rtc_ram[i] = m_pm_cmd[1+i];
666            }
667         }
668         break;
669
670      case 0x32:   // write extended PRAM byte(s).  cmd[2] = address, cmd[3] = length, cmd[4...] = data
671         if ((m_pm_cmd[2] + m_pm_cmd[3]) < 0x100)
672         {
673            int i;
674
675            for (i = 0; i < m_pm_cmd[3]; i++)
676            {
677               m_rtc_ram[m_pm_cmd[2] + i] = m_pm_cmd[4+i];
678            }
679         }
680         break;
681
682      case 0x38:  // read time
683         {
684            m_pm_out[0] = m_pm_out[1] = 4;
685            m_pm_out[2] = m_rtc_seconds[0];
686            m_pm_out[3] = m_rtc_seconds[1];
687            m_pm_out[4] = m_rtc_seconds[2];
688            m_pm_out[5] = m_rtc_seconds[3];
689            m_pm_slen = 6;
690            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
691         }
692         break;
693
694      case 0x39:   // read first 20 bytes of PRAM
695         {
696            int i;
697
698            m_pm_out[0] = m_pm_out[1] = 20;
699            for (i = 0; i < 20; i++)
700            {
701               m_pm_out[2 + i] = m_rtc_ram[i];
702            }
703            m_pm_slen = 22;
704            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
705         }
706         break;
707
708      case 0x3a:   // read extended PRAM byte(s).  cmd[2] = address, cmd[3] = length
709         if ((m_pm_cmd[2] + m_pm_cmd[3]) < 0x100)
710         {
711            int i;
712
713            m_pm_out[0] = m_pm_out[1] = m_pm_cmd[3];
714            for (i = 0; i < m_pm_cmd[3]; i++)
715            {
716               m_pm_out[2 + i] = m_rtc_ram[m_pm_cmd[2] + i];
717            }
718            m_pm_slen = m_pm_out[0] + 2;
719            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
720         }
721         break;
722
723      case 0x40:  // set screen contrast
724         break;
725
726      case 0x41:
727         break;
728
729      case 0x58:   // read internal modem status
730         pmu_one_byte_reply(0);
731         break;
732
733      case 0x60:   // set low power warning and cutoff battery levels
734         break;
735
736      case 0x68:   // read battery/charger level
737         pmu_three_byte_reply(255, 255, 255);
738         break;
739
740      case 0x69:  // read battery/charger instantaneous level and status
741         pmu_three_byte_reply(255, 255, 255);
742         break;
743
744      case 0x6b:   // read extended battery/charger level and status (wants an 8 byte reply)
745         m_pm_out[0] = m_pm_out[1] = 8;   // length
746         m_pm_out[2] = 255;
747         m_pm_out[3] = 255;
748         m_pm_out[4] = 255;
749         m_pm_out[5] = 255;
750         m_pm_out[6] = 255;
751         m_pm_out[7] = 255;
752         m_pm_out[8] = 255;
753         m_pm_out[9] = 255;
754         m_pm_slen = 10;
755         m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
756         break;
757
758      case 0x6c:   // read battery ID
759         pmu_one_byte_reply(1);
760         break;
761
762      case 0x78:   // read interrupt flag
763         if (ADB_IS_PM_VIA2_CLASS)   // PB 140/170 use a "leaner" PMU protocol where you get the data for a PMU interrupt here
764         {
765            #if 0
766            if ((m_pmu_int_status&0xf0) == 0x10)
767            {
768               if (m_adb_datasize > 0)
769               {
770                  m_adb_datasize = 1; // hack
771                  m_pm_out[0] = m_pm_out[1] = 2 + m_adb_datasize;
772                  m_pm_out[2] = m_pmu_int_status; // ADB status in low nibble
773                  m_pm_out[3] = m_pmu_last_adb_command;        // ADB command that was sent
774                  for (int i = 0; i < m_adb_datasize; i++)
775                  {
776                     m_pm_out[4+i] = 0; //m_adb_buffer[i];
777                  }
778                  m_pm_slen = 4 + m_adb_datasize;
779                  m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
780
781/*                      printf("ADB packet: ");
782                        for (int i = 0; i < m_pm_slen; i++)
783                        {
784                            printf("%02x ", m_pm_out[i]);
785                        }
786                        printf("\n");*/
787               }
788               else
789               {
790                  m_pm_out[0] = m_pm_out[1] = 2;
791                  m_pm_out[2] = m_pmu_int_status; // ADB status in low nibble
792                  m_pm_out[3] = m_pmu_last_adb_command;        // ADB command that was sent OR 0x80 for extra error-ness
793                  m_pm_out[4] = 0;                       // return data
794                  m_pm_slen = 4;
795                  m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
796               }
797            }
798            else
799            {
800               pmu_one_byte_reply(mac, m_pmu_int_status);
801            }
802            #else
803            if ((m_pmu_int_status&0xf0) == 0x10)
804            {
805               m_pm_out[0] = m_pm_out[1] = 2;
806               m_pm_out[2] = m_pmu_int_status; // ADB status in low nibble
807               m_pm_out[3] = m_pmu_last_adb_command;        // ADB command that was sent OR 0x80 for extra error-ness
808               m_pm_out[4] = 0;                       // return data
809               m_pm_slen = 4;
810               m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(1500)));
811            }
812            else
813            {
814               pmu_one_byte_reply(m_pmu_int_status);
815            }
816            #endif
817         }
818         else
819         {
820            pmu_one_byte_reply(m_pmu_int_status);
821         }
822
823         m_pmu_int_status = 0;
824         break;
825
826      case 0x90: // sound power control
827         break;
828
829      case 0x98:   // read sound power state
830         pmu_one_byte_reply(1);
831         break;
832
833      case 0xd8:   // read A/D converter (not sure what this does)
834         pmu_one_byte_reply(0);
835         break;
836
837      case 0xe0:   // write PMU internal RAM
838         break;
839
840      case 0xe8:  // read PMU internal RAM (just return zeroes)
841         {
842            int i;
843
844            m_pm_out[0] = m_pm_out[1] = m_pm_cmd[4];
845//              printf("PMU read at %x\n", m_pm_cmd[2] | (m_pm_cmd[3]<<8));
846
847            // note: read at 0xEE00 0 = target disk mode, 0xff = normal bootup
848            // (actually 0x00EE, the 50753 port 6)
849
850            for (i = 0; i < m_pm_cmd[4]; i++)
851            {
852               m_pm_out[2 + i] = 0xff;
853            }
854            m_pm_slen = m_pm_out[0] + 2;
855            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
856         }
857         break;
858
859      case 0xec:   // PMU self-test (send 1 count byte + reply)
860         pmu_one_byte_reply(0);
861         break;
862
863      default:
864         fatalerror("PMU: Unhandled command %02x\n", m_pm_cmd[0]);
865         break;
866   }
867
868   if (m_pm_slen > 0)
869   {
870      m_pm_state = 1;
871   }
872}
873
874void mac_state::adb_vblank()
875{
876   if ((m_adb_state == ADB_STATE_IDLE) || ((ADB_IS_PM_CLASS) && (m_pmu_poll)))
877   {
878      if (this->adb_pollmouse())
879      {
880         // if the mouse was the last TALK, we can just send the new data
881         // otherwise we need to pull SRQ
882         if ((m_adb_last_talk == m_adb_mouseaddr) && !(ADB_IS_PM_CLASS))
883         {
884            // repeat last TALK to get updated data
885            m_adb_waiting_cmd = 1;
886            this->adb_talk();
887
888            m_adb_timer_ticks = 8;
889            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
890         }
891         #if 0
892         else if (ADB_IS_PM_CLASS)
893         {
894            m_adb_waiting_cmd = 1;
895            this->adb_talk();
896            m_pm_state = 10;
897            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
898            if (ADB_IS_PM_VIA1_CLASS)
899            {
900               m_pmu_int_status = 0x1;
901            }
902            else if (ADB_IS_PM_VIA2_CLASS)
903            {
904               m_pmu_int_status = 0x10;
905            }
906         }
907         #endif
908         else
909         {
910            m_adb_irq_pending = 1;
911            m_adb_command = m_adb_send = 0;
912            m_adb_timer_ticks = 1;   // one tick should be sufficient to make it see  the IRQ
913            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
914            m_adb_srq_switch = 1;
915         }
916      }
917      else if (this->adb_pollkbd(0))
918      {
919         if ((m_adb_last_talk == m_adb_keybaddr) && !(ADB_IS_PM_CLASS))
920         {
921            // repeat last TALK to get updated data
922            m_adb_waiting_cmd = 1;
923            this->adb_talk();
924
925            m_adb_timer_ticks = 8;
926            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
927         }
928         #if 0
929         else if (ADB_IS_PM_CLASS)
930         {
931            m_adb_waiting_cmd = 1;
932            this->adb_talk();
933            m_pm_state = 10;
934            m_pmu_send_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(200)));
935            m_pmu_int_status = 0x1;
936         }
937         #endif
938         else
939         {
940            m_adb_irq_pending = 1;
941            m_adb_command = m_adb_send = 0;
942            m_adb_timer_ticks = 1;   // one tick should be sufficient to make it see  the IRQ
943            this->m_adb_timer->adjust(attotime(0, ATTOSECONDS_IN_USEC(100)));
944            m_adb_srq_switch = 1;
945         }
946      }
947   }
948}
949
950void mac_state::adb_reset()
951{
952   int i;
953
954   m_adb_srq_switch = 0;
955   m_adb_irq_pending = 0;      // no interrupt
956   m_adb_timer_ticks = 0;
957   m_adb_command = 0;
958   m_adb_extclock = 0;
959   m_adb_send = 0;
960   m_adb_waiting_cmd = 0;
961   m_adb_streaming = MCU_STREAMING_NONE;
962   m_adb_state = 0;
963   m_pmu_poll = 0;
964   if (ADB_IS_BITBANG_CLASS)
965   {
966      m_adb_state = ADB_STATE_NOTINIT;
967   }
968   m_adb_direction = 0;
969   m_adb_datasize = 0;
970   m_adb_last_talk = -1;
971
972   // mouse
973   m_adb_mouseaddr = 3;
974   m_adb_lastmousex = m_adb_lastmousey = m_adb_lastbutton = 0;
975   m_adb_mouse_initialized = 0;
976
977   // keyboard
978   m_adb_keybaddr = 2;
979   m_adb_keybinitialized = 0;
980   m_adb_currentkeys[0] = m_adb_currentkeys[1] = 0xff;
981   m_adb_modifiers = 0;
982   for (i=0; i<7; i++)
983   {
984      m_key_matrix[i] = 0;
985   }
986}
987
src/mess/video/mac.c
r13611r13612
10571057            break;
10581058
10591059        default:
1060            printf("macwd_w: %x @ %x, mask %08x (PC=%x)\n", data, offset, mem_mask, cpu_get_pc(m_maincpu));
1060//            printf("macwd_w: %x @ %x, mask %08x (PC=%x)\n", data, offset, mem_mask, cpu_get_pc(m_maincpu));
10611061            break;
10621062    }
10631063}
src/mess/includes/mac.h
r13611r13612
1515#include "machine/egret.h"
1616#include "machine/nubus.h"
1717#include "machine/ncr539x.h"
18#include "sound/asc.h"
1819#include "sound/awacs.h"
1920
2021#define MAC_SCREEN_NAME "screen"
2122#define MAC_539X_1_TAG "539x_1"
2223#define MAC_539X_2_TAG "539x_2"
2324
25// model helpers
26#define ADB_IS_BITBANG   ((mac->m_model == MODEL_MAC_SE || mac->m_model == MODEL_MAC_CLASSIC) || (mac->m_model >= MODEL_MAC_II && mac->m_model <= MODEL_MAC_IICI) || (mac->m_model == MODEL_MAC_SE30) || (mac->m_model == MODEL_MAC_QUADRA_700))
27#define ADB_IS_BITBANG_CLASS   ((m_model == MODEL_MAC_SE || m_model == MODEL_MAC_CLASSIC) || (m_model >= MODEL_MAC_II && m_model <= MODEL_MAC_IICI) || (m_model == MODEL_MAC_SE30) || (m_model == MODEL_MAC_QUADRA_700))
28#define ADB_IS_EGRET   (mac->m_model >= MODEL_MAC_LC && mac->m_model <= MODEL_MAC_CLASSIC_II) || ((mac->m_model >= MODEL_MAC_IISI) && (mac->m_model <= MODEL_MAC_IIVI))
29#define ADB_IS_PM_VIA1   (mac->m_model >= MODEL_MAC_PORTABLE && mac->m_model <= MODEL_MAC_PB100)
30#define ADB_IS_PM_VIA2   (mac->m_model >= MODEL_MAC_PB140 && mac->m_model <= MODEL_MAC_PBDUO_270c)
31#define ADB_IS_PM_VIA1_CLASS   (m_model >= MODEL_MAC_PORTABLE && m_model <= MODEL_MAC_PB100)
32#define ADB_IS_PM_VIA2_CLASS   (m_model >= MODEL_MAC_PB140 && m_model <= MODEL_MAC_PBDUO_270c)
33#define ADB_IS_PM_CLASS   ((m_model >= MODEL_MAC_PORTABLE && m_model <= MODEL_MAC_PB100) || (m_model >= MODEL_MAC_PB140 && m_model <= MODEL_MAC_PBDUO_270c))
34
2435/* for Egret and CUDA streaming MCU commands, command types */
2536typedef enum
2637{
r13611r13612
150161DRIVER_INIT(macpb160);
151162DRIVER_INIT(maciivx);
152163DRIVER_INIT(maciifx);
153DRIVER_INIT(macpbduo210);
164DRIVER_INIT(macpd210);
154165DRIVER_INIT(macquadra700);
155166
156167NVRAM_HANDLER( mac );
r13611r13612
340351   void adb_talk();
341352   void mouse_callback();
342353    void rbv_recalc_irqs();
354   void pmu_exec();
355   void mac_adb_newaction(int state);
343356
344357   DECLARE_READ16_MEMBER ( mac_via_r );
345358   DECLARE_WRITE16_MEMBER ( mac_via_w );
r13611r13612
419432   int adb_pollkbd(int update);
420433   int adb_pollmouse();
421434   void adb_accummouse( UINT8 *MouseX, UINT8 *MouseY );
435   void pmu_one_byte_reply(UINT8 result);
436   void pmu_three_byte_reply(UINT8 result1, UINT8 result2, UINT8 result3);
422437
423438    // wait states for accessing the VIA
424439    int m_via_cycles;
src/mess/drivers/mac.c
r13611r13612
392392   }
393393}
394394
395READ32_MEMBER(mac_state::mac_read_id)
396{
397//  printf("Mac read ID reg @ PC=%x\n", cpu_get_pc(m_maincpu));
398
399   switch (m_model)
400   {
401      case MODEL_MAC_LC_III:
402         return 0xa55a0001;   // 25 MHz LC III
403
404      case MODEL_MAC_LC_III_PLUS:
405         return 0xa55a0003;   // 33 MHz LC III+
406
407      case MODEL_MAC_POWERMAC_6100:
408         return 0xa55a3011;
409
410      case MODEL_MAC_POWERMAC_7100:
411         return 0xa55a3012;
412
413      case MODEL_MAC_POWERMAC_8100:
414         return 0xa55a3013;
415
416      case MODEL_MAC_PBDUO_210:
417         return 0xa55a1004;
418
419      case MODEL_MAC_PBDUO_230:
420         return 0xa55a1005;
421
422      case MODEL_MAC_PBDUO_250:
423         return 0xa55a1006;
424
425      default:
426         return 0;
427   }
428}
429
430395// Portable/PB100 video
431396static VIDEO_START( macprtb )
432397{
r13611r13612
768733// fc4003da bit 3 is VBL
769734    AM_RANGE(0xfcff8000, 0xfcffffff) AM_ROM AM_REGION("vrom", 0x0000)
770735ADDRESS_MAP_END
736#if 0
737static ADDRESS_MAP_START(macpd210_map, AS_PROGRAM, 32, mac_state )
738   AM_RANGE(0x40000000, 0x400fffff) AM_ROM AM_REGION("bootrom", 0) AM_MIRROR(0x0ff00000)
771739
740   AM_RANGE(0x50f00000, 0x50f01fff) AM_READWRITE16(mac_via_r, mac_via_w, 0xffffffff)
741   AM_RANGE(0x50f02000, 0x50f03fff) AM_READWRITE16(mac_via2_r, mac_via2_w, 0xffffffff)
742   AM_RANGE(0x50f04000, 0x50f05fff) AM_READWRITE16(mac_scc_r, mac_scc_2_w, 0xffffffff)
743   AM_RANGE(0x50f06000, 0x50f07fff) AM_READWRITE(macii_scsi_drq_r, macii_scsi_drq_w)
744   AM_RANGE(0x50f10000, 0x50f11fff) AM_READWRITE16(macplus_scsi_r, macii_scsi_w, 0xffffffff)
745   AM_RANGE(0x50f12060, 0x50f12063) AM_READ(macii_scsi_drq_r)
746   AM_RANGE(0x50f14000, 0x50f15fff) AM_DEVREADWRITE8("asc", asc_device, read, write, 0xffffffff)
747   AM_RANGE(0x50f16000, 0x50f17fff) AM_READWRITE16(mac_iwm_r, mac_iwm_w, 0xffffffff)
748   AM_RANGE(0x50f20000, 0x50f21fff) AM_READWRITE8(mac_gsc_r, mac_gsc_w, 0xffffffff)
749   AM_RANGE(0x50f24000, 0x50f27fff) AM_READ(buserror_r)   // bus error here to make sure we aren't mistaken for another decoder
750
751   AM_RANGE(0x5ffffffc, 0x5fffffff) AM_READ(mac_read_id)
752
753   AM_RANGE(0x60000000, 0x6001ffff) AM_RAM   AM_BASE(m_vram) AM_MIRROR(0x0ffe0000)
754ADDRESS_MAP_END
755#endif
772756static ADDRESS_MAP_START(quadra700_map, AS_PROGRAM, 32, mac_state )
773757   AM_RANGE(0x40000000, 0x400fffff) AM_ROM AM_REGION("bootrom", 0) AM_MIRROR(0x0ff00000)
774758
r13611r13612
14721456    MCFG_RAM_DEFAULT_SIZE("4M")
14731457    MCFG_RAM_EXTRA_OPTIONS("8M,12M,16M")
14741458MACHINE_CONFIG_END
1459#if 0
1460static MACHINE_CONFIG_DERIVED( macpd210, macpb160 )
1461   MCFG_CPU_REPLACE("maincpu", M68030, 25000000)
1462   MCFG_CPU_PROGRAM_MAP(macpd210_map)
14751463
1464   MCFG_RAM_MODIFY(RAM_TAG)
1465   MCFG_RAM_DEFAULT_SIZE("4M")
1466   MCFG_RAM_EXTRA_OPTIONS("8M,12M,16M,20M,24M")
1467MACHINE_CONFIG_END
1468#endif
14761469static MACHINE_CONFIG_DERIVED( macclas2, maclc )
14771470
14781471   MCFG_CPU_REPLACE("maincpu", M68030, C15M)
r13611r13612
21232116   ROM_LOAD( "ecd99dc0.rom", 0x000000, 0x100000, CRC(c84c3aa5) SHA1(fd9e852e2d77fe17287ba678709b9334d4d74f1e) )
21242117ROM_END
21252118
2119/*ROM_START( macpd210 )
2120   ROM_REGION32_BE(0x100000, "bootrom", 0)
2121   ROM_LOAD( "ecfa989b.rom", 0x000000, 0x100000, CRC(b86ed854) SHA1(ed1371c97117a5884da4a6605ecfc5abed48ae5a) )
2122ROM_END*/
2123
21262124/*    YEAR  NAME      PARENT    COMPAT  MACHINE   INPUT     INIT     COMPANY          FULLNAME */
21272125COMP( 1984, mac128k,  0,      0,   mac128k,  macplus,  mac128k512k,  "Apple Computer", "Macintosh 128k",  GAME_NOT_WORKING )
21282126COMP( 1984, mac512k,  mac128k,  0,   mac512ke, macplus,  mac128k512k,  "Apple Computer", "Macintosh 512k",  GAME_NOT_WORKING )
r13611r13612
21522150COMP( 1992, macpb160, 0,        0,  macpb160, macadb,   macpb160,     "Apple Computer", "Macintosh PowerBook 160", GAME_NOT_WORKING )
21532151COMP( 1992, macpb180, macpb160, 0,  macpb180, macadb,   macpb160,     "Apple Computer", "Macintosh PowerBook 180", GAME_NOT_WORKING )
21542152COMP( 1992, macpb180c,macpb160, 0,  macpb180c,macadb,   macpb160,     "Apple Computer", "Macintosh PowerBook 180c", GAME_NOT_WORKING )
2153//COMP( 1992, macpd210, 0,        0,  macpd210, macadb,   macpd210,     "Apple Computer", "Macintosh PowerBook Duo 210", GAME_NOT_WORKING )
21552154COMP( 1993, maccclas, 0,        0,  maclc2,   macadb,   maclrcclassic,"Apple Computer", "Macintosh Color Classic", GAME_NOT_WORKING )
21562155COMP( 1992, macpb145b,macpb140, 0,  macpb170, macadb,   macpb140,     "Apple Computer", "Macintosh PowerBook 145B", GAME_NOT_WORKING )
21572156COMP( 1993, maclc3,   0,      0,   maclc3,   maciici,  maclc3,         "Apple Computer", "Macintosh LC III",  GAME_NOT_WORKING )

Previous 15994 Revisions Next


© 1998-2018 The MESS Team