Previous 15994 Revisions Next

r4154 Saturday 17th January, 2009 at 15:04:16 UTC by Wilbert Pol
[CPC family] Updated all drivers to use the mc6845 device.
[docs]messnew.txt
[src/emu/video]mc6845.c
[src/mess]mess.mak
[src/mess/drivers]amstrad.c
[src/mess/includes]amstrad.h
[src/mess/machine]amstrad.c
[src/mess/video]amstrad.c

src/emu/video/mc6845.c
r4153r4154
623623   if (clock != mc6845->clock)
624624   {
625625      mc6845->clock = clock;
626      recompute_parameters(mc6845, FALSE);
626      recompute_parameters(mc6845, TRUE);
627627   }
628628}
629629
src/mess/video/amstrad.c
r4153r4154
1/***************************************************************************
2
3  amstrad.c
4
5  Functions to emulate the video hardware of the amstrad CPC.
6
7***************************************************************************/
8
9
10#include "driver.h"
11#include "devices/snapquik.h"
12#include "includes/amstrad.h"
13
14/* CRTC emulation code */
15#include "video/m6845.h"
16
17#include "sound/ay8910.h"
18
19int prev_reg;
20
21static int amstrad_plus_dma_repeat[3];  // marks the location of the channels' last repeat
22static int amstrad_plus_dma_pause[3];  // pause count
23static int amstrad_plus_dma_loopcount[3]; // counts loops taken on this channel
24
25static int amstrad_plus_split_scanline;  // ASIC split screen
26static int amstrad_plus_split_address;
27static int amstrad_screen_width;  // width in bytes
28
29static void amstrad_plus_handle_dma(running_machine *machine);
30
31/***************************************************************************
32  Start the video hardware emulation.
33***************************************************************************/
34
35/* CRTC status */
36static int amstrad_CRTC_MA = 0; /* MA = Memory Address output */
37static int amstrad_CRTC_RA = 0; /* RA = Row Address output */
38static int amstrad_CRTC_HS = 0; /* HS = Horizontal Sync */
39int amstrad_CRTC_VS = 0;        /* VS = Vertical Sync */
40static int amstrad_CRTC_DE = 0; /* DE = Display Enabled */
41int amstrad_CRTC_CR = 0;        /* CR = Cursor Enabled */
42
43/* this is the real pixel position */
44static int x_screen_pos;
45static int y_screen_pos;
46
47/* this is the pixel position of the start of a scanline for the amstrad screen */
48static int x_screen_offset = -224;
49static int y_screen_offset = -32;
50
51/* display origin - used to align hardware sprites */
52static int display_x;
53static int display_y;
54static int display_update;  // flag to get location at first DE
55
56/* this contains the colours in machine->pens form.*/
57/* this is updated from the eventlist and reflects the current state
58of the render colours - these may be different to the current colour palette values */
59/* colours can be changed at any time and will take effect immediatly */
60static unsigned long amstrad_GateArray_render_colours[17];
61/* The gate array counts CRTC HSYNC pulses. (It has a internal 6-bit counter). */
62int amstrad_CRTC_HS_Counter;
63/* 2 HSYNCS after the VSYNC Counter */
64static int amstrad_CRTC_HS_After_VS_Counter;
65
66static bitmap_t   *amstrad_bitmap;
67
68static int amstrad_scanline;
69static int border_counter;  // counts length of the soft scroll border extend
70
71/* the mode is re-loaded at each HSYNC */
72/* current mode to render */
73static int amstrad_render_mode;
74static void (*draw_function)(void);
75/* current programmed mode */
76static int amstrad_current_mode;
77
78static unsigned long Mode0Lookup[256];
79static unsigned long Mode1Lookup[256];
80static unsigned long Mode3Lookup[256];
81
82/* The Amstrad CPC has a fixed palette of 27 colours generated from 3 levels of Red, Green and Blue.
83The hardware allows selection of 32 colours, but these extra colours are copies of existing colours.*/
84
85static const rgb_t amstrad_palette[32] =
86{
87   MAKE_RGB(0x060, 0x060, 0x060),            /* white */
88   MAKE_RGB(0x060, 0x060, 0x060),            /* white */
89   MAKE_RGB(0x000, 0x0ff, 0x060),            /* sea green */
90   MAKE_RGB(0x0ff, 0x0ff, 0x060),            /* pastel yellow */
91   MAKE_RGB(0x000, 0x000, 0x060),            /* blue */
92   MAKE_RGB(0x0ff, 0x000, 0x060),            /* purple */
93   MAKE_RGB(0x000, 0x060, 0x060),            /* cyan */
94   MAKE_RGB(0x0ff, 0x060, 0x060),            /* pink */
95   MAKE_RGB(0x0ff, 0x000, 0x060),            /* purple */
96   MAKE_RGB(0x0ff, 0x0ff, 0x060),            /* pastel yellow */
97   MAKE_RGB(0x0ff, 0x0ff, 0x000),            /* bright yellow */
98   MAKE_RGB(0x0ff, 0x0ff, 0x0ff),            /* bright white */
99   MAKE_RGB(0x0ff, 0x000, 0x000),            /* bright red */
100   MAKE_RGB(0x0ff, 0x000, 0x0ff),            /* bright magenta */
101   MAKE_RGB(0x0ff, 0x060, 0x000),            /* orange */
102   MAKE_RGB(0x0ff, 0x060, 0x0ff),            /* pastel magenta */
103   MAKE_RGB(0x000, 0x000, 0x060),            /* blue */
104   MAKE_RGB(0x000, 0x0ff, 0x060),            /* sea green */
105   MAKE_RGB(0x000, 0x0ff, 0x000),            /* bright green */
106   MAKE_RGB(0x000, 0x0ff, 0x0ff),            /* bright cyan */
107   MAKE_RGB(0x000, 0x000, 0x000),            /* black */
108   MAKE_RGB(0x000, 0x000, 0x0ff),            /* bright blue */
109   MAKE_RGB(0x000, 0x060, 0x000),            /* green */
110   MAKE_RGB(0x000, 0x060, 0x0ff),            /* sky blue */
111   MAKE_RGB(0x060, 0x000, 0x060),            /* magenta */
112   MAKE_RGB(0x060, 0x0ff, 0x060),            /* pastel green */
113   MAKE_RGB(0x060, 0x0ff, 0x060),            /* lime */
114   MAKE_RGB(0x060, 0x0ff, 0x0ff),            /* pastel cyan */
115   MAKE_RGB(0x060, 0x000, 0x000),            /* Red */
116   MAKE_RGB(0x060, 0x000, 0x0ff),            /* mauve */
117   MAKE_RGB(0x060, 0x060, 0x000),            /* yellow */
118   MAKE_RGB(0x060, 0x060, 0x0ff)              /* pastel blue */
119};
120
121/* the green brightness is equal to the firmware colour index */
122static const rgb_t amstrad_green_palette[32] =
123{
124   MAKE_RGB(0x000, 0x07F, 0x000),        /*13*/
125   MAKE_RGB(0x000, 0x07F, 0x000),        /*13*/
126   MAKE_RGB(0x000, 0x0BA, 0x000),        /*19*/
127   MAKE_RGB(0x000, 0x0F5, 0x000),        /*25*/
128   MAKE_RGB(0x000, 0x009, 0x000),        /*1*/
129   MAKE_RGB(0x000, 0x044, 0x000),        /*7*/
130   MAKE_RGB(0x000, 0x062, 0x000),        /*10*/
131   MAKE_RGB(0x000, 0x09C, 0x000),        /*16*/
132   MAKE_RGB(0x000, 0x044, 0x000),        /*7*/
133   MAKE_RGB(0x000, 0x0F5, 0x000),        /*25*/
134   MAKE_RGB(0x000, 0x0EB, 0x000),        /*24*/
135   MAKE_RGB(0x000, 0x0FF, 0x000),        /*26*/
136   MAKE_RGB(0x000, 0x03A, 0x000),        /*6*/
137   MAKE_RGB(0x000, 0x04E, 0x000),        /*8*/
138   MAKE_RGB(0x000, 0x093, 0x000),        /*15*/
139   MAKE_RGB(0x000, 0x0A6, 0x000),        /*17*/
140   MAKE_RGB(0x000, 0x009, 0x000),        /*1*/
141   MAKE_RGB(0x000, 0x0BA, 0x000),        /*19*/
142   MAKE_RGB(0x000, 0x0B0, 0x000),        /*18*/
143   MAKE_RGB(0x000, 0x0C4, 0x000),        /*20*/
144   MAKE_RGB(0x000, 0x000, 0x000),        /*0*/
145   MAKE_RGB(0x000, 0x013, 0x000),        /*2*/
146   MAKE_RGB(0x000, 0x058, 0x000),        /*9*/
147   MAKE_RGB(0x000, 0x06B, 0x000),        /*11*/
148   MAKE_RGB(0x000, 0x027, 0x000),        /*4*/
149   MAKE_RGB(0x000, 0x0D7, 0x000),        /*22*/
150   MAKE_RGB(0x000, 0x0CD, 0x000),        /*21*/
151   MAKE_RGB(0x000, 0x0E1, 0x000),        /*23*/
152   MAKE_RGB(0x000, 0x01D, 0x000),        /*3*/
153   MAKE_RGB(0x000, 0x031, 0x000),        /*5*/
154   MAKE_RGB(0x000, 0x075, 0x000),        /*12*/
155   MAKE_RGB(0x000, 0x089, 0x000)         /*14*/
156};
157/* Initialise the palette */
158void amstrad_init_palette(running_machine* machine)
159{
160      if ( ((input_port_read(machine, "green_display")) & 0x01)==0 )
161      palette_set_colors(machine, 0, amstrad_palette, ARRAY_LENGTH(amstrad_palette));
162      else
163      palette_set_colors(machine, 0, amstrad_green_palette, ARRAY_LENGTH(amstrad_green_palette));
164}
165
166/*************************************************************************/
167/* KC Compact
168
169The palette is defined by a colour rom. The only rom dump that exists (from the KC-Club webpage)
170is 2K, which seems correct. In this rom the same 32 bytes of data is repeated throughout the rom.
171
172When a I/O write is made to "Gate Array" to select the colour, Bit 7 and 6 are used by the
173"Gate Array" to define the function, bit 7 = 0, bit 6 = 1. In the  Amstrad CPC, bits 4..0
174define the hardware colour number, but in the KC Compact, it seems bits 5..0
175define the hardware colour number allowing 64 colours to be chosen.
176
177It is possible therefore that the colour rom could be reprogrammed, so that other colour
178selections could be chosen allowing 64 different colours to be used. But this has not been tested
179and co
180
181colour rom byte:
182
183Bit Function
1847 not used
1856 not used
1865,4 Green value
1873,2 Red value
1881,0 Blue value
189
190Green value, Red value, Blue value: 0 = 0%, 01/10 = 50%, 11 = 100%.
191The 01 case is not used, it is unknown if this produces a different amount of colour.
192*/
193
194static unsigned char kccomp_get_colour_element(int colour_value)
195{
196   switch (colour_value)
197   {
198      case 0:
199         return 0x00;
200      case 1:
201         return 0x60;
202      case 2:
203         return 0x60;
204      case 3:
205         return 0xff;
206   }
207
208   return 0xff;
209}
210
211
212/* the colour rom has the same 32 bytes repeated, but it might be possible to put a new rom in
213with different data and be able to select the other entries - not tested on a real kc compact yet
214and not supported by this driver */
215PALETTE_INIT( kccomp )
216{
217   int i;
218
219   color_prom = color_prom+0x018000;
220
221   for (i=0; i<32; i++)
222   {
223      palette_set_color_rgb(machine, i,
224         kccomp_get_colour_element((color_prom[i]>>2) & 0x03),
225         kccomp_get_colour_element((color_prom[i]>>4) & 0x03),
226         kccomp_get_colour_element((color_prom[i]>>0) & 0x03));
227   }
228}
229
230
231/********************************************
232Amstrad Plus
233
234The Amstrad Plus has a 4096 colour palette
235*********************************************/
236
237PALETTE_INIT( amstrad_plus )
238{
239   int i;
240
241   palette_set_colors(machine, 0, amstrad_palette, sizeof(amstrad_palette) / 3);
242   for ( i = 0; i < 0x1000; i++ )
243   {
244      int r, g, b;
245
246      r = ( i >> 8 ) & 0x0f;
247      g = ( i >> 4 ) & 0x0f;
248      b = i & 0x0f;
249
250      r = ( r << 4 ) | ( r );
251      g = ( g << 4 ) | ( g );
252      b = ( b << 4 ) | ( b );
253
254      palette_set_color_rgb(machine, i+48, g, r, b);
255   }
256}
257
258
259PALETTE_INIT( aleste )
260{
261   int i;
262
263   /* CPC Colour data is stored in the colour ROM (RFCOLDAT.BIN) at 0x140-0x17f */
264   unsigned char* pal = memory_region(machine,"user4");
265
266   for(i=0; i<32; i++)
267   {
268      int r,g,b;
269     
270      b = (pal[0x140+i] >> 4) & 0x03;
271      g = (pal[0x140+i] >> 2) & 0x03;
272      r = pal[0x140+i] & 0x03;
273     
274      r = (r << 6);
275      g = (g << 6);
276      b = (b << 6);
277
278      palette_set_color_rgb(machine, i, r, g, b);
279   }
280   
281   /* MSX colour palette is 6-bit RGB */
282   for(i=0; i<64; i++)
283   {
284      int r,g,b;
285
286      r = (i >> 4) & 0x03;
287      g = (i >> 2) & 0x03;
288      b = i & 0x03;
289
290      r = (r << 6);
291      g = (g << 6);
292      b = (b << 6);
293
294      palette_set_color_rgb(machine, i+32, r, g, b);
295   }
296}
297
298void amstrad_plus_setspritecolour(running_machine *machine, unsigned int off, int r, int g, int b)
299{
300   palette_set_color_rgb(machine, (off/2) + 33, r, g, b);
301}
302
303static void amstrad_init_lookups(void)
304{
305   int i;
306
307   for (i=0; i<256; i++)
308   {
309      int pen;
310
311      pen = (
312         ((i & (1<<7))>>(7-0)) |
313         ((i & (1<<3))>>(3-1)) |
314         ((i & (1<<5))>>(5-2)) |
315                        ((i & (1<<1))<<2)
316         );
317
318      Mode0Lookup[i] = pen;
319
320      Mode3Lookup[i] = pen & 0x03;
321
322      pen = (
323         ( ( (i & (1<<7)) >>7) <<0) |
324              ( ( (i & (1<<3)) >>3) <<1)
325         );
326
327      Mode1Lookup[i] = pen;
328
329   }
330}
331/* Set the new colour from the GateArray */
332void amstrad_vh_update_colour(int PenIndex, int hw_colour_index)
333{
334   int val;
335/*  int cpu_cycles = ((cycles_currently_ran()>>2)-1) & 63;
336
337   logerror("color is changed(%d,%d) = %d\n",PenIndex, cpu_cycles, hw_colour_index);
338  amstrad_GateArray_colours_ischanged++;
339   amstrad_GateArray_changed_colours[cpu_cycles][PenIndex] = hw_colour_index;
340*/
341   amstrad_GateArray_render_colours[PenIndex] = hw_colour_index;
342   if(amstrad_system_type != 0)
343   {  // CPC+/GX4000 - normal palette changes through the Gate Array also makes the corresponding change in the ASIC palette
344      val = (amstrad_palette[hw_colour_index] & 0xf00000) >> 16; // red
345      val |= (amstrad_palette[hw_colour_index] & 0x0000f0) >> 4; // blue
346      amstrad_plus_asic_ram[0x2400+PenIndex*2] = val;
347      val = (amstrad_palette[hw_colour_index] & 0x00f000) >> 12; // green
348      amstrad_plus_asic_ram[0x2401+PenIndex*2] = val;
349   }
350}
351
352void aleste_vh_update_colour(int PenIndex, int hw_colour_index)
353{
354/*  int cpu_cycles = ((cycles_currently_ran()>>2)-1) & 63;
355
356   logerror("color is changed(%d,%d) = %d\n",PenIndex, cpu_cycles, hw_colour_index);
357  amstrad_GateArray_colours_ischanged++;
358   amstrad_GateArray_changed_colours[cpu_cycles][PenIndex] = hw_colour_index;
359*/
360   amstrad_GateArray_render_colours[PenIndex] = hw_colour_index+32;
361}
362
363/* Set the new screen mode (0,1,2,4) from the GateArray */
364void amstrad_vh_update_mode(int new_mode)
365{
366   amstrad_current_mode = new_mode;
367
368}
369
370static void amstrad_draw_screen_disabled(void)
371{
372   int colour;
373
374   if(amstrad_system_type == 0)
375      plot_box(amstrad_bitmap,x_screen_pos,y_screen_pos,AMSTRAD_CHARACTERS*2,1,amstrad_GateArray_render_colours[16]);
376   else
377   {
378      colour = 48 + amstrad_plus_asic_ram[0x2420];
379      colour += amstrad_plus_asic_ram[0x2421] << 8;
380      plot_box(amstrad_bitmap,x_screen_pos,y_screen_pos,AMSTRAD_CHARACTERS*2,1,colour);
381   }
382}
383
384/* mode 0 - low resolution - 16 colours */
385static void amstrad_plus_draw_screen_enabled_mode_0(void)
386{
387   bitmap_t *bitmap = amstrad_bitmap;
388
389   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
390   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
391   /* calc mem addr to fetch data from   based on ma, and ra */
392   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
393         ((ra & 0x07)<<11) |
394         ((ma & 0x03ff)<<1);
395
396   int x = x_screen_pos;
397   int y = y_screen_pos;
398   int cpcpen, messpen;
399
400   unsigned char data;
401
402   if(border_counter > 0)
403   {
404      if(border_counter > 0)
405         amstrad_draw_screen_disabled();
406      border_counter--;
407      return;
408   }
409
410   addr -= (amstrad_plus_scroll_x / 8);  // adjust for soft scroll register
411   if(ra > 0x07)
412      addr += amstrad_screen_width;
413   addr &= 0xffff;
414   data = mess_ram[addr];
415   cpcpen = Mode0Lookup[data];
416   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
417   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
418   plot_box(bitmap,x,y,4,1,messpen);
419
420   data = data<<1;
421
422   cpcpen = Mode0Lookup[data];
423   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
424   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
425   plot_box(bitmap,x+4,y,4,1,messpen);
426
427   data = mess_ram[addr+1];
428
429   cpcpen = Mode0Lookup[data];
430   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
431   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
432   plot_box(bitmap,x+8,y,4,1,messpen);
433
434   data = data<<1;
435
436   cpcpen = Mode0Lookup[data];
437   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
438   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
439   plot_box(bitmap,x+12,y,4,1,messpen);
440}
441
442/* mode 1 - medium resolution - 4 colours */
443static void amstrad_plus_draw_screen_enabled_mode_1(void)
444{
445   bitmap_t *bitmap = amstrad_bitmap;
446
447   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
448   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
449/* calc mem addr to fetch data from   based on ma, and ra */
450   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
451         ((ra & 0x07)<<11) |
452         ((ma & 0x03ff)<<1);
453
454   int x = x_screen_pos;
455   int y = y_screen_pos;
456
457  int i, cpcpen, messpen;
458  unsigned char data1;
459  unsigned char data2;
460
461   if(border_counter > 0)
462   {
463      if(border_counter > 0)
464         amstrad_draw_screen_disabled();
465      border_counter--;
466      return;
467   }
468
469   addr -= (amstrad_plus_scroll_x / 8);  // adjust for soft scroll register
470   if(ra > 0x07)
471      addr += amstrad_screen_width;
472   addr &= 0xffff;
473   data1 = mess_ram[addr];
474   data2 = mess_ram[addr+1];
475
476   for (i=0;i<4;i++) {
477      cpcpen = Mode1Lookup[data1& 0xFF];
478   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
479   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
480     plot_box(bitmap,x,y,2,1,messpen);
481
482      cpcpen = Mode1Lookup[data2];
483   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
484   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
485     plot_box(bitmap,x+8,y,2,1,messpen);
486
487     x += 2;
488      data1 = data1<<1;
489      data2 = data2<<1;
490   }
491}
492
493/* mode 2: high resolution - 2 colours */
494static void amstrad_plus_draw_screen_enabled_mode_2(void)
495{
496   bitmap_t *bitmap = amstrad_bitmap;
497
498   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
499   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
500/* calc mem addr to fetch data from   based on ma, and ra */
501   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
502         ((ra & 0x07)<<11) |
503         ((ma & 0x03ff)<<1);
504
505   int x = x_screen_pos;
506   int y = y_screen_pos;
507   int i, cpcpen, messpen;
508   unsigned long data;
509
510   if(border_counter > 0)
511   {
512      if(border_counter > 0)
513         amstrad_draw_screen_disabled();
514      border_counter--;
515      return;
516   }
517   if(ra > 0x07)  // soft scroll adjust
518      addr += amstrad_screen_width;
519   addr &= 0xffff;
520   data = (mess_ram[addr]<<8) | mess_ram[addr+1];
521
522   for (i=0; i<16; i++)
523   {
524      cpcpen = (data>>15) & 0x01;
525      messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
526      messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
527      *BITMAP_ADDR16(bitmap, y, x) = messpen;
528      x++;
529      data = data<<1;
530   }
531}
532
533/* undocumented mode. low resolution - 4 colours */
534static void amstrad_plus_draw_screen_enabled_mode_3(void)
535{
536   bitmap_t *bitmap = amstrad_bitmap;
537
538   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
539   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
540/* calc mem addr to fetch data from   based on ma, and ra */
541   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
542         ((ra & 0x07)<<11) |
543         ((ma & 0x03ff)<<1);
544
545   int x = x_screen_pos;
546   int y = y_screen_pos;
547   int cpcpen, messpen;
548   unsigned char data;
549
550   if(border_counter > 0)
551   {
552      if(border_counter > 0)
553         amstrad_draw_screen_disabled();
554      border_counter--;
555      return;
556   }
557
558   addr -= (amstrad_plus_scroll_x / 8);  // adjust for soft scroll register
559   if(ra > 0x07)
560      addr += amstrad_screen_width;
561   addr &= 0xffff;
562   data = mess_ram[addr];
563
564   cpcpen = Mode3Lookup[data];
565   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
566   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
567   plot_box(bitmap,x,y,4,1,messpen);
568
569   data = data<<1;
570
571   cpcpen = Mode3Lookup[data];
572   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
573   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
574   plot_box(bitmap,x+4,y,4,1,messpen);
575
576   data = mess_ram[addr+1];
577
578   cpcpen = Mode3Lookup[data];
579   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
580   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
581   plot_box(bitmap,x+8,y,4,1,messpen);
582
583   data = data<<1;
584
585   cpcpen = Mode3Lookup[data];
586   messpen = 48 + (amstrad_plus_asic_ram[0x2400+cpcpen*2]);//amstrad_GateArray_render_colours[cpcpen];
587   messpen += (amstrad_plus_asic_ram[0x2401+cpcpen*2]) << 8;
588   plot_box(bitmap,x+12,y,4,1,messpen);
589}
590
591/* mode 0 - low resolution - 16 colours */
592static void amstrad_draw_screen_enabled_mode_0(void)
593{
594   bitmap_t *bitmap = amstrad_bitmap;
595
596   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
597   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
598   /* calc mem addr to fetch data from   based on ma, and ra */
599   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
600         ((ra & 0x07)<<11) |
601         ((ma & 0x03ff)<<1);
602
603   int x = x_screen_pos;
604   int y = y_screen_pos;
605   int cpcpen, messpen;
606
607   unsigned char data = mess_ram[addr];
608
609   if(amstrad_system_type != 0)
610   {
611      amstrad_plus_draw_screen_enabled_mode_0();
612      return;
613   }
614
615   cpcpen = Mode0Lookup[data];
616   messpen = amstrad_GateArray_render_colours[cpcpen];
617   plot_box(bitmap,x,y,4,1,messpen);
618
619   data = data<<1;
620
621   cpcpen = Mode0Lookup[data];
622   messpen = amstrad_GateArray_render_colours[cpcpen];
623   plot_box(bitmap,x+4,y,4,1,messpen);
624
625   data = mess_ram[addr+1];
626
627   cpcpen = Mode0Lookup[data];
628   messpen = amstrad_GateArray_render_colours[cpcpen];
629   plot_box(bitmap,x+8,y,4,1,messpen);
630
631   data = data<<1;
632
633   cpcpen = Mode0Lookup[data];
634   messpen = amstrad_GateArray_render_colours[cpcpen];
635   plot_box(bitmap,x+12,y,4,1,messpen);
636}
637
638/* mode 1 - medium resolution - 4 colours */
639static void amstrad_draw_screen_enabled_mode_1(void)
640{
641   bitmap_t *bitmap = amstrad_bitmap;
642
643   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
644   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
645/* calc mem addr to fetch data from   based on ma, and ra */
646   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
647         ((ra & 0x07)<<11) |
648         ((ma & 0x03ff)<<1);
649
650   int x = x_screen_pos;
651   int y = y_screen_pos;
652
653  int i, cpcpen, messpen;
654  unsigned char data1 = mess_ram[addr];
655  unsigned char data2 = mess_ram[addr+1];
656
657   if(amstrad_system_type != 0)
658   {
659      amstrad_plus_draw_screen_enabled_mode_1();
660      return;
661   }
662
663   if(aleste_mode & 0x04) // if in Aleste mode
664   {
665      if(~aleste_mode & 0x08)  // and VRAM is not active
666      {
667         amstrad_draw_screen_disabled();
668         return;
669      }
670   }
671
672   for (i=0;i<4;i++) {
673      cpcpen = Mode1Lookup[data1& 0xFF];
674    messpen = amstrad_GateArray_render_colours[cpcpen];
675     plot_box(bitmap,x,y,2,1,messpen);
676
677      cpcpen = Mode1Lookup[data2];
678    messpen = amstrad_GateArray_render_colours[cpcpen];
679     plot_box(bitmap,x+8,y,2,1,messpen);
680
681     x += 2;
682      data1 = data1<<1;
683      data2 = data2<<1;
684   }
685}
686
687/* mode 2: high resolution - 2 colours */
688static void amstrad_draw_screen_enabled_mode_2(void)
689{
690   bitmap_t *bitmap = amstrad_bitmap;
691
692   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
693   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
694/* calc mem addr to fetch data from   based on ma, and ra */
695   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
696         ((ra & 0x07)<<11) |
697         ((ma & 0x03ff)<<1);
698
699   int x = x_screen_pos;
700   int y = y_screen_pos;
701   int i, cpcpen, messpen;
702   unsigned long data = (mess_ram[addr]<<8) | mess_ram[addr+1];
703
704   if(amstrad_system_type != 0)
705   {
706      amstrad_plus_draw_screen_enabled_mode_2();
707      return;
708   }
709
710   if(aleste_mode & 0x04) // if in Aleste mode
711   {
712      if(~aleste_mode & 0x08)  // and VRAM is not active
713      {
714         amstrad_draw_screen_disabled();
715         return;
716      }
717   }
718
719   for (i=0; i<16; i++)
720   {
721      cpcpen = (data>>15) & 0x01;
722      messpen = amstrad_GateArray_render_colours[cpcpen];
723      if ((x >= 0) && (x < bitmap->width) && (y >= 0) && (y < bitmap->height))
724         *BITMAP_ADDR16(bitmap, y, x) = messpen;
725      x++;
726      data = data<<1;
727   }
728}
729
730/* undocumented mode. low resolution - 4 colours */
731static void amstrad_draw_screen_enabled_mode_3(void)
732{
733   bitmap_t *bitmap = amstrad_bitmap;
734
735   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
736   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
737/* calc mem addr to fetch data from   based on ma, and ra */
738   unsigned int addr = (((ma>>(4+8)) & 0x03)<<14) |
739         ((ra & 0x07)<<11) |
740         ((ma & 0x03ff)<<1);
741
742   int x = x_screen_pos;
743   int y = y_screen_pos;
744   int cpcpen, messpen;
745   unsigned char data = mess_ram[addr];
746
747   if(amstrad_system_type != 0)
748   {
749      amstrad_plus_draw_screen_enabled_mode_3();
750      return;
751   }
752
753   cpcpen = Mode3Lookup[data];
754   messpen = amstrad_GateArray_render_colours[cpcpen];
755   plot_box(bitmap,x,y,4,1,messpen);
756
757   data = data<<1;
758
759   cpcpen = Mode3Lookup[data];
760   messpen = amstrad_GateArray_render_colours[cpcpen];
761   plot_box(bitmap,x+4,y,4,1,messpen);
762
763   data = mess_ram[addr+1];
764
765   cpcpen = Mode3Lookup[data];
766   messpen = amstrad_GateArray_render_colours[cpcpen];
767   plot_box(bitmap,x+8,y,4,1,messpen);
768
769   data = data<<1;
770
771   cpcpen = Mode3Lookup[data];
772   messpen = amstrad_GateArray_render_colours[cpcpen];
773   plot_box(bitmap,x+12,y,4,1,messpen);
774}
775
776/* Aleste mode 2: high resolution - 4 colours */
777static void aleste_draw_screen_enabled_mode_2(void)
778{
779   bitmap_t *bitmap = amstrad_bitmap;
780
781   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
782   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
783/* calc mem addr to fetch data from   based on ma, and ra */
784   unsigned int addr = (((ma>>13) & 0x01)<<15) |
785         ((ra & 0x06)<<11) |
786         ((ra & 0x01) << 14) |
787         ((ma & 0x07ff) << 1);
788
789   int x = x_screen_pos;
790   int y = y_screen_pos;
791   int i, cpcpen, messpen;
792   unsigned char data1;
793   unsigned char data2;
794
795   if(~aleste_mode & 0x08)
796   {
797      amstrad_draw_screen_disabled();
798      return;
799   }
800
801   data1 = mess_ram[addr];
802   data2 = mess_ram[addr+1];
803
804   for (i=0;i<4;i++)
805   {
806      cpcpen = Mode1Lookup[data1& 0xFF];
807      messpen = amstrad_GateArray_render_colours[cpcpen];
808        plot_box(bitmap,x,y,1,1,messpen);
809
810      cpcpen = Mode1Lookup[data2];
811      messpen = amstrad_GateArray_render_colours[cpcpen];
812        plot_box(bitmap,x+4,y,1,1,messpen);
813
814      x += 1;
815      data1 = data1<<1;
816      data2 = data2<<1;
817   }
818}
819
820/* Aleste mode 3 - medium resolution, 16 colours */
821static void aleste_draw_screen_enabled_mode_3(void)
822{
823   bitmap_t *bitmap = amstrad_bitmap;
824
825   int ma = amstrad_CRTC_MA; // m6845_memory_address_r(0);
826   int ra = amstrad_CRTC_RA; // m6845_row_address_r(0);
827   /* calc mem addr to fetch data from   based on ma, and ra */
828   unsigned int addr = (((ma>>13) & 0x01)<<15) |
829         ((ra & 0x06)<<11) |
830         ((ra & 0x01) << 14) |
831         ((ma & 0x07ff) << 1);
832
833   int x = x_screen_pos;
834   int y = y_screen_pos;
835   int cpcpen, messpen;
836
837   unsigned char data;
838
839   if(~aleste_mode & 0x08)
840   {
841      amstrad_draw_screen_disabled();
842      return;
843   }
844
845   data = mess_ram[addr];
846
847   cpcpen = Mode0Lookup[data];
848   messpen = amstrad_GateArray_render_colours[cpcpen];
849   plot_box(bitmap,x,y,2,1,messpen);
850
851   data = data<<1;
852
853   cpcpen = Mode0Lookup[data];
854   messpen = amstrad_GateArray_render_colours[cpcpen];
855   plot_box(bitmap,x+2,y,2,1,messpen);
856
857   data = mess_ram[addr+1];
858
859   cpcpen = Mode0Lookup[data];
860   messpen = amstrad_GateArray_render_colours[cpcpen];
861   plot_box(bitmap,x+4,y,2,1,messpen);
862
863   data = data<<1;
864
865   cpcpen = Mode0Lookup[data];
866   messpen = amstrad_GateArray_render_colours[cpcpen];
867   plot_box(bitmap,x+6,y,2,1,messpen);
868
869}
870
871
872
873/* execute crtc_execute_cycles of crtc */
874void amstrad_vh_execute_crtc_cycles(running_machine *machine, int dummy)
875{
876   int scrwidth = AMSTRAD_SCREEN_WIDTH;
877   m6845_clock(machine); // Clock the 6845
878   if(aleste_mode & 0x02)
879   {
880//      scrwidth = ALESTE_SCREEN_WIDTH;
881   }
882
883   if ((x_screen_pos >= 0) && (x_screen_pos < scrwidth) && (y_screen_pos >= 0))
884   {
885      /* render the screen */
886      (draw_function)();
887   }
888   if(aleste_mode & 0x02)  // Aleste high res mode
889      x_screen_pos += (AMSTRAD_CHARACTERS); // Move to next raster
890   else
891      x_screen_pos += (AMSTRAD_CHARACTERS*2); // Move to next raster
892/*    }*/
893}
894
895/*
896  ASIC hardware sprites
897  16 sprites, 15 colours, 4096 colour palette
898
899  ASIC sprite memory map, must be mapped in using the secondary lower ROM select register
900
901  &4000 - &4fff   Sprite bitmap data, lower 4 bits.
902
903  &6000 - &607f   Sprite properties, 8 bytes each
904                  byte 0-1: Sprite X location
905              byte 2-3: Sprite Y location
906              byte 5:   Sprite magnication (LSB first)
907                        bit 0-1: Y Magnification
908                     bit 2-3: X magnification
909                                     00 = not displayed
910                            01 = x1
911                            10 = x2
912                            11 = x4
913
914  &6422 - &643f   Sprite palette, 12-bit, xxxxGGGGRRRRBBBB, sprite pens 1-15 (0 is always transparent)
915*/
916static void amstrad_plus_sprite_draw(running_machine *machine, bitmap_t* scr_bitmap)
917{
918   int spr;  // sprite number
919   int xloc,yloc;
920   int xmag,ymag;  // sprite properties
921   int sprptr;  // sprite location in ASIC RAM
922   rectangle rect;
923
924   // get display bounds from CRTC registers (sprites are bound and clipped to inside the border)
925   rect.min_x = display_x;//(((vid.registers[0] - 1) - (vid.registers[2] - 1))*4)+8;
926   rect.max_x = rect.min_x + (m6845_get_register(1) * 16);
927   rect.min_y = display_y;//(((vid.registers[4] - 1) - (vid.registers[7] - 1))*4)+4;
928   rect.max_y = rect.min_y + (m6845_get_register(6) * (m6845_get_register(9)+1));
929
930   for(spr = 15; spr >= 0; spr--)
931   {
932      sprptr = 0x2000 + (8*spr);
933      xmag = (amstrad_plus_asic_ram[sprptr+4] & 0x0c) >> 2;
934      ymag = amstrad_plus_asic_ram[sprptr+4] & 0x03;
935      if(xmag != 0 && ymag != 0)
936      {
937         xmag = 1<<(15+xmag);
938         ymag = 1<<(15+ymag);
939         xloc = amstrad_plus_asic_ram[sprptr] + (amstrad_plus_asic_ram[sprptr+1] << 8);
940         xloc += rect.min_x;
941         yloc = amstrad_plus_asic_ram[sprptr+2] + (amstrad_plus_asic_ram[sprptr+3] << 8);
942         yloc += rect.min_y;
943         drawgfxzoom(scr_bitmap,machine->gfx[0],spr,0,0,0,xloc,yloc,&rect,
944            TRANSPARENCY_PEN,0,xmag,ymag);
945      }
946   }
947}
948
949/*
950   CPC+ / GX4000 ASIC split screen registers
951*/
952void amstrad_plus_setsplitline(unsigned int line, unsigned int address)
953{
954   amstrad_plus_split_scanline = line;
955   amstrad_plus_split_address = address;
956}
957
958/*
959DMA commands
960
9610RDDh    LOAD R,D    Load 8 bit data D to PSG register R (0<=R<=15)
9621NNNh    PAUSE N    Pause for N prescaled ticks (0<N<=4095)
9632NNNh    REPEAT N    Set loop counter to N for this stream (0<N<=4095), and mark next instruction as loop start.
9643xxxh    (reserved)    Do not use
9654000h    NOP    No operation (64us idle)
9664001h    LOOP    If loop counter non zero, loop back to the first instruction after REPEAT instruction and decrement loop counter.
9674010h    INT    Interrupt the CPU
9684020h    STOP    Stop processing the sound list.
969*/
970
971static void amstrad_plus_dma_parse(running_machine *machine, int channel, int *addr)
972{
973   unsigned short command;
974   const address_space* space = cpu_get_address_space(machine->cpu[0],ADDRESS_SPACE_PROGRAM);
975
976   if(*addr & 0x01)
977      (*addr)++;  // align to even address
978
979   if(amstrad_plus_dma_pause[channel] != 0)
980   {  // do nothing, this channel is paused
981      amstrad_plus_dma_prescaler[channel]--;
982      if(amstrad_plus_dma_prescaler[channel] == 0)
983      {
984         amstrad_plus_dma_pause[channel]--;
985         amstrad_plus_dma_prescaler[channel] = amstrad_plus_asic_ram[0x2c02 + (4*channel)] + 1;
986      }
987      return;
988   }
989   command = (mess_ram[(*addr)+1] << 8) + mess_ram[(*addr)];
990//   logerror("DMA #%i: address %04x: command %04x\n",channel,*addr,command);
991   switch(command & 0xf000)
992   {
993   case 0x0000:  // Load PSG register
994      ay8910_control_port_0_w(space, 0,(command & 0x0f00) >> 8);
995      ay8910_write_port_0_w(space, 0,command & 0x00ff);
996      ay8910_control_port_0_w(space, 0,prev_reg);
997      logerror("DMA %i: LOAD %i, %i\n",channel,(command & 0x0f00) >> 8, command & 0x00ff);
998      break;
999   case 0x1000:  // Pause for n HSYNCs (0 - 4095)
1000      amstrad_plus_dma_pause[channel] = (command & 0x0fff) - 1;
1001      logerror("DMA %i: PAUSE %i\n",channel,command & 0x0fff);
1002      break;
1003   case 0x2000:  // Beginning of repeat loop
1004      amstrad_plus_dma_repeat[channel] = *addr;
1005      amstrad_plus_dma_loopcount[channel] = (command & 0x0fff);
1006      logerror("DMA %i: REPEAT %i\n",channel,command & 0x0fff);
1007      break;
1008   case 0x4000:  // Control functions
1009      if(command & 0x01) // Loop back to last Repeat instruction
1010      {
1011         if(amstrad_plus_dma_loopcount[channel] > 0)
1012         {
1013            (*addr) = amstrad_plus_dma_repeat[channel];
1014            logerror("DMA %i: LOOP (%i left)\n",channel,amstrad_plus_dma_loopcount[channel]);
1015            amstrad_plus_dma_loopcount[channel]--;
1016         }
1017         else
1018            logerror("DMA %i: LOOP (end)\n",channel);
1019      }
1020      if(command & 0x10) // Cause interrupt
1021      {
1022         amstrad_plus_irq_cause = channel * 2;
1023         amstrad_plus_asic_ram[0x2c0f] |= (0x40 >> channel);
1024         cpu_set_input_line(machine->cpu[0],0,ASSERT_LINE);
1025         logerror("DMA %i: INT\n",channel);
1026      }
1027      if(command & 0x20)  // Stop processing on this channel
1028      {
1029         amstrad_plus_dma_status &= ~(0x01 << channel);
1030         logerror("DMA %i: STOP\n",channel);
1031      }
1032      break;
1033   default:
1034      logerror("DMA: Unknown DMA command - %04x - at address &%04x\n",command,*addr);
1035   }
1036   (*addr)+=2;  // point to next DMA instruction
1037}
1038
1039static void amstrad_plus_handle_dma(running_machine *machine)
1040{
1041   if(amstrad_plus_dma_status & 0x01)  // DMA channel 0
1042      amstrad_plus_dma_parse(machine,0,&amstrad_plus_dma_0_addr);
1043
1044   if(amstrad_plus_dma_status & 0x02)  // DMA channel 1
1045      amstrad_plus_dma_parse(machine,1,&amstrad_plus_dma_1_addr);
1046
1047   if(amstrad_plus_dma_status & 0x04)  // DMA channel 2
1048      amstrad_plus_dma_parse(machine,2,&amstrad_plus_dma_2_addr);
1049}
1050
1051/************************************************************************
1052 * amstrad CRTC 6845 Status
1053 ************************************************************************/
1054/* CRTC - Set the new Memory Address output */
1055static void amstrad_Set_MA(int offset, int data)
1056{
1057   amstrad_CRTC_MA = data;
1058}
1059/* CRTC - Set the new Row Address output */
1060static void amstrad_Set_RA(int offset, int data)
1061{
1062   if(amstrad_plus_asic_enabled == 0 && amstrad_plus_scroll_y == 0)
1063      amstrad_CRTC_RA = data;
1064   else
1065      amstrad_CRTC_RA = data + amstrad_plus_scroll_y;
1066}
1067
1068/* CRTC - Set new Display Enabled Status*/
1069static void amstrad_Set_DE(int offset, int data)
1070{
1071   amstrad_CRTC_DE = data;
1072
1073   if (amstrad_CRTC_DE == 0)
1074   {
1075      draw_function = amstrad_draw_screen_disabled;
1076   }
1077   else
1078   {
1079      if(display_update == 1 && amstrad_plus_asic_enabled != 0)  // first scanline
1080      {
1081         if(amstrad_plus_scroll_y != 0)
1082            amstrad_screen_width = m6845_get_register(1) * 2;
1083         display_x = x_screen_pos;
1084         display_y = y_screen_pos;
1085         display_update = 0;
1086      }
1087
1088      if(aleste_mode & 0x02)
1089      {
1090         switch (amstrad_current_mode)
1091         {
1092         case 0x00:
1093            draw_function = amstrad_draw_screen_enabled_mode_2;
1094            break;
1095         case 0x01:
1096            draw_function = amstrad_draw_screen_enabled_mode_1;
1097            break;
1098         case 0x02:
1099            draw_function = aleste_draw_screen_enabled_mode_2;
1100            break;
1101         case 0x03:
1102            draw_function = aleste_draw_screen_enabled_mode_3;
1103            break;
1104         }
1105      }
1106      else
1107      {
1108         switch (amstrad_current_mode)
1109         {
1110         case 0x00:
1111            draw_function = amstrad_draw_screen_enabled_mode_0;
1112            break;
1113         case 0x01:
1114            draw_function = amstrad_draw_screen_enabled_mode_1;
1115            break;
1116         case 0x02:
1117            draw_function = amstrad_draw_screen_enabled_mode_2;
1118            break;
1119         case 0x03:
1120            draw_function = amstrad_draw_screen_enabled_mode_3;
1121            break;
1122         }
1123      }
1124      }
1125}
1126
1127/* CRTC - Set new Horizontal Sync Status */
1128static void amstrad_Set_HS(running_machine *machine, int offset, int data)
1129{
1130   if (data != 0)
1131   {
1132      amstrad_render_mode = amstrad_current_mode;
1133      x_screen_pos = x_screen_offset;
1134   }
1135   else
1136   {
1137      /* End of CRTC_HSync */
1138      if (y_screen_pos<AMSTRAD_SCREEN_HEIGHT)
1139      {
1140         y_screen_pos++;
1141      }
1142//   The GA has a counter that increments on every falling edge of the CRTC generated HSYNC signal.
1143
1144      amstrad_CRTC_HS_Counter++;
1145      amstrad_scanline++;
1146
1147      if (amstrad_CRTC_HS_After_VS_Counter != 0)  // counters still operate regardless of PRI state
1148      {
1149         amstrad_CRTC_HS_After_VS_Counter--;
1150
1151         if (amstrad_CRTC_HS_After_VS_Counter == 0)
1152         {
1153            if (amstrad_CRTC_HS_Counter >= 32)
1154            {
1155               if(amstrad_plus_pri == 0 || amstrad_plus_asic_enabled == 0)
1156               {
1157                  cpu_set_input_line(machine->cpu[0],0, ASSERT_LINE);
1158               }
1159            }
1160            amstrad_CRTC_HS_Counter = 0;
1161         }
1162      }
1163
1164      if (amstrad_CRTC_HS_Counter == 52)
1165      {
1166         amstrad_CRTC_HS_Counter = 0;
1167         if(amstrad_plus_pri == 0 || amstrad_plus_asic_enabled == 0)
1168         {
1169            cpu_set_input_line(machine->cpu[0],0, ASSERT_LINE);
1170         }
1171      }
1172      if(amstrad_plus_asic_enabled != 0)
1173      {
1174         // CPC+/GX4000 Programmable Raster Interrupt (disabled if &6800 in ASIC RAM is 0)
1175         if(amstrad_plus_pri != 0)
1176         {
1177            if(m6845_get_row_counter() == ((amstrad_plus_pri >> 3) & 0x1f) && m6845_get_scanline_counter() == (amstrad_plus_pri & 0x07))
1178            {
1179//               logerror("PRI: triggered, scanline %i, VSync width = %i\n",amstrad_scanline,vid.vertical_sync_width);
1180               cpu_set_input_line(machine->cpu[0],0,ASSERT_LINE);
1181               amstrad_plus_irq_cause = 0x06;  // raster interrupt vector
1182               amstrad_CRTC_HS_Counter &= ~0x20;  // ASIC PRI resets the MSB of the raster counter
1183            }
1184         }
1185         // CPC+/GX4000 Split screen registers  (disabled if &6801 in ASIC RAM is 0)
1186         if(amstrad_plus_split_scanline != 0)
1187         {
1188            if(m6845_get_row_counter() == ((amstrad_plus_split_scanline >> 3) & 0x1f) && m6845_get_scanline_counter() == (amstrad_plus_split_scanline & 0x07)) // split occurs here (hopefully)
1189            {
1190               m6845_set_address(amstrad_plus_split_address);
1191//               logerror("SSCR: Split screen occured at scanline %i",amstrad_plus_split_scanline);
1192            }
1193         }
1194         // CPC+/GX4000 soft scroll register
1195         if(amstrad_plus_scroll_border != 0)
1196         {
1197            border_counter = 1;  // border extended to cover garbage data from using the soft scroll functions
1198         }
1199         // CPC+/GX4000 DMA channels
1200         amstrad_plus_handle_dma(machine);  // a DMA command is handled at the leading edge of HSYNC (every 64us)
1201      }
1202   }
1203   amstrad_CRTC_HS = data;
1204   if(amstrad_scanline > 311) // 312 scanlines by default
1205      amstrad_scanline = 0;
1206}
1207
1208/* CRTC - Set new Vertical Sync Status*/
1209static void amstrad_Set_VS(running_machine *machine, int offset, int data)
1210{
1211/* New CRTC_VSync */
1212//  if (((amstrad_CRTC_VS^data) != 0)&&(data != 0)) {
1213  if (data != 0) {
1214    y_screen_pos = y_screen_offset;
1215      x_screen_pos = x_screen_offset;
1216/* Reset the amstrad_CRTC_HS_After_VS_Counter */
1217   amstrad_CRTC_HS_After_VS_Counter = 2;
1218   display_update = 1;
1219  }
1220  amstrad_CRTC_VS = data;
1221}
1222
1223/* CRTC - Set new Cursor Status */
1224static void amstrad_Set_CR(int offset, int data)
1225{
1226   amstrad_CRTC_CR = data;
1227}
1228/* The cursor is not used on Amstrad. The CURSOR signal is available on the Expansion port for other hardware to use. */
1229
1230static const struct m6845_interface amstrad6845= {
1231   amstrad_Set_MA, // Memory Address register
1232   amstrad_Set_RA, // Row Address register
1233   amstrad_Set_HS, // Horizontal status
1234   amstrad_Set_VS, // Vertical status
1235   amstrad_Set_DE, // Display Enabled status
1236   amstrad_Set_CR, // Cursor status
1237};
1238
1239/************************************************************************
1240 * amstrad_vh_screenrefresh
1241 * resfresh the amstrad video screen
1242 ************************************************************************/
1243
1244VIDEO_UPDATE( amstrad )
1245{
1246   rectangle rect;
1247
1248//   if(aleste_mode & 0x08)
1249//   {  // MSX video
1250//      VIDEO_UPDATE_CALL(generic_bitmapped);
1251//      return 0;
1252//   }
1253//   else
1254   {  // CPC video
1255      rect.min_x = 0;
1256      rect.max_x = AMSTRAD_SCREEN_WIDTH-1;
1257      rect.min_y = 0;
1258      rect.max_y = AMSTRAD_SCREEN_HEIGHT-1;
1259
1260   #ifdef MAME_DEBUG
1261      if(input_code_pressed(KEYCODE_Z) && amstrad_system_type != 0)
1262      {
1263         int x;
1264         for(x=0;x<32;x+=2)
1265         {
1266            amstrad_plus_asic_ram[0x2400+x] = ((x/2)<< 4) + x/2;
1267            amstrad_plus_asic_ram[0x2401+x] = x/2;
1268         }
1269      }
1270   #endif
1271      copybitmap(bitmap, amstrad_bitmap, 0,0,0,0,&rect);
1272      if(amstrad_plus_asic_enabled != 0)
1273         amstrad_plus_sprite_draw(screen->machine, bitmap);
1274      amstrad_scanline = y_screen_pos - 32;
1275      return 0;
1276   }
1277}
1278
1279
1280
1281/************************************************************************
1282 * amstrad_vh_start
1283 * Initialize the amstrad video emulation
1284 ************************************************************************/
1285
1286VIDEO_START( amstrad )
1287{
1288   amstrad_init_lookups();
1289
1290   m6845_config(&amstrad6845);
1291
1292   draw_function = amstrad_draw_screen_disabled;
1293
1294   amstrad_CRTC_HS_After_VS_Counter = 2;
1295   x_screen_pos = x_screen_offset;
1296   y_screen_pos = y_screen_offset;
1297
1298   amstrad_bitmap = auto_bitmap_alloc(AMSTRAD_SCREEN_WIDTH, AMSTRAD_SCREEN_HEIGHT, BITMAP_FORMAT_INDEXED16);
1299   display_update = 1;
1300}
1301
src/mess/includes/amstrad.h
r4153r4154
77#ifndef AMSTRAD_H_
88#define AMSTRAD_H_
99
10#include "machine/nec765.h"
11#include "video/mc6845.h"
1012#include "devices/snapquik.h"
1113
1214
13//#define AMSTRAD_VIDEO_USE_EVENT_LIST
14#ifdef AMSTRAD_VIDEO_USE_EVENT_LIST
15/* codes for eventlist */
16enum {
17   EVENT_LIST_CODE_GA_COLOUR = 0,       /* change pen colour with gate array */
18   EVENT_LIST_CODE_GA_MODE = 1,         /* change mode with gate array */
19   EVENT_LIST_CODE_CRTC_WRITE = 2,      /* change CRTC register data */
20  EVENT_LIST_CODE_CRTC_INDEX_WRITE = 3 /* change CRTC register selection */
21};
22#endif
15READ8_HANDLER ( amstrad_cpc_io_r );
16WRITE8_HANDLER ( amstrad_cpc_io_w );
2317
18WRITE8_HANDLER( amstrad_plus_asic_4000_w );
19WRITE8_HANDLER( amstrad_plus_asic_6000_w );
20READ8_HANDLER( amstrad_plus_asic_4000_r );
21READ8_HANDLER( amstrad_plus_asic_6000_r );
2422
25/*----------- defined in drivers/amstrad.c -----------*/
23READ8_DEVICE_HANDLER( amstrad_ppi_porta_r );
24READ8_DEVICE_HANDLER( amstrad_ppi_portb_r );
25WRITE8_DEVICE_HANDLER( amstrad_ppi_porta_w );
26WRITE8_DEVICE_HANDLER( amstrad_ppi_portc_w );
2627
27extern int amstrad_plus_asic_enabled;
28extern int amstrad_plus_pri;
29extern int amstrad_system_type;
30extern int amstrad_plus_irq_cause;
31extern int amstrad_plus_scroll_x;
32extern int amstrad_plus_scroll_y;
33extern int amstrad_plus_scroll_border;
28READ8_HANDLER ( amstrad_psg_porta_read );
3429
35extern int amstrad_plus_dma_status;
36extern int amstrad_plus_dma_0_addr;   // DMA channel address
37extern int amstrad_plus_dma_1_addr;
38extern int amstrad_plus_dma_2_addr;
39extern int amstrad_plus_dma_prescaler[3];  // DMA channel prescaler
30NEC765_INTERRUPT( aleste_interrupt );
4031
41extern unsigned char *amstrad_plus_asic_ram;
32MACHINE_RESET( amstrad );
33MACHINE_RESET( kccomp );
34MACHINE_START( plus );
35MACHINE_RESET( plus );
36MACHINE_RESET( gx4000 );
37MACHINE_RESET( aleste );
4238
43extern int aleste_mode;
44
45void amstrad_reset_machine(running_machine *machine);
46void amstrad_GateArray_write(running_machine *machine,int);
47void amstrad_rethinkMemory(running_machine *machine);
48void amstrad_setLowerRom(running_machine *machine);
49void amstrad_setUpperRom(running_machine *machine);
50
51void AmstradCPC_SetUpperRom(running_machine *machine, int);
52void AmstradCPC_PALWrite(running_machine *machine, int);
53
54
55/*----------- defined in machine/amstrad.c -----------*/
56
57void amstrad_setup_machine(running_machine *);
58
5939SNAPSHOT_LOAD( amstrad );
6040
61void amstrad_handle_snapshot(running_machine *, unsigned char *);
62
6341DEVICE_IMAGE_LOAD(amstrad_plus_cartridge);
6442
65/*----------- defined in video/amstrad.c -----------*/
43extern const mc6845_interface mc6845_amstrad_intf;
44extern const mc6845_interface mc6845_amstrad_plus_intf;
6645
67void amstrad_init_palette(running_machine* machine);
68void amstrad_plus_setspritecolour(running_machine *machine, unsigned int off, int r, int g, int b);
69void amstrad_plus_setsplitline(unsigned int line, unsigned int address);
46VIDEO_START( amstrad );
47VIDEO_UPDATE( amstrad );
48VIDEO_EOF( amstrad );
7049
71/* On the Amstrad, any part of the 64k memory can be access by the video
72hardware (GA and CRTC - the CRTC specifies the memory address to access,
73and the GA fetches 2 bytes of data for each 1us cycle.
50PALETTE_INIT( amstrad_cpc );         /* For CPC464, CPC664, and CPC6128 */
51PALETTE_INIT( amstrad_cpc_green );      /* For CPC464, CPC664, and CPC6128 */
52PALETTE_INIT( kccomp );               /* For KC Compact */
53PALETTE_INIT( amstrad_plus );         /* For CPC464+ and CPC6128+ */
54PALETTE_INIT( aleste );               /* For aleste */
7455
75The Z80 must also access the same ram.
76
77To maintain the screen display, the Z80 is halted on each memory access.
78
79The result is that timing for opcodes, appears to fall into a nice pattern,
80where the time for each opcode can be measured in NOP cycles. NOP cycles is
81the name I give to the time taken for one NOP command to execute.
82
83This happens to be 1us.
84
85From measurement, there are 64 NOPs per line, with 312 lines per screen.
86This gives a total of 19968 NOPs per frame. */
87
88#define AMSTRAD_CHARACTERS 8
89
90/* REAL AMSTRAD SCREEN WIDTH and HEIGHT */
91#define AMSTRAD_SCREEN_WIDTH   (50*AMSTRAD_CHARACTERS*2)
92#define AMSTRAD_SCREEN_HEIGHT   (39*AMSTRAD_CHARACTERS)
93#define ALESTE_SCREEN_WIDTH     (82*AMSTRAD_CHARACTERS)
94
95/* number of us cycles per frame (measured) */
96#define AMSTRAD_US_PER_SCANLINE   64
97#define AMSTRAD_FPS               50.080128205128205
98
99extern VIDEO_START( amstrad );
100extern VIDEO_UPDATE( amstrad );
101void amstrad_vh_execute_crtc_cycles(running_machine*, int);
102void amstrad_vh_update_colour(int, int);
103void amstrad_vh_update_mode(int);
104
105/* The VSYNC signal of the CRTC */
106extern int amstrad_CRTC_VS;
107extern int amstrad_CRTC_CR;
108
109/*** AMSTRAD CPC SPECIFIC ***/
110
111/* initialise palette for CPC464, CPC664 and CPC6128 */
112//extern PALETTE_INIT( amstrad_cpc );
113
114/**** KC COMPACT SPECIFIC ***/
115
116/* initialise palette for KC Compact */
117extern PALETTE_INIT( kccomp );
118
119/**** AMSTRAD PLUS SPECIFIC ***/
120
121/* initialise palette for 464plus, 6128plus */
122extern PALETTE_INIT( amstrad_plus );
123
124/**** ALESTE SPECIFIC ***/
125extern PALETTE_INIT( aleste );
126void aleste_vh_update_colour(int, int);
127
128extern int prev_reg;
129
130/* The gate array counts CRTC HSYNC pulses. (It has a internal 6-bit counter). */
131extern int amstrad_CRTC_HS_Counter;
132
13356#endif /* AMSTRAD_H_ */
src/mess/machine/amstrad.c
r4153r4154
1515rom/ram selection
1616
1717
18On the Amstrad, any part of the 64k memory can be access by the video
19hardware (GA and CRTC - the CRTC specifies the memory address to access,
20and the GA fetches 2 bytes of data for each 1us cycle.
21
22The Z80 must also access the same ram.
23
24To maintain the screen display, the Z80 is halted on each memory access.
25
26The result is that timing for opcodes, appears to fall into a nice pattern,
27where the time for each opcode can be measured in NOP cycles. NOP cycles is
28the name I give to the time taken for one NOP command to execute.
29
30This happens to be 1us.
31
32From measurement, there are 64 NOPs per line, with 312 lines per screen.
33This gives a total of 19968 NOPs per frame.
34
1835***************************************************************************/
1936
2037
2138#include "driver.h"
2239#include "cpu/z80/z80.h"
23#include "video/m6845.h"
2440#include "machine/8255ppi.h"
41#include "machine/mc146818.h"
2542#include "machine/nec765.h"
2643#include "devices/dsk.h"
2744#include "devices/cassette.h"
45#include "devices/printer.h"
2846#include "devices/snapquik.h"
2947#include "includes/amstrad.h"
3048#include "sound/ay8910.h"
3149
3250
33void amstrad_setup_machine(running_machine *machine)
51#define MANUFACTURER_NAME 0x07
52#define TV_REFRESH_RATE 0x10
53
54enum {
55   SYSTEM_CPC,
56   SYSTEM_ALESTE,
57   SYSTEM_PLUS,
58   SYSTEM_GX4000
59};
60
61
62/* originally from drivers/amstrad.c */
63// &ff,&77,&b3,&51,&a8,&d4,&62,&39,&9c,&46,&2b,&15,&8a,&cd,&ee
64// This is the sequence for unlocking the ASIC in the CPC+/GX4000
65// These are outed to port &bc00, after syncing the lock by outing a non-zero value then a zero to &bc00
66// To lock the ASIC again, repeat the sequence without the last &ee
67static const UINT8 asic_unlock_seq[15] =
3468{
35   amstrad_reset_machine(machine);
69   0xff, 0x77, 0xb3, 0x51, 0xa8, 0xd4, 0x62, 0x39, 0x9c, 0x46, 0x2b, 0x15, 0x8a, 0xcd, 0xee
70};
71
72static int amstrad_system_type;
73static UINT8 aleste_mode;
74static int amstrad_plus_irq_cause;  // part of the interrupt vector for IM 2.  6 = raster IRQ, 4 = DMA channel 2, 2 = DMA channel 1, 0 = DMA channel 0
75
76
77/****************************
78 * Gate Array data (CPC) -
79 ****************************/
80static struct {
81   UINT8   pen_selected;      /* Pen selection */
82   UINT8   mrer;            /* Mode and ROM Enable Register */
83   UINT8   upper_bank;
84} gate_array;
85
86
87/****************************
88 * ASIC data (CPC plus)
89 ****************************/
90static struct {
91   UINT8   enabled;         /* Are CPC plus features enabled/unlocked */
92   UINT8   pri;            /* Programmable raster interrupt */
93   UINT8   seqptr;            /* Current position in the ASIC unlocking sequence */
94   UINT8   rmr2;            /* ROM mapping register 2 */
95   UINT16   split_ma_base;      /* Used to handle split screen support */
96   UINT16   split_ma_started;   /* Used to handle split screen support */
97
98   /* DMA */
99   UINT8   dma_status;
100   UINT8   dma_clear;         /* Set if DMA interrupts are to be cleared automatically */
101   UINT8   dma_prescaler[3];   /* DMA channel prescaler */
102   UINT16   dma_repeat[3];      /* Location of the DMA channel's last repeat */
103   UINT16   dma_addr[3];      /* DMA channel address */
104   UINT16   dma_loopcount[3];   /* Count loops taken on this channel */
105   UINT16   dma_pause[3];      /* DMA pause count */
106} asic;
107
108
109/* Ram configuration */
110static int amstrad_GateArray_RamConfiguration = 0;
111
112/* pointers to current ram configuration selected for banks */
113static unsigned char *AmstradCPC_RamBanks[4];
114static unsigned char *Aleste_RamBanks[4];
115static int aleste_active_page[4];  // page mapped to CPU RAM (is readable?)
116
117/* pointer to RAM used for the CPC+ ASIC memory-mapped registers */
118static UINT8 *amstrad_plus_asic_ram;
119
120/* the hardware allows selection of 256 ROMs. Rom 0 is usually BASIC and Rom 7 is AMSDOS */
121/* With the CPC hardware, if a expansion ROM is not connected, BASIC rom will be selected instead */
122static unsigned char *Amstrad_ROM_Table[256];
123/* data present on input of ppi, and data written to ppi output */
124#define amstrad_ppi_PortA 0
125#define amstrad_ppi_PortB 1
126#define amstrad_ppi_PortC 2
127
128static UINT8 ppi_port_inputs[3];
129static UINT8 ppi_port_outputs[3];
130
131static UINT8 previous_printer_data_byte;
132
133static int aleste_rtc_function;
134static int aleste_fdc_int;
135
136/*------------------
137  - Ram Management -
138  ------------------*/
139/* There are 8 different ram configurations which work on the currently selected 64k logical block.
140   The following tables show the possible ram configurations :*/
141static const int RamConfigurations[8 * 4] =
142{
143   0, 1, 2, 3,                   /* config 0 */
144   0, 1, 2, 7,                   /* config 1 */
145   4, 5, 6, 7,                   /* config 2 */
146   0, 3, 2, 7,                   /* config 3 */
147   0, 4, 2, 3,                   /* config 4 */
148   0, 5, 2, 3,                   /* config 5 */
149   0, 6, 2, 3,                   /* config 6 */
150   0, 7, 2, 3                    /* config 7 */
151};
152
153
154static int prev_reg;
155
156static int amstrad_CRTC_HS = 0; /* HS = Horizontal Sync */
157static int amstrad_CRTC_VS = 0;        /* VS = Vertical Sync */
158//static int amstrad_CRTC_CR = 0;        /* CR = Cursor Enabled */
159
160/* this contains the colours in machine->pens form.*/
161/* this is updated from the eventlist and reflects the current state
162of the render colours - these may be different to the current colour palette values */
163/* colours can be changed at any time and will take effect immediatly */
164static UINT16 amstrad_GateArray_render_colours[17];
165/* The gate array counts CRTC HSYNC pulses. (It has a internal 6-bit counter). */
166static int amstrad_CRTC_HS_Counter;
167/* 2 HSYNCS after the VSYNC Counter */
168static int amstrad_CRTC_HS_After_VS_Counter;
169
170static mc6845_update_row_func draw_function;
171
172static UINT8 mode0_lookup[256][2];
173static UINT8 mode1_lookup[256][4];
174
175
176/* The Amstrad CPC has a fixed palette of 27 colours generated from 3 levels of Red, Green and Blue.
177The hardware allows selection of 32 colours, but these extra colours are copies of existing colours.*/
178
179static const rgb_t amstrad_palette[32] =
180{
181   MAKE_RGB(0x060, 0x060, 0x060),            /* white */
182   MAKE_RGB(0x060, 0x060, 0x060),            /* white */
183   MAKE_RGB(0x000, 0x0ff, 0x060),            /* sea green */
184   MAKE_RGB(0x0ff, 0x0ff, 0x060),            /* pastel yellow */
185   MAKE_RGB(0x000, 0x000, 0x060),            /* blue */
186   MAKE_RGB(0x0ff, 0x000, 0x060),            /* purple */
187   MAKE_RGB(0x000, 0x060, 0x060),            /* cyan */
188   MAKE_RGB(0x0ff, 0x060, 0x060),            /* pink */
189   MAKE_RGB(0x0ff, 0x000, 0x060),            /* purple */
190   MAKE_RGB(0x0ff, 0x0ff, 0x060),            /* pastel yellow */
191   MAKE_RGB(0x0ff, 0x0ff, 0x000),            /* bright yellow */
192   MAKE_RGB(0x0ff, 0x0ff, 0x0ff),            /* bright white */
193   MAKE_RGB(0x0ff, 0x000, 0x000),            /* bright red */
194   MAKE_RGB(0x0ff, 0x000, 0x0ff),            /* bright magenta */
195   MAKE_RGB(0x0ff, 0x060, 0x000),            /* orange */
196   MAKE_RGB(0x0ff, 0x060, 0x0ff),            /* pastel magenta */
197   MAKE_RGB(0x000, 0x000, 0x060),            /* blue */
198   MAKE_RGB(0x000, 0x0ff, 0x060),            /* sea green */
199   MAKE_RGB(0x000, 0x0ff, 0x000),            /* bright green */
200   MAKE_RGB(0x000, 0x0ff, 0x0ff),            /* bright cyan */
201   MAKE_RGB(0x000, 0x000, 0x000),            /* black */
202   MAKE_RGB(0x000, 0x000, 0x0ff),            /* bright blue */
203   MAKE_RGB(0x000, 0x060, 0x000),            /* green */
204   MAKE_RGB(0x000, 0x060, 0x0ff),            /* sky blue */
205   MAKE_RGB(0x060, 0x000, 0x060),            /* magenta */
206   MAKE_RGB(0x060, 0x0ff, 0x060),            /* pastel green */
207   MAKE_RGB(0x060, 0x0ff, 0x060),            /* lime */
208   MAKE_RGB(0x060, 0x0ff, 0x0ff),            /* pastel cyan */
209   MAKE_RGB(0x060, 0x000, 0x000),            /* Red */
210   MAKE_RGB(0x060, 0x000, 0x0ff),            /* mauve */
211   MAKE_RGB(0x060, 0x060, 0x000),            /* yellow */
212   MAKE_RGB(0x060, 0x060, 0x0ff)              /* pastel blue */
213};
214
215
216/* the green brightness is equal to the firmware colour index */
217static const rgb_t amstrad_green_palette[32] =
218{
219   MAKE_RGB(0x000, 0x07F, 0x000),        /*13*/
220   MAKE_RGB(0x000, 0x07F, 0x000),        /*13*/
221   MAKE_RGB(0x000, 0x0BA, 0x000),        /*19*/
222   MAKE_RGB(0x000, 0x0F5, 0x000),        /*25*/
223   MAKE_RGB(0x000, 0x009, 0x000),        /*1*/
224   MAKE_RGB(0x000, 0x044, 0x000),        /*7*/
225   MAKE_RGB(0x000, 0x062, 0x000),        /*10*/
226   MAKE_RGB(0x000, 0x09C, 0x000),        /*16*/
227   MAKE_RGB(0x000, 0x044, 0x000),        /*7*/
228   MAKE_RGB(0x000, 0x0F5, 0x000),        /*25*/
229   MAKE_RGB(0x000, 0x0EB, 0x000),        /*24*/
230   MAKE_RGB(0x000, 0x0FF, 0x000),        /*26*/
231   MAKE_RGB(0x000, 0x03A, 0x000),        /*6*/
232   MAKE_RGB(0x000, 0x04E, 0x000),        /*8*/
233   MAKE_RGB(0x000, 0x093, 0x000),        /*15*/
234   MAKE_RGB(0x000, 0x0A6, 0x000),        /*17*/
235   MAKE_RGB(0x000, 0x009, 0x000),        /*1*/
236   MAKE_RGB(0x000, 0x0BA, 0x000),        /*19*/
237   MAKE_RGB(0x000, 0x0B0, 0x000),        /*18*/
238   MAKE_RGB(0x000, 0x0C4, 0x000),        /*20*/
239   MAKE_RGB(0x000, 0x000, 0x000),        /*0*/
240   MAKE_RGB(0x000, 0x013, 0x000),        /*2*/
241   MAKE_RGB(0x000, 0x058, 0x000),        /*9*/
242   MAKE_RGB(0x000, 0x06B, 0x000),        /*11*/
243   MAKE_RGB(0x000, 0x027, 0x000),        /*4*/
244   MAKE_RGB(0x000, 0x0D7, 0x000),        /*22*/
245   MAKE_RGB(0x000, 0x0CD, 0x000),        /*21*/
246   MAKE_RGB(0x000, 0x0E1, 0x000),        /*23*/
247   MAKE_RGB(0x000, 0x01D, 0x000),        /*3*/
248   MAKE_RGB(0x000, 0x031, 0x000),        /*5*/
249   MAKE_RGB(0x000, 0x075, 0x000),        /*12*/
250   MAKE_RGB(0x000, 0x089, 0x000)         /*14*/
251};
252
253
254/*******************************************************************
255  Prototypes
256*******************************************************************/
257
258static MC6845_UPDATE_ROW( amstrad_update_row_mode0 );
259static MC6845_UPDATE_ROW( amstrad_update_row_mode1 );
260static MC6845_UPDATE_ROW( amstrad_update_row_mode2 );
261static MC6845_UPDATE_ROW( amstrad_update_row_mode3 );
262static MC6845_UPDATE_ROW( aleste_update_row_mode2 );
263static MC6845_UPDATE_ROW( aleste_update_row_mode3 );
264static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode0 );
265static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode1 );
266static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode2 );
267static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode3 );
268static MC6845_UPDATE_ROW( amstrad_plus_update_row_sprites );
269static void amstrad_rethinkMemory(running_machine *machine);
270
271
272static const device_config *printer_device(running_machine *machine)
273{
274   return devtag_get_device(machine, PRINTER, "printer");
36275}
37276
277
278/* Initialise the palette */
279PALETTE_INIT( amstrad_cpc )
280{
281   palette_set_colors(machine, 0, amstrad_palette, ARRAY_LENGTH(amstrad_palette));
282}
283
284
285PALETTE_INIT( amstrad_cpc_green )
286{
287   palette_set_colors(machine, 0, amstrad_green_palette, ARRAY_LENGTH(amstrad_green_palette));
288}
289
290
291NEC765_INTERRUPT( aleste_interrupt )
292{
293   if(state == CLEAR_LINE)
294      aleste_fdc_int = 0;
295   else
296      aleste_fdc_int = 1;
297}
298
299
300/*************************************************************************/
301/* KC Compact
302
303The palette is defined by a colour rom. The only rom dump that exists (from the KC-Club webpage)
304is 2K, which seems correct. In this rom the same 32 bytes of data is repeated throughout the rom.
305
306When a I/O write is made to "Gate Array" to select the colour, Bit 7 and 6 are used by the
307"Gate Array" to define the function, bit 7 = 0, bit 6 = 1. In the  Amstrad CPC, bits 4..0
308define the hardware colour number, but in the KC Compact, it seems bits 5..0
309define the hardware colour number allowing 64 colours to be chosen.
310
311It is possible therefore that the colour rom could be reprogrammed, so that other colour
312selections could be chosen allowing 64 different colours to be used. But this has not been tested
313and co
314
315colour rom byte:
316
317Bit Function
3187 not used
3196 not used
3205,4 Green value
3213,2 Red value
3221,0 Blue value
323
324Green value, Red value, Blue value: 0 = 0%, 01/10 = 50%, 11 = 100%.
325The 01 case is not used, it is unknown if this produces a different amount of colour.
326*/
327
328static unsigned char kccomp_get_colour_element(int colour_value)
329{
330   switch (colour_value)
331   {
332      case 0:
333         return 0x00;
334      case 1:
335         return 0x60;
336      case 2:
337         return 0x60;
338      case 3:
339         return 0xff;
340   }
341
342   return 0xff;
343}
344
345
346/* the colour rom has the same 32 bytes repeated, but it might be possible to put a new rom in
347with different data and be able to select the other entries - not tested on a real kc compact yet
348and not supported by this driver */
349PALETTE_INIT( kccomp )
350{
351   int i;
352
353   color_prom = color_prom+0x018000;
354
355   for (i=0; i<32; i++)
356   {
357      palette_set_color_rgb(machine, i,
358         kccomp_get_colour_element((color_prom[i]>>2) & 0x03),
359         kccomp_get_colour_element((color_prom[i]>>4) & 0x03),
360         kccomp_get_colour_element((color_prom[i]>>0) & 0x03));
361   }
362}
363
364
365/********************************************
366Amstrad Plus
367
368The Amstrad Plus has a 4096 colour palette
369*********************************************/
370
371PALETTE_INIT( amstrad_plus )
372{
373   int i;
374
375   palette_set_colors(machine, 0, amstrad_palette, sizeof(amstrad_palette) / 3);
376   for ( i = 0; i < 0x1000; i++ )
377   {
378      int r, g, b;
379
380      g = ( i >> 8 ) & 0x0f;
381      r = ( i >> 4 ) & 0x0f;
382      b = i & 0x0f;
383
384      r = ( r << 4 ) | ( r );
385      g = ( g << 4 ) | ( g );
386      b = ( b << 4 ) | ( b );
387
388      palette_set_color_rgb(machine, i, r, g, b);
389   }
390}
391
392
393PALETTE_INIT( aleste )
394{
395   int i;
396
397   /* CPC Colour data is stored in the colour ROM (RFCOLDAT.BIN) at 0x140-0x17f */
398   unsigned char* pal = memory_region(machine,"user4");
399
400   for(i=0; i<32; i++)
401   {
402      int r,g,b;
403     
404      b = (pal[0x140+i] >> 4) & 0x03;
405      g = (pal[0x140+i] >> 2) & 0x03;
406      r = pal[0x140+i] & 0x03;
407     
408      r = (r << 6);
409      g = (g << 6);
410      b = (b << 6);
411
412      palette_set_color_rgb(machine, i, r, g, b);
413   }
414   
415   /* MSX colour palette is 6-bit RGB */
416   for(i=0; i<64; i++)
417   {
418      int r,g,b;
419
420      r = (i >> 4) & 0x03;
421      g = (i >> 2) & 0x03;
422      b = i & 0x03;
423
424      r = (r << 6);
425      g = (g << 6);
426      b = (b << 6);
427
428      palette_set_color_rgb(machine, i+32, r, g, b);
429   }
430}
431
432
433static void amstrad_init_lookups(void)
434{
435   int i;
436
437   for ( i = 0; i < 256; i++ )
438   {
439      mode0_lookup[i][0] = ( ( i & 0x80 ) >> 7 ) | ( ( i & 0x20 ) >> 3 ) | ( ( i & 0x08 ) >> 2 ) | ( ( i & 0x02 ) << 2 );
440      mode0_lookup[i][1] = ( ( i & 0x40 ) >> 6 ) | ( ( i & 0x10 ) >> 2 ) | ( ( i & 0x04 ) >> 1 ) | ( ( i & 0x01 ) << 3 );
441
442      mode1_lookup[i][0] = ( ( i & 0x80 ) >> 7 ) | ( ( i & 0x08 ) >> 2 );
443      mode1_lookup[i][1] = ( ( i & 0x40 ) >> 6 ) | ( ( i & 0x04 ) >> 1 );
444      mode1_lookup[i][2] = ( ( i & 0x20 ) >> 5 ) | ( ( i & 0x02 )      );
445      mode1_lookup[i][3] = ( ( i & 0x10 ) >> 4 ) | ( ( i & 0x01 ) << 1 );
446   }
447}
448
449
450/* Set the new screen mode (0,1,2,4) from the GateArray */
451void amstrad_vh_update_mode( void )
452{
453   if ( amstrad_system_type == SYSTEM_PLUS || amstrad_system_type == SYSTEM_GX4000 )
454   {
455      /* select a cpc plus mode */
456      switch ( gate_array.mrer & 0x03 )
457      {
458      case 0:      /* Mode 0: 160x200, 16 colours */
459         draw_function = amstrad_plus_update_row_mode0;
460         break;
461
462      case 1:      /* Mode 1: 320x200, 4 colous */
463         draw_function = amstrad_plus_update_row_mode1;
464         break;
465
466      case 2:      /* Mode 2: 640x200, 2 colours */
467         draw_function = amstrad_plus_update_row_mode2;
468         break;
469
470      case 3:      /* Mode 3: 160x200, 4 colours */
471         draw_function = amstrad_plus_update_row_mode3;
472         break;
473      }
474   }
475   else
476   {
477      if ( aleste_mode & 0x02 )
478      {
479         /* select an aleste mode */
480         switch ( gate_array.mrer & 0x03 )
481         {
482         case 0:      /* Aleste Mode 0 (= Amstrad CPC mode 2): 640x200, 2 colours */
483            draw_function = amstrad_update_row_mode2;
484            break;
485
486         case 1:      /* Aleste mode 1: 320x200, 4 colours */
487            draw_function = amstrad_update_row_mode1;
488            break;
489
490         case 2:      /* Aleste mode 2: 4 colours */
491            draw_function = aleste_update_row_mode2;
492            break;
493
494         case 3:      /* Aleste mode 3: 16 colours */
495            draw_function = aleste_update_row_mode3;
496            break;
497         }
498      }
499      else
500      {
501         /* select an original cpc mode */
502         switch ( gate_array.mrer & 0x03 )
503         {
504         case 0:      /* Mode 0: 160x200, 16 colours */
505            draw_function = amstrad_update_row_mode0;
506            break;
507
508         case 1:      /* Mode 1: 320x200, 4 colous */
509            draw_function = amstrad_update_row_mode1;
510            break;
511
512         case 2:      /* Mode 2: 640x200, 2 colours */
513            draw_function = amstrad_update_row_mode2;
514            break;
515
516         case 3:      /* Mode 3: 160x200, 4 colours */
517            draw_function = amstrad_update_row_mode3;
518            break;
519         }
520      }
521   }
522}
523
524
525/*
526DMA commands
527
5280RDDh    LOAD R,D    Load 8 bit data D to PSG register R (0<=R<=15)
5291NNNh    PAUSE N    Pause for N prescaled ticks (0<N<=4095)
5302NNNh    REPEAT N    Set loop counter to N for this stream (0<N<=4095), and mark next instruction as loop start.
5313xxxh    (reserved)    Do not use
5324000h    NOP    No operation (64us idle)
5334001h    LOOP    If loop counter non zero, loop back to the first instruction after REPEAT instruction and decrement loop counter.
5344010h    INT    Interrupt the CPU
5354020h    STOP    Stop processing the sound list.
536*/
537
538static void amstrad_plus_dma_parse(running_machine *machine, int channel)
539{
540   unsigned short command;
541   const address_space* space = cpu_get_address_space( machine->cpu[0],ADDRESS_SPACE_PROGRAM );
542
543   if( asic.dma_addr[channel] & 0x01)
544      asic.dma_addr[channel]++;  // align to even address
545
546   if ( asic.dma_pause[channel] != 0 )
547   {  // do nothing, this channel is paused
548      asic.dma_prescaler[channel]--;
549      if ( asic.dma_prescaler[channel] == 0 )
550      {
551         asic.dma_pause[channel]--;
552         asic.dma_prescaler[channel] = amstrad_plus_asic_ram[0x2c02 + (4*channel)] + 1;
553      }
554      return;
555   }
556   command = (mess_ram[asic.dma_addr[channel]+1] << 8) + mess_ram[asic.dma_addr[channel]];
557//   logerror("DMA #%i: address %04x: command %04x\n",channel,asic.dma_addr[channel],command);
558   switch (command & 0xf000)
559   {
560   case 0x0000:  // Load PSG register
561      ay8910_control_port_0_w(space, 0,(command & 0x0f00) >> 8);
562      ay8910_write_port_0_w(space, 0,command & 0x00ff);
563      ay8910_control_port_0_w(space, 0,prev_reg);
564      logerror("DMA %i: LOAD %i, %i\n",channel,(command & 0x0f00) >> 8, command & 0x00ff);
565      break;
566   case 0x1000:  // Pause for n HSYNCs (0 - 4095)
567      asic.dma_pause[channel] = (command & 0x0fff) - 1;
568      logerror("DMA %i: PAUSE %i\n",channel,command & 0x0fff);
569      break;
570   case 0x2000:  // Beginning of repeat loop
571      asic.dma_repeat[channel] = asic.dma_addr[channel];
572      asic.dma_loopcount[channel] = (command & 0x0fff);
573      logerror("DMA %i: REPEAT %i\n",channel,command & 0x0fff);
574      break;
575   case 0x4000:  // Control functions
576      if (command & 0x01) // Loop back to last Repeat instruction
577      {
578         if (asic.dma_loopcount[channel] > 0)
579         {
580            asic.dma_addr[channel] = asic.dma_repeat[channel];
581            logerror("DMA %i: LOOP (%i left)\n",channel,asic.dma_loopcount[channel]);
582            asic.dma_loopcount[channel]--;
583         }
584         else
585            logerror("DMA %i: LOOP (end)\n",channel);
586      }
587      if (command & 0x10) // Cause interrupt
588      {
589         amstrad_plus_irq_cause = channel * 2;
590         amstrad_plus_asic_ram[0x2c0f] |= (0x40 >> channel);
591         cpu_set_input_line(machine->cpu[0],0,ASSERT_LINE);
592         logerror("DMA %i: INT\n",channel);
593      }
594      if (command & 0x20)  // Stop processing on this channel
595      {
596         asic.dma_status &= ~(0x01 << channel);
597         logerror("DMA %i: STOP\n",channel);
598      }
599      break;
600   default:
601      logerror("DMA: Unknown DMA command - %04x - at address &%04x\n",command,asic.dma_addr[channel]);
602   }
603   asic.dma_addr[channel] += 2;  // point to next DMA instruction
604}
605
606
607static void amstrad_plus_handle_dma(running_machine *machine)
608{
609   if ( asic.dma_status & 0x01 )  // DMA channel 0
610      amstrad_plus_dma_parse( machine, 0 );
611
612   if ( asic.dma_status & 0x02 )  // DMA channel 1
613      amstrad_plus_dma_parse( machine, 1 );
614
615   if ( asic.dma_status & 0x04 )  // DMA channel 2
616      amstrad_plus_dma_parse( machine, 2 );
617}
618
619
620/* Set the new colour from the GateArray */
621static void amstrad_vh_update_colour(int PenIndex, UINT16 hw_colour_index)
622{
623   int val;
624
625   amstrad_GateArray_render_colours[PenIndex] = hw_colour_index;
626
627   if ( amstrad_system_type == SYSTEM_PLUS || amstrad_system_type == SYSTEM_GX4000 )
628   {
629      /* CPC+/GX4000 - normal palette changes through the Gate Array also makes the corresponding change in the ASIC palette */
630      val = (amstrad_palette[hw_colour_index] & 0xf00000) >> 16; /* red */
631      val |= (amstrad_palette[hw_colour_index] & 0x0000f0) >> 4; /* blue */
632      amstrad_plus_asic_ram[0x2400+PenIndex*2] = val;
633      val = (amstrad_palette[hw_colour_index] & 0x00f000) >> 12; /* green */
634      amstrad_plus_asic_ram[0x2401+PenIndex*2] = val;
635   }
636}
637
638
639static void aleste_vh_update_colour(int PenIndex, UINT16 hw_colour_index)
640{
641   amstrad_GateArray_render_colours[PenIndex] = hw_colour_index+32;
642}
643
644
645/* Mode 0: 160x200, 16 colours */
646static MC6845_UPDATE_ROW( amstrad_update_row_mode0 )
647{
648   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
649   int i;
650
651   for ( i = 0; i < x_count; i++ )
652   {
653      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
654      UINT8   data0 = mess_ram[ address ];
655      UINT8   data1 = mess_ram[ address + 1 ];
656      UINT16   c;
657
658      c = amstrad_GateArray_render_colours[ mode0_lookup[data0][0] ];
659      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
660      c = amstrad_GateArray_render_colours[ mode0_lookup[data0][1] ];
661      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
662      c = amstrad_GateArray_render_colours[ mode0_lookup[data1][0] ];
663      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
664      c = amstrad_GateArray_render_colours[ mode0_lookup[data1][1] ];
665      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
666   }
667}
668
669
670/* Mode 1: 320x200, 4 colous */
671static MC6845_UPDATE_ROW( amstrad_update_row_mode1 )
672{
673   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
674   int i;
675
676   for ( i = 0; i < x_count; i++ )
677   {
678      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
679      UINT8   data0 = mess_ram[ address ];
680      UINT8   data1 = mess_ram[ address + 1 ];
681      UINT16   c;
682
683      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][0] ];
684      *p = c; p++; *p = c; p++;
685      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][1] ];
686      *p = c; p++; *p = c; p++;
687      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][2] ];
688      *p = c; p++; *p = c; p++;
689      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][3] ];
690      *p = c; p++; *p = c; p++;
691      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][0] ];
692      *p = c; p++; *p = c; p++;
693      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][1] ];
694      *p = c; p++; *p = c; p++;
695      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][2] ];
696      *p = c; p++; *p = c; p++;
697      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][3] ];
698      *p = c; p++; *p = c; p++;
699   }
700}
701
702
703/* Mode 2: 640x200, 2 colours */
704static MC6845_UPDATE_ROW( amstrad_update_row_mode2 )
705{
706   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
707   int i;
708
709   for ( i = 0; i < x_count; i++ )
710   {
711      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
712      UINT8   data0 = mess_ram[ address ];
713      UINT8   data1 = mess_ram[ address + 1 ];
714
715      *p = amstrad_GateArray_render_colours[ ( data0 & 0x80 ) >> 7 ]; p++;
716      *p = amstrad_GateArray_render_colours[ ( data0 & 0x40 ) >> 6 ]; p++;
717      *p = amstrad_GateArray_render_colours[ ( data0 & 0x20 ) >> 5 ]; p++;
718      *p = amstrad_GateArray_render_colours[ ( data0 & 0x10 ) >> 4 ]; p++;
719      *p = amstrad_GateArray_render_colours[ ( data0 & 0x08 ) >> 3 ]; p++;
720      *p = amstrad_GateArray_render_colours[ ( data0 & 0x04 ) >> 2 ]; p++;
721      *p = amstrad_GateArray_render_colours[ ( data0 & 0x02 ) >> 1 ]; p++;
722      *p = amstrad_GateArray_render_colours[ ( data0 & 0x01 ) ]; p++;
723      *p = amstrad_GateArray_render_colours[ ( data1 & 0x80 ) >> 7 ]; p++;
724      *p = amstrad_GateArray_render_colours[ ( data1 & 0x40 ) >> 6 ]; p++;
725      *p = amstrad_GateArray_render_colours[ ( data1 & 0x20 ) >> 5 ]; p++;
726      *p = amstrad_GateArray_render_colours[ ( data1 & 0x10 ) >> 4 ]; p++;
727      *p = amstrad_GateArray_render_colours[ ( data1 & 0x08 ) >> 3 ]; p++;
728      *p = amstrad_GateArray_render_colours[ ( data1 & 0x04 ) >> 2 ]; p++;
729      *p = amstrad_GateArray_render_colours[ ( data1 & 0x02 ) >> 1 ]; p++;
730      *p = amstrad_GateArray_render_colours[ ( data1 & 0x01 ) ]; p++;
731   }
732}
733
734
735/* Mode 3: 160x200, 4 colours */
736static MC6845_UPDATE_ROW( amstrad_update_row_mode3 )
737{
738   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
739   int i;
740
741   for ( i = 0; i < x_count; i++ )
742   {
743      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
744      UINT8   data0 = mess_ram[ address ];
745      UINT8   data1 = mess_ram[ address + 1 ];
746      UINT16   c;
747
748      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][0] ];
749      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
750      c = amstrad_GateArray_render_colours[ mode1_lookup[data0][1] ];
751      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
752      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][0] ];
753      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
754      c = amstrad_GateArray_render_colours[ mode1_lookup[data1][1] ];
755      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
756
757   }
758}
759
760
761/* Plus Mode 0: 160x200, 16 colours */
762static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode0 )
763{
764   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
765   UINT16   c;
766   int i;
767
768   logerror( "amstrad_plus_update_row_mode0: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
769
770   /* Apply horizontal background scrolling */
771   for ( i = amstrad_plus_asic_ram[0x2804] & 0x0f; i > 0; i-- )
772   {
773      if ( amstrad_plus_asic_ram[0x2804] & 0x80 )
774      {
775         /* Border colour */
776         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
777      }
778      else
779      {
780         /* Random(?) colour data */
781         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
782      }
783      *p = c; p++;
784   }
785
786   /* Apply vertical background scrolling */
787   ra += ( ( amstrad_plus_asic_ram[0x2804] >> 4 ) & 0x07 );
788   if ( ra > 7 )
789   {   
790      ma += x_count;
791   }   
792
793   for ( i = 0; i < x_count; i++ )
794   {
795      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
796      UINT16   caddr;
797      UINT8   data0 = mess_ram[ address ];
798      UINT8   data1 = mess_ram[ address + 1 ];
799
800      caddr = 0x2400 + mode0_lookup[data0][0] * 2;
801      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
802      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
803      caddr = 0x2400 + mode0_lookup[data0][1] * 2;
804      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
805      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
806      caddr = 0x2400 + mode0_lookup[data1][0] * 2;
807      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
808      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
809      caddr = 0x2400 + mode0_lookup[data1][1] * 2;
810      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
811      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
812   }
813}
814
815
816/* Plus Mode 1: 320x200, 4 colours */
817static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode1 )
818{
819   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
820   UINT16   c;
821   int i;
822
823   logerror( "amstrad_plus_update_row_mode1: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
824
825   /* Apply horizontal background scrolling */
826   for ( i = amstrad_plus_asic_ram[0x2804] & 0x0f; i > 0; i-- )
827   {
828      if ( amstrad_plus_asic_ram[0x2804] & 0x80 )
829      {
830         /* Border colour */
831         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
832      }
833      else
834      {
835         /* Random(?) colour data */
836         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
837      }
838      *p = c; p++;
839   }
840
841   /* Apply vertical background scrolling */
842   ra += ( ( amstrad_plus_asic_ram[0x2804] >> 4 ) & 0x07 );
843   if ( ra > 7 )
844   {   
845      ma += x_count;
846   }   
847
848   for ( i = 0; i < x_count; i++ )
849   {
850      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
851      UINT16   caddr;
852      UINT8   data0 = mess_ram[ address ];
853      UINT8   data1 = mess_ram[ address + 1 ];
854
855      caddr = 0x2400 + mode1_lookup[data0][0] * 2;
856      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
857      *p = c; p++; *p = c; p++;
858      caddr = 0x2400 + mode1_lookup[data0][1] * 2;
859      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
860      *p = c; p++; *p = c; p++;
861      caddr = 0x2400 + mode1_lookup[data0][2] * 2;
862      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
863      *p = c; p++; *p = c; p++;
864      caddr = 0x2400 + mode1_lookup[data0][3] * 2;
865      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
866      *p = c; p++; *p = c; p++;
867      caddr = 0x2400 + mode1_lookup[data1][0] * 2;
868      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
869      *p = c; p++; *p = c; p++;
870      caddr = 0x2400 + mode1_lookup[data1][1] * 2;
871      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
872      *p = c; p++; *p = c; p++;
873      caddr = 0x2400 + mode1_lookup[data1][2] * 2;
874      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
875      *p = c; p++; *p = c; p++;
876      caddr = 0x2400 + mode1_lookup[data1][3] * 2;
877      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
878      *p = c; p++; *p = c; p++;
879   }
880}
881
882
883/* Plus Mode 2: 640x200, 2 colours */
884static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode2 )
885{
886   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
887   UINT16   c;
888   int i;
889
890   logerror( "amstrad_plus_update_row_mode2: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
891
892   /* Apply horizontal background scrolling */
893   for ( i = amstrad_plus_asic_ram[0x2804] & 0x0f; i > 0; i-- )
894   {
895      if ( amstrad_plus_asic_ram[0x2804] & 0x80 )
896      {
897         /* Border colour */
898         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
899      }
900      else
901      {
902         /* Random(?) colour data */
903         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
904      }
905      *p = c; p++;
906   }
907
908   /* Apply vertical background scrolling */
909   ra += ( ( amstrad_plus_asic_ram[0x2804] >> 4 ) & 0x07 );
910   if ( ra > 7 )
911   {
912      ma += x_count;
913   }
914
915   for ( i = 0; i < x_count; i++ )
916   {
917      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
918      UINT16   caddr;
919      UINT8   data0 = mess_ram[ address ];
920      UINT8   data1 = mess_ram[ address + 1 ];
921
922      caddr = 0x2400 + ( ( data0 & 0x80 ) ? 2 : 0 );
923      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
924      *p = c; p++;
925      caddr = 0x2400 + ( ( data0 & 0x40 ) ? 2 : 0 );
926      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
927      *p = c; p++;
928      caddr = 0x2400 + ( ( data0 & 0x20 ) ? 2 : 0 );
929      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
930      *p = c; p++;
931      caddr = 0x2400 + ( ( data0 & 0x10 ) ? 2 : 0 );
932      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
933      *p = c; p++;
934      caddr = 0x2400 + ( ( data0 & 0x08 ) ? 2 : 0 );
935      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
936      *p = c; p++;
937      caddr = 0x2400 + ( ( data0 & 0x04 ) ? 2 : 0 );
938      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
939      *p = c; p++;
940      caddr = 0x2400 + ( ( data0 & 0x02 ) ? 2 : 0 );
941      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
942      *p = c; p++;
943      caddr = 0x2400 + ( ( data0 & 0x01 ) ? 2 : 0 );
944      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
945      *p = c; p++;
946      caddr = 0x2400 + ( ( data1 & 0x80 ) ? 2 : 0 );
947      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
948      *p = c; p++;
949      caddr = 0x2400 + ( ( data1 & 0x40 ) ? 2 : 0 );
950      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
951      *p = c; p++;
952      caddr = 0x2400 + ( ( data1 & 0x20 ) ? 2 : 0 );
953      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
954      *p = c; p++;
955      caddr = 0x2400 + ( ( data1 & 0x10 ) ? 2 : 0 );
956      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
957      *p = c; p++;
958      caddr = 0x2400 + ( ( data1 & 0x08 ) ? 2 : 0 );
959      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
960      *p = c; p++;
961      caddr = 0x2400 + ( ( data1 & 0x04 ) ? 2 : 0 );
962      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
963      *p = c; p++;
964      caddr = 0x2400 + ( ( data1 & 0x02 ) ? 2 : 0 );
965      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
966      *p = c; p++;
967      caddr = 0x2400 + ( ( data1 & 0x01 ) ? 2 : 0 );
968      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
969      *p = c; p++;
970   }
971}
972
973
974/* Plus Mode 3: 160x200, 4 colours */
975static MC6845_UPDATE_ROW( amstrad_plus_update_row_mode3 )
976{
977   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
978   UINT16   c;
979   int i;
980
981   logerror( "amstrad_plus_update_row_mode3: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
982
983   /* Apply horizontal background scrolling */
984   for ( i = amstrad_plus_asic_ram[0x2804] & 0x0f; i > 0; i-- )
985   {
986      if ( amstrad_plus_asic_ram[0x2804] & 0x80 )
987      {
988         /* Border colour */
989         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
990      }
991      else
992      {
993         /* Random(?) colour data */
994         c = amstrad_plus_asic_ram[0x2420] + ( amstrad_plus_asic_ram[0x2421] << 8 );
995      }
996      *p = c; p++;
997   }
998
999   /* Apply vertical background scrolling */
1000   ra += ( ( amstrad_plus_asic_ram[0x2804] >> 4 ) & 0x07 );
1001   if ( ra > 7 )
1002   {
1003      ma += x_count;
1004   }
1005
1006   for ( i = 0; i < x_count; i++ )
1007   {
1008      UINT16   address = ( ( ( ma + i ) & 0x3000 ) << 2 ) | ( ( ra & 0x07 ) << 11 ) | ( ( ( ma + i ) & 0x3ff ) << 1 );
1009      UINT16   caddr;
1010      UINT8   data0 = mess_ram[ address ];
1011      UINT8   data1 = mess_ram[ address + 1 ];
1012
1013      caddr = 0x2400 + mode1_lookup[data0][0] * 2;
1014      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
1015      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1016      caddr = 0x2400 + mode1_lookup[data0][1] * 2;
1017      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
1018      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1019      caddr = 0x2400 + mode1_lookup[data1][0] * 2;
1020      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
1021      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1022      caddr = 0x2400 + mode1_lookup[data1][1] * 2;
1023      c = amstrad_plus_asic_ram[caddr] + ( amstrad_plus_asic_ram[caddr+1] << 8 );
1024      *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1025   }
1026}
1027
1028
1029/* Plus sprites: 16 16x16 sprites, 16 colours */
1030static MC6845_UPDATE_ROW( amstrad_plus_update_row_sprites )
1031{
1032   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
1033   int      i;
1034
1035//   logerror( "amstrad_plus_update_row_sprites: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
1036
1037   for ( i = 15 * 8; i >= 0; i -= 8 )
1038   {
1039      UINT8   xmag = ( amstrad_plus_asic_ram[ 0x2000 + i + 4 ] >> 2 ) & 0x03;
1040      UINT8   ymag = amstrad_plus_asic_ram[ 0x2000 + i + 4 ] & 0x03;
1041
1042      /* Check if sprite is enabled */
1043      if ( xmag && ymag )
1044      {
1045         INT16   spr_x = amstrad_plus_asic_ram[ 0x2000 + i ] + ( amstrad_plus_asic_ram[ 0x2001 + i ] << 8 );
1046         INT16   spr_y = amstrad_plus_asic_ram[ 0x2002 + i ] + ( amstrad_plus_asic_ram[ 0x2003 + i ] << 8 );
1047
1048         xmag -= 1;
1049         ymag -= 1;
1050
1051         /* Check if sprite would appear on this scanline */
1052         if ( spr_y <= y && y < spr_y + ( 16 << ymag ) && spr_x < 16 * x_count && spr_x + ( 16 << xmag ) > 0 )
1053         {
1054            UINT16   spr_addr = i * 32 + ( ( ( y - spr_y ) >> ymag ) * 16 );
1055            int      j, k;
1056
1057            for ( j = 0; j < 16; j++ )
1058            {
1059               for ( k = 0; k < ( 1 << xmag ); k++ )
1060               {
1061                  INT16 x = spr_x + ( j << xmag ) + k;
1062
1063                  if ( x >= 0 && x < 16 * x_count )
1064                  {
1065                     UINT8   spr_col = ( amstrad_plus_asic_ram[ spr_addr + j ] & 0x0f ) * 2;
1066
1067                     if ( spr_col )
1068                        p[x] = amstrad_plus_asic_ram[ 0x2420 + spr_col ] + ( amstrad_plus_asic_ram[ 0x2421 + spr_col ] << 8 );
1069                  }
1070               }
1071            }
1072         }
1073      }
1074   }
1075}
1076
1077
1078/* Aleste Mode 2: 640x200(?), 4 colours */
1079static MC6845_UPDATE_ROW( aleste_update_row_mode2 )
1080{
1081   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
1082   int i;
1083
1084//   logerror( "aleste_update_row_mode2: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
1085
1086   for ( i = 0; i < x_count; i++ )
1087   {
1088      UINT16   address = ( ( ( ma + i ) & 0x2000 ) << 2 ) | ( ( ra & 0x06 ) << 11 ) | ( ( ra & 0x01 ) << 14 ) | ( ( ( ma + i ) & 0x7ff ) << 1 );
1089      UINT8   data0 = mess_ram[ address ];
1090      UINT8   data1 = mess_ram[ address + 1 ];
1091      UINT16   c;
1092
1093      if ( ~ aleste_mode & 0x08 )
1094      {
1095         c = amstrad_GateArray_render_colours[ 16 ];
1096         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1097         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1098         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1099         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1100      }
1101      else
1102      {
1103         c = amstrad_GateArray_render_colours[ mode1_lookup[data0][0] ];
1104         *p = c; p++; *p = c; p++;
1105         c = amstrad_GateArray_render_colours[ mode1_lookup[data0][1] ];
1106         *p = c; p++; *p = c; p++;
1107         c = amstrad_GateArray_render_colours[ mode1_lookup[data0][2] ];
1108         *p = c; p++; *p = c; p++;
1109         c = amstrad_GateArray_render_colours[ mode1_lookup[data0][3] ];
1110         *p = c; p++; *p = c; p++;
1111         c = amstrad_GateArray_render_colours[ mode1_lookup[data1][0] ];
1112         *p = c; p++; *p = c; p++;
1113         c = amstrad_GateArray_render_colours[ mode1_lookup[data1][1] ];
1114         *p = c; p++; *p = c; p++;
1115         c = amstrad_GateArray_render_colours[ mode1_lookup[data1][2] ];
1116         *p = c; p++; *p = c; p++;
1117         c = amstrad_GateArray_render_colours[ mode1_lookup[data1][3] ];
1118         *p = c; p++; *p = c; p++;
1119      }
1120   }
1121}
1122
1123
1124/* Aleste Mode 3: 320x200, 16 colours */
1125static MC6845_UPDATE_ROW( aleste_update_row_mode3 )
1126{
1127   UINT16   *p = BITMAP_ADDR16( bitmap, y, 0 );
1128   int i;
1129
1130//   logerror( "aleste_update_row_mode3: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
1131
1132   for ( i = 0; i < x_count; i++ )
1133   {
1134      UINT16   address = ( ( ( ma + i ) & 0x2000 ) << 2 ) | ( ( ra & 0x06 ) << 11 ) | ( ( ra & 0x01 ) << 14 ) | ( ( ( ma + i ) & 0x7ff ) << 1 );
1135      UINT8   data0 = mess_ram[ address ];
1136      UINT8   data1 = mess_ram[ address + 1 ];
1137      UINT16   c;
1138
1139      if ( ~ aleste_mode & 0x08 )
1140      {
1141         c = amstrad_GateArray_render_colours[ 16 ];
1142         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1143         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1144         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1145         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1146      }
1147      else
1148      {
1149         c = amstrad_GateArray_render_colours[ mode0_lookup[data0][0] ];
1150         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1151         c = amstrad_GateArray_render_colours[ mode0_lookup[data0][1] ];
1152         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1153         c = amstrad_GateArray_render_colours[ mode0_lookup[data1][0] ];
1154         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1155         c = amstrad_GateArray_render_colours[ mode0_lookup[data1][1] ];
1156         *p = c; p++; *p = c; p++; *p = c; p++; *p = c; p++;
1157      }
1158   }
1159}
1160
1161
1162static MC6845_UPDATE_ROW( amstrad_update_row )
1163{
1164//   logerror( "amstrad_update_row: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
1165
1166   if ( draw_function )
1167      draw_function( device, bitmap, cliprect, ma, ra, y, x_count, cursor_x, param );
1168}
1169
1170
1171INLINE UINT16 amstrad_plus_translate_split( UINT16 ma, int y )
1172{
1173   if ( y == 0 )
1174   {
1175      asic.split_ma_base = 0x0000;
1176      asic.split_ma_started = 0x0000;
1177   }
1178   else if ( asic.enabled && amstrad_plus_asic_ram[0x2801] == y )
1179   {
1180      asic.split_ma_started = ma;
1181      asic.split_ma_base = ( amstrad_plus_asic_ram[0x2802] << 8 ) | amstrad_plus_asic_ram[0x2803];
1182   }
1183
1184   return ( ma - asic.split_ma_started ) + asic.split_ma_base;
1185}
1186
1187
1188static MC6845_UPDATE_ROW( amstrad_plus_update_row )
1189{
1190//  logerror( "amstrad_update_row: ma = 0x%04x, ra = %d, y = %d, x_count = %d, cursor_x = %d\n", ma, ra, y, x_count, cursor_x );
1191
1192   ma = amstrad_plus_translate_split( ma, y );
1193
1194   if ( draw_function )
1195      draw_function( device, bitmap, cliprect, ma, ra, y, x_count, cursor_x, param );
1196
1197   amstrad_plus_update_row_sprites( device, bitmap, cliprect, ma, ra, y, x_count, cursor_x, param );
1198}
1199
1200
1201static MC6845_ON_HSYNC_CHANGED( amstrad_hsync_changed )
1202{
1203   if ( hsync )
1204   {
1205      amstrad_CRTC_HS_Counter++;
1206
1207      if ( amstrad_CRTC_HS_After_VS_Counter != 0 )  // counters still operate regardless of PRI state
1208      {
1209         amstrad_CRTC_HS_After_VS_Counter--;
1210
1211         if (amstrad_CRTC_HS_After_VS_Counter == 0)
1212         {
1213            if (amstrad_CRTC_HS_Counter >= 32)
1214            {
1215               cpu_set_input_line(device->machine->cpu[0],0, ASSERT_LINE);
1216            }
1217            amstrad_CRTC_HS_Counter = 0;
1218         }
1219      }
1220
1221      if ( amstrad_CRTC_HS_Counter >= 52 )
1222      {
1223         amstrad_CRTC_HS_Counter = 0;
1224         cpu_set_input_line(device->machine->cpu[0],0, ASSERT_LINE);
1225      }
1226   }
1227   amstrad_CRTC_HS = hsync ? 1 : 0;
1228}
1229
1230
1231static MC6845_ON_HSYNC_CHANGED( amstrad_plus_hsync_changed )
1232{
1233   if ( hsync )
1234   {
1235      amstrad_CRTC_HS_Counter++;
1236
1237      if ( amstrad_CRTC_HS_After_VS_Counter != 0 )  // counters still operate regardless of PRI state
1238      {
1239         amstrad_CRTC_HS_After_VS_Counter--;
1240
1241         if (amstrad_CRTC_HS_After_VS_Counter == 0)
1242         {
1243            if (amstrad_CRTC_HS_Counter >= 32)
1244            {
1245               if( asic.pri == 0 || asic.enabled == 0)
1246               {
1247                  cpu_set_input_line(device->machine->cpu[0],0, ASSERT_LINE);
1248               }
1249            }
1250            amstrad_CRTC_HS_Counter = 0;
1251         }
1252      }
1253
1254      if ( amstrad_CRTC_HS_Counter >= 52 )
1255      {
1256         amstrad_CRTC_HS_Counter = 0;
1257         if ( asic.pri == 0 || asic.enabled == 0 )
1258         {
1259            cpu_set_input_line(device->machine->cpu[0],0, ASSERT_LINE);
1260         }
1261      }
1262
1263      if ( asic.enabled )
1264      {
1265         // CPC+/GX4000 Programmable Raster Interrupt (disabled if &6800 in ASIC RAM is 0)
1266         if ( asic.pri != 0 )
1267         {
1268            if ( asic.pri == video_screen_get_vpos( device->machine->primary_screen ) )
1269            {
1270               logerror("PRI: triggered, scanline %d\n",asic.pri);
1271               cpu_set_input_line(device->machine->cpu[0],0,ASSERT_LINE);
1272               amstrad_plus_irq_cause = 0x06;  // raster interrupt vector
1273               amstrad_CRTC_HS_Counter &= ~0x20;  // ASIC PRI resets the MSB of the raster counter
1274            }
1275         }
1276         // CPC+/GX4000 Split screen registers  (disabled if &6801 in ASIC RAM is 0)
1277         if(amstrad_plus_asic_ram[0x2801] != 0)
1278         {
1279            if ( amstrad_plus_asic_ram[0x2801] == video_screen_get_vpos( device->machine->primary_screen ) )   // split occurs here (hopefully)
1280            {
1281               video_screen_update_partial( device->machine->primary_screen, video_screen_get_vpos( device->machine->primary_screen ) );
1282               logerror("SSCR: Split screen occured at scanline %d\n",amstrad_plus_asic_ram[0x2801]);
1283            }
1284         }
1285         // CPC+/GX4000 DMA channels
1286         amstrad_plus_handle_dma(device->machine);  // a DMA command is handled at the leading edge of HSYNC (every 64us)
1287      }
1288   }
1289   amstrad_CRTC_HS = hsync ? 1 : 0;
1290}
1291
1292
1293static MC6845_ON_VSYNC_CHANGED( amstrad_vsync_changed )
1294{
1295   if ( ! amstrad_CRTC_VS && vsync )
1296   {
1297      /* Reset the amstrad_CRTC_HS_After_VS_Counter */
1298      amstrad_CRTC_HS_After_VS_Counter = 2;
1299   }
1300
1301   amstrad_CRTC_VS = vsync ? 1 : 0;
1302}
1303
1304
1305VIDEO_START( amstrad )
1306{
1307   amstrad_init_lookups();
1308
1309   draw_function = NULL;
1310   amstrad_CRTC_HS_After_VS_Counter = 2;
1311}
1312
1313
1314VIDEO_UPDATE( amstrad )
1315{
1316   const device_config *mc6845 = (device_config *) devtag_get_device( screen->machine, MC6845, "mc6845" );
1317   mc6845_update( mc6845, bitmap, cliprect );
1318   return 0;
1319}
1320
1321
1322const mc6845_interface mc6845_amstrad_intf =
1323{
1324   "main",                  /* screen name */
1325   16,                     /* number of pixels per video memory address */
1326   NULL,                  /* begin_update */
1327   amstrad_update_row,         /* update_row */
1328   NULL,                  /* end_update */
1329   NULL,                  /* on_de_changed */
1330   amstrad_hsync_changed,      /* on_hsync_changed */
1331   amstrad_vsync_changed      /* on_vsync_changed */
1332};
1333
1334
1335const mc6845_interface mc6845_amstrad_plus_intf =
1336{
1337   "main",                  /* screen name */
1338   16,                     /* number of pixels per video memory address */
1339   NULL,                  /* begin_update */
1340   amstrad_plus_update_row,   /* update_row */
1341   NULL,                  /* end_update */
1342   NULL,                  /* on_de_changed */
1343   amstrad_plus_hsync_changed,   /* on_hsync_changed */
1344   amstrad_vsync_changed      /* on_vsync_changed */
1345};
1346
1347
1348/******************************************************************************************
1349    Multiface emulation
1350  ****************************************************************************************/
1351
1352static unsigned char *multiface_ram;
1353static unsigned long multiface_flags;
1354
1355/* stop button has been pressed */
1356#define MULTIFACE_STOP_BUTTON_PRESSED   0x0001
1357/* ram/rom is paged into memory space */
1358#define MULTIFACE_RAM_ROM_ENABLED      0x0002
1359/* when visible OUT commands are performed! */
1360#define MULTIFACE_VISIBLE            0x0004
1361
1362/* multiface traps calls to 0x0065 when it is active.
1363This address has a RET and so executes no code.
1364
1365It is believed that it is used to make multiface invisible to programs */
1366
1367/*#define MULTIFACE_0065_TOGGLE                   0x0008*/
1368
1369
1370/* used to setup computer if a snapshot was specified */
1371static DIRECT_UPDATE_HANDLER( amstrad_multiface_directoverride )
1372{
1373      int pc;
1374
1375      pc = cpu_get_pc(space->machine->cpu[0]);
1376
1377      /* there are two places where CALL &0065 can be found
1378        in the multiface rom. At this address there is a RET.
1379
1380        To disable the multiface from being detected, the multiface
1381        stop button must be pressed, then the program that was stopped
1382        must be returned to. When this is done, the multiface cannot
1383        be detected and the out operations to page the multiface
1384        ram/rom into the address space will not work! */
1385
1386      /* I assume that the hardware in the multiface detects
1387        the PC set to 0x065 and uses this to enable/disable the multiface
1388        */
1389
1390      /* I also use this to allow the stop button to be pressed again */
1391      if (pc==0x0164)
1392      {
1393         /* first call? */
1394         multiface_flags |= MULTIFACE_VISIBLE;
1395      }
1396      else if (pc==0x0c98)
1397      {
1398        /* second call */
1399
1400        /* no longer visible */
1401        multiface_flags &= ~(MULTIFACE_VISIBLE|MULTIFACE_STOP_BUTTON_PRESSED);
1402
1403       /* clear op base override */
1404            memory_set_direct_update_handler(space,0);
1405      }
1406
1407      return pc;
1408}
1409
1410void multiface_init(void)
1411{
1412   /* after a reset the multiface is visible */
1413   multiface_flags = MULTIFACE_VISIBLE;
1414
1415   /* allocate ram */
1416   multiface_ram = (unsigned char *)auto_malloc(8192);
1417}
1418
1419/* call when a system reset is done */
1420void multiface_reset(void)
1421{
1422      /* stop button not pressed and ram/rom disabled */
1423      multiface_flags &= ~(MULTIFACE_STOP_BUTTON_PRESSED |
1424                  MULTIFACE_RAM_ROM_ENABLED);
1425      /* as on the real hardware the multiface is visible after a reset! */
1426      multiface_flags |= MULTIFACE_VISIBLE;
1427}
1428
1429int multiface_hardware_enabled(running_machine *machine)
1430{
1431      if (multiface_ram!=NULL)
1432      {
1433            if ((input_port_read(machine, "multiface") & 0x01)!=0)
1434            {
1435                  return 1;
1436            }
1437      }
1438
1439      return 0;
1440}
1441
1442
1443static void multiface_rethink_memory(running_machine *machine)
1444{
1445   unsigned char *multiface_rom;
1446
1447   /* multiface hardware enabled? */
1448   if (!multiface_hardware_enabled(machine))
1449      return;
1450
1451   multiface_rom = &memory_region(machine, "main")[0x01C000];
1452
1453   if (
1454      ((multiface_flags & MULTIFACE_RAM_ROM_ENABLED)!=0) &&
1455      ((gate_array.mrer & 0x04) == 0)
1456      )
1457   {
1458
1459      /* set bank addressess */
1460      memory_set_bankptr(machine,1, multiface_rom);
1461      memory_set_bankptr(machine,2, multiface_ram);
1462      memory_set_bankptr(machine,9, multiface_rom);
1463      memory_set_bankptr(machine,10, multiface_ram);
1464   }
1465}
1466
1467
1468/* simulate the stop button has been pressed */
1469void multiface_stop(running_machine *machine)
1470{
1471   /* multiface hardware enabled? */
1472   if (!multiface_hardware_enabled(machine))
1473      return;
1474
1475   /* if stop button not already pressed, do press action */
1476   /* pressing stop button while multiface is running has no effect */
1477   if ((multiface_flags & MULTIFACE_STOP_BUTTON_PRESSED)==0)
1478   {
1479      /* initialise 0065 toggle */
1480            /*multiface_flags &= ~MULTIFACE_0065_TOGGLE;*/
1481
1482      multiface_flags |= MULTIFACE_RAM_ROM_ENABLED;
1483
1484      /* stop button has been pressed, furthur pressess will not issue a NMI */
1485      multiface_flags |= MULTIFACE_STOP_BUTTON_PRESSED;
1486
1487      gate_array.mrer &=~0x04;
1488
1489      /* page rom into memory */
1490      multiface_rethink_memory(machine);
1491
1492      /* pulse the nmi line */
1493      cpu_set_input_line(machine->cpu[0], INPUT_LINE_NMI, PULSE_LINE);
1494
1495      /* initialise 0065 override to monitor calls to 0065 */
1496      memory_set_direct_update_handler(cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM),amstrad_multiface_directoverride);
1497   }
1498
1499}
1500
1501
1502/* any io writes are passed through here */
1503static WRITE8_HANDLER(multiface_io_write)
1504{
1505   /* multiface hardware enabled? */
1506      if (!multiface_hardware_enabled(space->machine))
1507      return;
1508
1509      /* visible? */
1510   if (multiface_flags & MULTIFACE_VISIBLE)
1511   {
1512      if (offset==0x0fee8)
1513      {
1514         multiface_flags |= MULTIFACE_RAM_ROM_ENABLED;
1515         amstrad_rethinkMemory(space->machine);
1516      }
1517
1518      if (offset==0x0feea)
1519      {
1520         multiface_flags &= ~MULTIFACE_RAM_ROM_ENABLED;
1521         amstrad_rethinkMemory(space->machine);
1522      }
1523   }
1524
1525   /* update multiface ram with data */
1526      /* these are decoded fully! */
1527      switch ((offset>>8) & 0x0ff)
1528      {
1529            /* gate array */
1530            case 0x07f:
1531            {
1532                  switch (data & 0x0c0)
1533                  {
1534                        /* pen index */
1535                        case 0x00:
1536                        {
1537                           multiface_ram[0x01fcf] = data;
1538
1539                        }
1540                        break;
1541
1542                        /* pen colour */
1543                        case 0x040:
1544                        {
1545                           int pen_index;
1546
1547                           pen_index = multiface_ram[0x01fcf] & 0x0f;
1548
1549                           if (multiface_ram[0x01fcf] & 0x010)
1550                           {
1551
1552                              multiface_ram[0x01fdf + pen_index] = data;
1553                           }
1554                           else
1555                           {
1556                              multiface_ram[0x01f90 + pen_index] = data & 0x01f;
1557                           }
1558
1559                        }
1560                        break;
1561
1562                        /* rom/mode selection */
1563                        case 0x080:
1564                        {
1565
1566                           multiface_ram[0x01fef] = data;
1567
1568                        }
1569                        break;
1570
1571                        /* ram configuration */
1572                        case 0x0c0:
1573                        {
1574
1575                           multiface_ram[0x01fff] = data;
1576
1577                        }
1578                        break;
1579
1580                        default:
1581                          break;
1582
1583                  }
1584
1585            }
1586            break;
1587
1588
1589            /* crtc register index */
1590            case 0x0bc:
1591            {
1592                  multiface_ram[0x01cff] = data;
1593            }
1594            break;
1595
1596            /* crtc register write */
1597            case 0x0bd:
1598            {
1599                  int reg_index;
1600
1601                  reg_index = multiface_ram[0x01cff] & 0x0f;
1602
1603                  multiface_ram[0x01db0 + reg_index] = data;
1604            }
1605            break;
1606
1607
1608            /* 8255 ppi control */
1609            case 0x0f7:
1610            {
1611              multiface_ram[0x017ff] = data;
1612
1613            }
1614            break;
1615
1616            /* rom select */
1617            case 0x0df:
1618            {
1619               multiface_ram[0x01aac] = data;
1620            }
1621            break;
1622
1623            default:
1624               break;
1625
1626       }
1627
1628}
1629
1630
1631/*--------------------------
1632  - Ram and Rom management -
1633  --------------------------*/
1634/*-----------------
1635  - Set Lower Rom -
1636  -----------------*/
1637static void amstrad_setLowerRom(running_machine *machine)
1638{
1639   UINT8 *bank_base;
1640
1641   /* b2 : "1" Lower rom area disable or "0" Lower rom area enable */
1642   if ( amstrad_system_type == SYSTEM_CPC || amstrad_system_type == SYSTEM_ALESTE )
1643   {
1644      if ((gate_array.mrer & (1<<2)) == 0)
1645      {
1646         bank_base = &memory_region(machine, "main")[0x010000];
1647      }
1648      else
1649      {
1650         if(aleste_mode & 0x04)
1651            bank_base = Aleste_RamBanks[0];
1652         else
1653            bank_base = AmstradCPC_RamBanks[0];
1654      }
1655      memory_set_bankptr(machine,1, bank_base);
1656      memory_set_bankptr(machine,2, bank_base+0x02000);
1657   }
1658   else  // CPC+/GX4000
1659   {
1660      const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
1661
1662      if ( asic.enabled && ( asic.rmr2 & 0x18 ) == 0x18 )
1663      {
1664         memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, amstrad_plus_asic_4000_r);
1665         memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, amstrad_plus_asic_6000_r);
1666         memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, amstrad_plus_asic_4000_w);
1667         memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, amstrad_plus_asic_6000_w);
1668      }
1669      else
1670      {
1671         memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK3);
1672         memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK4);
1673         memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK11);
1674         memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK12);
1675      }
1676
1677      if(AmstradCPC_RamBanks[0] != NULL)
1678      {
1679         memory_set_bankptr(machine,1, AmstradCPC_RamBanks[0]);
1680         memory_set_bankptr(machine,2, AmstradCPC_RamBanks[0]+0x2000);
1681         memory_set_bankptr(machine,3, AmstradCPC_RamBanks[1]);
1682         memory_set_bankptr(machine,4, AmstradCPC_RamBanks[1]+0x2000);
1683         memory_set_bankptr(machine,5, AmstradCPC_RamBanks[2]);
1684         memory_set_bankptr(machine,6, AmstradCPC_RamBanks[2]+0x2000);
1685      }
1686
1687      if ( (gate_array.mrer & (1<<2)) == 0)
1688      {  // ASIC secondary lower ROM selection (bit 5: 1 = enabled)
1689         if ( asic.enabled )
1690         {
1691//            logerror("L-ROM: Lower ROM enabled, cart bank %i\n", asic.rmr2 & 0x07 );
1692            bank_base = &memory_region(machine, "main")[0x4000 * ( asic.rmr2 & 0x07 )];
1693            switch( asic.rmr2 & 0x18 )
1694            {
1695            case 0x00:
1696//               logerror("L-ROM: located at &0000\n");
1697               memory_set_bankptr(machine,1, bank_base);
1698               memory_set_bankptr(machine,2, bank_base+0x02000);
1699               break;
1700            case 0x08:
1701//               logerror("L-ROM: located at &4000\n");
1702               memory_set_bankptr(machine,3, bank_base);
1703               memory_set_bankptr(machine,4, bank_base+0x02000);
1704               break;
1705            case 0x10:
1706//               logerror("L-ROM: located at &8000\n");
1707               memory_set_bankptr(machine,5, bank_base);
1708               memory_set_bankptr(machine,6, bank_base+0x02000);
1709               break;
1710            case 0x18:
1711//               logerror("L-ROM: located at &0000, ASIC registers enabled\n");
1712               memory_set_bankptr(machine,1, bank_base);
1713               memory_set_bankptr(machine,2, bank_base+0x02000);
1714               break;
1715            }
1716         }
1717         else
1718         {
1719            memory_set_bankptr( machine, 1, memory_region( machine,"main" ) );
1720            memory_set_bankptr( machine, 2, memory_region( machine,"main" ) + 0x2000 );
1721         }
1722      }
1723   }
1724}
1725
1726
1727/*-----------------
1728  - Set Upper Rom -
1729  -----------------*/
1730static void amstrad_setUpperRom(running_machine *machine)
1731{
1732   UINT8 *bank_base = NULL;
1733
1734   /* b3 : "1" Upper rom area disable or "0" Upper rom area enable */
1735   if ( ! ( gate_array.mrer & 0x08 ) )
1736   {
1737      bank_base = Amstrad_ROM_Table[ gate_array.upper_bank ];
1738   }
1739   else
1740   {
1741      if(aleste_mode & 0x04)
1742         bank_base = Aleste_RamBanks[3];
1743      else
1744         bank_base = AmstradCPC_RamBanks[3];
1745   }
1746
1747   if (bank_base)
1748   {
1749      memory_set_bankptr(machine,7, bank_base);
1750      memory_set_bankptr(machine,8, bank_base+0x2000);
1751   }
1752}
1753
1754
1755/*--------------------------
1756  - Ram and Rom management -
1757  --------------------------*/
1758
1759/* simplified ram configuration - e.g. only correct for 128k machines
1760
1761RAM Expansion Bits
1762                             7 6 5 4  3  2  1  0
1763CPC6128                      1 1 - -  -  s2 s1 s0
1764Dk'tronics 256K Silicon Disk 1 1 1 b1 b0 b2 -  -
1765
1766"-" - this bit is ignored. The value of this bit is not important.
1767"0" - this bit must be set to "0"
1768"1" - this bit must be set to "1"
1769"b0,b1,b2" - this bit is used to define the logical 64k block that the ram configuration uses
1770"s0,s1,s2" - this bit is used to define the ram configuration
1771
1772The CPC6128 has a 64k ram expansion built-in, giving 128K of RAM in this system.
1773In the CPC464,CPC664 and KC compact if a ram expansion is not present, then writing to this port has no effect and the ram will be in the same arrangement as if configuration 0 had been selected.
1774*/
1775static void AmstradCPC_GA_SetRamConfiguration(running_machine *machine)
1776{
1777   int ConfigurationIndex = amstrad_GateArray_RamConfiguration & 0x07;
1778   int BankIndex,i;
1779   unsigned char *BankAddr;
1780
1781/* if b5 = 0 */
1782   if(((amstrad_GateArray_RamConfiguration) & (1<<5)) == 0)
1783   {
1784       for (i=0;i<4;i++)
1785      {
1786         BankIndex = RamConfigurations[(ConfigurationIndex << 2) + i];
1787         BankAddr = mess_ram + (BankIndex << 14);
1788         Aleste_RamBanks[i] = BankAddr;
1789         AmstradCPC_RamBanks[i] = BankAddr;
1790      }
1791   } else {/* Need to add the ram expansion configuration here ! */
1792   }
1793   amstrad_rethinkMemory(machine);
1794}
1795
1796
1797/* ASIC RAM */
1798/*
1799    ASIC RAM Layout.  Always is mapped to &4000-&7fff
1800
1801    Hardware sprites: 16 sprites, 16x16, basic zooming, 15 colour (12-bit palette)
1802    Pixel data is 0 - 15 for sprite pen number, low 4 bits
1803    &4000 - &400f Pixel data for first line of first sprite
1804    &4010 - &401f Pixel data for second line of first sprite
1805      ...     ...
1806    &40f0 - &40ff Pixel data for last (16th) line of first sprite
1807      ...     ...
1808    &4100 - &41ff Pixel data for second sprite
1809    &4200 - &42ff Third sprite
1810      ...     ...
1811    &4f00 - &4fff Pixel data for last (16th) sprite
1812
1813    &6000 - &607f Sprite properties (8 bytes each)
1814                  +0 Sprite X position LSB
1815                  +1 Sprite X position MSB
1816                  +2 Sprite Y position LSB (scanline)
1817                  +3 Sprite Y position MSB
1818                  +4 Sprite zoom - bits 3,2 X magnification, bits 1,0 Y magnification
1819                      Magnification: 00 = not displayed, 01 = x1, 10 = x2, 11 = x4
1820
1821    Palette: LSB first, presumably GGGGBBBBxxxxRRRR
1822    &6400 - &641f Pen palette (12bpp, 2 bytes each, 16 total)
1823    &6420 - &6421 Border palette  (12bpp, 2 bytes)
1824    &6422 - &643f Hardware sprite palette (12bpp, 2 bytes each, 15 total)
1825
1826    Programmable Raster Interrupt:
1827    &6800  Scanline for IRQ to be triggered after
1828           If 0, raster interrupts and DMA interrupts occur
1829           Otherwise, the PRI interrupt is triggered only
1830
1831    Hardware split screen:
1832    &6801  Scanline for split to occur at
1833    &6802  LSB of screen address for split (like reg 12 of the 6845)
1834    &6803  MSB of the above (like reg 13 of the 6845)
1835
1836    Soft Scroll Control Register:
1837    &6804  bits 3-0 - horizontal delay in mode 2 pixels (shifts display to the right)
1838           bits 6-4 - added to the 3 LSBs for the scanline address (shifts display up)
1839           bit 7    - extends the border by two bytes (16 mode 2 pixels), masking the bad data from the horizontal scroll
1840
1841    Analogue paddle ports:
1842    &6808 - &680f  Analogue input, read-only, 6 bit
1843
1844    PSG DMA channels:
1845    &6c00  DMA channel 0 address LSB
1846    &6c01  DMA channel 0 address MSB
1847    &6c02  DMA channel 0 prescaler
1848    &6c03  unused
1849    &6c04-7  DMA channel 1
1850    &6c08-b  DMA channel 2
1851    &6c0f  Control and Status register
1852             bit 7 - raster interrupt
1853             bit 6 - DMA channel 0 interrupt
1854             bit 5 - DMA channel 1 interrupt
1855             bit 4 - DMA channel 2 interrupt
1856             bit 3 - unused (write 0)
1857             bit 2 - DMA channel 2 enable
1858             bit 1 - DMA channel 1 enable
1859             bit 0 - DMA channel 0 enable
1860
1861 */
1862
1863WRITE8_HANDLER( amstrad_plus_asic_4000_w )
1864{
1865   video_screen_update_partial( space->machine->primary_screen, video_screen_get_vpos( space->machine->primary_screen ) );
1866//   logerror("ASIC: Write to register at &%04x\n",offset+0x4000);
1867   amstrad_plus_asic_ram[offset] = data & 0x0f;
1868}
1869
1870
1871WRITE8_HANDLER( amstrad_plus_asic_6000_w )
1872{
1873   video_screen_update_partial( space->machine->primary_screen, video_screen_get_vpos( space->machine->primary_screen ) );
1874   amstrad_plus_asic_ram[offset+0x2000] = data;
1875   if(offset >= 0x0400 && offset < 0x440 && ( offset & 0x01 ) ) // ASIC palette
1876   {
1877      amstrad_plus_asic_ram[ offset + 0x2000 ] = data & 0x0f;
1878   }
1879   if(offset == 0x0800)  // Programmable raster interrupt
1880   {
1881//      logerror("ASIC: Wrote %02x to PRI\n",data);
1882      asic.pri = data;
1883   }
1884   if(offset >= 0x0801 && offset <= 0x0803)  // Split screen registers
1885   {
1886      logerror("ASIC: Split screen at line %i, address &%04x\n",amstrad_plus_asic_ram[0x2801],amstrad_plus_asic_ram[0x2803] + (amstrad_plus_asic_ram[0x2802] << 8));
1887   }
1888   if(offset == 0x0804)  // Soft scroll register
1889   {
1890   }
1891   if(offset == 0x0805)  // Interrupt vector register (for IM 2, used by Pang)
1892   {
1893      // high 5 bits go to interrupt vector
1894      int vector;
1895
1896      if ( asic.enabled )
1897      {
1898         vector = (data & 0xf8) + (amstrad_plus_irq_cause);
1899         cpu_set_input_line_vector(space->machine->cpu[0], 0, vector);
1900         logerror("ASIC: IM 2 vector write %02x, data = &%02x\n",vector,data);
1901      }
1902      asic.dma_clear = data & 0x01;
1903   }
1904   // DMA channels
1905   switch(offset)
1906   {
1907   case 0x0c00:
1908   case 0x0c01:
1909      asic.dma_addr[0] = (amstrad_plus_asic_ram[0x2c01] << 8) + amstrad_plus_asic_ram[0x2c00];
1910      asic.dma_status &= ~0x01;
1911      logerror("ASIC: DMA 0 address set to &%04x\n",asic.dma_addr[0]);
1912      break;
1913   case 0x0c04:
1914   case 0x0c05:
1915      asic.dma_addr[1] = (amstrad_plus_asic_ram[0x2c05] << 8) + amstrad_plus_asic_ram[0x2c04];
1916      asic.dma_status &= ~0x02;
1917      logerror("ASIC: DMA 1 address set to &%04x\n",asic.dma_addr[1]);
1918      break;
1919   case 0x0c08:
1920   case 0x0c09:
1921      asic.dma_addr[2] = (amstrad_plus_asic_ram[0x2c09] << 8) + amstrad_plus_asic_ram[0x2c08];
1922      asic.dma_status &= ~0x04;
1923      logerror("ASIC: DMA 2 address set to &%04x\n",asic.dma_addr[2]);
1924      break;
1925   case 0x0c02:
1926      asic.dma_prescaler[0] = data + 1;
1927      logerror("ASIC: DMA 0 pause prescaler set to %i\n",data);
1928      break;
1929   case 0x0c06:
1930      asic.dma_prescaler[1] = data + 1;
1931      logerror("ASIC: DMA 1 pause prescaler set to %i\n",data);
1932      break;
1933   case 0x0c0a:
1934      asic.dma_prescaler[2] = data + 1;
1935      logerror("ASIC: DMA 2 pause prescaler set to %i\n",data);
1936      break;
1937   case 0x0c0f:
1938      asic.dma_status = data;
1939      logerror("ASIC: DMA status write - %02x\n",data);
1940      if(data & 0x40)
1941      {
1942         logerror("ASIC: DMA 0 IRQ acknowledge\n");
1943         cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
1944         amstrad_plus_irq_cause = 0x06;
1945         amstrad_plus_asic_ram[0x2c0f] &= ~0x40;
1946      }
1947      if(data & 0x20)
1948      {
1949         logerror("ASIC: DMA 1 IRQ acknowledge\n");
1950         cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
1951         amstrad_plus_irq_cause = 0x06;
1952         amstrad_plus_asic_ram[0x2c0f] &= ~0x20;
1953      }
1954      if(data & 0x10)
1955      {
1956         logerror("ASIC: DMA 2 IRQ acknowledge\n");
1957         cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
1958         amstrad_plus_irq_cause = 0x06;
1959         amstrad_plus_asic_ram[0x2c0f] &= ~0x10;
1960      }
1961      break;
1962   }
1963}
1964
1965
1966READ8_HANDLER( amstrad_plus_asic_4000_r )
1967{
1968//  logerror("RAM: read from &%04x\n",offset+0x4000);
1969   return amstrad_plus_asic_ram[offset];
1970}
1971
1972
1973READ8_HANDLER( amstrad_plus_asic_6000_r )
1974{
1975//  logerror("RAM: read from &%04x\n",offset+0x6000);
1976   // Analogue ports
1977   if(offset == 0x0808)
1978   {
1979      return (input_port_read(space->machine, "analog1") & 0x3f);
1980   }
1981   if(offset == 0x0809)
1982   {
1983      return (input_port_read(space->machine, "analog2") & 0x3f);
1984   }
1985   if(offset == 0x080a)
1986   {
1987      return (input_port_read(space->machine, "analog3") & 0x3f);
1988   }
1989   if(offset == 0x080b)
1990   {
1991      return (input_port_read(space->machine, "analog4") & 0x3f);
1992   }
1993   if(offset == 0x080c || offset == 0x080e)
1994   {
1995      return 0x3f;
1996   }
1997   if(offset == 0x080d || offset == 0x080f)
1998   {
1999      return 0x00;
2000   }
2001/*      if(offset == 0x0c0f)  // DMA status and control
2002        {
2003            int result = 0;
2004            if(amstrad_plus_irq_cause == 0x00)
2005                result |= 0x40;
2006            if(amstrad_plus_irq_cause == 0x02)
2007                result |= 0x20;
2008            if(amstrad_plus_irq_cause == 0x04)
2009                result |= 0x10;
2010            if(amstrad_plus_irq_cause == 0x06)
2011                result |= 0x80;
2012            return result;
2013        }
2014*/
2015   return amstrad_plus_asic_ram[offset+0x2000];
2016}
2017
2018
2019/* used for loading snapshot only ! */
2020static void AmstradCPC_PALWrite(running_machine *machine, int data)
2021{
2022   if ((data & 0x0c0)==0x0c0)
2023   {
2024      amstrad_GateArray_RamConfiguration = data;
2025      AmstradCPC_GA_SetRamConfiguration(machine);
2026   }
2027}
2028
2029
2030/* -------------------
2031   -  the Gate Array -
2032   -------------------
2033The gate array is controlled by I/O. The recommended I/O port address is &7Fxx.
2034The gate array is selected when bit 15 of the I/O port address is set to "0" and bit 14 of the I/O port address is set to "1".
2035The values of the other bits are ignored.
2036However, to avoid conflict with other devices in the system, these bits should be set to "1".
2037
2038The function to be performed is selected by writing data to the Gate-Array, bit 7 and 6 of the data define the function selected (see table below).
2039It is not possible to read from the Gate-Array.
2040
2041Bit 7 Bit 6 Function
20420     0     Select pen
20430     1     Select colour for selected pen
20441     0     Select screen mode, rom configuration and interrupt control
20451     1     Ram Memory Management (note 1)
2046
2047Note 1 : This function is not available in the Gate-Array, but is performed by a device at the same I/O port address location. In the CPC464,CPC664 and KC compact, this function is performed in a memory-expansion (e.g. Dk'Tronics 64K Ram Expansion), if this expansion is not present then the function is not available. In the CPC6128, this function is performed by a PAL located on the main PCB, or a memory-expansion. In the 464+ and 6128+ this function is performed by the ASIC or a memory expansion. Please read the document on Ram Management for more information.*/
2048
2049
2050static void amstrad_GateArray_write(running_machine *machine, UINT8 dataToGateArray)
2051{
2052/* Get Bit 7 and 6 of the dataToGateArray = Gate Array function selected */
2053   switch ((dataToGateArray & 0xc0)>>6) {
2054/* Pen selection
2055   -------------
2056Bit Value Function        Bit Value Function
20575   x     not used        5   x     not used
20584   1     Select border   4   0     Select pen
20593   x     | ignored       3   x     | Pen Number
20602   x     |               2   x       |
20611   x     |               1   x     |
20620   x     |               0   x     |
2063*/
2064     case 0x00:
2065      /* Select Border Number, get b4 */
2066      /* if b4 = 0 : Select Pen Number, get b3-b0 */
2067      gate_array.pen_selected = ( dataToGateArray & 0x10 ) ? 0x10 : ( dataToGateArray & 0x0f );
2068      break;
2069
2070/* Colour selection
2071   ----------------
2072Even though there is provision for 32 colours, only 27 are possible.
2073The remaining colours are duplicates of those already in the colour palette.
2074
2075Bit Value Function
20765   x     not used
20774   x     | Colour number
20783   x     |
20792   x     |
20801   x     |
20810   x     |*/
2082   case 0x01:
2083      video_screen_update_partial( machine->primary_screen, video_screen_get_vpos( machine->primary_screen ) );
2084      amstrad_vh_update_colour( gate_array.pen_selected, (dataToGateArray & 0x1F));
2085      break;
2086
2087/* Select screen mode and rom configuration
2088   ----------------------------------------
2089Bit Value Function
20905   x     not used
20914   x     Interrupt generation control
20923   1     Upper rom area disable or 0 Upper rom area enable
20932   1     Lower rom area disable or 0 Lower rom area enable
20941   x     | Mode selection
20950   x     |
2096
2097Screen mode selection : The settings for bits 1 and 0 and the corresponding screen mode are given in the table below.
2098-----------------------
2099b1 b0 Screen mode
21000  0  Mode 0, 160x200 resolution, 16 colours
21010  1  Mode 1, 320x200 resolution, 4 colours
21021  0  Mode 2, 640x200 resolution, 2 colours
21031  1  Mode 3, 160x200 resolution, 4 colours (note 1)
2104
2105This mode is not official. From the combinations possible, we can see that 4 modes can be defined, although the Amstrad only has 3. Mode 3 is similar to mode 0, because it has the same resolution, but it is limited to only 4 colours.
2106Mode changing is synchronised with HSYNC. If the mode is changed, it will take effect from the next HSYNC.
2107
2108Rom configuration selection :
2109-----------------------------
2110Bit 2 is used to enable or disable the lower rom area. The lower rom area occupies memory addressess &0000-&3fff and is used to access the operating system rom. When the lower rom area is is enabled, reading from &0000-&3FFF will return data in the rom. When a value is written to &0000-&3FFF, it will be written to the ram underneath the rom. When it is disabled, data read from &0000-&3FFF will return the data in the ram.
2111Similarly, bit 3 controls enabling or disabling of the upper rom area. The upper rom area occupies memory addressess &C000-&FFFF and is BASIC or any expansion roms which may be plugged into a rom board/box. See the document on upper rom selection for more details. When the upper rom area enabled, reading from &c000-&ffff, will return data in the rom. When data is written to &c000-&FFFF, it will be written to the ram at the same address as the rom. When the upper rom area is disabled, and data is read from &c000-&ffff the data returned will be the data in the ram.
2112
2113Bit 4 controls the interrupt generation. It can be used to delay interrupts.*/
2114   case 0x02:
2115      /* If bit 5 is enabled on a CPC Plus/GX4000 when the ASIC is unlocked, sets the lower ROM position and cart bank
2116           b5 = 1, b4b3 = RAM position for lower ROM area and if the ASIC registers are visible at &4000,
2117           b2b1b0 = cartridge bank to read from lower ROM area (0-7 only) */
2118      if ( amstrad_system_type == SYSTEM_PLUS || amstrad_system_type == SYSTEM_GX4000 )
2119      {
2120         if ( asic.enabled && (dataToGateArray & 0x20) )
2121         {
2122            asic.rmr2 = dataToGateArray;
2123         }
2124         else
2125         {
2126            gate_array.mrer = dataToGateArray;
2127         }
2128      }
2129      else
2130      {
2131         gate_array.mrer = dataToGateArray;
2132      }
2133
2134      /* If bit 4 of the "Select screen mode and rom configuration" register of the Gate-Array is set to "1"
2135       then the interrupt request is cleared and the 6-bit counter is reset to "0".  */
2136      if ( gate_array.mrer & 0x10 )
2137      {
2138         amstrad_CRTC_HS_Counter = 0;
2139         cpu_set_input_line(machine->cpu[0], 0, CLEAR_LINE);
2140      }
2141
2142      /* b3b2 != 0 then change the state of upper or lower rom area and rethink memory */
2143      amstrad_setLowerRom(machine);
2144      amstrad_setUpperRom(machine);
2145
2146      /* b1b0 mode */
2147      video_screen_update_partial( machine->primary_screen, video_screen_get_vpos( machine->primary_screen ) );
2148      amstrad_vh_update_mode();
2149
2150      break;
2151
2152/* Ram Memory Management
2153     ---------------------
2154This function is not available in the Gate-Array, but is performed by a device at the same I/O port address location.
2155In the CPC464,CPC664 and KC compact, this function is performed in a memory-expansion (e.g. Dk'Tronics 64K Ram Expansion), if this expansion is not present then the function is not available.
2156In the CPC6128, this function is performed by a PAL located on the main PCB, or a memory-expansion.
2157In the 464+ and 6128+ this function is performed by the ASIC or a memory expansion.
2158*/
2159   case 0x03:
2160      amstrad_GateArray_RamConfiguration = dataToGateArray;
2161      break;
2162
2163   default:
2164      break;
2165   }
2166}
2167
2168
2169static WRITE8_HANDLER( aleste_msx_mapper )
2170{
2171   int page = (offset & 0x0300) >> 8;
2172   int ramptr = (data & 0x1f) * 0x4000;
2173   int rampage = data & 0x1f;
2174   int function = (data & 0xc0) >> 6;
2175
2176   // It is assumed that functions are all mapped to each port &7cff-&7fff, and b8 and b9 are only used for RAM bank location
2177   switch(function)
2178   {
2179   case 0:  // Pen select (same as Gate Array?)
2180      amstrad_GateArray_write(space->machine, data);
2181      break;
2182   case 1:  // Colour select (6-bit palette)
2183      aleste_vh_update_colour( gate_array.pen_selected, data & 0x3f );
2184      break;
2185   case 2:  // Screen mode, Upper/Lower ROM select
2186      amstrad_GateArray_write(space->machine, data);
2187      break;
2188   case 3: // RAM banks
2189      switch(page)
2190      {
2191      case 0:  /* 0x0000 - 0x3fff */
2192         memory_set_bankptr(space->machine,1,mess_ram+ramptr);
2193         memory_set_bankptr(space->machine,2,mess_ram+ramptr+0x2000);
2194         memory_set_bankptr(space->machine,9,mess_ram+ramptr);
2195         memory_set_bankptr(space->machine,10,mess_ram+ramptr+0x2000);
2196         Aleste_RamBanks[0] = mess_ram+ramptr;
2197         aleste_active_page[0] = data;
2198         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x0000\n",ramptr,rampage);
2199         break;
2200      case 1:  /* 0x4000 - 0x7fff */
2201         memory_set_bankptr(space->machine,3,mess_ram+ramptr);
2202         memory_set_bankptr(space->machine,4,mess_ram+ramptr+0x2000);
2203         memory_set_bankptr(space->machine,11,mess_ram+ramptr);
2204         memory_set_bankptr(space->machine,12,mess_ram+ramptr+0x2000);
2205         Aleste_RamBanks[1] = mess_ram+ramptr;
2206         aleste_active_page[1] = data;
2207         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x4000\n",ramptr,rampage);
2208         break;
2209      case 2:  /* 0x8000 - 0xbfff */
2210         memory_set_bankptr(space->machine,5,mess_ram+ramptr);
2211         memory_set_bankptr(space->machine,6,mess_ram+ramptr+0x2000);
2212         memory_set_bankptr(space->machine,13,mess_ram+ramptr);
2213         memory_set_bankptr(space->machine,14,mess_ram+ramptr+0x2000);
2214         Aleste_RamBanks[2] = mess_ram+ramptr;
2215         aleste_active_page[2] = data;
2216         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x8000\n",ramptr,rampage);
2217         break;
2218      case 3:  /* 0xc000 - 0xffff */
2219         memory_set_bankptr(space->machine,7,mess_ram+ramptr);
2220         memory_set_bankptr(space->machine,8,mess_ram+ramptr+0x2000);
2221         memory_set_bankptr(space->machine,15,mess_ram+ramptr);
2222         memory_set_bankptr(space->machine,16,mess_ram+ramptr+0x2000);
2223         Aleste_RamBanks[3] = mess_ram+ramptr;
2224         aleste_active_page[3] = data;
2225         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0xc000\n",ramptr,rampage);
2226         break;
2227      }
2228      break;
2229   }
2230}
2231
2232
2233/* CRTC Differences
2234   ----------------
2235The following tables list the functions that can be accessed for each type:
2236
2237Type 0
2238b1 b0 Function Read/Write
22390  0  Select internal 6845 register Write Only
22400  1  Write to selected internal 6845 register Write Only
22411  0  - -
22421  1  Read from selected internal 6845 register Read only
2243
2244Type 1
2245b1 b0 Function Read/Write
22460  0  Select internal 6845 register Write Only
22470  1  Write to selected internal 6845 register Write Only
22481  0  Read Status Register Read Only
22491  1  Read from selected internal 6845 register Read only
2250
2251Type 2
2252b1 b0 Function Read/Write
22530  0  Select internal 6845 register Write Only
22540  1  Write to selected internal 6845 register Write Only
22551  0  - -
22561  1  Read from selected internal 6845 register Read only
2257
2258Type 3 and 4
2259b1 b0 Function Read/Write
22600  0  Select internal 6845 register Write Only
22610  1  Write to selected internal 6845 register Write Only
22621  0  Read from selected internal 6845 register Read Only
22631  1  Read from selected internal 6845 register Read only
2264*/
2265
2266/* I/O port allocation
2267   -------------------
2268
2269Many thanks to Mark Rison for providing the original information. Thankyou to Richard Wilson for his discoveries concerning RAM management I/O decoding.
2270
2271This document will explain the decoding of the I/O ports. The port address is not decoded fully which means a hardware device can be accessed through more than one address, in addition, using some addressess can access more than one element of the hardware at the same time. The CPC IN/OUT design differs from the norm in that port numbers are defined using 16 bits, as opposed to the traditional 8 bits.
2272
2273IN r,(C)/OUT (C),r instructions: Bits b15-b8 come from the B register, bits b7-b0 come from "r"
2274IN A,(n)/OUT (n),A instructions: Bits b15-b8 come from the A register, bits b7-b0 come from "n"
2275Listed below are the internal hardware devices and the bit fields to which they respond. In the table:
2276
2277"-" means this bit is ignored,
2278"0" means the bit must be set to "0" for the hardware device to respond,
2279"1" means the bit must be set to "1" for the hardware device to respond.
2280"r1" and "r0" mean a bit used to define a register
2281
2282Hardware device       Read/Write Port bits
2283                                 b15 b14 b13 b12 b11 b10 b9  b8  b7  b6  b5  b4  b3  b2  b1  b0
2284Gate-Array            Write Only 0   1   -   -   -   -   -   -   -   -   -   -   -   -   -   -
2285RAM Configuration     Write Only 0   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
2286CRTC                  Read/Write -   0   -   -   -   -   r1  r0  -   -   -   -   -   -   -   -
2287ROM select            Write only -   -   0   -   -   -   -   -   -   -   -   -   -   -   -   -
2288Printer port          Write only -   -   -   0   -   -   -   -   -   -   -   -   -   -   -   -
22898255 PPI              Read/Write -   -   -   -   0   -   r1  r0  -   -   -   -   -   -   -   -
2290Expansion Peripherals Read/Write -   -   -   -   -   0   -   -   -   -   -   -   -   -   -   -
2291
2292*/
2293
2294READ8_HANDLER ( amstrad_cpc_io_r )
2295{
2296   const device_config *fdc = (device_config*)devtag_get_device( space->machine, NEC765A, "nec765");
2297   const device_config *mc6845 = (device_config *) devtag_get_device( space->machine, MC6845, "mc6845" );
2298
2299   unsigned char data = 0xFF;
2300   unsigned int r1r0 = (unsigned int)((offset & 0x0300) >> 8);
2301//   m6845_personality_t crtc_type;
2302   int page;
2303
2304//   crtc_type = input_port_read_safe(space->machine, "crtc", 0);
2305//   m6845_set_personality(crtc_type);
2306
2307   if(aleste_mode & 0x04)
2308   {
2309      if ((offset & (1<<15)) == 0)  // Aleste Mapper is readable?
2310      {
2311         page = (offset & 0x0300) >> 8;
2312         data = aleste_active_page[page];
2313         return data;
2314      }
2315   }
2316
2317   /* if b14 = 0 : CRTC Read selected */
2318   if ((offset & (1<<14)) == 0)
2319   {
2320      switch(r1r0) {
2321      case 0x02:
2322         data = mc6845_status_r( mc6845, 0 );
2323//         /* CRTC Type 1 : Read Status Register
2324//               CRTC Type 3 or 4 : Read from selected internal 6845 register */
2325//         switch(crtc_type) {
2326//         case M6845_PERSONALITY_UM6845R:
2327//            data = amstrad_CRTC_CR; /* Read Status Register */
2328//            break;
2329//         case M6845_PERSONALITY_AMS40489:
2330//         case M6845_PERSONALITY_PREASIC:
2331//            data = m6845_register_r(0);
2332//            break;
2333//         default:
2334//            break;
2335//         }
2336         break;
2337      case 0x03:
2338         /* All CRTC type : Read from selected internal 6845 register Read only */
2339         data = mc6845_register_r( mc6845, 0 );
2340         break;
2341      }
2342   }
2343
2344/* if b11 = 0 : 8255 PPI Read selected - bits 9 and 8 then define the PPI function access as shown below:
2345
2346b9 b8 | PPI Function Read/Write status
23470  0  | Port A data  Read/Write
23480  1  | Port B data  Read/Write
23491  0  | Port C data  Read/Write
23501  1  | Control      Write Only
2351*/
2352   if ((offset & (1<<11)) == 0)
2353   {
2354      if (r1r0 < 0x03 )
2355         data = ppi8255_r((device_config*)devtag_get_device( space->machine, PPI8255, "ppi8255" ), r1r0);
2356   }
2357
2358/* if b10 = 0 : Expansion Peripherals Read selected
2359
2360bits b7-b5 are used to select an expansion peripheral. Again, this is done by resetting the peripheral's bit:
2361Bit | Device
2362b7  | FDC
2363b6  | Reserved (was it ever used?)
2364b5  | Serial port
2365
2366In the case of the FDC, bits b8 and b0 are used to select the specific mode of operation; all the other bits (b9,b4-b1) are ignored:
2367b8 b0 Function Read/Write state
23680 0 FDD motor control Write Only
23690 1 not used N/A
23701 0 Main Status register of FDC (MSR) Read Only
23711 1 Data register of FDC Read/Write
2372
2373If b10 is reset but none of b7-b5 are reset, user expansion peripherals are selected.
2374The exception is the case where none of b7-b0 are reset (i.e. port &FBFF), which causes the expansion peripherals to reset.
2375 */
2376   if ( amstrad_system_type != SYSTEM_GX4000 )
2377   {
2378       if ( ( offset & (1<<10) ) == 0 )
2379      {
2380         if ( ( offset & (1<<10) ) == 0 )
2381         {
2382            int b8b0 = ( ( offset & (1<<8) ) >> (8 - 1) ) | ( offset & 0x01 );
2383
2384            switch (b8b0)
2385            {
2386              case 0x02:
2387                 data = nec765_status_r(fdc, 0);
2388                 break;
2389              case 0x03:
2390                 data = nec765_data_r(fdc, 0);
2391                 break;
2392              default:
2393                 break;
2394            }
2395         }
2396      }
2397   }
2398   return data;
2399}
2400
2401
2402// Handler for checking the ASIC unlocking sequence
2403static void amstrad_plus_seqcheck(int data)
2404{
2405   static int prev_data;
2406
2407   if(data == 0 && prev_data != 0)
2408   {
2409      asic.seqptr = 0;  // non-zero value followed by zero will sync the locking mechanism
2410   }
2411   if(data == asic_unlock_seq[asic.seqptr])
2412   {
2413      asic.seqptr++;
2414      if ( asic.seqptr == 14 && asic.enabled )
2415      {
2416         logerror("SYS: ASIC locked\n");
2417         asic.enabled = 0;
2418      }
2419      if ( asic.seqptr >= 15 )  // end of sequence
2420      {
2421         logerror("SYS: ASIC unlocked\n");
2422         asic.enabled = 1;
2423      }
2424   }
2425   prev_data = data;
2426}
2427
2428
2429/* Offset handler for write */
2430WRITE8_HANDLER ( amstrad_cpc_io_w )
2431{
2432   device_config *fdc = (device_config*)devtag_get_device( space->machine, NEC765A, "nec765");
2433   device_config *mc6845 = (device_config *) devtag_get_device( space->machine, MC6845, "mc6845" );
2434
2435   if ((offset & (1<<15)) == 0)
2436   {
2437      if(aleste_mode & 0x04) // Aleste mode
2438      {
2439         aleste_msx_mapper(space, offset, data);
2440      }
2441      else
2442      {
2443         /* if b15 = 0 and b14 = 1 : Gate-Array Write Selected*/
2444         if ((offset & (1<<14)) != 0)
2445               amstrad_GateArray_write(space->machine, data);
2446
2447         /* if b15 = 0 : RAM Configuration Write Selected*/
2448         AmstradCPC_GA_SetRamConfiguration(space->machine);
2449      }
2450   }
2451
2452   /*The Gate-Array and CRTC can't be selected simultaneously, which would otherwise cause potential display corruption.*/
2453   /* if b14 = 0 : CRTC Write Selected*/
2454   if ((offset & (1<<14)) == 0)
2455   {
2456      switch ((offset & 0x0300) >> 8) // r1r0
2457      {
2458        case 0x00:      /* Select internal 6845 register Write Only */
2459         mc6845_address_w( mc6845, 0, data );
2460         if ( amstrad_system_type == SYSTEM_PLUS || amstrad_system_type == SYSTEM_GX4000 )
2461            amstrad_plus_seqcheck(data);
2462         break;
2463      case 0x01:      /* Write to selected internal 6845 register Write Only */
2464         video_screen_update_partial( space->machine->primary_screen, video_screen_get_vpos( space->machine->primary_screen ) );
2465         mc6845_register_w( mc6845, 0, data );
2466         break;
2467      default:
2468         break;
2469      }
2470   }
2471
2472   /* ROM select before GateArrayWrite ?*/
2473   /* b13 = 0 : ROM select Write Selected*/
2474   if ((offset & (1<<13)) == 0)
2475   {
2476      gate_array.upper_bank = data;
2477      amstrad_setUpperRom( space->machine );
2478   }
2479
2480   /* b12 = 0 : Printer port Write Selected*/
2481   if(amstrad_system_type != SYSTEM_GX4000)
2482   {
2483      if ((offset & (1<<12)) == 0)
2484      {
2485         /* on CPC, write to printer through LS chip */
2486         /* the amstrad is crippled with a 7-bit port :( */
2487         /* bit 7 of the data is the printer /strobe */
2488
2489         /* strobe state changed? */
2490         if (((previous_printer_data_byte^data) & (1<<7)) !=0 )
2491         {
2492            /* check for only one transition */
2493            if ((data & (1<<7)) == 0)
2494            {
2495               /* output data to printer */
2496               printer_output(printer_device(space->machine), data & 0x07f);
2497            }
2498         }
2499         previous_printer_data_byte = data;
2500      }
2501   }
2502
2503   /* if b11 = 0 : 8255 PPI Write selected - bits 9 and 8 then define the PPI function access as shown below:
2504   b9 b8 | PPI Function Read/Write status
2505   0  0  | Port A data  Read/Write
2506   0  1  | Port B data  Read/Write
2507   1  0  | Port C data  Read/Write
2508   1  1  | Control      Write Only
2509   */
2510   if ((offset & (1<<11)) == 0)
2511   {
2512      unsigned int Index = ((offset & 0x0300) >> 8);
2513
2514      ppi8255_w((device_config*)devtag_get_device( space->machine, PPI8255, "ppi8255" ), Index, data);
2515   }
2516
2517   /* if b10 = 0 : Expansion Peripherals Write selected */
2518   if ((offset & (1<<10)) == 0)
2519   {
2520/* bits b7-b5 are used to select an expansion peripheral. This is done by resetting the peripheral's bit:
2521Bit | Device
2522b7  | FDC
2523b6  | Reserved (was it ever used?)
2524b5  | Serial port
2525
2526In the case of the FDC, bits b8 and b0 are used to select the specific mode of operation;
2527all the other bits (b9,b4-b1) are ignored:
2528b8 b0 Function Read/Write state
25290 0 FDD motor control Write Only
25300 1 not used N/A
25311 0 Main Status register of FDC (MSR) Read Only
25321 1 Data register of FDC Read/Write
2533
2534If b10 is reset but none of b7-b5 are reset, user expansion peripherals are selected.
2535The exception is the case where none of b7-b0 are reset (i.e. port &FBFF), which causes the expansion peripherals to reset.
2536*/
2537      if(amstrad_system_type != SYSTEM_GX4000)
2538      {
2539         if ((offset & (1<<7)) == 0)
2540         {
2541            unsigned int b8b0 = ((offset & 0x0100) >> (8 - 1)) | (offset & 0x01);
2542
2543            switch (b8b0)
2544            {
2545            case 0x00:
2546               /* FDC Motor Control - Bit 0 defines the state of the FDD motor:
2547                * "1" the FDD motor will be active.
2548                * "0" the FDD motor will be in-active.*/
2549               floppy_drive_set_motor_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 0), (data & 0x01));
2550               floppy_drive_set_motor_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 1), (data & 0x01));
2551               floppy_drive_set_ready_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 0), 1,1);
2552               floppy_drive_set_ready_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 1), 1,1);
2553              break;
2554
2555            case 0x03: /* Write Data register of FDC */
2556               nec765_data_w(fdc, 0,data);
2557               break;
2558
2559            default:
2560               break;
2561            }
2562         }
2563      }
2564   }
2565
2566   /*   Aleste Extend Port:
2567      D0 - VRAM Bank 0/1 (64K)
2568      D1 - MODE 0-Norm ,1-High resolution
2569      D2 - MAPMOD 0-Amstrad ,1-Yamaha mapper
2570      D3 - MAP Page 0/1 (256K)
2571      D4 - 580WI53
2572      D5 - 0-AY8910 ,1-512WI1 */
2573   if(offset == 0xfabf)
2574   {
2575      aleste_mode = data;
2576      logerror("EXTEND: Port &FABF write 0x%02x\n",data);
2577      mc6845_set_clock( mc6845, ( aleste_mode & 0x02 ) ? ( XTAL_16MHz / 8 ) : ( XTAL_16MHz / 16 ) );
2578   }
2579
2580   multiface_io_write(space, offset, data);
2581}
2582
2583
382584/* load CPCEMU style snapshots */
39void amstrad_handle_snapshot(running_machine *machine, unsigned char *pSnapshot)
2585static void amstrad_handle_snapshot(running_machine *machine, unsigned char *pSnapshot)
402586{
412587   const address_space* space = cpu_get_address_space(machine->cpu[0],ADDRESS_SPACE_PROGRAM);
2588   const device_config *mc6845 = (device_config *) devtag_get_device( space->machine, MC6845, "mc6845" );
422589   int RegData;
432590   int i;
442591
r4153r4154
1262673   /* init CRTC */
1272674   for (i=0; i<18; i++)
1282675   {
129                m6845_address_w(0,i);
130                m6845_register_w(0, pSnapshot[0x043+i] & 0x0ff);
2676      mc6845_address_w( mc6845, 0, i );
2677      mc6845_register_w( mc6845, 0, pSnapshot[0x043+i] & 0xff );
1312678   }
1322679
133   m6845_address_w(0,i);
2680   mc6845_address_w( mc6845, 0, i );
1342681
1352682   /* upper rom selection */
136   AmstradCPC_SetUpperRom(machine, pSnapshot[0x055]);
2683   gate_array.upper_bank = pSnapshot[0x055];
1372684
1382685   /* PPI */
1392686   ppi8255_w((device_config*)devtag_get_device(machine, PPI8255, "ppi8255"),3,pSnapshot[0x059] & 0x0ff);
r4153r4154
1722719   amstrad_rethinkMemory(machine);
1732720}
1742721
2722
2723/* sets up for a machine reset
2724All hardware is reset and the firmware is completely initialized
2725Once all tables and jumpblocks have been set up,
2726control is passed to the default entry in rom 0*/
2727static void amstrad_reset_machine(running_machine *machine)
2728{
2729   /* enable lower rom (OS rom) */
2730   amstrad_GateArray_write(machine, 0x089);
2731
2732   /* set ram config 0 */
2733   amstrad_GateArray_write(machine, 0x0c0);
2734
2735  // Get manufacturer name and TV refresh rate from PCB link (dipswitch for mess emulation)
2736   ppi_port_inputs[amstrad_ppi_PortB] = (((input_port_read(machine, "solder_links")&MANUFACTURER_NAME)<<1) | (input_port_read(machine, "solder_links")&TV_REFRESH_RATE));
2737
2738   if ( amstrad_system_type == SYSTEM_PLUS || amstrad_system_type == SYSTEM_GX4000 )
2739      memset(amstrad_plus_asic_ram,0,16384);  // clear ASIC RAM
2740
2741   multiface_reset();
2742}
2743
2744
2745/*------------------
2746  - Rethink Memory -
2747  ------------------*/
2748static void amstrad_rethinkMemory(running_machine *machine)
2749{
2750   /* the following is used for banked memory read/writes and for setting up
2751     * opcode and opcode argument reads */
2752
2753   /* bank 0 - 0x0000..0x03fff */
2754   amstrad_setLowerRom(machine);
2755
2756   /* bank 1 - 0x04000..0x07fff */
2757   if ( amstrad_system_type == SYSTEM_CPC || amstrad_system_type == SYSTEM_ALESTE || asic.enabled == 0 )
2758   {
2759      if(aleste_mode & 0x04)
2760      {
2761         memory_set_bankptr(machine,3, Aleste_RamBanks[1]);
2762         memory_set_bankptr(machine,4, Aleste_RamBanks[1]+0x2000);
2763         /* bank 2 - 0x08000..0x0bfff */
2764         memory_set_bankptr(machine,5, Aleste_RamBanks[2]);
2765         memory_set_bankptr(machine,6, Aleste_RamBanks[2]+0x2000);
2766      }
2767      else
2768      {
2769         memory_set_bankptr(machine,3, AmstradCPC_RamBanks[1]);
2770         memory_set_bankptr(machine,4, AmstradCPC_RamBanks[1]+0x2000);
2771         /* bank 2 - 0x08000..0x0bfff */
2772         memory_set_bankptr(machine,5, AmstradCPC_RamBanks[2]);
2773         memory_set_bankptr(machine,6, AmstradCPC_RamBanks[2]+0x2000);
2774      }
2775   }
2776   else
2777      amstrad_setLowerRom(machine);
2778
2779   /* bank 3 - 0x0c000..0x0ffff */
2780   amstrad_setUpperRom(machine);
2781
2782   /* other banks */
2783   if(aleste_mode & 0x04)
2784   {
2785      memory_set_bankptr(machine,9, Aleste_RamBanks[0]);
2786      memory_set_bankptr(machine,10, Aleste_RamBanks[0]+0x2000);
2787      memory_set_bankptr(machine,11, Aleste_RamBanks[1]);
2788      memory_set_bankptr(machine,12, Aleste_RamBanks[1]+0x2000);
2789      memory_set_bankptr(machine,13, Aleste_RamBanks[2]);
2790      memory_set_bankptr(machine,14, Aleste_RamBanks[2]+0x2000);
2791      memory_set_bankptr(machine,15, Aleste_RamBanks[3]);
2792      memory_set_bankptr(machine,16, Aleste_RamBanks[3]+0x2000);
2793   }
2794   else
2795   {
2796      memory_set_bankptr(machine,9, AmstradCPC_RamBanks[0]);
2797      memory_set_bankptr(machine,10, AmstradCPC_RamBanks[0]+0x2000);
2798      memory_set_bankptr(machine,11, AmstradCPC_RamBanks[1]);
2799      memory_set_bankptr(machine,12, AmstradCPC_RamBanks[1]+0x2000);
2800      memory_set_bankptr(machine,13, AmstradCPC_RamBanks[2]);
2801      memory_set_bankptr(machine,14, AmstradCPC_RamBanks[2]+0x2000);
2802      memory_set_bankptr(machine,15, AmstradCPC_RamBanks[3]);
2803      memory_set_bankptr(machine,16, AmstradCPC_RamBanks[3]+0x2000);
2804   }
2805
2806   /* multiface hardware enabled? */
2807   if (multiface_hardware_enabled(machine)) {
2808      multiface_rethink_memory(machine);
2809   }
2810}
2811
2812
2813static void kccomp_reset_machine(running_machine *machine)
2814{
2815   /* enable lower rom (OS rom) */
2816   amstrad_GateArray_write(machine, 0x089);
2817
2818   /* set ram config 0 */
2819   amstrad_GateArray_write(machine, 0x0c0);
2820}
2821
2822
2823VIDEO_EOF( amstrad )
2824{
2825   if (input_port_read_safe(machine, "multiface", 0) & 0x02)
2826   {
2827      multiface_stop(machine);
2828   }
2829}
2830
2831
2832/* ---------------------------------------
2833   - 27.05.2004 - PSG function selection -
2834   ---------------------------------------
2835The databus of the PSG is connected to PPI Port A.
2836Data is read from/written to the PSG through this port.
2837
2838The PSG function, defined by the BC1,BC2 and BDIR signals, is controlled by bit 7 and bit 6 of PPI Port C.
2839
2840PSG function selection:
2841-----------------------
2842Function
2843
2844BDIR = PPI Port C Bit 7 and BC1 = PPI Port C Bit 6
2845
2846PPI Port C Bit | PSG Function
2847BDIR BC1       |
28480    0         | Inactive
28490    1         | Read from selected PSG register. When function is set, the PSG will make the register data available to PPI Port A
28501    0         | Write to selected PSG register. When set, the PSG will take the data at PPI Port A and write it into the selected PSG register
28511    1         | Select PSG register. When set, the PSG will take the data at PPI Port A and select a register
2852*/
2853/* PSG function selected */
2854static unsigned char amstrad_Psg_FunctionSelected;
2855static void update_psg(running_machine *machine)
2856{
2857   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
2858   
2859   if(aleste_mode & 0x20)  // RTC selected
2860   {
2861      switch(aleste_rtc_function)
2862      {
2863      case 0x02:  // AS
2864         mc146818_port_w(space, 0,ppi_port_outputs[amstrad_ppi_PortA]);
2865         break;
2866      case 0x04:  // DS write
2867         mc146818_port_w(space, 1,ppi_port_outputs[amstrad_ppi_PortA]);
2868         break;
2869      case 0x05:  // DS read
2870         ppi_port_inputs[amstrad_ppi_PortA] = mc146818_port_r(space, 1);
2871         break;
2872      }
2873      return;
2874   }
2875  switch (amstrad_Psg_FunctionSelected)
2876  {
2877     case 0:
2878      {/* Inactive */
2879      } break;
2880     case 1:
2881      {/* b6 = 1 ? : Read from selected PSG register and make the register data available to PPI Port A */
2882           ppi_port_inputs[amstrad_ppi_PortA] = ay8910_read_port_0_r(space, 0);
2883        }
2884      break;
2885     case 2:
2886      {/* b7 = 1 ? : Write to selected PSG register and write data to PPI Port A */
2887           ay8910_write_port_0_w(space, 0, ppi_port_outputs[amstrad_ppi_PortA]);
2888        }
2889      break;
2890     case 3:
2891      {/* b6 and b7 = 1 ? : The register will now be selected and the user can read from or write to it.  The register will remain selected until another is chosen.*/
2892           ay8910_control_port_0_w(space, 0, ppi_port_outputs[amstrad_ppi_PortA]);
2893         prev_reg = ppi_port_outputs[amstrad_ppi_PortA];
2894        }
2895      break;
2896     default: {
2897    } break;
2898   }
2899}
2900
2901
2902/* Read/Write 8255 PPI port A (connected to AY-3-8912 databus) */
2903READ8_DEVICE_HANDLER ( amstrad_ppi_porta_r )
2904{
2905   update_psg(device->machine);
2906   return ppi_port_inputs[amstrad_ppi_PortA];
2907}
2908
2909
2910WRITE8_DEVICE_HANDLER ( amstrad_ppi_porta_w )
2911{
2912   ppi_port_outputs[amstrad_ppi_PortA] = data;
2913   update_psg(device->machine);
2914}
2915
2916
2917/* - Read PPI Port B -
2918   -------------------
2919Bit Description
29207   Cassette read data
29216   Parallel/Printer port ready signal ("1" = not ready, "0" = Ready)
29225   /EXP signal on expansion port (note 6)
29234   50/60 Hz (link on PCB. For this MESS driver I have used the dipswitch feature) (note 5)
29243   | PCB links to define manufacturer name. For this MESS driver I have used the dipswitch feature. (note 1) (note 4)
29252   | (note 2)
29261   | (note 3)
29270   VSYNC State from 6845. "1" = VSYNC active, "0" = VSYNC inactive
2928
2929Note:
2930
29311 On CPC464,CPC664,CPC6128 and GX4000 this is LK3 on the PCB. On the CPC464+ and CPC6128+ this is LK103 on the PCB. On the KC compact this is "1".
29322 On CPC464,CPC664,CPC6128 and GX4000 this is LK2 on the PCB. On the CPC464+ and CPC6128+ this is LK102 on the PCB. On the KC compact this is "0".
29333 On CPC464,CPC664,CPC6128 and GX4000 this is LK1 on the PCB. On the CPC464+ and CPC6128+ this is LK101 on the PCB. On the KC compact this is /TEST signal from the expansion port.
29344 On the CPC464,CPC664,CPC6128,CPC464+,CPC6128+ and GX4000 bits 3,2 and 1 define the manufacturer name. See below to see the options available. The manufacturer name is defined on the PCB and cannot be changed through software.
29355 On the CPC464,CPC664,CPC6128,CPC464+,CPC6128+ and GX4000 bit 4 defines the Screen refresh frequency. "1" = 50 Hz, "0" = 60 Hz. This is defined on the PCB and cannot be changed with software. On the KC compact bit 4 is "1"
29366 This bit is connected to /EXP signal on the expansion port.
2937  On the KC Compact this bit is used to define bit 7 of the printer data.
2938  On the CPC, it is possible to use this bit to define bit 7 of the printer data, so a 8-bit printer port is made, with a hardware modification,
2939  On the CPC this can be used by a expansion device to report it's presence. "1" = device connected, "0" = device not connected. This is not always used by all expansion devices.
2940*/
2941
2942READ8_DEVICE_HANDLER (amstrad_ppi_portb_r)
2943{
2944   int data = 0;
2945/* Set b7 with cassette tape input */
2946   if(amstrad_system_type != SYSTEM_GX4000)
2947   {
2948      if (cassette_input(devtag_get_device( device->machine, CASSETTE, "cassette" )) > 0.03) {
2949         data |= (1<<7);
2950      }
2951   }
2952/* Set b6 with Parallel/Printer port ready */
2953   if(amstrad_system_type != SYSTEM_GX4000)
2954   {
2955      if (printer_is_ready(printer_device(device->machine))==0 ) {
2956         data |= (1<<6);
2957      }
2958   }
2959/* Set b4-b1 50Hz/60Hz state and manufacturer name defined by links on PCB */
2960   data |= (ppi_port_inputs[amstrad_ppi_PortB] & 0x1e);
2961
2962/*  Set b0 with VSync state from the CRTC */
2963   data |= amstrad_CRTC_VS;
2964
2965   if(aleste_mode & 0x04)
2966   {
2967      if(aleste_fdc_int == 0)
2968         data &= ~0x02;
2969      else
2970         data |= 0x02;
2971   }
2972
2973   return data;
2974}
2975
2976
2977/* 26-May-2005 - PPI Port C
2978   -----------------------
2979Bit Description  Usage
29807   PSG BDIR     | PSG function selection
29816   PSG BC1      |
29825                Cassette Write data
29834                Cassette Motor Control set bit to "1" for motor on, or "0" for motor off
29843                | Keyboard line Select keyboard line to be scanned (0-15)
29852                |
29861                |
29870                |*/
2988
2989/* previous_ppi_portc_w value */
2990static int previous_ppi_portc_w;
2991
2992WRITE8_DEVICE_HANDLER ( amstrad_ppi_portc_w )
2993{
2994   int changed_data;
2995
2996   previous_ppi_portc_w = ppi_port_outputs[amstrad_ppi_PortC];
2997/* Write the data to Port C */
2998   changed_data = previous_ppi_portc_w^data;
2999
3000   ppi_port_outputs[amstrad_ppi_PortC] = data;
3001
3002/* get b7 and b6 (PSG Function Selected */
3003   amstrad_Psg_FunctionSelected = ((data & 0xC0)>>6);
3004
3005   /* MC146818 function */
3006   aleste_rtc_function = data & 0x07;
3007
3008   /* Perform PSG function */
3009   update_psg(device->machine);
3010
3011   /* b5 Cassette Write data */
3012   if(amstrad_system_type != SYSTEM_GX4000)
3013   {
3014      if ((changed_data & 0x20) != 0) {
3015         cassette_output(devtag_get_device( device->machine, CASSETTE, "cassette" ),
3016            ((data & 0x20) ? -1.0 : +1.0));
3017      }
3018   }
3019
3020   /* b4 Cassette Motor Control */
3021   if(amstrad_system_type != SYSTEM_GX4000)
3022   {
3023      if ((changed_data & 0x10) != 0) {
3024         cassette_change_state(devtag_get_device( device->machine, CASSETTE, "cassette" ),
3025            ((data & 0x10) ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED),
3026            CASSETTE_MASK_MOTOR);
3027      }
3028   }
3029}
3030
3031
3032/* Additional notes for the AY-3-8912 in the CPC design
3033Port A of the AY-3-8912 is connected to the keyboard.
3034The data for a selected keyboard line can be read through Port A, as long as it is defined as input.
3035The operating system and I believe all programs assume this port has been defined as input. (NWC has found a bug in the Multiface 2 software. The Multiface does not reprogram the input/output state of the AY-3-8912's registers, therefore if port A is programmed as output, the keyboard will be unresponsive and it will not be possible to use the Multiface functions.)
3036When port B is defined as input (bit 7 of register 7 is set to "0"), a read of this port will return &FF.
3037*/
3038
3039/* read PSG port A */
3040READ8_HANDLER ( amstrad_psg_porta_read )
3041{
3042   /* Read CPC Keyboard
3043   If keyboard matrix line 11-14 are selected, the byte is always &ff.
3044   After testing on a real CPC, it is found that these never change, they always return &FF. */
3045
3046   static const char *const keynames[] = { "keyboard_row_0", "keyboard_row_1", "keyboard_row_2", "keyboard_row_3", "keyboard_row_4",
3047                              "keyboard_row_5", "keyboard_row_6", "keyboard_row_7", "keyboard_row_8", "keyboard_row_9",
3048                              "keyboard_row_10" };
3049
3050   if ( ( ppi_port_outputs[amstrad_ppi_PortC] & 0x0F ) > 10)
3051   {
3052      return 0xFF;
3053   }
3054   else
3055   {
3056      if(aleste_mode == 0x08 && ( ppi_port_outputs[amstrad_ppi_PortC] & 0x0F ) == 10)
3057         return 0xff;
3058
3059      return input_port_read_safe(space->machine, keynames[ppi_port_outputs[amstrad_ppi_PortC] & 0x0F], 0) & 0xFF;
3060   }
3061}
3062
3063
3064/* called when cpu acknowledges int */
3065/* reset top bit of interrupt line counter */
3066/* this ensures that the next interrupt is no closer than 32 lines */
3067static IRQ_CALLBACK(amstrad_cpu_acknowledge_int)
3068{
3069   // DMA interrupts can be automatically cleared if bit 0 of &6805 is set to 0
3070   if( asic.enabled && amstrad_plus_irq_cause != 0x06 && asic.dma_clear & 0x01)
3071   {
3072      logerror("IRQ: Not cleared, IRQ was called by DMA [%i]\n",amstrad_plus_irq_cause);
3073      amstrad_plus_asic_ram[0x2c0f] &= ~0x80;  // not a raster interrupt, so this bit is reset
3074      return (amstrad_plus_asic_ram[0x2805] & 0xf8) | amstrad_plus_irq_cause;
3075   }
3076   cpu_set_input_line(device->machine->cpu[0],0, CLEAR_LINE);
3077   amstrad_CRTC_HS_Counter &= 0x1F;
3078   if ( asic.enabled )
3079   {
3080      if(amstrad_plus_irq_cause == 6)  // bit 7 is set "if last interrupt acknowledge cycle was caused by a raster interrupt"
3081         amstrad_plus_asic_ram[0x2c0f] |= 0x80;
3082      else
3083      {
3084         amstrad_plus_asic_ram[0x2c0f] &= ~0x80;
3085         amstrad_plus_asic_ram[0x2c0f] &= (0x40 >> amstrad_plus_irq_cause/2);
3086      }
3087      return (amstrad_plus_asic_ram[0x2805] & 0xf8) | amstrad_plus_irq_cause;
3088   }
3089   return 0xFF;
3090}
3091
3092
3093/* the following timings have been measured! */
3094static const UINT8 amstrad_cycle_table_op[256] = {
3095    4, 12,  8,  8,  4,  4,  8,  4,  4, 12,  8,  8,  4,  4,  8,  4,
3096   12, 12,  8,  8,  4,  4,  8,  4, 12, 12,  8,  8,  4,  4,  8,  4,
3097    8, 12, 20,  8,  4,  4,  8,  4,  8, 12, 20,  8,  4,  4,  8,  4,
3098    8, 12, 16,  8, 12, 12, 12,  4,  8, 12, 16,  8,  4,  4,  8,  4,
3099    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3100    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3101    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3102    8,  8,  8,  8,  8,  8,  4,  8,  4,  4,  4,  4,  4,  4,  8,  4,
3103    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3104    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3105    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3106    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3107    8, 12, 12, 12, 12, 16,  8, 16,  8, 12, 12,  4, 12, 20,  8, 16,
3108    8, 12, 12, 12, 12, 16,  8, 16,  8,  4, 12, 12, 12,  4,  8, 16,
3109    8, 12, 12, 24, 12, 16,  8, 16,  8,  4, 12,  4, 12,  4,  8, 16,
3110    8, 12, 12,  4, 12, 16,  8, 16,  8,  8, 12,  4, 12,  4,  8, 16
3111};
3112
3113static const UINT8 amstrad_cycle_table_cb[256]=
3114{
3115    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3116    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3117    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3118    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3119    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3120    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3121    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3122    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
3123    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3124    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3125    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3126    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3127    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3128    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3129    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
3130    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4
3131};
3132
3133static const UINT8 amstrad_cycle_table_ed[256]=
3134{
3135    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3136    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3137    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3138    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3139   12, 12, 12, 20,  4, 12,  4,  8, 12, 12, 12, 20,  4, 12,  4,  8,
3140   12, 12, 12, 20,  4, 12,  4,  8, 12, 12, 12, 20,  4, 12,  4,  8,
3141   12, 12, 12, 20,  4, 12,  4, 16, 12, 12, 12, 20,  4, 12,  4, 16,
3142   12, 12, 12, 20,  4, 12,  4,  4, 12, 12, 12, 20,  4, 12,  4,  4,
3143    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3144    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3145   16, 12, 16, 16,  4,  4,  4,  4, 16, 12, 16, 16,  4,  4,  4,  4,
3146   16, 12, 16, 16,  4,  4,  4,  4, 16, 12, 16, 16,  4,  4,  4,  4,
3147    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3148    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3149    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
3150    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4
3151};
3152
3153static const UINT8 amstrad_cycle_table_xy[256]=
3154{
3155    4, 12,  8,  8,  4,  4,  8,  4,  4, 12,  8,  8,  4,  4,  8,  4,
3156   12, 12,  8,  8,  4,  4,  8,  4, 12, 12,  8,  8,  4,  4,  8,  4,
3157    8, 12, 20,  8,  4,  4,  8,  4,  8, 12, 20,  8,  4,  4,  8,  4,
3158    8, 12, 16,  8, 20, 20, 20,  4,  8, 12, 16,  8,  4,  4,  8,  4,
3159    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3160    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3161    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3162   16, 16, 16, 16, 16, 16,  4, 16,  4,  4,  4,  4,  4,  4, 16,  4,
3163    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3164    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3165    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3166    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
3167    8, 12, 12, 12, 12, 16,  8, 16,  8, 12, 12,  4, 12, 20,  8, 16,
3168    8, 12, 12, 12, 12, 16,  8, 16,  8,  4, 12, 12, 12,  4,  8, 16,
3169    8, 12, 12, 24, 12, 16,  8, 16,  8,  4, 12,  4, 12,  4,  8, 16,
3170    8, 12, 12,  4, 12, 16,  8, 16,  8,  8, 12,  4, 12,  4,  8, 16
3171};
3172
3173static const UINT8 amstrad_cycle_table_xycb[256]=
3174{
3175   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3176   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3177   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3178   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3179   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
3180   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
3181   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
3182   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
3183   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3184   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3185   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3186   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3187   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3188   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3189   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
3190   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20
3191};
3192
3193static const UINT8 amstrad_cycle_table_ex[256]=
3194{
3195    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3196    4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3197    4,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0,
3198    4,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0,
3199    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3200    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3201    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3202    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3203    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3204    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3205    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
3206    4,  8,  4,  4,  0,  0,  0,  0,  4,  8,  4,  4,  0,  0,  0,  0,
3207    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
3208    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
3209    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
3210    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0
3211};
3212
3213static void amstrad_common_init(running_machine *machine)
3214{
3215   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
3216   
3217   aleste_mode = 0;
3218
3219   gate_array.mrer = 0;
3220   amstrad_GateArray_RamConfiguration = 0;
3221   amstrad_CRTC_HS_Counter = 2;
3222
3223   memory_install_read8_handler(space, 0x0000, 0x1fff, 0, 0, SMH_BANK1);
3224   memory_install_read8_handler(space, 0x2000, 0x3fff, 0, 0, SMH_BANK2);
3225
3226   memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK3);
3227   memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK4);
3228
3229   memory_install_read8_handler(space, 0x8000, 0x9fff, 0, 0, SMH_BANK5);
3230   memory_install_read8_handler(space, 0xa000, 0xbfff, 0, 0, SMH_BANK6);
3231
3232   memory_install_read8_handler(space, 0xc000, 0xdfff, 0, 0, SMH_BANK7);
3233   memory_install_read8_handler(space, 0xe000, 0xffff, 0, 0, SMH_BANK8);
3234
3235   memory_install_write8_handler(space, 0x0000, 0x1fff, 0, 0, SMH_BANK9);
3236   memory_install_write8_handler(space, 0x2000, 0x3fff, 0, 0, SMH_BANK10);
3237
3238   memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK11);
3239   memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK12);
3240
3241   memory_install_write8_handler(space, 0x8000, 0x9fff, 0, 0, SMH_BANK13);
3242   memory_install_write8_handler(space, 0xa000, 0xbfff, 0, 0, SMH_BANK14);
3243
3244   memory_install_write8_handler(space, 0xc000, 0xdfff, 0, 0, SMH_BANK15);
3245   memory_install_write8_handler(space, 0xe000, 0xffff, 0, 0, SMH_BANK16);
3246
3247   device_reset(space->machine->cpu[0]);
3248   if ( amstrad_system_type == SYSTEM_CPC || amstrad_system_type == SYSTEM_ALESTE )
3249      cpu_set_input_line_vector(machine->cpu[0], 0,0xff);
3250   else
3251      cpu_set_input_line_vector(machine->cpu[0], 0,0x00);
3252
3253   if(amstrad_system_type != SYSTEM_GX4000)
3254   {
3255      floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 0),  FLOPPY_DRIVE_SS_40);
3256      floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 1),  FLOPPY_DRIVE_SS_40);
3257   }
3258
3259   /* The opcode timing in the Amstrad is different to the opcode
3260    timing in the core for the Z80 CPU.
3261
3262    The Amstrad hardware issues a HALT for each memory fetch.
3263    This has the effect of stretching the timing for Z80 opcodes,
3264    so that they are all multiple of 4 T states long. All opcode
3265    timings are a multiple of 1us in length. */
3266
3267   /* Using the cool code Juergen has provided, I will override
3268    the timing tables with the values for the amstrad */
3269   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_op, (void*)amstrad_cycle_table_op);
3270   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_cb, (void*)amstrad_cycle_table_cb);
3271   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_ed, (void*)amstrad_cycle_table_ed);
3272   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_xy, (void*)amstrad_cycle_table_xy);
3273   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_xycb, (void*)amstrad_cycle_table_xycb);
3274   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_ex, (void*)amstrad_cycle_table_ex);
3275
3276   /* Juergen is a cool dude! */
3277   cpu_set_irq_callback(machine->cpu[0], amstrad_cpu_acknowledge_int);
3278}
3279
3280
3281MACHINE_RESET( amstrad )
3282{
3283   int i;
3284   UINT8 *rom = memory_region(machine, "main");
3285
3286   amstrad_system_type = SYSTEM_CPC;
3287
3288   for (i=0; i<256; i++)
3289   {
3290      Amstrad_ROM_Table[i] = &rom[0x014000];
3291   }
3292
3293   Amstrad_ROM_Table[7] = &rom[0x018000];
3294   amstrad_common_init(machine);
3295   amstrad_reset_machine(machine);
3296//   amstrad_init_palette(machine);
3297   
3298   multiface_init();
3299
3300}
3301
3302
3303MACHINE_START( plus )
3304{
3305   amstrad_plus_asic_ram = memory_region(machine, "user1");  // 16kB RAM for ASIC, memory-mapped registers.
3306}
3307
3308
3309MACHINE_RESET( plus )
3310{
3311   int i;
3312   UINT8 *rom = memory_region(machine, "main");
3313
3314   amstrad_system_type = SYSTEM_PLUS;
3315
3316   for (i=0; i<128; i++)  // fill ROM table
3317   {
3318      Amstrad_ROM_Table[i] = &rom[0x4000];  // BASIC in system cart
3319   }
3320   for(i=128;i<160;i++)
3321   {
3322      Amstrad_ROM_Table[i] = &rom[(i-128)*0x4000];
3323   }
3324   Amstrad_ROM_Table[7] = &rom[0xc000];  // AMSDOS in system cart
3325
3326   asic.enabled = 0;
3327   asic.seqptr = 0;
3328   asic.pri = 0;  // disable PRI
3329   asic.dma_status = 0;  // disable all DMA channels
3330   asic.dma_addr[0] = 0;
3331   asic.dma_prescaler[0] = 0;
3332   asic.dma_addr[1] = 0;
3333   asic.dma_prescaler[1] = 0;
3334   asic.dma_addr[2] = 0;
3335   asic.dma_prescaler[2] = 0;
3336   asic.dma_clear = 1;  // by default, DMA interrupts must be cleared by writing to the DSCR (&6c0f)
3337   amstrad_plus_irq_cause = 6;
3338
3339   amstrad_common_init(machine);
3340   amstrad_reset_machine(machine);
3341   amstrad_plus_asic_ram[0x2805] = 0x01;  // interrupt vector is undefined at startup, except that bit 0 is always 1.
3342   AmstradCPC_GA_SetRamConfiguration(machine);
3343   amstrad_GateArray_write(machine, 0x081); // Epyx World of Sports requires upper ROM to be enabled by default
3344   
3345   //  multiface_init();
3346}
3347
3348
3349MACHINE_RESET( gx4000 )
3350{
3351   int i;
3352   UINT8 *rom = memory_region(machine, "main");
3353
3354   amstrad_system_type = SYSTEM_GX4000;
3355
3356   for (i=0; i<128; i++)  // fill ROM table
3357   {
3358      Amstrad_ROM_Table[i] = &rom[0x4000];
3359   }
3360   for(i=128;i<160;i++)
3361   {
3362      Amstrad_ROM_Table[i] = &rom[(i-128)*0x4000];
3363   }
3364   Amstrad_ROM_Table[7] = &rom[0xc000];
3365
3366   asic.enabled = 0;
3367   asic.seqptr = 0;
3368   asic.pri = 0;  // disable PRI
3369   asic.dma_status = 0;  // disable all DMA channels
3370   asic.dma_addr[0] = 0;
3371   asic.dma_prescaler[0] = 0;
3372   asic.dma_addr[1] = 0;
3373   asic.dma_prescaler[1] = 0;
3374   asic.dma_addr[2] = 0;
3375   asic.dma_prescaler[2] = 0;
3376   asic.dma_clear = 1;  // by default, DMA interrupts must be cleared by writing to the DSCR (&6c0f)
3377   amstrad_plus_irq_cause = 6;
3378
3379   amstrad_common_init(machine);
3380   amstrad_reset_machine(machine);
3381   amstrad_plus_asic_ram[0x2805] = 0x01;  // interrupt vector is undefined at startup, except that bit 0 is always 1.
3382   AmstradCPC_GA_SetRamConfiguration(machine);
3383   amstrad_GateArray_write(machine, 0x081); // Epyx World of Sports requires upper ROM to be enabled by default
3384   //  multiface_init();
3385}
3386
3387
3388MACHINE_RESET( kccomp )
3389{
3390   int i;
3391   UINT8 *rom = memory_region(machine, "main");
3392
3393   amstrad_system_type = SYSTEM_CPC;
3394
3395   for (i=0; i<256; i++)
3396   {
3397      Amstrad_ROM_Table[i] = &rom[0x014000];
3398   }
3399
3400   amstrad_common_init(machine);
3401   kccomp_reset_machine(machine);
3402
3403   /* bit 1 = /TEST. When 0, KC compact will enter data transfer
3404    sequence, where another system using the expansion port signals
3405    DATA2,DATA1, /STROBE and DATA7 can transfer 256 bytes of program.
3406    When the program has been transfered, it will be executed. This
3407    is not supported in the driver */
3408   /* bit 3,4 are tied to +5V, bit 2 is tied to 0V */
3409   ppi_port_inputs[amstrad_ppi_PortB] = (1<<4) | (1<<3) | 2;
3410}
3411
3412
3413MACHINE_RESET( aleste )
3414{
3415   int i;
3416   UINT8 *rom = memory_region(machine, "main");
3417
3418   amstrad_system_type = SYSTEM_ALESTE;
3419
3420   for (i=0; i<256; i++)
3421   {
3422      Amstrad_ROM_Table[i] = &rom[0x014000];
3423   }
3424
3425   Amstrad_ROM_Table[3] = &rom[0x01c000];  // MSX-DOS / BIOS
3426   Amstrad_ROM_Table[7] = &rom[0x018000];  // AMSDOS
3427   amstrad_common_init(machine);
3428   amstrad_reset_machine(machine);
3429
3430   floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 0),  FLOPPY_DRIVE_DS_80);
3431   floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 1),  FLOPPY_DRIVE_DS_80);
3432}
3433
3434
1753435/* load snapshot */
1763436SNAPSHOT_LOAD(amstrad)
1773437{
r4153r4154
1993459   return INIT_PASS;
2003460}
2013461
3462
2023463DEVICE_IMAGE_LOAD(amstrad_plus_cartridge)
2033464{
2043465   // load CPC Plus / GX4000 cartridge image
src/mess/mess.mak
r4153r4154
894894$(MESSOBJ)/amstrad.a:  \
895895   $(MESS_DRIVERS)/amstrad.o  \
896896   $(MESS_MACHINE)/amstrad.o  \
897   $(MESS_VIDEO)/amstrad.o  \
898897   $(MESS_VIDEO)/pcw.o    \
899898   $(MESS_DRIVERS)/pcw.o    \
900899   $(MESS_DRIVERS)/pcw16.o    \
src/mess/drivers/amstrad.c
r4153r4154
8585#include "machine/8255ppi.h"   /* for 8255 ppi */
8686#include "cpu/z80/z80.h"      /* for cycle tables */
8787#include "video/m6845.h"      /* CRTC display */
88#include "video/mc6845.h"      /* CRTC */
8889#include "machine/nec765.h"   /* for floppy disc controller */
8990#include "sound/ay8910.h"
9091#include "sound/wave.h"
r4153r4154
102103#include "formats/tzx_cas.h"
103104
104105
105#ifdef AMSTRAD_VIDEO_EVENT_LIST
106/* for event list */
107#include "eventlst.h"
108#endif
109
110106#define MANUFACTURER_NAME 0x07
111107#define TV_REFRESH_RATE 0x10
112108
r4153r4154
114110#define SYSTEM_PLUS   1
115111#define SYSTEM_GX4000 2
116112
117//int selected_m6845_address = 0;
118113
119// &ff,&77,&b3,&51,&a8,&d4,&62,&39,&9c,&46,&2b,&15,&8a,&cd,&ee
120// This is the sequence for unlocking the ASIC in the CPC+/GX4000
121// These are outed to port &bc00, after syncing the lock by outing a non-zero value then a zero to &bc00
122// To lock the ASIC again, repeat the sequence without the last &ee
123static const unsigned char asic_unlock_seq[15] =
124{
125   0xff, 0x77, 0xb3, 0x51, 0xa8, 0xd4, 0x62, 0x39, 0x9c, 0x46, 0x2b, 0x15, 0x8a, 0xcd, 0xee
126};
127114
128int amstrad_system_type;
129int aleste_mode;
130static int amstrad_plus_lower;  // CPC+/GX4000 cartridge bank loaded in lower ROM area
131static int amstrad_plus_lower_addr;  // CPC+/GX4000 lower ROM area address / ASIC register page enable
132static int amstrad_plus_lower_enabled;  // secondary lower ROM select in use?
133int amstrad_plus_asic_enabled;  // ASIC enabled
134static int amstrad_plus_asic_regpage;  // ASIC register page enabled
135static int amstrad_plus_asic_seqptr;   // current position in the ASIC unlocking sequence
136int amstrad_plus_pri;  // Programmable raster interrupt, 0=disabled.
137int amstrad_plus_irq_cause;  // part of the interrupt vector for IM 2.  6 = raster IRQ, 4 = DMA channel 2, 2 = DMA channel 1, 0 = DMA channel 0
138int amstrad_plus_scroll_x;  // soft scroll - horizontal (0-15), in mode 2 pixels
139int amstrad_plus_scroll_y;  // soft scroll - vertical (0-7), in scanlines
140int amstrad_plus_scroll_border;  // soft scroll - extend border, any program that uses soft scrolling should enable this
141int amstrad_plus_dma_status;
142int amstrad_plus_dma_0_addr;   // DMA channel address
143int amstrad_plus_dma_1_addr;
144int amstrad_plus_dma_2_addr;
145int amstrad_plus_dma_prescaler[3];  // DMA channel prescaler
146static int amstrad_plus_dma_clear;  // set if DMA interrupts are to be cleared automatically
147
148static void amstrad_plus_seqcheck(int data);
149static WRITE8_HANDLER( amstrad_plus_asic_4000_w );
150static WRITE8_HANDLER( amstrad_plus_asic_6000_w );
151static READ8_HANDLER( amstrad_plus_asic_4000_r );
152static READ8_HANDLER( amstrad_plus_asic_6000_r );
153
154/* the hardware allows selection of 256 ROMs. Rom 0 is usually BASIC and Rom 7 is AMSDOS */
155/* With the CPC hardware, if a expansion ROM is not connected, BASIC rom will be selected instead */
156static unsigned char *Amstrad_ROM_Table[256];
157/* data present on input of ppi, and data written to ppi output */
158#define amstrad_ppi_PortA 0
159#define amstrad_ppi_PortB 1
160#define amstrad_ppi_PortC 2
161
162static int ppi_port_inputs[3];
163static int ppi_port_outputs[3];
164
165
166/* keyboard line 0-9 */
167static int amstrad_keyboard_line;
168
169static unsigned char previous_amstrad_UpperRom_data;
170static unsigned char previous_printer_data_byte;
171
172static int aleste_rtc_function;
173static int aleste_fdc_int;
174
175/*------------------
176  - Ram Management -
177  ------------------*/
178/* current selected upper rom */
179static unsigned char *Amstrad_UpperRom;
180/* There are 8 different ram configurations which work on the currently selected 64k logical block.
181   The following tables show the possible ram configurations :*/
182static const int RamConfigurations[8 * 4] =
183{
184   0, 1, 2, 3,                   /* config 0 */
185   0, 1, 2, 7,                   /* config 1 */
186   4, 5, 6, 7,                   /* config 2 */
187   0, 3, 2, 7,                   /* config 3 */
188   0, 4, 2, 3,                   /* config 4 */
189   0, 5, 2, 3,                   /* config 5 */
190   0, 6, 2, 3,                   /* config 6 */
191   0, 7, 2, 3                    /* config 7 */
192};
193/*-------------------
194  - Gate Array data -
195  -------------------*/
196/* Pen selection */
197static int amstrad_GateArray_PenSelected = 0;
198/* Rom configuration */
199static int amstrad_GateArray_ModeAndRomConfiguration = 0;
200/* Ram configuration */
201static int amstrad_GateArray_RamConfiguration = 0;
202
203/*-------------
204  - MULTIFACE -
205  -------------*/
206static void multiface_rethink_memory(running_machine *machine);
207static WRITE8_HANDLER(multiface_io_write);
208static void multiface_init(void);
209static void multiface_stop(running_machine *machine);
210static int multiface_hardware_enabled(running_machine *machine);
211static void multiface_reset(void);
212
213static const device_config *printer_device(running_machine *machine)
214{
215   return devtag_get_device(machine, PRINTER, "printer");
216}
217
218
219
220/* ---------------------------------------
221   - 27.05.2004 - PSG function selection -
222   ---------------------------------------
223The databus of the PSG is connected to PPI Port A.
224Data is read from/written to the PSG through this port.
225
226The PSG function, defined by the BC1,BC2 and BDIR signals, is controlled by bit 7 and bit 6 of PPI Port C.
227
228PSG function selection:
229-----------------------
230Function
231
232BDIR = PPI Port C Bit 7 and BC1 = PPI Port C Bit 6
233
234PPI Port C Bit | PSG Function
235BDIR BC1       |
2360    0         | Inactive
2370    1         | Read from selected PSG register. When function is set, the PSG will make the register data available to PPI Port A
2381    0         | Write to selected PSG register. When set, the PSG will take the data at PPI Port A and write it into the selected PSG register
2391    1         | Select PSG register. When set, the PSG will take the data at PPI Port A and select a register
240*/
241/* PSG function selected */
242static unsigned char amstrad_Psg_FunctionSelected;
243
244static void update_psg(running_machine *machine)
245{
246   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
247   
248   if(aleste_mode & 0x20)  // RTC selected
249   {
250      switch(aleste_rtc_function)
251      {
252      case 0x02:  // AS
253         mc146818_port_w(space, 0,ppi_port_outputs[amstrad_ppi_PortA]);
254         break;
255      case 0x04:  // DS write
256         mc146818_port_w(space, 1,ppi_port_outputs[amstrad_ppi_PortA]);
257         break;
258      case 0x05:  // DS read
259         ppi_port_inputs[amstrad_ppi_PortA] = mc146818_port_r(space, 1);
260         break;
261      }
262      return;
263   }
264  switch (amstrad_Psg_FunctionSelected)
265  {
266     case 0:
267      {/* Inactive */
268      } break;
269     case 1:
270      {/* b6 = 1 ? : Read from selected PSG register and make the register data available to PPI Port A */
271           ppi_port_inputs[amstrad_ppi_PortA] = ay8910_read_port_0_r(space, 0);
272        }
273      break;
274     case 2:
275      {/* b7 = 1 ? : Write to selected PSG register and write data to PPI Port A */
276           ay8910_write_port_0_w(space, 0, ppi_port_outputs[amstrad_ppi_PortA]);
277        }
278      break;
279     case 3:
280      {/* b6 and b7 = 1 ? : The register will now be selected and the user can read from or write to it.  The register will remain selected until another is chosen.*/
281           ay8910_control_port_0_w(space, 0, ppi_port_outputs[amstrad_ppi_PortA]);
282         prev_reg = ppi_port_outputs[amstrad_ppi_PortA];
283        }
284      break;
285     default: {
286    } break;
287   }
288}
289
290/* Read/Write 8255 PPI port A (connected to AY-3-8912 databus) */
291static READ8_DEVICE_HANDLER ( amstrad_ppi_porta_r )
292{
293   update_psg(device->machine);
294   return ppi_port_inputs[amstrad_ppi_PortA];
295}
296
297static WRITE8_DEVICE_HANDLER ( amstrad_ppi_porta_w )
298{
299   ppi_port_outputs[amstrad_ppi_PortA] = data;
300   update_psg(device->machine);
301}
302
303/* - Read PPI Port B -
304   -------------------
305Bit Description
3067   Cassette read data
3076   Parallel/Printer port ready signal ("1" = not ready, "0" = Ready)
3085   /EXP signal on expansion port (note 6)
3094   50/60 Hz (link on PCB. For this MESS driver I have used the dipswitch feature) (note 5)
3103   | PCB links to define manufacturer name. For this MESS driver I have used the dipswitch feature. (note 1) (note 4)
3112   | (note 2)
3121   | (note 3)
3130   VSYNC State from 6845. "1" = VSYNC active, "0" = VSYNC inactive
314
315Note:
316
3171 On CPC464,CPC664,CPC6128 and GX4000 this is LK3 on the PCB. On the CPC464+ and CPC6128+ this is LK103 on the PCB. On the KC compact this is "1".
3182 On CPC464,CPC664,CPC6128 and GX4000 this is LK2 on the PCB. On the CPC464+ and CPC6128+ this is LK102 on the PCB. On the KC compact this is "0".
3193 On CPC464,CPC664,CPC6128 and GX4000 this is LK1 on the PCB. On the CPC464+ and CPC6128+ this is LK101 on the PCB. On the KC compact this is /TEST signal from the expansion port.
3204 On the CPC464,CPC664,CPC6128,CPC464+,CPC6128+ and GX4000 bits 3,2 and 1 define the manufacturer name. See below to see the options available. The manufacturer name is defined on the PCB and cannot be changed through software.
3215 On the CPC464,CPC664,CPC6128,CPC464+,CPC6128+ and GX4000 bit 4 defines the Screen refresh frequency. "1" = 50 Hz, "0" = 60 Hz. This is defined on the PCB and cannot be changed with software. On the KC compact bit 4 is "1"
3226 This bit is connected to /EXP signal on the expansion port.
323  On the KC Compact this bit is used to define bit 7 of the printer data.
324  On the CPC, it is possible to use this bit to define bit 7 of the printer data, so a 8-bit printer port is made, with a hardware modification,
325  On the CPC this can be used by a expansion device to report it's presence. "1" = device connected, "0" = device not connected. This is not always used by all expansion devices.
326*/
327
328static READ8_DEVICE_HANDLER (amstrad_ppi_portb_r)
329{
330   int data = 0;
331/* Set b7 with cassette tape input */
332   if(amstrad_system_type != SYSTEM_GX4000)
333   {
334      if (cassette_input(devtag_get_device(device->machine, CASSETTE, "cassette")) > 0.03) {
335         data |= (1<<7);
336      }
337   }
338/* Set b6 with Parallel/Printer port ready */
339   if(amstrad_system_type != SYSTEM_GX4000)
340   {
341      if (printer_is_ready(printer_device(device->machine))==0 ) {
342         data |= (1<<6);
343      }
344   }
345/* Set b4-b1 50Hz/60Hz state and manufacturer name defined by links on PCB */
346   data |= (ppi_port_inputs[amstrad_ppi_PortB] & 0x1e);
347
348/*  Set b0 with VSync state from the CRTC */
349   data |= amstrad_CRTC_VS; // m6845_vertical_sync_r(0);
350
351   if(aleste_mode & 0x04)
352   {
353      if(aleste_fdc_int == 0)
354         data &= ~0x02;
355      else
356         data |= 0x02;
357   }
358
359   return data;
360}
361
362/* 26-May-2005 - PPI Port C
363   -----------------------
364Bit Description  Usage
3657   PSG BDIR     | PSG function selection
3666   PSG BC1      |
3675                Cassette Write data
3684                Cassette Motor Control set bit to "1" for motor on, or "0" for motor off
3693                | Keyboard line Select keyboard line to be scanned (0-15)
3702                |
3711                |
3720                |*/
373
374/* previous_ppi_portc_w value */
375static int previous_ppi_portc_w;
376
377static WRITE8_DEVICE_HANDLER ( amstrad_ppi_portc_w )
378{
379   int changed_data;
380
381   previous_ppi_portc_w = ppi_port_outputs[amstrad_ppi_PortC];
382/* Write the data to Port C */
383   changed_data = previous_ppi_portc_w^data;
384
385   ppi_port_outputs[amstrad_ppi_PortC] = data;
386
387/* get b7 and b6 (PSG Function Selected */
388   amstrad_Psg_FunctionSelected = ((data & 0xC0)>>6);
389
390   /* MC146818 function */
391   aleste_rtc_function = data & 0x07;
392
393/* Perform PSG function */
394   update_psg(device->machine);
395
396/* b5 Cassette Write data */
397   if(amstrad_system_type != SYSTEM_GX4000)
398   {
399      if ((changed_data & 0x20) != 0) {
400         cassette_output(devtag_get_device(device->machine, CASSETTE, "cassette"),
401            ((data & 0x20) ? -1.0 : +1.0));
402      }
403   }
404
405/* b4 Cassette Motor Control */
406   if(amstrad_system_type != SYSTEM_GX4000)
407   {
408      if ((changed_data & 0x10) != 0) {
409         cassette_change_state(devtag_get_device(device->machine, CASSETTE, "cassette"),
410            ((data & 0x10) ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED),
411            CASSETTE_MASK_MOTOR);
412      }
413   }
414
415/* b3-b0 Keyboard line Select keyboard line to be scanned */
416  amstrad_keyboard_line = (data & 0x0F);
417}
418
419115/* -----------------------------
420116   - amstrad_ppi8255_interface -
421117   -----------------------------*/
r4153r4154
429125   amstrad_ppi_portc_w    /* port C write */
430126};
431127
432static NEC765_INTERRUPT( aleste_interrupt )
433{
434   if(state == CLEAR_LINE)
435      aleste_fdc_int = 0;
436   else
437      aleste_fdc_int = 1;
438}
439128
440129/* Amstrad NEC765 interface doesn't use interrupts or DMA! */
441130static const nec765_interface amstrad_nec765_interface =
r4153r4154
455144   NEC765_RDY_PIN_CONNECTED   
456145};
457146
458/* pointers to current ram configuration selected for banks */
459static unsigned char *AmstradCPC_RamBanks[4];
460static unsigned char *Aleste_RamBanks[4];
461static int aleste_active_page[4];  // page mapped to CPU RAM (is readable?)
462147
463/* pointer to RAM used for the CPC+ ASIC memory-mapped registers */
464unsigned char *amstrad_plus_asic_ram;
465
466/*--------------------------
467  - Ram and Rom management -
468  --------------------------*/
469/*-----------------
470  - Set Lower Rom -
471  -----------------*/
472void amstrad_setLowerRom(running_machine *machine)
473{
474   unsigned char *BankBase;
475   int banknum;
476/* b2 : "1" Lower rom area disable or "0" Lower rom area enable */
477   if(amstrad_system_type == SYSTEM_CPC)
478   {
479      if ((amstrad_GateArray_ModeAndRomConfiguration & (1<<2)) == 0) {
480         BankBase = &memory_region(machine, "main")[0x010000];
481      } else
482      {
483         if(aleste_mode & 0x04)
484            BankBase = Aleste_RamBanks[0];
485         else
486            BankBase = AmstradCPC_RamBanks[0];
487      }
488      memory_set_bankptr(machine,1, BankBase);
489      memory_set_bankptr(machine,2, BankBase+0x02000);
490   }
491   else  // CPC+/GX4000
492   {
493      banknum = amstrad_plus_lower_addr;
494
495      if(banknum == 3)
496         banknum = 0;
497
498      if(AmstradCPC_RamBanks[0] != NULL)
499      {
500         memory_set_bankptr(machine,1, AmstradCPC_RamBanks[0]);
501         memory_set_bankptr(machine,2, AmstradCPC_RamBanks[0]+0x2000);
502         memory_set_bankptr(machine,3, AmstradCPC_RamBanks[1]);
503         memory_set_bankptr(machine,4, AmstradCPC_RamBanks[1]+0x2000);
504         memory_set_bankptr(machine,5, AmstradCPC_RamBanks[2]);
505         memory_set_bankptr(machine,6, AmstradCPC_RamBanks[2]+0x2000);
506      }
507
508      if ((amstrad_GateArray_ModeAndRomConfiguration & (1<<2)) == 0)
509         amstrad_plus_lower_enabled = 1;
510      else
511         amstrad_plus_lower_enabled = 0;
512
513      if(amstrad_plus_lower_enabled == 1)
514      {  // ASIC secondary lower ROM selection (bit 5: 1 = enabled)
515//          logerror("L-ROM: Lower ROM enabled, cart bank %i\n",amstrad_plus_lower);
516         BankBase = &memory_region(machine, "main")[0x4000 * amstrad_plus_lower];
517         if(BankBase != NULL)
518         {
519            switch(amstrad_plus_lower_addr)
520            {
521            case 0:
522//                  logerror("L-ROM: located at &0000\n");
523               memory_set_bankptr(machine,1, BankBase);
524               memory_set_bankptr(machine,2, BankBase+0x02000);
525               break;
526            case 1:
527//                  logerror("L-ROM: located at &4000\n");
528               memory_set_bankptr(machine,3, BankBase);
529               memory_set_bankptr(machine,4, BankBase+0x02000);
530               break;
531            case 2:
532//                  logerror("L-ROM: located at &8000\n");
533               memory_set_bankptr(machine,5, BankBase);
534               memory_set_bankptr(machine,6, BankBase+0x02000);
535               break;
536            case 3:
537//                  logerror("L-ROM: located at &0000, ASIC registers enabled\n");
538               memory_set_bankptr(machine,1, BankBase);
539               memory_set_bankptr(machine,2, BankBase+0x02000);
540               break;
541            }
542         }
543      }
544   }
545}
546/*-----------------
547  - Set Upper Rom -
548  -----------------*/
549void amstrad_setUpperRom(running_machine *machine)
550{
551   unsigned char *BankBase;
552/* b3 : "1" Upper rom area disable or "0" Upper rom area enable */
553   if ((amstrad_GateArray_ModeAndRomConfiguration & (1<<3)) == 0)
554   {
555      BankBase = Amstrad_UpperRom;
556   }
557   else
558   {
559      if(aleste_mode & 0x04)
560         BankBase = Aleste_RamBanks[3];
561      else
562         BankBase = AmstradCPC_RamBanks[3];
563   }
564
565   if (BankBase)
566   {
567      memory_set_bankptr(machine,7, BankBase);
568      memory_set_bankptr(machine,8, BankBase+0x2000);
569   }
570}
571
572static void AmstradCPC_SetLowerRom(running_machine *machine, int Data)
573{
574   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
575   if(amstrad_plus_asic_enabled != 0)
576   {
577      amstrad_plus_lower = Data & 0x07;  // only lower 8 cart banks available for lower ROM
578      amstrad_plus_lower_addr = (Data & 0x18) >> 3;  // address of lower ROM area
579      if(amstrad_plus_lower_addr == 3)
580      {
581         memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, amstrad_plus_asic_4000_r);
582         memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, amstrad_plus_asic_6000_r);
583         memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, amstrad_plus_asic_4000_w);
584         memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, amstrad_plus_asic_6000_w);
585         amstrad_plus_asic_regpage = 1;  // enable ASIC registers
586      }
587      else
588      {
589         memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK3);
590         memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK4);
591         memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK11);
592         memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK12);
593         amstrad_plus_asic_regpage = 0;  // disable ASIC registers
594      }
595//      logerror("SYS: Secondary ROM select (lower ROM) - data = %02x - cart bank %i, addr %i\n",Data,amstrad_plus_lower,amstrad_plus_lower_addr);
596   }
597   else
598   {  // secondary ROM register not available if ASIC is locked
599      amstrad_plus_lower = 0;
600      amstrad_plus_lower_addr = 0;
601   }
602//  amstrad_setLowerRom(machine);
603}
604
605void AmstradCPC_SetUpperRom(running_machine *machine, int Data)
606{
607   Amstrad_UpperRom = Amstrad_ROM_Table[Data & 0xFF];
608//  logerror("H-ROM: set to ROM bank %i\n",Data);
609   amstrad_setUpperRom(machine);
610}
611
612/* ASIC RAM */
613/*
614    ASIC RAM Layout.  Always is mapped to &4000-&7fff
615
616    Hardware sprites: 16 sprites, 16x16, basic zooming, 15 colour (12-bit palette)
617    Pixel data is 0 - 15 for sprite pen number, low 4 bits
618    &4000 - &400f Pixel data for first line of first sprite
619    &4010 - &401f Pixel data for second line of first sprite
620      ...     ...
621    &40f0 - &40ff Pixel data for last (16th) line of first sprite
622      ...     ...
623    &4100 - &41ff Pixel data for second sprite
624    &4200 - &42ff Third sprite
625      ...     ...
626    &4f00 - &4fff Pixel data for last (16th) sprite
627
628    &6000 - &607f Sprite properties (8 bytes each)
629                  +0 Sprite X position LSB
630                  +1 Sprite X position MSB
631                  +2 Sprite Y position LSB (scanline)
632                  +3 Sprite Y position MSB
633                  +4 Sprite zoom - bits 3,2 X magnification, bits 1,0 Y magnification
634                      Magnification: 00 = not displayed, 01 = x1, 10 = x2, 11 = x4
635
636    Palette: LSB first, presumably GGGGBBBBxxxxRRRR
637    &6400 - &641f Pen palette (12bpp, 2 bytes each, 16 total)
638    &6420 - &6421 Border palette  (12bpp, 2 bytes)
639    &6422 - &643f Hardware sprite palette (12bpp, 2 bytes each, 15 total)
640
641    Programmable Raster Interrupt:
642    &6800  Scanline for IRQ to be triggered after
643           If 0, raster interrupts and DMA interrupts occur
644           Otherwise, the PRI interrupt is triggered only
645
646    Hardware split screen:
647    &6801  Scanline for split to occur at
648    &6802  LSB of screen address for split (like reg 12 of the 6845)
649    &6803  MSB of the above (like reg 13 of the 6845)
650
651    Soft Scroll Control Register:
652    &6804  bits 3-0 - horizontal delay in mode 2 pixels (shifts display to the right)
653           bits 6-4 - added to the 3 LSBs for the scanline address (shifts display up)
654           bit 7    - extends the border by two bytes (16 mode 2 pixels), masking the bad data from the horizontal scroll
655
656    Analogue paddle ports:
657    &6808 - &680f  Analogue input, read-only, 6 bit
658
659    PSG DMA channels:
660    &6c00  DMA channel 0 address LSB
661    &6c01  DMA channel 0 address MSB
662    &6c02  DMA channel 0 prescaler
663    &6c03  unused
664    &6c04-7  DMA channel 1
665    &6c08-b  DMA channel 2
666    &6c0f  Control and Status register
667             bit 7 - raster interrupt
668             bit 6 - DMA channel 0 interrupt
669             bit 5 - DMA channel 1 interrupt
670             bit 4 - DMA channel 2 interrupt
671             bit 3 - unused (write 0)
672             bit 2 - DMA channel 2 enable
673             bit 1 - DMA channel 1 enable
674             bit 0 - DMA channel 0 enable
675
676 */
677
678static WRITE8_HANDLER( amstrad_plus_asic_4000_w )
679{
680   if(amstrad_plus_asic_regpage != 0)
681   {
682//      logerror("ASIC: Write to register at &%04x\n",offset+0x4000);
683      amstrad_plus_asic_ram[offset] = data;
684   }
685   else
686   {
687      *(AmstradCPC_RamBanks[1]+offset) = data;
688   }
689}
690
691static WRITE8_HANDLER( amstrad_plus_asic_6000_w )
692{
693   if(amstrad_plus_asic_regpage != 0)
694   {
695      amstrad_plus_asic_ram[offset+0x2000] = data;
696      if(offset >= 0x0422 && offset < 0x440) // ASIC sprite palette
697      {
698         int r,g,b;
699
700         if((offset & 0x01) == 0)
701         {
702            g = (amstrad_plus_asic_ram[offset+0x2001] & 0x0f) << 4;
703            r = (amstrad_plus_asic_ram[offset+0x2000] & 0xf0);
704            b = (amstrad_plus_asic_ram[offset+0x2000] & 0x0f) << 4;
705         }
706         else
707         {
708            g = (amstrad_plus_asic_ram[offset+0x2000] & 0x0f) << 4;
709            r = (amstrad_plus_asic_ram[offset+0x1fff] & 0xf0);
710            b = (amstrad_plus_asic_ram[offset+0x1fff] & 0x0f) << 4;
711            offset--;
712         }
713         amstrad_plus_setspritecolour(space->machine, (offset - 0x422), r, g, b);
714      }
715      if(offset == 0x0800)  // Programmable raster interrupt
716      {
717//          logerror("ASIC: Wrote %02x to PRI\n",data);
718         amstrad_plus_pri = data;
719      }
720      if(offset >= 0x0801 && offset <= 0x0803)  // Split screen registers
721      {
722         int addr;
723         addr = amstrad_plus_asic_ram[0x2803] + (amstrad_plus_asic_ram[0x2802] << 8);  // high byte first this time
724         amstrad_plus_setsplitline(amstrad_plus_asic_ram[0x2801],addr);
725         logerror("ASIC: Split screen at line %i, address &%04x\n",amstrad_plus_asic_ram[0x2801],addr);
726      }
727      if(offset == 0x0804)  // Soft scroll register
728      {
729         amstrad_plus_scroll_border = data & 0x80;  // border extend - covers garbage on the left when soft scrolling is used
730         amstrad_plus_scroll_x = data & 0x0f;  // low 4 bits
731         amstrad_plus_scroll_y = (data & 0x70) >> 4;  // next 3 bits
732      }
733      if(offset == 0x0805)  // Interrupt vector register (for IM 2, used by Pang)
734      {
735         // high 5 bits go to interrupt vector
736         int vector;
737
738         if(amstrad_plus_asic_enabled != 0)
739         {
740            vector = (data & 0xf8) + (amstrad_plus_irq_cause);
741            cpu_set_input_line_vector(space->machine->cpu[0], 0, vector);
742            logerror("ASIC: IM 2 vector write %02x, data = &%02x\n",vector,data);
743         }
744         amstrad_plus_dma_clear = data & 0x01;
745      }
746      // DMA channels
747      switch(offset)
748      {
749      case 0x0c00:
750      case 0x0c01:
751         amstrad_plus_dma_0_addr = (amstrad_plus_asic_ram[0x2c01] << 8) + amstrad_plus_asic_ram[0x2c00];
752         amstrad_plus_dma_status &= ~0x01;
753         logerror("ASIC: DMA 0 address set to &%04x\n",amstrad_plus_dma_0_addr);
754         break;
755      case 0x0c04:
756      case 0x0c05:
757         amstrad_plus_dma_1_addr = (amstrad_plus_asic_ram[0x2c05] << 8) + amstrad_plus_asic_ram[0x2c04];
758         amstrad_plus_dma_status &= ~0x02;
759         logerror("ASIC: DMA 1 address set to &%04x\n",amstrad_plus_dma_1_addr);
760         break;
761      case 0x0c08:
762      case 0x0c09:
763         amstrad_plus_dma_2_addr = (amstrad_plus_asic_ram[0x2c09] << 8) + amstrad_plus_asic_ram[0x2c08];
764         amstrad_plus_dma_status &= ~0x04;
765         logerror("ASIC: DMA 2 address set to &%04x\n",amstrad_plus_dma_2_addr);
766         break;
767      case 0x0c02:
768         amstrad_plus_dma_prescaler[0] = data + 1;
769         logerror("ASIC: DMA 0 pause prescaler set to %i\n",data);
770         break;
771      case 0x0c06:
772         amstrad_plus_dma_prescaler[1] = data + 1;
773         logerror("ASIC: DMA 1 pause prescaler set to %i\n",data);
774         break;
775      case 0x0c0a:
776         amstrad_plus_dma_prescaler[2] = data + 1;
777         logerror("ASIC: DMA 2 pause prescaler set to %i\n",data);
778         break;
779      case 0x0c0f:
780         amstrad_plus_dma_status = data;
781         logerror("ASIC: DMA status write - %02x\n",data);
782         if(data & 0x40)
783         {
784            logerror("ASIC: DMA 0 IRQ acknowledge\n");
785            cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
786            amstrad_plus_irq_cause = 0x06;
787            amstrad_plus_asic_ram[0x2c0f] &= ~0x40;
788         }
789         if(data & 0x20)
790         {
791            logerror("ASIC: DMA 1 IRQ acknowledge\n");
792            cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
793            amstrad_plus_irq_cause = 0x06;
794            amstrad_plus_asic_ram[0x2c0f] &= ~0x20;
795         }
796         if(data & 0x10)
797         {
798            logerror("ASIC: DMA 2 IRQ acknowledge\n");
799            cpu_set_input_line(space->machine->cpu[0],0,CLEAR_LINE);
800            amstrad_plus_irq_cause = 0x06;
801            amstrad_plus_asic_ram[0x2c0f] &= ~0x10;
802         }
803         break;
804      }
805   }
806   else
807   {
808      *(AmstradCPC_RamBanks[1]+offset+0x2000) = data;
809   }
810}
811
812static READ8_HANDLER( amstrad_plus_asic_4000_r )
813{
814//  logerror("RAM: read from &%04x, ASIC page = %i\n",offset+0x4000,amstrad_plus_asic_regpage);
815   if(amstrad_plus_asic_regpage != 0)
816   {
817      return amstrad_plus_asic_ram[offset];
818   }
819   return *(AmstradCPC_RamBanks[1]+offset);
820}
821
822static READ8_HANDLER( amstrad_plus_asic_6000_r )
823{
824//  logerror("RAM: read from &%04x, ASIC page = %i\n",offset+0x6000,amstrad_plus_asic_regpage);
825   if(amstrad_plus_asic_regpage != 0)
826   {
827      // Analogue ports
828      if(offset == 0x0808)
829      {
830         return (input_port_read(space->machine, "analog1") & 0x3f);
831      }
832      if(offset == 0x0809)
833      {
834         return (input_port_read(space->machine, "analog2") & 0x3f);
835      }
836      if(offset == 0x080a)
837      {
838         return (input_port_read(space->machine, "analog3") & 0x3f);
839      }
840      if(offset == 0x080b)
841      {
842         return (input_port_read(space->machine, "analog4") & 0x3f);
843      }
844      if(offset == 0x080c || offset == 0x080e)
845      {
846         return 0x3f;
847      }
848      if(offset == 0x080d || offset == 0x080f)
849      {
850         return 0x00;
851      }
852/*      if(offset == 0x0c0f)  // DMA status and control
853        {
854            int result = 0;
855            if(amstrad_plus_irq_cause == 0x00)
856                result |= 0x40;
857            if(amstrad_plus_irq_cause == 0x02)
858                result |= 0x20;
859            if(amstrad_plus_irq_cause == 0x04)
860                result |= 0x10;
861            if(amstrad_plus_irq_cause == 0x06)
862                result |= 0x80;
863            return result;
864        }
865*/
866      return amstrad_plus_asic_ram[offset+0x2000];
867   }
868
869   return *(AmstradCPC_RamBanks[1]+offset+0x2000);
870}
871
872/*------------------
873  - Rethink Memory -
874  ------------------*/
875void amstrad_rethinkMemory(running_machine *machine)
876{
877   /* the following is used for banked memory read/writes and for setting up
878     * opcode and opcode argument reads */
879/* bank 0 - 0x0000..0x03fff */
880   amstrad_setLowerRom(machine);
881/* bank 1 - 0x04000..0x07fff */
882   if(amstrad_system_type == SYSTEM_CPC || amstrad_plus_asic_enabled == 0)
883   {
884      if(aleste_mode & 0x04)
885      {
886         memory_set_bankptr(machine,3, Aleste_RamBanks[1]);
887         memory_set_bankptr(machine,4, Aleste_RamBanks[1]+0x2000);
888         /* bank 2 - 0x08000..0x0bfff */
889         memory_set_bankptr(machine,5, Aleste_RamBanks[2]);
890         memory_set_bankptr(machine,6, Aleste_RamBanks[2]+0x2000);
891      }
892      else
893      {
894         memory_set_bankptr(machine,3, AmstradCPC_RamBanks[1]);
895         memory_set_bankptr(machine,4, AmstradCPC_RamBanks[1]+0x2000);
896         /* bank 2 - 0x08000..0x0bfff */
897         memory_set_bankptr(machine,5, AmstradCPC_RamBanks[2]);
898         memory_set_bankptr(machine,6, AmstradCPC_RamBanks[2]+0x2000);
899      }
900   }
901   else
902      amstrad_setLowerRom(machine);
903/* bank 3 - 0x0c000..0x0ffff */
904   amstrad_setUpperRom(machine);
905/* other banks */
906      if(aleste_mode & 0x04)
907      {
908         memory_set_bankptr(machine,9, Aleste_RamBanks[0]);
909         memory_set_bankptr(machine,10, Aleste_RamBanks[0]+0x2000);
910         memory_set_bankptr(machine,11, Aleste_RamBanks[1]);
911         memory_set_bankptr(machine,12, Aleste_RamBanks[1]+0x2000);
912         memory_set_bankptr(machine,13, Aleste_RamBanks[2]);
913         memory_set_bankptr(machine,14, Aleste_RamBanks[2]+0x2000);
914         memory_set_bankptr(machine,15, Aleste_RamBanks[3]);
915         memory_set_bankptr(machine,16, Aleste_RamBanks[3]+0x2000);
916      }
917      else
918      {
919         memory_set_bankptr(machine,9, AmstradCPC_RamBanks[0]);
920         memory_set_bankptr(machine,10, AmstradCPC_RamBanks[0]+0x2000);
921         memory_set_bankptr(machine,11, AmstradCPC_RamBanks[1]);
922         memory_set_bankptr(machine,12, AmstradCPC_RamBanks[1]+0x2000);
923         memory_set_bankptr(machine,13, AmstradCPC_RamBanks[2]);
924         memory_set_bankptr(machine,14, AmstradCPC_RamBanks[2]+0x2000);
925         memory_set_bankptr(machine,15, AmstradCPC_RamBanks[3]);
926         memory_set_bankptr(machine,16, AmstradCPC_RamBanks[3]+0x2000);
927      }
928
929/* multiface hardware enabled? */
930      if (multiface_hardware_enabled(machine)) {
931         multiface_rethink_memory(machine);
932      }
933}
934/* simplified ram configuration - e.g. only correct for 128k machines
935
936RAM Expansion Bits
937                             7 6 5 4  3  2  1  0
938CPC6128                      1 1 - -  -  s2 s1 s0
939Dk'tronics 256K Silicon Disk 1 1 1 b1 b0 b2 -  -
940
941"-" - this bit is ignored. The value of this bit is not important.
942"0" - this bit must be set to "0"
943"1" - this bit must be set to "1"
944"b0,b1,b2" - this bit is used to define the logical 64k block that the ram configuration uses
945"s0,s1,s2" - this bit is used to define the ram configuration
946
947The CPC6128 has a 64k ram expansion built-in, giving 128K of RAM in this system.
948In the CPC464,CPC664 and KC compact if a ram expansion is not present, then writing to this port has no effect and the ram will be in the same arrangement as if configuration 0 had been selected.
949*/
950static void AmstradCPC_GA_SetRamConfiguration(running_machine *machine)
951{
952   int ConfigurationIndex = amstrad_GateArray_RamConfiguration & 0x07;
953   int BankIndex,i;
954   unsigned char *BankAddr;
955/* if b5 = 0 */
956  if(((amstrad_GateArray_RamConfiguration) & (1<<5)) == 0)  {
957    for (i=0;i<4;i++) {
958       BankIndex = RamConfigurations[(ConfigurationIndex << 2) + i];
959       BankAddr = mess_ram + (BankIndex << 14);
960      Aleste_RamBanks[i] = BankAddr;
961       AmstradCPC_RamBanks[i] = BankAddr;
962    }
963  } else {/* Need to add the ram expansion configuration here ! */
964  }
965   amstrad_rethinkMemory(machine);
966}
967/* -------------------
968   -  the Gate Array -
969   -------------------
970The gate array is controlled by I/O. The recommended I/O port address is &7Fxx.
971The gate array is selected when bit 15 of the I/O port address is set to "0" and bit 14 of the I/O port address is set to "1".
972The values of the other bits are ignored.
973However, to avoid conflict with other devices in the system, these bits should be set to "1".
974
975The function to be performed is selected by writing data to the Gate-Array, bit 7 and 6 of the data define the function selected (see table below).
976It is not possible to read from the Gate-Array.
977
978Bit 7 Bit 6 Function
9790     0     Select pen
9800     1     Select colour for selected pen
9811     0     Select screen mode, rom configuration and interrupt control
9821     1     Ram Memory Management (note 1)
983
984Note 1 : This function is not available in the Gate-Array, but is performed by a device at the same I/O port address location. In the CPC464,CPC664 and KC compact, this function is performed in a memory-expansion (e.g. Dk'Tronics 64K Ram Expansion), if this expansion is not present then the function is not available. In the CPC6128, this function is performed by a PAL located on the main PCB, or a memory-expansion. In the 464+ and 6128+ this function is performed by the ASIC or a memory expansion. Please read the document on Ram Management for more information.*/
985
986void amstrad_GateArray_write(running_machine *machine, int dataToGateArray)
987{
988/* Get Bit 7 and 6 of the dataToGateArray = Gate Array function selected */
989   switch ((dataToGateArray & 0xc0)>>6) {
990/* Pen selection
991   -------------
992Bit Value Function        Bit Value Function
9935   x     not used        5   x     not used
9944   1     Select border   4   0     Select pen
9953   x     | ignored       3   x     | Pen Number
9962   x     |               2   x       |
9971   x     |               1   x     |
9980   x     |               0   x     |
999*/
1000     case 0x00:   {
1001/* Select Border Number, get b4 */
1002       amstrad_GateArray_PenSelected = dataToGateArray & (1<<4);
1003/* if b4 = 0 : Select Pen Number, get b3-b0 */
1004        if (amstrad_GateArray_PenSelected == 0) {
1005             amstrad_GateArray_PenSelected = dataToGateArray & 0x0F;
1006           }
1007    }   break;
1008/* Colour selection
1009   ----------------
1010Even though there is provision for 32 colours, only 27 are possible.
1011The remaining colours are duplicates of those already in the colour palette.
1012
1013Bit Value Function
10145   x     not used
10154   x     | Colour number
10163   x     |
10172   x     |
10181   x     |
10190   x     |*/
1020     case 0x01: {
1021#ifdef AMSTRAD_VIDEO_EVENT_LIST
1022    EventList_AddItemOffset(machine, (EVENT_LIST_CODE_GA_COLOUR<<6) | PenIndex, AmstradCPC_PenColours[PenIndex], TIME_TO_CYCLES(0,video_screen_get_vpos(machine->primary_screen)*video_screen_get_scan_period(machine->primary_screen)));
1023#else
1024      amstrad_vh_update_colour(amstrad_GateArray_PenSelected, (dataToGateArray & 0x1F));
1025#endif
1026    } break;
1027/* Select screen mode and rom configuration
1028   ----------------------------------------
1029Bit Value Function
10305   x     not used
10314   x     Interrupt generation control
10323   1     Upper rom area disable or 0 Upper rom area enable
10332   1     Lower rom area disable or 0 Lower rom area enable
10341   x     | Mode selection
10350   x     |
1036
1037Screen mode selection : The settings for bits 1 and 0 and the corresponding screen mode are given in the table below.
1038-----------------------
1039b1 b0 Screen mode
10400  0  Mode 0, 160x200 resolution, 16 colours
10410  1  Mode 1, 320x200 resolution, 4 colours
10421  0  Mode 2, 640x200 resolution, 2 colours
10431  1  Mode 3, 160x200 resolution, 4 colours (note 1)
1044
1045This mode is not official. From the combinations possible, we can see that 4 modes can be defined, although the Amstrad only has 3. Mode 3 is similar to mode 0, because it has the same resolution, but it is limited to only 4 colours.
1046Mode changing is synchronised with HSYNC. If the mode is changed, it will take effect from the next HSYNC.
1047
1048Rom configuration selection :
1049-----------------------------
1050Bit 2 is used to enable or disable the lower rom area. The lower rom area occupies memory addressess &0000-&3fff and is used to access the operating system rom. When the lower rom area is is enabled, reading from &0000-&3FFF will return data in the rom. When a value is written to &0000-&3FFF, it will be written to the ram underneath the rom. When it is disabled, data read from &0000-&3FFF will return the data in the ram.
1051Similarly, bit 3 controls enabling or disabling of the upper rom area. The upper rom area occupies memory addressess &C000-&FFFF and is BASIC or any expansion roms which may be plugged into a rom board/box. See the document on upper rom selection for more details. When the upper rom area enabled, reading from &c000-&ffff, will return data in the rom. When data is written to &c000-&FFFF, it will be written to the ram at the same address as the rom. When the upper rom area is disabled, and data is read from &c000-&ffff the data returned will be the data in the ram.
1052
1053Bit 4 controls the interrupt generation. It can be used to delay interrupts.*/
1054     case 0x02: {
1055      int Previous_GateArray_ModeAndRomConfiguration = amstrad_GateArray_ModeAndRomConfiguration;
1056      /* If bit 5 is enabled on a CPC Plus/GX4000 when the ASIC is unlocked, sets the lower ROM position and cart bank
1057           b5 = 1, b4b3 = RAM position for lower ROM area and if the ASIC registers are visible at &4000,
1058           b2b1b0 = cartridge bank to read from lower ROM area (0-7 only) */
1059      if(amstrad_system_type != SYSTEM_CPC && amstrad_plus_asic_enabled != 0)
1060      {
1061         if((dataToGateArray & 0x20) != 0)
1062         {
1063            AmstradCPC_SetLowerRom(machine, dataToGateArray);
1064            break;
1065         }
1066         else
1067         {
1068            Previous_GateArray_ModeAndRomConfiguration = amstrad_GateArray_ModeAndRomConfiguration;
1069               amstrad_GateArray_ModeAndRomConfiguration = dataToGateArray;
1070         }
1071      }
1072      else
1073      {
1074         Previous_GateArray_ModeAndRomConfiguration = amstrad_GateArray_ModeAndRomConfiguration;
1075         amstrad_GateArray_ModeAndRomConfiguration = dataToGateArray;
1076      }
1077
1078/* If bit 4 of the "Select screen mode and rom configuration" register of the Gate-Array is set to "1"
1079 then the interrupt request is cleared and the 6-bit counter is reset to "0".  */
1080           if ((amstrad_GateArray_ModeAndRomConfiguration & (1<<4)) != 0) {
1081            amstrad_CRTC_HS_Counter = 0;
1082               cpu_set_input_line(machine->cpu[0],0, CLEAR_LINE);
1083
1084           }
1085/* b3b2 != 0 then change the state of upper or lower rom area and rethink memory */
1086        if (((amstrad_GateArray_ModeAndRomConfiguration & 0x0C)^(Previous_GateArray_ModeAndRomConfiguration & 0x0C)) != 0) {
1087          amstrad_setLowerRom(machine);
1088          amstrad_setUpperRom(machine);
1089        }
1090/* b1b0 mode change? */
1091           if (((amstrad_GateArray_ModeAndRomConfiguration & 0x03)^(Previous_GateArray_ModeAndRomConfiguration & 0x03)) != 0) {
1092#ifdef AMSTRAD_VIDEO_EVENT_LIST
1093          EventList_AddItemOffset(machine, (EVENT_LIST_CODE_GA_MODE<<6) , amstrad_GateArray_ModeAndRomConfiguration & 0x03, TIME_TO_CYCLES(0,video_screen_get_vpos(machine->primary_screen)*video_screen_get_scan_period(machine->primary_screen)));
1094#else
1095              amstrad_vh_update_mode(amstrad_GateArray_ModeAndRomConfiguration & 0x03);
1096#endif
1097           }
1098      }  break;
1099/* Ram Memory Management
1100     ---------------------
1101This function is not available in the Gate-Array, but is performed by a device at the same I/O port address location.
1102In the CPC464,CPC664 and KC compact, this function is performed in a memory-expansion (e.g. Dk'Tronics 64K Ram Expansion), if this expansion is not present then the function is not available.
1103In the CPC6128, this function is performed by a PAL located on the main PCB, or a memory-expansion.
1104In the 464+ and 6128+ this function is performed by the ASIC or a memory expansion.
1105*/
1106     case 0x03: {
1107           amstrad_GateArray_RamConfiguration = dataToGateArray;
1108     } break;
1109
1110    default: {
1111    } break;
1112  }
1113}
1114
1115static void aleste_msx_mapper(running_machine *machine, int offset, int data)
1116{
1117   int page = (offset & 0x0300) >> 8;
1118   int ramptr = (data & 0x1f) * 0x4000;
1119   int rampage = data & 0x1f;
1120   int function = (data & 0xc0) >> 6;
1121
1122   // It is assumed that functions are all mapped to each port &7cff-&7fff, and b8 and b9 are only used for RAM bank location
1123   switch(function)
1124   {
1125   case 0:  // Pen select (same as Gate Array?)
1126      amstrad_GateArray_write(machine, data);
1127      break;
1128   case 1:  // Colour select (6-bit palette)
1129      aleste_vh_update_colour(amstrad_GateArray_PenSelected,data & 0x3f);
1130      break;
1131   case 2:  // Screen mode, Upper/Lower ROM select
1132      amstrad_GateArray_write(machine, data);
1133      break;
1134   case 3: // RAM banks
1135      switch(page)
1136      {
1137      case 0:  /* 0x0000 - 0x3fff */
1138         memory_set_bankptr(machine,1,mess_ram+ramptr);
1139         memory_set_bankptr(machine,2,mess_ram+ramptr+0x2000);
1140         memory_set_bankptr(machine,9,mess_ram+ramptr);
1141         memory_set_bankptr(machine,10,mess_ram+ramptr+0x2000);
1142         Aleste_RamBanks[0] = mess_ram+ramptr;
1143         aleste_active_page[0] = data;
1144         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x0000\n",ramptr,rampage);
1145         break;
1146      case 1:  /* 0x4000 - 0x7fff */
1147         memory_set_bankptr(machine,3,mess_ram+ramptr);
1148         memory_set_bankptr(machine,4,mess_ram+ramptr+0x2000);
1149         memory_set_bankptr(machine,11,mess_ram+ramptr);
1150         memory_set_bankptr(machine,12,mess_ram+ramptr+0x2000);
1151         Aleste_RamBanks[1] = mess_ram+ramptr;
1152         aleste_active_page[1] = data;
1153         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x4000\n",ramptr,rampage);
1154         break;
1155      case 2:  /* 0x8000 - 0xbfff */
1156         memory_set_bankptr(machine,5,mess_ram+ramptr);
1157         memory_set_bankptr(machine,6,mess_ram+ramptr+0x2000);
1158         memory_set_bankptr(machine,13,mess_ram+ramptr);
1159         memory_set_bankptr(machine,14,mess_ram+ramptr+0x2000);
1160         Aleste_RamBanks[2] = mess_ram+ramptr;
1161         aleste_active_page[2] = data;
1162         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0x8000\n",ramptr,rampage);
1163         break;
1164      case 3:  /* 0xc000 - 0xffff */
1165         memory_set_bankptr(machine,7,mess_ram+ramptr);
1166         memory_set_bankptr(machine,8,mess_ram+ramptr+0x2000);
1167         memory_set_bankptr(machine,15,mess_ram+ramptr);
1168         memory_set_bankptr(machine,16,mess_ram+ramptr+0x2000);
1169         Aleste_RamBanks[3] = mess_ram+ramptr;
1170         aleste_active_page[3] = data;
1171         logerror("RAM: RAM location 0x%06x (page %02x) mapped to 0xc000\n",ramptr,rampage);
1172         break;
1173      }
1174      break;
1175   }
1176}
1177
1178/* used for loading snapshot only ! */
1179void AmstradCPC_PALWrite(running_machine *machine, int data)
1180{
1181   if ((data & 0x0c0)==0x0c0)   {
1182      amstrad_GateArray_RamConfiguration = data;
1183      AmstradCPC_GA_SetRamConfiguration(machine);
1184   }
1185}
1186
1187/* CRTC Differences
1188   ----------------
1189The following tables list the functions that can be accessed for each type:
1190
1191Type 0
1192b1 b0 Function Read/Write
11930  0  Select internal 6845 register Write Only
11940  1  Write to selected internal 6845 register Write Only
11951  0  - -
11961  1  Read from selected internal 6845 register Read only
1197
1198Type 1
1199b1 b0 Function Read/Write
12000  0  Select internal 6845 register Write Only
12010  1  Write to selected internal 6845 register Write Only
12021  0  Read Status Register Read Only
12031  1  Read from selected internal 6845 register Read only
1204
1205Type 2
1206b1 b0 Function Read/Write
12070  0  Select internal 6845 register Write Only
12080  1  Write to selected internal 6845 register Write Only
12091  0  - -
12101  1  Read from selected internal 6845 register Read only
1211
1212Type 3 and 4
1213b1 b0 Function Read/Write
12140  0  Select internal 6845 register Write Only
12150  1  Write to selected internal 6845 register Write Only
12161  0  Read from selected internal 6845 register Read Only
12171  1  Read from selected internal 6845 register Read only
1218*/
1219
1220/* I/O port allocation
1221   -------------------
1222
1223Many thanks to Mark Rison for providing the original information. Thankyou to Richard Wilson for his discoveries concerning RAM management I/O decoding.
1224
1225This document will explain the decoding of the I/O ports. The port address is not decoded fully which means a hardware device can be accessed through more than one address, in addition, using some addressess can access more than one element of the hardware at the same time. The CPC IN/OUT design differs from the norm in that port numbers are defined using 16 bits, as opposed to the traditional 8 bits.
1226
1227IN r,(C)/OUT (C),r instructions: Bits b15-b8 come from the B register, bits b7-b0 come from "r"
1228IN A,(n)/OUT (n),A instructions: Bits b15-b8 come from the A register, bits b7-b0 come from "n"
1229Listed below are the internal hardware devices and the bit fields to which they respond. In the table:
1230
1231"-" means this bit is ignored,
1232"0" means the bit must be set to "0" for the hardware device to respond,
1233"1" means the bit must be set to "1" for the hardware device to respond.
1234"r1" and "r0" mean a bit used to define a register
1235
1236Hardware device       Read/Write Port bits
1237                                 b15 b14 b13 b12 b11 b10 b9  b8  b7  b6  b5  b4  b3  b2  b1  b0
1238Gate-Array            Write Only 0   1   -   -   -   -   -   -   -   -   -   -   -   -   -   -
1239RAM Configuration     Write Only 0   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
1240CRTC                  Read/Write -   0   -   -   -   -   r1  r0  -   -   -   -   -   -   -   -
1241ROM select            Write only -   -   0   -   -   -   -   -   -   -   -   -   -   -   -   -
1242Printer port          Write only -   -   -   0   -   -   -   -   -   -   -   -   -   -   -   -
12438255 PPI              Read/Write -   -   -   -   0   -   r1  r0  -   -   -   -   -   -   -   -
1244Expansion Peripherals Read/Write -   -   -   -   -   0   -   -   -   -   -   -   -   -   -   -
1245
1246*/
1247
1248static READ8_HANDLER ( AmstradCPC_ReadPortHandler )
1249{
1250   device_config *fdc = (device_config*)devtag_get_device(space->machine, NEC765A, "nec765");
1251   unsigned char data = 0xFF;
1252   unsigned int r1r0 = (unsigned int)((offset & 0x0300) >> 8);
1253   m6845_personality_t crtc_type;
1254   int page;
1255
1256   crtc_type = input_port_read_safe(space->machine, "crtc", 0);
1257   m6845_set_personality(crtc_type);
1258
1259   if(aleste_mode & 0x04)
1260   {
1261      if ((offset & (1<<15)) == 0)  // Aleste Mapper is readable?
1262      {
1263         page = (offset & 0x0300) >> 8;
1264         data = aleste_active_page[page];
1265         return data;
1266      }
1267   }
1268
1269   /* if b14 = 0 : CRTC Read selected */
1270   if ((offset & (1<<14)) == 0)
1271   {
1272      switch(r1r0) {
1273      case 0x02:
1274         /* CRTC Type 1 : Read Status Register
1275               CRTC Type 3 or 4 : Read from selected internal 6845 register */
1276         switch(crtc_type) {
1277         case M6845_PERSONALITY_UM6845R:
1278            data = amstrad_CRTC_CR; /* Read Status Register */
1279            break;
1280         case M6845_PERSONALITY_AMS40489:
1281         case M6845_PERSONALITY_PREASIC:
1282            data = m6845_register_r(0);
1283            break;
1284         default:
1285            break;
1286         }
1287         break;
1288      case 0x03:
1289         /* All CRTC type : Read from selected internal 6845 register Read only */
1290         data = m6845_register_r(0);
1291         break;
1292      }
1293   }
1294
1295/* if b11 = 0 : 8255 PPI Read selected - bits 9 and 8 then define the PPI function access as shown below:
1296
1297b9 b8 | PPI Function Read/Write status
12980  0  | Port A data  Read/Write
12990  1  | Port B data  Read/Write
13001  0  | Port C data  Read/Write
13011  1  | Control      Write Only
1302*/
1303   if ((offset & (1<<11)) == 0)
1304   {
1305      if (r1r0 < 0x03 )
1306         data = ppi8255_r((device_config*)devtag_get_device(space->machine, PPI8255, "ppi8255"), r1r0);
1307   }
1308/* if b10 = 0 : Expansion Peripherals Read selected
1309
1310bits b7-b5 are used to select an expansion peripheral. Again, this is done by resetting the peripheral's bit:
1311Bit | Device
1312b7  | FDC
1313b6  | Reserved (was it ever used?)
1314b5  | Serial port
1315
1316In the case of the FDC, bits b8 and b0 are used to select the specific mode of operation; all the other bits (b9,b4-b1) are ignored:
1317b8 b0 Function Read/Write state
13180 0 FDD motor control Write Only
13190 1 not used N/A
13201 0 Main Status register of FDC (MSR) Read Only
13211 1 Data register of FDC Read/Write
1322
1323If b10 is reset but none of b7-b5 are reset, user expansion peripherals are selected.
1324The exception is the case where none of b7-b0 are reset (i.e. port &FBFF), which causes the expansion peripherals to reset.
1325 */
1326   if(amstrad_system_type != SYSTEM_GX4000)
1327   {
1328       if ((offset & (1<<10)) == 0)   {
1329         if ((offset & (1<<10)) == 0) {
1330            int b8b0 = ((offset & (1<<8)) >> (8 - 1)) | (offset & (0x01));
1331            switch (b8b0) {
1332              case 0x02: {
1333                    data = nec765_status_r(fdc, 0);
1334              } break;
1335              case 0x03: {
1336                    data = nec765_data_r(fdc, 0);
1337              } break;
1338              default: {
1339              } break;
1340            }
1341         }
1342      }
1343   }
1344   return data;
1345}
1346
1347/* Offset handler for write */
1348static WRITE8_HANDLER ( AmstradCPC_WritePortHandler )
1349{
1350   device_config *fdc = (device_config*)devtag_get_device(space->machine, NEC765A, "nec765");
1351   if ((offset & (1<<15)) == 0)
1352   {
1353      if(aleste_mode & 0x04) // Aleste mode
1354      {
1355         aleste_msx_mapper(space->machine, offset,data);
1356      }
1357      else
1358      {
1359      /* if b15 = 0 and b14 = 1 : Gate-Array Write Selected*/
1360      if ((offset & (1<<14)) != 0)
1361               amstrad_GateArray_write(space->machine, data);
1362      /* if b15 = 0 : RAM Configuration Write Selected*/
1363         AmstradCPC_GA_SetRamConfiguration(space->machine);
1364      }
1365}
1366/*The Gate-Array and CRTC can't be selected simultaneously, which would otherwise cause potential display corruption.*/
1367/* if b14 = 0 : CRTC Write Selected*/
1368  if ((offset & (1<<14)) == 0) {
1369      switch ((offset & 0x0300) >> 8) { // r1r0
1370        case 0x00: {/* Select internal 6845 register Write Only */
1371#ifdef AMSTRAD_VIDEO_EVENT_LIST
1372           EventList_AddItemOffset(space->machine, (EVENT_LIST_CODE_CRTC_INDEX_WRITE<<6), data, TIME_TO_CYCLES(0,video_screen_get_vpos(machine->primary_screen)*video_screen_get_scan_period(machine->primary_screen)));
1373#endif
1374        m6845_address_w(0,data);
1375      if(amstrad_system_type != SYSTEM_CPC)
1376         amstrad_plus_seqcheck(data);
1377//          selected_m6845_address = (data & 0x1F);
1378      } break;
1379        case 0x01: {/* Write to selected internal 6845 register Write Only */
1380#ifdef AMSTRAD_VIDEO_EVENT_LIST
1381            EventList_AddItemOffset(space->machine, (EVENT_LIST_CODE_CRTC_WRITE<<6), data, TIME_TO_CYCLES(0,video_screen_get_vpos(machine->primary_screen)*video_screen_get_scan_period(machine->primary_screen)));
1382#endif
1383//            logerror("m6845 register (%02d : %04x)\n", selected_m6845_address, (data&0x3F));
1384        m6845_register_w(0,data);
1385        } break;
1386        default: {
1387        } break;
1388      }
1389   }
1390/* ROM select before GateArrayWrite ?*/
1391/* b13 = 0 : ROM select Write Selected*/
1392   if ((offset & (1<<13)) == 0)
1393   {
1394      if (previous_amstrad_UpperRom_data != (data & 0xff))
1395      {
1396         AmstradCPC_SetUpperRom(space->machine, data);
1397      }
1398      previous_amstrad_UpperRom_data = (data & 0xff);
1399   }
1400/* b12 = 0 : Printer port Write Selected*/
1401   if(amstrad_system_type != SYSTEM_GX4000)
1402   {
1403      if ((offset & (1<<12)) == 0) {
1404         /* on CPC, write to printer through LS chip */
1405         /* the amstrad is crippled with a 7-bit port :( */
1406         /* bit 7 of the data is the printer /strobe */
1407
1408         /* strobe state changed? */
1409         if (((previous_printer_data_byte^data) & (1<<7)) !=0 ) {
1410            /* check for only one transition */
1411            if ((data & (1<<7)) == 0)  {
1412               /* output data to printer */
1413               printer_output(printer_device(space->machine), data & 0x07f);
1414            }
1415         }
1416         previous_printer_data_byte = data;
1417      }
1418   }
1419/* if b11 = 0 : 8255 PPI Write selected - bits 9 and 8 then define the PPI function access as shown below:
1420b9 b8 | PPI Function Read/Write status
14210  0  | Port A data  Read/Write
14220  1  | Port B data  Read/Write
14231  0  | Port C data  Read/Write
14241  1  | Control      Write Only
1425*/
1426   if ((offset & (1<<11)) == 0) {
1427      unsigned int Index = ((offset & 0x0300) >> 8);
1428      ppi8255_w((device_config*)devtag_get_device(space->machine, PPI8255, "ppi8255"), Index, data);
1429   }
1430/* if b10 = 0 : Expansion Peripherals Write selected */
1431   if ((offset & (1<<10)) == 0) {
1432/* bits b7-b5 are used to select an expansion peripheral. This is done by resetting the peripheral's bit:
1433Bit | Device
1434b7  | FDC
1435b6  | Reserved (was it ever used?)
1436b5  | Serial port
1437
1438In the case of the FDC, bits b8 and b0 are used to select the specific mode of operation;
1439all the other bits (b9,b4-b1) are ignored:
1440b8 b0 Function Read/Write state
14410 0 FDD motor control Write Only
14420 1 not used N/A
14431 0 Main Status register of FDC (MSR) Read Only
14441 1 Data register of FDC Read/Write
1445
1446If b10 is reset but none of b7-b5 are reset, user expansion peripherals are selected.
1447The exception is the case where none of b7-b0 are reset (i.e. port &FBFF), which causes the expansion peripherals to reset.
1448*/
1449   if(amstrad_system_type != SYSTEM_GX4000)
1450   {
1451      if ((offset & (1<<7)) == 0) {
1452         unsigned int b8b0 = ((offset & 0x0100) >> (8 - 1)) | (offset & 0x01);
1453
1454            switch (b8b0) {
1455            case 0x00:
1456         /* FDC Motor Control - Bit 0 defines the state of the FDD motor:
1457          * "1" the FDD motor will be active.
1458          * "0" the FDD motor will be in-active.*/
1459               floppy_drive_set_motor_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 0), (data & 0x01));
1460               floppy_drive_set_motor_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 1), (data & 0x01));
1461               floppy_drive_set_ready_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 0), 1,1);
1462               floppy_drive_set_ready_state(image_from_devtype_and_index(space->machine, IO_FLOPPY, 1), 1,1);
1463           break;
1464
1465        case 0x03: /* Write Data register of FDC */
1466               nec765_data_w(fdc, 0,data);
1467            break;
1468            default:
1469            break;
1470            }
1471         }
1472      }
1473   }
1474
1475/*   Aleste Extend Port:
1476      D0 - VRAM Bank 0/1 (64K)
1477      D1 - MODE 0-Norm ,1-High resolution
1478      D2 - MAPMOD 0-Amstrad ,1-Yamaha mapper
1479      D3 - MAP Page 0/1 (256K)
1480      D4 - 580WI53
1481      D5 - 0-AY8910 ,1-512WI1 */
1482   if(offset == 0xfabf)
1483   {
1484      rectangle rect;
1485      aleste_mode = data;
1486      logerror("EXTEND: Port &FABF write 0x%02x\n",data);
1487      if(data & 0x02)
1488      {
1489         // Aleste high resolution settings
1490         rect.min_x = rect.min_y = 0;
1491         rect.max_x = (ALESTE_SCREEN_WIDTH-32) - 1;
1492         rect.max_y = (AMSTRAD_SCREEN_HEIGHT-40) - 1;
1493         video_screen_configure(space->machine->primary_screen,800,312,&rect,HZ_TO_ATTOSECONDS(AMSTRAD_FPS));
1494      }
1495      else
1496      {
1497         // Normal CPC video settings
1498         rect.min_x = rect.min_y = 0;
1499         rect.max_x = (AMSTRAD_SCREEN_WIDTH-32) - 1;
1500         rect.max_y = (AMSTRAD_SCREEN_HEIGHT-40) - 1;
1501         video_screen_configure(space->machine->primary_screen,800,312,&rect,HZ_TO_ATTOSECONDS(AMSTRAD_FPS));
1502      }
1503   }
1504   multiface_io_write(space, offset,data);
1505}
1506
1507// Handler for checking the ASIC unlocking sequence
1508static void amstrad_plus_seqcheck(int data)
1509{
1510   static int prev_data;
1511
1512   if(data == 0 && prev_data != 0)
1513   {
1514      amstrad_plus_asic_seqptr = 0;  // non-zero value followed by zero will sync the locking mechanism
1515   }
1516   if(data == asic_unlock_seq[amstrad_plus_asic_seqptr])
1517   {
1518      amstrad_plus_asic_seqptr++;
1519      if(amstrad_plus_asic_seqptr == 14 && amstrad_plus_asic_enabled != 0)
1520      {
1521         logerror("SYS: ASIC locked\n");
1522         amstrad_plus_asic_enabled = 0;
1523      }
1524      if(amstrad_plus_asic_seqptr >= 15)  // end of sequence
1525      {
1526         logerror("SYS: ASIC unlocked\n");
1527         amstrad_plus_asic_enabled = 1;
1528      }
1529   }
1530   prev_data = data;
1531}
1532
1533/******************************************************************************************
1534    Multiface emulation
1535  ****************************************************************************************/
1536
1537static unsigned char *multiface_ram;
1538static unsigned long multiface_flags;
1539
1540/* stop button has been pressed */
1541#define MULTIFACE_STOP_BUTTON_PRESSED   0x0001
1542/* ram/rom is paged into memory space */
1543#define MULTIFACE_RAM_ROM_ENABLED      0x0002
1544/* when visible OUT commands are performed! */
1545#define MULTIFACE_VISIBLE            0x0004
1546
1547/* multiface traps calls to 0x0065 when it is active.
1548This address has a RET and so executes no code.
1549
1550It is believed that it is used to make multiface invisible to programs */
1551
1552/*#define MULTIFACE_0065_TOGGLE                   0x0008*/
1553
1554
1555/* used to setup computer if a snapshot was specified */
1556static DIRECT_UPDATE_HANDLER( amstrad_multiface_directoverride )
1557{
1558      int pc;
1559
1560      pc = cpu_get_pc(space->machine->cpu[0]);
1561
1562      /* there are two places where CALL &0065 can be found
1563        in the multiface rom. At this address there is a RET.
1564
1565        To disable the multiface from being detected, the multiface
1566        stop button must be pressed, then the program that was stopped
1567        must be returned to. When this is done, the multiface cannot
1568        be detected and the out operations to page the multiface
1569        ram/rom into the address space will not work! */
1570
1571      /* I assume that the hardware in the multiface detects
1572        the PC set to 0x065 and uses this to enable/disable the multiface
1573        */
1574
1575      /* I also use this to allow the stop button to be pressed again */
1576      if (pc==0x0164)
1577      {
1578         /* first call? */
1579         multiface_flags |= MULTIFACE_VISIBLE;
1580      }
1581      else if (pc==0x0c98)
1582      {
1583        /* second call */
1584
1585        /* no longer visible */
1586        multiface_flags &= ~(MULTIFACE_VISIBLE|MULTIFACE_STOP_BUTTON_PRESSED);
1587
1588       /* clear op base override */
1589            memory_set_direct_update_handler(space,0);
1590      }
1591
1592      return pc;
1593}
1594
1595void multiface_init(void)
1596{
1597   /* after a reset the multiface is visible */
1598   multiface_flags = MULTIFACE_VISIBLE;
1599
1600   /* allocate ram */
1601   multiface_ram = (unsigned char *)auto_malloc(8192);
1602}
1603
1604/* call when a system reset is done */
1605void multiface_reset(void)
1606{
1607      /* stop button not pressed and ram/rom disabled */
1608      multiface_flags &= ~(MULTIFACE_STOP_BUTTON_PRESSED |
1609                  MULTIFACE_RAM_ROM_ENABLED);
1610      /* as on the real hardware the multiface is visible after a reset! */
1611      multiface_flags |= MULTIFACE_VISIBLE;
1612}
1613
1614int multiface_hardware_enabled(running_machine *machine)
1615{
1616      if (multiface_ram!=NULL)
1617      {
1618            if ((input_port_read(machine, "multiface") & 0x01)!=0)
1619            {
1620                  return 1;
1621            }
1622      }
1623
1624      return 0;
1625}
1626
1627/* simulate the stop button has been pressed */
1628void multiface_stop(running_machine *machine)
1629{
1630   /* multiface hardware enabled? */
1631   if (!multiface_hardware_enabled(machine))
1632      return;
1633
1634   /* if stop button not already pressed, do press action */
1635   /* pressing stop button while multiface is running has no effect */
1636   if ((multiface_flags & MULTIFACE_STOP_BUTTON_PRESSED)==0)
1637   {
1638      /* initialise 0065 toggle */
1639            /*multiface_flags &= ~MULTIFACE_0065_TOGGLE;*/
1640
1641      multiface_flags |= MULTIFACE_RAM_ROM_ENABLED;
1642
1643      /* stop button has been pressed, furthur pressess will not issue a NMI */
1644      multiface_flags |= MULTIFACE_STOP_BUTTON_PRESSED;
1645
1646      amstrad_GateArray_ModeAndRomConfiguration &=~0x04;
1647
1648      /* page rom into memory */
1649      multiface_rethink_memory(machine);
1650
1651      /* pulse the nmi line */
1652      cpu_set_input_line(machine->cpu[0], INPUT_LINE_NMI, PULSE_LINE);
1653
1654      /* initialise 0065 override to monitor calls to 0065 */
1655      memory_set_direct_update_handler(cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM),amstrad_multiface_directoverride);
1656   }
1657
1658}
1659
1660static void multiface_rethink_memory(running_machine *machine)
1661{
1662   unsigned char *multiface_rom;
1663
1664   /* multiface hardware enabled? */
1665   if (!multiface_hardware_enabled(machine))
1666      return;
1667
1668   multiface_rom = &memory_region(machine, "main")[0x01C000];
1669
1670   if (
1671      ((multiface_flags & MULTIFACE_RAM_ROM_ENABLED)!=0) &&
1672      ((amstrad_GateArray_ModeAndRomConfiguration & 0x04) == 0)
1673      )
1674   {
1675
1676      /* set bank addressess */
1677      memory_set_bankptr(machine,1, multiface_rom);
1678      memory_set_bankptr(machine,2, multiface_ram);
1679      memory_set_bankptr(machine,9, multiface_rom);
1680      memory_set_bankptr(machine,10, multiface_ram);
1681   }
1682}
1683
1684/* any io writes are passed through here */
1685static WRITE8_HANDLER(multiface_io_write)
1686{
1687   /* multiface hardware enabled? */
1688      if (!multiface_hardware_enabled(space->machine))
1689      return;
1690
1691      /* visible? */
1692   if (multiface_flags & MULTIFACE_VISIBLE)
1693   {
1694      if (offset==0x0fee8)
1695      {
1696         multiface_flags |= MULTIFACE_RAM_ROM_ENABLED;
1697         amstrad_rethinkMemory(space->machine);
1698      }
1699
1700      if (offset==0x0feea)
1701      {
1702         multiface_flags &= ~MULTIFACE_RAM_ROM_ENABLED;
1703         amstrad_rethinkMemory(space->machine);
1704      }
1705   }
1706
1707   /* update multiface ram with data */
1708      /* these are decoded fully! */
1709      switch ((offset>>8) & 0x0ff)
1710      {
1711            /* gate array */
1712            case 0x07f:
1713            {
1714                  switch (data & 0x0c0)
1715                  {
1716                        /* pen index */
1717                        case 0x00:
1718                        {
1719                           multiface_ram[0x01fcf] = data;
1720
1721                        }
1722                        break;
1723
1724                        /* pen colour */
1725                        case 0x040:
1726                        {
1727                           int pen_index;
1728
1729                           pen_index = multiface_ram[0x01fcf] & 0x0f;
1730
1731                           if (multiface_ram[0x01fcf] & 0x010)
1732                           {
1733
1734                              multiface_ram[0x01fdf + pen_index] = data;
1735                           }
1736                           else
1737                           {
1738                              multiface_ram[0x01f90 + pen_index] = data & 0x01f;
1739                           }
1740
1741                        }
1742                        break;
1743
1744                        /* rom/mode selection */
1745                        case 0x080:
1746                        {
1747
1748                           multiface_ram[0x01fef] = data;
1749
1750                        }
1751                        break;
1752
1753                        /* ram configuration */
1754                        case 0x0c0:
1755                        {
1756
1757                           multiface_ram[0x01fff] = data;
1758
1759                        }
1760                        break;
1761
1762                        default:
1763                          break;
1764
1765                  }
1766
1767            }
1768            break;
1769
1770
1771            /* crtc register index */
1772            case 0x0bc:
1773            {
1774                  multiface_ram[0x01cff] = data;
1775            }
1776            break;
1777
1778            /* crtc register write */
1779            case 0x0bd:
1780            {
1781                  int reg_index;
1782
1783                  reg_index = multiface_ram[0x01cff] & 0x0f;
1784
1785                  multiface_ram[0x01db0 + reg_index] = data;
1786            }
1787            break;
1788
1789
1790            /* 8255 ppi control */
1791            case 0x0f7:
1792            {
1793              multiface_ram[0x017ff] = data;
1794
1795            }
1796            break;
1797
1798            /* rom select */
1799            case 0x0df:
1800            {
1801               multiface_ram[0x01aac] = data;
1802            }
1803            break;
1804
1805            default:
1806               break;
1807
1808       }
1809
1810}
1811/* called when cpu acknowledges int */
1812/* reset top bit of interrupt line counter */
1813/* this ensures that the next interrupt is no closer than 32 lines */
1814static IRQ_CALLBACK(amstrad_cpu_acknowledge_int)
1815{
1816   // DMA interrupts can be automatically cleared if bit 0 of &6805 is set to 0
1817   if(amstrad_plus_asic_enabled != 0 && amstrad_plus_irq_cause != 0x06 && amstrad_plus_dma_clear & 0x01)
1818   {
1819      logerror("IRQ: Not cleared, IRQ was called by DMA [%i]\n",amstrad_plus_irq_cause);
1820      amstrad_plus_asic_ram[0x2c0f] &= ~0x80;  // not a raster interrupt, so this bit is reset
1821      return (amstrad_plus_asic_ram[0x2805] & 0xf8) | amstrad_plus_irq_cause;
1822   }
1823   cpu_set_input_line(device->machine->cpu[0],0, CLEAR_LINE);
1824   amstrad_CRTC_HS_Counter &= 0x1F;
1825   if(amstrad_plus_asic_enabled != 0)
1826   {
1827      if(amstrad_plus_irq_cause == 6)  // bit 7 is set "if last interrupt acknowledge cycle was caused by a raster interrupt"
1828         amstrad_plus_asic_ram[0x2c0f] |= 0x80;
1829      else
1830      {
1831         amstrad_plus_asic_ram[0x2c0f] &= ~0x80;
1832         amstrad_plus_asic_ram[0x2c0f] &= (0x40 >> amstrad_plus_irq_cause/2);
1833      }
1834      return (amstrad_plus_asic_ram[0x2805] & 0xf8) | amstrad_plus_irq_cause;
1835   }
1836   return 0xFF;
1837}
1838
1839static VIDEO_EOF( amstrad )
1840{
1841   if (input_port_read_safe(machine, "multiface", 0) & 0x02)
1842   {
1843      multiface_stop(machine);
1844   }
1845}
1846/* sets up for a machine reset
1847All hardware is reset and the firmware is completely initialized
1848Once all tables and jumpblocks have been set up,
1849control is passed to the default entry in rom 0*/
1850void amstrad_reset_machine(running_machine *machine)
1851{
1852   /* enable lower rom (OS rom) */
1853   amstrad_GateArray_write(machine, 0x089);
1854
1855   /* set ram config 0 */
1856   amstrad_GateArray_write(machine, 0x0c0);
1857
1858  // Get manufacturer name and TV refresh rate from PCB link (dipswitch for mess emulation)
1859   ppi_port_inputs[amstrad_ppi_PortB] = (((input_port_read(machine, "solder_links")&MANUFACTURER_NAME)<<1) | (input_port_read(machine, "solder_links")&TV_REFRESH_RATE));
1860
1861   if(amstrad_system_type != SYSTEM_CPC)
1862      memset(amstrad_plus_asic_ram,0,16384);  // clear ASIC RAM
1863
1864   multiface_reset();
1865}
1866
1867static void kccomp_reset_machine(running_machine *machine)
1868{
1869   /* enable lower rom (OS rom) */
1870   amstrad_GateArray_write(machine, 0x089);
1871
1872   /* set ram config 0 */
1873   amstrad_GateArray_write(machine, 0x0c0);
1874}
1875
1876/* the following timings have been measured! */
1877static const UINT8 amstrad_cycle_table_op[256] = {
1878    4, 12,  8,  8,  4,  4,  8,  4,  4, 12,  8,  8,  4,  4,  8,  4,
1879   12, 12,  8,  8,  4,  4,  8,  4, 12, 12,  8,  8,  4,  4,  8,  4,
1880    8, 12, 20,  8,  4,  4,  8,  4,  8, 12, 20,  8,  4,  4,  8,  4,
1881    8, 12, 16,  8, 12, 12, 12,  4,  8, 12, 16,  8,  4,  4,  8,  4,
1882    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1883    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1884    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1885    8,  8,  8,  8,  8,  8,  4,  8,  4,  4,  4,  4,  4,  4,  8,  4,
1886    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1887    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1888    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1889    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1890    8, 12, 12, 12, 12, 16,  8, 16,  8, 12, 12,  4, 12, 20,  8, 16,
1891    8, 12, 12, 12, 12, 16,  8, 16,  8,  4, 12, 12, 12,  4,  8, 16,
1892    8, 12, 12, 24, 12, 16,  8, 16,  8,  4, 12,  4, 12,  4,  8, 16,
1893    8, 12, 12,  4, 12, 16,  8, 16,  8,  8, 12,  4, 12,  4,  8, 16
1894};
1895
1896static const UINT8 amstrad_cycle_table_cb[256]=
1897{
1898    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1899    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1900    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1901    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1902    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1903    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1904    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1905    4,  4,  4,  4,  4,  4,  8,  4,  4,  4,  4,  4,  4,  4,  8,  4,
1906    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1907    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1908    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1909    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1910    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1911    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1912    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4,
1913    4,  4,  4,  4,  4,  4, 12,  4,  4,  4,  4,  4,  4,  4, 12,  4
1914};
1915
1916
1917static const UINT8 amstrad_cycle_table_ed[256]=
1918{
1919    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1920    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1921    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1922    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1923   12, 12, 12, 20,  4, 12,  4,  8, 12, 12, 12, 20,  4, 12,  4,  8,
1924   12, 12, 12, 20,  4, 12,  4,  8, 12, 12, 12, 20,  4, 12,  4,  8,
1925   12, 12, 12, 20,  4, 12,  4, 16, 12, 12, 12, 20,  4, 12,  4, 16,
1926   12, 12, 12, 20,  4, 12,  4,  4, 12, 12, 12, 20,  4, 12,  4,  4,
1927    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1928    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1929   16, 12, 16, 16,  4,  4,  4,  4, 16, 12, 16, 16,  4,  4,  4,  4,
1930   16, 12, 16, 16,  4,  4,  4,  4, 16, 12, 16, 16,  4,  4,  4,  4,
1931    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1932    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1933    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
1934    4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4
1935};
1936
1937
1938static const UINT8 amstrad_cycle_table_xy[256]=
1939{
1940    4, 12,  8,  8,  4,  4,  8,  4,  4, 12,  8,  8,  4,  4,  8,  4,
1941   12, 12,  8,  8,  4,  4,  8,  4, 12, 12,  8,  8,  4,  4,  8,  4,
1942    8, 12, 20,  8,  4,  4,  8,  4,  8, 12, 20,  8,  4,  4,  8,  4,
1943    8, 12, 16,  8, 20, 20, 20,  4,  8, 12, 16,  8,  4,  4,  8,  4,
1944    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1945    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1946    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1947   16, 16, 16, 16, 16, 16,  4, 16,  4,  4,  4,  4,  4,  4, 16,  4,
1948    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1949    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1950    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1951    4,  4,  4,  4,  4,  4, 16,  4,  4,  4,  4,  4,  4,  4, 16,  4,
1952    8, 12, 12, 12, 12, 16,  8, 16,  8, 12, 12,  4, 12, 20,  8, 16,
1953    8, 12, 12, 12, 12, 16,  8, 16,  8,  4, 12, 12, 12,  4,  8, 16,
1954    8, 12, 12, 24, 12, 16,  8, 16,  8,  4, 12,  4, 12,  4,  8, 16,
1955    8, 12, 12,  4, 12, 16,  8, 16,  8,  8, 12,  4, 12,  4,  8, 16
1956};
1957
1958static const UINT8 amstrad_cycle_table_xycb[256]=
1959{
1960   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1961   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1962   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1963   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1964   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1965   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1966   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1967   16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1968   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1969   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1970   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1971   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1972   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1973   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1974   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
1975   20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20
1976};
1977
1978static const UINT8 amstrad_cycle_table_ex[256]=
1979{
1980    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1981    4,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1982    4,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0,
1983    4,  0,  0,  0,  0,  0,  0,  0,  4,  0,  0,  0,  0,  0,  0,  0,
1984    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1985    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1986    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1987    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1988    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1989    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1990    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
1991    4,  8,  4,  4,  0,  0,  0,  0,  4,  8,  4,  4,  0,  0,  0,  0,
1992    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
1993    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
1994    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,
1995    8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0,  8,  0,  0,  0
1996};
1997
1998#ifdef UNUSED_FUNCTION
1999/* every 2us let's the crtc do the job !*/
2000static void amstrad_update_video_1(running_machine *machine, int dummy)
2001{
2002   amstrad_vh_execute_crtc_cycles(machine, 2);
2003}
2004#endif
2005
2006static TIMER_CALLBACK(amstrad_vh_execute_crtc_cycles_callback)
2007{
2008   amstrad_vh_execute_crtc_cycles(machine, param);
2009}
2010
2011static void amstrad_common_init(running_machine *machine)
2012{
2013   const address_space *space = cpu_get_address_space(machine->cpu[0], ADDRESS_SPACE_PROGRAM);
2014   
2015   aleste_mode = 0;
2016
2017   amstrad_GateArray_ModeAndRomConfiguration = 0;
2018   amstrad_GateArray_RamConfiguration = 0;
2019   amstrad_CRTC_HS_Counter = 2;
2020   previous_amstrad_UpperRom_data = 0xff;
2021
2022   memory_install_read8_handler(space, 0x0000, 0x1fff, 0, 0, SMH_BANK1);
2023   memory_install_read8_handler(space, 0x2000, 0x3fff, 0, 0, SMH_BANK2);
2024
2025   memory_install_read8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK3);
2026   memory_install_read8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK4);
2027
2028   memory_install_read8_handler(space, 0x8000, 0x9fff, 0, 0, SMH_BANK5);
2029   memory_install_read8_handler(space, 0xa000, 0xbfff, 0, 0, SMH_BANK6);
2030
2031   memory_install_read8_handler(space, 0xc000, 0xdfff, 0, 0, SMH_BANK7);
2032   memory_install_read8_handler(space, 0xe000, 0xffff, 0, 0, SMH_BANK8);
2033
2034   memory_install_write8_handler(space, 0x0000, 0x1fff, 0, 0, SMH_BANK9);
2035   memory_install_write8_handler(space, 0x2000, 0x3fff, 0, 0, SMH_BANK10);
2036
2037   memory_install_write8_handler(space, 0x4000, 0x5fff, 0, 0, SMH_BANK11);
2038   memory_install_write8_handler(space, 0x6000, 0x7fff, 0, 0, SMH_BANK12);
2039
2040   memory_install_write8_handler(space, 0x8000, 0x9fff, 0, 0, SMH_BANK13);
2041   memory_install_write8_handler(space, 0xa000, 0xbfff, 0, 0, SMH_BANK14);
2042
2043   memory_install_write8_handler(space, 0xc000, 0xdfff, 0, 0, SMH_BANK15);
2044   memory_install_write8_handler(space, 0xe000, 0xffff, 0, 0, SMH_BANK16);
2045
2046   device_reset(space->machine->cpu[0]);
2047   if(amstrad_system_type == SYSTEM_CPC)
2048      cpu_set_input_line_vector(machine->cpu[0], 0,0xff);
2049   else
2050      cpu_set_input_line_vector(machine->cpu[0], 0,0x00);
2051
2052   if(amstrad_system_type != SYSTEM_GX4000)
2053   {
2054      floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 0),  FLOPPY_DRIVE_SS_40);
2055      floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 1),  FLOPPY_DRIVE_SS_40);
2056   }
2057/* Every microsecond:
2058
2059The CRTC generates a memory address using it's MA and RA signal outputs
2060The Gate-Array fetches two bytes for each address*/
2061
2062   timer_pulse(machine, ATTOTIME_IN_USEC(1), NULL, 0, amstrad_vh_execute_crtc_cycles_callback);
2063
2064   /* The opcode timing in the Amstrad is different to the opcode
2065    timing in the core for the Z80 CPU.
2066
2067    The Amstrad hardware issues a HALT for each memory fetch.
2068    This has the effect of stretching the timing for Z80 opcodes,
2069    so that they are all multiple of 4 T states long. All opcode
2070    timings are a multiple of 1us in length. */
2071
2072   /* Using the cool code Juergen has provided, I will override
2073    the timing tables with the values for the amstrad */
2074   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_op, (void*)amstrad_cycle_table_op);
2075   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_cb, (void*)amstrad_cycle_table_cb);
2076   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_ed, (void*)amstrad_cycle_table_ed);
2077   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_xy, (void*)amstrad_cycle_table_xy);
2078   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_xycb, (void*)amstrad_cycle_table_xycb);
2079   device_set_info_ptr(machine->cpu[0],CPUINFO_PTR_Z80_CYCLE_TABLE+Z80_TABLE_ex, (void*)amstrad_cycle_table_ex);
2080
2081   /* Juergen is a cool dude! */
2082   cpu_set_irq_callback(machine->cpu[0], amstrad_cpu_acknowledge_int);
2083}
2084
2085static MACHINE_RESET( amstrad )
2086{
2087   int i;
2088   UINT8 *rom = memory_region(machine, "main");
2089
2090   amstrad_system_type = SYSTEM_CPC;
2091
2092   for (i=0; i<256; i++)
2093   {
2094      Amstrad_ROM_Table[i] = &rom[0x014000];
2095   }
2096
2097   Amstrad_ROM_Table[7] = &rom[0x018000];
2098   amstrad_common_init(machine);
2099   amstrad_reset_machine(machine);
2100   amstrad_init_palette(machine);
2101   
2102   multiface_init();
2103
2104}
2105
2106static MACHINE_RESET( plus )
2107{
2108   int i;
2109   UINT8 *rom = memory_region(machine, "main");
2110
2111   amstrad_system_type = SYSTEM_PLUS;
2112
2113   for (i=0; i<128; i++)  // fill ROM table
2114   {
2115      Amstrad_ROM_Table[i] = &rom[0x4000];  // BASIC in system cart
2116   }
2117   for(i=128;i<160;i++)
2118   {
2119      Amstrad_ROM_Table[i] = &rom[(i-128)*0x4000];
2120   }
2121   Amstrad_ROM_Table[7] = &rom[0xc000];  // AMSDOS in system cart
2122
2123   amstrad_plus_lower = 0;  // cart bank 0
2124   amstrad_plus_lower_addr = 0;  // at 0x0000, reg page disabled by default
2125   amstrad_plus_lower_enabled = 1; // lower ROM enabled by default (as per usual)
2126   amstrad_plus_asic_enabled = 0;  // ASIC disabled
2127   amstrad_plus_asic_regpage = 0;  // ASIC register page disabled
2128   amstrad_plus_asic_seqptr = 0;
2129   amstrad_plus_pri = 0;  // disable PRI
2130   amstrad_plus_scroll_x = 0;
2131   amstrad_plus_scroll_y = 0;
2132   amstrad_plus_scroll_border = 0;  // disable soft scroll
2133   amstrad_plus_dma_status = 0;  // disable all DMA channels
2134   amstrad_plus_dma_0_addr = 0;
2135   amstrad_plus_dma_prescaler[0] = 0;
2136   amstrad_plus_dma_1_addr = 0;
2137   amstrad_plus_dma_prescaler[1] = 0;
2138   amstrad_plus_dma_2_addr = 0;
2139   amstrad_plus_dma_prescaler[2] = 0;
2140   amstrad_plus_dma_clear = 1;  // by default, DMA interrupts must be cleared by writing to the DSCR (&6c0f)
2141   amstrad_plus_irq_cause = 6;
2142
2143   amstrad_common_init(machine);
2144   amstrad_reset_machine(machine);
2145   amstrad_plus_asic_ram[0x2805] = 0x01;  // interrupt vector is undefined at startup, except that bit 0 is always 1.
2146   AmstradCPC_GA_SetRamConfiguration(machine);
2147   amstrad_plus_setsplitline(0,0);
2148   amstrad_GateArray_write(machine, 0x081); // Epyx World of Sports requires upper ROM to be enabled by default
2149   
2150   //  multiface_init();
2151}
2152
2153static MACHINE_START( plus )
2154{
2155   amstrad_plus_asic_ram = memory_region(machine, "user1");  // 16kB RAM for ASIC, memory-mapped registers.
2156}
2157
2158static MACHINE_RESET( gx4000 )
2159{
2160   int i;
2161   UINT8 *rom = memory_region(machine, "main");
2162
2163   amstrad_system_type = SYSTEM_GX4000;
2164
2165   for (i=0; i<128; i++)  // fill ROM table
2166   {
2167      Amstrad_ROM_Table[i] = &rom[0x4000];
2168   }
2169   for(i=128;i<160;i++)
2170   {
2171      Amstrad_ROM_Table[i] = &rom[(i-128)*0x4000];
2172   }
2173   Amstrad_ROM_Table[7] = &rom[0xc000];
2174
2175   amstrad_plus_lower = 0;  // cart bank 0
2176   amstrad_plus_lower_addr = 0;  // at 0x0000, reg page disabled by default
2177   amstrad_plus_lower_enabled = 1; // lower ROM enabled by default (as per usual)
2178   amstrad_plus_asic_enabled = 0;  // ASIC disabled
2179   amstrad_plus_asic_regpage = 0;  // ASIC register page disabled
2180   amstrad_plus_asic_seqptr = 0;
2181   amstrad_plus_pri = 0;  // disable PRI
2182   amstrad_plus_scroll_x = 0;
2183   amstrad_plus_scroll_y = 0;
2184   amstrad_plus_scroll_border = 0;  // disable soft scroll
2185   amstrad_plus_dma_status = 0;  // disable all DMA channels
2186   amstrad_plus_dma_0_addr = 0;
2187   amstrad_plus_dma_prescaler[0] = 0;
2188   amstrad_plus_dma_1_addr = 0;
2189   amstrad_plus_dma_prescaler[1] = 0;
2190   amstrad_plus_dma_2_addr = 0;
2191   amstrad_plus_dma_prescaler[2] = 0;
2192   amstrad_plus_dma_clear = 1;  // by default, DMA interrupts must be cleared by writing to the DSCR (&6c0f)
2193   amstrad_plus_irq_cause = 6;
2194
2195   amstrad_common_init(machine);
2196   amstrad_reset_machine(machine);
2197   amstrad_plus_asic_ram[0x2805] = 0x01;  // interrupt vector is undefined at startup, except that bit 0 is always 1.
2198   AmstradCPC_GA_SetRamConfiguration(machine);
2199   amstrad_plus_setsplitline(0,0);
2200   amstrad_GateArray_write(machine, 0x081); // Epyx World of Sports requires upper ROM to be enabled by default
2201   //  multiface_init();
2202}
2203
2204static MACHINE_RESET( kccomp )
2205{
2206   int i;
2207   UINT8 *rom = memory_region(machine, "main");
2208
2209   amstrad_system_type = SYSTEM_CPC;
2210
2211   for (i=0; i<256; i++)
2212   {
2213      Amstrad_ROM_Table[i] = &rom[0x014000];
2214   }
2215
2216   amstrad_common_init(machine);
2217   kccomp_reset_machine(machine);
2218
2219   /* bit 1 = /TEST. When 0, KC compact will enter data transfer
2220    sequence, where another system using the expansion port signals
2221    DATA2,DATA1, /STROBE and DATA7 can transfer 256 bytes of program.
2222    When the program has been transfered, it will be executed. This
2223    is not supported in the driver */
2224   /* bit 3,4 are tied to +5V, bit 2 is tied to 0V */
2225   ppi_port_inputs[amstrad_ppi_PortB] = (1<<4) | (1<<3) | 2;
2226}
2227
2228148static DRIVER_INIT( aleste )
2229149{
2230150   mc146818_init(machine, MC146818_IGNORE_CENTURY);
2231151}
2232152
2233static MACHINE_RESET( aleste )
2234{
2235   int i;
2236   UINT8 *rom = memory_region(machine, "main");
2237153
2238   amstrad_system_type = SYSTEM_CPC;
2239
2240   for (i=0; i<256; i++)
2241   {
2242      Amstrad_ROM_Table[i] = &rom[0x014000];
2243   }
2244
2245   Amstrad_ROM_Table[3] = &rom[0x01c000];  // MSX-DOS / BIOS
2246   Amstrad_ROM_Table[7] = &rom[0x018000];  // AMSDOS
2247   amstrad_common_init(machine);
2248   amstrad_reset_machine(machine);
2249
2250   floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 0),  FLOPPY_DRIVE_DS_80);
2251   floppy_drive_set_geometry(image_from_devtype_and_index(machine, IO_FLOPPY, 1),  FLOPPY_DRIVE_DS_80);
2252}
2253
2254154/* Memory is banked in 16k blocks. However, the multiface
2255155pages the memory in 8k blocks! The ROM can
2256156be paged into bank 0 and bank 3. */
r4153r4154
2269169are not fully decoded by the CPC h/w. Doing it this way means
2270170I can decode it myself and a lot of  software should work */
2271171static ADDRESS_MAP_START(amstrad_io, ADDRESS_SPACE_IO, 8)
2272   AM_RANGE(0x0000, 0xffff) AM_READWRITE(AmstradCPC_ReadPortHandler, AmstradCPC_WritePortHandler)
172   AM_RANGE(0x0000, 0xffff) AM_READWRITE( amstrad_cpc_io_r, amstrad_cpc_io_w )
2273173ADDRESS_MAP_END
2274174
2275/* Additional notes for the AY-3-8912 in the CPC design
2276Port A of the AY-3-8912 is connected to the keyboard.
2277The data for a selected keyboard line can be read through Port A, as long as it is defined as input.
2278The operating system and I believe all programs assume this port has been defined as input. (NWC has found a bug in the Multiface 2 software. The Multiface does not reprogram the input/output state of the AY-3-8912's registers, therefore if port A is programmed as output, the keyboard will be unresponsive and it will not be possible to use the Multiface functions.)
2279When port B is defined as input (bit 7 of register 7 is set to "0"), a read of this port will return &FF.
2280*/
2281175
2282/* read PSG port A */
2283static READ8_HANDLER ( amstrad_psg_porta_read )
2284{
2285/* Read CPC Keyboard
2286   If keyboard matrix line 11-14 are selected, the byte is always &ff.
2287   After testing on a real CPC, it is found that these never change, they always return &FF. */
2288
2289   static const char *const keynames[] = { "keyboard_row_0", "keyboard_row_1", "keyboard_row_2", "keyboard_row_3", "keyboard_row_4",
2290                              "keyboard_row_5", "keyboard_row_6", "keyboard_row_7", "keyboard_row_8", "keyboard_row_9",
2291                              "keyboard_row_10" };
2292
2293   if (amstrad_keyboard_line > 10)
2294   {
2295      return 0xFF;
2296   }
2297   else
2298   {
2299      int amstrad_read_keyboard_line = amstrad_keyboard_line;
2300      if(aleste_mode == 0x08 && amstrad_keyboard_line == 10)
2301         return 0xff;
2302      amstrad_keyboard_line = 0xFF;
2303
2304      return input_port_read_safe(space->machine, keynames[amstrad_read_keyboard_line], 0) & 0xFF;
2305   }
2306}
2307
2308
2309
2310176/*************************************
2311177 *
2312178 *  Input ports
r4153r4154
2438304
2439305
2440306/* Steph 2000-10-27 I remapped the 'Machine Name' Dip Switches (easier to understand) */
307static INPUT_CHANGED( cpc_monitor_changed )
308{
309   running_machine *machine = field->port->machine;
310   const UINT8   *color_prom = NULL;
2441311
312   if ( (input_port_read(machine, "green_display")) & 0x01 )
313   {
314      PALETTE_INIT_CALL( amstrad_cpc_green );
315   }
316   else
317   {
318      PALETTE_INIT_CALL( amstrad_cpc );
319   }
320}
321
322
2442323static INPUT_PORTS_START( crtc_links )
2443324
2444325/* the following are defined as dipswitches, but are in fact solder links on the
r4153r4154
2503384//  PORT_BIT(0x04, IP_ACTIVE_HIGH, IPT_OTHER) PORT_NAME("Multiface Two's Reset Button") PORT_CODE(KEYCODE_F3)  Not implemented
2504385
2505386   PORT_START("green_display")
2506   PORT_CONFNAME( 0x01, 0x00, "Monitor" )
387   PORT_CONFNAME( 0x01, 0x00, "Monitor" ) PORT_CHANGED( cpc_monitor_changed, 0 )
2507388   PORT_CONFSETTING(0x00, "CTM640 Colour Monitor" )
2508389   PORT_CONFSETTING(0x01, "GT64 Green Monitor" )
2509390
r4153r4154
2938819  This is the reason why the displayed area is not the same as the visible area.
2939820 */
2940821
822
2941823static const cassette_config amstrad_cassette_config =
2942824{
2943825   cdt_cassette_formats,
r4153r4154
2966848
2967849    /* video hardware */
2968850   MDRV_SCREEN_ADD("main", RASTER)
2969   MDRV_SCREEN_REFRESH_RATE(AMSTRAD_FPS)
2970   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(19968))
2971851   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
2972   MDRV_SCREEN_SIZE(800, 312)
2973   /* Amstrad Monitor Visible AREA : 768x272 */
2974   MDRV_SCREEN_VISIBLE_AREA(0, ((AMSTRAD_SCREEN_WIDTH-32) - 1), 0, ((AMSTRAD_SCREEN_HEIGHT-40) - 1))
852   MDRV_SCREEN_RAW_PARAMS( XTAL_16MHz, 1024, 0, 639, 312, 0, 199 )
853   MDRV_VIDEO_ATTRIBUTES(VIDEO_ALWAYS_UPDATE)
854
2975855   MDRV_PALETTE_LENGTH(32)
2976//   MDRV_PALETTE_INIT(amstrad_cpc)
856   MDRV_PALETTE_INIT(amstrad_cpc)
2977857
858   MDRV_MC6845_ADD( "mc6845", MC6845, XTAL_16MHz / 16, mc6845_amstrad_intf )
859
2978860   MDRV_VIDEO_START(amstrad)
2979861   MDRV_VIDEO_UPDATE(amstrad)
2980862   MDRV_VIDEO_EOF(amstrad)
r4153r4154
3003885   MDRV_IMPORT_FROM(amstrad)
3004886   MDRV_MACHINE_RESET(kccomp)
3005887
3006   MDRV_SCREEN_MODIFY("main")
3007   MDRV_SCREEN_REFRESH_RATE(AMSTRAD_FPS)
3008   MDRV_SCREEN_SIZE(800, 312)
3009
3010888   MDRV_PALETTE_INIT(kccomp)
3011889MACHINE_DRIVER_END
3012890
3013891
3014892static MACHINE_DRIVER_START( cpcplus )
3015   MDRV_IMPORT_FROM(amstrad)
3016   MDRV_GFXDECODE(asic_sprite)
3017   MDRV_MACHINE_START(plus)
3018   MDRV_MACHINE_RESET(plus)
893   /* Machine hardware */
894   MDRV_CPU_ADD("main", Z80, XTAL_40MHz / 10)
895   MDRV_CPU_PROGRAM_MAP(amstrad_mem, 0)
896   MDRV_CPU_IO_MAP(amstrad_io, 0)
3019897
3020   MDRV_SCREEN_MODIFY("main")
3021   MDRV_SCREEN_REFRESH_RATE(AMSTRAD_FPS)
3022   MDRV_SCREEN_SIZE(800, 312)
898   MDRV_QUANTUM_TIME(HZ(60))
3023899
3024   MDRV_PALETTE_LENGTH(4096+48)  // extended 12-bit palette, and standard 32 colour palette
900   MDRV_MACHINE_START( plus )
901   MDRV_MACHINE_RESET( plus )
902
903   MDRV_PPI8255_ADD( "ppi8255", amstrad_ppi8255_interface )
904
905   /* video hardware */
906   MDRV_SCREEN_ADD("main", RASTER)
907   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
908   MDRV_SCREEN_RAW_PARAMS( ( XTAL_40MHz * 2 ) / 5, 1024, 0, 639, 312, 0, 199 )
909   MDRV_VIDEO_ATTRIBUTES(VIDEO_ALWAYS_UPDATE)
910
911   MDRV_PALETTE_LENGTH(4096)
3025912   MDRV_PALETTE_INIT(amstrad_plus)
913
914   MDRV_MC6845_ADD( "mc6845", MC6845, XTAL_40MHz / 40, mc6845_amstrad_plus_intf )
915
916   MDRV_VIDEO_START(amstrad)
917   MDRV_VIDEO_UPDATE(amstrad)
918   MDRV_VIDEO_EOF(amstrad)
919
920   /* sound hardware */
921   MDRV_SPEAKER_STANDARD_MONO("mono")
922   MDRV_SOUND_ADD("cassette", WAVE, 0)
923   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50)
924   MDRV_SOUND_ADD("ay", AY8912, XTAL_40MHz / 40)
925   MDRV_SOUND_CONFIG(ay8912_interface)
926   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
927
928   /* printer */
929   MDRV_PRINTER_ADD("printer")
930
931   /* snapshot */
932   MDRV_SNAPSHOT_ADD(amstrad, "sna", 0)
933
934   MDRV_CASSETTE_ADD( "cassette", amstrad_cassette_config )
935   
936   MDRV_NEC765A_ADD("nec765", amstrad_nec765_interface)
3026937   
3027938   MDRV_IMPORT_FROM(cpcplus_cartslot)
3028939MACHINE_DRIVER_END
3029940
3030941
3031942static MACHINE_DRIVER_START( gx4000 )
3032   MDRV_IMPORT_FROM(amstrad)
3033   MDRV_SOUND_REMOVE("cassette")
3034   MDRV_GFXDECODE(asic_sprite)
3035   MDRV_MACHINE_START(plus)
3036   MDRV_MACHINE_RESET(gx4000)
943   /* Machine hardware */
944   MDRV_CPU_ADD("main", Z80, XTAL_40MHz / 10)
945   MDRV_CPU_PROGRAM_MAP(amstrad_mem, 0)
946   MDRV_CPU_IO_MAP(amstrad_io, 0)
3037947
3038   MDRV_SCREEN_MODIFY("main")
3039   MDRV_SCREEN_REFRESH_RATE(AMSTRAD_FPS)
3040   MDRV_SCREEN_SIZE(800, 312)
948   MDRV_QUANTUM_TIME(HZ(60))
3041949
3042   MDRV_PALETTE_LENGTH(4096+48)  // extended 12-bit palette, and standard 32 colour palette
950   MDRV_MACHINE_START( plus )
951   MDRV_MACHINE_RESET( gx4000 )
952
953   MDRV_PPI8255_ADD( "ppi8255", amstrad_ppi8255_interface )
954
955   /* video hardware */
956   MDRV_SCREEN_ADD("main", RASTER)
957   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
958   MDRV_SCREEN_RAW_PARAMS( ( XTAL_40MHz * 2 ) / 5, 1024, 0, 639, 312, 0, 199 )
959   MDRV_VIDEO_ATTRIBUTES(VIDEO_ALWAYS_UPDATE)
960
961   MDRV_PALETTE_LENGTH(4096)
3043962   MDRV_PALETTE_INIT(amstrad_plus)
3044963
3045   /* printer not present in the gx4000 */
3046   MDRV_DEVICE_REMOVE("printer", PRINTER)
964   MDRV_MC6845_ADD( "mc6845", MC6845, XTAL_40MHz / 40, mc6845_amstrad_plus_intf )
3047965
3048   MDRV_CASSETTE_REMOVE( "cassette" )
3049   MDRV_NEC765A_REMOVE( "nec765" )
3050   
966   MDRV_VIDEO_START(amstrad)
967   MDRV_VIDEO_UPDATE(amstrad)
968   MDRV_VIDEO_EOF(amstrad)
969
970   /* sound hardware */
971   MDRV_SPEAKER_STANDARD_MONO("mono")
972   MDRV_SOUND_ADD("ay", AY8912, XTAL_40MHz / 40)
973   MDRV_SOUND_CONFIG(ay8912_interface)
974   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
975
3051976   MDRV_IMPORT_FROM(cpcplus_cartslot)
3052977MACHINE_DRIVER_END
3053978
docs/messnew.txt
r4153r4154
1212----------------------
1313- [COCO] Cleanups  [Nathan Woods]
1414
15- [CPC family] Updated all drivers to use the mc6845 device.  [Wilbert Pol]
16
1517- [TI81, TI85, TI86] Added the new ROM_DEFAULT_BIOS feature to select the most
1618  recent rom versions.  [Hobie Troxell]
1719

Previous 15994 Revisions Next


© 1998-2018 The MESS Team