Previous 15994 Revisions Next

r12732 Monday 29th August, 2011 at 08:36:05 UTC by Miodrag Milanović
Removed not used files (code is in driver file)
[src/mess/machine]pdp1.c tx0.c

src/mess/machine/pdp1.c
r12731r12732
1/*
2    pdp1 machine code
3
4    includes emulation for I/O devices (with the IOT opcode) and control panel functions
5
6    TODO:
7    * typewriter out should overwrite the typewriter buffer
8    * improve emulation of the internals of tape reader?
9    * improve puncher timing?
10*/
11
12#include <stdio.h>
13
14#include "emu.h"
15#include "cpu/pdp1/pdp1.h"
16#include "includes/pdp1.h"
17
18
19/* This flag makes the emulated pdp-1 trigger a sequence break request when a character has been
20typed on the typewriter keyboard.  It is useful in order to test sequence break, but we need
21to emulate a connection box in which we can connect each wire to any interrupt line.  Also,
22we need to determine the exact relationship between the status register and the sequence break
23system (both standard and type 20). */
24#define USE_SBS 0
25
26#define LOG_IOT 0
27#define LOG_IOT_EXTRA 0
28
29/* IOT completion may take much more than the 5us instruction time.  A possible programming
30error would be to execute an IOT before the last IOT of the same kind is over: such a thing
31would confuse the targeted IO device, which might ignore the latter IOT, execute either
32or both IOT incompletely, or screw up completely.  I insist that such an error can be caused
33by a pdp-1 programming error, even if there is no emulator error. */
34#define LOG_IOT_OVERLAP 0
35
36
37static int tape_read(pdp1_state *state, UINT8 *reply);
38static TIMER_CALLBACK(reader_callback);
39static TIMER_CALLBACK(puncher_callback);
40static TIMER_CALLBACK(tyo_callback);
41static TIMER_CALLBACK(dpy_callback);
42static void pdp1_machine_stop(running_machine &machine);
43
44static void pdp1_tape_read_binary(device_t *device);
45static void pdp1_io_sc_callback(device_t *device);
46
47static void iot_rpa(device_t *device, int op2, int nac, int mb, int *io, int ac);
48static void iot_rpb(device_t *device, int op2, int nac, int mb, int *io, int ac);
49static void iot_rrb(device_t *device, int op2, int nac, int mb, int *io, int ac);
50static void iot_ppa(device_t *device, int op2, int nac, int mb, int *io, int ac);
51static void iot_ppb(device_t *device, int op2, int nac, int mb, int *io, int ac);
52
53static void iot_tyo(device_t *device, int op2, int nac, int mb, int *io, int ac);
54static void iot_tyi(device_t *device, int op2, int nac, int mb, int *io, int ac);
55
56static void iot_dpy(device_t *device, int op2, int nac, int mb, int *io, int ac);
57
58static void iot_dia(device_t *device, int op2, int nac, int mb, int *io, int ac);
59static void iot_dba(device_t *device, int op2, int nac, int mb, int *io, int ac);
60static void iot_dcc(device_t *device, int op2, int nac, int mb, int *io, int ac);
61static void iot_dra(device_t *device, int op2, int nac, int mb, int *io, int ac);
62
63static void iot_011(device_t *device, int op2, int nac, int mb, int *io, int ac);
64
65static void iot_cks(device_t *device, int op2, int nac, int mb, int *io, int ac);
66
67
68/*
69    devices which are known to generate a completion pulse (source: maintainance manual 9-??,
70    and 9-20, 9-21):
71    emulated:
72    * perforated tape reader
73    * perforated tape punch
74    * typewriter output
75    * CRT display
76    unemulated:
77    * card punch (pac: 43)
78    * line printer (lpr, lsp, but NOT lfb)
79
80    This list should probably include additional optional devices (card reader comes to mind).
81*/
82
83/* IO status word */
84
85/* defines for io_status bits */
86enum
87{
88   io_st_pen = 0400000,   /* light pen: light has hit the pen */
89   io_st_ptr = 0200000,   /* perforated tape reader: reader buffer full */
90   io_st_tyo = 0100000,   /* typewriter out: device ready */
91   io_st_tyi = 0040000,   /* typewriter in: new character in buffer */
92   io_st_ptp = 0020000      /* perforated tape punch: device ready */
93};
94
95
96
97
98
99
100
101/* crt display timer */
102
103/* light pen config */
104
105
106
107#define PARALLEL_DRUM_WORD_TIME attotime::from_nsec(8500)
108#define PARALLEL_DRUM_ROTATION_TIME attotime::from_nsec(8500*4096)
109
110
111pdp1_reset_param_t pdp1_reset_param =
112{
113   {   /* external iot handlers.  NULL means that the iot is unimplemented, unless there are
114        parentheses around the iot name, in which case the iot is internal to the cpu core. */
115      /* I put a ? when the source is the handbook, since a) I have used the maintainance manual
116        as the primary source (as it goes more into details) b) the handbook and the maintainance
117        manual occasionnally contradict each other. */
118      /* dia, dba, dcc, dra are documented in MIT PDP-1 COMPUTER MODIFICATION
119        BULLETIN no. 2 (drumInstrWriteup.bin/drumInstrWriteup.txt), and are
120        similar to IOT documented in Parallel Drum Type 23 Instruction Manual. */
121   /*  (iot)       rpa         rpb         tyo         tyi         ppa         ppb         dpy */
122      NULL,      iot_rpa,   iot_rpb,   iot_tyo,   iot_tyi,   iot_ppa,   iot_ppb,   iot_dpy,
123   /*              spacewar                                                                 */
124      NULL,      iot_011,   NULL,      NULL,      NULL,      NULL,      NULL,      NULL,
125   /*                          lag                                             glf?/jsp?   gpl?/gpr?/gcf? */
126      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,
127   /*  rrb         rcb?        rcc?        cks         mcs         mes         mel          */
128      iot_rrb,   NULL,      NULL,      iot_cks,   NULL,      NULL,      NULL,      NULL,
129   /*  cad?        rac?        rbc?        pac                     lpr/lfb/lsp swc/sci/sdf?/shr?   scv? */
130      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,
131   /*  (dsc)       (asc)       (isb)       (cac)       (lsm)       (esm)       (cbs)        */
132      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,
133   /*  icv?        dia         dba         dcc         dra                     mri|rlc?    mrf/inr?/ccr? */
134      NULL,      iot_dia,   iot_dba,   iot_dcc,   iot_dra,   NULL,      NULL,      NULL,
135   /*  mcb|dur?    mwc|mtf?    mrc|sfc?... msm|cgo?    (eem/lem)   mic         muf          */
136      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,      NULL,
137   },
138   pdp1_tape_read_binary,
139   pdp1_io_sc_callback,
140   0,   /* extend mode support defined in input ports and pdp1_init_machine */
141   0,   /* hardware multiply/divide support defined in input ports and pdp1_init_machine */
142   0   /* type 20 sequence break system support defined in input ports and pdp1_init_machine */
143};
144
145MACHINE_RESET( pdp1 )
146{
147   pdp1_state *state = machine.driver_data<pdp1_state>();
148   int cfg;
149
150   cfg = input_port_read(machine, "CFG");
151   pdp1_reset_param.extend_support = (cfg >> pdp1_config_extend_bit) & pdp1_config_extend_mask;
152   pdp1_reset_param.hw_mul_div = (cfg >> pdp1_config_hw_mul_div_bit) & pdp1_config_hw_mul_div_mask;
153   pdp1_reset_param.type_20_sbs = (cfg >> pdp1_config_type_20_sbs_bit) & pdp1_config_type_20_sbs_mask;
154
155   /* reset device state */
156   state->m_tape_reader.rcl = state->m_tape_reader.rc = 0;
157   state->m_io_status = io_st_tyo | io_st_ptp;
158   state->m_lightpen.active = state->m_lightpen.down = 0;
159   state->m_lightpen.x = state->m_lightpen.y = 0;
160   state->m_lightpen.radius = 10;   /* ??? */
161   pdp1_update_lightpen_state(machine, &state->m_lightpen);
162}
163
164
165static void pdp1_machine_stop(running_machine &machine)
166{
167   pdp1_state *state = machine.driver_data<pdp1_state>();
168   /* the core will take care of freeing the timers, BUT we must set the variables
169    to NULL if we don't want to risk confusing the tape image init function */
170   state->m_tape_reader.timer = state->m_tape_puncher.timer = state->m_typewriter.tyo_timer = state->m_dpy_timer = NULL;
171}
172
173
174/*
175    driver init function
176
177    Set up the pdp1_memory pointer, and generate font data.
178*/
179MACHINE_START( pdp1 )
180{
181   pdp1_state *state = machine.driver_data<pdp1_state>();
182   UINT8 *dst;
183
184   static const unsigned char fontdata6x8[pdp1_fontdata_size] =
185   {   /* ASCII characters */
186      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x00,0x20,0x00,
187      0x50,0x50,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0xf8,0x50,0xf8,0x50,0x00,0x00,
188      0x20,0x70,0xc0,0x70,0x18,0xf0,0x20,0x00,0x40,0xa4,0x48,0x10,0x20,0x48,0x94,0x08,
189      0x60,0x90,0xa0,0x40,0xa8,0x90,0x68,0x00,0x10,0x20,0x40,0x00,0x00,0x00,0x00,0x00,
190      0x20,0x40,0x40,0x40,0x40,0x40,0x20,0x00,0x10,0x08,0x08,0x08,0x08,0x08,0x10,0x00,
191      0x20,0xa8,0x70,0xf8,0x70,0xa8,0x20,0x00,0x00,0x20,0x20,0xf8,0x20,0x20,0x00,0x00,
192      0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x60,0x00,0x00,0x00,0xf8,0x00,0x00,0x00,0x00,
193      0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
194      0x70,0x88,0x88,0x88,0x88,0x88,0x70,0x00,0x10,0x30,0x10,0x10,0x10,0x10,0x10,0x00,
195      0x70,0x88,0x08,0x10,0x20,0x40,0xf8,0x00,0x70,0x88,0x08,0x30,0x08,0x88,0x70,0x00,
196      0x10,0x30,0x50,0x90,0xf8,0x10,0x10,0x00,0xf8,0x80,0xf0,0x08,0x08,0x88,0x70,0x00,
197      0x70,0x80,0xf0,0x88,0x88,0x88,0x70,0x00,0xf8,0x08,0x08,0x10,0x20,0x20,0x20,0x00,
198      0x70,0x88,0x88,0x70,0x88,0x88,0x70,0x00,0x70,0x88,0x88,0x88,0x78,0x08,0x70,0x00,
199      0x00,0x00,0x30,0x30,0x00,0x30,0x30,0x00,0x00,0x00,0x30,0x30,0x00,0x30,0x30,0x60,
200      0x10,0x20,0x40,0x80,0x40,0x20,0x10,0x00,0x00,0x00,0xf8,0x00,0xf8,0x00,0x00,0x00,
201      0x40,0x20,0x10,0x08,0x10,0x20,0x40,0x00,0x70,0x88,0x08,0x10,0x20,0x00,0x20,0x00,
202      0x70,0x88,0xb8,0xa8,0xb8,0x80,0x70,0x00,0x70,0x88,0x88,0xf8,0x88,0x88,0x88,0x00,
203      0xf0,0x88,0x88,0xf0,0x88,0x88,0xf0,0x00,0x70,0x88,0x80,0x80,0x80,0x88,0x70,0x00,
204      0xf0,0x88,0x88,0x88,0x88,0x88,0xf0,0x00,0xf8,0x80,0x80,0xf0,0x80,0x80,0xf8,0x00,
205      0xf8,0x80,0x80,0xf0,0x80,0x80,0x80,0x00,0x70,0x88,0x80,0x98,0x88,0x88,0x70,0x00,
206      0x88,0x88,0x88,0xf8,0x88,0x88,0x88,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
207      0x08,0x08,0x08,0x08,0x88,0x88,0x70,0x00,0x88,0x90,0xa0,0xc0,0xa0,0x90,0x88,0x00,
208      0x80,0x80,0x80,0x80,0x80,0x80,0xf8,0x00,0x88,0xd8,0xa8,0x88,0x88,0x88,0x88,0x00,
209      0x88,0xc8,0xa8,0x98,0x88,0x88,0x88,0x00,0x70,0x88,0x88,0x88,0x88,0x88,0x70,0x00,
210      0xf0,0x88,0x88,0xf0,0x80,0x80,0x80,0x00,0x70,0x88,0x88,0x88,0x88,0x88,0x70,0x08,
211      0xf0,0x88,0x88,0xf0,0x88,0x88,0x88,0x00,0x70,0x88,0x80,0x70,0x08,0x88,0x70,0x00,
212      0xf8,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x88,0x88,0x88,0x88,0x88,0x88,0x70,0x00,
213      0x88,0x88,0x88,0x88,0x88,0x50,0x20,0x00,0x88,0x88,0x88,0x88,0xa8,0xd8,0x88,0x00,
214      0x88,0x50,0x20,0x20,0x20,0x50,0x88,0x00,0x88,0x88,0x88,0x50,0x20,0x20,0x20,0x00,
215      0xf8,0x08,0x10,0x20,0x40,0x80,0xf8,0x00,0x30,0x20,0x20,0x20,0x20,0x20,0x30,0x00,
216      0x40,0x40,0x20,0x20,0x10,0x10,0x08,0x08,0x30,0x10,0x10,0x10,0x10,0x10,0x30,0x00,
217      0x20,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xfc,
218      0x40,0x20,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x08,0x78,0x88,0x78,0x00,
219      0x80,0x80,0xf0,0x88,0x88,0x88,0xf0,0x00,0x00,0x00,0x70,0x88,0x80,0x80,0x78,0x00,
220      0x08,0x08,0x78,0x88,0x88,0x88,0x78,0x00,0x00,0x00,0x70,0x88,0xf8,0x80,0x78,0x00,
221      0x18,0x20,0x70,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x78,0x88,0x88,0x78,0x08,0x70,
222      0x80,0x80,0xf0,0x88,0x88,0x88,0x88,0x00,0x20,0x00,0x20,0x20,0x20,0x20,0x20,0x00,
223      0x20,0x00,0x20,0x20,0x20,0x20,0x20,0xc0,0x80,0x80,0x90,0xa0,0xe0,0x90,0x88,0x00,
224      0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0xf0,0xa8,0xa8,0xa8,0xa8,0x00,
225      0x00,0x00,0xb0,0xc8,0x88,0x88,0x88,0x00,0x00,0x00,0x70,0x88,0x88,0x88,0x70,0x00,
226      0x00,0x00,0xf0,0x88,0x88,0xf0,0x80,0x80,0x00,0x00,0x78,0x88,0x88,0x78,0x08,0x08,
227      0x00,0x00,0xb0,0xc8,0x80,0x80,0x80,0x00,0x00,0x00,0x78,0x80,0x70,0x08,0xf0,0x00,
228      0x20,0x20,0x70,0x20,0x20,0x20,0x18,0x00,0x00,0x00,0x88,0x88,0x88,0x98,0x68,0x00,
229      0x00,0x00,0x88,0x88,0x88,0x50,0x20,0x00,0x00,0x00,0xa8,0xa8,0xa8,0xa8,0x50,0x00,
230      0x00,0x00,0x88,0x50,0x20,0x50,0x88,0x00,0x00,0x00,0x88,0x88,0x88,0x78,0x08,0x70,
231      0x00,0x00,0xf8,0x10,0x20,0x40,0xf8,0x00,0x08,0x10,0x10,0x20,0x10,0x10,0x08,0x00,
232      0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x40,0x20,0x20,0x10,0x20,0x20,0x40,0x00,
233      0x00,0x68,0xb0,0x00,0x00,0x00,0x00,0x00,0x20,0x50,0x20,0x50,0xa8,0x50,0x00,0x00,
234
235      /* non-spacing middle dot */
236      0x00,
237      0x00,
238      0x00,
239      0x20,
240      0x00,
241      0x00,
242      0x00,
243      0x00,
244      /* non-spacing overstrike */
245      0x00,
246      0x00,
247      0x00,
248      0x00,
249      0xfc,
250      0x00,
251      0x00,
252      0x00,
253      /* implies */
254      0x00,
255      0x00,
256      0x70,
257      0x08,
258      0x08,
259      0x08,
260      0x70,
261      0x00,
262      /* or */
263      0x88,
264      0x88,
265      0x88,
266      0x50,
267      0x50,
268      0x50,
269      0x20,
270      0x00,
271      /* and */
272      0x20,
273      0x50,
274      0x50,
275      0x50,
276      0x88,
277      0x88,
278      0x88,
279      0x00,
280      /* up arrow */
281      0x20,
282      0x70,
283      0xa8,
284      0x20,
285      0x20,
286      0x20,
287      0x20,
288      0x00,
289      /* right arrow */
290      0x00,
291      0x20,
292      0x10,
293      0xf8,
294      0x10,
295      0x20,
296      0x00,
297      0x00,
298      /* multiply */
299      0x00,
300      0x88,
301      0x50,
302      0x20,
303      0x50,
304      0x88,
305      0x00,
306      0x00,
307   };
308
309   /* set up our font */
310   dst = machine.region("gfx1")->base();
311   memcpy(dst, fontdata6x8, pdp1_fontdata_size);
312
313   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(pdp1_machine_stop),&machine));
314
315   state->m_tape_reader.timer = machine.scheduler().timer_alloc(FUNC(reader_callback));
316   state->m_tape_puncher.timer = machine.scheduler().timer_alloc(FUNC(puncher_callback));
317   state->m_typewriter.tyo_timer = machine.scheduler().timer_alloc(FUNC(tyo_callback));
318   state->m_dpy_timer = machine.scheduler().timer_alloc(FUNC(dpy_callback));
319}
320
321
322/*
323    perforated tape handling
324*/
325
326void pdp1_get_open_mode(int id, unsigned int *readable, unsigned int *writeable, unsigned int *creatable)
327{
328   /* unit 0 is read-only, unit 1 is write-only */
329   if (id)
330   {
331      *readable = 0;
332      *writeable = 1;
333      *creatable = 1;
334   }
335   else
336   {
337      *readable = 1;
338      *writeable = 1;
339      *creatable = 1;
340   }
341}
342
343
344#if 0
345DEVICE_START( pdp1_tape )
346{
347}
348
349
350
351/*
352    Open a perforated tape image
353
354    unit 0 is reader (read-only), unit 1 is puncher (write-only)
355*/
356DEVICE_IMAGE_LOAD( pdp1_tape )
357{
358   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
359   int id = image_index_in_device(image);
360
361   switch (id)
362   {
363   case 0:
364      /* reader unit */
365      state->m_tape_reader.fd = image;
366
367      /* start motor */
368      state->m_tape_reader.motor_on = 1;
369
370      /* restart reader IO when necessary */
371      /* note that this function may be called before pdp1_init_machine, therefore
372        before state->m_tape_reader.timer is allocated.  It does not matter, as the clutch is never
373        down at power-up, but we must not call timer_enable with a NULL parameter! */
374      if (state->m_tape_reader.timer)
375      {
376         if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
377         {
378            /* delay is approximately 1/400s */
379            state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
380         }
381         else
382         {
383            state->m_tape_reader.timer->enable(0);
384         }
385      }
386      break;
387
388   case 1:
389      /* punch unit */
390      state->m_tape_puncher.fd = image;
391      break;
392   }
393
394   return IMAGE_INIT_PASS;
395}
396
397DEVICE_IMAGE_UNLOAD( pdp1_tape )
398{
399   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
400   int id = image_index_in_device(image);
401
402   switch (id)
403   {
404   case 0:
405      /* reader unit */
406      state->m_tape_reader.fd = NULL;
407
408      /* stop motor */
409      state->m_tape_reader.motor_on = 0;
410
411      if (state->m_tape_reader.timer)
412         state->m_tape_reader.timer->enable(0);
413      break;
414
415   case 1:
416      /* punch unit */
417      state->m_tape_puncher.fd = NULL;
418      break;
419   }
420}
421#endif
422/*
423    Read a byte from perforated tape
424*/
425static int tape_read(pdp1_state *state, UINT8 *reply)
426{
427   if (state->m_tape_reader.fd && (state->m_tape_reader.fd->fread(reply, 1) == 1))
428      return 0;   /* unit OK */
429   else
430      return 1;   /* unit not ready */
431}
432
433/*
434    Write a byte to perforated tape
435*/
436static void tape_write(pdp1_state *state, UINT8 data)
437{
438   if (state->m_tape_puncher.fd)
439      state->m_tape_puncher.fd->fwrite(& data, 1);
440}
441
442/*
443    common code for tape read commands (RPA, RPB, and read-in mode)
444*/
445static void begin_tape_read(pdp1_state *state, int binary, int nac)
446{
447   state->m_tape_reader.rb = 0;
448   state->m_tape_reader.rcl = 1;
449   state->m_tape_reader.rc = (binary) ? 1 : 3;
450   state->m_tape_reader.rby = (binary) ? 1 : 0;
451   state->m_tape_reader.rcp = nac;
452
453   if (LOG_IOT_OVERLAP)
454   {
455      if (state->m_tape_reader.timer->enable(0))
456         logerror("Error: overlapped perforated tape reads (Read-in mode, RPA/RPB instruction)\n");
457   }
458   /* set up delay if tape is advancing */
459   if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
460   {
461      /* delay is approximately 1/400s */
462      state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
463   }
464   else
465   {
466      state->m_tape_reader.timer->enable(0);
467   }
468}
469
470/*
471    timer callback to simulate reader IO
472*/
473static TIMER_CALLBACK(reader_callback)
474{
475   pdp1_state *state = machine.driver_data<pdp1_state>();
476   int not_ready;
477   UINT8 data;
478
479   if (state->m_tape_reader.rc)
480   {
481      not_ready = tape_read(state, & data);
482      if (not_ready)
483      {
484         state->m_tape_reader.motor_on = 0;   /* let us stop the motor */
485      }
486      else
487      {
488         if ((! state->m_tape_reader.rby) || (data & 0200))
489         {
490            state->m_tape_reader.rb |= (state->m_tape_reader.rby) ? (data & 077) : data;
491
492            if (state->m_tape_reader.rc != 3)
493            {
494               state->m_tape_reader.rb <<= 6;
495            }
496
497            state->m_tape_reader.rc = (state->m_tape_reader.rc+1) & 3;
498
499            if (state->m_tape_reader.rc == 0)
500            {   /* IO complete */
501               state->m_tape_reader.rcl = 0;
502               if (state->m_tape_reader.rcp)
503               {
504                  cpu_set_reg(machine.device("maincpu"), PDP1_IO, state->m_tape_reader.rb);   /* transfer reader buffer to IO */
505                  pdp1_pulse_iot_done(machine.device("maincpu"));
506               }
507               else
508                  state->m_io_status |= io_st_ptr;
509            }
510         }
511      }
512   }
513
514   if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
515      /* delay is approximately 1/400s */
516      state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
517   else
518      state->m_tape_reader.timer->enable(0);
519}
520
521/*
522    timer callback to generate punch completion pulse
523*/
524static TIMER_CALLBACK(puncher_callback)
525{
526   pdp1_state *state = machine.driver_data<pdp1_state>();
527   int nac = param;
528   state->m_io_status |= io_st_ptp;
529   if (nac)
530   {
531      pdp1_pulse_iot_done(machine.device("maincpu"));
532   }
533}
534
535/*
536    Initiate read of a 18-bit word in binary format from tape (used in read-in mode)
537*/
538void pdp1_tape_read_binary(device_t *device)
539{
540   pdp1_state *state = device->machine().driver_data<pdp1_state>();
541   begin_tape_read(state, 1, 1);
542}
543
544/*
545    perforated tape reader iot callbacks
546*/
547
548/*
549    rpa iot callback
550
551    op2: iot command operation (equivalent to mb & 077)
552    nac: nead a completion pulse
553    mb: contents of the MB register
554    io: pointer on the IO register
555    ac: contents of the AC register
556*/
557/*
558 * Read Perforated Tape, Alphanumeric
559 * rpa Address 0001
560 *
561 * This instruction reads one line of tape (all eight Channels) and transfers the resulting 8-bit code to
562 * the Reader Buffer. If bits 5 and 6 of the rpa function are both zero (720001), the contents of the
563 * Reader Buffer must be transferred to the IO Register by executing the rrb instruction. When the Reader
564 * Buffer has information ready to be transferred to the IO Register, Status Register Bit 1 is set to
565 * one. If bits 5 and 6 are different (730001 or 724001) the 8-bit code read from tape is automatically
566 * transferred to the IO Register via the Reader Buffer and appears as follows:
567 *
568 * IO Bits        10 11 12 13 14 15 16 17
569 * TAPE CHANNELS  8  7  6  5  4  3  2  1
570 */
571static void iot_rpa(device_t *device, int op2, int nac, int mb, int *io, int ac)
572{
573   pdp1_state *state = device->machine().driver_data<pdp1_state>();
574   if (LOG_IOT_EXTRA)
575      logerror("Warning, RPA instruction not fully emulated: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
576
577   begin_tape_read(state, 0, nac);
578}
579
580/*
581    rpb iot callback
582*/
583/*
584 * Read Perforated Tape, Binary rpb
585 * Address 0002
586 *
587 * The instruction reads three lines of tape (six Channels per line) and assembles
588 * the resulting 18-bit word in the Reader Buffer.  For a line to be recognized by
589 * this instruction Channel 8 must be punched (lines with Channel 8 not punched will
590 * be skipped over).  Channel 7 is ignored.  The instruction sub 5137, for example,
591 * appears on tape and is assembled by rpb as follows:
592 *
593 * Channel 8 7 6 5 4 | 3 2 1
594 * Line 1  X   X     |   X
595 * Line 2  X   X   X |     X
596 * Line 3  X     X X | X X X
597 * Reader Buffer 100 010 101 001 011 111
598 *
599 * (Vertical dashed line indicates sprocket holes and the symbols "X" indicate holes
600 * punched in tape).
601 *
602 * If bits 5 and 6 of the rpb instruction are both zero (720002), the contents of
603 * the Reader Buffer must be transferred to the IO Register by executing
604 * a rrb instruction.  When the Reader Buffer has information ready to be transferred
605 * to the IO Register, Status Register Bit 1 is set to one.  If bits 5 and 6 are
606 * different (730002 or 724002) the 18-bit word read from tape is automatically
607 * transferred to the IO Register via the Reader Buffer.
608 */
609static void iot_rpb(device_t *device, int op2, int nac, int mb, int *io, int ac)
610{
611   pdp1_state *state = device->machine().driver_data<pdp1_state>();
612   if (LOG_IOT_EXTRA)
613      logerror("Warning, RPB instruction not fully emulated: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
614
615   begin_tape_read(state, 1, nac);
616}
617
618/*
619    rrb iot callback
620*/
621static void iot_rrb(device_t *device, int op2, int nac, int mb, int *io, int ac)
622{
623   pdp1_state *state = device->machine().driver_data<pdp1_state>();
624   if (LOG_IOT_EXTRA)
625      logerror("RRB instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
626
627   *io = state->m_tape_reader.rb;
628   state->m_io_status &= ~io_st_ptr;
629}
630
631/*
632    perforated tape punch iot callbacks
633*/
634
635/*
636    ppa iot callback
637*/
638/*
639 * Punch Perforated Tape, Alphanumeric
640 * ppa Address 0005
641 *
642 * For each In-Out Transfer instruction one line of tape is punched. In-Out Register
643 * Bit 17 conditions Hole 1. Bit 16 conditions Hole 2, etc. Bit 10 conditions Hole 8
644 */
645static void iot_ppa(device_t *device, int op2, int nac, int mb, int *io, int ac)
646{
647   pdp1_state *state = device->machine().driver_data<pdp1_state>();
648   if (LOG_IOT_EXTRA)
649      logerror("PPA instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
650
651   tape_write(state, *io & 0377);
652   state->m_io_status &= ~io_st_ptp;
653   /* delay is approximately 1/63.3 second */
654   if (LOG_IOT_OVERLAP)
655   {
656      if (state->m_tape_puncher.timer->enable(0))
657         logerror("Error: overlapped PPA/PPB instructions: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
658   }
659
660   state->m_tape_puncher.timer->adjust(attotime::from_usec(15800), nac);
661}
662
663/*
664    ppb iot callback
665*/
666/*
667 * Punch Perforated Tape, Binary
668 * ppb Addres 0006
669 *
670 * For each In-Out Transfer instruction one line of tape is punched. In-Out Register
671 * Bit 5 conditions Hole 1. Bit 4 conditions Hole 2, etc. Bit 0 conditions Hole 6.
672 * Hole 7 is left blank. Hole 8 is always punched in this mode.
673 */
674static void iot_ppb(device_t *device, int op2, int nac, int mb, int *io, int ac)
675{
676   pdp1_state *state = device->machine().driver_data<pdp1_state>();
677   if (LOG_IOT_EXTRA)
678      logerror("PPB instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
679
680   tape_write(state, (*io >> 12) | 0200);
681   state->m_io_status &= ~io_st_ptp;
682   /* delay is approximately 1/63.3 second */
683   if (LOG_IOT_OVERLAP)
684   {
685      if (state->m_tape_puncher.timer->enable(0))
686         logerror("Error: overlapped PPA/PPB instructions: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
687   }
688   state->m_tape_puncher.timer->adjust(attotime::from_usec(15800), nac);
689}
690
691
692/*
693    Typewriter handling
694
695    The alphanumeric on-line typewriter is a standard device on pdp-1: it
696    can both handle keyboard input and print output text.
697*/
698
699/*
700    Open a file for typewriter output
701*/
702DEVICE_IMAGE_LOAD(pdp1_typewriter)
703{
704   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
705   /* open file */
706   state->m_typewriter.fd = &image;
707
708   state->m_io_status |= io_st_tyo;
709
710   return IMAGE_INIT_PASS;
711}
712
713DEVICE_IMAGE_UNLOAD(pdp1_typewriter)
714{
715   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
716   state->m_typewriter.fd = NULL;
717}
718
719/*
720    Write a character to typewriter
721*/
722static void typewriter_out(running_machine &machine, UINT8 data)
723{
724   pdp1_state *state = machine.driver_data<pdp1_state>();
725   if (LOG_IOT_EXTRA)
726      logerror("typewriter output %o\n", data);
727
728   pdp1_typewriter_drawchar(machine, data);
729   if (state->m_typewriter.fd)
730#if 1
731      state->m_typewriter.fd->fwrite(& data, 1);
732#else
733   {
734      static const char ascii_table[2][64] =
735      {   /* n-s = non-spacing */
736         {   /* lower case */
737            ' ',            '1',            '2',            '3',
738            '4',            '5',            '6',            '7',
739            '8',            '9',            '*',            '*',
740            '*',            '*',            '*',            '*',
741            '0',            '/',            's',            't',
742            'u',            'v',            'w',            'x',
743            'y',            'z',            '*',            ',',
744            '*',/*black*/      '*',/*red*/         '*',/*Tab*/         '*',
745            '\200',/*n-s middle dot*/'j',         'k',            'l',
746            'm',            'n',            'o',            'p',
747            'q',            'r',            '*',            '*',
748            '-',            ')',            '\201',/*n-s overstrike*/'(',
749            '*',            'a',            'b',            'c',
750            'd',            'e',            'f',            'g',
751            'h',            'i',            '*',/*Lower Case*/   '.',
752            '*',/*Upper Case*/   '*',/*Backspace*/   '*',            '*'/*Carriage Return*/
753         },
754         {   /* upper case */
755            ' ',            '"',            '\'',            '~',
756            '\202',/*implies*/   '\203',/*or*/      '\204',/*and*/      '<',
757            '>',            '\205',/*up arrow*/   '*',            '*',
758            '*',            '*',            '*',            '*',
759            '\206',/*right arrow*/'?',            'S',            'T',
760            'U',            'V',            'W',            'X',
761            'Y',            'Z',            '*',            '=',
762            '*',/*black*/      '*',/*red*/         '\t',/*Tab*/      '*',
763            '_',/*n-s???*/      'J',            'K',            'L',
764            'M',            'N',            'O',            'P',
765            'Q',            'R',            '*',            '*',
766            '+',            ']',            '|',/*n-s???*/      '[',
767            '*',            'A',            'B',            'C',
768            'D',            'E',            'F',            'G',
769            'H',            'I',            '*',/*Lower Case*/   '\207',/*multiply*/
770            '*',/*Upper Case*/   '\b',/*Backspace*/   '*',            '*'/*Carriage Return*/
771         }
772      };
773
774      data &= 0x3f;
775
776      switch (data)
777      {
778      case 034:
779         /* Black: ignore */
780         //color = color_typewriter_black;
781         {
782            static const char black[5] = { '\033', '[', '3', '0', 'm' };
783            image_fwrite(state->m_typewriter.fd, black, sizeof(black));
784         }
785         break;
786
787      case 035:
788         /* Red: ignore */
789         //color = color_typewriter_red;
790         {
791            static const char red[5] = { '\033', '[', '3', '1', 'm' };
792            image_fwrite(state->m_typewriter.fd, red, sizeof(red));
793         }
794         break;
795
796      case 072:
797         /* Lower case */
798         state->m_case_shift = 0;
799         break;
800
801      case 074:
802         /* Upper case */
803         state->m_case_shift = 1;
804         break;
805
806      case 077:
807         /* Carriage Return */
808         {
809            static const char line_end[2] = { '\r', '\n' };
810            image_fwrite(state->m_typewriter.fd, line_end, sizeof(line_end));
811         }
812         break;
813
814      default:
815         /* Any printable character... */
816
817         if ((data != 040) && (data != 056))   /* 040 and 056 are non-spacing characters: don't try to print right now */
818            /* print character (lookup ASCII equivalent in table) */
819            image_fwrite(state->m_typewriter.fd, & ascii_table[state->m_case_shift][data], 1);
820
821         break;
822      }
823   }
824#endif
825}
826
827/*
828    timer callback to generate typewriter completion pulse
829*/
830static TIMER_CALLBACK(tyo_callback)
831{
832   pdp1_state *state = machine.driver_data<pdp1_state>();
833   int nac = param;
834   state->m_io_status |= io_st_tyo;
835   if (nac)
836   {
837      pdp1_pulse_iot_done(machine.device("maincpu"));
838   }
839}
840
841/*
842    tyo iot callback
843*/
844static void iot_tyo(device_t *device, int op2, int nac, int mb, int *io, int ac)
845{
846   pdp1_state *state = device->machine().driver_data<pdp1_state>();
847   int ch, delay;
848
849   if (LOG_IOT_EXTRA)
850      logerror("Warning, TYO instruction not fully emulated: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
851
852   ch = (*io) & 077;
853
854   typewriter_out(device->machine(), ch);
855   state->m_io_status &= ~io_st_tyo;
856
857   /* compute completion delay (source: maintainance manual 9-12, 9-13 and 9-14) */
858   switch (ch)
859   {
860   case 072:   /* lower-case */
861   case 074:   /* upper-case */
862   case 034:   /* black */
863   case 035:   /* red */
864      delay = 175;   /* approximately 175ms (?) */
865      break;
866
867   case 077:   /* carriage return */
868      delay = 205;   /* approximately 205ms (?) */
869      break;
870
871   default:
872      delay = 105;   /* approximately 105ms */
873      break;
874   }
875   if (LOG_IOT_OVERLAP)
876   {
877      if (state->m_typewriter.tyo_timer->enable(0))
878         logerror("Error: overlapped TYO instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
879   }
880
881   state->m_typewriter.tyo_timer->adjust(attotime::from_msec(delay), nac);
882}
883
884/*
885    tyi iot callback
886*/
887/* When a typewriter key is struck, the code for the struck key is placed in the
888 * typewriter buffer, Program Flag 1 is set, and the type-in status bit is set to
889 * one. A program designed to accept typed-in data would periodically check
890 * Program Flag 1, and if found to be set, an In-Out Transfer Instruction with
891 * address 4 could be executed for the information to be transferred to the
892 * In-Out Register. This In-Out Transfer should not use the optional in-out wait.
893 * The information contained in the typewriter buffer is then transferred to the
894 * right six bits of the In-Out Register. The tyi instruction automatically
895 * clears the In-Out Register before transferring the information and also clears
896 * the type-in status bit.
897 */
898static void iot_tyi(device_t *device, int op2, int nac, int mb, int *io, int ac)
899{
900   pdp1_state *state = device->machine().driver_data<pdp1_state>();
901   if (LOG_IOT_EXTRA)
902      logerror("Warning, TYI instruction not fully emulated: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
903
904   *io = state->m_typewriter.tb;
905   if (! (state->m_io_status & io_st_tyi))
906      *io |= 0100;
907   else
908   {
909      state->m_io_status &= ~io_st_tyi;
910      if (USE_SBS)
911         cputag_set_input_line_and_vector(device->machine(), "maincpu", 0, CLEAR_LINE, 0);   /* interrupt it, baby */
912   }
913}
914
915
916/*
917 * PRECISION CRT DISPLAY (TYPE 30)
918 *
919 * This sixteen-inch cathode ray tube display is intended to be used as an on-line output device for the
920 * PDP-1. It is useful for high speed presentation of graphs, diagrams, drawings, and alphanumerical
921 * information. The unit is solid state, self-powered and completely buffered. It has magnetic focus and
922 * deflection.
923 *
924 * Display characteristics are as follows:
925 *
926 *     Random point plotting
927 *     Accuracy of points +/-3 per cent of raster size
928 *     Raster size 9.25 by 9.25 inches
929 *     1024 by 1024 addressable locations
930 *     Fixed origin at center of CRT
931 *     Ones complement binary arithmetic
932 *     Plots 20,000 points per second
933 *
934 * Resolution is such that 512 points along each axis are discernable on the face of the tube.
935 *
936 * One instruction is added to the PDP-1 with the installation of this display:
937 *
938 * Display One Point On CRT
939 * dpy Address 0007
940 *
941 * This instruction clears the light pen status bit and displays one point using bits 0 through 9 of the
942 * AC to represent the (signed) X coordinate of the point and bits 0 through 9 of the IO as the (signed)
943 * Y coordinate.
944 *
945 * Many variations of the Type 30 Display are available. Some of these include hardware for line and
946 * curve generation.
947 */
948
949
950/*
951    timer callback to generate crt completion pulse
952*/
953static TIMER_CALLBACK(dpy_callback)
954{
955   pdp1_pulse_iot_done(machine.device("maincpu"));
956}
957
958
959/*
960    dpy iot callback
961
962    Light on one pixel on CRT
963*/
964static void iot_dpy(device_t *device, int op2, int nac, int mb, int *io, int ac)
965{
966   pdp1_state *state = device->machine().driver_data<pdp1_state>();
967   int x;
968   int y;
969
970
971   x = ((ac+0400000) & 0777777) >> 8;
972   y = (((*io)+0400000) & 0777777) >> 8;
973   pdp1_plot(device->machine(), x, y);
974
975   /* light pen 32 support */
976   state->m_io_status &= ~io_st_pen;
977
978   if (state->m_lightpen.down && ((x-state->m_lightpen.x)*(x-state->m_lightpen.x)+(y-state->m_lightpen.y)*(y-state->m_lightpen.y) < state->m_lightpen.radius*state->m_lightpen.radius))
979   {
980      state->m_io_status |= io_st_pen;
981
982      cpu_set_reg(device->machine().device("maincpu"), PDP1_PF3, 1);
983   }
984
985   if (nac)
986   {
987      /* 50us delay */
988      if (LOG_IOT_OVERLAP)
989      {
990         /* note that overlap detection is incomplete: it will only work if both DPY
991            instructions require a completion pulse */
992         if (state->m_dpy_timer->enable(0))
993            logerror("Error: overlapped DPY instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
994      }
995      state->m_dpy_timer->adjust(attotime::from_usec(50));
996   }
997}
998
999
1000
1001/*
1002    MIT parallel drum (variant of type 23)
1003*/
1004
1005static void parallel_drum_set_il(pdp1_state *state, int il)
1006{
1007   attotime il_phase;
1008
1009   state->m_parallel_drum.il = il;
1010
1011   il_phase = ((PARALLEL_DRUM_WORD_TIME * il) - state->m_parallel_drum.rotation_timer->elapsed());
1012   if (il_phase < attotime::zero)
1013      il_phase = il_phase + PARALLEL_DRUM_ROTATION_TIME;
1014   state->m_parallel_drum.il_timer->adjust(il_phase, 0, PARALLEL_DRUM_ROTATION_TIME);
1015}
1016
1017#ifdef UNUSED_FUNCTION
1018static TIMER_CALLBACK(il_timer_callback)
1019{
1020   pdp1_state *state = machine.driver_data<pdp1_state>();
1021   if (state->m_parallel_drum.dba)
1022   {
1023      /* set break request and status bit 5 */
1024      /* ... */
1025   }
1026}
1027
1028static void parallel_drum_init(pdp1_state *state)
1029{
1030   state->m_parallel_drum.rotation_timer = machine.scheduler().timer_alloc();
1031   state->m_parallel_drum.rotation_timer->adjust(PARALLEL_DRUM_ROTATION_TIME, 0, PARALLEL_DRUM_ROTATION_TIME);
1032
1033   state->m_parallel_drum.il_timer = machine.scheduler().timer_alloc(FUNC(il_timer_callback));
1034   parallel_drum_set_il(0);
1035}
1036#endif
1037
1038/*
1039    Open a file for drum
1040*/
1041DEVICE_IMAGE_LOAD(pdp1_drum)
1042{
1043   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
1044   /* open file */
1045   state->m_parallel_drum.fd = &image;
1046
1047   return IMAGE_INIT_PASS;
1048}
1049
1050DEVICE_IMAGE_UNLOAD(pdp1_drum)
1051{
1052   pdp1_state *state = image.device().machine().driver_data<pdp1_state>();
1053   state->m_parallel_drum.fd = NULL;
1054}
1055
1056static void iot_dia(device_t *device, int op2, int nac, int mb, int *io, int ac)
1057{
1058   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1059   state->m_parallel_drum.wfb = ((*io) & 0370000) >> 12;
1060   parallel_drum_set_il(state, (*io) & 0007777);
1061
1062   state->m_parallel_drum.dba = 0;   /* right? */
1063}
1064
1065static void iot_dba(device_t *device, int op2, int nac, int mb, int *io, int ac)
1066{
1067   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1068   state->m_parallel_drum.wfb = ((*io) & 0370000) >> 12;
1069   parallel_drum_set_il(state, (*io) & 0007777);
1070
1071   state->m_parallel_drum.dba = 1;
1072}
1073
1074/*
1075    Read a word from drum
1076*/
1077static UINT32 drum_read(pdp1_state *state, int field, int position)
1078{
1079   int offset = (field*4096+position)*3;
1080   UINT8 buf[3];
1081
1082   if (state->m_parallel_drum.fd && (!state->m_parallel_drum.fd->fseek(offset, SEEK_SET)) && (state->m_parallel_drum.fd->fread( buf, 3) == 3))
1083      return ((buf[0] << 16) | (buf[1] << 8) | buf[2]) & 0777777;
1084
1085   return 0;
1086}
1087
1088/*
1089    Write a word to drum
1090*/
1091static void drum_write(pdp1_state *state, int field, int position, UINT32 data)
1092{
1093   int offset = (field*4096+position)*3;
1094   UINT8 buf[3];
1095
1096   if (state->m_parallel_drum.fd)
1097   {
1098      buf[0] = data >> 16;
1099      buf[1] = data >> 8;
1100      buf[2] = data;
1101
1102      state->m_parallel_drum.fd->fseek(offset, SEEK_SET);
1103      state->m_parallel_drum.fd->fwrite( buf, 3);
1104   }
1105}
1106
1107static void iot_dcc(device_t *device, int op2, int nac, int mb, int *io, int ac)
1108{
1109   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1110   attotime delay;
1111   int dc;
1112
1113   state->m_parallel_drum.rfb = ((*io) & 0370000) >> 12;
1114   state->m_parallel_drum.wc = - ((*io) & 0007777);
1115
1116   state->m_parallel_drum.wcl = ac & 0177777/*0007777???*/;
1117
1118   state->m_parallel_drum.dba = 0;   /* right? */
1119   /* clear status bit 5... */
1120
1121   /* do transfer */
1122   delay = state->m_parallel_drum.il_timer->remaining();
1123   dc = state->m_parallel_drum.il;
1124   do
1125   {
1126      if ((state->m_parallel_drum.wfb >= 1) && (state->m_parallel_drum.wfb <= 22))
1127      {
1128         drum_write(state, state->m_parallel_drum.wfb-1, dc, (signed)device->machine().device("maincpu")->memory().space(AS_PROGRAM)->read_dword(state->m_parallel_drum.wcl<<2));
1129      }
1130
1131      if ((state->m_parallel_drum.rfb >= 1) && (state->m_parallel_drum.rfb <= 22))
1132      {
1133         device->machine().device("maincpu")->memory().space(AS_PROGRAM)->write_dword(state->m_parallel_drum.wcl<<2, drum_read(state, state->m_parallel_drum.rfb-1, dc));
1134      }
1135
1136      state->m_parallel_drum.wc = (state->m_parallel_drum.wc+1) & 07777;
1137      state->m_parallel_drum.wcl = (state->m_parallel_drum.wcl+1) & 0177777/*0007777???*/;
1138      dc = (dc+1) & 07777;
1139      if (state->m_parallel_drum.wc)
1140         delay = delay + PARALLEL_DRUM_WORD_TIME;
1141   } while (state->m_parallel_drum.wc);
1142   device_adjust_icount(device->machine().device("maincpu"),-device->machine().device<cpu_device>("maincpu")->attotime_to_cycles(delay));
1143   /* if no error, skip */
1144   cpu_set_reg(device->machine().device("maincpu"), PDP1_PC, cpu_get_reg(device->machine().device("maincpu"), PDP1_PC)+1);
1145}
1146
1147static void iot_dra(device_t *device, int op2, int nac, int mb, int *io, int ac)
1148{
1149   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1150   (*io) = (state->m_parallel_drum.rotation_timer->elapsed() *
1151      (ATTOSECONDS_PER_SECOND / (PARALLEL_DRUM_WORD_TIME.as_attoseconds()))).seconds & 0007777;
1152
1153   /* set parity error and timing error... */
1154}
1155
1156
1157
1158/*
1159    iot 11 callback
1160
1161    Read state of Spacewar! controllers
1162
1163    Not documented, except a few comments in the Spacewar! source code:
1164        it should leave the control word in the io as follows.
1165        high order 4 bits, rotate ccw, rotate cw, (both mean hyperspace)
1166        fire rocket, and fire torpedo. low order 4 bits, same for
1167        other ship. routine is entered by jsp cwg.
1168*/
1169static void iot_011(device_t *device, int op2, int nac, int mb, int *io, int ac)
1170{
1171   int key_state = input_port_read(device->machine(), "SPACEWAR");
1172   int reply;
1173
1174
1175   reply = 0;
1176
1177   if (key_state & ROTATE_RIGHT_PLAYER2)
1178      reply |= 0400000;
1179   if (key_state & ROTATE_LEFT_PLAYER2)
1180      reply |= 0200000;
1181   if (key_state & THRUST_PLAYER2)
1182      reply |= 0100000;
1183   if (key_state & FIRE_PLAYER2)
1184      reply |= 0040000;
1185   if (key_state & HSPACE_PLAYER2)
1186      reply |= 0600000;
1187
1188   if (key_state & ROTATE_RIGHT_PLAYER1)
1189      reply |= 010;
1190   if (key_state & ROTATE_LEFT_PLAYER1)
1191      reply |= 004;
1192   if (key_state & THRUST_PLAYER1)
1193      reply |= 002;
1194   if (key_state & FIRE_PLAYER1)
1195      reply |= 001;
1196   if (key_state & HSPACE_PLAYER1)
1197      reply |= 014;
1198
1199   *io = reply;
1200}
1201
1202
1203/*
1204    cks iot callback
1205
1206    check IO status
1207*/
1208static void iot_cks(device_t *device, int op2, int nac, int mb, int *io, int ac)
1209{
1210   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1211   if (LOG_IOT_EXTRA)
1212      logerror("CKS instruction: mb=0%06o, (%s)\n", (unsigned) mb, device->machine().describe_context());
1213
1214   *io = state->m_io_status;
1215}
1216
1217
1218/*
1219    callback which is called when Start Clear is pulsed
1220
1221    IO devices should reset
1222*/
1223void pdp1_io_sc_callback(device_t *device)
1224{
1225   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1226   state->m_tape_reader.rcl = state->m_tape_reader.rc = 0;
1227   if (state->m_tape_reader.timer)
1228      state->m_tape_reader.timer->enable(0);
1229
1230   if (state->m_tape_puncher.timer)
1231      state->m_tape_puncher.timer->enable(0);
1232
1233   if (state->m_typewriter.tyo_timer)
1234      state->m_typewriter.tyo_timer->enable(0);
1235
1236   if (state->m_dpy_timer)
1237      state->m_dpy_timer->enable(0);
1238
1239   state->m_io_status = io_st_tyo | io_st_ptp;
1240}
1241
1242
1243/*
1244    typewriter keyboard handler
1245*/
1246static void pdp1_keyboard(running_machine &machine)
1247{
1248   pdp1_state *state = machine.driver_data<pdp1_state>();
1249   int i;
1250   int j;
1251
1252   int typewriter_keys[4];
1253
1254   int typewriter_transitions;
1255   static const char *const twrnames[] = { "TWR0", "TWR1", "TWR2", "TWR3" };
1256
1257
1258   for (i=0; i<4; i++)
1259   {
1260      typewriter_keys[i] = input_port_read(machine, twrnames[i]);
1261   }
1262
1263   for (i=0; i<4; i++)
1264   {
1265      typewriter_transitions = typewriter_keys[i] & (~ state->m_old_typewriter_keys[i]);
1266      if (typewriter_transitions)
1267      {
1268         for (j=0; (((typewriter_transitions >> j) & 1) == 0) /*&& (j<16)*/; j++)
1269            ;
1270         state->m_typewriter.tb = (i << 4) + j;
1271         state->m_io_status |= io_st_tyi;
1272         #if USE_SBS
1273            cputag_set_input_line_and_vector(machine, "maincpu", 0, ASSERT_LINE, 0);   /* interrupt it, baby */
1274         #endif
1275         cpu_set_reg(machine.device("maincpu"), PDP1_PF1, 1);
1276         pdp1_typewriter_drawchar(machine, state->m_typewriter.tb);   /* we want to echo input */
1277         break;
1278      }
1279   }
1280
1281   for (i=0; i<4; i++)
1282      state->m_old_typewriter_keys[i] = typewriter_keys[i];
1283}
1284
1285static void pdp1_lightpen(running_machine &machine)
1286{
1287   pdp1_state *state = machine.driver_data<pdp1_state>();
1288   int x_delta, y_delta;
1289   int current_state;
1290
1291   state->m_lightpen.active = (input_port_read(machine, "CFG") >> pdp1_config_lightpen_bit) & pdp1_config_lightpen_mask;
1292
1293   current_state = input_port_read(machine, "LIGHTPEN");
1294
1295   /* update pen down state */
1296   state->m_lightpen.down = state->m_lightpen.active && (current_state & pdp1_lightpen_down);
1297
1298   /* update size of pen tip hole */
1299   if ((current_state & ~state->m_old_lightpen) & pdp1_lightpen_smaller)
1300   {
1301      state->m_lightpen.radius --;
1302      if (state->m_lightpen.radius < 0)
1303         state->m_lightpen.radius = 0;
1304   }
1305   if ((current_state & ~state->m_old_lightpen) & pdp1_lightpen_larger)
1306   {
1307      state->m_lightpen.radius ++;
1308      if (state->m_lightpen.radius > 32)
1309         state->m_lightpen.radius = 32;
1310   }
1311
1312   state->m_old_lightpen = current_state;
1313
1314   /* update pen position */
1315   x_delta = input_port_read(machine, "LIGHTX");
1316   y_delta = input_port_read(machine, "LIGHTY");
1317
1318   if (x_delta >= 0x80)
1319      x_delta -= 0x100;
1320   if (y_delta >= 0x80)
1321      y_delta -= 256;
1322
1323   state->m_lightpen.x += x_delta;
1324   state->m_lightpen.y += y_delta;
1325
1326   if (state->m_lightpen.x < 0)
1327      state->m_lightpen.x = 0;
1328   if (state->m_lightpen.x > 1023)
1329      state->m_lightpen.x = 1023;
1330   if (state->m_lightpen.y < 0)
1331      state->m_lightpen.y = 0;
1332   if (state->m_lightpen.y > 1023)
1333      state->m_lightpen.y = 1023;
1334
1335   pdp1_update_lightpen_state(machine, &state->m_lightpen);
1336}
1337
1338/*
1339    Not a real interrupt - just handle keyboard input
1340*/
1341INTERRUPT_GEN( pdp1_interrupt )
1342{
1343   pdp1_state *state = device->machine().driver_data<pdp1_state>();
1344   running_machine &machine = device->machine();
1345   int control_keys;
1346   int tw_keys;
1347   int ta_keys;
1348
1349   int control_transitions;
1350   int tw_transitions;
1351   int ta_transitions;
1352
1353
1354   cpu_set_reg(device, PDP1_SS, input_port_read(device->machine(), "SENSE"));
1355
1356   /* read new state of control keys */
1357   control_keys = input_port_read(device->machine(), "CSW");
1358
1359   if (control_keys & pdp1_control)
1360   {
1361      /* compute transitions */
1362      control_transitions = control_keys & (~ state->m_old_control_keys);
1363
1364      if (control_transitions & pdp1_extend)
1365      {
1366         cpu_set_reg(device, PDP1_EXTEND_SW, ! cpu_get_reg(device, PDP1_EXTEND_SW));
1367      }
1368      if (control_transitions & pdp1_start_nobrk)
1369      {
1370         pdp1_pulse_start_clear(device);   /* pulse Start Clear line */
1371         cpu_set_reg(device, PDP1_EXD, cpu_get_reg(device, PDP1_EXTEND_SW));
1372         cpu_set_reg(device, PDP1_SBM, (UINT64)0);
1373         cpu_set_reg(device, PDP1_OV, (UINT64)0);
1374         cpu_set_reg(device, PDP1_PC, cpu_get_reg(device, PDP1_TA));
1375         cpu_set_reg(device, PDP1_RUN, 1);
1376      }
1377      if (control_transitions & pdp1_start_brk)
1378      {
1379         pdp1_pulse_start_clear(device);   /* pulse Start Clear line */
1380         cpu_set_reg(device, PDP1_EXD, cpu_get_reg(device, PDP1_EXTEND_SW));
1381         cpu_set_reg(device, PDP1_SBM, 1);
1382         cpu_set_reg(device, PDP1_OV, (UINT64)0);
1383         cpu_set_reg(device, PDP1_PC, cpu_get_reg(device, PDP1_TA));
1384         cpu_set_reg(device, PDP1_RUN, 1);
1385      }
1386      if (control_transitions & pdp1_stop)
1387      {
1388         cpu_set_reg(device, PDP1_RUN, (UINT64)0);
1389         cpu_set_reg(device, PDP1_RIM, (UINT64)0);   /* bug : we stop after reading an even-numbered word
1390                                            (i.e. data), whereas a real pdp-1 stops after reading
1391                                            an odd-numbered word (i.e. dio instruciton) */
1392      }
1393      if (control_transitions & pdp1_continue)
1394      {
1395         cpu_set_reg(device, PDP1_RUN, 1);
1396      }
1397      if (control_transitions & pdp1_examine)
1398      {
1399         pdp1_pulse_start_clear(device);   /* pulse Start Clear line */
1400         cpu_set_reg(device, PDP1_PC, cpu_get_reg(device, PDP1_TA));
1401         cpu_set_reg(device, PDP1_MA, cpu_get_reg(device, PDP1_PC));
1402         cpu_set_reg(device, PDP1_IR, LAC);   /* this instruction is actually executed */
1403
1404         cpu_set_reg(device, PDP1_MB, (signed)device->memory().space(AS_PROGRAM)->read_dword(PDP1_MA<<2));
1405         cpu_set_reg(device, PDP1_AC, cpu_get_reg(device, PDP1_MB));
1406      }
1407      if (control_transitions & pdp1_deposit)
1408      {
1409         pdp1_pulse_start_clear(device);   /* pulse Start Clear line */
1410         cpu_set_reg(device, PDP1_PC, cpu_get_reg(device, PDP1_TA));
1411         cpu_set_reg(device, PDP1_MA, cpu_get_reg(device, PDP1_PC));
1412         cpu_set_reg(device, PDP1_AC, cpu_get_reg(device, PDP1_TW));
1413         cpu_set_reg(device, PDP1_IR, DAC);   /* this instruction is actually executed */
1414
1415         cpu_set_reg(device, PDP1_MB, cpu_get_reg(device, PDP1_AC));
1416         device->memory().space(AS_PROGRAM)->write_dword(cpu_get_reg(device, PDP1_MA)<<2, cpu_get_reg(device, PDP1_MB));
1417      }
1418      if (control_transitions & pdp1_read_in)
1419      {   /* set cpu to read instructions from perforated tape */
1420         pdp1_pulse_start_clear(device);   /* pulse Start Clear line */
1421         cpu_set_reg(device, PDP1_PC, (  cpu_get_reg(device, PDP1_TA) & 0170000)
1422                              |  (cpu_get_reg(device, PDP1_PC) & 0007777));   /* transfer ETA to EPC */
1423         /*cpu_set_reg(machine.device("maincpu"), PDP1_MA, cpu_get_reg(machine.device("maincpu"), PDP1_PC));*/
1424         cpu_set_reg(device, PDP1_EXD, cpu_get_reg(device, PDP1_EXTEND_SW));
1425         cpu_set_reg(device, PDP1_OV, (UINT64)0);      /* right??? */
1426         cpu_set_reg(device, PDP1_RUN, (UINT64)0);
1427         cpu_set_reg(device, PDP1_RIM, 1);
1428      }
1429      if (control_transitions & pdp1_reader)
1430      {
1431      }
1432      if (control_transitions & pdp1_tape_feed)
1433      {
1434      }
1435      if (control_transitions & pdp1_single_step)
1436      {
1437         cpu_set_reg(device, PDP1_SNGL_STEP, ! cpu_get_reg(device, PDP1_SNGL_STEP));
1438      }
1439      if (control_transitions & pdp1_single_inst)
1440      {
1441         cpu_set_reg(device, PDP1_SNGL_INST, ! cpu_get_reg(device, PDP1_SNGL_INST));
1442      }
1443
1444      /* remember new state of control keys */
1445      state->m_old_control_keys = control_keys;
1446
1447
1448      /* handle test word keys */
1449      tw_keys = (input_port_read(machine, "TWDMSB") << 16) | input_port_read(machine, "TWDLSB");
1450
1451      /* compute transitions */
1452      tw_transitions = tw_keys & (~ state->m_old_tw_keys);
1453
1454      if (tw_transitions)
1455         cpu_set_reg(device, PDP1_TW, cpu_get_reg(device, PDP1_TW) ^ tw_transitions);
1456
1457      /* remember new state of test word keys */
1458      state->m_old_tw_keys = tw_keys;
1459
1460
1461      /* handle address keys */
1462      ta_keys = input_port_read(machine, "TSTADD");
1463
1464      /* compute transitions */
1465      ta_transitions = ta_keys & (~ state->m_old_ta_keys);
1466
1467      if (ta_transitions)
1468         cpu_set_reg(device, PDP1_TA, cpu_get_reg(device, PDP1_TA) ^ ta_transitions);
1469
1470      /* remember new state of test word keys */
1471      state->m_old_ta_keys = ta_keys;
1472
1473   }
1474   else
1475   {
1476      state->m_old_control_keys = 0;
1477      state->m_old_tw_keys = 0;
1478      state->m_old_ta_keys = 0;
1479
1480      pdp1_keyboard(machine);
1481   }
1482
1483   pdp1_lightpen(machine);
1484}
src/mess/machine/tx0.c
r12731r12732
1/*
2    TX-0
3*
4    Raphael Nabet, 2004
5*/
6
7#include "emu.h"
8#include "cpu/pdp1/tx0.h"
9#include "includes/tx0.h"
10#include "video/crt.h"
11
12
13static TIMER_CALLBACK(reader_callback);
14static TIMER_CALLBACK(puncher_callback);
15static TIMER_CALLBACK(prt_callback);
16static TIMER_CALLBACK(dis_callback);
17
18
19
20
21
22
23
24
25/* crt display timer */
26
27
28
29enum
30{
31   PF_RWC = 040,
32   PF_EOR = 020,
33   PF_PC  = 010,
34   PF_EOT = 004
35};
36
37
38MACHINE_RESET( tx0 )
39{
40   tx0_state *state = machine.driver_data<tx0_state>();
41   /* reset device state */
42   state->m_tape_reader.rcl = state->m_tape_reader.rc = 0;
43}
44
45
46static void tx0_machine_stop(running_machine &machine)
47{
48   tx0_state *state = machine.driver_data<tx0_state>();
49   /* the core will take care of freeing the timers, BUT we must set the variables
50    to NULL if we don't want to risk confusing the tape image init function */
51   state->m_tape_reader.timer = state->m_tape_puncher.timer = state->m_typewriter.prt_timer = state->m_dis_timer = NULL;
52}
53
54
55MACHINE_START( tx0 )
56{
57   tx0_state *state = machine.driver_data<tx0_state>();
58   state->m_tape_reader.timer = machine.scheduler().timer_alloc(FUNC(reader_callback));
59   state->m_tape_puncher.timer = machine.scheduler().timer_alloc(FUNC(puncher_callback));
60   state->m_typewriter.prt_timer = machine.scheduler().timer_alloc(FUNC(prt_callback));
61   state->m_dis_timer = machine.scheduler().timer_alloc(FUNC(dis_callback));
62
63   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(tx0_machine_stop),&machine));
64}
65
66
67/*
68    perforated tape handling
69*/
70
71void tx0_tape_get_open_mode(int id,   unsigned int *readable, unsigned int *writeable, unsigned int *creatable)
72{
73   /* unit 0 is read-only, unit 1 is write-only */
74   if (id)
75   {
76      *readable = 0;
77      *writeable = 1;
78      *creatable = 1;
79   }
80   else
81   {
82      *readable = 1;
83      *writeable = 0;
84      *creatable = 0;
85   }
86}
87
88#if 0
89DEVICE_START( tx0_tape )
90{
91}
92
93
94/*
95    Open a perforated tape image
96
97    unit 0 is reader (read-only), unit 1 is puncher (write-only)
98*/
99DEVICE_IMAGE_LOAD( tx0_tape )
100{
101   tx0_state *state = image.device().machine().driver_data<tx0_state>();
102   int id = image_index_in_device(image);
103
104   switch (id)
105   {
106   case 0:
107      /* reader unit */
108      state->m_tape_reader.fd = image;
109
110      /* start motor */
111      state->m_tape_reader.motor_on = 1;
112
113      /* restart reader IO when necessary */
114      /* note that this function may be called before tx0_init_machine, therefore
115        before tape_reader.timer is allocated.  It does not matter, as the clutch is never
116        down at power-up, but we must not call timer_enable with a NULL parameter! */
117      if (state->m_tape_reader.timer)
118      {
119         if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
120         {
121            /* delay is approximately 1/400s */
122            state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
123         }
124         else
125         {
126            state->m_tape_reader.timer->enable(0);
127         }
128      }
129      break;
130
131   case 1:
132      /* punch unit */
133      state->m_tape_puncher.fd = image;
134      break;
135   }
136
137   return IMAGE_INIT_PASS;
138}
139
140DEVICE_IMAGE_UNLOAD( tx0_tape )
141{
142   tx0_state *state = image.device().machine().driver_data<tx0_state>();
143   int id = image_index_in_device(image);
144
145   switch (id)
146   {
147   case 0:
148      /* reader unit */
149      state->m_tape_reader.fd = NULL;
150
151      /* stop motor */
152      state->m_tape_reader.motor_on = 0;
153
154      if (state->m_tape_reader.timer)
155         state->m_tape_reader.timer->enable(0);
156      break;
157
158   case 1:
159      /* punch unit */
160      state->m_tape_puncher.fd = NULL;
161      break;
162   }
163}
164#endif
165/*
166    Read a byte from perforated tape
167*/
168static int tape_read(tx0_state *state, UINT8 *reply)
169{
170   if (state->m_tape_reader.fd && (state->m_tape_reader.fd->fread(reply, 1) == 1))
171      return 0;   /* unit OK */
172   else
173      return 1;   /* unit not ready */
174}
175
176/*
177    Write a byte to perforated tape
178*/
179static void tape_write(tx0_state *state, UINT8 data)
180{
181   if (state->m_tape_puncher.fd)
182      state->m_tape_puncher.fd->fwrite(& data, 1);
183}
184
185/*
186    common code for tape read commands (R1C, R3C, and read-in mode)
187*/
188static void begin_tape_read(tx0_state *state, int binary)
189{
190   state->m_tape_reader.rcl = 1;
191   state->m_tape_reader.rc = (binary) ? 1 : 3;
192
193   /* set up delay if tape is advancing */
194   if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
195   {
196      /* delay is approximately 1/400s */
197      state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
198   }
199   else
200   {
201      state->m_tape_reader.timer->enable(0);
202   }
203}
204
205
206/*
207    timer callback to simulate reader IO
208*/
209static TIMER_CALLBACK(reader_callback)
210{
211   tx0_state *state = machine.driver_data<tx0_state>();
212   int not_ready;
213   UINT8 data;
214   int ac;
215
216   if (state->m_tape_reader.rc)
217   {
218      not_ready = tape_read(state, & data);
219      if (not_ready)
220      {
221         state->m_tape_reader.motor_on = 0;   /* let us stop the motor */
222      }
223      else
224      {
225         if (data & 0100)
226         {
227            /* read current AC */
228            ac = cpu_get_reg(machine.device("maincpu"), TX0_AC);
229            /* cycle right */
230            ac = (ac >> 1) | ((ac & 1) << 17);
231            /* shuffle and insert data into AC */
232            ac = (ac /*& 0333333*/) | ((data & 001) << 17) | ((data & 002) << 13) | ((data & 004) << 9) | ((data & 010) << 5) | ((data & 020) << 1) | ((data & 040) >> 3);
233            /* write modified AC */
234            cpu_set_reg(machine.device("maincpu"), TX0_AC, ac);
235
236            state->m_tape_reader.rc = (state->m_tape_reader.rc+1) & 3;
237
238            if (state->m_tape_reader.rc == 0)
239            {   /* IO complete */
240               state->m_tape_reader.rcl = 0;
241               cpu_set_reg(machine.device("maincpu"), TX0_IO_COMPLETE, (UINT64)0);
242            }
243         }
244      }
245   }
246
247   if (state->m_tape_reader.motor_on && state->m_tape_reader.rcl)
248      /* delay is approximately 1/400s */
249      state->m_tape_reader.timer->adjust(attotime::from_usec(2500));
250   else
251      state->m_tape_reader.timer->enable(0);
252}
253
254/*
255    timer callback to generate punch completion pulse
256*/
257static TIMER_CALLBACK(puncher_callback)
258{
259   cpu_set_reg(machine.device("maincpu"), TX0_IO_COMPLETE, (UINT64)0);
260}
261
262/*
263    Initiate read of a 6-bit word from tape
264*/
265void tx0_io_r1l(device_t *device)
266{
267   tx0_state *state = device->machine().driver_data<tx0_state>();
268   begin_tape_read(state, 0);
269}
270
271/*
272    Initiate read of a 18-bit word from tape (used in read-in mode)
273*/
274void tx0_io_r3l(device_t *device)
275{
276   tx0_state *state = device->machine().driver_data<tx0_state>();
277   begin_tape_read(state, 1);
278}
279
280/*
281    Write a 7-bit word to tape (7th bit clear)
282*/
283void tx0_io_p6h(device_t *device)
284{
285   tx0_state *state = device->machine().driver_data<tx0_state>();
286   int ac;
287
288   /* read current AC */
289   ac = cpu_get_reg(device, TX0_AC);
290   /* shuffle and punch 6-bit word */
291   tape_write(state, ((ac & 0100000) >> 15) | ((ac & 0010000) >> 11) | ((ac & 0001000) >> 7) | ((ac & 0000100) >> 3) | ((ac & 0000010) << 1) | ((ac & 0000001) << 5));
292
293   state->m_tape_puncher.timer->adjust(attotime::from_usec(15800));
294}
295
296/*
297    Write a 7-bit word to tape (7th bit set)
298*/
299void tx0_io_p7h(device_t *device)
300{
301   tx0_state *state = device->machine().driver_data<tx0_state>();
302   int ac;
303
304   /* read current AC */
305   ac = cpu_get_reg(device, TX0_AC);
306   /* shuffle and punch 6-bit word */
307   tape_write(state, ((ac & 0100000) >> 15) | ((ac & 0010000) >> 11) | ((ac & 0001000) >> 7) | ((ac & 0000100) >> 3) | ((ac & 0000010) << 1) | ((ac & 0000001) << 5) | 0100);
308
309   state->m_tape_puncher.timer->adjust(attotime::from_usec(15800));
310}
311
312
313/*
314    Typewriter handling
315
316    The alphanumeric on-line typewriter is a standard device on tx-0: it can
317    both handle keyboard input and print output text.
318*/
319
320/*
321    Open a file for typewriter output
322*/
323DEVICE_IMAGE_LOAD(tx0_typewriter)
324{
325   tx0_state *state = image.device().machine().driver_data<tx0_state>();
326   /* open file */
327   state->m_typewriter.fd = &image;
328
329   return IMAGE_INIT_PASS;
330}
331
332DEVICE_IMAGE_UNLOAD(tx0_typewriter)
333{
334   tx0_state *state = image.device().machine().driver_data<tx0_state>();
335   state->m_typewriter.fd = NULL;
336}
337
338/*
339    Write a character to typewriter
340*/
341static void typewriter_out(running_machine &machine, UINT8 data)
342{
343   tx0_state *state = machine.driver_data<tx0_state>();
344   tx0_typewriter_drawchar(machine, data);
345   if (state->m_typewriter.fd)
346      state->m_typewriter.fd->fwrite(& data, 1);
347}
348
349/*
350    timer callback to generate typewriter completion pulse
351*/
352static TIMER_CALLBACK(prt_callback)
353{
354   cpu_set_reg(machine.device("maincpu"), TX0_IO_COMPLETE, (UINT64)0);
355}
356
357/*
358    prt io callback
359*/
360void tx0_io_prt(device_t *device)
361{
362   tx0_state *state = device->machine().driver_data<tx0_state>();
363   int ac;
364   int ch;
365
366   /* read current AC */
367   ac = cpu_get_reg(device, TX0_AC);
368   /* shuffle and print 6-bit word */
369   ch = ((ac & 0100000) >> 15) | ((ac & 0010000) >> 11) | ((ac & 0001000) >> 7) | ((ac & 0000100) >> 3) | ((ac & 0000010) << 1) | ((ac & 0000001) << 5);
370   typewriter_out(device->machine(), ch);
371
372   state->m_typewriter.prt_timer->adjust(attotime::from_msec(100));
373}
374
375
376/*
377    timer callback to generate crt completion pulse
378*/
379static TIMER_CALLBACK(dis_callback)
380{
381   cpu_set_reg(machine.device("maincpu"), TX0_IO_COMPLETE, (UINT64)0);
382}
383
384/*
385    Plot one point on crt
386*/
387void tx0_io_dis(device_t *device)
388{
389   tx0_state *state = device->machine().driver_data<tx0_state>();
390   int ac;
391   int x;
392   int y;
393
394   ac = cpu_get_reg(device, TX0_AC);
395   x = ac >> 9;
396   y = ac & 0777;
397   tx0_plot(device->machine(), x, y);
398
399   state->m_dis_timer->adjust(attotime::from_usec(50));
400}
401
402
403/*
404    Magtape support
405
406    Magtape format:
407
408    7-track tape, 6-bit data, 1-bit parity
409
410
411*/
412
413static void schedule_select(tx0_state *state)
414{
415   attotime delay = attotime::zero;
416
417   switch (state->m_magtape.command)
418   {
419   case 0:   /* backspace */
420      delay = attotime::from_usec(4600);
421      break;
422   case 1:   /* read */
423      delay = attotime::from_usec(8600);
424      break;
425   case 2:   /* rewind */
426      delay = attotime::from_usec(12000);
427      break;
428   case 3:   /* write */
429      delay = attotime::from_usec(4600);
430      break;
431   }
432   state->m_magtape.timer->adjust(delay);
433}
434
435static void schedule_unselect(tx0_state *state)
436{
437   attotime delay = attotime::zero;
438
439   switch (state->m_magtape.command)
440   {
441   case 0:   /* backspace */
442      delay = attotime::from_usec(5750);
443      break;
444   case 1:   /* read */
445      delay = attotime::from_usec(1750);
446      break;
447   case 2:   /* rewind */
448      delay = attotime::from_usec(0);
449      break;
450   case 3:   /* write */
451      delay = attotime::from_usec(5750);
452      break;
453   }
454   state->m_magtape.timer->adjust(delay);
455}
456
457DEVICE_START( tx0_magtape )
458{
459   tx0_state *state = device->machine().driver_data<tx0_state>();
460   state->m_magtape.img = dynamic_cast<device_image_interface *>(device);
461}
462
463/*
464    Open a magnetic tape image
465*/
466DEVICE_IMAGE_LOAD( tx0_magtape )
467{
468   tx0_state *state = image.device().machine().driver_data<tx0_state>();
469   state->m_magtape.img = &image;
470
471   state->m_magtape.irg_pos = MTIRGP_END;
472
473   /* restart IO when necessary */
474   /* note that this function may be called before tx0_init_machine, therefore
475    before magtape.timer is allocated.  We must not call timer_enable with a
476    NULL parameter! */
477   if (state->m_magtape.timer)
478   {
479      if (state->m_magtape.state == MTS_SELECTING)
480         schedule_select(state);
481   }
482
483   return IMAGE_INIT_PASS;
484}
485
486DEVICE_IMAGE_UNLOAD( tx0_magtape )
487{
488   tx0_state *state = image.device().machine().driver_data<tx0_state>();
489   state->m_magtape.img = NULL;
490
491   if (state->m_magtape.timer)
492   {
493      if (state->m_magtape.state == MTS_SELECTING)
494         /* I/O has not actually started, we can cancel the selection */
495         state->m_tape_reader.timer->enable(0);
496      if ((state->m_magtape.state == MTS_SELECTED) || ((state->m_magtape.state == MTS_SELECTING) && (state->m_magtape.command == 2)))
497      {   /* unit has become unavailable */
498         state->m_magtape.state = MTS_UNSELECTING;
499         cpu_set_reg(image.device().machine().device("maincpu"), TX0_PF, cpu_get_reg(image.device().machine().device("maincpu"), TX0_PF) | PF_RWC);
500         schedule_unselect(state);
501      }
502   }
503}
504
505static void magtape_callback(device_t *device)
506{
507   tx0_state *state = device->machine().driver_data<tx0_state>();
508   UINT8 buf = 0;
509   int lr;
510
511   switch (state->m_magtape.state)
512   {
513   case MTS_UNSELECTING:
514      state->m_magtape.state = MTS_UNSELECTED;
515
516   case MTS_UNSELECTED:
517      if (state->m_magtape.sel_pending)
518      {
519         int mar;
520
521         mar = cpu_get_reg(device, TX0_MAR);
522
523         if ((mar & 03) != 1)
524         {   /* unimplemented device: remain in unselected state and set rwc
525                flag? */
526            cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_RWC);
527         }
528         else
529         {
530            state->m_magtape.state = MTS_SELECTING;
531
532            state->m_magtape.command = (mar & 014 >> 2);
533
534            state->m_magtape.binary_flag = (mar & 020 >> 4);
535
536            if (state->m_magtape.img)
537               schedule_select(state);
538         }
539
540         state->m_magtape.sel_pending = FALSE;
541         cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
542      }
543      break;
544
545   case MTS_SELECTING:
546      state->m_magtape.state = MTS_SELECTED;
547      switch (state->m_magtape.command)
548      {
549      case 0:   /* backspace */
550         state->m_magtape.long_parity = 0177;
551         state->m_magtape.u.backspace_state = MTBSS_STATE0;
552         break;
553      case 1:   /* read */
554         state->m_magtape.long_parity = 0177;
555         state->m_magtape.u.read.state = MTRDS_STATE0;
556         break;
557      case 2:   /* rewind */
558         break;
559      case 3:   /* write */
560         state->m_magtape.long_parity = 0177;
561         state->m_magtape.u.write.state = MTWTS_STATE0;
562         switch (state->m_magtape.irg_pos)
563         {
564         case MTIRGP_START:
565            state->m_magtape.u.write.counter = 150;
566            break;
567         case MTIRGP_ENDMINUS1:
568            state->m_magtape.u.write.counter = 1;
569            break;
570         case MTIRGP_END:
571            state->m_magtape.u.write.counter = 0;
572            break;
573         }
574         break;
575      }
576
577   case MTS_SELECTED:
578      switch (state->m_magtape.command)
579      {
580      case 0:   /* backspace */
581         if (state->m_magtape.img->ftell() == 0)
582         {   /* tape at ldp */
583            state->m_magtape.state = MTS_UNSELECTING;
584            cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_RWC);
585            schedule_unselect(state);
586         }
587         else if (state->m_magtape.img->fseek( -1, SEEK_CUR))
588         {   /* eject tape */
589            state->m_magtape.img->unload();
590         }
591         else if (state->m_magtape.img->fread(&buf, 1) != 1)
592         {   /* eject tape */
593            state->m_magtape.img->unload();
594         }
595         else if (state->m_magtape.img->fseek( -1, SEEK_CUR))
596         {   /* eject tape */
597            state->m_magtape.img->unload();
598         }
599         else
600         {
601            buf &= 0x7f;   /* 7-bit tape, ignore 8th bit */
602            state->m_magtape.long_parity ^= buf;
603            switch (state->m_magtape.u.backspace_state)
604            {
605            case MTBSS_STATE0:
606               /* STATE0 -> initial interrecord gap, longitudinal parity;
607                    if longitudinal parity was all 0s, gap between longitudinal
608                    parity and data, first byte of data */
609               if (buf != 0)
610                  state->m_magtape.u.backspace_state = MTBSS_STATE1;
611               break;
612            case MTBSS_STATE1:
613               /* STATE1 -> first byte of gap between longitudinal parity and
614                    data, second byte of data */
615               if (buf == 0)
616                  state->m_magtape.u.backspace_state = MTBSS_STATE2;
617               else
618                  state->m_magtape.u.backspace_state = MTBSS_STATE5;
619               break;
620            case MTBSS_STATE2:
621               /* STATE2 -> second byte of gap between longitudinal parity and
622                    data */
623               if (buf == 0)
624                  state->m_magtape.u.backspace_state = MTBSS_STATE3;
625               else
626               {
627                  logerror("tape seems to be corrupt\n");
628                  /* eject tape */
629                  state->m_magtape.img->unload();
630               }
631               break;
632            case MTBSS_STATE3:
633               /* STATE3 -> third byte of gap between longitudinal parity and
634                    data */
635               if (buf == 0)
636                  state->m_magtape.u.backspace_state = MTBSS_STATE4;
637               else
638               {
639                  logerror("tape seems to be corrupt\n");
640                  /* eject tape */
641                  state->m_magtape.img->unload();
642               }
643               break;
644            case MTBSS_STATE4:
645               /* STATE4 -> first byte of data word, first byte of
646                    interrecord gap after data */
647               if (buf == 0)
648               {
649                  if (state->m_magtape.long_parity)
650                     logerror("invalid longitudinal parity\n");
651                  /* set EOR and unselect... */
652                  state->m_magtape.state = MTS_UNSELECTING;
653                  cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_EOR);
654                  schedule_unselect(state);
655                  state->m_magtape.irg_pos = MTIRGP_ENDMINUS1;
656               }
657               else
658                  state->m_magtape.u.backspace_state = MTBSS_STATE5;
659               break;
660            case MTBSS_STATE5:
661               /* STATE5 -> second byte of data word */
662               if (buf == 0)
663               {
664                  logerror("tape seems to be corrupt\n");
665                  /* eject tape */
666                  state->m_magtape.img->unload();
667               }
668               else
669                  state->m_magtape.u.backspace_state = MTBSS_STATE6;
670               break;
671            case MTBSS_STATE6:
672               /* STATE6 -> third byte of data word */
673               if (buf == 0)
674               {
675                  logerror("tape seems to be corrupt\n");
676                  /* eject tape */
677                  state->m_magtape.img->unload();
678               }
679               else
680                  state->m_magtape.u.backspace_state = MTBSS_STATE6;
681               break;
682            }
683            if (state->m_magtape.state != MTS_UNSELECTING)
684               state->m_magtape.timer->adjust(attotime::from_usec(66));
685         }
686         break;
687
688      case 1:   /* read */
689         if (state->m_magtape.img->fread(&buf, 1) != 1)
690         {   /* I/O error or EOF? */
691            /* The MAME fileio layer makes it very hard to make the
692                difference...  MAME seems to assume that I/O errors never
693                happen, whereas it is really easy to cause one by
694                deconnecting an external drive the image is located on!!! */
695            UINT64 offs;
696            offs = state->m_magtape.img->ftell();
697            if (state->m_magtape.img->fseek( 0, SEEK_END) || (offs != state->m_magtape.img->ftell()))
698            {   /* I/O error */
699               /* eject tape */
700               state->m_magtape.img->unload();
701            }
702            else
703            {   /* end of tape -> ??? */
704               /* maybe we run past end of tape, so that tape is ejected from
705                    upper reel and unit becomes unavailable??? */
706               /*state->m_magtape.img->unload();*/
707               /* Or do we stop at EOT mark??? */
708               state->m_magtape.state = MTS_UNSELECTING;
709               cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_EOT);
710               schedule_unselect(state);
711            }
712         }
713         else
714         {
715            buf &= 0x7f;   /* 7-bit tape, ignore 8th bit */
716            state->m_magtape.long_parity ^= buf;
717            switch (state->m_magtape.u.read.state)
718            {
719            case MTRDS_STATE0:
720               /* STATE0 -> interrecord blank or first byte of data */
721               if (buf != 0)
722               {
723                  if (state->m_magtape.cpy_pending)
724                  {   /* read command */
725                     state->m_magtape.u.read.space_flag = FALSE;
726                     cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
727                     cpu_set_reg(device, TX0_LR, ((cpu_get_reg(device, TX0_LR) >> 1) & 0333333)
728                                          | ((buf & 040) << 12) | ((buf & 020) << 10) | ((buf & 010) << 8) | ((buf & 004) << 6) | ((buf & 002) << 4) | ((buf & 001) << 2));
729                     /* check parity */
730                     if (! (((buf ^ (buf >> 1) ^ (buf >> 2) ^ (buf >> 3) ^ (buf >> 4) ^ (buf >> 5) ^ (buf >> 6) ^ (buf >> 7)) & 1) ^ state->m_magtape.binary_flag))
731                        cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_PC);
732                  }
733                  else
734                  {   /* space command */
735                     state->m_magtape.u.read.space_flag = TRUE;
736                  }
737                  state->m_magtape.u.read.state = MTRDS_STATE1;
738               }
739               break;
740            case MTRDS_STATE1:
741               /* STATE1 -> second byte of data word */
742               if (buf == 0)
743               {
744                  logerror("tape seems to be corrupt\n");
745                  /* eject tape */
746                  state->m_magtape.img->unload();
747               }
748               if (!state->m_magtape.u.read.space_flag)
749               {
750                  cpu_set_reg(device, TX0_LR, ((cpu_get_reg(device, TX0_LR) >> 1) & 0333333)
751                                       | ((buf & 040) << 12) | ((buf & 020) << 10) | ((buf & 010) << 8) | ((buf & 004) << 6) | ((buf & 002) << 4) | ((buf & 001) << 2));
752                  /* check parity */
753                  if (! (((buf ^ (buf >> 1) ^ (buf >> 2) ^ (buf >> 3) ^ (buf >> 4) ^ (buf >> 5) ^ (buf >> 6) ^ (buf >> 7)) & 1) ^ state->m_magtape.binary_flag))
754                     cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_PC);
755               }
756               state->m_magtape.u.read.state = MTRDS_STATE2;
757               break;
758            case MTRDS_STATE2:
759               /* STATE2 -> third byte of data word */
760               if (buf == 0)
761               {
762                  logerror("tape seems to be corrupt\n");
763                  /* eject tape */
764                  state->m_magtape.img->unload();
765               }
766               if (!state->m_magtape.u.read.space_flag)
767               {
768                  cpu_set_reg(device, TX0_LR, ((cpu_get_reg(device, TX0_LR) >> 1) & 0333333)
769                                       | ((buf & 040) << 12) | ((buf & 020) << 10) | ((buf & 010) << 8) | ((buf & 004) << 6) | ((buf & 002) << 4) | ((buf & 001) << 2));
770                  /* check parity */
771                  if (! (((buf ^ (buf >> 1) ^ (buf >> 2) ^ (buf >> 3) ^ (buf >> 4) ^ (buf >> 5) ^ (buf >> 6) ^ (buf >> 7)) & 1) ^ state->m_magtape.binary_flag))
772                     cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_PC);
773                  /* synchronize with cpy instruction */
774                  if (state->m_magtape.cpy_pending)
775                     cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
776                  else
777                     cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_RWC);
778               }
779               state->m_magtape.u.read.state = MTRDS_STATE3;
780               break;
781            case MTRDS_STATE3:
782               /* STATE3 -> first byte of new word of data, or first byte
783                    of gap between data and longitudinal parity */
784               if (buf != 0)
785               {
786                  state->m_magtape.u.read.state = MTRDS_STATE1;
787                  if (!state->m_magtape.u.read.space_flag)
788                  {
789                     cpu_set_reg(device, TX0_LR, ((cpu_get_reg(device, TX0_LR) >> 1) & 0333333)
790                                          | ((buf & 040) << 12) | ((buf & 020) << 10) | ((buf & 010) << 8) | ((buf & 004) << 6) | ((buf & 002) << 4) | ((buf & 001) << 2));
791                     /* check parity */
792                     if (! (((buf ^ (buf >> 1) ^ (buf >> 2) ^ (buf >> 3) ^ (buf >> 4) ^ (buf >> 5) ^ (buf >> 6) ^ (buf >> 7)) & 1) ^ state->m_magtape.binary_flag))
793                        cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_PC);
794                  }
795               }
796               else
797                  state->m_magtape.u.read.state = MTRDS_STATE4;
798               break;
799            case MTRDS_STATE4:
800               /* STATE4 -> second byte of gap between data and
801                    longitudinal parity */
802               if (buf != 0)
803               {
804                  logerror("tape seems to be corrupt\n");
805                  /* eject tape */
806                  state->m_magtape.img->unload();
807               }
808               else
809                  state->m_magtape.u.read.state = MTRDS_STATE5;
810               break;
811
812            case MTRDS_STATE5:
813               /* STATE5 -> third byte of gap between data and
814                    longitudinal parity */
815               if (buf != 0)
816               {
817                  logerror("tape seems to be corrupt\n");
818                  /* eject tape */
819                  state->m_magtape.img->unload();
820               }
821               else
822                  state->m_magtape.u.read.state = MTRDS_STATE6;
823               break;
824
825            case MTRDS_STATE6:
826               /* STATE6 -> longitudinal parity */
827               /* check parity */
828               if (state->m_magtape.long_parity)
829               {
830                  logerror("invalid longitudinal parity\n");
831                  /* no idea if the original tx-0 magtape controller
832                        checks parity, but can't harm if we do */
833                  cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_PC);
834               }
835               /* set EOR and unselect... */
836               state->m_magtape.state = MTS_UNSELECTING;
837               cpu_set_reg(device, TX0_PF, cpu_get_reg(device, TX0_PF) | PF_EOR);
838               schedule_unselect(state);
839               state->m_magtape.irg_pos = MTIRGP_START;
840               break;
841            }
842            if (state->m_magtape.state != MTS_UNSELECTING)
843               state->m_magtape.timer->adjust(attotime::from_usec(66));
844         }
845         break;
846
847      case 2:   /* rewind */
848         state->m_magtape.state = MTS_UNSELECTING;
849         /* we rewind at 10*read speed (I don't know the real value) */
850         state->m_magtape.timer->adjust((attotime::from_nsec(6600) * state->m_magtape.img->ftell()));
851         //schedule_unselect(state);
852         state->m_magtape.img->fseek( 0, SEEK_END);
853         state->m_magtape.irg_pos = MTIRGP_END;
854         break;
855
856      case 3:   /* write */
857         switch (state->m_magtape.u.write.state)
858         {
859         case MTWTS_STATE0:
860            if (state->m_magtape.u.write.counter != 0)
861            {
862               state->m_magtape.u.write.counter--;
863               buf = 0;
864               break;
865            }
866            else
867            {
868               state->m_magtape.u.write.state = MTWTS_STATE1;
869            }
870
871         case MTWTS_STATE1:
872            if (state->m_magtape.u.write.counter)
873            {
874               state->m_magtape.u.write.counter--;
875               lr = cpu_get_reg(device, TX0_LR);
876               buf = ((lr >> 10) & 040) | ((lr >> 8) & 020) | ((lr >> 6) & 010) | ((lr >> 4) & 004) | ((lr >> 2) & 002) | (lr & 001);
877               buf |= ((buf << 1) ^ (buf << 2) ^ (buf << 3) ^ (buf << 4) ^ (buf << 5) ^ (buf << 6) ^ ((!state->m_magtape.binary_flag) << 6)) & 0100;
878               cpu_set_reg(device, TX0_LR, lr >> 1);
879            }
880            else
881            {
882               if (state->m_magtape.cpy_pending)
883               {
884                  cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
885                  lr = cpu_get_reg(device, TX0_LR);
886                  buf = ((lr >> 10) & 040) | ((lr >> 8) & 020) | ((lr >> 6) & 010) | ((lr >> 4) & 004) | ((lr >> 2) & 002) | (lr & 001);
887                  buf |= ((buf << 1) ^ (buf << 2) ^ (buf << 3) ^ (buf << 4) ^ (buf << 5) ^ (buf << 6) ^ ((!state->m_magtape.binary_flag) << 6)) & 0100;
888                  cpu_set_reg(device, TX0_LR, lr >> 1);
889                  state->m_magtape.u.write.counter = 2;
890                  break;
891               }
892               else
893               {
894                  state->m_magtape.u.write.state = MTWTS_STATE2;
895                  state->m_magtape.u.write.counter = 3;
896               }
897            }
898
899         case MTWTS_STATE2:
900            if (state->m_magtape.u.write.counter != 0)
901            {
902               state->m_magtape.u.write.counter--;
903               buf = 0;
904               break;
905            }
906            else
907            {
908               buf = state->m_magtape.long_parity;
909               state->m_magtape.state = (state_t)MTWTS_STATE3;
910               state->m_magtape.u.write.counter = 150;
911            }
912            break;
913
914         case MTWTS_STATE3:
915            if (state->m_magtape.u.write.counter != 0)
916            {
917               state->m_magtape.u.write.counter--;
918               buf = 0;
919               break;
920            }
921            else
922            {
923               state->m_magtape.state = MTS_UNSELECTING;
924               schedule_unselect(state);
925               state->m_magtape.irg_pos = MTIRGP_END;
926            }
927            break;
928         }
929         if (state->m_magtape.state != MTS_UNSELECTING)
930         {   /* write data word */
931            state->m_magtape.long_parity ^= buf;
932            if (state->m_magtape.img->fwrite(&buf, 1) != 1)
933            {   /* I/O error */
934               /* eject tape */
935               state->m_magtape.img->unload();
936            }
937            else
938               state->m_magtape.timer->adjust(attotime::from_usec(66));
939         }
940         break;
941      }
942      break;
943   }
944}
945
946void tx0_sel(device_t *device)
947{
948   tx0_state *state = device->machine().driver_data<tx0_state>();
949   state->m_magtape.sel_pending = TRUE;
950
951   if (state->m_magtape.state == MTS_UNSELECTED)
952   {
953      if (0)
954         magtape_callback(device);
955      state->m_magtape.timer->adjust(attotime::zero);
956   }
957}
958
959void tx0_io_cpy(device_t *device)
960{
961   tx0_state *state = device->machine().driver_data<tx0_state>();
962   switch (state->m_magtape.state)
963   {
964   case MTS_UNSELECTED:
965   case MTS_UNSELECTING:
966      /* ignore instruction and set rwc flag? */
967      cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
968      break;
969
970   case MTS_SELECTING:
971   case MTS_SELECTED:
972      switch (state->m_magtape.command)
973      {
974      case 0:   /* backspace */
975      case 2:   /* rewind */
976         /* ignore instruction and set rwc flag? */
977         cpu_set_reg(device, TX0_IO_COMPLETE, (UINT64)0);
978         break;
979      case 1:   /* read */
980      case 3:   /* write */
981         state->m_magtape.cpy_pending = TRUE;
982         break;
983      }
984      break;
985   }
986}
987
988
989/*
990    callback which is called when reset line is pulsed
991
992    IO devices should reset
993*/
994void tx0_io_reset_callback(device_t *device)
995{
996   tx0_state *state = device->machine().driver_data<tx0_state>();
997   state->m_tape_reader.rcl = state->m_tape_reader.rc = 0;
998   if (state->m_tape_reader.timer)
999      state->m_tape_reader.timer->enable(0);
1000
1001   if (state->m_tape_puncher.timer)
1002      state->m_tape_puncher.timer->enable(0);
1003
1004   if (state->m_typewriter.prt_timer)
1005      state->m_typewriter.prt_timer->enable(0);
1006
1007   if (state->m_dis_timer)
1008      state->m_dis_timer->enable(0);
1009}
1010
1011
1012/*
1013    typewriter keyboard handler
1014*/
1015static void tx0_keyboard(running_machine &machine)
1016{
1017   tx0_state *state = machine.driver_data<tx0_state>();
1018   int i;
1019   int j;
1020
1021   int typewriter_keys[4];
1022
1023   int typewriter_transitions;
1024   int charcode, lr;
1025   static const char *const twrnames[] = { "TWR0", "TWR1", "TWR2", "TWR3" };
1026
1027   for (i=0; i<4; i++)
1028   {
1029      typewriter_keys[i] = input_port_read(machine, twrnames[i]);
1030   }
1031
1032   for (i=0; i<4; i++)
1033   {
1034      typewriter_transitions = typewriter_keys[i] & (~ state->m_old_typewriter_keys[i]);
1035      if (typewriter_transitions)
1036      {
1037         for (j=0; (((typewriter_transitions >> j) & 1) == 0) /*&& (j<16)*/; j++)
1038            ;
1039         charcode = (i << 4) + j;
1040         /* shuffle and insert data into LR */
1041         /* BTW, I am not sure how the char code is combined with the
1042            previous LR */
1043         lr = (1 << 17) | ((charcode & 040) << 10) | ((charcode & 020) << 8) | ((charcode & 010) << 6) | ((charcode & 004) << 4) | ((charcode & 002) << 2) | ((charcode & 001) << 1);
1044         /* write modified LR */
1045         cpu_set_reg(machine.device("maincpu"), TX0_LR, lr);
1046         tx0_typewriter_drawchar(machine, charcode);   /* we want to echo input */
1047         break;
1048      }
1049   }
1050
1051   for (i=0; i<4; i++)
1052      state->m_old_typewriter_keys[i] = typewriter_keys[i];
1053}
1054
1055/*
1056    Not a real interrupt - just handle keyboard input
1057*/
1058INTERRUPT_GEN( tx0_interrupt )
1059{
1060   tx0_state *state = device->machine().driver_data<tx0_state>();
1061   int control_keys;
1062   int tsr_keys;
1063
1064   int control_transitions;
1065   int tsr_transitions;
1066
1067
1068   /* read new state of control keys */
1069   control_keys = input_port_read(device->machine(), "CSW");
1070
1071   if (control_keys & tx0_control)
1072   {
1073      /* compute transitions */
1074      control_transitions = control_keys & (~ state->m_old_control_keys);
1075
1076      if (control_transitions & tx0_stop_cyc0)
1077      {
1078         cpu_set_reg(device->machine().device("maincpu"), TX0_STOP_CYC0, !cpu_get_reg(device->machine().device("maincpu"), TX0_STOP_CYC0));
1079      }
1080      if (control_transitions & tx0_stop_cyc1)
1081      {
1082         cpu_set_reg(device->machine().device("maincpu"), TX0_STOP_CYC1, !cpu_get_reg(device->machine().device("maincpu"), TX0_STOP_CYC1));
1083      }
1084      if (control_transitions & tx0_gbl_cm_sel)
1085      {
1086         cpu_set_reg(device->machine().device("maincpu"), TX0_GBL_CM_SEL, !cpu_get_reg(device->machine().device("maincpu"), TX0_GBL_CM_SEL));
1087      }
1088      if (control_transitions & tx0_stop)
1089      {
1090         cpu_set_reg(device->machine().device("maincpu"), TX0_RUN, (UINT64)0);
1091         cpu_set_reg(device->machine().device("maincpu"), TX0_RIM, (UINT64)0);
1092      }
1093      if (control_transitions & tx0_restart)
1094      {
1095         cpu_set_reg(device->machine().device("maincpu"), TX0_RUN, 1);
1096         cpu_set_reg(device->machine().device("maincpu"), TX0_RIM, (UINT64)0);
1097      }
1098      if (control_transitions & tx0_read_in)
1099      {   /* set cpu to read instructions from perforated tape */
1100         cpu_set_reg(device->machine().device("maincpu"), TX0_RESET, (UINT64)0);
1101         cpu_set_reg(device->machine().device("maincpu"), TX0_RUN, (UINT64)0);
1102         cpu_set_reg(device->machine().device("maincpu"), TX0_RIM, 1);
1103      }
1104      if (control_transitions & tx0_toggle_dn)
1105      {
1106         state->m_tsr_index++;
1107         if (state->m_tsr_index == 18)
1108            state->m_tsr_index = 0;
1109      }
1110      if (control_transitions & tx0_toggle_up)
1111      {
1112         state->m_tsr_index--;
1113         if (state->m_tsr_index == -1)
1114            state->m_tsr_index = 17;
1115      }
1116      if (control_transitions & tx0_cm_sel)
1117      {
1118         if (state->m_tsr_index >= 2)
1119         {
1120            UINT32 cm_sel = (UINT32) cpu_get_reg(device->machine().device("maincpu"), TX0_CM_SEL);
1121            cpu_set_reg(device->machine().device("maincpu"), TX0_CM_SEL, cm_sel ^ (1 << (state->m_tsr_index - 2)));
1122         }
1123      }
1124      if (control_transitions & tx0_lr_sel)
1125      {
1126         if (state->m_tsr_index >= 2)
1127         {
1128            UINT32 lr_sel = (UINT32) cpu_get_reg(device->machine().device("maincpu"), TX0_LR_SEL);
1129            cpu_set_reg(device->machine().device("maincpu"), TX0_LR_SEL, (lr_sel ^ (1 << (state->m_tsr_index - 2))));
1130         }
1131      }
1132
1133      /* remember new state of control keys */
1134      state->m_old_control_keys = control_keys;
1135
1136
1137      /* handle toggle switch register keys */
1138      tsr_keys = (input_port_read(device->machine(), "MSW") << 16) | input_port_read(device->machine(), "LSW");
1139
1140      /* compute transitions */
1141      tsr_transitions = tsr_keys & (~ state->m_old_tsr_keys);
1142
1143      /* update toggle switch register */
1144      if (tsr_transitions)
1145         cpu_set_reg(device->machine().device("maincpu"), TX0_TBR+state->m_tsr_index, cpu_get_reg(device->machine().device("maincpu"), TX0_TBR+state->m_tsr_index) ^ tsr_transitions);
1146
1147      /* remember new state of toggle switch register keys */
1148      state->m_old_tsr_keys = tsr_keys;
1149   }
1150   else
1151   {
1152      state->m_old_control_keys = 0;
1153      state->m_old_tsr_keys = 0;
1154
1155      tx0_keyboard(device->machine());
1156   }
1157}
1158
1159

Previous 15994 Revisions Next


© 1998-2018 The MESS Team