Previous 199869 Revisions Next

r31656 Thursday 14th August, 2014 at 18:52:04 UTC by hap
small cleanup, i give up on mametesters.org/view.php?id=5377
[src/emu/machine]pit8253.c pit8253.h

trunk/src/emu/machine/pit8253.c
r31655r31656
4141#define LOG1(msg)       do { if (VERBOSE >= 1) logerror msg; } while (0)
4242#define LOG2(msg)       do { if (VERBOSE >= 2) logerror msg; } while (0)
4343
44#define CYCLES_NEVER    (0xffffffff)
4544
46
4745const device_type PIT8253 = &device_creator<pit8253_device>;
4846
4947
r31655r31656
5654   m_out1_handler(*this),
5755   m_out2_handler(*this)
5856{
59   for (int i = 0; i < PIT8253_MAX_TIMER; i++)
60   {
61      m_timers[i].gate = 1;
62      m_timers[i].phase = 0;
63      m_timers[i].clock = 0;
64   }
6557}
6658
6759pit8253_device::pit8253_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, const char *shortname, const char *source) :
r31655r31656
7365   m_out1_handler(*this),
7466   m_out2_handler(*this)
7567{
76   for (int i = 0; i < PIT8253_MAX_TIMER; i++)
77   {
78      m_timers[i].gate = 1;
79      m_timers[i].phase = 0;
80      m_timers[i].clock = 0;
81   }
8268}
8369
8470
r31655r31656
9076}
9177
9278
79pit8253_device::pit8253_timer *pit8253_device::get_timer(int which)
80{
81   which &= 3;
82   if (which < PIT8253_MAX_TIMER)
83      return &m_timers[which];
84
85   return NULL;
86}
87
88
9389//-------------------------------------------------
9490//  device_start - device-specific startup
9591//-------------------------------------------------
r31655r31656
107103   /* register for state saving */
108104   for (int timerno = 0; timerno < PIT8253_MAX_TIMER; timerno++)
109105   {
110      pit8253_timer *timer = &m_timers[timerno];
111
106      pit8253_timer *timer = get_timer(timerno);
107     
112108      /* initialize timer */
113109      timer->updatetimer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(pit8253_device::update_timer_cb),this));
114110      timer->updatetimer->adjust(attotime::never, timerno);
r31655r31656
131127      save_item(NAME(timer->phase), timerno);
132128      save_item(NAME(timer->last_updated), timerno);
133129      save_item(NAME(timer->clock), timerno);
130
131      /* zerofill */
132      timer->gate = 1;
133      timer->phase = 0;
134      timer->clock = 0;
135
136      timer->index = timerno;
137      timer->control = timer->status = 0x30;
138      timer->rmsb = timer->wmsb = 0;
139      timer->count = timer->value = timer->latch = 0;
140      timer->lowcount = 0;
141
142      timer->output = 0;
143      timer->latched_count = 0;
144      timer->latched_status = 0;
145      timer->null_count = 1;
146
147      timer->last_updated = machine().time();
134148   }
135149}
136150
r31655r31656
143157{
144158   for (int i = 0; i < PIT8253_MAX_TIMER; i++)
145159   {
146      pit8253_timer *timer = &m_timers[i];
160      pit8253_timer *timer = get_timer(i);
161
147162      /* According to Intel's 8254 docs, the state of a timer is undefined
148163       until the first mode control word is written. Here we define this
149164       undefined behaviour */
150      timer->index = i;
151165      timer->control = timer->status = 0x30;
152166      timer->rmsb = timer->wmsb = 0;
153167      timer->count = timer->value = timer->latch = 0;
154168      timer->lowcount = 0;
155169
156      timer->output = 2; /* output is undetermined */
170      timer->output = 2; /* output is undetermined */
157171      timer->latched_count = 0;
158172      timer->latched_status = 0;
159173      timer->null_count = 1;
r31655r31656
164178   }
165179}
166180
167#define CTRL_ACCESS(control)        (((control) >> 4) & 0x03)
168#define CTRL_MODE(control)          (((control) >> 1) & (((control) & 0x04) ? 0x03 : 0x07))
169#define CTRL_BCD(control)           (((control) >> 0) & 0x01)
170181
171
172182/***************************************************************************
173183
174184    Functions
175185
176186***************************************************************************/
177187
188#define CTRL_ACCESS(control)        (((control) >> 4) & 0x03)
189#define CTRL_MODE(control)          (((control) >> 1) & (((control) & 0x04) ? 0x03 : 0x07))
190#define CTRL_BCD(control)           (((control) >> 0) & 0x01)
178191
179pit8253_timer *pit8253_device::get_timer(int which)
180{
181   which &= 3;
182   if (which < PIT8253_MAX_TIMER)
183      return &m_timers[which];
184192
185   return NULL;
186}
187
188
189int pit8253_device::pit8253_gate(pit8253_timer *timer)
193inline UINT32 pit8253_device::adjusted_count(int bcd, UINT16 val)
190194{
191   return timer->gate;
192}
195   if (!bcd)
196      return (val == 0) ? 0x10000 : val;
197   else if (val == 0)
198      return 10000;
193199
194
195INLINE UINT32 decimal_from_bcd(UINT16 val)
196{
197200   /* In BCD mode, a nybble loaded with value A-F counts down the same as in
198201      binary mode, but wraps around to 9 instead of F after 0, so loading the
199202      count register with 0xFFFF gives a period of
r31655r31656
211214}
212215
213216
214static UINT32 adjusted_count(int bcd, UINT16 val)
215{
216   if (!bcd)
217      return (val == 0) ? 0x10000 : val;
218   return (val == 0) ? 10000 : decimal_from_bcd(val);
219}
220
221
222217/* This function subtracts 1 from timer->value "cycles" times, taking into
223218   account binary or BCD operation, and wrapping around from 0 to 0xFFFF or
224219   0x9999 as necessary. */
r31655r31656
319314   UINT32 adjusted_value;
320315   int bcd = CTRL_BCD(timer->control);
321316   int mode = CTRL_MODE(timer->control);
317   static const UINT32 CYCLES_NEVER = (0xffffffff);
322318   UINT32 cycles_to_output = CYCLES_NEVER;
323319
324320   LOG2(("pit8253: simulate2(): simulating %d cycles for %d in mode %d, bcd = %d, phase = %d, gate = %d, output %d, value = 0x%04x\n",
325         (int)elapsed_cycles, timer->index, mode, bcd, timer->phase, pit8253_gate(timer), timer->output, timer->value));
321         (int)elapsed_cycles, timer->index, mode, bcd, timer->phase, timer->gate, timer->output, timer->value));
326322
327323   switch (mode)
328324   {
r31655r31656
363359            load_counter_value(timer);
364360         }
365361
366         if (pit8253_gate(timer) == 0)
362         if (timer->gate == 0)
367363         {
368364            cycles_to_output = CYCLES_NEVER;
369365         }
r31655r31656
475471      Rising-edge reloads count and initiates counting
476472      Gate high enables counting. */
477473
478      if (pit8253_gate(timer) == 0 || timer->phase == 0)
474      if (timer->gate == 0 || timer->phase == 0)
479475      {
480476         /* Gate low or mode control write forces output high */
481477         set_output(timer, 1);
r31655r31656
555551      Rising-edge reloads count and initiates counting
556552      Gate high enables counting. */
557553
558      if (pit8253_gate(timer) == 0 || timer->phase == 0)
554      if (timer->gate == 0 || timer->phase == 0)
559555      {
560556         /* Gate low or mode control write forces output high */
561557         set_output(timer, 1);
r31655r31656
641637      Mode 4 only: Gate level sensitive only. Low disables counting, high enables it.
642638      Mode 5 only: Gate rising-edge sensitive only. Rising edge initiates counting */
643639
644      if (pit8253_gate(timer) == 0 && mode == 4)
640      if (timer->gate == 0 && mode == 4)
645641      {
646642         cycles_to_output = CYCLES_NEVER;
647643      }
r31655r31656
703699   }
704700
705701   LOG2(("pit8253: simulate2(): simulating %d cycles for %d in mode %d, bcd = %d, phase = %d, gate = %d, output %d, value = 0x%04x, cycles_to_output = %04x\n",
706         (int)elapsed_cycles, timer->index, mode, bcd, timer->phase, pit8253_gate(timer), timer->output, timer->value, cycles_to_output));
702         (int)elapsed_cycles, timer->index, mode, bcd, timer->phase, timer->gate, timer->output, timer->value, cycles_to_output));
707703}
708704
709705
r31655r31656
755751
756752TIMER_CALLBACK_MEMBER( pit8253_device::update_timer_cb )
757753{
758   pit8253_timer *timer = &m_timers[param];
754   pit8253_timer *timer = get_timer(param);
759755
760756   LOG2(("pit8253: output_changed(): timer %d\n", param));
761757
r31655r31656
817813            value = masked_value(timer);
818814
819815            /* Read back current count */
820            switch(CTRL_ACCESS(timer->control))
816            switch (CTRL_ACCESS(timer->control))
821817            {
822818            case 0:
823819            default:
r31655r31656
895891         timer->latched_status = 1;
896892      }
897893   }
894
898895   /* Experimentally determined: the read latch command seems to have no
899896      effect if we're halfway through a 16-bit read */
900897   if ((command & 2) == 0 && !timer->rmsb)
901898   {
902899      /* readback count command */
903
904900      if (timer->latched_count == 0)
905901      {
906902         value = masked_value(timer);
907         switch(CTRL_ACCESS(timer->control))
903         switch (CTRL_ACCESS(timer->control))
908904         {
909905         case 0:
910906            /* This should never happen */
r31655r31656
1001997      if (machine().time() > timer->last_updated && timer->clockin != 0)
1002998         middle_of_a_cycle = 1;
1003999
1004      switch(CTRL_ACCESS(timer->control))
1000      switch (CTRL_ACCESS(timer->control))
10051001      {
10061002      case 0:
10071003         /* This should never happen */
r31655r31656
10661062void pit8253_device::gate_w(int gate, int state)
10671063{
10681064   pit8253_timer *timer = get_timer(gate);
1065   assert(timer != 0);
10691066
10701067   LOG2(("pit8253 : gate_w(): gate=%d state=%d\n", gate, state));
10711068
r31655r31656
11071104void pit8253_device::set_clockin(int timerno, double new_clockin)
11081105{
11091106   pit8253_timer *timer = get_timer(timerno);
1107   assert(timer != 0);
11101108
11111109   LOG2(("pit8253_set_clockin(): PIT timer=%d, clockin = %lf\n", timerno, new_clockin));
11121110
r31655r31656
11191117void pit8253_device::set_clock_signal(int timerno, int state)
11201118{
11211119   pit8253_timer *timer = get_timer(timerno);
1120   assert(timer != 0);
11221121
11231122   LOG2(("pit8253_set_clock_signal(): PIT timer=%d, state = %d\n", timerno, state));
11241123
trunk/src/emu/machine/pit8253.h
r31655r31656
4646   devcb = &pit8253_device::set_out2_handler(*device, DEVCB_##_devcb);
4747
4848
49struct pit8253_timer
50{
51   int index;                      /* index number of the timer */
52   double clockin;                 /* input clock frequency in Hz */
53   int clock;                      /* clock signal when clockin is 0 */
54
55   attotime last_updated;          /* time when last updated */
56
57   emu_timer *updatetimer;         /* MAME timer to process updates */
58
59   UINT16 value;                   /* current counter value ("CE" in Intel docs) */
60   UINT16 latch;                   /* latched counter value ("OL" in Intel docs) */
61   UINT16 count;                   /* new counter value ("CR" in Intel docs) */
62   UINT8 control;                  /* 6-bit control byte */
63   UINT8 status;                   /* status byte - 8254 only */
64   UINT8 lowcount;                 /* LSB of new counter value for 16-bit writes */
65   int rmsb;                       /* 1 = Next read is MSB of 16-bit value */
66   int wmsb;                       /* 1 = Next write is MSB of 16-bit value */
67   int output;                     /* 0 = low, 1 = high */
68
69   int gate;                       /* gate input (0 = low, 1 = high) */
70   int latched_count;              /* number of bytes of count latched */
71   int latched_status;             /* 1 = status latched (8254 only) */
72   int null_count;                 /* 1 = mode control or count written, 0 = count loaded */
73   int phase;                      /* see phase definition tables in simulate2(), below */
74};
75
7649class pit8253_device : public device_t
7750{
7851public:
r31655r31656
11790   virtual void device_reset();
11891
11992   // internal state
93   struct pit8253_timer
94   {
95      int index;              /* index number of the timer */
96      double clockin;         /* input clock frequency in Hz */
97      int clock;              /* clock signal when clockin is 0 */
98
99      attotime last_updated;  /* time when last updated */
100
101      emu_timer *updatetimer; /* MAME timer to process updates */
102
103      UINT16 value;           /* current counter value ("CE" in Intel docs) */
104      UINT16 latch;           /* latched counter value ("OL" in Intel docs) */
105      UINT16 count;           /* new counter value ("CR" in Intel docs) */
106      UINT8 control;          /* 6-bit control byte */
107      UINT8 status;           /* status byte - 8254 only */
108      UINT8 lowcount;         /* LSB of new counter value for 16-bit writes */
109      int rmsb;               /* 1 = Next read is MSB of 16-bit value */
110      int wmsb;               /* 1 = Next write is MSB of 16-bit value */
111      int output;             /* 0 = low, 1 = high */
112
113      int gate;               /* gate input (0 = low, 1 = high) */
114      int latched_count;      /* number of bytes of count latched */
115      int latched_status;     /* 1 = status latched (8254 only) */
116      int null_count;         /* 1 = mode control or count written, 0 = count loaded */
117      int phase;              /* see phase definition tables in simulate2(), below */
118   };
119
120120   void readback(pit8253_timer *timer, int command);
121121   virtual void readback_command(UINT8 data);
122122   pit8253_timer *get_timer(int which);
123123
124124private:
125   int pit8253_gate(pit8253_timer *timer);
125   double m_clk0;
126   double m_clk1;
127   double m_clk2;
128   devcb_write_line m_out0_handler;
129   devcb_write_line m_out1_handler;
130   devcb_write_line m_out2_handler;
131
132   enum
133   {
134      PIT8253_MAX_TIMER = 3
135   };
136
137   pit8253_timer m_timers[PIT8253_MAX_TIMER];
138
139   TIMER_CALLBACK_MEMBER(update_timer_cb);
140
141   inline UINT32 adjusted_count(int bcd, UINT16 val);
126142   void decrease_counter_value(pit8253_timer *timer, INT64 cycles);
127143   void load_counter_value(pit8253_timer *timer);
128144   void set_output(pit8253_timer *timer, int output);
r31655r31656
133149   void load_count(pit8253_timer *timer, UINT16 newcount);
134150   void gate_w(int gate, int state);
135151   void set_clock_signal(int timerno, int state);
136
137   TIMER_CALLBACK_MEMBER(update_timer_cb);
138
139   enum
140   {
141      PIT8253_MAX_TIMER = 3
142   };
143
144   pit8253_timer m_timers[PIT8253_MAX_TIMER];
145
146   double m_clk0;
147   double m_clk1;
148   double m_clk2;
149   devcb_write_line m_out0_handler;
150   devcb_write_line m_out1_handler;
151   devcb_write_line m_out2_handler;
152152};
153153
154154extern const device_type PIT8253;

Previous 199869 Revisions Next


© 1997-2024 The MAME Team