Previous 15994 Revisions Next

r11543 Saturday 14th May, 2011 at 12:12:49 UTC by Wilbert Pol
mc6845.c: Converted to C++.
[src/emu/video]mc6845.c mc6845.h

src/emu/video/mc6845.c
r11542r11543
2424
2525#include "emu.h"
2626#include "mc6845.h"
27#include "machine/devhelpr.h"
2728
2829
2930#define LOG      (0)
3031
3132
32/* device types */
33enum
34{
35   TYPE_MC6845 = 0,
36   TYPE_MC6845_1,
37   TYPE_C6545_1,
38   TYPE_R6545_1,
39   TYPE_H46505,
40   TYPE_HD6845,
41   TYPE_SY6545_1,
42   TYPE_SY6845E,
33const device_type MC6845 = &device_creator<mc6845_device>;
34const device_type MC6845_1 = &device_creator<mc6845_1_device>;
35const device_type R6545_1 = &device_creator<r6545_1_device>;
36const device_type C6545_1 = &device_creator<c6545_1_device>;
37const device_type H46505 = &device_creator<h46505_device>;
38const device_type HD6845 = &device_creator<hd6845_device>;
39const device_type SY6545_1 = &device_creator<sy6545_1_device>;
40const device_type SY6845E = &device_creator<sy6845e_device>;
4341
44   NUM_TYPES
45};
4642
4743/* mode macros */
48#define MODE_TRANSPARENT(d)            (((d)->mode_control & 0x08) != 0)
49#define MODE_TRANSPARENT_PHI2(d)      (((d)->mode_control & 0x88) == 0x88)
44#define MODE_TRANSPARENT         ((m_mode_control & 0x08) != 0)
45#define MODE_TRANSPARENT_PHI2      ((m_mode_control & 0x88) == 0x88)
5046/* FIXME: not supported yet */
51#define MODE_TRANSPARENT_BLANK(d)      (((d)->mode_control & 0x88) == 0x08)
52#define MODE_UPDATE_STROBE(d)         (((d)->mode_control & 0x40) != 0)
53#define MODE_CURSOR_SKEW(d)            (((d)->mode_control & 0x20) != 0)
54#define MODE_DISPLAY_ENABLE_SKEW(d)      (((d)->mode_control & 0x10) != 0)
55#define MODE_ROW_COLUMN_ADDRESSING(d)   (((d)->mode_control & 0x04) != 0)
47#define MODE_TRANSPARENT_BLANK      ((m_mode_control & 0x88) == 0x08)
48#define MODE_UPDATE_STROBE         ((m_mode_control & 0x40) != 0)
49#define MODE_CURSOR_SKEW         ((m_mode_control & 0x20) != 0)
50#define MODE_DISPLAY_ENABLE_SKEW   ((m_mode_control & 0x10) != 0)
51#define MODE_ROW_COLUMN_ADDRESSING   ((m_mode_control & 0x04) != 0)
5652
57/* capabilities */                                     /* MC6845    MC6845-1    C6545-1    R6545-1    H46505    HD6845    SY6545-1    SY6845E */
58static const int supports_disp_start_addr_r[NUM_TYPES] = {  TRUE,       TRUE,     FALSE,     FALSE,    FALSE,    FALSE,      FALSE,      FALSE };
59static const int supports_vert_sync_width[NUM_TYPES]   = { FALSE,       TRUE,      TRUE,      TRUE,    FALSE,     TRUE,       TRUE,       TRUE };
60static const int supports_status_reg_d5[NUM_TYPES]     = { FALSE,      FALSE,      TRUE,      TRUE,    FALSE,    FALSE,       TRUE,       TRUE };
61static const int supports_status_reg_d6[NUM_TYPES]     = { FALSE,      FALSE,      TRUE,      TRUE,    FALSE,    FALSE,       TRUE,       TRUE };
6253
63/* FIXME: check other variants */
64static const int supports_status_reg_d7[NUM_TYPES]     = { FALSE,      FALSE,     FALSE,      TRUE,    FALSE,    FALSE,       TRUE,       TRUE };
65
66/* FIXME: check other variants */
67static const int supports_transparent[NUM_TYPES]       = { FALSE,      FALSE,     FALSE,      TRUE,    FALSE,    FALSE,       TRUE,       TRUE };
68
69
70typedef struct _mc6845_t mc6845_t;
71struct _mc6845_t
54void mc6845_device::device_config_complete()
7255{
73   devcb_resolved_write_line         out_de_func;
74   devcb_resolved_write_line         out_cur_func;
75   devcb_resolved_write_line         out_hsync_func;
76   devcb_resolved_write_line         out_vsync_func;
56   const mc6845_interface *intf = reinterpret_cast<const mc6845_interface *>(static_config());
7757
78   int device_type;
79   const mc6845_interface *intf;
80   screen_device *screen;
58   if ( intf != NULL )
59   {
60      *static_cast<mc6845_interface *>(this) = *intf;
61   }
62   else
63   {
64      m_screen_tag = NULL;
65      m_hpixels_per_column = 0;
66      m_begin_update = NULL;
67      m_update_row = NULL;
68      m_end_update = NULL;
69      m_on_update_addr_changed = NULL;
70      memset(&m_out_de_func, 0, sizeof(m_out_de_func));
71      memset(&m_out_cur_func, 0, sizeof(m_out_cur_func));
72      memset(&m_out_hsync_func, 0, sizeof(m_out_hsync_func));
73      memset(&m_out_vsync_func, 0, sizeof(m_out_vsync_func));
74   }
75}
8176
82   /* register file */
83   UINT8   horiz_char_total;   /* 0x00 */
84   UINT8   horiz_disp;         /* 0x01 */
85   UINT8   horiz_sync_pos;      /* 0x02 */
86   UINT8   sync_width;         /* 0x03 */
87   UINT8   vert_char_total;   /* 0x04 */
88   UINT8   vert_total_adj;      /* 0x05 */
89   UINT8   vert_disp;         /* 0x06 */
90   UINT8   vert_sync_pos;      /* 0x07 */
91   UINT8   mode_control;      /* 0x08 */
92   UINT8   max_ras_addr;      /* 0x09 */
93   UINT8   cursor_start_ras;   /* 0x0a */
94   UINT8   cursor_end_ras;      /* 0x0b */
95   UINT16   disp_start_addr;   /* 0x0c/0x0d */
96   UINT16   cursor_addr;      /* 0x0e/0x0f */
97   UINT16   light_pen_addr;      /* 0x10/0x11 */
98   UINT16   update_addr;      /* 0x12/0x13 */
9977
100   /* other internal state */
101   UINT64   clock;
102   UINT8   register_address_latch;
103   UINT8   hpixels_per_column;
104   UINT8   cursor_state;   /* 0 = off, 1 = on */
105   UINT8   cursor_blink_count;
106   UINT8   update_ready_bit;
107   /* output signals */
108   int      cur;
109   int      hsync;
110   int      vsync;
111   int      de;
112
113   /* internal counters */
114   UINT8   character_counter;      /* Not used yet */
115   UINT8   hsync_width_counter;   /* Not used yet */
116   UINT8   line_counter;
117   UINT8   raster_counter;
118   UINT8   adjust_counter;
119   UINT8   vsync_width_counter;
120
121   UINT8   line_enable_ff;      /* Internal flip flop which is set when the line_counter is reset and reset when vert_disp is reached */
122   UINT8   vsync_ff;
123   UINT8   adjust_active;
124   UINT16   line_address;
125   INT16   cursor_x;
126
127   /* timers */
128   emu_timer *line_timer;
129   emu_timer *de_off_timer;
130   emu_timer *cur_on_timer;
131   emu_timer *cur_off_timer;
132   emu_timer *hsync_on_timer;
133   emu_timer *hsync_off_timer;
134   emu_timer *light_pen_latch_timer;
135   emu_timer *upd_adr_timer;
136
137   /* computed values - do NOT state save these! */
138   /* These computed are used to define the screen parameters for a driver */
139   UINT16   horiz_pix_total;
140   UINT16   vert_pix_total;
141   UINT16   max_visible_x;
142   UINT16   max_visible_y;
143   UINT16   hsync_on_pos;
144   UINT16   hsync_off_pos;
145   UINT16   vsync_on_pos;
146   UINT16   vsync_off_pos;
147   int      has_valid_parameters;
148
149   UINT16   current_disp_addr;   /* the display address currently drawn (used only in mc6845_update) */
150
151   UINT8    light_pen_latched;
152   attotime upd_time;
153};
154
155
156static void mc6845_state_save_postload(mc6845_t *mc6845);
157static void recompute_parameters(mc6845_t *mc6845, int postload);
158static void update_upd_adr_timer(mc6845_t *mc6845);
159static void update_cursor_state(mc6845_t *mc6845);
160
161
162const mc6845_interface mc6845_null_interface = { 0 };
163
164
165/* makes sure that the passed in device is the right type */
166INLINE mc6845_t *get_safe_token(device_t *device)
78mc6845_device::mc6845_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock)
79   : device_t(mconfig, type, name, tag, owner, clock)
16780{
168   assert(device != NULL);
169   assert((device->type() == MC6845) ||
170         (device->type() == MC6845_1) ||
171         (device->type() == C6545_1) ||
172         (device->type() == R6545_1) ||
173         (device->type() == H46505) ||
174         (device->type() == HD6845) ||
175         (device->type() == SY6545_1) ||
176         (device->type() == SY6845E));
177
178   return (mc6845_t *)downcast<legacy_device_base *>(device)->token();
17981}
18082
181
182static void mc6845_state_save_postload(mc6845_t *mc6845)
83mc6845_device::mc6845_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
84   : device_t(mconfig, MC6845, "mc6845", tag, owner, clock)
18385{
184   recompute_parameters(mc6845, TRUE);
18586}
18687
18788
188static TIMER_CALLBACK( on_update_address_cb )
89void mc6845_device::device_post_load()
18990{
190   device_t *device = (device_t *)ptr;
191   mc6845_t *mc6845 = get_safe_token(device);
192   int addr = (param >> 8);
193   int strobe = (param & 0xff);
194
195   /* call the callback function -- we know it exists */
196   mc6845->intf->on_update_addr_changed(device, addr, strobe);
197
198   if(!mc6845->update_ready_bit && MODE_TRANSPARENT_BLANK(mc6845))
199   {
200      mc6845->update_addr++;
201      mc6845->update_addr &= 0x3fff;
202      mc6845->update_ready_bit = 1;
203   }
91   recompute_parameters(true);
20492}
20593
206INLINE void call_on_update_address(device_t *device, int strobe)
207{
208   mc6845_t *mc6845 = get_safe_token(device);
20994
210   if (mc6845->intf->on_update_addr_changed)
211      device->machine().scheduler().timer_set(attotime::zero, FUNC(on_update_address_cb), (mc6845->update_addr << 8) | strobe, (void *) device);
95void mc6845_device::call_on_update_address(int strobe)
96{
97   if (m_on_update_addr_changed)
98      m_upd_trans_timer->adjust(attotime::zero, (m_update_addr << 8) | strobe);
21299   else
213100      fatalerror("M6845: transparent memory mode without handler\n");
214101}
215102
216103
217WRITE8_DEVICE_HANDLER( mc6845_address_w )
104void mc6845_device::address_w(UINT8 data)
218105{
219   mc6845_t *mc6845 = get_safe_token(device);
220
221   mc6845->register_address_latch = data & 0x1f;
106   m_register_address_latch = data & 0x1f;
222107}
223108
224109
225READ8_DEVICE_HANDLER( mc6845_status_r )
110UINT8 mc6845_device::status_r()
226111{
227   mc6845_t *mc6845 = get_safe_token(device);
228112   UINT8 ret = 0;
229113
230114   /* VBLANK bit */
231   if (supports_status_reg_d5[mc6845->device_type] && ( mc6845->line_enable_ff == 0 ))
115   if (m_supports_status_reg_d5 && !m_line_enable_ff)
232116      ret = ret | 0x20;
233117
234118   /* light pen latched */
235   if (supports_status_reg_d6[mc6845->device_type] && mc6845->light_pen_latched)
119   if (m_supports_status_reg_d6 && m_light_pen_latched)
236120      ret = ret | 0x40;
237121
238122   /* UPDATE ready */
239   if (supports_status_reg_d7[mc6845->device_type] && mc6845->update_ready_bit)
123   if (m_supports_status_reg_d7 && m_update_ready_bit)
240124      ret = ret | 0x80;
241125
242126   return ret;
243127}
244128
245129
246READ8_DEVICE_HANDLER( mc6845_register_r )
130UINT8 mc6845_device::register_r()
247131{
248   mc6845_t *mc6845 = get_safe_token(device);
249132   UINT8 ret = 0;
250133
251   switch (mc6845->register_address_latch)
134   switch (m_register_address_latch)
252135   {
253      case 0x0c:  ret = supports_disp_start_addr_r[mc6845->device_type] ? (mc6845->disp_start_addr >> 8) & 0xff : 0; break;
254      case 0x0d:  ret = supports_disp_start_addr_r[mc6845->device_type] ? (mc6845->disp_start_addr >> 0) & 0xff : 0; break;
255      case 0x0e:  ret = (mc6845->cursor_addr    >> 8) & 0xff; break;
256      case 0x0f:  ret = (mc6845->cursor_addr    >> 0) & 0xff; break;
257      case 0x10:  ret = (mc6845->light_pen_addr >> 8) & 0xff; mc6845->light_pen_latched = FALSE; break;
258      case 0x11:  ret = (mc6845->light_pen_addr >> 0) & 0xff; mc6845->light_pen_latched = FALSE; break;
136      case 0x0c:  ret = m_supports_disp_start_addr_r ? (m_disp_start_addr >> 8) & 0xff : 0; break;
137      case 0x0d:  ret = m_supports_disp_start_addr_r ? (m_disp_start_addr >> 0) & 0xff : 0; break;
138      case 0x0e:  ret = (m_cursor_addr    >> 8) & 0xff; break;
139      case 0x0f:  ret = (m_cursor_addr    >> 0) & 0xff; break;
140      case 0x10:  ret = (m_light_pen_addr >> 8) & 0xff; m_light_pen_latched = false; break;
141      case 0x11:  ret = (m_light_pen_addr >> 0) & 0xff; m_light_pen_latched = false; break;
259142      case 0x1f:
260         if (supports_transparent[mc6845->device_type] && MODE_TRANSPARENT(mc6845))
143         if (m_supports_transparent && MODE_TRANSPARENT)
261144         {
262            if(MODE_TRANSPARENT_PHI2(mc6845))
145            if(MODE_TRANSPARENT_PHI2)
263146            {
264               mc6845->update_addr++;
265               mc6845->update_addr &= 0x3fff;
266               call_on_update_address(device, 0);
147               m_update_addr++;
148               m_update_addr &= 0x3fff;
149               call_on_update_address(0);
267150            }
268151            else
269152            {
270153               /* MODE_TRANSPARENT_BLANK */
271               if(mc6845->update_ready_bit)
154               if(m_update_ready_bit)
272155               {
273                  mc6845->update_ready_bit = 0;
274                  update_upd_adr_timer(mc6845);
156                  m_update_ready_bit = false;
157                  update_upd_adr_timer();
275158               }
276159            }
277160         }
r11542r11543
285168}
286169
287170
288WRITE8_DEVICE_HANDLER( mc6845_register_w )
171void mc6845_device::register_w(UINT8 data)
289172{
290   mc6845_t *mc6845 = get_safe_token(device);
173   if (LOG)  logerror("%s:M6845 reg 0x%02x = 0x%02x\n", machine().describe_context(), m_register_address_latch, data);
291174
292   if (LOG)  logerror("%s:M6845 reg 0x%02x = 0x%02x\n", device->machine().describe_context(), mc6845->register_address_latch, data);
293
294   switch (mc6845->register_address_latch)
175   switch (m_register_address_latch)
295176   {
296      case 0x00:  mc6845->horiz_char_total =   data & 0xff; break;
297      case 0x01:  mc6845->horiz_disp       =   data & 0xff; break;
298      case 0x02:  mc6845->horiz_sync_pos   =   data & 0xff; break;
299      case 0x03:  mc6845->sync_width       =   data & 0xff; break;
300      case 0x04:  mc6845->vert_char_total  =   data & 0x7f; break;
301      case 0x05:  mc6845->vert_total_adj   =   data & 0x1f; break;
302      case 0x06:  mc6845->vert_disp        =   data & 0x7f; break;
303      case 0x07:  mc6845->vert_sync_pos    =   data & 0x7f; break;
304      case 0x08:  mc6845->mode_control     =   data & 0xff; break;
305      case 0x09:  mc6845->max_ras_addr     =   data & 0x1f; break;
306      case 0x0a:  mc6845->cursor_start_ras =   data & 0x7f; break;
307      case 0x0b:  mc6845->cursor_end_ras   =   data & 0x1f; break;
308      case 0x0c:  mc6845->disp_start_addr  = ((data & 0x3f) << 8) | (mc6845->disp_start_addr & 0x00ff); break;
309      case 0x0d:  mc6845->disp_start_addr  = ((data & 0xff) << 0) | (mc6845->disp_start_addr & 0xff00); break;
310      case 0x0e:  mc6845->cursor_addr      = ((data & 0x3f) << 8) | (mc6845->cursor_addr & 0x00ff); break;
311      case 0x0f:  mc6845->cursor_addr      = ((data & 0xff) << 0) | (mc6845->cursor_addr & 0xff00); break;
177      case 0x00:  m_horiz_char_total =   data & 0xff; break;
178      case 0x01:  m_horiz_disp       =   data & 0xff; break;
179      case 0x02:  m_horiz_sync_pos   =   data & 0xff; break;
180      case 0x03:  m_sync_width       =   data & 0xff; break;
181      case 0x04:  m_vert_char_total  =   data & 0x7f; break;
182      case 0x05:  m_vert_total_adj   =   data & 0x1f; break;
183      case 0x06:  m_vert_disp        =   data & 0x7f; break;
184      case 0x07:  m_vert_sync_pos    =   data & 0x7f; break;
185      case 0x08:  m_mode_control     =   data & 0xff; break;
186      case 0x09:  m_max_ras_addr     =   data & 0x1f; break;
187      case 0x0a:  m_cursor_start_ras =   data & 0x7f; break;
188      case 0x0b:  m_cursor_end_ras   =   data & 0x1f; break;
189      case 0x0c:  m_disp_start_addr  = ((data & 0x3f) << 8) | (m_disp_start_addr & 0x00ff); break;
190      case 0x0d:  m_disp_start_addr  = ((data & 0xff) << 0) | (m_disp_start_addr & 0xff00); break;
191      case 0x0e:  m_cursor_addr      = ((data & 0x3f) << 8) | (m_cursor_addr & 0x00ff); break;
192      case 0x0f:  m_cursor_addr      = ((data & 0xff) << 0) | (m_cursor_addr & 0xff00); break;
312193      case 0x10: /* read-only */ break;
313194      case 0x11: /* read-only */ break;
314195      case 0x12:
315         if (supports_transparent[mc6845->device_type])
196         if (m_supports_transparent)
316197         {
317            mc6845->update_addr = ((data & 0x3f) << 8) | (mc6845->update_addr & 0x00ff);
318            if(MODE_TRANSPARENT_PHI2(mc6845))
319               call_on_update_address(device, 0);
198            m_update_addr = ((data & 0x3f) << 8) | (m_update_addr & 0x00ff);
199            if(MODE_TRANSPARENT_PHI2)
200               call_on_update_address(0);
320201         }
321202         break;
322203      case 0x13:
323         if (supports_transparent[mc6845->device_type])
204         if (m_supports_transparent)
324205         {
325            mc6845->update_addr = ((data & 0xff) << 0) | (mc6845->update_addr & 0xff00);
326            if(MODE_TRANSPARENT_PHI2(mc6845))
327               call_on_update_address(device, 0);
206            m_update_addr = ((data & 0xff) << 0) | (m_update_addr & 0xff00);
207            if(MODE_TRANSPARENT_PHI2)
208               call_on_update_address(0);
328209         }
329210         break;
330211      case 0x1f:
331         if (supports_transparent[mc6845->device_type] && MODE_TRANSPARENT(mc6845))
212         if (m_supports_transparent && MODE_TRANSPARENT)
332213         {
333            if(MODE_TRANSPARENT_PHI2(mc6845))
214            if(MODE_TRANSPARENT_PHI2)
334215            {
335               mc6845->update_addr++;
336               mc6845->update_addr &= 0x3fff;
337               call_on_update_address(device, 0);
216               m_update_addr++;
217               m_update_addr &= 0x3fff;
218               call_on_update_address(0);
338219            }
339220            else
340221            {
341222               /* MODE_TRANSPARENT_BLANK */
342               if(mc6845->update_ready_bit)
223               if(m_update_ready_bit)
343224               {
344                  mc6845->update_ready_bit = 0;
345                  update_upd_adr_timer(mc6845);
225                  m_update_ready_bit = false;
226                  update_upd_adr_timer();
346227               }
347228            }
348229         }
r11542r11543
351232   }
352233
353234   /* display message if the Mode Control register is not zero */
354   if ((mc6845->register_address_latch == 0x08) && (mc6845->mode_control != 0))
355      if (!supports_transparent[mc6845->device_type])
356         popmessage("Mode Control %02X is not supported!!!", mc6845->mode_control);
235   if ((m_register_address_latch == 0x08) && (m_mode_control != 0))
236      if (!m_supports_transparent)
237         popmessage("Mode Control %02X is not supported!!!", m_mode_control);
357238
358   recompute_parameters(mc6845, FALSE);
239   recompute_parameters(false);
359240}
360241
361242
362static void recompute_parameters(mc6845_t *mc6845, int postload)
243void mc6845_device::recompute_parameters(bool postload)
363244{
364   if (mc6845->intf != NULL)
365   {
366      UINT16 hsync_on_pos, hsync_off_pos, vsync_on_pos, vsync_off_pos;
245   UINT16 hsync_on_pos, hsync_off_pos, vsync_on_pos, vsync_off_pos;
367246
368      /* compute the screen sizes */
369      UINT16 horiz_pix_total = (mc6845->horiz_char_total + 1) * mc6845->hpixels_per_column;
370      UINT16 vert_pix_total = (mc6845->vert_char_total + 1) * (mc6845->max_ras_addr + 1) + mc6845->vert_total_adj;
247   /* compute the screen sizes */
248   UINT16 horiz_pix_total = (m_horiz_char_total + 1) * m_hpixels_per_column;
249   UINT16 vert_pix_total = (m_vert_char_total + 1) * (m_max_ras_addr + 1) + m_vert_total_adj;
371250
372      /* determine the visible area, avoid division by 0 */
373      UINT16 max_visible_x = mc6845->horiz_disp * mc6845->hpixels_per_column - 1;
374      UINT16 max_visible_y = mc6845->vert_disp * (mc6845->max_ras_addr + 1) - 1;
251   /* determine the visible area, avoid division by 0 */
252   UINT16 max_visible_x = m_horiz_disp * m_hpixels_per_column - 1;
253   UINT16 max_visible_y = m_vert_disp * (m_max_ras_addr + 1) - 1;
375254
376      /* determine the syncing positions */
377      UINT8 horiz_sync_char_width = mc6845->sync_width & 0x0f;
378      UINT8 vert_sync_pix_width = supports_vert_sync_width[mc6845->device_type] ? (mc6845->sync_width >> 4) & 0x0f : 0x10;
255   /* determine the syncing positions */
256   UINT8 horiz_sync_char_width = m_sync_width & 0x0f;
257   UINT8 vert_sync_pix_width = m_supports_vert_sync_width ? (m_sync_width >> 4) & 0x0f : 0x10;
379258
380      if (horiz_sync_char_width == 0)
381         horiz_sync_char_width = 0x10;
259   if (horiz_sync_char_width == 0)
260      horiz_sync_char_width = 0x10;
382261
383      if (vert_sync_pix_width == 0)
384         vert_sync_pix_width = 0x10;
262   if (vert_sync_pix_width == 0)
263      vert_sync_pix_width = 0x10;
385264
386      /* determine the transparent update cycle time, 1 update every 4 character clocks */
387      mc6845->upd_time = attotime::from_hz(mc6845->clock) * (4 * mc6845->hpixels_per_column);
265   /* determine the transparent update cycle time, 1 update every 4 character clocks */
266   m_upd_time = attotime::from_hz(m_clock) * (4 * m_hpixels_per_column);
388267
389      hsync_on_pos = mc6845->horiz_sync_pos * mc6845->hpixels_per_column;
390      hsync_off_pos = hsync_on_pos + (horiz_sync_char_width * mc6845->hpixels_per_column);
391      vsync_on_pos = mc6845->vert_sync_pos * (mc6845->max_ras_addr + 1);
392      vsync_off_pos = vsync_on_pos + vert_sync_pix_width;
268   hsync_on_pos = m_horiz_sync_pos * m_hpixels_per_column;
269   hsync_off_pos = hsync_on_pos + (horiz_sync_char_width * m_hpixels_per_column);
270   vsync_on_pos = m_vert_sync_pos * (m_max_ras_addr + 1);
271   vsync_off_pos = vsync_on_pos + vert_sync_pix_width;
393272
394      /* the Commodore PET computers program a horizontal synch pulse that extends
395           past the scanline width.  I assume that the real device will clamp it */
396      if (hsync_off_pos > horiz_pix_total)
397         hsync_off_pos = horiz_pix_total;
273   /* the Commodore PET computers program a horizontal synch pulse that extends
274       past the scanline width.  I assume that the real device will clamp it */
275   if (hsync_off_pos > horiz_pix_total)
276      hsync_off_pos = horiz_pix_total;
398277
399      if (vsync_off_pos > vert_pix_total)
400         vsync_off_pos = vert_pix_total;
278   if (vsync_off_pos > vert_pix_total)
279      vsync_off_pos = vert_pix_total;
401280
402      /* update only if screen parameters changed, unless we are coming here after loading the saved state */
403      if (postload ||
404          (horiz_pix_total != mc6845->horiz_pix_total) || (vert_pix_total != mc6845->vert_pix_total) ||
405         (max_visible_x != mc6845->max_visible_x) || (max_visible_y != mc6845->max_visible_y) ||
406         (hsync_on_pos != mc6845->hsync_on_pos) || (vsync_on_pos != mc6845->vsync_on_pos) ||
407         (hsync_off_pos != mc6845->hsync_off_pos) || (vsync_off_pos != mc6845->vsync_off_pos))
281   /* update only if screen parameters changed, unless we are coming here after loading the saved state */
282   if (postload ||
283       (horiz_pix_total != m_horiz_pix_total) || (vert_pix_total != m_vert_pix_total) ||
284      (max_visible_x != m_max_visible_x) || (max_visible_y != m_max_visible_y) ||
285      (hsync_on_pos != m_hsync_on_pos) || (vsync_on_pos != m_vsync_on_pos) ||
286      (hsync_off_pos != m_hsync_off_pos) || (vsync_off_pos != m_vsync_off_pos))
287   {
288      /* update the screen if we have valid data */
289      if ((horiz_pix_total > 0) && (max_visible_x < horiz_pix_total) &&
290         (vert_pix_total > 0) && (max_visible_y < vert_pix_total) &&
291         (hsync_on_pos <= horiz_pix_total) && (vsync_on_pos <= vert_pix_total) &&
292         (hsync_on_pos != hsync_off_pos))
408293      {
409         /* update the screen if we have valid data */
410         if ((horiz_pix_total > 0) && (max_visible_x < horiz_pix_total) &&
411            (vert_pix_total > 0) && (max_visible_y < vert_pix_total) &&
412            (hsync_on_pos <= horiz_pix_total) && (vsync_on_pos <= vert_pix_total) &&
413            (hsync_on_pos != hsync_off_pos))
414         {
415            rectangle visarea;
294         rectangle visarea;
416295
417            attoseconds_t refresh = HZ_TO_ATTOSECONDS(mc6845->clock) * (mc6845->horiz_char_total + 1) * vert_pix_total;
296         attoseconds_t refresh = HZ_TO_ATTOSECONDS(m_clock) * (m_horiz_char_total + 1) * vert_pix_total;
418297
419            visarea.min_x = 0;
420            visarea.min_y = 0;
421            visarea.max_x = max_visible_x;
422            visarea.max_y = max_visible_y;
298         visarea.min_x = 0;
299         visarea.min_y = 0;
300         visarea.max_x = max_visible_x;
301         visarea.max_y = max_visible_y;
423302
424            if (LOG) logerror("M6845 config screen: HTOTAL: 0x%x  VTOTAL: 0x%x  MAX_X: 0x%x  MAX_Y: 0x%x  HSYNC: 0x%x-0x%x  VSYNC: 0x%x-0x%x  Freq: %ffps\n",
425                          horiz_pix_total, vert_pix_total, max_visible_x, max_visible_y, hsync_on_pos, hsync_off_pos - 1, vsync_on_pos, vsync_off_pos - 1, 1 / ATTOSECONDS_TO_DOUBLE(refresh));
303         if (LOG) logerror("M6845 config screen: HTOTAL: 0x%x  VTOTAL: 0x%x  MAX_X: 0x%x  MAX_Y: 0x%x  HSYNC: 0x%x-0x%x  VSYNC: 0x%x-0x%x  Freq: %ffps\n",
304                       horiz_pix_total, vert_pix_total, max_visible_x, max_visible_y, hsync_on_pos, hsync_off_pos - 1, vsync_on_pos, vsync_off_pos - 1, 1 / ATTOSECONDS_TO_DOUBLE(refresh));
426305
427            if ( mc6845->screen != NULL )
428               mc6845->screen->configure(horiz_pix_total, vert_pix_total, visarea, refresh);
306         if ( m_screen != NULL )
307            m_screen->configure(horiz_pix_total, vert_pix_total, visarea, refresh);
429308
430            mc6845->has_valid_parameters = TRUE;
431         }
432         else
433            mc6845->has_valid_parameters = FALSE;
434
435         mc6845->horiz_pix_total = horiz_pix_total;
436         mc6845->vert_pix_total = vert_pix_total;
437         mc6845->max_visible_x = max_visible_x;
438         mc6845->max_visible_y = max_visible_y;
439         mc6845->hsync_on_pos = hsync_on_pos;
440         mc6845->hsync_off_pos = hsync_off_pos;
441         mc6845->vsync_on_pos = vsync_on_pos;
442         mc6845->vsync_off_pos = vsync_off_pos;
309         m_has_valid_parameters = true;
443310      }
311      else
312         m_has_valid_parameters = false;
313
314      m_horiz_pix_total = horiz_pix_total;
315      m_vert_pix_total = vert_pix_total;
316      m_max_visible_x = max_visible_x;
317      m_max_visible_y = max_visible_y;
318      m_hsync_on_pos = hsync_on_pos;
319      m_hsync_off_pos = hsync_off_pos;
320      m_vsync_on_pos = vsync_on_pos;
321      m_vsync_off_pos = vsync_off_pos;
444322   }
445323}
446324
447325
448INLINE void mc6845_update_counters(mc6845_t *mc6845)
326void mc6845_device::update_counters()
449327{
450   mc6845->character_counter = mc6845->line_timer ->elapsed( ).as_ticks( mc6845->clock );
328   m_character_counter = m_line_timer->elapsed( ).as_ticks( m_clock );
451329
452   if ( mc6845->hsync_off_timer ->enabled( ) )
330   if ( m_hsync_off_timer ->enabled( ) )
453331   {
454      mc6845->hsync_width_counter = mc6845->hsync_off_timer ->elapsed( ).as_ticks( mc6845->clock );
332      m_hsync_width_counter = m_hsync_off_timer ->elapsed( ).as_ticks( m_clock );
455333   }
456334}
457335
458336
459INLINE void mc6845_set_de(mc6845_t *mc6845, int state)
337void mc6845_device::set_de(int state)
460338{
461   if ( mc6845->de != state )
339   if ( m_de != state )
462340   {
463      mc6845->de = state;
341      m_de = state;
464342
465      if ( mc6845->de )
343      if ( m_de )
466344      {
467345         /* If the upd_adr_timer was running, cancel it */
468         mc6845->upd_adr_timer->adjust(attotime::never);
346         m_upd_adr_timer->adjust(attotime::never);
469347      }
470348      else
471349      {
472350         /* if transparent update was requested fire the update timer */
473         if(!mc6845->update_ready_bit)
474            update_upd_adr_timer(mc6845);
351         if(!m_update_ready_bit)
352            update_upd_adr_timer();
475353      }
476354
477      if ( !mc6845->out_de_func.isnull() )
478         mc6845->out_de_func(mc6845->de );
355      if ( !m_res_out_de_func.isnull() )
356         m_res_out_de_func( m_de );
479357   }
480358}
481359
482360
483INLINE void mc6845_set_hsync(mc6845_t *mc6845, int state)
361void mc6845_device::set_hsync(int state)
484362{
485   if ( mc6845->hsync != state )
363   if ( m_hsync != state )
486364   {
487      mc6845->hsync = state;
365      m_hsync = state;
488366
489      if ( !mc6845->out_hsync_func.isnull() )
490         mc6845->out_hsync_func(mc6845->hsync );
367      if ( !m_res_out_hsync_func.isnull() )
368         m_res_out_hsync_func( m_hsync );
491369   }
492370}
493371
494372
495INLINE void mc6845_set_vsync(mc6845_t *mc6845, int state)
373void mc6845_device::set_vsync(int state)
496374{
497   if ( mc6845->vsync != state )
375   if ( m_vsync != state )
498376   {
499      mc6845->vsync = state;
377      m_vsync = state;
500378
501      if ( !mc6845->out_vsync_func.isnull() )
502         mc6845->out_vsync_func(mc6845->vsync );
379      if ( !m_res_out_vsync_func.isnull() )
380         m_res_out_vsync_func( m_vsync );
503381   }
504382}
505383
506384
507INLINE void mc6845_set_cur(mc6845_t *mc6845, int state)
385void mc6845_device::set_cur(int state)
508386{
509   if ( mc6845->cur != state )
387   if ( m_cur != state )
510388   {
511      mc6845->cur = state;
389      m_cur = state;
512390
513      if ( !mc6845->out_cur_func.isnull() )
514         mc6845->out_cur_func(mc6845->cur );
391      if ( !m_res_out_cur_func.isnull() )
392         m_res_out_cur_func( m_cur );
515393   }
516394}
517395
518396
519static void update_upd_adr_timer(mc6845_t *mc6845)
397void mc6845_device::update_upd_adr_timer()
520398{
521   if (! mc6845->de && supports_transparent[mc6845->device_type])
522      mc6845->upd_adr_timer->adjust(mc6845->upd_time);
399   if (! m_de && m_supports_transparent)
400      m_upd_adr_timer->adjust(m_upd_time);
523401}
524402
525403
526static TIMER_CALLBACK( upd_adr_timer_cb )
404void mc6845_device::handle_line_timer()
527405{
528   device_t *device = (device_t *)ptr;
406   int new_vsync = m_vsync;
529407
530   /* fire a update address strobe */
531   call_on_update_address(device, 0);
532}
408   m_character_counter = 0;
409   m_cursor_x = -1;
533410
534
535static TIMER_CALLBACK( de_off_timer_cb )
536{
537   device_t *device = (device_t *)ptr;
538   mc6845_t *mc6845 = get_safe_token(device);
539
540   mc6845_set_de( mc6845, FALSE );
541}
542
543
544static TIMER_CALLBACK( cur_on_timer_cb )
545{
546   device_t *device = (device_t *)ptr;
547   mc6845_t *mc6845 = get_safe_token(device);
548
549   mc6845_set_cur( mc6845, TRUE );
550
551   /* Schedule CURSOR off signal */
552   mc6845->cur_off_timer->adjust( attotime::from_ticks( 1, mc6845->clock ) );
553}
554
555
556static TIMER_CALLBACK( cur_off_timer_cb )
557{
558   device_t *device = (device_t *)ptr;
559   mc6845_t *mc6845 = get_safe_token(device);
560
561   mc6845_set_cur( mc6845, FALSE );
562}
563
564
565static TIMER_CALLBACK( hsync_on_timer_cb )
566{
567   device_t *device = (device_t *)ptr;
568   mc6845_t *mc6845 = get_safe_token(device);
569   UINT8 hsync_width = ( mc6845->sync_width & 0x0f ) ? ( mc6845->sync_width & 0x0f ) : 0x10;
570
571   mc6845->hsync_width_counter = 0;
572   mc6845_set_hsync( mc6845, TRUE );
573
574   /* Schedule HSYNC off signal */
575   mc6845->hsync_off_timer->adjust( attotime::from_ticks( hsync_width, mc6845->clock ) );
576}
577
578
579static TIMER_CALLBACK( hsync_off_timer_cb )
580{
581   device_t *device = (device_t *)ptr;
582   mc6845_t *mc6845 = get_safe_token(device);
583
584   mc6845_set_hsync( mc6845, FALSE );
585}
586
587
588static TIMER_CALLBACK( line_timer_cb )
589{
590   device_t *device = (device_t *)ptr;
591   mc6845_t *mc6845 = get_safe_token(device);
592   int new_vsync = mc6845->vsync;
593
594   mc6845->character_counter = 0;
595   mc6845->cursor_x = -1;
596
597411   /* Check if VSYNC is active */
598   if ( mc6845->vsync_ff )
412   if ( m_vsync_ff )
599413   {
600      UINT8 vsync_width = supports_vert_sync_width[mc6845->device_type] ? (mc6845->sync_width >> 4) & 0x0f : 0;
414      UINT8 vsync_width = m_supports_vert_sync_width ? (m_sync_width >> 4) & 0x0f : 0;
601415
602      mc6845->vsync_width_counter = ( mc6845->vsync_width_counter + 1 ) & 0x0F;
416      m_vsync_width_counter = ( m_vsync_width_counter + 1 ) & 0x0F;
603417
604418      /* Check if we've reached end of VSYNC */
605      if ( mc6845->vsync_width_counter == vsync_width )
419      if ( m_vsync_width_counter == vsync_width )
606420      {
607         mc6845->vsync_ff = 0;
421         m_vsync_ff = 0;
608422
609423         new_vsync = FALSE;
610424      }
611425   }
612426
613   if ( mc6845->raster_counter == mc6845->max_ras_addr )
427   if ( m_raster_counter == m_max_ras_addr )
614428   {
615429      /* Check if we have reached the end of the vertical area */
616      if ( mc6845->line_counter == mc6845->vert_char_total )
430      if ( m_line_counter == m_vert_char_total )
617431      {
618         mc6845->adjust_counter = 0;
619         mc6845->adjust_active = 1;
432         m_adjust_counter = 0;
433         m_adjust_active = 1;
620434      }
621435
622      mc6845->raster_counter = 0;
623      mc6845->line_counter = ( mc6845->line_counter + 1 ) & 0x7F;
624      mc6845->line_address = ( mc6845->line_address + mc6845->horiz_disp ) & 0x3fff;
436      m_raster_counter = 0;
437      m_line_counter = ( m_line_counter + 1 ) & 0x7F;
438      m_line_address = ( m_line_address + m_horiz_disp ) & 0x3fff;
625439
626440      /* Check if we've reached the end of active display */
627      if ( mc6845->line_counter == mc6845->vert_disp )
441      if ( m_line_counter == m_vert_disp )
628442      {
629         mc6845->line_enable_ff = 0;
443         m_line_enable_ff = false;
630444      }
631445
632446      /* Check if VSYNC should be enabled */
633      if ( mc6845->line_counter == mc6845->vert_sync_pos )
447      if ( m_line_counter == m_vert_sync_pos )
634448      {
635         mc6845->vsync_width_counter = 0;
636         mc6845->vsync_ff = 1;
449         m_vsync_width_counter = 0;
450         m_vsync_ff = 1;
637451
638452         new_vsync = TRUE;
639453      }
640454   }
641455   else
642456   {
643      mc6845->raster_counter = ( mc6845->raster_counter + 1 ) & 0x1F;
457      m_raster_counter = ( m_raster_counter + 1 ) & 0x1F;
644458   }
645459
646   if ( mc6845->adjust_active )
460   if ( m_adjust_active )
647461   {
648462      /* Check if we have reached the end of a full cycle */
649      if ( mc6845->adjust_counter == mc6845->vert_total_adj )
463      if ( m_adjust_counter == m_vert_total_adj )
650464      {
651         mc6845->adjust_active = 0;
652         mc6845->raster_counter = 0;
653         mc6845->line_counter = 0;
654         mc6845->line_address = mc6845->disp_start_addr;
655         mc6845->line_enable_ff = 1;
465         m_adjust_active = 0;
466         m_raster_counter = 0;
467         m_line_counter = 0;
468         m_line_address = m_disp_start_addr;
469         m_line_enable_ff = true;
656470         /* also update the cursor state now */
657         update_cursor_state(mc6845);
471         update_cursor_state();
658472
659         if (mc6845->screen != NULL)
660            mc6845->screen->reset_origin();
473         if (m_screen != NULL)
474            m_screen->reset_origin();
661475      }
662476      else
663477      {
664         mc6845->adjust_counter = ( mc6845->adjust_counter + 1 ) & 0x1F;
478         m_adjust_counter = ( m_adjust_counter + 1 ) & 0x1F;
665479      }
666480   }
667481
668   if ( mc6845->line_enable_ff )
482   if ( m_line_enable_ff )
669483   {
670484      /* Schedule DE off signal change */
671      mc6845->de_off_timer->adjust(attotime::from_ticks( mc6845->horiz_disp, mc6845->clock ));
485      m_de_off_timer->adjust(attotime::from_ticks( m_horiz_disp, m_clock ));
672486
673487      /* Is cursor visible on this line? */
674      if ( mc6845->cursor_state &&
675         (mc6845->raster_counter >= (mc6845->cursor_start_ras & 0x1f)) &&
676         (mc6845->raster_counter <= mc6845->cursor_end_ras) &&
677         (mc6845->cursor_addr >= mc6845->line_address) &&
678         (mc6845->cursor_addr < (mc6845->line_address + mc6845->horiz_disp)) )
488      if ( m_cursor_state &&
489         (m_raster_counter >= (m_cursor_start_ras & 0x1f)) &&
490         (m_raster_counter <= m_cursor_end_ras) &&
491         (m_cursor_addr >= m_line_address) &&
492         (m_cursor_addr < (m_line_address + m_horiz_disp)) )
679493      {
680         mc6845->cursor_x = mc6845->cursor_addr - mc6845->line_address;
494         m_cursor_x = m_cursor_addr - m_line_address;
681495
682496         /* Schedule CURSOR ON signal */
683         mc6845->cur_on_timer->adjust( attotime::from_ticks( mc6845->cursor_x, mc6845->clock ) );
497         m_cur_on_timer->adjust( attotime::from_ticks( m_cursor_x, m_clock ) );
684498      }
685499   }
686500
687501   /* Schedule HSYNC on signal */
688   mc6845->hsync_on_timer->adjust( attotime::from_ticks( mc6845->horiz_sync_pos, mc6845->clock ) );
502   m_hsync_on_timer->adjust( attotime::from_ticks( m_horiz_sync_pos, m_clock ) );
689503
690504   /* Schedule our next callback */
691   mc6845->line_timer->adjust( attotime::from_ticks( mc6845->horiz_char_total + 1, mc6845->clock ) );
505   m_line_timer->adjust( attotime::from_ticks( m_horiz_char_total + 1, m_clock ) );
692506
693507   /* Set VSYNC and DE signals */
694   mc6845_set_vsync( mc6845, new_vsync );
695   mc6845_set_de( mc6845, mc6845->line_enable_ff ? TRUE : FALSE );
508   set_vsync( new_vsync );
509   set_de( m_line_enable_ff ? TRUE : FALSE );
696510}
697511
698512
699UINT16 mc6845_get_ma(device_t *device)
513void mc6845_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
700514{
701   mc6845_t *mc6845 = get_safe_token(device);
515   switch (id)
516   {
517   case TIMER_LINE:
518      handle_line_timer();
519      break;
702520
703   mc6845_update_counters( mc6845 );
521   case TIMER_DE_OFF:
522      set_de( FALSE );
523      break;
704524
705   return ( mc6845->line_address + mc6845->character_counter ) & 0x3fff;
706}
525   case TIMER_CUR_ON:
526      set_cur( TRUE );
707527
528      /* Schedule CURSOR off signal */
529      m_cur_off_timer->adjust( attotime::from_ticks( 1, m_clock ) );
530      break;
708531
709UINT8 mc6845_get_ra(device_t *device)
710{
711   mc6845_t *mc6845 = get_safe_token(device);
532   case TIMER_CUR_OFF:
533      set_cur( FALSE );
534      break;
712535
713   return mc6845->raster_counter;
536   case TIMER_HSYNC_ON:
537      {
538         UINT8 hsync_width = ( m_sync_width & 0x0f ) ? ( m_sync_width & 0x0f ) : 0x10;
539
540         m_hsync_width_counter = 0;
541         set_hsync( TRUE );
542
543         /* Schedule HSYNC off signal */
544         m_hsync_off_timer->adjust( attotime::from_ticks( hsync_width, m_clock ) );
545      }
546      break;
547
548   case TIMER_HSYNC_OFF:
549      set_hsync( FALSE );
550      break;
551
552   case TIMER_LIGHT_PEN_LATCH:
553      m_light_pen_addr = get_ma();
554      m_light_pen_latched = true;
555      break;
556
557   case TIMER_UPD_ADR:
558      /* fire a update address strobe */
559      call_on_update_address(0);
560      break;
561
562   case TIMER_UPD_TRANS:
563      {
564         int addr = (param >> 8);
565         int strobe = (param & 0xff);
566
567         /* call the callback function -- we know it exists */
568         m_on_update_addr_changed(this, addr, strobe);
569
570         if(!m_update_ready_bit && MODE_TRANSPARENT_BLANK)
571         {
572            m_update_addr++;
573            m_update_addr &= 0x3fff;
574            m_update_ready_bit = true;
575         }
576      }
577      break;
578
579   }
714580}
715581
716582
717static TIMER_CALLBACK( light_pen_latch_timer_cb )
583UINT16 mc6845_device::get_ma()
718584{
719   device_t *device = (device_t *)ptr;
720   mc6845_t *mc6845 = get_safe_token(device);
585   update_counters();
721586
722   mc6845->light_pen_addr = mc6845_get_ma(device);
723   mc6845->light_pen_latched = TRUE;
587   return ( m_line_address + m_character_counter ) & 0x3fff;
724588}
725589
726590
727void mc6845_assert_light_pen_input(device_t *device)
591UINT8 mc6845_device::get_ra()
728592{
729   mc6845_t *mc6845 = get_safe_token(device);
593   return m_raster_counter;
594}
730595
596
597void mc6845_device::assert_light_pen_input()
598{
731599   /* compute the pixel coordinate of the NEXT character -- this is when the light pen latches */
732600   /* set the timer that will latch the display address into the light pen registers */
733   mc6845->light_pen_latch_timer->adjust(attotime::from_ticks( 1, mc6845->clock ));
601   m_light_pen_latch_timer->adjust(attotime::from_ticks( 1, m_clock ));
734602}
735603
736604
737void mc6845_set_clock(device_t *device, int clock)
605void mc6845_device::set_clock(int clock)
738606{
739   mc6845_t *mc6845 = get_safe_token(device);
740
741607   /* validate arguments */
742608   assert(clock > 0);
743609
744   if (clock != mc6845->clock)
610   if (clock != m_clock)
745611   {
746      mc6845->clock = clock;
747      recompute_parameters(mc6845, TRUE);
612      m_clock = clock;
613      recompute_parameters(true);
748614   }
749615}
750616
751617
752void mc6845_set_hpixels_per_column(device_t *device, int hpixels_per_column)
618void mc6845_device::set_hpixels_per_column(int hpixels_per_column)
753619{
754   mc6845_t *mc6845 = get_safe_token(device);
755
756620   /* validate arguments */
757621   assert(hpixels_per_column > 0);
758622
759   if (hpixels_per_column != mc6845->hpixels_per_column)
623   if (hpixels_per_column != m_hpixels_per_column)
760624   {
761      mc6845->hpixels_per_column = hpixels_per_column;
762      recompute_parameters(mc6845, FALSE);
625      m_hpixels_per_column = hpixels_per_column;
626      recompute_parameters(false);
763627   }
764628}
765629
766630
767static void update_cursor_state(mc6845_t *mc6845)
631void mc6845_device::update_cursor_state()
768632{
769633   /* save and increment cursor counter */
770   UINT8 last_cursor_blink_count = mc6845->cursor_blink_count;
771   mc6845->cursor_blink_count = mc6845->cursor_blink_count + 1;
634   UINT8 last_cursor_blink_count = m_cursor_blink_count;
635   m_cursor_blink_count = m_cursor_blink_count + 1;
772636
773637   /* switch on cursor blinking mode */
774   switch (mc6845->cursor_start_ras & 0x60)
638   switch (m_cursor_start_ras & 0x60)
775639   {
776640      /* always on */
777      case 0x00: mc6845->cursor_state = TRUE; break;
641      case 0x00: m_cursor_state = true; break;
778642
779643      /* always off */
780644      default:
781      case 0x20: mc6845->cursor_state = FALSE; break;
645      case 0x20: m_cursor_state = false; break;
782646
783647      /* fast blink */
784648      case 0x40:
785         if ((last_cursor_blink_count & 0x10) != (mc6845->cursor_blink_count & 0x10))
786            mc6845->cursor_state = !mc6845->cursor_state;
649         if ((last_cursor_blink_count & 0x10) != (m_cursor_blink_count & 0x10))
650            m_cursor_state = !m_cursor_state;
787651         break;
788652
789653      /* slow blink */
790654      case 0x60:
791         if ((last_cursor_blink_count & 0x20) != (mc6845->cursor_blink_count & 0x20))
792            mc6845->cursor_state = !mc6845->cursor_state;
655         if ((last_cursor_blink_count & 0x20) != (m_cursor_blink_count & 0x20))
656            m_cursor_state = !m_cursor_state;
793657         break;
794658   }
795659}
796660
797661
798void mc6845_update(device_t *device, bitmap_t *bitmap, const rectangle *cliprect)
662void mc6845_device::update(bitmap_t *bitmap, const rectangle *cliprect)
799663{
800   mc6845_t *mc6845 = get_safe_token(device);
801664   assert(bitmap != NULL);
802665   assert(cliprect != NULL);
803666
804   if (mc6845->has_valid_parameters)
667   if (m_has_valid_parameters)
805668   {
806669      UINT16 y;
807670
808671      void *param = NULL;
809672
810      assert(mc6845->intf != NULL);
811      assert(mc6845->intf->update_row != NULL);
673      assert(m_update_row != NULL);
812674
813675      /* call the set up function if any */
814      if (mc6845->intf->begin_update != NULL)
815         param = mc6845->intf->begin_update(device, bitmap, cliprect);
676      if (m_begin_update != NULL)
677         param = m_begin_update(this, bitmap, cliprect);
816678
817679      if (cliprect->min_y == 0)
818680      {
819681         /* read the start address at the beginning of the frame */
820         mc6845->current_disp_addr = mc6845->disp_start_addr;
682         m_current_disp_addr = m_disp_start_addr;
821683      }
822684
823685      /* for each row in the visible region */
824686      for (y = cliprect->min_y; y <= cliprect->max_y; y++)
825687      {
826688         /* compute the current raster line */
827         UINT8 ra = y % (mc6845->max_ras_addr + 1);
689         UINT8 ra = y % (m_max_ras_addr + 1);
828690
829691         /* check if the cursor is visible and is on this scanline */
830         int cursor_visible = mc6845->cursor_state &&
831                        (ra >= (mc6845->cursor_start_ras & 0x1f)) &&
832                        (ra <= mc6845->cursor_end_ras) &&
833                        (mc6845->cursor_addr >= mc6845->current_disp_addr) &&
834                        (mc6845->cursor_addr < (mc6845->current_disp_addr + mc6845->horiz_disp));
692         int cursor_visible = m_cursor_state &&
693                        (ra >= (m_cursor_start_ras & 0x1f)) &&
694                        (ra <= m_cursor_end_ras) &&
695                        (m_cursor_addr >= m_current_disp_addr) &&
696                        (m_cursor_addr < (m_current_disp_addr + m_horiz_disp));
835697
836698         /* compute the cursor X position, or -1 if not visible */
837         INT8 cursor_x = cursor_visible ? (mc6845->cursor_addr - mc6845->current_disp_addr) : -1;
699         INT8 cursor_x = cursor_visible ? (m_cursor_addr - m_current_disp_addr) : -1;
838700
839701         /* call the external system to draw it */
840         if (MODE_ROW_COLUMN_ADDRESSING(mc6845))
702         if (MODE_ROW_COLUMN_ADDRESSING)
841703         {
842704            UINT8 cc = 0;
843            UINT8 cr = y / (mc6845->max_ras_addr + 1);
705            UINT8 cr = y / (m_max_ras_addr + 1);
844706            UINT16 ma = (cr << 8) | cc;
845707
846            mc6845->intf->update_row(device, bitmap, cliprect, ma, ra, y, mc6845->horiz_disp, cursor_x, param);
708            m_update_row(this, bitmap, cliprect, ma, ra, y, m_horiz_disp, cursor_x, param);
847709         }
848710         else
849711         {
850            mc6845->intf->update_row(device, bitmap, cliprect, mc6845->current_disp_addr, ra, y, mc6845->horiz_disp, cursor_x, param);
712            m_update_row(this, bitmap, cliprect, m_current_disp_addr, ra, y, m_horiz_disp, cursor_x, param);
851713         }
852714
853715         /* update MA if the last raster address */
854         if (ra == mc6845->max_ras_addr)
855            mc6845->current_disp_addr = (mc6845->current_disp_addr + mc6845->horiz_disp) & 0x3fff;
716         if (ra == m_max_ras_addr)
717            m_current_disp_addr = (m_current_disp_addr + m_horiz_disp) & 0x3fff;
856718      }
857719
858720      /* call the tear down function if any */
859      if (mc6845->intf->end_update != NULL)
860         mc6845->intf->end_update(device, bitmap, cliprect, param);
721      if (m_end_update != NULL)
722         m_end_update(this, bitmap, cliprect, param);
861723   }
862724   else
863725      popmessage("Invalid MC6845 screen parameters - display disabled!!!");
864726}
865727
866728
867/* device interface */
868static void common_start(device_t *device, int device_type)
729void mc6845_device::device_start()
869730{
870   mc6845_t *mc6845 = get_safe_token(device);
731   assert(m_clock > 0);
732   assert(m_hpixels_per_column > 0);
871733
872   /* validate arguments */
873   assert(device != NULL);
734   /* resolve callbacks */
735   m_res_out_de_func.resolve(m_out_de_func, *this);
736   m_res_out_cur_func.resolve(m_out_cur_func, *this);
737   m_res_out_hsync_func.resolve(m_out_hsync_func, *this);
738   m_res_out_vsync_func.resolve(m_out_vsync_func, *this);
874739
875   mc6845->intf = (const mc6845_interface *)device->static_config();
876   mc6845->device_type = device_type;
877
878   if (mc6845->intf != NULL)
740   /* get the screen device */
741   if ( m_screen_tag != NULL )
879742   {
880      assert(device->clock() > 0);
881      assert(mc6845->intf->hpixels_per_column > 0);
882
883      /* resolve callbacks */
884      mc6845->out_de_func.resolve(mc6845->intf->out_de_func, *device);
885      mc6845->out_cur_func.resolve(mc6845->intf->out_cur_func, *device);
886      mc6845->out_hsync_func.resolve(mc6845->intf->out_hsync_func, *device);
887      mc6845->out_vsync_func.resolve(mc6845->intf->out_vsync_func, *device);
888
889      /* copy the initial parameters */
890      mc6845->clock = device->clock();
891      mc6845->hpixels_per_column = mc6845->intf->hpixels_per_column;
892
893      /* get the screen device */
894      if ( mc6845->intf->screen_tag != NULL )
895      {
896         mc6845->screen = downcast<screen_device *>(device->machine().device(mc6845->intf->screen_tag));
897         if (mc6845->screen == NULL) {
898            astring tempstring;
899            mc6845->screen = downcast<screen_device *>(device->machine().device(device->owner()->subtag(tempstring,mc6845->intf->screen_tag)));
900         }
901         assert(mc6845->screen != NULL);
743      m_screen = downcast<screen_device *>(machine().device(m_screen_tag));
744      if (m_screen == NULL) {
745         astring tempstring;
746         m_screen = downcast<screen_device *>(machine().device(owner()->subtag(tempstring,m_screen_tag)));
902747      }
903      else
904         mc6845->screen = NULL;
905
906      /* create the timers */
907      mc6845->line_timer = device->machine().scheduler().timer_alloc(FUNC(line_timer_cb), (void *)device);
908
909      mc6845->de_off_timer = device->machine().scheduler().timer_alloc(FUNC(de_off_timer_cb), (void *)device);
910      mc6845->upd_adr_timer = device->machine().scheduler().timer_alloc(FUNC(upd_adr_timer_cb), (void *)device);
911
912      mc6845->cur_on_timer = device->machine().scheduler().timer_alloc(FUNC(cur_on_timer_cb), (void *)device);
913      mc6845->cur_off_timer = device->machine().scheduler().timer_alloc(FUNC(cur_off_timer_cb), (void *)device);
914
915      mc6845->hsync_on_timer = device->machine().scheduler().timer_alloc(FUNC(hsync_on_timer_cb), (void *)device);
916      mc6845->hsync_off_timer = device->machine().scheduler().timer_alloc(FUNC(hsync_off_timer_cb), (void *)device);
748      assert(m_screen != NULL);
917749   }
750   else
751      m_screen = NULL;
918752
919   mc6845->light_pen_latch_timer = device->machine().scheduler().timer_alloc(FUNC(light_pen_latch_timer_cb), (void *)device);
753   /* create the timers */
754   m_line_timer = timer_alloc(TIMER_LINE);
755   m_de_off_timer = timer_alloc(TIMER_DE_OFF);
756   m_cur_on_timer = timer_alloc(TIMER_CUR_ON);
757   m_cur_off_timer = timer_alloc(TIMER_CUR_OFF);
758   m_hsync_on_timer = timer_alloc(TIMER_HSYNC_ON);
759   m_hsync_off_timer = timer_alloc(TIMER_HSYNC_OFF);
760   m_light_pen_latch_timer = timer_alloc(TIMER_LIGHT_PEN_LATCH);
761   m_upd_adr_timer = timer_alloc(TIMER_UPD_ADR);
762   m_upd_trans_timer = timer_alloc(TIMER_UPD_TRANS);
920763
921764   /* Use some large startup values */
922   mc6845->horiz_char_total = 0xff;
923   mc6845->max_ras_addr = 0x1f;
924   mc6845->vert_char_total = 0x7f;
765   m_horiz_char_total = 0xff;
766   m_max_ras_addr = 0x1f;
767   m_vert_char_total = 0x7f;
925768
926   /* register for state saving */
927   device->machine().save().register_postload(save_prepost_delegate(FUNC(mc6845_state_save_postload), mc6845));
928
929   device->save_item(NAME(mc6845->clock));
930   device->save_item(NAME(mc6845->hpixels_per_column));
931   device->save_item(NAME(mc6845->register_address_latch));
932   device->save_item(NAME(mc6845->horiz_char_total));
933   device->save_item(NAME(mc6845->horiz_disp));
934   device->save_item(NAME(mc6845->horiz_sync_pos));
935   device->save_item(NAME(mc6845->sync_width));
936   device->save_item(NAME(mc6845->vert_char_total));
937   device->save_item(NAME(mc6845->vert_total_adj));
938   device->save_item(NAME(mc6845->vert_disp));
939   device->save_item(NAME(mc6845->vert_sync_pos));
940   device->save_item(NAME(mc6845->mode_control));
941   device->save_item(NAME(mc6845->max_ras_addr));
942   device->save_item(NAME(mc6845->cursor_start_ras));
943   device->save_item(NAME(mc6845->cursor_end_ras));
944   device->save_item(NAME(mc6845->disp_start_addr));
945   device->save_item(NAME(mc6845->cursor_addr));
946   device->save_item(NAME(mc6845->light_pen_addr));
947   device->save_item(NAME(mc6845->light_pen_latched));
948   device->save_item(NAME(mc6845->cursor_state));
949   device->save_item(NAME(mc6845->cursor_blink_count));
950   device->save_item(NAME(mc6845->update_addr));
951   device->save_item(NAME(mc6845->update_ready_bit));
952   device->save_item(NAME(mc6845->cur));
953   device->save_item(NAME(mc6845->hsync));
954   device->save_item(NAME(mc6845->vsync));
955   device->save_item(NAME(mc6845->de));
956   device->save_item(NAME(mc6845->character_counter));
957   device->save_item(NAME(mc6845->hsync_width_counter));
958   device->save_item(NAME(mc6845->line_counter));
959   device->save_item(NAME(mc6845->raster_counter));
960   device->save_item(NAME(mc6845->adjust_counter));
961   device->save_item(NAME(mc6845->vsync_width_counter));
962   device->save_item(NAME(mc6845->line_enable_ff));
963   device->save_item(NAME(mc6845->vsync_ff));
964   device->save_item(NAME(mc6845->adjust_active));
965   device->save_item(NAME(mc6845->line_address));
966   device->save_item(NAME(mc6845->cursor_x));
769   m_supports_disp_start_addr_r = true;
770   m_supports_vert_sync_width = false;
771   m_supports_status_reg_d5 = false;
772   m_supports_status_reg_d6 = false;
773   m_supports_status_reg_d7 = false;
774   m_supports_transparent = false;
775   
776   save_item(NAME(m_hpixels_per_column));
777   save_item(NAME(m_register_address_latch));
778   save_item(NAME(m_horiz_char_total));
779   save_item(NAME(m_horiz_disp));
780   save_item(NAME(m_horiz_sync_pos));
781   save_item(NAME(m_sync_width));
782   save_item(NAME(m_vert_char_total));
783   save_item(NAME(m_vert_total_adj));
784   save_item(NAME(m_vert_disp));
785   save_item(NAME(m_vert_sync_pos));
786   save_item(NAME(m_mode_control));
787   save_item(NAME(m_max_ras_addr));
788   save_item(NAME(m_cursor_start_ras));
789   save_item(NAME(m_cursor_end_ras));
790   save_item(NAME(m_disp_start_addr));
791   save_item(NAME(m_cursor_addr));
792   save_item(NAME(m_light_pen_addr));
793   save_item(NAME(m_light_pen_latched));
794   save_item(NAME(m_cursor_state));
795   save_item(NAME(m_cursor_blink_count));
796   save_item(NAME(m_update_addr));
797   save_item(NAME(m_update_ready_bit));
798   save_item(NAME(m_cur));
799   save_item(NAME(m_hsync));
800   save_item(NAME(m_vsync));
801   save_item(NAME(m_de));
802   save_item(NAME(m_character_counter));
803   save_item(NAME(m_hsync_width_counter));
804   save_item(NAME(m_line_counter));
805   save_item(NAME(m_raster_counter));
806   save_item(NAME(m_adjust_counter));
807   save_item(NAME(m_vsync_width_counter));
808   save_item(NAME(m_line_enable_ff));
809   save_item(NAME(m_vsync_ff));
810   save_item(NAME(m_adjust_active));
811   save_item(NAME(m_line_address));
812   save_item(NAME(m_cursor_x));
967813}
968814
969static DEVICE_START( mc6845 )
970{
971   common_start(device, TYPE_MC6845);
972}
973815
974static DEVICE_START( mc6845_1 )
816void mc6845_1_device::device_start()
975817{
976   common_start(device, TYPE_MC6845_1);
818   mc6845_device::device_start();
819
820   m_supports_disp_start_addr_r = true;
821   m_supports_vert_sync_width = true;
822   m_supports_status_reg_d5 = false;
823   m_supports_status_reg_d6 = false;
824   m_supports_status_reg_d7 = false;
825   m_supports_transparent = false;
977826}
978827
979static DEVICE_START( c6545_1 )
828
829void c6545_1_device::device_start()
980830{
981   common_start(device, TYPE_C6545_1);
831   mc6845_device::device_start();
832
833   m_supports_disp_start_addr_r = false;
834   m_supports_vert_sync_width = true;
835   m_supports_status_reg_d5 = true;
836   m_supports_status_reg_d6 = true;
837   m_supports_status_reg_d7 = false;
838   m_supports_transparent = false;
982839}
983840
984static DEVICE_START( r6545_1 )
841
842void r6545_1_device::device_start()
985843{
986   common_start(device, TYPE_R6545_1);
844   mc6845_device::device_start();
845
846   m_supports_disp_start_addr_r = false;
847   m_supports_vert_sync_width = true;
848   m_supports_status_reg_d5 = true;
849   m_supports_status_reg_d6 = true;
850   m_supports_status_reg_d7 = true;
851   m_supports_transparent = true;
987852}
988853
989static DEVICE_START( h46505 )
854
855void h46505_device::device_start()
990856{
991   common_start(device, TYPE_H46505);
857   mc6845_device::device_start();
858
859   m_supports_disp_start_addr_r = false;
860   m_supports_vert_sync_width = false;
861   m_supports_status_reg_d5 = false;
862   m_supports_status_reg_d6 = false;
863   m_supports_status_reg_d7 = false;
864   m_supports_transparent = false;
992865}
993866
994static DEVICE_START( hd6845 )
867
868void hd6845_device::device_start()
995869{
996   common_start(device, TYPE_HD6845);
870   mc6845_device::device_start();
871
872   m_supports_disp_start_addr_r = false;
873   m_supports_vert_sync_width = true;
874   m_supports_status_reg_d5 = false;
875   m_supports_status_reg_d6 = false;
876   m_supports_status_reg_d7 = false;
877   m_supports_transparent = false;
997878}
998879
999static DEVICE_START( sy6545_1 )
880
881void sy6545_1_device::device_start()
1000882{
1001   common_start(device, TYPE_SY6545_1);
883   mc6845_device::device_start();
884
885   m_supports_disp_start_addr_r = false;
886   m_supports_vert_sync_width = true;
887   m_supports_status_reg_d5 = true;
888   m_supports_status_reg_d6 = true;
889   m_supports_status_reg_d7 = true;
890   m_supports_transparent = true;
1002891}
1003892
1004static DEVICE_START( sy6845e )
893
894void sy6845e_device::device_start()
1005895{
1006   common_start(device, TYPE_SY6845E);
896   mc6845_device::device_start();
897
898   m_supports_disp_start_addr_r = false;
899   m_supports_vert_sync_width = true;
900   m_supports_status_reg_d5 = true;
901   m_supports_status_reg_d6 = true;
902   m_supports_status_reg_d7 = true;
903   m_supports_transparent = true;
1007904}
1008905
1009906
1010static DEVICE_RESET( mc6845 )
907void mc6845_device::device_reset()
1011908{
1012   mc6845_t *mc6845 = get_safe_token(device);
1013
1014909   /* internal registers other than status remain unchanged, all outputs go low */
1015   if (mc6845->intf != NULL)
1016   {
1017      if (!mc6845->out_de_func.isnull())
1018         mc6845->out_de_func(FALSE);
910   if ( !m_res_out_de_func.isnull() )
911      m_res_out_de_func( FALSE );
1019912
1020      if (!mc6845->out_hsync_func.isnull())
1021         mc6845->out_hsync_func(FALSE);
913   if ( !m_res_out_hsync_func.isnull() )
914      m_res_out_hsync_func( FALSE );
1022915
1023      if (!mc6845->out_vsync_func.isnull())
1024         mc6845->out_vsync_func(FALSE);
1025   }
916   if ( !m_res_out_vsync_func.isnull() )
917      m_res_out_vsync_func( FALSE );
1026918
1027   if ( ! mc6845->line_timer ->enabled( ) )
919   if ( ! m_line_timer->enabled( ) )
1028920   {
1029      mc6845->line_timer->adjust( attotime::from_ticks( mc6845->horiz_char_total + 1, mc6845->clock ) );
921      m_line_timer->adjust( attotime::from_ticks( m_horiz_char_total + 1, m_clock ) );
1030922   }
1031923
1032   mc6845->light_pen_latched = FALSE;
924   m_light_pen_latched = false;
1033925}
1034926
1035#if 0
1036static DEVICE_VALIDITY_CHECK( mc6845 )
1037{
1038   int error = FALSE;
1039   const mc6845_interface *intf = (const mc6845_interface *) device->static_config();
1040927
1041   if (intf != NULL && intf->screen_tag != NULL)
1042   {
1043      if (device->clock() <= 0)
1044      {
1045         mame_printf_error("%s: %s has an mc6845 with an invalid clock\n", driver->source_file, driver->name);
1046         error = TRUE;
1047      }
928void r6545_1_device::device_reset() { mc6845_device::device_reset(); }
929void h46505_device::device_reset() { mc6845_device::device_reset(); }
930void mc6845_1_device::device_reset() { mc6845_device::device_reset(); }
931void hd6845_device::device_reset() { mc6845_device::device_reset(); }
932void c6545_1_device::device_reset() { mc6845_device::device_reset(); }
933void sy6545_1_device::device_reset() { mc6845_device::device_reset(); }
934void sy6845e_device::device_reset() { mc6845_device::device_reset(); }
1048935
1049      if (intf->hpixels_per_column <= 0)
1050      {
1051         mame_printf_error("%s: %s has an mc6845 with an invalid hpixels_per_column\n", driver->source_file, driver->name);
1052         error = TRUE;
1053      }
1054   }
1055936
1056   return error;
937r6545_1_device::r6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
938   : mc6845_device(mconfig, R6545_1, "r6545_1", tag, owner, clock)
939{
1057940}
1058#endif
1059941
1060DEVICE_GET_INFO( mc6845 )
942
943h46505_device::h46505_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
944   : mc6845_device(mconfig, H46505, "h46505", tag, owner, clock)
1061945{
1062   switch (state)
1063   {
1064      /* --- the following bits of info are returned as 64-bit signed integers --- */
1065      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(mc6845_t);               break;
1066      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
946}
1067947
1068      /* --- the following bits of info are returned as pointers to functions --- */
1069      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(mc6845);   break;
1070      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
1071      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(mc6845);   break;
1072//      case DEVINFO_FCT_VALIDITY_CHECK:                info->validity_check = DEVICE_VALIDITY_CHECK_NAME(mc6845); break;
1073948
1074      /* --- the following bits of info are returned as NULL-terminated strings --- */
1075      case DEVINFO_STR_NAME:                     strcpy(info->s, "Motorola 6845");         break;
1076      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "MC6845 CRTC");            break;
1077      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.61");               break;
1078      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
1079      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
1080   }
949mc6845_1_device::mc6845_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
950   : mc6845_device(mconfig, MC6845_1, "mc6845_1", tag, owner, clock)
951{
1081952}
1082953
1083954
1084DEVICE_GET_INFO( mc6845_1 )
955hd6845_device::hd6845_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
956   : mc6845_device(mconfig, HD6845, "hd6845", tag, owner, clock)
1085957{
1086   switch (state)
1087   {
1088      /* --- the following bits of info are returned as NULL-terminated strings --- */
1089      case DEVINFO_STR_NAME:                     strcpy(info->s, "Motorola 6845-1");         break;
958}
1090959
1091      /* --- the following bits of info are returned as pointers to data or functions --- */
1092      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(mc6845_1);   break;
1093960
1094      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1095   }
961c6545_1_device::c6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
962   : mc6845_device(mconfig, C6545_1, "c6545_1", tag, owner, clock)
963{
1096964}
1097965
1098966
1099DEVICE_GET_INFO( c6545_1 )
967sy6545_1_device::sy6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
968   : mc6845_device(mconfig, SY6545_1, "sy6545_1", tag, owner, clock)
1100969{
1101   switch (state)
1102   {
1103      /* --- the following bits of info are returned as NULL-terminated strings --- */
1104      case DEVINFO_STR_NAME:                     strcpy(info->s, "MOS Technology 6545-1");   break;
970}
1105971
1106      /* --- the following bits of info are returned as pointers to data or functions --- */
1107      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(c6545_1);   break;
1108972
1109      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1110   }
973sy6845e_device::sy6845e_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
974   : mc6845_device(mconfig, SY6845E, "sy6845e", tag, owner, clock)
975{
1111976}
1112977
1113978
1114DEVICE_GET_INFO( r6545_1 )
979/*****************************************
980 * TRAMPOLINES
981 *****************************************/
982
983WRITE8_DEVICE_HANDLER( mc6845_address_w )
1115984{
1116   switch (state)
1117   {
1118      /* --- the following bits of info are returned as NULL-terminated strings --- */
1119      case DEVINFO_STR_NAME:                     strcpy(info->s, "Rockwell 6545-1");         break;
985   downcast<mc6845_device *>(device)->address_w( data );
986}
1120987
1121      /* --- the following bits of info are returned as pointers to data or functions --- */
1122      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(r6545_1);   break;
1123
1124      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1125   }
988READ8_DEVICE_HANDLER( mc6845_status_r )
989{
990   return downcast<mc6845_device *>(device)->status_r();
1126991}
1127992
1128
1129DEVICE_GET_INFO( h46505 )
993READ8_DEVICE_HANDLER( mc6845_register_r )
1130994{
1131   switch (state)
1132   {
1133      /* --- the following bits of info are returned as NULL-terminated strings --- */
1134      case DEVINFO_STR_NAME:                     strcpy(info->s, "Hitachi 46505");         break;
995   return downcast<mc6845_device *>(device)->register_r();
996}
1135997
1136      /* --- the following bits of info are returned as pointers to data or functions --- */
1137      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(h46505);   break;
1138
1139      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1140   }
998WRITE8_DEVICE_HANDLER( mc6845_register_w )
999{
1000   downcast<mc6845_device *>(device)->register_w( data );
11411001}
11421002
1143
1144DEVICE_GET_INFO( hd6845 )
1003UINT16 mc6845_get_ma(device_t *device)
11451004{
1146   switch (state)
1147   {
1148      /* --- the following bits of info are returned as NULL-terminated strings --- */
1149      case DEVINFO_STR_NAME:                     strcpy(info->s, "Hitachi 6845");         break;
1005   return downcast<mc6845_device *>(device)->get_ma();
1006}
11501007
1151      /* --- the following bits of info are returned as pointers to data or functions --- */
1152      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(hd6845);   break;
1153
1154      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1155   }
1008UINT8 mc6845_get_ra(device_t *device)
1009{
1010   return downcast<mc6845_device *>(device)->get_ra();
11561011}
11571012
1158
1159DEVICE_GET_INFO( sy6545_1 )
1013void mc6845_assert_light_pen_input(device_t *device)
11601014{
1161   switch (state)
1162   {
1163      /* --- the following bits of info are returned as NULL-terminated strings --- */
1164      case DEVINFO_STR_NAME:                     strcpy(info->s, "Synertek 6545-1");         break;
1015   downcast<mc6845_device *>(device)->assert_light_pen_input();
1016}
11651017
1166      /* --- the following bits of info are returned as pointers to data or functions --- */
1167      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(sy6545_1);   break;
1168
1169      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1170   }
1018void mc6845_set_clock(device_t *device, int clock)
1019{
1020   downcast<mc6845_device *>(device)->set_clock( clock );
11711021}
11721022
1173
1174DEVICE_GET_INFO( sy6845e )
1023void mc6845_set_hpixels_per_column(device_t *device, int hpixels_per_column)
11751024{
1176   switch (state)
1177   {
1178      /* --- the following bits of info are returned as NULL-terminated strings --- */
1179      case DEVINFO_STR_NAME:                     strcpy(info->s, "Synertek 6845E");         break;
1025   downcast<mc6845_device *>(device)->set_hpixels_per_column( hpixels_per_column );
1026}
11801027
1181      /* --- the following bits of info are returned as pointers to data or functions --- */
1182      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(sy6845e);   break;
1183
1184      default:                              DEVICE_GET_INFO_CALL(mc6845);            break;
1185   }
1028void mc6845_update(device_t *device, bitmap_t *bitmap, const rectangle *cliprect)
1029{
1030   downcast<mc6845_device *>(device)->update( bitmap, cliprect );
11861031}
11871032
1188DEFINE_LEGACY_DEVICE(MC6845, mc6845);
1189DEFINE_LEGACY_DEVICE(MC6845_1, mc6845_1);
1190DEFINE_LEGACY_DEVICE(R6545_1, r6545_1);
1191DEFINE_LEGACY_DEVICE(C6545_1, c6545_1);
1192DEFINE_LEGACY_DEVICE(H46505, h46505);
1193DEFINE_LEGACY_DEVICE(HD6845, hd6845);
1194DEFINE_LEGACY_DEVICE(SY6545_1, sy6545_1);
1195DEFINE_LEGACY_DEVICE(SY6845E, sy6845e);
1033
src/emu/video/mc6845.h
r11542r11543
1010#ifndef __MC6845__
1111#define __MC6845__
1212
13#include "devlegcy.h"
13#include "emu.h"
14#include "machine/devhelpr.h"
1415
15DECLARE_LEGACY_DEVICE(MC6845, mc6845);
16DECLARE_LEGACY_DEVICE(MC6845_1, mc6845_1);
17DECLARE_LEGACY_DEVICE(R6545_1, r6545_1);
18DECLARE_LEGACY_DEVICE(C6545_1, c6545_1);
19DECLARE_LEGACY_DEVICE(H46505, h46505);
20DECLARE_LEGACY_DEVICE(HD6845, hd6845);
21DECLARE_LEGACY_DEVICE(SY6545_1, sy6545_1);
22DECLARE_LEGACY_DEVICE(SY6845E, sy6845e);
2316
17#define MC6845_INTERFACE(name) \
18   const mc6845_interface (name) =
2419
2520#define MCFG_MC6845_ADD(_tag, _variant, _clock, _config) \
2621   MCFG_DEVICE_ADD(_tag, _variant, _clock) \
2722   MCFG_DEVICE_CONFIG(_config)
2823
2924
25class mc6845_device;
26
3027/* callback definitions */
31typedef void * (*mc6845_begin_update_func)(device_t *device, bitmap_t *bitmap, const rectangle *cliprect);
32#define MC6845_BEGIN_UPDATE(name)   void *name(device_t *device, bitmap_t *bitmap, const rectangle *cliprect)
28typedef void * (*mc6845_begin_update_func)(mc6845_device *device, bitmap_t *bitmap, const rectangle *cliprect);
29#define MC6845_BEGIN_UPDATE(name)   void *name(mc6845_device *device, bitmap_t *bitmap, const rectangle *cliprect)
3330
34typedef void (*mc6845_update_row_func)(device_t *device, bitmap_t *bitmap,
31
32typedef void (*mc6845_update_row_func)(mc6845_device *device, bitmap_t *bitmap,
3533                              const rectangle *cliprect, UINT16 ma, UINT8 ra,
3634                              UINT16 y, UINT8 x_count, INT8 cursor_x, void *param);
37#define MC6845_UPDATE_ROW(name)      void name(device_t *device, bitmap_t *bitmap,   \
35
36
37#define MC6845_UPDATE_ROW(name)      void name(mc6845_device *device, bitmap_t *bitmap,   \
3838                                   const rectangle *cliprect, UINT16 ma, UINT8 ra,   \
3939                                   UINT16 y, UINT8 x_count, INT8 cursor_x, void *param)
4040
41typedef void (*mc6845_end_update_func)(device_t *device, bitmap_t *bitmap, const rectangle *cliprect, void *param);
42#define MC6845_END_UPDATE(name)      void name(device_t *device, bitmap_t *bitmap, const rectangle *cliprect, void *param)
4341
44typedef void (*mc6845_on_update_addr_changed_func)(device_t *device, int address, int strobe);
45#define MC6845_ON_UPDATE_ADDR_CHANGED(name)   void name(device_t *device, int address, int strobe)
42typedef void (*mc6845_end_update_func)(mc6845_device *device, bitmap_t *bitmap, const rectangle *cliprect, void *param);
43#define MC6845_END_UPDATE(name)      void name(mc6845_device *device, bitmap_t *bitmap, const rectangle *cliprect, void *param)
4644
45
46typedef void (*mc6845_on_update_addr_changed_func)(mc6845_device *device, int address, int strobe);
47#define MC6845_ON_UPDATE_ADDR_CHANGED(name)   void name(mc6845_device *device, int address, int strobe)
48
49
4750/* interface */
4851typedef struct _mc6845_interface mc6845_interface;
4952struct _mc6845_interface
5053{
51   const char *screen_tag;      /* screen we are acting on */
52   int hpixels_per_column;      /* number of pixels per video memory address */
54   const char *m_screen_tag;      /* screen we are acting on */
55   int m_hpixels_per_column;      /* number of pixels per video memory address */
5356
5457   /* if specified, this gets called before any pixel update,
5558       optionally return a pointer that will be passed to the
5659       update and tear down callbacks */
57   mc6845_begin_update_func      begin_update;
60   mc6845_begin_update_func      m_begin_update;
5861
5962   /* this gets called for every row, the driver must output
6063       x_count * hpixels_per_column pixels.
6164       cursor_x indicates the character position where the cursor is, or -1
6265       if there is no cursor on this row */
63   mc6845_update_row_func         update_row;
66   mc6845_update_row_func         m_update_row;
6467
6568   /* if specified, this gets called after all row updating is complete */
66   mc6845_end_update_func         end_update;
69   mc6845_end_update_func         m_end_update;
6770
6871   /* if specified, this gets called for every change of the disply enable pin (pin 18) */
69   devcb_write_line            out_de_func;
72   devcb_write_line         m_out_de_func;
7073
7174   /* if specified, this gets called for every change of the cursor pin (pin 19) */
72   devcb_write_line            out_cur_func;
75   devcb_write_line         m_out_cur_func;
7376
7477   /* if specified, this gets called for every change of the HSYNC pin (pin 39) */
75   devcb_write_line            out_hsync_func;
78   devcb_write_line         m_out_hsync_func;
7679
7780   /* if specified, this gets called for every change of the VSYNC pin (pin 40) */
78   devcb_write_line            out_vsync_func;
81   devcb_write_line         m_out_vsync_func;
7982
8083   /* Called whenenever the update address changes
8184     * For vblank/hblank timing strobe indicates the physical update.
8285     * vblank/hblank timing not supported yet! */
8386
84   mc6845_on_update_addr_changed_func   on_update_addr_changed;
87   mc6845_on_update_addr_changed_func   m_on_update_addr_changed;
8588};
8689
87extern const mc6845_interface mc6845_null_interface;
8890
91class mc6845_device :   public device_t,
92                  public mc6845_interface
93{
94   friend class mc6845_1_device;
95   friend class r6545_1_device;
96   friend class c6545_1_device;
97   friend class h46505_device;
98   friend class hd6845_device;
99   friend class sy6545_1_device;
100   friend class sy6845e_device;
89101
90/* select one of the registers for reading or writing */
102
103public:
104    // construction/destruction
105    mc6845_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
106   mc6845_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
107
108   /* select one of the registers for reading or writing */
109   void address_w(UINT8 data);
110
111   /* read from the status register */
112   UINT8 status_r();
113
114   /* read from the currently selected register */
115   UINT8 register_r();
116
117   /* write to the currently selected register */
118   void register_w(UINT8 data);
119
120   /* return the current value on the MA0-MA13 pins */
121   UINT16 get_ma();
122
123   /* return the current value on the RA0-RA4 pins */
124   UINT8 get_ra();
125
126   /* simulates the LO->HI clocking of the light pen pin (pin 3) */
127   void assert_light_pen_input();
128
129   /* set the clock (pin 21) of the chip */
130   void set_clock(int clock);
131
132   /* set number of pixels per video memory address */
133   void set_hpixels_per_column(int hpixels_per_column);
134
135   /* updates the screen -- this will call begin_update(),
136       followed by update_row() reapeatedly and after all row
137       updating is complete, end_update() */
138   void update(bitmap_t *bitmap, const rectangle *cliprect);
139
140protected:
141    // device-level overrides
142   virtual void device_config_complete();
143    virtual void device_start();
144   virtual void device_reset();
145   virtual void device_post_load();
146   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
147
148private:
149   bool m_supports_disp_start_addr_r;
150   bool m_supports_vert_sync_width;
151   bool m_supports_status_reg_d5;
152   bool m_supports_status_reg_d6;
153   bool m_supports_status_reg_d7;
154   bool m_supports_transparent;
155
156   devcb_resolved_write_line   m_res_out_de_func;
157   devcb_resolved_write_line   m_res_out_cur_func;
158   devcb_resolved_write_line   m_res_out_hsync_func;
159   devcb_resolved_write_line   m_res_out_vsync_func;
160
161   int m_device_type;
162   screen_device *m_screen;
163
164   /* register file */
165   UINT8   m_horiz_char_total;      /* 0x00 */
166   UINT8   m_horiz_disp;         /* 0x01 */
167   UINT8   m_horiz_sync_pos;      /* 0x02 */
168   UINT8   m_sync_width;         /* 0x03 */
169   UINT8   m_vert_char_total;      /* 0x04 */
170   UINT8   m_vert_total_adj;      /* 0x05 */
171   UINT8   m_vert_disp;         /* 0x06 */
172   UINT8   m_vert_sync_pos;      /* 0x07 */
173   UINT8   m_mode_control;         /* 0x08 */
174   UINT8   m_max_ras_addr;         /* 0x09 */
175   UINT8   m_cursor_start_ras;      /* 0x0a */
176   UINT8   m_cursor_end_ras;      /* 0x0b */
177   UINT16   m_disp_start_addr;      /* 0x0c/0x0d */
178   UINT16   m_cursor_addr;         /* 0x0e/0x0f */
179   UINT16   m_light_pen_addr;      /* 0x10/0x11 */
180   UINT16   m_update_addr;         /* 0x12/0x13 */
181
182   /* other internal state */
183   UINT8   m_register_address_latch;
184   bool   m_cursor_state;
185   UINT8   m_cursor_blink_count;
186   bool   m_update_ready_bit;
187   /* output signals */
188   int      m_cur;
189   int      m_hsync;
190   int      m_vsync;
191   int      m_de;
192
193   /* internal counters */
194   UINT8   m_character_counter;      /* Not used yet */
195   UINT8   m_hsync_width_counter;   /* Not used yet */
196   UINT8   m_line_counter;
197   UINT8   m_raster_counter;
198   UINT8   m_adjust_counter;
199   UINT8   m_vsync_width_counter;
200
201   bool   m_line_enable_ff;      /* Internal flip flop which is set when the line_counter is reset and reset when vert_disp is reached */
202   UINT8   m_vsync_ff;
203   UINT8   m_adjust_active;
204   UINT16   m_line_address;
205   INT16   m_cursor_x;
206
207   /* timers */
208   static const device_timer_id TIMER_LINE = 0;
209   static const device_timer_id TIMER_DE_OFF = 1;
210   static const device_timer_id TIMER_CUR_ON = 2;
211   static const device_timer_id TIMER_CUR_OFF = 3;
212   static const device_timer_id TIMER_HSYNC_ON = 4;
213   static const device_timer_id TIMER_HSYNC_OFF = 5;
214   static const device_timer_id TIMER_LIGHT_PEN_LATCH = 6;
215   static const device_timer_id TIMER_UPD_ADR = 7;
216   static const device_timer_id TIMER_UPD_TRANS = 8;
217
218   emu_timer *m_line_timer;
219   emu_timer *m_de_off_timer;
220   emu_timer *m_cur_on_timer;
221   emu_timer *m_cur_off_timer;
222   emu_timer *m_hsync_on_timer;
223   emu_timer *m_hsync_off_timer;
224   emu_timer *m_light_pen_latch_timer;
225   emu_timer *m_upd_adr_timer;
226   emu_timer *m_upd_trans_timer;
227
228   /* computed values - do NOT state save these! */
229   /* These computed are used to define the screen parameters for a driver */
230   UINT16   m_horiz_pix_total;
231   UINT16   m_vert_pix_total;
232   UINT16   m_max_visible_x;
233   UINT16   m_max_visible_y;
234   UINT16   m_hsync_on_pos;
235   UINT16   m_hsync_off_pos;
236   UINT16   m_vsync_on_pos;
237   UINT16   m_vsync_off_pos;
238   bool   m_has_valid_parameters;
239
240   UINT16   m_current_disp_addr;   /* the display address currently drawn (used only in mc6845_update) */
241
242   bool    m_light_pen_latched;
243   attotime m_upd_time;
244
245   void update_upd_adr_timer();
246   void call_on_update_address(int strobe);
247   void recompute_parameters(bool postload);
248   void update_counters();
249   void set_de(int state);
250   void set_hsync(int state);
251   void set_vsync(int state);
252   void set_cur(int state);
253   void handle_line_timer();
254   void update_cursor_state();
255};
256
257
258class mc6845_1_device : public mc6845_device
259{
260public:
261   mc6845_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
262
263protected:
264   virtual void device_start();
265   virtual void device_reset();
266};
267
268class r6545_1_device : public mc6845_device
269{
270public:
271   r6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
272
273protected:
274   virtual void device_start();
275   virtual void device_reset();
276};
277
278class c6545_1_device : public mc6845_device
279{
280public:
281   c6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
282
283protected:
284   virtual void device_start();
285   virtual void device_reset();
286};
287
288class h46505_device : public mc6845_device
289{
290public:
291   h46505_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
292
293protected:
294   virtual void device_start();
295   virtual void device_reset();
296};
297
298class hd6845_device : public mc6845_device
299{
300public:
301   hd6845_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
302
303protected:
304   virtual void device_start();
305   virtual void device_reset();
306};
307
308class sy6545_1_device : public mc6845_device
309{
310public:
311   sy6545_1_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
312
313protected:
314   virtual void device_start();
315   virtual void device_reset();
316};
317
318class sy6845e_device : public mc6845_device
319{
320public:
321   sy6845e_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
322
323protected:
324   virtual void device_start();
325   virtual void device_reset();
326};
327
328
329extern const device_type MC6845;
330extern const device_type MC6845_1;
331extern const device_type R6545_1;
332extern const device_type C6545_1;
333extern const device_type H46505;
334extern const device_type HD6845;
335extern const device_type SY6545_1;
336extern const device_type SY6845E;
337
338
91339WRITE8_DEVICE_HANDLER( mc6845_address_w );
92
93/* read from the status register */
94340READ8_DEVICE_HANDLER( mc6845_status_r );
95
96/* read from the currently selected register */
97341READ8_DEVICE_HANDLER( mc6845_register_r );
98
99/* write to the currently selected register */
100342WRITE8_DEVICE_HANDLER( mc6845_register_w );
101
102/* return the current value on the MA0-MA13 pins */
103343UINT16 mc6845_get_ma(device_t *device);
104
105/* return the current value on the RA0-RA4 pins */
106344UINT8 mc6845_get_ra(device_t *device);
107
108/* simulates the LO->HI clocking of the light pen pin (pin 3) */
109345void mc6845_assert_light_pen_input(device_t *device);
110
111/* set the clock (pin 21) of the chip */
112346void mc6845_set_clock(device_t *device, int clock);
113
114/* set number of pixels per video memory address */
115347void mc6845_set_hpixels_per_column(device_t *device, int hpixels_per_column);
116
117/* updates the screen -- this will call begin_update(),
118   followed by update_row() reapeatedly and after all row
119   updating is complete, end_update() */
120348void mc6845_update(device_t *device, bitmap_t *bitmap, const rectangle *cliprect);
121349
122350#endif

Previous 15994 Revisions Next


© 1998-2018 The MESS Team