Previous 15994 Revisions Next

r3081 Friday 22nd August, 2008 at 15:56:38 UTC by Justin Kerk
Fix svn:eol-style
[src/mess/machine]adc080x.c adc080x.h

src/mess/machine/adc080x.c
r3080r3081
1/**********************************************************************
2 
3    ADC0808/ADC0809 8-Bit A/D Converter emulation
4 
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7 
8**********************************************************************/
9 
10#include "driver.h"
11#include "adc080x.h"
12 
13typedef struct _adc080x_t adc080x_t;
14struct _adc080x_t
15{
16   const adc080x_interface *intf;      /* interface */
17 
18   int address;                  /* analog channel address */
19   int ale;                     /* address latch enable pin */
20   int start;                     /* start conversion pin */
21   int eoc;                     /* end of conversion pin */
22   int next_eoc;                  /* next value end of conversion pin */
23   
24   UINT8 sar;                     /* successive approximation register */
25 
26   int cycle;                     /* clock cycle counter */
27   int bit;                     /* bit counter */
28 
29   /* timers */
30   emu_timer *cycle_timer;            /* cycle timer */
31};
32 
33INLINE adc080x_t *get_safe_token(const device_config *device)
34{
35   assert(device != NULL);
36   assert(device->token != NULL);
37 
38   return (adc080x_t *)device->token;
39}
40 
41/* Approximation Cycle */
42 
43static TIMER_CALLBACK( cycle_tick )
44{
45   const device_config *device = ptr;
46   adc080x_t *adc080x = get_safe_token(device);
47 
48   if (!adc080x->start)
49   {
50      if (adc080x->cycle == 7)
51      {
52         adc080x->bit++;
53 
54         if (adc080x->bit == 8)
55         {
56            /* sample input */
57            double vref_pos = adc080x->intf->vref_pos_r(device);
58            double vref_neg = adc080x->intf->vref_neg_r(device);
59            double input = adc080x->intf->input_r(device, adc080x->address);
60 
61            adc080x->sar = (255 * (input - vref_neg)) / (vref_pos - vref_neg);
62 
63            /* trigger end of conversion */
64            adc080x->next_eoc = 1;
65         }
66      }
67   }
68 
69   if (adc080x->cycle == 0)
70   {
71      /* set end of conversion pin */
72      if (adc080x->next_eoc != adc080x->eoc)
73      {
74         adc080x->intf->on_eoc_changed(device, adc080x->next_eoc);
75         adc080x->eoc = adc080x->next_eoc;
76      }
77   }
78 
79   adc080x->cycle++;
80 
81   if (adc080x->cycle == 8)
82   {
83      adc080x->cycle = 0;
84   }
85}
86 
87/* Address Latch Enable */
88 
89void adc080x_ale_w(const device_config *device, int level, int address)
90{
91   adc080x_t *adc080x = get_safe_token(device);
92 
93   if (adc080x->ale && !level) // falling edge
94   {
95      adc080x->address = address;
96   }
97 
98   adc080x->ale = level;
99}
100 
101/* Start Conversion */
102 
103void adc080x_start_w(const device_config *device, int level)
104{
105   adc080x_t *adc080x = get_safe_token(device);
106 
107   if (!adc080x->start && level) // rising edge
108   {
109      // reset registers
110 
111      adc080x->sar = 0;
112      adc080x->bit = 0;
113   }
114   else if (adc080x->start && !level) // falling edge
115   {
116      // start conversion
117 
118      adc080x->next_eoc = 0;
119   }
120 
121   adc080x->start = level;
122}
123 
124/* Conversion Data */
125 
126READ8_DEVICE_HANDLER( adc080x_data_r )
127{
128   adc080x_t *adc080x = get_safe_token(device);
129 
130   return adc080x->sar;
131}
132 
133/* Device Interface */
134 
135static DEVICE_START( adc080x )
136{
137   adc080x_t *adc080x = get_safe_token(device);
138   char unique_tag[30];
139 
140   /* validate arguments */
141   assert(device != NULL);
142   assert(device->tag != NULL);
143   assert(strlen(device->tag) < 20);
144 
145   adc080x->intf = device->static_config;
146 
147   assert(adc080x->intf != NULL);
148   assert(adc080x->intf->clock > 0);
149 
150   /* set initial values */
151   adc080x->eoc = 1;
152 
153   /* allocate cycle timer */
154   adc080x->cycle_timer = timer_alloc(cycle_tick, (void *)device);
155   timer_adjust_periodic(adc080x->cycle_timer, attotime_zero, 0, ATTOTIME_IN_HZ(adc080x->intf->clock));
156 
157   /* register for state saving */
158   state_save_combine_module_and_tag(unique_tag, "adc080x", device->tag);
159 
160   state_save_register_item(unique_tag, 0, adc080x->address);
161   state_save_register_item(unique_tag, 0, adc080x->ale);
162   state_save_register_item(unique_tag, 0, adc080x->start);
163   state_save_register_item(unique_tag, 0, adc080x->eoc);
164   state_save_register_item(unique_tag, 0, adc080x->next_eoc);
165   state_save_register_item(unique_tag, 0, adc080x->sar);
166   state_save_register_item(unique_tag, 0, adc080x->cycle);
167   state_save_register_item(unique_tag, 0, adc080x->bit);
168}
169 
170static DEVICE_SET_INFO( adc080x )
171{
172   switch (state)
173   {
174      /* no parameters to set */
175   }
176}
177 
178DEVICE_GET_INFO( adc0808 )
179{
180   switch (state)
181   {
182      /* --- the following bits of info are returned as 64-bit signed integers --- */
183      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(adc080x_t);            break;
184      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
185      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;         break;
186 
187      /* --- the following bits of info are returned as pointers to data or functions --- */
188      case DEVINFO_FCT_SET_INFO:                  info->set_info = DEVICE_SET_INFO_NAME(adc080x); break;
189      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(adc080x);   break;
190      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
191      case DEVINFO_FCT_RESET:                     /* Nothing */                        break;
192 
193      /* --- the following bits of info are returned as NULL-terminated strings --- */
194      case DEVINFO_STR_NAME:                     info->s = "National Semiconductor ADC0808";   break;
195      case DEVINFO_STR_FAMILY:                  info->s = "National Semiconductor ADC080";   break;
196      case DEVINFO_STR_VERSION:                  info->s = "1.0";                     break;
197      case DEVINFO_STR_SOURCE_FILE:               info->s = __FILE__;                     break;
198      case DEVINFO_STR_CREDITS:                  info->s = "Copyright MESS Team";         break;
199   }
200}
201 
202DEVICE_GET_INFO( adc0809 )
203{
204   switch (state)
205   {
206      /* --- the following bits of info are returned as NULL-terminated strings --- */
207      case DEVINFO_STR_NAME:                     info->s = "National Semiconductor ADC0809";   break;
208 
209      default:                              DEVICE_GET_INFO_CALL(adc0808);            break;
210   }
211}
1/**********************************************************************
2
3    ADC0808/ADC0809 8-Bit A/D Converter emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************/
9
10#include "driver.h"
11#include "adc080x.h"
12
13typedef struct _adc080x_t adc080x_t;
14struct _adc080x_t
15{
16   const adc080x_interface *intf;      /* interface */
17
18   int address;                  /* analog channel address */
19   int ale;                     /* address latch enable pin */
20   int start;                     /* start conversion pin */
21   int eoc;                     /* end of conversion pin */
22   int next_eoc;                  /* next value end of conversion pin */
23   
24   UINT8 sar;                     /* successive approximation register */
25
26   int cycle;                     /* clock cycle counter */
27   int bit;                     /* bit counter */
28
29   /* timers */
30   emu_timer *cycle_timer;            /* cycle timer */
31};
32
33INLINE adc080x_t *get_safe_token(const device_config *device)
34{
35   assert(device != NULL);
36   assert(device->token != NULL);
37
38   return (adc080x_t *)device->token;
39}
40
41/* Approximation Cycle */
42
43static TIMER_CALLBACK( cycle_tick )
44{
45   const device_config *device = ptr;
46   adc080x_t *adc080x = get_safe_token(device);
47
48   if (!adc080x->start)
49   {
50      if (adc080x->cycle == 7)
51      {
52         adc080x->bit++;
53
54         if (adc080x->bit == 8)
55         {
56            /* sample input */
57            double vref_pos = adc080x->intf->vref_pos_r(device);
58            double vref_neg = adc080x->intf->vref_neg_r(device);
59            double input = adc080x->intf->input_r(device, adc080x->address);
60
61            adc080x->sar = (255 * (input - vref_neg)) / (vref_pos - vref_neg);
62
63            /* trigger end of conversion */
64            adc080x->next_eoc = 1;
65         }
66      }
67   }
68
69   if (adc080x->cycle == 0)
70   {
71      /* set end of conversion pin */
72      if (adc080x->next_eoc != adc080x->eoc)
73      {
74         adc080x->intf->on_eoc_changed(device, adc080x->next_eoc);
75         adc080x->eoc = adc080x->next_eoc;
76      }
77   }
78
79   adc080x->cycle++;
80
81   if (adc080x->cycle == 8)
82   {
83      adc080x->cycle = 0;
84   }
85}
86
87/* Address Latch Enable */
88
89void adc080x_ale_w(const device_config *device, int level, int address)
90{
91   adc080x_t *adc080x = get_safe_token(device);
92
93   if (adc080x->ale && !level) // falling edge
94   {
95      adc080x->address = address;
96   }
97
98   adc080x->ale = level;
99}
100
101/* Start Conversion */
102
103void adc080x_start_w(const device_config *device, int level)
104{
105   adc080x_t *adc080x = get_safe_token(device);
106
107   if (!adc080x->start && level) // rising edge
108   {
109      // reset registers
110
111      adc080x->sar = 0;
112      adc080x->bit = 0;
113   }
114   else if (adc080x->start && !level) // falling edge
115   {
116      // start conversion
117
118      adc080x->next_eoc = 0;
119   }
120
121   adc080x->start = level;
122}
123
124/* Conversion Data */
125
126READ8_DEVICE_HANDLER( adc080x_data_r )
127{
128   adc080x_t *adc080x = get_safe_token(device);
129
130   return adc080x->sar;
131}
132
133/* Device Interface */
134
135static DEVICE_START( adc080x )
136{
137   adc080x_t *adc080x = get_safe_token(device);
138   char unique_tag[30];
139
140   /* validate arguments */
141   assert(device != NULL);
142   assert(device->tag != NULL);
143   assert(strlen(device->tag) < 20);
144
145   adc080x->intf = device->static_config;
146
147   assert(adc080x->intf != NULL);
148   assert(adc080x->intf->clock > 0);
149
150   /* set initial values */
151   adc080x->eoc = 1;
152
153   /* allocate cycle timer */
154   adc080x->cycle_timer = timer_alloc(cycle_tick, (void *)device);
155   timer_adjust_periodic(adc080x->cycle_timer, attotime_zero, 0, ATTOTIME_IN_HZ(adc080x->intf->clock));
156
157   /* register for state saving */
158   state_save_combine_module_and_tag(unique_tag, "adc080x", device->tag);
159
160   state_save_register_item(unique_tag, 0, adc080x->address);
161   state_save_register_item(unique_tag, 0, adc080x->ale);
162   state_save_register_item(unique_tag, 0, adc080x->start);
163   state_save_register_item(unique_tag, 0, adc080x->eoc);
164   state_save_register_item(unique_tag, 0, adc080x->next_eoc);
165   state_save_register_item(unique_tag, 0, adc080x->sar);
166   state_save_register_item(unique_tag, 0, adc080x->cycle);
167   state_save_register_item(unique_tag, 0, adc080x->bit);
168}
169
170static DEVICE_SET_INFO( adc080x )
171{
172   switch (state)
173   {
174      /* no parameters to set */
175   }
176}
177
178DEVICE_GET_INFO( adc0808 )
179{
180   switch (state)
181   {
182      /* --- the following bits of info are returned as 64-bit signed integers --- */
183      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(adc080x_t);            break;
184      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
185      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;         break;
186
187      /* --- the following bits of info are returned as pointers to data or functions --- */
188      case DEVINFO_FCT_SET_INFO:                  info->set_info = DEVICE_SET_INFO_NAME(adc080x); break;
189      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(adc080x);   break;
190      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
191      case DEVINFO_FCT_RESET:                     /* Nothing */                        break;
192
193      /* --- the following bits of info are returned as NULL-terminated strings --- */
194      case DEVINFO_STR_NAME:                     info->s = "National Semiconductor ADC0808";   break;
195      case DEVINFO_STR_FAMILY:                  info->s = "National Semiconductor ADC080";   break;
196      case DEVINFO_STR_VERSION:                  info->s = "1.0";                     break;
197      case DEVINFO_STR_SOURCE_FILE:               info->s = __FILE__;                     break;
198      case DEVINFO_STR_CREDITS:                  info->s = "Copyright MESS Team";         break;
199   }
200}
201
202DEVICE_GET_INFO( adc0809 )
203{
204   switch (state)
205   {
206      /* --- the following bits of info are returned as NULL-terminated strings --- */
207      case DEVINFO_STR_NAME:                     info->s = "National Semiconductor ADC0809";   break;
208
209      default:                              DEVICE_GET_INFO_CALL(adc0808);            break;
210   }
211}
src/mess/machine/adc080x.h
r3080r3081
1/**********************************************************************
2 
3    ADC0808/ADC0809 8-Bit A/D Converter emulation
4 
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7 
8**********************************************************************
9                     _____   _____
10               IN3    1 |*    \_/     | 28  IN2
11               IN4    2 |          | 27  IN1
12               IN5    3 |          | 26  IN0
13               IN6    4 |          | 25  ADD A
14               IN7    5 |          | 24  ADD B
15             START    6 |          | 23  ADD C
16               EOC    7 |   ADC0808    | 22  ALE
17               2-5    8 |   ADC0809    | 21  2-1 MSB
18       OUTPUT ENABLE    9 |          | 20  2-2
19             CLOCK   10 |          | 19  2-3
20               Vcc   11 |          | 18  2-4
21             Vref+   12 |          | 17  2-8 LSB
22               GND   13 |          | 16  Vref-
23               2-7   14 |_____________| 15  2-6
24 
25**********************************************************************/
26 
27#ifndef __ADC080X__
28#define __ADC080X__
29 
30typedef void (*adc080x_on_eoc_changed_func) (const device_config *device, int level);
31#define ADC080X_ON_EOC_CHANGED(name) void name(const device_config *device, int level)
32 
33typedef double (*adc080x_vref_pos_read) (const device_config *device);
34#define ADC080X_VREF_POSITIVE_READ(name) double name(const device_config *device)
35 
36typedef double (*adc080x_vref_neg_read) (const device_config *device);
37#define ADC080X_VREF_NEGATIVE_READ(name) double name(const device_config *device)
38 
39typedef double (*adc080x_input_read) (const device_config *device, int channel);
40#define ADC080X_INPUT_READ(name) double name(const device_config *device, int channel)
41 
42#define ADC0808      DEVICE_GET_INFO_NAME(adc0808)
43#define ADC0809      DEVICE_GET_INFO_NAME(adc0809)
44 
45/* interface */
46typedef struct _adc080x_interface adc080x_interface;
47struct _adc080x_interface
48{
49   int clock;               /* the clock (pin 10) of the chip */
50 
51   /* this gets called for every change of the EOC pin (pin 7) */
52   adc080x_on_eoc_changed_func      on_eoc_changed;
53 
54   /* this gets called for every sampling of the positive reference voltage (pin 12) */
55   adc080x_vref_pos_read         vref_pos_r;
56 
57   /* this gets called for every sampling of the negative reference voltage (pin 16) */
58   adc080x_vref_neg_read         vref_neg_r;
59 
60   /* this gets called for every sampling of the input pins IN0-IN7 */
61   adc080x_input_read            input_r;
62};
63#define ADC080X_INTERFACE(name) const adc080x_interface (name)=
64 
65/* device interface */
66DEVICE_GET_INFO( adc0808 );
67DEVICE_GET_INFO( adc0809 );
68 
69/* address latching */
70void adc080x_ale_w(const device_config *device, int level, int address);
71 
72/* start conversion */
73void adc080x_start_w(const device_config *device, int level);
74 
75/* conversion data */
76READ8_DEVICE_HANDLER( adc080x_data_r );
77 
78#endif
1/**********************************************************************
2
3    ADC0808/ADC0809 8-Bit A/D Converter emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************
9                     _____   _____
10               IN3    1 |*    \_/     | 28  IN2
11               IN4    2 |          | 27  IN1
12               IN5    3 |          | 26  IN0
13               IN6    4 |          | 25  ADD A
14               IN7    5 |          | 24  ADD B
15             START    6 |          | 23  ADD C
16               EOC    7 |   ADC0808    | 22  ALE
17               2-5    8 |   ADC0809    | 21  2-1 MSB
18       OUTPUT ENABLE    9 |          | 20  2-2
19             CLOCK   10 |          | 19  2-3
20               Vcc   11 |          | 18  2-4
21             Vref+   12 |          | 17  2-8 LSB
22               GND   13 |          | 16  Vref-
23               2-7   14 |_____________| 15  2-6
24
25**********************************************************************/
26
27#ifndef __ADC080X__
28#define __ADC080X__
29
30typedef void (*adc080x_on_eoc_changed_func) (const device_config *device, int level);
31#define ADC080X_ON_EOC_CHANGED(name) void name(const device_config *device, int level)
32
33typedef double (*adc080x_vref_pos_read) (const device_config *device);
34#define ADC080X_VREF_POSITIVE_READ(name) double name(const device_config *device)
35
36typedef double (*adc080x_vref_neg_read) (const device_config *device);
37#define ADC080X_VREF_NEGATIVE_READ(name) double name(const device_config *device)
38
39typedef double (*adc080x_input_read) (const device_config *device, int channel);
40#define ADC080X_INPUT_READ(name) double name(const device_config *device, int channel)
41
42#define ADC0808      DEVICE_GET_INFO_NAME(adc0808)
43#define ADC0809      DEVICE_GET_INFO_NAME(adc0809)
44
45/* interface */
46typedef struct _adc080x_interface adc080x_interface;
47struct _adc080x_interface
48{
49   int clock;               /* the clock (pin 10) of the chip */
50
51   /* this gets called for every change of the EOC pin (pin 7) */
52   adc080x_on_eoc_changed_func      on_eoc_changed;
53
54   /* this gets called for every sampling of the positive reference voltage (pin 12) */
55   adc080x_vref_pos_read         vref_pos_r;
56
57   /* this gets called for every sampling of the negative reference voltage (pin 16) */
58   adc080x_vref_neg_read         vref_neg_r;
59
60   /* this gets called for every sampling of the input pins IN0-IN7 */
61   adc080x_input_read            input_r;
62};
63#define ADC080X_INTERFACE(name) const adc080x_interface (name)=
64
65/* device interface */
66DEVICE_GET_INFO( adc0808 );
67DEVICE_GET_INFO( adc0809 );
68
69/* address latching */
70void adc080x_ale_w(const device_config *device, int level, int address);
71
72/* start conversion */
73void adc080x_start_w(const device_config *device, int level);
74
75/* conversion data */
76READ8_DEVICE_HANDLER( adc080x_data_r );
77
78#endif

Previous 15994 Revisions Next


© 1998-2018 The MESS Team